drop deprecated isl_map_n_in
[isl.git] / isl_vertices.c
blobe2d208497e79b46cf3ae0086ae7d7753f078b5e4
1 /*
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,
8 * 91893 Orsay, France
9 */
11 #include <isl_map_private.h>
12 #include <isl_aff_private.h>
13 #include <isl/set.h>
14 #include <isl_seq.h>
15 #include <isl_tab.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>
22 #define SELECTED 1
23 #define DESELECTED -1
24 #define UNSELECTED 0
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)
31 if (!vertices)
32 return NULL;
34 vertices->ref++;
35 return vertices;
38 __isl_null isl_vertices *isl_vertices_free(__isl_take isl_vertices *vertices)
40 int i;
42 if (!vertices)
43 return NULL;
45 if (--vertices->ref > 0)
46 return NULL;
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);
52 free(vertices->v);
54 for (i = 0; i < vertices->n_chambers; ++i) {
55 free(vertices->c[i].vertices);
56 isl_basic_set_free(vertices->c[i].dom);
58 free(vertices->c);
60 isl_basic_set_free(vertices->bset);
61 free(vertices);
63 return NULL;
66 struct isl_vertex_list {
67 struct isl_vertex v;
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) {
76 next = list->next;
77 isl_basic_set_free(list->v.vertex);
78 isl_basic_set_free(list->v.dom);
79 free(list);
82 return NULL;
85 static __isl_give isl_vertices *vertices_from_list(__isl_keep isl_basic_set *bset,
86 int n_vertices, struct isl_vertex_list *list)
88 int i;
89 struct isl_vertex_list *next;
90 isl_vertices *vertices;
92 vertices = isl_calloc_type(bset->ctx, isl_vertices);
93 if (!vertices)
94 goto error;
95 vertices->ref = 1;
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)
99 goto error;
100 vertices->n_vertices = n_vertices;
102 for (i = 0; list; list = next, i++) {
103 next = list->next;
104 vertices->v[i] = list->v;
105 free(list);
108 return vertices;
109 error:
110 isl_vertices_free(vertices);
111 free_vertex_list(list);
112 return NULL;
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)
125 unsigned nvar;
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 v = isl_calloc_type(tab->mat->ctx, struct isl_vertex_list);
134 if (!v)
135 goto error;
137 v->v.vertex = isl_basic_set_copy(bset);
138 v->v.vertex = isl_basic_set_cow(v->v.vertex);
139 v->v.vertex = isl_basic_set_update_from_tab(v->v.vertex, tab);
140 v->v.vertex = isl_basic_set_simplify(v->v.vertex);
141 v->v.vertex = isl_basic_set_finalize(v->v.vertex);
142 if (!v->v.vertex)
143 goto error;
144 isl_assert(bset->ctx, v->v.vertex->n_eq >= nvar, goto error);
145 v->v.dom = isl_basic_set_copy(v->v.vertex);
146 v->v.dom = isl_basic_set_params(v->v.dom);
147 if (!v->v.dom)
148 goto error;
150 if (v->v.dom->n_eq > 0) {
151 free_vertex_list(v);
152 return isl_bool_false;
155 v->next = *list;
156 *list = v;
158 return isl_bool_true;
159 error:
160 free_vertex_list(v);
161 return isl_bool_error;
164 /* Compute the parametric vertices and the chamber decomposition
165 * of an empty parametric polytope.
167 static __isl_give isl_vertices *vertices_empty(__isl_keep isl_basic_set *bset)
169 isl_vertices *vertices;
171 if (!bset)
172 return NULL;
174 vertices = isl_calloc_type(bset->ctx, isl_vertices);
175 if (!vertices)
176 return NULL;
177 vertices->bset = isl_basic_set_copy(bset);
178 vertices->ref = 1;
180 vertices->n_vertices = 0;
181 vertices->n_chambers = 0;
183 return vertices;
186 /* Compute the parametric vertices and the chamber decomposition
187 * of the parametric polytope defined using the same constraints
188 * as "bset" in the 0D case.
189 * There is exactly one 0D vertex and a single chamber containing
190 * the vertex.
192 static __isl_give isl_vertices *vertices_0D(__isl_keep isl_basic_set *bset)
194 isl_vertices *vertices;
196 if (!bset)
197 return NULL;
199 vertices = isl_calloc_type(bset->ctx, isl_vertices);
200 if (!vertices)
201 return NULL;
202 vertices->ref = 1;
203 vertices->bset = isl_basic_set_copy(bset);
205 vertices->v = isl_calloc_array(bset->ctx, struct isl_vertex, 1);
206 if (!vertices->v)
207 goto error;
208 vertices->n_vertices = 1;
209 vertices->v[0].vertex = isl_basic_set_copy(bset);
210 vertices->v[0].dom = isl_basic_set_params(isl_basic_set_copy(bset));
211 if (!vertices->v[0].vertex || !vertices->v[0].dom)
212 goto error;
214 vertices->c = isl_calloc_array(bset->ctx, struct isl_chamber, 1);
215 if (!vertices->c)
216 goto error;
217 vertices->n_chambers = 1;
218 vertices->c[0].n_vertices = 1;
219 vertices->c[0].vertices = isl_calloc_array(bset->ctx, int, 1);
220 if (!vertices->c[0].vertices)
221 goto error;
222 vertices->c[0].dom = isl_basic_set_copy(vertices->v[0].dom);
223 if (!vertices->c[0].dom)
224 goto error;
226 return vertices;
227 error:
228 isl_vertices_free(vertices);
229 return NULL;
232 /* Is the row pointed to by "f" linearly independent of the "n" first
233 * rows in "facets"?
235 static isl_bool is_independent(__isl_keep isl_mat *facets, int n, isl_int *f)
237 int rank;
239 if (isl_seq_first_non_zero(f, facets->n_col) < 0)
240 return isl_bool_false;
242 isl_seq_cpy(facets->row[n], f, facets->n_col);
243 facets->n_row = n + 1;
244 rank = isl_mat_rank(facets);
245 if (rank < 0)
246 return isl_bool_error;
248 return rank == n + 1;
251 /* Check whether we can select constraint "level", given the current selection
252 * reflected by facets in "tab", the rows of "facets" and the earlier
253 * "selected" elements of "selection".
255 * If the constraint is (strictly) redundant in the tableau, selecting it would
256 * result in an empty tableau, so it can't be selected.
257 * If the set variable part of the constraint is not linearly independent
258 * of the set variable parts of the already selected constraints,
259 * the constraint cannot be selected.
260 * If selecting the constraint results in an empty tableau, the constraint
261 * cannot be selected.
262 * Finally, if selecting the constraint results in some explicitly
263 * deselected constraints turning into equalities, then the corresponding
264 * vertices have already been generated, so the constraint cannot be selected.
266 static isl_bool can_select(__isl_keep isl_basic_set *bset, int level,
267 struct isl_tab *tab, __isl_keep isl_mat *facets, int selected,
268 int *selection)
270 int i;
271 isl_bool indep;
272 unsigned ovar;
273 struct isl_tab_undo *snap;
275 if (isl_tab_is_redundant(tab, level))
276 return isl_bool_false;
278 ovar = isl_space_offset(bset->dim, isl_dim_set);
280 indep = is_independent(facets, selected, bset->ineq[level] + 1 + ovar);
281 if (indep < 0 || !indep)
282 return indep;
284 snap = isl_tab_snap(tab);
285 if (isl_tab_select_facet(tab, level) < 0)
286 return isl_bool_error;
288 if (tab->empty) {
289 if (isl_tab_rollback(tab, snap) < 0)
290 return isl_bool_error;
291 return isl_bool_false;
294 for (i = 0; i < level; ++i) {
295 int sgn;
297 if (selection[i] != DESELECTED)
298 continue;
300 if (isl_tab_is_equality(tab, i))
301 sgn = 0;
302 else if (isl_tab_is_redundant(tab, i))
303 sgn = 1;
304 else
305 sgn = isl_tab_sign_of_max(tab, i);
306 if (sgn < -1)
307 return isl_bool_error;
308 if (sgn <= 0) {
309 if (isl_tab_rollback(tab, snap) < 0)
310 return isl_bool_error;
311 return isl_bool_false;
315 return isl_bool_true;
318 /* Compute the parametric vertices and the chamber decomposition
319 * of a parametric polytope that is not full-dimensional.
321 * Simply map the parametric polytope to a lower dimensional space
322 * and map the resulting vertices back.
324 static __isl_give isl_vertices *lower_dim_vertices(
325 __isl_keep isl_basic_set *bset)
327 isl_morph *morph;
328 isl_vertices *vertices;
330 bset = isl_basic_set_copy(bset);
331 morph = isl_basic_set_full_compression(bset);
332 bset = isl_morph_basic_set(isl_morph_copy(morph), bset);
334 vertices = isl_basic_set_compute_vertices(bset);
335 isl_basic_set_free(bset);
337 morph = isl_morph_inverse(morph);
339 vertices = isl_morph_vertices(morph, vertices);
341 return vertices;
344 /* Compute the parametric vertices and the chamber decomposition
345 * of the parametric polytope defined using the same constraints
346 * as "bset". "bset" is assumed to have no existentially quantified
347 * variables.
349 * The vertices themselves are computed in a fairly simplistic way.
350 * We simply run through all combinations of d constraints,
351 * with d the number of set variables, and check if those d constraints
352 * define a vertex. To avoid the generation of duplicate vertices,
353 * which we may happen if a vertex is defined by more that d constraints,
354 * we make sure we only generate the vertex for the d constraints with
355 * smallest index.
357 * We set up a tableau and keep track of which facets have been
358 * selected. The tableau is marked strict_redundant so that we can be
359 * sure that any constraint that is marked redundant (and that is not
360 * also marked zero) is not an equality.
361 * If a constraint is marked DESELECTED, it means the constraint was
362 * SELECTED before (in combination with the same selection of earlier
363 * constraints). If such a deselected constraint turns out to be an
364 * equality, then any vertex that may still be found with the current
365 * selection has already been generated when the constraint was selected.
366 * A constraint is marked UNSELECTED when there is no way selecting
367 * the constraint could lead to a vertex (in combination with the current
368 * selection of earlier constraints).
370 * The set variable coefficients of the selected constraints are stored
371 * in the facets matrix.
373 __isl_give isl_vertices *isl_basic_set_compute_vertices(
374 __isl_keep isl_basic_set *bset)
376 struct isl_tab *tab;
377 int level;
378 int init;
379 unsigned nvar;
380 int *selection = NULL;
381 int selected;
382 struct isl_tab_undo **snap = NULL;
383 isl_mat *facets = NULL;
384 struct isl_vertex_list *list = NULL;
385 int n_vertices = 0;
386 isl_vertices *vertices;
388 if (!bset)
389 return NULL;
391 if (isl_basic_set_plain_is_empty(bset))
392 return vertices_empty(bset);
394 if (bset->n_eq != 0)
395 return lower_dim_vertices(bset);
397 if (isl_basic_set_check_no_locals(bset) < 0)
398 return NULL;
400 if (isl_basic_set_dim(bset, isl_dim_set) == 0)
401 return vertices_0D(bset);
403 nvar = isl_basic_set_dim(bset, isl_dim_set);
405 bset = isl_basic_set_copy(bset);
406 bset = isl_basic_set_set_rational(bset);
407 if (!bset)
408 return NULL;
410 tab = isl_tab_from_basic_set(bset, 0);
411 if (!tab)
412 goto error;
413 tab->strict_redundant = 1;
415 if (tab->empty) {
416 vertices = vertices_empty(bset);
417 isl_basic_set_free(bset);
418 isl_tab_free(tab);
419 return vertices;
422 selection = isl_alloc_array(bset->ctx, int, bset->n_ineq);
423 snap = isl_alloc_array(bset->ctx, struct isl_tab_undo *, bset->n_ineq);
424 facets = isl_mat_alloc(bset->ctx, nvar, nvar);
425 if ((bset->n_ineq && (!selection || !snap)) || !facets)
426 goto error;
428 level = 0;
429 init = 1;
430 selected = 0;
432 while (level >= 0) {
433 if (level >= bset->n_ineq ||
434 (!init && selection[level] != SELECTED)) {
435 --level;
436 init = 0;
437 continue;
439 if (init) {
440 isl_bool ok;
441 snap[level] = isl_tab_snap(tab);
442 ok = can_select(bset, level, tab, facets, selected,
443 selection);
444 if (ok < 0)
445 goto error;
446 if (ok) {
447 selection[level] = SELECTED;
448 selected++;
449 } else
450 selection[level] = UNSELECTED;
451 } else {
452 selection[level] = DESELECTED;
453 selected--;
454 if (isl_tab_rollback(tab, snap[level]) < 0)
455 goto error;
457 if (selected == nvar) {
458 if (tab->n_dead == nvar) {
459 isl_bool added = add_vertex(&list, bset, tab);
460 if (added < 0)
461 goto error;
462 if (added)
463 n_vertices++;
465 init = 0;
466 continue;
468 ++level;
469 init = 1;
472 isl_mat_free(facets);
473 free(selection);
474 free(snap);
476 isl_tab_free(tab);
478 vertices = vertices_from_list(bset, n_vertices, list);
480 vertices = compute_chambers(bset, vertices);
482 return vertices;
483 error:
484 free_vertex_list(list);
485 isl_mat_free(facets);
486 free(selection);
487 free(snap);
488 isl_tab_free(tab);
489 isl_basic_set_free(bset);
490 return NULL;
493 struct isl_chamber_list {
494 struct isl_chamber c;
495 struct isl_chamber_list *next;
498 static void free_chamber_list(struct isl_chamber_list *list)
500 struct isl_chamber_list *next;
502 for (; list; list = next) {
503 next = list->next;
504 isl_basic_set_free(list->c.dom);
505 free(list->c.vertices);
506 free(list);
510 /* Check whether the basic set "bset" is a superset of the basic set described
511 * by "tab", i.e., check whether all constraints of "bset" are redundant.
513 static isl_bool bset_covers_tab(__isl_keep isl_basic_set *bset,
514 struct isl_tab *tab)
516 int i;
518 if (!bset || !tab)
519 return isl_bool_error;
521 for (i = 0; i < bset->n_ineq; ++i) {
522 enum isl_ineq_type type = isl_tab_ineq_type(tab, bset->ineq[i]);
523 switch (type) {
524 case isl_ineq_error: return isl_bool_error;
525 case isl_ineq_redundant: continue;
526 default: return isl_bool_false;
530 return isl_bool_true;
533 static __isl_give isl_vertices *vertices_add_chambers(
534 __isl_take isl_vertices *vertices, int n_chambers,
535 struct isl_chamber_list *list)
537 int i;
538 isl_ctx *ctx;
539 struct isl_chamber_list *next;
541 ctx = isl_vertices_get_ctx(vertices);
542 vertices->c = isl_alloc_array(ctx, struct isl_chamber, n_chambers);
543 if (!vertices->c)
544 goto error;
545 vertices->n_chambers = n_chambers;
547 for (i = 0; list; list = next, i++) {
548 next = list->next;
549 vertices->c[i] = list->c;
550 free(list);
553 return vertices;
554 error:
555 isl_vertices_free(vertices);
556 free_chamber_list(list);
557 return NULL;
560 /* Can "tab" be intersected with "bset" without resulting in
561 * a lower-dimensional set.
562 * "bset" itself is assumed to be full-dimensional.
564 static isl_bool can_intersect(struct isl_tab *tab,
565 __isl_keep isl_basic_set *bset)
567 int i;
568 struct isl_tab_undo *snap;
570 if (bset->n_eq > 0)
571 isl_die(isl_basic_set_get_ctx(bset), isl_error_internal,
572 "expecting full-dimensional input",
573 return isl_bool_error);
575 if (isl_tab_extend_cons(tab, bset->n_ineq) < 0)
576 return isl_bool_error;
578 snap = isl_tab_snap(tab);
580 for (i = 0; i < bset->n_ineq; ++i) {
581 if (isl_tab_ineq_type(tab, bset->ineq[i]) == isl_ineq_redundant)
582 continue;
583 if (isl_tab_add_ineq(tab, bset->ineq[i]) < 0)
584 return isl_bool_error;
587 if (isl_tab_detect_implicit_equalities(tab) < 0)
588 return isl_bool_error;
589 if (tab->n_dead) {
590 if (isl_tab_rollback(tab, snap) < 0)
591 return isl_bool_error;
592 return isl_bool_false;
595 return isl_bool_true;
598 static int add_chamber(struct isl_chamber_list **list,
599 __isl_keep isl_vertices *vertices, struct isl_tab *tab, int *selection)
601 int n_frozen;
602 int i, j;
603 int n_vertices = 0;
604 struct isl_tab_undo *snap;
605 struct isl_chamber_list *c = NULL;
607 for (i = 0; i < vertices->n_vertices; ++i)
608 if (selection[i])
609 n_vertices++;
611 snap = isl_tab_snap(tab);
613 for (i = 0; i < tab->n_con && tab->con[i].frozen; ++i)
614 tab->con[i].frozen = 0;
615 n_frozen = i;
617 if (isl_tab_detect_redundant(tab) < 0)
618 return -1;
620 c = isl_calloc_type(tab->mat->ctx, struct isl_chamber_list);
621 if (!c)
622 goto error;
623 c->c.vertices = isl_alloc_array(tab->mat->ctx, int, n_vertices);
624 if (n_vertices && !c->c.vertices)
625 goto error;
626 c->c.dom = isl_basic_set_copy(isl_tab_peek_bset(tab));
627 c->c.dom = isl_basic_set_set_rational(c->c.dom);
628 c->c.dom = isl_basic_set_cow(c->c.dom);
629 c->c.dom = isl_basic_set_update_from_tab(c->c.dom, tab);
630 c->c.dom = isl_basic_set_simplify(c->c.dom);
631 c->c.dom = isl_basic_set_finalize(c->c.dom);
632 if (!c->c.dom)
633 goto error;
635 c->c.n_vertices = n_vertices;
637 for (i = 0, j = 0; i < vertices->n_vertices; ++i)
638 if (selection[i]) {
639 c->c.vertices[j] = i;
640 j++;
643 c->next = *list;
644 *list = c;
646 for (i = 0; i < n_frozen; ++i)
647 tab->con[i].frozen = 1;
649 if (isl_tab_rollback(tab, snap) < 0)
650 return -1;
652 return 0;
653 error:
654 free_chamber_list(c);
655 return -1;
658 struct isl_facet_todo {
659 struct isl_tab *tab; /* A tableau representation of the facet */
660 isl_basic_set *bset; /* A normalized basic set representation */
661 isl_vec *constraint; /* Constraint pointing to the other side */
662 struct isl_facet_todo *next;
665 static void free_todo(struct isl_facet_todo *todo)
667 while (todo) {
668 struct isl_facet_todo *next = todo->next;
670 isl_tab_free(todo->tab);
671 isl_basic_set_free(todo->bset);
672 isl_vec_free(todo->constraint);
673 free(todo);
675 todo = next;
679 static struct isl_facet_todo *create_todo(struct isl_tab *tab, int con)
681 int i;
682 int n_frozen;
683 struct isl_tab_undo *snap;
684 struct isl_facet_todo *todo;
686 snap = isl_tab_snap(tab);
688 for (i = 0; i < tab->n_con && tab->con[i].frozen; ++i)
689 tab->con[i].frozen = 0;
690 n_frozen = i;
692 if (isl_tab_detect_redundant(tab) < 0)
693 return NULL;
695 todo = isl_calloc_type(tab->mat->ctx, struct isl_facet_todo);
696 if (!todo)
697 return NULL;
699 todo->constraint = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
700 if (!todo->constraint)
701 goto error;
702 isl_seq_neg(todo->constraint->el, tab->bmap->ineq[con], 1 + tab->n_var);
703 todo->bset = isl_basic_set_copy(isl_tab_peek_bset(tab));
704 todo->bset = isl_basic_set_set_rational(todo->bset);
705 todo->bset = isl_basic_set_cow(todo->bset);
706 todo->bset = isl_basic_set_update_from_tab(todo->bset, tab);
707 todo->bset = isl_basic_set_simplify(todo->bset);
708 todo->bset = isl_basic_set_sort_constraints(todo->bset);
709 if (!todo->bset)
710 goto error;
711 ISL_F_SET(todo->bset, ISL_BASIC_SET_NO_REDUNDANT);
712 todo->tab = isl_tab_dup(tab);
713 if (!todo->tab)
714 goto error;
716 for (i = 0; i < n_frozen; ++i)
717 tab->con[i].frozen = 1;
719 if (isl_tab_rollback(tab, snap) < 0)
720 goto error;
722 return todo;
723 error:
724 free_todo(todo);
725 return NULL;
728 /* Create todo items for all interior facets of the chamber represented
729 * by "tab" and collect them in "next".
731 static int init_todo(struct isl_facet_todo **next, struct isl_tab *tab)
733 int i;
734 struct isl_tab_undo *snap;
735 struct isl_facet_todo *todo;
737 snap = isl_tab_snap(tab);
739 for (i = 0; i < tab->n_con; ++i) {
740 if (tab->con[i].frozen)
741 continue;
742 if (tab->con[i].is_redundant)
743 continue;
745 if (isl_tab_select_facet(tab, i) < 0)
746 return -1;
748 todo = create_todo(tab, i);
749 if (!todo)
750 return -1;
752 todo->next = *next;
753 *next = todo;
755 if (isl_tab_rollback(tab, snap) < 0)
756 return -1;
759 return 0;
762 /* Does the linked list contain a todo item that is the opposite of "todo".
763 * If so, return 1 and remove the opposite todo item.
765 static int has_opposite(struct isl_facet_todo *todo,
766 struct isl_facet_todo **list)
768 for (; *list; list = &(*list)->next) {
769 int eq;
770 eq = isl_basic_set_plain_is_equal(todo->bset, (*list)->bset);
771 if (eq < 0)
772 return -1;
773 if (!eq)
774 continue;
775 todo = *list;
776 *list = todo->next;
777 todo->next = NULL;
778 free_todo(todo);
779 return 1;
782 return 0;
785 /* Create todo items for all interior facets of the chamber represented
786 * by "tab" and collect them in first->next, taking care to cancel
787 * opposite todo items.
789 static int update_todo(struct isl_facet_todo *first, struct isl_tab *tab)
791 int i;
792 struct isl_tab_undo *snap;
793 struct isl_facet_todo *todo;
795 snap = isl_tab_snap(tab);
797 for (i = 0; i < tab->n_con; ++i) {
798 int drop;
800 if (tab->con[i].frozen)
801 continue;
802 if (tab->con[i].is_redundant)
803 continue;
805 if (isl_tab_select_facet(tab, i) < 0)
806 return -1;
808 todo = create_todo(tab, i);
809 if (!todo)
810 return -1;
812 drop = has_opposite(todo, &first->next);
813 if (drop < 0)
814 return -1;
816 if (drop)
817 free_todo(todo);
818 else {
819 todo->next = first->next;
820 first->next = todo;
823 if (isl_tab_rollback(tab, snap) < 0)
824 return -1;
827 return 0;
830 /* Compute the chamber decomposition of the parametric polytope respresented
831 * by "bset" given the parametric vertices and their activity domains.
833 * We are only interested in full-dimensional chambers.
834 * Each of these chambers is the intersection of the activity domains of
835 * one or more vertices and the union of all chambers is equal to the
836 * projection of the entire parametric polytope onto the parameter space.
838 * We first create an initial chamber by intersecting as many activity
839 * domains as possible without ending up with an empty or lower-dimensional
840 * set. As a minor optimization, we only consider those activity domains
841 * that contain some arbitrary point.
843 * For each of the interior facets of the chamber, we construct a todo item,
844 * containing the facet and a constraint containing the other side of the facet,
845 * for constructing the chamber on the other side.
846 * While their are any todo items left, we pick a todo item and
847 * create the required chamber by intersecting all activity domains
848 * that contain the facet and have a full-dimensional intersection with
849 * the other side of the facet. For each of the interior facets, we
850 * again create todo items, taking care to cancel opposite todo items.
852 static __isl_give isl_vertices *compute_chambers(__isl_take isl_basic_set *bset,
853 __isl_take isl_vertices *vertices)
855 int i;
856 isl_ctx *ctx;
857 isl_vec *sample = NULL;
858 struct isl_tab *tab = NULL;
859 struct isl_tab_undo *snap;
860 int *selection = NULL;
861 int n_chambers = 0;
862 struct isl_chamber_list *list = NULL;
863 struct isl_facet_todo *todo = NULL;
865 if (!bset || !vertices)
866 goto error;
868 ctx = isl_vertices_get_ctx(vertices);
869 selection = isl_alloc_array(ctx, int, vertices->n_vertices);
870 if (vertices->n_vertices && !selection)
871 goto error;
873 bset = isl_basic_set_params(bset);
875 tab = isl_tab_from_basic_set(bset, 1);
876 if (!tab)
877 goto error;
878 for (i = 0; i < bset->n_ineq; ++i)
879 if (isl_tab_freeze_constraint(tab, i) < 0)
880 goto error;
881 isl_basic_set_free(bset);
883 snap = isl_tab_snap(tab);
885 sample = isl_tab_get_sample_value(tab);
887 for (i = 0; i < vertices->n_vertices; ++i) {
888 selection[i] = isl_basic_set_contains(vertices->v[i].dom, sample);
889 if (selection[i] < 0)
890 goto error;
891 if (!selection[i])
892 continue;
893 selection[i] = can_intersect(tab, vertices->v[i].dom);
894 if (selection[i] < 0)
895 goto error;
898 if (isl_tab_detect_redundant(tab) < 0)
899 goto error;
901 if (add_chamber(&list, vertices, tab, selection) < 0)
902 goto error;
903 n_chambers++;
905 if (init_todo(&todo, tab) < 0)
906 goto error;
908 while (todo) {
909 struct isl_facet_todo *next;
911 if (isl_tab_rollback(tab, snap) < 0)
912 goto error;
914 if (isl_tab_add_ineq(tab, todo->constraint->el) < 0)
915 goto error;
916 if (isl_tab_freeze_constraint(tab, tab->n_con - 1) < 0)
917 goto error;
919 for (i = 0; i < vertices->n_vertices; ++i) {
920 selection[i] = bset_covers_tab(vertices->v[i].dom,
921 todo->tab);
922 if (selection[i] < 0)
923 goto error;
924 if (!selection[i])
925 continue;
926 selection[i] = can_intersect(tab, vertices->v[i].dom);
927 if (selection[i] < 0)
928 goto error;
931 if (isl_tab_detect_redundant(tab) < 0)
932 goto error;
934 if (add_chamber(&list, vertices, tab, selection) < 0)
935 goto error;
936 n_chambers++;
938 if (update_todo(todo, tab) < 0)
939 goto error;
941 next = todo->next;
942 todo->next = NULL;
943 free_todo(todo);
944 todo = next;
947 isl_vec_free(sample);
949 isl_tab_free(tab);
950 free(selection);
952 vertices = vertices_add_chambers(vertices, n_chambers, list);
954 for (i = 0; vertices && i < vertices->n_vertices; ++i) {
955 isl_basic_set_free(vertices->v[i].dom);
956 vertices->v[i].dom = NULL;
959 return vertices;
960 error:
961 free_chamber_list(list);
962 free_todo(todo);
963 isl_vec_free(sample);
964 isl_tab_free(tab);
965 free(selection);
966 if (!tab)
967 isl_basic_set_free(bset);
968 isl_vertices_free(vertices);
969 return NULL;
972 isl_ctx *isl_vertex_get_ctx(__isl_keep isl_vertex *vertex)
974 return vertex ? isl_vertices_get_ctx(vertex->vertices) : NULL;
977 int isl_vertex_get_id(__isl_keep isl_vertex *vertex)
979 return vertex ? vertex->id : -1;
982 /* Return the activity domain of the vertex "vertex".
984 __isl_give isl_basic_set *isl_vertex_get_domain(__isl_keep isl_vertex *vertex)
986 struct isl_vertex *v;
988 if (!vertex)
989 return NULL;
991 v = &vertex->vertices->v[vertex->id];
992 if (!v->dom) {
993 v->dom = isl_basic_set_copy(v->vertex);
994 v->dom = isl_basic_set_params(v->dom);
995 v->dom = isl_basic_set_set_integral(v->dom);
998 return isl_basic_set_copy(v->dom);
1001 /* Return a multiple quasi-affine expression describing the vertex "vertex"
1002 * in terms of the parameters,
1004 __isl_give isl_multi_aff *isl_vertex_get_expr(__isl_keep isl_vertex *vertex)
1006 struct isl_vertex *v;
1007 isl_basic_set *bset;
1009 if (!vertex)
1010 return NULL;
1012 v = &vertex->vertices->v[vertex->id];
1014 bset = isl_basic_set_copy(v->vertex);
1015 return isl_multi_aff_from_basic_set_equalities(bset);
1018 static __isl_give isl_vertex *isl_vertex_alloc(__isl_take isl_vertices *vertices,
1019 int id)
1021 isl_ctx *ctx;
1022 isl_vertex *vertex;
1024 if (!vertices)
1025 return NULL;
1027 ctx = isl_vertices_get_ctx(vertices);
1028 vertex = isl_alloc_type(ctx, isl_vertex);
1029 if (!vertex)
1030 goto error;
1032 vertex->vertices = vertices;
1033 vertex->id = id;
1035 return vertex;
1036 error:
1037 isl_vertices_free(vertices);
1038 return NULL;
1041 __isl_null isl_vertex *isl_vertex_free(__isl_take isl_vertex *vertex)
1043 if (!vertex)
1044 return NULL;
1045 isl_vertices_free(vertex->vertices);
1046 free(vertex);
1048 return NULL;
1051 isl_ctx *isl_cell_get_ctx(__isl_keep isl_cell *cell)
1053 return cell ? cell->dom->ctx : NULL;
1056 __isl_give isl_basic_set *isl_cell_get_domain(__isl_keep isl_cell *cell)
1058 return cell ? isl_basic_set_copy(cell->dom) : NULL;
1061 static __isl_give isl_cell *isl_cell_alloc(__isl_take isl_vertices *vertices,
1062 __isl_take isl_basic_set *dom, int id)
1064 int i;
1065 isl_cell *cell = NULL;
1067 if (!vertices || !dom)
1068 goto error;
1070 cell = isl_calloc_type(dom->ctx, isl_cell);
1071 if (!cell)
1072 goto error;
1074 cell->n_vertices = vertices->c[id].n_vertices;
1075 cell->ids = isl_alloc_array(dom->ctx, int, cell->n_vertices);
1076 if (cell->n_vertices && !cell->ids)
1077 goto error;
1078 for (i = 0; i < cell->n_vertices; ++i)
1079 cell->ids[i] = vertices->c[id].vertices[i];
1080 cell->vertices = vertices;
1081 cell->dom = dom;
1083 return cell;
1084 error:
1085 isl_cell_free(cell);
1086 isl_vertices_free(vertices);
1087 isl_basic_set_free(dom);
1088 return NULL;
1091 __isl_null isl_cell *isl_cell_free(__isl_take isl_cell *cell)
1093 if (!cell)
1094 return NULL;
1096 isl_vertices_free(cell->vertices);
1097 free(cell->ids);
1098 isl_basic_set_free(cell->dom);
1099 free(cell);
1101 return NULL;
1104 /* Create a tableau of the cone obtained by first homogenizing the given
1105 * polytope and then making all inequalities strict by setting the
1106 * constant term to -1.
1108 static struct isl_tab *tab_for_shifted_cone(__isl_keep isl_basic_set *bset)
1110 int i;
1111 isl_vec *c = NULL;
1112 struct isl_tab *tab;
1114 if (!bset)
1115 return NULL;
1116 tab = isl_tab_alloc(bset->ctx, bset->n_eq + bset->n_ineq + 1,
1117 1 + isl_basic_set_total_dim(bset), 0);
1118 if (!tab)
1119 return NULL;
1120 tab->rational = ISL_F_ISSET(bset, ISL_BASIC_SET_RATIONAL);
1121 if (ISL_F_ISSET(bset, ISL_BASIC_MAP_EMPTY)) {
1122 if (isl_tab_mark_empty(tab) < 0)
1123 goto error;
1124 return tab;
1127 c = isl_vec_alloc(bset->ctx, 1 + 1 + isl_basic_set_total_dim(bset));
1128 if (!c)
1129 goto error;
1131 isl_int_set_si(c->el[0], 0);
1132 for (i = 0; i < bset->n_eq; ++i) {
1133 isl_seq_cpy(c->el + 1, bset->eq[i], c->size - 1);
1134 if (isl_tab_add_eq(tab, c->el) < 0)
1135 goto error;
1138 isl_int_set_si(c->el[0], -1);
1139 for (i = 0; i < bset->n_ineq; ++i) {
1140 isl_seq_cpy(c->el + 1, bset->ineq[i], c->size - 1);
1141 if (isl_tab_add_ineq(tab, c->el) < 0)
1142 goto error;
1143 if (tab->empty) {
1144 isl_vec_free(c);
1145 return tab;
1149 isl_seq_clr(c->el + 1, c->size - 1);
1150 isl_int_set_si(c->el[1], 1);
1151 if (isl_tab_add_ineq(tab, c->el) < 0)
1152 goto error;
1154 isl_vec_free(c);
1155 return tab;
1156 error:
1157 isl_vec_free(c);
1158 isl_tab_free(tab);
1159 return NULL;
1162 /* Compute an interior point of "bset" by selecting an interior
1163 * point in homogeneous space and projecting the point back down.
1165 static __isl_give isl_vec *isl_basic_set_interior_point(
1166 __isl_keep isl_basic_set *bset)
1168 isl_vec *vec;
1169 struct isl_tab *tab;
1171 tab = tab_for_shifted_cone(bset);
1172 vec = isl_tab_get_sample_value(tab);
1173 isl_tab_free(tab);
1174 if (!vec)
1175 return NULL;
1177 isl_seq_cpy(vec->el, vec->el + 1, vec->size - 1);
1178 vec->size--;
1180 return vec;
1183 /* Call "fn" on all chambers of the parametric polytope with the shared
1184 * facets of neighboring chambers only appearing in one of the chambers.
1186 * We pick an interior point from one of the chambers and then make
1187 * all constraints that do not satisfy this point strict.
1188 * For constraints that saturate the interior point, the sign
1189 * of the first non-zero coefficient is used to determine which
1190 * of the two (internal) constraints should be tightened.
1192 isl_stat isl_vertices_foreach_disjoint_cell(__isl_keep isl_vertices *vertices,
1193 isl_stat (*fn)(__isl_take isl_cell *cell, void *user), void *user)
1195 int i;
1196 isl_vec *vec;
1197 isl_cell *cell;
1199 if (!vertices)
1200 return isl_stat_error;
1202 if (vertices->n_chambers == 0)
1203 return isl_stat_ok;
1205 if (vertices->n_chambers == 1) {
1206 isl_basic_set *dom = isl_basic_set_copy(vertices->c[0].dom);
1207 dom = isl_basic_set_set_integral(dom);
1208 cell = isl_cell_alloc(isl_vertices_copy(vertices), dom, 0);
1209 if (!cell)
1210 return isl_stat_error;
1211 return fn(cell, user);
1214 vec = isl_basic_set_interior_point(vertices->c[0].dom);
1215 if (!vec)
1216 return isl_stat_error;
1218 for (i = 0; i < vertices->n_chambers; ++i) {
1219 int r;
1220 isl_basic_set *dom = isl_basic_set_copy(vertices->c[i].dom);
1221 if (i)
1222 dom = isl_basic_set_tighten_outward(dom, vec);
1223 dom = isl_basic_set_set_integral(dom);
1224 cell = isl_cell_alloc(isl_vertices_copy(vertices), dom, i);
1225 if (!cell)
1226 goto error;
1227 r = fn(cell, user);
1228 if (r < 0)
1229 goto error;
1232 isl_vec_free(vec);
1234 return isl_stat_ok;
1235 error:
1236 isl_vec_free(vec);
1237 return isl_stat_error;
1240 isl_stat isl_vertices_foreach_cell(__isl_keep isl_vertices *vertices,
1241 isl_stat (*fn)(__isl_take isl_cell *cell, void *user), void *user)
1243 int i;
1244 isl_cell *cell;
1246 if (!vertices)
1247 return isl_stat_error;
1249 if (vertices->n_chambers == 0)
1250 return isl_stat_ok;
1252 for (i = 0; i < vertices->n_chambers; ++i) {
1253 isl_stat r;
1254 isl_basic_set *dom = isl_basic_set_copy(vertices->c[i].dom);
1256 cell = isl_cell_alloc(isl_vertices_copy(vertices), dom, i);
1257 if (!cell)
1258 return isl_stat_error;
1260 r = fn(cell, user);
1261 if (r < 0)
1262 return isl_stat_error;
1265 return isl_stat_ok;
1268 isl_stat isl_vertices_foreach_vertex(__isl_keep isl_vertices *vertices,
1269 isl_stat (*fn)(__isl_take isl_vertex *vertex, void *user), void *user)
1271 int i;
1272 isl_vertex *vertex;
1274 if (!vertices)
1275 return isl_stat_error;
1277 if (vertices->n_vertices == 0)
1278 return isl_stat_ok;
1280 for (i = 0; i < vertices->n_vertices; ++i) {
1281 isl_stat r;
1283 vertex = isl_vertex_alloc(isl_vertices_copy(vertices), i);
1284 if (!vertex)
1285 return isl_stat_error;
1287 r = fn(vertex, user);
1288 if (r < 0)
1289 return isl_stat_error;
1292 return isl_stat_ok;
1295 isl_stat isl_cell_foreach_vertex(__isl_keep isl_cell *cell,
1296 isl_stat (*fn)(__isl_take isl_vertex *vertex, void *user), void *user)
1298 int i;
1299 isl_vertex *vertex;
1301 if (!cell)
1302 return isl_stat_error;
1304 if (cell->n_vertices == 0)
1305 return isl_stat_ok;
1307 for (i = 0; i < cell->n_vertices; ++i) {
1308 isl_stat r;
1310 vertex = isl_vertex_alloc(isl_vertices_copy(cell->vertices),
1311 cell->ids[i]);
1312 if (!vertex)
1313 return isl_stat_error;
1315 r = fn(vertex, user);
1316 if (r < 0)
1317 return isl_stat_error;
1320 return isl_stat_ok;
1323 isl_ctx *isl_vertices_get_ctx(__isl_keep isl_vertices *vertices)
1325 return vertices ? vertices->bset->ctx : NULL;
1328 int isl_vertices_get_n_vertices(__isl_keep isl_vertices *vertices)
1330 return vertices ? vertices->n_vertices : -1;
1333 __isl_give isl_vertices *isl_morph_vertices(__isl_take isl_morph *morph,
1334 __isl_take isl_vertices *vertices)
1336 int i;
1337 isl_morph *param_morph = NULL;
1339 if (!morph || !vertices)
1340 goto error;
1342 isl_assert(vertices->bset->ctx, vertices->ref == 1, goto error);
1344 param_morph = isl_morph_copy(morph);
1345 param_morph = isl_morph_dom_params(param_morph);
1346 param_morph = isl_morph_ran_params(param_morph);
1348 for (i = 0; i < vertices->n_vertices; ++i) {
1349 vertices->v[i].dom = isl_morph_basic_set(
1350 isl_morph_copy(param_morph), vertices->v[i].dom);
1351 vertices->v[i].vertex = isl_morph_basic_set(
1352 isl_morph_copy(morph), vertices->v[i].vertex);
1353 if (!vertices->v[i].vertex)
1354 goto error;
1357 for (i = 0; i < vertices->n_chambers; ++i) {
1358 vertices->c[i].dom = isl_morph_basic_set(
1359 isl_morph_copy(param_morph), vertices->c[i].dom);
1360 if (!vertices->c[i].dom)
1361 goto error;
1364 isl_morph_free(param_morph);
1365 isl_morph_free(morph);
1366 return vertices;
1367 error:
1368 isl_morph_free(param_morph);
1369 isl_morph_free(morph);
1370 isl_vertices_free(vertices);
1371 return NULL;
1374 /* Construct a simplex isl_cell spanned by the vertices with indices in
1375 * "simplex_ids" and "other_ids" and call "fn" on this isl_cell.
1377 static isl_stat call_on_simplex(__isl_keep isl_cell *cell,
1378 int *simplex_ids, int n_simplex, int *other_ids, int n_other,
1379 isl_stat (*fn)(__isl_take isl_cell *simplex, void *user), void *user)
1381 int i;
1382 isl_ctx *ctx;
1383 struct isl_cell *simplex;
1385 ctx = isl_cell_get_ctx(cell);
1387 simplex = isl_calloc_type(ctx, struct isl_cell);
1388 if (!simplex)
1389 return isl_stat_error;
1390 simplex->vertices = isl_vertices_copy(cell->vertices);
1391 if (!simplex->vertices)
1392 goto error;
1393 simplex->dom = isl_basic_set_copy(cell->dom);
1394 if (!simplex->dom)
1395 goto error;
1396 simplex->n_vertices = n_simplex + n_other;
1397 simplex->ids = isl_alloc_array(ctx, int, simplex->n_vertices);
1398 if (!simplex->ids)
1399 goto error;
1401 for (i = 0; i < n_simplex; ++i)
1402 simplex->ids[i] = simplex_ids[i];
1403 for (i = 0; i < n_other; ++i)
1404 simplex->ids[n_simplex + i] = other_ids[i];
1406 return fn(simplex, user);
1407 error:
1408 isl_cell_free(simplex);
1409 return isl_stat_error;
1412 /* Check whether the parametric vertex described by "vertex"
1413 * lies on the facet corresponding to constraint "facet" of "bset".
1414 * The isl_vec "v" is a temporary vector than can be used by this function.
1416 * We eliminate the variables from the facet constraint using the
1417 * equalities defining the vertex and check if the result is identical
1418 * to zero.
1420 * It would probably be better to keep track of the constraints defining
1421 * a vertex during the vertex construction so that we could simply look
1422 * it up here.
1424 static int vertex_on_facet(__isl_keep isl_basic_set *vertex,
1425 __isl_keep isl_basic_set *bset, int facet, __isl_keep isl_vec *v)
1427 int i;
1428 isl_int m;
1430 isl_seq_cpy(v->el, bset->ineq[facet], v->size);
1432 isl_int_init(m);
1433 for (i = 0; i < vertex->n_eq; ++i) {
1434 int k = isl_seq_last_non_zero(vertex->eq[i], v->size);
1435 isl_seq_elim(v->el, vertex->eq[i], k, v->size, &m);
1437 isl_int_clear(m);
1439 return isl_seq_first_non_zero(v->el, v->size) == -1;
1442 /* Triangulate the polytope spanned by the vertices with ids
1443 * in "simplex_ids" and "other_ids" and call "fn" on each of
1444 * the resulting simplices.
1445 * If the input polytope is already a simplex, we simply call "fn".
1446 * Otherwise, we pick a point from "other_ids" and add it to "simplex_ids".
1447 * Then we consider each facet of "bset" that does not contain the point
1448 * we just picked, but does contain some of the other points in "other_ids"
1449 * and call ourselves recursively on the polytope spanned by the new
1450 * "simplex_ids" and those points in "other_ids" that lie on the facet.
1452 static isl_stat triangulate(__isl_keep isl_cell *cell, __isl_keep isl_vec *v,
1453 int *simplex_ids, int n_simplex, int *other_ids, int n_other,
1454 isl_stat (*fn)(__isl_take isl_cell *simplex, void *user), void *user)
1456 int i, j, k;
1457 int d, nparam;
1458 int *ids;
1459 isl_ctx *ctx;
1460 isl_basic_set *vertex;
1461 isl_basic_set *bset;
1463 ctx = isl_cell_get_ctx(cell);
1464 d = isl_basic_set_dim(cell->vertices->bset, isl_dim_set);
1465 nparam = isl_basic_set_dim(cell->vertices->bset, isl_dim_param);
1467 if (n_simplex + n_other == d + 1)
1468 return call_on_simplex(cell, simplex_ids, n_simplex,
1469 other_ids, n_other, fn, user);
1471 simplex_ids[n_simplex] = other_ids[0];
1472 vertex = cell->vertices->v[other_ids[0]].vertex;
1473 bset = cell->vertices->bset;
1475 ids = isl_alloc_array(ctx, int, n_other - 1);
1476 if (!ids)
1477 goto error;
1478 for (i = 0; i < bset->n_ineq; ++i) {
1479 if (isl_seq_first_non_zero(bset->ineq[i] + 1 + nparam, d) == -1)
1480 continue;
1481 if (vertex_on_facet(vertex, bset, i, v))
1482 continue;
1484 for (j = 1, k = 0; j < n_other; ++j) {
1485 isl_basic_set *ov;
1486 ov = cell->vertices->v[other_ids[j]].vertex;
1487 if (vertex_on_facet(ov, bset, i, v))
1488 ids[k++] = other_ids[j];
1490 if (k == 0)
1491 continue;
1493 if (triangulate(cell, v, simplex_ids, n_simplex + 1,
1494 ids, k, fn, user) < 0)
1495 goto error;
1497 free(ids);
1499 return isl_stat_ok;
1500 error:
1501 free(ids);
1502 return isl_stat_error;
1505 /* Triangulate the given cell and call "fn" on each of the resulting
1506 * simplices.
1508 isl_stat isl_cell_foreach_simplex(__isl_take isl_cell *cell,
1509 isl_stat (*fn)(__isl_take isl_cell *simplex, void *user), void *user)
1511 int d, total;
1512 isl_stat r;
1513 isl_ctx *ctx;
1514 isl_vec *v = NULL;
1515 int *simplex_ids = NULL;
1517 if (!cell)
1518 return isl_stat_error;
1520 d = isl_basic_set_dim(cell->vertices->bset, isl_dim_set);
1521 total = isl_basic_set_total_dim(cell->vertices->bset);
1523 if (cell->n_vertices == d + 1)
1524 return fn(cell, user);
1526 ctx = isl_cell_get_ctx(cell);
1527 simplex_ids = isl_alloc_array(ctx, int, d + 1);
1528 if (!simplex_ids)
1529 goto error;
1531 v = isl_vec_alloc(ctx, 1 + total);
1532 if (!v)
1533 goto error;
1535 r = triangulate(cell, v, simplex_ids, 0,
1536 cell->ids, cell->n_vertices, fn, user);
1538 isl_vec_free(v);
1539 free(simplex_ids);
1541 isl_cell_free(cell);
1543 return r;
1544 error:
1545 free(simplex_ids);
1546 isl_vec_free(v);
1547 isl_cell_free(cell);
1548 return isl_stat_error;