2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2014 INRIA Rocquencourt
5 * Use of this software is governed by the MIT license
7 * Written by Sven Verdoolaege, K.U.Leuven, Departement
8 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
9 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
10 * B.P. 105 - 78153 Le Chesnay, France
13 #include <isl_ctx_private.h>
14 #include <isl_map_private.h>
15 #include <isl_lp_private.h>
17 #include <isl_mat_private.h>
18 #include <isl_vec_private.h>
21 #include <isl_options_private.h>
22 #include "isl_equalities.h"
26 static struct isl_basic_set
*uset_convex_hull_wrap_bounded(struct isl_set
*set
);
28 /* Return 1 if constraint c is redundant with respect to the constraints
29 * in bmap. If c is a lower [upper] bound in some variable and bmap
30 * does not have a lower [upper] bound in that variable, then c cannot
31 * be redundant and we do not need solve any lp.
33 int isl_basic_map_constraint_is_redundant(struct isl_basic_map
**bmap
,
34 isl_int
*c
, isl_int
*opt_n
, isl_int
*opt_d
)
36 enum isl_lp_result res
;
43 total
= isl_basic_map_total_dim(*bmap
);
44 for (i
= 0; i
< total
; ++i
) {
46 if (isl_int_is_zero(c
[1+i
]))
48 sign
= isl_int_sgn(c
[1+i
]);
49 for (j
= 0; j
< (*bmap
)->n_ineq
; ++j
)
50 if (sign
== isl_int_sgn((*bmap
)->ineq
[j
][1+i
]))
52 if (j
== (*bmap
)->n_ineq
)
58 res
= isl_basic_map_solve_lp(*bmap
, 0, c
, (*bmap
)->ctx
->one
,
60 if (res
== isl_lp_unbounded
)
62 if (res
== isl_lp_error
)
64 if (res
== isl_lp_empty
) {
65 *bmap
= isl_basic_map_set_to_empty(*bmap
);
68 return !isl_int_is_neg(*opt_n
);
71 int isl_basic_set_constraint_is_redundant(struct isl_basic_set
**bset
,
72 isl_int
*c
, isl_int
*opt_n
, isl_int
*opt_d
)
74 return isl_basic_map_constraint_is_redundant(
75 (struct isl_basic_map
**)bset
, c
, opt_n
, opt_d
);
79 * constraints. If the minimal value along the normal of a constraint
80 * is the same if the constraint is removed, then the constraint is redundant.
82 * Since some constraints may be mutually redundant, sort the constraints
83 * first such that constraints that involve existentially quantified
84 * variables are considered for removal before those that do not.
85 * The sorting is also need for the use in map_simple_hull.
87 * Alternatively, we could have intersected the basic map with the
88 * corresponding equality and then checked if the dimension was that
91 __isl_give isl_basic_map
*isl_basic_map_remove_redundancies(
92 __isl_take isl_basic_map
*bmap
)
99 bmap
= isl_basic_map_gauss(bmap
, NULL
);
100 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
102 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
))
104 if (bmap
->n_ineq
<= 1)
107 bmap
= isl_basic_map_sort_constraints(bmap
);
108 tab
= isl_tab_from_basic_map(bmap
, 0);
109 if (isl_tab_detect_implicit_equalities(tab
) < 0)
111 if (isl_tab_detect_redundant(tab
) < 0)
113 bmap
= isl_basic_map_update_from_tab(bmap
, tab
);
117 ISL_F_SET(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
118 ISL_F_SET(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
122 isl_basic_map_free(bmap
);
126 __isl_give isl_basic_set
*isl_basic_set_remove_redundancies(
127 __isl_take isl_basic_set
*bset
)
129 return (struct isl_basic_set
*)
130 isl_basic_map_remove_redundancies((struct isl_basic_map
*)bset
);
133 /* Remove redundant constraints in each of the basic maps.
135 __isl_give isl_map
*isl_map_remove_redundancies(__isl_take isl_map
*map
)
137 return isl_map_inline_foreach_basic_map(map
,
138 &isl_basic_map_remove_redundancies
);
141 __isl_give isl_set
*isl_set_remove_redundancies(__isl_take isl_set
*set
)
143 return isl_map_remove_redundancies(set
);
146 /* Check if the set set is bound in the direction of the affine
147 * constraint c and if so, set the constant term such that the
148 * resulting constraint is a bounding constraint for the set.
150 static int uset_is_bound(struct isl_set
*set
, isl_int
*c
, unsigned len
)
158 isl_int_init(opt_denom
);
160 for (j
= 0; j
< set
->n
; ++j
) {
161 enum isl_lp_result res
;
163 if (ISL_F_ISSET(set
->p
[j
], ISL_BASIC_SET_EMPTY
))
166 res
= isl_basic_set_solve_lp(set
->p
[j
],
167 0, c
, set
->ctx
->one
, &opt
, &opt_denom
, NULL
);
168 if (res
== isl_lp_unbounded
)
170 if (res
== isl_lp_error
)
172 if (res
== isl_lp_empty
) {
173 set
->p
[j
] = isl_basic_set_set_to_empty(set
->p
[j
]);
178 if (first
|| isl_int_is_neg(opt
)) {
179 if (!isl_int_is_one(opt_denom
))
180 isl_seq_scale(c
, c
, opt_denom
, len
);
181 isl_int_sub(c
[0], c
[0], opt
);
186 isl_int_clear(opt_denom
);
190 isl_int_clear(opt_denom
);
194 __isl_give isl_basic_map
*isl_basic_map_set_rational(
195 __isl_take isl_basic_set
*bmap
)
200 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
203 bmap
= isl_basic_map_cow(bmap
);
207 ISL_F_SET(bmap
, ISL_BASIC_MAP_RATIONAL
);
209 return isl_basic_map_finalize(bmap
);
212 __isl_give isl_basic_set
*isl_basic_set_set_rational(
213 __isl_take isl_basic_set
*bset
)
215 return isl_basic_map_set_rational(bset
);
218 __isl_give isl_map
*isl_map_set_rational(__isl_take isl_map
*map
)
222 map
= isl_map_cow(map
);
225 for (i
= 0; i
< map
->n
; ++i
) {
226 map
->p
[i
] = isl_basic_map_set_rational(map
->p
[i
]);
236 __isl_give isl_set
*isl_set_set_rational(__isl_take isl_set
*set
)
238 return isl_map_set_rational(set
);
241 static struct isl_basic_set
*isl_basic_set_add_equality(
242 struct isl_basic_set
*bset
, isl_int
*c
)
250 if (ISL_F_ISSET(bset
, ISL_BASIC_SET_EMPTY
))
253 isl_assert(bset
->ctx
, isl_basic_set_n_param(bset
) == 0, goto error
);
254 isl_assert(bset
->ctx
, bset
->n_div
== 0, goto error
);
255 dim
= isl_basic_set_n_dim(bset
);
256 bset
= isl_basic_set_cow(bset
);
257 bset
= isl_basic_set_extend(bset
, 0, dim
, 0, 1, 0);
258 i
= isl_basic_set_alloc_equality(bset
);
261 isl_seq_cpy(bset
->eq
[i
], c
, 1 + dim
);
264 isl_basic_set_free(bset
);
268 static struct isl_set
*isl_set_add_basic_set_equality(struct isl_set
*set
, isl_int
*c
)
272 set
= isl_set_cow(set
);
275 for (i
= 0; i
< set
->n
; ++i
) {
276 set
->p
[i
] = isl_basic_set_add_equality(set
->p
[i
], c
);
286 /* Given a union of basic sets, construct the constraints for wrapping
287 * a facet around one of its ridges.
288 * In particular, if each of n the d-dimensional basic sets i in "set"
289 * contains the origin, satisfies the constraints x_1 >= 0 and x_2 >= 0
290 * and is defined by the constraints
294 * then the resulting set is of dimension n*(1+d) and has as constraints
303 static struct isl_basic_set
*wrap_constraints(struct isl_set
*set
)
305 struct isl_basic_set
*lp
;
309 unsigned dim
, lp_dim
;
314 dim
= 1 + isl_set_n_dim(set
);
317 for (i
= 0; i
< set
->n
; ++i
) {
318 n_eq
+= set
->p
[i
]->n_eq
;
319 n_ineq
+= set
->p
[i
]->n_ineq
;
321 lp
= isl_basic_set_alloc(set
->ctx
, 0, dim
* set
->n
, 0, n_eq
, n_ineq
);
322 lp
= isl_basic_set_set_rational(lp
);
325 lp_dim
= isl_basic_set_n_dim(lp
);
326 k
= isl_basic_set_alloc_equality(lp
);
327 isl_int_set_si(lp
->eq
[k
][0], -1);
328 for (i
= 0; i
< set
->n
; ++i
) {
329 isl_int_set_si(lp
->eq
[k
][1+dim
*i
], 0);
330 isl_int_set_si(lp
->eq
[k
][1+dim
*i
+1], 1);
331 isl_seq_clr(lp
->eq
[k
]+1+dim
*i
+2, dim
-2);
333 for (i
= 0; i
< set
->n
; ++i
) {
334 k
= isl_basic_set_alloc_inequality(lp
);
335 isl_seq_clr(lp
->ineq
[k
], 1+lp_dim
);
336 isl_int_set_si(lp
->ineq
[k
][1+dim
*i
], 1);
338 for (j
= 0; j
< set
->p
[i
]->n_eq
; ++j
) {
339 k
= isl_basic_set_alloc_equality(lp
);
340 isl_seq_clr(lp
->eq
[k
], 1+dim
*i
);
341 isl_seq_cpy(lp
->eq
[k
]+1+dim
*i
, set
->p
[i
]->eq
[j
], dim
);
342 isl_seq_clr(lp
->eq
[k
]+1+dim
*(i
+1), dim
*(set
->n
-i
-1));
345 for (j
= 0; j
< set
->p
[i
]->n_ineq
; ++j
) {
346 k
= isl_basic_set_alloc_inequality(lp
);
347 isl_seq_clr(lp
->ineq
[k
], 1+dim
*i
);
348 isl_seq_cpy(lp
->ineq
[k
]+1+dim
*i
, set
->p
[i
]->ineq
[j
], dim
);
349 isl_seq_clr(lp
->ineq
[k
]+1+dim
*(i
+1), dim
*(set
->n
-i
-1));
355 /* Given a facet "facet" of the convex hull of "set" and a facet "ridge"
356 * of that facet, compute the other facet of the convex hull that contains
359 * We first transform the set such that the facet constraint becomes
363 * I.e., the facet lies in
367 * and on that facet, the constraint that defines the ridge is
371 * (This transformation is not strictly needed, all that is needed is
372 * that the ridge contains the origin.)
374 * Since the ridge contains the origin, the cone of the convex hull
375 * will be of the form
380 * with this second constraint defining the new facet.
381 * The constant a is obtained by settting x_1 in the cone of the
382 * convex hull to 1 and minimizing x_2.
383 * Now, each element in the cone of the convex hull is the sum
384 * of elements in the cones of the basic sets.
385 * If a_i is the dilation factor of basic set i, then the problem
386 * we need to solve is
399 * the constraints of each (transformed) basic set.
400 * If a = n/d, then the constraint defining the new facet (in the transformed
403 * -n x_1 + d x_2 >= 0
405 * In the original space, we need to take the same combination of the
406 * corresponding constraints "facet" and "ridge".
408 * If a = -infty = "-1/0", then we just return the original facet constraint.
409 * This means that the facet is unbounded, but has a bounded intersection
410 * with the union of sets.
412 isl_int
*isl_set_wrap_facet(__isl_keep isl_set
*set
,
413 isl_int
*facet
, isl_int
*ridge
)
417 struct isl_mat
*T
= NULL
;
418 struct isl_basic_set
*lp
= NULL
;
420 enum isl_lp_result res
;
427 set
= isl_set_copy(set
);
428 set
= isl_set_set_rational(set
);
430 dim
= 1 + isl_set_n_dim(set
);
431 T
= isl_mat_alloc(ctx
, 3, dim
);
434 isl_int_set_si(T
->row
[0][0], 1);
435 isl_seq_clr(T
->row
[0]+1, dim
- 1);
436 isl_seq_cpy(T
->row
[1], facet
, dim
);
437 isl_seq_cpy(T
->row
[2], ridge
, dim
);
438 T
= isl_mat_right_inverse(T
);
439 set
= isl_set_preimage(set
, T
);
443 lp
= wrap_constraints(set
);
444 obj
= isl_vec_alloc(ctx
, 1 + dim
*set
->n
);
447 isl_int_set_si(obj
->block
.data
[0], 0);
448 for (i
= 0; i
< set
->n
; ++i
) {
449 isl_seq_clr(obj
->block
.data
+ 1 + dim
*i
, 2);
450 isl_int_set_si(obj
->block
.data
[1 + dim
*i
+2], 1);
451 isl_seq_clr(obj
->block
.data
+ 1 + dim
*i
+3, dim
-3);
455 res
= isl_basic_set_solve_lp(lp
, 0,
456 obj
->block
.data
, ctx
->one
, &num
, &den
, NULL
);
457 if (res
== isl_lp_ok
) {
458 isl_int_neg(num
, num
);
459 isl_seq_combine(facet
, num
, facet
, den
, ridge
, dim
);
460 isl_seq_normalize(ctx
, facet
, dim
);
465 isl_basic_set_free(lp
);
467 if (res
== isl_lp_error
)
469 isl_assert(ctx
, res
== isl_lp_ok
|| res
== isl_lp_unbounded
,
473 isl_basic_set_free(lp
);
479 /* Compute the constraint of a facet of "set".
481 * We first compute the intersection with a bounding constraint
482 * that is orthogonal to one of the coordinate axes.
483 * If the affine hull of this intersection has only one equality,
484 * we have found a facet.
485 * Otherwise, we wrap the current bounding constraint around
486 * one of the equalities of the face (one that is not equal to
487 * the current bounding constraint).
488 * This process continues until we have found a facet.
489 * The dimension of the intersection increases by at least
490 * one on each iteration, so termination is guaranteed.
492 static __isl_give isl_mat
*initial_facet_constraint(__isl_keep isl_set
*set
)
494 struct isl_set
*slice
= NULL
;
495 struct isl_basic_set
*face
= NULL
;
497 unsigned dim
= isl_set_n_dim(set
);
499 isl_mat
*bounds
= NULL
;
501 isl_assert(set
->ctx
, set
->n
> 0, goto error
);
502 bounds
= isl_mat_alloc(set
->ctx
, 1, 1 + dim
);
506 isl_seq_clr(bounds
->row
[0], dim
);
507 isl_int_set_si(bounds
->row
[0][1 + dim
- 1], 1);
508 is_bound
= uset_is_bound(set
, bounds
->row
[0], 1 + dim
);
511 isl_assert(set
->ctx
, is_bound
, goto error
);
512 isl_seq_normalize(set
->ctx
, bounds
->row
[0], 1 + dim
);
516 slice
= isl_set_copy(set
);
517 slice
= isl_set_add_basic_set_equality(slice
, bounds
->row
[0]);
518 face
= isl_set_affine_hull(slice
);
521 if (face
->n_eq
== 1) {
522 isl_basic_set_free(face
);
525 for (i
= 0; i
< face
->n_eq
; ++i
)
526 if (!isl_seq_eq(bounds
->row
[0], face
->eq
[i
], 1 + dim
) &&
527 !isl_seq_is_neg(bounds
->row
[0],
528 face
->eq
[i
], 1 + dim
))
530 isl_assert(set
->ctx
, i
< face
->n_eq
, goto error
);
531 if (!isl_set_wrap_facet(set
, bounds
->row
[0], face
->eq
[i
]))
533 isl_seq_normalize(set
->ctx
, bounds
->row
[0], bounds
->n_col
);
534 isl_basic_set_free(face
);
539 isl_basic_set_free(face
);
540 isl_mat_free(bounds
);
544 /* Given the bounding constraint "c" of a facet of the convex hull of "set",
545 * compute a hyperplane description of the facet, i.e., compute the facets
548 * We compute an affine transformation that transforms the constraint
557 * by computing the right inverse U of a matrix that starts with the rows
570 * Since z_1 is zero, we can drop this variable as well as the corresponding
571 * column of U to obtain
579 * with Q' equal to Q, but without the corresponding row.
580 * After computing the facets of the facet in the z' space,
581 * we convert them back to the x space through Q.
583 static struct isl_basic_set
*compute_facet(struct isl_set
*set
, isl_int
*c
)
585 struct isl_mat
*m
, *U
, *Q
;
586 struct isl_basic_set
*facet
= NULL
;
591 set
= isl_set_copy(set
);
592 dim
= isl_set_n_dim(set
);
593 m
= isl_mat_alloc(set
->ctx
, 2, 1 + dim
);
596 isl_int_set_si(m
->row
[0][0], 1);
597 isl_seq_clr(m
->row
[0]+1, dim
);
598 isl_seq_cpy(m
->row
[1], c
, 1+dim
);
599 U
= isl_mat_right_inverse(m
);
600 Q
= isl_mat_right_inverse(isl_mat_copy(U
));
601 U
= isl_mat_drop_cols(U
, 1, 1);
602 Q
= isl_mat_drop_rows(Q
, 1, 1);
603 set
= isl_set_preimage(set
, U
);
604 facet
= uset_convex_hull_wrap_bounded(set
);
605 facet
= isl_basic_set_preimage(facet
, Q
);
606 if (facet
&& facet
->n_eq
!= 0)
607 isl_die(ctx
, isl_error_internal
, "unexpected equality",
608 return isl_basic_set_free(facet
));
611 isl_basic_set_free(facet
);
616 /* Given an initial facet constraint, compute the remaining facets.
617 * We do this by running through all facets found so far and computing
618 * the adjacent facets through wrapping, adding those facets that we
619 * hadn't already found before.
621 * For each facet we have found so far, we first compute its facets
622 * in the resulting convex hull. That is, we compute the ridges
623 * of the resulting convex hull contained in the facet.
624 * We also compute the corresponding facet in the current approximation
625 * of the convex hull. There is no need to wrap around the ridges
626 * in this facet since that would result in a facet that is already
627 * present in the current approximation.
629 * This function can still be significantly optimized by checking which of
630 * the facets of the basic sets are also facets of the convex hull and
631 * using all the facets so far to help in constructing the facets of the
634 * using the technique in section "3.1 Ridge Generation" of
635 * "Extended Convex Hull" by Fukuda et al.
637 static struct isl_basic_set
*extend(struct isl_basic_set
*hull
,
642 struct isl_basic_set
*facet
= NULL
;
643 struct isl_basic_set
*hull_facet
= NULL
;
649 isl_assert(set
->ctx
, set
->n
> 0, goto error
);
651 dim
= isl_set_n_dim(set
);
653 for (i
= 0; i
< hull
->n_ineq
; ++i
) {
654 facet
= compute_facet(set
, hull
->ineq
[i
]);
655 facet
= isl_basic_set_add_equality(facet
, hull
->ineq
[i
]);
656 facet
= isl_basic_set_gauss(facet
, NULL
);
657 facet
= isl_basic_set_normalize_constraints(facet
);
658 hull_facet
= isl_basic_set_copy(hull
);
659 hull_facet
= isl_basic_set_add_equality(hull_facet
, hull
->ineq
[i
]);
660 hull_facet
= isl_basic_set_gauss(hull_facet
, NULL
);
661 hull_facet
= isl_basic_set_normalize_constraints(hull_facet
);
662 if (!facet
|| !hull_facet
)
664 hull
= isl_basic_set_cow(hull
);
665 hull
= isl_basic_set_extend_space(hull
,
666 isl_space_copy(hull
->dim
), 0, 0, facet
->n_ineq
);
669 for (j
= 0; j
< facet
->n_ineq
; ++j
) {
670 for (f
= 0; f
< hull_facet
->n_ineq
; ++f
)
671 if (isl_seq_eq(facet
->ineq
[j
],
672 hull_facet
->ineq
[f
], 1 + dim
))
674 if (f
< hull_facet
->n_ineq
)
676 k
= isl_basic_set_alloc_inequality(hull
);
679 isl_seq_cpy(hull
->ineq
[k
], hull
->ineq
[i
], 1+dim
);
680 if (!isl_set_wrap_facet(set
, hull
->ineq
[k
], facet
->ineq
[j
]))
683 isl_basic_set_free(hull_facet
);
684 isl_basic_set_free(facet
);
686 hull
= isl_basic_set_simplify(hull
);
687 hull
= isl_basic_set_finalize(hull
);
690 isl_basic_set_free(hull_facet
);
691 isl_basic_set_free(facet
);
692 isl_basic_set_free(hull
);
696 /* Special case for computing the convex hull of a one dimensional set.
697 * We simply collect the lower and upper bounds of each basic set
698 * and the biggest of those.
700 static struct isl_basic_set
*convex_hull_1d(struct isl_set
*set
)
702 struct isl_mat
*c
= NULL
;
703 isl_int
*lower
= NULL
;
704 isl_int
*upper
= NULL
;
707 struct isl_basic_set
*hull
;
709 for (i
= 0; i
< set
->n
; ++i
) {
710 set
->p
[i
] = isl_basic_set_simplify(set
->p
[i
]);
714 set
= isl_set_remove_empty_parts(set
);
717 isl_assert(set
->ctx
, set
->n
> 0, goto error
);
718 c
= isl_mat_alloc(set
->ctx
, 2, 2);
722 if (set
->p
[0]->n_eq
> 0) {
723 isl_assert(set
->ctx
, set
->p
[0]->n_eq
== 1, goto error
);
726 if (isl_int_is_pos(set
->p
[0]->eq
[0][1])) {
727 isl_seq_cpy(lower
, set
->p
[0]->eq
[0], 2);
728 isl_seq_neg(upper
, set
->p
[0]->eq
[0], 2);
730 isl_seq_neg(lower
, set
->p
[0]->eq
[0], 2);
731 isl_seq_cpy(upper
, set
->p
[0]->eq
[0], 2);
734 for (j
= 0; j
< set
->p
[0]->n_ineq
; ++j
) {
735 if (isl_int_is_pos(set
->p
[0]->ineq
[j
][1])) {
737 isl_seq_cpy(lower
, set
->p
[0]->ineq
[j
], 2);
740 isl_seq_cpy(upper
, set
->p
[0]->ineq
[j
], 2);
747 for (i
= 0; i
< set
->n
; ++i
) {
748 struct isl_basic_set
*bset
= set
->p
[i
];
752 for (j
= 0; j
< bset
->n_eq
; ++j
) {
756 isl_int_mul(a
, lower
[0], bset
->eq
[j
][1]);
757 isl_int_mul(b
, lower
[1], bset
->eq
[j
][0]);
758 if (isl_int_lt(a
, b
) && isl_int_is_pos(bset
->eq
[j
][1]))
759 isl_seq_cpy(lower
, bset
->eq
[j
], 2);
760 if (isl_int_gt(a
, b
) && isl_int_is_neg(bset
->eq
[j
][1]))
761 isl_seq_neg(lower
, bset
->eq
[j
], 2);
764 isl_int_mul(a
, upper
[0], bset
->eq
[j
][1]);
765 isl_int_mul(b
, upper
[1], bset
->eq
[j
][0]);
766 if (isl_int_lt(a
, b
) && isl_int_is_pos(bset
->eq
[j
][1]))
767 isl_seq_neg(upper
, bset
->eq
[j
], 2);
768 if (isl_int_gt(a
, b
) && isl_int_is_neg(bset
->eq
[j
][1]))
769 isl_seq_cpy(upper
, bset
->eq
[j
], 2);
772 for (j
= 0; j
< bset
->n_ineq
; ++j
) {
773 if (isl_int_is_pos(bset
->ineq
[j
][1]))
775 if (isl_int_is_neg(bset
->ineq
[j
][1]))
777 if (lower
&& isl_int_is_pos(bset
->ineq
[j
][1])) {
778 isl_int_mul(a
, lower
[0], bset
->ineq
[j
][1]);
779 isl_int_mul(b
, lower
[1], bset
->ineq
[j
][0]);
780 if (isl_int_lt(a
, b
))
781 isl_seq_cpy(lower
, bset
->ineq
[j
], 2);
783 if (upper
&& isl_int_is_neg(bset
->ineq
[j
][1])) {
784 isl_int_mul(a
, upper
[0], bset
->ineq
[j
][1]);
785 isl_int_mul(b
, upper
[1], bset
->ineq
[j
][0]);
786 if (isl_int_gt(a
, b
))
787 isl_seq_cpy(upper
, bset
->ineq
[j
], 2);
798 hull
= isl_basic_set_alloc(set
->ctx
, 0, 1, 0, 0, 2);
799 hull
= isl_basic_set_set_rational(hull
);
803 k
= isl_basic_set_alloc_inequality(hull
);
804 isl_seq_cpy(hull
->ineq
[k
], lower
, 2);
807 k
= isl_basic_set_alloc_inequality(hull
);
808 isl_seq_cpy(hull
->ineq
[k
], upper
, 2);
810 hull
= isl_basic_set_finalize(hull
);
820 static struct isl_basic_set
*convex_hull_0d(struct isl_set
*set
)
822 struct isl_basic_set
*convex_hull
;
827 if (isl_set_is_empty(set
))
828 convex_hull
= isl_basic_set_empty(isl_space_copy(set
->dim
));
830 convex_hull
= isl_basic_set_universe(isl_space_copy(set
->dim
));
835 /* Compute the convex hull of a pair of basic sets without any parameters or
836 * integer divisions using Fourier-Motzkin elimination.
837 * The convex hull is the set of all points that can be written as
838 * the sum of points from both basic sets (in homogeneous coordinates).
839 * We set up the constraints in a space with dimensions for each of
840 * the three sets and then project out the dimensions corresponding
841 * to the two original basic sets, retaining only those corresponding
842 * to the convex hull.
844 static struct isl_basic_set
*convex_hull_pair_elim(struct isl_basic_set
*bset1
,
845 struct isl_basic_set
*bset2
)
848 struct isl_basic_set
*bset
[2];
849 struct isl_basic_set
*hull
= NULL
;
852 if (!bset1
|| !bset2
)
855 dim
= isl_basic_set_n_dim(bset1
);
856 hull
= isl_basic_set_alloc(bset1
->ctx
, 0, 2 + 3 * dim
, 0,
857 1 + dim
+ bset1
->n_eq
+ bset2
->n_eq
,
858 2 + bset1
->n_ineq
+ bset2
->n_ineq
);
861 for (i
= 0; i
< 2; ++i
) {
862 for (j
= 0; j
< bset
[i
]->n_eq
; ++j
) {
863 k
= isl_basic_set_alloc_equality(hull
);
866 isl_seq_clr(hull
->eq
[k
], (i
+1) * (1+dim
));
867 isl_seq_clr(hull
->eq
[k
]+(i
+2)*(1+dim
), (1-i
)*(1+dim
));
868 isl_seq_cpy(hull
->eq
[k
]+(i
+1)*(1+dim
), bset
[i
]->eq
[j
],
871 for (j
= 0; j
< bset
[i
]->n_ineq
; ++j
) {
872 k
= isl_basic_set_alloc_inequality(hull
);
875 isl_seq_clr(hull
->ineq
[k
], (i
+1) * (1+dim
));
876 isl_seq_clr(hull
->ineq
[k
]+(i
+2)*(1+dim
), (1-i
)*(1+dim
));
877 isl_seq_cpy(hull
->ineq
[k
]+(i
+1)*(1+dim
),
878 bset
[i
]->ineq
[j
], 1+dim
);
880 k
= isl_basic_set_alloc_inequality(hull
);
883 isl_seq_clr(hull
->ineq
[k
], 1+2+3*dim
);
884 isl_int_set_si(hull
->ineq
[k
][(i
+1)*(1+dim
)], 1);
886 for (j
= 0; j
< 1+dim
; ++j
) {
887 k
= isl_basic_set_alloc_equality(hull
);
890 isl_seq_clr(hull
->eq
[k
], 1+2+3*dim
);
891 isl_int_set_si(hull
->eq
[k
][j
], -1);
892 isl_int_set_si(hull
->eq
[k
][1+dim
+j
], 1);
893 isl_int_set_si(hull
->eq
[k
][2*(1+dim
)+j
], 1);
895 hull
= isl_basic_set_set_rational(hull
);
896 hull
= isl_basic_set_remove_dims(hull
, isl_dim_set
, dim
, 2*(1+dim
));
897 hull
= isl_basic_set_remove_redundancies(hull
);
898 isl_basic_set_free(bset1
);
899 isl_basic_set_free(bset2
);
902 isl_basic_set_free(bset1
);
903 isl_basic_set_free(bset2
);
904 isl_basic_set_free(hull
);
908 /* Is the set bounded for each value of the parameters?
910 int isl_basic_set_is_bounded(__isl_keep isl_basic_set
*bset
)
917 if (isl_basic_set_plain_is_empty(bset
))
920 tab
= isl_tab_from_recession_cone(bset
, 1);
921 bounded
= isl_tab_cone_is_bounded(tab
);
926 /* Is the image bounded for each value of the parameters and
927 * the domain variables?
929 int isl_basic_map_image_is_bounded(__isl_keep isl_basic_map
*bmap
)
931 unsigned nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
932 unsigned n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
935 bmap
= isl_basic_map_copy(bmap
);
936 bmap
= isl_basic_map_cow(bmap
);
937 bmap
= isl_basic_map_move_dims(bmap
, isl_dim_param
, nparam
,
938 isl_dim_in
, 0, n_in
);
939 bounded
= isl_basic_set_is_bounded((isl_basic_set
*)bmap
);
940 isl_basic_map_free(bmap
);
945 /* Is the set bounded for each value of the parameters?
947 int isl_set_is_bounded(__isl_keep isl_set
*set
)
954 for (i
= 0; i
< set
->n
; ++i
) {
955 int bounded
= isl_basic_set_is_bounded(set
->p
[i
]);
956 if (!bounded
|| bounded
< 0)
962 /* Compute the lineality space of the convex hull of bset1 and bset2.
964 * We first compute the intersection of the recession cone of bset1
965 * with the negative of the recession cone of bset2 and then compute
966 * the linear hull of the resulting cone.
968 static struct isl_basic_set
*induced_lineality_space(
969 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
972 struct isl_basic_set
*lin
= NULL
;
975 if (!bset1
|| !bset2
)
978 dim
= isl_basic_set_total_dim(bset1
);
979 lin
= isl_basic_set_alloc_space(isl_basic_set_get_space(bset1
), 0,
980 bset1
->n_eq
+ bset2
->n_eq
,
981 bset1
->n_ineq
+ bset2
->n_ineq
);
982 lin
= isl_basic_set_set_rational(lin
);
985 for (i
= 0; i
< bset1
->n_eq
; ++i
) {
986 k
= isl_basic_set_alloc_equality(lin
);
989 isl_int_set_si(lin
->eq
[k
][0], 0);
990 isl_seq_cpy(lin
->eq
[k
] + 1, bset1
->eq
[i
] + 1, dim
);
992 for (i
= 0; i
< bset1
->n_ineq
; ++i
) {
993 k
= isl_basic_set_alloc_inequality(lin
);
996 isl_int_set_si(lin
->ineq
[k
][0], 0);
997 isl_seq_cpy(lin
->ineq
[k
] + 1, bset1
->ineq
[i
] + 1, dim
);
999 for (i
= 0; i
< bset2
->n_eq
; ++i
) {
1000 k
= isl_basic_set_alloc_equality(lin
);
1003 isl_int_set_si(lin
->eq
[k
][0], 0);
1004 isl_seq_neg(lin
->eq
[k
] + 1, bset2
->eq
[i
] + 1, dim
);
1006 for (i
= 0; i
< bset2
->n_ineq
; ++i
) {
1007 k
= isl_basic_set_alloc_inequality(lin
);
1010 isl_int_set_si(lin
->ineq
[k
][0], 0);
1011 isl_seq_neg(lin
->ineq
[k
] + 1, bset2
->ineq
[i
] + 1, dim
);
1014 isl_basic_set_free(bset1
);
1015 isl_basic_set_free(bset2
);
1016 return isl_basic_set_affine_hull(lin
);
1018 isl_basic_set_free(lin
);
1019 isl_basic_set_free(bset1
);
1020 isl_basic_set_free(bset2
);
1024 static struct isl_basic_set
*uset_convex_hull(struct isl_set
*set
);
1026 /* Given a set and a linear space "lin" of dimension n > 0,
1027 * project the linear space from the set, compute the convex hull
1028 * and then map the set back to the original space.
1034 * describe the linear space. We first compute the Hermite normal
1035 * form H = M U of M = H Q, to obtain
1039 * The last n rows of H will be zero, so the last n variables of x' = Q x
1040 * are the one we want to project out. We do this by transforming each
1041 * basic set A x >= b to A U x' >= b and then removing the last n dimensions.
1042 * After computing the convex hull in x'_1, i.e., A' x'_1 >= b',
1043 * we transform the hull back to the original space as A' Q_1 x >= b',
1044 * with Q_1 all but the last n rows of Q.
1046 static struct isl_basic_set
*modulo_lineality(struct isl_set
*set
,
1047 struct isl_basic_set
*lin
)
1049 unsigned total
= isl_basic_set_total_dim(lin
);
1051 struct isl_basic_set
*hull
;
1052 struct isl_mat
*M
, *U
, *Q
;
1056 lin_dim
= total
- lin
->n_eq
;
1057 M
= isl_mat_sub_alloc6(set
->ctx
, lin
->eq
, 0, lin
->n_eq
, 1, total
);
1058 M
= isl_mat_left_hermite(M
, 0, &U
, &Q
);
1062 isl_basic_set_free(lin
);
1064 Q
= isl_mat_drop_rows(Q
, Q
->n_row
- lin_dim
, lin_dim
);
1066 U
= isl_mat_lin_to_aff(U
);
1067 Q
= isl_mat_lin_to_aff(Q
);
1069 set
= isl_set_preimage(set
, U
);
1070 set
= isl_set_remove_dims(set
, isl_dim_set
, total
- lin_dim
, lin_dim
);
1071 hull
= uset_convex_hull(set
);
1072 hull
= isl_basic_set_preimage(hull
, Q
);
1076 isl_basic_set_free(lin
);
1081 /* Given two polyhedra with as constraints h_{ij} x >= 0 in homegeneous space,
1082 * set up an LP for solving
1084 * \sum_j \alpha_{1j} h_{1j} = \sum_j \alpha_{2j} h_{2j}
1086 * \alpha{i0} corresponds to the (implicit) positivity constraint 1 >= 0
1087 * The next \alpha{ij} correspond to the equalities and come in pairs.
1088 * The final \alpha{ij} correspond to the inequalities.
1090 static struct isl_basic_set
*valid_direction_lp(
1091 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
1094 struct isl_basic_set
*lp
;
1099 if (!bset1
|| !bset2
)
1101 d
= 1 + isl_basic_set_total_dim(bset1
);
1103 2 * bset1
->n_eq
+ bset1
->n_ineq
+ 2 * bset2
->n_eq
+ bset2
->n_ineq
;
1104 dim
= isl_space_set_alloc(bset1
->ctx
, 0, n
);
1105 lp
= isl_basic_set_alloc_space(dim
, 0, d
, n
);
1108 for (i
= 0; i
< n
; ++i
) {
1109 k
= isl_basic_set_alloc_inequality(lp
);
1112 isl_seq_clr(lp
->ineq
[k
] + 1, n
);
1113 isl_int_set_si(lp
->ineq
[k
][0], -1);
1114 isl_int_set_si(lp
->ineq
[k
][1 + i
], 1);
1116 for (i
= 0; i
< d
; ++i
) {
1117 k
= isl_basic_set_alloc_equality(lp
);
1121 isl_int_set_si(lp
->eq
[k
][n
], 0); n
++;
1122 /* positivity constraint 1 >= 0 */
1123 isl_int_set_si(lp
->eq
[k
][n
], i
== 0); n
++;
1124 for (j
= 0; j
< bset1
->n_eq
; ++j
) {
1125 isl_int_set(lp
->eq
[k
][n
], bset1
->eq
[j
][i
]); n
++;
1126 isl_int_neg(lp
->eq
[k
][n
], bset1
->eq
[j
][i
]); n
++;
1128 for (j
= 0; j
< bset1
->n_ineq
; ++j
) {
1129 isl_int_set(lp
->eq
[k
][n
], bset1
->ineq
[j
][i
]); n
++;
1131 /* positivity constraint 1 >= 0 */
1132 isl_int_set_si(lp
->eq
[k
][n
], -(i
== 0)); n
++;
1133 for (j
= 0; j
< bset2
->n_eq
; ++j
) {
1134 isl_int_neg(lp
->eq
[k
][n
], bset2
->eq
[j
][i
]); n
++;
1135 isl_int_set(lp
->eq
[k
][n
], bset2
->eq
[j
][i
]); n
++;
1137 for (j
= 0; j
< bset2
->n_ineq
; ++j
) {
1138 isl_int_neg(lp
->eq
[k
][n
], bset2
->ineq
[j
][i
]); n
++;
1141 lp
= isl_basic_set_gauss(lp
, NULL
);
1142 isl_basic_set_free(bset1
);
1143 isl_basic_set_free(bset2
);
1146 isl_basic_set_free(bset1
);
1147 isl_basic_set_free(bset2
);
1151 /* Compute a vector s in the homogeneous space such that <s, r> > 0
1152 * for all rays in the homogeneous space of the two cones that correspond
1153 * to the input polyhedra bset1 and bset2.
1155 * We compute s as a vector that satisfies
1157 * s = \sum_j \alpha_{ij} h_{ij} for i = 1,2 (*)
1159 * with h_{ij} the normals of the facets of polyhedron i
1160 * (including the "positivity constraint" 1 >= 0) and \alpha_{ij}
1161 * strictly positive numbers. For simplicity we impose \alpha_{ij} >= 1.
1162 * We first set up an LP with as variables the \alpha{ij}.
1163 * In this formulation, for each polyhedron i,
1164 * the first constraint is the positivity constraint, followed by pairs
1165 * of variables for the equalities, followed by variables for the inequalities.
1166 * We then simply pick a feasible solution and compute s using (*).
1168 * Note that we simply pick any valid direction and make no attempt
1169 * to pick a "good" or even the "best" valid direction.
1171 static struct isl_vec
*valid_direction(
1172 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
1174 struct isl_basic_set
*lp
;
1175 struct isl_tab
*tab
;
1176 struct isl_vec
*sample
= NULL
;
1177 struct isl_vec
*dir
;
1182 if (!bset1
|| !bset2
)
1184 lp
= valid_direction_lp(isl_basic_set_copy(bset1
),
1185 isl_basic_set_copy(bset2
));
1186 tab
= isl_tab_from_basic_set(lp
, 0);
1187 sample
= isl_tab_get_sample_value(tab
);
1189 isl_basic_set_free(lp
);
1192 d
= isl_basic_set_total_dim(bset1
);
1193 dir
= isl_vec_alloc(bset1
->ctx
, 1 + d
);
1196 isl_seq_clr(dir
->block
.data
+ 1, dir
->size
- 1);
1198 /* positivity constraint 1 >= 0 */
1199 isl_int_set(dir
->block
.data
[0], sample
->block
.data
[n
]); n
++;
1200 for (i
= 0; i
< bset1
->n_eq
; ++i
) {
1201 isl_int_sub(sample
->block
.data
[n
],
1202 sample
->block
.data
[n
], sample
->block
.data
[n
+1]);
1203 isl_seq_combine(dir
->block
.data
,
1204 bset1
->ctx
->one
, dir
->block
.data
,
1205 sample
->block
.data
[n
], bset1
->eq
[i
], 1 + d
);
1209 for (i
= 0; i
< bset1
->n_ineq
; ++i
)
1210 isl_seq_combine(dir
->block
.data
,
1211 bset1
->ctx
->one
, dir
->block
.data
,
1212 sample
->block
.data
[n
++], bset1
->ineq
[i
], 1 + d
);
1213 isl_vec_free(sample
);
1214 isl_seq_normalize(bset1
->ctx
, dir
->el
, dir
->size
);
1215 isl_basic_set_free(bset1
);
1216 isl_basic_set_free(bset2
);
1219 isl_vec_free(sample
);
1220 isl_basic_set_free(bset1
);
1221 isl_basic_set_free(bset2
);
1225 /* Given a polyhedron b_i + A_i x >= 0 and a map T = S^{-1},
1226 * compute b_i' + A_i' x' >= 0, with
1228 * [ b_i A_i ] [ y' ] [ y' ]
1229 * [ 1 0 ] S^{-1} [ x' ] >= 0 or [ b_i' A_i' ] [ x' ] >= 0
1231 * In particular, add the "positivity constraint" and then perform
1234 static struct isl_basic_set
*homogeneous_map(struct isl_basic_set
*bset
,
1241 bset
= isl_basic_set_extend_constraints(bset
, 0, 1);
1242 k
= isl_basic_set_alloc_inequality(bset
);
1245 isl_seq_clr(bset
->ineq
[k
] + 1, isl_basic_set_total_dim(bset
));
1246 isl_int_set_si(bset
->ineq
[k
][0], 1);
1247 bset
= isl_basic_set_preimage(bset
, T
);
1251 isl_basic_set_free(bset
);
1255 /* Compute the convex hull of a pair of basic sets without any parameters or
1256 * integer divisions, where the convex hull is known to be pointed,
1257 * but the basic sets may be unbounded.
1259 * We turn this problem into the computation of a convex hull of a pair
1260 * _bounded_ polyhedra by "changing the direction of the homogeneous
1261 * dimension". This idea is due to Matthias Koeppe.
1263 * Consider the cones in homogeneous space that correspond to the
1264 * input polyhedra. The rays of these cones are also rays of the
1265 * polyhedra if the coordinate that corresponds to the homogeneous
1266 * dimension is zero. That is, if the inner product of the rays
1267 * with the homogeneous direction is zero.
1268 * The cones in the homogeneous space can also be considered to
1269 * correspond to other pairs of polyhedra by chosing a different
1270 * homogeneous direction. To ensure that both of these polyhedra
1271 * are bounded, we need to make sure that all rays of the cones
1272 * correspond to vertices and not to rays.
1273 * Let s be a direction such that <s, r> > 0 for all rays r of both cones.
1274 * Then using s as a homogeneous direction, we obtain a pair of polytopes.
1275 * The vector s is computed in valid_direction.
1277 * Note that we need to consider _all_ rays of the cones and not just
1278 * the rays that correspond to rays in the polyhedra. If we were to
1279 * only consider those rays and turn them into vertices, then we
1280 * may inadvertently turn some vertices into rays.
1282 * The standard homogeneous direction is the unit vector in the 0th coordinate.
1283 * We therefore transform the two polyhedra such that the selected
1284 * direction is mapped onto this standard direction and then proceed
1285 * with the normal computation.
1286 * Let S be a non-singular square matrix with s as its first row,
1287 * then we want to map the polyhedra to the space
1289 * [ y' ] [ y ] [ y ] [ y' ]
1290 * [ x' ] = S [ x ] i.e., [ x ] = S^{-1} [ x' ]
1292 * We take S to be the unimodular completion of s to limit the growth
1293 * of the coefficients in the following computations.
1295 * Let b_i + A_i x >= 0 be the constraints of polyhedron i.
1296 * We first move to the homogeneous dimension
1298 * b_i y + A_i x >= 0 [ b_i A_i ] [ y ] [ 0 ]
1299 * y >= 0 or [ 1 0 ] [ x ] >= [ 0 ]
1301 * Then we change directoin
1303 * [ b_i A_i ] [ y' ] [ y' ]
1304 * [ 1 0 ] S^{-1} [ x' ] >= 0 or [ b_i' A_i' ] [ x' ] >= 0
1306 * Then we compute the convex hull of the polytopes b_i' + A_i' x' >= 0
1307 * resulting in b' + A' x' >= 0, which we then convert back
1310 * [ b' A' ] S [ x ] >= 0 or [ b A ] [ x ] >= 0
1312 * The polyhedron b + A x >= 0 is then the convex hull of the input polyhedra.
1314 static struct isl_basic_set
*convex_hull_pair_pointed(
1315 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
1317 struct isl_ctx
*ctx
= NULL
;
1318 struct isl_vec
*dir
= NULL
;
1319 struct isl_mat
*T
= NULL
;
1320 struct isl_mat
*T2
= NULL
;
1321 struct isl_basic_set
*hull
;
1322 struct isl_set
*set
;
1324 if (!bset1
|| !bset2
)
1326 ctx
= isl_basic_set_get_ctx(bset1
);
1327 dir
= valid_direction(isl_basic_set_copy(bset1
),
1328 isl_basic_set_copy(bset2
));
1331 T
= isl_mat_alloc(ctx
, dir
->size
, dir
->size
);
1334 isl_seq_cpy(T
->row
[0], dir
->block
.data
, dir
->size
);
1335 T
= isl_mat_unimodular_complete(T
, 1);
1336 T2
= isl_mat_right_inverse(isl_mat_copy(T
));
1338 bset1
= homogeneous_map(bset1
, isl_mat_copy(T2
));
1339 bset2
= homogeneous_map(bset2
, T2
);
1340 set
= isl_set_alloc_space(isl_basic_set_get_space(bset1
), 2, 0);
1341 set
= isl_set_add_basic_set(set
, bset1
);
1342 set
= isl_set_add_basic_set(set
, bset2
);
1343 hull
= uset_convex_hull(set
);
1344 hull
= isl_basic_set_preimage(hull
, T
);
1351 isl_basic_set_free(bset1
);
1352 isl_basic_set_free(bset2
);
1356 static struct isl_basic_set
*uset_convex_hull_wrap(struct isl_set
*set
);
1357 static struct isl_basic_set
*modulo_affine_hull(
1358 struct isl_set
*set
, struct isl_basic_set
*affine_hull
);
1360 /* Compute the convex hull of a pair of basic sets without any parameters or
1361 * integer divisions.
1363 * This function is called from uset_convex_hull_unbounded, which
1364 * means that the complete convex hull is unbounded. Some pairs
1365 * of basic sets may still be bounded, though.
1366 * They may even lie inside a lower dimensional space, in which
1367 * case they need to be handled inside their affine hull since
1368 * the main algorithm assumes that the result is full-dimensional.
1370 * If the convex hull of the two basic sets would have a non-trivial
1371 * lineality space, we first project out this lineality space.
1373 static struct isl_basic_set
*convex_hull_pair(struct isl_basic_set
*bset1
,
1374 struct isl_basic_set
*bset2
)
1376 isl_basic_set
*lin
, *aff
;
1377 int bounded1
, bounded2
;
1379 if (bset1
->ctx
->opt
->convex
== ISL_CONVEX_HULL_FM
)
1380 return convex_hull_pair_elim(bset1
, bset2
);
1382 aff
= isl_set_affine_hull(isl_basic_set_union(isl_basic_set_copy(bset1
),
1383 isl_basic_set_copy(bset2
)));
1387 return modulo_affine_hull(isl_basic_set_union(bset1
, bset2
), aff
);
1388 isl_basic_set_free(aff
);
1390 bounded1
= isl_basic_set_is_bounded(bset1
);
1391 bounded2
= isl_basic_set_is_bounded(bset2
);
1393 if (bounded1
< 0 || bounded2
< 0)
1396 if (bounded1
&& bounded2
)
1397 return uset_convex_hull_wrap(isl_basic_set_union(bset1
, bset2
));
1399 if (bounded1
|| bounded2
)
1400 return convex_hull_pair_pointed(bset1
, bset2
);
1402 lin
= induced_lineality_space(isl_basic_set_copy(bset1
),
1403 isl_basic_set_copy(bset2
));
1406 if (isl_basic_set_plain_is_universe(lin
)) {
1407 isl_basic_set_free(bset1
);
1408 isl_basic_set_free(bset2
);
1411 if (lin
->n_eq
< isl_basic_set_total_dim(lin
)) {
1412 struct isl_set
*set
;
1413 set
= isl_set_alloc_space(isl_basic_set_get_space(bset1
), 2, 0);
1414 set
= isl_set_add_basic_set(set
, bset1
);
1415 set
= isl_set_add_basic_set(set
, bset2
);
1416 return modulo_lineality(set
, lin
);
1418 isl_basic_set_free(lin
);
1420 return convex_hull_pair_pointed(bset1
, bset2
);
1422 isl_basic_set_free(bset1
);
1423 isl_basic_set_free(bset2
);
1427 /* Compute the lineality space of a basic set.
1428 * We currently do not allow the basic set to have any divs.
1429 * We basically just drop the constants and turn every inequality
1432 struct isl_basic_set
*isl_basic_set_lineality_space(struct isl_basic_set
*bset
)
1435 struct isl_basic_set
*lin
= NULL
;
1440 isl_assert(bset
->ctx
, bset
->n_div
== 0, goto error
);
1441 dim
= isl_basic_set_total_dim(bset
);
1443 lin
= isl_basic_set_alloc_space(isl_basic_set_get_space(bset
), 0, dim
, 0);
1446 for (i
= 0; i
< bset
->n_eq
; ++i
) {
1447 k
= isl_basic_set_alloc_equality(lin
);
1450 isl_int_set_si(lin
->eq
[k
][0], 0);
1451 isl_seq_cpy(lin
->eq
[k
] + 1, bset
->eq
[i
] + 1, dim
);
1453 lin
= isl_basic_set_gauss(lin
, NULL
);
1456 for (i
= 0; i
< bset
->n_ineq
&& lin
->n_eq
< dim
; ++i
) {
1457 k
= isl_basic_set_alloc_equality(lin
);
1460 isl_int_set_si(lin
->eq
[k
][0], 0);
1461 isl_seq_cpy(lin
->eq
[k
] + 1, bset
->ineq
[i
] + 1, dim
);
1462 lin
= isl_basic_set_gauss(lin
, NULL
);
1466 isl_basic_set_free(bset
);
1469 isl_basic_set_free(lin
);
1470 isl_basic_set_free(bset
);
1474 /* Compute the (linear) hull of the lineality spaces of the basic sets in the
1475 * "underlying" set "set".
1477 static struct isl_basic_set
*uset_combined_lineality_space(struct isl_set
*set
)
1480 struct isl_set
*lin
= NULL
;
1485 isl_space
*dim
= isl_set_get_space(set
);
1487 return isl_basic_set_empty(dim
);
1490 lin
= isl_set_alloc_space(isl_set_get_space(set
), set
->n
, 0);
1491 for (i
= 0; i
< set
->n
; ++i
)
1492 lin
= isl_set_add_basic_set(lin
,
1493 isl_basic_set_lineality_space(isl_basic_set_copy(set
->p
[i
])));
1495 return isl_set_affine_hull(lin
);
1498 /* Compute the convex hull of a set without any parameters or
1499 * integer divisions.
1500 * In each step, we combined two basic sets until only one
1501 * basic set is left.
1502 * The input basic sets are assumed not to have a non-trivial
1503 * lineality space. If any of the intermediate results has
1504 * a non-trivial lineality space, it is projected out.
1506 static struct isl_basic_set
*uset_convex_hull_unbounded(struct isl_set
*set
)
1508 struct isl_basic_set
*convex_hull
= NULL
;
1510 convex_hull
= isl_set_copy_basic_set(set
);
1511 set
= isl_set_drop_basic_set(set
, convex_hull
);
1514 while (set
->n
> 0) {
1515 struct isl_basic_set
*t
;
1516 t
= isl_set_copy_basic_set(set
);
1519 set
= isl_set_drop_basic_set(set
, t
);
1522 convex_hull
= convex_hull_pair(convex_hull
, t
);
1525 t
= isl_basic_set_lineality_space(isl_basic_set_copy(convex_hull
));
1528 if (isl_basic_set_plain_is_universe(t
)) {
1529 isl_basic_set_free(convex_hull
);
1533 if (t
->n_eq
< isl_basic_set_total_dim(t
)) {
1534 set
= isl_set_add_basic_set(set
, convex_hull
);
1535 return modulo_lineality(set
, t
);
1537 isl_basic_set_free(t
);
1543 isl_basic_set_free(convex_hull
);
1547 /* Compute an initial hull for wrapping containing a single initial
1549 * This function assumes that the given set is bounded.
1551 static struct isl_basic_set
*initial_hull(struct isl_basic_set
*hull
,
1552 struct isl_set
*set
)
1554 struct isl_mat
*bounds
= NULL
;
1560 bounds
= initial_facet_constraint(set
);
1563 k
= isl_basic_set_alloc_inequality(hull
);
1566 dim
= isl_set_n_dim(set
);
1567 isl_assert(set
->ctx
, 1 + dim
== bounds
->n_col
, goto error
);
1568 isl_seq_cpy(hull
->ineq
[k
], bounds
->row
[0], bounds
->n_col
);
1569 isl_mat_free(bounds
);
1573 isl_basic_set_free(hull
);
1574 isl_mat_free(bounds
);
1578 struct max_constraint
{
1584 static int max_constraint_equal(const void *entry
, const void *val
)
1586 struct max_constraint
*a
= (struct max_constraint
*)entry
;
1587 isl_int
*b
= (isl_int
*)val
;
1589 return isl_seq_eq(a
->c
->row
[0] + 1, b
, a
->c
->n_col
- 1);
1592 static void update_constraint(struct isl_ctx
*ctx
, struct isl_hash_table
*table
,
1593 isl_int
*con
, unsigned len
, int n
, int ineq
)
1595 struct isl_hash_table_entry
*entry
;
1596 struct max_constraint
*c
;
1599 c_hash
= isl_seq_get_hash(con
+ 1, len
);
1600 entry
= isl_hash_table_find(ctx
, table
, c_hash
, max_constraint_equal
,
1606 isl_hash_table_remove(ctx
, table
, entry
);
1610 if (isl_int_gt(c
->c
->row
[0][0], con
[0]))
1612 if (isl_int_eq(c
->c
->row
[0][0], con
[0])) {
1617 c
->c
= isl_mat_cow(c
->c
);
1618 isl_int_set(c
->c
->row
[0][0], con
[0]);
1622 /* Check whether the constraint hash table "table" constains the constraint
1625 static int has_constraint(struct isl_ctx
*ctx
, struct isl_hash_table
*table
,
1626 isl_int
*con
, unsigned len
, int n
)
1628 struct isl_hash_table_entry
*entry
;
1629 struct max_constraint
*c
;
1632 c_hash
= isl_seq_get_hash(con
+ 1, len
);
1633 entry
= isl_hash_table_find(ctx
, table
, c_hash
, max_constraint_equal
,
1640 return isl_int_eq(c
->c
->row
[0][0], con
[0]);
1643 /* Check for inequality constraints of a basic set without equalities
1644 * such that the same or more stringent copies of the constraint appear
1645 * in all of the basic sets. Such constraints are necessarily facet
1646 * constraints of the convex hull.
1648 * If the resulting basic set is by chance identical to one of
1649 * the basic sets in "set", then we know that this basic set contains
1650 * all other basic sets and is therefore the convex hull of set.
1651 * In this case we set *is_hull to 1.
1653 static struct isl_basic_set
*common_constraints(struct isl_basic_set
*hull
,
1654 struct isl_set
*set
, int *is_hull
)
1657 int min_constraints
;
1659 struct max_constraint
*constraints
= NULL
;
1660 struct isl_hash_table
*table
= NULL
;
1665 for (i
= 0; i
< set
->n
; ++i
)
1666 if (set
->p
[i
]->n_eq
== 0)
1670 min_constraints
= set
->p
[i
]->n_ineq
;
1672 for (i
= best
+ 1; i
< set
->n
; ++i
) {
1673 if (set
->p
[i
]->n_eq
!= 0)
1675 if (set
->p
[i
]->n_ineq
>= min_constraints
)
1677 min_constraints
= set
->p
[i
]->n_ineq
;
1680 constraints
= isl_calloc_array(hull
->ctx
, struct max_constraint
,
1684 table
= isl_alloc_type(hull
->ctx
, struct isl_hash_table
);
1685 if (isl_hash_table_init(hull
->ctx
, table
, min_constraints
))
1688 total
= isl_space_dim(set
->dim
, isl_dim_all
);
1689 for (i
= 0; i
< set
->p
[best
]->n_ineq
; ++i
) {
1690 constraints
[i
].c
= isl_mat_sub_alloc6(hull
->ctx
,
1691 set
->p
[best
]->ineq
+ i
, 0, 1, 0, 1 + total
);
1692 if (!constraints
[i
].c
)
1694 constraints
[i
].ineq
= 1;
1696 for (i
= 0; i
< min_constraints
; ++i
) {
1697 struct isl_hash_table_entry
*entry
;
1699 c_hash
= isl_seq_get_hash(constraints
[i
].c
->row
[0] + 1, total
);
1700 entry
= isl_hash_table_find(hull
->ctx
, table
, c_hash
,
1701 max_constraint_equal
, constraints
[i
].c
->row
[0] + 1, 1);
1704 isl_assert(hull
->ctx
, !entry
->data
, goto error
);
1705 entry
->data
= &constraints
[i
];
1709 for (s
= 0; s
< set
->n
; ++s
) {
1713 for (i
= 0; i
< set
->p
[s
]->n_eq
; ++i
) {
1714 isl_int
*eq
= set
->p
[s
]->eq
[i
];
1715 for (j
= 0; j
< 2; ++j
) {
1716 isl_seq_neg(eq
, eq
, 1 + total
);
1717 update_constraint(hull
->ctx
, table
,
1721 for (i
= 0; i
< set
->p
[s
]->n_ineq
; ++i
) {
1722 isl_int
*ineq
= set
->p
[s
]->ineq
[i
];
1723 update_constraint(hull
->ctx
, table
, ineq
, total
, n
,
1724 set
->p
[s
]->n_eq
== 0);
1729 for (i
= 0; i
< min_constraints
; ++i
) {
1730 if (constraints
[i
].count
< n
)
1732 if (!constraints
[i
].ineq
)
1734 j
= isl_basic_set_alloc_inequality(hull
);
1737 isl_seq_cpy(hull
->ineq
[j
], constraints
[i
].c
->row
[0], 1 + total
);
1740 for (s
= 0; s
< set
->n
; ++s
) {
1741 if (set
->p
[s
]->n_eq
)
1743 if (set
->p
[s
]->n_ineq
!= hull
->n_ineq
)
1745 for (i
= 0; i
< set
->p
[s
]->n_ineq
; ++i
) {
1746 isl_int
*ineq
= set
->p
[s
]->ineq
[i
];
1747 if (!has_constraint(hull
->ctx
, table
, ineq
, total
, n
))
1750 if (i
== set
->p
[s
]->n_ineq
)
1754 isl_hash_table_clear(table
);
1755 for (i
= 0; i
< min_constraints
; ++i
)
1756 isl_mat_free(constraints
[i
].c
);
1761 isl_hash_table_clear(table
);
1764 for (i
= 0; i
< min_constraints
; ++i
)
1765 isl_mat_free(constraints
[i
].c
);
1770 /* Create a template for the convex hull of "set" and fill it up
1771 * obvious facet constraints, if any. If the result happens to
1772 * be the convex hull of "set" then *is_hull is set to 1.
1774 static struct isl_basic_set
*proto_hull(struct isl_set
*set
, int *is_hull
)
1776 struct isl_basic_set
*hull
;
1781 for (i
= 0; i
< set
->n
; ++i
) {
1782 n_ineq
+= set
->p
[i
]->n_eq
;
1783 n_ineq
+= set
->p
[i
]->n_ineq
;
1785 hull
= isl_basic_set_alloc_space(isl_space_copy(set
->dim
), 0, 0, n_ineq
);
1786 hull
= isl_basic_set_set_rational(hull
);
1789 return common_constraints(hull
, set
, is_hull
);
1792 static struct isl_basic_set
*uset_convex_hull_wrap(struct isl_set
*set
)
1794 struct isl_basic_set
*hull
;
1797 hull
= proto_hull(set
, &is_hull
);
1798 if (hull
&& !is_hull
) {
1799 if (hull
->n_ineq
== 0)
1800 hull
= initial_hull(hull
, set
);
1801 hull
= extend(hull
, set
);
1808 /* Compute the convex hull of a set without any parameters or
1809 * integer divisions. Depending on whether the set is bounded,
1810 * we pass control to the wrapping based convex hull or
1811 * the Fourier-Motzkin elimination based convex hull.
1812 * We also handle a few special cases before checking the boundedness.
1814 static struct isl_basic_set
*uset_convex_hull(struct isl_set
*set
)
1816 struct isl_basic_set
*convex_hull
= NULL
;
1817 struct isl_basic_set
*lin
;
1819 if (isl_set_n_dim(set
) == 0)
1820 return convex_hull_0d(set
);
1822 set
= isl_set_coalesce(set
);
1823 set
= isl_set_set_rational(set
);
1830 convex_hull
= isl_basic_set_copy(set
->p
[0]);
1834 if (isl_set_n_dim(set
) == 1)
1835 return convex_hull_1d(set
);
1837 if (isl_set_is_bounded(set
) &&
1838 set
->ctx
->opt
->convex
== ISL_CONVEX_HULL_WRAP
)
1839 return uset_convex_hull_wrap(set
);
1841 lin
= uset_combined_lineality_space(isl_set_copy(set
));
1844 if (isl_basic_set_plain_is_universe(lin
)) {
1848 if (lin
->n_eq
< isl_basic_set_total_dim(lin
))
1849 return modulo_lineality(set
, lin
);
1850 isl_basic_set_free(lin
);
1852 return uset_convex_hull_unbounded(set
);
1855 isl_basic_set_free(convex_hull
);
1859 /* This is the core procedure, where "set" is a "pure" set, i.e.,
1860 * without parameters or divs and where the convex hull of set is
1861 * known to be full-dimensional.
1863 static struct isl_basic_set
*uset_convex_hull_wrap_bounded(struct isl_set
*set
)
1865 struct isl_basic_set
*convex_hull
= NULL
;
1870 if (isl_set_n_dim(set
) == 0) {
1871 convex_hull
= isl_basic_set_universe(isl_space_copy(set
->dim
));
1873 convex_hull
= isl_basic_set_set_rational(convex_hull
);
1877 set
= isl_set_set_rational(set
);
1878 set
= isl_set_coalesce(set
);
1882 convex_hull
= isl_basic_set_copy(set
->p
[0]);
1884 convex_hull
= isl_basic_map_remove_redundancies(convex_hull
);
1887 if (isl_set_n_dim(set
) == 1)
1888 return convex_hull_1d(set
);
1890 return uset_convex_hull_wrap(set
);
1896 /* Compute the convex hull of set "set" with affine hull "affine_hull",
1897 * We first remove the equalities (transforming the set), compute the
1898 * convex hull of the transformed set and then add the equalities back
1899 * (after performing the inverse transformation.
1901 static struct isl_basic_set
*modulo_affine_hull(
1902 struct isl_set
*set
, struct isl_basic_set
*affine_hull
)
1906 struct isl_basic_set
*dummy
;
1907 struct isl_basic_set
*convex_hull
;
1909 dummy
= isl_basic_set_remove_equalities(
1910 isl_basic_set_copy(affine_hull
), &T
, &T2
);
1913 isl_basic_set_free(dummy
);
1914 set
= isl_set_preimage(set
, T
);
1915 convex_hull
= uset_convex_hull(set
);
1916 convex_hull
= isl_basic_set_preimage(convex_hull
, T2
);
1917 convex_hull
= isl_basic_set_intersect(convex_hull
, affine_hull
);
1920 isl_basic_set_free(affine_hull
);
1925 /* Return an empty basic map living in the same space as "map".
1927 static __isl_give isl_basic_map
*replace_map_by_empty_basic_map(
1928 __isl_take isl_map
*map
)
1932 space
= isl_map_get_space(map
);
1934 return isl_basic_map_empty(space
);
1937 /* Compute the convex hull of a map.
1939 * The implementation was inspired by "Extended Convex Hull" by Fukuda et al.,
1940 * specifically, the wrapping of facets to obtain new facets.
1942 struct isl_basic_map
*isl_map_convex_hull(struct isl_map
*map
)
1944 struct isl_basic_set
*bset
;
1945 struct isl_basic_map
*model
= NULL
;
1946 struct isl_basic_set
*affine_hull
= NULL
;
1947 struct isl_basic_map
*convex_hull
= NULL
;
1948 struct isl_set
*set
= NULL
;
1950 map
= isl_map_detect_equalities(map
);
1951 map
= isl_map_align_divs(map
);
1956 return replace_map_by_empty_basic_map(map
);
1958 model
= isl_basic_map_copy(map
->p
[0]);
1959 set
= isl_map_underlying_set(map
);
1963 affine_hull
= isl_set_affine_hull(isl_set_copy(set
));
1966 if (affine_hull
->n_eq
!= 0)
1967 bset
= modulo_affine_hull(set
, affine_hull
);
1969 isl_basic_set_free(affine_hull
);
1970 bset
= uset_convex_hull(set
);
1973 convex_hull
= isl_basic_map_overlying_set(bset
, model
);
1977 ISL_F_SET(convex_hull
, ISL_BASIC_MAP_NO_IMPLICIT
);
1978 ISL_F_SET(convex_hull
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1979 ISL_F_CLR(convex_hull
, ISL_BASIC_MAP_RATIONAL
);
1983 isl_basic_map_free(model
);
1987 struct isl_basic_set
*isl_set_convex_hull(struct isl_set
*set
)
1989 return (struct isl_basic_set
*)
1990 isl_map_convex_hull((struct isl_map
*)set
);
1993 __isl_give isl_basic_map
*isl_map_polyhedral_hull(__isl_take isl_map
*map
)
1995 isl_basic_map
*hull
;
1997 hull
= isl_map_convex_hull(map
);
1998 return isl_basic_map_remove_divs(hull
);
2001 __isl_give isl_basic_set
*isl_set_polyhedral_hull(__isl_take isl_set
*set
)
2003 return (isl_basic_set
*)isl_map_polyhedral_hull((isl_map
*)set
);
2006 struct sh_data_entry
{
2007 struct isl_hash_table
*table
;
2008 struct isl_tab
*tab
;
2011 /* Holds the data needed during the simple hull computation.
2013 * n the number of basic sets in the original set
2014 * hull_table a hash table of already computed constraints
2015 * in the simple hull
2016 * p for each basic set,
2017 * table a hash table of the constraints
2018 * tab the tableau corresponding to the basic set
2021 struct isl_ctx
*ctx
;
2023 struct isl_hash_table
*hull_table
;
2024 struct sh_data_entry p
[1];
2027 static void sh_data_free(struct sh_data
*data
)
2033 isl_hash_table_free(data
->ctx
, data
->hull_table
);
2034 for (i
= 0; i
< data
->n
; ++i
) {
2035 isl_hash_table_free(data
->ctx
, data
->p
[i
].table
);
2036 isl_tab_free(data
->p
[i
].tab
);
2041 struct ineq_cmp_data
{
2046 static int has_ineq(const void *entry
, const void *val
)
2048 isl_int
*row
= (isl_int
*)entry
;
2049 struct ineq_cmp_data
*v
= (struct ineq_cmp_data
*)val
;
2051 return isl_seq_eq(row
+ 1, v
->p
+ 1, v
->len
) ||
2052 isl_seq_is_neg(row
+ 1, v
->p
+ 1, v
->len
);
2055 static int hash_ineq(struct isl_ctx
*ctx
, struct isl_hash_table
*table
,
2056 isl_int
*ineq
, unsigned len
)
2059 struct ineq_cmp_data v
;
2060 struct isl_hash_table_entry
*entry
;
2064 c_hash
= isl_seq_get_hash(ineq
+ 1, len
);
2065 entry
= isl_hash_table_find(ctx
, table
, c_hash
, has_ineq
, &v
, 1);
2072 /* Fill hash table "table" with the constraints of "bset".
2073 * Equalities are added as two inequalities.
2074 * The value in the hash table is a pointer to the (in)equality of "bset".
2076 static int hash_basic_set(struct isl_hash_table
*table
,
2077 struct isl_basic_set
*bset
)
2080 unsigned dim
= isl_basic_set_total_dim(bset
);
2082 for (i
= 0; i
< bset
->n_eq
; ++i
) {
2083 for (j
= 0; j
< 2; ++j
) {
2084 isl_seq_neg(bset
->eq
[i
], bset
->eq
[i
], 1 + dim
);
2085 if (hash_ineq(bset
->ctx
, table
, bset
->eq
[i
], dim
) < 0)
2089 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
2090 if (hash_ineq(bset
->ctx
, table
, bset
->ineq
[i
], dim
) < 0)
2096 static struct sh_data
*sh_data_alloc(struct isl_set
*set
, unsigned n_ineq
)
2098 struct sh_data
*data
;
2101 data
= isl_calloc(set
->ctx
, struct sh_data
,
2102 sizeof(struct sh_data
) +
2103 (set
->n
- 1) * sizeof(struct sh_data_entry
));
2106 data
->ctx
= set
->ctx
;
2108 data
->hull_table
= isl_hash_table_alloc(set
->ctx
, n_ineq
);
2109 if (!data
->hull_table
)
2111 for (i
= 0; i
< set
->n
; ++i
) {
2112 data
->p
[i
].table
= isl_hash_table_alloc(set
->ctx
,
2113 2 * set
->p
[i
]->n_eq
+ set
->p
[i
]->n_ineq
);
2114 if (!data
->p
[i
].table
)
2116 if (hash_basic_set(data
->p
[i
].table
, set
->p
[i
]) < 0)
2125 /* Check if inequality "ineq" is a bound for basic set "j" or if
2126 * it can be relaxed (by increasing the constant term) to become
2127 * a bound for that basic set. In the latter case, the constant
2129 * Relaxation of the constant term is only allowed if "shift" is set.
2131 * Return 1 if "ineq" is a bound
2132 * 0 if "ineq" may attain arbitrarily small values on basic set "j"
2133 * -1 if some error occurred
2135 static int is_bound(struct sh_data
*data
, struct isl_set
*set
, int j
,
2136 isl_int
*ineq
, int shift
)
2138 enum isl_lp_result res
;
2141 if (!data
->p
[j
].tab
) {
2142 data
->p
[j
].tab
= isl_tab_from_basic_set(set
->p
[j
], 0);
2143 if (!data
->p
[j
].tab
)
2149 res
= isl_tab_min(data
->p
[j
].tab
, ineq
, data
->ctx
->one
,
2151 if (res
== isl_lp_ok
&& isl_int_is_neg(opt
)) {
2153 isl_int_sub(ineq
[0], ineq
[0], opt
);
2155 res
= isl_lp_unbounded
;
2160 return (res
== isl_lp_ok
|| res
== isl_lp_empty
) ? 1 :
2161 res
== isl_lp_unbounded
? 0 : -1;
2164 /* Set the constant term of "ineq" to the maximum of those of the constraints
2165 * in the basic sets of "set" following "i" that are parallel to "ineq".
2166 * That is, if any of the basic sets of "set" following "i" have a more
2167 * relaxed copy of "ineq", then replace "ineq" by the most relaxed copy.
2168 * "c_hash" is the hash value of the linear part of "ineq".
2169 * "v" has been set up for use by has_ineq.
2171 * Note that the two inequality constraints corresponding to an equality are
2172 * represented by the same inequality constraint in data->p[j].table
2173 * (but with different hash values). This means the constraint (or at
2174 * least its constant term) may need to be temporarily negated to get
2175 * the actually hashed constraint.
2177 static void set_max_constant_term(struct sh_data
*data
, __isl_keep isl_set
*set
,
2178 int i
, isl_int
*ineq
, uint32_t c_hash
, struct ineq_cmp_data
*v
)
2182 struct isl_hash_table_entry
*entry
;
2184 ctx
= isl_set_get_ctx(set
);
2185 for (j
= i
+ 1; j
< set
->n
; ++j
) {
2189 entry
= isl_hash_table_find(ctx
, data
->p
[j
].table
,
2190 c_hash
, &has_ineq
, v
, 0);
2194 ineq_j
= entry
->data
;
2195 neg
= isl_seq_is_neg(ineq_j
+ 1, ineq
+ 1, v
->len
);
2197 isl_int_neg(ineq_j
[0], ineq_j
[0]);
2198 if (isl_int_gt(ineq_j
[0], ineq
[0]))
2199 isl_int_set(ineq
[0], ineq_j
[0]);
2201 isl_int_neg(ineq_j
[0], ineq_j
[0]);
2205 /* Check if inequality "ineq" from basic set "i" is or can be relaxed to
2206 * become a bound on the whole set. If so, add the (relaxed) inequality
2207 * to "hull". Relaxation is only allowed if "shift" is set.
2209 * We first check if "hull" already contains a translate of the inequality.
2210 * If so, we are done.
2211 * Then, we check if any of the previous basic sets contains a translate
2212 * of the inequality. If so, then we have already considered this
2213 * inequality and we are done.
2214 * Otherwise, for each basic set other than "i", we check if the inequality
2215 * is a bound on the basic set, but first replace the constant term
2216 * by the maximal value of any translate of the inequality in any
2217 * of the following basic sets.
2218 * For previous basic sets, we know that they do not contain a translate
2219 * of the inequality, so we directly call is_bound.
2220 * For following basic sets, we first check if a translate of the
2221 * inequality appears in its description. If so, the constant term
2222 * of the inequality has already been updated with respect to this
2223 * translate and the inequality is therefore known to be a bound
2224 * of this basic set.
2226 static struct isl_basic_set
*add_bound(struct isl_basic_set
*hull
,
2227 struct sh_data
*data
, struct isl_set
*set
, int i
, isl_int
*ineq
,
2231 struct ineq_cmp_data v
;
2232 struct isl_hash_table_entry
*entry
;
2238 v
.len
= isl_basic_set_total_dim(hull
);
2240 c_hash
= isl_seq_get_hash(ineq
+ 1, v
.len
);
2242 entry
= isl_hash_table_find(hull
->ctx
, data
->hull_table
, c_hash
,
2247 for (j
= 0; j
< i
; ++j
) {
2248 entry
= isl_hash_table_find(hull
->ctx
, data
->p
[j
].table
,
2249 c_hash
, has_ineq
, &v
, 0);
2256 k
= isl_basic_set_alloc_inequality(hull
);
2259 isl_seq_cpy(hull
->ineq
[k
], ineq
, 1 + v
.len
);
2261 set_max_constant_term(data
, set
, i
, hull
->ineq
[k
], c_hash
, &v
);
2262 for (j
= 0; j
< i
; ++j
) {
2264 bound
= is_bound(data
, set
, j
, hull
->ineq
[k
], shift
);
2271 isl_basic_set_free_inequality(hull
, 1);
2275 for (j
= i
+ 1; j
< set
->n
; ++j
) {
2277 entry
= isl_hash_table_find(hull
->ctx
, data
->p
[j
].table
,
2278 c_hash
, has_ineq
, &v
, 0);
2281 bound
= is_bound(data
, set
, j
, hull
->ineq
[k
], shift
);
2288 isl_basic_set_free_inequality(hull
, 1);
2292 entry
= isl_hash_table_find(hull
->ctx
, data
->hull_table
, c_hash
,
2296 entry
->data
= hull
->ineq
[k
];
2300 isl_basic_set_free(hull
);
2304 /* Check if any inequality from basic set "i" is or can be relaxed to
2305 * become a bound on the whole set. If so, add the (relaxed) inequality
2306 * to "hull". Relaxation is only allowed if "shift" is set.
2308 static struct isl_basic_set
*add_bounds(struct isl_basic_set
*bset
,
2309 struct sh_data
*data
, struct isl_set
*set
, int i
, int shift
)
2312 unsigned dim
= isl_basic_set_total_dim(bset
);
2314 for (j
= 0; j
< set
->p
[i
]->n_eq
; ++j
) {
2315 for (k
= 0; k
< 2; ++k
) {
2316 isl_seq_neg(set
->p
[i
]->eq
[j
], set
->p
[i
]->eq
[j
], 1+dim
);
2317 bset
= add_bound(bset
, data
, set
, i
, set
->p
[i
]->eq
[j
],
2321 for (j
= 0; j
< set
->p
[i
]->n_ineq
; ++j
)
2322 bset
= add_bound(bset
, data
, set
, i
, set
->p
[i
]->ineq
[j
], shift
);
2326 /* Compute a superset of the convex hull of set that is described
2327 * by only (translates of) the constraints in the constituents of set.
2328 * Translation is only allowed if "shift" is set.
2330 static __isl_give isl_basic_set
*uset_simple_hull(__isl_take isl_set
*set
,
2333 struct sh_data
*data
= NULL
;
2334 struct isl_basic_set
*hull
= NULL
;
2342 for (i
= 0; i
< set
->n
; ++i
) {
2345 n_ineq
+= 2 * set
->p
[i
]->n_eq
+ set
->p
[i
]->n_ineq
;
2348 hull
= isl_basic_set_alloc_space(isl_space_copy(set
->dim
), 0, 0, n_ineq
);
2352 data
= sh_data_alloc(set
, n_ineq
);
2356 for (i
= 0; i
< set
->n
; ++i
)
2357 hull
= add_bounds(hull
, data
, set
, i
, shift
);
2365 isl_basic_set_free(hull
);
2370 /* Compute a superset of the convex hull of map that is described
2371 * by only (translates of) the constraints in the constituents of map.
2372 * Handle trivial cases where map is NULL or contains at most one disjunct.
2374 static __isl_give isl_basic_map
*map_simple_hull_trivial(
2375 __isl_take isl_map
*map
)
2377 isl_basic_map
*hull
;
2382 return replace_map_by_empty_basic_map(map
);
2384 hull
= isl_basic_map_copy(map
->p
[0]);
2389 /* Return a copy of the simple hull cached inside "map".
2390 * "shift" determines whether to return the cached unshifted or shifted
2393 static __isl_give isl_basic_map
*cached_simple_hull(__isl_take isl_map
*map
,
2396 isl_basic_map
*hull
;
2398 hull
= isl_basic_map_copy(map
->cached_simple_hull
[shift
]);
2404 /* Compute a superset of the convex hull of map that is described
2405 * by only (translates of) the constraints in the constituents of map.
2406 * Translation is only allowed if "shift" is set.
2408 * The constraints are sorted while removing redundant constraints
2409 * in order to indicate a preference of which constraints should
2410 * be preserved. In particular, pairs of constraints that are
2411 * sorted together are preferred to either both be preserved
2412 * or both be removed. The sorting is performed inside
2413 * isl_basic_map_remove_redundancies.
2415 * The result of the computation is stored in map->cached_simple_hull[shift]
2416 * such that it can be reused in subsequent calls. The cache is cleared
2417 * whenever the map is modified (in isl_map_cow).
2418 * Note that the results need to be stored in the input map for there
2419 * to be any chance that they may get reused. In particular, they
2420 * are stored in a copy of the input map that is saved before
2421 * the integer division alignment.
2423 static __isl_give isl_basic_map
*map_simple_hull(__isl_take isl_map
*map
,
2426 struct isl_set
*set
= NULL
;
2427 struct isl_basic_map
*model
= NULL
;
2428 struct isl_basic_map
*hull
;
2429 struct isl_basic_map
*affine_hull
;
2430 struct isl_basic_set
*bset
= NULL
;
2433 if (!map
|| map
->n
<= 1)
2434 return map_simple_hull_trivial(map
);
2436 if (map
->cached_simple_hull
[shift
])
2437 return cached_simple_hull(map
, shift
);
2439 map
= isl_map_detect_equalities(map
);
2440 if (!map
|| map
->n
<= 1)
2441 return map_simple_hull_trivial(map
);
2442 affine_hull
= isl_map_affine_hull(isl_map_copy(map
));
2443 input
= isl_map_copy(map
);
2444 map
= isl_map_align_divs(map
);
2445 model
= map
? isl_basic_map_copy(map
->p
[0]) : NULL
;
2447 set
= isl_map_underlying_set(map
);
2449 bset
= uset_simple_hull(set
, shift
);
2451 hull
= isl_basic_map_overlying_set(bset
, model
);
2453 hull
= isl_basic_map_intersect(hull
, affine_hull
);
2454 hull
= isl_basic_map_remove_redundancies(hull
);
2457 ISL_F_SET(hull
, ISL_BASIC_MAP_NO_IMPLICIT
);
2458 ISL_F_SET(hull
, ISL_BASIC_MAP_ALL_EQUALITIES
);
2461 hull
= isl_basic_map_finalize(hull
);
2463 input
->cached_simple_hull
[shift
] = isl_basic_map_copy(hull
);
2464 isl_map_free(input
);
2469 /* Compute a superset of the convex hull of map that is described
2470 * by only translates of the constraints in the constituents of map.
2472 __isl_give isl_basic_map
*isl_map_simple_hull(__isl_take isl_map
*map
)
2474 return map_simple_hull(map
, 1);
2477 struct isl_basic_set
*isl_set_simple_hull(struct isl_set
*set
)
2479 return (struct isl_basic_set
*)
2480 isl_map_simple_hull((struct isl_map
*)set
);
2483 /* Compute a superset of the convex hull of map that is described
2484 * by only the constraints in the constituents of map.
2486 __isl_give isl_basic_map
*isl_map_unshifted_simple_hull(
2487 __isl_take isl_map
*map
)
2489 return map_simple_hull(map
, 0);
2492 __isl_give isl_basic_set
*isl_set_unshifted_simple_hull(
2493 __isl_take isl_set
*set
)
2495 return isl_map_unshifted_simple_hull(set
);
2498 /* Drop all inequalities from "bmap1" that do not also appear in "bmap2".
2499 * A constraint that appears with different constant terms
2500 * in "bmap1" and "bmap2" is also kept, with the least restrictive
2501 * (i.e., greatest) constant term.
2502 * "bmap1" and "bmap2" are assumed to have the same (known)
2503 * integer divisions.
2504 * The constraints of both "bmap1" and "bmap2" are assumed
2505 * to have been sorted using isl_basic_map_sort_constraints.
2507 * Run through the inequality constraints of "bmap1" and "bmap2"
2509 * Each constraint of "bmap1" without a matching constraint in "bmap2"
2511 * If a match is found, the constraint is kept. If needed, the constant
2512 * term of the constraint is adjusted.
2514 static __isl_give isl_basic_map
*select_shared_inequalities(
2515 __isl_take isl_basic_map
*bmap1
, __isl_keep isl_basic_map
*bmap2
)
2519 bmap1
= isl_basic_map_cow(bmap1
);
2520 if (!bmap1
|| !bmap2
)
2521 return isl_basic_map_free(bmap1
);
2523 i1
= bmap1
->n_ineq
- 1;
2524 i2
= bmap2
->n_ineq
- 1;
2525 while (bmap1
&& i1
>= 0 && i2
>= 0) {
2528 cmp
= isl_basic_map_constraint_cmp(bmap1
, bmap1
->ineq
[i1
],
2535 if (isl_basic_map_drop_inequality(bmap1
, i1
) < 0)
2536 bmap1
= isl_basic_map_free(bmap1
);
2540 if (isl_int_lt(bmap1
->ineq
[i1
][0], bmap2
->ineq
[i2
][0]))
2541 isl_int_set(bmap1
->ineq
[i1
][0], bmap2
->ineq
[i2
][0]);
2545 for (; i1
>= 0; --i1
)
2546 if (isl_basic_map_drop_inequality(bmap1
, i1
) < 0)
2547 bmap1
= isl_basic_map_free(bmap1
);
2552 /* Drop all equalities from "bmap1" that do not also appear in "bmap2".
2553 * "bmap1" and "bmap2" are assumed to have the same (known)
2554 * integer divisions.
2556 * Run through the equality constraints of "bmap1" and "bmap2".
2557 * Each constraint of "bmap1" without a matching constraint in "bmap2"
2560 static __isl_give isl_basic_map
*select_shared_equalities(
2561 __isl_take isl_basic_map
*bmap1
, __isl_keep isl_basic_map
*bmap2
)
2566 bmap1
= isl_basic_map_cow(bmap1
);
2567 if (!bmap1
|| !bmap2
)
2568 return isl_basic_map_free(bmap1
);
2570 total
= isl_basic_map_total_dim(bmap1
);
2572 i1
= bmap1
->n_eq
- 1;
2573 i2
= bmap2
->n_eq
- 1;
2574 while (bmap1
&& i1
>= 0 && i2
>= 0) {
2577 last1
= isl_seq_last_non_zero(bmap1
->eq
[i1
] + 1, total
);
2578 last2
= isl_seq_last_non_zero(bmap2
->eq
[i2
] + 1, total
);
2579 if (last1
> last2
) {
2583 if (last1
< last2
) {
2584 if (isl_basic_map_drop_equality(bmap1
, i1
) < 0)
2585 bmap1
= isl_basic_map_free(bmap1
);
2589 if (!isl_seq_eq(bmap1
->eq
[i1
], bmap2
->eq
[i2
], 1 + total
)) {
2590 if (isl_basic_map_drop_equality(bmap1
, i1
) < 0)
2591 bmap1
= isl_basic_map_free(bmap1
);
2596 for (; i1
>= 0; --i1
)
2597 if (isl_basic_map_drop_equality(bmap1
, i1
) < 0)
2598 bmap1
= isl_basic_map_free(bmap1
);
2603 /* Compute a superset of "bmap1" and "bmap2" that is described
2604 * by only the constraints that appear in both "bmap1" and "bmap2".
2606 * First drop constraints that involve unknown integer divisions
2607 * since it is not trivial to check whether two such integer divisions
2608 * in different basic maps are the same.
2609 * Then align the remaining (known) divs and sort the constraints.
2610 * Finally drop all inequalities and equalities from "bmap1" that
2611 * do not also appear in "bmap2".
2613 __isl_give isl_basic_map
*isl_basic_map_plain_unshifted_simple_hull(
2614 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
2616 bmap1
= isl_basic_map_drop_constraint_involving_unknown_divs(bmap1
);
2617 bmap2
= isl_basic_map_drop_constraint_involving_unknown_divs(bmap2
);
2618 bmap2
= isl_basic_map_align_divs(bmap2
, bmap1
);
2619 bmap1
= isl_basic_map_align_divs(bmap1
, bmap2
);
2620 bmap1
= isl_basic_map_gauss(bmap1
, NULL
);
2621 bmap2
= isl_basic_map_gauss(bmap2
, NULL
);
2622 bmap1
= isl_basic_map_sort_constraints(bmap1
);
2623 bmap2
= isl_basic_map_sort_constraints(bmap2
);
2625 bmap1
= select_shared_inequalities(bmap1
, bmap2
);
2626 bmap1
= select_shared_equalities(bmap1
, bmap2
);
2628 isl_basic_map_free(bmap2
);
2629 bmap1
= isl_basic_map_finalize(bmap1
);
2633 /* Compute a superset of the convex hull of "map" that is described
2634 * by only the constraints in the constituents of "map".
2635 * In particular, the result is composed of constraints that appear
2636 * in each of the basic maps of "map"
2638 * Constraints that involve unknown integer divisions are dropped
2639 * since it is not trivial to check whether two such integer divisions
2640 * in different basic maps are the same.
2642 * The hull is initialized from the first basic map and then
2643 * updated with respect to the other basic maps in turn.
2645 __isl_give isl_basic_map
*isl_map_plain_unshifted_simple_hull(
2646 __isl_take isl_map
*map
)
2649 isl_basic_map
*hull
;
2654 return map_simple_hull_trivial(map
);
2655 map
= isl_map_drop_constraint_involving_unknown_divs(map
);
2656 hull
= isl_basic_map_copy(map
->p
[0]);
2657 for (i
= 1; i
< map
->n
; ++i
) {
2658 isl_basic_map
*bmap_i
;
2660 bmap_i
= isl_basic_map_copy(map
->p
[i
]);
2661 hull
= isl_basic_map_plain_unshifted_simple_hull(hull
, bmap_i
);
2668 /* Compute a superset of the convex hull of "set" that is described
2669 * by only the constraints in the constituents of "set".
2670 * In particular, the result is composed of constraints that appear
2671 * in each of the basic sets of "set"
2673 __isl_give isl_basic_set
*isl_set_plain_unshifted_simple_hull(
2674 __isl_take isl_set
*set
)
2676 return isl_map_plain_unshifted_simple_hull(set
);
2679 /* Check if "ineq" is a bound on "set" and, if so, add it to "hull".
2681 * For each basic set in "set", we first check if the basic set
2682 * contains a translate of "ineq". If this translate is more relaxed,
2683 * then we assume that "ineq" is not a bound on this basic set.
2684 * Otherwise, we know that it is a bound.
2685 * If the basic set does not contain a translate of "ineq", then
2686 * we call is_bound to perform the test.
2688 static __isl_give isl_basic_set
*add_bound_from_constraint(
2689 __isl_take isl_basic_set
*hull
, struct sh_data
*data
,
2690 __isl_keep isl_set
*set
, isl_int
*ineq
)
2695 struct ineq_cmp_data v
;
2698 return isl_basic_set_free(hull
);
2700 v
.len
= isl_basic_set_total_dim(hull
);
2702 c_hash
= isl_seq_get_hash(ineq
+ 1, v
.len
);
2704 ctx
= isl_basic_set_get_ctx(hull
);
2705 for (i
= 0; i
< set
->n
; ++i
) {
2707 struct isl_hash_table_entry
*entry
;
2709 entry
= isl_hash_table_find(ctx
, data
->p
[i
].table
,
2710 c_hash
, &has_ineq
, &v
, 0);
2712 isl_int
*ineq_i
= entry
->data
;
2713 int neg
, more_relaxed
;
2715 neg
= isl_seq_is_neg(ineq_i
+ 1, ineq
+ 1, v
.len
);
2717 isl_int_neg(ineq_i
[0], ineq_i
[0]);
2718 more_relaxed
= isl_int_gt(ineq_i
[0], ineq
[0]);
2720 isl_int_neg(ineq_i
[0], ineq_i
[0]);
2726 bound
= is_bound(data
, set
, i
, ineq
, 0);
2728 return isl_basic_set_free(hull
);
2735 k
= isl_basic_set_alloc_inequality(hull
);
2737 return isl_basic_set_free(hull
);
2738 isl_seq_cpy(hull
->ineq
[k
], ineq
, 1 + v
.len
);
2743 /* Compute a superset of the convex hull of "set" that is described
2744 * by only some of the "n_ineq" constraints in the list "ineq", where "set"
2745 * has no parameters or integer divisions.
2747 * The inequalities in "ineq" are assumed to have been sorted such
2748 * that constraints with the same linear part appear together and
2749 * that among constraints with the same linear part, those with
2750 * smaller constant term appear first.
2752 * We reuse the same data structure that is used by uset_simple_hull,
2753 * but we do not need the hull table since we will not consider the
2754 * same constraint more than once. We therefore allocate it with zero size.
2756 * We run through the constraints and try to add them one by one,
2757 * skipping identical constraints. If we have added a constraint and
2758 * the next constraint is a more relaxed translate, then we skip this
2759 * next constraint as well.
2761 static __isl_give isl_basic_set
*uset_unshifted_simple_hull_from_constraints(
2762 __isl_take isl_set
*set
, int n_ineq
, isl_int
**ineq
)
2766 struct sh_data
*data
= NULL
;
2767 isl_basic_set
*hull
= NULL
;
2770 hull
= isl_basic_set_alloc_space(isl_set_get_space(set
), 0, 0, n_ineq
);
2774 data
= sh_data_alloc(set
, 0);
2778 dim
= isl_set_dim(set
, isl_dim_set
);
2779 for (i
= 0; i
< n_ineq
; ++i
) {
2780 int hull_n_ineq
= hull
->n_ineq
;
2783 parallel
= i
> 0 && isl_seq_eq(ineq
[i
- 1] + 1, ineq
[i
] + 1,
2786 (last_added
|| isl_int_eq(ineq
[i
- 1][0], ineq
[i
][0])))
2788 hull
= add_bound_from_constraint(hull
, data
, set
, ineq
[i
]);
2791 last_added
= hull
->n_ineq
> hull_n_ineq
;
2800 isl_basic_set_free(hull
);
2804 /* Collect pointers to all the inequalities in the elements of "list"
2805 * in "ineq". For equalities, store both a pointer to the equality and
2806 * a pointer to its opposite, which is first copied to "mat".
2807 * "ineq" and "mat" are assumed to have been preallocated to the right size
2808 * (the number of inequalities + 2 times the number of equalites and
2809 * the number of equalities, respectively).
2811 static __isl_give isl_mat
*collect_inequalities(__isl_take isl_mat
*mat
,
2812 __isl_keep isl_basic_set_list
*list
, isl_int
**ineq
)
2814 int i
, j
, n
, n_eq
, n_ineq
;
2821 n
= isl_basic_set_list_n_basic_set(list
);
2822 for (i
= 0; i
< n
; ++i
) {
2823 isl_basic_set
*bset
;
2824 bset
= isl_basic_set_list_get_basic_set(list
, i
);
2826 return isl_mat_free(mat
);
2827 for (j
= 0; j
< bset
->n_eq
; ++j
) {
2828 ineq
[n_ineq
++] = mat
->row
[n_eq
];
2829 ineq
[n_ineq
++] = bset
->eq
[j
];
2830 isl_seq_neg(mat
->row
[n_eq
++], bset
->eq
[j
], mat
->n_col
);
2832 for (j
= 0; j
< bset
->n_ineq
; ++j
)
2833 ineq
[n_ineq
++] = bset
->ineq
[j
];
2834 isl_basic_set_free(bset
);
2840 /* Comparison routine for use as an isl_sort callback.
2842 * Constraints with the same linear part are sorted together and
2843 * among constraints with the same linear part, those with smaller
2844 * constant term are sorted first.
2846 static int cmp_ineq(const void *a
, const void *b
, void *arg
)
2848 unsigned dim
= *(unsigned *) arg
;
2849 isl_int
* const *ineq1
= a
;
2850 isl_int
* const *ineq2
= b
;
2853 cmp
= isl_seq_cmp((*ineq1
) + 1, (*ineq2
) + 1, dim
);
2856 return isl_int_cmp((*ineq1
)[0], (*ineq2
)[0]);
2859 /* Compute a superset of the convex hull of "set" that is described
2860 * by only constraints in the elements of "list", where "set" has
2861 * no parameters or integer divisions.
2863 * We collect all the constraints in those elements and then
2864 * sort the constraints such that constraints with the same linear part
2865 * are sorted together and that those with smaller constant term are
2868 static __isl_give isl_basic_set
*uset_unshifted_simple_hull_from_basic_set_list(
2869 __isl_take isl_set
*set
, __isl_take isl_basic_set_list
*list
)
2871 int i
, n
, n_eq
, n_ineq
;
2874 isl_mat
*mat
= NULL
;
2875 isl_int
**ineq
= NULL
;
2876 isl_basic_set
*hull
;
2880 ctx
= isl_set_get_ctx(set
);
2884 n
= isl_basic_set_list_n_basic_set(list
);
2885 for (i
= 0; i
< n
; ++i
) {
2886 isl_basic_set
*bset
;
2887 bset
= isl_basic_set_list_get_basic_set(list
, i
);
2891 n_ineq
+= 2 * bset
->n_eq
+ bset
->n_ineq
;
2892 isl_basic_set_free(bset
);
2895 ineq
= isl_alloc_array(ctx
, isl_int
*, n_ineq
);
2896 if (n_ineq
> 0 && !ineq
)
2899 dim
= isl_set_dim(set
, isl_dim_set
);
2900 mat
= isl_mat_alloc(ctx
, n_eq
, 1 + dim
);
2901 mat
= collect_inequalities(mat
, list
, ineq
);
2905 if (isl_sort(ineq
, n_ineq
, sizeof(ineq
[0]), &cmp_ineq
, &dim
) < 0)
2908 hull
= uset_unshifted_simple_hull_from_constraints(set
, n_ineq
, ineq
);
2912 isl_basic_set_list_free(list
);
2918 isl_basic_set_list_free(list
);
2922 /* Compute a superset of the convex hull of "map" that is described
2923 * by only constraints in the elements of "list".
2925 * If the list is empty, then we can only describe the universe set.
2926 * If the input map is empty, then all constraints are valid, so
2927 * we return the intersection of the elements in "list".
2929 * Otherwise, we align all divs and temporarily treat them
2930 * as regular variables, computing the unshifted simple hull in
2931 * uset_unshifted_simple_hull_from_basic_set_list.
2933 static __isl_give isl_basic_map
*map_unshifted_simple_hull_from_basic_map_list(
2934 __isl_take isl_map
*map
, __isl_take isl_basic_map_list
*list
)
2936 isl_basic_map
*model
;
2937 isl_basic_map
*hull
;
2939 isl_basic_set_list
*bset_list
;
2944 if (isl_basic_map_list_n_basic_map(list
) == 0) {
2947 space
= isl_map_get_space(map
);
2949 isl_basic_map_list_free(list
);
2950 return isl_basic_map_universe(space
);
2952 if (isl_map_plain_is_empty(map
)) {
2954 return isl_basic_map_list_intersect(list
);
2957 map
= isl_map_align_divs_to_basic_map_list(map
, list
);
2960 list
= isl_basic_map_list_align_divs_to_basic_map(list
, map
->p
[0]);
2962 model
= isl_basic_map_list_get_basic_map(list
, 0);
2964 set
= isl_map_underlying_set(map
);
2965 bset_list
= isl_basic_map_list_underlying_set(list
);
2967 hull
= uset_unshifted_simple_hull_from_basic_set_list(set
, bset_list
);
2968 hull
= isl_basic_map_overlying_set(hull
, model
);
2973 isl_basic_map_list_free(list
);
2977 /* Return a sequence of the basic maps that make up the maps in "list".
2979 static __isl_give isl_basic_set_list
*collect_basic_maps(
2980 __isl_take isl_map_list
*list
)
2984 isl_basic_map_list
*bmap_list
;
2988 n
= isl_map_list_n_map(list
);
2989 ctx
= isl_map_list_get_ctx(list
);
2990 bmap_list
= isl_basic_map_list_alloc(ctx
, 0);
2992 for (i
= 0; i
< n
; ++i
) {
2994 isl_basic_map_list
*list_i
;
2996 map
= isl_map_list_get_map(list
, i
);
2997 map
= isl_map_compute_divs(map
);
2998 list_i
= isl_map_get_basic_map_list(map
);
3000 bmap_list
= isl_basic_map_list_concat(bmap_list
, list_i
);
3003 isl_map_list_free(list
);
3007 /* Compute a superset of the convex hull of "map" that is described
3008 * by only constraints in the elements of "list".
3010 * If "map" is the universe, then the convex hull (and therefore
3011 * any superset of the convexhull) is the universe as well.
3013 * Otherwise, we collect all the basic maps in the map list and
3014 * continue with map_unshifted_simple_hull_from_basic_map_list.
3016 __isl_give isl_basic_map
*isl_map_unshifted_simple_hull_from_map_list(
3017 __isl_take isl_map
*map
, __isl_take isl_map_list
*list
)
3019 isl_basic_map_list
*bmap_list
;
3022 is_universe
= isl_map_plain_is_universe(map
);
3023 if (is_universe
< 0)
3024 map
= isl_map_free(map
);
3025 if (is_universe
< 0 || is_universe
) {
3026 isl_map_list_free(list
);
3027 return isl_map_unshifted_simple_hull(map
);
3030 bmap_list
= collect_basic_maps(list
);
3031 return map_unshifted_simple_hull_from_basic_map_list(map
, bmap_list
);
3034 /* Compute a superset of the convex hull of "set" that is described
3035 * by only constraints in the elements of "list".
3037 __isl_give isl_basic_set
*isl_set_unshifted_simple_hull_from_set_list(
3038 __isl_take isl_set
*set
, __isl_take isl_set_list
*list
)
3040 return isl_map_unshifted_simple_hull_from_map_list(set
, list
);
3043 /* Given a set "set", return parametric bounds on the dimension "dim".
3045 static struct isl_basic_set
*set_bounds(struct isl_set
*set
, int dim
)
3047 unsigned set_dim
= isl_set_dim(set
, isl_dim_set
);
3048 set
= isl_set_copy(set
);
3049 set
= isl_set_eliminate_dims(set
, dim
+ 1, set_dim
- (dim
+ 1));
3050 set
= isl_set_eliminate_dims(set
, 0, dim
);
3051 return isl_set_convex_hull(set
);
3054 /* Computes a "simple hull" and then check if each dimension in the
3055 * resulting hull is bounded by a symbolic constant. If not, the
3056 * hull is intersected with the corresponding bounds on the whole set.
3058 struct isl_basic_set
*isl_set_bounded_simple_hull(struct isl_set
*set
)
3061 struct isl_basic_set
*hull
;
3062 unsigned nparam
, left
;
3063 int removed_divs
= 0;
3065 hull
= isl_set_simple_hull(isl_set_copy(set
));
3069 nparam
= isl_basic_set_dim(hull
, isl_dim_param
);
3070 for (i
= 0; i
< isl_basic_set_dim(hull
, isl_dim_set
); ++i
) {
3071 int lower
= 0, upper
= 0;
3072 struct isl_basic_set
*bounds
;
3074 left
= isl_basic_set_total_dim(hull
) - nparam
- i
- 1;
3075 for (j
= 0; j
< hull
->n_eq
; ++j
) {
3076 if (isl_int_is_zero(hull
->eq
[j
][1 + nparam
+ i
]))
3078 if (isl_seq_first_non_zero(hull
->eq
[j
]+1+nparam
+i
+1,
3085 for (j
= 0; j
< hull
->n_ineq
; ++j
) {
3086 if (isl_int_is_zero(hull
->ineq
[j
][1 + nparam
+ i
]))
3088 if (isl_seq_first_non_zero(hull
->ineq
[j
]+1+nparam
+i
+1,
3090 isl_seq_first_non_zero(hull
->ineq
[j
]+1+nparam
,
3093 if (isl_int_is_pos(hull
->ineq
[j
][1 + nparam
+ i
]))
3104 if (!removed_divs
) {
3105 set
= isl_set_remove_divs(set
);
3110 bounds
= set_bounds(set
, i
);
3111 hull
= isl_basic_set_intersect(hull
, bounds
);