3 #include "isl_map_private.h"
7 #include "isl_equalities.h"
10 static struct isl_basic_set
*uset_convex_hull_wrap_bounded(struct isl_set
*set
);
12 static void swap_ineq(struct isl_basic_map
*bmap
, unsigned i
, unsigned j
)
18 bmap
->ineq
[i
] = bmap
->ineq
[j
];
23 /* Return 1 if constraint c is redundant with respect to the constraints
24 * in bmap. If c is a lower [upper] bound in some variable and bmap
25 * does not have a lower [upper] bound in that variable, then c cannot
26 * be redundant and we do not need solve any lp.
28 int isl_basic_map_constraint_is_redundant(struct isl_basic_map
**bmap
,
29 isl_int
*c
, isl_int
*opt_n
, isl_int
*opt_d
)
31 enum isl_lp_result res
;
38 total
= isl_basic_map_total_dim(*bmap
);
39 for (i
= 0; i
< total
; ++i
) {
41 if (isl_int_is_zero(c
[1+i
]))
43 sign
= isl_int_sgn(c
[1+i
]);
44 for (j
= 0; j
< (*bmap
)->n_ineq
; ++j
)
45 if (sign
== isl_int_sgn((*bmap
)->ineq
[j
][1+i
]))
47 if (j
== (*bmap
)->n_ineq
)
53 res
= isl_basic_map_solve_lp(*bmap
, 0, c
, (*bmap
)->ctx
->one
,
55 if (res
== isl_lp_unbounded
)
57 if (res
== isl_lp_error
)
59 if (res
== isl_lp_empty
) {
60 *bmap
= isl_basic_map_set_to_empty(*bmap
);
63 return !isl_int_is_neg(*opt_n
);
66 int isl_basic_set_constraint_is_redundant(struct isl_basic_set
**bset
,
67 isl_int
*c
, isl_int
*opt_n
, isl_int
*opt_d
)
69 return isl_basic_map_constraint_is_redundant(
70 (struct isl_basic_map
**)bset
, c
, opt_n
, opt_d
);
73 /* Compute the convex hull of a basic map, by removing the redundant
74 * constraints. If the minimal value along the normal of a constraint
75 * is the same if the constraint is removed, then the constraint is redundant.
77 * Alternatively, we could have intersected the basic map with the
78 * corresponding equality and the checked if the dimension was that
81 struct isl_basic_map
*isl_basic_map_convex_hull(struct isl_basic_map
*bmap
)
88 bmap
= isl_basic_map_gauss(bmap
, NULL
);
89 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
91 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
))
93 if (bmap
->n_ineq
<= 1)
96 tab
= isl_tab_from_basic_map(bmap
);
97 tab
= isl_tab_detect_equalities(tab
);
98 tab
= isl_tab_detect_redundant(tab
);
99 bmap
= isl_basic_map_update_from_tab(bmap
, tab
);
101 ISL_F_SET(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
102 ISL_F_SET(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
106 struct isl_basic_set
*isl_basic_set_convex_hull(struct isl_basic_set
*bset
)
108 return (struct isl_basic_set
*)
109 isl_basic_map_convex_hull((struct isl_basic_map
*)bset
);
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 int uset_is_bound(struct 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 (!isl_int_is_one(opt_denom
))
145 isl_seq_scale(c
, c
, opt_denom
, len
);
146 if (first
|| isl_int_is_neg(opt
))
147 isl_int_sub(c
[0], c
[0], opt
);
151 isl_int_clear(opt_denom
);
155 isl_int_clear(opt_denom
);
159 /* Check if "c" is a direction that is independent of the previously found "n"
161 * If so, add it to the list, with the negative of the lower bound
162 * in the constant position, i.e., such that c corresponds to a bounding
163 * hyperplane (but not necessarily a facet).
164 * Assumes set "set" is bounded.
166 static int is_independent_bound(struct isl_set
*set
, isl_int
*c
,
167 struct isl_mat
*dirs
, int n
)
172 isl_seq_cpy(dirs
->row
[n
]+1, c
+1, dirs
->n_col
-1);
174 int pos
= isl_seq_first_non_zero(dirs
->row
[n
]+1, dirs
->n_col
-1);
177 for (i
= 0; i
< n
; ++i
) {
179 pos_i
= isl_seq_first_non_zero(dirs
->row
[i
]+1, dirs
->n_col
-1);
184 isl_seq_elim(dirs
->row
[n
]+1, dirs
->row
[i
]+1, pos
,
185 dirs
->n_col
-1, NULL
);
186 pos
= isl_seq_first_non_zero(dirs
->row
[n
]+1, dirs
->n_col
-1);
192 is_bound
= uset_is_bound(set
, dirs
->row
[n
], dirs
->n_col
);
197 isl_int
*t
= dirs
->row
[n
];
198 for (k
= n
; k
> i
; --k
)
199 dirs
->row
[k
] = dirs
->row
[k
-1];
205 /* Compute and return a maximal set of linearly independent bounds
206 * on the set "set", based on the constraints of the basic sets
209 static struct isl_mat
*independent_bounds(struct isl_set
*set
)
212 struct isl_mat
*dirs
= NULL
;
213 unsigned dim
= isl_set_n_dim(set
);
215 dirs
= isl_mat_alloc(set
->ctx
, dim
, 1+dim
);
220 for (i
= 0; n
< dim
&& i
< set
->n
; ++i
) {
222 struct isl_basic_set
*bset
= set
->p
[i
];
224 for (j
= 0; n
< dim
&& j
< bset
->n_eq
; ++j
) {
225 f
= is_independent_bound(set
, bset
->eq
[j
], dirs
, n
);
231 for (j
= 0; n
< dim
&& j
< bset
->n_ineq
; ++j
) {
232 f
= is_independent_bound(set
, bset
->ineq
[j
], dirs
, n
);
246 struct isl_basic_set
*isl_basic_set_set_rational(struct isl_basic_set
*bset
)
251 if (ISL_F_ISSET(bset
, ISL_BASIC_MAP_RATIONAL
))
254 bset
= isl_basic_set_cow(bset
);
258 ISL_F_SET(bset
, ISL_BASIC_MAP_RATIONAL
);
260 return isl_basic_set_finalize(bset
);
263 static struct isl_set
*isl_set_set_rational(struct isl_set
*set
)
267 set
= isl_set_cow(set
);
270 for (i
= 0; i
< set
->n
; ++i
) {
271 set
->p
[i
] = isl_basic_set_set_rational(set
->p
[i
]);
281 static struct isl_basic_set
*isl_basic_set_add_equality(
282 struct isl_basic_set
*bset
, isl_int
*c
)
288 if (ISL_F_ISSET(bset
, ISL_BASIC_SET_EMPTY
))
291 isl_assert(ctx
, isl_basic_set_n_param(bset
) == 0, goto error
);
292 isl_assert(ctx
, bset
->n_div
== 0, goto error
);
293 dim
= isl_basic_set_n_dim(bset
);
294 bset
= isl_basic_set_cow(bset
);
295 bset
= isl_basic_set_extend(bset
, 0, dim
, 0, 1, 0);
296 i
= isl_basic_set_alloc_equality(bset
);
299 isl_seq_cpy(bset
->eq
[i
], c
, 1 + dim
);
302 isl_basic_set_free(bset
);
306 static struct isl_set
*isl_set_add_equality(struct isl_set
*set
, isl_int
*c
)
310 set
= isl_set_cow(set
);
313 for (i
= 0; i
< set
->n
; ++i
) {
314 set
->p
[i
] = isl_basic_set_add_equality(set
->p
[i
], c
);
324 /* Given a union of basic sets, construct the constraints for wrapping
325 * a facet around one of its ridges.
326 * In particular, if each of n the d-dimensional basic sets i in "set"
327 * contains the origin, satisfies the constraints x_1 >= 0 and x_2 >= 0
328 * and is defined by the constraints
332 * then the resulting set is of dimension n*(1+d) and has as constraints
341 static struct isl_basic_set
*wrap_constraints(struct isl_set
*set
)
343 struct isl_basic_set
*lp
;
347 unsigned dim
, lp_dim
;
352 dim
= 1 + isl_set_n_dim(set
);
355 for (i
= 0; i
< set
->n
; ++i
) {
356 n_eq
+= set
->p
[i
]->n_eq
;
357 n_ineq
+= set
->p
[i
]->n_ineq
;
359 lp
= isl_basic_set_alloc(set
->ctx
, 0, dim
* set
->n
, 0, n_eq
, n_ineq
);
362 lp_dim
= isl_basic_set_n_dim(lp
);
363 k
= isl_basic_set_alloc_equality(lp
);
364 isl_int_set_si(lp
->eq
[k
][0], -1);
365 for (i
= 0; i
< set
->n
; ++i
) {
366 isl_int_set_si(lp
->eq
[k
][1+dim
*i
], 0);
367 isl_int_set_si(lp
->eq
[k
][1+dim
*i
+1], 1);
368 isl_seq_clr(lp
->eq
[k
]+1+dim
*i
+2, dim
-2);
370 for (i
= 0; i
< set
->n
; ++i
) {
371 k
= isl_basic_set_alloc_inequality(lp
);
372 isl_seq_clr(lp
->ineq
[k
], 1+lp_dim
);
373 isl_int_set_si(lp
->ineq
[k
][1+dim
*i
], 1);
375 for (j
= 0; j
< set
->p
[i
]->n_eq
; ++j
) {
376 k
= isl_basic_set_alloc_equality(lp
);
377 isl_seq_clr(lp
->eq
[k
], 1+dim
*i
);
378 isl_seq_cpy(lp
->eq
[k
]+1+dim
*i
, set
->p
[i
]->eq
[j
], dim
);
379 isl_seq_clr(lp
->eq
[k
]+1+dim
*(i
+1), dim
*(set
->n
-i
-1));
382 for (j
= 0; j
< set
->p
[i
]->n_ineq
; ++j
) {
383 k
= isl_basic_set_alloc_inequality(lp
);
384 isl_seq_clr(lp
->ineq
[k
], 1+dim
*i
);
385 isl_seq_cpy(lp
->ineq
[k
]+1+dim
*i
, set
->p
[i
]->ineq
[j
], dim
);
386 isl_seq_clr(lp
->ineq
[k
]+1+dim
*(i
+1), dim
*(set
->n
-i
-1));
392 /* Given a facet "facet" of the convex hull of "set" and a facet "ridge"
393 * of that facet, compute the other facet of the convex hull that contains
396 * We first transform the set such that the facet constraint becomes
400 * I.e., the facet lies in
404 * and on that facet, the constraint that defines the ridge is
408 * (This transformation is not strictly needed, all that is needed is
409 * that the ridge contains the origin.)
411 * Since the ridge contains the origin, the cone of the convex hull
412 * will be of the form
417 * with this second constraint defining the new facet.
418 * The constant a is obtained by settting x_1 in the cone of the
419 * convex hull to 1 and minimizing x_2.
420 * Now, each element in the cone of the convex hull is the sum
421 * of elements in the cones of the basic sets.
422 * If a_i is the dilation factor of basic set i, then the problem
423 * we need to solve is
436 * the constraints of each (transformed) basic set.
437 * If a = n/d, then the constraint defining the new facet (in the transformed
440 * -n x_1 + d x_2 >= 0
442 * In the original space, we need to take the same combination of the
443 * corresponding constraints "facet" and "ridge".
445 * Note that a is always finite, since we only apply the wrapping
446 * technique to a union of polytopes.
448 static isl_int
*wrap_facet(struct isl_set
*set
, isl_int
*facet
, isl_int
*ridge
)
451 struct isl_mat
*T
= NULL
;
452 struct isl_basic_set
*lp
= NULL
;
454 enum isl_lp_result res
;
458 set
= isl_set_copy(set
);
460 dim
= 1 + isl_set_n_dim(set
);
461 T
= isl_mat_alloc(set
->ctx
, 3, dim
);
464 isl_int_set_si(T
->row
[0][0], 1);
465 isl_seq_clr(T
->row
[0]+1, dim
- 1);
466 isl_seq_cpy(T
->row
[1], facet
, dim
);
467 isl_seq_cpy(T
->row
[2], ridge
, dim
);
468 T
= isl_mat_right_inverse(T
);
469 set
= isl_set_preimage(set
, T
);
473 lp
= wrap_constraints(set
);
474 obj
= isl_vec_alloc(set
->ctx
, 1 + dim
*set
->n
);
477 isl_int_set_si(obj
->block
.data
[0], 0);
478 for (i
= 0; i
< set
->n
; ++i
) {
479 isl_seq_clr(obj
->block
.data
+ 1 + dim
*i
, 2);
480 isl_int_set_si(obj
->block
.data
[1 + dim
*i
+2], 1);
481 isl_seq_clr(obj
->block
.data
+ 1 + dim
*i
+3, dim
-3);
485 res
= isl_basic_set_solve_lp(lp
, 0,
486 obj
->block
.data
, set
->ctx
->one
, &num
, &den
, NULL
);
487 if (res
== isl_lp_ok
) {
488 isl_int_neg(num
, num
);
489 isl_seq_combine(facet
, num
, facet
, den
, ridge
, dim
);
494 isl_basic_set_free(lp
);
496 isl_assert(set
->ctx
, res
== isl_lp_ok
, return NULL
);
499 isl_basic_set_free(lp
);
505 /* Given a set of d linearly independent bounding constraints of the
506 * convex hull of "set", compute the constraint of a facet of "set".
508 * We first compute the intersection with the first bounding hyperplane
509 * and remove the component corresponding to this hyperplane from
510 * other bounds (in homogeneous space).
511 * We then wrap around one of the remaining bounding constraints
512 * and continue the process until all bounding constraints have been
513 * taken into account.
514 * The resulting linear combination of the bounding constraints will
515 * correspond to a facet of the convex hull.
517 static struct isl_mat
*initial_facet_constraint(struct isl_set
*set
,
518 struct isl_mat
*bounds
)
520 struct isl_set
*slice
= NULL
;
521 struct isl_basic_set
*face
= NULL
;
522 struct isl_mat
*m
, *U
, *Q
;
524 unsigned dim
= isl_set_n_dim(set
);
526 isl_assert(ctx
, set
->n
> 0, goto error
);
527 isl_assert(ctx
, bounds
->n_row
== dim
, goto error
);
529 while (bounds
->n_row
> 1) {
530 slice
= isl_set_copy(set
);
531 slice
= isl_set_add_equality(slice
, bounds
->row
[0]);
532 face
= isl_set_affine_hull(slice
);
535 if (face
->n_eq
== 1) {
536 isl_basic_set_free(face
);
539 m
= isl_mat_alloc(set
->ctx
, 1 + face
->n_eq
, 1 + dim
);
542 isl_int_set_si(m
->row
[0][0], 1);
543 isl_seq_clr(m
->row
[0]+1, dim
);
544 for (i
= 0; i
< face
->n_eq
; ++i
)
545 isl_seq_cpy(m
->row
[1 + i
], face
->eq
[i
], 1 + dim
);
546 U
= isl_mat_right_inverse(m
);
547 Q
= isl_mat_right_inverse(isl_mat_copy(U
));
548 U
= isl_mat_drop_cols(U
, 1 + face
->n_eq
, dim
- face
->n_eq
);
549 Q
= isl_mat_drop_rows(Q
, 1 + face
->n_eq
, dim
- face
->n_eq
);
550 U
= isl_mat_drop_cols(U
, 0, 1);
551 Q
= isl_mat_drop_rows(Q
, 0, 1);
552 bounds
= isl_mat_product(bounds
, U
);
553 bounds
= isl_mat_product(bounds
, Q
);
554 while (isl_seq_first_non_zero(bounds
->row
[bounds
->n_row
-1],
555 bounds
->n_col
) == -1) {
557 isl_assert(ctx
, bounds
->n_row
> 1, goto error
);
559 if (!wrap_facet(set
, bounds
->row
[0],
560 bounds
->row
[bounds
->n_row
-1]))
562 isl_basic_set_free(face
);
567 isl_basic_set_free(face
);
568 isl_mat_free(bounds
);
572 /* Given the bounding constraint "c" of a facet of the convex hull of "set",
573 * compute a hyperplane description of the facet, i.e., compute the facets
576 * We compute an affine transformation that transforms the constraint
585 * by computing the right inverse U of a matrix that starts with the rows
598 * Since z_1 is zero, we can drop this variable as well as the corresponding
599 * column of U to obtain
607 * with Q' equal to Q, but without the corresponding row.
608 * After computing the facets of the facet in the z' space,
609 * we convert them back to the x space through Q.
611 static struct isl_basic_set
*compute_facet(struct isl_set
*set
, isl_int
*c
)
613 struct isl_mat
*m
, *U
, *Q
;
614 struct isl_basic_set
*facet
= NULL
;
619 set
= isl_set_copy(set
);
620 dim
= isl_set_n_dim(set
);
621 m
= isl_mat_alloc(set
->ctx
, 2, 1 + dim
);
624 isl_int_set_si(m
->row
[0][0], 1);
625 isl_seq_clr(m
->row
[0]+1, dim
);
626 isl_seq_cpy(m
->row
[1], c
, 1+dim
);
627 U
= isl_mat_right_inverse(m
);
628 Q
= isl_mat_right_inverse(isl_mat_copy(U
));
629 U
= isl_mat_drop_cols(U
, 1, 1);
630 Q
= isl_mat_drop_rows(Q
, 1, 1);
631 set
= isl_set_preimage(set
, U
);
632 facet
= uset_convex_hull_wrap_bounded(set
);
633 facet
= isl_basic_set_preimage(facet
, Q
);
634 isl_assert(ctx
, facet
->n_eq
== 0, goto error
);
637 isl_basic_set_free(facet
);
642 /* Given an initial facet constraint, compute the remaining facets.
643 * We do this by running through all facets found so far and computing
644 * the adjacent facets through wrapping, adding those facets that we
645 * hadn't already found before.
647 * For each facet we have found so far, we first compute its facets
648 * in the resulting convex hull. That is, we compute the ridges
649 * of the resulting convex hull contained in the facet.
650 * We also compute the corresponding facet in the current approximation
651 * of the convex hull. There is no need to wrap around the ridges
652 * in this facet since that would result in a facet that is already
653 * present in the current approximation.
655 * This function can still be significantly optimized by checking which of
656 * the facets of the basic sets are also facets of the convex hull and
657 * using all the facets so far to help in constructing the facets of the
660 * using the technique in section "3.1 Ridge Generation" of
661 * "Extended Convex Hull" by Fukuda et al.
663 static struct isl_basic_set
*extend(struct isl_basic_set
*hull
,
668 struct isl_basic_set
*facet
= NULL
;
669 struct isl_basic_set
*hull_facet
= NULL
;
673 isl_assert(set
->ctx
, set
->n
> 0, goto error
);
675 dim
= isl_set_n_dim(set
);
677 for (i
= 0; i
< hull
->n_ineq
; ++i
) {
678 facet
= compute_facet(set
, hull
->ineq
[i
]);
679 facet
= isl_basic_set_add_equality(facet
, hull
->ineq
[i
]);
680 facet
= isl_basic_set_gauss(facet
, NULL
);
681 facet
= isl_basic_set_normalize_constraints(facet
);
682 hull_facet
= isl_basic_set_copy(hull
);
683 hull_facet
= isl_basic_set_add_equality(hull_facet
, hull
->ineq
[i
]);
684 hull_facet
= isl_basic_set_gauss(hull_facet
, NULL
);
685 hull_facet
= isl_basic_set_normalize_constraints(hull_facet
);
688 hull
= isl_basic_set_cow(hull
);
689 hull
= isl_basic_set_extend_dim(hull
,
690 isl_dim_copy(hull
->dim
), 0, 0, facet
->n_ineq
);
691 for (j
= 0; j
< facet
->n_ineq
; ++j
) {
692 for (f
= 0; f
< hull_facet
->n_ineq
; ++f
)
693 if (isl_seq_eq(facet
->ineq
[j
],
694 hull_facet
->ineq
[f
], 1 + dim
))
696 if (f
< hull_facet
->n_ineq
)
698 k
= isl_basic_set_alloc_inequality(hull
);
701 isl_seq_cpy(hull
->ineq
[k
], hull
->ineq
[i
], 1+dim
);
702 if (!wrap_facet(set
, hull
->ineq
[k
], facet
->ineq
[j
]))
705 isl_basic_set_free(hull_facet
);
706 isl_basic_set_free(facet
);
708 hull
= isl_basic_set_simplify(hull
);
709 hull
= isl_basic_set_finalize(hull
);
712 isl_basic_set_free(hull_facet
);
713 isl_basic_set_free(facet
);
714 isl_basic_set_free(hull
);
718 /* Special case for computing the convex hull of a one dimensional set.
719 * We simply collect the lower and upper bounds of each basic set
720 * and the biggest of those.
722 static struct isl_basic_set
*convex_hull_1d(struct isl_set
*set
)
724 struct isl_mat
*c
= NULL
;
725 isl_int
*lower
= NULL
;
726 isl_int
*upper
= NULL
;
729 struct isl_basic_set
*hull
;
731 for (i
= 0; i
< set
->n
; ++i
) {
732 set
->p
[i
] = isl_basic_set_simplify(set
->p
[i
]);
736 set
= isl_set_remove_empty_parts(set
);
739 isl_assert(set
->ctx
, set
->n
> 0, goto error
);
740 c
= isl_mat_alloc(set
->ctx
, 2, 2);
744 if (set
->p
[0]->n_eq
> 0) {
745 isl_assert(set
->ctx
, set
->p
[0]->n_eq
== 1, goto error
);
748 if (isl_int_is_pos(set
->p
[0]->eq
[0][1])) {
749 isl_seq_cpy(lower
, set
->p
[0]->eq
[0], 2);
750 isl_seq_neg(upper
, set
->p
[0]->eq
[0], 2);
752 isl_seq_neg(lower
, set
->p
[0]->eq
[0], 2);
753 isl_seq_cpy(upper
, set
->p
[0]->eq
[0], 2);
756 for (j
= 0; j
< set
->p
[0]->n_ineq
; ++j
) {
757 if (isl_int_is_pos(set
->p
[0]->ineq
[j
][1])) {
759 isl_seq_cpy(lower
, set
->p
[0]->ineq
[j
], 2);
762 isl_seq_cpy(upper
, set
->p
[0]->ineq
[j
], 2);
769 for (i
= 0; i
< set
->n
; ++i
) {
770 struct isl_basic_set
*bset
= set
->p
[i
];
774 for (j
= 0; j
< bset
->n_eq
; ++j
) {
778 isl_int_mul(a
, lower
[0], bset
->eq
[j
][1]);
779 isl_int_mul(b
, lower
[1], bset
->eq
[j
][0]);
780 if (isl_int_lt(a
, b
) && isl_int_is_pos(bset
->eq
[j
][1]))
781 isl_seq_cpy(lower
, bset
->eq
[j
], 2);
782 if (isl_int_gt(a
, b
) && isl_int_is_neg(bset
->eq
[j
][1]))
783 isl_seq_neg(lower
, bset
->eq
[j
], 2);
786 isl_int_mul(a
, upper
[0], bset
->eq
[j
][1]);
787 isl_int_mul(b
, upper
[1], bset
->eq
[j
][0]);
788 if (isl_int_lt(a
, b
) && isl_int_is_pos(bset
->eq
[j
][1]))
789 isl_seq_neg(upper
, bset
->eq
[j
], 2);
790 if (isl_int_gt(a
, b
) && isl_int_is_neg(bset
->eq
[j
][1]))
791 isl_seq_cpy(upper
, bset
->eq
[j
], 2);
794 for (j
= 0; j
< bset
->n_ineq
; ++j
) {
795 if (isl_int_is_pos(bset
->ineq
[j
][1]))
797 if (isl_int_is_neg(bset
->ineq
[j
][1]))
799 if (lower
&& isl_int_is_pos(bset
->ineq
[j
][1])) {
800 isl_int_mul(a
, lower
[0], bset
->ineq
[j
][1]);
801 isl_int_mul(b
, lower
[1], bset
->ineq
[j
][0]);
802 if (isl_int_lt(a
, b
))
803 isl_seq_cpy(lower
, bset
->ineq
[j
], 2);
805 if (upper
&& isl_int_is_neg(bset
->ineq
[j
][1])) {
806 isl_int_mul(a
, upper
[0], bset
->ineq
[j
][1]);
807 isl_int_mul(b
, upper
[1], bset
->ineq
[j
][0]);
808 if (isl_int_gt(a
, b
))
809 isl_seq_cpy(upper
, bset
->ineq
[j
], 2);
820 hull
= isl_basic_set_alloc(set
->ctx
, 0, 1, 0, 0, 2);
821 hull
= isl_basic_set_set_rational(hull
);
825 k
= isl_basic_set_alloc_inequality(hull
);
826 isl_seq_cpy(hull
->ineq
[k
], lower
, 2);
829 k
= isl_basic_set_alloc_inequality(hull
);
830 isl_seq_cpy(hull
->ineq
[k
], upper
, 2);
832 hull
= isl_basic_set_finalize(hull
);
842 /* Project out final n dimensions using Fourier-Motzkin */
843 static struct isl_set
*set_project_out(struct isl_ctx
*ctx
,
844 struct isl_set
*set
, unsigned n
)
846 return isl_set_remove_dims(set
, isl_set_n_dim(set
) - n
, n
);
849 static struct isl_basic_set
*convex_hull_0d(struct isl_set
*set
)
851 struct isl_basic_set
*convex_hull
;
856 if (isl_set_is_empty(set
))
857 convex_hull
= isl_basic_set_empty(isl_dim_copy(set
->dim
));
859 convex_hull
= isl_basic_set_universe(isl_dim_copy(set
->dim
));
864 /* Compute the convex hull of a pair of basic sets without any parameters or
865 * integer divisions using Fourier-Motzkin elimination.
866 * The convex hull is the set of all points that can be written as
867 * the sum of points from both basic sets (in homogeneous coordinates).
868 * We set up the constraints in a space with dimensions for each of
869 * the three sets and then project out the dimensions corresponding
870 * to the two original basic sets, retaining only those corresponding
871 * to the convex hull.
873 static struct isl_basic_set
*convex_hull_pair_elim(struct isl_basic_set
*bset1
,
874 struct isl_basic_set
*bset2
)
877 struct isl_basic_set
*bset
[2];
878 struct isl_basic_set
*hull
= NULL
;
881 if (!bset1
|| !bset2
)
884 dim
= isl_basic_set_n_dim(bset1
);
885 hull
= isl_basic_set_alloc(bset1
->ctx
, 0, 2 + 3 * dim
, 0,
886 1 + dim
+ bset1
->n_eq
+ bset2
->n_eq
,
887 2 + bset1
->n_ineq
+ bset2
->n_ineq
);
890 for (i
= 0; i
< 2; ++i
) {
891 for (j
= 0; j
< bset
[i
]->n_eq
; ++j
) {
892 k
= isl_basic_set_alloc_equality(hull
);
895 isl_seq_clr(hull
->eq
[k
], (i
+1) * (1+dim
));
896 isl_seq_clr(hull
->eq
[k
]+(i
+2)*(1+dim
), (1-i
)*(1+dim
));
897 isl_seq_cpy(hull
->eq
[k
]+(i
+1)*(1+dim
), bset
[i
]->eq
[j
],
900 for (j
= 0; j
< bset
[i
]->n_ineq
; ++j
) {
901 k
= isl_basic_set_alloc_inequality(hull
);
904 isl_seq_clr(hull
->ineq
[k
], (i
+1) * (1+dim
));
905 isl_seq_clr(hull
->ineq
[k
]+(i
+2)*(1+dim
), (1-i
)*(1+dim
));
906 isl_seq_cpy(hull
->ineq
[k
]+(i
+1)*(1+dim
),
907 bset
[i
]->ineq
[j
], 1+dim
);
909 k
= isl_basic_set_alloc_inequality(hull
);
912 isl_seq_clr(hull
->ineq
[k
], 1+2+3*dim
);
913 isl_int_set_si(hull
->ineq
[k
][(i
+1)*(1+dim
)], 1);
915 for (j
= 0; j
< 1+dim
; ++j
) {
916 k
= isl_basic_set_alloc_equality(hull
);
919 isl_seq_clr(hull
->eq
[k
], 1+2+3*dim
);
920 isl_int_set_si(hull
->eq
[k
][j
], -1);
921 isl_int_set_si(hull
->eq
[k
][1+dim
+j
], 1);
922 isl_int_set_si(hull
->eq
[k
][2*(1+dim
)+j
], 1);
924 hull
= isl_basic_set_set_rational(hull
);
925 hull
= isl_basic_set_remove_dims(hull
, dim
, 2*(1+dim
));
926 hull
= isl_basic_set_convex_hull(hull
);
927 isl_basic_set_free(bset1
);
928 isl_basic_set_free(bset2
);
931 isl_basic_set_free(bset1
);
932 isl_basic_set_free(bset2
);
933 isl_basic_set_free(hull
);
937 static int isl_basic_set_is_bounded(struct isl_basic_set
*bset
)
942 tab
= isl_tab_from_recession_cone((struct isl_basic_map
*)bset
);
943 bounded
= isl_tab_cone_is_bounded(tab
);
948 static int isl_set_is_bounded(struct isl_set
*set
)
952 for (i
= 0; i
< set
->n
; ++i
) {
953 int bounded
= isl_basic_set_is_bounded(set
->p
[i
]);
954 if (!bounded
|| bounded
< 0)
960 /* Compute the lineality space of the convex hull of bset1 and bset2.
962 * We first compute the intersection of the recession cone of bset1
963 * with the negative of the recession cone of bset2 and then compute
964 * the linear hull of the resulting cone.
966 static struct isl_basic_set
*induced_lineality_space(
967 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
970 struct isl_basic_set
*lin
= NULL
;
973 if (!bset1
|| !bset2
)
976 dim
= isl_basic_set_total_dim(bset1
);
977 lin
= isl_basic_set_alloc_dim(isl_basic_set_get_dim(bset1
), 0,
978 bset1
->n_eq
+ bset2
->n_eq
,
979 bset1
->n_ineq
+ bset2
->n_ineq
);
980 lin
= isl_basic_set_set_rational(lin
);
983 for (i
= 0; i
< bset1
->n_eq
; ++i
) {
984 k
= isl_basic_set_alloc_equality(lin
);
987 isl_int_set_si(lin
->eq
[k
][0], 0);
988 isl_seq_cpy(lin
->eq
[k
] + 1, bset1
->eq
[i
] + 1, dim
);
990 for (i
= 0; i
< bset1
->n_ineq
; ++i
) {
991 k
= isl_basic_set_alloc_inequality(lin
);
994 isl_int_set_si(lin
->ineq
[k
][0], 0);
995 isl_seq_cpy(lin
->ineq
[k
] + 1, bset1
->ineq
[i
] + 1, dim
);
997 for (i
= 0; i
< bset2
->n_eq
; ++i
) {
998 k
= isl_basic_set_alloc_equality(lin
);
1001 isl_int_set_si(lin
->eq
[k
][0], 0);
1002 isl_seq_neg(lin
->eq
[k
] + 1, bset2
->eq
[i
] + 1, dim
);
1004 for (i
= 0; i
< bset2
->n_ineq
; ++i
) {
1005 k
= isl_basic_set_alloc_inequality(lin
);
1008 isl_int_set_si(lin
->ineq
[k
][0], 0);
1009 isl_seq_neg(lin
->ineq
[k
] + 1, bset2
->ineq
[i
] + 1, dim
);
1012 isl_basic_set_free(bset1
);
1013 isl_basic_set_free(bset2
);
1014 return isl_basic_set_affine_hull(lin
);
1016 isl_basic_set_free(lin
);
1017 isl_basic_set_free(bset1
);
1018 isl_basic_set_free(bset2
);
1022 static struct isl_basic_set
*uset_convex_hull(struct isl_set
*set
);
1024 /* Given a set and a linear space "lin" of dimension n > 0,
1025 * project the linear space from the set, compute the convex hull
1026 * and then map the set back to the original space.
1032 * describe the linear space. We first compute the Hermite normal
1033 * form H = M U of M = H Q, to obtain
1037 * The last n rows of H will be zero, so the last n variables of x' = Q x
1038 * are the one we want to project out. We do this by transforming each
1039 * basic set A x >= b to A U x' >= b and then removing the last n dimensions.
1040 * After computing the convex hull in x'_1, i.e., A' x'_1 >= b',
1041 * we transform the hull back to the original space as A' Q_1 x >= b',
1042 * with Q_1 all but the last n rows of Q.
1044 static struct isl_basic_set
*modulo_lineality(struct isl_set
*set
,
1045 struct isl_basic_set
*lin
)
1047 unsigned total
= isl_basic_set_total_dim(lin
);
1049 struct isl_basic_set
*hull
;
1050 struct isl_mat
*M
, *U
, *Q
;
1054 lin_dim
= total
- lin
->n_eq
;
1055 M
= isl_mat_sub_alloc(set
->ctx
, lin
->eq
, 0, lin
->n_eq
, 1, total
);
1056 M
= isl_mat_left_hermite(M
, 0, &U
, &Q
);
1060 isl_basic_set_free(lin
);
1062 Q
= isl_mat_drop_rows(Q
, Q
->n_row
- lin_dim
, lin_dim
);
1064 U
= isl_mat_lin_to_aff(U
);
1065 Q
= isl_mat_lin_to_aff(Q
);
1067 set
= isl_set_preimage(set
, U
);
1068 set
= isl_set_remove_dims(set
, total
- lin_dim
, lin_dim
);
1069 hull
= uset_convex_hull(set
);
1070 hull
= isl_basic_set_preimage(hull
, Q
);
1074 isl_basic_set_free(lin
);
1079 /* Given two polyhedra with as constraints h_{ij} x >= 0 in homegeneous space,
1080 * set up an LP for solving
1082 * \sum_j \alpha_{1j} h_{1j} = \sum_j \alpha_{2j} h_{2j}
1084 * \alpha{i0} corresponds to the (implicit) positivity constraint 1 >= 0
1085 * The next \alpha{ij} correspond to the equalities and come in pairs.
1086 * The final \alpha{ij} correspond to the inequalities.
1088 static struct isl_basic_set
*valid_direction_lp(
1089 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
1091 struct isl_dim
*dim
;
1092 struct isl_basic_set
*lp
;
1097 if (!bset1
|| !bset2
)
1099 d
= 1 + isl_basic_set_total_dim(bset1
);
1101 2 * bset1
->n_eq
+ bset1
->n_ineq
+ 2 * bset2
->n_eq
+ bset2
->n_ineq
;
1102 dim
= isl_dim_set_alloc(bset1
->ctx
, 0, n
);
1103 lp
= isl_basic_set_alloc_dim(dim
, 0, d
, n
);
1106 for (i
= 0; i
< n
; ++i
) {
1107 k
= isl_basic_set_alloc_inequality(lp
);
1110 isl_seq_clr(lp
->ineq
[k
] + 1, n
);
1111 isl_int_set_si(lp
->ineq
[k
][0], -1);
1112 isl_int_set_si(lp
->ineq
[k
][1 + i
], 1);
1114 for (i
= 0; i
< d
; ++i
) {
1115 k
= isl_basic_set_alloc_equality(lp
);
1119 isl_int_set_si(lp
->eq
[k
][n
++], 0);
1120 /* positivity constraint 1 >= 0 */
1121 isl_int_set_si(lp
->eq
[k
][n
++], i
== 0);
1122 for (j
= 0; j
< bset1
->n_eq
; ++j
) {
1123 isl_int_set(lp
->eq
[k
][n
++], bset1
->eq
[j
][i
]);
1124 isl_int_neg(lp
->eq
[k
][n
++], bset1
->eq
[j
][i
]);
1126 for (j
= 0; j
< bset1
->n_ineq
; ++j
)
1127 isl_int_set(lp
->eq
[k
][n
++], bset1
->ineq
[j
][i
]);
1128 /* positivity constraint 1 >= 0 */
1129 isl_int_set_si(lp
->eq
[k
][n
++], -(i
== 0));
1130 for (j
= 0; j
< bset2
->n_eq
; ++j
) {
1131 isl_int_neg(lp
->eq
[k
][n
++], bset2
->eq
[j
][i
]);
1132 isl_int_set(lp
->eq
[k
][n
++], bset2
->eq
[j
][i
]);
1134 for (j
= 0; j
< bset2
->n_ineq
; ++j
)
1135 isl_int_neg(lp
->eq
[k
][n
++], bset2
->ineq
[j
][i
]);
1137 lp
= isl_basic_set_gauss(lp
, NULL
);
1138 isl_basic_set_free(bset1
);
1139 isl_basic_set_free(bset2
);
1142 isl_basic_set_free(bset1
);
1143 isl_basic_set_free(bset2
);
1147 /* Compute a vector s in the homogeneous space such that <s, r> > 0
1148 * for all rays in the homogeneous space of the two cones that correspond
1149 * to the input polyhedra bset1 and bset2.
1151 * We compute s as a vector that satisfies
1153 * s = \sum_j \alpha_{ij} h_{ij} for i = 1,2 (*)
1155 * with h_{ij} the normals of the facets of polyhedron i
1156 * (including the "positivity constraint" 1 >= 0) and \alpha_{ij}
1157 * strictly positive numbers. For simplicity we impose \alpha_{ij} >= 1.
1158 * We first set up an LP with as variables the \alpha{ij}.
1159 * In this formulateion, for each polyhedron i,
1160 * the first constraint is the positivity constraint, followed by pairs
1161 * of variables for the equalities, followed by variables for the inequalities.
1162 * We then simply pick a feasible solution and compute s using (*).
1164 * Note that we simply pick any valid direction and make no attempt
1165 * to pick a "good" or even the "best" valid direction.
1167 static struct isl_vec
*valid_direction(
1168 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
1170 struct isl_basic_set
*lp
;
1171 struct isl_tab
*tab
;
1172 struct isl_vec
*sample
= NULL
;
1173 struct isl_vec
*dir
;
1178 if (!bset1
|| !bset2
)
1180 lp
= valid_direction_lp(isl_basic_set_copy(bset1
),
1181 isl_basic_set_copy(bset2
));
1182 tab
= isl_tab_from_basic_set(lp
);
1183 sample
= isl_tab_get_sample_value(tab
);
1185 isl_basic_set_free(lp
);
1188 d
= isl_basic_set_total_dim(bset1
);
1189 dir
= isl_vec_alloc(bset1
->ctx
, 1 + d
);
1192 isl_seq_clr(dir
->block
.data
+ 1, dir
->size
- 1);
1194 /* positivity constraint 1 >= 0 */
1195 isl_int_set(dir
->block
.data
[0], sample
->block
.data
[n
++]);
1196 for (i
= 0; i
< bset1
->n_eq
; ++i
) {
1197 isl_int_sub(sample
->block
.data
[n
],
1198 sample
->block
.data
[n
], sample
->block
.data
[n
+1]);
1199 isl_seq_combine(dir
->block
.data
,
1200 bset1
->ctx
->one
, dir
->block
.data
,
1201 sample
->block
.data
[n
], bset1
->eq
[i
], 1 + d
);
1205 for (i
= 0; i
< bset1
->n_ineq
; ++i
)
1206 isl_seq_combine(dir
->block
.data
,
1207 bset1
->ctx
->one
, dir
->block
.data
,
1208 sample
->block
.data
[n
++], bset1
->ineq
[i
], 1 + d
);
1209 isl_vec_free(sample
);
1210 isl_seq_normalize(bset1
->ctx
, dir
->block
.data
+ 1, dir
->size
- 1);
1211 isl_basic_set_free(bset1
);
1212 isl_basic_set_free(bset2
);
1215 isl_vec_free(sample
);
1216 isl_basic_set_free(bset1
);
1217 isl_basic_set_free(bset2
);
1221 /* Given a polyhedron b_i + A_i x >= 0 and a map T = S^{-1},
1222 * compute b_i' + A_i' x' >= 0, with
1224 * [ b_i A_i ] [ y' ] [ y' ]
1225 * [ 1 0 ] S^{-1} [ x' ] >= 0 or [ b_i' A_i' ] [ x' ] >= 0
1227 * In particular, add the "positivity constraint" and then perform
1230 static struct isl_basic_set
*homogeneous_map(struct isl_basic_set
*bset
,
1237 bset
= isl_basic_set_extend_constraints(bset
, 0, 1);
1238 k
= isl_basic_set_alloc_inequality(bset
);
1241 isl_seq_clr(bset
->ineq
[k
] + 1, isl_basic_set_total_dim(bset
));
1242 isl_int_set_si(bset
->ineq
[k
][0], 1);
1243 bset
= isl_basic_set_preimage(bset
, T
);
1247 isl_basic_set_free(bset
);
1251 /* Compute the convex hull of a pair of basic sets without any parameters or
1252 * integer divisions, where the convex hull is known to be pointed,
1253 * but the basic sets may be unbounded.
1255 * We turn this problem into the computation of a convex hull of a pair
1256 * _bounded_ polyhedra by "changing the direction of the homogeneous
1257 * dimension". This idea is due to Matthias Koeppe.
1259 * Consider the cones in homogeneous space that correspond to the
1260 * input polyhedra. The rays of these cones are also rays of the
1261 * polyhedra if the coordinate that corresponds to the homogeneous
1262 * dimension is zero. That is, if the inner product of the rays
1263 * with the homogeneous direction is zero.
1264 * The cones in the homogeneous space can also be considered to
1265 * correspond to other pairs of polyhedra by chosing a different
1266 * homogeneous direction. To ensure that both of these polyhedra
1267 * are bounded, we need to make sure that all rays of the cones
1268 * correspond to vertices and not to rays.
1269 * Let s be a direction such that <s, r> > 0 for all rays r of both cones.
1270 * Then using s as a homogeneous direction, we obtain a pair of polytopes.
1271 * The vector s is computed in valid_direction.
1273 * Note that we need to consider _all_ rays of the cones and not just
1274 * the rays that correspond to rays in the polyhedra. If we were to
1275 * only consider those rays and turn them into vertices, then we
1276 * may inadvertently turn some vertices into rays.
1278 * The standard homogeneous direction is the unit vector in the 0th coordinate.
1279 * We therefore transform the two polyhedra such that the selected
1280 * direction is mapped onto this standard direction and then proceed
1281 * with the normal computation.
1282 * Let S be a non-singular square matrix with s as its first row,
1283 * then we want to map the polyhedra to the space
1285 * [ y' ] [ y ] [ y ] [ y' ]
1286 * [ x' ] = S [ x ] i.e., [ x ] = S^{-1} [ x' ]
1288 * We take S to be the unimodular completion of s to limit the growth
1289 * of the coefficients in the following computations.
1291 * Let b_i + A_i x >= 0 be the constraints of polyhedron i.
1292 * We first move to the homogeneous dimension
1294 * b_i y + A_i x >= 0 [ b_i A_i ] [ y ] [ 0 ]
1295 * y >= 0 or [ 1 0 ] [ x ] >= [ 0 ]
1297 * Then we change directoin
1299 * [ b_i A_i ] [ y' ] [ y' ]
1300 * [ 1 0 ] S^{-1} [ x' ] >= 0 or [ b_i' A_i' ] [ x' ] >= 0
1302 * Then we compute the convex hull of the polytopes b_i' + A_i' x' >= 0
1303 * resulting in b' + A' x' >= 0, which we then convert back
1306 * [ b' A' ] S [ x ] >= 0 or [ b A ] [ x ] >= 0
1308 * The polyhedron b + A x >= 0 is then the convex hull of the input polyhedra.
1310 static struct isl_basic_set
*convex_hull_pair_pointed(
1311 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
1313 struct isl_ctx
*ctx
= NULL
;
1314 struct isl_vec
*dir
= NULL
;
1315 struct isl_mat
*T
= NULL
;
1316 struct isl_mat
*T2
= NULL
;
1317 struct isl_basic_set
*hull
;
1318 struct isl_set
*set
;
1320 if (!bset1
|| !bset2
)
1323 dir
= valid_direction(isl_basic_set_copy(bset1
),
1324 isl_basic_set_copy(bset2
));
1327 T
= isl_mat_alloc(bset1
->ctx
, dir
->size
, dir
->size
);
1330 isl_seq_cpy(T
->row
[0], dir
->block
.data
, dir
->size
);
1331 T
= isl_mat_unimodular_complete(T
, 1);
1332 T2
= isl_mat_right_inverse(isl_mat_copy(T
));
1334 bset1
= homogeneous_map(bset1
, isl_mat_copy(T2
));
1335 bset2
= homogeneous_map(bset2
, T2
);
1336 set
= isl_set_alloc_dim(isl_basic_set_get_dim(bset1
), 2, 0);
1337 set
= isl_set_add(set
, bset1
);
1338 set
= isl_set_add(set
, bset2
);
1339 hull
= uset_convex_hull(set
);
1340 hull
= isl_basic_set_preimage(hull
, T
);
1347 isl_basic_set_free(bset1
);
1348 isl_basic_set_free(bset2
);
1352 /* Compute the convex hull of a pair of basic sets without any parameters or
1353 * integer divisions.
1355 * If the convex hull of the two basic sets would have a non-trivial
1356 * lineality space, we first project out this lineality space.
1358 static struct isl_basic_set
*convex_hull_pair(struct isl_basic_set
*bset1
,
1359 struct isl_basic_set
*bset2
)
1361 struct isl_basic_set
*lin
;
1363 if (isl_basic_set_is_bounded(bset1
) || isl_basic_set_is_bounded(bset2
))
1364 return convex_hull_pair_pointed(bset1
, bset2
);
1366 lin
= induced_lineality_space(isl_basic_set_copy(bset1
),
1367 isl_basic_set_copy(bset2
));
1370 if (isl_basic_set_is_universe(lin
)) {
1371 isl_basic_set_free(bset1
);
1372 isl_basic_set_free(bset2
);
1375 if (lin
->n_eq
< isl_basic_set_total_dim(lin
)) {
1376 struct isl_set
*set
;
1377 set
= isl_set_alloc_dim(isl_basic_set_get_dim(bset1
), 2, 0);
1378 set
= isl_set_add(set
, bset1
);
1379 set
= isl_set_add(set
, bset2
);
1380 return modulo_lineality(set
, lin
);
1382 isl_basic_set_free(lin
);
1384 return convex_hull_pair_pointed(bset1
, bset2
);
1386 isl_basic_set_free(bset1
);
1387 isl_basic_set_free(bset2
);
1391 /* Compute the lineality space of a basic set.
1392 * We currently do not allow the basic set to have any divs.
1393 * We basically just drop the constants and turn every inequality
1396 struct isl_basic_set
*isl_basic_set_lineality_space(struct isl_basic_set
*bset
)
1399 struct isl_basic_set
*lin
= NULL
;
1404 isl_assert(bset
->ctx
, bset
->n_div
== 0, goto error
);
1405 dim
= isl_basic_set_total_dim(bset
);
1407 lin
= isl_basic_set_alloc_dim(isl_basic_set_get_dim(bset
), 0, dim
, 0);
1410 for (i
= 0; i
< bset
->n_eq
; ++i
) {
1411 k
= isl_basic_set_alloc_equality(lin
);
1414 isl_int_set_si(lin
->eq
[k
][0], 0);
1415 isl_seq_cpy(lin
->eq
[k
] + 1, bset
->eq
[i
] + 1, dim
);
1417 lin
= isl_basic_set_gauss(lin
, NULL
);
1420 for (i
= 0; i
< bset
->n_ineq
&& lin
->n_eq
< dim
; ++i
) {
1421 k
= isl_basic_set_alloc_equality(lin
);
1424 isl_int_set_si(lin
->eq
[k
][0], 0);
1425 isl_seq_cpy(lin
->eq
[k
] + 1, bset
->ineq
[i
] + 1, dim
);
1426 lin
= isl_basic_set_gauss(lin
, NULL
);
1430 isl_basic_set_free(bset
);
1433 isl_basic_set_free(lin
);
1434 isl_basic_set_free(bset
);
1438 /* Compute the (linear) hull of the lineality spaces of the basic sets in the
1439 * "underlying" set "set".
1441 static struct isl_basic_set
*uset_combined_lineality_space(struct isl_set
*set
)
1444 struct isl_set
*lin
= NULL
;
1449 struct isl_dim
*dim
= isl_set_get_dim(set
);
1451 return isl_basic_set_empty(dim
);
1454 lin
= isl_set_alloc_dim(isl_set_get_dim(set
), set
->n
, 0);
1455 for (i
= 0; i
< set
->n
; ++i
)
1456 lin
= isl_set_add(lin
,
1457 isl_basic_set_lineality_space(isl_basic_set_copy(set
->p
[i
])));
1459 return isl_set_affine_hull(lin
);
1462 /* Compute the convex hull of a set without any parameters or
1463 * integer divisions.
1464 * In each step, we combined two basic sets until only one
1465 * basic set is left.
1466 * The input basic sets are assumed not to have a non-trivial
1467 * lineality space. If any of the intermediate results has
1468 * a non-trivial lineality space, it is projected out.
1470 static struct isl_basic_set
*uset_convex_hull_unbounded(struct isl_set
*set
)
1472 struct isl_basic_set
*convex_hull
= NULL
;
1474 convex_hull
= isl_set_copy_basic_set(set
);
1475 set
= isl_set_drop_basic_set(set
, convex_hull
);
1478 while (set
->n
> 0) {
1479 struct isl_basic_set
*t
;
1480 t
= isl_set_copy_basic_set(set
);
1483 set
= isl_set_drop_basic_set(set
, t
);
1486 convex_hull
= convex_hull_pair(convex_hull
, t
);
1489 t
= isl_basic_set_lineality_space(isl_basic_set_copy(convex_hull
));
1492 if (isl_basic_set_is_universe(t
)) {
1493 isl_basic_set_free(convex_hull
);
1497 if (t
->n_eq
< isl_basic_set_total_dim(t
)) {
1498 set
= isl_set_add(set
, convex_hull
);
1499 return modulo_lineality(set
, t
);
1501 isl_basic_set_free(t
);
1507 isl_basic_set_free(convex_hull
);
1511 /* Compute an initial hull for wrapping containing a single initial
1512 * facet by first computing bounds on the set and then using these
1513 * bounds to construct an initial facet.
1514 * This function is a remnant of an older implementation where the
1515 * bounds were also used to check whether the set was bounded.
1516 * Since this function will now only be called when we know the
1517 * set to be bounded, the initial facet should probably be constructed
1518 * by simply using the coordinate directions instead.
1520 static struct isl_basic_set
*initial_hull(struct isl_basic_set
*hull
,
1521 struct isl_set
*set
)
1523 struct isl_mat
*bounds
= NULL
;
1529 bounds
= independent_bounds(set
);
1532 isl_assert(set
->ctx
, bounds
->n_row
== isl_set_n_dim(set
), goto error
);
1533 bounds
= initial_facet_constraint(set
, bounds
);
1536 k
= isl_basic_set_alloc_inequality(hull
);
1539 dim
= isl_set_n_dim(set
);
1540 isl_assert(set
->ctx
, 1 + dim
== bounds
->n_col
, goto error
);
1541 isl_seq_cpy(hull
->ineq
[k
], bounds
->row
[0], bounds
->n_col
);
1542 isl_mat_free(bounds
);
1546 isl_basic_set_free(hull
);
1547 isl_mat_free(bounds
);
1551 struct max_constraint
{
1557 static int max_constraint_equal(const void *entry
, const void *val
)
1559 struct max_constraint
*a
= (struct max_constraint
*)entry
;
1560 isl_int
*b
= (isl_int
*)val
;
1562 return isl_seq_eq(a
->c
->row
[0] + 1, b
, a
->c
->n_col
- 1);
1565 static void update_constraint(struct isl_ctx
*ctx
, struct isl_hash_table
*table
,
1566 isl_int
*con
, unsigned len
, int n
, int ineq
)
1568 struct isl_hash_table_entry
*entry
;
1569 struct max_constraint
*c
;
1572 c_hash
= isl_seq_get_hash(con
+ 1, len
);
1573 entry
= isl_hash_table_find(ctx
, table
, c_hash
, max_constraint_equal
,
1579 isl_hash_table_remove(ctx
, table
, entry
);
1583 if (isl_int_gt(c
->c
->row
[0][0], con
[0]))
1585 if (isl_int_eq(c
->c
->row
[0][0], con
[0])) {
1590 c
->c
= isl_mat_cow(c
->c
);
1591 isl_int_set(c
->c
->row
[0][0], con
[0]);
1595 /* Check whether the constraint hash table "table" constains the constraint
1598 static int has_constraint(struct isl_ctx
*ctx
, struct isl_hash_table
*table
,
1599 isl_int
*con
, unsigned len
, int n
)
1601 struct isl_hash_table_entry
*entry
;
1602 struct max_constraint
*c
;
1605 c_hash
= isl_seq_get_hash(con
+ 1, len
);
1606 entry
= isl_hash_table_find(ctx
, table
, c_hash
, max_constraint_equal
,
1613 return isl_int_eq(c
->c
->row
[0][0], con
[0]);
1616 /* Check for inequality constraints of a basic set without equalities
1617 * such that the same or more stringent copies of the constraint appear
1618 * in all of the basic sets. Such constraints are necessarily facet
1619 * constraints of the convex hull.
1621 * If the resulting basic set is by chance identical to one of
1622 * the basic sets in "set", then we know that this basic set contains
1623 * all other basic sets and is therefore the convex hull of set.
1624 * In this case we set *is_hull to 1.
1626 static struct isl_basic_set
*common_constraints(struct isl_basic_set
*hull
,
1627 struct isl_set
*set
, int *is_hull
)
1630 int min_constraints
;
1632 struct max_constraint
*constraints
= NULL
;
1633 struct isl_hash_table
*table
= NULL
;
1638 for (i
= 0; i
< set
->n
; ++i
)
1639 if (set
->p
[i
]->n_eq
== 0)
1643 min_constraints
= set
->p
[i
]->n_ineq
;
1645 for (i
= best
+ 1; i
< set
->n
; ++i
) {
1646 if (set
->p
[i
]->n_eq
!= 0)
1648 if (set
->p
[i
]->n_ineq
>= min_constraints
)
1650 min_constraints
= set
->p
[i
]->n_ineq
;
1653 constraints
= isl_calloc_array(hull
->ctx
, struct max_constraint
,
1657 table
= isl_alloc_type(hull
->ctx
, struct isl_hash_table
);
1658 if (isl_hash_table_init(hull
->ctx
, table
, min_constraints
))
1661 total
= isl_dim_total(set
->dim
);
1662 for (i
= 0; i
< set
->p
[best
]->n_ineq
; ++i
) {
1663 constraints
[i
].c
= isl_mat_sub_alloc(hull
->ctx
,
1664 set
->p
[best
]->ineq
+ i
, 0, 1, 0, 1 + total
);
1665 if (!constraints
[i
].c
)
1667 constraints
[i
].ineq
= 1;
1669 for (i
= 0; i
< min_constraints
; ++i
) {
1670 struct isl_hash_table_entry
*entry
;
1672 c_hash
= isl_seq_get_hash(constraints
[i
].c
->row
[0] + 1, total
);
1673 entry
= isl_hash_table_find(hull
->ctx
, table
, c_hash
,
1674 max_constraint_equal
, constraints
[i
].c
->row
[0] + 1, 1);
1677 isl_assert(hull
->ctx
, !entry
->data
, goto error
);
1678 entry
->data
= &constraints
[i
];
1682 for (s
= 0; s
< set
->n
; ++s
) {
1686 for (i
= 0; i
< set
->p
[s
]->n_eq
; ++i
) {
1687 isl_int
*eq
= set
->p
[s
]->eq
[i
];
1688 for (j
= 0; j
< 2; ++j
) {
1689 isl_seq_neg(eq
, eq
, 1 + total
);
1690 update_constraint(hull
->ctx
, table
,
1694 for (i
= 0; i
< set
->p
[s
]->n_ineq
; ++i
) {
1695 isl_int
*ineq
= set
->p
[s
]->ineq
[i
];
1696 update_constraint(hull
->ctx
, table
, ineq
, total
, n
,
1697 set
->p
[s
]->n_eq
== 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
) {
1719 isl_int
*ineq
= set
->p
[s
]->ineq
[i
];
1720 if (!has_constraint(hull
->ctx
, table
, ineq
, total
, n
))
1723 if (i
== set
->p
[s
]->n_ineq
)
1727 isl_hash_table_clear(table
);
1728 for (i
= 0; i
< min_constraints
; ++i
)
1729 isl_mat_free(constraints
[i
].c
);
1734 isl_hash_table_clear(table
);
1737 for (i
= 0; i
< min_constraints
; ++i
)
1738 isl_mat_free(constraints
[i
].c
);
1743 /* Create a template for the convex hull of "set" and fill it up
1744 * obvious facet constraints, if any. If the result happens to
1745 * be the convex hull of "set" then *is_hull is set to 1.
1747 static struct isl_basic_set
*proto_hull(struct isl_set
*set
, int *is_hull
)
1749 struct isl_basic_set
*hull
;
1754 for (i
= 0; i
< set
->n
; ++i
) {
1755 n_ineq
+= set
->p
[i
]->n_eq
;
1756 n_ineq
+= set
->p
[i
]->n_ineq
;
1758 hull
= isl_basic_set_alloc_dim(isl_dim_copy(set
->dim
), 0, 0, n_ineq
);
1759 hull
= isl_basic_set_set_rational(hull
);
1762 return common_constraints(hull
, set
, is_hull
);
1765 static struct isl_basic_set
*uset_convex_hull_wrap(struct isl_set
*set
)
1767 struct isl_basic_set
*hull
;
1770 hull
= proto_hull(set
, &is_hull
);
1771 if (hull
&& !is_hull
) {
1772 if (hull
->n_ineq
== 0)
1773 hull
= initial_hull(hull
, set
);
1774 hull
= extend(hull
, set
);
1781 /* Compute the convex hull of a set without any parameters or
1782 * integer divisions. Depending on whether the set is bounded,
1783 * we pass control to the wrapping based convex hull or
1784 * the Fourier-Motzkin elimination based convex hull.
1785 * We also handle a few special cases before checking the boundedness.
1787 static struct isl_basic_set
*uset_convex_hull(struct isl_set
*set
)
1790 struct isl_basic_set
*convex_hull
= NULL
;
1791 struct isl_basic_set
*lin
;
1793 if (isl_set_n_dim(set
) == 0)
1794 return convex_hull_0d(set
);
1796 set
= isl_set_coalesce(set
);
1797 set
= isl_set_set_rational(set
);
1804 convex_hull
= isl_basic_set_copy(set
->p
[0]);
1808 if (isl_set_n_dim(set
) == 1)
1809 return convex_hull_1d(set
);
1811 if (isl_set_is_bounded(set
))
1812 return uset_convex_hull_wrap(set
);
1814 lin
= uset_combined_lineality_space(isl_set_copy(set
));
1817 if (isl_basic_set_is_universe(lin
)) {
1821 if (lin
->n_eq
< isl_basic_set_total_dim(lin
))
1822 return modulo_lineality(set
, lin
);
1823 isl_basic_set_free(lin
);
1825 return uset_convex_hull_unbounded(set
);
1828 isl_basic_set_free(convex_hull
);
1832 /* This is the core procedure, where "set" is a "pure" set, i.e.,
1833 * without parameters or divs and where the convex hull of set is
1834 * known to be full-dimensional.
1836 static struct isl_basic_set
*uset_convex_hull_wrap_bounded(struct isl_set
*set
)
1839 struct isl_basic_set
*convex_hull
= NULL
;
1841 if (isl_set_n_dim(set
) == 0) {
1842 convex_hull
= isl_basic_set_universe(isl_dim_copy(set
->dim
));
1844 convex_hull
= isl_basic_set_set_rational(convex_hull
);
1848 set
= isl_set_set_rational(set
);
1852 set
= isl_set_coalesce(set
);
1856 convex_hull
= isl_basic_set_copy(set
->p
[0]);
1860 if (isl_set_n_dim(set
) == 1)
1861 return convex_hull_1d(set
);
1863 return uset_convex_hull_wrap(set
);
1869 /* Compute the convex hull of set "set" with affine hull "affine_hull",
1870 * We first remove the equalities (transforming the set), compute the
1871 * convex hull of the transformed set and then add the equalities back
1872 * (after performing the inverse transformation.
1874 static struct isl_basic_set
*modulo_affine_hull(struct isl_ctx
*ctx
,
1875 struct isl_set
*set
, struct isl_basic_set
*affine_hull
)
1879 struct isl_basic_set
*dummy
;
1880 struct isl_basic_set
*convex_hull
;
1882 dummy
= isl_basic_set_remove_equalities(
1883 isl_basic_set_copy(affine_hull
), &T
, &T2
);
1886 isl_basic_set_free(dummy
);
1887 set
= isl_set_preimage(set
, T
);
1888 convex_hull
= uset_convex_hull(set
);
1889 convex_hull
= isl_basic_set_preimage(convex_hull
, T2
);
1890 convex_hull
= isl_basic_set_intersect(convex_hull
, affine_hull
);
1893 isl_basic_set_free(affine_hull
);
1898 /* Compute the convex hull of a map.
1900 * The implementation was inspired by "Extended Convex Hull" by Fukuda et al.,
1901 * specifically, the wrapping of facets to obtain new facets.
1903 struct isl_basic_map
*isl_map_convex_hull(struct isl_map
*map
)
1905 struct isl_basic_set
*bset
;
1906 struct isl_basic_map
*model
= NULL
;
1907 struct isl_basic_set
*affine_hull
= NULL
;
1908 struct isl_basic_map
*convex_hull
= NULL
;
1909 struct isl_set
*set
= NULL
;
1910 struct isl_ctx
*ctx
;
1917 convex_hull
= isl_basic_map_empty_like_map(map
);
1922 map
= isl_map_detect_equalities(map
);
1923 map
= isl_map_align_divs(map
);
1924 model
= isl_basic_map_copy(map
->p
[0]);
1925 set
= isl_map_underlying_set(map
);
1929 affine_hull
= isl_set_affine_hull(isl_set_copy(set
));
1932 if (affine_hull
->n_eq
!= 0)
1933 bset
= modulo_affine_hull(ctx
, set
, affine_hull
);
1935 isl_basic_set_free(affine_hull
);
1936 bset
= uset_convex_hull(set
);
1939 convex_hull
= isl_basic_map_overlying_set(bset
, model
);
1941 ISL_F_SET(convex_hull
, ISL_BASIC_MAP_NO_IMPLICIT
);
1942 ISL_F_SET(convex_hull
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1943 ISL_F_CLR(convex_hull
, ISL_BASIC_MAP_RATIONAL
);
1947 isl_basic_map_free(model
);
1951 struct isl_basic_set
*isl_set_convex_hull(struct isl_set
*set
)
1953 return (struct isl_basic_set
*)
1954 isl_map_convex_hull((struct isl_map
*)set
);
1957 struct sh_data_entry
{
1958 struct isl_hash_table
*table
;
1959 struct isl_tab
*tab
;
1962 /* Holds the data needed during the simple hull computation.
1964 * n the number of basic sets in the original set
1965 * hull_table a hash table of already computed constraints
1966 * in the simple hull
1967 * p for each basic set,
1968 * table a hash table of the constraints
1969 * tab the tableau corresponding to the basic set
1972 struct isl_ctx
*ctx
;
1974 struct isl_hash_table
*hull_table
;
1975 struct sh_data_entry p
[1];
1978 static void sh_data_free(struct sh_data
*data
)
1984 isl_hash_table_free(data
->ctx
, data
->hull_table
);
1985 for (i
= 0; i
< data
->n
; ++i
) {
1986 isl_hash_table_free(data
->ctx
, data
->p
[i
].table
);
1987 isl_tab_free(data
->p
[i
].tab
);
1992 struct ineq_cmp_data
{
1997 static int has_ineq(const void *entry
, const void *val
)
1999 isl_int
*row
= (isl_int
*)entry
;
2000 struct ineq_cmp_data
*v
= (struct ineq_cmp_data
*)val
;
2002 return isl_seq_eq(row
+ 1, v
->p
+ 1, v
->len
) ||
2003 isl_seq_is_neg(row
+ 1, v
->p
+ 1, v
->len
);
2006 static int hash_ineq(struct isl_ctx
*ctx
, struct isl_hash_table
*table
,
2007 isl_int
*ineq
, unsigned len
)
2010 struct ineq_cmp_data v
;
2011 struct isl_hash_table_entry
*entry
;
2015 c_hash
= isl_seq_get_hash(ineq
+ 1, len
);
2016 entry
= isl_hash_table_find(ctx
, table
, c_hash
, has_ineq
, &v
, 1);
2023 /* Fill hash table "table" with the constraints of "bset".
2024 * Equalities are added as two inequalities.
2025 * The value in the hash table is a pointer to the (in)equality of "bset".
2027 static int hash_basic_set(struct isl_hash_table
*table
,
2028 struct isl_basic_set
*bset
)
2031 unsigned dim
= isl_basic_set_total_dim(bset
);
2033 for (i
= 0; i
< bset
->n_eq
; ++i
) {
2034 for (j
= 0; j
< 2; ++j
) {
2035 isl_seq_neg(bset
->eq
[i
], bset
->eq
[i
], 1 + dim
);
2036 if (hash_ineq(bset
->ctx
, table
, bset
->eq
[i
], dim
) < 0)
2040 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
2041 if (hash_ineq(bset
->ctx
, table
, bset
->ineq
[i
], dim
) < 0)
2047 static struct sh_data
*sh_data_alloc(struct isl_set
*set
, unsigned n_ineq
)
2049 struct sh_data
*data
;
2052 data
= isl_calloc(set
->ctx
, struct sh_data
,
2053 sizeof(struct sh_data
) +
2054 (set
->n
- 1) * sizeof(struct sh_data_entry
));
2057 data
->ctx
= set
->ctx
;
2059 data
->hull_table
= isl_hash_table_alloc(set
->ctx
, n_ineq
);
2060 if (!data
->hull_table
)
2062 for (i
= 0; i
< set
->n
; ++i
) {
2063 data
->p
[i
].table
= isl_hash_table_alloc(set
->ctx
,
2064 2 * set
->p
[i
]->n_eq
+ set
->p
[i
]->n_ineq
);
2065 if (!data
->p
[i
].table
)
2067 if (hash_basic_set(data
->p
[i
].table
, set
->p
[i
]) < 0)
2076 /* Check if inequality "ineq" is a bound for basic set "j" or if
2077 * it can be relaxed (by increasing the constant term) to become
2078 * a bound for that basic set. In the latter case, the constant
2080 * Return 1 if "ineq" is a bound
2081 * 0 if "ineq" may attain arbitrarily small values on basic set "j"
2082 * -1 if some error occurred
2084 static int is_bound(struct sh_data
*data
, struct isl_set
*set
, int j
,
2087 enum isl_lp_result res
;
2090 if (!data
->p
[j
].tab
) {
2091 data
->p
[j
].tab
= isl_tab_from_basic_set(set
->p
[j
]);
2092 if (!data
->p
[j
].tab
)
2098 res
= isl_tab_min(data
->p
[j
].tab
, ineq
, data
->ctx
->one
,
2100 if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
2101 isl_int_sub(ineq
[0], ineq
[0], opt
);
2105 return res
== isl_lp_ok
? 1 :
2106 res
== isl_lp_unbounded
? 0 : -1;
2109 /* Check if inequality "ineq" from basic set "i" can be relaxed to
2110 * become a bound on the whole set. If so, add the (relaxed) inequality
2113 * We first check if "hull" already contains a translate of the inequality.
2114 * If so, we are done.
2115 * Then, we check if any of the previous basic sets contains a translate
2116 * of the inequality. If so, then we have already considered this
2117 * inequality and we are done.
2118 * Otherwise, for each basic set other than "i", we check if the inequality
2119 * is a bound on the basic set.
2120 * For previous basic sets, we know that they do not contain a translate
2121 * of the inequality, so we directly call is_bound.
2122 * For following basic sets, we first check if a translate of the
2123 * inequality appears in its description and if so directly update
2124 * the inequality accordingly.
2126 static struct isl_basic_set
*add_bound(struct isl_basic_set
*hull
,
2127 struct sh_data
*data
, struct isl_set
*set
, int i
, isl_int
*ineq
)
2130 struct ineq_cmp_data v
;
2131 struct isl_hash_table_entry
*entry
;
2137 v
.len
= isl_basic_set_total_dim(hull
);
2139 c_hash
= isl_seq_get_hash(ineq
+ 1, v
.len
);
2141 entry
= isl_hash_table_find(hull
->ctx
, data
->hull_table
, c_hash
,
2146 for (j
= 0; j
< i
; ++j
) {
2147 entry
= isl_hash_table_find(hull
->ctx
, data
->p
[j
].table
,
2148 c_hash
, has_ineq
, &v
, 0);
2155 k
= isl_basic_set_alloc_inequality(hull
);
2156 isl_seq_cpy(hull
->ineq
[k
], ineq
, 1 + v
.len
);
2160 for (j
= 0; j
< i
; ++j
) {
2162 bound
= is_bound(data
, set
, j
, hull
->ineq
[k
]);
2169 isl_basic_set_free_inequality(hull
, 1);
2173 for (j
= i
+ 1; j
< set
->n
; ++j
) {
2176 entry
= isl_hash_table_find(hull
->ctx
, data
->p
[j
].table
,
2177 c_hash
, has_ineq
, &v
, 0);
2179 ineq_j
= entry
->data
;
2180 neg
= isl_seq_is_neg(ineq_j
+ 1,
2181 hull
->ineq
[k
] + 1, v
.len
);
2183 isl_int_neg(ineq_j
[0], ineq_j
[0]);
2184 if (isl_int_gt(ineq_j
[0], hull
->ineq
[k
][0]))
2185 isl_int_set(hull
->ineq
[k
][0], ineq_j
[0]);
2187 isl_int_neg(ineq_j
[0], ineq_j
[0]);
2190 bound
= is_bound(data
, set
, j
, hull
->ineq
[k
]);
2197 isl_basic_set_free_inequality(hull
, 1);
2201 entry
= isl_hash_table_find(hull
->ctx
, data
->hull_table
, c_hash
,
2205 entry
->data
= hull
->ineq
[k
];
2209 isl_basic_set_free(hull
);
2213 /* Check if any inequality from basic set "i" can be relaxed to
2214 * become a bound on the whole set. If so, add the (relaxed) inequality
2217 static struct isl_basic_set
*add_bounds(struct isl_basic_set
*bset
,
2218 struct sh_data
*data
, struct isl_set
*set
, int i
)
2221 unsigned dim
= isl_basic_set_total_dim(bset
);
2223 for (j
= 0; j
< set
->p
[i
]->n_eq
; ++j
) {
2224 for (k
= 0; k
< 2; ++k
) {
2225 isl_seq_neg(set
->p
[i
]->eq
[j
], set
->p
[i
]->eq
[j
], 1+dim
);
2226 add_bound(bset
, data
, set
, i
, set
->p
[i
]->eq
[j
]);
2229 for (j
= 0; j
< set
->p
[i
]->n_ineq
; ++j
)
2230 add_bound(bset
, data
, set
, i
, set
->p
[i
]->ineq
[j
]);
2234 /* Compute a superset of the convex hull of set that is described
2235 * by only translates of the constraints in the constituents of set.
2237 static struct isl_basic_set
*uset_simple_hull(struct isl_set
*set
)
2239 struct sh_data
*data
= NULL
;
2240 struct isl_basic_set
*hull
= NULL
;
2248 for (i
= 0; i
< set
->n
; ++i
) {
2251 n_ineq
+= 2 * set
->p
[i
]->n_eq
+ set
->p
[i
]->n_ineq
;
2254 hull
= isl_basic_set_alloc_dim(isl_dim_copy(set
->dim
), 0, 0, n_ineq
);
2258 data
= sh_data_alloc(set
, n_ineq
);
2262 for (i
= 0; i
< set
->n
; ++i
)
2263 hull
= add_bounds(hull
, data
, set
, i
);
2271 isl_basic_set_free(hull
);
2276 /* Compute a superset of the convex hull of map that is described
2277 * by only translates of the constraints in the constituents of map.
2279 struct isl_basic_map
*isl_map_simple_hull(struct isl_map
*map
)
2281 struct isl_set
*set
= NULL
;
2282 struct isl_basic_map
*model
= NULL
;
2283 struct isl_basic_map
*hull
;
2284 struct isl_basic_map
*affine_hull
;
2285 struct isl_basic_set
*bset
= NULL
;
2290 hull
= isl_basic_map_empty_like_map(map
);
2295 hull
= isl_basic_map_copy(map
->p
[0]);
2300 map
= isl_map_detect_equalities(map
);
2301 affine_hull
= isl_map_affine_hull(isl_map_copy(map
));
2302 map
= isl_map_align_divs(map
);
2303 model
= isl_basic_map_copy(map
->p
[0]);
2305 set
= isl_map_underlying_set(map
);
2307 bset
= uset_simple_hull(set
);
2309 hull
= isl_basic_map_overlying_set(bset
, model
);
2311 hull
= isl_basic_map_intersect(hull
, affine_hull
);
2312 hull
= isl_basic_map_convex_hull(hull
);
2313 ISL_F_SET(hull
, ISL_BASIC_MAP_NO_IMPLICIT
);
2314 ISL_F_SET(hull
, ISL_BASIC_MAP_ALL_EQUALITIES
);
2319 struct isl_basic_set
*isl_set_simple_hull(struct isl_set
*set
)
2321 return (struct isl_basic_set
*)
2322 isl_map_simple_hull((struct isl_map
*)set
);
2325 /* Given a set "set", return parametric bounds on the dimension "dim".
2327 static struct isl_basic_set
*set_bounds(struct isl_set
*set
, int dim
)
2329 unsigned set_dim
= isl_set_dim(set
, isl_dim_set
);
2330 set
= isl_set_copy(set
);
2331 set
= isl_set_eliminate_dims(set
, dim
+ 1, set_dim
- (dim
+ 1));
2332 set
= isl_set_eliminate_dims(set
, 0, dim
);
2333 return isl_set_convex_hull(set
);
2336 /* Computes a "simple hull" and then check if each dimension in the
2337 * resulting hull is bounded by a symbolic constant. If not, the
2338 * hull is intersected with the corresponding bounds on the whole set.
2340 struct isl_basic_set
*isl_set_bounded_simple_hull(struct isl_set
*set
)
2343 struct isl_basic_set
*hull
;
2344 unsigned nparam
, left
;
2345 int removed_divs
= 0;
2347 hull
= isl_set_simple_hull(isl_set_copy(set
));
2351 nparam
= isl_basic_set_dim(hull
, isl_dim_param
);
2352 for (i
= 0; i
< isl_basic_set_dim(hull
, isl_dim_set
); ++i
) {
2353 int lower
= 0, upper
= 0;
2354 struct isl_basic_set
*bounds
;
2356 left
= isl_basic_set_total_dim(hull
) - nparam
- i
- 1;
2357 for (j
= 0; j
< hull
->n_eq
; ++j
) {
2358 if (isl_int_is_zero(hull
->eq
[j
][1 + nparam
+ i
]))
2360 if (isl_seq_first_non_zero(hull
->eq
[j
]+1+nparam
+i
+1,
2367 for (j
= 0; j
< hull
->n_ineq
; ++j
) {
2368 if (isl_int_is_zero(hull
->ineq
[j
][1 + nparam
+ i
]))
2370 if (isl_seq_first_non_zero(hull
->ineq
[j
]+1+nparam
+i
+1,
2372 isl_seq_first_non_zero(hull
->ineq
[j
]+1+nparam
,
2375 if (isl_int_is_pos(hull
->ineq
[j
][1 + nparam
+ i
]))
2386 if (!removed_divs
) {
2387 set
= isl_set_remove_divs(set
);
2392 bounds
= set_bounds(set
, i
);
2393 hull
= isl_basic_set_intersect(hull
, bounds
);