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 * Alternatively, we could have intersected the basic map with the
83 * corresponding equality and the checked if the dimension was that
86 __isl_give isl_basic_map
*isl_basic_map_remove_redundancies(
87 __isl_take isl_basic_map
*bmap
)
94 bmap
= isl_basic_map_gauss(bmap
, NULL
);
95 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
97 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
))
99 if (bmap
->n_ineq
<= 1)
102 tab
= isl_tab_from_basic_map(bmap
, 0);
103 if (isl_tab_detect_implicit_equalities(tab
) < 0)
105 if (isl_tab_detect_redundant(tab
) < 0)
107 bmap
= isl_basic_map_update_from_tab(bmap
, tab
);
111 ISL_F_SET(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
112 ISL_F_SET(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
116 isl_basic_map_free(bmap
);
120 __isl_give isl_basic_set
*isl_basic_set_remove_redundancies(
121 __isl_take isl_basic_set
*bset
)
123 return (struct isl_basic_set
*)
124 isl_basic_map_remove_redundancies((struct isl_basic_map
*)bset
);
127 /* Remove redundant constraints in each of the basic maps.
129 __isl_give isl_map
*isl_map_remove_redundancies(__isl_take isl_map
*map
)
131 return isl_map_inline_foreach_basic_map(map
,
132 &isl_basic_map_remove_redundancies
);
135 __isl_give isl_set
*isl_set_remove_redundancies(__isl_take isl_set
*set
)
137 return isl_map_remove_redundancies(set
);
140 /* Check if the set set is bound in the direction of the affine
141 * constraint c and if so, set the constant term such that the
142 * resulting constraint is a bounding constraint for the set.
144 static int uset_is_bound(struct isl_set
*set
, isl_int
*c
, unsigned len
)
152 isl_int_init(opt_denom
);
154 for (j
= 0; j
< set
->n
; ++j
) {
155 enum isl_lp_result res
;
157 if (ISL_F_ISSET(set
->p
[j
], ISL_BASIC_SET_EMPTY
))
160 res
= isl_basic_set_solve_lp(set
->p
[j
],
161 0, c
, set
->ctx
->one
, &opt
, &opt_denom
, NULL
);
162 if (res
== isl_lp_unbounded
)
164 if (res
== isl_lp_error
)
166 if (res
== isl_lp_empty
) {
167 set
->p
[j
] = isl_basic_set_set_to_empty(set
->p
[j
]);
172 if (first
|| isl_int_is_neg(opt
)) {
173 if (!isl_int_is_one(opt_denom
))
174 isl_seq_scale(c
, c
, opt_denom
, len
);
175 isl_int_sub(c
[0], c
[0], opt
);
180 isl_int_clear(opt_denom
);
184 isl_int_clear(opt_denom
);
188 __isl_give isl_basic_map
*isl_basic_map_set_rational(
189 __isl_take isl_basic_set
*bmap
)
194 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
197 bmap
= isl_basic_map_cow(bmap
);
201 ISL_F_SET(bmap
, ISL_BASIC_MAP_RATIONAL
);
203 return isl_basic_map_finalize(bmap
);
206 __isl_give isl_basic_set
*isl_basic_set_set_rational(
207 __isl_take isl_basic_set
*bset
)
209 return isl_basic_map_set_rational(bset
);
212 __isl_give isl_map
*isl_map_set_rational(__isl_take isl_map
*map
)
216 map
= isl_map_cow(map
);
219 for (i
= 0; i
< map
->n
; ++i
) {
220 map
->p
[i
] = isl_basic_map_set_rational(map
->p
[i
]);
230 __isl_give isl_set
*isl_set_set_rational(__isl_take isl_set
*set
)
232 return isl_map_set_rational(set
);
235 static struct isl_basic_set
*isl_basic_set_add_equality(
236 struct isl_basic_set
*bset
, isl_int
*c
)
244 if (ISL_F_ISSET(bset
, ISL_BASIC_SET_EMPTY
))
247 isl_assert(bset
->ctx
, isl_basic_set_n_param(bset
) == 0, goto error
);
248 isl_assert(bset
->ctx
, bset
->n_div
== 0, goto error
);
249 dim
= isl_basic_set_n_dim(bset
);
250 bset
= isl_basic_set_cow(bset
);
251 bset
= isl_basic_set_extend(bset
, 0, dim
, 0, 1, 0);
252 i
= isl_basic_set_alloc_equality(bset
);
255 isl_seq_cpy(bset
->eq
[i
], c
, 1 + dim
);
258 isl_basic_set_free(bset
);
262 static struct isl_set
*isl_set_add_basic_set_equality(struct isl_set
*set
, isl_int
*c
)
266 set
= isl_set_cow(set
);
269 for (i
= 0; i
< set
->n
; ++i
) {
270 set
->p
[i
] = isl_basic_set_add_equality(set
->p
[i
], c
);
280 /* Given a union of basic sets, construct the constraints for wrapping
281 * a facet around one of its ridges.
282 * In particular, if each of n the d-dimensional basic sets i in "set"
283 * contains the origin, satisfies the constraints x_1 >= 0 and x_2 >= 0
284 * and is defined by the constraints
288 * then the resulting set is of dimension n*(1+d) and has as constraints
297 static struct isl_basic_set
*wrap_constraints(struct isl_set
*set
)
299 struct isl_basic_set
*lp
;
303 unsigned dim
, lp_dim
;
308 dim
= 1 + isl_set_n_dim(set
);
311 for (i
= 0; i
< set
->n
; ++i
) {
312 n_eq
+= set
->p
[i
]->n_eq
;
313 n_ineq
+= set
->p
[i
]->n_ineq
;
315 lp
= isl_basic_set_alloc(set
->ctx
, 0, dim
* set
->n
, 0, n_eq
, n_ineq
);
316 lp
= isl_basic_set_set_rational(lp
);
319 lp_dim
= isl_basic_set_n_dim(lp
);
320 k
= isl_basic_set_alloc_equality(lp
);
321 isl_int_set_si(lp
->eq
[k
][0], -1);
322 for (i
= 0; i
< set
->n
; ++i
) {
323 isl_int_set_si(lp
->eq
[k
][1+dim
*i
], 0);
324 isl_int_set_si(lp
->eq
[k
][1+dim
*i
+1], 1);
325 isl_seq_clr(lp
->eq
[k
]+1+dim
*i
+2, dim
-2);
327 for (i
= 0; i
< set
->n
; ++i
) {
328 k
= isl_basic_set_alloc_inequality(lp
);
329 isl_seq_clr(lp
->ineq
[k
], 1+lp_dim
);
330 isl_int_set_si(lp
->ineq
[k
][1+dim
*i
], 1);
332 for (j
= 0; j
< set
->p
[i
]->n_eq
; ++j
) {
333 k
= isl_basic_set_alloc_equality(lp
);
334 isl_seq_clr(lp
->eq
[k
], 1+dim
*i
);
335 isl_seq_cpy(lp
->eq
[k
]+1+dim
*i
, set
->p
[i
]->eq
[j
], dim
);
336 isl_seq_clr(lp
->eq
[k
]+1+dim
*(i
+1), dim
*(set
->n
-i
-1));
339 for (j
= 0; j
< set
->p
[i
]->n_ineq
; ++j
) {
340 k
= isl_basic_set_alloc_inequality(lp
);
341 isl_seq_clr(lp
->ineq
[k
], 1+dim
*i
);
342 isl_seq_cpy(lp
->ineq
[k
]+1+dim
*i
, set
->p
[i
]->ineq
[j
], dim
);
343 isl_seq_clr(lp
->ineq
[k
]+1+dim
*(i
+1), dim
*(set
->n
-i
-1));
349 /* Given a facet "facet" of the convex hull of "set" and a facet "ridge"
350 * of that facet, compute the other facet of the convex hull that contains
353 * We first transform the set such that the facet constraint becomes
357 * I.e., the facet lies in
361 * and on that facet, the constraint that defines the ridge is
365 * (This transformation is not strictly needed, all that is needed is
366 * that the ridge contains the origin.)
368 * Since the ridge contains the origin, the cone of the convex hull
369 * will be of the form
374 * with this second constraint defining the new facet.
375 * The constant a is obtained by settting x_1 in the cone of the
376 * convex hull to 1 and minimizing x_2.
377 * Now, each element in the cone of the convex hull is the sum
378 * of elements in the cones of the basic sets.
379 * If a_i is the dilation factor of basic set i, then the problem
380 * we need to solve is
393 * the constraints of each (transformed) basic set.
394 * If a = n/d, then the constraint defining the new facet (in the transformed
397 * -n x_1 + d x_2 >= 0
399 * In the original space, we need to take the same combination of the
400 * corresponding constraints "facet" and "ridge".
402 * If a = -infty = "-1/0", then we just return the original facet constraint.
403 * This means that the facet is unbounded, but has a bounded intersection
404 * with the union of sets.
406 isl_int
*isl_set_wrap_facet(__isl_keep isl_set
*set
,
407 isl_int
*facet
, isl_int
*ridge
)
411 struct isl_mat
*T
= NULL
;
412 struct isl_basic_set
*lp
= NULL
;
414 enum isl_lp_result res
;
421 set
= isl_set_copy(set
);
422 set
= isl_set_set_rational(set
);
424 dim
= 1 + isl_set_n_dim(set
);
425 T
= isl_mat_alloc(ctx
, 3, dim
);
428 isl_int_set_si(T
->row
[0][0], 1);
429 isl_seq_clr(T
->row
[0]+1, dim
- 1);
430 isl_seq_cpy(T
->row
[1], facet
, dim
);
431 isl_seq_cpy(T
->row
[2], ridge
, dim
);
432 T
= isl_mat_right_inverse(T
);
433 set
= isl_set_preimage(set
, T
);
437 lp
= wrap_constraints(set
);
438 obj
= isl_vec_alloc(ctx
, 1 + dim
*set
->n
);
441 isl_int_set_si(obj
->block
.data
[0], 0);
442 for (i
= 0; i
< set
->n
; ++i
) {
443 isl_seq_clr(obj
->block
.data
+ 1 + dim
*i
, 2);
444 isl_int_set_si(obj
->block
.data
[1 + dim
*i
+2], 1);
445 isl_seq_clr(obj
->block
.data
+ 1 + dim
*i
+3, dim
-3);
449 res
= isl_basic_set_solve_lp(lp
, 0,
450 obj
->block
.data
, ctx
->one
, &num
, &den
, NULL
);
451 if (res
== isl_lp_ok
) {
452 isl_int_neg(num
, num
);
453 isl_seq_combine(facet
, num
, facet
, den
, ridge
, dim
);
454 isl_seq_normalize(ctx
, facet
, dim
);
459 isl_basic_set_free(lp
);
461 if (res
== isl_lp_error
)
463 isl_assert(ctx
, res
== isl_lp_ok
|| res
== isl_lp_unbounded
,
467 isl_basic_set_free(lp
);
473 /* Compute the constraint of a facet of "set".
475 * We first compute the intersection with a bounding constraint
476 * that is orthogonal to one of the coordinate axes.
477 * If the affine hull of this intersection has only one equality,
478 * we have found a facet.
479 * Otherwise, we wrap the current bounding constraint around
480 * one of the equalities of the face (one that is not equal to
481 * the current bounding constraint).
482 * This process continues until we have found a facet.
483 * The dimension of the intersection increases by at least
484 * one on each iteration, so termination is guaranteed.
486 static __isl_give isl_mat
*initial_facet_constraint(__isl_keep isl_set
*set
)
488 struct isl_set
*slice
= NULL
;
489 struct isl_basic_set
*face
= NULL
;
491 unsigned dim
= isl_set_n_dim(set
);
493 isl_mat
*bounds
= NULL
;
495 isl_assert(set
->ctx
, set
->n
> 0, goto error
);
496 bounds
= isl_mat_alloc(set
->ctx
, 1, 1 + dim
);
500 isl_seq_clr(bounds
->row
[0], dim
);
501 isl_int_set_si(bounds
->row
[0][1 + dim
- 1], 1);
502 is_bound
= uset_is_bound(set
, bounds
->row
[0], 1 + dim
);
505 isl_assert(set
->ctx
, is_bound
, goto error
);
506 isl_seq_normalize(set
->ctx
, bounds
->row
[0], 1 + dim
);
510 slice
= isl_set_copy(set
);
511 slice
= isl_set_add_basic_set_equality(slice
, bounds
->row
[0]);
512 face
= isl_set_affine_hull(slice
);
515 if (face
->n_eq
== 1) {
516 isl_basic_set_free(face
);
519 for (i
= 0; i
< face
->n_eq
; ++i
)
520 if (!isl_seq_eq(bounds
->row
[0], face
->eq
[i
], 1 + dim
) &&
521 !isl_seq_is_neg(bounds
->row
[0],
522 face
->eq
[i
], 1 + dim
))
524 isl_assert(set
->ctx
, i
< face
->n_eq
, goto error
);
525 if (!isl_set_wrap_facet(set
, bounds
->row
[0], face
->eq
[i
]))
527 isl_seq_normalize(set
->ctx
, bounds
->row
[0], bounds
->n_col
);
528 isl_basic_set_free(face
);
533 isl_basic_set_free(face
);
534 isl_mat_free(bounds
);
538 /* Given the bounding constraint "c" of a facet of the convex hull of "set",
539 * compute a hyperplane description of the facet, i.e., compute the facets
542 * We compute an affine transformation that transforms the constraint
551 * by computing the right inverse U of a matrix that starts with the rows
564 * Since z_1 is zero, we can drop this variable as well as the corresponding
565 * column of U to obtain
573 * with Q' equal to Q, but without the corresponding row.
574 * After computing the facets of the facet in the z' space,
575 * we convert them back to the x space through Q.
577 static struct isl_basic_set
*compute_facet(struct isl_set
*set
, isl_int
*c
)
579 struct isl_mat
*m
, *U
, *Q
;
580 struct isl_basic_set
*facet
= NULL
;
585 set
= isl_set_copy(set
);
586 dim
= isl_set_n_dim(set
);
587 m
= isl_mat_alloc(set
->ctx
, 2, 1 + dim
);
590 isl_int_set_si(m
->row
[0][0], 1);
591 isl_seq_clr(m
->row
[0]+1, dim
);
592 isl_seq_cpy(m
->row
[1], c
, 1+dim
);
593 U
= isl_mat_right_inverse(m
);
594 Q
= isl_mat_right_inverse(isl_mat_copy(U
));
595 U
= isl_mat_drop_cols(U
, 1, 1);
596 Q
= isl_mat_drop_rows(Q
, 1, 1);
597 set
= isl_set_preimage(set
, U
);
598 facet
= uset_convex_hull_wrap_bounded(set
);
599 facet
= isl_basic_set_preimage(facet
, Q
);
600 if (facet
&& facet
->n_eq
!= 0)
601 isl_die(ctx
, isl_error_internal
, "unexpected equality",
602 return isl_basic_set_free(facet
));
605 isl_basic_set_free(facet
);
610 /* Given an initial facet constraint, compute the remaining facets.
611 * We do this by running through all facets found so far and computing
612 * the adjacent facets through wrapping, adding those facets that we
613 * hadn't already found before.
615 * For each facet we have found so far, we first compute its facets
616 * in the resulting convex hull. That is, we compute the ridges
617 * of the resulting convex hull contained in the facet.
618 * We also compute the corresponding facet in the current approximation
619 * of the convex hull. There is no need to wrap around the ridges
620 * in this facet since that would result in a facet that is already
621 * present in the current approximation.
623 * This function can still be significantly optimized by checking which of
624 * the facets of the basic sets are also facets of the convex hull and
625 * using all the facets so far to help in constructing the facets of the
628 * using the technique in section "3.1 Ridge Generation" of
629 * "Extended Convex Hull" by Fukuda et al.
631 static struct isl_basic_set
*extend(struct isl_basic_set
*hull
,
636 struct isl_basic_set
*facet
= NULL
;
637 struct isl_basic_set
*hull_facet
= NULL
;
643 isl_assert(set
->ctx
, set
->n
> 0, goto error
);
645 dim
= isl_set_n_dim(set
);
647 for (i
= 0; i
< hull
->n_ineq
; ++i
) {
648 facet
= compute_facet(set
, hull
->ineq
[i
]);
649 facet
= isl_basic_set_add_equality(facet
, hull
->ineq
[i
]);
650 facet
= isl_basic_set_gauss(facet
, NULL
);
651 facet
= isl_basic_set_normalize_constraints(facet
);
652 hull_facet
= isl_basic_set_copy(hull
);
653 hull_facet
= isl_basic_set_add_equality(hull_facet
, hull
->ineq
[i
]);
654 hull_facet
= isl_basic_set_gauss(hull_facet
, NULL
);
655 hull_facet
= isl_basic_set_normalize_constraints(hull_facet
);
656 if (!facet
|| !hull_facet
)
658 hull
= isl_basic_set_cow(hull
);
659 hull
= isl_basic_set_extend_space(hull
,
660 isl_space_copy(hull
->dim
), 0, 0, facet
->n_ineq
);
663 for (j
= 0; j
< facet
->n_ineq
; ++j
) {
664 for (f
= 0; f
< hull_facet
->n_ineq
; ++f
)
665 if (isl_seq_eq(facet
->ineq
[j
],
666 hull_facet
->ineq
[f
], 1 + dim
))
668 if (f
< hull_facet
->n_ineq
)
670 k
= isl_basic_set_alloc_inequality(hull
);
673 isl_seq_cpy(hull
->ineq
[k
], hull
->ineq
[i
], 1+dim
);
674 if (!isl_set_wrap_facet(set
, hull
->ineq
[k
], facet
->ineq
[j
]))
677 isl_basic_set_free(hull_facet
);
678 isl_basic_set_free(facet
);
680 hull
= isl_basic_set_simplify(hull
);
681 hull
= isl_basic_set_finalize(hull
);
684 isl_basic_set_free(hull_facet
);
685 isl_basic_set_free(facet
);
686 isl_basic_set_free(hull
);
690 /* Special case for computing the convex hull of a one dimensional set.
691 * We simply collect the lower and upper bounds of each basic set
692 * and the biggest of those.
694 static struct isl_basic_set
*convex_hull_1d(struct isl_set
*set
)
696 struct isl_mat
*c
= NULL
;
697 isl_int
*lower
= NULL
;
698 isl_int
*upper
= NULL
;
701 struct isl_basic_set
*hull
;
703 for (i
= 0; i
< set
->n
; ++i
) {
704 set
->p
[i
] = isl_basic_set_simplify(set
->p
[i
]);
708 set
= isl_set_remove_empty_parts(set
);
711 isl_assert(set
->ctx
, set
->n
> 0, goto error
);
712 c
= isl_mat_alloc(set
->ctx
, 2, 2);
716 if (set
->p
[0]->n_eq
> 0) {
717 isl_assert(set
->ctx
, set
->p
[0]->n_eq
== 1, goto error
);
720 if (isl_int_is_pos(set
->p
[0]->eq
[0][1])) {
721 isl_seq_cpy(lower
, set
->p
[0]->eq
[0], 2);
722 isl_seq_neg(upper
, set
->p
[0]->eq
[0], 2);
724 isl_seq_neg(lower
, set
->p
[0]->eq
[0], 2);
725 isl_seq_cpy(upper
, set
->p
[0]->eq
[0], 2);
728 for (j
= 0; j
< set
->p
[0]->n_ineq
; ++j
) {
729 if (isl_int_is_pos(set
->p
[0]->ineq
[j
][1])) {
731 isl_seq_cpy(lower
, set
->p
[0]->ineq
[j
], 2);
734 isl_seq_cpy(upper
, set
->p
[0]->ineq
[j
], 2);
741 for (i
= 0; i
< set
->n
; ++i
) {
742 struct isl_basic_set
*bset
= set
->p
[i
];
746 for (j
= 0; j
< bset
->n_eq
; ++j
) {
750 isl_int_mul(a
, lower
[0], bset
->eq
[j
][1]);
751 isl_int_mul(b
, lower
[1], bset
->eq
[j
][0]);
752 if (isl_int_lt(a
, b
) && isl_int_is_pos(bset
->eq
[j
][1]))
753 isl_seq_cpy(lower
, bset
->eq
[j
], 2);
754 if (isl_int_gt(a
, b
) && isl_int_is_neg(bset
->eq
[j
][1]))
755 isl_seq_neg(lower
, bset
->eq
[j
], 2);
758 isl_int_mul(a
, upper
[0], bset
->eq
[j
][1]);
759 isl_int_mul(b
, upper
[1], bset
->eq
[j
][0]);
760 if (isl_int_lt(a
, b
) && isl_int_is_pos(bset
->eq
[j
][1]))
761 isl_seq_neg(upper
, bset
->eq
[j
], 2);
762 if (isl_int_gt(a
, b
) && isl_int_is_neg(bset
->eq
[j
][1]))
763 isl_seq_cpy(upper
, bset
->eq
[j
], 2);
766 for (j
= 0; j
< bset
->n_ineq
; ++j
) {
767 if (isl_int_is_pos(bset
->ineq
[j
][1]))
769 if (isl_int_is_neg(bset
->ineq
[j
][1]))
771 if (lower
&& isl_int_is_pos(bset
->ineq
[j
][1])) {
772 isl_int_mul(a
, lower
[0], bset
->ineq
[j
][1]);
773 isl_int_mul(b
, lower
[1], bset
->ineq
[j
][0]);
774 if (isl_int_lt(a
, b
))
775 isl_seq_cpy(lower
, bset
->ineq
[j
], 2);
777 if (upper
&& isl_int_is_neg(bset
->ineq
[j
][1])) {
778 isl_int_mul(a
, upper
[0], bset
->ineq
[j
][1]);
779 isl_int_mul(b
, upper
[1], bset
->ineq
[j
][0]);
780 if (isl_int_gt(a
, b
))
781 isl_seq_cpy(upper
, bset
->ineq
[j
], 2);
792 hull
= isl_basic_set_alloc(set
->ctx
, 0, 1, 0, 0, 2);
793 hull
= isl_basic_set_set_rational(hull
);
797 k
= isl_basic_set_alloc_inequality(hull
);
798 isl_seq_cpy(hull
->ineq
[k
], lower
, 2);
801 k
= isl_basic_set_alloc_inequality(hull
);
802 isl_seq_cpy(hull
->ineq
[k
], upper
, 2);
804 hull
= isl_basic_set_finalize(hull
);
814 static struct isl_basic_set
*convex_hull_0d(struct isl_set
*set
)
816 struct isl_basic_set
*convex_hull
;
821 if (isl_set_is_empty(set
))
822 convex_hull
= isl_basic_set_empty(isl_space_copy(set
->dim
));
824 convex_hull
= isl_basic_set_universe(isl_space_copy(set
->dim
));
829 /* Compute the convex hull of a pair of basic sets without any parameters or
830 * integer divisions using Fourier-Motzkin elimination.
831 * The convex hull is the set of all points that can be written as
832 * the sum of points from both basic sets (in homogeneous coordinates).
833 * We set up the constraints in a space with dimensions for each of
834 * the three sets and then project out the dimensions corresponding
835 * to the two original basic sets, retaining only those corresponding
836 * to the convex hull.
838 static struct isl_basic_set
*convex_hull_pair_elim(struct isl_basic_set
*bset1
,
839 struct isl_basic_set
*bset2
)
842 struct isl_basic_set
*bset
[2];
843 struct isl_basic_set
*hull
= NULL
;
846 if (!bset1
|| !bset2
)
849 dim
= isl_basic_set_n_dim(bset1
);
850 hull
= isl_basic_set_alloc(bset1
->ctx
, 0, 2 + 3 * dim
, 0,
851 1 + dim
+ bset1
->n_eq
+ bset2
->n_eq
,
852 2 + bset1
->n_ineq
+ bset2
->n_ineq
);
855 for (i
= 0; i
< 2; ++i
) {
856 for (j
= 0; j
< bset
[i
]->n_eq
; ++j
) {
857 k
= isl_basic_set_alloc_equality(hull
);
860 isl_seq_clr(hull
->eq
[k
], (i
+1) * (1+dim
));
861 isl_seq_clr(hull
->eq
[k
]+(i
+2)*(1+dim
), (1-i
)*(1+dim
));
862 isl_seq_cpy(hull
->eq
[k
]+(i
+1)*(1+dim
), bset
[i
]->eq
[j
],
865 for (j
= 0; j
< bset
[i
]->n_ineq
; ++j
) {
866 k
= isl_basic_set_alloc_inequality(hull
);
869 isl_seq_clr(hull
->ineq
[k
], (i
+1) * (1+dim
));
870 isl_seq_clr(hull
->ineq
[k
]+(i
+2)*(1+dim
), (1-i
)*(1+dim
));
871 isl_seq_cpy(hull
->ineq
[k
]+(i
+1)*(1+dim
),
872 bset
[i
]->ineq
[j
], 1+dim
);
874 k
= isl_basic_set_alloc_inequality(hull
);
877 isl_seq_clr(hull
->ineq
[k
], 1+2+3*dim
);
878 isl_int_set_si(hull
->ineq
[k
][(i
+1)*(1+dim
)], 1);
880 for (j
= 0; j
< 1+dim
; ++j
) {
881 k
= isl_basic_set_alloc_equality(hull
);
884 isl_seq_clr(hull
->eq
[k
], 1+2+3*dim
);
885 isl_int_set_si(hull
->eq
[k
][j
], -1);
886 isl_int_set_si(hull
->eq
[k
][1+dim
+j
], 1);
887 isl_int_set_si(hull
->eq
[k
][2*(1+dim
)+j
], 1);
889 hull
= isl_basic_set_set_rational(hull
);
890 hull
= isl_basic_set_remove_dims(hull
, isl_dim_set
, dim
, 2*(1+dim
));
891 hull
= isl_basic_set_remove_redundancies(hull
);
892 isl_basic_set_free(bset1
);
893 isl_basic_set_free(bset2
);
896 isl_basic_set_free(bset1
);
897 isl_basic_set_free(bset2
);
898 isl_basic_set_free(hull
);
902 /* Is the set bounded for each value of the parameters?
904 int isl_basic_set_is_bounded(__isl_keep isl_basic_set
*bset
)
911 if (isl_basic_set_plain_is_empty(bset
))
914 tab
= isl_tab_from_recession_cone(bset
, 1);
915 bounded
= isl_tab_cone_is_bounded(tab
);
920 /* Is the image bounded for each value of the parameters and
921 * the domain variables?
923 int isl_basic_map_image_is_bounded(__isl_keep isl_basic_map
*bmap
)
925 unsigned nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
926 unsigned n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
929 bmap
= isl_basic_map_copy(bmap
);
930 bmap
= isl_basic_map_cow(bmap
);
931 bmap
= isl_basic_map_move_dims(bmap
, isl_dim_param
, nparam
,
932 isl_dim_in
, 0, n_in
);
933 bounded
= isl_basic_set_is_bounded((isl_basic_set
*)bmap
);
934 isl_basic_map_free(bmap
);
939 /* Is the set bounded for each value of the parameters?
941 int isl_set_is_bounded(__isl_keep isl_set
*set
)
948 for (i
= 0; i
< set
->n
; ++i
) {
949 int bounded
= isl_basic_set_is_bounded(set
->p
[i
]);
950 if (!bounded
|| bounded
< 0)
956 /* Compute the lineality space of the convex hull of bset1 and bset2.
958 * We first compute the intersection of the recession cone of bset1
959 * with the negative of the recession cone of bset2 and then compute
960 * the linear hull of the resulting cone.
962 static struct isl_basic_set
*induced_lineality_space(
963 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
966 struct isl_basic_set
*lin
= NULL
;
969 if (!bset1
|| !bset2
)
972 dim
= isl_basic_set_total_dim(bset1
);
973 lin
= isl_basic_set_alloc_space(isl_basic_set_get_space(bset1
), 0,
974 bset1
->n_eq
+ bset2
->n_eq
,
975 bset1
->n_ineq
+ bset2
->n_ineq
);
976 lin
= isl_basic_set_set_rational(lin
);
979 for (i
= 0; i
< bset1
->n_eq
; ++i
) {
980 k
= isl_basic_set_alloc_equality(lin
);
983 isl_int_set_si(lin
->eq
[k
][0], 0);
984 isl_seq_cpy(lin
->eq
[k
] + 1, bset1
->eq
[i
] + 1, dim
);
986 for (i
= 0; i
< bset1
->n_ineq
; ++i
) {
987 k
= isl_basic_set_alloc_inequality(lin
);
990 isl_int_set_si(lin
->ineq
[k
][0], 0);
991 isl_seq_cpy(lin
->ineq
[k
] + 1, bset1
->ineq
[i
] + 1, dim
);
993 for (i
= 0; i
< bset2
->n_eq
; ++i
) {
994 k
= isl_basic_set_alloc_equality(lin
);
997 isl_int_set_si(lin
->eq
[k
][0], 0);
998 isl_seq_neg(lin
->eq
[k
] + 1, bset2
->eq
[i
] + 1, dim
);
1000 for (i
= 0; i
< bset2
->n_ineq
; ++i
) {
1001 k
= isl_basic_set_alloc_inequality(lin
);
1004 isl_int_set_si(lin
->ineq
[k
][0], 0);
1005 isl_seq_neg(lin
->ineq
[k
] + 1, bset2
->ineq
[i
] + 1, dim
);
1008 isl_basic_set_free(bset1
);
1009 isl_basic_set_free(bset2
);
1010 return isl_basic_set_affine_hull(lin
);
1012 isl_basic_set_free(lin
);
1013 isl_basic_set_free(bset1
);
1014 isl_basic_set_free(bset2
);
1018 static struct isl_basic_set
*uset_convex_hull(struct isl_set
*set
);
1020 /* Given a set and a linear space "lin" of dimension n > 0,
1021 * project the linear space from the set, compute the convex hull
1022 * and then map the set back to the original space.
1028 * describe the linear space. We first compute the Hermite normal
1029 * form H = M U of M = H Q, to obtain
1033 * The last n rows of H will be zero, so the last n variables of x' = Q x
1034 * are the one we want to project out. We do this by transforming each
1035 * basic set A x >= b to A U x' >= b and then removing the last n dimensions.
1036 * After computing the convex hull in x'_1, i.e., A' x'_1 >= b',
1037 * we transform the hull back to the original space as A' Q_1 x >= b',
1038 * with Q_1 all but the last n rows of Q.
1040 static struct isl_basic_set
*modulo_lineality(struct isl_set
*set
,
1041 struct isl_basic_set
*lin
)
1043 unsigned total
= isl_basic_set_total_dim(lin
);
1045 struct isl_basic_set
*hull
;
1046 struct isl_mat
*M
, *U
, *Q
;
1050 lin_dim
= total
- lin
->n_eq
;
1051 M
= isl_mat_sub_alloc6(set
->ctx
, lin
->eq
, 0, lin
->n_eq
, 1, total
);
1052 M
= isl_mat_left_hermite(M
, 0, &U
, &Q
);
1056 isl_basic_set_free(lin
);
1058 Q
= isl_mat_drop_rows(Q
, Q
->n_row
- lin_dim
, lin_dim
);
1060 U
= isl_mat_lin_to_aff(U
);
1061 Q
= isl_mat_lin_to_aff(Q
);
1063 set
= isl_set_preimage(set
, U
);
1064 set
= isl_set_remove_dims(set
, isl_dim_set
, total
- lin_dim
, lin_dim
);
1065 hull
= uset_convex_hull(set
);
1066 hull
= isl_basic_set_preimage(hull
, Q
);
1070 isl_basic_set_free(lin
);
1075 /* Given two polyhedra with as constraints h_{ij} x >= 0 in homegeneous space,
1076 * set up an LP for solving
1078 * \sum_j \alpha_{1j} h_{1j} = \sum_j \alpha_{2j} h_{2j}
1080 * \alpha{i0} corresponds to the (implicit) positivity constraint 1 >= 0
1081 * The next \alpha{ij} correspond to the equalities and come in pairs.
1082 * The final \alpha{ij} correspond to the inequalities.
1084 static struct isl_basic_set
*valid_direction_lp(
1085 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
1088 struct isl_basic_set
*lp
;
1093 if (!bset1
|| !bset2
)
1095 d
= 1 + isl_basic_set_total_dim(bset1
);
1097 2 * bset1
->n_eq
+ bset1
->n_ineq
+ 2 * bset2
->n_eq
+ bset2
->n_ineq
;
1098 dim
= isl_space_set_alloc(bset1
->ctx
, 0, n
);
1099 lp
= isl_basic_set_alloc_space(dim
, 0, d
, n
);
1102 for (i
= 0; i
< n
; ++i
) {
1103 k
= isl_basic_set_alloc_inequality(lp
);
1106 isl_seq_clr(lp
->ineq
[k
] + 1, n
);
1107 isl_int_set_si(lp
->ineq
[k
][0], -1);
1108 isl_int_set_si(lp
->ineq
[k
][1 + i
], 1);
1110 for (i
= 0; i
< d
; ++i
) {
1111 k
= isl_basic_set_alloc_equality(lp
);
1115 isl_int_set_si(lp
->eq
[k
][n
], 0); n
++;
1116 /* positivity constraint 1 >= 0 */
1117 isl_int_set_si(lp
->eq
[k
][n
], i
== 0); n
++;
1118 for (j
= 0; j
< bset1
->n_eq
; ++j
) {
1119 isl_int_set(lp
->eq
[k
][n
], bset1
->eq
[j
][i
]); n
++;
1120 isl_int_neg(lp
->eq
[k
][n
], bset1
->eq
[j
][i
]); n
++;
1122 for (j
= 0; j
< bset1
->n_ineq
; ++j
) {
1123 isl_int_set(lp
->eq
[k
][n
], bset1
->ineq
[j
][i
]); n
++;
1125 /* positivity constraint 1 >= 0 */
1126 isl_int_set_si(lp
->eq
[k
][n
], -(i
== 0)); n
++;
1127 for (j
= 0; j
< bset2
->n_eq
; ++j
) {
1128 isl_int_neg(lp
->eq
[k
][n
], bset2
->eq
[j
][i
]); n
++;
1129 isl_int_set(lp
->eq
[k
][n
], bset2
->eq
[j
][i
]); n
++;
1131 for (j
= 0; j
< bset2
->n_ineq
; ++j
) {
1132 isl_int_neg(lp
->eq
[k
][n
], bset2
->ineq
[j
][i
]); n
++;
1135 lp
= isl_basic_set_gauss(lp
, NULL
);
1136 isl_basic_set_free(bset1
);
1137 isl_basic_set_free(bset2
);
1140 isl_basic_set_free(bset1
);
1141 isl_basic_set_free(bset2
);
1145 /* Compute a vector s in the homogeneous space such that <s, r> > 0
1146 * for all rays in the homogeneous space of the two cones that correspond
1147 * to the input polyhedra bset1 and bset2.
1149 * We compute s as a vector that satisfies
1151 * s = \sum_j \alpha_{ij} h_{ij} for i = 1,2 (*)
1153 * with h_{ij} the normals of the facets of polyhedron i
1154 * (including the "positivity constraint" 1 >= 0) and \alpha_{ij}
1155 * strictly positive numbers. For simplicity we impose \alpha_{ij} >= 1.
1156 * We first set up an LP with as variables the \alpha{ij}.
1157 * In this formulation, for each polyhedron i,
1158 * the first constraint is the positivity constraint, followed by pairs
1159 * of variables for the equalities, followed by variables for the inequalities.
1160 * We then simply pick a feasible solution and compute s using (*).
1162 * Note that we simply pick any valid direction and make no attempt
1163 * to pick a "good" or even the "best" valid direction.
1165 static struct isl_vec
*valid_direction(
1166 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
1168 struct isl_basic_set
*lp
;
1169 struct isl_tab
*tab
;
1170 struct isl_vec
*sample
= NULL
;
1171 struct isl_vec
*dir
;
1176 if (!bset1
|| !bset2
)
1178 lp
= valid_direction_lp(isl_basic_set_copy(bset1
),
1179 isl_basic_set_copy(bset2
));
1180 tab
= isl_tab_from_basic_set(lp
, 0);
1181 sample
= isl_tab_get_sample_value(tab
);
1183 isl_basic_set_free(lp
);
1186 d
= isl_basic_set_total_dim(bset1
);
1187 dir
= isl_vec_alloc(bset1
->ctx
, 1 + d
);
1190 isl_seq_clr(dir
->block
.data
+ 1, dir
->size
- 1);
1192 /* positivity constraint 1 >= 0 */
1193 isl_int_set(dir
->block
.data
[0], sample
->block
.data
[n
]); n
++;
1194 for (i
= 0; i
< bset1
->n_eq
; ++i
) {
1195 isl_int_sub(sample
->block
.data
[n
],
1196 sample
->block
.data
[n
], sample
->block
.data
[n
+1]);
1197 isl_seq_combine(dir
->block
.data
,
1198 bset1
->ctx
->one
, dir
->block
.data
,
1199 sample
->block
.data
[n
], bset1
->eq
[i
], 1 + d
);
1203 for (i
= 0; i
< bset1
->n_ineq
; ++i
)
1204 isl_seq_combine(dir
->block
.data
,
1205 bset1
->ctx
->one
, dir
->block
.data
,
1206 sample
->block
.data
[n
++], bset1
->ineq
[i
], 1 + d
);
1207 isl_vec_free(sample
);
1208 isl_seq_normalize(bset1
->ctx
, dir
->el
, dir
->size
);
1209 isl_basic_set_free(bset1
);
1210 isl_basic_set_free(bset2
);
1213 isl_vec_free(sample
);
1214 isl_basic_set_free(bset1
);
1215 isl_basic_set_free(bset2
);
1219 /* Given a polyhedron b_i + A_i x >= 0 and a map T = S^{-1},
1220 * compute b_i' + A_i' x' >= 0, with
1222 * [ b_i A_i ] [ y' ] [ y' ]
1223 * [ 1 0 ] S^{-1} [ x' ] >= 0 or [ b_i' A_i' ] [ x' ] >= 0
1225 * In particular, add the "positivity constraint" and then perform
1228 static struct isl_basic_set
*homogeneous_map(struct isl_basic_set
*bset
,
1235 bset
= isl_basic_set_extend_constraints(bset
, 0, 1);
1236 k
= isl_basic_set_alloc_inequality(bset
);
1239 isl_seq_clr(bset
->ineq
[k
] + 1, isl_basic_set_total_dim(bset
));
1240 isl_int_set_si(bset
->ineq
[k
][0], 1);
1241 bset
= isl_basic_set_preimage(bset
, T
);
1245 isl_basic_set_free(bset
);
1249 /* Compute the convex hull of a pair of basic sets without any parameters or
1250 * integer divisions, where the convex hull is known to be pointed,
1251 * but the basic sets may be unbounded.
1253 * We turn this problem into the computation of a convex hull of a pair
1254 * _bounded_ polyhedra by "changing the direction of the homogeneous
1255 * dimension". This idea is due to Matthias Koeppe.
1257 * Consider the cones in homogeneous space that correspond to the
1258 * input polyhedra. The rays of these cones are also rays of the
1259 * polyhedra if the coordinate that corresponds to the homogeneous
1260 * dimension is zero. That is, if the inner product of the rays
1261 * with the homogeneous direction is zero.
1262 * The cones in the homogeneous space can also be considered to
1263 * correspond to other pairs of polyhedra by chosing a different
1264 * homogeneous direction. To ensure that both of these polyhedra
1265 * are bounded, we need to make sure that all rays of the cones
1266 * correspond to vertices and not to rays.
1267 * Let s be a direction such that <s, r> > 0 for all rays r of both cones.
1268 * Then using s as a homogeneous direction, we obtain a pair of polytopes.
1269 * The vector s is computed in valid_direction.
1271 * Note that we need to consider _all_ rays of the cones and not just
1272 * the rays that correspond to rays in the polyhedra. If we were to
1273 * only consider those rays and turn them into vertices, then we
1274 * may inadvertently turn some vertices into rays.
1276 * The standard homogeneous direction is the unit vector in the 0th coordinate.
1277 * We therefore transform the two polyhedra such that the selected
1278 * direction is mapped onto this standard direction and then proceed
1279 * with the normal computation.
1280 * Let S be a non-singular square matrix with s as its first row,
1281 * then we want to map the polyhedra to the space
1283 * [ y' ] [ y ] [ y ] [ y' ]
1284 * [ x' ] = S [ x ] i.e., [ x ] = S^{-1} [ x' ]
1286 * We take S to be the unimodular completion of s to limit the growth
1287 * of the coefficients in the following computations.
1289 * Let b_i + A_i x >= 0 be the constraints of polyhedron i.
1290 * We first move to the homogeneous dimension
1292 * b_i y + A_i x >= 0 [ b_i A_i ] [ y ] [ 0 ]
1293 * y >= 0 or [ 1 0 ] [ x ] >= [ 0 ]
1295 * Then we change directoin
1297 * [ b_i A_i ] [ y' ] [ y' ]
1298 * [ 1 0 ] S^{-1} [ x' ] >= 0 or [ b_i' A_i' ] [ x' ] >= 0
1300 * Then we compute the convex hull of the polytopes b_i' + A_i' x' >= 0
1301 * resulting in b' + A' x' >= 0, which we then convert back
1304 * [ b' A' ] S [ x ] >= 0 or [ b A ] [ x ] >= 0
1306 * The polyhedron b + A x >= 0 is then the convex hull of the input polyhedra.
1308 static struct isl_basic_set
*convex_hull_pair_pointed(
1309 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
1311 struct isl_ctx
*ctx
= NULL
;
1312 struct isl_vec
*dir
= NULL
;
1313 struct isl_mat
*T
= NULL
;
1314 struct isl_mat
*T2
= NULL
;
1315 struct isl_basic_set
*hull
;
1316 struct isl_set
*set
;
1318 if (!bset1
|| !bset2
)
1320 ctx
= isl_basic_set_get_ctx(bset1
);
1321 dir
= valid_direction(isl_basic_set_copy(bset1
),
1322 isl_basic_set_copy(bset2
));
1325 T
= isl_mat_alloc(ctx
, dir
->size
, dir
->size
);
1328 isl_seq_cpy(T
->row
[0], dir
->block
.data
, dir
->size
);
1329 T
= isl_mat_unimodular_complete(T
, 1);
1330 T2
= isl_mat_right_inverse(isl_mat_copy(T
));
1332 bset1
= homogeneous_map(bset1
, isl_mat_copy(T2
));
1333 bset2
= homogeneous_map(bset2
, T2
);
1334 set
= isl_set_alloc_space(isl_basic_set_get_space(bset1
), 2, 0);
1335 set
= isl_set_add_basic_set(set
, bset1
);
1336 set
= isl_set_add_basic_set(set
, bset2
);
1337 hull
= uset_convex_hull(set
);
1338 hull
= isl_basic_set_preimage(hull
, T
);
1345 isl_basic_set_free(bset1
);
1346 isl_basic_set_free(bset2
);
1350 static struct isl_basic_set
*uset_convex_hull_wrap(struct isl_set
*set
);
1351 static struct isl_basic_set
*modulo_affine_hull(
1352 struct isl_set
*set
, struct isl_basic_set
*affine_hull
);
1354 /* Compute the convex hull of a pair of basic sets without any parameters or
1355 * integer divisions.
1357 * This function is called from uset_convex_hull_unbounded, which
1358 * means that the complete convex hull is unbounded. Some pairs
1359 * of basic sets may still be bounded, though.
1360 * They may even lie inside a lower dimensional space, in which
1361 * case they need to be handled inside their affine hull since
1362 * the main algorithm assumes that the result is full-dimensional.
1364 * If the convex hull of the two basic sets would have a non-trivial
1365 * lineality space, we first project out this lineality space.
1367 static struct isl_basic_set
*convex_hull_pair(struct isl_basic_set
*bset1
,
1368 struct isl_basic_set
*bset2
)
1370 isl_basic_set
*lin
, *aff
;
1371 int bounded1
, bounded2
;
1373 if (bset1
->ctx
->opt
->convex
== ISL_CONVEX_HULL_FM
)
1374 return convex_hull_pair_elim(bset1
, bset2
);
1376 aff
= isl_set_affine_hull(isl_basic_set_union(isl_basic_set_copy(bset1
),
1377 isl_basic_set_copy(bset2
)));
1381 return modulo_affine_hull(isl_basic_set_union(bset1
, bset2
), aff
);
1382 isl_basic_set_free(aff
);
1384 bounded1
= isl_basic_set_is_bounded(bset1
);
1385 bounded2
= isl_basic_set_is_bounded(bset2
);
1387 if (bounded1
< 0 || bounded2
< 0)
1390 if (bounded1
&& bounded2
)
1391 return uset_convex_hull_wrap(isl_basic_set_union(bset1
, bset2
));
1393 if (bounded1
|| bounded2
)
1394 return convex_hull_pair_pointed(bset1
, bset2
);
1396 lin
= induced_lineality_space(isl_basic_set_copy(bset1
),
1397 isl_basic_set_copy(bset2
));
1400 if (isl_basic_set_plain_is_universe(lin
)) {
1401 isl_basic_set_free(bset1
);
1402 isl_basic_set_free(bset2
);
1405 if (lin
->n_eq
< isl_basic_set_total_dim(lin
)) {
1406 struct isl_set
*set
;
1407 set
= isl_set_alloc_space(isl_basic_set_get_space(bset1
), 2, 0);
1408 set
= isl_set_add_basic_set(set
, bset1
);
1409 set
= isl_set_add_basic_set(set
, bset2
);
1410 return modulo_lineality(set
, lin
);
1412 isl_basic_set_free(lin
);
1414 return convex_hull_pair_pointed(bset1
, bset2
);
1416 isl_basic_set_free(bset1
);
1417 isl_basic_set_free(bset2
);
1421 /* Compute the lineality space of a basic set.
1422 * We currently do not allow the basic set to have any divs.
1423 * We basically just drop the constants and turn every inequality
1426 struct isl_basic_set
*isl_basic_set_lineality_space(struct isl_basic_set
*bset
)
1429 struct isl_basic_set
*lin
= NULL
;
1434 isl_assert(bset
->ctx
, bset
->n_div
== 0, goto error
);
1435 dim
= isl_basic_set_total_dim(bset
);
1437 lin
= isl_basic_set_alloc_space(isl_basic_set_get_space(bset
), 0, dim
, 0);
1440 for (i
= 0; i
< bset
->n_eq
; ++i
) {
1441 k
= isl_basic_set_alloc_equality(lin
);
1444 isl_int_set_si(lin
->eq
[k
][0], 0);
1445 isl_seq_cpy(lin
->eq
[k
] + 1, bset
->eq
[i
] + 1, dim
);
1447 lin
= isl_basic_set_gauss(lin
, NULL
);
1450 for (i
= 0; i
< bset
->n_ineq
&& lin
->n_eq
< dim
; ++i
) {
1451 k
= isl_basic_set_alloc_equality(lin
);
1454 isl_int_set_si(lin
->eq
[k
][0], 0);
1455 isl_seq_cpy(lin
->eq
[k
] + 1, bset
->ineq
[i
] + 1, dim
);
1456 lin
= isl_basic_set_gauss(lin
, NULL
);
1460 isl_basic_set_free(bset
);
1463 isl_basic_set_free(lin
);
1464 isl_basic_set_free(bset
);
1468 /* Compute the (linear) hull of the lineality spaces of the basic sets in the
1469 * "underlying" set "set".
1471 static struct isl_basic_set
*uset_combined_lineality_space(struct isl_set
*set
)
1474 struct isl_set
*lin
= NULL
;
1479 isl_space
*dim
= isl_set_get_space(set
);
1481 return isl_basic_set_empty(dim
);
1484 lin
= isl_set_alloc_space(isl_set_get_space(set
), set
->n
, 0);
1485 for (i
= 0; i
< set
->n
; ++i
)
1486 lin
= isl_set_add_basic_set(lin
,
1487 isl_basic_set_lineality_space(isl_basic_set_copy(set
->p
[i
])));
1489 return isl_set_affine_hull(lin
);
1492 /* Compute the convex hull of a set without any parameters or
1493 * integer divisions.
1494 * In each step, we combined two basic sets until only one
1495 * basic set is left.
1496 * The input basic sets are assumed not to have a non-trivial
1497 * lineality space. If any of the intermediate results has
1498 * a non-trivial lineality space, it is projected out.
1500 static struct isl_basic_set
*uset_convex_hull_unbounded(struct isl_set
*set
)
1502 struct isl_basic_set
*convex_hull
= NULL
;
1504 convex_hull
= isl_set_copy_basic_set(set
);
1505 set
= isl_set_drop_basic_set(set
, convex_hull
);
1508 while (set
->n
> 0) {
1509 struct isl_basic_set
*t
;
1510 t
= isl_set_copy_basic_set(set
);
1513 set
= isl_set_drop_basic_set(set
, t
);
1516 convex_hull
= convex_hull_pair(convex_hull
, t
);
1519 t
= isl_basic_set_lineality_space(isl_basic_set_copy(convex_hull
));
1522 if (isl_basic_set_plain_is_universe(t
)) {
1523 isl_basic_set_free(convex_hull
);
1527 if (t
->n_eq
< isl_basic_set_total_dim(t
)) {
1528 set
= isl_set_add_basic_set(set
, convex_hull
);
1529 return modulo_lineality(set
, t
);
1531 isl_basic_set_free(t
);
1537 isl_basic_set_free(convex_hull
);
1541 /* Compute an initial hull for wrapping containing a single initial
1543 * This function assumes that the given set is bounded.
1545 static struct isl_basic_set
*initial_hull(struct isl_basic_set
*hull
,
1546 struct isl_set
*set
)
1548 struct isl_mat
*bounds
= NULL
;
1554 bounds
= initial_facet_constraint(set
);
1557 k
= isl_basic_set_alloc_inequality(hull
);
1560 dim
= isl_set_n_dim(set
);
1561 isl_assert(set
->ctx
, 1 + dim
== bounds
->n_col
, goto error
);
1562 isl_seq_cpy(hull
->ineq
[k
], bounds
->row
[0], bounds
->n_col
);
1563 isl_mat_free(bounds
);
1567 isl_basic_set_free(hull
);
1568 isl_mat_free(bounds
);
1572 struct max_constraint
{
1578 static int max_constraint_equal(const void *entry
, const void *val
)
1580 struct max_constraint
*a
= (struct max_constraint
*)entry
;
1581 isl_int
*b
= (isl_int
*)val
;
1583 return isl_seq_eq(a
->c
->row
[0] + 1, b
, a
->c
->n_col
- 1);
1586 static void update_constraint(struct isl_ctx
*ctx
, struct isl_hash_table
*table
,
1587 isl_int
*con
, unsigned len
, int n
, int ineq
)
1589 struct isl_hash_table_entry
*entry
;
1590 struct max_constraint
*c
;
1593 c_hash
= isl_seq_get_hash(con
+ 1, len
);
1594 entry
= isl_hash_table_find(ctx
, table
, c_hash
, max_constraint_equal
,
1600 isl_hash_table_remove(ctx
, table
, entry
);
1604 if (isl_int_gt(c
->c
->row
[0][0], con
[0]))
1606 if (isl_int_eq(c
->c
->row
[0][0], con
[0])) {
1611 c
->c
= isl_mat_cow(c
->c
);
1612 isl_int_set(c
->c
->row
[0][0], con
[0]);
1616 /* Check whether the constraint hash table "table" constains the constraint
1619 static int has_constraint(struct isl_ctx
*ctx
, struct isl_hash_table
*table
,
1620 isl_int
*con
, unsigned len
, int n
)
1622 struct isl_hash_table_entry
*entry
;
1623 struct max_constraint
*c
;
1626 c_hash
= isl_seq_get_hash(con
+ 1, len
);
1627 entry
= isl_hash_table_find(ctx
, table
, c_hash
, max_constraint_equal
,
1634 return isl_int_eq(c
->c
->row
[0][0], con
[0]);
1637 /* Check for inequality constraints of a basic set without equalities
1638 * such that the same or more stringent copies of the constraint appear
1639 * in all of the basic sets. Such constraints are necessarily facet
1640 * constraints of the convex hull.
1642 * If the resulting basic set is by chance identical to one of
1643 * the basic sets in "set", then we know that this basic set contains
1644 * all other basic sets and is therefore the convex hull of set.
1645 * In this case we set *is_hull to 1.
1647 static struct isl_basic_set
*common_constraints(struct isl_basic_set
*hull
,
1648 struct isl_set
*set
, int *is_hull
)
1651 int min_constraints
;
1653 struct max_constraint
*constraints
= NULL
;
1654 struct isl_hash_table
*table
= NULL
;
1659 for (i
= 0; i
< set
->n
; ++i
)
1660 if (set
->p
[i
]->n_eq
== 0)
1664 min_constraints
= set
->p
[i
]->n_ineq
;
1666 for (i
= best
+ 1; i
< set
->n
; ++i
) {
1667 if (set
->p
[i
]->n_eq
!= 0)
1669 if (set
->p
[i
]->n_ineq
>= min_constraints
)
1671 min_constraints
= set
->p
[i
]->n_ineq
;
1674 constraints
= isl_calloc_array(hull
->ctx
, struct max_constraint
,
1678 table
= isl_alloc_type(hull
->ctx
, struct isl_hash_table
);
1679 if (isl_hash_table_init(hull
->ctx
, table
, min_constraints
))
1682 total
= isl_space_dim(set
->dim
, isl_dim_all
);
1683 for (i
= 0; i
< set
->p
[best
]->n_ineq
; ++i
) {
1684 constraints
[i
].c
= isl_mat_sub_alloc6(hull
->ctx
,
1685 set
->p
[best
]->ineq
+ i
, 0, 1, 0, 1 + total
);
1686 if (!constraints
[i
].c
)
1688 constraints
[i
].ineq
= 1;
1690 for (i
= 0; i
< min_constraints
; ++i
) {
1691 struct isl_hash_table_entry
*entry
;
1693 c_hash
= isl_seq_get_hash(constraints
[i
].c
->row
[0] + 1, total
);
1694 entry
= isl_hash_table_find(hull
->ctx
, table
, c_hash
,
1695 max_constraint_equal
, constraints
[i
].c
->row
[0] + 1, 1);
1698 isl_assert(hull
->ctx
, !entry
->data
, goto error
);
1699 entry
->data
= &constraints
[i
];
1703 for (s
= 0; s
< set
->n
; ++s
) {
1707 for (i
= 0; i
< set
->p
[s
]->n_eq
; ++i
) {
1708 isl_int
*eq
= set
->p
[s
]->eq
[i
];
1709 for (j
= 0; j
< 2; ++j
) {
1710 isl_seq_neg(eq
, eq
, 1 + total
);
1711 update_constraint(hull
->ctx
, table
,
1715 for (i
= 0; i
< set
->p
[s
]->n_ineq
; ++i
) {
1716 isl_int
*ineq
= set
->p
[s
]->ineq
[i
];
1717 update_constraint(hull
->ctx
, table
, ineq
, total
, n
,
1718 set
->p
[s
]->n_eq
== 0);
1723 for (i
= 0; i
< min_constraints
; ++i
) {
1724 if (constraints
[i
].count
< n
)
1726 if (!constraints
[i
].ineq
)
1728 j
= isl_basic_set_alloc_inequality(hull
);
1731 isl_seq_cpy(hull
->ineq
[j
], constraints
[i
].c
->row
[0], 1 + total
);
1734 for (s
= 0; s
< set
->n
; ++s
) {
1735 if (set
->p
[s
]->n_eq
)
1737 if (set
->p
[s
]->n_ineq
!= hull
->n_ineq
)
1739 for (i
= 0; i
< set
->p
[s
]->n_ineq
; ++i
) {
1740 isl_int
*ineq
= set
->p
[s
]->ineq
[i
];
1741 if (!has_constraint(hull
->ctx
, table
, ineq
, total
, n
))
1744 if (i
== set
->p
[s
]->n_ineq
)
1748 isl_hash_table_clear(table
);
1749 for (i
= 0; i
< min_constraints
; ++i
)
1750 isl_mat_free(constraints
[i
].c
);
1755 isl_hash_table_clear(table
);
1758 for (i
= 0; i
< min_constraints
; ++i
)
1759 isl_mat_free(constraints
[i
].c
);
1764 /* Create a template for the convex hull of "set" and fill it up
1765 * obvious facet constraints, if any. If the result happens to
1766 * be the convex hull of "set" then *is_hull is set to 1.
1768 static struct isl_basic_set
*proto_hull(struct isl_set
*set
, int *is_hull
)
1770 struct isl_basic_set
*hull
;
1775 for (i
= 0; i
< set
->n
; ++i
) {
1776 n_ineq
+= set
->p
[i
]->n_eq
;
1777 n_ineq
+= set
->p
[i
]->n_ineq
;
1779 hull
= isl_basic_set_alloc_space(isl_space_copy(set
->dim
), 0, 0, n_ineq
);
1780 hull
= isl_basic_set_set_rational(hull
);
1783 return common_constraints(hull
, set
, is_hull
);
1786 static struct isl_basic_set
*uset_convex_hull_wrap(struct isl_set
*set
)
1788 struct isl_basic_set
*hull
;
1791 hull
= proto_hull(set
, &is_hull
);
1792 if (hull
&& !is_hull
) {
1793 if (hull
->n_ineq
== 0)
1794 hull
= initial_hull(hull
, set
);
1795 hull
= extend(hull
, set
);
1802 /* Compute the convex hull of a set without any parameters or
1803 * integer divisions. Depending on whether the set is bounded,
1804 * we pass control to the wrapping based convex hull or
1805 * the Fourier-Motzkin elimination based convex hull.
1806 * We also handle a few special cases before checking the boundedness.
1808 static struct isl_basic_set
*uset_convex_hull(struct isl_set
*set
)
1810 struct isl_basic_set
*convex_hull
= NULL
;
1811 struct isl_basic_set
*lin
;
1813 if (isl_set_n_dim(set
) == 0)
1814 return convex_hull_0d(set
);
1816 set
= isl_set_coalesce(set
);
1817 set
= isl_set_set_rational(set
);
1824 convex_hull
= isl_basic_set_copy(set
->p
[0]);
1828 if (isl_set_n_dim(set
) == 1)
1829 return convex_hull_1d(set
);
1831 if (isl_set_is_bounded(set
) &&
1832 set
->ctx
->opt
->convex
== ISL_CONVEX_HULL_WRAP
)
1833 return uset_convex_hull_wrap(set
);
1835 lin
= uset_combined_lineality_space(isl_set_copy(set
));
1838 if (isl_basic_set_plain_is_universe(lin
)) {
1842 if (lin
->n_eq
< isl_basic_set_total_dim(lin
))
1843 return modulo_lineality(set
, lin
);
1844 isl_basic_set_free(lin
);
1846 return uset_convex_hull_unbounded(set
);
1849 isl_basic_set_free(convex_hull
);
1853 /* This is the core procedure, where "set" is a "pure" set, i.e.,
1854 * without parameters or divs and where the convex hull of set is
1855 * known to be full-dimensional.
1857 static struct isl_basic_set
*uset_convex_hull_wrap_bounded(struct isl_set
*set
)
1859 struct isl_basic_set
*convex_hull
= NULL
;
1864 if (isl_set_n_dim(set
) == 0) {
1865 convex_hull
= isl_basic_set_universe(isl_space_copy(set
->dim
));
1867 convex_hull
= isl_basic_set_set_rational(convex_hull
);
1871 set
= isl_set_set_rational(set
);
1872 set
= isl_set_coalesce(set
);
1876 convex_hull
= isl_basic_set_copy(set
->p
[0]);
1878 convex_hull
= isl_basic_map_remove_redundancies(convex_hull
);
1881 if (isl_set_n_dim(set
) == 1)
1882 return convex_hull_1d(set
);
1884 return uset_convex_hull_wrap(set
);
1890 /* Compute the convex hull of set "set" with affine hull "affine_hull",
1891 * We first remove the equalities (transforming the set), compute the
1892 * convex hull of the transformed set and then add the equalities back
1893 * (after performing the inverse transformation.
1895 static struct isl_basic_set
*modulo_affine_hull(
1896 struct isl_set
*set
, struct isl_basic_set
*affine_hull
)
1900 struct isl_basic_set
*dummy
;
1901 struct isl_basic_set
*convex_hull
;
1903 dummy
= isl_basic_set_remove_equalities(
1904 isl_basic_set_copy(affine_hull
), &T
, &T2
);
1907 isl_basic_set_free(dummy
);
1908 set
= isl_set_preimage(set
, T
);
1909 convex_hull
= uset_convex_hull(set
);
1910 convex_hull
= isl_basic_set_preimage(convex_hull
, T2
);
1911 convex_hull
= isl_basic_set_intersect(convex_hull
, affine_hull
);
1914 isl_basic_set_free(affine_hull
);
1919 /* Return an empty basic map living in the same space as "map".
1921 static __isl_give isl_basic_map
*replace_map_by_empty_basic_map(
1922 __isl_take isl_map
*map
)
1926 space
= isl_map_get_space(map
);
1928 return isl_basic_map_empty(space
);
1931 /* Compute the convex hull of a map.
1933 * The implementation was inspired by "Extended Convex Hull" by Fukuda et al.,
1934 * specifically, the wrapping of facets to obtain new facets.
1936 struct isl_basic_map
*isl_map_convex_hull(struct isl_map
*map
)
1938 struct isl_basic_set
*bset
;
1939 struct isl_basic_map
*model
= NULL
;
1940 struct isl_basic_set
*affine_hull
= NULL
;
1941 struct isl_basic_map
*convex_hull
= NULL
;
1942 struct isl_set
*set
= NULL
;
1944 map
= isl_map_detect_equalities(map
);
1945 map
= isl_map_align_divs(map
);
1950 return replace_map_by_empty_basic_map(map
);
1952 model
= isl_basic_map_copy(map
->p
[0]);
1953 set
= isl_map_underlying_set(map
);
1957 affine_hull
= isl_set_affine_hull(isl_set_copy(set
));
1960 if (affine_hull
->n_eq
!= 0)
1961 bset
= modulo_affine_hull(set
, affine_hull
);
1963 isl_basic_set_free(affine_hull
);
1964 bset
= uset_convex_hull(set
);
1967 convex_hull
= isl_basic_map_overlying_set(bset
, model
);
1971 ISL_F_SET(convex_hull
, ISL_BASIC_MAP_NO_IMPLICIT
);
1972 ISL_F_SET(convex_hull
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1973 ISL_F_CLR(convex_hull
, ISL_BASIC_MAP_RATIONAL
);
1977 isl_basic_map_free(model
);
1981 struct isl_basic_set
*isl_set_convex_hull(struct isl_set
*set
)
1983 return (struct isl_basic_set
*)
1984 isl_map_convex_hull((struct isl_map
*)set
);
1987 __isl_give isl_basic_map
*isl_map_polyhedral_hull(__isl_take isl_map
*map
)
1989 isl_basic_map
*hull
;
1991 hull
= isl_map_convex_hull(map
);
1992 return isl_basic_map_remove_divs(hull
);
1995 __isl_give isl_basic_set
*isl_set_polyhedral_hull(__isl_take isl_set
*set
)
1997 return (isl_basic_set
*)isl_map_polyhedral_hull((isl_map
*)set
);
2000 struct sh_data_entry
{
2001 struct isl_hash_table
*table
;
2002 struct isl_tab
*tab
;
2005 /* Holds the data needed during the simple hull computation.
2007 * n the number of basic sets in the original set
2008 * hull_table a hash table of already computed constraints
2009 * in the simple hull
2010 * p for each basic set,
2011 * table a hash table of the constraints
2012 * tab the tableau corresponding to the basic set
2015 struct isl_ctx
*ctx
;
2017 struct isl_hash_table
*hull_table
;
2018 struct sh_data_entry p
[1];
2021 static void sh_data_free(struct sh_data
*data
)
2027 isl_hash_table_free(data
->ctx
, data
->hull_table
);
2028 for (i
= 0; i
< data
->n
; ++i
) {
2029 isl_hash_table_free(data
->ctx
, data
->p
[i
].table
);
2030 isl_tab_free(data
->p
[i
].tab
);
2035 struct ineq_cmp_data
{
2040 static int has_ineq(const void *entry
, const void *val
)
2042 isl_int
*row
= (isl_int
*)entry
;
2043 struct ineq_cmp_data
*v
= (struct ineq_cmp_data
*)val
;
2045 return isl_seq_eq(row
+ 1, v
->p
+ 1, v
->len
) ||
2046 isl_seq_is_neg(row
+ 1, v
->p
+ 1, v
->len
);
2049 static int hash_ineq(struct isl_ctx
*ctx
, struct isl_hash_table
*table
,
2050 isl_int
*ineq
, unsigned len
)
2053 struct ineq_cmp_data v
;
2054 struct isl_hash_table_entry
*entry
;
2058 c_hash
= isl_seq_get_hash(ineq
+ 1, len
);
2059 entry
= isl_hash_table_find(ctx
, table
, c_hash
, has_ineq
, &v
, 1);
2066 /* Fill hash table "table" with the constraints of "bset".
2067 * Equalities are added as two inequalities.
2068 * The value in the hash table is a pointer to the (in)equality of "bset".
2070 static int hash_basic_set(struct isl_hash_table
*table
,
2071 struct isl_basic_set
*bset
)
2074 unsigned dim
= isl_basic_set_total_dim(bset
);
2076 for (i
= 0; i
< bset
->n_eq
; ++i
) {
2077 for (j
= 0; j
< 2; ++j
) {
2078 isl_seq_neg(bset
->eq
[i
], bset
->eq
[i
], 1 + dim
);
2079 if (hash_ineq(bset
->ctx
, table
, bset
->eq
[i
], dim
) < 0)
2083 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
2084 if (hash_ineq(bset
->ctx
, table
, bset
->ineq
[i
], dim
) < 0)
2090 static struct sh_data
*sh_data_alloc(struct isl_set
*set
, unsigned n_ineq
)
2092 struct sh_data
*data
;
2095 data
= isl_calloc(set
->ctx
, struct sh_data
,
2096 sizeof(struct sh_data
) +
2097 (set
->n
- 1) * sizeof(struct sh_data_entry
));
2100 data
->ctx
= set
->ctx
;
2102 data
->hull_table
= isl_hash_table_alloc(set
->ctx
, n_ineq
);
2103 if (!data
->hull_table
)
2105 for (i
= 0; i
< set
->n
; ++i
) {
2106 data
->p
[i
].table
= isl_hash_table_alloc(set
->ctx
,
2107 2 * set
->p
[i
]->n_eq
+ set
->p
[i
]->n_ineq
);
2108 if (!data
->p
[i
].table
)
2110 if (hash_basic_set(data
->p
[i
].table
, set
->p
[i
]) < 0)
2119 /* Check if inequality "ineq" is a bound for basic set "j" or if
2120 * it can be relaxed (by increasing the constant term) to become
2121 * a bound for that basic set. In the latter case, the constant
2123 * Relaxation of the constant term is only allowed if "shift" is set.
2125 * Return 1 if "ineq" is a bound
2126 * 0 if "ineq" may attain arbitrarily small values on basic set "j"
2127 * -1 if some error occurred
2129 static int is_bound(struct sh_data
*data
, struct isl_set
*set
, int j
,
2130 isl_int
*ineq
, int shift
)
2132 enum isl_lp_result res
;
2135 if (!data
->p
[j
].tab
) {
2136 data
->p
[j
].tab
= isl_tab_from_basic_set(set
->p
[j
], 0);
2137 if (!data
->p
[j
].tab
)
2143 res
= isl_tab_min(data
->p
[j
].tab
, ineq
, data
->ctx
->one
,
2145 if (res
== isl_lp_ok
&& isl_int_is_neg(opt
)) {
2147 isl_int_sub(ineq
[0], ineq
[0], opt
);
2149 res
= isl_lp_unbounded
;
2154 return (res
== isl_lp_ok
|| res
== isl_lp_empty
) ? 1 :
2155 res
== isl_lp_unbounded
? 0 : -1;
2158 /* Set the constant term of "ineq" to the maximum of those of the constraints
2159 * in the basic sets of "set" following "i" that are parallel to "ineq".
2160 * That is, if any of the basic sets of "set" following "i" have a more
2161 * relaxed copy of "ineq", then replace "ineq" by the most relaxed copy.
2162 * "c_hash" is the hash value of the linear part of "ineq".
2163 * "v" has been set up for use by has_ineq.
2165 * Note that the two inequality constraints corresponding to an equality are
2166 * represented by the same inequality constraint in data->p[j].table
2167 * (but with different hash values). This means the constraint (or at
2168 * least its constant term) may need to be temporarily negated to get
2169 * the actually hashed constraint.
2171 static void set_max_constant_term(struct sh_data
*data
, __isl_keep isl_set
*set
,
2172 int i
, isl_int
*ineq
, uint32_t c_hash
, struct ineq_cmp_data
*v
)
2176 struct isl_hash_table_entry
*entry
;
2178 ctx
= isl_set_get_ctx(set
);
2179 for (j
= i
+ 1; j
< set
->n
; ++j
) {
2183 entry
= isl_hash_table_find(ctx
, data
->p
[j
].table
,
2184 c_hash
, &has_ineq
, v
, 0);
2188 ineq_j
= entry
->data
;
2189 neg
= isl_seq_is_neg(ineq_j
+ 1, ineq
+ 1, v
->len
);
2191 isl_int_neg(ineq_j
[0], ineq_j
[0]);
2192 if (isl_int_gt(ineq_j
[0], ineq
[0]))
2193 isl_int_set(ineq
[0], ineq_j
[0]);
2195 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 struct isl_basic_set
*add_bound(struct isl_basic_set
*hull
,
2221 struct sh_data
*data
, struct isl_set
*set
, int i
, isl_int
*ineq
,
2225 struct ineq_cmp_data v
;
2226 struct isl_hash_table_entry
*entry
;
2232 v
.len
= isl_basic_set_total_dim(hull
);
2234 c_hash
= isl_seq_get_hash(ineq
+ 1, v
.len
);
2236 entry
= isl_hash_table_find(hull
->ctx
, data
->hull_table
, c_hash
,
2241 for (j
= 0; j
< i
; ++j
) {
2242 entry
= isl_hash_table_find(hull
->ctx
, data
->p
[j
].table
,
2243 c_hash
, has_ineq
, &v
, 0);
2250 k
= isl_basic_set_alloc_inequality(hull
);
2253 isl_seq_cpy(hull
->ineq
[k
], ineq
, 1 + v
.len
);
2255 set_max_constant_term(data
, set
, i
, hull
->ineq
[k
], c_hash
, &v
);
2256 for (j
= 0; j
< i
; ++j
) {
2258 bound
= is_bound(data
, set
, j
, hull
->ineq
[k
], shift
);
2265 isl_basic_set_free_inequality(hull
, 1);
2269 for (j
= i
+ 1; j
< set
->n
; ++j
) {
2271 entry
= isl_hash_table_find(hull
->ctx
, data
->p
[j
].table
,
2272 c_hash
, has_ineq
, &v
, 0);
2275 bound
= is_bound(data
, set
, j
, hull
->ineq
[k
], shift
);
2282 isl_basic_set_free_inequality(hull
, 1);
2286 entry
= isl_hash_table_find(hull
->ctx
, data
->hull_table
, c_hash
,
2290 entry
->data
= hull
->ineq
[k
];
2294 isl_basic_set_free(hull
);
2298 /* Check if any inequality from basic set "i" is or can be relaxed to
2299 * become a bound on the whole set. If so, add the (relaxed) inequality
2300 * to "hull". Relaxation is only allowed if "shift" is set.
2302 static struct isl_basic_set
*add_bounds(struct isl_basic_set
*bset
,
2303 struct sh_data
*data
, struct isl_set
*set
, int i
, int shift
)
2306 unsigned dim
= isl_basic_set_total_dim(bset
);
2308 for (j
= 0; j
< set
->p
[i
]->n_eq
; ++j
) {
2309 for (k
= 0; k
< 2; ++k
) {
2310 isl_seq_neg(set
->p
[i
]->eq
[j
], set
->p
[i
]->eq
[j
], 1+dim
);
2311 bset
= add_bound(bset
, data
, set
, i
, set
->p
[i
]->eq
[j
],
2315 for (j
= 0; j
< set
->p
[i
]->n_ineq
; ++j
)
2316 bset
= add_bound(bset
, data
, set
, i
, set
->p
[i
]->ineq
[j
], shift
);
2320 /* Compute a superset of the convex hull of set that is described
2321 * by only (translates of) the constraints in the constituents of set.
2322 * Translation is only allowed if "shift" is set.
2324 static __isl_give isl_basic_set
*uset_simple_hull(__isl_take isl_set
*set
,
2327 struct sh_data
*data
= NULL
;
2328 struct isl_basic_set
*hull
= NULL
;
2336 for (i
= 0; i
< set
->n
; ++i
) {
2339 n_ineq
+= 2 * set
->p
[i
]->n_eq
+ set
->p
[i
]->n_ineq
;
2342 hull
= isl_basic_set_alloc_space(isl_space_copy(set
->dim
), 0, 0, n_ineq
);
2346 data
= sh_data_alloc(set
, n_ineq
);
2350 for (i
= 0; i
< set
->n
; ++i
)
2351 hull
= add_bounds(hull
, data
, set
, i
, shift
);
2359 isl_basic_set_free(hull
);
2364 /* Compute a superset of the convex hull of map that is described
2365 * by only (translates of) the constraints in the constituents of map.
2366 * Handle trivial cases where map is NULL or contains at most one disjunct.
2368 static __isl_give isl_basic_map
*map_simple_hull_trivial(
2369 __isl_take isl_map
*map
)
2371 isl_basic_map
*hull
;
2376 return replace_map_by_empty_basic_map(map
);
2378 hull
= isl_basic_map_copy(map
->p
[0]);
2383 /* Compute a superset of the convex hull of map that is described
2384 * by only (translates of) the constraints in the constituents of map.
2385 * Translation is only allowed if "shift" is set.
2387 * Sort the constraints before removing redundant constraints
2388 * in order to indicate a preference of which constraints should
2389 * be preserved. In particular, pairs of constraints that are
2390 * sorted together are preferred to either both be preserved
2391 * or both be removed.
2393 static __isl_give isl_basic_map
*map_simple_hull(__isl_take isl_map
*map
,
2396 struct isl_set
*set
= NULL
;
2397 struct isl_basic_map
*model
= NULL
;
2398 struct isl_basic_map
*hull
;
2399 struct isl_basic_map
*affine_hull
;
2400 struct isl_basic_set
*bset
= NULL
;
2402 if (!map
|| map
->n
<= 1)
2403 return map_simple_hull_trivial(map
);
2405 map
= isl_map_detect_equalities(map
);
2406 if (!map
|| map
->n
<= 1)
2407 return map_simple_hull_trivial(map
);
2408 affine_hull
= isl_map_affine_hull(isl_map_copy(map
));
2409 map
= isl_map_align_divs(map
);
2410 model
= map
? isl_basic_map_copy(map
->p
[0]) : NULL
;
2412 set
= isl_map_underlying_set(map
);
2414 bset
= uset_simple_hull(set
, shift
);
2416 hull
= isl_basic_map_overlying_set(bset
, model
);
2418 hull
= isl_basic_map_intersect(hull
, affine_hull
);
2419 hull
= isl_basic_map_sort_constraints(hull
);
2420 hull
= isl_basic_map_remove_redundancies(hull
);
2424 ISL_F_SET(hull
, ISL_BASIC_MAP_NO_IMPLICIT
);
2425 ISL_F_SET(hull
, ISL_BASIC_MAP_ALL_EQUALITIES
);
2427 hull
= isl_basic_map_finalize(hull
);
2432 /* Compute a superset of the convex hull of map that is described
2433 * by only translates of the constraints in the constituents of map.
2435 __isl_give isl_basic_map
*isl_map_simple_hull(__isl_take isl_map
*map
)
2437 return map_simple_hull(map
, 1);
2440 struct isl_basic_set
*isl_set_simple_hull(struct isl_set
*set
)
2442 return (struct isl_basic_set
*)
2443 isl_map_simple_hull((struct isl_map
*)set
);
2446 /* Compute a superset of the convex hull of map that is described
2447 * by only the constraints in the constituents of map.
2449 __isl_give isl_basic_map
*isl_map_unshifted_simple_hull(
2450 __isl_take isl_map
*map
)
2452 return map_simple_hull(map
, 0);
2455 __isl_give isl_basic_set
*isl_set_unshifted_simple_hull(
2456 __isl_take isl_set
*set
)
2458 return isl_map_unshifted_simple_hull(set
);
2461 /* Drop all inequalities from "bmap1" that do not also appear in "bmap2".
2462 * A constraint that appears with different constant terms
2463 * in "bmap1" and "bmap2" is also kept, with the least restrictive
2464 * (i.e., greatest) constant term.
2465 * "bmap1" and "bmap2" are assumed to have the same (known)
2466 * integer divisions.
2467 * The constraints of both "bmap1" and "bmap2" are assumed
2468 * to have been sorted using isl_basic_map_sort_constraints.
2470 * Run through the inequality constraints of "bmap1" and "bmap2"
2472 * Each constraint of "bmap1" without a matching constraint in "bmap2"
2474 * If a match is found, the constraint is kept. If needed, the constant
2475 * term of the constraint is adjusted.
2477 static __isl_give isl_basic_map
*select_shared_inequalities(
2478 __isl_take isl_basic_map
*bmap1
, __isl_keep isl_basic_map
*bmap2
)
2482 bmap1
= isl_basic_map_cow(bmap1
);
2483 if (!bmap1
|| !bmap2
)
2484 return isl_basic_map_free(bmap1
);
2486 i1
= bmap1
->n_ineq
- 1;
2487 i2
= bmap2
->n_ineq
- 1;
2488 while (bmap1
&& i1
>= 0 && i2
>= 0) {
2491 cmp
= isl_basic_map_constraint_cmp(bmap1
, bmap1
->ineq
[i1
],
2498 if (isl_basic_map_drop_inequality(bmap1
, i1
) < 0)
2499 bmap1
= isl_basic_map_free(bmap1
);
2503 if (isl_int_lt(bmap1
->ineq
[i1
][0], bmap2
->ineq
[i2
][0]))
2504 isl_int_set(bmap1
->ineq
[i1
][0], bmap2
->ineq
[i2
][0]);
2508 for (; i1
>= 0; --i1
)
2509 if (isl_basic_map_drop_inequality(bmap1
, i1
) < 0)
2510 bmap1
= isl_basic_map_free(bmap1
);
2515 /* Drop all equalities from "bmap1" that do not also appear in "bmap2".
2516 * "bmap1" and "bmap2" are assumed to have the same (known)
2517 * integer divisions.
2519 * Run through the equality constraints of "bmap1" and "bmap2".
2520 * Each constraint of "bmap1" without a matching constraint in "bmap2"
2523 static __isl_give isl_basic_map
*select_shared_equalities(
2524 __isl_take isl_basic_map
*bmap1
, __isl_keep isl_basic_map
*bmap2
)
2529 bmap1
= isl_basic_map_cow(bmap1
);
2530 if (!bmap1
|| !bmap2
)
2531 return isl_basic_map_free(bmap1
);
2533 total
= isl_basic_map_total_dim(bmap1
);
2535 i1
= bmap1
->n_eq
- 1;
2536 i2
= bmap2
->n_eq
- 1;
2537 while (bmap1
&& i1
>= 0 && i2
>= 0) {
2540 last1
= isl_seq_last_non_zero(bmap1
->eq
[i1
] + 1, total
);
2541 last2
= isl_seq_last_non_zero(bmap2
->eq
[i2
] + 1, total
);
2542 if (last1
> last2
) {
2546 if (last1
< last2
) {
2547 if (isl_basic_map_drop_equality(bmap1
, i1
) < 0)
2548 bmap1
= isl_basic_map_free(bmap1
);
2552 if (!isl_seq_eq(bmap1
->eq
[i1
], bmap2
->eq
[i2
], 1 + total
)) {
2553 if (isl_basic_map_drop_equality(bmap1
, i1
) < 0)
2554 bmap1
= isl_basic_map_free(bmap1
);
2559 for (; i1
>= 0; --i1
)
2560 if (isl_basic_map_drop_equality(bmap1
, i1
) < 0)
2561 bmap1
= isl_basic_map_free(bmap1
);
2566 /* Compute a superset of "bmap1" and "bmap2" that is described
2567 * by only the constraints that appear in both "bmap1" and "bmap2".
2569 * First drop constraints that involve unknown integer divisions
2570 * since it is not trivial to check whether two such integer divisions
2571 * in different basic maps are the same.
2572 * Then align the remaining (known) divs and sort the constraints.
2573 * Finally drop all inequalities and equalities from "bmap1" that
2574 * do not also appear in "bmap2".
2576 __isl_give isl_basic_map
*isl_basic_map_plain_unshifted_simple_hull(
2577 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
2579 bmap1
= isl_basic_map_drop_constraint_involving_unknown_divs(bmap1
);
2580 bmap2
= isl_basic_map_drop_constraint_involving_unknown_divs(bmap2
);
2581 bmap2
= isl_basic_map_align_divs(bmap2
, bmap1
);
2582 bmap1
= isl_basic_map_align_divs(bmap1
, bmap2
);
2583 bmap1
= isl_basic_map_gauss(bmap1
, NULL
);
2584 bmap2
= isl_basic_map_gauss(bmap2
, NULL
);
2585 bmap1
= isl_basic_map_sort_constraints(bmap1
);
2586 bmap2
= isl_basic_map_sort_constraints(bmap2
);
2588 bmap1
= select_shared_inequalities(bmap1
, bmap2
);
2589 bmap1
= select_shared_equalities(bmap1
, bmap2
);
2591 isl_basic_map_free(bmap2
);
2592 bmap1
= isl_basic_map_finalize(bmap1
);
2596 /* Compute a superset of the convex hull of "map" that is described
2597 * by only the constraints in the constituents of "map".
2598 * In particular, the result is composed of constraints that appear
2599 * in each of the basic maps of "map"
2601 * Constraints that involve unknown integer divisions are dropped
2602 * since it is not trivial to check whether two such integer divisions
2603 * in different basic maps are the same.
2605 * The hull is initialized from the first basic map and then
2606 * updated with respect to the other basic maps in turn.
2608 __isl_give isl_basic_map
*isl_map_plain_unshifted_simple_hull(
2609 __isl_take isl_map
*map
)
2612 isl_basic_map
*hull
;
2617 return map_simple_hull_trivial(map
);
2618 map
= isl_map_drop_constraint_involving_unknown_divs(map
);
2619 hull
= isl_basic_map_copy(map
->p
[0]);
2620 for (i
= 1; i
< map
->n
; ++i
) {
2621 isl_basic_map
*bmap_i
;
2623 bmap_i
= isl_basic_map_copy(map
->p
[i
]);
2624 hull
= isl_basic_map_plain_unshifted_simple_hull(hull
, bmap_i
);
2631 /* Check if "ineq" is a bound on "set" and, if so, add it to "hull".
2633 * For each basic set in "set", we first check if the basic set
2634 * contains a translate of "ineq". If this translate is more relaxed,
2635 * then we assume that "ineq" is not a bound on this basic set.
2636 * Otherwise, we know that it is a bound.
2637 * If the basic set does not contain a translate of "ineq", then
2638 * we call is_bound to perform the test.
2640 static __isl_give isl_basic_set
*add_bound_from_constraint(
2641 __isl_take isl_basic_set
*hull
, struct sh_data
*data
,
2642 __isl_keep isl_set
*set
, isl_int
*ineq
)
2647 struct ineq_cmp_data v
;
2650 return isl_basic_set_free(hull
);
2652 v
.len
= isl_basic_set_total_dim(hull
);
2654 c_hash
= isl_seq_get_hash(ineq
+ 1, v
.len
);
2656 ctx
= isl_basic_set_get_ctx(hull
);
2657 for (i
= 0; i
< set
->n
; ++i
) {
2659 struct isl_hash_table_entry
*entry
;
2661 entry
= isl_hash_table_find(ctx
, data
->p
[i
].table
,
2662 c_hash
, &has_ineq
, &v
, 0);
2664 isl_int
*ineq_i
= entry
->data
;
2665 int neg
, more_relaxed
;
2667 neg
= isl_seq_is_neg(ineq_i
+ 1, ineq
+ 1, v
.len
);
2669 isl_int_neg(ineq_i
[0], ineq_i
[0]);
2670 more_relaxed
= isl_int_gt(ineq_i
[0], ineq
[0]);
2672 isl_int_neg(ineq_i
[0], ineq_i
[0]);
2678 bound
= is_bound(data
, set
, i
, ineq
, 0);
2680 return isl_basic_set_free(hull
);
2687 k
= isl_basic_set_alloc_inequality(hull
);
2689 return isl_basic_set_free(hull
);
2690 isl_seq_cpy(hull
->ineq
[k
], ineq
, 1 + v
.len
);
2695 /* Compute a superset of the convex hull of "set" that is described
2696 * by only some of the "n_ineq" constraints in the list "ineq", where "set"
2697 * has no parameters or integer divisions.
2699 * The inequalities in "ineq" are assumed to have been sorted such
2700 * that constraints with the same linear part appear together and
2701 * that among constraints with the same linear part, those with
2702 * smaller constant term appear first.
2704 * We reuse the same data structure that is used by uset_simple_hull,
2705 * but we do not need the hull table since we will not consider the
2706 * same constraint more than once. We therefore allocate it with zero size.
2708 * We run through the constraints and try to add them one by one,
2709 * skipping identical constraints. If we have added a constraint and
2710 * the next constraint is a more relaxed translate, then we skip this
2711 * next constraint as well.
2713 static __isl_give isl_basic_set
*uset_unshifted_simple_hull_from_constraints(
2714 __isl_take isl_set
*set
, int n_ineq
, isl_int
**ineq
)
2718 struct sh_data
*data
= NULL
;
2719 isl_basic_set
*hull
= NULL
;
2722 hull
= isl_basic_set_alloc_space(isl_set_get_space(set
), 0, 0, n_ineq
);
2726 data
= sh_data_alloc(set
, 0);
2730 dim
= isl_set_dim(set
, isl_dim_set
);
2731 for (i
= 0; i
< n_ineq
; ++i
) {
2732 int hull_n_ineq
= hull
->n_ineq
;
2735 parallel
= i
> 0 && isl_seq_eq(ineq
[i
- 1] + 1, ineq
[i
] + 1,
2738 (last_added
|| isl_int_eq(ineq
[i
- 1][0], ineq
[i
][0])))
2740 hull
= add_bound_from_constraint(hull
, data
, set
, ineq
[i
]);
2743 last_added
= hull
->n_ineq
> hull_n_ineq
;
2752 isl_basic_set_free(hull
);
2756 /* Collect pointers to all the inequalities in the elements of "list"
2757 * in "ineq". For equalities, store both a pointer to the equality and
2758 * a pointer to its opposite, which is first copied to "mat".
2759 * "ineq" and "mat" are assumed to have been preallocated to the right size
2760 * (the number of inequalities + 2 times the number of equalites and
2761 * the number of equalities, respectively).
2763 static __isl_give isl_mat
*collect_inequalities(__isl_take isl_mat
*mat
,
2764 __isl_keep isl_basic_set_list
*list
, isl_int
**ineq
)
2766 int i
, j
, n
, n_eq
, n_ineq
;
2773 n
= isl_basic_set_list_n_basic_set(list
);
2774 for (i
= 0; i
< n
; ++i
) {
2775 isl_basic_set
*bset
;
2776 bset
= isl_basic_set_list_get_basic_set(list
, i
);
2778 return isl_mat_free(mat
);
2779 for (j
= 0; j
< bset
->n_eq
; ++j
) {
2780 ineq
[n_ineq
++] = mat
->row
[n_eq
];
2781 ineq
[n_ineq
++] = bset
->eq
[j
];
2782 isl_seq_neg(mat
->row
[n_eq
++], bset
->eq
[j
], mat
->n_col
);
2784 for (j
= 0; j
< bset
->n_ineq
; ++j
)
2785 ineq
[n_ineq
++] = bset
->ineq
[j
];
2786 isl_basic_set_free(bset
);
2792 /* Comparison routine for use as an isl_sort callback.
2794 * Constraints with the same linear part are sorted together and
2795 * among constraints with the same linear part, those with smaller
2796 * constant term are sorted first.
2798 static int cmp_ineq(const void *a
, const void *b
, void *arg
)
2800 unsigned dim
= *(unsigned *) arg
;
2801 isl_int
* const *ineq1
= a
;
2802 isl_int
* const *ineq2
= b
;
2805 cmp
= isl_seq_cmp((*ineq1
) + 1, (*ineq2
) + 1, dim
);
2808 return isl_int_cmp((*ineq1
)[0], (*ineq2
)[0]);
2811 /* Compute a superset of the convex hull of "set" that is described
2812 * by only constraints in the elements of "list", where "set" has
2813 * no parameters or integer divisions.
2815 * We collect all the constraints in those elements and then
2816 * sort the constraints such that constraints with the same linear part
2817 * are sorted together and that those with smaller constant term are
2820 static __isl_give isl_basic_set
*uset_unshifted_simple_hull_from_basic_set_list(
2821 __isl_take isl_set
*set
, __isl_take isl_basic_set_list
*list
)
2823 int i
, n
, n_eq
, n_ineq
;
2826 isl_mat
*mat
= NULL
;
2827 isl_int
**ineq
= NULL
;
2828 isl_basic_set
*hull
;
2832 ctx
= isl_set_get_ctx(set
);
2836 n
= isl_basic_set_list_n_basic_set(list
);
2837 for (i
= 0; i
< n
; ++i
) {
2838 isl_basic_set
*bset
;
2839 bset
= isl_basic_set_list_get_basic_set(list
, i
);
2843 n_ineq
+= 2 * bset
->n_eq
+ bset
->n_ineq
;
2844 isl_basic_set_free(bset
);
2847 ineq
= isl_alloc_array(ctx
, isl_int
*, n_ineq
);
2848 if (n_ineq
> 0 && !ineq
)
2851 dim
= isl_set_dim(set
, isl_dim_set
);
2852 mat
= isl_mat_alloc(ctx
, n_eq
, 1 + dim
);
2853 mat
= collect_inequalities(mat
, list
, ineq
);
2857 if (isl_sort(ineq
, n_ineq
, sizeof(ineq
[0]), &cmp_ineq
, &dim
) < 0)
2860 hull
= uset_unshifted_simple_hull_from_constraints(set
, n_ineq
, ineq
);
2864 isl_basic_set_list_free(list
);
2870 isl_basic_set_list_free(list
);
2874 /* Compute a superset of the convex hull of "map" that is described
2875 * by only constraints in the elements of "list".
2877 * If the list is empty, then we can only describe the universe set.
2878 * If the input map is empty, then all constraints are valid, so
2879 * we return the intersection of the elements in "list".
2881 * Otherwise, we align all divs and temporarily treat them
2882 * as regular variables, computing the unshifted simple hull in
2883 * uset_unshifted_simple_hull_from_basic_set_list.
2885 static __isl_give isl_basic_map
*map_unshifted_simple_hull_from_basic_map_list(
2886 __isl_take isl_map
*map
, __isl_take isl_basic_map_list
*list
)
2888 isl_basic_map
*model
;
2889 isl_basic_map
*hull
;
2891 isl_basic_set_list
*bset_list
;
2896 if (isl_basic_map_list_n_basic_map(list
) == 0) {
2899 space
= isl_map_get_space(map
);
2901 isl_basic_map_list_free(list
);
2902 return isl_basic_map_universe(space
);
2904 if (isl_map_plain_is_empty(map
)) {
2906 return isl_basic_map_list_intersect(list
);
2909 map
= isl_map_align_divs_to_basic_map_list(map
, list
);
2912 list
= isl_basic_map_list_align_divs_to_basic_map(list
, map
->p
[0]);
2914 model
= isl_basic_map_list_get_basic_map(list
, 0);
2916 set
= isl_map_underlying_set(map
);
2917 bset_list
= isl_basic_map_list_underlying_set(list
);
2919 hull
= uset_unshifted_simple_hull_from_basic_set_list(set
, bset_list
);
2920 hull
= isl_basic_map_overlying_set(hull
, model
);
2925 isl_basic_map_list_free(list
);
2929 /* Return a sequence of the basic maps that make up the maps in "list".
2931 static __isl_give isl_basic_set_list
*collect_basic_maps(
2932 __isl_take isl_map_list
*list
)
2936 isl_basic_map_list
*bmap_list
;
2940 n
= isl_map_list_n_map(list
);
2941 ctx
= isl_map_list_get_ctx(list
);
2942 bmap_list
= isl_basic_map_list_alloc(ctx
, 0);
2944 for (i
= 0; i
< n
; ++i
) {
2946 isl_basic_map_list
*list_i
;
2948 map
= isl_map_list_get_map(list
, i
);
2949 map
= isl_map_compute_divs(map
);
2950 list_i
= isl_map_get_basic_map_list(map
);
2952 bmap_list
= isl_basic_map_list_concat(bmap_list
, list_i
);
2955 isl_map_list_free(list
);
2959 /* Compute a superset of the convex hull of "map" that is described
2960 * by only constraints in the elements of "list".
2962 * If "map" is the universe, then the convex hull (and therefore
2963 * any superset of the convexhull) is the universe as well.
2965 * Otherwise, we collect all the basic maps in the map list and
2966 * continue with map_unshifted_simple_hull_from_basic_map_list.
2968 __isl_give isl_basic_map
*isl_map_unshifted_simple_hull_from_map_list(
2969 __isl_take isl_map
*map
, __isl_take isl_map_list
*list
)
2971 isl_basic_map_list
*bmap_list
;
2974 is_universe
= isl_map_plain_is_universe(map
);
2975 if (is_universe
< 0)
2976 map
= isl_map_free(map
);
2977 if (is_universe
< 0 || is_universe
) {
2978 isl_map_list_free(list
);
2979 return isl_map_unshifted_simple_hull(map
);
2982 bmap_list
= collect_basic_maps(list
);
2983 return map_unshifted_simple_hull_from_basic_map_list(map
, bmap_list
);
2986 /* Compute a superset of the convex hull of "set" that is described
2987 * by only constraints in the elements of "list".
2989 __isl_give isl_basic_set
*isl_set_unshifted_simple_hull_from_set_list(
2990 __isl_take isl_set
*set
, __isl_take isl_set_list
*list
)
2992 return isl_map_unshifted_simple_hull_from_map_list(set
, list
);
2995 /* Given a set "set", return parametric bounds on the dimension "dim".
2997 static struct isl_basic_set
*set_bounds(struct isl_set
*set
, int dim
)
2999 unsigned set_dim
= isl_set_dim(set
, isl_dim_set
);
3000 set
= isl_set_copy(set
);
3001 set
= isl_set_eliminate_dims(set
, dim
+ 1, set_dim
- (dim
+ 1));
3002 set
= isl_set_eliminate_dims(set
, 0, dim
);
3003 return isl_set_convex_hull(set
);
3006 /* Computes a "simple hull" and then check if each dimension in the
3007 * resulting hull is bounded by a symbolic constant. If not, the
3008 * hull is intersected with the corresponding bounds on the whole set.
3010 struct isl_basic_set
*isl_set_bounded_simple_hull(struct isl_set
*set
)
3013 struct isl_basic_set
*hull
;
3014 unsigned nparam
, left
;
3015 int removed_divs
= 0;
3017 hull
= isl_set_simple_hull(isl_set_copy(set
));
3021 nparam
= isl_basic_set_dim(hull
, isl_dim_param
);
3022 for (i
= 0; i
< isl_basic_set_dim(hull
, isl_dim_set
); ++i
) {
3023 int lower
= 0, upper
= 0;
3024 struct isl_basic_set
*bounds
;
3026 left
= isl_basic_set_total_dim(hull
) - nparam
- i
- 1;
3027 for (j
= 0; j
< hull
->n_eq
; ++j
) {
3028 if (isl_int_is_zero(hull
->eq
[j
][1 + nparam
+ i
]))
3030 if (isl_seq_first_non_zero(hull
->eq
[j
]+1+nparam
+i
+1,
3037 for (j
= 0; j
< hull
->n_ineq
; ++j
) {
3038 if (isl_int_is_zero(hull
->ineq
[j
][1 + nparam
+ i
]))
3040 if (isl_seq_first_non_zero(hull
->ineq
[j
]+1+nparam
+i
+1,
3042 isl_seq_first_non_zero(hull
->ineq
[j
]+1+nparam
,
3045 if (isl_int_is_pos(hull
->ineq
[j
][1 + nparam
+ i
]))
3056 if (!removed_divs
) {
3057 set
= isl_set_remove_divs(set
);
3062 bounds
= set_bounds(set
, i
);
3063 hull
= isl_basic_set_intersect(hull
, bounds
);