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_solve_lp(*bmap
, 0, c
, (*bmap
)->ctx
->one
, opt_n
, opt_d
);
54 if (res
== isl_lp_unbounded
)
56 if (res
== isl_lp_error
)
58 if (res
== isl_lp_empty
) {
59 *bmap
= isl_basic_map_set_to_empty(*bmap
);
62 return !isl_int_is_neg(*opt_n
);
65 int isl_basic_set_constraint_is_redundant(struct isl_basic_set
**bset
,
66 isl_int
*c
, isl_int
*opt_n
, isl_int
*opt_d
)
68 return isl_basic_map_constraint_is_redundant(
69 (struct isl_basic_map
**)bset
, c
, opt_n
, opt_d
);
72 /* Compute the convex hull of a basic map, by removing the redundant
73 * constraints. If the minimal value along the normal of a constraint
74 * is the same if the constraint is removed, then the constraint is redundant.
76 * Alternatively, we could have intersected the basic map with the
77 * corresponding equality and the checked if the dimension was that
80 struct isl_basic_map
*isl_basic_map_convex_hull(struct isl_basic_map
*bmap
)
87 bmap
= isl_basic_map_gauss(bmap
, NULL
);
88 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
90 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
))
92 if (bmap
->n_ineq
<= 1)
95 tab
= isl_tab_from_basic_map(bmap
);
96 tab
= isl_tab_detect_equalities(bmap
->ctx
, tab
);
97 tab
= isl_tab_detect_redundant(bmap
->ctx
, tab
);
98 bmap
= isl_basic_map_update_from_tab(bmap
, tab
);
99 isl_tab_free(bmap
->ctx
, tab
);
100 ISL_F_SET(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
101 ISL_F_SET(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
105 struct isl_basic_set
*isl_basic_set_convex_hull(struct isl_basic_set
*bset
)
107 return (struct isl_basic_set
*)
108 isl_basic_map_convex_hull((struct isl_basic_map
*)bset
);
111 /* Check if the set set is bound in the direction of the affine
112 * constraint c and if so, set the constant term such that the
113 * resulting constraint is a bounding constraint for the set.
115 static int uset_is_bound(struct isl_ctx
*ctx
, struct isl_set
*set
,
116 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_solve_lp((struct isl_basic_map
*)set
->p
[j
],
133 0, c
, ctx
->one
, &opt
, &opt_denom
);
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_ctx
*ctx
,
167 struct isl_set
*set
, isl_int
*c
,
168 struct isl_mat
*dirs
, int n
)
173 isl_seq_cpy(dirs
->row
[n
]+1, c
+1, dirs
->n_col
-1);
175 int pos
= isl_seq_first_non_zero(dirs
->row
[n
]+1, dirs
->n_col
-1);
178 for (i
= 0; i
< n
; ++i
) {
180 pos_i
= isl_seq_first_non_zero(dirs
->row
[i
]+1, dirs
->n_col
-1);
185 isl_seq_elim(dirs
->row
[n
]+1, dirs
->row
[i
]+1, pos
,
186 dirs
->n_col
-1, NULL
);
187 pos
= isl_seq_first_non_zero(dirs
->row
[n
]+1, dirs
->n_col
-1);
193 is_bound
= uset_is_bound(ctx
, set
, dirs
->row
[n
], dirs
->n_col
);
198 isl_int
*t
= dirs
->row
[n
];
199 for (k
= n
; k
> i
; --k
)
200 dirs
->row
[k
] = dirs
->row
[k
-1];
206 /* Compute and return a maximal set of linearly independent bounds
207 * on the set "set", based on the constraints of the basic sets
210 static struct isl_mat
*independent_bounds(struct isl_ctx
*ctx
,
214 struct isl_mat
*dirs
= NULL
;
215 unsigned dim
= isl_set_n_dim(set
);
217 dirs
= isl_mat_alloc(ctx
, dim
, 1+dim
);
222 for (i
= 0; n
< dim
&& i
< set
->n
; ++i
) {
224 struct isl_basic_set
*bset
= set
->p
[i
];
226 for (j
= 0; n
< dim
&& j
< bset
->n_eq
; ++j
) {
227 f
= is_independent_bound(ctx
, set
, bset
->eq
[j
],
234 for (j
= 0; n
< dim
&& j
< bset
->n_ineq
; ++j
) {
235 f
= is_independent_bound(ctx
, set
, bset
->ineq
[j
],
246 isl_mat_free(ctx
, dirs
);
250 static struct isl_basic_set
*isl_basic_set_set_rational(
251 struct isl_basic_set
*bset
)
256 if (ISL_F_ISSET(bset
, ISL_BASIC_MAP_RATIONAL
))
259 bset
= isl_basic_set_cow(bset
);
263 ISL_F_SET(bset
, ISL_BASIC_MAP_RATIONAL
);
265 return isl_basic_set_finalize(bset
);
268 static struct isl_set
*isl_set_set_rational(struct isl_set
*set
)
272 set
= isl_set_cow(set
);
275 for (i
= 0; i
< set
->n
; ++i
) {
276 set
->p
[i
] = isl_basic_set_set_rational(set
->p
[i
]);
286 static struct isl_basic_set
*isl_basic_set_add_equality(struct isl_ctx
*ctx
,
287 struct isl_basic_set
*bset
, isl_int
*c
)
293 if (ISL_F_ISSET(bset
, ISL_BASIC_SET_EMPTY
))
296 isl_assert(ctx
, isl_basic_set_n_param(bset
) == 0, goto error
);
297 isl_assert(ctx
, bset
->n_div
== 0, goto error
);
298 dim
= isl_basic_set_n_dim(bset
);
299 bset
= isl_basic_set_cow(bset
);
300 bset
= isl_basic_set_extend(bset
, 0, dim
, 0, 1, 0);
301 i
= isl_basic_set_alloc_equality(bset
);
304 isl_seq_cpy(bset
->eq
[i
], c
, 1 + dim
);
307 isl_basic_set_free(bset
);
311 static struct isl_set
*isl_set_add_equality(struct isl_ctx
*ctx
,
312 struct isl_set
*set
, isl_int
*c
)
316 set
= isl_set_cow(set
);
319 for (i
= 0; i
< set
->n
; ++i
) {
320 set
->p
[i
] = isl_basic_set_add_equality(ctx
, set
->p
[i
], c
);
330 /* Given a union of basic sets, construct the constraints for wrapping
331 * a facet around one of its ridges.
332 * In particular, if each of n the d-dimensional basic sets i in "set"
333 * contains the origin, satisfies the constraints x_1 >= 0 and x_2 >= 0
334 * and is defined by the constraints
338 * then the resulting set is of dimension n*(1+d) and has as constraints
347 static struct isl_basic_set
*wrap_constraints(struct isl_set
*set
)
349 struct isl_basic_set
*lp
;
353 unsigned dim
, lp_dim
;
358 dim
= 1 + isl_set_n_dim(set
);
361 for (i
= 0; i
< set
->n
; ++i
) {
362 n_eq
+= set
->p
[i
]->n_eq
;
363 n_ineq
+= set
->p
[i
]->n_ineq
;
365 lp
= isl_basic_set_alloc(set
->ctx
, 0, dim
* set
->n
, 0, n_eq
, n_ineq
);
368 lp_dim
= isl_basic_set_n_dim(lp
);
369 k
= isl_basic_set_alloc_equality(lp
);
370 isl_int_set_si(lp
->eq
[k
][0], -1);
371 for (i
= 0; i
< set
->n
; ++i
) {
372 isl_int_set_si(lp
->eq
[k
][1+dim
*i
], 0);
373 isl_int_set_si(lp
->eq
[k
][1+dim
*i
+1], 1);
374 isl_seq_clr(lp
->eq
[k
]+1+dim
*i
+2, dim
-2);
376 for (i
= 0; i
< set
->n
; ++i
) {
377 k
= isl_basic_set_alloc_inequality(lp
);
378 isl_seq_clr(lp
->ineq
[k
], 1+lp_dim
);
379 isl_int_set_si(lp
->ineq
[k
][1+dim
*i
], 1);
381 for (j
= 0; j
< set
->p
[i
]->n_eq
; ++j
) {
382 k
= isl_basic_set_alloc_equality(lp
);
383 isl_seq_clr(lp
->eq
[k
], 1+dim
*i
);
384 isl_seq_cpy(lp
->eq
[k
]+1+dim
*i
, set
->p
[i
]->eq
[j
], dim
);
385 isl_seq_clr(lp
->eq
[k
]+1+dim
*(i
+1), dim
*(set
->n
-i
-1));
388 for (j
= 0; j
< set
->p
[i
]->n_ineq
; ++j
) {
389 k
= isl_basic_set_alloc_inequality(lp
);
390 isl_seq_clr(lp
->ineq
[k
], 1+dim
*i
);
391 isl_seq_cpy(lp
->ineq
[k
]+1+dim
*i
, set
->p
[i
]->ineq
[j
], dim
);
392 isl_seq_clr(lp
->ineq
[k
]+1+dim
*(i
+1), dim
*(set
->n
-i
-1));
398 /* Given a facet "facet" of the convex hull of "set" and a facet "ridge"
399 * of that facet, compute the other facet of the convex hull that contains
402 * We first transform the set such that the facet constraint becomes
406 * I.e., the facet lies in
410 * and on that facet, the constraint that defines the ridge is
414 * (This transformation is not strictly needed, all that is needed is
415 * that the ridge contains the origin.)
417 * Since the ridge contains the origin, the cone of the convex hull
418 * will be of the form
423 * with this second constraint defining the new facet.
424 * The constant a is obtained by settting x_1 in the cone of the
425 * convex hull to 1 and minimizing x_2.
426 * Now, each element in the cone of the convex hull is the sum
427 * of elements in the cones of the basic sets.
428 * If a_i is the dilation factor of basic set i, then the problem
429 * we need to solve is
442 * the constraints of each (transformed) basic set.
443 * If a = n/d, then the constraint defining the new facet (in the transformed
446 * -n x_1 + d x_2 >= 0
448 * In the original space, we need to take the same combination of the
449 * corresponding constraints "facet" and "ridge".
451 * Note that a is always finite, since we only apply the wrapping
452 * technique to a union of polytopes.
454 static isl_int
*wrap_facet(struct isl_set
*set
, isl_int
*facet
, isl_int
*ridge
)
457 struct isl_mat
*T
= NULL
;
458 struct isl_basic_set
*lp
= NULL
;
460 enum isl_lp_result res
;
464 set
= isl_set_copy(set
);
466 dim
= 1 + isl_set_n_dim(set
);
467 T
= isl_mat_alloc(set
->ctx
, 3, dim
);
470 isl_int_set_si(T
->row
[0][0], 1);
471 isl_seq_clr(T
->row
[0]+1, dim
- 1);
472 isl_seq_cpy(T
->row
[1], facet
, dim
);
473 isl_seq_cpy(T
->row
[2], ridge
, dim
);
474 T
= isl_mat_right_inverse(set
->ctx
, T
);
475 set
= isl_set_preimage(set
, T
);
479 lp
= wrap_constraints(set
);
480 obj
= isl_vec_alloc(set
->ctx
, 1 + dim
*set
->n
);
483 isl_int_set_si(obj
->block
.data
[0], 0);
484 for (i
= 0; i
< set
->n
; ++i
) {
485 isl_seq_clr(obj
->block
.data
+ 1 + dim
*i
, 2);
486 isl_int_set_si(obj
->block
.data
[1 + dim
*i
+2], 1);
487 isl_seq_clr(obj
->block
.data
+ 1 + dim
*i
+3, dim
-3);
491 res
= isl_solve_lp((struct isl_basic_map
*)lp
, 0,
492 obj
->block
.data
, set
->ctx
->one
, &num
, &den
);
493 if (res
== isl_lp_ok
) {
494 isl_int_neg(num
, num
);
495 isl_seq_combine(facet
, num
, facet
, den
, ridge
, dim
);
499 isl_vec_free(set
->ctx
, obj
);
500 isl_basic_set_free(lp
);
502 isl_assert(set
->ctx
, res
== isl_lp_ok
, return NULL
);
505 isl_basic_set_free(lp
);
506 isl_mat_free(set
->ctx
, T
);
511 /* Given a set of d linearly independent bounding constraints of the
512 * convex hull of "set", compute the constraint of a facet of "set".
514 * We first compute the intersection with the first bounding hyperplane
515 * and remove the component corresponding to this hyperplane from
516 * other bounds (in homogeneous space).
517 * We then wrap around one of the remaining bounding constraints
518 * and continue the process until all bounding constraints have been
519 * taken into account.
520 * The resulting linear combination of the bounding constraints will
521 * correspond to a facet of the convex hull.
523 static struct isl_mat
*initial_facet_constraint(struct isl_ctx
*ctx
,
524 struct isl_set
*set
, struct isl_mat
*bounds
)
526 struct isl_set
*slice
= NULL
;
527 struct isl_basic_set
*face
= NULL
;
528 struct isl_mat
*m
, *U
, *Q
;
530 unsigned dim
= isl_set_n_dim(set
);
532 isl_assert(ctx
, set
->n
> 0, goto error
);
533 isl_assert(ctx
, bounds
->n_row
== dim
, goto error
);
535 while (bounds
->n_row
> 1) {
536 slice
= isl_set_copy(set
);
537 slice
= isl_set_add_equality(ctx
, slice
, bounds
->row
[0]);
538 face
= isl_set_affine_hull(slice
);
541 if (face
->n_eq
== 1) {
542 isl_basic_set_free(face
);
545 m
= isl_mat_alloc(ctx
, 1 + face
->n_eq
, 1 + dim
);
548 isl_int_set_si(m
->row
[0][0], 1);
549 isl_seq_clr(m
->row
[0]+1, dim
);
550 for (i
= 0; i
< face
->n_eq
; ++i
)
551 isl_seq_cpy(m
->row
[1 + i
], face
->eq
[i
], 1 + dim
);
552 U
= isl_mat_right_inverse(ctx
, m
);
553 Q
= isl_mat_right_inverse(ctx
, isl_mat_copy(ctx
, U
));
554 U
= isl_mat_drop_cols(ctx
, U
, 1 + face
->n_eq
,
556 Q
= isl_mat_drop_rows(ctx
, Q
, 1 + face
->n_eq
,
558 U
= isl_mat_drop_cols(ctx
, U
, 0, 1);
559 Q
= isl_mat_drop_rows(ctx
, Q
, 0, 1);
560 bounds
= isl_mat_product(ctx
, bounds
, U
);
561 bounds
= isl_mat_product(ctx
, bounds
, Q
);
562 while (isl_seq_first_non_zero(bounds
->row
[bounds
->n_row
-1],
563 bounds
->n_col
) == -1) {
565 isl_assert(ctx
, bounds
->n_row
> 1, goto error
);
567 if (!wrap_facet(set
, bounds
->row
[0],
568 bounds
->row
[bounds
->n_row
-1]))
570 isl_basic_set_free(face
);
575 isl_basic_set_free(face
);
576 isl_mat_free(ctx
, bounds
);
580 /* Given the bounding constraint "c" of a facet of the convex hull of "set",
581 * compute a hyperplane description of the facet, i.e., compute the facets
584 * We compute an affine transformation that transforms the constraint
593 * by computing the right inverse U of a matrix that starts with the rows
606 * Since z_1 is zero, we can drop this variable as well as the corresponding
607 * column of U to obtain
615 * with Q' equal to Q, but without the corresponding row.
616 * After computing the facets of the facet in the z' space,
617 * we convert them back to the x space through Q.
619 static struct isl_basic_set
*compute_facet(struct isl_set
*set
, isl_int
*c
)
621 struct isl_mat
*m
, *U
, *Q
;
622 struct isl_basic_set
*facet
= NULL
;
627 set
= isl_set_copy(set
);
628 dim
= isl_set_n_dim(set
);
629 m
= isl_mat_alloc(set
->ctx
, 2, 1 + dim
);
632 isl_int_set_si(m
->row
[0][0], 1);
633 isl_seq_clr(m
->row
[0]+1, dim
);
634 isl_seq_cpy(m
->row
[1], c
, 1+dim
);
635 U
= isl_mat_right_inverse(set
->ctx
, m
);
636 Q
= isl_mat_right_inverse(set
->ctx
, isl_mat_copy(set
->ctx
, U
));
637 U
= isl_mat_drop_cols(set
->ctx
, U
, 1, 1);
638 Q
= isl_mat_drop_rows(set
->ctx
, Q
, 1, 1);
639 set
= isl_set_preimage(set
, U
);
640 facet
= uset_convex_hull_wrap_bounded(set
);
641 facet
= isl_basic_set_preimage(facet
, Q
);
642 isl_assert(ctx
, facet
->n_eq
== 0, goto error
);
645 isl_basic_set_free(facet
);
650 /* Given an initial facet constraint, compute the remaining facets.
651 * We do this by running through all facets found so far and computing
652 * the adjacent facets through wrapping, adding those facets that we
653 * hadn't already found before.
655 * For each facet we have found so far, we first compute its facets
656 * in the resulting convex hull. That is, we compute the ridges
657 * of the resulting convex hull contained in the facet.
658 * We also compute the corresponding facet in the current approximation
659 * of the convex hull. There is no need to wrap around the ridges
660 * in this facet since that would result in a facet that is already
661 * present in the current approximation.
663 * This function can still be significantly optimized by checking which of
664 * the facets of the basic sets are also facets of the convex hull and
665 * using all the facets so far to help in constructing the facets of the
668 * using the technique in section "3.1 Ridge Generation" of
669 * "Extended Convex Hull" by Fukuda et al.
671 static struct isl_basic_set
*extend(struct isl_basic_set
*hull
,
676 struct isl_basic_set
*facet
= NULL
;
677 struct isl_basic_set
*hull_facet
= NULL
;
681 isl_assert(set
->ctx
, set
->n
> 0, goto error
);
683 dim
= isl_set_n_dim(set
);
685 for (i
= 0; i
< hull
->n_ineq
; ++i
) {
686 facet
= compute_facet(set
, hull
->ineq
[i
]);
687 facet
= isl_basic_set_add_equality(facet
->ctx
, facet
, hull
->ineq
[i
]);
688 facet
= isl_basic_set_gauss(facet
, NULL
);
689 facet
= isl_basic_set_normalize_constraints(facet
);
690 hull_facet
= isl_basic_set_copy(hull
);
691 hull_facet
= isl_basic_set_add_equality(hull_facet
->ctx
, hull_facet
, hull
->ineq
[i
]);
692 hull_facet
= isl_basic_set_gauss(hull_facet
, NULL
);
693 hull_facet
= isl_basic_set_normalize_constraints(hull_facet
);
696 hull
= isl_basic_set_cow(hull
);
697 hull
= isl_basic_set_extend_dim(hull
,
698 isl_dim_copy(hull
->dim
), 0, 0, facet
->n_ineq
);
699 for (j
= 0; j
< facet
->n_ineq
; ++j
) {
700 for (f
= 0; f
< hull_facet
->n_ineq
; ++f
)
701 if (isl_seq_eq(facet
->ineq
[j
],
702 hull_facet
->ineq
[f
], 1 + dim
))
704 if (f
< hull_facet
->n_ineq
)
706 k
= isl_basic_set_alloc_inequality(hull
);
709 isl_seq_cpy(hull
->ineq
[k
], hull
->ineq
[i
], 1+dim
);
710 if (!wrap_facet(set
, hull
->ineq
[k
], facet
->ineq
[j
]))
713 isl_basic_set_free(hull_facet
);
714 isl_basic_set_free(facet
);
716 hull
= isl_basic_set_simplify(hull
);
717 hull
= isl_basic_set_finalize(hull
);
720 isl_basic_set_free(hull_facet
);
721 isl_basic_set_free(facet
);
722 isl_basic_set_free(hull
);
726 /* Special case for computing the convex hull of a one dimensional set.
727 * We simply collect the lower and upper bounds of each basic set
728 * and the biggest of those.
730 static struct isl_basic_set
*convex_hull_1d(struct isl_ctx
*ctx
,
733 struct isl_mat
*c
= NULL
;
734 isl_int
*lower
= NULL
;
735 isl_int
*upper
= NULL
;
738 struct isl_basic_set
*hull
;
740 for (i
= 0; i
< set
->n
; ++i
) {
741 set
->p
[i
] = isl_basic_set_simplify(set
->p
[i
]);
745 set
= isl_set_remove_empty_parts(set
);
748 isl_assert(ctx
, set
->n
> 0, goto error
);
749 c
= isl_mat_alloc(ctx
, 2, 2);
753 if (set
->p
[0]->n_eq
> 0) {
754 isl_assert(ctx
, set
->p
[0]->n_eq
== 1, goto error
);
757 if (isl_int_is_pos(set
->p
[0]->eq
[0][1])) {
758 isl_seq_cpy(lower
, set
->p
[0]->eq
[0], 2);
759 isl_seq_neg(upper
, set
->p
[0]->eq
[0], 2);
761 isl_seq_neg(lower
, set
->p
[0]->eq
[0], 2);
762 isl_seq_cpy(upper
, set
->p
[0]->eq
[0], 2);
765 for (j
= 0; j
< set
->p
[0]->n_ineq
; ++j
) {
766 if (isl_int_is_pos(set
->p
[0]->ineq
[j
][1])) {
768 isl_seq_cpy(lower
, set
->p
[0]->ineq
[j
], 2);
771 isl_seq_cpy(upper
, set
->p
[0]->ineq
[j
], 2);
778 for (i
= 0; i
< set
->n
; ++i
) {
779 struct isl_basic_set
*bset
= set
->p
[i
];
783 for (j
= 0; j
< bset
->n_eq
; ++j
) {
787 isl_int_mul(a
, lower
[0], bset
->eq
[j
][1]);
788 isl_int_mul(b
, lower
[1], bset
->eq
[j
][0]);
789 if (isl_int_lt(a
, b
) && isl_int_is_pos(bset
->eq
[j
][1]))
790 isl_seq_cpy(lower
, bset
->eq
[j
], 2);
791 if (isl_int_gt(a
, b
) && isl_int_is_neg(bset
->eq
[j
][1]))
792 isl_seq_neg(lower
, bset
->eq
[j
], 2);
795 isl_int_mul(a
, upper
[0], bset
->eq
[j
][1]);
796 isl_int_mul(b
, upper
[1], bset
->eq
[j
][0]);
797 if (isl_int_lt(a
, b
) && isl_int_is_pos(bset
->eq
[j
][1]))
798 isl_seq_neg(upper
, bset
->eq
[j
], 2);
799 if (isl_int_gt(a
, b
) && isl_int_is_neg(bset
->eq
[j
][1]))
800 isl_seq_cpy(upper
, bset
->eq
[j
], 2);
803 for (j
= 0; j
< bset
->n_ineq
; ++j
) {
804 if (isl_int_is_pos(bset
->ineq
[j
][1]))
806 if (isl_int_is_neg(bset
->ineq
[j
][1]))
808 if (lower
&& isl_int_is_pos(bset
->ineq
[j
][1])) {
809 isl_int_mul(a
, lower
[0], bset
->ineq
[j
][1]);
810 isl_int_mul(b
, lower
[1], bset
->ineq
[j
][0]);
811 if (isl_int_lt(a
, b
))
812 isl_seq_cpy(lower
, bset
->ineq
[j
], 2);
814 if (upper
&& isl_int_is_neg(bset
->ineq
[j
][1])) {
815 isl_int_mul(a
, upper
[0], bset
->ineq
[j
][1]);
816 isl_int_mul(b
, upper
[1], bset
->ineq
[j
][0]);
817 if (isl_int_gt(a
, b
))
818 isl_seq_cpy(upper
, bset
->ineq
[j
], 2);
829 hull
= isl_basic_set_alloc(ctx
, 0, 1, 0, 0, 2);
830 hull
= isl_basic_set_set_rational(hull
);
834 k
= isl_basic_set_alloc_inequality(hull
);
835 isl_seq_cpy(hull
->ineq
[k
], lower
, 2);
838 k
= isl_basic_set_alloc_inequality(hull
);
839 isl_seq_cpy(hull
->ineq
[k
], upper
, 2);
841 hull
= isl_basic_set_finalize(hull
);
843 isl_mat_free(ctx
, c
);
847 isl_mat_free(ctx
, c
);
851 /* Project out final n dimensions using Fourier-Motzkin */
852 static struct isl_set
*set_project_out(struct isl_ctx
*ctx
,
853 struct isl_set
*set
, unsigned n
)
855 return isl_set_remove_dims(set
, isl_set_n_dim(set
) - n
, n
);
858 static struct isl_basic_set
*convex_hull_0d(struct isl_set
*set
)
860 struct isl_basic_set
*convex_hull
;
865 if (isl_set_is_empty(set
))
866 convex_hull
= isl_basic_set_empty(isl_dim_copy(set
->dim
));
868 convex_hull
= isl_basic_set_universe(isl_dim_copy(set
->dim
));
873 /* Compute the convex hull of a pair of basic sets without any parameters or
874 * integer divisions using Fourier-Motzkin elimination.
875 * The convex hull is the set of all points that can be written as
876 * the sum of points from both basic sets (in homogeneous coordinates).
877 * We set up the constraints in a space with dimensions for each of
878 * the three sets and then project out the dimensions corresponding
879 * to the two original basic sets, retaining only those corresponding
880 * to the convex hull.
882 static struct isl_basic_set
*convex_hull_pair(struct isl_basic_set
*bset1
,
883 struct isl_basic_set
*bset2
)
886 struct isl_basic_set
*bset
[2];
887 struct isl_basic_set
*hull
= NULL
;
890 if (!bset1
|| !bset2
)
893 dim
= isl_basic_set_n_dim(bset1
);
894 hull
= isl_basic_set_alloc(bset1
->ctx
, 0, 2 + 3 * dim
, 0,
895 1 + dim
+ bset1
->n_eq
+ bset2
->n_eq
,
896 2 + bset1
->n_ineq
+ bset2
->n_ineq
);
899 for (i
= 0; i
< 2; ++i
) {
900 for (j
= 0; j
< bset
[i
]->n_eq
; ++j
) {
901 k
= isl_basic_set_alloc_equality(hull
);
904 isl_seq_clr(hull
->eq
[k
], (i
+1) * (1+dim
));
905 isl_seq_clr(hull
->eq
[k
]+(i
+2)*(1+dim
), (1-i
)*(1+dim
));
906 isl_seq_cpy(hull
->eq
[k
]+(i
+1)*(1+dim
), bset
[i
]->eq
[j
],
909 for (j
= 0; j
< bset
[i
]->n_ineq
; ++j
) {
910 k
= isl_basic_set_alloc_inequality(hull
);
913 isl_seq_clr(hull
->ineq
[k
], (i
+1) * (1+dim
));
914 isl_seq_clr(hull
->ineq
[k
]+(i
+2)*(1+dim
), (1-i
)*(1+dim
));
915 isl_seq_cpy(hull
->ineq
[k
]+(i
+1)*(1+dim
),
916 bset
[i
]->ineq
[j
], 1+dim
);
918 k
= isl_basic_set_alloc_inequality(hull
);
921 isl_seq_clr(hull
->ineq
[k
], 1+2+3*dim
);
922 isl_int_set_si(hull
->ineq
[k
][(i
+1)*(1+dim
)], 1);
924 for (j
= 0; j
< 1+dim
; ++j
) {
925 k
= isl_basic_set_alloc_equality(hull
);
928 isl_seq_clr(hull
->eq
[k
], 1+2+3*dim
);
929 isl_int_set_si(hull
->eq
[k
][j
], -1);
930 isl_int_set_si(hull
->eq
[k
][1+dim
+j
], 1);
931 isl_int_set_si(hull
->eq
[k
][2*(1+dim
)+j
], 1);
933 hull
= isl_basic_set_set_rational(hull
);
934 hull
= isl_basic_set_remove_dims(hull
, dim
, 2*(1+dim
));
935 hull
= isl_basic_set_convex_hull(hull
);
936 isl_basic_set_free(bset1
);
937 isl_basic_set_free(bset2
);
940 isl_basic_set_free(bset1
);
941 isl_basic_set_free(bset2
);
942 isl_basic_set_free(hull
);
946 /* Compute the convex hull of a set without any parameters or
947 * integer divisions using Fourier-Motzkin elimination.
948 * In each step, we combined two basic sets until only one
951 static struct isl_basic_set
*uset_convex_hull_elim(struct isl_set
*set
)
953 struct isl_basic_set
*convex_hull
= NULL
;
955 convex_hull
= isl_set_copy_basic_set(set
);
956 set
= isl_set_drop_basic_set(set
, convex_hull
);
960 struct isl_basic_set
*t
;
961 t
= isl_set_copy_basic_set(set
);
964 set
= isl_set_drop_basic_set(set
, t
);
967 convex_hull
= convex_hull_pair(convex_hull
, t
);
973 isl_basic_set_free(convex_hull
);
977 /* Compute an initial hull for wrapping containing a single initial
978 * facet by first computing bounds on the set and then using these
979 * bounds to construct an initial facet.
980 * This function is a remnant of an older implementation where the
981 * bounds were also used to check whether the set was bounded.
982 * Since this function will now only be called when we know the
983 * set to be bounded, the initial facet should probably be constructed
984 * by simply using the coordinate directions instead.
986 static struct isl_basic_set
*initial_hull(struct isl_basic_set
*hull
,
989 struct isl_mat
*bounds
= NULL
;
995 bounds
= independent_bounds(set
->ctx
, set
);
998 isl_assert(set
->ctx
, bounds
->n_row
== isl_set_n_dim(set
), goto error
);
999 bounds
= initial_facet_constraint(set
->ctx
, set
, bounds
);
1002 k
= isl_basic_set_alloc_inequality(hull
);
1005 dim
= isl_set_n_dim(set
);
1006 isl_assert(set
->ctx
, 1 + dim
== bounds
->n_col
, goto error
);
1007 isl_seq_cpy(hull
->ineq
[k
], bounds
->row
[0], bounds
->n_col
);
1008 isl_mat_free(set
->ctx
, bounds
);
1012 isl_basic_set_free(hull
);
1013 isl_mat_free(set
->ctx
, bounds
);
1017 struct max_constraint
{
1023 static int max_constraint_equal(const void *entry
, const void *val
)
1025 struct max_constraint
*a
= (struct max_constraint
*)entry
;
1026 isl_int
*b
= (isl_int
*)val
;
1028 return isl_seq_eq(a
->c
->row
[0] + 1, b
, a
->c
->n_col
- 1);
1031 static void update_constraint(struct isl_ctx
*ctx
, struct isl_hash_table
*table
,
1032 isl_int
*con
, unsigned len
, int n
, int ineq
)
1034 struct isl_hash_table_entry
*entry
;
1035 struct max_constraint
*c
;
1038 c_hash
= isl_seq_hash(con
+ 1, len
, isl_hash_init());
1039 entry
= isl_hash_table_find(ctx
, table
, c_hash
, max_constraint_equal
,
1045 isl_hash_table_remove(ctx
, table
, entry
);
1049 if (isl_int_gt(c
->c
->row
[0][0], con
[0]))
1051 if (isl_int_eq(c
->c
->row
[0][0], con
[0])) {
1056 c
->c
= isl_mat_cow(ctx
, c
->c
);
1057 isl_int_set(c
->c
->row
[0][0], con
[0]);
1061 /* Check whether the constraint hash table "table" constains the constraint
1064 static int has_constraint(struct isl_ctx
*ctx
, struct isl_hash_table
*table
,
1065 isl_int
*con
, unsigned len
, int n
)
1067 struct isl_hash_table_entry
*entry
;
1068 struct max_constraint
*c
;
1071 c_hash
= isl_seq_hash(con
+ 1, len
, isl_hash_init());
1072 entry
= isl_hash_table_find(ctx
, table
, c_hash
, max_constraint_equal
,
1079 return isl_int_eq(c
->c
->row
[0][0], con
[0]);
1082 /* Check for inequality constraints of a basic set without equalities
1083 * such that the same or more stringent copies of the constraint appear
1084 * in all of the basic sets. Such constraints are necessarily facet
1085 * constraints of the convex hull.
1087 * If the resulting basic set is by chance identical to one of
1088 * the basic sets in "set", then we know that this basic set contains
1089 * all other basic sets and is therefore the convex hull of set.
1090 * In this case we set *is_hull to 1.
1092 static struct isl_basic_set
*common_constraints(struct isl_basic_set
*hull
,
1093 struct isl_set
*set
, int *is_hull
)
1096 int min_constraints
;
1098 struct max_constraint
*constraints
= NULL
;
1099 struct isl_hash_table
*table
= NULL
;
1104 for (i
= 0; i
< set
->n
; ++i
)
1105 if (set
->p
[i
]->n_eq
== 0)
1109 min_constraints
= set
->p
[i
]->n_ineq
;
1111 for (i
= best
+ 1; i
< set
->n
; ++i
) {
1112 if (set
->p
[i
]->n_eq
!= 0)
1114 if (set
->p
[i
]->n_ineq
>= min_constraints
)
1116 min_constraints
= set
->p
[i
]->n_ineq
;
1119 constraints
= isl_calloc_array(hull
->ctx
, struct max_constraint
,
1123 table
= isl_alloc_type(hull
->ctx
, struct isl_hash_table
);
1124 if (isl_hash_table_init(hull
->ctx
, table
, min_constraints
))
1127 total
= isl_dim_total(set
->dim
);
1128 for (i
= 0; i
< set
->p
[best
]->n_ineq
; ++i
) {
1129 constraints
[i
].c
= isl_mat_sub_alloc(hull
->ctx
,
1130 set
->p
[best
]->ineq
+ i
, 0, 1, 0, 1 + total
);
1131 if (!constraints
[i
].c
)
1133 constraints
[i
].ineq
= 1;
1135 for (i
= 0; i
< min_constraints
; ++i
) {
1136 struct isl_hash_table_entry
*entry
;
1138 c_hash
= isl_seq_hash(constraints
[i
].c
->row
[0] + 1, total
,
1140 entry
= isl_hash_table_find(hull
->ctx
, table
, c_hash
,
1141 max_constraint_equal
, constraints
[i
].c
->row
[0] + 1, 1);
1144 isl_assert(hull
->ctx
, !entry
->data
, goto error
);
1145 entry
->data
= &constraints
[i
];
1149 for (s
= 0; s
< set
->n
; ++s
) {
1153 for (i
= 0; i
< set
->p
[s
]->n_eq
; ++i
) {
1154 isl_int
*eq
= set
->p
[s
]->eq
[i
];
1155 for (j
= 0; j
< 2; ++j
) {
1156 isl_seq_neg(eq
, eq
, 1 + total
);
1157 update_constraint(hull
->ctx
, table
,
1161 for (i
= 0; i
< set
->p
[s
]->n_ineq
; ++i
) {
1162 isl_int
*ineq
= set
->p
[s
]->ineq
[i
];
1163 update_constraint(hull
->ctx
, table
, ineq
, total
, n
,
1164 set
->p
[s
]->n_eq
== 0);
1169 for (i
= 0; i
< min_constraints
; ++i
) {
1170 if (constraints
[i
].count
< n
)
1172 if (!constraints
[i
].ineq
)
1174 j
= isl_basic_set_alloc_inequality(hull
);
1177 isl_seq_cpy(hull
->ineq
[j
], constraints
[i
].c
->row
[0], 1 + total
);
1180 for (s
= 0; s
< set
->n
; ++s
) {
1181 if (set
->p
[s
]->n_eq
)
1183 if (set
->p
[s
]->n_ineq
!= hull
->n_ineq
)
1185 for (i
= 0; i
< set
->p
[s
]->n_ineq
; ++i
) {
1186 isl_int
*ineq
= set
->p
[s
]->ineq
[i
];
1187 if (!has_constraint(hull
->ctx
, table
, ineq
, total
, n
))
1190 if (i
== set
->p
[s
]->n_ineq
)
1194 isl_hash_table_clear(table
);
1195 for (i
= 0; i
< min_constraints
; ++i
)
1196 isl_mat_free(hull
->ctx
, constraints
[i
].c
);
1201 isl_hash_table_clear(table
);
1204 for (i
= 0; i
< min_constraints
; ++i
)
1205 isl_mat_free(hull
->ctx
, constraints
[i
].c
);
1210 /* Create a template for the convex hull of "set" and fill it up
1211 * obvious facet constraints, if any. If the result happens to
1212 * be the convex hull of "set" then *is_hull is set to 1.
1214 static struct isl_basic_set
*proto_hull(struct isl_set
*set
, int *is_hull
)
1216 struct isl_basic_set
*hull
;
1221 for (i
= 0; i
< set
->n
; ++i
) {
1222 n_ineq
+= set
->p
[i
]->n_eq
;
1223 n_ineq
+= set
->p
[i
]->n_ineq
;
1225 hull
= isl_basic_set_alloc_dim(isl_dim_copy(set
->dim
), 0, 0, n_ineq
);
1226 hull
= isl_basic_set_set_rational(hull
);
1229 return common_constraints(hull
, set
, is_hull
);
1232 static struct isl_basic_set
*uset_convex_hull_wrap(struct isl_set
*set
)
1234 struct isl_basic_set
*hull
;
1237 hull
= proto_hull(set
, &is_hull
);
1238 if (hull
&& !is_hull
) {
1239 if (hull
->n_ineq
== 0)
1240 hull
= initial_hull(hull
, set
);
1241 hull
= extend(hull
, set
);
1248 static int isl_basic_set_is_bounded(struct isl_basic_set
*bset
)
1250 struct isl_tab
*tab
;
1253 tab
= isl_tab_from_recession_cone((struct isl_basic_map
*)bset
);
1254 bounded
= isl_tab_cone_is_bounded(bset
->ctx
, tab
);
1255 isl_tab_free(bset
->ctx
, tab
);
1259 static int isl_set_is_bounded(struct isl_set
*set
)
1263 for (i
= 0; i
< set
->n
; ++i
) {
1264 int bounded
= isl_basic_set_is_bounded(set
->p
[i
]);
1265 if (!bounded
|| bounded
< 0)
1271 /* Compute the convex hull of a set without any parameters or
1272 * integer divisions. Depending on whether the set is bounded,
1273 * we pass control to the wrapping based convex hull or
1274 * the Fourier-Motzkin elimination based convex hull.
1275 * We also handle a few special cases before checking the boundedness.
1277 static struct isl_basic_set
*uset_convex_hull(struct isl_set
*set
)
1280 struct isl_basic_set
*convex_hull
= NULL
;
1282 if (isl_set_n_dim(set
) == 0)
1283 return convex_hull_0d(set
);
1285 set
= isl_set_coalesce(set
);
1286 set
= isl_set_set_rational(set
);
1293 convex_hull
= isl_basic_set_copy(set
->p
[0]);
1297 if (isl_set_n_dim(set
) == 1)
1298 return convex_hull_1d(set
->ctx
, set
);
1300 if (!isl_set_is_bounded(set
))
1301 return uset_convex_hull_elim(set
);
1303 return uset_convex_hull_wrap(set
);
1306 isl_basic_set_free(convex_hull
);
1310 /* This is the core procedure, where "set" is a "pure" set, i.e.,
1311 * without parameters or divs and where the convex hull of set is
1312 * known to be full-dimensional.
1314 static struct isl_basic_set
*uset_convex_hull_wrap_bounded(struct isl_set
*set
)
1317 struct isl_basic_set
*convex_hull
= NULL
;
1319 if (isl_set_n_dim(set
) == 0) {
1320 convex_hull
= isl_basic_set_universe(isl_dim_copy(set
->dim
));
1322 convex_hull
= isl_basic_set_set_rational(convex_hull
);
1326 set
= isl_set_set_rational(set
);
1330 set
= isl_set_normalize(set
);
1334 convex_hull
= isl_basic_set_copy(set
->p
[0]);
1338 if (isl_set_n_dim(set
) == 1)
1339 return convex_hull_1d(set
->ctx
, set
);
1341 return uset_convex_hull_wrap(set
);
1347 /* Compute the convex hull of set "set" with affine hull "affine_hull",
1348 * We first remove the equalities (transforming the set), compute the
1349 * convex hull of the transformed set and then add the equalities back
1350 * (after performing the inverse transformation.
1352 static struct isl_basic_set
*modulo_affine_hull(struct isl_ctx
*ctx
,
1353 struct isl_set
*set
, struct isl_basic_set
*affine_hull
)
1357 struct isl_basic_set
*dummy
;
1358 struct isl_basic_set
*convex_hull
;
1360 dummy
= isl_basic_set_remove_equalities(
1361 isl_basic_set_copy(affine_hull
), &T
, &T2
);
1364 isl_basic_set_free(dummy
);
1365 set
= isl_set_preimage(set
, T
);
1366 convex_hull
= uset_convex_hull(set
);
1367 convex_hull
= isl_basic_set_preimage(convex_hull
, T2
);
1368 convex_hull
= isl_basic_set_intersect(convex_hull
, affine_hull
);
1371 isl_basic_set_free(affine_hull
);
1376 /* Compute the convex hull of a map.
1378 * The implementation was inspired by "Extended Convex Hull" by Fukuda et al.,
1379 * specifically, the wrapping of facets to obtain new facets.
1381 struct isl_basic_map
*isl_map_convex_hull(struct isl_map
*map
)
1383 struct isl_basic_set
*bset
;
1384 struct isl_basic_map
*model
= NULL
;
1385 struct isl_basic_set
*affine_hull
= NULL
;
1386 struct isl_basic_map
*convex_hull
= NULL
;
1387 struct isl_set
*set
= NULL
;
1388 struct isl_ctx
*ctx
;
1395 convex_hull
= isl_basic_map_empty_like_map(map
);
1400 map
= isl_map_detect_equalities(map
);
1401 map
= isl_map_align_divs(map
);
1402 model
= isl_basic_map_copy(map
->p
[0]);
1403 set
= isl_map_underlying_set(map
);
1407 affine_hull
= isl_set_affine_hull(isl_set_copy(set
));
1410 if (affine_hull
->n_eq
!= 0)
1411 bset
= modulo_affine_hull(ctx
, set
, affine_hull
);
1413 isl_basic_set_free(affine_hull
);
1414 bset
= uset_convex_hull(set
);
1417 convex_hull
= isl_basic_map_overlying_set(bset
, model
);
1419 ISL_F_SET(convex_hull
, ISL_BASIC_MAP_NO_IMPLICIT
);
1420 ISL_F_SET(convex_hull
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1421 ISL_F_CLR(convex_hull
, ISL_BASIC_MAP_RATIONAL
);
1425 isl_basic_map_free(model
);
1429 struct isl_basic_set
*isl_set_convex_hull(struct isl_set
*set
)
1431 return (struct isl_basic_set
*)
1432 isl_map_convex_hull((struct isl_map
*)set
);
1435 struct sh_data_entry
{
1436 struct isl_hash_table
*table
;
1437 struct isl_tab
*tab
;
1440 /* Holds the data needed during the simple hull computation.
1442 * n the number of basic sets in the original set
1443 * hull_table a hash table of already computed constraints
1444 * in the simple hull
1445 * p for each basic set,
1446 * table a hash table of the constraints
1447 * tab the tableau corresponding to the basic set
1450 struct isl_ctx
*ctx
;
1452 struct isl_hash_table
*hull_table
;
1453 struct sh_data_entry p
[0];
1456 static void sh_data_free(struct sh_data
*data
)
1462 isl_hash_table_free(data
->ctx
, data
->hull_table
);
1463 for (i
= 0; i
< data
->n
; ++i
) {
1464 isl_hash_table_free(data
->ctx
, data
->p
[i
].table
);
1465 isl_tab_free(data
->ctx
, data
->p
[i
].tab
);
1470 struct ineq_cmp_data
{
1475 static int has_ineq(const void *entry
, const void *val
)
1477 isl_int
*row
= (isl_int
*)entry
;
1478 struct ineq_cmp_data
*v
= (struct ineq_cmp_data
*)val
;
1480 return isl_seq_eq(row
+ 1, v
->p
+ 1, v
->len
) ||
1481 isl_seq_is_neg(row
+ 1, v
->p
+ 1, v
->len
);
1484 static int hash_ineq(struct isl_ctx
*ctx
, struct isl_hash_table
*table
,
1485 isl_int
*ineq
, unsigned len
)
1488 struct ineq_cmp_data v
;
1489 struct isl_hash_table_entry
*entry
;
1493 c_hash
= isl_seq_hash(ineq
+ 1, len
, isl_hash_init());
1494 entry
= isl_hash_table_find(ctx
, table
, c_hash
, has_ineq
, &v
, 1);
1501 /* Fill hash table "table" with the constraints of "bset".
1502 * Equalities are added as two inequalities.
1503 * The value in the hash table is a pointer to the (in)equality of "bset".
1505 static int hash_basic_set(struct isl_hash_table
*table
,
1506 struct isl_basic_set
*bset
)
1509 unsigned dim
= isl_basic_set_total_dim(bset
);
1511 for (i
= 0; i
< bset
->n_eq
; ++i
) {
1512 for (j
= 0; j
< 2; ++j
) {
1513 isl_seq_neg(bset
->eq
[i
], bset
->eq
[i
], 1 + dim
);
1514 if (hash_ineq(bset
->ctx
, table
, bset
->eq
[i
], dim
) < 0)
1518 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
1519 if (hash_ineq(bset
->ctx
, table
, bset
->ineq
[i
], dim
) < 0)
1525 static struct sh_data
*sh_data_alloc(struct isl_set
*set
, unsigned n_ineq
)
1527 struct sh_data
*data
;
1530 data
= isl_calloc(set
->ctx
, struct sh_data
,
1531 sizeof(struct sh_data
) + set
->n
* sizeof(struct sh_data_entry
));
1534 data
->ctx
= set
->ctx
;
1536 data
->hull_table
= isl_hash_table_alloc(set
->ctx
, n_ineq
);
1537 if (!data
->hull_table
)
1539 for (i
= 0; i
< set
->n
; ++i
) {
1540 data
->p
[i
].table
= isl_hash_table_alloc(set
->ctx
,
1541 2 * set
->p
[i
]->n_eq
+ set
->p
[i
]->n_ineq
);
1542 if (!data
->p
[i
].table
)
1544 if (hash_basic_set(data
->p
[i
].table
, set
->p
[i
]) < 0)
1553 /* Check if inequality "ineq" is a bound for basic set "j" or if
1554 * it can be relaxed (by increasing the constant term) to become
1555 * a bound for that basic set. In the latter case, the constant
1557 * Return 1 if "ineq" is a bound
1558 * 0 if "ineq" may attain arbitrarily small values on basic set "j"
1559 * -1 if some error occurred
1561 static int is_bound(struct sh_data
*data
, struct isl_set
*set
, int j
,
1564 enum isl_lp_result res
;
1567 if (!data
->p
[j
].tab
) {
1568 data
->p
[j
].tab
= isl_tab_from_basic_set(set
->p
[j
]);
1569 if (!data
->p
[j
].tab
)
1575 res
= isl_tab_min(data
->ctx
, data
->p
[j
].tab
, ineq
, data
->ctx
->one
,
1577 if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
1578 isl_int_sub(ineq
[0], ineq
[0], opt
);
1582 return res
== isl_lp_ok
? 1 :
1583 res
== isl_lp_unbounded
? 0 : -1;
1586 /* Check if inequality "ineq" from basic set "i" can be relaxed to
1587 * become a bound on the whole set. If so, add the (relaxed) inequality
1590 * We first check if "hull" already contains a translate of the inequality.
1591 * If so, we are done.
1592 * Then, we check if any of the previous basic sets contains a translate
1593 * of the inequality. If so, then we have already considered this
1594 * inequality and we are done.
1595 * Otherwise, for each basic set other than "i", we check if the inequality
1596 * is a bound on the basic set.
1597 * For previous basic sets, we know that they do not contain a translate
1598 * of the inequality, so we directly call is_bound.
1599 * For following basic sets, we first check if a translate of the
1600 * inequality appears in its description and if so directly update
1601 * the inequality accordingly.
1603 static struct isl_basic_set
*add_bound(struct isl_basic_set
*hull
,
1604 struct sh_data
*data
, struct isl_set
*set
, int i
, isl_int
*ineq
)
1607 struct ineq_cmp_data v
;
1608 struct isl_hash_table_entry
*entry
;
1614 v
.len
= isl_basic_set_total_dim(hull
);
1616 c_hash
= isl_seq_hash(ineq
+ 1, v
.len
, isl_hash_init());
1618 entry
= isl_hash_table_find(hull
->ctx
, data
->hull_table
, c_hash
,
1623 for (j
= 0; j
< i
; ++j
) {
1624 entry
= isl_hash_table_find(hull
->ctx
, data
->p
[j
].table
,
1625 c_hash
, has_ineq
, &v
, 0);
1632 k
= isl_basic_set_alloc_inequality(hull
);
1633 isl_seq_cpy(hull
->ineq
[k
], ineq
, 1 + v
.len
);
1637 for (j
= 0; j
< i
; ++j
) {
1639 bound
= is_bound(data
, set
, j
, hull
->ineq
[k
]);
1646 isl_basic_set_free_inequality(hull
, 1);
1650 for (j
= i
+ 1; j
< set
->n
; ++j
) {
1653 entry
= isl_hash_table_find(hull
->ctx
, data
->p
[j
].table
,
1654 c_hash
, has_ineq
, &v
, 0);
1656 ineq_j
= entry
->data
;
1657 neg
= isl_seq_is_neg(ineq_j
+ 1,
1658 hull
->ineq
[k
] + 1, v
.len
);
1660 isl_int_neg(ineq_j
[0], ineq_j
[0]);
1661 if (isl_int_gt(ineq_j
[0], hull
->ineq
[k
][0]))
1662 isl_int_set(hull
->ineq
[k
][0], ineq_j
[0]);
1664 isl_int_neg(ineq_j
[0], ineq_j
[0]);
1667 bound
= is_bound(data
, set
, j
, hull
->ineq
[k
]);
1674 isl_basic_set_free_inequality(hull
, 1);
1678 entry
= isl_hash_table_find(hull
->ctx
, data
->hull_table
, c_hash
,
1682 entry
->data
= hull
->ineq
[k
];
1686 isl_basic_set_free(hull
);
1690 /* Check if any inequality from basic set "i" can be relaxed to
1691 * become a bound on the whole set. If so, add the (relaxed) inequality
1694 static struct isl_basic_set
*add_bounds(struct isl_basic_set
*bset
,
1695 struct sh_data
*data
, struct isl_set
*set
, int i
)
1698 unsigned dim
= isl_basic_set_total_dim(bset
);
1700 for (j
= 0; j
< set
->p
[i
]->n_eq
; ++j
) {
1701 for (k
= 0; k
< 2; ++k
) {
1702 isl_seq_neg(set
->p
[i
]->eq
[j
], set
->p
[i
]->eq
[j
], 1+dim
);
1703 add_bound(bset
, data
, set
, i
, set
->p
[i
]->eq
[j
]);
1706 for (j
= 0; j
< set
->p
[i
]->n_ineq
; ++j
)
1707 add_bound(bset
, data
, set
, i
, set
->p
[i
]->ineq
[j
]);
1711 /* Compute a superset of the convex hull of set that is described
1712 * by only translates of the constraints in the constituents of set.
1714 static struct isl_basic_set
*uset_simple_hull(struct isl_set
*set
)
1716 struct sh_data
*data
= NULL
;
1717 struct isl_basic_set
*hull
= NULL
;
1725 for (i
= 0; i
< set
->n
; ++i
) {
1728 n_ineq
+= 2 * set
->p
[i
]->n_eq
+ set
->p
[i
]->n_ineq
;
1731 hull
= isl_basic_set_alloc_dim(isl_dim_copy(set
->dim
), 0, 0, n_ineq
);
1735 data
= sh_data_alloc(set
, n_ineq
);
1739 for (i
= 0; i
< set
->n
; ++i
)
1740 hull
= add_bounds(hull
, data
, set
, i
);
1748 isl_basic_set_free(hull
);
1753 /* Compute a superset of the convex hull of map that is described
1754 * by only translates of the constraints in the constituents of map.
1756 struct isl_basic_map
*isl_map_simple_hull(struct isl_map
*map
)
1758 struct isl_set
*set
= NULL
;
1759 struct isl_basic_map
*model
= NULL
;
1760 struct isl_basic_map
*hull
;
1761 struct isl_basic_map
*affine_hull
;
1762 struct isl_basic_set
*bset
= NULL
;
1767 hull
= isl_basic_map_empty_like_map(map
);
1772 hull
= isl_basic_map_copy(map
->p
[0]);
1777 map
= isl_map_detect_equalities(map
);
1778 affine_hull
= isl_map_affine_hull(isl_map_copy(map
));
1779 map
= isl_map_align_divs(map
);
1780 model
= isl_basic_map_copy(map
->p
[0]);
1782 set
= isl_map_underlying_set(map
);
1784 bset
= uset_simple_hull(set
);
1786 hull
= isl_basic_map_overlying_set(bset
, model
);
1788 hull
= isl_basic_map_intersect(hull
, affine_hull
);
1789 hull
= isl_basic_map_convex_hull(hull
);
1790 ISL_F_SET(hull
, ISL_BASIC_MAP_NO_IMPLICIT
);
1791 ISL_F_SET(hull
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1796 struct isl_basic_set
*isl_set_simple_hull(struct isl_set
*set
)
1798 return (struct isl_basic_set
*)
1799 isl_map_simple_hull((struct isl_map
*)set
);
1802 /* Given a set "set", return parametric bounds on the dimension "dim".
1804 static struct isl_basic_set
*set_bounds(struct isl_set
*set
, int dim
)
1806 unsigned set_dim
= isl_set_dim(set
, isl_dim_set
);
1807 set
= isl_set_copy(set
);
1808 set
= isl_set_eliminate_dims(set
, dim
+ 1, set_dim
- (dim
+ 1));
1809 set
= isl_set_eliminate_dims(set
, 0, dim
);
1810 return isl_set_convex_hull(set
);
1813 /* Computes a "simple hull" and then check if each dimension in the
1814 * resulting hull is bounded by a symbolic constant. If not, the
1815 * hull is intersected with the corresponding bounds on the whole set.
1817 struct isl_basic_set
*isl_set_bounded_simple_hull(struct isl_set
*set
)
1820 struct isl_basic_set
*hull
;
1821 unsigned nparam
, left
;
1822 int removed_divs
= 0;
1824 hull
= isl_set_simple_hull(isl_set_copy(set
));
1828 nparam
= isl_basic_set_dim(hull
, isl_dim_param
);
1829 for (i
= 0; i
< isl_basic_set_dim(hull
, isl_dim_set
); ++i
) {
1830 int lower
= 0, upper
= 0;
1831 struct isl_basic_set
*bounds
;
1833 left
= isl_basic_set_total_dim(hull
) - nparam
- i
- 1;
1834 for (j
= 0; j
< hull
->n_eq
; ++j
) {
1835 if (isl_int_is_zero(hull
->eq
[j
][1 + nparam
+ i
]))
1837 if (isl_seq_first_non_zero(hull
->eq
[j
]+1+nparam
+i
+1,
1844 for (j
= 0; j
< hull
->n_ineq
; ++j
) {
1845 if (isl_int_is_zero(hull
->ineq
[j
][1 + nparam
+ i
]))
1847 if (isl_seq_first_non_zero(hull
->ineq
[j
]+1+nparam
+i
+1,
1849 isl_seq_first_non_zero(hull
->ineq
[j
]+1+nparam
,
1852 if (isl_int_is_pos(hull
->ineq
[j
][1 + nparam
+ i
]))
1863 if (!removed_divs
) {
1864 set
= isl_set_remove_divs(set
);
1869 bounds
= set_bounds(set
, i
);
1870 hull
= isl_basic_set_intersect(hull
, bounds
);