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 if (isl_tab_detect_implicit_equalities(tab
) < 0)
108 if (isl_tab_detect_redundant(tab
) < 0)
110 bmap
= isl_basic_map_update_from_tab(bmap
, tab
);
112 ISL_F_SET(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
113 ISL_F_SET(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
117 isl_basic_map_free(bmap
);
121 struct isl_basic_set
*isl_basic_set_convex_hull(struct isl_basic_set
*bset
)
123 return (struct isl_basic_set
*)
124 isl_basic_map_convex_hull((struct isl_basic_map
*)bset
);
127 /* Check if the set set is bound in the direction of the affine
128 * constraint c and if so, set the constant term such that the
129 * resulting constraint is a bounding constraint for the set.
131 static int uset_is_bound(struct isl_set
*set
, isl_int
*c
, unsigned len
)
139 isl_int_init(opt_denom
);
141 for (j
= 0; j
< set
->n
; ++j
) {
142 enum isl_lp_result res
;
144 if (ISL_F_ISSET(set
->p
[j
], ISL_BASIC_SET_EMPTY
))
147 res
= isl_basic_set_solve_lp(set
->p
[j
],
148 0, c
, set
->ctx
->one
, &opt
, &opt_denom
, NULL
);
149 if (res
== isl_lp_unbounded
)
151 if (res
== isl_lp_error
)
153 if (res
== isl_lp_empty
) {
154 set
->p
[j
] = isl_basic_set_set_to_empty(set
->p
[j
]);
159 if (first
|| isl_int_is_neg(opt
)) {
160 if (!isl_int_is_one(opt_denom
))
161 isl_seq_scale(c
, c
, opt_denom
, len
);
162 isl_int_sub(c
[0], c
[0], opt
);
167 isl_int_clear(opt_denom
);
171 isl_int_clear(opt_denom
);
175 struct isl_basic_set
*isl_basic_set_set_rational(struct isl_basic_set
*bset
)
180 if (ISL_F_ISSET(bset
, ISL_BASIC_MAP_RATIONAL
))
183 bset
= isl_basic_set_cow(bset
);
187 ISL_F_SET(bset
, ISL_BASIC_MAP_RATIONAL
);
189 return isl_basic_set_finalize(bset
);
192 static struct isl_set
*isl_set_set_rational(struct isl_set
*set
)
196 set
= isl_set_cow(set
);
199 for (i
= 0; i
< set
->n
; ++i
) {
200 set
->p
[i
] = isl_basic_set_set_rational(set
->p
[i
]);
210 static struct isl_basic_set
*isl_basic_set_add_equality(
211 struct isl_basic_set
*bset
, isl_int
*c
)
219 if (ISL_F_ISSET(bset
, ISL_BASIC_SET_EMPTY
))
222 isl_assert(bset
->ctx
, isl_basic_set_n_param(bset
) == 0, goto error
);
223 isl_assert(bset
->ctx
, bset
->n_div
== 0, goto error
);
224 dim
= isl_basic_set_n_dim(bset
);
225 bset
= isl_basic_set_cow(bset
);
226 bset
= isl_basic_set_extend(bset
, 0, dim
, 0, 1, 0);
227 i
= isl_basic_set_alloc_equality(bset
);
230 isl_seq_cpy(bset
->eq
[i
], c
, 1 + dim
);
233 isl_basic_set_free(bset
);
237 static struct isl_set
*isl_set_add_basic_set_equality(struct isl_set
*set
, isl_int
*c
)
241 set
= isl_set_cow(set
);
244 for (i
= 0; i
< set
->n
; ++i
) {
245 set
->p
[i
] = isl_basic_set_add_equality(set
->p
[i
], c
);
255 /* Given a union of basic sets, construct the constraints for wrapping
256 * a facet around one of its ridges.
257 * In particular, if each of n the d-dimensional basic sets i in "set"
258 * contains the origin, satisfies the constraints x_1 >= 0 and x_2 >= 0
259 * and is defined by the constraints
263 * then the resulting set is of dimension n*(1+d) and has as constraints
272 static struct isl_basic_set
*wrap_constraints(struct isl_set
*set
)
274 struct isl_basic_set
*lp
;
278 unsigned dim
, lp_dim
;
283 dim
= 1 + isl_set_n_dim(set
);
286 for (i
= 0; i
< set
->n
; ++i
) {
287 n_eq
+= set
->p
[i
]->n_eq
;
288 n_ineq
+= set
->p
[i
]->n_ineq
;
290 lp
= isl_basic_set_alloc(set
->ctx
, 0, dim
* set
->n
, 0, n_eq
, n_ineq
);
293 lp_dim
= isl_basic_set_n_dim(lp
);
294 k
= isl_basic_set_alloc_equality(lp
);
295 isl_int_set_si(lp
->eq
[k
][0], -1);
296 for (i
= 0; i
< set
->n
; ++i
) {
297 isl_int_set_si(lp
->eq
[k
][1+dim
*i
], 0);
298 isl_int_set_si(lp
->eq
[k
][1+dim
*i
+1], 1);
299 isl_seq_clr(lp
->eq
[k
]+1+dim
*i
+2, dim
-2);
301 for (i
= 0; i
< set
->n
; ++i
) {
302 k
= isl_basic_set_alloc_inequality(lp
);
303 isl_seq_clr(lp
->ineq
[k
], 1+lp_dim
);
304 isl_int_set_si(lp
->ineq
[k
][1+dim
*i
], 1);
306 for (j
= 0; j
< set
->p
[i
]->n_eq
; ++j
) {
307 k
= isl_basic_set_alloc_equality(lp
);
308 isl_seq_clr(lp
->eq
[k
], 1+dim
*i
);
309 isl_seq_cpy(lp
->eq
[k
]+1+dim
*i
, set
->p
[i
]->eq
[j
], dim
);
310 isl_seq_clr(lp
->eq
[k
]+1+dim
*(i
+1), dim
*(set
->n
-i
-1));
313 for (j
= 0; j
< set
->p
[i
]->n_ineq
; ++j
) {
314 k
= isl_basic_set_alloc_inequality(lp
);
315 isl_seq_clr(lp
->ineq
[k
], 1+dim
*i
);
316 isl_seq_cpy(lp
->ineq
[k
]+1+dim
*i
, set
->p
[i
]->ineq
[j
], dim
);
317 isl_seq_clr(lp
->ineq
[k
]+1+dim
*(i
+1), dim
*(set
->n
-i
-1));
323 /* Given a facet "facet" of the convex hull of "set" and a facet "ridge"
324 * of that facet, compute the other facet of the convex hull that contains
327 * We first transform the set such that the facet constraint becomes
331 * I.e., the facet lies in
335 * and on that facet, the constraint that defines the ridge is
339 * (This transformation is not strictly needed, all that is needed is
340 * that the ridge contains the origin.)
342 * Since the ridge contains the origin, the cone of the convex hull
343 * will be of the form
348 * with this second constraint defining the new facet.
349 * The constant a is obtained by settting x_1 in the cone of the
350 * convex hull to 1 and minimizing x_2.
351 * Now, each element in the cone of the convex hull is the sum
352 * of elements in the cones of the basic sets.
353 * If a_i is the dilation factor of basic set i, then the problem
354 * we need to solve is
367 * the constraints of each (transformed) basic set.
368 * If a = n/d, then the constraint defining the new facet (in the transformed
371 * -n x_1 + d x_2 >= 0
373 * In the original space, we need to take the same combination of the
374 * corresponding constraints "facet" and "ridge".
376 * If a = -infty = "-1/0", then we just return the original facet constraint.
377 * This means that the facet is unbounded, but has a bounded intersection
378 * with the union of sets.
380 isl_int
*isl_set_wrap_facet(__isl_keep isl_set
*set
,
381 isl_int
*facet
, isl_int
*ridge
)
384 struct isl_mat
*T
= NULL
;
385 struct isl_basic_set
*lp
= NULL
;
387 enum isl_lp_result res
;
391 set
= isl_set_copy(set
);
392 set
= isl_set_set_rational(set
);
394 dim
= 1 + isl_set_n_dim(set
);
395 T
= isl_mat_alloc(set
->ctx
, 3, dim
);
398 isl_int_set_si(T
->row
[0][0], 1);
399 isl_seq_clr(T
->row
[0]+1, dim
- 1);
400 isl_seq_cpy(T
->row
[1], facet
, dim
);
401 isl_seq_cpy(T
->row
[2], ridge
, dim
);
402 T
= isl_mat_right_inverse(T
);
403 set
= isl_set_preimage(set
, T
);
407 lp
= wrap_constraints(set
);
408 obj
= isl_vec_alloc(set
->ctx
, 1 + dim
*set
->n
);
411 isl_int_set_si(obj
->block
.data
[0], 0);
412 for (i
= 0; i
< set
->n
; ++i
) {
413 isl_seq_clr(obj
->block
.data
+ 1 + dim
*i
, 2);
414 isl_int_set_si(obj
->block
.data
[1 + dim
*i
+2], 1);
415 isl_seq_clr(obj
->block
.data
+ 1 + dim
*i
+3, dim
-3);
419 res
= isl_basic_set_solve_lp(lp
, 0,
420 obj
->block
.data
, set
->ctx
->one
, &num
, &den
, NULL
);
421 if (res
== isl_lp_ok
) {
422 isl_int_neg(num
, num
);
423 isl_seq_combine(facet
, num
, facet
, den
, ridge
, dim
);
428 isl_basic_set_free(lp
);
430 isl_assert(set
->ctx
, res
== isl_lp_ok
|| res
== isl_lp_unbounded
,
434 isl_basic_set_free(lp
);
440 /* Compute the constraint of a facet of "set".
442 * We first compute the intersection with a bounding constraint
443 * that is orthogonal to one of the coordinate axes.
444 * If the affine hull of this intersection has only one equality,
445 * we have found a facet.
446 * Otherwise, we wrap the current bounding constraint around
447 * one of the equalities of the face (one that is not equal to
448 * the current bounding constraint).
449 * This process continues until we have found a facet.
450 * The dimension of the intersection increases by at least
451 * one on each iteration, so termination is guaranteed.
453 static __isl_give isl_mat
*initial_facet_constraint(__isl_keep isl_set
*set
)
455 struct isl_set
*slice
= NULL
;
456 struct isl_basic_set
*face
= NULL
;
458 unsigned dim
= isl_set_n_dim(set
);
462 isl_assert(set
->ctx
, set
->n
> 0, goto error
);
463 bounds
= isl_mat_alloc(set
->ctx
, 1, 1 + dim
);
467 isl_seq_clr(bounds
->row
[0], dim
);
468 isl_int_set_si(bounds
->row
[0][1 + dim
- 1], 1);
469 is_bound
= uset_is_bound(set
, bounds
->row
[0], 1 + dim
);
470 isl_assert(set
->ctx
, is_bound
== 1, goto error
);
471 isl_seq_normalize(set
->ctx
, bounds
->row
[0], 1 + dim
);
475 slice
= isl_set_copy(set
);
476 slice
= isl_set_add_basic_set_equality(slice
, bounds
->row
[0]);
477 face
= isl_set_affine_hull(slice
);
480 if (face
->n_eq
== 1) {
481 isl_basic_set_free(face
);
484 for (i
= 0; i
< face
->n_eq
; ++i
)
485 if (!isl_seq_eq(bounds
->row
[0], face
->eq
[i
], 1 + dim
) &&
486 !isl_seq_is_neg(bounds
->row
[0],
487 face
->eq
[i
], 1 + dim
))
489 isl_assert(set
->ctx
, i
< face
->n_eq
, goto error
);
490 if (!isl_set_wrap_facet(set
, bounds
->row
[0], face
->eq
[i
]))
492 isl_seq_normalize(set
->ctx
, bounds
->row
[0], bounds
->n_col
);
493 isl_basic_set_free(face
);
498 isl_basic_set_free(face
);
499 isl_mat_free(bounds
);
503 /* Given the bounding constraint "c" of a facet of the convex hull of "set",
504 * compute a hyperplane description of the facet, i.e., compute the facets
507 * We compute an affine transformation that transforms the constraint
516 * by computing the right inverse U of a matrix that starts with the rows
529 * Since z_1 is zero, we can drop this variable as well as the corresponding
530 * column of U to obtain
538 * with Q' equal to Q, but without the corresponding row.
539 * After computing the facets of the facet in the z' space,
540 * we convert them back to the x space through Q.
542 static struct isl_basic_set
*compute_facet(struct isl_set
*set
, isl_int
*c
)
544 struct isl_mat
*m
, *U
, *Q
;
545 struct isl_basic_set
*facet
= NULL
;
550 set
= isl_set_copy(set
);
551 dim
= isl_set_n_dim(set
);
552 m
= isl_mat_alloc(set
->ctx
, 2, 1 + dim
);
555 isl_int_set_si(m
->row
[0][0], 1);
556 isl_seq_clr(m
->row
[0]+1, dim
);
557 isl_seq_cpy(m
->row
[1], c
, 1+dim
);
558 U
= isl_mat_right_inverse(m
);
559 Q
= isl_mat_right_inverse(isl_mat_copy(U
));
560 U
= isl_mat_drop_cols(U
, 1, 1);
561 Q
= isl_mat_drop_rows(Q
, 1, 1);
562 set
= isl_set_preimage(set
, U
);
563 facet
= uset_convex_hull_wrap_bounded(set
);
564 facet
= isl_basic_set_preimage(facet
, Q
);
566 isl_assert(ctx
, facet
->n_eq
== 0, goto error
);
569 isl_basic_set_free(facet
);
574 /* Given an initial facet constraint, compute the remaining facets.
575 * We do this by running through all facets found so far and computing
576 * the adjacent facets through wrapping, adding those facets that we
577 * hadn't already found before.
579 * For each facet we have found so far, we first compute its facets
580 * in the resulting convex hull. That is, we compute the ridges
581 * of the resulting convex hull contained in the facet.
582 * We also compute the corresponding facet in the current approximation
583 * of the convex hull. There is no need to wrap around the ridges
584 * in this facet since that would result in a facet that is already
585 * present in the current approximation.
587 * This function can still be significantly optimized by checking which of
588 * the facets of the basic sets are also facets of the convex hull and
589 * using all the facets so far to help in constructing the facets of the
592 * using the technique in section "3.1 Ridge Generation" of
593 * "Extended Convex Hull" by Fukuda et al.
595 static struct isl_basic_set
*extend(struct isl_basic_set
*hull
,
600 struct isl_basic_set
*facet
= NULL
;
601 struct isl_basic_set
*hull_facet
= NULL
;
607 isl_assert(set
->ctx
, set
->n
> 0, goto error
);
609 dim
= isl_set_n_dim(set
);
611 for (i
= 0; i
< hull
->n_ineq
; ++i
) {
612 facet
= compute_facet(set
, hull
->ineq
[i
]);
613 facet
= isl_basic_set_add_equality(facet
, hull
->ineq
[i
]);
614 facet
= isl_basic_set_gauss(facet
, NULL
);
615 facet
= isl_basic_set_normalize_constraints(facet
);
616 hull_facet
= isl_basic_set_copy(hull
);
617 hull_facet
= isl_basic_set_add_equality(hull_facet
, hull
->ineq
[i
]);
618 hull_facet
= isl_basic_set_gauss(hull_facet
, NULL
);
619 hull_facet
= isl_basic_set_normalize_constraints(hull_facet
);
622 hull
= isl_basic_set_cow(hull
);
623 hull
= isl_basic_set_extend_dim(hull
,
624 isl_dim_copy(hull
->dim
), 0, 0, facet
->n_ineq
);
625 for (j
= 0; j
< facet
->n_ineq
; ++j
) {
626 for (f
= 0; f
< hull_facet
->n_ineq
; ++f
)
627 if (isl_seq_eq(facet
->ineq
[j
],
628 hull_facet
->ineq
[f
], 1 + dim
))
630 if (f
< hull_facet
->n_ineq
)
632 k
= isl_basic_set_alloc_inequality(hull
);
635 isl_seq_cpy(hull
->ineq
[k
], hull
->ineq
[i
], 1+dim
);
636 if (!isl_set_wrap_facet(set
, hull
->ineq
[k
], facet
->ineq
[j
]))
639 isl_basic_set_free(hull_facet
);
640 isl_basic_set_free(facet
);
642 hull
= isl_basic_set_simplify(hull
);
643 hull
= isl_basic_set_finalize(hull
);
646 isl_basic_set_free(hull_facet
);
647 isl_basic_set_free(facet
);
648 isl_basic_set_free(hull
);
652 /* Special case for computing the convex hull of a one dimensional set.
653 * We simply collect the lower and upper bounds of each basic set
654 * and the biggest of those.
656 static struct isl_basic_set
*convex_hull_1d(struct isl_set
*set
)
658 struct isl_mat
*c
= NULL
;
659 isl_int
*lower
= NULL
;
660 isl_int
*upper
= NULL
;
663 struct isl_basic_set
*hull
;
665 for (i
= 0; i
< set
->n
; ++i
) {
666 set
->p
[i
] = isl_basic_set_simplify(set
->p
[i
]);
670 set
= isl_set_remove_empty_parts(set
);
673 isl_assert(set
->ctx
, set
->n
> 0, goto error
);
674 c
= isl_mat_alloc(set
->ctx
, 2, 2);
678 if (set
->p
[0]->n_eq
> 0) {
679 isl_assert(set
->ctx
, set
->p
[0]->n_eq
== 1, goto error
);
682 if (isl_int_is_pos(set
->p
[0]->eq
[0][1])) {
683 isl_seq_cpy(lower
, set
->p
[0]->eq
[0], 2);
684 isl_seq_neg(upper
, set
->p
[0]->eq
[0], 2);
686 isl_seq_neg(lower
, set
->p
[0]->eq
[0], 2);
687 isl_seq_cpy(upper
, set
->p
[0]->eq
[0], 2);
690 for (j
= 0; j
< set
->p
[0]->n_ineq
; ++j
) {
691 if (isl_int_is_pos(set
->p
[0]->ineq
[j
][1])) {
693 isl_seq_cpy(lower
, set
->p
[0]->ineq
[j
], 2);
696 isl_seq_cpy(upper
, set
->p
[0]->ineq
[j
], 2);
703 for (i
= 0; i
< set
->n
; ++i
) {
704 struct isl_basic_set
*bset
= set
->p
[i
];
708 for (j
= 0; j
< bset
->n_eq
; ++j
) {
712 isl_int_mul(a
, lower
[0], bset
->eq
[j
][1]);
713 isl_int_mul(b
, lower
[1], bset
->eq
[j
][0]);
714 if (isl_int_lt(a
, b
) && isl_int_is_pos(bset
->eq
[j
][1]))
715 isl_seq_cpy(lower
, bset
->eq
[j
], 2);
716 if (isl_int_gt(a
, b
) && isl_int_is_neg(bset
->eq
[j
][1]))
717 isl_seq_neg(lower
, bset
->eq
[j
], 2);
720 isl_int_mul(a
, upper
[0], bset
->eq
[j
][1]);
721 isl_int_mul(b
, upper
[1], bset
->eq
[j
][0]);
722 if (isl_int_lt(a
, b
) && isl_int_is_pos(bset
->eq
[j
][1]))
723 isl_seq_neg(upper
, bset
->eq
[j
], 2);
724 if (isl_int_gt(a
, b
) && isl_int_is_neg(bset
->eq
[j
][1]))
725 isl_seq_cpy(upper
, bset
->eq
[j
], 2);
728 for (j
= 0; j
< bset
->n_ineq
; ++j
) {
729 if (isl_int_is_pos(bset
->ineq
[j
][1]))
731 if (isl_int_is_neg(bset
->ineq
[j
][1]))
733 if (lower
&& isl_int_is_pos(bset
->ineq
[j
][1])) {
734 isl_int_mul(a
, lower
[0], bset
->ineq
[j
][1]);
735 isl_int_mul(b
, lower
[1], bset
->ineq
[j
][0]);
736 if (isl_int_lt(a
, b
))
737 isl_seq_cpy(lower
, bset
->ineq
[j
], 2);
739 if (upper
&& isl_int_is_neg(bset
->ineq
[j
][1])) {
740 isl_int_mul(a
, upper
[0], bset
->ineq
[j
][1]);
741 isl_int_mul(b
, upper
[1], bset
->ineq
[j
][0]);
742 if (isl_int_gt(a
, b
))
743 isl_seq_cpy(upper
, bset
->ineq
[j
], 2);
754 hull
= isl_basic_set_alloc(set
->ctx
, 0, 1, 0, 0, 2);
755 hull
= isl_basic_set_set_rational(hull
);
759 k
= isl_basic_set_alloc_inequality(hull
);
760 isl_seq_cpy(hull
->ineq
[k
], lower
, 2);
763 k
= isl_basic_set_alloc_inequality(hull
);
764 isl_seq_cpy(hull
->ineq
[k
], upper
, 2);
766 hull
= isl_basic_set_finalize(hull
);
776 /* Project out final n dimensions using Fourier-Motzkin */
777 static struct isl_set
*set_project_out(struct isl_ctx
*ctx
,
778 struct isl_set
*set
, unsigned n
)
780 return isl_set_remove_dims(set
, isl_set_n_dim(set
) - n
, n
);
783 static struct isl_basic_set
*convex_hull_0d(struct isl_set
*set
)
785 struct isl_basic_set
*convex_hull
;
790 if (isl_set_is_empty(set
))
791 convex_hull
= isl_basic_set_empty(isl_dim_copy(set
->dim
));
793 convex_hull
= isl_basic_set_universe(isl_dim_copy(set
->dim
));
798 /* Compute the convex hull of a pair of basic sets without any parameters or
799 * integer divisions using Fourier-Motzkin elimination.
800 * The convex hull is the set of all points that can be written as
801 * the sum of points from both basic sets (in homogeneous coordinates).
802 * We set up the constraints in a space with dimensions for each of
803 * the three sets and then project out the dimensions corresponding
804 * to the two original basic sets, retaining only those corresponding
805 * to the convex hull.
807 static struct isl_basic_set
*convex_hull_pair_elim(struct isl_basic_set
*bset1
,
808 struct isl_basic_set
*bset2
)
811 struct isl_basic_set
*bset
[2];
812 struct isl_basic_set
*hull
= NULL
;
815 if (!bset1
|| !bset2
)
818 dim
= isl_basic_set_n_dim(bset1
);
819 hull
= isl_basic_set_alloc(bset1
->ctx
, 0, 2 + 3 * dim
, 0,
820 1 + dim
+ bset1
->n_eq
+ bset2
->n_eq
,
821 2 + bset1
->n_ineq
+ bset2
->n_ineq
);
824 for (i
= 0; i
< 2; ++i
) {
825 for (j
= 0; j
< bset
[i
]->n_eq
; ++j
) {
826 k
= isl_basic_set_alloc_equality(hull
);
829 isl_seq_clr(hull
->eq
[k
], (i
+1) * (1+dim
));
830 isl_seq_clr(hull
->eq
[k
]+(i
+2)*(1+dim
), (1-i
)*(1+dim
));
831 isl_seq_cpy(hull
->eq
[k
]+(i
+1)*(1+dim
), bset
[i
]->eq
[j
],
834 for (j
= 0; j
< bset
[i
]->n_ineq
; ++j
) {
835 k
= isl_basic_set_alloc_inequality(hull
);
838 isl_seq_clr(hull
->ineq
[k
], (i
+1) * (1+dim
));
839 isl_seq_clr(hull
->ineq
[k
]+(i
+2)*(1+dim
), (1-i
)*(1+dim
));
840 isl_seq_cpy(hull
->ineq
[k
]+(i
+1)*(1+dim
),
841 bset
[i
]->ineq
[j
], 1+dim
);
843 k
= isl_basic_set_alloc_inequality(hull
);
846 isl_seq_clr(hull
->ineq
[k
], 1+2+3*dim
);
847 isl_int_set_si(hull
->ineq
[k
][(i
+1)*(1+dim
)], 1);
849 for (j
= 0; j
< 1+dim
; ++j
) {
850 k
= isl_basic_set_alloc_equality(hull
);
853 isl_seq_clr(hull
->eq
[k
], 1+2+3*dim
);
854 isl_int_set_si(hull
->eq
[k
][j
], -1);
855 isl_int_set_si(hull
->eq
[k
][1+dim
+j
], 1);
856 isl_int_set_si(hull
->eq
[k
][2*(1+dim
)+j
], 1);
858 hull
= isl_basic_set_set_rational(hull
);
859 hull
= isl_basic_set_remove_dims(hull
, dim
, 2*(1+dim
));
860 hull
= isl_basic_set_convex_hull(hull
);
861 isl_basic_set_free(bset1
);
862 isl_basic_set_free(bset2
);
865 isl_basic_set_free(bset1
);
866 isl_basic_set_free(bset2
);
867 isl_basic_set_free(hull
);
871 /* Is the set bounded for each value of the parameters?
873 int isl_basic_set_is_bounded(__isl_keep isl_basic_set
*bset
)
880 if (isl_basic_set_fast_is_empty(bset
))
883 tab
= isl_tab_from_recession_cone(bset
, 1);
884 bounded
= isl_tab_cone_is_bounded(tab
);
889 /* Is the set bounded for each value of the parameters?
891 int isl_set_is_bounded(__isl_keep isl_set
*set
)
898 for (i
= 0; i
< set
->n
; ++i
) {
899 int bounded
= isl_basic_set_is_bounded(set
->p
[i
]);
900 if (!bounded
|| bounded
< 0)
906 /* Compute the lineality space of the convex hull of bset1 and bset2.
908 * We first compute the intersection of the recession cone of bset1
909 * with the negative of the recession cone of bset2 and then compute
910 * the linear hull of the resulting cone.
912 static struct isl_basic_set
*induced_lineality_space(
913 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
916 struct isl_basic_set
*lin
= NULL
;
919 if (!bset1
|| !bset2
)
922 dim
= isl_basic_set_total_dim(bset1
);
923 lin
= isl_basic_set_alloc_dim(isl_basic_set_get_dim(bset1
), 0,
924 bset1
->n_eq
+ bset2
->n_eq
,
925 bset1
->n_ineq
+ bset2
->n_ineq
);
926 lin
= isl_basic_set_set_rational(lin
);
929 for (i
= 0; i
< bset1
->n_eq
; ++i
) {
930 k
= isl_basic_set_alloc_equality(lin
);
933 isl_int_set_si(lin
->eq
[k
][0], 0);
934 isl_seq_cpy(lin
->eq
[k
] + 1, bset1
->eq
[i
] + 1, dim
);
936 for (i
= 0; i
< bset1
->n_ineq
; ++i
) {
937 k
= isl_basic_set_alloc_inequality(lin
);
940 isl_int_set_si(lin
->ineq
[k
][0], 0);
941 isl_seq_cpy(lin
->ineq
[k
] + 1, bset1
->ineq
[i
] + 1, dim
);
943 for (i
= 0; i
< bset2
->n_eq
; ++i
) {
944 k
= isl_basic_set_alloc_equality(lin
);
947 isl_int_set_si(lin
->eq
[k
][0], 0);
948 isl_seq_neg(lin
->eq
[k
] + 1, bset2
->eq
[i
] + 1, dim
);
950 for (i
= 0; i
< bset2
->n_ineq
; ++i
) {
951 k
= isl_basic_set_alloc_inequality(lin
);
954 isl_int_set_si(lin
->ineq
[k
][0], 0);
955 isl_seq_neg(lin
->ineq
[k
] + 1, bset2
->ineq
[i
] + 1, dim
);
958 isl_basic_set_free(bset1
);
959 isl_basic_set_free(bset2
);
960 return isl_basic_set_affine_hull(lin
);
962 isl_basic_set_free(lin
);
963 isl_basic_set_free(bset1
);
964 isl_basic_set_free(bset2
);
968 static struct isl_basic_set
*uset_convex_hull(struct isl_set
*set
);
970 /* Given a set and a linear space "lin" of dimension n > 0,
971 * project the linear space from the set, compute the convex hull
972 * and then map the set back to the original space.
978 * describe the linear space. We first compute the Hermite normal
979 * form H = M U of M = H Q, to obtain
983 * The last n rows of H will be zero, so the last n variables of x' = Q x
984 * are the one we want to project out. We do this by transforming each
985 * basic set A x >= b to A U x' >= b and then removing the last n dimensions.
986 * After computing the convex hull in x'_1, i.e., A' x'_1 >= b',
987 * we transform the hull back to the original space as A' Q_1 x >= b',
988 * with Q_1 all but the last n rows of Q.
990 static struct isl_basic_set
*modulo_lineality(struct isl_set
*set
,
991 struct isl_basic_set
*lin
)
993 unsigned total
= isl_basic_set_total_dim(lin
);
995 struct isl_basic_set
*hull
;
996 struct isl_mat
*M
, *U
, *Q
;
1000 lin_dim
= total
- lin
->n_eq
;
1001 M
= isl_mat_sub_alloc(set
->ctx
, lin
->eq
, 0, lin
->n_eq
, 1, total
);
1002 M
= isl_mat_left_hermite(M
, 0, &U
, &Q
);
1006 isl_basic_set_free(lin
);
1008 Q
= isl_mat_drop_rows(Q
, Q
->n_row
- lin_dim
, lin_dim
);
1010 U
= isl_mat_lin_to_aff(U
);
1011 Q
= isl_mat_lin_to_aff(Q
);
1013 set
= isl_set_preimage(set
, U
);
1014 set
= isl_set_remove_dims(set
, total
- lin_dim
, lin_dim
);
1015 hull
= uset_convex_hull(set
);
1016 hull
= isl_basic_set_preimage(hull
, Q
);
1020 isl_basic_set_free(lin
);
1025 /* Given two polyhedra with as constraints h_{ij} x >= 0 in homegeneous space,
1026 * set up an LP for solving
1028 * \sum_j \alpha_{1j} h_{1j} = \sum_j \alpha_{2j} h_{2j}
1030 * \alpha{i0} corresponds to the (implicit) positivity constraint 1 >= 0
1031 * The next \alpha{ij} correspond to the equalities and come in pairs.
1032 * The final \alpha{ij} correspond to the inequalities.
1034 static struct isl_basic_set
*valid_direction_lp(
1035 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
1037 struct isl_dim
*dim
;
1038 struct isl_basic_set
*lp
;
1043 if (!bset1
|| !bset2
)
1045 d
= 1 + isl_basic_set_total_dim(bset1
);
1047 2 * bset1
->n_eq
+ bset1
->n_ineq
+ 2 * bset2
->n_eq
+ bset2
->n_ineq
;
1048 dim
= isl_dim_set_alloc(bset1
->ctx
, 0, n
);
1049 lp
= isl_basic_set_alloc_dim(dim
, 0, d
, n
);
1052 for (i
= 0; i
< n
; ++i
) {
1053 k
= isl_basic_set_alloc_inequality(lp
);
1056 isl_seq_clr(lp
->ineq
[k
] + 1, n
);
1057 isl_int_set_si(lp
->ineq
[k
][0], -1);
1058 isl_int_set_si(lp
->ineq
[k
][1 + i
], 1);
1060 for (i
= 0; i
< d
; ++i
) {
1061 k
= isl_basic_set_alloc_equality(lp
);
1065 isl_int_set_si(lp
->eq
[k
][n
++], 0);
1066 /* positivity constraint 1 >= 0 */
1067 isl_int_set_si(lp
->eq
[k
][n
++], i
== 0);
1068 for (j
= 0; j
< bset1
->n_eq
; ++j
) {
1069 isl_int_set(lp
->eq
[k
][n
++], bset1
->eq
[j
][i
]);
1070 isl_int_neg(lp
->eq
[k
][n
++], bset1
->eq
[j
][i
]);
1072 for (j
= 0; j
< bset1
->n_ineq
; ++j
)
1073 isl_int_set(lp
->eq
[k
][n
++], bset1
->ineq
[j
][i
]);
1074 /* positivity constraint 1 >= 0 */
1075 isl_int_set_si(lp
->eq
[k
][n
++], -(i
== 0));
1076 for (j
= 0; j
< bset2
->n_eq
; ++j
) {
1077 isl_int_neg(lp
->eq
[k
][n
++], bset2
->eq
[j
][i
]);
1078 isl_int_set(lp
->eq
[k
][n
++], bset2
->eq
[j
][i
]);
1080 for (j
= 0; j
< bset2
->n_ineq
; ++j
)
1081 isl_int_neg(lp
->eq
[k
][n
++], bset2
->ineq
[j
][i
]);
1083 lp
= isl_basic_set_gauss(lp
, NULL
);
1084 isl_basic_set_free(bset1
);
1085 isl_basic_set_free(bset2
);
1088 isl_basic_set_free(bset1
);
1089 isl_basic_set_free(bset2
);
1093 /* Compute a vector s in the homogeneous space such that <s, r> > 0
1094 * for all rays in the homogeneous space of the two cones that correspond
1095 * to the input polyhedra bset1 and bset2.
1097 * We compute s as a vector that satisfies
1099 * s = \sum_j \alpha_{ij} h_{ij} for i = 1,2 (*)
1101 * with h_{ij} the normals of the facets of polyhedron i
1102 * (including the "positivity constraint" 1 >= 0) and \alpha_{ij}
1103 * strictly positive numbers. For simplicity we impose \alpha_{ij} >= 1.
1104 * We first set up an LP with as variables the \alpha{ij}.
1105 * In this formulation, for each polyhedron i,
1106 * the first constraint is the positivity constraint, followed by pairs
1107 * of variables for the equalities, followed by variables for the inequalities.
1108 * We then simply pick a feasible solution and compute s using (*).
1110 * Note that we simply pick any valid direction and make no attempt
1111 * to pick a "good" or even the "best" valid direction.
1113 static struct isl_vec
*valid_direction(
1114 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
1116 struct isl_basic_set
*lp
;
1117 struct isl_tab
*tab
;
1118 struct isl_vec
*sample
= NULL
;
1119 struct isl_vec
*dir
;
1124 if (!bset1
|| !bset2
)
1126 lp
= valid_direction_lp(isl_basic_set_copy(bset1
),
1127 isl_basic_set_copy(bset2
));
1128 tab
= isl_tab_from_basic_set(lp
);
1129 sample
= isl_tab_get_sample_value(tab
);
1131 isl_basic_set_free(lp
);
1134 d
= isl_basic_set_total_dim(bset1
);
1135 dir
= isl_vec_alloc(bset1
->ctx
, 1 + d
);
1138 isl_seq_clr(dir
->block
.data
+ 1, dir
->size
- 1);
1140 /* positivity constraint 1 >= 0 */
1141 isl_int_set(dir
->block
.data
[0], sample
->block
.data
[n
++]);
1142 for (i
= 0; i
< bset1
->n_eq
; ++i
) {
1143 isl_int_sub(sample
->block
.data
[n
],
1144 sample
->block
.data
[n
], sample
->block
.data
[n
+1]);
1145 isl_seq_combine(dir
->block
.data
,
1146 bset1
->ctx
->one
, dir
->block
.data
,
1147 sample
->block
.data
[n
], bset1
->eq
[i
], 1 + d
);
1151 for (i
= 0; i
< bset1
->n_ineq
; ++i
)
1152 isl_seq_combine(dir
->block
.data
,
1153 bset1
->ctx
->one
, dir
->block
.data
,
1154 sample
->block
.data
[n
++], bset1
->ineq
[i
], 1 + d
);
1155 isl_vec_free(sample
);
1156 isl_seq_normalize(bset1
->ctx
, dir
->el
, dir
->size
);
1157 isl_basic_set_free(bset1
);
1158 isl_basic_set_free(bset2
);
1161 isl_vec_free(sample
);
1162 isl_basic_set_free(bset1
);
1163 isl_basic_set_free(bset2
);
1167 /* Given a polyhedron b_i + A_i x >= 0 and a map T = S^{-1},
1168 * compute b_i' + A_i' x' >= 0, with
1170 * [ b_i A_i ] [ y' ] [ y' ]
1171 * [ 1 0 ] S^{-1} [ x' ] >= 0 or [ b_i' A_i' ] [ x' ] >= 0
1173 * In particular, add the "positivity constraint" and then perform
1176 static struct isl_basic_set
*homogeneous_map(struct isl_basic_set
*bset
,
1183 bset
= isl_basic_set_extend_constraints(bset
, 0, 1);
1184 k
= isl_basic_set_alloc_inequality(bset
);
1187 isl_seq_clr(bset
->ineq
[k
] + 1, isl_basic_set_total_dim(bset
));
1188 isl_int_set_si(bset
->ineq
[k
][0], 1);
1189 bset
= isl_basic_set_preimage(bset
, T
);
1193 isl_basic_set_free(bset
);
1197 /* Compute the convex hull of a pair of basic sets without any parameters or
1198 * integer divisions, where the convex hull is known to be pointed,
1199 * but the basic sets may be unbounded.
1201 * We turn this problem into the computation of a convex hull of a pair
1202 * _bounded_ polyhedra by "changing the direction of the homogeneous
1203 * dimension". This idea is due to Matthias Koeppe.
1205 * Consider the cones in homogeneous space that correspond to the
1206 * input polyhedra. The rays of these cones are also rays of the
1207 * polyhedra if the coordinate that corresponds to the homogeneous
1208 * dimension is zero. That is, if the inner product of the rays
1209 * with the homogeneous direction is zero.
1210 * The cones in the homogeneous space can also be considered to
1211 * correspond to other pairs of polyhedra by chosing a different
1212 * homogeneous direction. To ensure that both of these polyhedra
1213 * are bounded, we need to make sure that all rays of the cones
1214 * correspond to vertices and not to rays.
1215 * Let s be a direction such that <s, r> > 0 for all rays r of both cones.
1216 * Then using s as a homogeneous direction, we obtain a pair of polytopes.
1217 * The vector s is computed in valid_direction.
1219 * Note that we need to consider _all_ rays of the cones and not just
1220 * the rays that correspond to rays in the polyhedra. If we were to
1221 * only consider those rays and turn them into vertices, then we
1222 * may inadvertently turn some vertices into rays.
1224 * The standard homogeneous direction is the unit vector in the 0th coordinate.
1225 * We therefore transform the two polyhedra such that the selected
1226 * direction is mapped onto this standard direction and then proceed
1227 * with the normal computation.
1228 * Let S be a non-singular square matrix with s as its first row,
1229 * then we want to map the polyhedra to the space
1231 * [ y' ] [ y ] [ y ] [ y' ]
1232 * [ x' ] = S [ x ] i.e., [ x ] = S^{-1} [ x' ]
1234 * We take S to be the unimodular completion of s to limit the growth
1235 * of the coefficients in the following computations.
1237 * Let b_i + A_i x >= 0 be the constraints of polyhedron i.
1238 * We first move to the homogeneous dimension
1240 * b_i y + A_i x >= 0 [ b_i A_i ] [ y ] [ 0 ]
1241 * y >= 0 or [ 1 0 ] [ x ] >= [ 0 ]
1243 * Then we change directoin
1245 * [ b_i A_i ] [ y' ] [ y' ]
1246 * [ 1 0 ] S^{-1} [ x' ] >= 0 or [ b_i' A_i' ] [ x' ] >= 0
1248 * Then we compute the convex hull of the polytopes b_i' + A_i' x' >= 0
1249 * resulting in b' + A' x' >= 0, which we then convert back
1252 * [ b' A' ] S [ x ] >= 0 or [ b A ] [ x ] >= 0
1254 * The polyhedron b + A x >= 0 is then the convex hull of the input polyhedra.
1256 static struct isl_basic_set
*convex_hull_pair_pointed(
1257 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
1259 struct isl_ctx
*ctx
= NULL
;
1260 struct isl_vec
*dir
= NULL
;
1261 struct isl_mat
*T
= NULL
;
1262 struct isl_mat
*T2
= NULL
;
1263 struct isl_basic_set
*hull
;
1264 struct isl_set
*set
;
1266 if (!bset1
|| !bset2
)
1269 dir
= valid_direction(isl_basic_set_copy(bset1
),
1270 isl_basic_set_copy(bset2
));
1273 T
= isl_mat_alloc(bset1
->ctx
, dir
->size
, dir
->size
);
1276 isl_seq_cpy(T
->row
[0], dir
->block
.data
, dir
->size
);
1277 T
= isl_mat_unimodular_complete(T
, 1);
1278 T2
= isl_mat_right_inverse(isl_mat_copy(T
));
1280 bset1
= homogeneous_map(bset1
, isl_mat_copy(T2
));
1281 bset2
= homogeneous_map(bset2
, T2
);
1282 set
= isl_set_alloc_dim(isl_basic_set_get_dim(bset1
), 2, 0);
1283 set
= isl_set_add_basic_set(set
, bset1
);
1284 set
= isl_set_add_basic_set(set
, bset2
);
1285 hull
= uset_convex_hull(set
);
1286 hull
= isl_basic_set_preimage(hull
, T
);
1293 isl_basic_set_free(bset1
);
1294 isl_basic_set_free(bset2
);
1298 static struct isl_basic_set
*uset_convex_hull_wrap(struct isl_set
*set
);
1299 static struct isl_basic_set
*modulo_affine_hull(
1300 struct isl_set
*set
, struct isl_basic_set
*affine_hull
);
1302 /* Compute the convex hull of a pair of basic sets without any parameters or
1303 * integer divisions.
1305 * This function is called from uset_convex_hull_unbounded, which
1306 * means that the complete convex hull is unbounded. Some pairs
1307 * of basic sets may still be bounded, though.
1308 * They may even lie inside a lower dimensional space, in which
1309 * case they need to be handled inside their affine hull since
1310 * the main algorithm assumes that the result is full-dimensional.
1312 * If the convex hull of the two basic sets would have a non-trivial
1313 * lineality space, we first project out this lineality space.
1315 static struct isl_basic_set
*convex_hull_pair(struct isl_basic_set
*bset1
,
1316 struct isl_basic_set
*bset2
)
1318 isl_basic_set
*lin
, *aff
;
1319 int bounded1
, bounded2
;
1321 aff
= isl_set_affine_hull(isl_basic_set_union(isl_basic_set_copy(bset1
),
1322 isl_basic_set_copy(bset2
)));
1326 return modulo_affine_hull(isl_basic_set_union(bset1
, bset2
), aff
);
1327 isl_basic_set_free(aff
);
1329 bounded1
= isl_basic_set_is_bounded(bset1
);
1330 bounded2
= isl_basic_set_is_bounded(bset2
);
1332 if (bounded1
< 0 || bounded2
< 0)
1335 if (bounded1
&& bounded2
)
1336 uset_convex_hull_wrap(isl_basic_set_union(bset1
, bset2
));
1338 if (bounded1
|| bounded2
)
1339 return convex_hull_pair_pointed(bset1
, bset2
);
1341 lin
= induced_lineality_space(isl_basic_set_copy(bset1
),
1342 isl_basic_set_copy(bset2
));
1345 if (isl_basic_set_is_universe(lin
)) {
1346 isl_basic_set_free(bset1
);
1347 isl_basic_set_free(bset2
);
1350 if (lin
->n_eq
< isl_basic_set_total_dim(lin
)) {
1351 struct isl_set
*set
;
1352 set
= isl_set_alloc_dim(isl_basic_set_get_dim(bset1
), 2, 0);
1353 set
= isl_set_add_basic_set(set
, bset1
);
1354 set
= isl_set_add_basic_set(set
, bset2
);
1355 return modulo_lineality(set
, lin
);
1357 isl_basic_set_free(lin
);
1359 return convex_hull_pair_pointed(bset1
, bset2
);
1361 isl_basic_set_free(bset1
);
1362 isl_basic_set_free(bset2
);
1366 /* Compute the lineality space of a basic set.
1367 * We currently do not allow the basic set to have any divs.
1368 * We basically just drop the constants and turn every inequality
1371 struct isl_basic_set
*isl_basic_set_lineality_space(struct isl_basic_set
*bset
)
1374 struct isl_basic_set
*lin
= NULL
;
1379 isl_assert(bset
->ctx
, bset
->n_div
== 0, goto error
);
1380 dim
= isl_basic_set_total_dim(bset
);
1382 lin
= isl_basic_set_alloc_dim(isl_basic_set_get_dim(bset
), 0, dim
, 0);
1385 for (i
= 0; i
< bset
->n_eq
; ++i
) {
1386 k
= isl_basic_set_alloc_equality(lin
);
1389 isl_int_set_si(lin
->eq
[k
][0], 0);
1390 isl_seq_cpy(lin
->eq
[k
] + 1, bset
->eq
[i
] + 1, dim
);
1392 lin
= isl_basic_set_gauss(lin
, NULL
);
1395 for (i
= 0; i
< bset
->n_ineq
&& lin
->n_eq
< dim
; ++i
) {
1396 k
= isl_basic_set_alloc_equality(lin
);
1399 isl_int_set_si(lin
->eq
[k
][0], 0);
1400 isl_seq_cpy(lin
->eq
[k
] + 1, bset
->ineq
[i
] + 1, dim
);
1401 lin
= isl_basic_set_gauss(lin
, NULL
);
1405 isl_basic_set_free(bset
);
1408 isl_basic_set_free(lin
);
1409 isl_basic_set_free(bset
);
1413 /* Compute the (linear) hull of the lineality spaces of the basic sets in the
1414 * "underlying" set "set".
1416 static struct isl_basic_set
*uset_combined_lineality_space(struct isl_set
*set
)
1419 struct isl_set
*lin
= NULL
;
1424 struct isl_dim
*dim
= isl_set_get_dim(set
);
1426 return isl_basic_set_empty(dim
);
1429 lin
= isl_set_alloc_dim(isl_set_get_dim(set
), set
->n
, 0);
1430 for (i
= 0; i
< set
->n
; ++i
)
1431 lin
= isl_set_add_basic_set(lin
,
1432 isl_basic_set_lineality_space(isl_basic_set_copy(set
->p
[i
])));
1434 return isl_set_affine_hull(lin
);
1437 /* Compute the convex hull of a set without any parameters or
1438 * integer divisions.
1439 * In each step, we combined two basic sets until only one
1440 * basic set is left.
1441 * The input basic sets are assumed not to have a non-trivial
1442 * lineality space. If any of the intermediate results has
1443 * a non-trivial lineality space, it is projected out.
1445 static struct isl_basic_set
*uset_convex_hull_unbounded(struct isl_set
*set
)
1447 struct isl_basic_set
*convex_hull
= NULL
;
1449 convex_hull
= isl_set_copy_basic_set(set
);
1450 set
= isl_set_drop_basic_set(set
, convex_hull
);
1453 while (set
->n
> 0) {
1454 struct isl_basic_set
*t
;
1455 t
= isl_set_copy_basic_set(set
);
1458 set
= isl_set_drop_basic_set(set
, t
);
1461 convex_hull
= convex_hull_pair(convex_hull
, t
);
1464 t
= isl_basic_set_lineality_space(isl_basic_set_copy(convex_hull
));
1467 if (isl_basic_set_is_universe(t
)) {
1468 isl_basic_set_free(convex_hull
);
1472 if (t
->n_eq
< isl_basic_set_total_dim(t
)) {
1473 set
= isl_set_add_basic_set(set
, convex_hull
);
1474 return modulo_lineality(set
, t
);
1476 isl_basic_set_free(t
);
1482 isl_basic_set_free(convex_hull
);
1486 /* Compute an initial hull for wrapping containing a single initial
1488 * This function assumes that the given set is bounded.
1490 static struct isl_basic_set
*initial_hull(struct isl_basic_set
*hull
,
1491 struct isl_set
*set
)
1493 struct isl_mat
*bounds
= NULL
;
1499 bounds
= initial_facet_constraint(set
);
1502 k
= isl_basic_set_alloc_inequality(hull
);
1505 dim
= isl_set_n_dim(set
);
1506 isl_assert(set
->ctx
, 1 + dim
== bounds
->n_col
, goto error
);
1507 isl_seq_cpy(hull
->ineq
[k
], bounds
->row
[0], bounds
->n_col
);
1508 isl_mat_free(bounds
);
1512 isl_basic_set_free(hull
);
1513 isl_mat_free(bounds
);
1517 struct max_constraint
{
1523 static int max_constraint_equal(const void *entry
, const void *val
)
1525 struct max_constraint
*a
= (struct max_constraint
*)entry
;
1526 isl_int
*b
= (isl_int
*)val
;
1528 return isl_seq_eq(a
->c
->row
[0] + 1, b
, a
->c
->n_col
- 1);
1531 static void update_constraint(struct isl_ctx
*ctx
, struct isl_hash_table
*table
,
1532 isl_int
*con
, unsigned len
, int n
, int ineq
)
1534 struct isl_hash_table_entry
*entry
;
1535 struct max_constraint
*c
;
1538 c_hash
= isl_seq_get_hash(con
+ 1, len
);
1539 entry
= isl_hash_table_find(ctx
, table
, c_hash
, max_constraint_equal
,
1545 isl_hash_table_remove(ctx
, table
, entry
);
1549 if (isl_int_gt(c
->c
->row
[0][0], con
[0]))
1551 if (isl_int_eq(c
->c
->row
[0][0], con
[0])) {
1556 c
->c
= isl_mat_cow(c
->c
);
1557 isl_int_set(c
->c
->row
[0][0], con
[0]);
1561 /* Check whether the constraint hash table "table" constains the constraint
1564 static int has_constraint(struct isl_ctx
*ctx
, struct isl_hash_table
*table
,
1565 isl_int
*con
, unsigned len
, int n
)
1567 struct isl_hash_table_entry
*entry
;
1568 struct max_constraint
*c
;
1571 c_hash
= isl_seq_get_hash(con
+ 1, len
);
1572 entry
= isl_hash_table_find(ctx
, table
, c_hash
, max_constraint_equal
,
1579 return isl_int_eq(c
->c
->row
[0][0], con
[0]);
1582 /* Check for inequality constraints of a basic set without equalities
1583 * such that the same or more stringent copies of the constraint appear
1584 * in all of the basic sets. Such constraints are necessarily facet
1585 * constraints of the convex hull.
1587 * If the resulting basic set is by chance identical to one of
1588 * the basic sets in "set", then we know that this basic set contains
1589 * all other basic sets and is therefore the convex hull of set.
1590 * In this case we set *is_hull to 1.
1592 static struct isl_basic_set
*common_constraints(struct isl_basic_set
*hull
,
1593 struct isl_set
*set
, int *is_hull
)
1596 int min_constraints
;
1598 struct max_constraint
*constraints
= NULL
;
1599 struct isl_hash_table
*table
= NULL
;
1604 for (i
= 0; i
< set
->n
; ++i
)
1605 if (set
->p
[i
]->n_eq
== 0)
1609 min_constraints
= set
->p
[i
]->n_ineq
;
1611 for (i
= best
+ 1; i
< set
->n
; ++i
) {
1612 if (set
->p
[i
]->n_eq
!= 0)
1614 if (set
->p
[i
]->n_ineq
>= min_constraints
)
1616 min_constraints
= set
->p
[i
]->n_ineq
;
1619 constraints
= isl_calloc_array(hull
->ctx
, struct max_constraint
,
1623 table
= isl_alloc_type(hull
->ctx
, struct isl_hash_table
);
1624 if (isl_hash_table_init(hull
->ctx
, table
, min_constraints
))
1627 total
= isl_dim_total(set
->dim
);
1628 for (i
= 0; i
< set
->p
[best
]->n_ineq
; ++i
) {
1629 constraints
[i
].c
= isl_mat_sub_alloc(hull
->ctx
,
1630 set
->p
[best
]->ineq
+ i
, 0, 1, 0, 1 + total
);
1631 if (!constraints
[i
].c
)
1633 constraints
[i
].ineq
= 1;
1635 for (i
= 0; i
< min_constraints
; ++i
) {
1636 struct isl_hash_table_entry
*entry
;
1638 c_hash
= isl_seq_get_hash(constraints
[i
].c
->row
[0] + 1, total
);
1639 entry
= isl_hash_table_find(hull
->ctx
, table
, c_hash
,
1640 max_constraint_equal
, constraints
[i
].c
->row
[0] + 1, 1);
1643 isl_assert(hull
->ctx
, !entry
->data
, goto error
);
1644 entry
->data
= &constraints
[i
];
1648 for (s
= 0; s
< set
->n
; ++s
) {
1652 for (i
= 0; i
< set
->p
[s
]->n_eq
; ++i
) {
1653 isl_int
*eq
= set
->p
[s
]->eq
[i
];
1654 for (j
= 0; j
< 2; ++j
) {
1655 isl_seq_neg(eq
, eq
, 1 + total
);
1656 update_constraint(hull
->ctx
, table
,
1660 for (i
= 0; i
< set
->p
[s
]->n_ineq
; ++i
) {
1661 isl_int
*ineq
= set
->p
[s
]->ineq
[i
];
1662 update_constraint(hull
->ctx
, table
, ineq
, total
, n
,
1663 set
->p
[s
]->n_eq
== 0);
1668 for (i
= 0; i
< min_constraints
; ++i
) {
1669 if (constraints
[i
].count
< n
)
1671 if (!constraints
[i
].ineq
)
1673 j
= isl_basic_set_alloc_inequality(hull
);
1676 isl_seq_cpy(hull
->ineq
[j
], constraints
[i
].c
->row
[0], 1 + total
);
1679 for (s
= 0; s
< set
->n
; ++s
) {
1680 if (set
->p
[s
]->n_eq
)
1682 if (set
->p
[s
]->n_ineq
!= hull
->n_ineq
)
1684 for (i
= 0; i
< set
->p
[s
]->n_ineq
; ++i
) {
1685 isl_int
*ineq
= set
->p
[s
]->ineq
[i
];
1686 if (!has_constraint(hull
->ctx
, table
, ineq
, total
, n
))
1689 if (i
== set
->p
[s
]->n_ineq
)
1693 isl_hash_table_clear(table
);
1694 for (i
= 0; i
< min_constraints
; ++i
)
1695 isl_mat_free(constraints
[i
].c
);
1700 isl_hash_table_clear(table
);
1703 for (i
= 0; i
< min_constraints
; ++i
)
1704 isl_mat_free(constraints
[i
].c
);
1709 /* Create a template for the convex hull of "set" and fill it up
1710 * obvious facet constraints, if any. If the result happens to
1711 * be the convex hull of "set" then *is_hull is set to 1.
1713 static struct isl_basic_set
*proto_hull(struct isl_set
*set
, int *is_hull
)
1715 struct isl_basic_set
*hull
;
1720 for (i
= 0; i
< set
->n
; ++i
) {
1721 n_ineq
+= set
->p
[i
]->n_eq
;
1722 n_ineq
+= set
->p
[i
]->n_ineq
;
1724 hull
= isl_basic_set_alloc_dim(isl_dim_copy(set
->dim
), 0, 0, n_ineq
);
1725 hull
= isl_basic_set_set_rational(hull
);
1728 return common_constraints(hull
, set
, is_hull
);
1731 static struct isl_basic_set
*uset_convex_hull_wrap(struct isl_set
*set
)
1733 struct isl_basic_set
*hull
;
1736 hull
= proto_hull(set
, &is_hull
);
1737 if (hull
&& !is_hull
) {
1738 if (hull
->n_ineq
== 0)
1739 hull
= initial_hull(hull
, set
);
1740 hull
= extend(hull
, set
);
1747 /* Compute the convex hull of a set without any parameters or
1748 * integer divisions. Depending on whether the set is bounded,
1749 * we pass control to the wrapping based convex hull or
1750 * the Fourier-Motzkin elimination based convex hull.
1751 * We also handle a few special cases before checking the boundedness.
1753 static struct isl_basic_set
*uset_convex_hull(struct isl_set
*set
)
1755 struct isl_basic_set
*convex_hull
= NULL
;
1756 struct isl_basic_set
*lin
;
1758 if (isl_set_n_dim(set
) == 0)
1759 return convex_hull_0d(set
);
1761 set
= isl_set_coalesce(set
);
1762 set
= isl_set_set_rational(set
);
1769 convex_hull
= isl_basic_set_copy(set
->p
[0]);
1773 if (isl_set_n_dim(set
) == 1)
1774 return convex_hull_1d(set
);
1776 if (isl_set_is_bounded(set
))
1777 return uset_convex_hull_wrap(set
);
1779 lin
= uset_combined_lineality_space(isl_set_copy(set
));
1782 if (isl_basic_set_is_universe(lin
)) {
1786 if (lin
->n_eq
< isl_basic_set_total_dim(lin
))
1787 return modulo_lineality(set
, lin
);
1788 isl_basic_set_free(lin
);
1790 return uset_convex_hull_unbounded(set
);
1793 isl_basic_set_free(convex_hull
);
1797 /* This is the core procedure, where "set" is a "pure" set, i.e.,
1798 * without parameters or divs and where the convex hull of set is
1799 * known to be full-dimensional.
1801 static struct isl_basic_set
*uset_convex_hull_wrap_bounded(struct isl_set
*set
)
1803 struct isl_basic_set
*convex_hull
= NULL
;
1805 if (isl_set_n_dim(set
) == 0) {
1806 convex_hull
= isl_basic_set_universe(isl_dim_copy(set
->dim
));
1808 convex_hull
= isl_basic_set_set_rational(convex_hull
);
1812 set
= isl_set_set_rational(set
);
1816 set
= isl_set_coalesce(set
);
1820 convex_hull
= isl_basic_set_copy(set
->p
[0]);
1824 if (isl_set_n_dim(set
) == 1)
1825 return convex_hull_1d(set
);
1827 return uset_convex_hull_wrap(set
);
1833 /* Compute the convex hull of set "set" with affine hull "affine_hull",
1834 * We first remove the equalities (transforming the set), compute the
1835 * convex hull of the transformed set and then add the equalities back
1836 * (after performing the inverse transformation.
1838 static struct isl_basic_set
*modulo_affine_hull(
1839 struct isl_set
*set
, struct isl_basic_set
*affine_hull
)
1843 struct isl_basic_set
*dummy
;
1844 struct isl_basic_set
*convex_hull
;
1846 dummy
= isl_basic_set_remove_equalities(
1847 isl_basic_set_copy(affine_hull
), &T
, &T2
);
1850 isl_basic_set_free(dummy
);
1851 set
= isl_set_preimage(set
, T
);
1852 convex_hull
= uset_convex_hull(set
);
1853 convex_hull
= isl_basic_set_preimage(convex_hull
, T2
);
1854 convex_hull
= isl_basic_set_intersect(convex_hull
, affine_hull
);
1857 isl_basic_set_free(affine_hull
);
1862 /* Compute the convex hull of a map.
1864 * The implementation was inspired by "Extended Convex Hull" by Fukuda et al.,
1865 * specifically, the wrapping of facets to obtain new facets.
1867 struct isl_basic_map
*isl_map_convex_hull(struct isl_map
*map
)
1869 struct isl_basic_set
*bset
;
1870 struct isl_basic_map
*model
= NULL
;
1871 struct isl_basic_set
*affine_hull
= NULL
;
1872 struct isl_basic_map
*convex_hull
= NULL
;
1873 struct isl_set
*set
= NULL
;
1874 struct isl_ctx
*ctx
;
1881 convex_hull
= isl_basic_map_empty_like_map(map
);
1886 map
= isl_map_detect_equalities(map
);
1887 map
= isl_map_align_divs(map
);
1888 model
= isl_basic_map_copy(map
->p
[0]);
1889 set
= isl_map_underlying_set(map
);
1893 affine_hull
= isl_set_affine_hull(isl_set_copy(set
));
1896 if (affine_hull
->n_eq
!= 0)
1897 bset
= modulo_affine_hull(set
, affine_hull
);
1899 isl_basic_set_free(affine_hull
);
1900 bset
= uset_convex_hull(set
);
1903 convex_hull
= isl_basic_map_overlying_set(bset
, model
);
1907 ISL_F_SET(convex_hull
, ISL_BASIC_MAP_NO_IMPLICIT
);
1908 ISL_F_SET(convex_hull
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1909 ISL_F_CLR(convex_hull
, ISL_BASIC_MAP_RATIONAL
);
1913 isl_basic_map_free(model
);
1917 struct isl_basic_set
*isl_set_convex_hull(struct isl_set
*set
)
1919 return (struct isl_basic_set
*)
1920 isl_map_convex_hull((struct isl_map
*)set
);
1923 struct sh_data_entry
{
1924 struct isl_hash_table
*table
;
1925 struct isl_tab
*tab
;
1928 /* Holds the data needed during the simple hull computation.
1930 * n the number of basic sets in the original set
1931 * hull_table a hash table of already computed constraints
1932 * in the simple hull
1933 * p for each basic set,
1934 * table a hash table of the constraints
1935 * tab the tableau corresponding to the basic set
1938 struct isl_ctx
*ctx
;
1940 struct isl_hash_table
*hull_table
;
1941 struct sh_data_entry p
[1];
1944 static void sh_data_free(struct sh_data
*data
)
1950 isl_hash_table_free(data
->ctx
, data
->hull_table
);
1951 for (i
= 0; i
< data
->n
; ++i
) {
1952 isl_hash_table_free(data
->ctx
, data
->p
[i
].table
);
1953 isl_tab_free(data
->p
[i
].tab
);
1958 struct ineq_cmp_data
{
1963 static int has_ineq(const void *entry
, const void *val
)
1965 isl_int
*row
= (isl_int
*)entry
;
1966 struct ineq_cmp_data
*v
= (struct ineq_cmp_data
*)val
;
1968 return isl_seq_eq(row
+ 1, v
->p
+ 1, v
->len
) ||
1969 isl_seq_is_neg(row
+ 1, v
->p
+ 1, v
->len
);
1972 static int hash_ineq(struct isl_ctx
*ctx
, struct isl_hash_table
*table
,
1973 isl_int
*ineq
, unsigned len
)
1976 struct ineq_cmp_data v
;
1977 struct isl_hash_table_entry
*entry
;
1981 c_hash
= isl_seq_get_hash(ineq
+ 1, len
);
1982 entry
= isl_hash_table_find(ctx
, table
, c_hash
, has_ineq
, &v
, 1);
1989 /* Fill hash table "table" with the constraints of "bset".
1990 * Equalities are added as two inequalities.
1991 * The value in the hash table is a pointer to the (in)equality of "bset".
1993 static int hash_basic_set(struct isl_hash_table
*table
,
1994 struct isl_basic_set
*bset
)
1997 unsigned dim
= isl_basic_set_total_dim(bset
);
1999 for (i
= 0; i
< bset
->n_eq
; ++i
) {
2000 for (j
= 0; j
< 2; ++j
) {
2001 isl_seq_neg(bset
->eq
[i
], bset
->eq
[i
], 1 + dim
);
2002 if (hash_ineq(bset
->ctx
, table
, bset
->eq
[i
], dim
) < 0)
2006 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
2007 if (hash_ineq(bset
->ctx
, table
, bset
->ineq
[i
], dim
) < 0)
2013 static struct sh_data
*sh_data_alloc(struct isl_set
*set
, unsigned n_ineq
)
2015 struct sh_data
*data
;
2018 data
= isl_calloc(set
->ctx
, struct sh_data
,
2019 sizeof(struct sh_data
) +
2020 (set
->n
- 1) * sizeof(struct sh_data_entry
));
2023 data
->ctx
= set
->ctx
;
2025 data
->hull_table
= isl_hash_table_alloc(set
->ctx
, n_ineq
);
2026 if (!data
->hull_table
)
2028 for (i
= 0; i
< set
->n
; ++i
) {
2029 data
->p
[i
].table
= isl_hash_table_alloc(set
->ctx
,
2030 2 * set
->p
[i
]->n_eq
+ set
->p
[i
]->n_ineq
);
2031 if (!data
->p
[i
].table
)
2033 if (hash_basic_set(data
->p
[i
].table
, set
->p
[i
]) < 0)
2042 /* Check if inequality "ineq" is a bound for basic set "j" or if
2043 * it can be relaxed (by increasing the constant term) to become
2044 * a bound for that basic set. In the latter case, the constant
2046 * Return 1 if "ineq" is a bound
2047 * 0 if "ineq" may attain arbitrarily small values on basic set "j"
2048 * -1 if some error occurred
2050 static int is_bound(struct sh_data
*data
, struct isl_set
*set
, int j
,
2053 enum isl_lp_result res
;
2056 if (!data
->p
[j
].tab
) {
2057 data
->p
[j
].tab
= isl_tab_from_basic_set(set
->p
[j
]);
2058 if (!data
->p
[j
].tab
)
2064 res
= isl_tab_min(data
->p
[j
].tab
, ineq
, data
->ctx
->one
,
2066 if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
2067 isl_int_sub(ineq
[0], ineq
[0], opt
);
2071 return (res
== isl_lp_ok
|| res
== isl_lp_empty
) ? 1 :
2072 res
== isl_lp_unbounded
? 0 : -1;
2075 /* Check if inequality "ineq" from basic set "i" can be relaxed to
2076 * become a bound on the whole set. If so, add the (relaxed) inequality
2079 * We first check if "hull" already contains a translate of the inequality.
2080 * If so, we are done.
2081 * Then, we check if any of the previous basic sets contains a translate
2082 * of the inequality. If so, then we have already considered this
2083 * inequality and we are done.
2084 * Otherwise, for each basic set other than "i", we check if the inequality
2085 * is a bound on the basic set.
2086 * For previous basic sets, we know that they do not contain a translate
2087 * of the inequality, so we directly call is_bound.
2088 * For following basic sets, we first check if a translate of the
2089 * inequality appears in its description and if so directly update
2090 * the inequality accordingly.
2092 static struct isl_basic_set
*add_bound(struct isl_basic_set
*hull
,
2093 struct sh_data
*data
, struct isl_set
*set
, int i
, isl_int
*ineq
)
2096 struct ineq_cmp_data v
;
2097 struct isl_hash_table_entry
*entry
;
2103 v
.len
= isl_basic_set_total_dim(hull
);
2105 c_hash
= isl_seq_get_hash(ineq
+ 1, v
.len
);
2107 entry
= isl_hash_table_find(hull
->ctx
, data
->hull_table
, c_hash
,
2112 for (j
= 0; j
< i
; ++j
) {
2113 entry
= isl_hash_table_find(hull
->ctx
, data
->p
[j
].table
,
2114 c_hash
, has_ineq
, &v
, 0);
2121 k
= isl_basic_set_alloc_inequality(hull
);
2122 isl_seq_cpy(hull
->ineq
[k
], ineq
, 1 + v
.len
);
2126 for (j
= 0; j
< i
; ++j
) {
2128 bound
= is_bound(data
, set
, j
, hull
->ineq
[k
]);
2135 isl_basic_set_free_inequality(hull
, 1);
2139 for (j
= i
+ 1; j
< set
->n
; ++j
) {
2142 entry
= isl_hash_table_find(hull
->ctx
, data
->p
[j
].table
,
2143 c_hash
, has_ineq
, &v
, 0);
2145 ineq_j
= entry
->data
;
2146 neg
= isl_seq_is_neg(ineq_j
+ 1,
2147 hull
->ineq
[k
] + 1, v
.len
);
2149 isl_int_neg(ineq_j
[0], ineq_j
[0]);
2150 if (isl_int_gt(ineq_j
[0], hull
->ineq
[k
][0]))
2151 isl_int_set(hull
->ineq
[k
][0], ineq_j
[0]);
2153 isl_int_neg(ineq_j
[0], ineq_j
[0]);
2156 bound
= is_bound(data
, set
, j
, hull
->ineq
[k
]);
2163 isl_basic_set_free_inequality(hull
, 1);
2167 entry
= isl_hash_table_find(hull
->ctx
, data
->hull_table
, c_hash
,
2171 entry
->data
= hull
->ineq
[k
];
2175 isl_basic_set_free(hull
);
2179 /* Check if any inequality from basic set "i" can be relaxed to
2180 * become a bound on the whole set. If so, add the (relaxed) inequality
2183 static struct isl_basic_set
*add_bounds(struct isl_basic_set
*bset
,
2184 struct sh_data
*data
, struct isl_set
*set
, int i
)
2187 unsigned dim
= isl_basic_set_total_dim(bset
);
2189 for (j
= 0; j
< set
->p
[i
]->n_eq
; ++j
) {
2190 for (k
= 0; k
< 2; ++k
) {
2191 isl_seq_neg(set
->p
[i
]->eq
[j
], set
->p
[i
]->eq
[j
], 1+dim
);
2192 bset
= add_bound(bset
, data
, set
, i
, set
->p
[i
]->eq
[j
]);
2195 for (j
= 0; j
< set
->p
[i
]->n_ineq
; ++j
)
2196 bset
= add_bound(bset
, data
, set
, i
, set
->p
[i
]->ineq
[j
]);
2200 /* Compute a superset of the convex hull of set that is described
2201 * by only translates of the constraints in the constituents of set.
2203 static struct isl_basic_set
*uset_simple_hull(struct isl_set
*set
)
2205 struct sh_data
*data
= NULL
;
2206 struct isl_basic_set
*hull
= NULL
;
2214 for (i
= 0; i
< set
->n
; ++i
) {
2217 n_ineq
+= 2 * set
->p
[i
]->n_eq
+ set
->p
[i
]->n_ineq
;
2220 hull
= isl_basic_set_alloc_dim(isl_dim_copy(set
->dim
), 0, 0, n_ineq
);
2224 data
= sh_data_alloc(set
, n_ineq
);
2228 for (i
= 0; i
< set
->n
; ++i
)
2229 hull
= add_bounds(hull
, data
, set
, i
);
2237 isl_basic_set_free(hull
);
2242 /* Compute a superset of the convex hull of map that is described
2243 * by only translates of the constraints in the constituents of map.
2245 struct isl_basic_map
*isl_map_simple_hull(struct isl_map
*map
)
2247 struct isl_set
*set
= NULL
;
2248 struct isl_basic_map
*model
= NULL
;
2249 struct isl_basic_map
*hull
;
2250 struct isl_basic_map
*affine_hull
;
2251 struct isl_basic_set
*bset
= NULL
;
2256 hull
= isl_basic_map_empty_like_map(map
);
2261 hull
= isl_basic_map_copy(map
->p
[0]);
2266 map
= isl_map_detect_equalities(map
);
2267 affine_hull
= isl_map_affine_hull(isl_map_copy(map
));
2268 map
= isl_map_align_divs(map
);
2269 model
= isl_basic_map_copy(map
->p
[0]);
2271 set
= isl_map_underlying_set(map
);
2273 bset
= uset_simple_hull(set
);
2275 hull
= isl_basic_map_overlying_set(bset
, model
);
2277 hull
= isl_basic_map_intersect(hull
, affine_hull
);
2278 hull
= isl_basic_map_convex_hull(hull
);
2279 ISL_F_SET(hull
, ISL_BASIC_MAP_NO_IMPLICIT
);
2280 ISL_F_SET(hull
, ISL_BASIC_MAP_ALL_EQUALITIES
);
2285 struct isl_basic_set
*isl_set_simple_hull(struct isl_set
*set
)
2287 return (struct isl_basic_set
*)
2288 isl_map_simple_hull((struct isl_map
*)set
);
2291 /* Given a set "set", return parametric bounds on the dimension "dim".
2293 static struct isl_basic_set
*set_bounds(struct isl_set
*set
, int dim
)
2295 unsigned set_dim
= isl_set_dim(set
, isl_dim_set
);
2296 set
= isl_set_copy(set
);
2297 set
= isl_set_eliminate_dims(set
, dim
+ 1, set_dim
- (dim
+ 1));
2298 set
= isl_set_eliminate_dims(set
, 0, dim
);
2299 return isl_set_convex_hull(set
);
2302 /* Computes a "simple hull" and then check if each dimension in the
2303 * resulting hull is bounded by a symbolic constant. If not, the
2304 * hull is intersected with the corresponding bounds on the whole set.
2306 struct isl_basic_set
*isl_set_bounded_simple_hull(struct isl_set
*set
)
2309 struct isl_basic_set
*hull
;
2310 unsigned nparam
, left
;
2311 int removed_divs
= 0;
2313 hull
= isl_set_simple_hull(isl_set_copy(set
));
2317 nparam
= isl_basic_set_dim(hull
, isl_dim_param
);
2318 for (i
= 0; i
< isl_basic_set_dim(hull
, isl_dim_set
); ++i
) {
2319 int lower
= 0, upper
= 0;
2320 struct isl_basic_set
*bounds
;
2322 left
= isl_basic_set_total_dim(hull
) - nparam
- i
- 1;
2323 for (j
= 0; j
< hull
->n_eq
; ++j
) {
2324 if (isl_int_is_zero(hull
->eq
[j
][1 + nparam
+ i
]))
2326 if (isl_seq_first_non_zero(hull
->eq
[j
]+1+nparam
+i
+1,
2333 for (j
= 0; j
< hull
->n_ineq
; ++j
) {
2334 if (isl_int_is_zero(hull
->ineq
[j
][1 + nparam
+ i
]))
2336 if (isl_seq_first_non_zero(hull
->ineq
[j
]+1+nparam
+i
+1,
2338 isl_seq_first_non_zero(hull
->ineq
[j
]+1+nparam
,
2341 if (isl_int_is_pos(hull
->ineq
[j
][1 + nparam
+ i
]))
2352 if (!removed_divs
) {
2353 set
= isl_set_remove_divs(set
);
2358 bounds
= set_bounds(set
, i
);
2359 hull
= isl_basic_set_intersect(hull
, bounds
);