2 * Copyright 2008-2009 Katholieke Universiteit Leuven
4 * Use of this software is governed by the GNU LGPLv2.1 license
6 * Written by Sven Verdoolaege, K.U.Leuven, Departement
7 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
10 #include <isl_ctx_private.h>
11 #include <isl_map_private.h>
14 #include <isl_mat_private.h>
17 #include "isl_equalities.h"
20 static struct isl_basic_set
*uset_convex_hull_wrap_bounded(struct isl_set
*set
);
22 /* Return 1 if constraint c is redundant with respect to the constraints
23 * in bmap. If c is a lower [upper] bound in some variable and bmap
24 * does not have a lower [upper] bound in that variable, then c cannot
25 * be redundant and we do not need solve any lp.
27 int isl_basic_map_constraint_is_redundant(struct isl_basic_map
**bmap
,
28 isl_int
*c
, isl_int
*opt_n
, isl_int
*opt_d
)
30 enum isl_lp_result res
;
37 total
= isl_basic_map_total_dim(*bmap
);
38 for (i
= 0; i
< total
; ++i
) {
40 if (isl_int_is_zero(c
[1+i
]))
42 sign
= isl_int_sgn(c
[1+i
]);
43 for (j
= 0; j
< (*bmap
)->n_ineq
; ++j
)
44 if (sign
== isl_int_sgn((*bmap
)->ineq
[j
][1+i
]))
46 if (j
== (*bmap
)->n_ineq
)
52 res
= isl_basic_map_solve_lp(*bmap
, 0, c
, (*bmap
)->ctx
->one
,
54 if (res
== isl_lp_unbounded
)
56 if (res
== isl_lp_error
)
58 if (res
== isl_lp_empty
) {
59 *bmap
= isl_basic_map_set_to_empty(*bmap
);
62 return !isl_int_is_neg(*opt_n
);
65 int isl_basic_set_constraint_is_redundant(struct isl_basic_set
**bset
,
66 isl_int
*c
, isl_int
*opt_n
, isl_int
*opt_d
)
68 return isl_basic_map_constraint_is_redundant(
69 (struct isl_basic_map
**)bset
, c
, opt_n
, opt_d
);
73 * constraints. If the minimal value along the normal of a constraint
74 * is the same if the constraint is removed, then the constraint is redundant.
76 * Alternatively, we could have intersected the basic map with the
77 * corresponding equality and the checked if the dimension was that
80 __isl_give isl_basic_map
*isl_basic_map_remove_redundancies(
81 __isl_take isl_basic_map
*bmap
)
88 bmap
= isl_basic_map_gauss(bmap
, NULL
);
89 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
91 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
))
93 if (bmap
->n_ineq
<= 1)
96 tab
= isl_tab_from_basic_map(bmap
);
97 if (isl_tab_detect_implicit_equalities(tab
) < 0)
99 if (isl_tab_detect_redundant(tab
) < 0)
101 bmap
= isl_basic_map_update_from_tab(bmap
, tab
);
103 ISL_F_SET(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
104 ISL_F_SET(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
108 isl_basic_map_free(bmap
);
112 __isl_give isl_basic_set
*isl_basic_set_remove_redundancies(
113 __isl_take isl_basic_set
*bset
)
115 return (struct isl_basic_set
*)
116 isl_basic_map_remove_redundancies((struct isl_basic_map
*)bset
);
119 /* Remove redundant constraints in each of the basic maps.
121 __isl_give isl_map
*isl_map_remove_redundancies(__isl_take isl_map
*map
)
123 return isl_map_inline_foreach_basic_map(map
,
124 &isl_basic_map_remove_redundancies
);
127 __isl_give isl_set
*isl_set_remove_redundancies(__isl_take isl_set
*set
)
129 return isl_map_remove_redundancies(set
);
132 /* Check if the set set is bound in the direction of the affine
133 * constraint c and if so, set the constant term such that the
134 * resulting constraint is a bounding constraint for the set.
136 static int uset_is_bound(struct isl_set
*set
, isl_int
*c
, unsigned len
)
144 isl_int_init(opt_denom
);
146 for (j
= 0; j
< set
->n
; ++j
) {
147 enum isl_lp_result res
;
149 if (ISL_F_ISSET(set
->p
[j
], ISL_BASIC_SET_EMPTY
))
152 res
= isl_basic_set_solve_lp(set
->p
[j
],
153 0, c
, set
->ctx
->one
, &opt
, &opt_denom
, NULL
);
154 if (res
== isl_lp_unbounded
)
156 if (res
== isl_lp_error
)
158 if (res
== isl_lp_empty
) {
159 set
->p
[j
] = isl_basic_set_set_to_empty(set
->p
[j
]);
164 if (first
|| isl_int_is_neg(opt
)) {
165 if (!isl_int_is_one(opt_denom
))
166 isl_seq_scale(c
, c
, opt_denom
, len
);
167 isl_int_sub(c
[0], c
[0], opt
);
172 isl_int_clear(opt_denom
);
176 isl_int_clear(opt_denom
);
180 __isl_give isl_basic_map
*isl_basic_map_set_rational(
181 __isl_take isl_basic_set
*bmap
)
186 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
189 bmap
= isl_basic_map_cow(bmap
);
193 ISL_F_SET(bmap
, ISL_BASIC_MAP_RATIONAL
);
195 return isl_basic_map_finalize(bmap
);
198 __isl_give isl_basic_set
*isl_basic_set_set_rational(
199 __isl_take isl_basic_set
*bset
)
201 return isl_basic_map_set_rational(bset
);
204 static struct isl_set
*isl_set_set_rational(struct isl_set
*set
)
208 set
= isl_set_cow(set
);
211 for (i
= 0; i
< set
->n
; ++i
) {
212 set
->p
[i
] = isl_basic_set_set_rational(set
->p
[i
]);
222 static struct isl_basic_set
*isl_basic_set_add_equality(
223 struct isl_basic_set
*bset
, isl_int
*c
)
231 if (ISL_F_ISSET(bset
, ISL_BASIC_SET_EMPTY
))
234 isl_assert(bset
->ctx
, isl_basic_set_n_param(bset
) == 0, goto error
);
235 isl_assert(bset
->ctx
, bset
->n_div
== 0, goto error
);
236 dim
= isl_basic_set_n_dim(bset
);
237 bset
= isl_basic_set_cow(bset
);
238 bset
= isl_basic_set_extend(bset
, 0, dim
, 0, 1, 0);
239 i
= isl_basic_set_alloc_equality(bset
);
242 isl_seq_cpy(bset
->eq
[i
], c
, 1 + dim
);
245 isl_basic_set_free(bset
);
249 static struct isl_set
*isl_set_add_basic_set_equality(struct isl_set
*set
, isl_int
*c
)
253 set
= isl_set_cow(set
);
256 for (i
= 0; i
< set
->n
; ++i
) {
257 set
->p
[i
] = isl_basic_set_add_equality(set
->p
[i
], c
);
267 /* Given a union of basic sets, construct the constraints for wrapping
268 * a facet around one of its ridges.
269 * In particular, if each of n the d-dimensional basic sets i in "set"
270 * contains the origin, satisfies the constraints x_1 >= 0 and x_2 >= 0
271 * and is defined by the constraints
275 * then the resulting set is of dimension n*(1+d) and has as constraints
284 static struct isl_basic_set
*wrap_constraints(struct isl_set
*set
)
286 struct isl_basic_set
*lp
;
290 unsigned dim
, lp_dim
;
295 dim
= 1 + isl_set_n_dim(set
);
298 for (i
= 0; i
< set
->n
; ++i
) {
299 n_eq
+= set
->p
[i
]->n_eq
;
300 n_ineq
+= set
->p
[i
]->n_ineq
;
302 lp
= isl_basic_set_alloc(set
->ctx
, 0, dim
* set
->n
, 0, n_eq
, n_ineq
);
303 lp
= isl_basic_set_set_rational(lp
);
306 lp_dim
= isl_basic_set_n_dim(lp
);
307 k
= isl_basic_set_alloc_equality(lp
);
308 isl_int_set_si(lp
->eq
[k
][0], -1);
309 for (i
= 0; i
< set
->n
; ++i
) {
310 isl_int_set_si(lp
->eq
[k
][1+dim
*i
], 0);
311 isl_int_set_si(lp
->eq
[k
][1+dim
*i
+1], 1);
312 isl_seq_clr(lp
->eq
[k
]+1+dim
*i
+2, dim
-2);
314 for (i
= 0; i
< set
->n
; ++i
) {
315 k
= isl_basic_set_alloc_inequality(lp
);
316 isl_seq_clr(lp
->ineq
[k
], 1+lp_dim
);
317 isl_int_set_si(lp
->ineq
[k
][1+dim
*i
], 1);
319 for (j
= 0; j
< set
->p
[i
]->n_eq
; ++j
) {
320 k
= isl_basic_set_alloc_equality(lp
);
321 isl_seq_clr(lp
->eq
[k
], 1+dim
*i
);
322 isl_seq_cpy(lp
->eq
[k
]+1+dim
*i
, set
->p
[i
]->eq
[j
], dim
);
323 isl_seq_clr(lp
->eq
[k
]+1+dim
*(i
+1), dim
*(set
->n
-i
-1));
326 for (j
= 0; j
< set
->p
[i
]->n_ineq
; ++j
) {
327 k
= isl_basic_set_alloc_inequality(lp
);
328 isl_seq_clr(lp
->ineq
[k
], 1+dim
*i
);
329 isl_seq_cpy(lp
->ineq
[k
]+1+dim
*i
, set
->p
[i
]->ineq
[j
], dim
);
330 isl_seq_clr(lp
->ineq
[k
]+1+dim
*(i
+1), dim
*(set
->n
-i
-1));
336 /* Given a facet "facet" of the convex hull of "set" and a facet "ridge"
337 * of that facet, compute the other facet of the convex hull that contains
340 * We first transform the set such that the facet constraint becomes
344 * I.e., the facet lies in
348 * and on that facet, the constraint that defines the ridge is
352 * (This transformation is not strictly needed, all that is needed is
353 * that the ridge contains the origin.)
355 * Since the ridge contains the origin, the cone of the convex hull
356 * will be of the form
361 * with this second constraint defining the new facet.
362 * The constant a is obtained by settting x_1 in the cone of the
363 * convex hull to 1 and minimizing x_2.
364 * Now, each element in the cone of the convex hull is the sum
365 * of elements in the cones of the basic sets.
366 * If a_i is the dilation factor of basic set i, then the problem
367 * we need to solve is
380 * the constraints of each (transformed) basic set.
381 * If a = n/d, then the constraint defining the new facet (in the transformed
384 * -n x_1 + d x_2 >= 0
386 * In the original space, we need to take the same combination of the
387 * corresponding constraints "facet" and "ridge".
389 * If a = -infty = "-1/0", then we just return the original facet constraint.
390 * This means that the facet is unbounded, but has a bounded intersection
391 * with the union of sets.
393 isl_int
*isl_set_wrap_facet(__isl_keep isl_set
*set
,
394 isl_int
*facet
, isl_int
*ridge
)
398 struct isl_mat
*T
= NULL
;
399 struct isl_basic_set
*lp
= NULL
;
401 enum isl_lp_result res
;
408 set
= isl_set_copy(set
);
409 set
= isl_set_set_rational(set
);
411 dim
= 1 + isl_set_n_dim(set
);
412 T
= isl_mat_alloc(ctx
, 3, dim
);
415 isl_int_set_si(T
->row
[0][0], 1);
416 isl_seq_clr(T
->row
[0]+1, dim
- 1);
417 isl_seq_cpy(T
->row
[1], facet
, dim
);
418 isl_seq_cpy(T
->row
[2], ridge
, dim
);
419 T
= isl_mat_right_inverse(T
);
420 set
= isl_set_preimage(set
, T
);
424 lp
= wrap_constraints(set
);
425 obj
= isl_vec_alloc(ctx
, 1 + dim
*set
->n
);
428 isl_int_set_si(obj
->block
.data
[0], 0);
429 for (i
= 0; i
< set
->n
; ++i
) {
430 isl_seq_clr(obj
->block
.data
+ 1 + dim
*i
, 2);
431 isl_int_set_si(obj
->block
.data
[1 + dim
*i
+2], 1);
432 isl_seq_clr(obj
->block
.data
+ 1 + dim
*i
+3, dim
-3);
436 res
= isl_basic_set_solve_lp(lp
, 0,
437 obj
->block
.data
, ctx
->one
, &num
, &den
, NULL
);
438 if (res
== isl_lp_ok
) {
439 isl_int_neg(num
, num
);
440 isl_seq_combine(facet
, num
, facet
, den
, ridge
, dim
);
441 isl_seq_normalize(ctx
, facet
, dim
);
446 isl_basic_set_free(lp
);
448 if (res
== isl_lp_error
)
450 isl_assert(ctx
, res
== isl_lp_ok
|| res
== isl_lp_unbounded
,
454 isl_basic_set_free(lp
);
460 /* Compute the constraint of a facet of "set".
462 * We first compute the intersection with a bounding constraint
463 * that is orthogonal to one of the coordinate axes.
464 * If the affine hull of this intersection has only one equality,
465 * we have found a facet.
466 * Otherwise, we wrap the current bounding constraint around
467 * one of the equalities of the face (one that is not equal to
468 * the current bounding constraint).
469 * This process continues until we have found a facet.
470 * The dimension of the intersection increases by at least
471 * one on each iteration, so termination is guaranteed.
473 static __isl_give isl_mat
*initial_facet_constraint(__isl_keep isl_set
*set
)
475 struct isl_set
*slice
= NULL
;
476 struct isl_basic_set
*face
= NULL
;
478 unsigned dim
= isl_set_n_dim(set
);
482 isl_assert(set
->ctx
, set
->n
> 0, goto error
);
483 bounds
= isl_mat_alloc(set
->ctx
, 1, 1 + dim
);
487 isl_seq_clr(bounds
->row
[0], dim
);
488 isl_int_set_si(bounds
->row
[0][1 + dim
- 1], 1);
489 is_bound
= uset_is_bound(set
, bounds
->row
[0], 1 + dim
);
492 isl_assert(set
->ctx
, is_bound
, goto error
);
493 isl_seq_normalize(set
->ctx
, bounds
->row
[0], 1 + dim
);
497 slice
= isl_set_copy(set
);
498 slice
= isl_set_add_basic_set_equality(slice
, bounds
->row
[0]);
499 face
= isl_set_affine_hull(slice
);
502 if (face
->n_eq
== 1) {
503 isl_basic_set_free(face
);
506 for (i
= 0; i
< face
->n_eq
; ++i
)
507 if (!isl_seq_eq(bounds
->row
[0], face
->eq
[i
], 1 + dim
) &&
508 !isl_seq_is_neg(bounds
->row
[0],
509 face
->eq
[i
], 1 + dim
))
511 isl_assert(set
->ctx
, i
< face
->n_eq
, goto error
);
512 if (!isl_set_wrap_facet(set
, bounds
->row
[0], face
->eq
[i
]))
514 isl_seq_normalize(set
->ctx
, bounds
->row
[0], bounds
->n_col
);
515 isl_basic_set_free(face
);
520 isl_basic_set_free(face
);
521 isl_mat_free(bounds
);
525 /* Given the bounding constraint "c" of a facet of the convex hull of "set",
526 * compute a hyperplane description of the facet, i.e., compute the facets
529 * We compute an affine transformation that transforms the constraint
538 * by computing the right inverse U of a matrix that starts with the rows
551 * Since z_1 is zero, we can drop this variable as well as the corresponding
552 * column of U to obtain
560 * with Q' equal to Q, but without the corresponding row.
561 * After computing the facets of the facet in the z' space,
562 * we convert them back to the x space through Q.
564 static struct isl_basic_set
*compute_facet(struct isl_set
*set
, isl_int
*c
)
566 struct isl_mat
*m
, *U
, *Q
;
567 struct isl_basic_set
*facet
= NULL
;
572 set
= isl_set_copy(set
);
573 dim
= isl_set_n_dim(set
);
574 m
= isl_mat_alloc(set
->ctx
, 2, 1 + dim
);
577 isl_int_set_si(m
->row
[0][0], 1);
578 isl_seq_clr(m
->row
[0]+1, dim
);
579 isl_seq_cpy(m
->row
[1], c
, 1+dim
);
580 U
= isl_mat_right_inverse(m
);
581 Q
= isl_mat_right_inverse(isl_mat_copy(U
));
582 U
= isl_mat_drop_cols(U
, 1, 1);
583 Q
= isl_mat_drop_rows(Q
, 1, 1);
584 set
= isl_set_preimage(set
, U
);
585 facet
= uset_convex_hull_wrap_bounded(set
);
586 facet
= isl_basic_set_preimage(facet
, Q
);
588 isl_assert(ctx
, facet
->n_eq
== 0, goto error
);
591 isl_basic_set_free(facet
);
596 /* Given an initial facet constraint, compute the remaining facets.
597 * We do this by running through all facets found so far and computing
598 * the adjacent facets through wrapping, adding those facets that we
599 * hadn't already found before.
601 * For each facet we have found so far, we first compute its facets
602 * in the resulting convex hull. That is, we compute the ridges
603 * of the resulting convex hull contained in the facet.
604 * We also compute the corresponding facet in the current approximation
605 * of the convex hull. There is no need to wrap around the ridges
606 * in this facet since that would result in a facet that is already
607 * present in the current approximation.
609 * This function can still be significantly optimized by checking which of
610 * the facets of the basic sets are also facets of the convex hull and
611 * using all the facets so far to help in constructing the facets of the
614 * using the technique in section "3.1 Ridge Generation" of
615 * "Extended Convex Hull" by Fukuda et al.
617 static struct isl_basic_set
*extend(struct isl_basic_set
*hull
,
622 struct isl_basic_set
*facet
= NULL
;
623 struct isl_basic_set
*hull_facet
= NULL
;
629 isl_assert(set
->ctx
, set
->n
> 0, goto error
);
631 dim
= isl_set_n_dim(set
);
633 for (i
= 0; i
< hull
->n_ineq
; ++i
) {
634 facet
= compute_facet(set
, hull
->ineq
[i
]);
635 facet
= isl_basic_set_add_equality(facet
, hull
->ineq
[i
]);
636 facet
= isl_basic_set_gauss(facet
, NULL
);
637 facet
= isl_basic_set_normalize_constraints(facet
);
638 hull_facet
= isl_basic_set_copy(hull
);
639 hull_facet
= isl_basic_set_add_equality(hull_facet
, hull
->ineq
[i
]);
640 hull_facet
= isl_basic_set_gauss(hull_facet
, NULL
);
641 hull_facet
= isl_basic_set_normalize_constraints(hull_facet
);
642 if (!facet
|| !hull_facet
)
644 hull
= isl_basic_set_cow(hull
);
645 hull
= isl_basic_set_extend_dim(hull
,
646 isl_dim_copy(hull
->dim
), 0, 0, facet
->n_ineq
);
649 for (j
= 0; j
< facet
->n_ineq
; ++j
) {
650 for (f
= 0; f
< hull_facet
->n_ineq
; ++f
)
651 if (isl_seq_eq(facet
->ineq
[j
],
652 hull_facet
->ineq
[f
], 1 + dim
))
654 if (f
< hull_facet
->n_ineq
)
656 k
= isl_basic_set_alloc_inequality(hull
);
659 isl_seq_cpy(hull
->ineq
[k
], hull
->ineq
[i
], 1+dim
);
660 if (!isl_set_wrap_facet(set
, hull
->ineq
[k
], facet
->ineq
[j
]))
663 isl_basic_set_free(hull_facet
);
664 isl_basic_set_free(facet
);
666 hull
= isl_basic_set_simplify(hull
);
667 hull
= isl_basic_set_finalize(hull
);
670 isl_basic_set_free(hull_facet
);
671 isl_basic_set_free(facet
);
672 isl_basic_set_free(hull
);
676 /* Special case for computing the convex hull of a one dimensional set.
677 * We simply collect the lower and upper bounds of each basic set
678 * and the biggest of those.
680 static struct isl_basic_set
*convex_hull_1d(struct isl_set
*set
)
682 struct isl_mat
*c
= NULL
;
683 isl_int
*lower
= NULL
;
684 isl_int
*upper
= NULL
;
687 struct isl_basic_set
*hull
;
689 for (i
= 0; i
< set
->n
; ++i
) {
690 set
->p
[i
] = isl_basic_set_simplify(set
->p
[i
]);
694 set
= isl_set_remove_empty_parts(set
);
697 isl_assert(set
->ctx
, set
->n
> 0, goto error
);
698 c
= isl_mat_alloc(set
->ctx
, 2, 2);
702 if (set
->p
[0]->n_eq
> 0) {
703 isl_assert(set
->ctx
, set
->p
[0]->n_eq
== 1, goto error
);
706 if (isl_int_is_pos(set
->p
[0]->eq
[0][1])) {
707 isl_seq_cpy(lower
, set
->p
[0]->eq
[0], 2);
708 isl_seq_neg(upper
, set
->p
[0]->eq
[0], 2);
710 isl_seq_neg(lower
, set
->p
[0]->eq
[0], 2);
711 isl_seq_cpy(upper
, set
->p
[0]->eq
[0], 2);
714 for (j
= 0; j
< set
->p
[0]->n_ineq
; ++j
) {
715 if (isl_int_is_pos(set
->p
[0]->ineq
[j
][1])) {
717 isl_seq_cpy(lower
, set
->p
[0]->ineq
[j
], 2);
720 isl_seq_cpy(upper
, set
->p
[0]->ineq
[j
], 2);
727 for (i
= 0; i
< set
->n
; ++i
) {
728 struct isl_basic_set
*bset
= set
->p
[i
];
732 for (j
= 0; j
< bset
->n_eq
; ++j
) {
736 isl_int_mul(a
, lower
[0], bset
->eq
[j
][1]);
737 isl_int_mul(b
, lower
[1], bset
->eq
[j
][0]);
738 if (isl_int_lt(a
, b
) && isl_int_is_pos(bset
->eq
[j
][1]))
739 isl_seq_cpy(lower
, bset
->eq
[j
], 2);
740 if (isl_int_gt(a
, b
) && isl_int_is_neg(bset
->eq
[j
][1]))
741 isl_seq_neg(lower
, bset
->eq
[j
], 2);
744 isl_int_mul(a
, upper
[0], bset
->eq
[j
][1]);
745 isl_int_mul(b
, upper
[1], bset
->eq
[j
][0]);
746 if (isl_int_lt(a
, b
) && isl_int_is_pos(bset
->eq
[j
][1]))
747 isl_seq_neg(upper
, bset
->eq
[j
], 2);
748 if (isl_int_gt(a
, b
) && isl_int_is_neg(bset
->eq
[j
][1]))
749 isl_seq_cpy(upper
, bset
->eq
[j
], 2);
752 for (j
= 0; j
< bset
->n_ineq
; ++j
) {
753 if (isl_int_is_pos(bset
->ineq
[j
][1]))
755 if (isl_int_is_neg(bset
->ineq
[j
][1]))
757 if (lower
&& isl_int_is_pos(bset
->ineq
[j
][1])) {
758 isl_int_mul(a
, lower
[0], bset
->ineq
[j
][1]);
759 isl_int_mul(b
, lower
[1], bset
->ineq
[j
][0]);
760 if (isl_int_lt(a
, b
))
761 isl_seq_cpy(lower
, bset
->ineq
[j
], 2);
763 if (upper
&& isl_int_is_neg(bset
->ineq
[j
][1])) {
764 isl_int_mul(a
, upper
[0], bset
->ineq
[j
][1]);
765 isl_int_mul(b
, upper
[1], bset
->ineq
[j
][0]);
766 if (isl_int_gt(a
, b
))
767 isl_seq_cpy(upper
, bset
->ineq
[j
], 2);
778 hull
= isl_basic_set_alloc(set
->ctx
, 0, 1, 0, 0, 2);
779 hull
= isl_basic_set_set_rational(hull
);
783 k
= isl_basic_set_alloc_inequality(hull
);
784 isl_seq_cpy(hull
->ineq
[k
], lower
, 2);
787 k
= isl_basic_set_alloc_inequality(hull
);
788 isl_seq_cpy(hull
->ineq
[k
], upper
, 2);
790 hull
= isl_basic_set_finalize(hull
);
800 static struct isl_basic_set
*convex_hull_0d(struct isl_set
*set
)
802 struct isl_basic_set
*convex_hull
;
807 if (isl_set_is_empty(set
))
808 convex_hull
= isl_basic_set_empty(isl_dim_copy(set
->dim
));
810 convex_hull
= isl_basic_set_universe(isl_dim_copy(set
->dim
));
815 /* Compute the convex hull of a pair of basic sets without any parameters or
816 * integer divisions using Fourier-Motzkin elimination.
817 * The convex hull is the set of all points that can be written as
818 * the sum of points from both basic sets (in homogeneous coordinates).
819 * We set up the constraints in a space with dimensions for each of
820 * the three sets and then project out the dimensions corresponding
821 * to the two original basic sets, retaining only those corresponding
822 * to the convex hull.
824 static struct isl_basic_set
*convex_hull_pair_elim(struct isl_basic_set
*bset1
,
825 struct isl_basic_set
*bset2
)
828 struct isl_basic_set
*bset
[2];
829 struct isl_basic_set
*hull
= NULL
;
832 if (!bset1
|| !bset2
)
835 dim
= isl_basic_set_n_dim(bset1
);
836 hull
= isl_basic_set_alloc(bset1
->ctx
, 0, 2 + 3 * dim
, 0,
837 1 + dim
+ bset1
->n_eq
+ bset2
->n_eq
,
838 2 + bset1
->n_ineq
+ bset2
->n_ineq
);
841 for (i
= 0; i
< 2; ++i
) {
842 for (j
= 0; j
< bset
[i
]->n_eq
; ++j
) {
843 k
= isl_basic_set_alloc_equality(hull
);
846 isl_seq_clr(hull
->eq
[k
], (i
+1) * (1+dim
));
847 isl_seq_clr(hull
->eq
[k
]+(i
+2)*(1+dim
), (1-i
)*(1+dim
));
848 isl_seq_cpy(hull
->eq
[k
]+(i
+1)*(1+dim
), bset
[i
]->eq
[j
],
851 for (j
= 0; j
< bset
[i
]->n_ineq
; ++j
) {
852 k
= isl_basic_set_alloc_inequality(hull
);
855 isl_seq_clr(hull
->ineq
[k
], (i
+1) * (1+dim
));
856 isl_seq_clr(hull
->ineq
[k
]+(i
+2)*(1+dim
), (1-i
)*(1+dim
));
857 isl_seq_cpy(hull
->ineq
[k
]+(i
+1)*(1+dim
),
858 bset
[i
]->ineq
[j
], 1+dim
);
860 k
= isl_basic_set_alloc_inequality(hull
);
863 isl_seq_clr(hull
->ineq
[k
], 1+2+3*dim
);
864 isl_int_set_si(hull
->ineq
[k
][(i
+1)*(1+dim
)], 1);
866 for (j
= 0; j
< 1+dim
; ++j
) {
867 k
= isl_basic_set_alloc_equality(hull
);
870 isl_seq_clr(hull
->eq
[k
], 1+2+3*dim
);
871 isl_int_set_si(hull
->eq
[k
][j
], -1);
872 isl_int_set_si(hull
->eq
[k
][1+dim
+j
], 1);
873 isl_int_set_si(hull
->eq
[k
][2*(1+dim
)+j
], 1);
875 hull
= isl_basic_set_set_rational(hull
);
876 hull
= isl_basic_set_remove_dims(hull
, isl_dim_set
, dim
, 2*(1+dim
));
877 hull
= isl_basic_set_remove_redundancies(hull
);
878 isl_basic_set_free(bset1
);
879 isl_basic_set_free(bset2
);
882 isl_basic_set_free(bset1
);
883 isl_basic_set_free(bset2
);
884 isl_basic_set_free(hull
);
888 /* Is the set bounded for each value of the parameters?
890 int isl_basic_set_is_bounded(__isl_keep isl_basic_set
*bset
)
897 if (isl_basic_set_plain_is_empty(bset
))
900 tab
= isl_tab_from_recession_cone(bset
, 1);
901 bounded
= isl_tab_cone_is_bounded(tab
);
906 /* Is the image bounded for each value of the parameters and
907 * the domain variables?
909 int isl_basic_map_image_is_bounded(__isl_keep isl_basic_map
*bmap
)
911 unsigned nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
912 unsigned n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
915 bmap
= isl_basic_map_copy(bmap
);
916 bmap
= isl_basic_map_cow(bmap
);
917 bmap
= isl_basic_map_move_dims(bmap
, isl_dim_param
, nparam
,
918 isl_dim_in
, 0, n_in
);
919 bounded
= isl_basic_set_is_bounded((isl_basic_set
*)bmap
);
920 isl_basic_map_free(bmap
);
925 /* Is the set bounded for each value of the parameters?
927 int isl_set_is_bounded(__isl_keep isl_set
*set
)
934 for (i
= 0; i
< set
->n
; ++i
) {
935 int bounded
= isl_basic_set_is_bounded(set
->p
[i
]);
936 if (!bounded
|| bounded
< 0)
942 /* Compute the lineality space of the convex hull of bset1 and bset2.
944 * We first compute the intersection of the recession cone of bset1
945 * with the negative of the recession cone of bset2 and then compute
946 * the linear hull of the resulting cone.
948 static struct isl_basic_set
*induced_lineality_space(
949 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
952 struct isl_basic_set
*lin
= NULL
;
955 if (!bset1
|| !bset2
)
958 dim
= isl_basic_set_total_dim(bset1
);
959 lin
= isl_basic_set_alloc_dim(isl_basic_set_get_dim(bset1
), 0,
960 bset1
->n_eq
+ bset2
->n_eq
,
961 bset1
->n_ineq
+ bset2
->n_ineq
);
962 lin
= isl_basic_set_set_rational(lin
);
965 for (i
= 0; i
< bset1
->n_eq
; ++i
) {
966 k
= isl_basic_set_alloc_equality(lin
);
969 isl_int_set_si(lin
->eq
[k
][0], 0);
970 isl_seq_cpy(lin
->eq
[k
] + 1, bset1
->eq
[i
] + 1, dim
);
972 for (i
= 0; i
< bset1
->n_ineq
; ++i
) {
973 k
= isl_basic_set_alloc_inequality(lin
);
976 isl_int_set_si(lin
->ineq
[k
][0], 0);
977 isl_seq_cpy(lin
->ineq
[k
] + 1, bset1
->ineq
[i
] + 1, dim
);
979 for (i
= 0; i
< bset2
->n_eq
; ++i
) {
980 k
= isl_basic_set_alloc_equality(lin
);
983 isl_int_set_si(lin
->eq
[k
][0], 0);
984 isl_seq_neg(lin
->eq
[k
] + 1, bset2
->eq
[i
] + 1, dim
);
986 for (i
= 0; i
< bset2
->n_ineq
; ++i
) {
987 k
= isl_basic_set_alloc_inequality(lin
);
990 isl_int_set_si(lin
->ineq
[k
][0], 0);
991 isl_seq_neg(lin
->ineq
[k
] + 1, bset2
->ineq
[i
] + 1, dim
);
994 isl_basic_set_free(bset1
);
995 isl_basic_set_free(bset2
);
996 return isl_basic_set_affine_hull(lin
);
998 isl_basic_set_free(lin
);
999 isl_basic_set_free(bset1
);
1000 isl_basic_set_free(bset2
);
1004 static struct isl_basic_set
*uset_convex_hull(struct isl_set
*set
);
1006 /* Given a set and a linear space "lin" of dimension n > 0,
1007 * project the linear space from the set, compute the convex hull
1008 * and then map the set back to the original space.
1014 * describe the linear space. We first compute the Hermite normal
1015 * form H = M U of M = H Q, to obtain
1019 * The last n rows of H will be zero, so the last n variables of x' = Q x
1020 * are the one we want to project out. We do this by transforming each
1021 * basic set A x >= b to A U x' >= b and then removing the last n dimensions.
1022 * After computing the convex hull in x'_1, i.e., A' x'_1 >= b',
1023 * we transform the hull back to the original space as A' Q_1 x >= b',
1024 * with Q_1 all but the last n rows of Q.
1026 static struct isl_basic_set
*modulo_lineality(struct isl_set
*set
,
1027 struct isl_basic_set
*lin
)
1029 unsigned total
= isl_basic_set_total_dim(lin
);
1031 struct isl_basic_set
*hull
;
1032 struct isl_mat
*M
, *U
, *Q
;
1036 lin_dim
= total
- lin
->n_eq
;
1037 M
= isl_mat_sub_alloc6(set
->ctx
, lin
->eq
, 0, lin
->n_eq
, 1, total
);
1038 M
= isl_mat_left_hermite(M
, 0, &U
, &Q
);
1042 isl_basic_set_free(lin
);
1044 Q
= isl_mat_drop_rows(Q
, Q
->n_row
- lin_dim
, lin_dim
);
1046 U
= isl_mat_lin_to_aff(U
);
1047 Q
= isl_mat_lin_to_aff(Q
);
1049 set
= isl_set_preimage(set
, U
);
1050 set
= isl_set_remove_dims(set
, isl_dim_set
, total
- lin_dim
, lin_dim
);
1051 hull
= uset_convex_hull(set
);
1052 hull
= isl_basic_set_preimage(hull
, Q
);
1056 isl_basic_set_free(lin
);
1061 /* Given two polyhedra with as constraints h_{ij} x >= 0 in homegeneous space,
1062 * set up an LP for solving
1064 * \sum_j \alpha_{1j} h_{1j} = \sum_j \alpha_{2j} h_{2j}
1066 * \alpha{i0} corresponds to the (implicit) positivity constraint 1 >= 0
1067 * The next \alpha{ij} correspond to the equalities and come in pairs.
1068 * The final \alpha{ij} correspond to the inequalities.
1070 static struct isl_basic_set
*valid_direction_lp(
1071 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
1073 struct isl_dim
*dim
;
1074 struct isl_basic_set
*lp
;
1079 if (!bset1
|| !bset2
)
1081 d
= 1 + isl_basic_set_total_dim(bset1
);
1083 2 * bset1
->n_eq
+ bset1
->n_ineq
+ 2 * bset2
->n_eq
+ bset2
->n_ineq
;
1084 dim
= isl_dim_set_alloc(bset1
->ctx
, 0, n
);
1085 lp
= isl_basic_set_alloc_dim(dim
, 0, d
, n
);
1088 for (i
= 0; i
< n
; ++i
) {
1089 k
= isl_basic_set_alloc_inequality(lp
);
1092 isl_seq_clr(lp
->ineq
[k
] + 1, n
);
1093 isl_int_set_si(lp
->ineq
[k
][0], -1);
1094 isl_int_set_si(lp
->ineq
[k
][1 + i
], 1);
1096 for (i
= 0; i
< d
; ++i
) {
1097 k
= isl_basic_set_alloc_equality(lp
);
1101 isl_int_set_si(lp
->eq
[k
][n
], 0); n
++;
1102 /* positivity constraint 1 >= 0 */
1103 isl_int_set_si(lp
->eq
[k
][n
], i
== 0); n
++;
1104 for (j
= 0; j
< bset1
->n_eq
; ++j
) {
1105 isl_int_set(lp
->eq
[k
][n
], bset1
->eq
[j
][i
]); n
++;
1106 isl_int_neg(lp
->eq
[k
][n
], bset1
->eq
[j
][i
]); n
++;
1108 for (j
= 0; j
< bset1
->n_ineq
; ++j
) {
1109 isl_int_set(lp
->eq
[k
][n
], bset1
->ineq
[j
][i
]); n
++;
1111 /* positivity constraint 1 >= 0 */
1112 isl_int_set_si(lp
->eq
[k
][n
], -(i
== 0)); n
++;
1113 for (j
= 0; j
< bset2
->n_eq
; ++j
) {
1114 isl_int_neg(lp
->eq
[k
][n
], bset2
->eq
[j
][i
]); n
++;
1115 isl_int_set(lp
->eq
[k
][n
], bset2
->eq
[j
][i
]); n
++;
1117 for (j
= 0; j
< bset2
->n_ineq
; ++j
) {
1118 isl_int_neg(lp
->eq
[k
][n
], bset2
->ineq
[j
][i
]); n
++;
1121 lp
= isl_basic_set_gauss(lp
, NULL
);
1122 isl_basic_set_free(bset1
);
1123 isl_basic_set_free(bset2
);
1126 isl_basic_set_free(bset1
);
1127 isl_basic_set_free(bset2
);
1131 /* Compute a vector s in the homogeneous space such that <s, r> > 0
1132 * for all rays in the homogeneous space of the two cones that correspond
1133 * to the input polyhedra bset1 and bset2.
1135 * We compute s as a vector that satisfies
1137 * s = \sum_j \alpha_{ij} h_{ij} for i = 1,2 (*)
1139 * with h_{ij} the normals of the facets of polyhedron i
1140 * (including the "positivity constraint" 1 >= 0) and \alpha_{ij}
1141 * strictly positive numbers. For simplicity we impose \alpha_{ij} >= 1.
1142 * We first set up an LP with as variables the \alpha{ij}.
1143 * In this formulation, for each polyhedron i,
1144 * the first constraint is the positivity constraint, followed by pairs
1145 * of variables for the equalities, followed by variables for the inequalities.
1146 * We then simply pick a feasible solution and compute s using (*).
1148 * Note that we simply pick any valid direction and make no attempt
1149 * to pick a "good" or even the "best" valid direction.
1151 static struct isl_vec
*valid_direction(
1152 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
1154 struct isl_basic_set
*lp
;
1155 struct isl_tab
*tab
;
1156 struct isl_vec
*sample
= NULL
;
1157 struct isl_vec
*dir
;
1162 if (!bset1
|| !bset2
)
1164 lp
= valid_direction_lp(isl_basic_set_copy(bset1
),
1165 isl_basic_set_copy(bset2
));
1166 tab
= isl_tab_from_basic_set(lp
);
1167 sample
= isl_tab_get_sample_value(tab
);
1169 isl_basic_set_free(lp
);
1172 d
= isl_basic_set_total_dim(bset1
);
1173 dir
= isl_vec_alloc(bset1
->ctx
, 1 + d
);
1176 isl_seq_clr(dir
->block
.data
+ 1, dir
->size
- 1);
1178 /* positivity constraint 1 >= 0 */
1179 isl_int_set(dir
->block
.data
[0], sample
->block
.data
[n
]); n
++;
1180 for (i
= 0; i
< bset1
->n_eq
; ++i
) {
1181 isl_int_sub(sample
->block
.data
[n
],
1182 sample
->block
.data
[n
], sample
->block
.data
[n
+1]);
1183 isl_seq_combine(dir
->block
.data
,
1184 bset1
->ctx
->one
, dir
->block
.data
,
1185 sample
->block
.data
[n
], bset1
->eq
[i
], 1 + d
);
1189 for (i
= 0; i
< bset1
->n_ineq
; ++i
)
1190 isl_seq_combine(dir
->block
.data
,
1191 bset1
->ctx
->one
, dir
->block
.data
,
1192 sample
->block
.data
[n
++], bset1
->ineq
[i
], 1 + d
);
1193 isl_vec_free(sample
);
1194 isl_seq_normalize(bset1
->ctx
, dir
->el
, dir
->size
);
1195 isl_basic_set_free(bset1
);
1196 isl_basic_set_free(bset2
);
1199 isl_vec_free(sample
);
1200 isl_basic_set_free(bset1
);
1201 isl_basic_set_free(bset2
);
1205 /* Given a polyhedron b_i + A_i x >= 0 and a map T = S^{-1},
1206 * compute b_i' + A_i' x' >= 0, with
1208 * [ b_i A_i ] [ y' ] [ y' ]
1209 * [ 1 0 ] S^{-1} [ x' ] >= 0 or [ b_i' A_i' ] [ x' ] >= 0
1211 * In particular, add the "positivity constraint" and then perform
1214 static struct isl_basic_set
*homogeneous_map(struct isl_basic_set
*bset
,
1221 bset
= isl_basic_set_extend_constraints(bset
, 0, 1);
1222 k
= isl_basic_set_alloc_inequality(bset
);
1225 isl_seq_clr(bset
->ineq
[k
] + 1, isl_basic_set_total_dim(bset
));
1226 isl_int_set_si(bset
->ineq
[k
][0], 1);
1227 bset
= isl_basic_set_preimage(bset
, T
);
1231 isl_basic_set_free(bset
);
1235 /* Compute the convex hull of a pair of basic sets without any parameters or
1236 * integer divisions, where the convex hull is known to be pointed,
1237 * but the basic sets may be unbounded.
1239 * We turn this problem into the computation of a convex hull of a pair
1240 * _bounded_ polyhedra by "changing the direction of the homogeneous
1241 * dimension". This idea is due to Matthias Koeppe.
1243 * Consider the cones in homogeneous space that correspond to the
1244 * input polyhedra. The rays of these cones are also rays of the
1245 * polyhedra if the coordinate that corresponds to the homogeneous
1246 * dimension is zero. That is, if the inner product of the rays
1247 * with the homogeneous direction is zero.
1248 * The cones in the homogeneous space can also be considered to
1249 * correspond to other pairs of polyhedra by chosing a different
1250 * homogeneous direction. To ensure that both of these polyhedra
1251 * are bounded, we need to make sure that all rays of the cones
1252 * correspond to vertices and not to rays.
1253 * Let s be a direction such that <s, r> > 0 for all rays r of both cones.
1254 * Then using s as a homogeneous direction, we obtain a pair of polytopes.
1255 * The vector s is computed in valid_direction.
1257 * Note that we need to consider _all_ rays of the cones and not just
1258 * the rays that correspond to rays in the polyhedra. If we were to
1259 * only consider those rays and turn them into vertices, then we
1260 * may inadvertently turn some vertices into rays.
1262 * The standard homogeneous direction is the unit vector in the 0th coordinate.
1263 * We therefore transform the two polyhedra such that the selected
1264 * direction is mapped onto this standard direction and then proceed
1265 * with the normal computation.
1266 * Let S be a non-singular square matrix with s as its first row,
1267 * then we want to map the polyhedra to the space
1269 * [ y' ] [ y ] [ y ] [ y' ]
1270 * [ x' ] = S [ x ] i.e., [ x ] = S^{-1} [ x' ]
1272 * We take S to be the unimodular completion of s to limit the growth
1273 * of the coefficients in the following computations.
1275 * Let b_i + A_i x >= 0 be the constraints of polyhedron i.
1276 * We first move to the homogeneous dimension
1278 * b_i y + A_i x >= 0 [ b_i A_i ] [ y ] [ 0 ]
1279 * y >= 0 or [ 1 0 ] [ x ] >= [ 0 ]
1281 * Then we change directoin
1283 * [ b_i A_i ] [ y' ] [ y' ]
1284 * [ 1 0 ] S^{-1} [ x' ] >= 0 or [ b_i' A_i' ] [ x' ] >= 0
1286 * Then we compute the convex hull of the polytopes b_i' + A_i' x' >= 0
1287 * resulting in b' + A' x' >= 0, which we then convert back
1290 * [ b' A' ] S [ x ] >= 0 or [ b A ] [ x ] >= 0
1292 * The polyhedron b + A x >= 0 is then the convex hull of the input polyhedra.
1294 static struct isl_basic_set
*convex_hull_pair_pointed(
1295 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
1297 struct isl_ctx
*ctx
= NULL
;
1298 struct isl_vec
*dir
= NULL
;
1299 struct isl_mat
*T
= NULL
;
1300 struct isl_mat
*T2
= NULL
;
1301 struct isl_basic_set
*hull
;
1302 struct isl_set
*set
;
1304 if (!bset1
|| !bset2
)
1307 dir
= valid_direction(isl_basic_set_copy(bset1
),
1308 isl_basic_set_copy(bset2
));
1311 T
= isl_mat_alloc(bset1
->ctx
, dir
->size
, dir
->size
);
1314 isl_seq_cpy(T
->row
[0], dir
->block
.data
, dir
->size
);
1315 T
= isl_mat_unimodular_complete(T
, 1);
1316 T2
= isl_mat_right_inverse(isl_mat_copy(T
));
1318 bset1
= homogeneous_map(bset1
, isl_mat_copy(T2
));
1319 bset2
= homogeneous_map(bset2
, T2
);
1320 set
= isl_set_alloc_dim(isl_basic_set_get_dim(bset1
), 2, 0);
1321 set
= isl_set_add_basic_set(set
, bset1
);
1322 set
= isl_set_add_basic_set(set
, bset2
);
1323 hull
= uset_convex_hull(set
);
1324 hull
= isl_basic_set_preimage(hull
, T
);
1331 isl_basic_set_free(bset1
);
1332 isl_basic_set_free(bset2
);
1336 static struct isl_basic_set
*uset_convex_hull_wrap(struct isl_set
*set
);
1337 static struct isl_basic_set
*modulo_affine_hull(
1338 struct isl_set
*set
, struct isl_basic_set
*affine_hull
);
1340 /* Compute the convex hull of a pair of basic sets without any parameters or
1341 * integer divisions.
1343 * This function is called from uset_convex_hull_unbounded, which
1344 * means that the complete convex hull is unbounded. Some pairs
1345 * of basic sets may still be bounded, though.
1346 * They may even lie inside a lower dimensional space, in which
1347 * case they need to be handled inside their affine hull since
1348 * the main algorithm assumes that the result is full-dimensional.
1350 * If the convex hull of the two basic sets would have a non-trivial
1351 * lineality space, we first project out this lineality space.
1353 static struct isl_basic_set
*convex_hull_pair(struct isl_basic_set
*bset1
,
1354 struct isl_basic_set
*bset2
)
1356 isl_basic_set
*lin
, *aff
;
1357 int bounded1
, bounded2
;
1359 if (bset1
->ctx
->opt
->convex
== ISL_CONVEX_HULL_FM
)
1360 return convex_hull_pair_elim(bset1
, bset2
);
1362 aff
= isl_set_affine_hull(isl_basic_set_union(isl_basic_set_copy(bset1
),
1363 isl_basic_set_copy(bset2
)));
1367 return modulo_affine_hull(isl_basic_set_union(bset1
, bset2
), aff
);
1368 isl_basic_set_free(aff
);
1370 bounded1
= isl_basic_set_is_bounded(bset1
);
1371 bounded2
= isl_basic_set_is_bounded(bset2
);
1373 if (bounded1
< 0 || bounded2
< 0)
1376 if (bounded1
&& bounded2
)
1377 uset_convex_hull_wrap(isl_basic_set_union(bset1
, bset2
));
1379 if (bounded1
|| bounded2
)
1380 return convex_hull_pair_pointed(bset1
, bset2
);
1382 lin
= induced_lineality_space(isl_basic_set_copy(bset1
),
1383 isl_basic_set_copy(bset2
));
1386 if (isl_basic_set_is_universe(lin
)) {
1387 isl_basic_set_free(bset1
);
1388 isl_basic_set_free(bset2
);
1391 if (lin
->n_eq
< isl_basic_set_total_dim(lin
)) {
1392 struct isl_set
*set
;
1393 set
= isl_set_alloc_dim(isl_basic_set_get_dim(bset1
), 2, 0);
1394 set
= isl_set_add_basic_set(set
, bset1
);
1395 set
= isl_set_add_basic_set(set
, bset2
);
1396 return modulo_lineality(set
, lin
);
1398 isl_basic_set_free(lin
);
1400 return convex_hull_pair_pointed(bset1
, bset2
);
1402 isl_basic_set_free(bset1
);
1403 isl_basic_set_free(bset2
);
1407 /* Compute the lineality space of a basic set.
1408 * We currently do not allow the basic set to have any divs.
1409 * We basically just drop the constants and turn every inequality
1412 struct isl_basic_set
*isl_basic_set_lineality_space(struct isl_basic_set
*bset
)
1415 struct isl_basic_set
*lin
= NULL
;
1420 isl_assert(bset
->ctx
, bset
->n_div
== 0, goto error
);
1421 dim
= isl_basic_set_total_dim(bset
);
1423 lin
= isl_basic_set_alloc_dim(isl_basic_set_get_dim(bset
), 0, dim
, 0);
1426 for (i
= 0; i
< bset
->n_eq
; ++i
) {
1427 k
= isl_basic_set_alloc_equality(lin
);
1430 isl_int_set_si(lin
->eq
[k
][0], 0);
1431 isl_seq_cpy(lin
->eq
[k
] + 1, bset
->eq
[i
] + 1, dim
);
1433 lin
= isl_basic_set_gauss(lin
, NULL
);
1436 for (i
= 0; i
< bset
->n_ineq
&& lin
->n_eq
< dim
; ++i
) {
1437 k
= isl_basic_set_alloc_equality(lin
);
1440 isl_int_set_si(lin
->eq
[k
][0], 0);
1441 isl_seq_cpy(lin
->eq
[k
] + 1, bset
->ineq
[i
] + 1, dim
);
1442 lin
= isl_basic_set_gauss(lin
, NULL
);
1446 isl_basic_set_free(bset
);
1449 isl_basic_set_free(lin
);
1450 isl_basic_set_free(bset
);
1454 /* Compute the (linear) hull of the lineality spaces of the basic sets in the
1455 * "underlying" set "set".
1457 static struct isl_basic_set
*uset_combined_lineality_space(struct isl_set
*set
)
1460 struct isl_set
*lin
= NULL
;
1465 struct isl_dim
*dim
= isl_set_get_dim(set
);
1467 return isl_basic_set_empty(dim
);
1470 lin
= isl_set_alloc_dim(isl_set_get_dim(set
), set
->n
, 0);
1471 for (i
= 0; i
< set
->n
; ++i
)
1472 lin
= isl_set_add_basic_set(lin
,
1473 isl_basic_set_lineality_space(isl_basic_set_copy(set
->p
[i
])));
1475 return isl_set_affine_hull(lin
);
1478 /* Compute the convex hull of a set without any parameters or
1479 * integer divisions.
1480 * In each step, we combined two basic sets until only one
1481 * basic set is left.
1482 * The input basic sets are assumed not to have a non-trivial
1483 * lineality space. If any of the intermediate results has
1484 * a non-trivial lineality space, it is projected out.
1486 static struct isl_basic_set
*uset_convex_hull_unbounded(struct isl_set
*set
)
1488 struct isl_basic_set
*convex_hull
= NULL
;
1490 convex_hull
= isl_set_copy_basic_set(set
);
1491 set
= isl_set_drop_basic_set(set
, convex_hull
);
1494 while (set
->n
> 0) {
1495 struct isl_basic_set
*t
;
1496 t
= isl_set_copy_basic_set(set
);
1499 set
= isl_set_drop_basic_set(set
, t
);
1502 convex_hull
= convex_hull_pair(convex_hull
, t
);
1505 t
= isl_basic_set_lineality_space(isl_basic_set_copy(convex_hull
));
1508 if (isl_basic_set_is_universe(t
)) {
1509 isl_basic_set_free(convex_hull
);
1513 if (t
->n_eq
< isl_basic_set_total_dim(t
)) {
1514 set
= isl_set_add_basic_set(set
, convex_hull
);
1515 return modulo_lineality(set
, t
);
1517 isl_basic_set_free(t
);
1523 isl_basic_set_free(convex_hull
);
1527 /* Compute an initial hull for wrapping containing a single initial
1529 * This function assumes that the given set is bounded.
1531 static struct isl_basic_set
*initial_hull(struct isl_basic_set
*hull
,
1532 struct isl_set
*set
)
1534 struct isl_mat
*bounds
= NULL
;
1540 bounds
= initial_facet_constraint(set
);
1543 k
= isl_basic_set_alloc_inequality(hull
);
1546 dim
= isl_set_n_dim(set
);
1547 isl_assert(set
->ctx
, 1 + dim
== bounds
->n_col
, goto error
);
1548 isl_seq_cpy(hull
->ineq
[k
], bounds
->row
[0], bounds
->n_col
);
1549 isl_mat_free(bounds
);
1553 isl_basic_set_free(hull
);
1554 isl_mat_free(bounds
);
1558 struct max_constraint
{
1564 static int max_constraint_equal(const void *entry
, const void *val
)
1566 struct max_constraint
*a
= (struct max_constraint
*)entry
;
1567 isl_int
*b
= (isl_int
*)val
;
1569 return isl_seq_eq(a
->c
->row
[0] + 1, b
, a
->c
->n_col
- 1);
1572 static void update_constraint(struct isl_ctx
*ctx
, struct isl_hash_table
*table
,
1573 isl_int
*con
, unsigned len
, int n
, int ineq
)
1575 struct isl_hash_table_entry
*entry
;
1576 struct max_constraint
*c
;
1579 c_hash
= isl_seq_get_hash(con
+ 1, len
);
1580 entry
= isl_hash_table_find(ctx
, table
, c_hash
, max_constraint_equal
,
1586 isl_hash_table_remove(ctx
, table
, entry
);
1590 if (isl_int_gt(c
->c
->row
[0][0], con
[0]))
1592 if (isl_int_eq(c
->c
->row
[0][0], con
[0])) {
1597 c
->c
= isl_mat_cow(c
->c
);
1598 isl_int_set(c
->c
->row
[0][0], con
[0]);
1602 /* Check whether the constraint hash table "table" constains the constraint
1605 static int has_constraint(struct isl_ctx
*ctx
, struct isl_hash_table
*table
,
1606 isl_int
*con
, unsigned len
, int n
)
1608 struct isl_hash_table_entry
*entry
;
1609 struct max_constraint
*c
;
1612 c_hash
= isl_seq_get_hash(con
+ 1, len
);
1613 entry
= isl_hash_table_find(ctx
, table
, c_hash
, max_constraint_equal
,
1620 return isl_int_eq(c
->c
->row
[0][0], con
[0]);
1623 /* Check for inequality constraints of a basic set without equalities
1624 * such that the same or more stringent copies of the constraint appear
1625 * in all of the basic sets. Such constraints are necessarily facet
1626 * constraints of the convex hull.
1628 * If the resulting basic set is by chance identical to one of
1629 * the basic sets in "set", then we know that this basic set contains
1630 * all other basic sets and is therefore the convex hull of set.
1631 * In this case we set *is_hull to 1.
1633 static struct isl_basic_set
*common_constraints(struct isl_basic_set
*hull
,
1634 struct isl_set
*set
, int *is_hull
)
1637 int min_constraints
;
1639 struct max_constraint
*constraints
= NULL
;
1640 struct isl_hash_table
*table
= NULL
;
1645 for (i
= 0; i
< set
->n
; ++i
)
1646 if (set
->p
[i
]->n_eq
== 0)
1650 min_constraints
= set
->p
[i
]->n_ineq
;
1652 for (i
= best
+ 1; i
< set
->n
; ++i
) {
1653 if (set
->p
[i
]->n_eq
!= 0)
1655 if (set
->p
[i
]->n_ineq
>= min_constraints
)
1657 min_constraints
= set
->p
[i
]->n_ineq
;
1660 constraints
= isl_calloc_array(hull
->ctx
, struct max_constraint
,
1664 table
= isl_alloc_type(hull
->ctx
, struct isl_hash_table
);
1665 if (isl_hash_table_init(hull
->ctx
, table
, min_constraints
))
1668 total
= isl_dim_total(set
->dim
);
1669 for (i
= 0; i
< set
->p
[best
]->n_ineq
; ++i
) {
1670 constraints
[i
].c
= isl_mat_sub_alloc6(hull
->ctx
,
1671 set
->p
[best
]->ineq
+ i
, 0, 1, 0, 1 + total
);
1672 if (!constraints
[i
].c
)
1674 constraints
[i
].ineq
= 1;
1676 for (i
= 0; i
< min_constraints
; ++i
) {
1677 struct isl_hash_table_entry
*entry
;
1679 c_hash
= isl_seq_get_hash(constraints
[i
].c
->row
[0] + 1, total
);
1680 entry
= isl_hash_table_find(hull
->ctx
, table
, c_hash
,
1681 max_constraint_equal
, constraints
[i
].c
->row
[0] + 1, 1);
1684 isl_assert(hull
->ctx
, !entry
->data
, goto error
);
1685 entry
->data
= &constraints
[i
];
1689 for (s
= 0; s
< set
->n
; ++s
) {
1693 for (i
= 0; i
< set
->p
[s
]->n_eq
; ++i
) {
1694 isl_int
*eq
= set
->p
[s
]->eq
[i
];
1695 for (j
= 0; j
< 2; ++j
) {
1696 isl_seq_neg(eq
, eq
, 1 + total
);
1697 update_constraint(hull
->ctx
, table
,
1701 for (i
= 0; i
< set
->p
[s
]->n_ineq
; ++i
) {
1702 isl_int
*ineq
= set
->p
[s
]->ineq
[i
];
1703 update_constraint(hull
->ctx
, table
, ineq
, total
, n
,
1704 set
->p
[s
]->n_eq
== 0);
1709 for (i
= 0; i
< min_constraints
; ++i
) {
1710 if (constraints
[i
].count
< n
)
1712 if (!constraints
[i
].ineq
)
1714 j
= isl_basic_set_alloc_inequality(hull
);
1717 isl_seq_cpy(hull
->ineq
[j
], constraints
[i
].c
->row
[0], 1 + total
);
1720 for (s
= 0; s
< set
->n
; ++s
) {
1721 if (set
->p
[s
]->n_eq
)
1723 if (set
->p
[s
]->n_ineq
!= hull
->n_ineq
)
1725 for (i
= 0; i
< set
->p
[s
]->n_ineq
; ++i
) {
1726 isl_int
*ineq
= set
->p
[s
]->ineq
[i
];
1727 if (!has_constraint(hull
->ctx
, table
, ineq
, total
, n
))
1730 if (i
== set
->p
[s
]->n_ineq
)
1734 isl_hash_table_clear(table
);
1735 for (i
= 0; i
< min_constraints
; ++i
)
1736 isl_mat_free(constraints
[i
].c
);
1741 isl_hash_table_clear(table
);
1744 for (i
= 0; i
< min_constraints
; ++i
)
1745 isl_mat_free(constraints
[i
].c
);
1750 /* Create a template for the convex hull of "set" and fill it up
1751 * obvious facet constraints, if any. If the result happens to
1752 * be the convex hull of "set" then *is_hull is set to 1.
1754 static struct isl_basic_set
*proto_hull(struct isl_set
*set
, int *is_hull
)
1756 struct isl_basic_set
*hull
;
1761 for (i
= 0; i
< set
->n
; ++i
) {
1762 n_ineq
+= set
->p
[i
]->n_eq
;
1763 n_ineq
+= set
->p
[i
]->n_ineq
;
1765 hull
= isl_basic_set_alloc_dim(isl_dim_copy(set
->dim
), 0, 0, n_ineq
);
1766 hull
= isl_basic_set_set_rational(hull
);
1769 return common_constraints(hull
, set
, is_hull
);
1772 static struct isl_basic_set
*uset_convex_hull_wrap(struct isl_set
*set
)
1774 struct isl_basic_set
*hull
;
1777 hull
= proto_hull(set
, &is_hull
);
1778 if (hull
&& !is_hull
) {
1779 if (hull
->n_ineq
== 0)
1780 hull
= initial_hull(hull
, set
);
1781 hull
= extend(hull
, set
);
1788 /* Compute the convex hull of a set without any parameters or
1789 * integer divisions. Depending on whether the set is bounded,
1790 * we pass control to the wrapping based convex hull or
1791 * the Fourier-Motzkin elimination based convex hull.
1792 * We also handle a few special cases before checking the boundedness.
1794 static struct isl_basic_set
*uset_convex_hull(struct isl_set
*set
)
1796 struct isl_basic_set
*convex_hull
= NULL
;
1797 struct isl_basic_set
*lin
;
1799 if (isl_set_n_dim(set
) == 0)
1800 return convex_hull_0d(set
);
1802 set
= isl_set_coalesce(set
);
1803 set
= isl_set_set_rational(set
);
1810 convex_hull
= isl_basic_set_copy(set
->p
[0]);
1814 if (isl_set_n_dim(set
) == 1)
1815 return convex_hull_1d(set
);
1817 if (isl_set_is_bounded(set
) &&
1818 set
->ctx
->opt
->convex
== ISL_CONVEX_HULL_WRAP
)
1819 return uset_convex_hull_wrap(set
);
1821 lin
= uset_combined_lineality_space(isl_set_copy(set
));
1824 if (isl_basic_set_is_universe(lin
)) {
1828 if (lin
->n_eq
< isl_basic_set_total_dim(lin
))
1829 return modulo_lineality(set
, lin
);
1830 isl_basic_set_free(lin
);
1832 return uset_convex_hull_unbounded(set
);
1835 isl_basic_set_free(convex_hull
);
1839 /* This is the core procedure, where "set" is a "pure" set, i.e.,
1840 * without parameters or divs and where the convex hull of set is
1841 * known to be full-dimensional.
1843 static struct isl_basic_set
*uset_convex_hull_wrap_bounded(struct isl_set
*set
)
1845 struct isl_basic_set
*convex_hull
= NULL
;
1850 if (isl_set_n_dim(set
) == 0) {
1851 convex_hull
= isl_basic_set_universe(isl_dim_copy(set
->dim
));
1853 convex_hull
= isl_basic_set_set_rational(convex_hull
);
1857 set
= isl_set_set_rational(set
);
1858 set
= isl_set_coalesce(set
);
1862 convex_hull
= isl_basic_set_copy(set
->p
[0]);
1866 if (isl_set_n_dim(set
) == 1)
1867 return convex_hull_1d(set
);
1869 return uset_convex_hull_wrap(set
);
1875 /* Compute the convex hull of set "set" with affine hull "affine_hull",
1876 * We first remove the equalities (transforming the set), compute the
1877 * convex hull of the transformed set and then add the equalities back
1878 * (after performing the inverse transformation.
1880 static struct isl_basic_set
*modulo_affine_hull(
1881 struct isl_set
*set
, struct isl_basic_set
*affine_hull
)
1885 struct isl_basic_set
*dummy
;
1886 struct isl_basic_set
*convex_hull
;
1888 dummy
= isl_basic_set_remove_equalities(
1889 isl_basic_set_copy(affine_hull
), &T
, &T2
);
1892 isl_basic_set_free(dummy
);
1893 set
= isl_set_preimage(set
, T
);
1894 convex_hull
= uset_convex_hull(set
);
1895 convex_hull
= isl_basic_set_preimage(convex_hull
, T2
);
1896 convex_hull
= isl_basic_set_intersect(convex_hull
, affine_hull
);
1899 isl_basic_set_free(affine_hull
);
1904 /* Compute the convex hull of a map.
1906 * The implementation was inspired by "Extended Convex Hull" by Fukuda et al.,
1907 * specifically, the wrapping of facets to obtain new facets.
1909 struct isl_basic_map
*isl_map_convex_hull(struct isl_map
*map
)
1911 struct isl_basic_set
*bset
;
1912 struct isl_basic_map
*model
= NULL
;
1913 struct isl_basic_set
*affine_hull
= NULL
;
1914 struct isl_basic_map
*convex_hull
= NULL
;
1915 struct isl_set
*set
= NULL
;
1916 struct isl_ctx
*ctx
;
1923 convex_hull
= isl_basic_map_empty_like_map(map
);
1928 map
= isl_map_detect_equalities(map
);
1929 map
= isl_map_align_divs(map
);
1932 model
= isl_basic_map_copy(map
->p
[0]);
1933 set
= isl_map_underlying_set(map
);
1937 affine_hull
= isl_set_affine_hull(isl_set_copy(set
));
1940 if (affine_hull
->n_eq
!= 0)
1941 bset
= modulo_affine_hull(set
, affine_hull
);
1943 isl_basic_set_free(affine_hull
);
1944 bset
= uset_convex_hull(set
);
1947 convex_hull
= isl_basic_map_overlying_set(bset
, model
);
1951 ISL_F_SET(convex_hull
, ISL_BASIC_MAP_NO_IMPLICIT
);
1952 ISL_F_SET(convex_hull
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1953 ISL_F_CLR(convex_hull
, ISL_BASIC_MAP_RATIONAL
);
1957 isl_basic_map_free(model
);
1961 struct isl_basic_set
*isl_set_convex_hull(struct isl_set
*set
)
1963 return (struct isl_basic_set
*)
1964 isl_map_convex_hull((struct isl_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 (isl_basic_set
*)isl_map_polyhedral_hull((isl_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 * Return 1 if "ineq" is a bound
2104 * 0 if "ineq" may attain arbitrarily small values on basic set "j"
2105 * -1 if some error occurred
2107 static int is_bound(struct sh_data
*data
, struct isl_set
*set
, int j
,
2110 enum isl_lp_result res
;
2113 if (!data
->p
[j
].tab
) {
2114 data
->p
[j
].tab
= isl_tab_from_basic_set(set
->p
[j
]);
2115 if (!data
->p
[j
].tab
)
2121 res
= isl_tab_min(data
->p
[j
].tab
, ineq
, data
->ctx
->one
,
2123 if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
2124 isl_int_sub(ineq
[0], ineq
[0], opt
);
2128 return (res
== isl_lp_ok
|| res
== isl_lp_empty
) ? 1 :
2129 res
== isl_lp_unbounded
? 0 : -1;
2132 /* Check if inequality "ineq" from basic set "i" can be relaxed to
2133 * become a bound on the whole set. If so, add the (relaxed) inequality
2136 * We first check if "hull" already contains a translate of the inequality.
2137 * If so, we are done.
2138 * Then, we check if any of the previous basic sets contains a translate
2139 * of the inequality. If so, then we have already considered this
2140 * inequality and we are done.
2141 * Otherwise, for each basic set other than "i", we check if the inequality
2142 * is a bound on the basic set.
2143 * For previous basic sets, we know that they do not contain a translate
2144 * of the inequality, so we directly call is_bound.
2145 * For following basic sets, we first check if a translate of the
2146 * inequality appears in its description and if so directly update
2147 * the inequality accordingly.
2149 static struct isl_basic_set
*add_bound(struct isl_basic_set
*hull
,
2150 struct sh_data
*data
, struct isl_set
*set
, int i
, isl_int
*ineq
)
2153 struct ineq_cmp_data v
;
2154 struct isl_hash_table_entry
*entry
;
2160 v
.len
= isl_basic_set_total_dim(hull
);
2162 c_hash
= isl_seq_get_hash(ineq
+ 1, v
.len
);
2164 entry
= isl_hash_table_find(hull
->ctx
, data
->hull_table
, c_hash
,
2169 for (j
= 0; j
< i
; ++j
) {
2170 entry
= isl_hash_table_find(hull
->ctx
, data
->p
[j
].table
,
2171 c_hash
, has_ineq
, &v
, 0);
2178 k
= isl_basic_set_alloc_inequality(hull
);
2179 isl_seq_cpy(hull
->ineq
[k
], ineq
, 1 + v
.len
);
2183 for (j
= 0; j
< i
; ++j
) {
2185 bound
= is_bound(data
, set
, j
, hull
->ineq
[k
]);
2192 isl_basic_set_free_inequality(hull
, 1);
2196 for (j
= i
+ 1; j
< set
->n
; ++j
) {
2199 entry
= isl_hash_table_find(hull
->ctx
, data
->p
[j
].table
,
2200 c_hash
, has_ineq
, &v
, 0);
2202 ineq_j
= entry
->data
;
2203 neg
= isl_seq_is_neg(ineq_j
+ 1,
2204 hull
->ineq
[k
] + 1, v
.len
);
2206 isl_int_neg(ineq_j
[0], ineq_j
[0]);
2207 if (isl_int_gt(ineq_j
[0], hull
->ineq
[k
][0]))
2208 isl_int_set(hull
->ineq
[k
][0], ineq_j
[0]);
2210 isl_int_neg(ineq_j
[0], ineq_j
[0]);
2213 bound
= is_bound(data
, set
, j
, hull
->ineq
[k
]);
2220 isl_basic_set_free_inequality(hull
, 1);
2224 entry
= isl_hash_table_find(hull
->ctx
, data
->hull_table
, c_hash
,
2228 entry
->data
= hull
->ineq
[k
];
2232 isl_basic_set_free(hull
);
2236 /* Check if any inequality from basic set "i" can be relaxed to
2237 * become a bound on the whole set. If so, add the (relaxed) inequality
2240 static struct isl_basic_set
*add_bounds(struct isl_basic_set
*bset
,
2241 struct sh_data
*data
, struct isl_set
*set
, int i
)
2244 unsigned dim
= isl_basic_set_total_dim(bset
);
2246 for (j
= 0; j
< set
->p
[i
]->n_eq
; ++j
) {
2247 for (k
= 0; k
< 2; ++k
) {
2248 isl_seq_neg(set
->p
[i
]->eq
[j
], set
->p
[i
]->eq
[j
], 1+dim
);
2249 bset
= add_bound(bset
, data
, set
, i
, set
->p
[i
]->eq
[j
]);
2252 for (j
= 0; j
< set
->p
[i
]->n_ineq
; ++j
)
2253 bset
= add_bound(bset
, data
, set
, i
, set
->p
[i
]->ineq
[j
]);
2257 /* Compute a superset of the convex hull of set that is described
2258 * by only translates of the constraints in the constituents of set.
2260 static struct isl_basic_set
*uset_simple_hull(struct 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_dim(isl_dim_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
);
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.
2302 struct isl_basic_map
*isl_map_simple_hull(struct isl_map
*map
)
2304 struct isl_set
*set
= NULL
;
2305 struct isl_basic_map
*model
= NULL
;
2306 struct isl_basic_map
*hull
;
2307 struct isl_basic_map
*affine_hull
;
2308 struct isl_basic_set
*bset
= NULL
;
2313 hull
= isl_basic_map_empty_like_map(map
);
2318 hull
= isl_basic_map_copy(map
->p
[0]);
2323 map
= isl_map_detect_equalities(map
);
2324 affine_hull
= isl_map_affine_hull(isl_map_copy(map
));
2325 map
= isl_map_align_divs(map
);
2326 model
= isl_basic_map_copy(map
->p
[0]);
2328 set
= isl_map_underlying_set(map
);
2330 bset
= uset_simple_hull(set
);
2332 hull
= isl_basic_map_overlying_set(bset
, model
);
2334 hull
= isl_basic_map_intersect(hull
, affine_hull
);
2335 hull
= isl_basic_map_remove_redundancies(hull
);
2336 ISL_F_SET(hull
, ISL_BASIC_MAP_NO_IMPLICIT
);
2337 ISL_F_SET(hull
, ISL_BASIC_MAP_ALL_EQUALITIES
);
2342 struct isl_basic_set
*isl_set_simple_hull(struct isl_set
*set
)
2344 return (struct isl_basic_set
*)
2345 isl_map_simple_hull((struct isl_map
*)set
);
2348 /* Given a set "set", return parametric bounds on the dimension "dim".
2350 static struct isl_basic_set
*set_bounds(struct isl_set
*set
, int dim
)
2352 unsigned set_dim
= isl_set_dim(set
, isl_dim_set
);
2353 set
= isl_set_copy(set
);
2354 set
= isl_set_eliminate_dims(set
, dim
+ 1, set_dim
- (dim
+ 1));
2355 set
= isl_set_eliminate_dims(set
, 0, dim
);
2356 return isl_set_convex_hull(set
);
2359 /* Computes a "simple hull" and then check if each dimension in the
2360 * resulting hull is bounded by a symbolic constant. If not, the
2361 * hull is intersected with the corresponding bounds on the whole set.
2363 struct isl_basic_set
*isl_set_bounded_simple_hull(struct isl_set
*set
)
2366 struct isl_basic_set
*hull
;
2367 unsigned nparam
, left
;
2368 int removed_divs
= 0;
2370 hull
= isl_set_simple_hull(isl_set_copy(set
));
2374 nparam
= isl_basic_set_dim(hull
, isl_dim_param
);
2375 for (i
= 0; i
< isl_basic_set_dim(hull
, isl_dim_set
); ++i
) {
2376 int lower
= 0, upper
= 0;
2377 struct isl_basic_set
*bounds
;
2379 left
= isl_basic_set_total_dim(hull
) - nparam
- i
- 1;
2380 for (j
= 0; j
< hull
->n_eq
; ++j
) {
2381 if (isl_int_is_zero(hull
->eq
[j
][1 + nparam
+ i
]))
2383 if (isl_seq_first_non_zero(hull
->eq
[j
]+1+nparam
+i
+1,
2390 for (j
= 0; j
< hull
->n_ineq
; ++j
) {
2391 if (isl_int_is_zero(hull
->ineq
[j
][1 + nparam
+ i
]))
2393 if (isl_seq_first_non_zero(hull
->ineq
[j
]+1+nparam
+i
+1,
2395 isl_seq_first_non_zero(hull
->ineq
[j
]+1+nparam
,
2398 if (isl_int_is_pos(hull
->ineq
[j
][1 + nparam
+ i
]))
2409 if (!removed_divs
) {
2410 set
= isl_set_remove_divs(set
);
2415 bounds
= set_bounds(set
, i
);
2416 hull
= isl_basic_set_intersect(hull
, bounds
);