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 #include <bset_to_bmap.c>
27 #include <bset_from_bmap.c>
28 #include <set_to_map.c>
30 static __isl_give isl_basic_set
*uset_convex_hull_wrap_bounded(
31 __isl_take isl_set
*set
);
34 * constraints. If the minimal value along the normal of a constraint
35 * is the same if the constraint is removed, then the constraint is redundant.
37 * Since some constraints may be mutually redundant, sort the constraints
38 * first such that constraints that involve existentially quantified
39 * variables are considered for removal before those that do not.
40 * The sorting is also needed for the use in map_simple_hull.
42 * Note that isl_tab_detect_implicit_equalities may also end up
43 * marking some constraints as redundant. Make sure the constraints
44 * are preserved and undo those marking such that isl_tab_detect_redundant
45 * can consider the constraints in the sorted order.
47 * Alternatively, we could have intersected the basic map with the
48 * corresponding equality and then checked if the dimension was that
51 __isl_give isl_basic_map
*isl_basic_map_remove_redundancies(
52 __isl_take isl_basic_map
*bmap
)
59 bmap
= isl_basic_map_gauss(bmap
, NULL
);
60 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
62 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
))
64 if (bmap
->n_ineq
<= 1)
67 bmap
= isl_basic_map_sort_constraints(bmap
);
68 tab
= isl_tab_from_basic_map(bmap
, 0);
72 if (isl_tab_detect_implicit_equalities(tab
) < 0)
74 if (isl_tab_restore_redundant(tab
) < 0)
77 if (isl_tab_detect_redundant(tab
) < 0)
79 bmap
= isl_basic_map_update_from_tab(bmap
, tab
);
83 ISL_F_SET(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
84 ISL_F_SET(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
88 isl_basic_map_free(bmap
);
92 __isl_give isl_basic_set
*isl_basic_set_remove_redundancies(
93 __isl_take isl_basic_set
*bset
)
95 return bset_from_bmap(
96 isl_basic_map_remove_redundancies(bset_to_bmap(bset
)));
99 /* Remove redundant constraints in each of the basic maps.
101 __isl_give isl_map
*isl_map_remove_redundancies(__isl_take isl_map
*map
)
103 return isl_map_inline_foreach_basic_map(map
,
104 &isl_basic_map_remove_redundancies
);
107 __isl_give isl_set
*isl_set_remove_redundancies(__isl_take isl_set
*set
)
109 return isl_map_remove_redundancies(set
);
112 /* Check if the set set is bound in the direction of the affine
113 * constraint c and if so, set the constant term such that the
114 * resulting constraint is a bounding constraint for the set.
116 static isl_bool
uset_is_bound(__isl_keep isl_set
*set
, isl_int
*c
, unsigned len
)
124 isl_int_init(opt_denom
);
126 for (j
= 0; j
< set
->n
; ++j
) {
127 enum isl_lp_result res
;
129 if (ISL_F_ISSET(set
->p
[j
], ISL_BASIC_SET_EMPTY
))
132 res
= isl_basic_set_solve_lp(set
->p
[j
],
133 0, c
, set
->ctx
->one
, &opt
, &opt_denom
, NULL
);
134 if (res
== isl_lp_unbounded
)
136 if (res
== isl_lp_error
)
138 if (res
== isl_lp_empty
) {
139 set
->p
[j
] = isl_basic_set_set_to_empty(set
->p
[j
]);
144 if (first
|| isl_int_is_neg(opt
)) {
145 if (!isl_int_is_one(opt_denom
))
146 isl_seq_scale(c
, c
, opt_denom
, len
);
147 isl_int_sub(c
[0], c
[0], opt
);
152 isl_int_clear(opt_denom
);
153 return isl_bool_ok(j
>= set
->n
);
156 isl_int_clear(opt_denom
);
157 return isl_bool_error
;
160 static __isl_give isl_set
*isl_set_add_basic_set_equality(
161 __isl_take isl_set
*set
, isl_int
*c
)
165 set
= isl_set_cow(set
);
168 for (i
= 0; i
< set
->n
; ++i
) {
169 set
->p
[i
] = isl_basic_set_add_eq(set
->p
[i
], c
);
179 /* Given a union of basic sets, construct the constraints for wrapping
180 * a facet around one of its ridges.
181 * In particular, if each of n the d-dimensional basic sets i in "set"
182 * contains the origin, satisfies the constraints x_1 >= 0 and x_2 >= 0
183 * and is defined by the constraints
187 * then the resulting set is of dimension n*(1+d) and has as constraints
196 static __isl_give isl_basic_set
*wrap_constraints(__isl_keep isl_set
*set
)
198 struct isl_basic_set
*lp
;
202 isl_size dim
, lp_dim
;
204 dim
= isl_set_dim(set
, isl_dim_set
);
211 for (i
= 0; i
< set
->n
; ++i
) {
212 n_eq
+= set
->p
[i
]->n_eq
;
213 n_ineq
+= set
->p
[i
]->n_ineq
;
215 lp
= isl_basic_set_alloc(set
->ctx
, 0, dim
* set
->n
, 0, n_eq
, n_ineq
);
216 lp
= isl_basic_set_set_rational(lp
);
219 lp_dim
= isl_basic_set_dim(lp
, isl_dim_set
);
221 return isl_basic_set_free(lp
);
222 k
= isl_basic_set_alloc_equality(lp
);
223 isl_int_set_si(lp
->eq
[k
][0], -1);
224 for (i
= 0; i
< set
->n
; ++i
) {
225 isl_int_set_si(lp
->eq
[k
][1+dim
*i
], 0);
226 isl_int_set_si(lp
->eq
[k
][1+dim
*i
+1], 1);
227 isl_seq_clr(lp
->eq
[k
]+1+dim
*i
+2, dim
-2);
229 for (i
= 0; i
< set
->n
; ++i
) {
230 k
= isl_basic_set_alloc_inequality(lp
);
231 isl_seq_clr(lp
->ineq
[k
], 1+lp_dim
);
232 isl_int_set_si(lp
->ineq
[k
][1+dim
*i
], 1);
234 for (j
= 0; j
< set
->p
[i
]->n_eq
; ++j
) {
235 k
= isl_basic_set_alloc_equality(lp
);
236 isl_seq_clr(lp
->eq
[k
], 1+dim
*i
);
237 isl_seq_cpy(lp
->eq
[k
]+1+dim
*i
, set
->p
[i
]->eq
[j
], dim
);
238 isl_seq_clr(lp
->eq
[k
]+1+dim
*(i
+1), dim
*(set
->n
-i
-1));
241 for (j
= 0; j
< set
->p
[i
]->n_ineq
; ++j
) {
242 k
= isl_basic_set_alloc_inequality(lp
);
243 isl_seq_clr(lp
->ineq
[k
], 1+dim
*i
);
244 isl_seq_cpy(lp
->ineq
[k
]+1+dim
*i
, set
->p
[i
]->ineq
[j
], dim
);
245 isl_seq_clr(lp
->ineq
[k
]+1+dim
*(i
+1), dim
*(set
->n
-i
-1));
251 /* Given a facet "facet" of the convex hull of "set" and a facet "ridge"
252 * of that facet, compute the other facet of the convex hull that contains
255 * We first transform the set such that the facet constraint becomes
259 * I.e., the facet lies in
263 * and on that facet, the constraint that defines the ridge is
267 * (This transformation is not strictly needed, all that is needed is
268 * that the ridge contains the origin.)
270 * Since the ridge contains the origin, the cone of the convex hull
271 * will be of the form
276 * with this second constraint defining the new facet.
277 * The constant a is obtained by settting x_1 in the cone of the
278 * convex hull to 1 and minimizing x_2.
279 * Now, each element in the cone of the convex hull is the sum
280 * of elements in the cones of the basic sets.
281 * If a_i is the dilation factor of basic set i, then the problem
282 * we need to solve is
295 * the constraints of each (transformed) basic set.
296 * If a = n/d, then the constraint defining the new facet (in the transformed
299 * -n x_1 + d x_2 >= 0
301 * In the original space, we need to take the same combination of the
302 * corresponding constraints "facet" and "ridge".
304 * If a = -infty = "-1/0", then we just return the original facet constraint.
305 * This means that the facet is unbounded, but has a bounded intersection
306 * with the union of sets.
308 isl_int
*isl_set_wrap_facet(__isl_keep isl_set
*set
,
309 isl_int
*facet
, isl_int
*ridge
)
313 struct isl_mat
*T
= NULL
;
314 struct isl_basic_set
*lp
= NULL
;
316 enum isl_lp_result res
;
320 dim
= isl_set_dim(set
, isl_dim_set
);
324 set
= isl_set_copy(set
);
325 set
= isl_set_set_rational(set
);
328 T
= isl_mat_alloc(ctx
, 3, dim
);
331 isl_int_set_si(T
->row
[0][0], 1);
332 isl_seq_clr(T
->row
[0]+1, dim
- 1);
333 isl_seq_cpy(T
->row
[1], facet
, dim
);
334 isl_seq_cpy(T
->row
[2], ridge
, dim
);
335 T
= isl_mat_right_inverse(T
);
336 set
= isl_set_preimage(set
, T
);
340 lp
= wrap_constraints(set
);
341 obj
= isl_vec_alloc(ctx
, 1 + dim
*set
->n
);
344 isl_int_set_si(obj
->block
.data
[0], 0);
345 for (i
= 0; i
< set
->n
; ++i
) {
346 isl_seq_clr(obj
->block
.data
+ 1 + dim
*i
, 2);
347 isl_int_set_si(obj
->block
.data
[1 + dim
*i
+2], 1);
348 isl_seq_clr(obj
->block
.data
+ 1 + dim
*i
+3, dim
-3);
352 res
= isl_basic_set_solve_lp(lp
, 0,
353 obj
->block
.data
, ctx
->one
, &num
, &den
, NULL
);
354 if (res
== isl_lp_ok
) {
355 isl_int_neg(num
, num
);
356 isl_seq_combine(facet
, num
, facet
, den
, ridge
, dim
);
357 isl_seq_normalize(ctx
, facet
, dim
);
362 isl_basic_set_free(lp
);
364 if (res
== isl_lp_error
)
366 isl_assert(ctx
, res
== isl_lp_ok
|| res
== isl_lp_unbounded
,
370 isl_basic_set_free(lp
);
376 /* Compute the constraint of a facet of "set".
378 * We first compute the intersection with a bounding constraint
379 * that is orthogonal to one of the coordinate axes.
380 * If the affine hull of this intersection has only one equality,
381 * we have found a facet.
382 * Otherwise, we wrap the current bounding constraint around
383 * one of the equalities of the face (one that is not equal to
384 * the current bounding constraint).
385 * This process continues until we have found a facet.
386 * The dimension of the intersection increases by at least
387 * one on each iteration, so termination is guaranteed.
389 static __isl_give isl_mat
*initial_facet_constraint(__isl_keep isl_set
*set
)
391 struct isl_set
*slice
= NULL
;
392 struct isl_basic_set
*face
= NULL
;
394 isl_size dim
= isl_set_dim(set
, isl_dim_set
);
396 isl_mat
*bounds
= NULL
;
400 isl_assert(set
->ctx
, set
->n
> 0, goto error
);
401 bounds
= isl_mat_alloc(set
->ctx
, 1, 1 + dim
);
405 isl_seq_clr(bounds
->row
[0], dim
);
406 isl_int_set_si(bounds
->row
[0][1 + dim
- 1], 1);
407 is_bound
= uset_is_bound(set
, bounds
->row
[0], 1 + dim
);
410 isl_assert(set
->ctx
, is_bound
, goto error
);
411 isl_seq_normalize(set
->ctx
, bounds
->row
[0], 1 + dim
);
415 slice
= isl_set_copy(set
);
416 slice
= isl_set_add_basic_set_equality(slice
, bounds
->row
[0]);
417 face
= isl_set_affine_hull(slice
);
420 if (face
->n_eq
== 1) {
421 isl_basic_set_free(face
);
424 for (i
= 0; i
< face
->n_eq
; ++i
)
425 if (!isl_seq_eq(bounds
->row
[0], face
->eq
[i
], 1 + dim
) &&
426 !isl_seq_is_neg(bounds
->row
[0],
427 face
->eq
[i
], 1 + dim
))
429 isl_assert(set
->ctx
, i
< face
->n_eq
, goto error
);
430 if (!isl_set_wrap_facet(set
, bounds
->row
[0], face
->eq
[i
]))
432 isl_seq_normalize(set
->ctx
, bounds
->row
[0], bounds
->n_col
);
433 isl_basic_set_free(face
);
438 isl_basic_set_free(face
);
439 isl_mat_free(bounds
);
443 /* Given the bounding constraint "c" of a facet of the convex hull of "set",
444 * compute a hyperplane description of the facet, i.e., compute the facets
447 * We compute an affine transformation that transforms the constraint
456 * by computing the right inverse U of a matrix that starts with the rows
469 * Since z_1 is zero, we can drop this variable as well as the corresponding
470 * column of U to obtain
478 * with Q' equal to Q, but without the corresponding row.
479 * After computing the facets of the facet in the z' space,
480 * we convert them back to the x space through Q.
482 static __isl_give isl_basic_set
*compute_facet(__isl_keep isl_set
*set
,
485 struct isl_mat
*m
, *U
, *Q
;
486 struct isl_basic_set
*facet
= NULL
;
490 dim
= isl_set_dim(set
, isl_dim_set
);
494 set
= isl_set_copy(set
);
495 m
= isl_mat_alloc(set
->ctx
, 2, 1 + dim
);
498 isl_int_set_si(m
->row
[0][0], 1);
499 isl_seq_clr(m
->row
[0]+1, dim
);
500 isl_seq_cpy(m
->row
[1], c
, 1+dim
);
501 U
= isl_mat_right_inverse(m
);
502 Q
= isl_mat_right_inverse(isl_mat_copy(U
));
503 U
= isl_mat_drop_cols(U
, 1, 1);
504 Q
= isl_mat_drop_rows(Q
, 1, 1);
505 set
= isl_set_preimage(set
, U
);
506 facet
= uset_convex_hull_wrap_bounded(set
);
507 facet
= isl_basic_set_preimage(facet
, Q
);
508 if (facet
&& facet
->n_eq
!= 0)
509 isl_die(ctx
, isl_error_internal
, "unexpected equality",
510 return isl_basic_set_free(facet
));
513 isl_basic_set_free(facet
);
518 /* Given an initial facet constraint, compute the remaining facets.
519 * We do this by running through all facets found so far and computing
520 * the adjacent facets through wrapping, adding those facets that we
521 * hadn't already found before.
523 * For each facet we have found so far, we first compute its facets
524 * in the resulting convex hull. That is, we compute the ridges
525 * of the resulting convex hull contained in the facet.
526 * We also compute the corresponding facet in the current approximation
527 * of the convex hull. There is no need to wrap around the ridges
528 * in this facet since that would result in a facet that is already
529 * present in the current approximation.
531 * This function can still be significantly optimized by checking which of
532 * the facets of the basic sets are also facets of the convex hull and
533 * using all the facets so far to help in constructing the facets of the
536 * using the technique in section "3.1 Ridge Generation" of
537 * "Extended Convex Hull" by Fukuda et al.
539 static __isl_give isl_basic_set
*extend(__isl_take isl_basic_set
*hull
,
540 __isl_keep isl_set
*set
)
544 struct isl_basic_set
*facet
= NULL
;
545 struct isl_basic_set
*hull_facet
= NULL
;
548 dim
= isl_set_dim(set
, isl_dim_set
);
549 if (dim
< 0 || !hull
)
550 return isl_basic_set_free(hull
);
552 isl_assert(set
->ctx
, set
->n
> 0, goto error
);
554 for (i
= 0; i
< hull
->n_ineq
; ++i
) {
555 facet
= compute_facet(set
, hull
->ineq
[i
]);
556 facet
= isl_basic_set_add_eq(facet
, hull
->ineq
[i
]);
557 facet
= isl_basic_set_gauss(facet
, NULL
);
558 facet
= isl_basic_set_normalize_constraints(facet
);
559 hull_facet
= isl_basic_set_copy(hull
);
560 hull_facet
= isl_basic_set_add_eq(hull_facet
, hull
->ineq
[i
]);
561 hull_facet
= isl_basic_set_gauss(hull_facet
, NULL
);
562 hull_facet
= isl_basic_set_normalize_constraints(hull_facet
);
563 if (!facet
|| !hull_facet
)
565 hull
= isl_basic_set_cow(hull
);
566 hull
= isl_basic_set_extend(hull
, 0, 0, facet
->n_ineq
);
569 for (j
= 0; j
< facet
->n_ineq
; ++j
) {
570 for (f
= 0; f
< hull_facet
->n_ineq
; ++f
)
571 if (isl_seq_eq(facet
->ineq
[j
],
572 hull_facet
->ineq
[f
], 1 + dim
))
574 if (f
< hull_facet
->n_ineq
)
576 k
= isl_basic_set_alloc_inequality(hull
);
579 isl_seq_cpy(hull
->ineq
[k
], hull
->ineq
[i
], 1+dim
);
580 if (!isl_set_wrap_facet(set
, hull
->ineq
[k
], facet
->ineq
[j
]))
583 isl_basic_set_free(hull_facet
);
584 isl_basic_set_free(facet
);
586 hull
= isl_basic_set_simplify(hull
);
587 hull
= isl_basic_set_finalize(hull
);
590 isl_basic_set_free(hull_facet
);
591 isl_basic_set_free(facet
);
592 isl_basic_set_free(hull
);
596 /* Special case for computing the convex hull of a one dimensional set.
597 * We simply collect the lower and upper bounds of each basic set
598 * and the biggest of those.
600 static __isl_give isl_basic_set
*convex_hull_1d(__isl_take isl_set
*set
)
602 struct isl_mat
*c
= NULL
;
603 isl_int
*lower
= NULL
;
604 isl_int
*upper
= NULL
;
607 struct isl_basic_set
*hull
;
609 for (i
= 0; i
< set
->n
; ++i
) {
610 set
->p
[i
] = isl_basic_set_simplify(set
->p
[i
]);
614 set
= isl_set_remove_empty_parts(set
);
617 isl_assert(set
->ctx
, set
->n
> 0, goto error
);
618 c
= isl_mat_alloc(set
->ctx
, 2, 2);
622 if (set
->p
[0]->n_eq
> 0) {
623 isl_assert(set
->ctx
, set
->p
[0]->n_eq
== 1, goto error
);
626 if (isl_int_is_pos(set
->p
[0]->eq
[0][1])) {
627 isl_seq_cpy(lower
, set
->p
[0]->eq
[0], 2);
628 isl_seq_neg(upper
, set
->p
[0]->eq
[0], 2);
630 isl_seq_neg(lower
, set
->p
[0]->eq
[0], 2);
631 isl_seq_cpy(upper
, set
->p
[0]->eq
[0], 2);
634 for (j
= 0; j
< set
->p
[0]->n_ineq
; ++j
) {
635 if (isl_int_is_pos(set
->p
[0]->ineq
[j
][1])) {
637 isl_seq_cpy(lower
, set
->p
[0]->ineq
[j
], 2);
640 isl_seq_cpy(upper
, set
->p
[0]->ineq
[j
], 2);
647 for (i
= 0; i
< set
->n
; ++i
) {
648 struct isl_basic_set
*bset
= set
->p
[i
];
652 for (j
= 0; j
< bset
->n_eq
; ++j
) {
656 isl_int_mul(a
, lower
[0], bset
->eq
[j
][1]);
657 isl_int_mul(b
, lower
[1], bset
->eq
[j
][0]);
658 if (isl_int_lt(a
, b
) && isl_int_is_pos(bset
->eq
[j
][1]))
659 isl_seq_cpy(lower
, bset
->eq
[j
], 2);
660 if (isl_int_gt(a
, b
) && isl_int_is_neg(bset
->eq
[j
][1]))
661 isl_seq_neg(lower
, bset
->eq
[j
], 2);
664 isl_int_mul(a
, upper
[0], bset
->eq
[j
][1]);
665 isl_int_mul(b
, upper
[1], bset
->eq
[j
][0]);
666 if (isl_int_lt(a
, b
) && isl_int_is_pos(bset
->eq
[j
][1]))
667 isl_seq_neg(upper
, bset
->eq
[j
], 2);
668 if (isl_int_gt(a
, b
) && isl_int_is_neg(bset
->eq
[j
][1]))
669 isl_seq_cpy(upper
, bset
->eq
[j
], 2);
672 for (j
= 0; j
< bset
->n_ineq
; ++j
) {
673 if (isl_int_is_pos(bset
->ineq
[j
][1]))
675 if (isl_int_is_neg(bset
->ineq
[j
][1]))
677 if (lower
&& isl_int_is_pos(bset
->ineq
[j
][1])) {
678 isl_int_mul(a
, lower
[0], bset
->ineq
[j
][1]);
679 isl_int_mul(b
, lower
[1], bset
->ineq
[j
][0]);
680 if (isl_int_lt(a
, b
))
681 isl_seq_cpy(lower
, bset
->ineq
[j
], 2);
683 if (upper
&& isl_int_is_neg(bset
->ineq
[j
][1])) {
684 isl_int_mul(a
, upper
[0], bset
->ineq
[j
][1]);
685 isl_int_mul(b
, upper
[1], bset
->ineq
[j
][0]);
686 if (isl_int_gt(a
, b
))
687 isl_seq_cpy(upper
, bset
->ineq
[j
], 2);
698 hull
= isl_basic_set_alloc(set
->ctx
, 0, 1, 0, 0, 2);
699 hull
= isl_basic_set_set_rational(hull
);
703 k
= isl_basic_set_alloc_inequality(hull
);
704 isl_seq_cpy(hull
->ineq
[k
], lower
, 2);
707 k
= isl_basic_set_alloc_inequality(hull
);
708 isl_seq_cpy(hull
->ineq
[k
], upper
, 2);
710 hull
= isl_basic_set_finalize(hull
);
720 static __isl_give isl_basic_set
*convex_hull_0d(__isl_take isl_set
*set
)
722 struct isl_basic_set
*convex_hull
;
727 if (isl_set_is_empty(set
))
728 convex_hull
= isl_basic_set_empty(isl_space_copy(set
->dim
));
730 convex_hull
= isl_basic_set_universe(isl_space_copy(set
->dim
));
735 /* Compute the convex hull of a pair of basic sets without any parameters or
736 * integer divisions using Fourier-Motzkin elimination.
737 * The convex hull is the set of all points that can be written as
738 * the sum of points from both basic sets (in homogeneous coordinates).
739 * We set up the constraints in a space with dimensions for each of
740 * the three sets and then project out the dimensions corresponding
741 * to the two original basic sets, retaining only those corresponding
742 * to the convex hull.
744 static __isl_give isl_basic_set
*convex_hull_pair_elim(
745 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
748 struct isl_basic_set
*bset
[2];
749 struct isl_basic_set
*hull
= NULL
;
752 dim
= isl_basic_set_dim(bset1
, isl_dim_set
);
753 if (dim
< 0 || !bset2
)
756 hull
= isl_basic_set_alloc(bset1
->ctx
, 0, 2 + 3 * dim
, 0,
757 1 + dim
+ bset1
->n_eq
+ bset2
->n_eq
,
758 2 + bset1
->n_ineq
+ bset2
->n_ineq
);
761 for (i
= 0; i
< 2; ++i
) {
762 for (j
= 0; j
< bset
[i
]->n_eq
; ++j
) {
763 k
= isl_basic_set_alloc_equality(hull
);
766 isl_seq_clr(hull
->eq
[k
], (i
+1) * (1+dim
));
767 isl_seq_clr(hull
->eq
[k
]+(i
+2)*(1+dim
), (1-i
)*(1+dim
));
768 isl_seq_cpy(hull
->eq
[k
]+(i
+1)*(1+dim
), bset
[i
]->eq
[j
],
771 for (j
= 0; j
< bset
[i
]->n_ineq
; ++j
) {
772 k
= isl_basic_set_alloc_inequality(hull
);
775 isl_seq_clr(hull
->ineq
[k
], (i
+1) * (1+dim
));
776 isl_seq_clr(hull
->ineq
[k
]+(i
+2)*(1+dim
), (1-i
)*(1+dim
));
777 isl_seq_cpy(hull
->ineq
[k
]+(i
+1)*(1+dim
),
778 bset
[i
]->ineq
[j
], 1+dim
);
780 k
= isl_basic_set_alloc_inequality(hull
);
783 isl_seq_clr(hull
->ineq
[k
], 1+2+3*dim
);
784 isl_int_set_si(hull
->ineq
[k
][(i
+1)*(1+dim
)], 1);
786 for (j
= 0; j
< 1+dim
; ++j
) {
787 k
= isl_basic_set_alloc_equality(hull
);
790 isl_seq_clr(hull
->eq
[k
], 1+2+3*dim
);
791 isl_int_set_si(hull
->eq
[k
][j
], -1);
792 isl_int_set_si(hull
->eq
[k
][1+dim
+j
], 1);
793 isl_int_set_si(hull
->eq
[k
][2*(1+dim
)+j
], 1);
795 hull
= isl_basic_set_set_rational(hull
);
796 hull
= isl_basic_set_remove_dims(hull
, isl_dim_set
, dim
, 2*(1+dim
));
797 hull
= isl_basic_set_remove_redundancies(hull
);
798 isl_basic_set_free(bset1
);
799 isl_basic_set_free(bset2
);
802 isl_basic_set_free(bset1
);
803 isl_basic_set_free(bset2
);
804 isl_basic_set_free(hull
);
808 /* Is the set bounded for each value of the parameters?
810 isl_bool
isl_basic_set_is_bounded(__isl_keep isl_basic_set
*bset
)
816 return isl_bool_error
;
817 if (isl_basic_set_plain_is_empty(bset
))
818 return isl_bool_true
;
820 tab
= isl_tab_from_recession_cone(bset
, 1);
821 bounded
= isl_tab_cone_is_bounded(tab
);
826 /* Is the image bounded for each value of the parameters and
827 * the domain variables?
829 isl_bool
isl_basic_map_image_is_bounded(__isl_keep isl_basic_map
*bmap
)
831 isl_size nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
832 isl_size n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
835 if (nparam
< 0 || n_in
< 0)
836 return isl_bool_error
;
838 bmap
= isl_basic_map_copy(bmap
);
839 bmap
= isl_basic_map_cow(bmap
);
840 bmap
= isl_basic_map_move_dims(bmap
, isl_dim_param
, nparam
,
841 isl_dim_in
, 0, n_in
);
842 bounded
= isl_basic_set_is_bounded(bset_from_bmap(bmap
));
843 isl_basic_map_free(bmap
);
848 /* Is the set bounded for each value of the parameters?
850 isl_bool
isl_set_is_bounded(__isl_keep isl_set
*set
)
855 return isl_bool_error
;
857 for (i
= 0; i
< set
->n
; ++i
) {
858 isl_bool bounded
= isl_basic_set_is_bounded(set
->p
[i
]);
859 if (!bounded
|| bounded
< 0)
862 return isl_bool_true
;
865 /* Compute the lineality space of the convex hull of bset1 and bset2.
867 * We first compute the intersection of the recession cone of bset1
868 * with the negative of the recession cone of bset2 and then compute
869 * the linear hull of the resulting cone.
871 static __isl_give isl_basic_set
*induced_lineality_space(
872 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
875 struct isl_basic_set
*lin
= NULL
;
878 dim
= isl_basic_set_dim(bset1
, isl_dim_all
);
879 if (dim
< 0 || !bset2
)
882 lin
= isl_basic_set_alloc_space(isl_basic_set_get_space(bset1
), 0,
883 bset1
->n_eq
+ bset2
->n_eq
,
884 bset1
->n_ineq
+ bset2
->n_ineq
);
885 lin
= isl_basic_set_set_rational(lin
);
888 for (i
= 0; i
< bset1
->n_eq
; ++i
) {
889 k
= isl_basic_set_alloc_equality(lin
);
892 isl_int_set_si(lin
->eq
[k
][0], 0);
893 isl_seq_cpy(lin
->eq
[k
] + 1, bset1
->eq
[i
] + 1, dim
);
895 for (i
= 0; i
< bset1
->n_ineq
; ++i
) {
896 k
= isl_basic_set_alloc_inequality(lin
);
899 isl_int_set_si(lin
->ineq
[k
][0], 0);
900 isl_seq_cpy(lin
->ineq
[k
] + 1, bset1
->ineq
[i
] + 1, dim
);
902 for (i
= 0; i
< bset2
->n_eq
; ++i
) {
903 k
= isl_basic_set_alloc_equality(lin
);
906 isl_int_set_si(lin
->eq
[k
][0], 0);
907 isl_seq_neg(lin
->eq
[k
] + 1, bset2
->eq
[i
] + 1, dim
);
909 for (i
= 0; i
< bset2
->n_ineq
; ++i
) {
910 k
= isl_basic_set_alloc_inequality(lin
);
913 isl_int_set_si(lin
->ineq
[k
][0], 0);
914 isl_seq_neg(lin
->ineq
[k
] + 1, bset2
->ineq
[i
] + 1, dim
);
917 isl_basic_set_free(bset1
);
918 isl_basic_set_free(bset2
);
919 return isl_basic_set_affine_hull(lin
);
921 isl_basic_set_free(lin
);
922 isl_basic_set_free(bset1
);
923 isl_basic_set_free(bset2
);
927 static __isl_give isl_basic_set
*uset_convex_hull(__isl_take isl_set
*set
);
929 /* Given a set and a linear space "lin" of dimension n > 0,
930 * project the linear space from the set, compute the convex hull
931 * and then map the set back to the original space.
937 * describe the linear space. We first compute the Hermite normal
938 * form H = M U of M = H Q, to obtain
942 * The last n rows of H will be zero, so the last n variables of x' = Q x
943 * are the one we want to project out. We do this by transforming each
944 * basic set A x >= b to A U x' >= b and then removing the last n dimensions.
945 * After computing the convex hull in x'_1, i.e., A' x'_1 >= b',
946 * we transform the hull back to the original space as A' Q_1 x >= b',
947 * with Q_1 all but the last n rows of Q.
949 static __isl_give isl_basic_set
*modulo_lineality(__isl_take isl_set
*set
,
950 __isl_take isl_basic_set
*lin
)
952 isl_size total
= isl_basic_set_dim(lin
, isl_dim_all
);
954 struct isl_basic_set
*hull
;
955 struct isl_mat
*M
, *U
, *Q
;
957 if (!set
|| total
< 0)
959 lin_dim
= total
- lin
->n_eq
;
960 M
= isl_mat_sub_alloc6(set
->ctx
, lin
->eq
, 0, lin
->n_eq
, 1, total
);
961 M
= isl_mat_left_hermite(M
, 0, &U
, &Q
);
965 isl_basic_set_free(lin
);
967 Q
= isl_mat_drop_rows(Q
, Q
->n_row
- lin_dim
, lin_dim
);
969 U
= isl_mat_lin_to_aff(U
);
970 Q
= isl_mat_lin_to_aff(Q
);
972 set
= isl_set_preimage(set
, U
);
973 set
= isl_set_remove_dims(set
, isl_dim_set
, total
- lin_dim
, lin_dim
);
974 hull
= uset_convex_hull(set
);
975 hull
= isl_basic_set_preimage(hull
, Q
);
979 isl_basic_set_free(lin
);
984 /* Given two polyhedra with as constraints h_{ij} x >= 0 in homegeneous space,
985 * set up an LP for solving
987 * \sum_j \alpha_{1j} h_{1j} = \sum_j \alpha_{2j} h_{2j}
989 * \alpha{i0} corresponds to the (implicit) positivity constraint 1 >= 0
990 * The next \alpha{ij} correspond to the equalities and come in pairs.
991 * The final \alpha{ij} correspond to the inequalities.
993 static __isl_give isl_basic_set
*valid_direction_lp(
994 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
997 struct isl_basic_set
*lp
;
1003 total
= isl_basic_set_dim(bset1
, isl_dim_all
);
1004 if (total
< 0 || !bset2
)
1008 2 * bset1
->n_eq
+ bset1
->n_ineq
+ 2 * bset2
->n_eq
+ bset2
->n_ineq
;
1009 space
= isl_space_set_alloc(bset1
->ctx
, 0, n
);
1010 lp
= isl_basic_set_alloc_space(space
, 0, d
, n
);
1013 for (i
= 0; i
< n
; ++i
) {
1014 k
= isl_basic_set_alloc_inequality(lp
);
1017 isl_seq_clr(lp
->ineq
[k
] + 1, n
);
1018 isl_int_set_si(lp
->ineq
[k
][0], -1);
1019 isl_int_set_si(lp
->ineq
[k
][1 + i
], 1);
1021 for (i
= 0; i
< d
; ++i
) {
1022 k
= isl_basic_set_alloc_equality(lp
);
1026 isl_int_set_si(lp
->eq
[k
][n
], 0); n
++;
1027 /* positivity constraint 1 >= 0 */
1028 isl_int_set_si(lp
->eq
[k
][n
], i
== 0); n
++;
1029 for (j
= 0; j
< bset1
->n_eq
; ++j
) {
1030 isl_int_set(lp
->eq
[k
][n
], bset1
->eq
[j
][i
]); n
++;
1031 isl_int_neg(lp
->eq
[k
][n
], bset1
->eq
[j
][i
]); n
++;
1033 for (j
= 0; j
< bset1
->n_ineq
; ++j
) {
1034 isl_int_set(lp
->eq
[k
][n
], bset1
->ineq
[j
][i
]); n
++;
1036 /* positivity constraint 1 >= 0 */
1037 isl_int_set_si(lp
->eq
[k
][n
], -(i
== 0)); n
++;
1038 for (j
= 0; j
< bset2
->n_eq
; ++j
) {
1039 isl_int_neg(lp
->eq
[k
][n
], bset2
->eq
[j
][i
]); n
++;
1040 isl_int_set(lp
->eq
[k
][n
], bset2
->eq
[j
][i
]); n
++;
1042 for (j
= 0; j
< bset2
->n_ineq
; ++j
) {
1043 isl_int_neg(lp
->eq
[k
][n
], bset2
->ineq
[j
][i
]); n
++;
1046 lp
= isl_basic_set_gauss(lp
, NULL
);
1047 isl_basic_set_free(bset1
);
1048 isl_basic_set_free(bset2
);
1051 isl_basic_set_free(bset1
);
1052 isl_basic_set_free(bset2
);
1056 /* Compute a vector s in the homogeneous space such that <s, r> > 0
1057 * for all rays in the homogeneous space of the two cones that correspond
1058 * to the input polyhedra bset1 and bset2.
1060 * We compute s as a vector that satisfies
1062 * s = \sum_j \alpha_{ij} h_{ij} for i = 1,2 (*)
1064 * with h_{ij} the normals of the facets of polyhedron i
1065 * (including the "positivity constraint" 1 >= 0) and \alpha_{ij}
1066 * strictly positive numbers. For simplicity we impose \alpha_{ij} >= 1.
1067 * We first set up an LP with as variables the \alpha{ij}.
1068 * In this formulation, for each polyhedron i,
1069 * the first constraint is the positivity constraint, followed by pairs
1070 * of variables for the equalities, followed by variables for the inequalities.
1071 * We then simply pick a feasible solution and compute s using (*).
1073 * Note that we simply pick any valid direction and make no attempt
1074 * to pick a "good" or even the "best" valid direction.
1076 static __isl_give isl_vec
*valid_direction(
1077 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
1079 struct isl_basic_set
*lp
;
1080 struct isl_tab
*tab
;
1081 struct isl_vec
*sample
= NULL
;
1082 struct isl_vec
*dir
;
1087 if (!bset1
|| !bset2
)
1089 lp
= valid_direction_lp(isl_basic_set_copy(bset1
),
1090 isl_basic_set_copy(bset2
));
1091 tab
= isl_tab_from_basic_set(lp
, 0);
1092 sample
= isl_tab_get_sample_value(tab
);
1094 isl_basic_set_free(lp
);
1097 d
= isl_basic_set_dim(bset1
, isl_dim_all
);
1100 dir
= isl_vec_alloc(bset1
->ctx
, 1 + d
);
1103 isl_seq_clr(dir
->block
.data
+ 1, dir
->size
- 1);
1105 /* positivity constraint 1 >= 0 */
1106 isl_int_set(dir
->block
.data
[0], sample
->block
.data
[n
]); n
++;
1107 for (i
= 0; i
< bset1
->n_eq
; ++i
) {
1108 isl_int_sub(sample
->block
.data
[n
],
1109 sample
->block
.data
[n
], sample
->block
.data
[n
+1]);
1110 isl_seq_combine(dir
->block
.data
,
1111 bset1
->ctx
->one
, dir
->block
.data
,
1112 sample
->block
.data
[n
], bset1
->eq
[i
], 1 + d
);
1116 for (i
= 0; i
< bset1
->n_ineq
; ++i
)
1117 isl_seq_combine(dir
->block
.data
,
1118 bset1
->ctx
->one
, dir
->block
.data
,
1119 sample
->block
.data
[n
++], bset1
->ineq
[i
], 1 + d
);
1120 isl_vec_free(sample
);
1121 isl_seq_normalize(bset1
->ctx
, dir
->el
, dir
->size
);
1122 isl_basic_set_free(bset1
);
1123 isl_basic_set_free(bset2
);
1126 isl_vec_free(sample
);
1127 isl_basic_set_free(bset1
);
1128 isl_basic_set_free(bset2
);
1132 /* Given a polyhedron b_i + A_i x >= 0 and a map T = S^{-1},
1133 * compute b_i' + A_i' x' >= 0, with
1135 * [ b_i A_i ] [ y' ] [ y' ]
1136 * [ 1 0 ] S^{-1} [ x' ] >= 0 or [ b_i' A_i' ] [ x' ] >= 0
1138 * In particular, add the "positivity constraint" and then perform
1141 static __isl_give isl_basic_set
*homogeneous_map(__isl_take isl_basic_set
*bset
,
1142 __isl_take isl_mat
*T
)
1147 total
= isl_basic_set_dim(bset
, isl_dim_all
);
1150 bset
= isl_basic_set_extend_constraints(bset
, 0, 1);
1151 k
= isl_basic_set_alloc_inequality(bset
);
1154 isl_seq_clr(bset
->ineq
[k
] + 1, total
);
1155 isl_int_set_si(bset
->ineq
[k
][0], 1);
1156 bset
= isl_basic_set_preimage(bset
, T
);
1160 isl_basic_set_free(bset
);
1164 /* Compute the convex hull of a pair of basic sets without any parameters or
1165 * integer divisions, where the convex hull is known to be pointed,
1166 * but the basic sets may be unbounded.
1168 * We turn this problem into the computation of a convex hull of a pair
1169 * _bounded_ polyhedra by "changing the direction of the homogeneous
1170 * dimension". This idea is due to Matthias Koeppe.
1172 * Consider the cones in homogeneous space that correspond to the
1173 * input polyhedra. The rays of these cones are also rays of the
1174 * polyhedra if the coordinate that corresponds to the homogeneous
1175 * dimension is zero. That is, if the inner product of the rays
1176 * with the homogeneous direction is zero.
1177 * The cones in the homogeneous space can also be considered to
1178 * correspond to other pairs of polyhedra by chosing a different
1179 * homogeneous direction. To ensure that both of these polyhedra
1180 * are bounded, we need to make sure that all rays of the cones
1181 * correspond to vertices and not to rays.
1182 * Let s be a direction such that <s, r> > 0 for all rays r of both cones.
1183 * Then using s as a homogeneous direction, we obtain a pair of polytopes.
1184 * The vector s is computed in valid_direction.
1186 * Note that we need to consider _all_ rays of the cones and not just
1187 * the rays that correspond to rays in the polyhedra. If we were to
1188 * only consider those rays and turn them into vertices, then we
1189 * may inadvertently turn some vertices into rays.
1191 * The standard homogeneous direction is the unit vector in the 0th coordinate.
1192 * We therefore transform the two polyhedra such that the selected
1193 * direction is mapped onto this standard direction and then proceed
1194 * with the normal computation.
1195 * Let S be a non-singular square matrix with s as its first row,
1196 * then we want to map the polyhedra to the space
1198 * [ y' ] [ y ] [ y ] [ y' ]
1199 * [ x' ] = S [ x ] i.e., [ x ] = S^{-1} [ x' ]
1201 * We take S to be the unimodular completion of s to limit the growth
1202 * of the coefficients in the following computations.
1204 * Let b_i + A_i x >= 0 be the constraints of polyhedron i.
1205 * We first move to the homogeneous dimension
1207 * b_i y + A_i x >= 0 [ b_i A_i ] [ y ] [ 0 ]
1208 * y >= 0 or [ 1 0 ] [ x ] >= [ 0 ]
1210 * Then we change directoin
1212 * [ b_i A_i ] [ y' ] [ y' ]
1213 * [ 1 0 ] S^{-1} [ x' ] >= 0 or [ b_i' A_i' ] [ x' ] >= 0
1215 * Then we compute the convex hull of the polytopes b_i' + A_i' x' >= 0
1216 * resulting in b' + A' x' >= 0, which we then convert back
1219 * [ b' A' ] S [ x ] >= 0 or [ b A ] [ x ] >= 0
1221 * The polyhedron b + A x >= 0 is then the convex hull of the input polyhedra.
1223 static __isl_give isl_basic_set
*convex_hull_pair_pointed(
1224 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
1226 struct isl_ctx
*ctx
= NULL
;
1227 struct isl_vec
*dir
= NULL
;
1228 struct isl_mat
*T
= NULL
;
1229 struct isl_mat
*T2
= NULL
;
1230 struct isl_basic_set
*hull
;
1231 struct isl_set
*set
;
1233 if (!bset1
|| !bset2
)
1235 ctx
= isl_basic_set_get_ctx(bset1
);
1236 dir
= valid_direction(isl_basic_set_copy(bset1
),
1237 isl_basic_set_copy(bset2
));
1240 T
= isl_mat_alloc(ctx
, dir
->size
, dir
->size
);
1243 isl_seq_cpy(T
->row
[0], dir
->block
.data
, dir
->size
);
1244 T
= isl_mat_unimodular_complete(T
, 1);
1245 T2
= isl_mat_right_inverse(isl_mat_copy(T
));
1247 bset1
= homogeneous_map(bset1
, isl_mat_copy(T2
));
1248 bset2
= homogeneous_map(bset2
, T2
);
1249 set
= isl_set_alloc_space(isl_basic_set_get_space(bset1
), 2, 0);
1250 set
= isl_set_add_basic_set(set
, bset1
);
1251 set
= isl_set_add_basic_set(set
, bset2
);
1252 hull
= uset_convex_hull(set
);
1253 hull
= isl_basic_set_preimage(hull
, T
);
1260 isl_basic_set_free(bset1
);
1261 isl_basic_set_free(bset2
);
1265 static __isl_give isl_basic_set
*uset_convex_hull_wrap(__isl_take isl_set
*set
);
1266 static __isl_give isl_basic_set
*modulo_affine_hull(
1267 __isl_take isl_set
*set
, __isl_take isl_basic_set
*affine_hull
);
1269 /* Compute the convex hull of a pair of basic sets without any parameters or
1270 * integer divisions.
1272 * This function is called from uset_convex_hull_unbounded, which
1273 * means that the complete convex hull is unbounded. Some pairs
1274 * of basic sets may still be bounded, though.
1275 * They may even lie inside a lower dimensional space, in which
1276 * case they need to be handled inside their affine hull since
1277 * the main algorithm assumes that the result is full-dimensional.
1279 * If the convex hull of the two basic sets would have a non-trivial
1280 * lineality space, we first project out this lineality space.
1282 static __isl_give isl_basic_set
*convex_hull_pair(
1283 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
1285 isl_basic_set
*lin
, *aff
;
1286 isl_bool bounded1
, bounded2
;
1289 if (bset1
->ctx
->opt
->convex
== ISL_CONVEX_HULL_FM
)
1290 return convex_hull_pair_elim(bset1
, bset2
);
1292 aff
= isl_set_affine_hull(isl_basic_set_union(isl_basic_set_copy(bset1
),
1293 isl_basic_set_copy(bset2
)));
1297 return modulo_affine_hull(isl_basic_set_union(bset1
, bset2
), aff
);
1298 isl_basic_set_free(aff
);
1300 bounded1
= isl_basic_set_is_bounded(bset1
);
1301 bounded2
= isl_basic_set_is_bounded(bset2
);
1303 if (bounded1
< 0 || bounded2
< 0)
1306 if (bounded1
&& bounded2
)
1307 return uset_convex_hull_wrap(isl_basic_set_union(bset1
, bset2
));
1309 if (bounded1
|| bounded2
)
1310 return convex_hull_pair_pointed(bset1
, bset2
);
1312 lin
= induced_lineality_space(isl_basic_set_copy(bset1
),
1313 isl_basic_set_copy(bset2
));
1316 if (isl_basic_set_plain_is_universe(lin
)) {
1317 isl_basic_set_free(bset1
);
1318 isl_basic_set_free(bset2
);
1321 total
= isl_basic_set_dim(lin
, isl_dim_all
);
1322 if (lin
->n_eq
< total
) {
1323 struct isl_set
*set
;
1324 set
= isl_set_alloc_space(isl_basic_set_get_space(bset1
), 2, 0);
1325 set
= isl_set_add_basic_set(set
, bset1
);
1326 set
= isl_set_add_basic_set(set
, bset2
);
1327 return modulo_lineality(set
, lin
);
1329 isl_basic_set_free(lin
);
1333 return convex_hull_pair_pointed(bset1
, bset2
);
1335 isl_basic_set_free(bset1
);
1336 isl_basic_set_free(bset2
);
1340 /* Compute the lineality space of a basic set.
1341 * We basically just drop the constants and turn every inequality
1343 * Any explicit representations of local variables are removed
1344 * because they may no longer be valid representations
1345 * in the lineality space.
1347 __isl_give isl_basic_set
*isl_basic_set_lineality_space(
1348 __isl_take isl_basic_set
*bset
)
1351 struct isl_basic_set
*lin
= NULL
;
1352 isl_size n_div
, dim
;
1354 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
1355 dim
= isl_basic_set_dim(bset
, isl_dim_all
);
1356 if (n_div
< 0 || dim
< 0)
1357 return isl_basic_set_free(bset
);
1359 lin
= isl_basic_set_alloc_space(isl_basic_set_get_space(bset
),
1361 for (i
= 0; i
< n_div
; ++i
)
1362 if (isl_basic_set_alloc_div(lin
) < 0)
1366 for (i
= 0; i
< bset
->n_eq
; ++i
) {
1367 k
= isl_basic_set_alloc_equality(lin
);
1370 isl_int_set_si(lin
->eq
[k
][0], 0);
1371 isl_seq_cpy(lin
->eq
[k
] + 1, bset
->eq
[i
] + 1, dim
);
1373 lin
= isl_basic_set_gauss(lin
, NULL
);
1376 for (i
= 0; i
< bset
->n_ineq
&& lin
->n_eq
< dim
; ++i
) {
1377 k
= isl_basic_set_alloc_equality(lin
);
1380 isl_int_set_si(lin
->eq
[k
][0], 0);
1381 isl_seq_cpy(lin
->eq
[k
] + 1, bset
->ineq
[i
] + 1, dim
);
1382 lin
= isl_basic_set_gauss(lin
, NULL
);
1386 isl_basic_set_free(bset
);
1389 isl_basic_set_free(lin
);
1390 isl_basic_set_free(bset
);
1394 /* Compute the (linear) hull of the lineality spaces of the basic sets in the
1397 __isl_give isl_basic_set
*isl_set_combined_lineality_space(
1398 __isl_take isl_set
*set
)
1401 struct isl_set
*lin
= NULL
;
1406 isl_space
*space
= isl_set_get_space(set
);
1408 return isl_basic_set_empty(space
);
1411 lin
= isl_set_alloc_space(isl_set_get_space(set
), set
->n
, 0);
1412 for (i
= 0; i
< set
->n
; ++i
)
1413 lin
= isl_set_add_basic_set(lin
,
1414 isl_basic_set_lineality_space(isl_basic_set_copy(set
->p
[i
])));
1416 return isl_set_affine_hull(lin
);
1419 /* Compute the convex hull of a set without any parameters or
1420 * integer divisions.
1421 * In each step, we combined two basic sets until only one
1422 * basic set is left.
1423 * The input basic sets are assumed not to have a non-trivial
1424 * lineality space. If any of the intermediate results has
1425 * a non-trivial lineality space, it is projected out.
1427 static __isl_give isl_basic_set
*uset_convex_hull_unbounded(
1428 __isl_take isl_set
*set
)
1430 isl_basic_set_list
*list
;
1432 list
= isl_set_get_basic_set_list(set
);
1437 struct isl_basic_set
*t
;
1438 isl_basic_set
*bset1
, *bset2
;
1440 n
= isl_basic_set_list_n_basic_set(list
);
1444 isl_die(isl_basic_set_list_get_ctx(list
),
1446 "expecting at least two elements", goto error
);
1447 bset1
= isl_basic_set_list_get_basic_set(list
, n
- 1);
1448 bset2
= isl_basic_set_list_get_basic_set(list
, n
- 2);
1449 bset1
= convex_hull_pair(bset1
, bset2
);
1451 isl_basic_set_list_free(list
);
1454 bset1
= isl_basic_set_underlying_set(bset1
);
1455 list
= isl_basic_set_list_drop(list
, n
- 2, 2);
1456 list
= isl_basic_set_list_add(list
, bset1
);
1458 t
= isl_basic_set_list_get_basic_set(list
, n
- 2);
1459 t
= isl_basic_set_lineality_space(t
);
1462 if (isl_basic_set_plain_is_universe(t
)) {
1463 isl_basic_set_list_free(list
);
1466 total
= isl_basic_set_dim(t
, isl_dim_all
);
1467 if (t
->n_eq
< total
) {
1468 set
= isl_basic_set_list_union(list
);
1469 return modulo_lineality(set
, t
);
1471 isl_basic_set_free(t
);
1478 isl_basic_set_list_free(list
);
1482 /* Compute an initial hull for wrapping containing a single initial
1484 * This function assumes that the given set is bounded.
1486 static __isl_give isl_basic_set
*initial_hull(__isl_take isl_basic_set
*hull
,
1487 __isl_keep isl_set
*set
)
1489 struct isl_mat
*bounds
= NULL
;
1495 bounds
= initial_facet_constraint(set
);
1498 k
= isl_basic_set_alloc_inequality(hull
);
1501 dim
= isl_set_dim(set
, isl_dim_set
);
1504 isl_assert(set
->ctx
, 1 + dim
== bounds
->n_col
, goto error
);
1505 isl_seq_cpy(hull
->ineq
[k
], bounds
->row
[0], bounds
->n_col
);
1506 isl_mat_free(bounds
);
1510 isl_basic_set_free(hull
);
1511 isl_mat_free(bounds
);
1515 struct max_constraint
{
1521 static isl_bool
max_constraint_equal(const void *entry
, const void *val
)
1523 struct max_constraint
*a
= (struct max_constraint
*)entry
;
1524 isl_int
*b
= (isl_int
*)val
;
1526 return isl_bool_ok(isl_seq_eq(a
->c
->row
[0] + 1, b
, a
->c
->n_col
- 1));
1529 static isl_stat
update_constraint(struct isl_ctx
*ctx
,
1530 struct isl_hash_table
*table
,
1531 isl_int
*con
, unsigned len
, int n
, int ineq
)
1533 struct isl_hash_table_entry
*entry
;
1534 struct max_constraint
*c
;
1537 c_hash
= isl_seq_get_hash(con
+ 1, len
);
1538 entry
= isl_hash_table_find(ctx
, table
, c_hash
, max_constraint_equal
,
1541 return isl_stat_error
;
1542 if (entry
== isl_hash_table_entry_none
)
1546 isl_hash_table_remove(ctx
, table
, entry
);
1550 if (isl_int_gt(c
->c
->row
[0][0], con
[0]))
1552 if (isl_int_eq(c
->c
->row
[0][0], con
[0])) {
1557 c
->c
= isl_mat_cow(c
->c
);
1558 isl_int_set(c
->c
->row
[0][0], con
[0]);
1564 /* Check whether the constraint hash table "table" contains the constraint
1567 static isl_bool
has_constraint(struct isl_ctx
*ctx
,
1568 struct isl_hash_table
*table
, isl_int
*con
, unsigned len
, int n
)
1570 struct isl_hash_table_entry
*entry
;
1571 struct max_constraint
*c
;
1574 c_hash
= isl_seq_get_hash(con
+ 1, len
);
1575 entry
= isl_hash_table_find(ctx
, table
, c_hash
, max_constraint_equal
,
1578 return isl_bool_error
;
1579 if (entry
== isl_hash_table_entry_none
)
1580 return isl_bool_false
;
1583 return isl_bool_false
;
1584 return isl_bool_ok(isl_int_eq(c
->c
->row
[0][0], con
[0]));
1587 /* Are the constraints of "bset" known to be facets?
1588 * If there are any equality constraints, then they are not.
1589 * If there may be redundant constraints, then those
1590 * redundant constraints are not facets.
1592 static isl_bool
has_facets(__isl_keep isl_basic_set
*bset
)
1596 n_eq
= isl_basic_set_n_equality(bset
);
1598 return isl_bool_error
;
1600 return isl_bool_false
;
1601 return ISL_F_ISSET(bset
, ISL_BASIC_SET_NO_REDUNDANT
);
1604 /* Check for inequality constraints of a basic set without equalities
1605 * or redundant constraints
1606 * such that the same or more stringent copies of the constraint appear
1607 * in all of the basic sets. Such constraints are necessarily facet
1608 * constraints of the convex hull.
1610 * If the resulting basic set is by chance identical to one of
1611 * the basic sets in "set", then we know that this basic set contains
1612 * all other basic sets and is therefore the convex hull of set.
1613 * In this case we set *is_hull to 1.
1615 static __isl_give isl_basic_set
*common_constraints(
1616 __isl_take isl_basic_set
*hull
, __isl_keep isl_set
*set
, int *is_hull
)
1619 int min_constraints
;
1621 struct max_constraint
*constraints
= NULL
;
1622 struct isl_hash_table
*table
= NULL
;
1627 for (i
= 0; i
< set
->n
; ++i
) {
1628 isl_bool facets
= has_facets(set
->p
[i
]);
1630 return isl_basic_set_free(hull
);
1636 min_constraints
= set
->p
[i
]->n_ineq
;
1638 for (i
= best
+ 1; i
< set
->n
; ++i
) {
1639 isl_bool facets
= has_facets(set
->p
[i
]);
1641 return isl_basic_set_free(hull
);
1644 if (set
->p
[i
]->n_ineq
>= min_constraints
)
1646 min_constraints
= set
->p
[i
]->n_ineq
;
1649 constraints
= isl_calloc_array(hull
->ctx
, struct max_constraint
,
1653 table
= isl_alloc_type(hull
->ctx
, struct isl_hash_table
);
1654 if (isl_hash_table_init(hull
->ctx
, table
, min_constraints
))
1657 total
= isl_set_dim(set
, isl_dim_all
);
1660 for (i
= 0; i
< set
->p
[best
]->n_ineq
; ++i
) {
1661 constraints
[i
].c
= isl_mat_sub_alloc6(hull
->ctx
,
1662 set
->p
[best
]->ineq
+ i
, 0, 1, 0, 1 + total
);
1663 if (!constraints
[i
].c
)
1665 constraints
[i
].ineq
= 1;
1667 for (i
= 0; i
< min_constraints
; ++i
) {
1668 struct isl_hash_table_entry
*entry
;
1670 c_hash
= isl_seq_get_hash(constraints
[i
].c
->row
[0] + 1, total
);
1671 entry
= isl_hash_table_find(hull
->ctx
, table
, c_hash
,
1672 max_constraint_equal
, constraints
[i
].c
->row
[0] + 1, 1);
1675 isl_assert(hull
->ctx
, !entry
->data
, goto error
);
1676 entry
->data
= &constraints
[i
];
1680 for (s
= 0; s
< set
->n
; ++s
) {
1684 for (i
= 0; i
< set
->p
[s
]->n_eq
; ++i
) {
1685 isl_int
*eq
= set
->p
[s
]->eq
[i
];
1686 for (j
= 0; j
< 2; ++j
) {
1687 isl_seq_neg(eq
, eq
, 1 + total
);
1688 if (update_constraint(hull
->ctx
, table
,
1689 eq
, total
, n
, 0) < 0)
1693 for (i
= 0; i
< set
->p
[s
]->n_ineq
; ++i
) {
1694 isl_int
*ineq
= set
->p
[s
]->ineq
[i
];
1695 if (update_constraint(hull
->ctx
, table
, ineq
, total
, n
,
1696 set
->p
[s
]->n_eq
== 0) < 0)
1702 for (i
= 0; i
< min_constraints
; ++i
) {
1703 if (constraints
[i
].count
< n
)
1705 if (!constraints
[i
].ineq
)
1707 j
= isl_basic_set_alloc_inequality(hull
);
1710 isl_seq_cpy(hull
->ineq
[j
], constraints
[i
].c
->row
[0], 1 + total
);
1713 for (s
= 0; s
< set
->n
; ++s
) {
1714 if (set
->p
[s
]->n_eq
)
1716 if (set
->p
[s
]->n_ineq
!= hull
->n_ineq
)
1718 for (i
= 0; i
< set
->p
[s
]->n_ineq
; ++i
) {
1720 isl_int
*ineq
= set
->p
[s
]->ineq
[i
];
1721 has
= has_constraint(hull
->ctx
, table
, ineq
, total
, n
);
1727 if (i
== set
->p
[s
]->n_ineq
)
1731 isl_hash_table_clear(table
);
1732 for (i
= 0; i
< min_constraints
; ++i
)
1733 isl_mat_free(constraints
[i
].c
);
1738 isl_hash_table_clear(table
);
1741 for (i
= 0; i
< min_constraints
; ++i
)
1742 isl_mat_free(constraints
[i
].c
);
1747 /* Create a template for the convex hull of "set" and fill it up
1748 * obvious facet constraints, if any. If the result happens to
1749 * be the convex hull of "set" then *is_hull is set to 1.
1751 static __isl_give isl_basic_set
*proto_hull(__isl_keep isl_set
*set
,
1754 struct isl_basic_set
*hull
;
1759 for (i
= 0; i
< set
->n
; ++i
) {
1760 n_ineq
+= set
->p
[i
]->n_eq
;
1761 n_ineq
+= set
->p
[i
]->n_ineq
;
1763 hull
= isl_basic_set_alloc_space(isl_space_copy(set
->dim
), 0, 0, n_ineq
);
1764 hull
= isl_basic_set_set_rational(hull
);
1767 return common_constraints(hull
, set
, is_hull
);
1770 static __isl_give isl_basic_set
*uset_convex_hull_wrap(__isl_take isl_set
*set
)
1772 struct isl_basic_set
*hull
;
1775 hull
= proto_hull(set
, &is_hull
);
1776 if (hull
&& !is_hull
) {
1777 if (hull
->n_ineq
== 0)
1778 hull
= initial_hull(hull
, set
);
1779 hull
= extend(hull
, set
);
1786 /* Compute the convex hull of a set without any parameters or
1787 * integer divisions. Depending on whether the set is bounded,
1788 * we pass control to the wrapping based convex hull or
1789 * the Fourier-Motzkin elimination based convex hull.
1790 * We also handle a few special cases before checking the boundedness.
1792 static __isl_give isl_basic_set
*uset_convex_hull(__isl_take isl_set
*set
)
1796 struct isl_basic_set
*convex_hull
= NULL
;
1797 struct isl_basic_set
*lin
;
1799 dim
= isl_set_dim(set
, isl_dim_all
);
1803 return convex_hull_0d(set
);
1805 set
= isl_set_coalesce(set
);
1806 set
= isl_set_set_rational(set
);
1811 convex_hull
= isl_basic_set_copy(set
->p
[0]);
1816 return convex_hull_1d(set
);
1818 bounded
= isl_set_is_bounded(set
);
1821 if (bounded
&& set
->ctx
->opt
->convex
== ISL_CONVEX_HULL_WRAP
)
1822 return uset_convex_hull_wrap(set
);
1824 lin
= isl_set_combined_lineality_space(isl_set_copy(set
));
1827 if (isl_basic_set_plain_is_universe(lin
)) {
1831 if (lin
->n_eq
< dim
)
1832 return modulo_lineality(set
, lin
);
1833 isl_basic_set_free(lin
);
1835 return uset_convex_hull_unbounded(set
);
1838 isl_basic_set_free(convex_hull
);
1842 /* This is the core procedure, where "set" is a "pure" set, i.e.,
1843 * without parameters or divs and where the convex hull of set is
1844 * known to be full-dimensional.
1846 static __isl_give isl_basic_set
*uset_convex_hull_wrap_bounded(
1847 __isl_take isl_set
*set
)
1849 struct isl_basic_set
*convex_hull
= NULL
;
1852 dim
= isl_set_dim(set
, isl_dim_all
);
1857 convex_hull
= isl_basic_set_universe(isl_space_copy(set
->dim
));
1859 convex_hull
= isl_basic_set_set_rational(convex_hull
);
1863 set
= isl_set_set_rational(set
);
1864 set
= isl_set_coalesce(set
);
1868 convex_hull
= isl_basic_set_copy(set
->p
[0]);
1870 convex_hull
= isl_basic_map_remove_redundancies(convex_hull
);
1874 return convex_hull_1d(set
);
1876 return uset_convex_hull_wrap(set
);
1882 /* Compute the convex hull of set "set" with affine hull "affine_hull",
1883 * We first remove the equalities (transforming the set), compute the
1884 * convex hull of the transformed set and then add the equalities back
1885 * (after performing the inverse transformation.
1887 static __isl_give isl_basic_set
*modulo_affine_hull(
1888 __isl_take isl_set
*set
, __isl_take isl_basic_set
*affine_hull
)
1892 struct isl_basic_set
*dummy
;
1893 struct isl_basic_set
*convex_hull
;
1895 dummy
= isl_basic_set_remove_equalities(
1896 isl_basic_set_copy(affine_hull
), &T
, &T2
);
1899 isl_basic_set_free(dummy
);
1900 set
= isl_set_preimage(set
, T
);
1901 convex_hull
= uset_convex_hull(set
);
1902 convex_hull
= isl_basic_set_preimage(convex_hull
, T2
);
1903 convex_hull
= isl_basic_set_intersect(convex_hull
, affine_hull
);
1908 isl_basic_set_free(affine_hull
);
1913 /* Return an empty basic map living in the same space as "map".
1915 static __isl_give isl_basic_map
*replace_map_by_empty_basic_map(
1916 __isl_take isl_map
*map
)
1920 space
= isl_map_get_space(map
);
1922 return isl_basic_map_empty(space
);
1925 /* Compute the convex hull of a map.
1927 * The implementation was inspired by "Extended Convex Hull" by Fukuda et al.,
1928 * specifically, the wrapping of facets to obtain new facets.
1930 __isl_give isl_basic_map
*isl_map_convex_hull(__isl_take isl_map
*map
)
1932 struct isl_basic_set
*bset
;
1933 struct isl_basic_map
*model
= NULL
;
1934 struct isl_basic_set
*affine_hull
= NULL
;
1935 struct isl_basic_map
*convex_hull
= NULL
;
1936 struct isl_set
*set
= NULL
;
1938 map
= isl_map_detect_equalities(map
);
1939 map
= isl_map_align_divs_internal(map
);
1944 return replace_map_by_empty_basic_map(map
);
1946 model
= isl_basic_map_copy(map
->p
[0]);
1947 set
= isl_map_underlying_set(map
);
1951 affine_hull
= isl_set_affine_hull(isl_set_copy(set
));
1954 if (affine_hull
->n_eq
!= 0)
1955 bset
= modulo_affine_hull(set
, affine_hull
);
1957 isl_basic_set_free(affine_hull
);
1958 bset
= uset_convex_hull(set
);
1961 convex_hull
= isl_basic_map_overlying_set(bset
, model
);
1965 ISL_F_SET(convex_hull
, ISL_BASIC_MAP_NO_IMPLICIT
);
1966 ISL_F_SET(convex_hull
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1967 ISL_F_CLR(convex_hull
, ISL_BASIC_MAP_RATIONAL
);
1971 isl_basic_map_free(model
);
1975 __isl_give isl_basic_set
*isl_set_convex_hull(__isl_take isl_set
*set
)
1977 return bset_from_bmap(isl_map_convex_hull(set_to_map(set
)));
1980 __isl_give isl_basic_map
*isl_map_polyhedral_hull(__isl_take isl_map
*map
)
1982 isl_basic_map
*hull
;
1984 hull
= isl_map_convex_hull(map
);
1985 return isl_basic_map_remove_divs(hull
);
1988 __isl_give isl_basic_set
*isl_set_polyhedral_hull(__isl_take isl_set
*set
)
1990 return bset_from_bmap(isl_map_polyhedral_hull(set_to_map(set
)));
1993 struct sh_data_entry
{
1994 struct isl_hash_table
*table
;
1995 struct isl_tab
*tab
;
1998 /* Holds the data needed during the simple hull computation.
2000 * n the number of basic sets in the original set
2001 * hull_table a hash table of already computed constraints
2002 * in the simple hull
2003 * p for each basic set,
2004 * table a hash table of the constraints
2005 * tab the tableau corresponding to the basic set
2008 struct isl_ctx
*ctx
;
2010 struct isl_hash_table
*hull_table
;
2011 struct sh_data_entry p
[1];
2014 static void sh_data_free(struct sh_data
*data
)
2020 isl_hash_table_free(data
->ctx
, data
->hull_table
);
2021 for (i
= 0; i
< data
->n
; ++i
) {
2022 isl_hash_table_free(data
->ctx
, data
->p
[i
].table
);
2023 isl_tab_free(data
->p
[i
].tab
);
2028 struct ineq_cmp_data
{
2033 static isl_bool
has_ineq(const void *entry
, const void *val
)
2035 isl_int
*row
= (isl_int
*)entry
;
2036 struct ineq_cmp_data
*v
= (struct ineq_cmp_data
*)val
;
2038 return isl_bool_ok(isl_seq_eq(row
+ 1, v
->p
+ 1, v
->len
) ||
2039 isl_seq_is_neg(row
+ 1, v
->p
+ 1, v
->len
));
2042 static int hash_ineq(struct isl_ctx
*ctx
, struct isl_hash_table
*table
,
2043 isl_int
*ineq
, unsigned len
)
2046 struct ineq_cmp_data v
;
2047 struct isl_hash_table_entry
*entry
;
2051 c_hash
= isl_seq_get_hash(ineq
+ 1, len
);
2052 entry
= isl_hash_table_find(ctx
, table
, c_hash
, has_ineq
, &v
, 1);
2059 /* Fill hash table "table" with the constraints of "bset".
2060 * Equalities are added as two inequalities.
2061 * The value in the hash table is a pointer to the (in)equality of "bset".
2063 static isl_stat
hash_basic_set(struct isl_hash_table
*table
,
2064 __isl_keep isl_basic_set
*bset
)
2067 isl_size dim
= isl_basic_set_dim(bset
, isl_dim_all
);
2070 return isl_stat_error
;
2071 for (i
= 0; i
< bset
->n_eq
; ++i
) {
2072 for (j
= 0; j
< 2; ++j
) {
2073 isl_seq_neg(bset
->eq
[i
], bset
->eq
[i
], 1 + dim
);
2074 if (hash_ineq(bset
->ctx
, table
, bset
->eq
[i
], dim
) < 0)
2075 return isl_stat_error
;
2078 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
2079 if (hash_ineq(bset
->ctx
, table
, bset
->ineq
[i
], dim
) < 0)
2080 return isl_stat_error
;
2085 static struct sh_data
*sh_data_alloc(__isl_keep isl_set
*set
, unsigned n_ineq
)
2087 struct sh_data
*data
;
2090 data
= isl_calloc(set
->ctx
, struct sh_data
,
2091 sizeof(struct sh_data
) +
2092 (set
->n
- 1) * sizeof(struct sh_data_entry
));
2095 data
->ctx
= set
->ctx
;
2097 data
->hull_table
= isl_hash_table_alloc(set
->ctx
, n_ineq
);
2098 if (!data
->hull_table
)
2100 for (i
= 0; i
< set
->n
; ++i
) {
2101 data
->p
[i
].table
= isl_hash_table_alloc(set
->ctx
,
2102 2 * set
->p
[i
]->n_eq
+ set
->p
[i
]->n_ineq
);
2103 if (!data
->p
[i
].table
)
2105 if (hash_basic_set(data
->p
[i
].table
, set
->p
[i
]) < 0)
2114 /* Check if inequality "ineq" is a bound for basic set "j" or if
2115 * it can be relaxed (by increasing the constant term) to become
2116 * a bound for that basic set. In the latter case, the constant
2118 * Relaxation of the constant term is only allowed if "shift" is set.
2120 * Return 1 if "ineq" is a bound
2121 * 0 if "ineq" may attain arbitrarily small values on basic set "j"
2122 * -1 if some error occurred
2124 static int is_bound(struct sh_data
*data
, __isl_keep isl_set
*set
, int j
,
2125 isl_int
*ineq
, int shift
)
2127 enum isl_lp_result res
;
2130 if (!data
->p
[j
].tab
) {
2131 data
->p
[j
].tab
= isl_tab_from_basic_set(set
->p
[j
], 0);
2132 if (!data
->p
[j
].tab
)
2138 res
= isl_tab_min(data
->p
[j
].tab
, ineq
, data
->ctx
->one
,
2140 if (res
== isl_lp_ok
&& isl_int_is_neg(opt
)) {
2142 isl_int_sub(ineq
[0], ineq
[0], opt
);
2144 res
= isl_lp_unbounded
;
2149 return (res
== isl_lp_ok
|| res
== isl_lp_empty
) ? 1 :
2150 res
== isl_lp_unbounded
? 0 : -1;
2153 /* Set the constant term of "ineq" to the maximum of those of the constraints
2154 * in the basic sets of "set" following "i" that are parallel to "ineq".
2155 * That is, if any of the basic sets of "set" following "i" have a more
2156 * relaxed copy of "ineq", then replace "ineq" by the most relaxed copy.
2157 * "c_hash" is the hash value of the linear part of "ineq".
2158 * "v" has been set up for use by has_ineq.
2160 * Note that the two inequality constraints corresponding to an equality are
2161 * represented by the same inequality constraint in data->p[j].table
2162 * (but with different hash values). This means the constraint (or at
2163 * least its constant term) may need to be temporarily negated to get
2164 * the actually hashed constraint.
2166 static isl_stat
set_max_constant_term(struct sh_data
*data
,
2167 __isl_keep isl_set
*set
,
2168 int i
, isl_int
*ineq
, uint32_t c_hash
, struct ineq_cmp_data
*v
)
2172 struct isl_hash_table_entry
*entry
;
2174 ctx
= isl_set_get_ctx(set
);
2175 for (j
= i
+ 1; j
< set
->n
; ++j
) {
2179 entry
= isl_hash_table_find(ctx
, data
->p
[j
].table
,
2180 c_hash
, &has_ineq
, v
, 0);
2182 return isl_stat_error
;
2183 if (entry
== isl_hash_table_entry_none
)
2186 ineq_j
= entry
->data
;
2187 neg
= isl_seq_is_neg(ineq_j
+ 1, ineq
+ 1, v
->len
);
2189 isl_int_neg(ineq_j
[0], ineq_j
[0]);
2190 if (isl_int_gt(ineq_j
[0], ineq
[0]))
2191 isl_int_set(ineq
[0], ineq_j
[0]);
2193 isl_int_neg(ineq_j
[0], ineq_j
[0]);
2199 /* Check if inequality "ineq" from basic set "i" is or can be relaxed to
2200 * become a bound on the whole set. If so, add the (relaxed) inequality
2201 * to "hull". Relaxation is only allowed if "shift" is set.
2203 * We first check if "hull" already contains a translate of the inequality.
2204 * If so, we are done.
2205 * Then, we check if any of the previous basic sets contains a translate
2206 * of the inequality. If so, then we have already considered this
2207 * inequality and we are done.
2208 * Otherwise, for each basic set other than "i", we check if the inequality
2209 * is a bound on the basic set, but first replace the constant term
2210 * by the maximal value of any translate of the inequality in any
2211 * of the following basic sets.
2212 * For previous basic sets, we know that they do not contain a translate
2213 * of the inequality, so we directly call is_bound.
2214 * For following basic sets, we first check if a translate of the
2215 * inequality appears in its description. If so, the constant term
2216 * of the inequality has already been updated with respect to this
2217 * translate and the inequality is therefore known to be a bound
2218 * of this basic set.
2220 static __isl_give isl_basic_set
*add_bound(__isl_take isl_basic_set
*hull
,
2221 struct sh_data
*data
, __isl_keep isl_set
*set
, int i
, isl_int
*ineq
,
2225 struct ineq_cmp_data v
;
2226 struct isl_hash_table_entry
*entry
;
2230 total
= isl_basic_set_dim(hull
, isl_dim_all
);
2232 return isl_basic_set_free(hull
);
2236 c_hash
= isl_seq_get_hash(ineq
+ 1, v
.len
);
2238 entry
= isl_hash_table_find(hull
->ctx
, data
->hull_table
, c_hash
,
2241 return isl_basic_set_free(hull
);
2242 if (entry
!= isl_hash_table_entry_none
)
2245 for (j
= 0; j
< i
; ++j
) {
2246 entry
= isl_hash_table_find(hull
->ctx
, data
->p
[j
].table
,
2247 c_hash
, has_ineq
, &v
, 0);
2249 return isl_basic_set_free(hull
);
2250 if (entry
!= isl_hash_table_entry_none
)
2256 k
= isl_basic_set_alloc_inequality(hull
);
2259 isl_seq_cpy(hull
->ineq
[k
], ineq
, 1 + v
.len
);
2261 if (set_max_constant_term(data
, set
, i
, hull
->ineq
[k
], c_hash
, &v
) < 0)
2263 for (j
= 0; j
< i
; ++j
) {
2265 bound
= is_bound(data
, set
, j
, hull
->ineq
[k
], shift
);
2272 return isl_basic_set_free_inequality(hull
, 1);
2274 for (j
= i
+ 1; j
< set
->n
; ++j
) {
2276 entry
= isl_hash_table_find(hull
->ctx
, data
->p
[j
].table
,
2277 c_hash
, has_ineq
, &v
, 0);
2279 return isl_basic_set_free(hull
);
2280 if (entry
!= isl_hash_table_entry_none
)
2282 bound
= is_bound(data
, set
, j
, hull
->ineq
[k
], shift
);
2289 return isl_basic_set_free_inequality(hull
, 1);
2291 entry
= isl_hash_table_find(hull
->ctx
, data
->hull_table
, c_hash
,
2295 entry
->data
= hull
->ineq
[k
];
2299 isl_basic_set_free(hull
);
2303 /* Check if any inequality from basic set "i" is or can be relaxed to
2304 * become a bound on the whole set. If so, add the (relaxed) inequality
2305 * to "hull". Relaxation is only allowed if "shift" is set.
2307 static __isl_give isl_basic_set
*add_bounds(__isl_take isl_basic_set
*bset
,
2308 struct sh_data
*data
, __isl_keep isl_set
*set
, int i
, int shift
)
2311 isl_size dim
= isl_basic_set_dim(bset
, isl_dim_all
);
2314 return isl_basic_set_free(bset
);
2316 for (j
= 0; j
< set
->p
[i
]->n_eq
; ++j
) {
2317 for (k
= 0; k
< 2; ++k
) {
2318 isl_seq_neg(set
->p
[i
]->eq
[j
], set
->p
[i
]->eq
[j
], 1+dim
);
2319 bset
= add_bound(bset
, data
, set
, i
, set
->p
[i
]->eq
[j
],
2323 for (j
= 0; j
< set
->p
[i
]->n_ineq
; ++j
)
2324 bset
= add_bound(bset
, data
, set
, i
, set
->p
[i
]->ineq
[j
], shift
);
2328 /* Compute a superset of the convex hull of set that is described
2329 * by only (translates of) the constraints in the constituents of set.
2330 * Translation is only allowed if "shift" is set.
2332 static __isl_give isl_basic_set
*uset_simple_hull(__isl_take isl_set
*set
,
2335 struct sh_data
*data
= NULL
;
2336 struct isl_basic_set
*hull
= NULL
;
2344 for (i
= 0; i
< set
->n
; ++i
) {
2347 n_ineq
+= 2 * set
->p
[i
]->n_eq
+ set
->p
[i
]->n_ineq
;
2350 hull
= isl_basic_set_alloc_space(isl_space_copy(set
->dim
), 0, 0, n_ineq
);
2354 data
= sh_data_alloc(set
, n_ineq
);
2358 for (i
= 0; i
< set
->n
; ++i
)
2359 hull
= add_bounds(hull
, data
, set
, i
, shift
);
2367 isl_basic_set_free(hull
);
2372 /* Compute a superset of the convex hull of map that is described
2373 * by only (translates of) the constraints in the constituents of map.
2374 * Handle trivial cases where map is NULL or contains at most one disjunct.
2376 static __isl_give isl_basic_map
*map_simple_hull_trivial(
2377 __isl_take isl_map
*map
)
2379 isl_basic_map
*hull
;
2384 return replace_map_by_empty_basic_map(map
);
2386 hull
= isl_basic_map_copy(map
->p
[0]);
2391 /* Return a copy of the simple hull cached inside "map".
2392 * "shift" determines whether to return the cached unshifted or shifted
2395 static __isl_give isl_basic_map
*cached_simple_hull(__isl_take isl_map
*map
,
2398 isl_basic_map
*hull
;
2400 hull
= isl_basic_map_copy(map
->cached_simple_hull
[shift
]);
2406 /* Compute a superset of the convex hull of map that is described
2407 * by only (translates of) the constraints in the constituents of map.
2408 * Translation is only allowed if "shift" is set.
2410 * The constraints are sorted while removing redundant constraints
2411 * in order to indicate a preference of which constraints should
2412 * be preserved. In particular, pairs of constraints that are
2413 * sorted together are preferred to either both be preserved
2414 * or both be removed. The sorting is performed inside
2415 * isl_basic_map_remove_redundancies.
2417 * The result of the computation is stored in map->cached_simple_hull[shift]
2418 * such that it can be reused in subsequent calls. The cache is cleared
2419 * whenever the map is modified (in isl_map_cow).
2420 * Note that the results need to be stored in the input map for there
2421 * to be any chance that they may get reused. In particular, they
2422 * are stored in a copy of the input map that is saved before
2423 * the integer division alignment.
2425 static __isl_give isl_basic_map
*map_simple_hull(__isl_take isl_map
*map
,
2428 struct isl_set
*set
= NULL
;
2429 struct isl_basic_map
*model
= NULL
;
2430 struct isl_basic_map
*hull
;
2431 struct isl_basic_map
*affine_hull
;
2432 struct isl_basic_set
*bset
= NULL
;
2435 if (!map
|| map
->n
<= 1)
2436 return map_simple_hull_trivial(map
);
2438 if (map
->cached_simple_hull
[shift
])
2439 return cached_simple_hull(map
, shift
);
2441 map
= isl_map_detect_equalities(map
);
2442 if (!map
|| map
->n
<= 1)
2443 return map_simple_hull_trivial(map
);
2444 affine_hull
= isl_map_affine_hull(isl_map_copy(map
));
2445 input
= isl_map_copy(map
);
2446 map
= isl_map_align_divs_internal(map
);
2447 model
= map
? isl_basic_map_copy(map
->p
[0]) : NULL
;
2449 set
= isl_map_underlying_set(map
);
2451 bset
= uset_simple_hull(set
, shift
);
2453 hull
= isl_basic_map_overlying_set(bset
, model
);
2455 hull
= isl_basic_map_intersect(hull
, affine_hull
);
2456 hull
= isl_basic_map_remove_redundancies(hull
);
2459 ISL_F_SET(hull
, ISL_BASIC_MAP_NO_IMPLICIT
);
2460 ISL_F_SET(hull
, ISL_BASIC_MAP_ALL_EQUALITIES
);
2463 hull
= isl_basic_map_finalize(hull
);
2465 input
->cached_simple_hull
[shift
] = isl_basic_map_copy(hull
);
2466 isl_map_free(input
);
2471 /* Compute a superset of the convex hull of map that is described
2472 * by only translates of the constraints in the constituents of map.
2474 __isl_give isl_basic_map
*isl_map_simple_hull(__isl_take isl_map
*map
)
2476 return map_simple_hull(map
, 1);
2479 __isl_give isl_basic_set
*isl_set_simple_hull(__isl_take isl_set
*set
)
2481 return bset_from_bmap(isl_map_simple_hull(set_to_map(set
)));
2484 /* Compute a superset of the convex hull of map that is described
2485 * by only the constraints in the constituents of map.
2487 __isl_give isl_basic_map
*isl_map_unshifted_simple_hull(
2488 __isl_take isl_map
*map
)
2490 return map_simple_hull(map
, 0);
2493 __isl_give isl_basic_set
*isl_set_unshifted_simple_hull(
2494 __isl_take isl_set
*set
)
2496 return isl_map_unshifted_simple_hull(set
);
2499 /* Drop all inequalities from "bmap1" that do not also appear in "bmap2".
2500 * A constraint that appears with different constant terms
2501 * in "bmap1" and "bmap2" is also kept, with the least restrictive
2502 * (i.e., greatest) constant term.
2503 * "bmap1" and "bmap2" are assumed to have the same (known)
2504 * integer divisions.
2505 * The constraints of both "bmap1" and "bmap2" are assumed
2506 * to have been sorted using isl_basic_map_sort_constraints.
2508 * Run through the inequality constraints of "bmap1" and "bmap2"
2510 * Each constraint of "bmap1" without a matching constraint in "bmap2"
2512 * If a match is found, the constraint is kept. If needed, the constant
2513 * term of the constraint is adjusted.
2515 static __isl_give isl_basic_map
*select_shared_inequalities(
2516 __isl_take isl_basic_map
*bmap1
, __isl_keep isl_basic_map
*bmap2
)
2520 bmap1
= isl_basic_map_cow(bmap1
);
2521 if (!bmap1
|| !bmap2
)
2522 return isl_basic_map_free(bmap1
);
2524 i1
= bmap1
->n_ineq
- 1;
2525 i2
= bmap2
->n_ineq
- 1;
2526 while (bmap1
&& i1
>= 0 && i2
>= 0) {
2529 cmp
= isl_basic_map_constraint_cmp(bmap1
, bmap1
->ineq
[i1
],
2536 if (isl_basic_map_drop_inequality(bmap1
, i1
) < 0)
2537 bmap1
= isl_basic_map_free(bmap1
);
2541 if (isl_int_lt(bmap1
->ineq
[i1
][0], bmap2
->ineq
[i2
][0]))
2542 isl_int_set(bmap1
->ineq
[i1
][0], bmap2
->ineq
[i2
][0]);
2546 for (; i1
>= 0; --i1
)
2547 if (isl_basic_map_drop_inequality(bmap1
, i1
) < 0)
2548 bmap1
= isl_basic_map_free(bmap1
);
2553 /* Drop all equalities from "bmap1" that do not also appear in "bmap2".
2554 * "bmap1" and "bmap2" are assumed to have the same (known)
2555 * integer divisions.
2557 * Run through the equality constraints of "bmap1" and "bmap2".
2558 * Each constraint of "bmap1" without a matching constraint in "bmap2"
2561 static __isl_give isl_basic_map
*select_shared_equalities(
2562 __isl_take isl_basic_map
*bmap1
, __isl_keep isl_basic_map
*bmap2
)
2567 bmap1
= isl_basic_map_cow(bmap1
);
2568 total
= isl_basic_map_dim(bmap1
, isl_dim_all
);
2569 if (total
< 0 || !bmap2
)
2570 return isl_basic_map_free(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 if (isl_basic_map_check_equal_space(bmap1
, bmap2
) < 0)
2619 bmap1
= isl_basic_map_drop_constraints_involving_unknown_divs(bmap1
);
2620 bmap2
= isl_basic_map_drop_constraints_involving_unknown_divs(bmap2
);
2621 bmap1
= isl_basic_map_order_divs(bmap1
);
2622 bmap2
= isl_basic_map_align_divs(bmap2
, bmap1
);
2623 bmap1
= isl_basic_map_align_divs(bmap1
, bmap2
);
2624 bmap1
= isl_basic_map_sort_constraints(bmap1
);
2625 bmap2
= isl_basic_map_sort_constraints(bmap2
);
2627 bmap1
= select_shared_inequalities(bmap1
, bmap2
);
2628 bmap1
= select_shared_equalities(bmap1
, bmap2
);
2630 isl_basic_map_free(bmap2
);
2631 bmap1
= isl_basic_map_finalize(bmap1
);
2634 isl_basic_map_free(bmap1
);
2635 isl_basic_map_free(bmap2
);
2639 /* Compute a superset of the convex hull of "map" that is described
2640 * by only the constraints in the constituents of "map".
2641 * In particular, the result is composed of constraints that appear
2642 * in each of the basic maps of "map"
2644 * Constraints that involve unknown integer divisions are dropped
2645 * since it is not trivial to check whether two such integer divisions
2646 * in different basic maps are the same.
2648 * The hull is initialized from the first basic map and then
2649 * updated with respect to the other basic maps in turn.
2651 __isl_give isl_basic_map
*isl_map_plain_unshifted_simple_hull(
2652 __isl_take isl_map
*map
)
2655 isl_basic_map
*hull
;
2660 return map_simple_hull_trivial(map
);
2661 map
= isl_map_drop_constraints_involving_unknown_divs(map
);
2662 hull
= isl_basic_map_copy(map
->p
[0]);
2663 for (i
= 1; i
< map
->n
; ++i
) {
2664 isl_basic_map
*bmap_i
;
2666 bmap_i
= isl_basic_map_copy(map
->p
[i
]);
2667 hull
= isl_basic_map_plain_unshifted_simple_hull(hull
, bmap_i
);
2674 /* Compute a superset of the convex hull of "set" that is described
2675 * by only the constraints in the constituents of "set".
2676 * In particular, the result is composed of constraints that appear
2677 * in each of the basic sets of "set"
2679 __isl_give isl_basic_set
*isl_set_plain_unshifted_simple_hull(
2680 __isl_take isl_set
*set
)
2682 return isl_map_plain_unshifted_simple_hull(set
);
2685 /* Check if "ineq" is a bound on "set" and, if so, add it to "hull".
2687 * For each basic set in "set", we first check if the basic set
2688 * contains a translate of "ineq". If this translate is more relaxed,
2689 * then we assume that "ineq" is not a bound on this basic set.
2690 * Otherwise, we know that it is a bound.
2691 * If the basic set does not contain a translate of "ineq", then
2692 * we call is_bound to perform the test.
2694 static __isl_give isl_basic_set
*add_bound_from_constraint(
2695 __isl_take isl_basic_set
*hull
, struct sh_data
*data
,
2696 __isl_keep isl_set
*set
, isl_int
*ineq
)
2701 struct ineq_cmp_data v
;
2704 total
= isl_basic_set_dim(hull
, isl_dim_all
);
2705 if (total
< 0 || !set
)
2706 return isl_basic_set_free(hull
);
2710 c_hash
= isl_seq_get_hash(ineq
+ 1, v
.len
);
2712 ctx
= isl_basic_set_get_ctx(hull
);
2713 for (i
= 0; i
< set
->n
; ++i
) {
2715 struct isl_hash_table_entry
*entry
;
2717 entry
= isl_hash_table_find(ctx
, data
->p
[i
].table
,
2718 c_hash
, &has_ineq
, &v
, 0);
2720 return isl_basic_set_free(hull
);
2721 if (entry
!= isl_hash_table_entry_none
) {
2722 isl_int
*ineq_i
= entry
->data
;
2723 int neg
, more_relaxed
;
2725 neg
= isl_seq_is_neg(ineq_i
+ 1, ineq
+ 1, v
.len
);
2727 isl_int_neg(ineq_i
[0], ineq_i
[0]);
2728 more_relaxed
= isl_int_gt(ineq_i
[0], ineq
[0]);
2730 isl_int_neg(ineq_i
[0], ineq_i
[0]);
2736 bound
= is_bound(data
, set
, i
, ineq
, 0);
2738 return isl_basic_set_free(hull
);
2745 k
= isl_basic_set_alloc_inequality(hull
);
2747 return isl_basic_set_free(hull
);
2748 isl_seq_cpy(hull
->ineq
[k
], ineq
, 1 + v
.len
);
2753 /* Compute a superset of the convex hull of "set" that is described
2754 * by only some of the "n_ineq" constraints in the list "ineq", where "set"
2755 * has no parameters or integer divisions.
2757 * The inequalities in "ineq" are assumed to have been sorted such
2758 * that constraints with the same linear part appear together and
2759 * that among constraints with the same linear part, those with
2760 * smaller constant term appear first.
2762 * We reuse the same data structure that is used by uset_simple_hull,
2763 * but we do not need the hull table since we will not consider the
2764 * same constraint more than once. We therefore allocate it with zero size.
2766 * We run through the constraints and try to add them one by one,
2767 * skipping identical constraints. If we have added a constraint and
2768 * the next constraint is a more relaxed translate, then we skip this
2769 * next constraint as well.
2771 static __isl_give isl_basic_set
*uset_unshifted_simple_hull_from_constraints(
2772 __isl_take isl_set
*set
, int n_ineq
, isl_int
**ineq
)
2776 struct sh_data
*data
= NULL
;
2777 isl_basic_set
*hull
= NULL
;
2780 hull
= isl_basic_set_alloc_space(isl_set_get_space(set
), 0, 0, n_ineq
);
2784 data
= sh_data_alloc(set
, 0);
2788 dim
= isl_set_dim(set
, isl_dim_set
);
2791 for (i
= 0; i
< n_ineq
; ++i
) {
2792 int hull_n_ineq
= hull
->n_ineq
;
2795 parallel
= i
> 0 && isl_seq_eq(ineq
[i
- 1] + 1, ineq
[i
] + 1,
2798 (last_added
|| isl_int_eq(ineq
[i
- 1][0], ineq
[i
][0])))
2800 hull
= add_bound_from_constraint(hull
, data
, set
, ineq
[i
]);
2803 last_added
= hull
->n_ineq
> hull_n_ineq
;
2812 isl_basic_set_free(hull
);
2816 /* Collect pointers to all the inequalities in the elements of "list"
2817 * in "ineq". For equalities, store both a pointer to the equality and
2818 * a pointer to its opposite, which is first copied to "mat".
2819 * "ineq" and "mat" are assumed to have been preallocated to the right size
2820 * (the number of inequalities + 2 times the number of equalites and
2821 * the number of equalities, respectively).
2823 static __isl_give isl_mat
*collect_inequalities(__isl_take isl_mat
*mat
,
2824 __isl_keep isl_basic_set_list
*list
, isl_int
**ineq
)
2826 int i
, j
, n_eq
, n_ineq
;
2829 n
= isl_basic_set_list_n_basic_set(list
);
2831 return isl_mat_free(mat
);
2835 for (i
= 0; i
< n
; ++i
) {
2836 isl_basic_set
*bset
;
2837 bset
= isl_basic_set_list_get_basic_set(list
, i
);
2839 return isl_mat_free(mat
);
2840 for (j
= 0; j
< bset
->n_eq
; ++j
) {
2841 ineq
[n_ineq
++] = mat
->row
[n_eq
];
2842 ineq
[n_ineq
++] = bset
->eq
[j
];
2843 isl_seq_neg(mat
->row
[n_eq
++], bset
->eq
[j
], mat
->n_col
);
2845 for (j
= 0; j
< bset
->n_ineq
; ++j
)
2846 ineq
[n_ineq
++] = bset
->ineq
[j
];
2847 isl_basic_set_free(bset
);
2853 /* Comparison routine for use as an isl_sort callback.
2855 * Constraints with the same linear part are sorted together and
2856 * among constraints with the same linear part, those with smaller
2857 * constant term are sorted first.
2859 static int cmp_ineq(const void *a
, const void *b
, void *arg
)
2861 unsigned dim
= *(unsigned *) arg
;
2862 isl_int
* const *ineq1
= a
;
2863 isl_int
* const *ineq2
= b
;
2866 cmp
= isl_seq_cmp((*ineq1
) + 1, (*ineq2
) + 1, dim
);
2869 return isl_int_cmp((*ineq1
)[0], (*ineq2
)[0]);
2872 /* Compute a superset of the convex hull of "set" that is described
2873 * by only constraints in the elements of "list", where "set" has
2874 * no parameters or integer divisions.
2876 * We collect all the constraints in those elements and then
2877 * sort the constraints such that constraints with the same linear part
2878 * are sorted together and that those with smaller constant term are
2881 static __isl_give isl_basic_set
*uset_unshifted_simple_hull_from_basic_set_list(
2882 __isl_take isl_set
*set
, __isl_take isl_basic_set_list
*list
)
2884 int i
, n_eq
, n_ineq
;
2888 isl_mat
*mat
= NULL
;
2889 isl_int
**ineq
= NULL
;
2890 isl_basic_set
*hull
;
2892 n
= isl_basic_set_list_n_basic_set(list
);
2895 ctx
= isl_set_get_ctx(set
);
2899 for (i
= 0; i
< n
; ++i
) {
2900 isl_basic_set
*bset
;
2901 bset
= isl_basic_set_list_get_basic_set(list
, i
);
2905 n_ineq
+= 2 * bset
->n_eq
+ bset
->n_ineq
;
2906 isl_basic_set_free(bset
);
2909 ineq
= isl_alloc_array(ctx
, isl_int
*, n_ineq
);
2910 if (n_ineq
> 0 && !ineq
)
2913 dim
= isl_set_dim(set
, isl_dim_set
);
2916 mat
= isl_mat_alloc(ctx
, n_eq
, 1 + dim
);
2917 mat
= collect_inequalities(mat
, list
, ineq
);
2921 if (isl_sort(ineq
, n_ineq
, sizeof(ineq
[0]), &cmp_ineq
, &dim
) < 0)
2924 hull
= uset_unshifted_simple_hull_from_constraints(set
, n_ineq
, ineq
);
2928 isl_basic_set_list_free(list
);
2934 isl_basic_set_list_free(list
);
2938 /* Compute a superset of the convex hull of "map" that is described
2939 * by only constraints in the elements of "list".
2941 * If the list is empty, then we can only describe the universe set.
2942 * If the input map is empty, then all constraints are valid, so
2943 * we return the intersection of the elements in "list".
2945 * Otherwise, we align all divs and temporarily treat them
2946 * as regular variables, computing the unshifted simple hull in
2947 * uset_unshifted_simple_hull_from_basic_set_list.
2949 static __isl_give isl_basic_map
*map_unshifted_simple_hull_from_basic_map_list(
2950 __isl_take isl_map
*map
, __isl_take isl_basic_map_list
*list
)
2953 isl_basic_map
*model
;
2954 isl_basic_map
*hull
;
2956 isl_basic_set_list
*bset_list
;
2958 n
= isl_basic_map_list_n_basic_map(list
);
2965 space
= isl_map_get_space(map
);
2967 isl_basic_map_list_free(list
);
2968 return isl_basic_map_universe(space
);
2970 if (isl_map_plain_is_empty(map
)) {
2972 return isl_basic_map_list_intersect(list
);
2975 map
= isl_map_align_divs_to_basic_map_list(map
, list
);
2978 list
= isl_basic_map_list_align_divs_to_basic_map(list
, map
->p
[0]);
2980 model
= isl_basic_map_list_get_basic_map(list
, 0);
2982 set
= isl_map_underlying_set(map
);
2983 bset_list
= isl_basic_map_list_underlying_set(list
);
2985 hull
= uset_unshifted_simple_hull_from_basic_set_list(set
, bset_list
);
2986 hull
= isl_basic_map_overlying_set(hull
, model
);
2991 isl_basic_map_list_free(list
);
2995 /* Return a sequence of the basic maps that make up the maps in "list".
2997 static __isl_give isl_basic_map_list
*collect_basic_maps(
2998 __isl_take isl_map_list
*list
)
3003 isl_basic_map_list
*bmap_list
;
3007 n
= isl_map_list_n_map(list
);
3008 ctx
= isl_map_list_get_ctx(list
);
3009 bmap_list
= isl_basic_map_list_alloc(ctx
, 0);
3011 bmap_list
= isl_basic_map_list_free(bmap_list
);
3013 for (i
= 0; i
< n
; ++i
) {
3015 isl_basic_map_list
*list_i
;
3017 map
= isl_map_list_get_map(list
, i
);
3018 map
= isl_map_compute_divs(map
);
3019 list_i
= isl_map_get_basic_map_list(map
);
3021 bmap_list
= isl_basic_map_list_concat(bmap_list
, list_i
);
3024 isl_map_list_free(list
);
3028 /* Compute a superset of the convex hull of "map" that is described
3029 * by only constraints in the elements of "list".
3031 * If "map" is the universe, then the convex hull (and therefore
3032 * any superset of the convexhull) is the universe as well.
3034 * Otherwise, we collect all the basic maps in the map list and
3035 * continue with map_unshifted_simple_hull_from_basic_map_list.
3037 __isl_give isl_basic_map
*isl_map_unshifted_simple_hull_from_map_list(
3038 __isl_take isl_map
*map
, __isl_take isl_map_list
*list
)
3040 isl_basic_map_list
*bmap_list
;
3043 is_universe
= isl_map_plain_is_universe(map
);
3044 if (is_universe
< 0)
3045 map
= isl_map_free(map
);
3046 if (is_universe
< 0 || is_universe
) {
3047 isl_map_list_free(list
);
3048 return isl_map_unshifted_simple_hull(map
);
3051 bmap_list
= collect_basic_maps(list
);
3052 return map_unshifted_simple_hull_from_basic_map_list(map
, bmap_list
);
3055 /* Compute a superset of the convex hull of "set" that is described
3056 * by only constraints in the elements of "list".
3058 __isl_give isl_basic_set
*isl_set_unshifted_simple_hull_from_set_list(
3059 __isl_take isl_set
*set
, __isl_take isl_set_list
*list
)
3061 return isl_map_unshifted_simple_hull_from_map_list(set
, list
);
3064 /* Given a set "set", return parametric bounds on the dimension "dim".
3066 static __isl_give isl_basic_set
*set_bounds(__isl_keep isl_set
*set
, int dim
)
3068 isl_size set_dim
= isl_set_dim(set
, isl_dim_set
);
3071 set
= isl_set_copy(set
);
3072 set
= isl_set_eliminate_dims(set
, dim
+ 1, set_dim
- (dim
+ 1));
3073 set
= isl_set_eliminate_dims(set
, 0, dim
);
3074 return isl_set_convex_hull(set
);
3077 /* Computes a "simple hull" and then check if each dimension in the
3078 * resulting hull is bounded by a symbolic constant. If not, the
3079 * hull is intersected with the corresponding bounds on the whole set.
3081 __isl_give isl_basic_set
*isl_set_bounded_simple_hull(__isl_take isl_set
*set
)
3084 struct isl_basic_set
*hull
;
3085 isl_size nparam
, dim
, total
;
3087 int removed_divs
= 0;
3089 hull
= isl_set_simple_hull(isl_set_copy(set
));
3090 nparam
= isl_basic_set_dim(hull
, isl_dim_param
);
3091 dim
= isl_basic_set_dim(hull
, isl_dim_set
);
3092 total
= isl_basic_set_dim(hull
, isl_dim_all
);
3093 if (nparam
< 0 || dim
< 0 || total
< 0)
3096 for (i
= 0; i
< dim
; ++i
) {
3097 int lower
= 0, upper
= 0;
3098 struct isl_basic_set
*bounds
;
3100 left
= total
- nparam
- i
- 1;
3101 for (j
= 0; j
< hull
->n_eq
; ++j
) {
3102 if (isl_int_is_zero(hull
->eq
[j
][1 + nparam
+ i
]))
3104 if (isl_seq_first_non_zero(hull
->eq
[j
]+1+nparam
+i
+1,
3111 for (j
= 0; j
< hull
->n_ineq
; ++j
) {
3112 if (isl_int_is_zero(hull
->ineq
[j
][1 + nparam
+ i
]))
3114 if (isl_seq_first_non_zero(hull
->ineq
[j
]+1+nparam
+i
+1,
3116 isl_seq_first_non_zero(hull
->ineq
[j
]+1+nparam
,
3119 if (isl_int_is_pos(hull
->ineq
[j
][1 + nparam
+ i
]))
3130 if (!removed_divs
) {
3131 set
= isl_set_remove_divs(set
);
3136 bounds
= set_bounds(set
, i
);
3137 hull
= isl_basic_set_intersect(hull
, bounds
);
3146 isl_basic_set_free(hull
);