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
)
385 struct isl_mat
*T
= NULL
;
386 struct isl_basic_set
*lp
= NULL
;
388 enum isl_lp_result res
;
395 set
= isl_set_copy(set
);
396 set
= isl_set_set_rational(set
);
398 dim
= 1 + isl_set_n_dim(set
);
399 T
= isl_mat_alloc(ctx
, 3, dim
);
402 isl_int_set_si(T
->row
[0][0], 1);
403 isl_seq_clr(T
->row
[0]+1, dim
- 1);
404 isl_seq_cpy(T
->row
[1], facet
, dim
);
405 isl_seq_cpy(T
->row
[2], ridge
, dim
);
406 T
= isl_mat_right_inverse(T
);
407 set
= isl_set_preimage(set
, T
);
411 lp
= wrap_constraints(set
);
412 obj
= isl_vec_alloc(ctx
, 1 + dim
*set
->n
);
415 isl_int_set_si(obj
->block
.data
[0], 0);
416 for (i
= 0; i
< set
->n
; ++i
) {
417 isl_seq_clr(obj
->block
.data
+ 1 + dim
*i
, 2);
418 isl_int_set_si(obj
->block
.data
[1 + dim
*i
+2], 1);
419 isl_seq_clr(obj
->block
.data
+ 1 + dim
*i
+3, dim
-3);
423 res
= isl_basic_set_solve_lp(lp
, 0,
424 obj
->block
.data
, ctx
->one
, &num
, &den
, NULL
);
425 if (res
== isl_lp_ok
) {
426 isl_int_neg(num
, num
);
427 isl_seq_combine(facet
, num
, facet
, den
, ridge
, dim
);
432 isl_basic_set_free(lp
);
434 if (res
== isl_lp_error
)
436 isl_assert(ctx
, res
== isl_lp_ok
|| res
== isl_lp_unbounded
,
440 isl_basic_set_free(lp
);
446 /* Compute the constraint of a facet of "set".
448 * We first compute the intersection with a bounding constraint
449 * that is orthogonal to one of the coordinate axes.
450 * If the affine hull of this intersection has only one equality,
451 * we have found a facet.
452 * Otherwise, we wrap the current bounding constraint around
453 * one of the equalities of the face (one that is not equal to
454 * the current bounding constraint).
455 * This process continues until we have found a facet.
456 * The dimension of the intersection increases by at least
457 * one on each iteration, so termination is guaranteed.
459 static __isl_give isl_mat
*initial_facet_constraint(__isl_keep isl_set
*set
)
461 struct isl_set
*slice
= NULL
;
462 struct isl_basic_set
*face
= NULL
;
464 unsigned dim
= isl_set_n_dim(set
);
468 isl_assert(set
->ctx
, set
->n
> 0, goto error
);
469 bounds
= isl_mat_alloc(set
->ctx
, 1, 1 + dim
);
473 isl_seq_clr(bounds
->row
[0], dim
);
474 isl_int_set_si(bounds
->row
[0][1 + dim
- 1], 1);
475 is_bound
= uset_is_bound(set
, bounds
->row
[0], 1 + dim
);
476 isl_assert(set
->ctx
, is_bound
== 1, goto error
);
477 isl_seq_normalize(set
->ctx
, bounds
->row
[0], 1 + dim
);
481 slice
= isl_set_copy(set
);
482 slice
= isl_set_add_basic_set_equality(slice
, bounds
->row
[0]);
483 face
= isl_set_affine_hull(slice
);
486 if (face
->n_eq
== 1) {
487 isl_basic_set_free(face
);
490 for (i
= 0; i
< face
->n_eq
; ++i
)
491 if (!isl_seq_eq(bounds
->row
[0], face
->eq
[i
], 1 + dim
) &&
492 !isl_seq_is_neg(bounds
->row
[0],
493 face
->eq
[i
], 1 + dim
))
495 isl_assert(set
->ctx
, i
< face
->n_eq
, goto error
);
496 if (!isl_set_wrap_facet(set
, bounds
->row
[0], face
->eq
[i
]))
498 isl_seq_normalize(set
->ctx
, bounds
->row
[0], bounds
->n_col
);
499 isl_basic_set_free(face
);
504 isl_basic_set_free(face
);
505 isl_mat_free(bounds
);
509 /* Given the bounding constraint "c" of a facet of the convex hull of "set",
510 * compute a hyperplane description of the facet, i.e., compute the facets
513 * We compute an affine transformation that transforms the constraint
522 * by computing the right inverse U of a matrix that starts with the rows
535 * Since z_1 is zero, we can drop this variable as well as the corresponding
536 * column of U to obtain
544 * with Q' equal to Q, but without the corresponding row.
545 * After computing the facets of the facet in the z' space,
546 * we convert them back to the x space through Q.
548 static struct isl_basic_set
*compute_facet(struct isl_set
*set
, isl_int
*c
)
550 struct isl_mat
*m
, *U
, *Q
;
551 struct isl_basic_set
*facet
= NULL
;
556 set
= isl_set_copy(set
);
557 dim
= isl_set_n_dim(set
);
558 m
= isl_mat_alloc(set
->ctx
, 2, 1 + dim
);
561 isl_int_set_si(m
->row
[0][0], 1);
562 isl_seq_clr(m
->row
[0]+1, dim
);
563 isl_seq_cpy(m
->row
[1], c
, 1+dim
);
564 U
= isl_mat_right_inverse(m
);
565 Q
= isl_mat_right_inverse(isl_mat_copy(U
));
566 U
= isl_mat_drop_cols(U
, 1, 1);
567 Q
= isl_mat_drop_rows(Q
, 1, 1);
568 set
= isl_set_preimage(set
, U
);
569 facet
= uset_convex_hull_wrap_bounded(set
);
570 facet
= isl_basic_set_preimage(facet
, Q
);
572 isl_assert(ctx
, facet
->n_eq
== 0, goto error
);
575 isl_basic_set_free(facet
);
580 /* Given an initial facet constraint, compute the remaining facets.
581 * We do this by running through all facets found so far and computing
582 * the adjacent facets through wrapping, adding those facets that we
583 * hadn't already found before.
585 * For each facet we have found so far, we first compute its facets
586 * in the resulting convex hull. That is, we compute the ridges
587 * of the resulting convex hull contained in the facet.
588 * We also compute the corresponding facet in the current approximation
589 * of the convex hull. There is no need to wrap around the ridges
590 * in this facet since that would result in a facet that is already
591 * present in the current approximation.
593 * This function can still be significantly optimized by checking which of
594 * the facets of the basic sets are also facets of the convex hull and
595 * using all the facets so far to help in constructing the facets of the
598 * using the technique in section "3.1 Ridge Generation" of
599 * "Extended Convex Hull" by Fukuda et al.
601 static struct isl_basic_set
*extend(struct isl_basic_set
*hull
,
606 struct isl_basic_set
*facet
= NULL
;
607 struct isl_basic_set
*hull_facet
= NULL
;
613 isl_assert(set
->ctx
, set
->n
> 0, goto error
);
615 dim
= isl_set_n_dim(set
);
617 for (i
= 0; i
< hull
->n_ineq
; ++i
) {
618 facet
= compute_facet(set
, hull
->ineq
[i
]);
619 facet
= isl_basic_set_add_equality(facet
, hull
->ineq
[i
]);
620 facet
= isl_basic_set_gauss(facet
, NULL
);
621 facet
= isl_basic_set_normalize_constraints(facet
);
622 hull_facet
= isl_basic_set_copy(hull
);
623 hull_facet
= isl_basic_set_add_equality(hull_facet
, hull
->ineq
[i
]);
624 hull_facet
= isl_basic_set_gauss(hull_facet
, NULL
);
625 hull_facet
= isl_basic_set_normalize_constraints(hull_facet
);
626 if (!facet
|| !hull_facet
)
628 hull
= isl_basic_set_cow(hull
);
629 hull
= isl_basic_set_extend_dim(hull
,
630 isl_dim_copy(hull
->dim
), 0, 0, facet
->n_ineq
);
633 for (j
= 0; j
< facet
->n_ineq
; ++j
) {
634 for (f
= 0; f
< hull_facet
->n_ineq
; ++f
)
635 if (isl_seq_eq(facet
->ineq
[j
],
636 hull_facet
->ineq
[f
], 1 + dim
))
638 if (f
< hull_facet
->n_ineq
)
640 k
= isl_basic_set_alloc_inequality(hull
);
643 isl_seq_cpy(hull
->ineq
[k
], hull
->ineq
[i
], 1+dim
);
644 if (!isl_set_wrap_facet(set
, hull
->ineq
[k
], facet
->ineq
[j
]))
647 isl_basic_set_free(hull_facet
);
648 isl_basic_set_free(facet
);
650 hull
= isl_basic_set_simplify(hull
);
651 hull
= isl_basic_set_finalize(hull
);
654 isl_basic_set_free(hull_facet
);
655 isl_basic_set_free(facet
);
656 isl_basic_set_free(hull
);
660 /* Special case for computing the convex hull of a one dimensional set.
661 * We simply collect the lower and upper bounds of each basic set
662 * and the biggest of those.
664 static struct isl_basic_set
*convex_hull_1d(struct isl_set
*set
)
666 struct isl_mat
*c
= NULL
;
667 isl_int
*lower
= NULL
;
668 isl_int
*upper
= NULL
;
671 struct isl_basic_set
*hull
;
673 for (i
= 0; i
< set
->n
; ++i
) {
674 set
->p
[i
] = isl_basic_set_simplify(set
->p
[i
]);
678 set
= isl_set_remove_empty_parts(set
);
681 isl_assert(set
->ctx
, set
->n
> 0, goto error
);
682 c
= isl_mat_alloc(set
->ctx
, 2, 2);
686 if (set
->p
[0]->n_eq
> 0) {
687 isl_assert(set
->ctx
, set
->p
[0]->n_eq
== 1, goto error
);
690 if (isl_int_is_pos(set
->p
[0]->eq
[0][1])) {
691 isl_seq_cpy(lower
, set
->p
[0]->eq
[0], 2);
692 isl_seq_neg(upper
, set
->p
[0]->eq
[0], 2);
694 isl_seq_neg(lower
, set
->p
[0]->eq
[0], 2);
695 isl_seq_cpy(upper
, set
->p
[0]->eq
[0], 2);
698 for (j
= 0; j
< set
->p
[0]->n_ineq
; ++j
) {
699 if (isl_int_is_pos(set
->p
[0]->ineq
[j
][1])) {
701 isl_seq_cpy(lower
, set
->p
[0]->ineq
[j
], 2);
704 isl_seq_cpy(upper
, set
->p
[0]->ineq
[j
], 2);
711 for (i
= 0; i
< set
->n
; ++i
) {
712 struct isl_basic_set
*bset
= set
->p
[i
];
716 for (j
= 0; j
< bset
->n_eq
; ++j
) {
720 isl_int_mul(a
, lower
[0], bset
->eq
[j
][1]);
721 isl_int_mul(b
, lower
[1], bset
->eq
[j
][0]);
722 if (isl_int_lt(a
, b
) && isl_int_is_pos(bset
->eq
[j
][1]))
723 isl_seq_cpy(lower
, bset
->eq
[j
], 2);
724 if (isl_int_gt(a
, b
) && isl_int_is_neg(bset
->eq
[j
][1]))
725 isl_seq_neg(lower
, bset
->eq
[j
], 2);
728 isl_int_mul(a
, upper
[0], bset
->eq
[j
][1]);
729 isl_int_mul(b
, upper
[1], bset
->eq
[j
][0]);
730 if (isl_int_lt(a
, b
) && isl_int_is_pos(bset
->eq
[j
][1]))
731 isl_seq_neg(upper
, bset
->eq
[j
], 2);
732 if (isl_int_gt(a
, b
) && isl_int_is_neg(bset
->eq
[j
][1]))
733 isl_seq_cpy(upper
, bset
->eq
[j
], 2);
736 for (j
= 0; j
< bset
->n_ineq
; ++j
) {
737 if (isl_int_is_pos(bset
->ineq
[j
][1]))
739 if (isl_int_is_neg(bset
->ineq
[j
][1]))
741 if (lower
&& isl_int_is_pos(bset
->ineq
[j
][1])) {
742 isl_int_mul(a
, lower
[0], bset
->ineq
[j
][1]);
743 isl_int_mul(b
, lower
[1], bset
->ineq
[j
][0]);
744 if (isl_int_lt(a
, b
))
745 isl_seq_cpy(lower
, bset
->ineq
[j
], 2);
747 if (upper
&& isl_int_is_neg(bset
->ineq
[j
][1])) {
748 isl_int_mul(a
, upper
[0], bset
->ineq
[j
][1]);
749 isl_int_mul(b
, upper
[1], bset
->ineq
[j
][0]);
750 if (isl_int_gt(a
, b
))
751 isl_seq_cpy(upper
, bset
->ineq
[j
], 2);
762 hull
= isl_basic_set_alloc(set
->ctx
, 0, 1, 0, 0, 2);
763 hull
= isl_basic_set_set_rational(hull
);
767 k
= isl_basic_set_alloc_inequality(hull
);
768 isl_seq_cpy(hull
->ineq
[k
], lower
, 2);
771 k
= isl_basic_set_alloc_inequality(hull
);
772 isl_seq_cpy(hull
->ineq
[k
], upper
, 2);
774 hull
= isl_basic_set_finalize(hull
);
784 /* Project out final n dimensions using Fourier-Motzkin */
785 static struct isl_set
*set_project_out(struct isl_ctx
*ctx
,
786 struct isl_set
*set
, unsigned n
)
788 return isl_set_remove_dims(set
, isl_set_n_dim(set
) - n
, n
);
791 static struct isl_basic_set
*convex_hull_0d(struct isl_set
*set
)
793 struct isl_basic_set
*convex_hull
;
798 if (isl_set_is_empty(set
))
799 convex_hull
= isl_basic_set_empty(isl_dim_copy(set
->dim
));
801 convex_hull
= isl_basic_set_universe(isl_dim_copy(set
->dim
));
806 /* Compute the convex hull of a pair of basic sets without any parameters or
807 * integer divisions using Fourier-Motzkin elimination.
808 * The convex hull is the set of all points that can be written as
809 * the sum of points from both basic sets (in homogeneous coordinates).
810 * We set up the constraints in a space with dimensions for each of
811 * the three sets and then project out the dimensions corresponding
812 * to the two original basic sets, retaining only those corresponding
813 * to the convex hull.
815 static struct isl_basic_set
*convex_hull_pair_elim(struct isl_basic_set
*bset1
,
816 struct isl_basic_set
*bset2
)
819 struct isl_basic_set
*bset
[2];
820 struct isl_basic_set
*hull
= NULL
;
823 if (!bset1
|| !bset2
)
826 dim
= isl_basic_set_n_dim(bset1
);
827 hull
= isl_basic_set_alloc(bset1
->ctx
, 0, 2 + 3 * dim
, 0,
828 1 + dim
+ bset1
->n_eq
+ bset2
->n_eq
,
829 2 + bset1
->n_ineq
+ bset2
->n_ineq
);
832 for (i
= 0; i
< 2; ++i
) {
833 for (j
= 0; j
< bset
[i
]->n_eq
; ++j
) {
834 k
= isl_basic_set_alloc_equality(hull
);
837 isl_seq_clr(hull
->eq
[k
], (i
+1) * (1+dim
));
838 isl_seq_clr(hull
->eq
[k
]+(i
+2)*(1+dim
), (1-i
)*(1+dim
));
839 isl_seq_cpy(hull
->eq
[k
]+(i
+1)*(1+dim
), bset
[i
]->eq
[j
],
842 for (j
= 0; j
< bset
[i
]->n_ineq
; ++j
) {
843 k
= isl_basic_set_alloc_inequality(hull
);
846 isl_seq_clr(hull
->ineq
[k
], (i
+1) * (1+dim
));
847 isl_seq_clr(hull
->ineq
[k
]+(i
+2)*(1+dim
), (1-i
)*(1+dim
));
848 isl_seq_cpy(hull
->ineq
[k
]+(i
+1)*(1+dim
),
849 bset
[i
]->ineq
[j
], 1+dim
);
851 k
= isl_basic_set_alloc_inequality(hull
);
854 isl_seq_clr(hull
->ineq
[k
], 1+2+3*dim
);
855 isl_int_set_si(hull
->ineq
[k
][(i
+1)*(1+dim
)], 1);
857 for (j
= 0; j
< 1+dim
; ++j
) {
858 k
= isl_basic_set_alloc_equality(hull
);
861 isl_seq_clr(hull
->eq
[k
], 1+2+3*dim
);
862 isl_int_set_si(hull
->eq
[k
][j
], -1);
863 isl_int_set_si(hull
->eq
[k
][1+dim
+j
], 1);
864 isl_int_set_si(hull
->eq
[k
][2*(1+dim
)+j
], 1);
866 hull
= isl_basic_set_set_rational(hull
);
867 hull
= isl_basic_set_remove_dims(hull
, dim
, 2*(1+dim
));
868 hull
= isl_basic_set_convex_hull(hull
);
869 isl_basic_set_free(bset1
);
870 isl_basic_set_free(bset2
);
873 isl_basic_set_free(bset1
);
874 isl_basic_set_free(bset2
);
875 isl_basic_set_free(hull
);
879 /* Is the set bounded for each value of the parameters?
881 int isl_basic_set_is_bounded(__isl_keep isl_basic_set
*bset
)
888 if (isl_basic_set_fast_is_empty(bset
))
891 tab
= isl_tab_from_recession_cone(bset
, 1);
892 bounded
= isl_tab_cone_is_bounded(tab
);
897 /* Is the set bounded for each value of the parameters?
899 int isl_set_is_bounded(__isl_keep isl_set
*set
)
906 for (i
= 0; i
< set
->n
; ++i
) {
907 int bounded
= isl_basic_set_is_bounded(set
->p
[i
]);
908 if (!bounded
|| bounded
< 0)
914 /* Compute the lineality space of the convex hull of bset1 and bset2.
916 * We first compute the intersection of the recession cone of bset1
917 * with the negative of the recession cone of bset2 and then compute
918 * the linear hull of the resulting cone.
920 static struct isl_basic_set
*induced_lineality_space(
921 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
924 struct isl_basic_set
*lin
= NULL
;
927 if (!bset1
|| !bset2
)
930 dim
= isl_basic_set_total_dim(bset1
);
931 lin
= isl_basic_set_alloc_dim(isl_basic_set_get_dim(bset1
), 0,
932 bset1
->n_eq
+ bset2
->n_eq
,
933 bset1
->n_ineq
+ bset2
->n_ineq
);
934 lin
= isl_basic_set_set_rational(lin
);
937 for (i
= 0; i
< bset1
->n_eq
; ++i
) {
938 k
= isl_basic_set_alloc_equality(lin
);
941 isl_int_set_si(lin
->eq
[k
][0], 0);
942 isl_seq_cpy(lin
->eq
[k
] + 1, bset1
->eq
[i
] + 1, dim
);
944 for (i
= 0; i
< bset1
->n_ineq
; ++i
) {
945 k
= isl_basic_set_alloc_inequality(lin
);
948 isl_int_set_si(lin
->ineq
[k
][0], 0);
949 isl_seq_cpy(lin
->ineq
[k
] + 1, bset1
->ineq
[i
] + 1, dim
);
951 for (i
= 0; i
< bset2
->n_eq
; ++i
) {
952 k
= isl_basic_set_alloc_equality(lin
);
955 isl_int_set_si(lin
->eq
[k
][0], 0);
956 isl_seq_neg(lin
->eq
[k
] + 1, bset2
->eq
[i
] + 1, dim
);
958 for (i
= 0; i
< bset2
->n_ineq
; ++i
) {
959 k
= isl_basic_set_alloc_inequality(lin
);
962 isl_int_set_si(lin
->ineq
[k
][0], 0);
963 isl_seq_neg(lin
->ineq
[k
] + 1, bset2
->ineq
[i
] + 1, dim
);
966 isl_basic_set_free(bset1
);
967 isl_basic_set_free(bset2
);
968 return isl_basic_set_affine_hull(lin
);
970 isl_basic_set_free(lin
);
971 isl_basic_set_free(bset1
);
972 isl_basic_set_free(bset2
);
976 static struct isl_basic_set
*uset_convex_hull(struct isl_set
*set
);
978 /* Given a set and a linear space "lin" of dimension n > 0,
979 * project the linear space from the set, compute the convex hull
980 * and then map the set back to the original space.
986 * describe the linear space. We first compute the Hermite normal
987 * form H = M U of M = H Q, to obtain
991 * The last n rows of H will be zero, so the last n variables of x' = Q x
992 * are the one we want to project out. We do this by transforming each
993 * basic set A x >= b to A U x' >= b and then removing the last n dimensions.
994 * After computing the convex hull in x'_1, i.e., A' x'_1 >= b',
995 * we transform the hull back to the original space as A' Q_1 x >= b',
996 * with Q_1 all but the last n rows of Q.
998 static struct isl_basic_set
*modulo_lineality(struct isl_set
*set
,
999 struct isl_basic_set
*lin
)
1001 unsigned total
= isl_basic_set_total_dim(lin
);
1003 struct isl_basic_set
*hull
;
1004 struct isl_mat
*M
, *U
, *Q
;
1008 lin_dim
= total
- lin
->n_eq
;
1009 M
= isl_mat_sub_alloc(set
->ctx
, lin
->eq
, 0, lin
->n_eq
, 1, total
);
1010 M
= isl_mat_left_hermite(M
, 0, &U
, &Q
);
1014 isl_basic_set_free(lin
);
1016 Q
= isl_mat_drop_rows(Q
, Q
->n_row
- lin_dim
, lin_dim
);
1018 U
= isl_mat_lin_to_aff(U
);
1019 Q
= isl_mat_lin_to_aff(Q
);
1021 set
= isl_set_preimage(set
, U
);
1022 set
= isl_set_remove_dims(set
, total
- lin_dim
, lin_dim
);
1023 hull
= uset_convex_hull(set
);
1024 hull
= isl_basic_set_preimage(hull
, Q
);
1028 isl_basic_set_free(lin
);
1033 /* Given two polyhedra with as constraints h_{ij} x >= 0 in homegeneous space,
1034 * set up an LP for solving
1036 * \sum_j \alpha_{1j} h_{1j} = \sum_j \alpha_{2j} h_{2j}
1038 * \alpha{i0} corresponds to the (implicit) positivity constraint 1 >= 0
1039 * The next \alpha{ij} correspond to the equalities and come in pairs.
1040 * The final \alpha{ij} correspond to the inequalities.
1042 static struct isl_basic_set
*valid_direction_lp(
1043 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
1045 struct isl_dim
*dim
;
1046 struct isl_basic_set
*lp
;
1051 if (!bset1
|| !bset2
)
1053 d
= 1 + isl_basic_set_total_dim(bset1
);
1055 2 * bset1
->n_eq
+ bset1
->n_ineq
+ 2 * bset2
->n_eq
+ bset2
->n_ineq
;
1056 dim
= isl_dim_set_alloc(bset1
->ctx
, 0, n
);
1057 lp
= isl_basic_set_alloc_dim(dim
, 0, d
, n
);
1060 for (i
= 0; i
< n
; ++i
) {
1061 k
= isl_basic_set_alloc_inequality(lp
);
1064 isl_seq_clr(lp
->ineq
[k
] + 1, n
);
1065 isl_int_set_si(lp
->ineq
[k
][0], -1);
1066 isl_int_set_si(lp
->ineq
[k
][1 + i
], 1);
1068 for (i
= 0; i
< d
; ++i
) {
1069 k
= isl_basic_set_alloc_equality(lp
);
1073 isl_int_set_si(lp
->eq
[k
][n
++], 0);
1074 /* positivity constraint 1 >= 0 */
1075 isl_int_set_si(lp
->eq
[k
][n
++], i
== 0);
1076 for (j
= 0; j
< bset1
->n_eq
; ++j
) {
1077 isl_int_set(lp
->eq
[k
][n
++], bset1
->eq
[j
][i
]);
1078 isl_int_neg(lp
->eq
[k
][n
++], bset1
->eq
[j
][i
]);
1080 for (j
= 0; j
< bset1
->n_ineq
; ++j
)
1081 isl_int_set(lp
->eq
[k
][n
++], bset1
->ineq
[j
][i
]);
1082 /* positivity constraint 1 >= 0 */
1083 isl_int_set_si(lp
->eq
[k
][n
++], -(i
== 0));
1084 for (j
= 0; j
< bset2
->n_eq
; ++j
) {
1085 isl_int_neg(lp
->eq
[k
][n
++], bset2
->eq
[j
][i
]);
1086 isl_int_set(lp
->eq
[k
][n
++], bset2
->eq
[j
][i
]);
1088 for (j
= 0; j
< bset2
->n_ineq
; ++j
)
1089 isl_int_neg(lp
->eq
[k
][n
++], bset2
->ineq
[j
][i
]);
1091 lp
= isl_basic_set_gauss(lp
, NULL
);
1092 isl_basic_set_free(bset1
);
1093 isl_basic_set_free(bset2
);
1096 isl_basic_set_free(bset1
);
1097 isl_basic_set_free(bset2
);
1101 /* Compute a vector s in the homogeneous space such that <s, r> > 0
1102 * for all rays in the homogeneous space of the two cones that correspond
1103 * to the input polyhedra bset1 and bset2.
1105 * We compute s as a vector that satisfies
1107 * s = \sum_j \alpha_{ij} h_{ij} for i = 1,2 (*)
1109 * with h_{ij} the normals of the facets of polyhedron i
1110 * (including the "positivity constraint" 1 >= 0) and \alpha_{ij}
1111 * strictly positive numbers. For simplicity we impose \alpha_{ij} >= 1.
1112 * We first set up an LP with as variables the \alpha{ij}.
1113 * In this formulation, for each polyhedron i,
1114 * the first constraint is the positivity constraint, followed by pairs
1115 * of variables for the equalities, followed by variables for the inequalities.
1116 * We then simply pick a feasible solution and compute s using (*).
1118 * Note that we simply pick any valid direction and make no attempt
1119 * to pick a "good" or even the "best" valid direction.
1121 static struct isl_vec
*valid_direction(
1122 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
1124 struct isl_basic_set
*lp
;
1125 struct isl_tab
*tab
;
1126 struct isl_vec
*sample
= NULL
;
1127 struct isl_vec
*dir
;
1132 if (!bset1
|| !bset2
)
1134 lp
= valid_direction_lp(isl_basic_set_copy(bset1
),
1135 isl_basic_set_copy(bset2
));
1136 tab
= isl_tab_from_basic_set(lp
);
1137 sample
= isl_tab_get_sample_value(tab
);
1139 isl_basic_set_free(lp
);
1142 d
= isl_basic_set_total_dim(bset1
);
1143 dir
= isl_vec_alloc(bset1
->ctx
, 1 + d
);
1146 isl_seq_clr(dir
->block
.data
+ 1, dir
->size
- 1);
1148 /* positivity constraint 1 >= 0 */
1149 isl_int_set(dir
->block
.data
[0], sample
->block
.data
[n
++]);
1150 for (i
= 0; i
< bset1
->n_eq
; ++i
) {
1151 isl_int_sub(sample
->block
.data
[n
],
1152 sample
->block
.data
[n
], sample
->block
.data
[n
+1]);
1153 isl_seq_combine(dir
->block
.data
,
1154 bset1
->ctx
->one
, dir
->block
.data
,
1155 sample
->block
.data
[n
], bset1
->eq
[i
], 1 + d
);
1159 for (i
= 0; i
< bset1
->n_ineq
; ++i
)
1160 isl_seq_combine(dir
->block
.data
,
1161 bset1
->ctx
->one
, dir
->block
.data
,
1162 sample
->block
.data
[n
++], bset1
->ineq
[i
], 1 + d
);
1163 isl_vec_free(sample
);
1164 isl_seq_normalize(bset1
->ctx
, dir
->el
, dir
->size
);
1165 isl_basic_set_free(bset1
);
1166 isl_basic_set_free(bset2
);
1169 isl_vec_free(sample
);
1170 isl_basic_set_free(bset1
);
1171 isl_basic_set_free(bset2
);
1175 /* Given a polyhedron b_i + A_i x >= 0 and a map T = S^{-1},
1176 * compute b_i' + A_i' x' >= 0, with
1178 * [ b_i A_i ] [ y' ] [ y' ]
1179 * [ 1 0 ] S^{-1} [ x' ] >= 0 or [ b_i' A_i' ] [ x' ] >= 0
1181 * In particular, add the "positivity constraint" and then perform
1184 static struct isl_basic_set
*homogeneous_map(struct isl_basic_set
*bset
,
1191 bset
= isl_basic_set_extend_constraints(bset
, 0, 1);
1192 k
= isl_basic_set_alloc_inequality(bset
);
1195 isl_seq_clr(bset
->ineq
[k
] + 1, isl_basic_set_total_dim(bset
));
1196 isl_int_set_si(bset
->ineq
[k
][0], 1);
1197 bset
= isl_basic_set_preimage(bset
, T
);
1201 isl_basic_set_free(bset
);
1205 /* Compute the convex hull of a pair of basic sets without any parameters or
1206 * integer divisions, where the convex hull is known to be pointed,
1207 * but the basic sets may be unbounded.
1209 * We turn this problem into the computation of a convex hull of a pair
1210 * _bounded_ polyhedra by "changing the direction of the homogeneous
1211 * dimension". This idea is due to Matthias Koeppe.
1213 * Consider the cones in homogeneous space that correspond to the
1214 * input polyhedra. The rays of these cones are also rays of the
1215 * polyhedra if the coordinate that corresponds to the homogeneous
1216 * dimension is zero. That is, if the inner product of the rays
1217 * with the homogeneous direction is zero.
1218 * The cones in the homogeneous space can also be considered to
1219 * correspond to other pairs of polyhedra by chosing a different
1220 * homogeneous direction. To ensure that both of these polyhedra
1221 * are bounded, we need to make sure that all rays of the cones
1222 * correspond to vertices and not to rays.
1223 * Let s be a direction such that <s, r> > 0 for all rays r of both cones.
1224 * Then using s as a homogeneous direction, we obtain a pair of polytopes.
1225 * The vector s is computed in valid_direction.
1227 * Note that we need to consider _all_ rays of the cones and not just
1228 * the rays that correspond to rays in the polyhedra. If we were to
1229 * only consider those rays and turn them into vertices, then we
1230 * may inadvertently turn some vertices into rays.
1232 * The standard homogeneous direction is the unit vector in the 0th coordinate.
1233 * We therefore transform the two polyhedra such that the selected
1234 * direction is mapped onto this standard direction and then proceed
1235 * with the normal computation.
1236 * Let S be a non-singular square matrix with s as its first row,
1237 * then we want to map the polyhedra to the space
1239 * [ y' ] [ y ] [ y ] [ y' ]
1240 * [ x' ] = S [ x ] i.e., [ x ] = S^{-1} [ x' ]
1242 * We take S to be the unimodular completion of s to limit the growth
1243 * of the coefficients in the following computations.
1245 * Let b_i + A_i x >= 0 be the constraints of polyhedron i.
1246 * We first move to the homogeneous dimension
1248 * b_i y + A_i x >= 0 [ b_i A_i ] [ y ] [ 0 ]
1249 * y >= 0 or [ 1 0 ] [ x ] >= [ 0 ]
1251 * Then we change directoin
1253 * [ b_i A_i ] [ y' ] [ y' ]
1254 * [ 1 0 ] S^{-1} [ x' ] >= 0 or [ b_i' A_i' ] [ x' ] >= 0
1256 * Then we compute the convex hull of the polytopes b_i' + A_i' x' >= 0
1257 * resulting in b' + A' x' >= 0, which we then convert back
1260 * [ b' A' ] S [ x ] >= 0 or [ b A ] [ x ] >= 0
1262 * The polyhedron b + A x >= 0 is then the convex hull of the input polyhedra.
1264 static struct isl_basic_set
*convex_hull_pair_pointed(
1265 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
1267 struct isl_ctx
*ctx
= NULL
;
1268 struct isl_vec
*dir
= NULL
;
1269 struct isl_mat
*T
= NULL
;
1270 struct isl_mat
*T2
= NULL
;
1271 struct isl_basic_set
*hull
;
1272 struct isl_set
*set
;
1274 if (!bset1
|| !bset2
)
1277 dir
= valid_direction(isl_basic_set_copy(bset1
),
1278 isl_basic_set_copy(bset2
));
1281 T
= isl_mat_alloc(bset1
->ctx
, dir
->size
, dir
->size
);
1284 isl_seq_cpy(T
->row
[0], dir
->block
.data
, dir
->size
);
1285 T
= isl_mat_unimodular_complete(T
, 1);
1286 T2
= isl_mat_right_inverse(isl_mat_copy(T
));
1288 bset1
= homogeneous_map(bset1
, isl_mat_copy(T2
));
1289 bset2
= homogeneous_map(bset2
, T2
);
1290 set
= isl_set_alloc_dim(isl_basic_set_get_dim(bset1
), 2, 0);
1291 set
= isl_set_add_basic_set(set
, bset1
);
1292 set
= isl_set_add_basic_set(set
, bset2
);
1293 hull
= uset_convex_hull(set
);
1294 hull
= isl_basic_set_preimage(hull
, T
);
1301 isl_basic_set_free(bset1
);
1302 isl_basic_set_free(bset2
);
1306 static struct isl_basic_set
*uset_convex_hull_wrap(struct isl_set
*set
);
1307 static struct isl_basic_set
*modulo_affine_hull(
1308 struct isl_set
*set
, struct isl_basic_set
*affine_hull
);
1310 /* Compute the convex hull of a pair of basic sets without any parameters or
1311 * integer divisions.
1313 * This function is called from uset_convex_hull_unbounded, which
1314 * means that the complete convex hull is unbounded. Some pairs
1315 * of basic sets may still be bounded, though.
1316 * They may even lie inside a lower dimensional space, in which
1317 * case they need to be handled inside their affine hull since
1318 * the main algorithm assumes that the result is full-dimensional.
1320 * If the convex hull of the two basic sets would have a non-trivial
1321 * lineality space, we first project out this lineality space.
1323 static struct isl_basic_set
*convex_hull_pair(struct isl_basic_set
*bset1
,
1324 struct isl_basic_set
*bset2
)
1326 isl_basic_set
*lin
, *aff
;
1327 int bounded1
, bounded2
;
1329 aff
= isl_set_affine_hull(isl_basic_set_union(isl_basic_set_copy(bset1
),
1330 isl_basic_set_copy(bset2
)));
1334 return modulo_affine_hull(isl_basic_set_union(bset1
, bset2
), aff
);
1335 isl_basic_set_free(aff
);
1337 bounded1
= isl_basic_set_is_bounded(bset1
);
1338 bounded2
= isl_basic_set_is_bounded(bset2
);
1340 if (bounded1
< 0 || bounded2
< 0)
1343 if (bounded1
&& bounded2
)
1344 uset_convex_hull_wrap(isl_basic_set_union(bset1
, bset2
));
1346 if (bounded1
|| bounded2
)
1347 return convex_hull_pair_pointed(bset1
, bset2
);
1349 lin
= induced_lineality_space(isl_basic_set_copy(bset1
),
1350 isl_basic_set_copy(bset2
));
1353 if (isl_basic_set_is_universe(lin
)) {
1354 isl_basic_set_free(bset1
);
1355 isl_basic_set_free(bset2
);
1358 if (lin
->n_eq
< isl_basic_set_total_dim(lin
)) {
1359 struct isl_set
*set
;
1360 set
= isl_set_alloc_dim(isl_basic_set_get_dim(bset1
), 2, 0);
1361 set
= isl_set_add_basic_set(set
, bset1
);
1362 set
= isl_set_add_basic_set(set
, bset2
);
1363 return modulo_lineality(set
, lin
);
1365 isl_basic_set_free(lin
);
1367 return convex_hull_pair_pointed(bset1
, bset2
);
1369 isl_basic_set_free(bset1
);
1370 isl_basic_set_free(bset2
);
1374 /* Compute the lineality space of a basic set.
1375 * We currently do not allow the basic set to have any divs.
1376 * We basically just drop the constants and turn every inequality
1379 struct isl_basic_set
*isl_basic_set_lineality_space(struct isl_basic_set
*bset
)
1382 struct isl_basic_set
*lin
= NULL
;
1387 isl_assert(bset
->ctx
, bset
->n_div
== 0, goto error
);
1388 dim
= isl_basic_set_total_dim(bset
);
1390 lin
= isl_basic_set_alloc_dim(isl_basic_set_get_dim(bset
), 0, dim
, 0);
1393 for (i
= 0; i
< bset
->n_eq
; ++i
) {
1394 k
= isl_basic_set_alloc_equality(lin
);
1397 isl_int_set_si(lin
->eq
[k
][0], 0);
1398 isl_seq_cpy(lin
->eq
[k
] + 1, bset
->eq
[i
] + 1, dim
);
1400 lin
= isl_basic_set_gauss(lin
, NULL
);
1403 for (i
= 0; i
< bset
->n_ineq
&& lin
->n_eq
< dim
; ++i
) {
1404 k
= isl_basic_set_alloc_equality(lin
);
1407 isl_int_set_si(lin
->eq
[k
][0], 0);
1408 isl_seq_cpy(lin
->eq
[k
] + 1, bset
->ineq
[i
] + 1, dim
);
1409 lin
= isl_basic_set_gauss(lin
, NULL
);
1413 isl_basic_set_free(bset
);
1416 isl_basic_set_free(lin
);
1417 isl_basic_set_free(bset
);
1421 /* Compute the (linear) hull of the lineality spaces of the basic sets in the
1422 * "underlying" set "set".
1424 static struct isl_basic_set
*uset_combined_lineality_space(struct isl_set
*set
)
1427 struct isl_set
*lin
= NULL
;
1432 struct isl_dim
*dim
= isl_set_get_dim(set
);
1434 return isl_basic_set_empty(dim
);
1437 lin
= isl_set_alloc_dim(isl_set_get_dim(set
), set
->n
, 0);
1438 for (i
= 0; i
< set
->n
; ++i
)
1439 lin
= isl_set_add_basic_set(lin
,
1440 isl_basic_set_lineality_space(isl_basic_set_copy(set
->p
[i
])));
1442 return isl_set_affine_hull(lin
);
1445 /* Compute the convex hull of a set without any parameters or
1446 * integer divisions.
1447 * In each step, we combined two basic sets until only one
1448 * basic set is left.
1449 * The input basic sets are assumed not to have a non-trivial
1450 * lineality space. If any of the intermediate results has
1451 * a non-trivial lineality space, it is projected out.
1453 static struct isl_basic_set
*uset_convex_hull_unbounded(struct isl_set
*set
)
1455 struct isl_basic_set
*convex_hull
= NULL
;
1457 convex_hull
= isl_set_copy_basic_set(set
);
1458 set
= isl_set_drop_basic_set(set
, convex_hull
);
1461 while (set
->n
> 0) {
1462 struct isl_basic_set
*t
;
1463 t
= isl_set_copy_basic_set(set
);
1466 set
= isl_set_drop_basic_set(set
, t
);
1469 convex_hull
= convex_hull_pair(convex_hull
, t
);
1472 t
= isl_basic_set_lineality_space(isl_basic_set_copy(convex_hull
));
1475 if (isl_basic_set_is_universe(t
)) {
1476 isl_basic_set_free(convex_hull
);
1480 if (t
->n_eq
< isl_basic_set_total_dim(t
)) {
1481 set
= isl_set_add_basic_set(set
, convex_hull
);
1482 return modulo_lineality(set
, t
);
1484 isl_basic_set_free(t
);
1490 isl_basic_set_free(convex_hull
);
1494 /* Compute an initial hull for wrapping containing a single initial
1496 * This function assumes that the given set is bounded.
1498 static struct isl_basic_set
*initial_hull(struct isl_basic_set
*hull
,
1499 struct isl_set
*set
)
1501 struct isl_mat
*bounds
= NULL
;
1507 bounds
= initial_facet_constraint(set
);
1510 k
= isl_basic_set_alloc_inequality(hull
);
1513 dim
= isl_set_n_dim(set
);
1514 isl_assert(set
->ctx
, 1 + dim
== bounds
->n_col
, goto error
);
1515 isl_seq_cpy(hull
->ineq
[k
], bounds
->row
[0], bounds
->n_col
);
1516 isl_mat_free(bounds
);
1520 isl_basic_set_free(hull
);
1521 isl_mat_free(bounds
);
1525 struct max_constraint
{
1531 static int max_constraint_equal(const void *entry
, const void *val
)
1533 struct max_constraint
*a
= (struct max_constraint
*)entry
;
1534 isl_int
*b
= (isl_int
*)val
;
1536 return isl_seq_eq(a
->c
->row
[0] + 1, b
, a
->c
->n_col
- 1);
1539 static void update_constraint(struct isl_ctx
*ctx
, struct isl_hash_table
*table
,
1540 isl_int
*con
, unsigned len
, int n
, int ineq
)
1542 struct isl_hash_table_entry
*entry
;
1543 struct max_constraint
*c
;
1546 c_hash
= isl_seq_get_hash(con
+ 1, len
);
1547 entry
= isl_hash_table_find(ctx
, table
, c_hash
, max_constraint_equal
,
1553 isl_hash_table_remove(ctx
, table
, entry
);
1557 if (isl_int_gt(c
->c
->row
[0][0], con
[0]))
1559 if (isl_int_eq(c
->c
->row
[0][0], con
[0])) {
1564 c
->c
= isl_mat_cow(c
->c
);
1565 isl_int_set(c
->c
->row
[0][0], con
[0]);
1569 /* Check whether the constraint hash table "table" constains the constraint
1572 static int has_constraint(struct isl_ctx
*ctx
, struct isl_hash_table
*table
,
1573 isl_int
*con
, unsigned len
, int n
)
1575 struct isl_hash_table_entry
*entry
;
1576 struct max_constraint
*c
;
1579 c_hash
= isl_seq_get_hash(con
+ 1, len
);
1580 entry
= isl_hash_table_find(ctx
, table
, c_hash
, max_constraint_equal
,
1587 return isl_int_eq(c
->c
->row
[0][0], con
[0]);
1590 /* Check for inequality constraints of a basic set without equalities
1591 * such that the same or more stringent copies of the constraint appear
1592 * in all of the basic sets. Such constraints are necessarily facet
1593 * constraints of the convex hull.
1595 * If the resulting basic set is by chance identical to one of
1596 * the basic sets in "set", then we know that this basic set contains
1597 * all other basic sets and is therefore the convex hull of set.
1598 * In this case we set *is_hull to 1.
1600 static struct isl_basic_set
*common_constraints(struct isl_basic_set
*hull
,
1601 struct isl_set
*set
, int *is_hull
)
1604 int min_constraints
;
1606 struct max_constraint
*constraints
= NULL
;
1607 struct isl_hash_table
*table
= NULL
;
1612 for (i
= 0; i
< set
->n
; ++i
)
1613 if (set
->p
[i
]->n_eq
== 0)
1617 min_constraints
= set
->p
[i
]->n_ineq
;
1619 for (i
= best
+ 1; i
< set
->n
; ++i
) {
1620 if (set
->p
[i
]->n_eq
!= 0)
1622 if (set
->p
[i
]->n_ineq
>= min_constraints
)
1624 min_constraints
= set
->p
[i
]->n_ineq
;
1627 constraints
= isl_calloc_array(hull
->ctx
, struct max_constraint
,
1631 table
= isl_alloc_type(hull
->ctx
, struct isl_hash_table
);
1632 if (isl_hash_table_init(hull
->ctx
, table
, min_constraints
))
1635 total
= isl_dim_total(set
->dim
);
1636 for (i
= 0; i
< set
->p
[best
]->n_ineq
; ++i
) {
1637 constraints
[i
].c
= isl_mat_sub_alloc(hull
->ctx
,
1638 set
->p
[best
]->ineq
+ i
, 0, 1, 0, 1 + total
);
1639 if (!constraints
[i
].c
)
1641 constraints
[i
].ineq
= 1;
1643 for (i
= 0; i
< min_constraints
; ++i
) {
1644 struct isl_hash_table_entry
*entry
;
1646 c_hash
= isl_seq_get_hash(constraints
[i
].c
->row
[0] + 1, total
);
1647 entry
= isl_hash_table_find(hull
->ctx
, table
, c_hash
,
1648 max_constraint_equal
, constraints
[i
].c
->row
[0] + 1, 1);
1651 isl_assert(hull
->ctx
, !entry
->data
, goto error
);
1652 entry
->data
= &constraints
[i
];
1656 for (s
= 0; s
< set
->n
; ++s
) {
1660 for (i
= 0; i
< set
->p
[s
]->n_eq
; ++i
) {
1661 isl_int
*eq
= set
->p
[s
]->eq
[i
];
1662 for (j
= 0; j
< 2; ++j
) {
1663 isl_seq_neg(eq
, eq
, 1 + total
);
1664 update_constraint(hull
->ctx
, table
,
1668 for (i
= 0; i
< set
->p
[s
]->n_ineq
; ++i
) {
1669 isl_int
*ineq
= set
->p
[s
]->ineq
[i
];
1670 update_constraint(hull
->ctx
, table
, ineq
, total
, n
,
1671 set
->p
[s
]->n_eq
== 0);
1676 for (i
= 0; i
< min_constraints
; ++i
) {
1677 if (constraints
[i
].count
< n
)
1679 if (!constraints
[i
].ineq
)
1681 j
= isl_basic_set_alloc_inequality(hull
);
1684 isl_seq_cpy(hull
->ineq
[j
], constraints
[i
].c
->row
[0], 1 + total
);
1687 for (s
= 0; s
< set
->n
; ++s
) {
1688 if (set
->p
[s
]->n_eq
)
1690 if (set
->p
[s
]->n_ineq
!= hull
->n_ineq
)
1692 for (i
= 0; i
< set
->p
[s
]->n_ineq
; ++i
) {
1693 isl_int
*ineq
= set
->p
[s
]->ineq
[i
];
1694 if (!has_constraint(hull
->ctx
, table
, ineq
, total
, n
))
1697 if (i
== set
->p
[s
]->n_ineq
)
1701 isl_hash_table_clear(table
);
1702 for (i
= 0; i
< min_constraints
; ++i
)
1703 isl_mat_free(constraints
[i
].c
);
1708 isl_hash_table_clear(table
);
1711 for (i
= 0; i
< min_constraints
; ++i
)
1712 isl_mat_free(constraints
[i
].c
);
1717 /* Create a template for the convex hull of "set" and fill it up
1718 * obvious facet constraints, if any. If the result happens to
1719 * be the convex hull of "set" then *is_hull is set to 1.
1721 static struct isl_basic_set
*proto_hull(struct isl_set
*set
, int *is_hull
)
1723 struct isl_basic_set
*hull
;
1728 for (i
= 0; i
< set
->n
; ++i
) {
1729 n_ineq
+= set
->p
[i
]->n_eq
;
1730 n_ineq
+= set
->p
[i
]->n_ineq
;
1732 hull
= isl_basic_set_alloc_dim(isl_dim_copy(set
->dim
), 0, 0, n_ineq
);
1733 hull
= isl_basic_set_set_rational(hull
);
1736 return common_constraints(hull
, set
, is_hull
);
1739 static struct isl_basic_set
*uset_convex_hull_wrap(struct isl_set
*set
)
1741 struct isl_basic_set
*hull
;
1744 hull
= proto_hull(set
, &is_hull
);
1745 if (hull
&& !is_hull
) {
1746 if (hull
->n_ineq
== 0)
1747 hull
= initial_hull(hull
, set
);
1748 hull
= extend(hull
, set
);
1755 /* Compute the convex hull of a set without any parameters or
1756 * integer divisions. Depending on whether the set is bounded,
1757 * we pass control to the wrapping based convex hull or
1758 * the Fourier-Motzkin elimination based convex hull.
1759 * We also handle a few special cases before checking the boundedness.
1761 static struct isl_basic_set
*uset_convex_hull(struct isl_set
*set
)
1763 struct isl_basic_set
*convex_hull
= NULL
;
1764 struct isl_basic_set
*lin
;
1766 if (isl_set_n_dim(set
) == 0)
1767 return convex_hull_0d(set
);
1769 set
= isl_set_coalesce(set
);
1770 set
= isl_set_set_rational(set
);
1777 convex_hull
= isl_basic_set_copy(set
->p
[0]);
1781 if (isl_set_n_dim(set
) == 1)
1782 return convex_hull_1d(set
);
1784 if (isl_set_is_bounded(set
))
1785 return uset_convex_hull_wrap(set
);
1787 lin
= uset_combined_lineality_space(isl_set_copy(set
));
1790 if (isl_basic_set_is_universe(lin
)) {
1794 if (lin
->n_eq
< isl_basic_set_total_dim(lin
))
1795 return modulo_lineality(set
, lin
);
1796 isl_basic_set_free(lin
);
1798 return uset_convex_hull_unbounded(set
);
1801 isl_basic_set_free(convex_hull
);
1805 /* This is the core procedure, where "set" is a "pure" set, i.e.,
1806 * without parameters or divs and where the convex hull of set is
1807 * known to be full-dimensional.
1809 static struct isl_basic_set
*uset_convex_hull_wrap_bounded(struct isl_set
*set
)
1811 struct isl_basic_set
*convex_hull
= NULL
;
1816 if (isl_set_n_dim(set
) == 0) {
1817 convex_hull
= isl_basic_set_universe(isl_dim_copy(set
->dim
));
1819 convex_hull
= isl_basic_set_set_rational(convex_hull
);
1823 set
= isl_set_set_rational(set
);
1824 set
= isl_set_coalesce(set
);
1828 convex_hull
= isl_basic_set_copy(set
->p
[0]);
1832 if (isl_set_n_dim(set
) == 1)
1833 return convex_hull_1d(set
);
1835 return uset_convex_hull_wrap(set
);
1841 /* Compute the convex hull of set "set" with affine hull "affine_hull",
1842 * We first remove the equalities (transforming the set), compute the
1843 * convex hull of the transformed set and then add the equalities back
1844 * (after performing the inverse transformation.
1846 static struct isl_basic_set
*modulo_affine_hull(
1847 struct isl_set
*set
, struct isl_basic_set
*affine_hull
)
1851 struct isl_basic_set
*dummy
;
1852 struct isl_basic_set
*convex_hull
;
1854 dummy
= isl_basic_set_remove_equalities(
1855 isl_basic_set_copy(affine_hull
), &T
, &T2
);
1858 isl_basic_set_free(dummy
);
1859 set
= isl_set_preimage(set
, T
);
1860 convex_hull
= uset_convex_hull(set
);
1861 convex_hull
= isl_basic_set_preimage(convex_hull
, T2
);
1862 convex_hull
= isl_basic_set_intersect(convex_hull
, affine_hull
);
1865 isl_basic_set_free(affine_hull
);
1870 /* Compute the convex hull of a map.
1872 * The implementation was inspired by "Extended Convex Hull" by Fukuda et al.,
1873 * specifically, the wrapping of facets to obtain new facets.
1875 struct isl_basic_map
*isl_map_convex_hull(struct isl_map
*map
)
1877 struct isl_basic_set
*bset
;
1878 struct isl_basic_map
*model
= NULL
;
1879 struct isl_basic_set
*affine_hull
= NULL
;
1880 struct isl_basic_map
*convex_hull
= NULL
;
1881 struct isl_set
*set
= NULL
;
1882 struct isl_ctx
*ctx
;
1889 convex_hull
= isl_basic_map_empty_like_map(map
);
1894 map
= isl_map_detect_equalities(map
);
1895 map
= isl_map_align_divs(map
);
1898 model
= isl_basic_map_copy(map
->p
[0]);
1899 set
= isl_map_underlying_set(map
);
1903 affine_hull
= isl_set_affine_hull(isl_set_copy(set
));
1906 if (affine_hull
->n_eq
!= 0)
1907 bset
= modulo_affine_hull(set
, affine_hull
);
1909 isl_basic_set_free(affine_hull
);
1910 bset
= uset_convex_hull(set
);
1913 convex_hull
= isl_basic_map_overlying_set(bset
, model
);
1917 ISL_F_SET(convex_hull
, ISL_BASIC_MAP_NO_IMPLICIT
);
1918 ISL_F_SET(convex_hull
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1919 ISL_F_CLR(convex_hull
, ISL_BASIC_MAP_RATIONAL
);
1923 isl_basic_map_free(model
);
1927 struct isl_basic_set
*isl_set_convex_hull(struct isl_set
*set
)
1929 return (struct isl_basic_set
*)
1930 isl_map_convex_hull((struct isl_map
*)set
);
1933 struct sh_data_entry
{
1934 struct isl_hash_table
*table
;
1935 struct isl_tab
*tab
;
1938 /* Holds the data needed during the simple hull computation.
1940 * n the number of basic sets in the original set
1941 * hull_table a hash table of already computed constraints
1942 * in the simple hull
1943 * p for each basic set,
1944 * table a hash table of the constraints
1945 * tab the tableau corresponding to the basic set
1948 struct isl_ctx
*ctx
;
1950 struct isl_hash_table
*hull_table
;
1951 struct sh_data_entry p
[1];
1954 static void sh_data_free(struct sh_data
*data
)
1960 isl_hash_table_free(data
->ctx
, data
->hull_table
);
1961 for (i
= 0; i
< data
->n
; ++i
) {
1962 isl_hash_table_free(data
->ctx
, data
->p
[i
].table
);
1963 isl_tab_free(data
->p
[i
].tab
);
1968 struct ineq_cmp_data
{
1973 static int has_ineq(const void *entry
, const void *val
)
1975 isl_int
*row
= (isl_int
*)entry
;
1976 struct ineq_cmp_data
*v
= (struct ineq_cmp_data
*)val
;
1978 return isl_seq_eq(row
+ 1, v
->p
+ 1, v
->len
) ||
1979 isl_seq_is_neg(row
+ 1, v
->p
+ 1, v
->len
);
1982 static int hash_ineq(struct isl_ctx
*ctx
, struct isl_hash_table
*table
,
1983 isl_int
*ineq
, unsigned len
)
1986 struct ineq_cmp_data v
;
1987 struct isl_hash_table_entry
*entry
;
1991 c_hash
= isl_seq_get_hash(ineq
+ 1, len
);
1992 entry
= isl_hash_table_find(ctx
, table
, c_hash
, has_ineq
, &v
, 1);
1999 /* Fill hash table "table" with the constraints of "bset".
2000 * Equalities are added as two inequalities.
2001 * The value in the hash table is a pointer to the (in)equality of "bset".
2003 static int hash_basic_set(struct isl_hash_table
*table
,
2004 struct isl_basic_set
*bset
)
2007 unsigned dim
= isl_basic_set_total_dim(bset
);
2009 for (i
= 0; i
< bset
->n_eq
; ++i
) {
2010 for (j
= 0; j
< 2; ++j
) {
2011 isl_seq_neg(bset
->eq
[i
], bset
->eq
[i
], 1 + dim
);
2012 if (hash_ineq(bset
->ctx
, table
, bset
->eq
[i
], dim
) < 0)
2016 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
2017 if (hash_ineq(bset
->ctx
, table
, bset
->ineq
[i
], dim
) < 0)
2023 static struct sh_data
*sh_data_alloc(struct isl_set
*set
, unsigned n_ineq
)
2025 struct sh_data
*data
;
2028 data
= isl_calloc(set
->ctx
, struct sh_data
,
2029 sizeof(struct sh_data
) +
2030 (set
->n
- 1) * sizeof(struct sh_data_entry
));
2033 data
->ctx
= set
->ctx
;
2035 data
->hull_table
= isl_hash_table_alloc(set
->ctx
, n_ineq
);
2036 if (!data
->hull_table
)
2038 for (i
= 0; i
< set
->n
; ++i
) {
2039 data
->p
[i
].table
= isl_hash_table_alloc(set
->ctx
,
2040 2 * set
->p
[i
]->n_eq
+ set
->p
[i
]->n_ineq
);
2041 if (!data
->p
[i
].table
)
2043 if (hash_basic_set(data
->p
[i
].table
, set
->p
[i
]) < 0)
2052 /* Check if inequality "ineq" is a bound for basic set "j" or if
2053 * it can be relaxed (by increasing the constant term) to become
2054 * a bound for that basic set. In the latter case, the constant
2056 * Return 1 if "ineq" is a bound
2057 * 0 if "ineq" may attain arbitrarily small values on basic set "j"
2058 * -1 if some error occurred
2060 static int is_bound(struct sh_data
*data
, struct isl_set
*set
, int j
,
2063 enum isl_lp_result res
;
2066 if (!data
->p
[j
].tab
) {
2067 data
->p
[j
].tab
= isl_tab_from_basic_set(set
->p
[j
]);
2068 if (!data
->p
[j
].tab
)
2074 res
= isl_tab_min(data
->p
[j
].tab
, ineq
, data
->ctx
->one
,
2076 if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
2077 isl_int_sub(ineq
[0], ineq
[0], opt
);
2081 return (res
== isl_lp_ok
|| res
== isl_lp_empty
) ? 1 :
2082 res
== isl_lp_unbounded
? 0 : -1;
2085 /* Check if inequality "ineq" from basic set "i" can be relaxed to
2086 * become a bound on the whole set. If so, add the (relaxed) inequality
2089 * We first check if "hull" already contains a translate of the inequality.
2090 * If so, we are done.
2091 * Then, we check if any of the previous basic sets contains a translate
2092 * of the inequality. If so, then we have already considered this
2093 * inequality and we are done.
2094 * Otherwise, for each basic set other than "i", we check if the inequality
2095 * is a bound on the basic set.
2096 * For previous basic sets, we know that they do not contain a translate
2097 * of the inequality, so we directly call is_bound.
2098 * For following basic sets, we first check if a translate of the
2099 * inequality appears in its description and if so directly update
2100 * the inequality accordingly.
2102 static struct isl_basic_set
*add_bound(struct isl_basic_set
*hull
,
2103 struct sh_data
*data
, struct isl_set
*set
, int i
, isl_int
*ineq
)
2106 struct ineq_cmp_data v
;
2107 struct isl_hash_table_entry
*entry
;
2113 v
.len
= isl_basic_set_total_dim(hull
);
2115 c_hash
= isl_seq_get_hash(ineq
+ 1, v
.len
);
2117 entry
= isl_hash_table_find(hull
->ctx
, data
->hull_table
, c_hash
,
2122 for (j
= 0; j
< i
; ++j
) {
2123 entry
= isl_hash_table_find(hull
->ctx
, data
->p
[j
].table
,
2124 c_hash
, has_ineq
, &v
, 0);
2131 k
= isl_basic_set_alloc_inequality(hull
);
2132 isl_seq_cpy(hull
->ineq
[k
], ineq
, 1 + v
.len
);
2136 for (j
= 0; j
< i
; ++j
) {
2138 bound
= is_bound(data
, set
, j
, hull
->ineq
[k
]);
2145 isl_basic_set_free_inequality(hull
, 1);
2149 for (j
= i
+ 1; j
< set
->n
; ++j
) {
2152 entry
= isl_hash_table_find(hull
->ctx
, data
->p
[j
].table
,
2153 c_hash
, has_ineq
, &v
, 0);
2155 ineq_j
= entry
->data
;
2156 neg
= isl_seq_is_neg(ineq_j
+ 1,
2157 hull
->ineq
[k
] + 1, v
.len
);
2159 isl_int_neg(ineq_j
[0], ineq_j
[0]);
2160 if (isl_int_gt(ineq_j
[0], hull
->ineq
[k
][0]))
2161 isl_int_set(hull
->ineq
[k
][0], ineq_j
[0]);
2163 isl_int_neg(ineq_j
[0], ineq_j
[0]);
2166 bound
= is_bound(data
, set
, j
, hull
->ineq
[k
]);
2173 isl_basic_set_free_inequality(hull
, 1);
2177 entry
= isl_hash_table_find(hull
->ctx
, data
->hull_table
, c_hash
,
2181 entry
->data
= hull
->ineq
[k
];
2185 isl_basic_set_free(hull
);
2189 /* Check if any inequality from basic set "i" can be relaxed to
2190 * become a bound on the whole set. If so, add the (relaxed) inequality
2193 static struct isl_basic_set
*add_bounds(struct isl_basic_set
*bset
,
2194 struct sh_data
*data
, struct isl_set
*set
, int i
)
2197 unsigned dim
= isl_basic_set_total_dim(bset
);
2199 for (j
= 0; j
< set
->p
[i
]->n_eq
; ++j
) {
2200 for (k
= 0; k
< 2; ++k
) {
2201 isl_seq_neg(set
->p
[i
]->eq
[j
], set
->p
[i
]->eq
[j
], 1+dim
);
2202 bset
= add_bound(bset
, data
, set
, i
, set
->p
[i
]->eq
[j
]);
2205 for (j
= 0; j
< set
->p
[i
]->n_ineq
; ++j
)
2206 bset
= add_bound(bset
, data
, set
, i
, set
->p
[i
]->ineq
[j
]);
2210 /* Compute a superset of the convex hull of set that is described
2211 * by only translates of the constraints in the constituents of set.
2213 static struct isl_basic_set
*uset_simple_hull(struct isl_set
*set
)
2215 struct sh_data
*data
= NULL
;
2216 struct isl_basic_set
*hull
= NULL
;
2224 for (i
= 0; i
< set
->n
; ++i
) {
2227 n_ineq
+= 2 * set
->p
[i
]->n_eq
+ set
->p
[i
]->n_ineq
;
2230 hull
= isl_basic_set_alloc_dim(isl_dim_copy(set
->dim
), 0, 0, n_ineq
);
2234 data
= sh_data_alloc(set
, n_ineq
);
2238 for (i
= 0; i
< set
->n
; ++i
)
2239 hull
= add_bounds(hull
, data
, set
, i
);
2247 isl_basic_set_free(hull
);
2252 /* Compute a superset of the convex hull of map that is described
2253 * by only translates of the constraints in the constituents of map.
2255 struct isl_basic_map
*isl_map_simple_hull(struct isl_map
*map
)
2257 struct isl_set
*set
= NULL
;
2258 struct isl_basic_map
*model
= NULL
;
2259 struct isl_basic_map
*hull
;
2260 struct isl_basic_map
*affine_hull
;
2261 struct isl_basic_set
*bset
= NULL
;
2266 hull
= isl_basic_map_empty_like_map(map
);
2271 hull
= isl_basic_map_copy(map
->p
[0]);
2276 map
= isl_map_detect_equalities(map
);
2277 affine_hull
= isl_map_affine_hull(isl_map_copy(map
));
2278 map
= isl_map_align_divs(map
);
2279 model
= isl_basic_map_copy(map
->p
[0]);
2281 set
= isl_map_underlying_set(map
);
2283 bset
= uset_simple_hull(set
);
2285 hull
= isl_basic_map_overlying_set(bset
, model
);
2287 hull
= isl_basic_map_intersect(hull
, affine_hull
);
2288 hull
= isl_basic_map_convex_hull(hull
);
2289 ISL_F_SET(hull
, ISL_BASIC_MAP_NO_IMPLICIT
);
2290 ISL_F_SET(hull
, ISL_BASIC_MAP_ALL_EQUALITIES
);
2295 struct isl_basic_set
*isl_set_simple_hull(struct isl_set
*set
)
2297 return (struct isl_basic_set
*)
2298 isl_map_simple_hull((struct isl_map
*)set
);
2301 /* Given a set "set", return parametric bounds on the dimension "dim".
2303 static struct isl_basic_set
*set_bounds(struct isl_set
*set
, int dim
)
2305 unsigned set_dim
= isl_set_dim(set
, isl_dim_set
);
2306 set
= isl_set_copy(set
);
2307 set
= isl_set_eliminate_dims(set
, dim
+ 1, set_dim
- (dim
+ 1));
2308 set
= isl_set_eliminate_dims(set
, 0, dim
);
2309 return isl_set_convex_hull(set
);
2312 /* Computes a "simple hull" and then check if each dimension in the
2313 * resulting hull is bounded by a symbolic constant. If not, the
2314 * hull is intersected with the corresponding bounds on the whole set.
2316 struct isl_basic_set
*isl_set_bounded_simple_hull(struct isl_set
*set
)
2319 struct isl_basic_set
*hull
;
2320 unsigned nparam
, left
;
2321 int removed_divs
= 0;
2323 hull
= isl_set_simple_hull(isl_set_copy(set
));
2327 nparam
= isl_basic_set_dim(hull
, isl_dim_param
);
2328 for (i
= 0; i
< isl_basic_set_dim(hull
, isl_dim_set
); ++i
) {
2329 int lower
= 0, upper
= 0;
2330 struct isl_basic_set
*bounds
;
2332 left
= isl_basic_set_total_dim(hull
) - nparam
- i
- 1;
2333 for (j
= 0; j
< hull
->n_eq
; ++j
) {
2334 if (isl_int_is_zero(hull
->eq
[j
][1 + nparam
+ i
]))
2336 if (isl_seq_first_non_zero(hull
->eq
[j
]+1+nparam
+i
+1,
2343 for (j
= 0; j
< hull
->n_ineq
; ++j
) {
2344 if (isl_int_is_zero(hull
->ineq
[j
][1 + nparam
+ i
]))
2346 if (isl_seq_first_non_zero(hull
->ineq
[j
]+1+nparam
+i
+1,
2348 isl_seq_first_non_zero(hull
->ineq
[j
]+1+nparam
,
2351 if (isl_int_is_pos(hull
->ineq
[j
][1 + nparam
+ i
]))
2362 if (!removed_divs
) {
2363 set
= isl_set_remove_divs(set
);
2368 bounds
= set_bounds(set
, i
);
2369 hull
= isl_basic_set_intersect(hull
, bounds
);