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 __isl_null isl_vertices
*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
);
66 struct isl_vertex_list
{
68 struct isl_vertex_list
*next
;
71 static struct isl_vertex_list
*free_vertex_list(struct isl_vertex_list
*list
)
73 struct isl_vertex_list
*next
;
75 for (; list
; list
= next
) {
77 isl_basic_set_free(list
->v
.vertex
);
78 isl_basic_set_free(list
->v
.dom
);
85 static __isl_give isl_vertices
*vertices_from_list(__isl_keep isl_basic_set
*bset
,
86 int n_vertices
, struct isl_vertex_list
*list
)
89 struct isl_vertex_list
*next
;
90 isl_vertices
*vertices
;
92 vertices
= isl_calloc_type(bset
->ctx
, isl_vertices
);
96 vertices
->bset
= isl_basic_set_copy(bset
);
97 vertices
->v
= isl_alloc_array(bset
->ctx
, struct isl_vertex
, n_vertices
);
98 if (n_vertices
&& !vertices
->v
)
100 vertices
->n_vertices
= n_vertices
;
102 for (i
= 0; list
; list
= next
, i
++) {
104 vertices
->v
[i
] = list
->v
;
110 isl_vertices_free(vertices
);
111 free_vertex_list(list
);
115 /* Prepend a vertex to the linked list "list" based on the equalities in "tab".
116 * Return isl_bool_true if the vertex was actually added and
117 * isl_bool_false otherwise.
118 * In particular, vertices with a lower-dimensional activity domain are
119 * not added to the list because they would not be included in any chamber.
120 * Return isl_bool_error on error.
122 static isl_bool
add_vertex(struct isl_vertex_list
**list
,
123 __isl_keep isl_basic_set
*bset
, struct isl_tab
*tab
)
126 struct isl_vertex_list
*v
= NULL
;
128 if (isl_tab_detect_implicit_equalities(tab
) < 0)
129 return isl_bool_error
;
131 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
133 return isl_bool_error
;
135 v
= isl_calloc_type(tab
->mat
->ctx
, struct isl_vertex_list
);
139 v
->v
.vertex
= isl_basic_set_copy(bset
);
140 v
->v
.vertex
= isl_basic_set_cow(v
->v
.vertex
);
141 v
->v
.vertex
= isl_basic_set_update_from_tab(v
->v
.vertex
, tab
);
142 v
->v
.vertex
= isl_basic_set_simplify(v
->v
.vertex
);
143 v
->v
.vertex
= isl_basic_set_finalize(v
->v
.vertex
);
146 isl_assert(bset
->ctx
, v
->v
.vertex
->n_eq
>= nvar
, goto error
);
147 v
->v
.dom
= isl_basic_set_copy(v
->v
.vertex
);
148 v
->v
.dom
= isl_basic_set_params(v
->v
.dom
);
152 if (v
->v
.dom
->n_eq
> 0) {
154 return isl_bool_false
;
160 return isl_bool_true
;
163 return isl_bool_error
;
166 /* Compute the parametric vertices and the chamber decomposition
167 * of an empty parametric polytope.
169 static __isl_give isl_vertices
*vertices_empty(__isl_keep isl_basic_set
*bset
)
171 isl_vertices
*vertices
;
176 vertices
= isl_calloc_type(bset
->ctx
, isl_vertices
);
179 vertices
->bset
= isl_basic_set_copy(bset
);
182 vertices
->n_vertices
= 0;
183 vertices
->n_chambers
= 0;
188 /* Compute the parametric vertices and the chamber decomposition
189 * of the parametric polytope defined using the same constraints
190 * as "bset" in the 0D case.
191 * There is exactly one 0D vertex and a single chamber containing
194 static __isl_give isl_vertices
*vertices_0D(__isl_keep isl_basic_set
*bset
)
196 isl_vertices
*vertices
;
201 vertices
= isl_calloc_type(bset
->ctx
, isl_vertices
);
205 vertices
->bset
= isl_basic_set_copy(bset
);
207 vertices
->v
= isl_calloc_array(bset
->ctx
, struct isl_vertex
, 1);
210 vertices
->n_vertices
= 1;
211 vertices
->v
[0].vertex
= isl_basic_set_copy(bset
);
212 vertices
->v
[0].dom
= isl_basic_set_params(isl_basic_set_copy(bset
));
213 if (!vertices
->v
[0].vertex
|| !vertices
->v
[0].dom
)
216 vertices
->c
= isl_calloc_array(bset
->ctx
, struct isl_chamber
, 1);
219 vertices
->n_chambers
= 1;
220 vertices
->c
[0].n_vertices
= 1;
221 vertices
->c
[0].vertices
= isl_calloc_array(bset
->ctx
, int, 1);
222 if (!vertices
->c
[0].vertices
)
224 vertices
->c
[0].dom
= isl_basic_set_copy(vertices
->v
[0].dom
);
225 if (!vertices
->c
[0].dom
)
230 isl_vertices_free(vertices
);
234 /* Is the row pointed to by "f" linearly independent of the "n" first
237 static isl_bool
is_independent(__isl_keep isl_mat
*facets
, int n
, isl_int
*f
)
241 if (isl_seq_first_non_zero(f
, facets
->n_col
) < 0)
242 return isl_bool_false
;
244 isl_seq_cpy(facets
->row
[n
], f
, facets
->n_col
);
245 facets
->n_row
= n
+ 1;
246 rank
= isl_mat_rank(facets
);
248 return isl_bool_error
;
250 return isl_bool_ok(rank
== n
+ 1);
253 /* Check whether we can select constraint "level", given the current selection
254 * reflected by facets in "tab", the rows of "facets" and the earlier
255 * "selected" elements of "selection".
257 * If the constraint is (strictly) redundant in the tableau, selecting it would
258 * result in an empty tableau, so it can't be selected.
259 * If the set variable part of the constraint is not linearly independent
260 * of the set variable parts of the already selected constraints,
261 * the constraint cannot be selected.
262 * If selecting the constraint results in an empty tableau, the constraint
263 * cannot be selected.
264 * Finally, if selecting the constraint results in some explicitly
265 * deselected constraints turning into equalities, then the corresponding
266 * vertices have already been generated, so the constraint cannot be selected.
268 static isl_bool
can_select(__isl_keep isl_basic_set
*bset
, int level
,
269 struct isl_tab
*tab
, __isl_keep isl_mat
*facets
, int selected
,
275 struct isl_tab_undo
*snap
;
277 if (isl_tab_is_redundant(tab
, level
))
278 return isl_bool_false
;
280 ovar
= isl_space_offset(bset
->dim
, isl_dim_set
);
282 indep
= is_independent(facets
, selected
, bset
->ineq
[level
] + 1 + ovar
);
283 if (indep
< 0 || !indep
)
286 snap
= isl_tab_snap(tab
);
287 if (isl_tab_select_facet(tab
, level
) < 0)
288 return isl_bool_error
;
291 if (isl_tab_rollback(tab
, snap
) < 0)
292 return isl_bool_error
;
293 return isl_bool_false
;
296 for (i
= 0; i
< level
; ++i
) {
299 if (selection
[i
] != DESELECTED
)
302 if (isl_tab_is_equality(tab
, i
))
304 else if (isl_tab_is_redundant(tab
, i
))
307 sgn
= isl_tab_sign_of_max(tab
, i
);
309 return isl_bool_error
;
311 if (isl_tab_rollback(tab
, snap
) < 0)
312 return isl_bool_error
;
313 return isl_bool_false
;
317 return isl_bool_true
;
320 /* Compute the parametric vertices and the chamber decomposition
321 * of a parametric polytope that is not full-dimensional.
323 * Simply map the parametric polytope to a lower dimensional space
324 * and map the resulting vertices back.
326 static __isl_give isl_vertices
*lower_dim_vertices(
327 __isl_take isl_basic_set
*bset
)
330 isl_vertices
*vertices
;
332 morph
= isl_basic_set_full_compression(bset
);
333 bset
= isl_morph_basic_set(isl_morph_copy(morph
), bset
);
335 vertices
= isl_basic_set_compute_vertices(bset
);
336 isl_basic_set_free(bset
);
338 morph
= isl_morph_inverse(morph
);
340 vertices
= isl_morph_vertices(morph
, vertices
);
345 /* Compute the parametric vertices and the chamber decomposition
346 * of a parametric polytope "bset" that is not full-dimensional.
347 * Additionally, free both "copy" and "tab".
349 static __isl_give isl_vertices
*lower_dim_vertices_free(
350 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*copy
,
353 isl_basic_set_free(copy
);
355 return lower_dim_vertices(bset
);
358 /* Detect implicit equality constraints in "bset" using the tableau
359 * representation "tab".
360 * Return a copy of "bset" with the implicit equality constraints
361 * made explicit, leaving the original "bset" unmodified.
363 static __isl_give isl_basic_set
*detect_implicit_equality_constraints(
364 __isl_keep isl_basic_set
*bset
, struct isl_tab
*tab
)
366 if (isl_tab_detect_implicit_equalities(tab
) < 0)
369 bset
= isl_basic_set_copy(bset
);
370 bset
= isl_basic_set_cow(bset
);
371 bset
= isl_basic_set_update_from_tab(bset
, tab
);
376 /* Compute the parametric vertices and the chamber decomposition
377 * of the parametric polytope defined using the same constraints
378 * as "bset". "bset" is assumed to have no existentially quantified
381 * The vertices themselves are computed in a fairly simplistic way.
382 * We simply run through all combinations of d constraints,
383 * with d the number of set variables, and check if those d constraints
384 * define a vertex. To avoid the generation of duplicate vertices,
385 * which may happen if a vertex is defined by more than d constraints,
386 * we make sure we only generate the vertex for the d constraints with
389 * Only potential vertices with a full-dimensional activity domain
390 * are considered. However, if the input has (implicit) equality
391 * constraints among the parameters, then activity domain
392 * should be considered full-dimensional if it does not satisfy
393 * any extra equality constraints beyond those of the input.
394 * The implicit equality constraints of the input are therefore first detected.
395 * If there are any, then the input is mapped to a lower dimensional space
396 * such that the check for full-dimensional activity domains
397 * can be performed with respect to a full-dimensional space.
398 * Note that it is important to leave "bset" unmodified while detecting
399 * equality constraints since the inequality constraints of "bset"
400 * are assumed to correspond to those of the tableau.
402 * We set up a tableau and keep track of which facets have been
403 * selected. The tableau is marked strict_redundant so that we can be
404 * sure that any constraint that is marked redundant (and that is not
405 * also marked zero) is not an equality.
406 * If a constraint is marked DESELECTED, it means the constraint was
407 * SELECTED before (in combination with the same selection of earlier
408 * constraints). If such a deselected constraint turns out to be an
409 * equality, then any vertex that may still be found with the current
410 * selection has already been generated when the constraint was selected.
411 * A constraint is marked UNSELECTED when there is no way selecting
412 * the constraint could lead to a vertex (in combination with the current
413 * selection of earlier constraints).
415 * The set variable coefficients of the selected constraints are stored
416 * in the facets matrix.
418 __isl_give isl_vertices
*isl_basic_set_compute_vertices(
419 __isl_keep isl_basic_set
*bset
)
426 int *selection
= NULL
;
428 struct isl_tab_undo
**snap
= NULL
;
429 isl_mat
*facets
= NULL
;
430 struct isl_vertex_list
*list
= NULL
;
432 isl_vertices
*vertices
;
439 if (isl_basic_set_plain_is_empty(bset
))
440 return vertices_empty(bset
);
443 return lower_dim_vertices(isl_basic_set_copy(bset
));
445 if (isl_basic_set_check_no_locals(bset
) < 0)
448 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
452 return vertices_0D(bset
);
454 copy
= isl_basic_set_copy(bset
);
455 copy
= isl_basic_set_set_rational(copy
);
459 tab
= isl_tab_from_basic_set(copy
, 0);
462 tab
->strict_redundant
= 1;
465 vertices
= vertices_empty(copy
);
466 isl_basic_set_free(copy
);
471 test
= detect_implicit_equality_constraints(bset
, tab
);
472 n_eq
= isl_basic_set_n_equality(test
);
474 test
= isl_basic_set_free(test
);
475 if (n_eq
< 0 || n_eq
> 0)
476 return lower_dim_vertices_free(test
, copy
, tab
);
477 isl_basic_set_free(test
);
479 selection
= isl_alloc_array(copy
->ctx
, int, copy
->n_ineq
);
480 snap
= isl_alloc_array(copy
->ctx
, struct isl_tab_undo
*, copy
->n_ineq
);
481 facets
= isl_mat_alloc(copy
->ctx
, nvar
, nvar
);
482 if ((copy
->n_ineq
&& (!selection
|| !snap
)) || !facets
)
490 if (level
>= copy
->n_ineq
||
491 (!init
&& selection
[level
] != SELECTED
)) {
498 snap
[level
] = isl_tab_snap(tab
);
499 ok
= can_select(copy
, level
, tab
, facets
, selected
,
504 selection
[level
] = SELECTED
;
507 selection
[level
] = UNSELECTED
;
509 selection
[level
] = DESELECTED
;
511 if (isl_tab_rollback(tab
, snap
[level
]) < 0)
514 if (selected
== nvar
) {
515 if (tab
->n_dead
== nvar
) {
516 isl_bool added
= add_vertex(&list
, copy
, tab
);
529 isl_mat_free(facets
);
535 vertices
= vertices_from_list(copy
, n_vertices
, list
);
537 vertices
= compute_chambers(copy
, vertices
);
541 free_vertex_list(list
);
542 isl_mat_free(facets
);
546 isl_basic_set_free(copy
);
550 struct isl_chamber_list
{
551 struct isl_chamber c
;
552 struct isl_chamber_list
*next
;
555 static void free_chamber_list(struct isl_chamber_list
*list
)
557 struct isl_chamber_list
*next
;
559 for (; list
; list
= next
) {
561 isl_basic_set_free(list
->c
.dom
);
562 free(list
->c
.vertices
);
567 /* Check whether the basic set "bset" is a superset of the basic set described
568 * by "tab", i.e., check whether all constraints of "bset" are redundant.
570 static isl_bool
bset_covers_tab(__isl_keep isl_basic_set
*bset
,
576 return isl_bool_error
;
578 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
579 enum isl_ineq_type type
= isl_tab_ineq_type(tab
, bset
->ineq
[i
]);
581 case isl_ineq_error
: return isl_bool_error
;
582 case isl_ineq_redundant
: continue;
583 default: return isl_bool_false
;
587 return isl_bool_true
;
590 static __isl_give isl_vertices
*vertices_add_chambers(
591 __isl_take isl_vertices
*vertices
, int n_chambers
,
592 struct isl_chamber_list
*list
)
596 struct isl_chamber_list
*next
;
598 ctx
= isl_vertices_get_ctx(vertices
);
599 vertices
->c
= isl_alloc_array(ctx
, struct isl_chamber
, n_chambers
);
602 vertices
->n_chambers
= n_chambers
;
604 for (i
= 0; list
; list
= next
, i
++) {
606 vertices
->c
[i
] = list
->c
;
612 isl_vertices_free(vertices
);
613 free_chamber_list(list
);
617 /* Can "tab" be intersected with "bset" without resulting in
618 * a lower-dimensional set.
619 * "bset" itself is assumed to be full-dimensional.
621 static isl_bool
can_intersect(struct isl_tab
*tab
,
622 __isl_keep isl_basic_set
*bset
)
625 struct isl_tab_undo
*snap
;
628 isl_die(isl_basic_set_get_ctx(bset
), isl_error_internal
,
629 "expecting full-dimensional input",
630 return isl_bool_error
);
632 if (isl_tab_extend_cons(tab
, bset
->n_ineq
) < 0)
633 return isl_bool_error
;
635 snap
= isl_tab_snap(tab
);
637 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
638 enum isl_ineq_type type
;
640 type
= isl_tab_ineq_type(tab
, bset
->ineq
[i
]);
642 return isl_bool_error
;
643 if (type
== isl_ineq_redundant
)
645 if (isl_tab_add_ineq(tab
, bset
->ineq
[i
]) < 0)
646 return isl_bool_error
;
649 if (isl_tab_detect_implicit_equalities(tab
) < 0)
650 return isl_bool_error
;
652 if (isl_tab_rollback(tab
, snap
) < 0)
653 return isl_bool_error
;
654 return isl_bool_false
;
657 return isl_bool_true
;
660 static int add_chamber(struct isl_chamber_list
**list
,
661 __isl_keep isl_vertices
*vertices
, struct isl_tab
*tab
, int *selection
)
666 struct isl_tab_undo
*snap
;
667 struct isl_chamber_list
*c
= NULL
;
669 for (i
= 0; i
< vertices
->n_vertices
; ++i
)
673 snap
= isl_tab_snap(tab
);
675 for (i
= 0; i
< tab
->n_con
&& tab
->con
[i
].frozen
; ++i
)
676 tab
->con
[i
].frozen
= 0;
679 if (isl_tab_detect_redundant(tab
) < 0)
682 c
= isl_calloc_type(tab
->mat
->ctx
, struct isl_chamber_list
);
685 c
->c
.vertices
= isl_alloc_array(tab
->mat
->ctx
, int, n_vertices
);
686 if (n_vertices
&& !c
->c
.vertices
)
688 c
->c
.dom
= isl_basic_set_copy(isl_tab_peek_bset(tab
));
689 c
->c
.dom
= isl_basic_set_set_rational(c
->c
.dom
);
690 c
->c
.dom
= isl_basic_set_cow(c
->c
.dom
);
691 c
->c
.dom
= isl_basic_set_update_from_tab(c
->c
.dom
, tab
);
692 c
->c
.dom
= isl_basic_set_simplify(c
->c
.dom
);
693 c
->c
.dom
= isl_basic_set_finalize(c
->c
.dom
);
697 c
->c
.n_vertices
= n_vertices
;
699 for (i
= 0, j
= 0; i
< vertices
->n_vertices
; ++i
)
701 c
->c
.vertices
[j
] = i
;
708 for (i
= 0; i
< n_frozen
; ++i
)
709 tab
->con
[i
].frozen
= 1;
711 if (isl_tab_rollback(tab
, snap
) < 0)
716 free_chamber_list(c
);
720 struct isl_facet_todo
{
721 struct isl_tab
*tab
; /* A tableau representation of the facet */
722 isl_basic_set
*bset
; /* A normalized basic set representation */
723 isl_vec
*constraint
; /* Constraint pointing to the other side */
724 struct isl_facet_todo
*next
;
727 static void free_todo(struct isl_facet_todo
*todo
)
730 struct isl_facet_todo
*next
= todo
->next
;
732 isl_tab_free(todo
->tab
);
733 isl_basic_set_free(todo
->bset
);
734 isl_vec_free(todo
->constraint
);
741 static struct isl_facet_todo
*create_todo(struct isl_tab
*tab
, int con
)
745 struct isl_tab_undo
*snap
;
746 struct isl_facet_todo
*todo
;
748 snap
= isl_tab_snap(tab
);
750 for (i
= 0; i
< tab
->n_con
&& tab
->con
[i
].frozen
; ++i
)
751 tab
->con
[i
].frozen
= 0;
754 if (isl_tab_detect_redundant(tab
) < 0)
757 todo
= isl_calloc_type(tab
->mat
->ctx
, struct isl_facet_todo
);
761 todo
->constraint
= isl_vec_alloc(tab
->mat
->ctx
, 1 + tab
->n_var
);
762 if (!todo
->constraint
)
764 isl_seq_neg(todo
->constraint
->el
, tab
->bmap
->ineq
[con
], 1 + tab
->n_var
);
765 todo
->bset
= isl_basic_set_copy(isl_tab_peek_bset(tab
));
766 todo
->bset
= isl_basic_set_set_rational(todo
->bset
);
767 todo
->bset
= isl_basic_set_cow(todo
->bset
);
768 todo
->bset
= isl_basic_set_update_from_tab(todo
->bset
, tab
);
769 todo
->bset
= isl_basic_set_simplify(todo
->bset
);
770 todo
->bset
= isl_basic_set_sort_constraints(todo
->bset
);
773 ISL_F_SET(todo
->bset
, ISL_BASIC_SET_NO_REDUNDANT
);
774 todo
->tab
= isl_tab_dup(tab
);
778 for (i
= 0; i
< n_frozen
; ++i
)
779 tab
->con
[i
].frozen
= 1;
781 if (isl_tab_rollback(tab
, snap
) < 0)
790 /* Create todo items for all interior facets of the chamber represented
791 * by "tab" and collect them in "next".
793 static int init_todo(struct isl_facet_todo
**next
, struct isl_tab
*tab
)
796 struct isl_tab_undo
*snap
;
797 struct isl_facet_todo
*todo
;
799 snap
= isl_tab_snap(tab
);
801 for (i
= 0; i
< tab
->n_con
; ++i
) {
802 if (tab
->con
[i
].frozen
)
804 if (tab
->con
[i
].is_redundant
)
807 if (isl_tab_select_facet(tab
, i
) < 0)
810 todo
= create_todo(tab
, i
);
817 if (isl_tab_rollback(tab
, snap
) < 0)
824 /* Does the linked list contain a todo item that is the opposite of "todo".
825 * If so, return 1 and remove the opposite todo item.
827 static int has_opposite(struct isl_facet_todo
*todo
,
828 struct isl_facet_todo
**list
)
830 for (; *list
; list
= &(*list
)->next
) {
832 eq
= isl_basic_set_plain_is_equal(todo
->bset
, (*list
)->bset
);
847 /* Create todo items for all interior facets of the chamber represented
848 * by "tab" and collect them in first->next, taking care to cancel
849 * opposite todo items.
851 static int update_todo(struct isl_facet_todo
*first
, struct isl_tab
*tab
)
854 struct isl_tab_undo
*snap
;
855 struct isl_facet_todo
*todo
;
857 snap
= isl_tab_snap(tab
);
859 for (i
= 0; i
< tab
->n_con
; ++i
) {
862 if (tab
->con
[i
].frozen
)
864 if (tab
->con
[i
].is_redundant
)
867 if (isl_tab_select_facet(tab
, i
) < 0)
870 todo
= create_todo(tab
, i
);
874 drop
= has_opposite(todo
, &first
->next
);
881 todo
->next
= first
->next
;
885 if (isl_tab_rollback(tab
, snap
) < 0)
892 /* Compute the chamber decomposition of the parametric polytope respresented
893 * by "bset" given the parametric vertices and their activity domains.
895 * We are only interested in full-dimensional chambers.
896 * Each of these chambers is the intersection of the activity domains of
897 * one or more vertices and the union of all chambers is equal to the
898 * projection of the entire parametric polytope onto the parameter space.
900 * We first create an initial chamber by intersecting as many activity
901 * domains as possible without ending up with an empty or lower-dimensional
902 * set. As a minor optimization, we only consider those activity domains
903 * that contain some arbitrary point.
905 * For each of the interior facets of the chamber, we construct a todo item,
906 * containing the facet and a constraint containing the other side of the facet,
907 * for constructing the chamber on the other side.
908 * While their are any todo items left, we pick a todo item and
909 * create the required chamber by intersecting all activity domains
910 * that contain the facet and have a full-dimensional intersection with
911 * the other side of the facet. For each of the interior facets, we
912 * again create todo items, taking care to cancel opposite todo items.
914 static __isl_give isl_vertices
*compute_chambers(__isl_take isl_basic_set
*bset
,
915 __isl_take isl_vertices
*vertices
)
920 isl_vec
*sample
= NULL
;
921 struct isl_tab
*tab
= NULL
;
922 struct isl_tab_undo
*snap
;
923 int *selection
= NULL
;
925 struct isl_chamber_list
*list
= NULL
;
926 struct isl_facet_todo
*todo
= NULL
;
928 if (!bset
|| !vertices
)
931 ctx
= isl_vertices_get_ctx(vertices
);
932 selection
= isl_alloc_array(ctx
, int, vertices
->n_vertices
);
933 if (vertices
->n_vertices
&& !selection
)
936 bset
= isl_basic_set_params(bset
);
937 n_eq
= isl_basic_set_n_equality(bset
);
941 isl_die(isl_basic_set_get_ctx(bset
), isl_error_internal
,
942 "expecting full-dimensional input", goto error
);
944 tab
= isl_tab_from_basic_set(bset
, 1);
947 for (i
= 0; i
< bset
->n_ineq
; ++i
)
948 if (isl_tab_freeze_constraint(tab
, i
) < 0)
950 isl_basic_set_free(bset
);
952 snap
= isl_tab_snap(tab
);
954 sample
= isl_tab_get_sample_value(tab
);
956 for (i
= 0; i
< vertices
->n_vertices
; ++i
) {
957 selection
[i
] = isl_basic_set_contains(vertices
->v
[i
].dom
, sample
);
958 if (selection
[i
] < 0)
962 selection
[i
] = can_intersect(tab
, vertices
->v
[i
].dom
);
963 if (selection
[i
] < 0)
967 if (isl_tab_detect_redundant(tab
) < 0)
970 if (add_chamber(&list
, vertices
, tab
, selection
) < 0)
974 if (init_todo(&todo
, tab
) < 0)
978 struct isl_facet_todo
*next
;
980 if (isl_tab_rollback(tab
, snap
) < 0)
983 if (isl_tab_add_ineq(tab
, todo
->constraint
->el
) < 0)
985 if (isl_tab_freeze_constraint(tab
, tab
->n_con
- 1) < 0)
988 for (i
= 0; i
< vertices
->n_vertices
; ++i
) {
989 selection
[i
] = bset_covers_tab(vertices
->v
[i
].dom
,
991 if (selection
[i
] < 0)
995 selection
[i
] = can_intersect(tab
, vertices
->v
[i
].dom
);
996 if (selection
[i
] < 0)
1000 if (isl_tab_detect_redundant(tab
) < 0)
1003 if (add_chamber(&list
, vertices
, tab
, selection
) < 0)
1007 if (update_todo(todo
, tab
) < 0)
1016 isl_vec_free(sample
);
1021 vertices
= vertices_add_chambers(vertices
, n_chambers
, list
);
1023 for (i
= 0; vertices
&& i
< vertices
->n_vertices
; ++i
) {
1024 isl_basic_set_free(vertices
->v
[i
].dom
);
1025 vertices
->v
[i
].dom
= NULL
;
1030 free_chamber_list(list
);
1032 isl_vec_free(sample
);
1036 isl_basic_set_free(bset
);
1037 isl_vertices_free(vertices
);
1041 isl_ctx
*isl_vertex_get_ctx(__isl_keep isl_vertex
*vertex
)
1043 return vertex
? isl_vertices_get_ctx(vertex
->vertices
) : NULL
;
1046 isl_size
isl_vertex_get_id(__isl_keep isl_vertex
*vertex
)
1048 return vertex
? vertex
->id
: isl_size_error
;
1051 /* Return the activity domain of the vertex "vertex".
1053 __isl_give isl_basic_set
*isl_vertex_get_domain(__isl_keep isl_vertex
*vertex
)
1055 struct isl_vertex
*v
;
1060 v
= &vertex
->vertices
->v
[vertex
->id
];
1062 v
->dom
= isl_basic_set_copy(v
->vertex
);
1063 v
->dom
= isl_basic_set_params(v
->dom
);
1064 v
->dom
= isl_basic_set_set_integral(v
->dom
);
1067 return isl_basic_set_copy(v
->dom
);
1070 /* Return a multiple quasi-affine expression describing the vertex "vertex"
1071 * in terms of the parameters,
1073 __isl_give isl_multi_aff
*isl_vertex_get_expr(__isl_keep isl_vertex
*vertex
)
1075 struct isl_vertex
*v
;
1076 isl_basic_set
*bset
;
1081 v
= &vertex
->vertices
->v
[vertex
->id
];
1083 bset
= isl_basic_set_copy(v
->vertex
);
1084 return isl_multi_aff_from_basic_set_equalities(bset
);
1087 static __isl_give isl_vertex
*isl_vertex_alloc(__isl_take isl_vertices
*vertices
,
1096 ctx
= isl_vertices_get_ctx(vertices
);
1097 vertex
= isl_alloc_type(ctx
, isl_vertex
);
1101 vertex
->vertices
= vertices
;
1106 isl_vertices_free(vertices
);
1110 __isl_null isl_vertex
*isl_vertex_free(__isl_take isl_vertex
*vertex
)
1114 isl_vertices_free(vertex
->vertices
);
1120 isl_ctx
*isl_cell_get_ctx(__isl_keep isl_cell
*cell
)
1122 return cell
? cell
->dom
->ctx
: NULL
;
1125 __isl_give isl_basic_set
*isl_cell_get_domain(__isl_keep isl_cell
*cell
)
1127 return cell
? isl_basic_set_copy(cell
->dom
) : NULL
;
1130 static __isl_give isl_cell
*isl_cell_alloc(__isl_take isl_vertices
*vertices
,
1131 __isl_take isl_basic_set
*dom
, int id
)
1134 isl_cell
*cell
= NULL
;
1136 if (!vertices
|| !dom
)
1139 cell
= isl_calloc_type(dom
->ctx
, isl_cell
);
1143 cell
->n_vertices
= vertices
->c
[id
].n_vertices
;
1144 cell
->ids
= isl_alloc_array(dom
->ctx
, int, cell
->n_vertices
);
1145 if (cell
->n_vertices
&& !cell
->ids
)
1147 for (i
= 0; i
< cell
->n_vertices
; ++i
)
1148 cell
->ids
[i
] = vertices
->c
[id
].vertices
[i
];
1149 cell
->vertices
= vertices
;
1154 isl_cell_free(cell
);
1155 isl_vertices_free(vertices
);
1156 isl_basic_set_free(dom
);
1160 __isl_null isl_cell
*isl_cell_free(__isl_take isl_cell
*cell
)
1165 isl_vertices_free(cell
->vertices
);
1167 isl_basic_set_free(cell
->dom
);
1173 /* Create a tableau of the cone obtained by first homogenizing the given
1174 * polytope and then making all inequalities strict by setting the
1175 * constant term to -1.
1177 static struct isl_tab
*tab_for_shifted_cone(__isl_keep isl_basic_set
*bset
)
1181 struct isl_tab
*tab
;
1184 total
= isl_basic_set_dim(bset
, isl_dim_all
);
1187 tab
= isl_tab_alloc(bset
->ctx
, bset
->n_eq
+ bset
->n_ineq
+ 1,
1191 tab
->rational
= ISL_F_ISSET(bset
, ISL_BASIC_SET_RATIONAL
);
1192 if (ISL_F_ISSET(bset
, ISL_BASIC_MAP_EMPTY
)) {
1193 if (isl_tab_mark_empty(tab
) < 0)
1198 c
= isl_vec_alloc(bset
->ctx
, 1 + 1 + total
);
1202 isl_int_set_si(c
->el
[0], 0);
1203 for (i
= 0; i
< bset
->n_eq
; ++i
) {
1204 isl_seq_cpy(c
->el
+ 1, bset
->eq
[i
], c
->size
- 1);
1205 if (isl_tab_add_eq(tab
, c
->el
) < 0)
1209 isl_int_set_si(c
->el
[0], -1);
1210 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
1211 isl_seq_cpy(c
->el
+ 1, bset
->ineq
[i
], c
->size
- 1);
1212 if (isl_tab_add_ineq(tab
, c
->el
) < 0)
1220 isl_seq_clr(c
->el
+ 1, c
->size
- 1);
1221 isl_int_set_si(c
->el
[1], 1);
1222 if (isl_tab_add_ineq(tab
, c
->el
) < 0)
1233 /* Compute an interior point of "bset" by selecting an interior
1234 * point in homogeneous space and projecting the point back down.
1236 static __isl_give isl_vec
*isl_basic_set_interior_point(
1237 __isl_keep isl_basic_set
*bset
)
1240 struct isl_tab
*tab
;
1242 tab
= tab_for_shifted_cone(bset
);
1243 vec
= isl_tab_get_sample_value(tab
);
1248 isl_seq_cpy(vec
->el
, vec
->el
+ 1, vec
->size
- 1);
1254 /* Call "fn" on all chambers of the parametric polytope with the shared
1255 * facets of neighboring chambers only appearing in one of the chambers.
1257 * We pick an interior point from one of the chambers and then make
1258 * all constraints that do not satisfy this point strict.
1259 * For constraints that saturate the interior point, the sign
1260 * of the first non-zero coefficient is used to determine which
1261 * of the two (internal) constraints should be tightened.
1263 isl_stat
isl_vertices_foreach_disjoint_cell(__isl_keep isl_vertices
*vertices
,
1264 isl_stat (*fn
)(__isl_take isl_cell
*cell
, void *user
), void *user
)
1271 return isl_stat_error
;
1273 if (vertices
->n_chambers
== 0)
1276 if (vertices
->n_chambers
== 1) {
1277 isl_basic_set
*dom
= isl_basic_set_copy(vertices
->c
[0].dom
);
1278 dom
= isl_basic_set_set_integral(dom
);
1279 cell
= isl_cell_alloc(isl_vertices_copy(vertices
), dom
, 0);
1281 return isl_stat_error
;
1282 return fn(cell
, user
);
1285 vec
= isl_basic_set_interior_point(vertices
->c
[0].dom
);
1287 return isl_stat_error
;
1289 for (i
= 0; i
< vertices
->n_chambers
; ++i
) {
1291 isl_basic_set
*dom
= isl_basic_set_copy(vertices
->c
[i
].dom
);
1293 dom
= isl_basic_set_tighten_outward(dom
, vec
);
1294 dom
= isl_basic_set_set_integral(dom
);
1295 cell
= isl_cell_alloc(isl_vertices_copy(vertices
), dom
, i
);
1308 return isl_stat_error
;
1311 isl_stat
isl_vertices_foreach_cell(__isl_keep isl_vertices
*vertices
,
1312 isl_stat (*fn
)(__isl_take isl_cell
*cell
, void *user
), void *user
)
1318 return isl_stat_error
;
1320 if (vertices
->n_chambers
== 0)
1323 for (i
= 0; i
< vertices
->n_chambers
; ++i
) {
1325 isl_basic_set
*dom
= isl_basic_set_copy(vertices
->c
[i
].dom
);
1327 cell
= isl_cell_alloc(isl_vertices_copy(vertices
), dom
, i
);
1329 return isl_stat_error
;
1333 return isl_stat_error
;
1339 isl_stat
isl_vertices_foreach_vertex(__isl_keep isl_vertices
*vertices
,
1340 isl_stat (*fn
)(__isl_take isl_vertex
*vertex
, void *user
), void *user
)
1346 return isl_stat_error
;
1348 if (vertices
->n_vertices
== 0)
1351 for (i
= 0; i
< vertices
->n_vertices
; ++i
) {
1354 vertex
= isl_vertex_alloc(isl_vertices_copy(vertices
), i
);
1356 return isl_stat_error
;
1358 r
= fn(vertex
, user
);
1360 return isl_stat_error
;
1366 isl_stat
isl_cell_foreach_vertex(__isl_keep isl_cell
*cell
,
1367 isl_stat (*fn
)(__isl_take isl_vertex
*vertex
, void *user
), void *user
)
1373 return isl_stat_error
;
1375 if (cell
->n_vertices
== 0)
1378 for (i
= 0; i
< cell
->n_vertices
; ++i
) {
1381 vertex
= isl_vertex_alloc(isl_vertices_copy(cell
->vertices
),
1384 return isl_stat_error
;
1386 r
= fn(vertex
, user
);
1388 return isl_stat_error
;
1394 isl_ctx
*isl_vertices_get_ctx(__isl_keep isl_vertices
*vertices
)
1396 return vertices
? vertices
->bset
->ctx
: NULL
;
1399 isl_size
isl_vertices_get_n_vertices(__isl_keep isl_vertices
*vertices
)
1401 return vertices
? vertices
->n_vertices
: isl_size_error
;
1404 __isl_give isl_vertices
*isl_morph_vertices(__isl_take isl_morph
*morph
,
1405 __isl_take isl_vertices
*vertices
)
1408 isl_morph
*param_morph
= NULL
;
1410 if (!morph
|| !vertices
)
1413 isl_assert(vertices
->bset
->ctx
, vertices
->ref
== 1, goto error
);
1415 param_morph
= isl_morph_copy(morph
);
1416 param_morph
= isl_morph_dom_params(param_morph
);
1417 param_morph
= isl_morph_ran_params(param_morph
);
1419 for (i
= 0; i
< vertices
->n_vertices
; ++i
) {
1420 vertices
->v
[i
].dom
= isl_morph_basic_set(
1421 isl_morph_copy(param_morph
), vertices
->v
[i
].dom
);
1422 vertices
->v
[i
].vertex
= isl_morph_basic_set(
1423 isl_morph_copy(morph
), vertices
->v
[i
].vertex
);
1424 if (!vertices
->v
[i
].vertex
)
1428 for (i
= 0; i
< vertices
->n_chambers
; ++i
) {
1429 vertices
->c
[i
].dom
= isl_morph_basic_set(
1430 isl_morph_copy(param_morph
), vertices
->c
[i
].dom
);
1431 if (!vertices
->c
[i
].dom
)
1435 isl_morph_free(param_morph
);
1436 isl_morph_free(morph
);
1439 isl_morph_free(param_morph
);
1440 isl_morph_free(morph
);
1441 isl_vertices_free(vertices
);
1445 /* Construct a simplex isl_cell spanned by the vertices with indices in
1446 * "simplex_ids" and "other_ids" and call "fn" on this isl_cell.
1448 static isl_stat
call_on_simplex(__isl_keep isl_cell
*cell
,
1449 int *simplex_ids
, int n_simplex
, int *other_ids
, int n_other
,
1450 isl_stat (*fn
)(__isl_take isl_cell
*simplex
, void *user
), void *user
)
1454 struct isl_cell
*simplex
;
1456 ctx
= isl_cell_get_ctx(cell
);
1458 simplex
= isl_calloc_type(ctx
, struct isl_cell
);
1460 return isl_stat_error
;
1461 simplex
->vertices
= isl_vertices_copy(cell
->vertices
);
1462 if (!simplex
->vertices
)
1464 simplex
->dom
= isl_basic_set_copy(cell
->dom
);
1467 simplex
->n_vertices
= n_simplex
+ n_other
;
1468 simplex
->ids
= isl_alloc_array(ctx
, int, simplex
->n_vertices
);
1472 for (i
= 0; i
< n_simplex
; ++i
)
1473 simplex
->ids
[i
] = simplex_ids
[i
];
1474 for (i
= 0; i
< n_other
; ++i
)
1475 simplex
->ids
[n_simplex
+ i
] = other_ids
[i
];
1477 return fn(simplex
, user
);
1479 isl_cell_free(simplex
);
1480 return isl_stat_error
;
1483 /* Check whether the parametric vertex described by "vertex"
1484 * lies on the facet corresponding to constraint "facet" of "bset".
1485 * The isl_vec "v" is a temporary vector than can be used by this function.
1487 * We eliminate the variables from the facet constraint using the
1488 * equalities defining the vertex and check if the result is identical
1491 * It would probably be better to keep track of the constraints defining
1492 * a vertex during the vertex construction so that we could simply look
1495 static int vertex_on_facet(__isl_keep isl_basic_set
*vertex
,
1496 __isl_keep isl_basic_set
*bset
, int facet
, __isl_keep isl_vec
*v
)
1501 isl_seq_cpy(v
->el
, bset
->ineq
[facet
], v
->size
);
1504 for (i
= 0; i
< vertex
->n_eq
; ++i
) {
1505 int k
= isl_seq_last_non_zero(vertex
->eq
[i
], v
->size
);
1506 isl_seq_elim(v
->el
, vertex
->eq
[i
], k
, v
->size
, &m
);
1510 return isl_seq_first_non_zero(v
->el
, v
->size
) == -1;
1513 /* Triangulate the polytope spanned by the vertices with ids
1514 * in "simplex_ids" and "other_ids" and call "fn" on each of
1515 * the resulting simplices.
1516 * If the input polytope is already a simplex, we simply call "fn".
1517 * Otherwise, we pick a point from "other_ids" and add it to "simplex_ids".
1518 * Then we consider each facet of "bset" that does not contain the point
1519 * we just picked, but does contain some of the other points in "other_ids"
1520 * and call ourselves recursively on the polytope spanned by the new
1521 * "simplex_ids" and those points in "other_ids" that lie on the facet.
1523 static isl_stat
triangulate(__isl_keep isl_cell
*cell
, __isl_keep isl_vec
*v
,
1524 int *simplex_ids
, int n_simplex
, int *other_ids
, int n_other
,
1525 isl_stat (*fn
)(__isl_take isl_cell
*simplex
, void *user
), void *user
)
1531 isl_basic_set
*vertex
;
1532 isl_basic_set
*bset
;
1534 ctx
= isl_cell_get_ctx(cell
);
1535 d
= isl_basic_set_dim(cell
->vertices
->bset
, isl_dim_set
);
1536 nparam
= isl_basic_set_dim(cell
->vertices
->bset
, isl_dim_param
);
1537 if (d
< 0 || nparam
< 0)
1538 return isl_stat_error
;
1540 if (n_simplex
+ n_other
== d
+ 1)
1541 return call_on_simplex(cell
, simplex_ids
, n_simplex
,
1542 other_ids
, n_other
, fn
, user
);
1544 simplex_ids
[n_simplex
] = other_ids
[0];
1545 vertex
= cell
->vertices
->v
[other_ids
[0]].vertex
;
1546 bset
= cell
->vertices
->bset
;
1548 ids
= isl_alloc_array(ctx
, int, n_other
- 1);
1551 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
1552 if (isl_seq_first_non_zero(bset
->ineq
[i
] + 1 + nparam
, d
) == -1)
1554 if (vertex_on_facet(vertex
, bset
, i
, v
))
1557 for (j
= 1, k
= 0; j
< n_other
; ++j
) {
1559 ov
= cell
->vertices
->v
[other_ids
[j
]].vertex
;
1560 if (vertex_on_facet(ov
, bset
, i
, v
))
1561 ids
[k
++] = other_ids
[j
];
1566 if (triangulate(cell
, v
, simplex_ids
, n_simplex
+ 1,
1567 ids
, k
, fn
, user
) < 0)
1575 return isl_stat_error
;
1578 /* Triangulate the given cell and call "fn" on each of the resulting
1581 isl_stat
isl_cell_foreach_simplex(__isl_take isl_cell
*cell
,
1582 isl_stat (*fn
)(__isl_take isl_cell
*simplex
, void *user
), void *user
)
1588 int *simplex_ids
= NULL
;
1591 return isl_stat_error
;
1593 d
= isl_basic_set_dim(cell
->vertices
->bset
, isl_dim_set
);
1594 total
= isl_basic_set_dim(cell
->vertices
->bset
, isl_dim_all
);
1595 if (d
< 0 || total
< 0)
1596 return isl_stat_error
;
1598 if (cell
->n_vertices
== d
+ 1)
1599 return fn(cell
, user
);
1601 ctx
= isl_cell_get_ctx(cell
);
1602 simplex_ids
= isl_alloc_array(ctx
, int, d
+ 1);
1606 v
= isl_vec_alloc(ctx
, 1 + total
);
1610 r
= triangulate(cell
, v
, simplex_ids
, 0,
1611 cell
->ids
, cell
->n_vertices
, fn
, user
);
1616 isl_cell_free(cell
);
1622 isl_cell_free(cell
);
1623 return isl_stat_error
;