2 * Copyright 2010 INRIA Saclay
4 * Use of this software is governed by the GNU LGPLv2.1 license
6 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
7 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
11 #include <isl_map_private.h>
15 #include <isl_space_private.h>
16 #include <isl_morph.h>
17 #include <isl_vertices_private.h>
18 #include <isl_mat_private.h>
24 static __isl_give isl_vertices
*compute_chambers(__isl_take isl_basic_set
*bset
,
25 __isl_take isl_vertices
*vertices
);
27 __isl_give isl_vertices
*isl_vertices_copy(__isl_keep isl_vertices
*vertices
)
36 void isl_vertices_free(__isl_take isl_vertices
*vertices
)
43 if (--vertices
->ref
> 0)
46 for (i
= 0; i
< vertices
->n_vertices
; ++i
) {
47 isl_basic_set_free(vertices
->v
[i
].vertex
);
48 isl_basic_set_free(vertices
->v
[i
].dom
);
52 for (i
= 0; i
< vertices
->n_chambers
; ++i
) {
53 free(vertices
->c
[i
].vertices
);
54 isl_basic_set_free(vertices
->c
[i
].dom
);
58 isl_basic_set_free(vertices
->bset
);
62 struct isl_vertex_list
{
64 struct isl_vertex_list
*next
;
67 static void free_vertex_list(struct isl_vertex_list
*list
)
69 struct isl_vertex_list
*next
;
71 for (; list
; list
= next
) {
73 isl_basic_set_free(list
->v
.vertex
);
74 isl_basic_set_free(list
->v
.dom
);
79 static __isl_give isl_vertices
*vertices_from_list(__isl_keep isl_basic_set
*bset
,
80 int n_vertices
, struct isl_vertex_list
*list
)
83 struct isl_vertex_list
*next
;
84 isl_vertices
*vertices
;
86 vertices
= isl_calloc_type(bset
->ctx
, isl_vertices
);
90 vertices
->bset
= isl_basic_set_copy(bset
);
91 vertices
->v
= isl_alloc_array(bset
->ctx
, struct isl_vertex
, n_vertices
);
94 vertices
->n_vertices
= n_vertices
;
96 for (i
= 0; list
; list
= next
, i
++) {
98 vertices
->v
[i
] = list
->v
;
105 free_vertex_list(list
);
109 /* Prepend a vertex to the linked list "list" based on the equalities in "tab".
111 static int add_vertex(struct isl_vertex_list
**list
,
112 __isl_keep isl_basic_set
*bset
, struct isl_tab
*tab
)
116 struct isl_vertex_list
*v
= NULL
;
118 if (isl_tab_detect_implicit_equalities(tab
) < 0)
121 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
122 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
124 v
= isl_calloc_type(tab
->mat
->ctx
, struct isl_vertex_list
);
128 v
->v
.vertex
= isl_basic_set_copy(bset
);
129 v
->v
.vertex
= isl_basic_set_cow(v
->v
.vertex
);
130 v
->v
.vertex
= isl_basic_set_update_from_tab(v
->v
.vertex
, tab
);
131 v
->v
.vertex
= isl_basic_set_simplify(v
->v
.vertex
);
132 v
->v
.vertex
= isl_basic_set_finalize(v
->v
.vertex
);
135 isl_assert(bset
->ctx
, v
->v
.vertex
->n_eq
>= nvar
, goto error
);
136 v
->v
.dom
= isl_basic_set_copy(v
->v
.vertex
);
137 v
->v
.dom
= isl_basic_set_project_out(v
->v
.dom
, isl_dim_set
, 0, nvar
);
150 /* Compute the parametric vertices and the chamber decomposition
151 * of an empty parametric polytope.
153 static __isl_give isl_vertices
*vertices_empty(__isl_keep isl_basic_set
*bset
)
155 isl_vertices
*vertices
;
161 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
163 vertices
= isl_calloc_type(bset
->ctx
, isl_vertices
);
166 vertices
->bset
= isl_basic_set_copy(bset
);
169 vertices
->n_vertices
= 0;
170 vertices
->n_chambers
= 0;
175 /* Compute the parametric vertices and the chamber decomposition
176 * of the parametric polytope defined using the same constraints
177 * as "bset" in the 0D case.
178 * There is exactly one 0D vertex and a single chamber containing
181 static __isl_give isl_vertices
*vertices_0D(__isl_keep isl_basic_set
*bset
)
183 isl_vertices
*vertices
;
189 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
191 vertices
= isl_calloc_type(bset
->ctx
, isl_vertices
);
195 vertices
->bset
= isl_basic_set_copy(bset
);
197 vertices
->v
= isl_calloc_array(bset
->ctx
, struct isl_vertex
, 1);
200 vertices
->n_vertices
= 1;
201 vertices
->v
[0].vertex
= isl_basic_set_copy(bset
);
202 vertices
->v
[0].dom
= isl_basic_set_params(isl_basic_set_copy(bset
));
203 if (!vertices
->v
[0].vertex
|| !vertices
->v
[0].dom
)
206 vertices
->c
= isl_calloc_array(bset
->ctx
, struct isl_chamber
, 1);
209 vertices
->n_chambers
= 1;
210 vertices
->c
[0].n_vertices
= 1;
211 vertices
->c
[0].vertices
= isl_calloc_array(bset
->ctx
, int, 1);
212 if (!vertices
->c
[0].vertices
)
214 vertices
->c
[0].dom
= isl_basic_set_copy(vertices
->v
[0].dom
);
215 if (!vertices
->c
[0].dom
)
220 isl_vertices_free(vertices
);
224 static int isl_mat_rank(__isl_keep isl_mat
*mat
)
229 H
= isl_mat_left_hermite(isl_mat_copy(mat
), 0, NULL
, NULL
);
233 for (col
= 0; col
< H
->n_col
; ++col
) {
234 for (row
= 0; row
< H
->n_row
; ++row
)
235 if (!isl_int_is_zero(H
->row
[row
][col
]))
246 /* Is the row pointed to by "f" linearly independent of the "n" first
249 static int is_independent(__isl_keep isl_mat
*facets
, int n
, isl_int
*f
)
253 if (isl_seq_first_non_zero(f
, facets
->n_col
) < 0)
256 isl_seq_cpy(facets
->row
[n
], f
, facets
->n_col
);
257 facets
->n_row
= n
+ 1;
258 rank
= isl_mat_rank(facets
);
262 return rank
== n
+ 1;
265 /* Check whether we can select constraint "level", given the current selection
266 * reflected by facets in "tab", the rows of "facets" and the earlier
267 * "selected" elements of "selection".
269 * If the constraint is (strictly) redundant in the tableau, selecting it would
270 * result in an empty tableau, so it can't be selected.
271 * If the set variable part of the constraint is not linearly indepedent
272 * of the set variable parts of the already selected constraints,
273 * the constraint cannot be selected.
274 * If selecting the constraint results in an empty tableau, the constraint
275 * cannot be selected.
276 * Finally, if selecting the constraint results in some explicitly
277 * deselected constraints turning into equalities, then the corresponding
278 * vertices have already been generated, so the constraint cannot be selected.
280 static int can_select(__isl_keep isl_basic_set
*bset
, int level
,
281 struct isl_tab
*tab
, __isl_keep isl_mat
*facets
, int selected
,
287 struct isl_tab_undo
*snap
;
289 if (isl_tab_is_redundant(tab
, level
))
292 ovar
= isl_space_offset(bset
->dim
, isl_dim_set
);
294 indep
= is_independent(facets
, selected
, bset
->ineq
[level
] + 1 + ovar
);
300 snap
= isl_tab_snap(tab
);
301 if (isl_tab_select_facet(tab
, level
) < 0)
305 if (isl_tab_rollback(tab
, snap
) < 0)
310 for (i
= 0; i
< level
; ++i
) {
313 if (selection
[i
] != DESELECTED
)
316 if (isl_tab_is_equality(tab
, i
))
318 else if (isl_tab_is_redundant(tab
, i
))
321 sgn
= isl_tab_sign_of_max(tab
, i
);
325 if (isl_tab_rollback(tab
, snap
) < 0)
334 /* Compute the parametric vertices and the chamber decomposition
335 * of a parametric polytope that is not full-dimensional.
337 * Simply map the parametric polytope to a lower dimensional space
338 * and map the resulting vertices back.
340 static __isl_give isl_vertices
*lower_dim_vertices(
341 __isl_keep isl_basic_set
*bset
)
344 isl_vertices
*vertices
;
346 bset
= isl_basic_set_copy(bset
);
347 morph
= isl_basic_set_full_compression(bset
);
348 bset
= isl_morph_basic_set(isl_morph_copy(morph
), bset
);
350 vertices
= isl_basic_set_compute_vertices(bset
);
351 isl_basic_set_free(bset
);
353 morph
= isl_morph_inverse(morph
);
355 vertices
= isl_morph_vertices(morph
, vertices
);
360 /* Compute the parametric vertices and the chamber decomposition
361 * of the parametric polytope defined using the same constraints
362 * as "bset". "bset" is assumed to have no existentially quantified
365 * The vertices themselves are computed in a fairly simplistic way.
366 * We simply run through all combinations of d constraints,
367 * with d the number of set variables, and check if those d constraints
368 * define a vertex. To avoid the generation of duplicate vertices,
369 * which we may happen if a vertex is defined by more that d constraints,
370 * we make sure we only generate the vertex for the d constraints with
373 * We set up a tableau and keep track of which facets have been
374 * selected. The tableau is marked strict_redundant so that we can be
375 * sure that any constraint that is marked redundant (and that is not
376 * also marked zero) is not an equality.
377 * If a constraint is marked DESELECTED, it means the constraint was
378 * SELECTED before (in combination with the same selection of earlier
379 * constraints). If such a deselected constraint turns out to be an
380 * equality, then any vertex that may still be found with the current
381 * selection has already been generated when the constraint was selected.
382 * A constraint is marked UNSELECTED when there is no way selecting
383 * the constraint could lead to a vertex (in combination with the current
384 * selection of earlier constraints).
386 * The set variable coefficients of the selected constraints are stored
387 * in the facets matrix.
389 __isl_give isl_vertices
*isl_basic_set_compute_vertices(
390 __isl_keep isl_basic_set
*bset
)
396 int *selection
= NULL
;
398 struct isl_tab_undo
**snap
= NULL
;
399 isl_mat
*facets
= NULL
;
400 struct isl_vertex_list
*list
= NULL
;
402 isl_vertices
*vertices
;
407 if (isl_basic_set_plain_is_empty(bset
))
408 return vertices_empty(bset
);
411 return lower_dim_vertices(bset
);
413 isl_assert(bset
->ctx
, isl_basic_set_dim(bset
, isl_dim_div
) == 0,
416 if (isl_basic_set_dim(bset
, isl_dim_set
) == 0)
417 return vertices_0D(bset
);
419 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
421 bset
= isl_basic_set_copy(bset
);
422 bset
= isl_basic_set_set_rational(bset
);
426 tab
= isl_tab_from_basic_set(bset
, 0);
429 tab
->strict_redundant
= 1;
432 vertices
= vertices_empty(bset
);
433 isl_basic_set_free(bset
);
438 selection
= isl_alloc_array(bset
->ctx
, int, bset
->n_ineq
);
439 snap
= isl_alloc_array(bset
->ctx
, struct isl_tab_undo
*, bset
->n_ineq
);
440 facets
= isl_mat_alloc(bset
->ctx
, nvar
, nvar
);
441 if (!selection
|| !snap
|| !facets
)
449 if (level
>= bset
->n_ineq
||
450 (!init
&& selection
[level
] != SELECTED
)) {
457 snap
[level
] = isl_tab_snap(tab
);
458 ok
= can_select(bset
, level
, tab
, facets
, selected
,
463 selection
[level
] = SELECTED
;
466 selection
[level
] = UNSELECTED
;
468 selection
[level
] = DESELECTED
;
470 if (isl_tab_rollback(tab
, snap
[level
]) < 0)
473 if (selected
== nvar
) {
474 if (tab
->n_dead
== nvar
) {
475 if (add_vertex(&list
, bset
, tab
) < 0)
486 isl_mat_free(facets
);
492 vertices
= vertices_from_list(bset
, n_vertices
, list
);
494 vertices
= compute_chambers(bset
, vertices
);
498 isl_mat_free(facets
);
502 isl_basic_set_free(bset
);
506 struct isl_chamber_list
{
507 struct isl_chamber c
;
508 struct isl_chamber_list
*next
;
511 static void free_chamber_list(struct isl_chamber_list
*list
)
513 struct isl_chamber_list
*next
;
515 for (; list
; list
= next
) {
517 isl_basic_set_free(list
->c
.dom
);
518 free(list
->c
.vertices
);
523 /* Check whether the basic set "bset" is a superset of the basic set described
524 * by "tab", i.e., check whether all constraints of "bset" are redundant.
526 static int bset_covers_tab(__isl_keep isl_basic_set
*bset
, struct isl_tab
*tab
)
533 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
534 enum isl_ineq_type type
= isl_tab_ineq_type(tab
, bset
->ineq
[i
]);
536 case isl_ineq_error
: return -1;
537 case isl_ineq_redundant
: continue;
545 static __isl_give isl_vertices
*vertices_add_chambers(
546 __isl_take isl_vertices
*vertices
, int n_chambers
,
547 struct isl_chamber_list
*list
)
551 struct isl_chamber_list
*next
;
553 ctx
= isl_vertices_get_ctx(vertices
);
554 vertices
->c
= isl_alloc_array(ctx
, struct isl_chamber
, n_chambers
);
557 vertices
->n_chambers
= n_chambers
;
559 for (i
= 0; list
; list
= next
, i
++) {
561 vertices
->c
[i
] = list
->c
;
567 isl_vertices_free(vertices
);
568 free_chamber_list(list
);
572 /* Can "tab" be intersected with "bset" without resulting in
573 * a lower-dimensional set.
575 static int can_intersect(struct isl_tab
*tab
, __isl_keep isl_basic_set
*bset
)
578 struct isl_tab_undo
*snap
;
580 if (isl_tab_extend_cons(tab
, bset
->n_ineq
) < 0)
583 snap
= isl_tab_snap(tab
);
585 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
586 if (isl_tab_ineq_type(tab
, bset
->ineq
[i
]) == isl_ineq_redundant
)
588 if (isl_tab_add_ineq(tab
, bset
->ineq
[i
]) < 0)
592 if (isl_tab_detect_implicit_equalities(tab
) < 0)
595 if (isl_tab_rollback(tab
, snap
) < 0)
603 static int add_chamber(struct isl_chamber_list
**list
,
604 __isl_keep isl_vertices
*vertices
, struct isl_tab
*tab
, int *selection
)
609 struct isl_tab_undo
*snap
;
610 struct isl_chamber_list
*c
= NULL
;
612 for (i
= 0; i
< vertices
->n_vertices
; ++i
)
616 snap
= isl_tab_snap(tab
);
618 for (i
= 0; i
< tab
->n_con
&& tab
->con
[i
].frozen
; ++i
)
619 tab
->con
[i
].frozen
= 0;
622 if (isl_tab_detect_redundant(tab
) < 0)
625 c
= isl_calloc_type(tab
->mat
->ctx
, struct isl_chamber_list
);
628 c
->c
.vertices
= isl_alloc_array(tab
->mat
->ctx
, int, n_vertices
);
631 c
->c
.dom
= isl_basic_set_from_basic_map(isl_basic_map_copy(tab
->bmap
));
632 c
->c
.dom
= isl_basic_set_set_rational(c
->c
.dom
);
633 c
->c
.dom
= isl_basic_set_cow(c
->c
.dom
);
634 c
->c
.dom
= isl_basic_set_update_from_tab(c
->c
.dom
, tab
);
635 c
->c
.dom
= isl_basic_set_simplify(c
->c
.dom
);
636 c
->c
.dom
= isl_basic_set_finalize(c
->c
.dom
);
640 c
->c
.n_vertices
= n_vertices
;
642 for (i
= 0, j
= 0; i
< vertices
->n_vertices
; ++i
)
644 c
->c
.vertices
[j
] = i
;
651 for (i
= 0; i
< n_frozen
; ++i
)
652 tab
->con
[i
].frozen
= 1;
654 if (isl_tab_rollback(tab
, snap
) < 0)
659 free_chamber_list(c
);
663 struct isl_facet_todo
{
664 struct isl_tab
*tab
; /* A tableau representation of the facet */
665 isl_basic_set
*bset
; /* A normalized basic set representation */
666 isl_vec
*constraint
; /* Constraint pointing to the other side */
667 struct isl_facet_todo
*next
;
670 static void free_todo(struct isl_facet_todo
*todo
)
673 struct isl_facet_todo
*next
= todo
->next
;
675 isl_tab_free(todo
->tab
);
676 isl_basic_set_free(todo
->bset
);
677 isl_vec_free(todo
->constraint
);
684 static struct isl_facet_todo
*create_todo(struct isl_tab
*tab
, int con
)
688 struct isl_tab_undo
*snap
;
689 struct isl_facet_todo
*todo
;
691 snap
= isl_tab_snap(tab
);
693 for (i
= 0; i
< tab
->n_con
&& tab
->con
[i
].frozen
; ++i
)
694 tab
->con
[i
].frozen
= 0;
697 if (isl_tab_detect_redundant(tab
) < 0)
700 todo
= isl_calloc_type(tab
->mat
->ctx
, struct isl_facet_todo
);
704 todo
->constraint
= isl_vec_alloc(tab
->mat
->ctx
, 1 + tab
->n_var
);
705 if (!todo
->constraint
)
707 isl_seq_neg(todo
->constraint
->el
, tab
->bmap
->ineq
[con
], 1 + tab
->n_var
);
708 todo
->bset
= isl_basic_set_from_basic_map(isl_basic_map_copy(tab
->bmap
));
709 todo
->bset
= isl_basic_set_set_rational(todo
->bset
);
710 todo
->bset
= isl_basic_set_cow(todo
->bset
);
711 todo
->bset
= isl_basic_set_update_from_tab(todo
->bset
, tab
);
712 todo
->bset
= isl_basic_set_simplify(todo
->bset
);
713 todo
->bset
= isl_basic_set_sort_constraints(todo
->bset
);
716 ISL_F_SET(todo
->bset
, ISL_BASIC_SET_NORMALIZED
);
717 todo
->tab
= isl_tab_dup(tab
);
721 for (i
= 0; i
< n_frozen
; ++i
)
722 tab
->con
[i
].frozen
= 1;
724 if (isl_tab_rollback(tab
, snap
) < 0)
733 /* Create todo items for all interior facets of the chamber represented
734 * by "tab" and collect them in "next".
736 static int init_todo(struct isl_facet_todo
**next
, struct isl_tab
*tab
)
739 struct isl_tab_undo
*snap
;
740 struct isl_facet_todo
*todo
;
742 snap
= isl_tab_snap(tab
);
744 for (i
= 0; i
< tab
->n_con
; ++i
) {
745 if (tab
->con
[i
].frozen
)
747 if (tab
->con
[i
].is_redundant
)
750 if (isl_tab_select_facet(tab
, i
) < 0)
753 todo
= create_todo(tab
, i
);
760 if (isl_tab_rollback(tab
, snap
) < 0)
767 /* Does the linked list contain a todo item that is the opposite of "todo".
768 * If so, return 1 and remove the opposite todo item.
770 static int has_opposite(struct isl_facet_todo
*todo
,
771 struct isl_facet_todo
**list
)
773 for (; *list
; list
= &(*list
)->next
) {
775 eq
= isl_basic_set_plain_is_equal(todo
->bset
, (*list
)->bset
);
790 /* Create todo items for all interior facets of the chamber represented
791 * by "tab" and collect them in first->next, taking care to cancel
792 * opposite todo items.
794 static int update_todo(struct isl_facet_todo
*first
, struct isl_tab
*tab
)
797 struct isl_tab_undo
*snap
;
798 struct isl_facet_todo
*todo
;
800 snap
= isl_tab_snap(tab
);
802 for (i
= 0; i
< tab
->n_con
; ++i
) {
805 if (tab
->con
[i
].frozen
)
807 if (tab
->con
[i
].is_redundant
)
810 if (isl_tab_select_facet(tab
, i
) < 0)
813 todo
= create_todo(tab
, i
);
817 drop
= has_opposite(todo
, &first
->next
);
824 todo
->next
= first
->next
;
828 if (isl_tab_rollback(tab
, snap
) < 0)
835 /* Compute the chamber decomposition of the parametric polytope respresented
836 * by "bset" given the parametric vertices and their activity domains.
838 * We are only interested in full-dimensional chambers.
839 * Each of these chambers is the intersection of the activity domains of
840 * one or more vertices and the union of all chambers is equal to the
841 * projection of the entire parametric polytope onto the parameter space.
843 * We first create an initial chamber by intersecting as many activity
844 * domains as possible without ending up with an empty or lower-dimensional
845 * set. As a minor optimization, we only consider those activity domains
846 * that contain some arbitrary point.
848 * For each of interior facets of the chamber, we construct a todo item,
849 * containing the facet and a constraint containing the other side of the facet,
850 * for constructing the chamber on the other side.
851 * While their are any todo items left, we pick a todo item and
852 * create the required chamber by intersecting all activity domains
853 * that contain the facet and have a full-dimensional intersection with
854 * the other side of the facet. For each of the interior facets, we
855 * again create todo items, taking care to cancel opposite todo items.
857 static __isl_give isl_vertices
*compute_chambers(__isl_take isl_basic_set
*bset
,
858 __isl_take isl_vertices
*vertices
)
862 isl_vec
*sample
= NULL
;
863 struct isl_tab
*tab
= NULL
;
864 struct isl_tab_undo
*snap
;
865 int *selection
= NULL
;
867 struct isl_chamber_list
*list
= NULL
;
868 struct isl_facet_todo
*todo
= NULL
;
870 if (!bset
|| !vertices
)
873 ctx
= isl_vertices_get_ctx(vertices
);
874 selection
= isl_alloc_array(ctx
, int, vertices
->n_vertices
);
878 bset
= isl_basic_set_params(bset
);
880 tab
= isl_tab_from_basic_set(bset
, 1);
881 for (i
= 0; i
< bset
->n_ineq
; ++i
)
882 if (isl_tab_freeze_constraint(tab
, i
) < 0)
884 isl_basic_set_free(bset
);
886 snap
= isl_tab_snap(tab
);
888 sample
= isl_tab_get_sample_value(tab
);
890 for (i
= 0; i
< vertices
->n_vertices
; ++i
) {
891 selection
[i
] = isl_basic_set_contains(vertices
->v
[i
].dom
, sample
);
892 if (selection
[i
] < 0)
896 selection
[i
] = can_intersect(tab
, vertices
->v
[i
].dom
);
897 if (selection
[i
] < 0)
901 if (isl_tab_detect_redundant(tab
) < 0)
904 if (add_chamber(&list
, vertices
, tab
, selection
) < 0)
908 if (init_todo(&todo
, tab
) < 0)
912 struct isl_facet_todo
*next
;
914 if (isl_tab_rollback(tab
, snap
) < 0)
917 if (isl_tab_add_ineq(tab
, todo
->constraint
->el
) < 0)
919 if (isl_tab_freeze_constraint(tab
, tab
->n_con
- 1) < 0)
922 for (i
= 0; i
< vertices
->n_vertices
; ++i
) {
923 selection
[i
] = bset_covers_tab(vertices
->v
[i
].dom
,
925 if (selection
[i
] < 0)
929 selection
[i
] = can_intersect(tab
, vertices
->v
[i
].dom
);
930 if (selection
[i
] < 0)
934 if (isl_tab_detect_redundant(tab
) < 0)
937 if (add_chamber(&list
, vertices
, tab
, selection
) < 0)
941 if (update_todo(todo
, tab
) < 0)
950 isl_vec_free(sample
);
955 vertices
= vertices_add_chambers(vertices
, n_chambers
, list
);
957 for (i
= 0; vertices
&& i
< vertices
->n_vertices
; ++i
) {
958 isl_basic_set_free(vertices
->v
[i
].dom
);
959 vertices
->v
[i
].dom
= NULL
;
964 free_chamber_list(list
);
966 isl_vec_free(sample
);
970 isl_basic_set_free(bset
);
971 isl_vertices_free(vertices
);
975 isl_ctx
*isl_vertex_get_ctx(__isl_keep isl_vertex
*vertex
)
977 return vertex
? isl_vertices_get_ctx(vertex
->vertices
) : NULL
;
980 int isl_vertex_get_id(__isl_keep isl_vertex
*vertex
)
982 return vertex
? vertex
->id
: -1;
985 __isl_give isl_basic_set
*isl_vertex_get_domain(__isl_keep isl_vertex
*vertex
)
987 struct isl_vertex
*v
;
992 v
= &vertex
->vertices
->v
[vertex
->id
];
995 nvar
= isl_basic_set_dim(v
->vertex
, isl_dim_set
);
996 v
->dom
= isl_basic_set_copy(v
->vertex
);
997 v
->dom
= isl_basic_set_project_out(v
->dom
, isl_dim_set
, 0, nvar
);
1000 return isl_basic_set_copy(v
->dom
);
1003 __isl_give isl_basic_set
*isl_vertex_get_expr(__isl_keep isl_vertex
*vertex
)
1005 struct isl_vertex
*v
;
1010 v
= &vertex
->vertices
->v
[vertex
->id
];
1012 return isl_basic_set_copy(v
->vertex
);
1015 static __isl_give isl_vertex
*isl_vertex_alloc(__isl_take isl_vertices
*vertices
,
1024 ctx
= isl_vertices_get_ctx(vertices
);
1025 vertex
= isl_alloc_type(ctx
, isl_vertex
);
1029 vertex
->vertices
= vertices
;
1034 isl_vertices_free(vertices
);
1038 void isl_vertex_free(__isl_take isl_vertex
*vertex
)
1042 isl_vertices_free(vertex
->vertices
);
1046 __isl_give isl_basic_set
*isl_basic_set_set_integral(__isl_take isl_basic_set
*bset
)
1051 if (!ISL_F_ISSET(bset
, ISL_BASIC_MAP_RATIONAL
))
1054 bset
= isl_basic_set_cow(bset
);
1058 ISL_F_CLR(bset
, ISL_BASIC_MAP_RATIONAL
);
1060 return isl_basic_set_finalize(bset
);
1063 isl_ctx
*isl_cell_get_ctx(__isl_keep isl_cell
*cell
)
1065 return cell
? cell
->dom
->ctx
: NULL
;
1068 __isl_give isl_basic_set
*isl_cell_get_domain(__isl_keep isl_cell
*cell
)
1070 return cell
? isl_basic_set_copy(cell
->dom
) : NULL
;
1073 static __isl_give isl_cell
*isl_cell_alloc(__isl_take isl_vertices
*vertices
,
1074 __isl_take isl_basic_set
*dom
, int id
)
1077 isl_cell
*cell
= NULL
;
1079 if (!vertices
|| !dom
)
1082 cell
= isl_calloc_type(dom
->ctx
, isl_cell
);
1086 cell
->n_vertices
= vertices
->c
[id
].n_vertices
;
1087 cell
->ids
= isl_alloc_array(dom
->ctx
, int, cell
->n_vertices
);
1090 for (i
= 0; i
< cell
->n_vertices
; ++i
)
1091 cell
->ids
[i
] = vertices
->c
[id
].vertices
[i
];
1092 cell
->vertices
= vertices
;
1097 isl_cell_free(cell
);
1098 isl_vertices_free(vertices
);
1099 isl_basic_set_free(dom
);
1103 void isl_cell_free(__isl_take isl_cell
*cell
)
1108 isl_vertices_free(cell
->vertices
);
1110 isl_basic_set_free(cell
->dom
);
1114 /* Create a tableau of the cone obtained by first homogenizing the given
1115 * polytope and then making all inequalities strict by setting the
1116 * constant term to -1.
1118 static struct isl_tab
*tab_for_shifted_cone(__isl_keep isl_basic_set
*bset
)
1122 struct isl_tab
*tab
;
1126 tab
= isl_tab_alloc(bset
->ctx
, bset
->n_ineq
+ 1,
1127 1 + isl_basic_set_total_dim(bset
), 0);
1130 tab
->rational
= ISL_F_ISSET(bset
, ISL_BASIC_SET_RATIONAL
);
1131 if (ISL_F_ISSET(bset
, ISL_BASIC_MAP_EMPTY
)) {
1132 if (isl_tab_mark_empty(tab
) < 0)
1137 c
= isl_vec_alloc(bset
->ctx
, 1 + 1 + isl_basic_set_total_dim(bset
));
1141 isl_int_set_si(c
->el
[0], 0);
1142 for (i
= 0; i
< bset
->n_eq
; ++i
) {
1143 isl_seq_cpy(c
->el
+ 1, bset
->eq
[i
], c
->size
- 1);
1144 if (isl_tab_add_eq(tab
, c
->el
) < 0)
1148 isl_int_set_si(c
->el
[0], -1);
1149 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
1150 isl_seq_cpy(c
->el
+ 1, bset
->ineq
[i
], c
->size
- 1);
1151 if (isl_tab_add_ineq(tab
, c
->el
) < 0)
1159 isl_seq_clr(c
->el
+ 1, c
->size
- 1);
1160 isl_int_set_si(c
->el
[1], 1);
1161 if (isl_tab_add_ineq(tab
, c
->el
) < 0)
1172 /* Compute an interior point of "bset" by selecting an interior
1173 * point in homogeneous space and projecting the point back down.
1175 static __isl_give isl_vec
*isl_basic_set_interior_point(
1176 __isl_keep isl_basic_set
*bset
)
1179 struct isl_tab
*tab
;
1181 tab
= tab_for_shifted_cone(bset
);
1182 vec
= isl_tab_get_sample_value(tab
);
1187 isl_seq_cpy(vec
->el
, vec
->el
+ 1, vec
->size
- 1);
1193 /* Call "fn" on all chambers of the parametric polytope with the shared
1194 * facets of neighboring chambers only appearing in one of the chambers.
1196 * We pick an interior point from one of the chambers and then make
1197 * all constraints that do not satisfy this point strict.
1199 int isl_vertices_foreach_disjoint_cell(__isl_keep isl_vertices
*vertices
,
1200 int (*fn
)(__isl_take isl_cell
*cell
, void *user
), void *user
)
1210 if (vertices
->n_chambers
== 0)
1213 if (vertices
->n_chambers
== 1) {
1214 isl_basic_set
*dom
= isl_basic_set_copy(vertices
->c
[0].dom
);
1215 dom
= isl_basic_set_set_integral(dom
);
1216 cell
= isl_cell_alloc(isl_vertices_copy(vertices
), dom
, 0);
1219 return fn(cell
, user
);
1222 vec
= isl_basic_set_interior_point(vertices
->c
[0].dom
);
1228 for (i
= 0; i
< vertices
->n_chambers
; ++i
) {
1230 isl_basic_set
*dom
= isl_basic_set_copy(vertices
->c
[i
].dom
);
1231 dom
= isl_basic_set_cow(dom
);
1234 for (j
= 0; i
&& j
< dom
->n_ineq
; ++j
) {
1235 isl_seq_inner_product(vec
->el
, dom
->ineq
[j
], vec
->size
,
1237 if (!isl_int_is_neg(v
))
1239 isl_int_sub_ui(dom
->ineq
[j
][0], dom
->ineq
[j
][0], 1);
1241 dom
= isl_basic_set_set_integral(dom
);
1242 cell
= isl_cell_alloc(isl_vertices_copy(vertices
), dom
, i
);
1260 int isl_vertices_foreach_cell(__isl_keep isl_vertices
*vertices
,
1261 int (*fn
)(__isl_take isl_cell
*cell
, void *user
), void *user
)
1269 if (vertices
->n_chambers
== 0)
1272 for (i
= 0; i
< vertices
->n_chambers
; ++i
) {
1274 isl_basic_set
*dom
= isl_basic_set_copy(vertices
->c
[i
].dom
);
1276 cell
= isl_cell_alloc(isl_vertices_copy(vertices
), dom
, i
);
1288 int isl_vertices_foreach_vertex(__isl_keep isl_vertices
*vertices
,
1289 int (*fn
)(__isl_take isl_vertex
*vertex
, void *user
), void *user
)
1297 if (vertices
->n_vertices
== 0)
1300 for (i
= 0; i
< vertices
->n_vertices
; ++i
) {
1303 vertex
= isl_vertex_alloc(isl_vertices_copy(vertices
), i
);
1307 r
= fn(vertex
, user
);
1315 int isl_cell_foreach_vertex(__isl_keep isl_cell
*cell
,
1316 int (*fn
)(__isl_take isl_vertex
*vertex
, void *user
), void *user
)
1324 if (cell
->n_vertices
== 0)
1327 for (i
= 0; i
< cell
->n_vertices
; ++i
) {
1330 vertex
= isl_vertex_alloc(isl_vertices_copy(cell
->vertices
),
1335 r
= fn(vertex
, user
);
1343 isl_ctx
*isl_vertices_get_ctx(__isl_keep isl_vertices
*vertices
)
1345 return vertices
? vertices
->bset
->ctx
: NULL
;
1348 int isl_vertices_get_n_vertices(__isl_keep isl_vertices
*vertices
)
1350 return vertices
? vertices
->n_vertices
: -1;
1353 __isl_give isl_vertices
*isl_morph_vertices(__isl_take isl_morph
*morph
,
1354 __isl_take isl_vertices
*vertices
)
1357 isl_morph
*param_morph
= NULL
;
1359 if (!morph
|| !vertices
)
1362 isl_assert(vertices
->bset
->ctx
, vertices
->ref
== 1, goto error
);
1364 param_morph
= isl_morph_copy(morph
);
1365 param_morph
= isl_morph_dom_params(param_morph
);
1366 param_morph
= isl_morph_ran_params(param_morph
);
1368 for (i
= 0; i
< vertices
->n_vertices
; ++i
) {
1369 vertices
->v
[i
].dom
= isl_morph_basic_set(
1370 isl_morph_copy(param_morph
), vertices
->v
[i
].dom
);
1371 vertices
->v
[i
].vertex
= isl_morph_basic_set(
1372 isl_morph_copy(morph
), vertices
->v
[i
].vertex
);
1373 if (!vertices
->v
[i
].vertex
)
1377 for (i
= 0; i
< vertices
->n_chambers
; ++i
) {
1378 vertices
->c
[i
].dom
= isl_morph_basic_set(
1379 isl_morph_copy(param_morph
), vertices
->c
[i
].dom
);
1380 if (!vertices
->c
[i
].dom
)
1384 isl_morph_free(param_morph
);
1385 isl_morph_free(morph
);
1388 isl_morph_free(param_morph
);
1389 isl_morph_free(morph
);
1390 isl_vertices_free(vertices
);
1394 /* Construct a simplex isl_cell spanned by the vertices with indices in
1395 * "simplex_ids" and "other_ids" and call "fn" on this isl_cell.
1397 static int call_on_simplex(__isl_keep isl_cell
*cell
,
1398 int *simplex_ids
, int n_simplex
, int *other_ids
, int n_other
,
1399 int (*fn
)(__isl_take isl_cell
*simplex
, void *user
), void *user
)
1403 struct isl_cell
*simplex
;
1405 ctx
= isl_cell_get_ctx(cell
);
1407 simplex
= isl_calloc_type(ctx
, struct isl_cell
);
1410 simplex
->vertices
= isl_vertices_copy(cell
->vertices
);
1411 if (!simplex
->vertices
)
1413 simplex
->dom
= isl_basic_set_copy(cell
->dom
);
1416 simplex
->n_vertices
= n_simplex
+ n_other
;
1417 simplex
->ids
= isl_alloc_array(ctx
, int, simplex
->n_vertices
);
1421 for (i
= 0; i
< n_simplex
; ++i
)
1422 simplex
->ids
[i
] = simplex_ids
[i
];
1423 for (i
= 0; i
< n_other
; ++i
)
1424 simplex
->ids
[n_simplex
+ i
] = other_ids
[i
];
1426 return fn(simplex
, user
);
1428 isl_cell_free(simplex
);
1432 /* Check whether the parametric vertex described by "vertex"
1433 * lies on the facet corresponding to constraint "facet" of "bset".
1434 * The isl_vec "v" is a temporary vector than can be used by this function.
1436 * We eliminate the variables from the facet constraint using the
1437 * equalities defining the vertex and check if the result is identical
1440 * It would probably be better to keep track of the constraints defining
1441 * a vertex during the vertex construction so that we could simply look
1444 static int vertex_on_facet(__isl_keep isl_basic_set
*vertex
,
1445 __isl_keep isl_basic_set
*bset
, int facet
, __isl_keep isl_vec
*v
)
1450 isl_seq_cpy(v
->el
, bset
->ineq
[facet
], v
->size
);
1453 for (i
= 0; i
< vertex
->n_eq
; ++i
) {
1454 int k
= isl_seq_last_non_zero(vertex
->eq
[i
], v
->size
);
1455 isl_seq_elim(v
->el
, vertex
->eq
[i
], k
, v
->size
, &m
);
1459 return isl_seq_first_non_zero(v
->el
, v
->size
) == -1;
1462 /* Triangulate the polytope spanned by the vertices with ids
1463 * in "simplex_ids" and "other_ids" and call "fn" on each of
1464 * the resulting simplices.
1465 * If the input polytope is already a simplex, we simply call "fn".
1466 * Otherwise, we pick a point from "other_ids" and add it to "simplex_ids".
1467 * Then we consider each facet of "bset" that does not contain the point
1468 * we just picked, but does contain some of the other points in "other_ids"
1469 * and call ourselves recursively on the polytope spanned by the new
1470 * "simplex_ids" and those points in "other_ids" that lie on the facet.
1472 static int triangulate(__isl_keep isl_cell
*cell
, __isl_keep isl_vec
*v
,
1473 int *simplex_ids
, int n_simplex
, int *other_ids
, int n_other
,
1474 int (*fn
)(__isl_take isl_cell
*simplex
, void *user
), void *user
)
1480 isl_basic_set
*vertex
;
1481 isl_basic_set
*bset
;
1483 ctx
= isl_cell_get_ctx(cell
);
1484 d
= isl_basic_set_dim(cell
->vertices
->bset
, isl_dim_set
);
1485 nparam
= isl_basic_set_dim(cell
->vertices
->bset
, isl_dim_param
);
1487 if (n_simplex
+ n_other
== d
+ 1)
1488 return call_on_simplex(cell
, simplex_ids
, n_simplex
,
1489 other_ids
, n_other
, fn
, user
);
1491 simplex_ids
[n_simplex
] = other_ids
[0];
1492 vertex
= cell
->vertices
->v
[other_ids
[0]].vertex
;
1493 bset
= cell
->vertices
->bset
;
1495 ids
= isl_alloc_array(ctx
, int, n_other
- 1);
1496 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
1497 if (isl_seq_first_non_zero(bset
->ineq
[i
] + 1 + nparam
, d
) == -1)
1499 if (vertex_on_facet(vertex
, bset
, i
, v
))
1502 for (j
= 1, k
= 0; j
< n_other
; ++j
) {
1504 ov
= cell
->vertices
->v
[other_ids
[j
]].vertex
;
1505 if (vertex_on_facet(ov
, bset
, i
, v
))
1506 ids
[k
++] = other_ids
[j
];
1511 if (triangulate(cell
, v
, simplex_ids
, n_simplex
+ 1,
1512 ids
, k
, fn
, user
) < 0)
1523 /* Triangulate the given cell and call "fn" on each of the resulting
1526 int isl_cell_foreach_simplex(__isl_take isl_cell
*cell
,
1527 int (*fn
)(__isl_take isl_cell
*simplex
, void *user
), void *user
)
1533 int *simplex_ids
= NULL
;
1538 d
= isl_basic_set_dim(cell
->vertices
->bset
, isl_dim_set
);
1539 total
= isl_basic_set_total_dim(cell
->vertices
->bset
);
1541 if (cell
->n_vertices
== d
+ 1)
1542 return fn(cell
, user
);
1544 ctx
= isl_cell_get_ctx(cell
);
1545 simplex_ids
= isl_alloc_array(ctx
, int, d
+ 1);
1549 v
= isl_vec_alloc(ctx
, 1 + total
);
1553 r
= triangulate(cell
, v
, simplex_ids
, 0,
1554 cell
->ids
, cell
->n_vertices
, fn
, user
);
1559 isl_cell_free(cell
);
1565 isl_cell_free(cell
);