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_keep isl_basic_set
*bset
)
330 isl_vertices
*vertices
;
332 bset
= isl_basic_set_copy(bset
);
333 morph
= isl_basic_set_full_compression(bset
);
334 bset
= isl_morph_basic_set(isl_morph_copy(morph
), bset
);
336 vertices
= isl_basic_set_compute_vertices(bset
);
337 isl_basic_set_free(bset
);
339 morph
= isl_morph_inverse(morph
);
341 vertices
= isl_morph_vertices(morph
, vertices
);
346 /* Compute the parametric vertices and the chamber decomposition
347 * of the parametric polytope defined using the same constraints
348 * as "bset". "bset" is assumed to have no existentially quantified
351 * The vertices themselves are computed in a fairly simplistic way.
352 * We simply run through all combinations of d constraints,
353 * with d the number of set variables, and check if those d constraints
354 * define a vertex. To avoid the generation of duplicate vertices,
355 * which we may happen if a vertex is defined by more that d constraints,
356 * we make sure we only generate the vertex for the d constraints with
359 * We set up a tableau and keep track of which facets have been
360 * selected. The tableau is marked strict_redundant so that we can be
361 * sure that any constraint that is marked redundant (and that is not
362 * also marked zero) is not an equality.
363 * If a constraint is marked DESELECTED, it means the constraint was
364 * SELECTED before (in combination with the same selection of earlier
365 * constraints). If such a deselected constraint turns out to be an
366 * equality, then any vertex that may still be found with the current
367 * selection has already been generated when the constraint was selected.
368 * A constraint is marked UNSELECTED when there is no way selecting
369 * the constraint could lead to a vertex (in combination with the current
370 * selection of earlier constraints).
372 * The set variable coefficients of the selected constraints are stored
373 * in the facets matrix.
375 __isl_give isl_vertices
*isl_basic_set_compute_vertices(
376 __isl_keep isl_basic_set
*bset
)
382 int *selection
= NULL
;
384 struct isl_tab_undo
**snap
= NULL
;
385 isl_mat
*facets
= NULL
;
386 struct isl_vertex_list
*list
= NULL
;
388 isl_vertices
*vertices
;
393 if (isl_basic_set_plain_is_empty(bset
))
394 return vertices_empty(bset
);
397 return lower_dim_vertices(bset
);
399 if (isl_basic_set_check_no_locals(bset
) < 0)
402 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
406 return vertices_0D(bset
);
408 bset
= isl_basic_set_copy(bset
);
409 bset
= isl_basic_set_set_rational(bset
);
413 tab
= isl_tab_from_basic_set(bset
, 0);
416 tab
->strict_redundant
= 1;
419 vertices
= vertices_empty(bset
);
420 isl_basic_set_free(bset
);
425 selection
= isl_alloc_array(bset
->ctx
, int, bset
->n_ineq
);
426 snap
= isl_alloc_array(bset
->ctx
, struct isl_tab_undo
*, bset
->n_ineq
);
427 facets
= isl_mat_alloc(bset
->ctx
, nvar
, nvar
);
428 if ((bset
->n_ineq
&& (!selection
|| !snap
)) || !facets
)
436 if (level
>= bset
->n_ineq
||
437 (!init
&& selection
[level
] != SELECTED
)) {
444 snap
[level
] = isl_tab_snap(tab
);
445 ok
= can_select(bset
, level
, tab
, facets
, selected
,
450 selection
[level
] = SELECTED
;
453 selection
[level
] = UNSELECTED
;
455 selection
[level
] = DESELECTED
;
457 if (isl_tab_rollback(tab
, snap
[level
]) < 0)
460 if (selected
== nvar
) {
461 if (tab
->n_dead
== nvar
) {
462 isl_bool added
= add_vertex(&list
, bset
, tab
);
475 isl_mat_free(facets
);
481 vertices
= vertices_from_list(bset
, n_vertices
, list
);
483 vertices
= compute_chambers(bset
, vertices
);
487 free_vertex_list(list
);
488 isl_mat_free(facets
);
492 isl_basic_set_free(bset
);
496 struct isl_chamber_list
{
497 struct isl_chamber c
;
498 struct isl_chamber_list
*next
;
501 static void free_chamber_list(struct isl_chamber_list
*list
)
503 struct isl_chamber_list
*next
;
505 for (; list
; list
= next
) {
507 isl_basic_set_free(list
->c
.dom
);
508 free(list
->c
.vertices
);
513 /* Check whether the basic set "bset" is a superset of the basic set described
514 * by "tab", i.e., check whether all constraints of "bset" are redundant.
516 static isl_bool
bset_covers_tab(__isl_keep isl_basic_set
*bset
,
522 return isl_bool_error
;
524 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
525 enum isl_ineq_type type
= isl_tab_ineq_type(tab
, bset
->ineq
[i
]);
527 case isl_ineq_error
: return isl_bool_error
;
528 case isl_ineq_redundant
: continue;
529 default: return isl_bool_false
;
533 return isl_bool_true
;
536 static __isl_give isl_vertices
*vertices_add_chambers(
537 __isl_take isl_vertices
*vertices
, int n_chambers
,
538 struct isl_chamber_list
*list
)
542 struct isl_chamber_list
*next
;
544 ctx
= isl_vertices_get_ctx(vertices
);
545 vertices
->c
= isl_alloc_array(ctx
, struct isl_chamber
, n_chambers
);
548 vertices
->n_chambers
= n_chambers
;
550 for (i
= 0; list
; list
= next
, i
++) {
552 vertices
->c
[i
] = list
->c
;
558 isl_vertices_free(vertices
);
559 free_chamber_list(list
);
563 /* Can "tab" be intersected with "bset" without resulting in
564 * a lower-dimensional set.
565 * "bset" itself is assumed to be full-dimensional.
567 static isl_bool
can_intersect(struct isl_tab
*tab
,
568 __isl_keep isl_basic_set
*bset
)
571 struct isl_tab_undo
*snap
;
574 isl_die(isl_basic_set_get_ctx(bset
), isl_error_internal
,
575 "expecting full-dimensional input",
576 return isl_bool_error
);
578 if (isl_tab_extend_cons(tab
, bset
->n_ineq
) < 0)
579 return isl_bool_error
;
581 snap
= isl_tab_snap(tab
);
583 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
584 if (isl_tab_ineq_type(tab
, bset
->ineq
[i
]) == isl_ineq_redundant
)
586 if (isl_tab_add_ineq(tab
, bset
->ineq
[i
]) < 0)
587 return isl_bool_error
;
590 if (isl_tab_detect_implicit_equalities(tab
) < 0)
591 return isl_bool_error
;
593 if (isl_tab_rollback(tab
, snap
) < 0)
594 return isl_bool_error
;
595 return isl_bool_false
;
598 return isl_bool_true
;
601 static int add_chamber(struct isl_chamber_list
**list
,
602 __isl_keep isl_vertices
*vertices
, struct isl_tab
*tab
, int *selection
)
607 struct isl_tab_undo
*snap
;
608 struct isl_chamber_list
*c
= NULL
;
610 for (i
= 0; i
< vertices
->n_vertices
; ++i
)
614 snap
= isl_tab_snap(tab
);
616 for (i
= 0; i
< tab
->n_con
&& tab
->con
[i
].frozen
; ++i
)
617 tab
->con
[i
].frozen
= 0;
620 if (isl_tab_detect_redundant(tab
) < 0)
623 c
= isl_calloc_type(tab
->mat
->ctx
, struct isl_chamber_list
);
626 c
->c
.vertices
= isl_alloc_array(tab
->mat
->ctx
, int, n_vertices
);
627 if (n_vertices
&& !c
->c
.vertices
)
629 c
->c
.dom
= isl_basic_set_copy(isl_tab_peek_bset(tab
));
630 c
->c
.dom
= isl_basic_set_set_rational(c
->c
.dom
);
631 c
->c
.dom
= isl_basic_set_cow(c
->c
.dom
);
632 c
->c
.dom
= isl_basic_set_update_from_tab(c
->c
.dom
, tab
);
633 c
->c
.dom
= isl_basic_set_simplify(c
->c
.dom
);
634 c
->c
.dom
= isl_basic_set_finalize(c
->c
.dom
);
638 c
->c
.n_vertices
= n_vertices
;
640 for (i
= 0, j
= 0; i
< vertices
->n_vertices
; ++i
)
642 c
->c
.vertices
[j
] = i
;
649 for (i
= 0; i
< n_frozen
; ++i
)
650 tab
->con
[i
].frozen
= 1;
652 if (isl_tab_rollback(tab
, snap
) < 0)
657 free_chamber_list(c
);
661 struct isl_facet_todo
{
662 struct isl_tab
*tab
; /* A tableau representation of the facet */
663 isl_basic_set
*bset
; /* A normalized basic set representation */
664 isl_vec
*constraint
; /* Constraint pointing to the other side */
665 struct isl_facet_todo
*next
;
668 static void free_todo(struct isl_facet_todo
*todo
)
671 struct isl_facet_todo
*next
= todo
->next
;
673 isl_tab_free(todo
->tab
);
674 isl_basic_set_free(todo
->bset
);
675 isl_vec_free(todo
->constraint
);
682 static struct isl_facet_todo
*create_todo(struct isl_tab
*tab
, int con
)
686 struct isl_tab_undo
*snap
;
687 struct isl_facet_todo
*todo
;
689 snap
= isl_tab_snap(tab
);
691 for (i
= 0; i
< tab
->n_con
&& tab
->con
[i
].frozen
; ++i
)
692 tab
->con
[i
].frozen
= 0;
695 if (isl_tab_detect_redundant(tab
) < 0)
698 todo
= isl_calloc_type(tab
->mat
->ctx
, struct isl_facet_todo
);
702 todo
->constraint
= isl_vec_alloc(tab
->mat
->ctx
, 1 + tab
->n_var
);
703 if (!todo
->constraint
)
705 isl_seq_neg(todo
->constraint
->el
, tab
->bmap
->ineq
[con
], 1 + tab
->n_var
);
706 todo
->bset
= isl_basic_set_copy(isl_tab_peek_bset(tab
));
707 todo
->bset
= isl_basic_set_set_rational(todo
->bset
);
708 todo
->bset
= isl_basic_set_cow(todo
->bset
);
709 todo
->bset
= isl_basic_set_update_from_tab(todo
->bset
, tab
);
710 todo
->bset
= isl_basic_set_simplify(todo
->bset
);
711 todo
->bset
= isl_basic_set_sort_constraints(todo
->bset
);
714 ISL_F_SET(todo
->bset
, ISL_BASIC_SET_NO_REDUNDANT
);
715 todo
->tab
= isl_tab_dup(tab
);
719 for (i
= 0; i
< n_frozen
; ++i
)
720 tab
->con
[i
].frozen
= 1;
722 if (isl_tab_rollback(tab
, snap
) < 0)
731 /* Create todo items for all interior facets of the chamber represented
732 * by "tab" and collect them in "next".
734 static int init_todo(struct isl_facet_todo
**next
, struct isl_tab
*tab
)
737 struct isl_tab_undo
*snap
;
738 struct isl_facet_todo
*todo
;
740 snap
= isl_tab_snap(tab
);
742 for (i
= 0; i
< tab
->n_con
; ++i
) {
743 if (tab
->con
[i
].frozen
)
745 if (tab
->con
[i
].is_redundant
)
748 if (isl_tab_select_facet(tab
, i
) < 0)
751 todo
= create_todo(tab
, i
);
758 if (isl_tab_rollback(tab
, snap
) < 0)
765 /* Does the linked list contain a todo item that is the opposite of "todo".
766 * If so, return 1 and remove the opposite todo item.
768 static int has_opposite(struct isl_facet_todo
*todo
,
769 struct isl_facet_todo
**list
)
771 for (; *list
; list
= &(*list
)->next
) {
773 eq
= isl_basic_set_plain_is_equal(todo
->bset
, (*list
)->bset
);
788 /* Create todo items for all interior facets of the chamber represented
789 * by "tab" and collect them in first->next, taking care to cancel
790 * opposite todo items.
792 static int update_todo(struct isl_facet_todo
*first
, struct isl_tab
*tab
)
795 struct isl_tab_undo
*snap
;
796 struct isl_facet_todo
*todo
;
798 snap
= isl_tab_snap(tab
);
800 for (i
= 0; i
< tab
->n_con
; ++i
) {
803 if (tab
->con
[i
].frozen
)
805 if (tab
->con
[i
].is_redundant
)
808 if (isl_tab_select_facet(tab
, i
) < 0)
811 todo
= create_todo(tab
, i
);
815 drop
= has_opposite(todo
, &first
->next
);
822 todo
->next
= first
->next
;
826 if (isl_tab_rollback(tab
, snap
) < 0)
833 /* Compute the chamber decomposition of the parametric polytope respresented
834 * by "bset" given the parametric vertices and their activity domains.
836 * We are only interested in full-dimensional chambers.
837 * Each of these chambers is the intersection of the activity domains of
838 * one or more vertices and the union of all chambers is equal to the
839 * projection of the entire parametric polytope onto the parameter space.
841 * We first create an initial chamber by intersecting as many activity
842 * domains as possible without ending up with an empty or lower-dimensional
843 * set. As a minor optimization, we only consider those activity domains
844 * that contain some arbitrary point.
846 * For each of the interior facets of the chamber, we construct a todo item,
847 * containing the facet and a constraint containing the other side of the facet,
848 * for constructing the chamber on the other side.
849 * While their are any todo items left, we pick a todo item and
850 * create the required chamber by intersecting all activity domains
851 * that contain the facet and have a full-dimensional intersection with
852 * the other side of the facet. For each of the interior facets, we
853 * again create todo items, taking care to cancel opposite todo items.
855 static __isl_give isl_vertices
*compute_chambers(__isl_take isl_basic_set
*bset
,
856 __isl_take isl_vertices
*vertices
)
860 isl_vec
*sample
= NULL
;
861 struct isl_tab
*tab
= NULL
;
862 struct isl_tab_undo
*snap
;
863 int *selection
= NULL
;
865 struct isl_chamber_list
*list
= NULL
;
866 struct isl_facet_todo
*todo
= NULL
;
868 if (!bset
|| !vertices
)
871 ctx
= isl_vertices_get_ctx(vertices
);
872 selection
= isl_alloc_array(ctx
, int, vertices
->n_vertices
);
873 if (vertices
->n_vertices
&& !selection
)
876 bset
= isl_basic_set_params(bset
);
878 tab
= isl_tab_from_basic_set(bset
, 1);
881 for (i
= 0; i
< bset
->n_ineq
; ++i
)
882 if (isl_tab_freeze_constraint(tab
, i
) < 0)
884 isl_basic_set_free(bset
);
886 snap
= isl_tab_snap(tab
);
888 sample
= isl_tab_get_sample_value(tab
);
890 for (i
= 0; i
< vertices
->n_vertices
; ++i
) {
891 selection
[i
] = isl_basic_set_contains(vertices
->v
[i
].dom
, sample
);
892 if (selection
[i
] < 0)
896 selection
[i
] = can_intersect(tab
, vertices
->v
[i
].dom
);
897 if (selection
[i
] < 0)
901 if (isl_tab_detect_redundant(tab
) < 0)
904 if (add_chamber(&list
, vertices
, tab
, selection
) < 0)
908 if (init_todo(&todo
, tab
) < 0)
912 struct isl_facet_todo
*next
;
914 if (isl_tab_rollback(tab
, snap
) < 0)
917 if (isl_tab_add_ineq(tab
, todo
->constraint
->el
) < 0)
919 if (isl_tab_freeze_constraint(tab
, tab
->n_con
- 1) < 0)
922 for (i
= 0; i
< vertices
->n_vertices
; ++i
) {
923 selection
[i
] = bset_covers_tab(vertices
->v
[i
].dom
,
925 if (selection
[i
] < 0)
929 selection
[i
] = can_intersect(tab
, vertices
->v
[i
].dom
);
930 if (selection
[i
] < 0)
934 if (isl_tab_detect_redundant(tab
) < 0)
937 if (add_chamber(&list
, vertices
, tab
, selection
) < 0)
941 if (update_todo(todo
, tab
) < 0)
950 isl_vec_free(sample
);
955 vertices
= vertices_add_chambers(vertices
, n_chambers
, list
);
957 for (i
= 0; vertices
&& i
< vertices
->n_vertices
; ++i
) {
958 isl_basic_set_free(vertices
->v
[i
].dom
);
959 vertices
->v
[i
].dom
= NULL
;
964 free_chamber_list(list
);
966 isl_vec_free(sample
);
970 isl_basic_set_free(bset
);
971 isl_vertices_free(vertices
);
975 isl_ctx
*isl_vertex_get_ctx(__isl_keep isl_vertex
*vertex
)
977 return vertex
? isl_vertices_get_ctx(vertex
->vertices
) : NULL
;
980 isl_size
isl_vertex_get_id(__isl_keep isl_vertex
*vertex
)
982 return vertex
? vertex
->id
: isl_size_error
;
985 /* Return the activity domain of the vertex "vertex".
987 __isl_give isl_basic_set
*isl_vertex_get_domain(__isl_keep isl_vertex
*vertex
)
989 struct isl_vertex
*v
;
994 v
= &vertex
->vertices
->v
[vertex
->id
];
996 v
->dom
= isl_basic_set_copy(v
->vertex
);
997 v
->dom
= isl_basic_set_params(v
->dom
);
998 v
->dom
= isl_basic_set_set_integral(v
->dom
);
1001 return isl_basic_set_copy(v
->dom
);
1004 /* Return a multiple quasi-affine expression describing the vertex "vertex"
1005 * in terms of the parameters,
1007 __isl_give isl_multi_aff
*isl_vertex_get_expr(__isl_keep isl_vertex
*vertex
)
1009 struct isl_vertex
*v
;
1010 isl_basic_set
*bset
;
1015 v
= &vertex
->vertices
->v
[vertex
->id
];
1017 bset
= isl_basic_set_copy(v
->vertex
);
1018 return isl_multi_aff_from_basic_set_equalities(bset
);
1021 static __isl_give isl_vertex
*isl_vertex_alloc(__isl_take isl_vertices
*vertices
,
1030 ctx
= isl_vertices_get_ctx(vertices
);
1031 vertex
= isl_alloc_type(ctx
, isl_vertex
);
1035 vertex
->vertices
= vertices
;
1040 isl_vertices_free(vertices
);
1044 __isl_null isl_vertex
*isl_vertex_free(__isl_take isl_vertex
*vertex
)
1048 isl_vertices_free(vertex
->vertices
);
1054 isl_ctx
*isl_cell_get_ctx(__isl_keep isl_cell
*cell
)
1056 return cell
? cell
->dom
->ctx
: NULL
;
1059 __isl_give isl_basic_set
*isl_cell_get_domain(__isl_keep isl_cell
*cell
)
1061 return cell
? isl_basic_set_copy(cell
->dom
) : NULL
;
1064 static __isl_give isl_cell
*isl_cell_alloc(__isl_take isl_vertices
*vertices
,
1065 __isl_take isl_basic_set
*dom
, int id
)
1068 isl_cell
*cell
= NULL
;
1070 if (!vertices
|| !dom
)
1073 cell
= isl_calloc_type(dom
->ctx
, isl_cell
);
1077 cell
->n_vertices
= vertices
->c
[id
].n_vertices
;
1078 cell
->ids
= isl_alloc_array(dom
->ctx
, int, cell
->n_vertices
);
1079 if (cell
->n_vertices
&& !cell
->ids
)
1081 for (i
= 0; i
< cell
->n_vertices
; ++i
)
1082 cell
->ids
[i
] = vertices
->c
[id
].vertices
[i
];
1083 cell
->vertices
= vertices
;
1088 isl_cell_free(cell
);
1089 isl_vertices_free(vertices
);
1090 isl_basic_set_free(dom
);
1094 __isl_null isl_cell
*isl_cell_free(__isl_take isl_cell
*cell
)
1099 isl_vertices_free(cell
->vertices
);
1101 isl_basic_set_free(cell
->dom
);
1107 /* Create a tableau of the cone obtained by first homogenizing the given
1108 * polytope and then making all inequalities strict by setting the
1109 * constant term to -1.
1111 static struct isl_tab
*tab_for_shifted_cone(__isl_keep isl_basic_set
*bset
)
1115 struct isl_tab
*tab
;
1118 total
= isl_basic_set_dim(bset
, isl_dim_all
);
1121 tab
= isl_tab_alloc(bset
->ctx
, bset
->n_eq
+ bset
->n_ineq
+ 1,
1125 tab
->rational
= ISL_F_ISSET(bset
, ISL_BASIC_SET_RATIONAL
);
1126 if (ISL_F_ISSET(bset
, ISL_BASIC_MAP_EMPTY
)) {
1127 if (isl_tab_mark_empty(tab
) < 0)
1132 c
= isl_vec_alloc(bset
->ctx
, 1 + 1 + total
);
1136 isl_int_set_si(c
->el
[0], 0);
1137 for (i
= 0; i
< bset
->n_eq
; ++i
) {
1138 isl_seq_cpy(c
->el
+ 1, bset
->eq
[i
], c
->size
- 1);
1139 if (isl_tab_add_eq(tab
, c
->el
) < 0)
1143 isl_int_set_si(c
->el
[0], -1);
1144 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
1145 isl_seq_cpy(c
->el
+ 1, bset
->ineq
[i
], c
->size
- 1);
1146 if (isl_tab_add_ineq(tab
, c
->el
) < 0)
1154 isl_seq_clr(c
->el
+ 1, c
->size
- 1);
1155 isl_int_set_si(c
->el
[1], 1);
1156 if (isl_tab_add_ineq(tab
, c
->el
) < 0)
1167 /* Compute an interior point of "bset" by selecting an interior
1168 * point in homogeneous space and projecting the point back down.
1170 static __isl_give isl_vec
*isl_basic_set_interior_point(
1171 __isl_keep isl_basic_set
*bset
)
1174 struct isl_tab
*tab
;
1176 tab
= tab_for_shifted_cone(bset
);
1177 vec
= isl_tab_get_sample_value(tab
);
1182 isl_seq_cpy(vec
->el
, vec
->el
+ 1, vec
->size
- 1);
1188 /* Call "fn" on all chambers of the parametric polytope with the shared
1189 * facets of neighboring chambers only appearing in one of the chambers.
1191 * We pick an interior point from one of the chambers and then make
1192 * all constraints that do not satisfy this point strict.
1193 * For constraints that saturate the interior point, the sign
1194 * of the first non-zero coefficient is used to determine which
1195 * of the two (internal) constraints should be tightened.
1197 isl_stat
isl_vertices_foreach_disjoint_cell(__isl_keep isl_vertices
*vertices
,
1198 isl_stat (*fn
)(__isl_take isl_cell
*cell
, void *user
), void *user
)
1205 return isl_stat_error
;
1207 if (vertices
->n_chambers
== 0)
1210 if (vertices
->n_chambers
== 1) {
1211 isl_basic_set
*dom
= isl_basic_set_copy(vertices
->c
[0].dom
);
1212 dom
= isl_basic_set_set_integral(dom
);
1213 cell
= isl_cell_alloc(isl_vertices_copy(vertices
), dom
, 0);
1215 return isl_stat_error
;
1216 return fn(cell
, user
);
1219 vec
= isl_basic_set_interior_point(vertices
->c
[0].dom
);
1221 return isl_stat_error
;
1223 for (i
= 0; i
< vertices
->n_chambers
; ++i
) {
1225 isl_basic_set
*dom
= isl_basic_set_copy(vertices
->c
[i
].dom
);
1227 dom
= isl_basic_set_tighten_outward(dom
, vec
);
1228 dom
= isl_basic_set_set_integral(dom
);
1229 cell
= isl_cell_alloc(isl_vertices_copy(vertices
), dom
, i
);
1242 return isl_stat_error
;
1245 isl_stat
isl_vertices_foreach_cell(__isl_keep isl_vertices
*vertices
,
1246 isl_stat (*fn
)(__isl_take isl_cell
*cell
, void *user
), void *user
)
1252 return isl_stat_error
;
1254 if (vertices
->n_chambers
== 0)
1257 for (i
= 0; i
< vertices
->n_chambers
; ++i
) {
1259 isl_basic_set
*dom
= isl_basic_set_copy(vertices
->c
[i
].dom
);
1261 cell
= isl_cell_alloc(isl_vertices_copy(vertices
), dom
, i
);
1263 return isl_stat_error
;
1267 return isl_stat_error
;
1273 isl_stat
isl_vertices_foreach_vertex(__isl_keep isl_vertices
*vertices
,
1274 isl_stat (*fn
)(__isl_take isl_vertex
*vertex
, void *user
), void *user
)
1280 return isl_stat_error
;
1282 if (vertices
->n_vertices
== 0)
1285 for (i
= 0; i
< vertices
->n_vertices
; ++i
) {
1288 vertex
= isl_vertex_alloc(isl_vertices_copy(vertices
), i
);
1290 return isl_stat_error
;
1292 r
= fn(vertex
, user
);
1294 return isl_stat_error
;
1300 isl_stat
isl_cell_foreach_vertex(__isl_keep isl_cell
*cell
,
1301 isl_stat (*fn
)(__isl_take isl_vertex
*vertex
, void *user
), void *user
)
1307 return isl_stat_error
;
1309 if (cell
->n_vertices
== 0)
1312 for (i
= 0; i
< cell
->n_vertices
; ++i
) {
1315 vertex
= isl_vertex_alloc(isl_vertices_copy(cell
->vertices
),
1318 return isl_stat_error
;
1320 r
= fn(vertex
, user
);
1322 return isl_stat_error
;
1328 isl_ctx
*isl_vertices_get_ctx(__isl_keep isl_vertices
*vertices
)
1330 return vertices
? vertices
->bset
->ctx
: NULL
;
1333 isl_size
isl_vertices_get_n_vertices(__isl_keep isl_vertices
*vertices
)
1335 return vertices
? vertices
->n_vertices
: isl_size_error
;
1338 __isl_give isl_vertices
*isl_morph_vertices(__isl_take isl_morph
*morph
,
1339 __isl_take isl_vertices
*vertices
)
1342 isl_morph
*param_morph
= NULL
;
1344 if (!morph
|| !vertices
)
1347 isl_assert(vertices
->bset
->ctx
, vertices
->ref
== 1, goto error
);
1349 param_morph
= isl_morph_copy(morph
);
1350 param_morph
= isl_morph_dom_params(param_morph
);
1351 param_morph
= isl_morph_ran_params(param_morph
);
1353 for (i
= 0; i
< vertices
->n_vertices
; ++i
) {
1354 vertices
->v
[i
].dom
= isl_morph_basic_set(
1355 isl_morph_copy(param_morph
), vertices
->v
[i
].dom
);
1356 vertices
->v
[i
].vertex
= isl_morph_basic_set(
1357 isl_morph_copy(morph
), vertices
->v
[i
].vertex
);
1358 if (!vertices
->v
[i
].vertex
)
1362 for (i
= 0; i
< vertices
->n_chambers
; ++i
) {
1363 vertices
->c
[i
].dom
= isl_morph_basic_set(
1364 isl_morph_copy(param_morph
), vertices
->c
[i
].dom
);
1365 if (!vertices
->c
[i
].dom
)
1369 isl_morph_free(param_morph
);
1370 isl_morph_free(morph
);
1373 isl_morph_free(param_morph
);
1374 isl_morph_free(morph
);
1375 isl_vertices_free(vertices
);
1379 /* Construct a simplex isl_cell spanned by the vertices with indices in
1380 * "simplex_ids" and "other_ids" and call "fn" on this isl_cell.
1382 static isl_stat
call_on_simplex(__isl_keep isl_cell
*cell
,
1383 int *simplex_ids
, int n_simplex
, int *other_ids
, int n_other
,
1384 isl_stat (*fn
)(__isl_take isl_cell
*simplex
, void *user
), void *user
)
1388 struct isl_cell
*simplex
;
1390 ctx
= isl_cell_get_ctx(cell
);
1392 simplex
= isl_calloc_type(ctx
, struct isl_cell
);
1394 return isl_stat_error
;
1395 simplex
->vertices
= isl_vertices_copy(cell
->vertices
);
1396 if (!simplex
->vertices
)
1398 simplex
->dom
= isl_basic_set_copy(cell
->dom
);
1401 simplex
->n_vertices
= n_simplex
+ n_other
;
1402 simplex
->ids
= isl_alloc_array(ctx
, int, simplex
->n_vertices
);
1406 for (i
= 0; i
< n_simplex
; ++i
)
1407 simplex
->ids
[i
] = simplex_ids
[i
];
1408 for (i
= 0; i
< n_other
; ++i
)
1409 simplex
->ids
[n_simplex
+ i
] = other_ids
[i
];
1411 return fn(simplex
, user
);
1413 isl_cell_free(simplex
);
1414 return isl_stat_error
;
1417 /* Check whether the parametric vertex described by "vertex"
1418 * lies on the facet corresponding to constraint "facet" of "bset".
1419 * The isl_vec "v" is a temporary vector than can be used by this function.
1421 * We eliminate the variables from the facet constraint using the
1422 * equalities defining the vertex and check if the result is identical
1425 * It would probably be better to keep track of the constraints defining
1426 * a vertex during the vertex construction so that we could simply look
1429 static int vertex_on_facet(__isl_keep isl_basic_set
*vertex
,
1430 __isl_keep isl_basic_set
*bset
, int facet
, __isl_keep isl_vec
*v
)
1435 isl_seq_cpy(v
->el
, bset
->ineq
[facet
], v
->size
);
1438 for (i
= 0; i
< vertex
->n_eq
; ++i
) {
1439 int k
= isl_seq_last_non_zero(vertex
->eq
[i
], v
->size
);
1440 isl_seq_elim(v
->el
, vertex
->eq
[i
], k
, v
->size
, &m
);
1444 return isl_seq_first_non_zero(v
->el
, v
->size
) == -1;
1447 /* Triangulate the polytope spanned by the vertices with ids
1448 * in "simplex_ids" and "other_ids" and call "fn" on each of
1449 * the resulting simplices.
1450 * If the input polytope is already a simplex, we simply call "fn".
1451 * Otherwise, we pick a point from "other_ids" and add it to "simplex_ids".
1452 * Then we consider each facet of "bset" that does not contain the point
1453 * we just picked, but does contain some of the other points in "other_ids"
1454 * and call ourselves recursively on the polytope spanned by the new
1455 * "simplex_ids" and those points in "other_ids" that lie on the facet.
1457 static isl_stat
triangulate(__isl_keep isl_cell
*cell
, __isl_keep isl_vec
*v
,
1458 int *simplex_ids
, int n_simplex
, int *other_ids
, int n_other
,
1459 isl_stat (*fn
)(__isl_take isl_cell
*simplex
, void *user
), void *user
)
1465 isl_basic_set
*vertex
;
1466 isl_basic_set
*bset
;
1468 ctx
= isl_cell_get_ctx(cell
);
1469 d
= isl_basic_set_dim(cell
->vertices
->bset
, isl_dim_set
);
1470 nparam
= isl_basic_set_dim(cell
->vertices
->bset
, isl_dim_param
);
1471 if (d
< 0 || nparam
< 0)
1472 return isl_stat_error
;
1474 if (n_simplex
+ n_other
== d
+ 1)
1475 return call_on_simplex(cell
, simplex_ids
, n_simplex
,
1476 other_ids
, n_other
, fn
, user
);
1478 simplex_ids
[n_simplex
] = other_ids
[0];
1479 vertex
= cell
->vertices
->v
[other_ids
[0]].vertex
;
1480 bset
= cell
->vertices
->bset
;
1482 ids
= isl_alloc_array(ctx
, int, n_other
- 1);
1485 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
1486 if (isl_seq_first_non_zero(bset
->ineq
[i
] + 1 + nparam
, d
) == -1)
1488 if (vertex_on_facet(vertex
, bset
, i
, v
))
1491 for (j
= 1, k
= 0; j
< n_other
; ++j
) {
1493 ov
= cell
->vertices
->v
[other_ids
[j
]].vertex
;
1494 if (vertex_on_facet(ov
, bset
, i
, v
))
1495 ids
[k
++] = other_ids
[j
];
1500 if (triangulate(cell
, v
, simplex_ids
, n_simplex
+ 1,
1501 ids
, k
, fn
, user
) < 0)
1509 return isl_stat_error
;
1512 /* Triangulate the given cell and call "fn" on each of the resulting
1515 isl_stat
isl_cell_foreach_simplex(__isl_take isl_cell
*cell
,
1516 isl_stat (*fn
)(__isl_take isl_cell
*simplex
, void *user
), void *user
)
1522 int *simplex_ids
= NULL
;
1525 return isl_stat_error
;
1527 d
= isl_basic_set_dim(cell
->vertices
->bset
, isl_dim_set
);
1528 total
= isl_basic_set_dim(cell
->vertices
->bset
, isl_dim_all
);
1529 if (d
< 0 || total
< 0)
1530 return isl_stat_error
;
1532 if (cell
->n_vertices
== d
+ 1)
1533 return fn(cell
, user
);
1535 ctx
= isl_cell_get_ctx(cell
);
1536 simplex_ids
= isl_alloc_array(ctx
, int, d
+ 1);
1540 v
= isl_vec_alloc(ctx
, 1 + total
);
1544 r
= triangulate(cell
, v
, simplex_ids
, 0,
1545 cell
->ids
, cell
->n_vertices
, fn
, user
);
1550 isl_cell_free(cell
);
1556 isl_cell_free(cell
);
1557 return isl_stat_error
;