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
12 #include "isl_map_private.h"
16 #include "isl_equalities.h"
19 static struct isl_basic_set
*uset_convex_hull_wrap_bounded(struct isl_set
*set
);
21 static void swap_ineq(struct isl_basic_map
*bmap
, unsigned i
, unsigned j
)
27 bmap
->ineq
[i
] = bmap
->ineq
[j
];
32 /* Return 1 if constraint c is redundant with respect to the constraints
33 * in bmap. If c is a lower [upper] bound in some variable and bmap
34 * does not have a lower [upper] bound in that variable, then c cannot
35 * be redundant and we do not need solve any lp.
37 int isl_basic_map_constraint_is_redundant(struct isl_basic_map
**bmap
,
38 isl_int
*c
, isl_int
*opt_n
, isl_int
*opt_d
)
40 enum isl_lp_result res
;
47 total
= isl_basic_map_total_dim(*bmap
);
48 for (i
= 0; i
< total
; ++i
) {
50 if (isl_int_is_zero(c
[1+i
]))
52 sign
= isl_int_sgn(c
[1+i
]);
53 for (j
= 0; j
< (*bmap
)->n_ineq
; ++j
)
54 if (sign
== isl_int_sgn((*bmap
)->ineq
[j
][1+i
]))
56 if (j
== (*bmap
)->n_ineq
)
62 res
= isl_basic_map_solve_lp(*bmap
, 0, c
, (*bmap
)->ctx
->one
,
64 if (res
== isl_lp_unbounded
)
66 if (res
== isl_lp_error
)
68 if (res
== isl_lp_empty
) {
69 *bmap
= isl_basic_map_set_to_empty(*bmap
);
72 return !isl_int_is_neg(*opt_n
);
75 int isl_basic_set_constraint_is_redundant(struct isl_basic_set
**bset
,
76 isl_int
*c
, isl_int
*opt_n
, isl_int
*opt_d
)
78 return isl_basic_map_constraint_is_redundant(
79 (struct isl_basic_map
**)bset
, c
, opt_n
, opt_d
);
82 /* Compute the convex hull of a basic map, by removing the redundant
83 * constraints. If the minimal value along the normal of a constraint
84 * is the same if the constraint is removed, then the constraint is redundant.
86 * Alternatively, we could have intersected the basic map with the
87 * corresponding equality and the checked if the dimension was that
90 struct isl_basic_map
*isl_basic_map_convex_hull(struct isl_basic_map
*bmap
)
97 bmap
= isl_basic_map_gauss(bmap
, NULL
);
98 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
100 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
))
102 if (bmap
->n_ineq
<= 1)
105 tab
= isl_tab_from_basic_map(bmap
);
106 tab
= isl_tab_detect_implicit_equalities(tab
);
107 if (isl_tab_detect_redundant(tab
) < 0)
109 bmap
= isl_basic_map_update_from_tab(bmap
, tab
);
111 ISL_F_SET(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
112 ISL_F_SET(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
116 isl_basic_map_free(bmap
);
120 struct isl_basic_set
*isl_basic_set_convex_hull(struct isl_basic_set
*bset
)
122 return (struct isl_basic_set
*)
123 isl_basic_map_convex_hull((struct isl_basic_map
*)bset
);
126 /* Check if the set set is bound in the direction of the affine
127 * constraint c and if so, set the constant term such that the
128 * resulting constraint is a bounding constraint for the set.
130 static int uset_is_bound(struct isl_set
*set
, isl_int
*c
, unsigned len
)
138 isl_int_init(opt_denom
);
140 for (j
= 0; j
< set
->n
; ++j
) {
141 enum isl_lp_result res
;
143 if (ISL_F_ISSET(set
->p
[j
], ISL_BASIC_SET_EMPTY
))
146 res
= isl_basic_set_solve_lp(set
->p
[j
],
147 0, c
, set
->ctx
->one
, &opt
, &opt_denom
, NULL
);
148 if (res
== isl_lp_unbounded
)
150 if (res
== isl_lp_error
)
152 if (res
== isl_lp_empty
) {
153 set
->p
[j
] = isl_basic_set_set_to_empty(set
->p
[j
]);
158 if (first
|| isl_int_is_neg(opt
)) {
159 if (!isl_int_is_one(opt_denom
))
160 isl_seq_scale(c
, c
, opt_denom
, len
);
161 isl_int_sub(c
[0], c
[0], opt
);
166 isl_int_clear(opt_denom
);
170 isl_int_clear(opt_denom
);
174 struct isl_basic_set
*isl_basic_set_set_rational(struct isl_basic_set
*bset
)
179 if (ISL_F_ISSET(bset
, ISL_BASIC_MAP_RATIONAL
))
182 bset
= isl_basic_set_cow(bset
);
186 ISL_F_SET(bset
, ISL_BASIC_MAP_RATIONAL
);
188 return isl_basic_set_finalize(bset
);
191 static struct isl_set
*isl_set_set_rational(struct isl_set
*set
)
195 set
= isl_set_cow(set
);
198 for (i
= 0; i
< set
->n
; ++i
) {
199 set
->p
[i
] = isl_basic_set_set_rational(set
->p
[i
]);
209 static struct isl_basic_set
*isl_basic_set_add_equality(
210 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
);
289 lp_dim
= isl_basic_set_n_dim(lp
);
290 k
= isl_basic_set_alloc_equality(lp
);
291 isl_int_set_si(lp
->eq
[k
][0], -1);
292 for (i
= 0; i
< set
->n
; ++i
) {
293 isl_int_set_si(lp
->eq
[k
][1+dim
*i
], 0);
294 isl_int_set_si(lp
->eq
[k
][1+dim
*i
+1], 1);
295 isl_seq_clr(lp
->eq
[k
]+1+dim
*i
+2, dim
-2);
297 for (i
= 0; i
< set
->n
; ++i
) {
298 k
= isl_basic_set_alloc_inequality(lp
);
299 isl_seq_clr(lp
->ineq
[k
], 1+lp_dim
);
300 isl_int_set_si(lp
->ineq
[k
][1+dim
*i
], 1);
302 for (j
= 0; j
< set
->p
[i
]->n_eq
; ++j
) {
303 k
= isl_basic_set_alloc_equality(lp
);
304 isl_seq_clr(lp
->eq
[k
], 1+dim
*i
);
305 isl_seq_cpy(lp
->eq
[k
]+1+dim
*i
, set
->p
[i
]->eq
[j
], dim
);
306 isl_seq_clr(lp
->eq
[k
]+1+dim
*(i
+1), dim
*(set
->n
-i
-1));
309 for (j
= 0; j
< set
->p
[i
]->n_ineq
; ++j
) {
310 k
= isl_basic_set_alloc_inequality(lp
);
311 isl_seq_clr(lp
->ineq
[k
], 1+dim
*i
);
312 isl_seq_cpy(lp
->ineq
[k
]+1+dim
*i
, set
->p
[i
]->ineq
[j
], dim
);
313 isl_seq_clr(lp
->ineq
[k
]+1+dim
*(i
+1), dim
*(set
->n
-i
-1));
319 /* Given a facet "facet" of the convex hull of "set" and a facet "ridge"
320 * of that facet, compute the other facet of the convex hull that contains
323 * We first transform the set such that the facet constraint becomes
327 * I.e., the facet lies in
331 * and on that facet, the constraint that defines the ridge is
335 * (This transformation is not strictly needed, all that is needed is
336 * that the ridge contains the origin.)
338 * Since the ridge contains the origin, the cone of the convex hull
339 * will be of the form
344 * with this second constraint defining the new facet.
345 * The constant a is obtained by settting x_1 in the cone of the
346 * convex hull to 1 and minimizing x_2.
347 * Now, each element in the cone of the convex hull is the sum
348 * of elements in the cones of the basic sets.
349 * If a_i is the dilation factor of basic set i, then the problem
350 * we need to solve is
363 * the constraints of each (transformed) basic set.
364 * If a = n/d, then the constraint defining the new facet (in the transformed
367 * -n x_1 + d x_2 >= 0
369 * In the original space, we need to take the same combination of the
370 * corresponding constraints "facet" and "ridge".
372 * If a = -infty = "-1/0", then we just return the original facet constraint.
373 * This means that the facet is unbounded, but has a bounded intersection
374 * with the union of sets.
376 isl_int
*isl_set_wrap_facet(__isl_keep isl_set
*set
,
377 isl_int
*facet
, isl_int
*ridge
)
380 struct isl_mat
*T
= NULL
;
381 struct isl_basic_set
*lp
= NULL
;
383 enum isl_lp_result res
;
387 set
= isl_set_copy(set
);
388 set
= isl_set_set_rational(set
);
390 dim
= 1 + isl_set_n_dim(set
);
391 T
= isl_mat_alloc(set
->ctx
, 3, dim
);
394 isl_int_set_si(T
->row
[0][0], 1);
395 isl_seq_clr(T
->row
[0]+1, dim
- 1);
396 isl_seq_cpy(T
->row
[1], facet
, dim
);
397 isl_seq_cpy(T
->row
[2], ridge
, dim
);
398 T
= isl_mat_right_inverse(T
);
399 set
= isl_set_preimage(set
, T
);
403 lp
= wrap_constraints(set
);
404 obj
= isl_vec_alloc(set
->ctx
, 1 + dim
*set
->n
);
407 isl_int_set_si(obj
->block
.data
[0], 0);
408 for (i
= 0; i
< set
->n
; ++i
) {
409 isl_seq_clr(obj
->block
.data
+ 1 + dim
*i
, 2);
410 isl_int_set_si(obj
->block
.data
[1 + dim
*i
+2], 1);
411 isl_seq_clr(obj
->block
.data
+ 1 + dim
*i
+3, dim
-3);
415 res
= isl_basic_set_solve_lp(lp
, 0,
416 obj
->block
.data
, set
->ctx
->one
, &num
, &den
, NULL
);
417 if (res
== isl_lp_ok
) {
418 isl_int_neg(num
, num
);
419 isl_seq_combine(facet
, num
, facet
, den
, ridge
, dim
);
424 isl_basic_set_free(lp
);
426 isl_assert(set
->ctx
, res
== isl_lp_ok
|| res
== isl_lp_unbounded
,
430 isl_basic_set_free(lp
);
436 /* Compute the constraint of a facet of "set".
438 * We first compute the intersection with a bounding constraint
439 * that is orthogonal to one of the coordinate axes.
440 * If the affine hull of this intersection has only one equality,
441 * we have found a facet.
442 * Otherwise, we wrap the current bounding constraint around
443 * one of the equalities of the face (one that is not equal to
444 * the current bounding constraint).
445 * This process continues until we have found a facet.
446 * The dimension of the intersection increases by at least
447 * one on each iteration, so termination is guaranteed.
449 static __isl_give isl_mat
*initial_facet_constraint(__isl_keep isl_set
*set
)
451 struct isl_set
*slice
= NULL
;
452 struct isl_basic_set
*face
= NULL
;
454 unsigned dim
= isl_set_n_dim(set
);
458 isl_assert(set
->ctx
, set
->n
> 0, goto error
);
459 bounds
= isl_mat_alloc(set
->ctx
, 1, 1 + dim
);
463 isl_seq_clr(bounds
->row
[0], dim
);
464 isl_int_set_si(bounds
->row
[0][1 + dim
- 1], 1);
465 is_bound
= uset_is_bound(set
, bounds
->row
[0], 1 + dim
);
466 isl_assert(set
->ctx
, is_bound
== 1, goto error
);
467 isl_seq_normalize(set
->ctx
, bounds
->row
[0], 1 + dim
);
471 slice
= isl_set_copy(set
);
472 slice
= isl_set_add_basic_set_equality(slice
, bounds
->row
[0]);
473 face
= isl_set_affine_hull(slice
);
476 if (face
->n_eq
== 1) {
477 isl_basic_set_free(face
);
480 for (i
= 0; i
< face
->n_eq
; ++i
)
481 if (!isl_seq_eq(bounds
->row
[0], face
->eq
[i
], 1 + dim
) &&
482 !isl_seq_is_neg(bounds
->row
[0],
483 face
->eq
[i
], 1 + dim
))
485 isl_assert(set
->ctx
, i
< face
->n_eq
, goto error
);
486 if (!isl_set_wrap_facet(set
, bounds
->row
[0], face
->eq
[i
]))
488 isl_seq_normalize(set
->ctx
, bounds
->row
[0], bounds
->n_col
);
489 isl_basic_set_free(face
);
494 isl_basic_set_free(face
);
495 isl_mat_free(bounds
);
499 /* Given the bounding constraint "c" of a facet of the convex hull of "set",
500 * compute a hyperplane description of the facet, i.e., compute the facets
503 * We compute an affine transformation that transforms the constraint
512 * by computing the right inverse U of a matrix that starts with the rows
525 * Since z_1 is zero, we can drop this variable as well as the corresponding
526 * column of U to obtain
534 * with Q' equal to Q, but without the corresponding row.
535 * After computing the facets of the facet in the z' space,
536 * we convert them back to the x space through Q.
538 static struct isl_basic_set
*compute_facet(struct isl_set
*set
, isl_int
*c
)
540 struct isl_mat
*m
, *U
, *Q
;
541 struct isl_basic_set
*facet
= NULL
;
546 set
= isl_set_copy(set
);
547 dim
= isl_set_n_dim(set
);
548 m
= isl_mat_alloc(set
->ctx
, 2, 1 + dim
);
551 isl_int_set_si(m
->row
[0][0], 1);
552 isl_seq_clr(m
->row
[0]+1, dim
);
553 isl_seq_cpy(m
->row
[1], c
, 1+dim
);
554 U
= isl_mat_right_inverse(m
);
555 Q
= isl_mat_right_inverse(isl_mat_copy(U
));
556 U
= isl_mat_drop_cols(U
, 1, 1);
557 Q
= isl_mat_drop_rows(Q
, 1, 1);
558 set
= isl_set_preimage(set
, U
);
559 facet
= uset_convex_hull_wrap_bounded(set
);
560 facet
= isl_basic_set_preimage(facet
, Q
);
561 isl_assert(ctx
, facet
->n_eq
== 0, goto error
);
564 isl_basic_set_free(facet
);
569 /* Given an initial facet constraint, compute the remaining facets.
570 * We do this by running through all facets found so far and computing
571 * the adjacent facets through wrapping, adding those facets that we
572 * hadn't already found before.
574 * For each facet we have found so far, we first compute its facets
575 * in the resulting convex hull. That is, we compute the ridges
576 * of the resulting convex hull contained in the facet.
577 * We also compute the corresponding facet in the current approximation
578 * of the convex hull. There is no need to wrap around the ridges
579 * in this facet since that would result in a facet that is already
580 * present in the current approximation.
582 * This function can still be significantly optimized by checking which of
583 * the facets of the basic sets are also facets of the convex hull and
584 * using all the facets so far to help in constructing the facets of the
587 * using the technique in section "3.1 Ridge Generation" of
588 * "Extended Convex Hull" by Fukuda et al.
590 static struct isl_basic_set
*extend(struct isl_basic_set
*hull
,
595 struct isl_basic_set
*facet
= NULL
;
596 struct isl_basic_set
*hull_facet
= NULL
;
602 isl_assert(set
->ctx
, set
->n
> 0, goto error
);
604 dim
= isl_set_n_dim(set
);
606 for (i
= 0; i
< hull
->n_ineq
; ++i
) {
607 facet
= compute_facet(set
, hull
->ineq
[i
]);
608 facet
= isl_basic_set_add_equality(facet
, hull
->ineq
[i
]);
609 facet
= isl_basic_set_gauss(facet
, NULL
);
610 facet
= isl_basic_set_normalize_constraints(facet
);
611 hull_facet
= isl_basic_set_copy(hull
);
612 hull_facet
= isl_basic_set_add_equality(hull_facet
, hull
->ineq
[i
]);
613 hull_facet
= isl_basic_set_gauss(hull_facet
, NULL
);
614 hull_facet
= isl_basic_set_normalize_constraints(hull_facet
);
617 hull
= isl_basic_set_cow(hull
);
618 hull
= isl_basic_set_extend_dim(hull
,
619 isl_dim_copy(hull
->dim
), 0, 0, facet
->n_ineq
);
620 for (j
= 0; j
< facet
->n_ineq
; ++j
) {
621 for (f
= 0; f
< hull_facet
->n_ineq
; ++f
)
622 if (isl_seq_eq(facet
->ineq
[j
],
623 hull_facet
->ineq
[f
], 1 + dim
))
625 if (f
< hull_facet
->n_ineq
)
627 k
= isl_basic_set_alloc_inequality(hull
);
630 isl_seq_cpy(hull
->ineq
[k
], hull
->ineq
[i
], 1+dim
);
631 if (!isl_set_wrap_facet(set
, hull
->ineq
[k
], facet
->ineq
[j
]))
634 isl_basic_set_free(hull_facet
);
635 isl_basic_set_free(facet
);
637 hull
= isl_basic_set_simplify(hull
);
638 hull
= isl_basic_set_finalize(hull
);
641 isl_basic_set_free(hull_facet
);
642 isl_basic_set_free(facet
);
643 isl_basic_set_free(hull
);
647 /* Special case for computing the convex hull of a one dimensional set.
648 * We simply collect the lower and upper bounds of each basic set
649 * and the biggest of those.
651 static struct isl_basic_set
*convex_hull_1d(struct isl_set
*set
)
653 struct isl_mat
*c
= NULL
;
654 isl_int
*lower
= NULL
;
655 isl_int
*upper
= NULL
;
658 struct isl_basic_set
*hull
;
660 for (i
= 0; i
< set
->n
; ++i
) {
661 set
->p
[i
] = isl_basic_set_simplify(set
->p
[i
]);
665 set
= isl_set_remove_empty_parts(set
);
668 isl_assert(set
->ctx
, set
->n
> 0, goto error
);
669 c
= isl_mat_alloc(set
->ctx
, 2, 2);
673 if (set
->p
[0]->n_eq
> 0) {
674 isl_assert(set
->ctx
, set
->p
[0]->n_eq
== 1, goto error
);
677 if (isl_int_is_pos(set
->p
[0]->eq
[0][1])) {
678 isl_seq_cpy(lower
, set
->p
[0]->eq
[0], 2);
679 isl_seq_neg(upper
, set
->p
[0]->eq
[0], 2);
681 isl_seq_neg(lower
, set
->p
[0]->eq
[0], 2);
682 isl_seq_cpy(upper
, set
->p
[0]->eq
[0], 2);
685 for (j
= 0; j
< set
->p
[0]->n_ineq
; ++j
) {
686 if (isl_int_is_pos(set
->p
[0]->ineq
[j
][1])) {
688 isl_seq_cpy(lower
, set
->p
[0]->ineq
[j
], 2);
691 isl_seq_cpy(upper
, set
->p
[0]->ineq
[j
], 2);
698 for (i
= 0; i
< set
->n
; ++i
) {
699 struct isl_basic_set
*bset
= set
->p
[i
];
703 for (j
= 0; j
< bset
->n_eq
; ++j
) {
707 isl_int_mul(a
, lower
[0], bset
->eq
[j
][1]);
708 isl_int_mul(b
, lower
[1], bset
->eq
[j
][0]);
709 if (isl_int_lt(a
, b
) && isl_int_is_pos(bset
->eq
[j
][1]))
710 isl_seq_cpy(lower
, bset
->eq
[j
], 2);
711 if (isl_int_gt(a
, b
) && isl_int_is_neg(bset
->eq
[j
][1]))
712 isl_seq_neg(lower
, bset
->eq
[j
], 2);
715 isl_int_mul(a
, upper
[0], bset
->eq
[j
][1]);
716 isl_int_mul(b
, upper
[1], bset
->eq
[j
][0]);
717 if (isl_int_lt(a
, b
) && isl_int_is_pos(bset
->eq
[j
][1]))
718 isl_seq_neg(upper
, bset
->eq
[j
], 2);
719 if (isl_int_gt(a
, b
) && isl_int_is_neg(bset
->eq
[j
][1]))
720 isl_seq_cpy(upper
, bset
->eq
[j
], 2);
723 for (j
= 0; j
< bset
->n_ineq
; ++j
) {
724 if (isl_int_is_pos(bset
->ineq
[j
][1]))
726 if (isl_int_is_neg(bset
->ineq
[j
][1]))
728 if (lower
&& isl_int_is_pos(bset
->ineq
[j
][1])) {
729 isl_int_mul(a
, lower
[0], bset
->ineq
[j
][1]);
730 isl_int_mul(b
, lower
[1], bset
->ineq
[j
][0]);
731 if (isl_int_lt(a
, b
))
732 isl_seq_cpy(lower
, bset
->ineq
[j
], 2);
734 if (upper
&& isl_int_is_neg(bset
->ineq
[j
][1])) {
735 isl_int_mul(a
, upper
[0], bset
->ineq
[j
][1]);
736 isl_int_mul(b
, upper
[1], bset
->ineq
[j
][0]);
737 if (isl_int_gt(a
, b
))
738 isl_seq_cpy(upper
, bset
->ineq
[j
], 2);
749 hull
= isl_basic_set_alloc(set
->ctx
, 0, 1, 0, 0, 2);
750 hull
= isl_basic_set_set_rational(hull
);
754 k
= isl_basic_set_alloc_inequality(hull
);
755 isl_seq_cpy(hull
->ineq
[k
], lower
, 2);
758 k
= isl_basic_set_alloc_inequality(hull
);
759 isl_seq_cpy(hull
->ineq
[k
], upper
, 2);
761 hull
= isl_basic_set_finalize(hull
);
771 /* Project out final n dimensions using Fourier-Motzkin */
772 static struct isl_set
*set_project_out(struct isl_ctx
*ctx
,
773 struct isl_set
*set
, unsigned n
)
775 return isl_set_remove_dims(set
, isl_set_n_dim(set
) - n
, n
);
778 static struct isl_basic_set
*convex_hull_0d(struct isl_set
*set
)
780 struct isl_basic_set
*convex_hull
;
785 if (isl_set_is_empty(set
))
786 convex_hull
= isl_basic_set_empty(isl_dim_copy(set
->dim
));
788 convex_hull
= isl_basic_set_universe(isl_dim_copy(set
->dim
));
793 /* Compute the convex hull of a pair of basic sets without any parameters or
794 * integer divisions using Fourier-Motzkin elimination.
795 * The convex hull is the set of all points that can be written as
796 * the sum of points from both basic sets (in homogeneous coordinates).
797 * We set up the constraints in a space with dimensions for each of
798 * the three sets and then project out the dimensions corresponding
799 * to the two original basic sets, retaining only those corresponding
800 * to the convex hull.
802 static struct isl_basic_set
*convex_hull_pair_elim(struct isl_basic_set
*bset1
,
803 struct isl_basic_set
*bset2
)
806 struct isl_basic_set
*bset
[2];
807 struct isl_basic_set
*hull
= NULL
;
810 if (!bset1
|| !bset2
)
813 dim
= isl_basic_set_n_dim(bset1
);
814 hull
= isl_basic_set_alloc(bset1
->ctx
, 0, 2 + 3 * dim
, 0,
815 1 + dim
+ bset1
->n_eq
+ bset2
->n_eq
,
816 2 + bset1
->n_ineq
+ bset2
->n_ineq
);
819 for (i
= 0; i
< 2; ++i
) {
820 for (j
= 0; j
< bset
[i
]->n_eq
; ++j
) {
821 k
= isl_basic_set_alloc_equality(hull
);
824 isl_seq_clr(hull
->eq
[k
], (i
+1) * (1+dim
));
825 isl_seq_clr(hull
->eq
[k
]+(i
+2)*(1+dim
), (1-i
)*(1+dim
));
826 isl_seq_cpy(hull
->eq
[k
]+(i
+1)*(1+dim
), bset
[i
]->eq
[j
],
829 for (j
= 0; j
< bset
[i
]->n_ineq
; ++j
) {
830 k
= isl_basic_set_alloc_inequality(hull
);
833 isl_seq_clr(hull
->ineq
[k
], (i
+1) * (1+dim
));
834 isl_seq_clr(hull
->ineq
[k
]+(i
+2)*(1+dim
), (1-i
)*(1+dim
));
835 isl_seq_cpy(hull
->ineq
[k
]+(i
+1)*(1+dim
),
836 bset
[i
]->ineq
[j
], 1+dim
);
838 k
= isl_basic_set_alloc_inequality(hull
);
841 isl_seq_clr(hull
->ineq
[k
], 1+2+3*dim
);
842 isl_int_set_si(hull
->ineq
[k
][(i
+1)*(1+dim
)], 1);
844 for (j
= 0; j
< 1+dim
; ++j
) {
845 k
= isl_basic_set_alloc_equality(hull
);
848 isl_seq_clr(hull
->eq
[k
], 1+2+3*dim
);
849 isl_int_set_si(hull
->eq
[k
][j
], -1);
850 isl_int_set_si(hull
->eq
[k
][1+dim
+j
], 1);
851 isl_int_set_si(hull
->eq
[k
][2*(1+dim
)+j
], 1);
853 hull
= isl_basic_set_set_rational(hull
);
854 hull
= isl_basic_set_remove_dims(hull
, dim
, 2*(1+dim
));
855 hull
= isl_basic_set_convex_hull(hull
);
856 isl_basic_set_free(bset1
);
857 isl_basic_set_free(bset2
);
860 isl_basic_set_free(bset1
);
861 isl_basic_set_free(bset2
);
862 isl_basic_set_free(hull
);
866 static int isl_basic_set_is_bounded(struct isl_basic_set
*bset
)
873 if (isl_basic_set_fast_is_empty(bset
))
876 tab
= isl_tab_from_recession_cone(bset
);
877 bounded
= isl_tab_cone_is_bounded(tab
);
882 int isl_set_is_bounded(__isl_keep isl_set
*set
)
886 for (i
= 0; i
< set
->n
; ++i
) {
887 int bounded
= isl_basic_set_is_bounded(set
->p
[i
]);
888 if (!bounded
|| bounded
< 0)
894 /* Compute the lineality space of the convex hull of bset1 and bset2.
896 * We first compute the intersection of the recession cone of bset1
897 * with the negative of the recession cone of bset2 and then compute
898 * the linear hull of the resulting cone.
900 static struct isl_basic_set
*induced_lineality_space(
901 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
904 struct isl_basic_set
*lin
= NULL
;
907 if (!bset1
|| !bset2
)
910 dim
= isl_basic_set_total_dim(bset1
);
911 lin
= isl_basic_set_alloc_dim(isl_basic_set_get_dim(bset1
), 0,
912 bset1
->n_eq
+ bset2
->n_eq
,
913 bset1
->n_ineq
+ bset2
->n_ineq
);
914 lin
= isl_basic_set_set_rational(lin
);
917 for (i
= 0; i
< bset1
->n_eq
; ++i
) {
918 k
= isl_basic_set_alloc_equality(lin
);
921 isl_int_set_si(lin
->eq
[k
][0], 0);
922 isl_seq_cpy(lin
->eq
[k
] + 1, bset1
->eq
[i
] + 1, dim
);
924 for (i
= 0; i
< bset1
->n_ineq
; ++i
) {
925 k
= isl_basic_set_alloc_inequality(lin
);
928 isl_int_set_si(lin
->ineq
[k
][0], 0);
929 isl_seq_cpy(lin
->ineq
[k
] + 1, bset1
->ineq
[i
] + 1, dim
);
931 for (i
= 0; i
< bset2
->n_eq
; ++i
) {
932 k
= isl_basic_set_alloc_equality(lin
);
935 isl_int_set_si(lin
->eq
[k
][0], 0);
936 isl_seq_neg(lin
->eq
[k
] + 1, bset2
->eq
[i
] + 1, dim
);
938 for (i
= 0; i
< bset2
->n_ineq
; ++i
) {
939 k
= isl_basic_set_alloc_inequality(lin
);
942 isl_int_set_si(lin
->ineq
[k
][0], 0);
943 isl_seq_neg(lin
->ineq
[k
] + 1, bset2
->ineq
[i
] + 1, dim
);
946 isl_basic_set_free(bset1
);
947 isl_basic_set_free(bset2
);
948 return isl_basic_set_affine_hull(lin
);
950 isl_basic_set_free(lin
);
951 isl_basic_set_free(bset1
);
952 isl_basic_set_free(bset2
);
956 static struct isl_basic_set
*uset_convex_hull(struct isl_set
*set
);
958 /* Given a set and a linear space "lin" of dimension n > 0,
959 * project the linear space from the set, compute the convex hull
960 * and then map the set back to the original space.
966 * describe the linear space. We first compute the Hermite normal
967 * form H = M U of M = H Q, to obtain
971 * The last n rows of H will be zero, so the last n variables of x' = Q x
972 * are the one we want to project out. We do this by transforming each
973 * basic set A x >= b to A U x' >= b and then removing the last n dimensions.
974 * After computing the convex hull in x'_1, i.e., A' x'_1 >= b',
975 * we transform the hull back to the original space as A' Q_1 x >= b',
976 * with Q_1 all but the last n rows of Q.
978 static struct isl_basic_set
*modulo_lineality(struct isl_set
*set
,
979 struct isl_basic_set
*lin
)
981 unsigned total
= isl_basic_set_total_dim(lin
);
983 struct isl_basic_set
*hull
;
984 struct isl_mat
*M
, *U
, *Q
;
988 lin_dim
= total
- lin
->n_eq
;
989 M
= isl_mat_sub_alloc(set
->ctx
, lin
->eq
, 0, lin
->n_eq
, 1, total
);
990 M
= isl_mat_left_hermite(M
, 0, &U
, &Q
);
994 isl_basic_set_free(lin
);
996 Q
= isl_mat_drop_rows(Q
, Q
->n_row
- lin_dim
, lin_dim
);
998 U
= isl_mat_lin_to_aff(U
);
999 Q
= isl_mat_lin_to_aff(Q
);
1001 set
= isl_set_preimage(set
, U
);
1002 set
= isl_set_remove_dims(set
, total
- lin_dim
, lin_dim
);
1003 hull
= uset_convex_hull(set
);
1004 hull
= isl_basic_set_preimage(hull
, Q
);
1008 isl_basic_set_free(lin
);
1013 /* Given two polyhedra with as constraints h_{ij} x >= 0 in homegeneous space,
1014 * set up an LP for solving
1016 * \sum_j \alpha_{1j} h_{1j} = \sum_j \alpha_{2j} h_{2j}
1018 * \alpha{i0} corresponds to the (implicit) positivity constraint 1 >= 0
1019 * The next \alpha{ij} correspond to the equalities and come in pairs.
1020 * The final \alpha{ij} correspond to the inequalities.
1022 static struct isl_basic_set
*valid_direction_lp(
1023 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
1025 struct isl_dim
*dim
;
1026 struct isl_basic_set
*lp
;
1031 if (!bset1
|| !bset2
)
1033 d
= 1 + isl_basic_set_total_dim(bset1
);
1035 2 * bset1
->n_eq
+ bset1
->n_ineq
+ 2 * bset2
->n_eq
+ bset2
->n_ineq
;
1036 dim
= isl_dim_set_alloc(bset1
->ctx
, 0, n
);
1037 lp
= isl_basic_set_alloc_dim(dim
, 0, d
, n
);
1040 for (i
= 0; i
< n
; ++i
) {
1041 k
= isl_basic_set_alloc_inequality(lp
);
1044 isl_seq_clr(lp
->ineq
[k
] + 1, n
);
1045 isl_int_set_si(lp
->ineq
[k
][0], -1);
1046 isl_int_set_si(lp
->ineq
[k
][1 + i
], 1);
1048 for (i
= 0; i
< d
; ++i
) {
1049 k
= isl_basic_set_alloc_equality(lp
);
1053 isl_int_set_si(lp
->eq
[k
][n
++], 0);
1054 /* positivity constraint 1 >= 0 */
1055 isl_int_set_si(lp
->eq
[k
][n
++], i
== 0);
1056 for (j
= 0; j
< bset1
->n_eq
; ++j
) {
1057 isl_int_set(lp
->eq
[k
][n
++], bset1
->eq
[j
][i
]);
1058 isl_int_neg(lp
->eq
[k
][n
++], bset1
->eq
[j
][i
]);
1060 for (j
= 0; j
< bset1
->n_ineq
; ++j
)
1061 isl_int_set(lp
->eq
[k
][n
++], bset1
->ineq
[j
][i
]);
1062 /* positivity constraint 1 >= 0 */
1063 isl_int_set_si(lp
->eq
[k
][n
++], -(i
== 0));
1064 for (j
= 0; j
< bset2
->n_eq
; ++j
) {
1065 isl_int_neg(lp
->eq
[k
][n
++], bset2
->eq
[j
][i
]);
1066 isl_int_set(lp
->eq
[k
][n
++], bset2
->eq
[j
][i
]);
1068 for (j
= 0; j
< bset2
->n_ineq
; ++j
)
1069 isl_int_neg(lp
->eq
[k
][n
++], bset2
->ineq
[j
][i
]);
1071 lp
= isl_basic_set_gauss(lp
, NULL
);
1072 isl_basic_set_free(bset1
);
1073 isl_basic_set_free(bset2
);
1076 isl_basic_set_free(bset1
);
1077 isl_basic_set_free(bset2
);
1081 /* Compute a vector s in the homogeneous space such that <s, r> > 0
1082 * for all rays in the homogeneous space of the two cones that correspond
1083 * to the input polyhedra bset1 and bset2.
1085 * We compute s as a vector that satisfies
1087 * s = \sum_j \alpha_{ij} h_{ij} for i = 1,2 (*)
1089 * with h_{ij} the normals of the facets of polyhedron i
1090 * (including the "positivity constraint" 1 >= 0) and \alpha_{ij}
1091 * strictly positive numbers. For simplicity we impose \alpha_{ij} >= 1.
1092 * We first set up an LP with as variables the \alpha{ij}.
1093 * In this formulation, for each polyhedron i,
1094 * the first constraint is the positivity constraint, followed by pairs
1095 * of variables for the equalities, followed by variables for the inequalities.
1096 * We then simply pick a feasible solution and compute s using (*).
1098 * Note that we simply pick any valid direction and make no attempt
1099 * to pick a "good" or even the "best" valid direction.
1101 static struct isl_vec
*valid_direction(
1102 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
1104 struct isl_basic_set
*lp
;
1105 struct isl_tab
*tab
;
1106 struct isl_vec
*sample
= NULL
;
1107 struct isl_vec
*dir
;
1112 if (!bset1
|| !bset2
)
1114 lp
= valid_direction_lp(isl_basic_set_copy(bset1
),
1115 isl_basic_set_copy(bset2
));
1116 tab
= isl_tab_from_basic_set(lp
);
1117 sample
= isl_tab_get_sample_value(tab
);
1119 isl_basic_set_free(lp
);
1122 d
= isl_basic_set_total_dim(bset1
);
1123 dir
= isl_vec_alloc(bset1
->ctx
, 1 + d
);
1126 isl_seq_clr(dir
->block
.data
+ 1, dir
->size
- 1);
1128 /* positivity constraint 1 >= 0 */
1129 isl_int_set(dir
->block
.data
[0], sample
->block
.data
[n
++]);
1130 for (i
= 0; i
< bset1
->n_eq
; ++i
) {
1131 isl_int_sub(sample
->block
.data
[n
],
1132 sample
->block
.data
[n
], sample
->block
.data
[n
+1]);
1133 isl_seq_combine(dir
->block
.data
,
1134 bset1
->ctx
->one
, dir
->block
.data
,
1135 sample
->block
.data
[n
], bset1
->eq
[i
], 1 + d
);
1139 for (i
= 0; i
< bset1
->n_ineq
; ++i
)
1140 isl_seq_combine(dir
->block
.data
,
1141 bset1
->ctx
->one
, dir
->block
.data
,
1142 sample
->block
.data
[n
++], bset1
->ineq
[i
], 1 + d
);
1143 isl_vec_free(sample
);
1144 isl_seq_normalize(bset1
->ctx
, dir
->el
, dir
->size
);
1145 isl_basic_set_free(bset1
);
1146 isl_basic_set_free(bset2
);
1149 isl_vec_free(sample
);
1150 isl_basic_set_free(bset1
);
1151 isl_basic_set_free(bset2
);
1155 /* Given a polyhedron b_i + A_i x >= 0 and a map T = S^{-1},
1156 * compute b_i' + A_i' x' >= 0, with
1158 * [ b_i A_i ] [ y' ] [ y' ]
1159 * [ 1 0 ] S^{-1} [ x' ] >= 0 or [ b_i' A_i' ] [ x' ] >= 0
1161 * In particular, add the "positivity constraint" and then perform
1164 static struct isl_basic_set
*homogeneous_map(struct isl_basic_set
*bset
,
1171 bset
= isl_basic_set_extend_constraints(bset
, 0, 1);
1172 k
= isl_basic_set_alloc_inequality(bset
);
1175 isl_seq_clr(bset
->ineq
[k
] + 1, isl_basic_set_total_dim(bset
));
1176 isl_int_set_si(bset
->ineq
[k
][0], 1);
1177 bset
= isl_basic_set_preimage(bset
, T
);
1181 isl_basic_set_free(bset
);
1185 /* Compute the convex hull of a pair of basic sets without any parameters or
1186 * integer divisions, where the convex hull is known to be pointed,
1187 * but the basic sets may be unbounded.
1189 * We turn this problem into the computation of a convex hull of a pair
1190 * _bounded_ polyhedra by "changing the direction of the homogeneous
1191 * dimension". This idea is due to Matthias Koeppe.
1193 * Consider the cones in homogeneous space that correspond to the
1194 * input polyhedra. The rays of these cones are also rays of the
1195 * polyhedra if the coordinate that corresponds to the homogeneous
1196 * dimension is zero. That is, if the inner product of the rays
1197 * with the homogeneous direction is zero.
1198 * The cones in the homogeneous space can also be considered to
1199 * correspond to other pairs of polyhedra by chosing a different
1200 * homogeneous direction. To ensure that both of these polyhedra
1201 * are bounded, we need to make sure that all rays of the cones
1202 * correspond to vertices and not to rays.
1203 * Let s be a direction such that <s, r> > 0 for all rays r of both cones.
1204 * Then using s as a homogeneous direction, we obtain a pair of polytopes.
1205 * The vector s is computed in valid_direction.
1207 * Note that we need to consider _all_ rays of the cones and not just
1208 * the rays that correspond to rays in the polyhedra. If we were to
1209 * only consider those rays and turn them into vertices, then we
1210 * may inadvertently turn some vertices into rays.
1212 * The standard homogeneous direction is the unit vector in the 0th coordinate.
1213 * We therefore transform the two polyhedra such that the selected
1214 * direction is mapped onto this standard direction and then proceed
1215 * with the normal computation.
1216 * Let S be a non-singular square matrix with s as its first row,
1217 * then we want to map the polyhedra to the space
1219 * [ y' ] [ y ] [ y ] [ y' ]
1220 * [ x' ] = S [ x ] i.e., [ x ] = S^{-1} [ x' ]
1222 * We take S to be the unimodular completion of s to limit the growth
1223 * of the coefficients in the following computations.
1225 * Let b_i + A_i x >= 0 be the constraints of polyhedron i.
1226 * We first move to the homogeneous dimension
1228 * b_i y + A_i x >= 0 [ b_i A_i ] [ y ] [ 0 ]
1229 * y >= 0 or [ 1 0 ] [ x ] >= [ 0 ]
1231 * Then we change directoin
1233 * [ b_i A_i ] [ y' ] [ y' ]
1234 * [ 1 0 ] S^{-1} [ x' ] >= 0 or [ b_i' A_i' ] [ x' ] >= 0
1236 * Then we compute the convex hull of the polytopes b_i' + A_i' x' >= 0
1237 * resulting in b' + A' x' >= 0, which we then convert back
1240 * [ b' A' ] S [ x ] >= 0 or [ b A ] [ x ] >= 0
1242 * The polyhedron b + A x >= 0 is then the convex hull of the input polyhedra.
1244 static struct isl_basic_set
*convex_hull_pair_pointed(
1245 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
1247 struct isl_ctx
*ctx
= NULL
;
1248 struct isl_vec
*dir
= NULL
;
1249 struct isl_mat
*T
= NULL
;
1250 struct isl_mat
*T2
= NULL
;
1251 struct isl_basic_set
*hull
;
1252 struct isl_set
*set
;
1254 if (!bset1
|| !bset2
)
1257 dir
= valid_direction(isl_basic_set_copy(bset1
),
1258 isl_basic_set_copy(bset2
));
1261 T
= isl_mat_alloc(bset1
->ctx
, dir
->size
, dir
->size
);
1264 isl_seq_cpy(T
->row
[0], dir
->block
.data
, dir
->size
);
1265 T
= isl_mat_unimodular_complete(T
, 1);
1266 T2
= isl_mat_right_inverse(isl_mat_copy(T
));
1268 bset1
= homogeneous_map(bset1
, isl_mat_copy(T2
));
1269 bset2
= homogeneous_map(bset2
, T2
);
1270 set
= isl_set_alloc_dim(isl_basic_set_get_dim(bset1
), 2, 0);
1271 set
= isl_set_add_basic_set(set
, bset1
);
1272 set
= isl_set_add_basic_set(set
, bset2
);
1273 hull
= uset_convex_hull(set
);
1274 hull
= isl_basic_set_preimage(hull
, T
);
1281 isl_basic_set_free(bset1
);
1282 isl_basic_set_free(bset2
);
1286 static struct isl_basic_set
*uset_convex_hull_wrap(struct isl_set
*set
);
1287 static struct isl_basic_set
*modulo_affine_hull(
1288 struct isl_set
*set
, struct isl_basic_set
*affine_hull
);
1290 /* Compute the convex hull of a pair of basic sets without any parameters or
1291 * integer divisions.
1293 * This function is called from uset_convex_hull_unbounded, which
1294 * means that the complete convex hull is unbounded. Some pairs
1295 * of basic sets may still be bounded, though.
1296 * They may even lie inside a lower dimensional space, in which
1297 * case they need to be handled inside their affine hull since
1298 * the main algorithm assumes that the result is full-dimensional.
1300 * If the convex hull of the two basic sets would have a non-trivial
1301 * lineality space, we first project out this lineality space.
1303 static struct isl_basic_set
*convex_hull_pair(struct isl_basic_set
*bset1
,
1304 struct isl_basic_set
*bset2
)
1306 isl_basic_set
*lin
, *aff
;
1307 int bounded1
, bounded2
;
1309 aff
= isl_set_affine_hull(isl_basic_set_union(isl_basic_set_copy(bset1
),
1310 isl_basic_set_copy(bset2
)));
1314 return modulo_affine_hull(isl_basic_set_union(bset1
, bset2
), aff
);
1315 isl_basic_set_free(aff
);
1317 bounded1
= isl_basic_set_is_bounded(bset1
);
1318 bounded2
= isl_basic_set_is_bounded(bset2
);
1320 if (bounded1
< 0 || bounded2
< 0)
1323 if (bounded1
&& bounded2
)
1324 uset_convex_hull_wrap(isl_basic_set_union(bset1
, bset2
));
1326 if (bounded1
|| bounded2
)
1327 return convex_hull_pair_pointed(bset1
, bset2
);
1329 lin
= induced_lineality_space(isl_basic_set_copy(bset1
),
1330 isl_basic_set_copy(bset2
));
1333 if (isl_basic_set_is_universe(lin
)) {
1334 isl_basic_set_free(bset1
);
1335 isl_basic_set_free(bset2
);
1338 if (lin
->n_eq
< isl_basic_set_total_dim(lin
)) {
1339 struct isl_set
*set
;
1340 set
= isl_set_alloc_dim(isl_basic_set_get_dim(bset1
), 2, 0);
1341 set
= isl_set_add_basic_set(set
, bset1
);
1342 set
= isl_set_add_basic_set(set
, bset2
);
1343 return modulo_lineality(set
, lin
);
1345 isl_basic_set_free(lin
);
1347 return convex_hull_pair_pointed(bset1
, bset2
);
1349 isl_basic_set_free(bset1
);
1350 isl_basic_set_free(bset2
);
1354 /* Compute the lineality space of a basic set.
1355 * We currently do not allow the basic set to have any divs.
1356 * We basically just drop the constants and turn every inequality
1359 struct isl_basic_set
*isl_basic_set_lineality_space(struct isl_basic_set
*bset
)
1362 struct isl_basic_set
*lin
= NULL
;
1367 isl_assert(bset
->ctx
, bset
->n_div
== 0, goto error
);
1368 dim
= isl_basic_set_total_dim(bset
);
1370 lin
= isl_basic_set_alloc_dim(isl_basic_set_get_dim(bset
), 0, dim
, 0);
1373 for (i
= 0; i
< bset
->n_eq
; ++i
) {
1374 k
= isl_basic_set_alloc_equality(lin
);
1377 isl_int_set_si(lin
->eq
[k
][0], 0);
1378 isl_seq_cpy(lin
->eq
[k
] + 1, bset
->eq
[i
] + 1, dim
);
1380 lin
= isl_basic_set_gauss(lin
, NULL
);
1383 for (i
= 0; i
< bset
->n_ineq
&& lin
->n_eq
< dim
; ++i
) {
1384 k
= isl_basic_set_alloc_equality(lin
);
1387 isl_int_set_si(lin
->eq
[k
][0], 0);
1388 isl_seq_cpy(lin
->eq
[k
] + 1, bset
->ineq
[i
] + 1, dim
);
1389 lin
= isl_basic_set_gauss(lin
, NULL
);
1393 isl_basic_set_free(bset
);
1396 isl_basic_set_free(lin
);
1397 isl_basic_set_free(bset
);
1401 /* Compute the (linear) hull of the lineality spaces of the basic sets in the
1402 * "underlying" set "set".
1404 static struct isl_basic_set
*uset_combined_lineality_space(struct isl_set
*set
)
1407 struct isl_set
*lin
= NULL
;
1412 struct isl_dim
*dim
= isl_set_get_dim(set
);
1414 return isl_basic_set_empty(dim
);
1417 lin
= isl_set_alloc_dim(isl_set_get_dim(set
), set
->n
, 0);
1418 for (i
= 0; i
< set
->n
; ++i
)
1419 lin
= isl_set_add_basic_set(lin
,
1420 isl_basic_set_lineality_space(isl_basic_set_copy(set
->p
[i
])));
1422 return isl_set_affine_hull(lin
);
1425 /* Compute the convex hull of a set without any parameters or
1426 * integer divisions.
1427 * In each step, we combined two basic sets until only one
1428 * basic set is left.
1429 * The input basic sets are assumed not to have a non-trivial
1430 * lineality space. If any of the intermediate results has
1431 * a non-trivial lineality space, it is projected out.
1433 static struct isl_basic_set
*uset_convex_hull_unbounded(struct isl_set
*set
)
1435 struct isl_basic_set
*convex_hull
= NULL
;
1437 convex_hull
= isl_set_copy_basic_set(set
);
1438 set
= isl_set_drop_basic_set(set
, convex_hull
);
1441 while (set
->n
> 0) {
1442 struct isl_basic_set
*t
;
1443 t
= isl_set_copy_basic_set(set
);
1446 set
= isl_set_drop_basic_set(set
, t
);
1449 convex_hull
= convex_hull_pair(convex_hull
, t
);
1452 t
= isl_basic_set_lineality_space(isl_basic_set_copy(convex_hull
));
1455 if (isl_basic_set_is_universe(t
)) {
1456 isl_basic_set_free(convex_hull
);
1460 if (t
->n_eq
< isl_basic_set_total_dim(t
)) {
1461 set
= isl_set_add_basic_set(set
, convex_hull
);
1462 return modulo_lineality(set
, t
);
1464 isl_basic_set_free(t
);
1470 isl_basic_set_free(convex_hull
);
1474 /* Compute an initial hull for wrapping containing a single initial
1476 * This function assumes that the given set is bounded.
1478 static struct isl_basic_set
*initial_hull(struct isl_basic_set
*hull
,
1479 struct isl_set
*set
)
1481 struct isl_mat
*bounds
= NULL
;
1487 bounds
= initial_facet_constraint(set
);
1490 k
= isl_basic_set_alloc_inequality(hull
);
1493 dim
= isl_set_n_dim(set
);
1494 isl_assert(set
->ctx
, 1 + dim
== bounds
->n_col
, goto error
);
1495 isl_seq_cpy(hull
->ineq
[k
], bounds
->row
[0], bounds
->n_col
);
1496 isl_mat_free(bounds
);
1500 isl_basic_set_free(hull
);
1501 isl_mat_free(bounds
);
1505 struct max_constraint
{
1511 static int max_constraint_equal(const void *entry
, const void *val
)
1513 struct max_constraint
*a
= (struct max_constraint
*)entry
;
1514 isl_int
*b
= (isl_int
*)val
;
1516 return isl_seq_eq(a
->c
->row
[0] + 1, b
, a
->c
->n_col
- 1);
1519 static void update_constraint(struct isl_ctx
*ctx
, struct isl_hash_table
*table
,
1520 isl_int
*con
, unsigned len
, int n
, int ineq
)
1522 struct isl_hash_table_entry
*entry
;
1523 struct max_constraint
*c
;
1526 c_hash
= isl_seq_get_hash(con
+ 1, len
);
1527 entry
= isl_hash_table_find(ctx
, table
, c_hash
, max_constraint_equal
,
1533 isl_hash_table_remove(ctx
, table
, entry
);
1537 if (isl_int_gt(c
->c
->row
[0][0], con
[0]))
1539 if (isl_int_eq(c
->c
->row
[0][0], con
[0])) {
1544 c
->c
= isl_mat_cow(c
->c
);
1545 isl_int_set(c
->c
->row
[0][0], con
[0]);
1549 /* Check whether the constraint hash table "table" constains the constraint
1552 static int has_constraint(struct isl_ctx
*ctx
, struct isl_hash_table
*table
,
1553 isl_int
*con
, unsigned len
, int n
)
1555 struct isl_hash_table_entry
*entry
;
1556 struct max_constraint
*c
;
1559 c_hash
= isl_seq_get_hash(con
+ 1, len
);
1560 entry
= isl_hash_table_find(ctx
, table
, c_hash
, max_constraint_equal
,
1567 return isl_int_eq(c
->c
->row
[0][0], con
[0]);
1570 /* Check for inequality constraints of a basic set without equalities
1571 * such that the same or more stringent copies of the constraint appear
1572 * in all of the basic sets. Such constraints are necessarily facet
1573 * constraints of the convex hull.
1575 * If the resulting basic set is by chance identical to one of
1576 * the basic sets in "set", then we know that this basic set contains
1577 * all other basic sets and is therefore the convex hull of set.
1578 * In this case we set *is_hull to 1.
1580 static struct isl_basic_set
*common_constraints(struct isl_basic_set
*hull
,
1581 struct isl_set
*set
, int *is_hull
)
1584 int min_constraints
;
1586 struct max_constraint
*constraints
= NULL
;
1587 struct isl_hash_table
*table
= NULL
;
1592 for (i
= 0; i
< set
->n
; ++i
)
1593 if (set
->p
[i
]->n_eq
== 0)
1597 min_constraints
= set
->p
[i
]->n_ineq
;
1599 for (i
= best
+ 1; i
< set
->n
; ++i
) {
1600 if (set
->p
[i
]->n_eq
!= 0)
1602 if (set
->p
[i
]->n_ineq
>= min_constraints
)
1604 min_constraints
= set
->p
[i
]->n_ineq
;
1607 constraints
= isl_calloc_array(hull
->ctx
, struct max_constraint
,
1611 table
= isl_alloc_type(hull
->ctx
, struct isl_hash_table
);
1612 if (isl_hash_table_init(hull
->ctx
, table
, min_constraints
))
1615 total
= isl_dim_total(set
->dim
);
1616 for (i
= 0; i
< set
->p
[best
]->n_ineq
; ++i
) {
1617 constraints
[i
].c
= isl_mat_sub_alloc(hull
->ctx
,
1618 set
->p
[best
]->ineq
+ i
, 0, 1, 0, 1 + total
);
1619 if (!constraints
[i
].c
)
1621 constraints
[i
].ineq
= 1;
1623 for (i
= 0; i
< min_constraints
; ++i
) {
1624 struct isl_hash_table_entry
*entry
;
1626 c_hash
= isl_seq_get_hash(constraints
[i
].c
->row
[0] + 1, total
);
1627 entry
= isl_hash_table_find(hull
->ctx
, table
, c_hash
,
1628 max_constraint_equal
, constraints
[i
].c
->row
[0] + 1, 1);
1631 isl_assert(hull
->ctx
, !entry
->data
, goto error
);
1632 entry
->data
= &constraints
[i
];
1636 for (s
= 0; s
< set
->n
; ++s
) {
1640 for (i
= 0; i
< set
->p
[s
]->n_eq
; ++i
) {
1641 isl_int
*eq
= set
->p
[s
]->eq
[i
];
1642 for (j
= 0; j
< 2; ++j
) {
1643 isl_seq_neg(eq
, eq
, 1 + total
);
1644 update_constraint(hull
->ctx
, table
,
1648 for (i
= 0; i
< set
->p
[s
]->n_ineq
; ++i
) {
1649 isl_int
*ineq
= set
->p
[s
]->ineq
[i
];
1650 update_constraint(hull
->ctx
, table
, ineq
, total
, n
,
1651 set
->p
[s
]->n_eq
== 0);
1656 for (i
= 0; i
< min_constraints
; ++i
) {
1657 if (constraints
[i
].count
< n
)
1659 if (!constraints
[i
].ineq
)
1661 j
= isl_basic_set_alloc_inequality(hull
);
1664 isl_seq_cpy(hull
->ineq
[j
], constraints
[i
].c
->row
[0], 1 + total
);
1667 for (s
= 0; s
< set
->n
; ++s
) {
1668 if (set
->p
[s
]->n_eq
)
1670 if (set
->p
[s
]->n_ineq
!= hull
->n_ineq
)
1672 for (i
= 0; i
< set
->p
[s
]->n_ineq
; ++i
) {
1673 isl_int
*ineq
= set
->p
[s
]->ineq
[i
];
1674 if (!has_constraint(hull
->ctx
, table
, ineq
, total
, n
))
1677 if (i
== set
->p
[s
]->n_ineq
)
1681 isl_hash_table_clear(table
);
1682 for (i
= 0; i
< min_constraints
; ++i
)
1683 isl_mat_free(constraints
[i
].c
);
1688 isl_hash_table_clear(table
);
1691 for (i
= 0; i
< min_constraints
; ++i
)
1692 isl_mat_free(constraints
[i
].c
);
1697 /* Create a template for the convex hull of "set" and fill it up
1698 * obvious facet constraints, if any. If the result happens to
1699 * be the convex hull of "set" then *is_hull is set to 1.
1701 static struct isl_basic_set
*proto_hull(struct isl_set
*set
, int *is_hull
)
1703 struct isl_basic_set
*hull
;
1708 for (i
= 0; i
< set
->n
; ++i
) {
1709 n_ineq
+= set
->p
[i
]->n_eq
;
1710 n_ineq
+= set
->p
[i
]->n_ineq
;
1712 hull
= isl_basic_set_alloc_dim(isl_dim_copy(set
->dim
), 0, 0, n_ineq
);
1713 hull
= isl_basic_set_set_rational(hull
);
1716 return common_constraints(hull
, set
, is_hull
);
1719 static struct isl_basic_set
*uset_convex_hull_wrap(struct isl_set
*set
)
1721 struct isl_basic_set
*hull
;
1724 hull
= proto_hull(set
, &is_hull
);
1725 if (hull
&& !is_hull
) {
1726 if (hull
->n_ineq
== 0)
1727 hull
= initial_hull(hull
, set
);
1728 hull
= extend(hull
, set
);
1735 /* Compute the convex hull of a set without any parameters or
1736 * integer divisions. Depending on whether the set is bounded,
1737 * we pass control to the wrapping based convex hull or
1738 * the Fourier-Motzkin elimination based convex hull.
1739 * We also handle a few special cases before checking the boundedness.
1741 static struct isl_basic_set
*uset_convex_hull(struct isl_set
*set
)
1743 struct isl_basic_set
*convex_hull
= NULL
;
1744 struct isl_basic_set
*lin
;
1746 if (isl_set_n_dim(set
) == 0)
1747 return convex_hull_0d(set
);
1749 set
= isl_set_coalesce(set
);
1750 set
= isl_set_set_rational(set
);
1757 convex_hull
= isl_basic_set_copy(set
->p
[0]);
1761 if (isl_set_n_dim(set
) == 1)
1762 return convex_hull_1d(set
);
1764 if (isl_set_is_bounded(set
))
1765 return uset_convex_hull_wrap(set
);
1767 lin
= uset_combined_lineality_space(isl_set_copy(set
));
1770 if (isl_basic_set_is_universe(lin
)) {
1774 if (lin
->n_eq
< isl_basic_set_total_dim(lin
))
1775 return modulo_lineality(set
, lin
);
1776 isl_basic_set_free(lin
);
1778 return uset_convex_hull_unbounded(set
);
1781 isl_basic_set_free(convex_hull
);
1785 /* This is the core procedure, where "set" is a "pure" set, i.e.,
1786 * without parameters or divs and where the convex hull of set is
1787 * known to be full-dimensional.
1789 static struct isl_basic_set
*uset_convex_hull_wrap_bounded(struct isl_set
*set
)
1791 struct isl_basic_set
*convex_hull
= NULL
;
1793 if (isl_set_n_dim(set
) == 0) {
1794 convex_hull
= isl_basic_set_universe(isl_dim_copy(set
->dim
));
1796 convex_hull
= isl_basic_set_set_rational(convex_hull
);
1800 set
= isl_set_set_rational(set
);
1804 set
= isl_set_coalesce(set
);
1808 convex_hull
= isl_basic_set_copy(set
->p
[0]);
1812 if (isl_set_n_dim(set
) == 1)
1813 return convex_hull_1d(set
);
1815 return uset_convex_hull_wrap(set
);
1821 /* Compute the convex hull of set "set" with affine hull "affine_hull",
1822 * We first remove the equalities (transforming the set), compute the
1823 * convex hull of the transformed set and then add the equalities back
1824 * (after performing the inverse transformation.
1826 static struct isl_basic_set
*modulo_affine_hull(
1827 struct isl_set
*set
, struct isl_basic_set
*affine_hull
)
1831 struct isl_basic_set
*dummy
;
1832 struct isl_basic_set
*convex_hull
;
1834 dummy
= isl_basic_set_remove_equalities(
1835 isl_basic_set_copy(affine_hull
), &T
, &T2
);
1838 isl_basic_set_free(dummy
);
1839 set
= isl_set_preimage(set
, T
);
1840 convex_hull
= uset_convex_hull(set
);
1841 convex_hull
= isl_basic_set_preimage(convex_hull
, T2
);
1842 convex_hull
= isl_basic_set_intersect(convex_hull
, affine_hull
);
1845 isl_basic_set_free(affine_hull
);
1850 /* Compute the convex hull of a map.
1852 * The implementation was inspired by "Extended Convex Hull" by Fukuda et al.,
1853 * specifically, the wrapping of facets to obtain new facets.
1855 struct isl_basic_map
*isl_map_convex_hull(struct isl_map
*map
)
1857 struct isl_basic_set
*bset
;
1858 struct isl_basic_map
*model
= NULL
;
1859 struct isl_basic_set
*affine_hull
= NULL
;
1860 struct isl_basic_map
*convex_hull
= NULL
;
1861 struct isl_set
*set
= NULL
;
1862 struct isl_ctx
*ctx
;
1869 convex_hull
= isl_basic_map_empty_like_map(map
);
1874 map
= isl_map_detect_equalities(map
);
1875 map
= isl_map_align_divs(map
);
1876 model
= isl_basic_map_copy(map
->p
[0]);
1877 set
= isl_map_underlying_set(map
);
1881 affine_hull
= isl_set_affine_hull(isl_set_copy(set
));
1884 if (affine_hull
->n_eq
!= 0)
1885 bset
= modulo_affine_hull(set
, affine_hull
);
1887 isl_basic_set_free(affine_hull
);
1888 bset
= uset_convex_hull(set
);
1891 convex_hull
= isl_basic_map_overlying_set(bset
, model
);
1893 ISL_F_SET(convex_hull
, ISL_BASIC_MAP_NO_IMPLICIT
);
1894 ISL_F_SET(convex_hull
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1895 ISL_F_CLR(convex_hull
, ISL_BASIC_MAP_RATIONAL
);
1899 isl_basic_map_free(model
);
1903 struct isl_basic_set
*isl_set_convex_hull(struct isl_set
*set
)
1905 return (struct isl_basic_set
*)
1906 isl_map_convex_hull((struct isl_map
*)set
);
1909 struct sh_data_entry
{
1910 struct isl_hash_table
*table
;
1911 struct isl_tab
*tab
;
1914 /* Holds the data needed during the simple hull computation.
1916 * n the number of basic sets in the original set
1917 * hull_table a hash table of already computed constraints
1918 * in the simple hull
1919 * p for each basic set,
1920 * table a hash table of the constraints
1921 * tab the tableau corresponding to the basic set
1924 struct isl_ctx
*ctx
;
1926 struct isl_hash_table
*hull_table
;
1927 struct sh_data_entry p
[1];
1930 static void sh_data_free(struct sh_data
*data
)
1936 isl_hash_table_free(data
->ctx
, data
->hull_table
);
1937 for (i
= 0; i
< data
->n
; ++i
) {
1938 isl_hash_table_free(data
->ctx
, data
->p
[i
].table
);
1939 isl_tab_free(data
->p
[i
].tab
);
1944 struct ineq_cmp_data
{
1949 static int has_ineq(const void *entry
, const void *val
)
1951 isl_int
*row
= (isl_int
*)entry
;
1952 struct ineq_cmp_data
*v
= (struct ineq_cmp_data
*)val
;
1954 return isl_seq_eq(row
+ 1, v
->p
+ 1, v
->len
) ||
1955 isl_seq_is_neg(row
+ 1, v
->p
+ 1, v
->len
);
1958 static int hash_ineq(struct isl_ctx
*ctx
, struct isl_hash_table
*table
,
1959 isl_int
*ineq
, unsigned len
)
1962 struct ineq_cmp_data v
;
1963 struct isl_hash_table_entry
*entry
;
1967 c_hash
= isl_seq_get_hash(ineq
+ 1, len
);
1968 entry
= isl_hash_table_find(ctx
, table
, c_hash
, has_ineq
, &v
, 1);
1975 /* Fill hash table "table" with the constraints of "bset".
1976 * Equalities are added as two inequalities.
1977 * The value in the hash table is a pointer to the (in)equality of "bset".
1979 static int hash_basic_set(struct isl_hash_table
*table
,
1980 struct isl_basic_set
*bset
)
1983 unsigned dim
= isl_basic_set_total_dim(bset
);
1985 for (i
= 0; i
< bset
->n_eq
; ++i
) {
1986 for (j
= 0; j
< 2; ++j
) {
1987 isl_seq_neg(bset
->eq
[i
], bset
->eq
[i
], 1 + dim
);
1988 if (hash_ineq(bset
->ctx
, table
, bset
->eq
[i
], dim
) < 0)
1992 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
1993 if (hash_ineq(bset
->ctx
, table
, bset
->ineq
[i
], dim
) < 0)
1999 static struct sh_data
*sh_data_alloc(struct isl_set
*set
, unsigned n_ineq
)
2001 struct sh_data
*data
;
2004 data
= isl_calloc(set
->ctx
, struct sh_data
,
2005 sizeof(struct sh_data
) +
2006 (set
->n
- 1) * sizeof(struct sh_data_entry
));
2009 data
->ctx
= set
->ctx
;
2011 data
->hull_table
= isl_hash_table_alloc(set
->ctx
, n_ineq
);
2012 if (!data
->hull_table
)
2014 for (i
= 0; i
< set
->n
; ++i
) {
2015 data
->p
[i
].table
= isl_hash_table_alloc(set
->ctx
,
2016 2 * set
->p
[i
]->n_eq
+ set
->p
[i
]->n_ineq
);
2017 if (!data
->p
[i
].table
)
2019 if (hash_basic_set(data
->p
[i
].table
, set
->p
[i
]) < 0)
2028 /* Check if inequality "ineq" is a bound for basic set "j" or if
2029 * it can be relaxed (by increasing the constant term) to become
2030 * a bound for that basic set. In the latter case, the constant
2032 * Return 1 if "ineq" is a bound
2033 * 0 if "ineq" may attain arbitrarily small values on basic set "j"
2034 * -1 if some error occurred
2036 static int is_bound(struct sh_data
*data
, struct isl_set
*set
, int j
,
2039 enum isl_lp_result res
;
2042 if (!data
->p
[j
].tab
) {
2043 data
->p
[j
].tab
= isl_tab_from_basic_set(set
->p
[j
]);
2044 if (!data
->p
[j
].tab
)
2050 res
= isl_tab_min(data
->p
[j
].tab
, ineq
, data
->ctx
->one
,
2052 if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
2053 isl_int_sub(ineq
[0], ineq
[0], opt
);
2057 return (res
== isl_lp_ok
|| res
== isl_lp_empty
) ? 1 :
2058 res
== isl_lp_unbounded
? 0 : -1;
2061 /* Check if inequality "ineq" from basic set "i" can be relaxed to
2062 * become a bound on the whole set. If so, add the (relaxed) inequality
2065 * We first check if "hull" already contains a translate of the inequality.
2066 * If so, we are done.
2067 * Then, we check if any of the previous basic sets contains a translate
2068 * of the inequality. If so, then we have already considered this
2069 * inequality and we are done.
2070 * Otherwise, for each basic set other than "i", we check if the inequality
2071 * is a bound on the basic set.
2072 * For previous basic sets, we know that they do not contain a translate
2073 * of the inequality, so we directly call is_bound.
2074 * For following basic sets, we first check if a translate of the
2075 * inequality appears in its description and if so directly update
2076 * the inequality accordingly.
2078 static struct isl_basic_set
*add_bound(struct isl_basic_set
*hull
,
2079 struct sh_data
*data
, struct isl_set
*set
, int i
, isl_int
*ineq
)
2082 struct ineq_cmp_data v
;
2083 struct isl_hash_table_entry
*entry
;
2089 v
.len
= isl_basic_set_total_dim(hull
);
2091 c_hash
= isl_seq_get_hash(ineq
+ 1, v
.len
);
2093 entry
= isl_hash_table_find(hull
->ctx
, data
->hull_table
, c_hash
,
2098 for (j
= 0; j
< i
; ++j
) {
2099 entry
= isl_hash_table_find(hull
->ctx
, data
->p
[j
].table
,
2100 c_hash
, has_ineq
, &v
, 0);
2107 k
= isl_basic_set_alloc_inequality(hull
);
2108 isl_seq_cpy(hull
->ineq
[k
], ineq
, 1 + v
.len
);
2112 for (j
= 0; j
< i
; ++j
) {
2114 bound
= is_bound(data
, set
, j
, hull
->ineq
[k
]);
2121 isl_basic_set_free_inequality(hull
, 1);
2125 for (j
= i
+ 1; j
< set
->n
; ++j
) {
2128 entry
= isl_hash_table_find(hull
->ctx
, data
->p
[j
].table
,
2129 c_hash
, has_ineq
, &v
, 0);
2131 ineq_j
= entry
->data
;
2132 neg
= isl_seq_is_neg(ineq_j
+ 1,
2133 hull
->ineq
[k
] + 1, v
.len
);
2135 isl_int_neg(ineq_j
[0], ineq_j
[0]);
2136 if (isl_int_gt(ineq_j
[0], hull
->ineq
[k
][0]))
2137 isl_int_set(hull
->ineq
[k
][0], ineq_j
[0]);
2139 isl_int_neg(ineq_j
[0], ineq_j
[0]);
2142 bound
= is_bound(data
, set
, j
, hull
->ineq
[k
]);
2149 isl_basic_set_free_inequality(hull
, 1);
2153 entry
= isl_hash_table_find(hull
->ctx
, data
->hull_table
, c_hash
,
2157 entry
->data
= hull
->ineq
[k
];
2161 isl_basic_set_free(hull
);
2165 /* Check if any inequality from basic set "i" can be relaxed to
2166 * become a bound on the whole set. If so, add the (relaxed) inequality
2169 static struct isl_basic_set
*add_bounds(struct isl_basic_set
*bset
,
2170 struct sh_data
*data
, struct isl_set
*set
, int i
)
2173 unsigned dim
= isl_basic_set_total_dim(bset
);
2175 for (j
= 0; j
< set
->p
[i
]->n_eq
; ++j
) {
2176 for (k
= 0; k
< 2; ++k
) {
2177 isl_seq_neg(set
->p
[i
]->eq
[j
], set
->p
[i
]->eq
[j
], 1+dim
);
2178 bset
= add_bound(bset
, data
, set
, i
, set
->p
[i
]->eq
[j
]);
2181 for (j
= 0; j
< set
->p
[i
]->n_ineq
; ++j
)
2182 bset
= add_bound(bset
, data
, set
, i
, set
->p
[i
]->ineq
[j
]);
2186 /* Compute a superset of the convex hull of set that is described
2187 * by only translates of the constraints in the constituents of set.
2189 static struct isl_basic_set
*uset_simple_hull(struct isl_set
*set
)
2191 struct sh_data
*data
= NULL
;
2192 struct isl_basic_set
*hull
= NULL
;
2200 for (i
= 0; i
< set
->n
; ++i
) {
2203 n_ineq
+= 2 * set
->p
[i
]->n_eq
+ set
->p
[i
]->n_ineq
;
2206 hull
= isl_basic_set_alloc_dim(isl_dim_copy(set
->dim
), 0, 0, n_ineq
);
2210 data
= sh_data_alloc(set
, n_ineq
);
2214 for (i
= 0; i
< set
->n
; ++i
)
2215 hull
= add_bounds(hull
, data
, set
, i
);
2223 isl_basic_set_free(hull
);
2228 /* Compute a superset of the convex hull of map that is described
2229 * by only translates of the constraints in the constituents of map.
2231 struct isl_basic_map
*isl_map_simple_hull(struct isl_map
*map
)
2233 struct isl_set
*set
= NULL
;
2234 struct isl_basic_map
*model
= NULL
;
2235 struct isl_basic_map
*hull
;
2236 struct isl_basic_map
*affine_hull
;
2237 struct isl_basic_set
*bset
= NULL
;
2242 hull
= isl_basic_map_empty_like_map(map
);
2247 hull
= isl_basic_map_copy(map
->p
[0]);
2252 map
= isl_map_detect_equalities(map
);
2253 affine_hull
= isl_map_affine_hull(isl_map_copy(map
));
2254 map
= isl_map_align_divs(map
);
2255 model
= isl_basic_map_copy(map
->p
[0]);
2257 set
= isl_map_underlying_set(map
);
2259 bset
= uset_simple_hull(set
);
2261 hull
= isl_basic_map_overlying_set(bset
, model
);
2263 hull
= isl_basic_map_intersect(hull
, affine_hull
);
2264 hull
= isl_basic_map_convex_hull(hull
);
2265 ISL_F_SET(hull
, ISL_BASIC_MAP_NO_IMPLICIT
);
2266 ISL_F_SET(hull
, ISL_BASIC_MAP_ALL_EQUALITIES
);
2271 struct isl_basic_set
*isl_set_simple_hull(struct isl_set
*set
)
2273 return (struct isl_basic_set
*)
2274 isl_map_simple_hull((struct isl_map
*)set
);
2277 /* Given a set "set", return parametric bounds on the dimension "dim".
2279 static struct isl_basic_set
*set_bounds(struct isl_set
*set
, int dim
)
2281 unsigned set_dim
= isl_set_dim(set
, isl_dim_set
);
2282 set
= isl_set_copy(set
);
2283 set
= isl_set_eliminate_dims(set
, dim
+ 1, set_dim
- (dim
+ 1));
2284 set
= isl_set_eliminate_dims(set
, 0, dim
);
2285 return isl_set_convex_hull(set
);
2288 /* Computes a "simple hull" and then check if each dimension in the
2289 * resulting hull is bounded by a symbolic constant. If not, the
2290 * hull is intersected with the corresponding bounds on the whole set.
2292 struct isl_basic_set
*isl_set_bounded_simple_hull(struct isl_set
*set
)
2295 struct isl_basic_set
*hull
;
2296 unsigned nparam
, left
;
2297 int removed_divs
= 0;
2299 hull
= isl_set_simple_hull(isl_set_copy(set
));
2303 nparam
= isl_basic_set_dim(hull
, isl_dim_param
);
2304 for (i
= 0; i
< isl_basic_set_dim(hull
, isl_dim_set
); ++i
) {
2305 int lower
= 0, upper
= 0;
2306 struct isl_basic_set
*bounds
;
2308 left
= isl_basic_set_total_dim(hull
) - nparam
- i
- 1;
2309 for (j
= 0; j
< hull
->n_eq
; ++j
) {
2310 if (isl_int_is_zero(hull
->eq
[j
][1 + nparam
+ i
]))
2312 if (isl_seq_first_non_zero(hull
->eq
[j
]+1+nparam
+i
+1,
2319 for (j
= 0; j
< hull
->n_ineq
; ++j
) {
2320 if (isl_int_is_zero(hull
->ineq
[j
][1 + nparam
+ i
]))
2322 if (isl_seq_first_non_zero(hull
->ineq
[j
]+1+nparam
+i
+1,
2324 isl_seq_first_non_zero(hull
->ineq
[j
]+1+nparam
,
2327 if (isl_int_is_pos(hull
->ineq
[j
][1 + nparam
+ i
]))
2338 if (!removed_divs
) {
2339 set
= isl_set_remove_divs(set
);
2344 bounds
= set_bounds(set
, i
);
2345 hull
= isl_basic_set_intersect(hull
, bounds
);