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
);
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 __isl_give isl_basic_map
*isl_basic_map_remove_redundancies(
91 __isl_take isl_basic_map
*bmap
)
98 bmap
= isl_basic_map_gauss(bmap
, NULL
);
99 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
101 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
))
103 if (bmap
->n_ineq
<= 1)
106 tab
= isl_tab_from_basic_map(bmap
);
107 if (isl_tab_detect_implicit_equalities(tab
) < 0)
109 if (isl_tab_detect_redundant(tab
) < 0)
111 bmap
= isl_basic_map_update_from_tab(bmap
, tab
);
113 ISL_F_SET(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
114 ISL_F_SET(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
118 isl_basic_map_free(bmap
);
122 __isl_give isl_basic_set
*isl_basic_set_remove_redundancies(
123 __isl_take isl_basic_set
*bset
)
125 return (struct isl_basic_set
*)
126 isl_basic_map_remove_redundancies((struct isl_basic_map
*)bset
);
129 /* Check if the set set is bound in the direction of the affine
130 * constraint c and if so, set the constant term such that the
131 * resulting constraint is a bounding constraint for the set.
133 static int uset_is_bound(struct isl_set
*set
, isl_int
*c
, unsigned len
)
141 isl_int_init(opt_denom
);
143 for (j
= 0; j
< set
->n
; ++j
) {
144 enum isl_lp_result res
;
146 if (ISL_F_ISSET(set
->p
[j
], ISL_BASIC_SET_EMPTY
))
149 res
= isl_basic_set_solve_lp(set
->p
[j
],
150 0, c
, set
->ctx
->one
, &opt
, &opt_denom
, NULL
);
151 if (res
== isl_lp_unbounded
)
153 if (res
== isl_lp_error
)
155 if (res
== isl_lp_empty
) {
156 set
->p
[j
] = isl_basic_set_set_to_empty(set
->p
[j
]);
161 if (first
|| isl_int_is_neg(opt
)) {
162 if (!isl_int_is_one(opt_denom
))
163 isl_seq_scale(c
, c
, opt_denom
, len
);
164 isl_int_sub(c
[0], c
[0], opt
);
169 isl_int_clear(opt_denom
);
173 isl_int_clear(opt_denom
);
177 struct isl_basic_set
*isl_basic_set_set_rational(struct isl_basic_set
*bset
)
182 if (ISL_F_ISSET(bset
, ISL_BASIC_MAP_RATIONAL
))
185 bset
= isl_basic_set_cow(bset
);
189 ISL_F_SET(bset
, ISL_BASIC_MAP_RATIONAL
);
191 return isl_basic_set_finalize(bset
);
194 static struct isl_set
*isl_set_set_rational(struct isl_set
*set
)
198 set
= isl_set_cow(set
);
201 for (i
= 0; i
< set
->n
; ++i
) {
202 set
->p
[i
] = isl_basic_set_set_rational(set
->p
[i
]);
212 static struct isl_basic_set
*isl_basic_set_add_equality(
213 struct isl_basic_set
*bset
, isl_int
*c
)
221 if (ISL_F_ISSET(bset
, ISL_BASIC_SET_EMPTY
))
224 isl_assert(bset
->ctx
, isl_basic_set_n_param(bset
) == 0, goto error
);
225 isl_assert(bset
->ctx
, bset
->n_div
== 0, goto error
);
226 dim
= isl_basic_set_n_dim(bset
);
227 bset
= isl_basic_set_cow(bset
);
228 bset
= isl_basic_set_extend(bset
, 0, dim
, 0, 1, 0);
229 i
= isl_basic_set_alloc_equality(bset
);
232 isl_seq_cpy(bset
->eq
[i
], c
, 1 + dim
);
235 isl_basic_set_free(bset
);
239 static struct isl_set
*isl_set_add_basic_set_equality(struct isl_set
*set
, isl_int
*c
)
243 set
= isl_set_cow(set
);
246 for (i
= 0; i
< set
->n
; ++i
) {
247 set
->p
[i
] = isl_basic_set_add_equality(set
->p
[i
], c
);
257 /* Given a union of basic sets, construct the constraints for wrapping
258 * a facet around one of its ridges.
259 * In particular, if each of n the d-dimensional basic sets i in "set"
260 * contains the origin, satisfies the constraints x_1 >= 0 and x_2 >= 0
261 * and is defined by the constraints
265 * then the resulting set is of dimension n*(1+d) and has as constraints
274 static struct isl_basic_set
*wrap_constraints(struct isl_set
*set
)
276 struct isl_basic_set
*lp
;
280 unsigned dim
, lp_dim
;
285 dim
= 1 + isl_set_n_dim(set
);
288 for (i
= 0; i
< set
->n
; ++i
) {
289 n_eq
+= set
->p
[i
]->n_eq
;
290 n_ineq
+= set
->p
[i
]->n_ineq
;
292 lp
= isl_basic_set_alloc(set
->ctx
, 0, dim
* set
->n
, 0, n_eq
, n_ineq
);
295 lp_dim
= isl_basic_set_n_dim(lp
);
296 k
= isl_basic_set_alloc_equality(lp
);
297 isl_int_set_si(lp
->eq
[k
][0], -1);
298 for (i
= 0; i
< set
->n
; ++i
) {
299 isl_int_set_si(lp
->eq
[k
][1+dim
*i
], 0);
300 isl_int_set_si(lp
->eq
[k
][1+dim
*i
+1], 1);
301 isl_seq_clr(lp
->eq
[k
]+1+dim
*i
+2, dim
-2);
303 for (i
= 0; i
< set
->n
; ++i
) {
304 k
= isl_basic_set_alloc_inequality(lp
);
305 isl_seq_clr(lp
->ineq
[k
], 1+lp_dim
);
306 isl_int_set_si(lp
->ineq
[k
][1+dim
*i
], 1);
308 for (j
= 0; j
< set
->p
[i
]->n_eq
; ++j
) {
309 k
= isl_basic_set_alloc_equality(lp
);
310 isl_seq_clr(lp
->eq
[k
], 1+dim
*i
);
311 isl_seq_cpy(lp
->eq
[k
]+1+dim
*i
, set
->p
[i
]->eq
[j
], dim
);
312 isl_seq_clr(lp
->eq
[k
]+1+dim
*(i
+1), dim
*(set
->n
-i
-1));
315 for (j
= 0; j
< set
->p
[i
]->n_ineq
; ++j
) {
316 k
= isl_basic_set_alloc_inequality(lp
);
317 isl_seq_clr(lp
->ineq
[k
], 1+dim
*i
);
318 isl_seq_cpy(lp
->ineq
[k
]+1+dim
*i
, set
->p
[i
]->ineq
[j
], dim
);
319 isl_seq_clr(lp
->ineq
[k
]+1+dim
*(i
+1), dim
*(set
->n
-i
-1));
325 /* Given a facet "facet" of the convex hull of "set" and a facet "ridge"
326 * of that facet, compute the other facet of the convex hull that contains
329 * We first transform the set such that the facet constraint becomes
333 * I.e., the facet lies in
337 * and on that facet, the constraint that defines the ridge is
341 * (This transformation is not strictly needed, all that is needed is
342 * that the ridge contains the origin.)
344 * Since the ridge contains the origin, the cone of the convex hull
345 * will be of the form
350 * with this second constraint defining the new facet.
351 * The constant a is obtained by settting x_1 in the cone of the
352 * convex hull to 1 and minimizing x_2.
353 * Now, each element in the cone of the convex hull is the sum
354 * of elements in the cones of the basic sets.
355 * If a_i is the dilation factor of basic set i, then the problem
356 * we need to solve is
369 * the constraints of each (transformed) basic set.
370 * If a = n/d, then the constraint defining the new facet (in the transformed
373 * -n x_1 + d x_2 >= 0
375 * In the original space, we need to take the same combination of the
376 * corresponding constraints "facet" and "ridge".
378 * If a = -infty = "-1/0", then we just return the original facet constraint.
379 * This means that the facet is unbounded, but has a bounded intersection
380 * with the union of sets.
382 isl_int
*isl_set_wrap_facet(__isl_keep isl_set
*set
,
383 isl_int
*facet
, isl_int
*ridge
)
387 struct isl_mat
*T
= NULL
;
388 struct isl_basic_set
*lp
= NULL
;
390 enum isl_lp_result res
;
397 set
= isl_set_copy(set
);
398 set
= isl_set_set_rational(set
);
400 dim
= 1 + isl_set_n_dim(set
);
401 T
= isl_mat_alloc(ctx
, 3, dim
);
404 isl_int_set_si(T
->row
[0][0], 1);
405 isl_seq_clr(T
->row
[0]+1, dim
- 1);
406 isl_seq_cpy(T
->row
[1], facet
, dim
);
407 isl_seq_cpy(T
->row
[2], ridge
, dim
);
408 T
= isl_mat_right_inverse(T
);
409 set
= isl_set_preimage(set
, T
);
413 lp
= wrap_constraints(set
);
414 obj
= isl_vec_alloc(ctx
, 1 + dim
*set
->n
);
417 isl_int_set_si(obj
->block
.data
[0], 0);
418 for (i
= 0; i
< set
->n
; ++i
) {
419 isl_seq_clr(obj
->block
.data
+ 1 + dim
*i
, 2);
420 isl_int_set_si(obj
->block
.data
[1 + dim
*i
+2], 1);
421 isl_seq_clr(obj
->block
.data
+ 1 + dim
*i
+3, dim
-3);
425 res
= isl_basic_set_solve_lp(lp
, 0,
426 obj
->block
.data
, ctx
->one
, &num
, &den
, NULL
);
427 if (res
== isl_lp_ok
) {
428 isl_int_neg(num
, num
);
429 isl_seq_combine(facet
, num
, facet
, den
, ridge
, dim
);
434 isl_basic_set_free(lp
);
436 if (res
== isl_lp_error
)
438 isl_assert(ctx
, res
== isl_lp_ok
|| res
== isl_lp_unbounded
,
442 isl_basic_set_free(lp
);
448 /* Compute the constraint of a facet of "set".
450 * We first compute the intersection with a bounding constraint
451 * that is orthogonal to one of the coordinate axes.
452 * If the affine hull of this intersection has only one equality,
453 * we have found a facet.
454 * Otherwise, we wrap the current bounding constraint around
455 * one of the equalities of the face (one that is not equal to
456 * the current bounding constraint).
457 * This process continues until we have found a facet.
458 * The dimension of the intersection increases by at least
459 * one on each iteration, so termination is guaranteed.
461 static __isl_give isl_mat
*initial_facet_constraint(__isl_keep isl_set
*set
)
463 struct isl_set
*slice
= NULL
;
464 struct isl_basic_set
*face
= NULL
;
466 unsigned dim
= isl_set_n_dim(set
);
470 isl_assert(set
->ctx
, set
->n
> 0, goto error
);
471 bounds
= isl_mat_alloc(set
->ctx
, 1, 1 + dim
);
475 isl_seq_clr(bounds
->row
[0], dim
);
476 isl_int_set_si(bounds
->row
[0][1 + dim
- 1], 1);
477 is_bound
= uset_is_bound(set
, bounds
->row
[0], 1 + dim
);
480 isl_assert(set
->ctx
, is_bound
, goto error
);
481 isl_seq_normalize(set
->ctx
, bounds
->row
[0], 1 + dim
);
485 slice
= isl_set_copy(set
);
486 slice
= isl_set_add_basic_set_equality(slice
, bounds
->row
[0]);
487 face
= isl_set_affine_hull(slice
);
490 if (face
->n_eq
== 1) {
491 isl_basic_set_free(face
);
494 for (i
= 0; i
< face
->n_eq
; ++i
)
495 if (!isl_seq_eq(bounds
->row
[0], face
->eq
[i
], 1 + dim
) &&
496 !isl_seq_is_neg(bounds
->row
[0],
497 face
->eq
[i
], 1 + dim
))
499 isl_assert(set
->ctx
, i
< face
->n_eq
, goto error
);
500 if (!isl_set_wrap_facet(set
, bounds
->row
[0], face
->eq
[i
]))
502 isl_seq_normalize(set
->ctx
, bounds
->row
[0], bounds
->n_col
);
503 isl_basic_set_free(face
);
508 isl_basic_set_free(face
);
509 isl_mat_free(bounds
);
513 /* Given the bounding constraint "c" of a facet of the convex hull of "set",
514 * compute a hyperplane description of the facet, i.e., compute the facets
517 * We compute an affine transformation that transforms the constraint
526 * by computing the right inverse U of a matrix that starts with the rows
539 * Since z_1 is zero, we can drop this variable as well as the corresponding
540 * column of U to obtain
548 * with Q' equal to Q, but without the corresponding row.
549 * After computing the facets of the facet in the z' space,
550 * we convert them back to the x space through Q.
552 static struct isl_basic_set
*compute_facet(struct isl_set
*set
, isl_int
*c
)
554 struct isl_mat
*m
, *U
, *Q
;
555 struct isl_basic_set
*facet
= NULL
;
560 set
= isl_set_copy(set
);
561 dim
= isl_set_n_dim(set
);
562 m
= isl_mat_alloc(set
->ctx
, 2, 1 + dim
);
565 isl_int_set_si(m
->row
[0][0], 1);
566 isl_seq_clr(m
->row
[0]+1, dim
);
567 isl_seq_cpy(m
->row
[1], c
, 1+dim
);
568 U
= isl_mat_right_inverse(m
);
569 Q
= isl_mat_right_inverse(isl_mat_copy(U
));
570 U
= isl_mat_drop_cols(U
, 1, 1);
571 Q
= isl_mat_drop_rows(Q
, 1, 1);
572 set
= isl_set_preimage(set
, U
);
573 facet
= uset_convex_hull_wrap_bounded(set
);
574 facet
= isl_basic_set_preimage(facet
, Q
);
576 isl_assert(ctx
, facet
->n_eq
== 0, goto error
);
579 isl_basic_set_free(facet
);
584 /* Given an initial facet constraint, compute the remaining facets.
585 * We do this by running through all facets found so far and computing
586 * the adjacent facets through wrapping, adding those facets that we
587 * hadn't already found before.
589 * For each facet we have found so far, we first compute its facets
590 * in the resulting convex hull. That is, we compute the ridges
591 * of the resulting convex hull contained in the facet.
592 * We also compute the corresponding facet in the current approximation
593 * of the convex hull. There is no need to wrap around the ridges
594 * in this facet since that would result in a facet that is already
595 * present in the current approximation.
597 * This function can still be significantly optimized by checking which of
598 * the facets of the basic sets are also facets of the convex hull and
599 * using all the facets so far to help in constructing the facets of the
602 * using the technique in section "3.1 Ridge Generation" of
603 * "Extended Convex Hull" by Fukuda et al.
605 static struct isl_basic_set
*extend(struct isl_basic_set
*hull
,
610 struct isl_basic_set
*facet
= NULL
;
611 struct isl_basic_set
*hull_facet
= NULL
;
617 isl_assert(set
->ctx
, set
->n
> 0, goto error
);
619 dim
= isl_set_n_dim(set
);
621 for (i
= 0; i
< hull
->n_ineq
; ++i
) {
622 facet
= compute_facet(set
, hull
->ineq
[i
]);
623 facet
= isl_basic_set_add_equality(facet
, hull
->ineq
[i
]);
624 facet
= isl_basic_set_gauss(facet
, NULL
);
625 facet
= isl_basic_set_normalize_constraints(facet
);
626 hull_facet
= isl_basic_set_copy(hull
);
627 hull_facet
= isl_basic_set_add_equality(hull_facet
, hull
->ineq
[i
]);
628 hull_facet
= isl_basic_set_gauss(hull_facet
, NULL
);
629 hull_facet
= isl_basic_set_normalize_constraints(hull_facet
);
630 if (!facet
|| !hull_facet
)
632 hull
= isl_basic_set_cow(hull
);
633 hull
= isl_basic_set_extend_dim(hull
,
634 isl_dim_copy(hull
->dim
), 0, 0, facet
->n_ineq
);
637 for (j
= 0; j
< facet
->n_ineq
; ++j
) {
638 for (f
= 0; f
< hull_facet
->n_ineq
; ++f
)
639 if (isl_seq_eq(facet
->ineq
[j
],
640 hull_facet
->ineq
[f
], 1 + dim
))
642 if (f
< hull_facet
->n_ineq
)
644 k
= isl_basic_set_alloc_inequality(hull
);
647 isl_seq_cpy(hull
->ineq
[k
], hull
->ineq
[i
], 1+dim
);
648 if (!isl_set_wrap_facet(set
, hull
->ineq
[k
], facet
->ineq
[j
]))
651 isl_basic_set_free(hull_facet
);
652 isl_basic_set_free(facet
);
654 hull
= isl_basic_set_simplify(hull
);
655 hull
= isl_basic_set_finalize(hull
);
658 isl_basic_set_free(hull_facet
);
659 isl_basic_set_free(facet
);
660 isl_basic_set_free(hull
);
664 /* Special case for computing the convex hull of a one dimensional set.
665 * We simply collect the lower and upper bounds of each basic set
666 * and the biggest of those.
668 static struct isl_basic_set
*convex_hull_1d(struct isl_set
*set
)
670 struct isl_mat
*c
= NULL
;
671 isl_int
*lower
= NULL
;
672 isl_int
*upper
= NULL
;
675 struct isl_basic_set
*hull
;
677 for (i
= 0; i
< set
->n
; ++i
) {
678 set
->p
[i
] = isl_basic_set_simplify(set
->p
[i
]);
682 set
= isl_set_remove_empty_parts(set
);
685 isl_assert(set
->ctx
, set
->n
> 0, goto error
);
686 c
= isl_mat_alloc(set
->ctx
, 2, 2);
690 if (set
->p
[0]->n_eq
> 0) {
691 isl_assert(set
->ctx
, set
->p
[0]->n_eq
== 1, goto error
);
694 if (isl_int_is_pos(set
->p
[0]->eq
[0][1])) {
695 isl_seq_cpy(lower
, set
->p
[0]->eq
[0], 2);
696 isl_seq_neg(upper
, set
->p
[0]->eq
[0], 2);
698 isl_seq_neg(lower
, set
->p
[0]->eq
[0], 2);
699 isl_seq_cpy(upper
, set
->p
[0]->eq
[0], 2);
702 for (j
= 0; j
< set
->p
[0]->n_ineq
; ++j
) {
703 if (isl_int_is_pos(set
->p
[0]->ineq
[j
][1])) {
705 isl_seq_cpy(lower
, set
->p
[0]->ineq
[j
], 2);
708 isl_seq_cpy(upper
, set
->p
[0]->ineq
[j
], 2);
715 for (i
= 0; i
< set
->n
; ++i
) {
716 struct isl_basic_set
*bset
= set
->p
[i
];
720 for (j
= 0; j
< bset
->n_eq
; ++j
) {
724 isl_int_mul(a
, lower
[0], bset
->eq
[j
][1]);
725 isl_int_mul(b
, lower
[1], bset
->eq
[j
][0]);
726 if (isl_int_lt(a
, b
) && isl_int_is_pos(bset
->eq
[j
][1]))
727 isl_seq_cpy(lower
, bset
->eq
[j
], 2);
728 if (isl_int_gt(a
, b
) && isl_int_is_neg(bset
->eq
[j
][1]))
729 isl_seq_neg(lower
, bset
->eq
[j
], 2);
732 isl_int_mul(a
, upper
[0], bset
->eq
[j
][1]);
733 isl_int_mul(b
, upper
[1], bset
->eq
[j
][0]);
734 if (isl_int_lt(a
, b
) && isl_int_is_pos(bset
->eq
[j
][1]))
735 isl_seq_neg(upper
, bset
->eq
[j
], 2);
736 if (isl_int_gt(a
, b
) && isl_int_is_neg(bset
->eq
[j
][1]))
737 isl_seq_cpy(upper
, bset
->eq
[j
], 2);
740 for (j
= 0; j
< bset
->n_ineq
; ++j
) {
741 if (isl_int_is_pos(bset
->ineq
[j
][1]))
743 if (isl_int_is_neg(bset
->ineq
[j
][1]))
745 if (lower
&& isl_int_is_pos(bset
->ineq
[j
][1])) {
746 isl_int_mul(a
, lower
[0], bset
->ineq
[j
][1]);
747 isl_int_mul(b
, lower
[1], bset
->ineq
[j
][0]);
748 if (isl_int_lt(a
, b
))
749 isl_seq_cpy(lower
, bset
->ineq
[j
], 2);
751 if (upper
&& isl_int_is_neg(bset
->ineq
[j
][1])) {
752 isl_int_mul(a
, upper
[0], bset
->ineq
[j
][1]);
753 isl_int_mul(b
, upper
[1], bset
->ineq
[j
][0]);
754 if (isl_int_gt(a
, b
))
755 isl_seq_cpy(upper
, bset
->ineq
[j
], 2);
766 hull
= isl_basic_set_alloc(set
->ctx
, 0, 1, 0, 0, 2);
767 hull
= isl_basic_set_set_rational(hull
);
771 k
= isl_basic_set_alloc_inequality(hull
);
772 isl_seq_cpy(hull
->ineq
[k
], lower
, 2);
775 k
= isl_basic_set_alloc_inequality(hull
);
776 isl_seq_cpy(hull
->ineq
[k
], upper
, 2);
778 hull
= isl_basic_set_finalize(hull
);
788 /* Project out final n dimensions using Fourier-Motzkin */
789 static struct isl_set
*set_project_out(struct isl_ctx
*ctx
,
790 struct isl_set
*set
, unsigned n
)
792 return isl_set_remove_dims(set
, isl_set_n_dim(set
) - n
, n
);
795 static struct isl_basic_set
*convex_hull_0d(struct isl_set
*set
)
797 struct isl_basic_set
*convex_hull
;
802 if (isl_set_is_empty(set
))
803 convex_hull
= isl_basic_set_empty(isl_dim_copy(set
->dim
));
805 convex_hull
= isl_basic_set_universe(isl_dim_copy(set
->dim
));
810 /* Compute the convex hull of a pair of basic sets without any parameters or
811 * integer divisions using Fourier-Motzkin elimination.
812 * The convex hull is the set of all points that can be written as
813 * the sum of points from both basic sets (in homogeneous coordinates).
814 * We set up the constraints in a space with dimensions for each of
815 * the three sets and then project out the dimensions corresponding
816 * to the two original basic sets, retaining only those corresponding
817 * to the convex hull.
819 static struct isl_basic_set
*convex_hull_pair_elim(struct isl_basic_set
*bset1
,
820 struct isl_basic_set
*bset2
)
823 struct isl_basic_set
*bset
[2];
824 struct isl_basic_set
*hull
= NULL
;
827 if (!bset1
|| !bset2
)
830 dim
= isl_basic_set_n_dim(bset1
);
831 hull
= isl_basic_set_alloc(bset1
->ctx
, 0, 2 + 3 * dim
, 0,
832 1 + dim
+ bset1
->n_eq
+ bset2
->n_eq
,
833 2 + bset1
->n_ineq
+ bset2
->n_ineq
);
836 for (i
= 0; i
< 2; ++i
) {
837 for (j
= 0; j
< bset
[i
]->n_eq
; ++j
) {
838 k
= isl_basic_set_alloc_equality(hull
);
841 isl_seq_clr(hull
->eq
[k
], (i
+1) * (1+dim
));
842 isl_seq_clr(hull
->eq
[k
]+(i
+2)*(1+dim
), (1-i
)*(1+dim
));
843 isl_seq_cpy(hull
->eq
[k
]+(i
+1)*(1+dim
), bset
[i
]->eq
[j
],
846 for (j
= 0; j
< bset
[i
]->n_ineq
; ++j
) {
847 k
= isl_basic_set_alloc_inequality(hull
);
850 isl_seq_clr(hull
->ineq
[k
], (i
+1) * (1+dim
));
851 isl_seq_clr(hull
->ineq
[k
]+(i
+2)*(1+dim
), (1-i
)*(1+dim
));
852 isl_seq_cpy(hull
->ineq
[k
]+(i
+1)*(1+dim
),
853 bset
[i
]->ineq
[j
], 1+dim
);
855 k
= isl_basic_set_alloc_inequality(hull
);
858 isl_seq_clr(hull
->ineq
[k
], 1+2+3*dim
);
859 isl_int_set_si(hull
->ineq
[k
][(i
+1)*(1+dim
)], 1);
861 for (j
= 0; j
< 1+dim
; ++j
) {
862 k
= isl_basic_set_alloc_equality(hull
);
865 isl_seq_clr(hull
->eq
[k
], 1+2+3*dim
);
866 isl_int_set_si(hull
->eq
[k
][j
], -1);
867 isl_int_set_si(hull
->eq
[k
][1+dim
+j
], 1);
868 isl_int_set_si(hull
->eq
[k
][2*(1+dim
)+j
], 1);
870 hull
= isl_basic_set_set_rational(hull
);
871 hull
= isl_basic_set_remove_dims(hull
, dim
, 2*(1+dim
));
872 hull
= isl_basic_set_remove_redundancies(hull
);
873 isl_basic_set_free(bset1
);
874 isl_basic_set_free(bset2
);
877 isl_basic_set_free(bset1
);
878 isl_basic_set_free(bset2
);
879 isl_basic_set_free(hull
);
883 /* Is the set bounded for each value of the parameters?
885 int isl_basic_set_is_bounded(__isl_keep isl_basic_set
*bset
)
892 if (isl_basic_set_fast_is_empty(bset
))
895 tab
= isl_tab_from_recession_cone(bset
, 1);
896 bounded
= isl_tab_cone_is_bounded(tab
);
901 /* Is the set bounded for each value of the parameters?
903 int isl_set_is_bounded(__isl_keep isl_set
*set
)
910 for (i
= 0; i
< set
->n
; ++i
) {
911 int bounded
= isl_basic_set_is_bounded(set
->p
[i
]);
912 if (!bounded
|| bounded
< 0)
918 /* Compute the lineality space of the convex hull of bset1 and bset2.
920 * We first compute the intersection of the recession cone of bset1
921 * with the negative of the recession cone of bset2 and then compute
922 * the linear hull of the resulting cone.
924 static struct isl_basic_set
*induced_lineality_space(
925 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
928 struct isl_basic_set
*lin
= NULL
;
931 if (!bset1
|| !bset2
)
934 dim
= isl_basic_set_total_dim(bset1
);
935 lin
= isl_basic_set_alloc_dim(isl_basic_set_get_dim(bset1
), 0,
936 bset1
->n_eq
+ bset2
->n_eq
,
937 bset1
->n_ineq
+ bset2
->n_ineq
);
938 lin
= isl_basic_set_set_rational(lin
);
941 for (i
= 0; i
< bset1
->n_eq
; ++i
) {
942 k
= isl_basic_set_alloc_equality(lin
);
945 isl_int_set_si(lin
->eq
[k
][0], 0);
946 isl_seq_cpy(lin
->eq
[k
] + 1, bset1
->eq
[i
] + 1, dim
);
948 for (i
= 0; i
< bset1
->n_ineq
; ++i
) {
949 k
= isl_basic_set_alloc_inequality(lin
);
952 isl_int_set_si(lin
->ineq
[k
][0], 0);
953 isl_seq_cpy(lin
->ineq
[k
] + 1, bset1
->ineq
[i
] + 1, dim
);
955 for (i
= 0; i
< bset2
->n_eq
; ++i
) {
956 k
= isl_basic_set_alloc_equality(lin
);
959 isl_int_set_si(lin
->eq
[k
][0], 0);
960 isl_seq_neg(lin
->eq
[k
] + 1, bset2
->eq
[i
] + 1, dim
);
962 for (i
= 0; i
< bset2
->n_ineq
; ++i
) {
963 k
= isl_basic_set_alloc_inequality(lin
);
966 isl_int_set_si(lin
->ineq
[k
][0], 0);
967 isl_seq_neg(lin
->ineq
[k
] + 1, bset2
->ineq
[i
] + 1, dim
);
970 isl_basic_set_free(bset1
);
971 isl_basic_set_free(bset2
);
972 return isl_basic_set_affine_hull(lin
);
974 isl_basic_set_free(lin
);
975 isl_basic_set_free(bset1
);
976 isl_basic_set_free(bset2
);
980 static struct isl_basic_set
*uset_convex_hull(struct isl_set
*set
);
982 /* Given a set and a linear space "lin" of dimension n > 0,
983 * project the linear space from the set, compute the convex hull
984 * and then map the set back to the original space.
990 * describe the linear space. We first compute the Hermite normal
991 * form H = M U of M = H Q, to obtain
995 * The last n rows of H will be zero, so the last n variables of x' = Q x
996 * are the one we want to project out. We do this by transforming each
997 * basic set A x >= b to A U x' >= b and then removing the last n dimensions.
998 * After computing the convex hull in x'_1, i.e., A' x'_1 >= b',
999 * we transform the hull back to the original space as A' Q_1 x >= b',
1000 * with Q_1 all but the last n rows of Q.
1002 static struct isl_basic_set
*modulo_lineality(struct isl_set
*set
,
1003 struct isl_basic_set
*lin
)
1005 unsigned total
= isl_basic_set_total_dim(lin
);
1007 struct isl_basic_set
*hull
;
1008 struct isl_mat
*M
, *U
, *Q
;
1012 lin_dim
= total
- lin
->n_eq
;
1013 M
= isl_mat_sub_alloc(set
->ctx
, lin
->eq
, 0, lin
->n_eq
, 1, total
);
1014 M
= isl_mat_left_hermite(M
, 0, &U
, &Q
);
1018 isl_basic_set_free(lin
);
1020 Q
= isl_mat_drop_rows(Q
, Q
->n_row
- lin_dim
, lin_dim
);
1022 U
= isl_mat_lin_to_aff(U
);
1023 Q
= isl_mat_lin_to_aff(Q
);
1025 set
= isl_set_preimage(set
, U
);
1026 set
= isl_set_remove_dims(set
, total
- lin_dim
, lin_dim
);
1027 hull
= uset_convex_hull(set
);
1028 hull
= isl_basic_set_preimage(hull
, Q
);
1032 isl_basic_set_free(lin
);
1037 /* Given two polyhedra with as constraints h_{ij} x >= 0 in homegeneous space,
1038 * set up an LP for solving
1040 * \sum_j \alpha_{1j} h_{1j} = \sum_j \alpha_{2j} h_{2j}
1042 * \alpha{i0} corresponds to the (implicit) positivity constraint 1 >= 0
1043 * The next \alpha{ij} correspond to the equalities and come in pairs.
1044 * The final \alpha{ij} correspond to the inequalities.
1046 static struct isl_basic_set
*valid_direction_lp(
1047 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
1049 struct isl_dim
*dim
;
1050 struct isl_basic_set
*lp
;
1055 if (!bset1
|| !bset2
)
1057 d
= 1 + isl_basic_set_total_dim(bset1
);
1059 2 * bset1
->n_eq
+ bset1
->n_ineq
+ 2 * bset2
->n_eq
+ bset2
->n_ineq
;
1060 dim
= isl_dim_set_alloc(bset1
->ctx
, 0, n
);
1061 lp
= isl_basic_set_alloc_dim(dim
, 0, d
, n
);
1064 for (i
= 0; i
< n
; ++i
) {
1065 k
= isl_basic_set_alloc_inequality(lp
);
1068 isl_seq_clr(lp
->ineq
[k
] + 1, n
);
1069 isl_int_set_si(lp
->ineq
[k
][0], -1);
1070 isl_int_set_si(lp
->ineq
[k
][1 + i
], 1);
1072 for (i
= 0; i
< d
; ++i
) {
1073 k
= isl_basic_set_alloc_equality(lp
);
1077 isl_int_set_si(lp
->eq
[k
][n
], 0); n
++;
1078 /* positivity constraint 1 >= 0 */
1079 isl_int_set_si(lp
->eq
[k
][n
], i
== 0); n
++;
1080 for (j
= 0; j
< bset1
->n_eq
; ++j
) {
1081 isl_int_set(lp
->eq
[k
][n
], bset1
->eq
[j
][i
]); n
++;
1082 isl_int_neg(lp
->eq
[k
][n
], bset1
->eq
[j
][i
]); n
++;
1084 for (j
= 0; j
< bset1
->n_ineq
; ++j
) {
1085 isl_int_set(lp
->eq
[k
][n
], bset1
->ineq
[j
][i
]); n
++;
1087 /* positivity constraint 1 >= 0 */
1088 isl_int_set_si(lp
->eq
[k
][n
], -(i
== 0)); n
++;
1089 for (j
= 0; j
< bset2
->n_eq
; ++j
) {
1090 isl_int_neg(lp
->eq
[k
][n
], bset2
->eq
[j
][i
]); n
++;
1091 isl_int_set(lp
->eq
[k
][n
], bset2
->eq
[j
][i
]); n
++;
1093 for (j
= 0; j
< bset2
->n_ineq
; ++j
) {
1094 isl_int_neg(lp
->eq
[k
][n
], bset2
->ineq
[j
][i
]); n
++;
1097 lp
= isl_basic_set_gauss(lp
, NULL
);
1098 isl_basic_set_free(bset1
);
1099 isl_basic_set_free(bset2
);
1102 isl_basic_set_free(bset1
);
1103 isl_basic_set_free(bset2
);
1107 /* Compute a vector s in the homogeneous space such that <s, r> > 0
1108 * for all rays in the homogeneous space of the two cones that correspond
1109 * to the input polyhedra bset1 and bset2.
1111 * We compute s as a vector that satisfies
1113 * s = \sum_j \alpha_{ij} h_{ij} for i = 1,2 (*)
1115 * with h_{ij} the normals of the facets of polyhedron i
1116 * (including the "positivity constraint" 1 >= 0) and \alpha_{ij}
1117 * strictly positive numbers. For simplicity we impose \alpha_{ij} >= 1.
1118 * We first set up an LP with as variables the \alpha{ij}.
1119 * In this formulation, for each polyhedron i,
1120 * the first constraint is the positivity constraint, followed by pairs
1121 * of variables for the equalities, followed by variables for the inequalities.
1122 * We then simply pick a feasible solution and compute s using (*).
1124 * Note that we simply pick any valid direction and make no attempt
1125 * to pick a "good" or even the "best" valid direction.
1127 static struct isl_vec
*valid_direction(
1128 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
1130 struct isl_basic_set
*lp
;
1131 struct isl_tab
*tab
;
1132 struct isl_vec
*sample
= NULL
;
1133 struct isl_vec
*dir
;
1138 if (!bset1
|| !bset2
)
1140 lp
= valid_direction_lp(isl_basic_set_copy(bset1
),
1141 isl_basic_set_copy(bset2
));
1142 tab
= isl_tab_from_basic_set(lp
);
1143 sample
= isl_tab_get_sample_value(tab
);
1145 isl_basic_set_free(lp
);
1148 d
= isl_basic_set_total_dim(bset1
);
1149 dir
= isl_vec_alloc(bset1
->ctx
, 1 + d
);
1152 isl_seq_clr(dir
->block
.data
+ 1, dir
->size
- 1);
1154 /* positivity constraint 1 >= 0 */
1155 isl_int_set(dir
->block
.data
[0], sample
->block
.data
[n
]); n
++;
1156 for (i
= 0; i
< bset1
->n_eq
; ++i
) {
1157 isl_int_sub(sample
->block
.data
[n
],
1158 sample
->block
.data
[n
], sample
->block
.data
[n
+1]);
1159 isl_seq_combine(dir
->block
.data
,
1160 bset1
->ctx
->one
, dir
->block
.data
,
1161 sample
->block
.data
[n
], bset1
->eq
[i
], 1 + d
);
1165 for (i
= 0; i
< bset1
->n_ineq
; ++i
)
1166 isl_seq_combine(dir
->block
.data
,
1167 bset1
->ctx
->one
, dir
->block
.data
,
1168 sample
->block
.data
[n
++], bset1
->ineq
[i
], 1 + d
);
1169 isl_vec_free(sample
);
1170 isl_seq_normalize(bset1
->ctx
, dir
->el
, dir
->size
);
1171 isl_basic_set_free(bset1
);
1172 isl_basic_set_free(bset2
);
1175 isl_vec_free(sample
);
1176 isl_basic_set_free(bset1
);
1177 isl_basic_set_free(bset2
);
1181 /* Given a polyhedron b_i + A_i x >= 0 and a map T = S^{-1},
1182 * compute b_i' + A_i' x' >= 0, with
1184 * [ b_i A_i ] [ y' ] [ y' ]
1185 * [ 1 0 ] S^{-1} [ x' ] >= 0 or [ b_i' A_i' ] [ x' ] >= 0
1187 * In particular, add the "positivity constraint" and then perform
1190 static struct isl_basic_set
*homogeneous_map(struct isl_basic_set
*bset
,
1197 bset
= isl_basic_set_extend_constraints(bset
, 0, 1);
1198 k
= isl_basic_set_alloc_inequality(bset
);
1201 isl_seq_clr(bset
->ineq
[k
] + 1, isl_basic_set_total_dim(bset
));
1202 isl_int_set_si(bset
->ineq
[k
][0], 1);
1203 bset
= isl_basic_set_preimage(bset
, T
);
1207 isl_basic_set_free(bset
);
1211 /* Compute the convex hull of a pair of basic sets without any parameters or
1212 * integer divisions, where the convex hull is known to be pointed,
1213 * but the basic sets may be unbounded.
1215 * We turn this problem into the computation of a convex hull of a pair
1216 * _bounded_ polyhedra by "changing the direction of the homogeneous
1217 * dimension". This idea is due to Matthias Koeppe.
1219 * Consider the cones in homogeneous space that correspond to the
1220 * input polyhedra. The rays of these cones are also rays of the
1221 * polyhedra if the coordinate that corresponds to the homogeneous
1222 * dimension is zero. That is, if the inner product of the rays
1223 * with the homogeneous direction is zero.
1224 * The cones in the homogeneous space can also be considered to
1225 * correspond to other pairs of polyhedra by chosing a different
1226 * homogeneous direction. To ensure that both of these polyhedra
1227 * are bounded, we need to make sure that all rays of the cones
1228 * correspond to vertices and not to rays.
1229 * Let s be a direction such that <s, r> > 0 for all rays r of both cones.
1230 * Then using s as a homogeneous direction, we obtain a pair of polytopes.
1231 * The vector s is computed in valid_direction.
1233 * Note that we need to consider _all_ rays of the cones and not just
1234 * the rays that correspond to rays in the polyhedra. If we were to
1235 * only consider those rays and turn them into vertices, then we
1236 * may inadvertently turn some vertices into rays.
1238 * The standard homogeneous direction is the unit vector in the 0th coordinate.
1239 * We therefore transform the two polyhedra such that the selected
1240 * direction is mapped onto this standard direction and then proceed
1241 * with the normal computation.
1242 * Let S be a non-singular square matrix with s as its first row,
1243 * then we want to map the polyhedra to the space
1245 * [ y' ] [ y ] [ y ] [ y' ]
1246 * [ x' ] = S [ x ] i.e., [ x ] = S^{-1} [ x' ]
1248 * We take S to be the unimodular completion of s to limit the growth
1249 * of the coefficients in the following computations.
1251 * Let b_i + A_i x >= 0 be the constraints of polyhedron i.
1252 * We first move to the homogeneous dimension
1254 * b_i y + A_i x >= 0 [ b_i A_i ] [ y ] [ 0 ]
1255 * y >= 0 or [ 1 0 ] [ x ] >= [ 0 ]
1257 * Then we change directoin
1259 * [ b_i A_i ] [ y' ] [ y' ]
1260 * [ 1 0 ] S^{-1} [ x' ] >= 0 or [ b_i' A_i' ] [ x' ] >= 0
1262 * Then we compute the convex hull of the polytopes b_i' + A_i' x' >= 0
1263 * resulting in b' + A' x' >= 0, which we then convert back
1266 * [ b' A' ] S [ x ] >= 0 or [ b A ] [ x ] >= 0
1268 * The polyhedron b + A x >= 0 is then the convex hull of the input polyhedra.
1270 static struct isl_basic_set
*convex_hull_pair_pointed(
1271 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
1273 struct isl_ctx
*ctx
= NULL
;
1274 struct isl_vec
*dir
= NULL
;
1275 struct isl_mat
*T
= NULL
;
1276 struct isl_mat
*T2
= NULL
;
1277 struct isl_basic_set
*hull
;
1278 struct isl_set
*set
;
1280 if (!bset1
|| !bset2
)
1283 dir
= valid_direction(isl_basic_set_copy(bset1
),
1284 isl_basic_set_copy(bset2
));
1287 T
= isl_mat_alloc(bset1
->ctx
, dir
->size
, dir
->size
);
1290 isl_seq_cpy(T
->row
[0], dir
->block
.data
, dir
->size
);
1291 T
= isl_mat_unimodular_complete(T
, 1);
1292 T2
= isl_mat_right_inverse(isl_mat_copy(T
));
1294 bset1
= homogeneous_map(bset1
, isl_mat_copy(T2
));
1295 bset2
= homogeneous_map(bset2
, T2
);
1296 set
= isl_set_alloc_dim(isl_basic_set_get_dim(bset1
), 2, 0);
1297 set
= isl_set_add_basic_set(set
, bset1
);
1298 set
= isl_set_add_basic_set(set
, bset2
);
1299 hull
= uset_convex_hull(set
);
1300 hull
= isl_basic_set_preimage(hull
, T
);
1307 isl_basic_set_free(bset1
);
1308 isl_basic_set_free(bset2
);
1312 static struct isl_basic_set
*uset_convex_hull_wrap(struct isl_set
*set
);
1313 static struct isl_basic_set
*modulo_affine_hull(
1314 struct isl_set
*set
, struct isl_basic_set
*affine_hull
);
1316 /* Compute the convex hull of a pair of basic sets without any parameters or
1317 * integer divisions.
1319 * This function is called from uset_convex_hull_unbounded, which
1320 * means that the complete convex hull is unbounded. Some pairs
1321 * of basic sets may still be bounded, though.
1322 * They may even lie inside a lower dimensional space, in which
1323 * case they need to be handled inside their affine hull since
1324 * the main algorithm assumes that the result is full-dimensional.
1326 * If the convex hull of the two basic sets would have a non-trivial
1327 * lineality space, we first project out this lineality space.
1329 static struct isl_basic_set
*convex_hull_pair(struct isl_basic_set
*bset1
,
1330 struct isl_basic_set
*bset2
)
1332 isl_basic_set
*lin
, *aff
;
1333 int bounded1
, bounded2
;
1335 aff
= isl_set_affine_hull(isl_basic_set_union(isl_basic_set_copy(bset1
),
1336 isl_basic_set_copy(bset2
)));
1340 return modulo_affine_hull(isl_basic_set_union(bset1
, bset2
), aff
);
1341 isl_basic_set_free(aff
);
1343 bounded1
= isl_basic_set_is_bounded(bset1
);
1344 bounded2
= isl_basic_set_is_bounded(bset2
);
1346 if (bounded1
< 0 || bounded2
< 0)
1349 if (bounded1
&& bounded2
)
1350 uset_convex_hull_wrap(isl_basic_set_union(bset1
, bset2
));
1352 if (bounded1
|| bounded2
)
1353 return convex_hull_pair_pointed(bset1
, bset2
);
1355 lin
= induced_lineality_space(isl_basic_set_copy(bset1
),
1356 isl_basic_set_copy(bset2
));
1359 if (isl_basic_set_is_universe(lin
)) {
1360 isl_basic_set_free(bset1
);
1361 isl_basic_set_free(bset2
);
1364 if (lin
->n_eq
< isl_basic_set_total_dim(lin
)) {
1365 struct isl_set
*set
;
1366 set
= isl_set_alloc_dim(isl_basic_set_get_dim(bset1
), 2, 0);
1367 set
= isl_set_add_basic_set(set
, bset1
);
1368 set
= isl_set_add_basic_set(set
, bset2
);
1369 return modulo_lineality(set
, lin
);
1371 isl_basic_set_free(lin
);
1373 return convex_hull_pair_pointed(bset1
, bset2
);
1375 isl_basic_set_free(bset1
);
1376 isl_basic_set_free(bset2
);
1380 /* Compute the lineality space of a basic set.
1381 * We currently do not allow the basic set to have any divs.
1382 * We basically just drop the constants and turn every inequality
1385 struct isl_basic_set
*isl_basic_set_lineality_space(struct isl_basic_set
*bset
)
1388 struct isl_basic_set
*lin
= NULL
;
1393 isl_assert(bset
->ctx
, bset
->n_div
== 0, goto error
);
1394 dim
= isl_basic_set_total_dim(bset
);
1396 lin
= isl_basic_set_alloc_dim(isl_basic_set_get_dim(bset
), 0, dim
, 0);
1399 for (i
= 0; i
< bset
->n_eq
; ++i
) {
1400 k
= isl_basic_set_alloc_equality(lin
);
1403 isl_int_set_si(lin
->eq
[k
][0], 0);
1404 isl_seq_cpy(lin
->eq
[k
] + 1, bset
->eq
[i
] + 1, dim
);
1406 lin
= isl_basic_set_gauss(lin
, NULL
);
1409 for (i
= 0; i
< bset
->n_ineq
&& lin
->n_eq
< dim
; ++i
) {
1410 k
= isl_basic_set_alloc_equality(lin
);
1413 isl_int_set_si(lin
->eq
[k
][0], 0);
1414 isl_seq_cpy(lin
->eq
[k
] + 1, bset
->ineq
[i
] + 1, dim
);
1415 lin
= isl_basic_set_gauss(lin
, NULL
);
1419 isl_basic_set_free(bset
);
1422 isl_basic_set_free(lin
);
1423 isl_basic_set_free(bset
);
1427 /* Compute the (linear) hull of the lineality spaces of the basic sets in the
1428 * "underlying" set "set".
1430 static struct isl_basic_set
*uset_combined_lineality_space(struct isl_set
*set
)
1433 struct isl_set
*lin
= NULL
;
1438 struct isl_dim
*dim
= isl_set_get_dim(set
);
1440 return isl_basic_set_empty(dim
);
1443 lin
= isl_set_alloc_dim(isl_set_get_dim(set
), set
->n
, 0);
1444 for (i
= 0; i
< set
->n
; ++i
)
1445 lin
= isl_set_add_basic_set(lin
,
1446 isl_basic_set_lineality_space(isl_basic_set_copy(set
->p
[i
])));
1448 return isl_set_affine_hull(lin
);
1451 /* Compute the convex hull of a set without any parameters or
1452 * integer divisions.
1453 * In each step, we combined two basic sets until only one
1454 * basic set is left.
1455 * The input basic sets are assumed not to have a non-trivial
1456 * lineality space. If any of the intermediate results has
1457 * a non-trivial lineality space, it is projected out.
1459 static struct isl_basic_set
*uset_convex_hull_unbounded(struct isl_set
*set
)
1461 struct isl_basic_set
*convex_hull
= NULL
;
1463 convex_hull
= isl_set_copy_basic_set(set
);
1464 set
= isl_set_drop_basic_set(set
, convex_hull
);
1467 while (set
->n
> 0) {
1468 struct isl_basic_set
*t
;
1469 t
= isl_set_copy_basic_set(set
);
1472 set
= isl_set_drop_basic_set(set
, t
);
1475 convex_hull
= convex_hull_pair(convex_hull
, t
);
1478 t
= isl_basic_set_lineality_space(isl_basic_set_copy(convex_hull
));
1481 if (isl_basic_set_is_universe(t
)) {
1482 isl_basic_set_free(convex_hull
);
1486 if (t
->n_eq
< isl_basic_set_total_dim(t
)) {
1487 set
= isl_set_add_basic_set(set
, convex_hull
);
1488 return modulo_lineality(set
, t
);
1490 isl_basic_set_free(t
);
1496 isl_basic_set_free(convex_hull
);
1500 /* Compute an initial hull for wrapping containing a single initial
1502 * This function assumes that the given set is bounded.
1504 static struct isl_basic_set
*initial_hull(struct isl_basic_set
*hull
,
1505 struct isl_set
*set
)
1507 struct isl_mat
*bounds
= NULL
;
1513 bounds
= initial_facet_constraint(set
);
1516 k
= isl_basic_set_alloc_inequality(hull
);
1519 dim
= isl_set_n_dim(set
);
1520 isl_assert(set
->ctx
, 1 + dim
== bounds
->n_col
, goto error
);
1521 isl_seq_cpy(hull
->ineq
[k
], bounds
->row
[0], bounds
->n_col
);
1522 isl_mat_free(bounds
);
1526 isl_basic_set_free(hull
);
1527 isl_mat_free(bounds
);
1531 struct max_constraint
{
1537 static int max_constraint_equal(const void *entry
, const void *val
)
1539 struct max_constraint
*a
= (struct max_constraint
*)entry
;
1540 isl_int
*b
= (isl_int
*)val
;
1542 return isl_seq_eq(a
->c
->row
[0] + 1, b
, a
->c
->n_col
- 1);
1545 static void update_constraint(struct isl_ctx
*ctx
, struct isl_hash_table
*table
,
1546 isl_int
*con
, unsigned len
, int n
, int ineq
)
1548 struct isl_hash_table_entry
*entry
;
1549 struct max_constraint
*c
;
1552 c_hash
= isl_seq_get_hash(con
+ 1, len
);
1553 entry
= isl_hash_table_find(ctx
, table
, c_hash
, max_constraint_equal
,
1559 isl_hash_table_remove(ctx
, table
, entry
);
1563 if (isl_int_gt(c
->c
->row
[0][0], con
[0]))
1565 if (isl_int_eq(c
->c
->row
[0][0], con
[0])) {
1570 c
->c
= isl_mat_cow(c
->c
);
1571 isl_int_set(c
->c
->row
[0][0], con
[0]);
1575 /* Check whether the constraint hash table "table" constains the constraint
1578 static int has_constraint(struct isl_ctx
*ctx
, struct isl_hash_table
*table
,
1579 isl_int
*con
, unsigned len
, int n
)
1581 struct isl_hash_table_entry
*entry
;
1582 struct max_constraint
*c
;
1585 c_hash
= isl_seq_get_hash(con
+ 1, len
);
1586 entry
= isl_hash_table_find(ctx
, table
, c_hash
, max_constraint_equal
,
1593 return isl_int_eq(c
->c
->row
[0][0], con
[0]);
1596 /* Check for inequality constraints of a basic set without equalities
1597 * such that the same or more stringent copies of the constraint appear
1598 * in all of the basic sets. Such constraints are necessarily facet
1599 * constraints of the convex hull.
1601 * If the resulting basic set is by chance identical to one of
1602 * the basic sets in "set", then we know that this basic set contains
1603 * all other basic sets and is therefore the convex hull of set.
1604 * In this case we set *is_hull to 1.
1606 static struct isl_basic_set
*common_constraints(struct isl_basic_set
*hull
,
1607 struct isl_set
*set
, int *is_hull
)
1610 int min_constraints
;
1612 struct max_constraint
*constraints
= NULL
;
1613 struct isl_hash_table
*table
= NULL
;
1618 for (i
= 0; i
< set
->n
; ++i
)
1619 if (set
->p
[i
]->n_eq
== 0)
1623 min_constraints
= set
->p
[i
]->n_ineq
;
1625 for (i
= best
+ 1; i
< set
->n
; ++i
) {
1626 if (set
->p
[i
]->n_eq
!= 0)
1628 if (set
->p
[i
]->n_ineq
>= min_constraints
)
1630 min_constraints
= set
->p
[i
]->n_ineq
;
1633 constraints
= isl_calloc_array(hull
->ctx
, struct max_constraint
,
1637 table
= isl_alloc_type(hull
->ctx
, struct isl_hash_table
);
1638 if (isl_hash_table_init(hull
->ctx
, table
, min_constraints
))
1641 total
= isl_dim_total(set
->dim
);
1642 for (i
= 0; i
< set
->p
[best
]->n_ineq
; ++i
) {
1643 constraints
[i
].c
= isl_mat_sub_alloc(hull
->ctx
,
1644 set
->p
[best
]->ineq
+ i
, 0, 1, 0, 1 + total
);
1645 if (!constraints
[i
].c
)
1647 constraints
[i
].ineq
= 1;
1649 for (i
= 0; i
< min_constraints
; ++i
) {
1650 struct isl_hash_table_entry
*entry
;
1652 c_hash
= isl_seq_get_hash(constraints
[i
].c
->row
[0] + 1, total
);
1653 entry
= isl_hash_table_find(hull
->ctx
, table
, c_hash
,
1654 max_constraint_equal
, constraints
[i
].c
->row
[0] + 1, 1);
1657 isl_assert(hull
->ctx
, !entry
->data
, goto error
);
1658 entry
->data
= &constraints
[i
];
1662 for (s
= 0; s
< set
->n
; ++s
) {
1666 for (i
= 0; i
< set
->p
[s
]->n_eq
; ++i
) {
1667 isl_int
*eq
= set
->p
[s
]->eq
[i
];
1668 for (j
= 0; j
< 2; ++j
) {
1669 isl_seq_neg(eq
, eq
, 1 + total
);
1670 update_constraint(hull
->ctx
, table
,
1674 for (i
= 0; i
< set
->p
[s
]->n_ineq
; ++i
) {
1675 isl_int
*ineq
= set
->p
[s
]->ineq
[i
];
1676 update_constraint(hull
->ctx
, table
, ineq
, total
, n
,
1677 set
->p
[s
]->n_eq
== 0);
1682 for (i
= 0; i
< min_constraints
; ++i
) {
1683 if (constraints
[i
].count
< n
)
1685 if (!constraints
[i
].ineq
)
1687 j
= isl_basic_set_alloc_inequality(hull
);
1690 isl_seq_cpy(hull
->ineq
[j
], constraints
[i
].c
->row
[0], 1 + total
);
1693 for (s
= 0; s
< set
->n
; ++s
) {
1694 if (set
->p
[s
]->n_eq
)
1696 if (set
->p
[s
]->n_ineq
!= hull
->n_ineq
)
1698 for (i
= 0; i
< set
->p
[s
]->n_ineq
; ++i
) {
1699 isl_int
*ineq
= set
->p
[s
]->ineq
[i
];
1700 if (!has_constraint(hull
->ctx
, table
, ineq
, total
, n
))
1703 if (i
== set
->p
[s
]->n_ineq
)
1707 isl_hash_table_clear(table
);
1708 for (i
= 0; i
< min_constraints
; ++i
)
1709 isl_mat_free(constraints
[i
].c
);
1714 isl_hash_table_clear(table
);
1717 for (i
= 0; i
< min_constraints
; ++i
)
1718 isl_mat_free(constraints
[i
].c
);
1723 /* Create a template for the convex hull of "set" and fill it up
1724 * obvious facet constraints, if any. If the result happens to
1725 * be the convex hull of "set" then *is_hull is set to 1.
1727 static struct isl_basic_set
*proto_hull(struct isl_set
*set
, int *is_hull
)
1729 struct isl_basic_set
*hull
;
1734 for (i
= 0; i
< set
->n
; ++i
) {
1735 n_ineq
+= set
->p
[i
]->n_eq
;
1736 n_ineq
+= set
->p
[i
]->n_ineq
;
1738 hull
= isl_basic_set_alloc_dim(isl_dim_copy(set
->dim
), 0, 0, n_ineq
);
1739 hull
= isl_basic_set_set_rational(hull
);
1742 return common_constraints(hull
, set
, is_hull
);
1745 static struct isl_basic_set
*uset_convex_hull_wrap(struct isl_set
*set
)
1747 struct isl_basic_set
*hull
;
1750 hull
= proto_hull(set
, &is_hull
);
1751 if (hull
&& !is_hull
) {
1752 if (hull
->n_ineq
== 0)
1753 hull
= initial_hull(hull
, set
);
1754 hull
= extend(hull
, set
);
1761 /* Compute the convex hull of a set without any parameters or
1762 * integer divisions. Depending on whether the set is bounded,
1763 * we pass control to the wrapping based convex hull or
1764 * the Fourier-Motzkin elimination based convex hull.
1765 * We also handle a few special cases before checking the boundedness.
1767 static struct isl_basic_set
*uset_convex_hull(struct isl_set
*set
)
1769 struct isl_basic_set
*convex_hull
= NULL
;
1770 struct isl_basic_set
*lin
;
1772 if (isl_set_n_dim(set
) == 0)
1773 return convex_hull_0d(set
);
1775 set
= isl_set_coalesce(set
);
1776 set
= isl_set_set_rational(set
);
1783 convex_hull
= isl_basic_set_copy(set
->p
[0]);
1787 if (isl_set_n_dim(set
) == 1)
1788 return convex_hull_1d(set
);
1790 if (isl_set_is_bounded(set
))
1791 return uset_convex_hull_wrap(set
);
1793 lin
= uset_combined_lineality_space(isl_set_copy(set
));
1796 if (isl_basic_set_is_universe(lin
)) {
1800 if (lin
->n_eq
< isl_basic_set_total_dim(lin
))
1801 return modulo_lineality(set
, lin
);
1802 isl_basic_set_free(lin
);
1804 return uset_convex_hull_unbounded(set
);
1807 isl_basic_set_free(convex_hull
);
1811 /* This is the core procedure, where "set" is a "pure" set, i.e.,
1812 * without parameters or divs and where the convex hull of set is
1813 * known to be full-dimensional.
1815 static struct isl_basic_set
*uset_convex_hull_wrap_bounded(struct isl_set
*set
)
1817 struct isl_basic_set
*convex_hull
= NULL
;
1822 if (isl_set_n_dim(set
) == 0) {
1823 convex_hull
= isl_basic_set_universe(isl_dim_copy(set
->dim
));
1825 convex_hull
= isl_basic_set_set_rational(convex_hull
);
1829 set
= isl_set_set_rational(set
);
1830 set
= isl_set_coalesce(set
);
1834 convex_hull
= isl_basic_set_copy(set
->p
[0]);
1838 if (isl_set_n_dim(set
) == 1)
1839 return convex_hull_1d(set
);
1841 return uset_convex_hull_wrap(set
);
1847 /* Compute the convex hull of set "set" with affine hull "affine_hull",
1848 * We first remove the equalities (transforming the set), compute the
1849 * convex hull of the transformed set and then add the equalities back
1850 * (after performing the inverse transformation.
1852 static struct isl_basic_set
*modulo_affine_hull(
1853 struct isl_set
*set
, struct isl_basic_set
*affine_hull
)
1857 struct isl_basic_set
*dummy
;
1858 struct isl_basic_set
*convex_hull
;
1860 dummy
= isl_basic_set_remove_equalities(
1861 isl_basic_set_copy(affine_hull
), &T
, &T2
);
1864 isl_basic_set_free(dummy
);
1865 set
= isl_set_preimage(set
, T
);
1866 convex_hull
= uset_convex_hull(set
);
1867 convex_hull
= isl_basic_set_preimage(convex_hull
, T2
);
1868 convex_hull
= isl_basic_set_intersect(convex_hull
, affine_hull
);
1871 isl_basic_set_free(affine_hull
);
1876 /* Compute the convex hull of a map.
1878 * The implementation was inspired by "Extended Convex Hull" by Fukuda et al.,
1879 * specifically, the wrapping of facets to obtain new facets.
1881 struct isl_basic_map
*isl_map_convex_hull(struct isl_map
*map
)
1883 struct isl_basic_set
*bset
;
1884 struct isl_basic_map
*model
= NULL
;
1885 struct isl_basic_set
*affine_hull
= NULL
;
1886 struct isl_basic_map
*convex_hull
= NULL
;
1887 struct isl_set
*set
= NULL
;
1888 struct isl_ctx
*ctx
;
1895 convex_hull
= isl_basic_map_empty_like_map(map
);
1900 map
= isl_map_detect_equalities(map
);
1901 map
= isl_map_align_divs(map
);
1904 model
= isl_basic_map_copy(map
->p
[0]);
1905 set
= isl_map_underlying_set(map
);
1909 affine_hull
= isl_set_affine_hull(isl_set_copy(set
));
1912 if (affine_hull
->n_eq
!= 0)
1913 bset
= modulo_affine_hull(set
, affine_hull
);
1915 isl_basic_set_free(affine_hull
);
1916 bset
= uset_convex_hull(set
);
1919 convex_hull
= isl_basic_map_overlying_set(bset
, model
);
1923 ISL_F_SET(convex_hull
, ISL_BASIC_MAP_NO_IMPLICIT
);
1924 ISL_F_SET(convex_hull
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1925 ISL_F_CLR(convex_hull
, ISL_BASIC_MAP_RATIONAL
);
1929 isl_basic_map_free(model
);
1933 struct isl_basic_set
*isl_set_convex_hull(struct isl_set
*set
)
1935 return (struct isl_basic_set
*)
1936 isl_map_convex_hull((struct isl_map
*)set
);
1939 struct sh_data_entry
{
1940 struct isl_hash_table
*table
;
1941 struct isl_tab
*tab
;
1944 /* Holds the data needed during the simple hull computation.
1946 * n the number of basic sets in the original set
1947 * hull_table a hash table of already computed constraints
1948 * in the simple hull
1949 * p for each basic set,
1950 * table a hash table of the constraints
1951 * tab the tableau corresponding to the basic set
1954 struct isl_ctx
*ctx
;
1956 struct isl_hash_table
*hull_table
;
1957 struct sh_data_entry p
[1];
1960 static void sh_data_free(struct sh_data
*data
)
1966 isl_hash_table_free(data
->ctx
, data
->hull_table
);
1967 for (i
= 0; i
< data
->n
; ++i
) {
1968 isl_hash_table_free(data
->ctx
, data
->p
[i
].table
);
1969 isl_tab_free(data
->p
[i
].tab
);
1974 struct ineq_cmp_data
{
1979 static int has_ineq(const void *entry
, const void *val
)
1981 isl_int
*row
= (isl_int
*)entry
;
1982 struct ineq_cmp_data
*v
= (struct ineq_cmp_data
*)val
;
1984 return isl_seq_eq(row
+ 1, v
->p
+ 1, v
->len
) ||
1985 isl_seq_is_neg(row
+ 1, v
->p
+ 1, v
->len
);
1988 static int hash_ineq(struct isl_ctx
*ctx
, struct isl_hash_table
*table
,
1989 isl_int
*ineq
, unsigned len
)
1992 struct ineq_cmp_data v
;
1993 struct isl_hash_table_entry
*entry
;
1997 c_hash
= isl_seq_get_hash(ineq
+ 1, len
);
1998 entry
= isl_hash_table_find(ctx
, table
, c_hash
, has_ineq
, &v
, 1);
2005 /* Fill hash table "table" with the constraints of "bset".
2006 * Equalities are added as two inequalities.
2007 * The value in the hash table is a pointer to the (in)equality of "bset".
2009 static int hash_basic_set(struct isl_hash_table
*table
,
2010 struct isl_basic_set
*bset
)
2013 unsigned dim
= isl_basic_set_total_dim(bset
);
2015 for (i
= 0; i
< bset
->n_eq
; ++i
) {
2016 for (j
= 0; j
< 2; ++j
) {
2017 isl_seq_neg(bset
->eq
[i
], bset
->eq
[i
], 1 + dim
);
2018 if (hash_ineq(bset
->ctx
, table
, bset
->eq
[i
], dim
) < 0)
2022 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
2023 if (hash_ineq(bset
->ctx
, table
, bset
->ineq
[i
], dim
) < 0)
2029 static struct sh_data
*sh_data_alloc(struct isl_set
*set
, unsigned n_ineq
)
2031 struct sh_data
*data
;
2034 data
= isl_calloc(set
->ctx
, struct sh_data
,
2035 sizeof(struct sh_data
) +
2036 (set
->n
- 1) * sizeof(struct sh_data_entry
));
2039 data
->ctx
= set
->ctx
;
2041 data
->hull_table
= isl_hash_table_alloc(set
->ctx
, n_ineq
);
2042 if (!data
->hull_table
)
2044 for (i
= 0; i
< set
->n
; ++i
) {
2045 data
->p
[i
].table
= isl_hash_table_alloc(set
->ctx
,
2046 2 * set
->p
[i
]->n_eq
+ set
->p
[i
]->n_ineq
);
2047 if (!data
->p
[i
].table
)
2049 if (hash_basic_set(data
->p
[i
].table
, set
->p
[i
]) < 0)
2058 /* Check if inequality "ineq" is a bound for basic set "j" or if
2059 * it can be relaxed (by increasing the constant term) to become
2060 * a bound for that basic set. In the latter case, the constant
2062 * Return 1 if "ineq" is a bound
2063 * 0 if "ineq" may attain arbitrarily small values on basic set "j"
2064 * -1 if some error occurred
2066 static int is_bound(struct sh_data
*data
, struct isl_set
*set
, int j
,
2069 enum isl_lp_result res
;
2072 if (!data
->p
[j
].tab
) {
2073 data
->p
[j
].tab
= isl_tab_from_basic_set(set
->p
[j
]);
2074 if (!data
->p
[j
].tab
)
2080 res
= isl_tab_min(data
->p
[j
].tab
, ineq
, data
->ctx
->one
,
2082 if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
2083 isl_int_sub(ineq
[0], ineq
[0], opt
);
2087 return (res
== isl_lp_ok
|| res
== isl_lp_empty
) ? 1 :
2088 res
== isl_lp_unbounded
? 0 : -1;
2091 /* Check if inequality "ineq" from basic set "i" can be relaxed to
2092 * become a bound on the whole set. If so, add the (relaxed) inequality
2095 * We first check if "hull" already contains a translate of the inequality.
2096 * If so, we are done.
2097 * Then, we check if any of the previous basic sets contains a translate
2098 * of the inequality. If so, then we have already considered this
2099 * inequality and we are done.
2100 * Otherwise, for each basic set other than "i", we check if the inequality
2101 * is a bound on the basic set.
2102 * For previous basic sets, we know that they do not contain a translate
2103 * of the inequality, so we directly call is_bound.
2104 * For following basic sets, we first check if a translate of the
2105 * inequality appears in its description and if so directly update
2106 * the inequality accordingly.
2108 static struct isl_basic_set
*add_bound(struct isl_basic_set
*hull
,
2109 struct sh_data
*data
, struct isl_set
*set
, int i
, isl_int
*ineq
)
2112 struct ineq_cmp_data v
;
2113 struct isl_hash_table_entry
*entry
;
2119 v
.len
= isl_basic_set_total_dim(hull
);
2121 c_hash
= isl_seq_get_hash(ineq
+ 1, v
.len
);
2123 entry
= isl_hash_table_find(hull
->ctx
, data
->hull_table
, c_hash
,
2128 for (j
= 0; j
< i
; ++j
) {
2129 entry
= isl_hash_table_find(hull
->ctx
, data
->p
[j
].table
,
2130 c_hash
, has_ineq
, &v
, 0);
2137 k
= isl_basic_set_alloc_inequality(hull
);
2138 isl_seq_cpy(hull
->ineq
[k
], ineq
, 1 + v
.len
);
2142 for (j
= 0; j
< i
; ++j
) {
2144 bound
= is_bound(data
, set
, j
, hull
->ineq
[k
]);
2151 isl_basic_set_free_inequality(hull
, 1);
2155 for (j
= i
+ 1; j
< set
->n
; ++j
) {
2158 entry
= isl_hash_table_find(hull
->ctx
, data
->p
[j
].table
,
2159 c_hash
, has_ineq
, &v
, 0);
2161 ineq_j
= entry
->data
;
2162 neg
= isl_seq_is_neg(ineq_j
+ 1,
2163 hull
->ineq
[k
] + 1, v
.len
);
2165 isl_int_neg(ineq_j
[0], ineq_j
[0]);
2166 if (isl_int_gt(ineq_j
[0], hull
->ineq
[k
][0]))
2167 isl_int_set(hull
->ineq
[k
][0], ineq_j
[0]);
2169 isl_int_neg(ineq_j
[0], ineq_j
[0]);
2172 bound
= is_bound(data
, set
, j
, hull
->ineq
[k
]);
2179 isl_basic_set_free_inequality(hull
, 1);
2183 entry
= isl_hash_table_find(hull
->ctx
, data
->hull_table
, c_hash
,
2187 entry
->data
= hull
->ineq
[k
];
2191 isl_basic_set_free(hull
);
2195 /* Check if any inequality from basic set "i" can be relaxed to
2196 * become a bound on the whole set. If so, add the (relaxed) inequality
2199 static struct isl_basic_set
*add_bounds(struct isl_basic_set
*bset
,
2200 struct sh_data
*data
, struct isl_set
*set
, int i
)
2203 unsigned dim
= isl_basic_set_total_dim(bset
);
2205 for (j
= 0; j
< set
->p
[i
]->n_eq
; ++j
) {
2206 for (k
= 0; k
< 2; ++k
) {
2207 isl_seq_neg(set
->p
[i
]->eq
[j
], set
->p
[i
]->eq
[j
], 1+dim
);
2208 bset
= add_bound(bset
, data
, set
, i
, set
->p
[i
]->eq
[j
]);
2211 for (j
= 0; j
< set
->p
[i
]->n_ineq
; ++j
)
2212 bset
= add_bound(bset
, data
, set
, i
, set
->p
[i
]->ineq
[j
]);
2216 /* Compute a superset of the convex hull of set that is described
2217 * by only translates of the constraints in the constituents of set.
2219 static struct isl_basic_set
*uset_simple_hull(struct isl_set
*set
)
2221 struct sh_data
*data
= NULL
;
2222 struct isl_basic_set
*hull
= NULL
;
2230 for (i
= 0; i
< set
->n
; ++i
) {
2233 n_ineq
+= 2 * set
->p
[i
]->n_eq
+ set
->p
[i
]->n_ineq
;
2236 hull
= isl_basic_set_alloc_dim(isl_dim_copy(set
->dim
), 0, 0, n_ineq
);
2240 data
= sh_data_alloc(set
, n_ineq
);
2244 for (i
= 0; i
< set
->n
; ++i
)
2245 hull
= add_bounds(hull
, data
, set
, i
);
2253 isl_basic_set_free(hull
);
2258 /* Compute a superset of the convex hull of map that is described
2259 * by only translates of the constraints in the constituents of map.
2261 struct isl_basic_map
*isl_map_simple_hull(struct isl_map
*map
)
2263 struct isl_set
*set
= NULL
;
2264 struct isl_basic_map
*model
= NULL
;
2265 struct isl_basic_map
*hull
;
2266 struct isl_basic_map
*affine_hull
;
2267 struct isl_basic_set
*bset
= NULL
;
2272 hull
= isl_basic_map_empty_like_map(map
);
2277 hull
= isl_basic_map_copy(map
->p
[0]);
2282 map
= isl_map_detect_equalities(map
);
2283 affine_hull
= isl_map_affine_hull(isl_map_copy(map
));
2284 map
= isl_map_align_divs(map
);
2285 model
= isl_basic_map_copy(map
->p
[0]);
2287 set
= isl_map_underlying_set(map
);
2289 bset
= uset_simple_hull(set
);
2291 hull
= isl_basic_map_overlying_set(bset
, model
);
2293 hull
= isl_basic_map_intersect(hull
, affine_hull
);
2294 hull
= isl_basic_map_remove_redundancies(hull
);
2295 ISL_F_SET(hull
, ISL_BASIC_MAP_NO_IMPLICIT
);
2296 ISL_F_SET(hull
, ISL_BASIC_MAP_ALL_EQUALITIES
);
2301 struct isl_basic_set
*isl_set_simple_hull(struct isl_set
*set
)
2303 return (struct isl_basic_set
*)
2304 isl_map_simple_hull((struct isl_map
*)set
);
2307 /* Given a set "set", return parametric bounds on the dimension "dim".
2309 static struct isl_basic_set
*set_bounds(struct isl_set
*set
, int dim
)
2311 unsigned set_dim
= isl_set_dim(set
, isl_dim_set
);
2312 set
= isl_set_copy(set
);
2313 set
= isl_set_eliminate_dims(set
, dim
+ 1, set_dim
- (dim
+ 1));
2314 set
= isl_set_eliminate_dims(set
, 0, dim
);
2315 return isl_set_convex_hull(set
);
2318 /* Computes a "simple hull" and then check if each dimension in the
2319 * resulting hull is bounded by a symbolic constant. If not, the
2320 * hull is intersected with the corresponding bounds on the whole set.
2322 struct isl_basic_set
*isl_set_bounded_simple_hull(struct isl_set
*set
)
2325 struct isl_basic_set
*hull
;
2326 unsigned nparam
, left
;
2327 int removed_divs
= 0;
2329 hull
= isl_set_simple_hull(isl_set_copy(set
));
2333 nparam
= isl_basic_set_dim(hull
, isl_dim_param
);
2334 for (i
= 0; i
< isl_basic_set_dim(hull
, isl_dim_set
); ++i
) {
2335 int lower
= 0, upper
= 0;
2336 struct isl_basic_set
*bounds
;
2338 left
= isl_basic_set_total_dim(hull
) - nparam
- i
- 1;
2339 for (j
= 0; j
< hull
->n_eq
; ++j
) {
2340 if (isl_int_is_zero(hull
->eq
[j
][1 + nparam
+ i
]))
2342 if (isl_seq_first_non_zero(hull
->eq
[j
]+1+nparam
+i
+1,
2349 for (j
= 0; j
< hull
->n_ineq
; ++j
) {
2350 if (isl_int_is_zero(hull
->ineq
[j
][1 + nparam
+ i
]))
2352 if (isl_seq_first_non_zero(hull
->ineq
[j
]+1+nparam
+i
+1,
2354 isl_seq_first_non_zero(hull
->ineq
[j
]+1+nparam
,
2357 if (isl_int_is_pos(hull
->ineq
[j
][1 + nparam
+ i
]))
2368 if (!removed_divs
) {
2369 set
= isl_set_remove_divs(set
);
2374 bounds
= set_bounds(set
, i
);
2375 hull
= isl_basic_set_intersect(hull
, bounds
);