2 * Copyright 2010 INRIA Saclay
4 * Use of this software is governed by the MIT 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>
12 #include <isl_aff_private.h>
16 #include <isl_space_private.h>
17 #include <isl_morph.h>
18 #include <isl_vertices_private.h>
19 #include <isl_mat_private.h>
20 #include <isl_vec_private.h>
26 static __isl_give isl_vertices
*compute_chambers(__isl_take isl_basic_set
*bset
,
27 __isl_take isl_vertices
*vertices
);
29 __isl_give isl_vertices
*isl_vertices_copy(__isl_keep isl_vertices
*vertices
)
38 void isl_vertices_free(__isl_take isl_vertices
*vertices
)
45 if (--vertices
->ref
> 0)
48 for (i
= 0; i
< vertices
->n_vertices
; ++i
) {
49 isl_basic_set_free(vertices
->v
[i
].vertex
);
50 isl_basic_set_free(vertices
->v
[i
].dom
);
54 for (i
= 0; i
< vertices
->n_chambers
; ++i
) {
55 free(vertices
->c
[i
].vertices
);
56 isl_basic_set_free(vertices
->c
[i
].dom
);
60 isl_basic_set_free(vertices
->bset
);
64 struct isl_vertex_list
{
66 struct isl_vertex_list
*next
;
69 static void free_vertex_list(struct isl_vertex_list
*list
)
71 struct isl_vertex_list
*next
;
73 for (; list
; list
= next
) {
75 isl_basic_set_free(list
->v
.vertex
);
76 isl_basic_set_free(list
->v
.dom
);
81 static __isl_give isl_vertices
*vertices_from_list(__isl_keep isl_basic_set
*bset
,
82 int n_vertices
, struct isl_vertex_list
*list
)
85 struct isl_vertex_list
*next
;
86 isl_vertices
*vertices
;
88 vertices
= isl_calloc_type(bset
->ctx
, isl_vertices
);
92 vertices
->bset
= isl_basic_set_copy(bset
);
93 vertices
->v
= isl_alloc_array(bset
->ctx
, struct isl_vertex
, n_vertices
);
94 if (n_vertices
&& !vertices
->v
)
96 vertices
->n_vertices
= n_vertices
;
98 for (i
= 0; list
; list
= next
, i
++) {
100 vertices
->v
[i
] = list
->v
;
106 isl_vertices_free(vertices
);
107 free_vertex_list(list
);
111 /* Prepend a vertex to the linked list "list" based on the equalities in "tab".
112 * Return isl_bool_true if the vertex was actually added and
113 * isl_bool_false otherwise.
114 * In particular, vertices with a lower-dimensional activity domain are
115 * not added to the list because they would not be included in any chamber.
116 * Return isl_bool_error on error.
118 static isl_bool
add_vertex(struct isl_vertex_list
**list
,
119 __isl_keep isl_basic_set
*bset
, struct isl_tab
*tab
)
122 struct isl_vertex_list
*v
= NULL
;
124 if (isl_tab_detect_implicit_equalities(tab
) < 0)
125 return isl_bool_error
;
127 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
129 v
= isl_calloc_type(tab
->mat
->ctx
, struct isl_vertex_list
);
133 v
->v
.vertex
= isl_basic_set_copy(bset
);
134 v
->v
.vertex
= isl_basic_set_cow(v
->v
.vertex
);
135 v
->v
.vertex
= isl_basic_set_update_from_tab(v
->v
.vertex
, tab
);
136 v
->v
.vertex
= isl_basic_set_simplify(v
->v
.vertex
);
137 v
->v
.vertex
= isl_basic_set_finalize(v
->v
.vertex
);
140 isl_assert(bset
->ctx
, v
->v
.vertex
->n_eq
>= nvar
, goto error
);
141 v
->v
.dom
= isl_basic_set_copy(v
->v
.vertex
);
142 v
->v
.dom
= isl_basic_set_params(v
->v
.dom
);
146 if (v
->v
.dom
->n_eq
> 0) {
148 return isl_bool_false
;
154 return isl_bool_true
;
157 return isl_bool_error
;
160 /* Compute the parametric vertices and the chamber decomposition
161 * of an empty parametric polytope.
163 static __isl_give isl_vertices
*vertices_empty(__isl_keep isl_basic_set
*bset
)
165 isl_vertices
*vertices
;
170 vertices
= isl_calloc_type(bset
->ctx
, isl_vertices
);
173 vertices
->bset
= isl_basic_set_copy(bset
);
176 vertices
->n_vertices
= 0;
177 vertices
->n_chambers
= 0;
182 /* Compute the parametric vertices and the chamber decomposition
183 * of the parametric polytope defined using the same constraints
184 * as "bset" in the 0D case.
185 * There is exactly one 0D vertex and a single chamber containing
188 static __isl_give isl_vertices
*vertices_0D(__isl_keep isl_basic_set
*bset
)
190 isl_vertices
*vertices
;
195 vertices
= isl_calloc_type(bset
->ctx
, isl_vertices
);
199 vertices
->bset
= isl_basic_set_copy(bset
);
201 vertices
->v
= isl_calloc_array(bset
->ctx
, struct isl_vertex
, 1);
204 vertices
->n_vertices
= 1;
205 vertices
->v
[0].vertex
= isl_basic_set_copy(bset
);
206 vertices
->v
[0].dom
= isl_basic_set_params(isl_basic_set_copy(bset
));
207 if (!vertices
->v
[0].vertex
|| !vertices
->v
[0].dom
)
210 vertices
->c
= isl_calloc_array(bset
->ctx
, struct isl_chamber
, 1);
213 vertices
->n_chambers
= 1;
214 vertices
->c
[0].n_vertices
= 1;
215 vertices
->c
[0].vertices
= isl_calloc_array(bset
->ctx
, int, 1);
216 if (!vertices
->c
[0].vertices
)
218 vertices
->c
[0].dom
= isl_basic_set_copy(vertices
->v
[0].dom
);
219 if (!vertices
->c
[0].dom
)
224 isl_vertices_free(vertices
);
228 static int isl_mat_rank(__isl_keep isl_mat
*mat
)
233 H
= isl_mat_left_hermite(isl_mat_copy(mat
), 0, NULL
, NULL
);
237 for (col
= 0; col
< H
->n_col
; ++col
) {
238 for (row
= 0; row
< H
->n_row
; ++row
)
239 if (!isl_int_is_zero(H
->row
[row
][col
]))
250 /* Is the row pointed to by "f" linearly independent of the "n" first
253 static int is_independent(__isl_keep isl_mat
*facets
, int n
, isl_int
*f
)
257 if (isl_seq_first_non_zero(f
, facets
->n_col
) < 0)
260 isl_seq_cpy(facets
->row
[n
], f
, facets
->n_col
);
261 facets
->n_row
= n
+ 1;
262 rank
= isl_mat_rank(facets
);
266 return rank
== n
+ 1;
269 /* Check whether we can select constraint "level", given the current selection
270 * reflected by facets in "tab", the rows of "facets" and the earlier
271 * "selected" elements of "selection".
273 * If the constraint is (strictly) redundant in the tableau, selecting it would
274 * result in an empty tableau, so it can't be selected.
275 * If the set variable part of the constraint is not linearly independent
276 * of the set variable parts of the already selected constraints,
277 * the constraint cannot be selected.
278 * If selecting the constraint results in an empty tableau, the constraint
279 * cannot be selected.
280 * Finally, if selecting the constraint results in some explicitly
281 * deselected constraints turning into equalities, then the corresponding
282 * vertices have already been generated, so the constraint cannot be selected.
284 static int can_select(__isl_keep isl_basic_set
*bset
, int level
,
285 struct isl_tab
*tab
, __isl_keep isl_mat
*facets
, int selected
,
291 struct isl_tab_undo
*snap
;
293 if (isl_tab_is_redundant(tab
, level
))
296 ovar
= isl_space_offset(bset
->dim
, isl_dim_set
);
298 indep
= is_independent(facets
, selected
, bset
->ineq
[level
] + 1 + ovar
);
304 snap
= isl_tab_snap(tab
);
305 if (isl_tab_select_facet(tab
, level
) < 0)
309 if (isl_tab_rollback(tab
, snap
) < 0)
314 for (i
= 0; i
< level
; ++i
) {
317 if (selection
[i
] != DESELECTED
)
320 if (isl_tab_is_equality(tab
, i
))
322 else if (isl_tab_is_redundant(tab
, i
))
325 sgn
= isl_tab_sign_of_max(tab
, i
);
329 if (isl_tab_rollback(tab
, snap
) < 0)
338 /* Compute the parametric vertices and the chamber decomposition
339 * of a parametric polytope that is not full-dimensional.
341 * Simply map the parametric polytope to a lower dimensional space
342 * and map the resulting vertices back.
344 static __isl_give isl_vertices
*lower_dim_vertices(
345 __isl_keep isl_basic_set
*bset
)
348 isl_vertices
*vertices
;
350 bset
= isl_basic_set_copy(bset
);
351 morph
= isl_basic_set_full_compression(bset
);
352 bset
= isl_morph_basic_set(isl_morph_copy(morph
), bset
);
354 vertices
= isl_basic_set_compute_vertices(bset
);
355 isl_basic_set_free(bset
);
357 morph
= isl_morph_inverse(morph
);
359 vertices
= isl_morph_vertices(morph
, vertices
);
364 /* Compute the parametric vertices and the chamber decomposition
365 * of the parametric polytope defined using the same constraints
366 * as "bset". "bset" is assumed to have no existentially quantified
369 * The vertices themselves are computed in a fairly simplistic way.
370 * We simply run through all combinations of d constraints,
371 * with d the number of set variables, and check if those d constraints
372 * define a vertex. To avoid the generation of duplicate vertices,
373 * which we may happen if a vertex is defined by more that d constraints,
374 * we make sure we only generate the vertex for the d constraints with
377 * We set up a tableau and keep track of which facets have been
378 * selected. The tableau is marked strict_redundant so that we can be
379 * sure that any constraint that is marked redundant (and that is not
380 * also marked zero) is not an equality.
381 * If a constraint is marked DESELECTED, it means the constraint was
382 * SELECTED before (in combination with the same selection of earlier
383 * constraints). If such a deselected constraint turns out to be an
384 * equality, then any vertex that may still be found with the current
385 * selection has already been generated when the constraint was selected.
386 * A constraint is marked UNSELECTED when there is no way selecting
387 * the constraint could lead to a vertex (in combination with the current
388 * selection of earlier constraints).
390 * The set variable coefficients of the selected constraints are stored
391 * in the facets matrix.
393 __isl_give isl_vertices
*isl_basic_set_compute_vertices(
394 __isl_keep isl_basic_set
*bset
)
400 int *selection
= NULL
;
402 struct isl_tab_undo
**snap
= NULL
;
403 isl_mat
*facets
= NULL
;
404 struct isl_vertex_list
*list
= NULL
;
406 isl_vertices
*vertices
;
411 if (isl_basic_set_plain_is_empty(bset
))
412 return vertices_empty(bset
);
415 return lower_dim_vertices(bset
);
417 isl_assert(bset
->ctx
, isl_basic_set_dim(bset
, isl_dim_div
) == 0,
420 if (isl_basic_set_dim(bset
, isl_dim_set
) == 0)
421 return vertices_0D(bset
);
423 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
425 bset
= isl_basic_set_copy(bset
);
426 bset
= isl_basic_set_set_rational(bset
);
430 tab
= isl_tab_from_basic_set(bset
, 0);
433 tab
->strict_redundant
= 1;
436 vertices
= vertices_empty(bset
);
437 isl_basic_set_free(bset
);
442 selection
= isl_alloc_array(bset
->ctx
, int, bset
->n_ineq
);
443 snap
= isl_alloc_array(bset
->ctx
, struct isl_tab_undo
*, bset
->n_ineq
);
444 facets
= isl_mat_alloc(bset
->ctx
, nvar
, nvar
);
445 if ((bset
->n_ineq
&& (!selection
|| !snap
)) || !facets
)
453 if (level
>= bset
->n_ineq
||
454 (!init
&& selection
[level
] != SELECTED
)) {
461 snap
[level
] = isl_tab_snap(tab
);
462 ok
= can_select(bset
, level
, tab
, facets
, selected
,
467 selection
[level
] = SELECTED
;
470 selection
[level
] = UNSELECTED
;
472 selection
[level
] = DESELECTED
;
474 if (isl_tab_rollback(tab
, snap
[level
]) < 0)
477 if (selected
== nvar
) {
478 if (tab
->n_dead
== nvar
) {
479 isl_bool added
= add_vertex(&list
, bset
, tab
);
492 isl_mat_free(facets
);
498 vertices
= vertices_from_list(bset
, n_vertices
, list
);
500 vertices
= compute_chambers(bset
, vertices
);
504 free_vertex_list(list
);
505 isl_mat_free(facets
);
509 isl_basic_set_free(bset
);
513 struct isl_chamber_list
{
514 struct isl_chamber c
;
515 struct isl_chamber_list
*next
;
518 static void free_chamber_list(struct isl_chamber_list
*list
)
520 struct isl_chamber_list
*next
;
522 for (; list
; list
= next
) {
524 isl_basic_set_free(list
->c
.dom
);
525 free(list
->c
.vertices
);
530 /* Check whether the basic set "bset" is a superset of the basic set described
531 * by "tab", i.e., check whether all constraints of "bset" are redundant.
533 static int bset_covers_tab(__isl_keep isl_basic_set
*bset
, struct isl_tab
*tab
)
540 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
541 enum isl_ineq_type type
= isl_tab_ineq_type(tab
, bset
->ineq
[i
]);
543 case isl_ineq_error
: return -1;
544 case isl_ineq_redundant
: continue;
552 static __isl_give isl_vertices
*vertices_add_chambers(
553 __isl_take isl_vertices
*vertices
, int n_chambers
,
554 struct isl_chamber_list
*list
)
558 struct isl_chamber_list
*next
;
560 ctx
= isl_vertices_get_ctx(vertices
);
561 vertices
->c
= isl_alloc_array(ctx
, struct isl_chamber
, n_chambers
);
564 vertices
->n_chambers
= n_chambers
;
566 for (i
= 0; list
; list
= next
, i
++) {
568 vertices
->c
[i
] = list
->c
;
574 isl_vertices_free(vertices
);
575 free_chamber_list(list
);
579 /* Can "tab" be intersected with "bset" without resulting in
580 * a lower-dimensional set.
581 * "bset" itself is assumed to be full-dimensional.
583 static isl_bool
can_intersect(struct isl_tab
*tab
,
584 __isl_keep isl_basic_set
*bset
)
587 struct isl_tab_undo
*snap
;
590 isl_die(isl_basic_set_get_ctx(bset
), isl_error_internal
,
591 "expecting full-dimensional input",
592 return isl_bool_error
);
594 if (isl_tab_extend_cons(tab
, bset
->n_ineq
) < 0)
595 return isl_bool_error
;
597 snap
= isl_tab_snap(tab
);
599 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
600 if (isl_tab_ineq_type(tab
, bset
->ineq
[i
]) == isl_ineq_redundant
)
602 if (isl_tab_add_ineq(tab
, bset
->ineq
[i
]) < 0)
603 return isl_bool_error
;
606 if (isl_tab_detect_implicit_equalities(tab
) < 0)
607 return isl_bool_error
;
609 if (isl_tab_rollback(tab
, snap
) < 0)
610 return isl_bool_error
;
611 return isl_bool_false
;
614 return isl_bool_true
;
617 static int add_chamber(struct isl_chamber_list
**list
,
618 __isl_keep isl_vertices
*vertices
, struct isl_tab
*tab
, int *selection
)
623 struct isl_tab_undo
*snap
;
624 struct isl_chamber_list
*c
= NULL
;
626 for (i
= 0; i
< vertices
->n_vertices
; ++i
)
630 snap
= isl_tab_snap(tab
);
632 for (i
= 0; i
< tab
->n_con
&& tab
->con
[i
].frozen
; ++i
)
633 tab
->con
[i
].frozen
= 0;
636 if (isl_tab_detect_redundant(tab
) < 0)
639 c
= isl_calloc_type(tab
->mat
->ctx
, struct isl_chamber_list
);
642 c
->c
.vertices
= isl_alloc_array(tab
->mat
->ctx
, int, n_vertices
);
643 if (n_vertices
&& !c
->c
.vertices
)
645 c
->c
.dom
= isl_basic_set_copy(isl_tab_peek_bset(tab
));
646 c
->c
.dom
= isl_basic_set_set_rational(c
->c
.dom
);
647 c
->c
.dom
= isl_basic_set_cow(c
->c
.dom
);
648 c
->c
.dom
= isl_basic_set_update_from_tab(c
->c
.dom
, tab
);
649 c
->c
.dom
= isl_basic_set_simplify(c
->c
.dom
);
650 c
->c
.dom
= isl_basic_set_finalize(c
->c
.dom
);
654 c
->c
.n_vertices
= n_vertices
;
656 for (i
= 0, j
= 0; i
< vertices
->n_vertices
; ++i
)
658 c
->c
.vertices
[j
] = i
;
665 for (i
= 0; i
< n_frozen
; ++i
)
666 tab
->con
[i
].frozen
= 1;
668 if (isl_tab_rollback(tab
, snap
) < 0)
673 free_chamber_list(c
);
677 struct isl_facet_todo
{
678 struct isl_tab
*tab
; /* A tableau representation of the facet */
679 isl_basic_set
*bset
; /* A normalized basic set representation */
680 isl_vec
*constraint
; /* Constraint pointing to the other side */
681 struct isl_facet_todo
*next
;
684 static void free_todo(struct isl_facet_todo
*todo
)
687 struct isl_facet_todo
*next
= todo
->next
;
689 isl_tab_free(todo
->tab
);
690 isl_basic_set_free(todo
->bset
);
691 isl_vec_free(todo
->constraint
);
698 static struct isl_facet_todo
*create_todo(struct isl_tab
*tab
, int con
)
702 struct isl_tab_undo
*snap
;
703 struct isl_facet_todo
*todo
;
705 snap
= isl_tab_snap(tab
);
707 for (i
= 0; i
< tab
->n_con
&& tab
->con
[i
].frozen
; ++i
)
708 tab
->con
[i
].frozen
= 0;
711 if (isl_tab_detect_redundant(tab
) < 0)
714 todo
= isl_calloc_type(tab
->mat
->ctx
, struct isl_facet_todo
);
718 todo
->constraint
= isl_vec_alloc(tab
->mat
->ctx
, 1 + tab
->n_var
);
719 if (!todo
->constraint
)
721 isl_seq_neg(todo
->constraint
->el
, tab
->bmap
->ineq
[con
], 1 + tab
->n_var
);
722 todo
->bset
= isl_basic_set_copy(isl_tab_peek_bset(tab
));
723 todo
->bset
= isl_basic_set_set_rational(todo
->bset
);
724 todo
->bset
= isl_basic_set_cow(todo
->bset
);
725 todo
->bset
= isl_basic_set_update_from_tab(todo
->bset
, tab
);
726 todo
->bset
= isl_basic_set_simplify(todo
->bset
);
727 todo
->bset
= isl_basic_set_sort_constraints(todo
->bset
);
730 ISL_F_SET(todo
->bset
, ISL_BASIC_SET_NORMALIZED
);
731 todo
->tab
= isl_tab_dup(tab
);
735 for (i
= 0; i
< n_frozen
; ++i
)
736 tab
->con
[i
].frozen
= 1;
738 if (isl_tab_rollback(tab
, snap
) < 0)
747 /* Create todo items for all interior facets of the chamber represented
748 * by "tab" and collect them in "next".
750 static int init_todo(struct isl_facet_todo
**next
, struct isl_tab
*tab
)
753 struct isl_tab_undo
*snap
;
754 struct isl_facet_todo
*todo
;
756 snap
= isl_tab_snap(tab
);
758 for (i
= 0; i
< tab
->n_con
; ++i
) {
759 if (tab
->con
[i
].frozen
)
761 if (tab
->con
[i
].is_redundant
)
764 if (isl_tab_select_facet(tab
, i
) < 0)
767 todo
= create_todo(tab
, i
);
774 if (isl_tab_rollback(tab
, snap
) < 0)
781 /* Does the linked list contain a todo item that is the opposite of "todo".
782 * If so, return 1 and remove the opposite todo item.
784 static int has_opposite(struct isl_facet_todo
*todo
,
785 struct isl_facet_todo
**list
)
787 for (; *list
; list
= &(*list
)->next
) {
789 eq
= isl_basic_set_plain_is_equal(todo
->bset
, (*list
)->bset
);
804 /* Create todo items for all interior facets of the chamber represented
805 * by "tab" and collect them in first->next, taking care to cancel
806 * opposite todo items.
808 static int update_todo(struct isl_facet_todo
*first
, struct isl_tab
*tab
)
811 struct isl_tab_undo
*snap
;
812 struct isl_facet_todo
*todo
;
814 snap
= isl_tab_snap(tab
);
816 for (i
= 0; i
< tab
->n_con
; ++i
) {
819 if (tab
->con
[i
].frozen
)
821 if (tab
->con
[i
].is_redundant
)
824 if (isl_tab_select_facet(tab
, i
) < 0)
827 todo
= create_todo(tab
, i
);
831 drop
= has_opposite(todo
, &first
->next
);
838 todo
->next
= first
->next
;
842 if (isl_tab_rollback(tab
, snap
) < 0)
849 /* Compute the chamber decomposition of the parametric polytope respresented
850 * by "bset" given the parametric vertices and their activity domains.
852 * We are only interested in full-dimensional chambers.
853 * Each of these chambers is the intersection of the activity domains of
854 * one or more vertices and the union of all chambers is equal to the
855 * projection of the entire parametric polytope onto the parameter space.
857 * We first create an initial chamber by intersecting as many activity
858 * domains as possible without ending up with an empty or lower-dimensional
859 * set. As a minor optimization, we only consider those activity domains
860 * that contain some arbitrary point.
862 * For each of the interior facets of the chamber, we construct a todo item,
863 * containing the facet and a constraint containing the other side of the facet,
864 * for constructing the chamber on the other side.
865 * While their are any todo items left, we pick a todo item and
866 * create the required chamber by intersecting all activity domains
867 * that contain the facet and have a full-dimensional intersection with
868 * the other side of the facet. For each of the interior facets, we
869 * again create todo items, taking care to cancel opposite todo items.
871 static __isl_give isl_vertices
*compute_chambers(__isl_take isl_basic_set
*bset
,
872 __isl_take isl_vertices
*vertices
)
876 isl_vec
*sample
= NULL
;
877 struct isl_tab
*tab
= NULL
;
878 struct isl_tab_undo
*snap
;
879 int *selection
= NULL
;
881 struct isl_chamber_list
*list
= NULL
;
882 struct isl_facet_todo
*todo
= NULL
;
884 if (!bset
|| !vertices
)
887 ctx
= isl_vertices_get_ctx(vertices
);
888 selection
= isl_alloc_array(ctx
, int, vertices
->n_vertices
);
889 if (vertices
->n_vertices
&& !selection
)
892 bset
= isl_basic_set_params(bset
);
894 tab
= isl_tab_from_basic_set(bset
, 1);
897 for (i
= 0; i
< bset
->n_ineq
; ++i
)
898 if (isl_tab_freeze_constraint(tab
, i
) < 0)
900 isl_basic_set_free(bset
);
902 snap
= isl_tab_snap(tab
);
904 sample
= isl_tab_get_sample_value(tab
);
906 for (i
= 0; i
< vertices
->n_vertices
; ++i
) {
907 selection
[i
] = isl_basic_set_contains(vertices
->v
[i
].dom
, sample
);
908 if (selection
[i
] < 0)
912 selection
[i
] = can_intersect(tab
, vertices
->v
[i
].dom
);
913 if (selection
[i
] < 0)
917 if (isl_tab_detect_redundant(tab
) < 0)
920 if (add_chamber(&list
, vertices
, tab
, selection
) < 0)
924 if (init_todo(&todo
, tab
) < 0)
928 struct isl_facet_todo
*next
;
930 if (isl_tab_rollback(tab
, snap
) < 0)
933 if (isl_tab_add_ineq(tab
, todo
->constraint
->el
) < 0)
935 if (isl_tab_freeze_constraint(tab
, tab
->n_con
- 1) < 0)
938 for (i
= 0; i
< vertices
->n_vertices
; ++i
) {
939 selection
[i
] = bset_covers_tab(vertices
->v
[i
].dom
,
941 if (selection
[i
] < 0)
945 selection
[i
] = can_intersect(tab
, vertices
->v
[i
].dom
);
946 if (selection
[i
] < 0)
950 if (isl_tab_detect_redundant(tab
) < 0)
953 if (add_chamber(&list
, vertices
, tab
, selection
) < 0)
957 if (update_todo(todo
, tab
) < 0)
966 isl_vec_free(sample
);
971 vertices
= vertices_add_chambers(vertices
, n_chambers
, list
);
973 for (i
= 0; vertices
&& i
< vertices
->n_vertices
; ++i
) {
974 isl_basic_set_free(vertices
->v
[i
].dom
);
975 vertices
->v
[i
].dom
= NULL
;
980 free_chamber_list(list
);
982 isl_vec_free(sample
);
986 isl_basic_set_free(bset
);
987 isl_vertices_free(vertices
);
991 isl_ctx
*isl_vertex_get_ctx(__isl_keep isl_vertex
*vertex
)
993 return vertex
? isl_vertices_get_ctx(vertex
->vertices
) : NULL
;
996 int isl_vertex_get_id(__isl_keep isl_vertex
*vertex
)
998 return vertex
? vertex
->id
: -1;
1001 __isl_give isl_basic_set
*isl_basic_set_set_integral(__isl_take isl_basic_set
*bset
)
1006 if (!ISL_F_ISSET(bset
, ISL_BASIC_MAP_RATIONAL
))
1009 bset
= isl_basic_set_cow(bset
);
1013 ISL_F_CLR(bset
, ISL_BASIC_MAP_RATIONAL
);
1015 return isl_basic_set_finalize(bset
);
1018 /* Return the activity domain of the vertex "vertex".
1020 __isl_give isl_basic_set
*isl_vertex_get_domain(__isl_keep isl_vertex
*vertex
)
1022 struct isl_vertex
*v
;
1027 v
= &vertex
->vertices
->v
[vertex
->id
];
1029 v
->dom
= isl_basic_set_copy(v
->vertex
);
1030 v
->dom
= isl_basic_set_params(v
->dom
);
1031 v
->dom
= isl_basic_set_set_integral(v
->dom
);
1034 return isl_basic_set_copy(v
->dom
);
1037 /* Return a multiple quasi-affine expression describing the vertex "vertex"
1038 * in terms of the parameters,
1040 __isl_give isl_multi_aff
*isl_vertex_get_expr(__isl_keep isl_vertex
*vertex
)
1042 struct isl_vertex
*v
;
1043 isl_basic_set
*bset
;
1048 v
= &vertex
->vertices
->v
[vertex
->id
];
1050 bset
= isl_basic_set_copy(v
->vertex
);
1051 return isl_multi_aff_from_basic_set_equalities(bset
);
1054 static __isl_give isl_vertex
*isl_vertex_alloc(__isl_take isl_vertices
*vertices
,
1063 ctx
= isl_vertices_get_ctx(vertices
);
1064 vertex
= isl_alloc_type(ctx
, isl_vertex
);
1068 vertex
->vertices
= vertices
;
1073 isl_vertices_free(vertices
);
1077 void isl_vertex_free(__isl_take isl_vertex
*vertex
)
1081 isl_vertices_free(vertex
->vertices
);
1085 isl_ctx
*isl_cell_get_ctx(__isl_keep isl_cell
*cell
)
1087 return cell
? cell
->dom
->ctx
: NULL
;
1090 __isl_give isl_basic_set
*isl_cell_get_domain(__isl_keep isl_cell
*cell
)
1092 return cell
? isl_basic_set_copy(cell
->dom
) : NULL
;
1095 static __isl_give isl_cell
*isl_cell_alloc(__isl_take isl_vertices
*vertices
,
1096 __isl_take isl_basic_set
*dom
, int id
)
1099 isl_cell
*cell
= NULL
;
1101 if (!vertices
|| !dom
)
1104 cell
= isl_calloc_type(dom
->ctx
, isl_cell
);
1108 cell
->n_vertices
= vertices
->c
[id
].n_vertices
;
1109 cell
->ids
= isl_alloc_array(dom
->ctx
, int, cell
->n_vertices
);
1110 if (cell
->n_vertices
&& !cell
->ids
)
1112 for (i
= 0; i
< cell
->n_vertices
; ++i
)
1113 cell
->ids
[i
] = vertices
->c
[id
].vertices
[i
];
1114 cell
->vertices
= vertices
;
1119 isl_cell_free(cell
);
1120 isl_vertices_free(vertices
);
1121 isl_basic_set_free(dom
);
1125 void isl_cell_free(__isl_take isl_cell
*cell
)
1130 isl_vertices_free(cell
->vertices
);
1132 isl_basic_set_free(cell
->dom
);
1136 /* Create a tableau of the cone obtained by first homogenizing the given
1137 * polytope and then making all inequalities strict by setting the
1138 * constant term to -1.
1140 static struct isl_tab
*tab_for_shifted_cone(__isl_keep isl_basic_set
*bset
)
1144 struct isl_tab
*tab
;
1148 tab
= isl_tab_alloc(bset
->ctx
, bset
->n_eq
+ bset
->n_ineq
+ 1,
1149 1 + isl_basic_set_total_dim(bset
), 0);
1152 tab
->rational
= ISL_F_ISSET(bset
, ISL_BASIC_SET_RATIONAL
);
1153 if (ISL_F_ISSET(bset
, ISL_BASIC_MAP_EMPTY
)) {
1154 if (isl_tab_mark_empty(tab
) < 0)
1159 c
= isl_vec_alloc(bset
->ctx
, 1 + 1 + isl_basic_set_total_dim(bset
));
1163 isl_int_set_si(c
->el
[0], 0);
1164 for (i
= 0; i
< bset
->n_eq
; ++i
) {
1165 isl_seq_cpy(c
->el
+ 1, bset
->eq
[i
], c
->size
- 1);
1166 if (isl_tab_add_eq(tab
, c
->el
) < 0)
1170 isl_int_set_si(c
->el
[0], -1);
1171 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
1172 isl_seq_cpy(c
->el
+ 1, bset
->ineq
[i
], c
->size
- 1);
1173 if (isl_tab_add_ineq(tab
, c
->el
) < 0)
1181 isl_seq_clr(c
->el
+ 1, c
->size
- 1);
1182 isl_int_set_si(c
->el
[1], 1);
1183 if (isl_tab_add_ineq(tab
, c
->el
) < 0)
1194 /* Compute an interior point of "bset" by selecting an interior
1195 * point in homogeneous space and projecting the point back down.
1197 static __isl_give isl_vec
*isl_basic_set_interior_point(
1198 __isl_keep isl_basic_set
*bset
)
1201 struct isl_tab
*tab
;
1203 tab
= tab_for_shifted_cone(bset
);
1204 vec
= isl_tab_get_sample_value(tab
);
1209 isl_seq_cpy(vec
->el
, vec
->el
+ 1, vec
->size
- 1);
1215 /* Call "fn" on all chambers of the parametric polytope with the shared
1216 * facets of neighboring chambers only appearing in one of the chambers.
1218 * We pick an interior point from one of the chambers and then make
1219 * all constraints that do not satisfy this point strict.
1220 * For constraints that saturate the interior point, the sign
1221 * of the first non-zero coefficient is used to determine which
1222 * of the two (internal) constraints should be tightened.
1224 int isl_vertices_foreach_disjoint_cell(__isl_keep isl_vertices
*vertices
,
1225 int (*fn
)(__isl_take isl_cell
*cell
, void *user
), void *user
)
1234 if (vertices
->n_chambers
== 0)
1237 if (vertices
->n_chambers
== 1) {
1238 isl_basic_set
*dom
= isl_basic_set_copy(vertices
->c
[0].dom
);
1239 dom
= isl_basic_set_set_integral(dom
);
1240 cell
= isl_cell_alloc(isl_vertices_copy(vertices
), dom
, 0);
1243 return fn(cell
, user
);
1246 vec
= isl_basic_set_interior_point(vertices
->c
[0].dom
);
1250 for (i
= 0; i
< vertices
->n_chambers
; ++i
) {
1252 isl_basic_set
*dom
= isl_basic_set_copy(vertices
->c
[i
].dom
);
1254 dom
= isl_basic_set_tighten_outward(dom
, vec
);
1255 dom
= isl_basic_set_set_integral(dom
);
1256 cell
= isl_cell_alloc(isl_vertices_copy(vertices
), dom
, i
);
1272 isl_stat
isl_vertices_foreach_cell(__isl_keep isl_vertices
*vertices
,
1273 isl_stat (*fn
)(__isl_take isl_cell
*cell
, void *user
), void *user
)
1279 return isl_stat_error
;
1281 if (vertices
->n_chambers
== 0)
1284 for (i
= 0; i
< vertices
->n_chambers
; ++i
) {
1286 isl_basic_set
*dom
= isl_basic_set_copy(vertices
->c
[i
].dom
);
1288 cell
= isl_cell_alloc(isl_vertices_copy(vertices
), dom
, i
);
1290 return isl_stat_error
;
1294 return isl_stat_error
;
1300 isl_stat
isl_vertices_foreach_vertex(__isl_keep isl_vertices
*vertices
,
1301 isl_stat (*fn
)(__isl_take isl_vertex
*vertex
, void *user
), void *user
)
1307 return isl_stat_error
;
1309 if (vertices
->n_vertices
== 0)
1312 for (i
= 0; i
< vertices
->n_vertices
; ++i
) {
1315 vertex
= isl_vertex_alloc(isl_vertices_copy(vertices
), i
);
1317 return isl_stat_error
;
1319 r
= fn(vertex
, user
);
1321 return isl_stat_error
;
1327 isl_stat
isl_cell_foreach_vertex(__isl_keep isl_cell
*cell
,
1328 isl_stat (*fn
)(__isl_take isl_vertex
*vertex
, void *user
), void *user
)
1334 return isl_stat_error
;
1336 if (cell
->n_vertices
== 0)
1339 for (i
= 0; i
< cell
->n_vertices
; ++i
) {
1342 vertex
= isl_vertex_alloc(isl_vertices_copy(cell
->vertices
),
1345 return isl_stat_error
;
1347 r
= fn(vertex
, user
);
1349 return isl_stat_error
;
1355 isl_ctx
*isl_vertices_get_ctx(__isl_keep isl_vertices
*vertices
)
1357 return vertices
? vertices
->bset
->ctx
: NULL
;
1360 int isl_vertices_get_n_vertices(__isl_keep isl_vertices
*vertices
)
1362 return vertices
? vertices
->n_vertices
: -1;
1365 __isl_give isl_vertices
*isl_morph_vertices(__isl_take isl_morph
*morph
,
1366 __isl_take isl_vertices
*vertices
)
1369 isl_morph
*param_morph
= NULL
;
1371 if (!morph
|| !vertices
)
1374 isl_assert(vertices
->bset
->ctx
, vertices
->ref
== 1, goto error
);
1376 param_morph
= isl_morph_copy(morph
);
1377 param_morph
= isl_morph_dom_params(param_morph
);
1378 param_morph
= isl_morph_ran_params(param_morph
);
1380 for (i
= 0; i
< vertices
->n_vertices
; ++i
) {
1381 vertices
->v
[i
].dom
= isl_morph_basic_set(
1382 isl_morph_copy(param_morph
), vertices
->v
[i
].dom
);
1383 vertices
->v
[i
].vertex
= isl_morph_basic_set(
1384 isl_morph_copy(morph
), vertices
->v
[i
].vertex
);
1385 if (!vertices
->v
[i
].vertex
)
1389 for (i
= 0; i
< vertices
->n_chambers
; ++i
) {
1390 vertices
->c
[i
].dom
= isl_morph_basic_set(
1391 isl_morph_copy(param_morph
), vertices
->c
[i
].dom
);
1392 if (!vertices
->c
[i
].dom
)
1396 isl_morph_free(param_morph
);
1397 isl_morph_free(morph
);
1400 isl_morph_free(param_morph
);
1401 isl_morph_free(morph
);
1402 isl_vertices_free(vertices
);
1406 /* Construct a simplex isl_cell spanned by the vertices with indices in
1407 * "simplex_ids" and "other_ids" and call "fn" on this isl_cell.
1409 static int call_on_simplex(__isl_keep isl_cell
*cell
,
1410 int *simplex_ids
, int n_simplex
, int *other_ids
, int n_other
,
1411 int (*fn
)(__isl_take isl_cell
*simplex
, void *user
), void *user
)
1415 struct isl_cell
*simplex
;
1417 ctx
= isl_cell_get_ctx(cell
);
1419 simplex
= isl_calloc_type(ctx
, struct isl_cell
);
1422 simplex
->vertices
= isl_vertices_copy(cell
->vertices
);
1423 if (!simplex
->vertices
)
1425 simplex
->dom
= isl_basic_set_copy(cell
->dom
);
1428 simplex
->n_vertices
= n_simplex
+ n_other
;
1429 simplex
->ids
= isl_alloc_array(ctx
, int, simplex
->n_vertices
);
1433 for (i
= 0; i
< n_simplex
; ++i
)
1434 simplex
->ids
[i
] = simplex_ids
[i
];
1435 for (i
= 0; i
< n_other
; ++i
)
1436 simplex
->ids
[n_simplex
+ i
] = other_ids
[i
];
1438 return fn(simplex
, user
);
1440 isl_cell_free(simplex
);
1444 /* Check whether the parametric vertex described by "vertex"
1445 * lies on the facet corresponding to constraint "facet" of "bset".
1446 * The isl_vec "v" is a temporary vector than can be used by this function.
1448 * We eliminate the variables from the facet constraint using the
1449 * equalities defining the vertex and check if the result is identical
1452 * It would probably be better to keep track of the constraints defining
1453 * a vertex during the vertex construction so that we could simply look
1456 static int vertex_on_facet(__isl_keep isl_basic_set
*vertex
,
1457 __isl_keep isl_basic_set
*bset
, int facet
, __isl_keep isl_vec
*v
)
1462 isl_seq_cpy(v
->el
, bset
->ineq
[facet
], v
->size
);
1465 for (i
= 0; i
< vertex
->n_eq
; ++i
) {
1466 int k
= isl_seq_last_non_zero(vertex
->eq
[i
], v
->size
);
1467 isl_seq_elim(v
->el
, vertex
->eq
[i
], k
, v
->size
, &m
);
1471 return isl_seq_first_non_zero(v
->el
, v
->size
) == -1;
1474 /* Triangulate the polytope spanned by the vertices with ids
1475 * in "simplex_ids" and "other_ids" and call "fn" on each of
1476 * the resulting simplices.
1477 * If the input polytope is already a simplex, we simply call "fn".
1478 * Otherwise, we pick a point from "other_ids" and add it to "simplex_ids".
1479 * Then we consider each facet of "bset" that does not contain the point
1480 * we just picked, but does contain some of the other points in "other_ids"
1481 * and call ourselves recursively on the polytope spanned by the new
1482 * "simplex_ids" and those points in "other_ids" that lie on the facet.
1484 static int triangulate(__isl_keep isl_cell
*cell
, __isl_keep isl_vec
*v
,
1485 int *simplex_ids
, int n_simplex
, int *other_ids
, int n_other
,
1486 int (*fn
)(__isl_take isl_cell
*simplex
, void *user
), void *user
)
1492 isl_basic_set
*vertex
;
1493 isl_basic_set
*bset
;
1495 ctx
= isl_cell_get_ctx(cell
);
1496 d
= isl_basic_set_dim(cell
->vertices
->bset
, isl_dim_set
);
1497 nparam
= isl_basic_set_dim(cell
->vertices
->bset
, isl_dim_param
);
1499 if (n_simplex
+ n_other
== d
+ 1)
1500 return call_on_simplex(cell
, simplex_ids
, n_simplex
,
1501 other_ids
, n_other
, fn
, user
);
1503 simplex_ids
[n_simplex
] = other_ids
[0];
1504 vertex
= cell
->vertices
->v
[other_ids
[0]].vertex
;
1505 bset
= cell
->vertices
->bset
;
1507 ids
= isl_alloc_array(ctx
, int, n_other
- 1);
1508 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
1509 if (isl_seq_first_non_zero(bset
->ineq
[i
] + 1 + nparam
, d
) == -1)
1511 if (vertex_on_facet(vertex
, bset
, i
, v
))
1514 for (j
= 1, k
= 0; j
< n_other
; ++j
) {
1516 ov
= cell
->vertices
->v
[other_ids
[j
]].vertex
;
1517 if (vertex_on_facet(ov
, bset
, i
, v
))
1518 ids
[k
++] = other_ids
[j
];
1523 if (triangulate(cell
, v
, simplex_ids
, n_simplex
+ 1,
1524 ids
, k
, fn
, user
) < 0)
1535 /* Triangulate the given cell and call "fn" on each of the resulting
1538 int isl_cell_foreach_simplex(__isl_take isl_cell
*cell
,
1539 int (*fn
)(__isl_take isl_cell
*simplex
, void *user
), void *user
)
1545 int *simplex_ids
= NULL
;
1550 d
= isl_basic_set_dim(cell
->vertices
->bset
, isl_dim_set
);
1551 total
= isl_basic_set_total_dim(cell
->vertices
->bset
);
1553 if (cell
->n_vertices
== d
+ 1)
1554 return fn(cell
, user
);
1556 ctx
= isl_cell_get_ctx(cell
);
1557 simplex_ids
= isl_alloc_array(ctx
, int, d
+ 1);
1561 v
= isl_vec_alloc(ctx
, 1 + total
);
1565 r
= triangulate(cell
, v
, simplex_ids
, 0,
1566 cell
->ids
, cell
->n_vertices
, fn
, user
);
1571 isl_cell_free(cell
);
1577 isl_cell_free(cell
);