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 return isl_bool_error
;
284 indep
= is_independent(facets
, selected
, bset
->ineq
[level
] + 1 + ovar
);
285 if (indep
< 0 || !indep
)
288 snap
= isl_tab_snap(tab
);
289 if (isl_tab_select_facet(tab
, level
) < 0)
290 return isl_bool_error
;
293 if (isl_tab_rollback(tab
, snap
) < 0)
294 return isl_bool_error
;
295 return isl_bool_false
;
298 for (i
= 0; i
< level
; ++i
) {
301 if (selection
[i
] != DESELECTED
)
304 if (isl_tab_is_equality(tab
, i
))
306 else if (isl_tab_is_redundant(tab
, i
))
309 sgn
= isl_tab_sign_of_max(tab
, i
);
311 return isl_bool_error
;
313 if (isl_tab_rollback(tab
, snap
) < 0)
314 return isl_bool_error
;
315 return isl_bool_false
;
319 return isl_bool_true
;
322 /* Compute the parametric vertices and the chamber decomposition
323 * of a parametric polytope that is not full-dimensional.
325 * Simply map the parametric polytope to a lower dimensional space
326 * and map the resulting vertices back.
328 static __isl_give isl_vertices
*lower_dim_vertices(
329 __isl_take isl_basic_set
*bset
)
332 isl_vertices
*vertices
;
334 morph
= isl_basic_set_full_compression(bset
);
335 bset
= isl_morph_basic_set(isl_morph_copy(morph
), bset
);
337 vertices
= isl_basic_set_compute_vertices(bset
);
338 isl_basic_set_free(bset
);
340 morph
= isl_morph_inverse(morph
);
342 vertices
= isl_morph_vertices(morph
, vertices
);
347 /* Compute the parametric vertices and the chamber decomposition
348 * of a parametric polytope "bset" that is not full-dimensional.
349 * Additionally, free both "copy" and "tab".
351 static __isl_give isl_vertices
*lower_dim_vertices_free(
352 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*copy
,
355 isl_basic_set_free(copy
);
357 return lower_dim_vertices(bset
);
360 /* Detect implicit equality constraints in "bset" using the tableau
361 * representation "tab".
362 * Return a copy of "bset" with the implicit equality constraints
363 * made explicit, leaving the original "bset" unmodified.
365 static __isl_give isl_basic_set
*detect_implicit_equality_constraints(
366 __isl_keep isl_basic_set
*bset
, struct isl_tab
*tab
)
368 if (isl_tab_detect_implicit_equalities(tab
) < 0)
371 bset
= isl_basic_set_copy(bset
);
372 bset
= isl_basic_set_cow(bset
);
373 bset
= isl_basic_set_update_from_tab(bset
, tab
);
378 /* Compute the parametric vertices and the chamber decomposition
379 * of the parametric polytope defined using the same constraints
380 * as "bset". "bset" is assumed to have no existentially quantified
383 * The vertices themselves are computed in a fairly simplistic way.
384 * We simply run through all combinations of d constraints,
385 * with d the number of set variables, and check if those d constraints
386 * define a vertex. To avoid the generation of duplicate vertices,
387 * which may happen if a vertex is defined by more than d constraints,
388 * we make sure we only generate the vertex for the d constraints with
391 * Only potential vertices with a full-dimensional activity domain
392 * are considered. However, if the input has (implicit) equality
393 * constraints among the parameters, then activity domain
394 * should be considered full-dimensional if it does not satisfy
395 * any extra equality constraints beyond those of the input.
396 * The implicit equality constraints of the input are therefore first detected.
397 * If there are any, then the input is mapped to a lower dimensional space
398 * such that the check for full-dimensional activity domains
399 * can be performed with respect to a full-dimensional space.
400 * Note that it is important to leave "bset" unmodified while detecting
401 * equality constraints since the inequality constraints of "bset"
402 * are assumed to correspond to those of the tableau.
404 * We set up a tableau and keep track of which facets have been
405 * selected. The tableau is marked strict_redundant so that we can be
406 * sure that any constraint that is marked redundant (and that is not
407 * also marked zero) is not an equality.
408 * If a constraint is marked DESELECTED, it means the constraint was
409 * SELECTED before (in combination with the same selection of earlier
410 * constraints). If such a deselected constraint turns out to be an
411 * equality, then any vertex that may still be found with the current
412 * selection has already been generated when the constraint was selected.
413 * A constraint is marked UNSELECTED when there is no way selecting
414 * the constraint could lead to a vertex (in combination with the current
415 * selection of earlier constraints).
417 * The set variable coefficients of the selected constraints are stored
418 * in the facets matrix.
420 __isl_give isl_vertices
*isl_basic_set_compute_vertices(
421 __isl_keep isl_basic_set
*bset
)
428 int *selection
= NULL
;
430 struct isl_tab_undo
**snap
= NULL
;
431 isl_mat
*facets
= NULL
;
432 struct isl_vertex_list
*list
= NULL
;
434 isl_vertices
*vertices
;
441 if (isl_basic_set_plain_is_empty(bset
))
442 return vertices_empty(bset
);
445 return lower_dim_vertices(isl_basic_set_copy(bset
));
447 if (isl_basic_set_check_no_locals(bset
) < 0)
450 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
454 return vertices_0D(bset
);
456 copy
= isl_basic_set_copy(bset
);
457 copy
= isl_basic_set_set_rational(copy
);
461 tab
= isl_tab_from_basic_set(copy
, 0);
464 tab
->strict_redundant
= 1;
467 vertices
= vertices_empty(copy
);
468 isl_basic_set_free(copy
);
473 test
= detect_implicit_equality_constraints(bset
, tab
);
474 n_eq
= isl_basic_set_n_equality(test
);
476 test
= isl_basic_set_free(test
);
477 if (n_eq
< 0 || n_eq
> 0)
478 return lower_dim_vertices_free(test
, copy
, tab
);
479 isl_basic_set_free(test
);
481 selection
= isl_alloc_array(copy
->ctx
, int, copy
->n_ineq
);
482 snap
= isl_alloc_array(copy
->ctx
, struct isl_tab_undo
*, copy
->n_ineq
);
483 facets
= isl_mat_alloc(copy
->ctx
, nvar
, nvar
);
484 if ((copy
->n_ineq
&& (!selection
|| !snap
)) || !facets
)
492 if (level
>= copy
->n_ineq
||
493 (!init
&& selection
[level
] != SELECTED
)) {
500 snap
[level
] = isl_tab_snap(tab
);
501 ok
= can_select(copy
, level
, tab
, facets
, selected
,
506 selection
[level
] = SELECTED
;
509 selection
[level
] = UNSELECTED
;
511 selection
[level
] = DESELECTED
;
513 if (isl_tab_rollback(tab
, snap
[level
]) < 0)
516 if (selected
== nvar
) {
517 if (tab
->n_dead
== nvar
) {
518 isl_bool added
= add_vertex(&list
, copy
, tab
);
531 isl_mat_free(facets
);
537 vertices
= vertices_from_list(copy
, n_vertices
, list
);
539 vertices
= compute_chambers(copy
, vertices
);
543 free_vertex_list(list
);
544 isl_mat_free(facets
);
548 isl_basic_set_free(copy
);
552 struct isl_chamber_list
{
553 struct isl_chamber c
;
554 struct isl_chamber_list
*next
;
557 static void free_chamber_list(struct isl_chamber_list
*list
)
559 struct isl_chamber_list
*next
;
561 for (; list
; list
= next
) {
563 isl_basic_set_free(list
->c
.dom
);
564 free(list
->c
.vertices
);
569 /* Check whether the basic set "bset" is a superset of the basic set described
570 * by "tab", i.e., check whether all constraints of "bset" are redundant.
572 static isl_bool
bset_covers_tab(__isl_keep isl_basic_set
*bset
,
578 return isl_bool_error
;
580 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
581 enum isl_ineq_type type
= isl_tab_ineq_type(tab
, bset
->ineq
[i
]);
583 case isl_ineq_error
: return isl_bool_error
;
584 case isl_ineq_redundant
: continue;
585 default: return isl_bool_false
;
589 return isl_bool_true
;
592 static __isl_give isl_vertices
*vertices_add_chambers(
593 __isl_take isl_vertices
*vertices
, int n_chambers
,
594 struct isl_chamber_list
*list
)
598 struct isl_chamber_list
*next
;
600 ctx
= isl_vertices_get_ctx(vertices
);
601 vertices
->c
= isl_alloc_array(ctx
, struct isl_chamber
, n_chambers
);
604 vertices
->n_chambers
= n_chambers
;
606 for (i
= 0; list
; list
= next
, i
++) {
608 vertices
->c
[i
] = list
->c
;
614 isl_vertices_free(vertices
);
615 free_chamber_list(list
);
619 /* Can "tab" be intersected with "bset" without resulting in
620 * a lower-dimensional set.
621 * "bset" itself is assumed to be full-dimensional.
623 static isl_bool
can_intersect(struct isl_tab
*tab
,
624 __isl_keep isl_basic_set
*bset
)
627 struct isl_tab_undo
*snap
;
630 isl_die(isl_basic_set_get_ctx(bset
), isl_error_internal
,
631 "expecting full-dimensional input",
632 return isl_bool_error
);
634 if (isl_tab_extend_cons(tab
, bset
->n_ineq
) < 0)
635 return isl_bool_error
;
637 snap
= isl_tab_snap(tab
);
639 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
640 enum isl_ineq_type type
;
642 type
= isl_tab_ineq_type(tab
, bset
->ineq
[i
]);
644 return isl_bool_error
;
645 if (type
== isl_ineq_redundant
)
647 if (isl_tab_add_ineq(tab
, bset
->ineq
[i
]) < 0)
648 return isl_bool_error
;
651 if (isl_tab_detect_implicit_equalities(tab
) < 0)
652 return isl_bool_error
;
654 if (isl_tab_rollback(tab
, snap
) < 0)
655 return isl_bool_error
;
656 return isl_bool_false
;
659 return isl_bool_true
;
662 static int add_chamber(struct isl_chamber_list
**list
,
663 __isl_keep isl_vertices
*vertices
, struct isl_tab
*tab
, int *selection
)
668 struct isl_tab_undo
*snap
;
669 struct isl_chamber_list
*c
= NULL
;
671 for (i
= 0; i
< vertices
->n_vertices
; ++i
)
675 snap
= isl_tab_snap(tab
);
677 for (i
= 0; i
< tab
->n_con
&& tab
->con
[i
].frozen
; ++i
)
678 tab
->con
[i
].frozen
= 0;
681 if (isl_tab_detect_redundant(tab
) < 0)
684 c
= isl_calloc_type(tab
->mat
->ctx
, struct isl_chamber_list
);
687 c
->c
.vertices
= isl_alloc_array(tab
->mat
->ctx
, int, n_vertices
);
688 if (n_vertices
&& !c
->c
.vertices
)
690 c
->c
.dom
= isl_basic_set_copy(isl_tab_peek_bset(tab
));
691 c
->c
.dom
= isl_basic_set_set_rational(c
->c
.dom
);
692 c
->c
.dom
= isl_basic_set_cow(c
->c
.dom
);
693 c
->c
.dom
= isl_basic_set_update_from_tab(c
->c
.dom
, tab
);
694 c
->c
.dom
= isl_basic_set_simplify(c
->c
.dom
);
695 c
->c
.dom
= isl_basic_set_finalize(c
->c
.dom
);
699 c
->c
.n_vertices
= n_vertices
;
701 for (i
= 0, j
= 0; i
< vertices
->n_vertices
; ++i
)
703 c
->c
.vertices
[j
] = i
;
710 for (i
= 0; i
< n_frozen
; ++i
)
711 tab
->con
[i
].frozen
= 1;
713 if (isl_tab_rollback(tab
, snap
) < 0)
718 free_chamber_list(c
);
722 struct isl_facet_todo
{
723 struct isl_tab
*tab
; /* A tableau representation of the facet */
724 isl_basic_set
*bset
; /* A normalized basic set representation */
725 isl_vec
*constraint
; /* Constraint pointing to the other side */
726 struct isl_facet_todo
*next
;
729 static void free_todo(struct isl_facet_todo
*todo
)
732 struct isl_facet_todo
*next
= todo
->next
;
734 isl_tab_free(todo
->tab
);
735 isl_basic_set_free(todo
->bset
);
736 isl_vec_free(todo
->constraint
);
743 static struct isl_facet_todo
*create_todo(struct isl_tab
*tab
, int con
)
747 struct isl_tab_undo
*snap
;
748 struct isl_facet_todo
*todo
;
750 snap
= isl_tab_snap(tab
);
752 for (i
= 0; i
< tab
->n_con
&& tab
->con
[i
].frozen
; ++i
)
753 tab
->con
[i
].frozen
= 0;
756 if (isl_tab_detect_redundant(tab
) < 0)
759 todo
= isl_calloc_type(tab
->mat
->ctx
, struct isl_facet_todo
);
763 todo
->constraint
= isl_vec_alloc(tab
->mat
->ctx
, 1 + tab
->n_var
);
764 if (!todo
->constraint
)
766 isl_seq_neg(todo
->constraint
->el
, tab
->bmap
->ineq
[con
], 1 + tab
->n_var
);
767 todo
->bset
= isl_basic_set_copy(isl_tab_peek_bset(tab
));
768 todo
->bset
= isl_basic_set_set_rational(todo
->bset
);
769 todo
->bset
= isl_basic_set_cow(todo
->bset
);
770 todo
->bset
= isl_basic_set_update_from_tab(todo
->bset
, tab
);
771 todo
->bset
= isl_basic_set_simplify(todo
->bset
);
772 todo
->bset
= isl_basic_set_sort_constraints(todo
->bset
);
775 ISL_F_SET(todo
->bset
, ISL_BASIC_SET_NO_REDUNDANT
);
776 todo
->tab
= isl_tab_dup(tab
);
780 for (i
= 0; i
< n_frozen
; ++i
)
781 tab
->con
[i
].frozen
= 1;
783 if (isl_tab_rollback(tab
, snap
) < 0)
792 /* Create todo items for all interior facets of the chamber represented
793 * by "tab" and collect them in "next".
795 static int init_todo(struct isl_facet_todo
**next
, struct isl_tab
*tab
)
798 struct isl_tab_undo
*snap
;
799 struct isl_facet_todo
*todo
;
801 snap
= isl_tab_snap(tab
);
803 for (i
= 0; i
< tab
->n_con
; ++i
) {
804 if (tab
->con
[i
].frozen
)
806 if (tab
->con
[i
].is_redundant
)
809 if (isl_tab_select_facet(tab
, i
) < 0)
812 todo
= create_todo(tab
, i
);
819 if (isl_tab_rollback(tab
, snap
) < 0)
826 /* Does the linked list contain a todo item that is the opposite of "todo".
827 * If so, return 1 and remove the opposite todo item.
829 static int has_opposite(struct isl_facet_todo
*todo
,
830 struct isl_facet_todo
**list
)
832 for (; *list
; list
= &(*list
)->next
) {
834 eq
= isl_basic_set_plain_is_equal(todo
->bset
, (*list
)->bset
);
849 /* Create todo items for all interior facets of the chamber represented
850 * by "tab" and collect them in first->next, taking care to cancel
851 * opposite todo items.
853 static int update_todo(struct isl_facet_todo
*first
, struct isl_tab
*tab
)
856 struct isl_tab_undo
*snap
;
857 struct isl_facet_todo
*todo
;
859 snap
= isl_tab_snap(tab
);
861 for (i
= 0; i
< tab
->n_con
; ++i
) {
864 if (tab
->con
[i
].frozen
)
866 if (tab
->con
[i
].is_redundant
)
869 if (isl_tab_select_facet(tab
, i
) < 0)
872 todo
= create_todo(tab
, i
);
876 drop
= has_opposite(todo
, &first
->next
);
883 todo
->next
= first
->next
;
887 if (isl_tab_rollback(tab
, snap
) < 0)
894 /* Compute the chamber decomposition of the parametric polytope respresented
895 * by "bset" given the parametric vertices and their activity domains.
897 * We are only interested in full-dimensional chambers.
898 * Each of these chambers is the intersection of the activity domains of
899 * one or more vertices and the union of all chambers is equal to the
900 * projection of the entire parametric polytope onto the parameter space.
902 * We first create an initial chamber by intersecting as many activity
903 * domains as possible without ending up with an empty or lower-dimensional
904 * set. As a minor optimization, we only consider those activity domains
905 * that contain some arbitrary point.
907 * For each of the interior facets of the chamber, we construct a todo item,
908 * containing the facet and a constraint containing the other side of the facet,
909 * for constructing the chamber on the other side.
910 * While their are any todo items left, we pick a todo item and
911 * create the required chamber by intersecting all activity domains
912 * that contain the facet and have a full-dimensional intersection with
913 * the other side of the facet. For each of the interior facets, we
914 * again create todo items, taking care to cancel opposite todo items.
916 static __isl_give isl_vertices
*compute_chambers(__isl_take isl_basic_set
*bset
,
917 __isl_take isl_vertices
*vertices
)
922 isl_vec
*sample
= NULL
;
923 struct isl_tab
*tab
= NULL
;
924 struct isl_tab_undo
*snap
;
925 int *selection
= NULL
;
927 struct isl_chamber_list
*list
= NULL
;
928 struct isl_facet_todo
*todo
= NULL
;
930 if (!bset
|| !vertices
)
933 ctx
= isl_vertices_get_ctx(vertices
);
934 selection
= isl_alloc_array(ctx
, int, vertices
->n_vertices
);
935 if (vertices
->n_vertices
&& !selection
)
938 bset
= isl_basic_set_params(bset
);
939 n_eq
= isl_basic_set_n_equality(bset
);
943 isl_die(isl_basic_set_get_ctx(bset
), isl_error_internal
,
944 "expecting full-dimensional input", goto error
);
946 tab
= isl_tab_from_basic_set(bset
, 1);
949 for (i
= 0; i
< bset
->n_ineq
; ++i
)
950 if (isl_tab_freeze_constraint(tab
, i
) < 0)
952 isl_basic_set_free(bset
);
954 snap
= isl_tab_snap(tab
);
956 sample
= isl_tab_get_sample_value(tab
);
958 for (i
= 0; i
< vertices
->n_vertices
; ++i
) {
959 selection
[i
] = isl_basic_set_contains(vertices
->v
[i
].dom
, sample
);
960 if (selection
[i
] < 0)
964 selection
[i
] = can_intersect(tab
, vertices
->v
[i
].dom
);
965 if (selection
[i
] < 0)
969 if (isl_tab_detect_redundant(tab
) < 0)
972 if (add_chamber(&list
, vertices
, tab
, selection
) < 0)
976 if (init_todo(&todo
, tab
) < 0)
980 struct isl_facet_todo
*next
;
982 if (isl_tab_rollback(tab
, snap
) < 0)
985 if (isl_tab_add_ineq(tab
, todo
->constraint
->el
) < 0)
987 if (isl_tab_freeze_constraint(tab
, tab
->n_con
- 1) < 0)
990 for (i
= 0; i
< vertices
->n_vertices
; ++i
) {
991 selection
[i
] = bset_covers_tab(vertices
->v
[i
].dom
,
993 if (selection
[i
] < 0)
997 selection
[i
] = can_intersect(tab
, vertices
->v
[i
].dom
);
998 if (selection
[i
] < 0)
1002 if (isl_tab_detect_redundant(tab
) < 0)
1005 if (add_chamber(&list
, vertices
, tab
, selection
) < 0)
1009 if (update_todo(todo
, tab
) < 0)
1018 isl_vec_free(sample
);
1023 vertices
= vertices_add_chambers(vertices
, n_chambers
, list
);
1025 for (i
= 0; vertices
&& i
< vertices
->n_vertices
; ++i
) {
1026 isl_basic_set_free(vertices
->v
[i
].dom
);
1027 vertices
->v
[i
].dom
= NULL
;
1032 free_chamber_list(list
);
1034 isl_vec_free(sample
);
1038 isl_basic_set_free(bset
);
1039 isl_vertices_free(vertices
);
1043 isl_ctx
*isl_vertex_get_ctx(__isl_keep isl_vertex
*vertex
)
1045 return vertex
? isl_vertices_get_ctx(vertex
->vertices
) : NULL
;
1048 isl_size
isl_vertex_get_id(__isl_keep isl_vertex
*vertex
)
1050 return vertex
? vertex
->id
: isl_size_error
;
1053 /* Return the activity domain of the vertex "vertex".
1055 __isl_give isl_basic_set
*isl_vertex_get_domain(__isl_keep isl_vertex
*vertex
)
1057 struct isl_vertex
*v
;
1062 v
= &vertex
->vertices
->v
[vertex
->id
];
1064 v
->dom
= isl_basic_set_copy(v
->vertex
);
1065 v
->dom
= isl_basic_set_params(v
->dom
);
1066 v
->dom
= isl_basic_set_set_integral(v
->dom
);
1069 return isl_basic_set_copy(v
->dom
);
1072 /* Return a multiple quasi-affine expression describing the vertex "vertex"
1073 * in terms of the parameters,
1075 __isl_give isl_multi_aff
*isl_vertex_get_expr(__isl_keep isl_vertex
*vertex
)
1077 struct isl_vertex
*v
;
1078 isl_basic_set
*bset
;
1083 v
= &vertex
->vertices
->v
[vertex
->id
];
1085 bset
= isl_basic_set_copy(v
->vertex
);
1086 return isl_multi_aff_from_basic_set_equalities(bset
);
1089 static __isl_give isl_vertex
*isl_vertex_alloc(__isl_take isl_vertices
*vertices
,
1098 ctx
= isl_vertices_get_ctx(vertices
);
1099 vertex
= isl_alloc_type(ctx
, isl_vertex
);
1103 vertex
->vertices
= vertices
;
1108 isl_vertices_free(vertices
);
1112 __isl_null isl_vertex
*isl_vertex_free(__isl_take isl_vertex
*vertex
)
1116 isl_vertices_free(vertex
->vertices
);
1122 isl_ctx
*isl_cell_get_ctx(__isl_keep isl_cell
*cell
)
1124 return cell
? cell
->dom
->ctx
: NULL
;
1127 __isl_give isl_basic_set
*isl_cell_get_domain(__isl_keep isl_cell
*cell
)
1129 return cell
? isl_basic_set_copy(cell
->dom
) : NULL
;
1132 static __isl_give isl_cell
*isl_cell_alloc(__isl_take isl_vertices
*vertices
,
1133 __isl_take isl_basic_set
*dom
, int id
)
1136 isl_cell
*cell
= NULL
;
1138 if (!vertices
|| !dom
)
1141 cell
= isl_calloc_type(dom
->ctx
, isl_cell
);
1145 cell
->n_vertices
= vertices
->c
[id
].n_vertices
;
1146 cell
->ids
= isl_alloc_array(dom
->ctx
, int, cell
->n_vertices
);
1147 if (cell
->n_vertices
&& !cell
->ids
)
1149 for (i
= 0; i
< cell
->n_vertices
; ++i
)
1150 cell
->ids
[i
] = vertices
->c
[id
].vertices
[i
];
1151 cell
->vertices
= vertices
;
1156 isl_cell_free(cell
);
1157 isl_vertices_free(vertices
);
1158 isl_basic_set_free(dom
);
1162 __isl_null isl_cell
*isl_cell_free(__isl_take isl_cell
*cell
)
1167 isl_vertices_free(cell
->vertices
);
1169 isl_basic_set_free(cell
->dom
);
1175 /* Create a tableau of the cone obtained by first homogenizing the given
1176 * polytope and then making all inequalities strict by setting the
1177 * constant term to -1.
1179 static struct isl_tab
*tab_for_shifted_cone(__isl_keep isl_basic_set
*bset
)
1183 struct isl_tab
*tab
;
1186 total
= isl_basic_set_dim(bset
, isl_dim_all
);
1189 tab
= isl_tab_alloc(bset
->ctx
, bset
->n_eq
+ bset
->n_ineq
+ 1,
1193 tab
->rational
= ISL_F_ISSET(bset
, ISL_BASIC_SET_RATIONAL
);
1194 if (ISL_F_ISSET(bset
, ISL_BASIC_MAP_EMPTY
)) {
1195 if (isl_tab_mark_empty(tab
) < 0)
1200 c
= isl_vec_alloc(bset
->ctx
, 1 + 1 + total
);
1204 isl_int_set_si(c
->el
[0], 0);
1205 for (i
= 0; i
< bset
->n_eq
; ++i
) {
1206 isl_seq_cpy(c
->el
+ 1, bset
->eq
[i
], c
->size
- 1);
1207 if (isl_tab_add_eq(tab
, c
->el
) < 0)
1211 isl_int_set_si(c
->el
[0], -1);
1212 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
1213 isl_seq_cpy(c
->el
+ 1, bset
->ineq
[i
], c
->size
- 1);
1214 if (isl_tab_add_ineq(tab
, c
->el
) < 0)
1222 isl_seq_clr(c
->el
+ 1, c
->size
- 1);
1223 isl_int_set_si(c
->el
[1], 1);
1224 if (isl_tab_add_ineq(tab
, c
->el
) < 0)
1235 /* Compute an interior point of "bset" by selecting an interior
1236 * point in homogeneous space and projecting the point back down.
1238 static __isl_give isl_vec
*isl_basic_set_interior_point(
1239 __isl_keep isl_basic_set
*bset
)
1242 struct isl_tab
*tab
;
1244 tab
= tab_for_shifted_cone(bset
);
1245 vec
= isl_tab_get_sample_value(tab
);
1250 isl_seq_cpy(vec
->el
, vec
->el
+ 1, vec
->size
- 1);
1256 /* Call "fn" on all chambers of the parametric polytope with the shared
1257 * facets of neighboring chambers only appearing in one of the chambers.
1259 * We pick an interior point from one of the chambers and then make
1260 * all constraints that do not satisfy this point strict.
1261 * For constraints that saturate the interior point, the sign
1262 * of the first non-zero coefficient is used to determine which
1263 * of the two (internal) constraints should be tightened.
1265 isl_stat
isl_vertices_foreach_disjoint_cell(__isl_keep isl_vertices
*vertices
,
1266 isl_stat (*fn
)(__isl_take isl_cell
*cell
, void *user
), void *user
)
1273 return isl_stat_error
;
1275 if (vertices
->n_chambers
== 0)
1278 if (vertices
->n_chambers
== 1) {
1279 isl_basic_set
*dom
= isl_basic_set_copy(vertices
->c
[0].dom
);
1280 dom
= isl_basic_set_set_integral(dom
);
1281 cell
= isl_cell_alloc(isl_vertices_copy(vertices
), dom
, 0);
1283 return isl_stat_error
;
1284 return fn(cell
, user
);
1287 vec
= isl_basic_set_interior_point(vertices
->c
[0].dom
);
1289 return isl_stat_error
;
1291 for (i
= 0; i
< vertices
->n_chambers
; ++i
) {
1293 isl_basic_set
*dom
= isl_basic_set_copy(vertices
->c
[i
].dom
);
1295 dom
= isl_basic_set_tighten_outward(dom
, vec
);
1296 dom
= isl_basic_set_set_integral(dom
);
1297 cell
= isl_cell_alloc(isl_vertices_copy(vertices
), dom
, i
);
1310 return isl_stat_error
;
1313 isl_stat
isl_vertices_foreach_cell(__isl_keep isl_vertices
*vertices
,
1314 isl_stat (*fn
)(__isl_take isl_cell
*cell
, void *user
), void *user
)
1320 return isl_stat_error
;
1322 if (vertices
->n_chambers
== 0)
1325 for (i
= 0; i
< vertices
->n_chambers
; ++i
) {
1327 isl_basic_set
*dom
= isl_basic_set_copy(vertices
->c
[i
].dom
);
1329 cell
= isl_cell_alloc(isl_vertices_copy(vertices
), dom
, i
);
1331 return isl_stat_error
;
1335 return isl_stat_error
;
1341 isl_stat
isl_vertices_foreach_vertex(__isl_keep isl_vertices
*vertices
,
1342 isl_stat (*fn
)(__isl_take isl_vertex
*vertex
, void *user
), void *user
)
1348 return isl_stat_error
;
1350 if (vertices
->n_vertices
== 0)
1353 for (i
= 0; i
< vertices
->n_vertices
; ++i
) {
1356 vertex
= isl_vertex_alloc(isl_vertices_copy(vertices
), i
);
1358 return isl_stat_error
;
1360 r
= fn(vertex
, user
);
1362 return isl_stat_error
;
1368 isl_stat
isl_cell_foreach_vertex(__isl_keep isl_cell
*cell
,
1369 isl_stat (*fn
)(__isl_take isl_vertex
*vertex
, void *user
), void *user
)
1375 return isl_stat_error
;
1377 if (cell
->n_vertices
== 0)
1380 for (i
= 0; i
< cell
->n_vertices
; ++i
) {
1383 vertex
= isl_vertex_alloc(isl_vertices_copy(cell
->vertices
),
1386 return isl_stat_error
;
1388 r
= fn(vertex
, user
);
1390 return isl_stat_error
;
1396 isl_ctx
*isl_vertices_get_ctx(__isl_keep isl_vertices
*vertices
)
1398 return vertices
? vertices
->bset
->ctx
: NULL
;
1401 isl_size
isl_vertices_get_n_vertices(__isl_keep isl_vertices
*vertices
)
1403 return vertices
? vertices
->n_vertices
: isl_size_error
;
1406 __isl_give isl_vertices
*isl_morph_vertices(__isl_take isl_morph
*morph
,
1407 __isl_take isl_vertices
*vertices
)
1410 isl_morph
*param_morph
= NULL
;
1412 if (!morph
|| !vertices
)
1415 isl_assert(vertices
->bset
->ctx
, vertices
->ref
== 1, goto error
);
1417 param_morph
= isl_morph_copy(morph
);
1418 param_morph
= isl_morph_dom_params(param_morph
);
1419 param_morph
= isl_morph_ran_params(param_morph
);
1421 for (i
= 0; i
< vertices
->n_vertices
; ++i
) {
1422 vertices
->v
[i
].dom
= isl_morph_basic_set(
1423 isl_morph_copy(param_morph
), vertices
->v
[i
].dom
);
1424 vertices
->v
[i
].vertex
= isl_morph_basic_set(
1425 isl_morph_copy(morph
), vertices
->v
[i
].vertex
);
1426 if (!vertices
->v
[i
].vertex
)
1430 for (i
= 0; i
< vertices
->n_chambers
; ++i
) {
1431 vertices
->c
[i
].dom
= isl_morph_basic_set(
1432 isl_morph_copy(param_morph
), vertices
->c
[i
].dom
);
1433 if (!vertices
->c
[i
].dom
)
1437 isl_morph_free(param_morph
);
1438 isl_morph_free(morph
);
1441 isl_morph_free(param_morph
);
1442 isl_morph_free(morph
);
1443 isl_vertices_free(vertices
);
1447 /* Construct a simplex isl_cell spanned by the vertices with indices in
1448 * "simplex_ids" and "other_ids" and call "fn" on this isl_cell.
1450 static isl_stat
call_on_simplex(__isl_keep isl_cell
*cell
,
1451 int *simplex_ids
, int n_simplex
, int *other_ids
, int n_other
,
1452 isl_stat (*fn
)(__isl_take isl_cell
*simplex
, void *user
), void *user
)
1456 struct isl_cell
*simplex
;
1458 ctx
= isl_cell_get_ctx(cell
);
1460 simplex
= isl_calloc_type(ctx
, struct isl_cell
);
1462 return isl_stat_error
;
1463 simplex
->vertices
= isl_vertices_copy(cell
->vertices
);
1464 if (!simplex
->vertices
)
1466 simplex
->dom
= isl_basic_set_copy(cell
->dom
);
1469 simplex
->n_vertices
= n_simplex
+ n_other
;
1470 simplex
->ids
= isl_alloc_array(ctx
, int, simplex
->n_vertices
);
1474 for (i
= 0; i
< n_simplex
; ++i
)
1475 simplex
->ids
[i
] = simplex_ids
[i
];
1476 for (i
= 0; i
< n_other
; ++i
)
1477 simplex
->ids
[n_simplex
+ i
] = other_ids
[i
];
1479 return fn(simplex
, user
);
1481 isl_cell_free(simplex
);
1482 return isl_stat_error
;
1485 /* Check whether the parametric vertex described by "vertex"
1486 * lies on the facet corresponding to constraint "facet" of "bset".
1487 * The isl_vec "v" is a temporary vector than can be used by this function.
1489 * We eliminate the variables from the facet constraint using the
1490 * equalities defining the vertex and check if the result is identical
1493 * It would probably be better to keep track of the constraints defining
1494 * a vertex during the vertex construction so that we could simply look
1497 static int vertex_on_facet(__isl_keep isl_basic_set
*vertex
,
1498 __isl_keep isl_basic_set
*bset
, int facet
, __isl_keep isl_vec
*v
)
1503 isl_seq_cpy(v
->el
, bset
->ineq
[facet
], v
->size
);
1506 for (i
= 0; i
< vertex
->n_eq
; ++i
) {
1507 int k
= isl_seq_last_non_zero(vertex
->eq
[i
], v
->size
);
1508 isl_seq_elim(v
->el
, vertex
->eq
[i
], k
, v
->size
, &m
);
1512 return isl_seq_first_non_zero(v
->el
, v
->size
) == -1;
1515 /* Triangulate the polytope spanned by the vertices with ids
1516 * in "simplex_ids" and "other_ids" and call "fn" on each of
1517 * the resulting simplices.
1518 * If the input polytope is already a simplex, we simply call "fn".
1519 * Otherwise, we pick a point from "other_ids" and add it to "simplex_ids".
1520 * Then we consider each facet of "bset" that does not contain the point
1521 * we just picked, but does contain some of the other points in "other_ids"
1522 * and call ourselves recursively on the polytope spanned by the new
1523 * "simplex_ids" and those points in "other_ids" that lie on the facet.
1525 static isl_stat
triangulate(__isl_keep isl_cell
*cell
, __isl_keep isl_vec
*v
,
1526 int *simplex_ids
, int n_simplex
, int *other_ids
, int n_other
,
1527 isl_stat (*fn
)(__isl_take isl_cell
*simplex
, void *user
), void *user
)
1533 isl_basic_set
*vertex
;
1534 isl_basic_set
*bset
;
1536 ctx
= isl_cell_get_ctx(cell
);
1537 d
= isl_basic_set_dim(cell
->vertices
->bset
, isl_dim_set
);
1538 nparam
= isl_basic_set_dim(cell
->vertices
->bset
, isl_dim_param
);
1539 if (d
< 0 || nparam
< 0)
1540 return isl_stat_error
;
1542 if (n_simplex
+ n_other
== d
+ 1)
1543 return call_on_simplex(cell
, simplex_ids
, n_simplex
,
1544 other_ids
, n_other
, fn
, user
);
1546 simplex_ids
[n_simplex
] = other_ids
[0];
1547 vertex
= cell
->vertices
->v
[other_ids
[0]].vertex
;
1548 bset
= cell
->vertices
->bset
;
1550 ids
= isl_alloc_array(ctx
, int, n_other
- 1);
1553 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
1554 if (isl_seq_first_non_zero(bset
->ineq
[i
] + 1 + nparam
, d
) == -1)
1556 if (vertex_on_facet(vertex
, bset
, i
, v
))
1559 for (j
= 1, k
= 0; j
< n_other
; ++j
) {
1561 ov
= cell
->vertices
->v
[other_ids
[j
]].vertex
;
1562 if (vertex_on_facet(ov
, bset
, i
, v
))
1563 ids
[k
++] = other_ids
[j
];
1568 if (triangulate(cell
, v
, simplex_ids
, n_simplex
+ 1,
1569 ids
, k
, fn
, user
) < 0)
1577 return isl_stat_error
;
1580 /* Triangulate the given cell and call "fn" on each of the resulting
1583 isl_stat
isl_cell_foreach_simplex(__isl_take isl_cell
*cell
,
1584 isl_stat (*fn
)(__isl_take isl_cell
*simplex
, void *user
), void *user
)
1590 int *simplex_ids
= NULL
;
1593 return isl_stat_error
;
1595 d
= isl_basic_set_dim(cell
->vertices
->bset
, isl_dim_set
);
1596 total
= isl_basic_set_dim(cell
->vertices
->bset
, isl_dim_all
);
1597 if (d
< 0 || total
< 0)
1598 return isl_stat_error
;
1600 if (cell
->n_vertices
== d
+ 1)
1601 return fn(cell
, user
);
1603 ctx
= isl_cell_get_ctx(cell
);
1604 simplex_ids
= isl_alloc_array(ctx
, int, d
+ 1);
1608 v
= isl_vec_alloc(ctx
, 1 + total
);
1612 r
= triangulate(cell
, v
, simplex_ids
, 0,
1613 cell
->ids
, cell
->n_vertices
, fn
, user
);
1618 isl_cell_free(cell
);
1624 isl_cell_free(cell
);
1625 return isl_stat_error
;