isl_tab_rollback: return isl_stat
[isl.git] / isl_vertices.c
blobf2c776570f84b4284b62753fe3521eec2876afba
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 isl_size 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);
132 if (nvar < 0)
133 return isl_bool_error;
135 v = isl_calloc_type(tab->mat->ctx, struct isl_vertex_list);
136 if (!v)
137 goto error;
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);
144 if (!v->v.vertex)
145 goto error;
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);
149 if (!v->v.dom)
150 goto error;
152 if (v->v.dom->n_eq > 0) {
153 free_vertex_list(v);
154 return isl_bool_false;
157 v->next = *list;
158 *list = v;
160 return isl_bool_true;
161 error:
162 free_vertex_list(v);
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;
173 if (!bset)
174 return NULL;
176 vertices = isl_calloc_type(bset->ctx, isl_vertices);
177 if (!vertices)
178 return NULL;
179 vertices->bset = isl_basic_set_copy(bset);
180 vertices->ref = 1;
182 vertices->n_vertices = 0;
183 vertices->n_chambers = 0;
185 return vertices;
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
192 * the vertex.
194 static __isl_give isl_vertices *vertices_0D(__isl_keep isl_basic_set *bset)
196 isl_vertices *vertices;
198 if (!bset)
199 return NULL;
201 vertices = isl_calloc_type(bset->ctx, isl_vertices);
202 if (!vertices)
203 return NULL;
204 vertices->ref = 1;
205 vertices->bset = isl_basic_set_copy(bset);
207 vertices->v = isl_calloc_array(bset->ctx, struct isl_vertex, 1);
208 if (!vertices->v)
209 goto error;
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)
214 goto error;
216 vertices->c = isl_calloc_array(bset->ctx, struct isl_chamber, 1);
217 if (!vertices->c)
218 goto error;
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)
223 goto error;
224 vertices->c[0].dom = isl_basic_set_copy(vertices->v[0].dom);
225 if (!vertices->c[0].dom)
226 goto error;
228 return vertices;
229 error:
230 isl_vertices_free(vertices);
231 return NULL;
234 /* Is the row pointed to by "f" linearly independent of the "n" first
235 * rows in "facets"?
237 static isl_bool is_independent(__isl_keep isl_mat *facets, int n, isl_int *f)
239 isl_size rank;
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);
247 if (rank < 0)
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,
270 int *selection)
272 int i;
273 isl_bool indep;
274 unsigned ovar;
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)
284 return indep;
286 snap = isl_tab_snap(tab);
287 if (isl_tab_select_facet(tab, level) < 0)
288 return isl_bool_error;
290 if (tab->empty) {
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) {
297 int sgn;
299 if (selection[i] != DESELECTED)
300 continue;
302 if (isl_tab_is_equality(tab, i))
303 sgn = 0;
304 else if (isl_tab_is_redundant(tab, i))
305 sgn = 1;
306 else
307 sgn = isl_tab_sign_of_max(tab, i);
308 if (sgn < -1)
309 return isl_bool_error;
310 if (sgn <= 0) {
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)
329 isl_morph *morph;
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);
343 return 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
349 * variables.
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
357 * smallest index.
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)
378 struct isl_tab *tab;
379 int level;
380 int init;
381 isl_size nvar;
382 int *selection = NULL;
383 int selected;
384 struct isl_tab_undo **snap = NULL;
385 isl_mat *facets = NULL;
386 struct isl_vertex_list *list = NULL;
387 int n_vertices = 0;
388 isl_vertices *vertices;
390 if (!bset)
391 return NULL;
393 if (isl_basic_set_plain_is_empty(bset))
394 return vertices_empty(bset);
396 if (bset->n_eq != 0)
397 return lower_dim_vertices(bset);
399 if (isl_basic_set_check_no_locals(bset) < 0)
400 return NULL;
402 nvar = isl_basic_set_dim(bset, isl_dim_set);
403 if (nvar < 0)
404 return NULL;
405 if (nvar == 0)
406 return vertices_0D(bset);
408 bset = isl_basic_set_copy(bset);
409 bset = isl_basic_set_set_rational(bset);
410 if (!bset)
411 return NULL;
413 tab = isl_tab_from_basic_set(bset, 0);
414 if (!tab)
415 goto error;
416 tab->strict_redundant = 1;
418 if (tab->empty) {
419 vertices = vertices_empty(bset);
420 isl_basic_set_free(bset);
421 isl_tab_free(tab);
422 return vertices;
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)
429 goto error;
431 level = 0;
432 init = 1;
433 selected = 0;
435 while (level >= 0) {
436 if (level >= bset->n_ineq ||
437 (!init && selection[level] != SELECTED)) {
438 --level;
439 init = 0;
440 continue;
442 if (init) {
443 isl_bool ok;
444 snap[level] = isl_tab_snap(tab);
445 ok = can_select(bset, level, tab, facets, selected,
446 selection);
447 if (ok < 0)
448 goto error;
449 if (ok) {
450 selection[level] = SELECTED;
451 selected++;
452 } else
453 selection[level] = UNSELECTED;
454 } else {
455 selection[level] = DESELECTED;
456 selected--;
457 if (isl_tab_rollback(tab, snap[level]) < 0)
458 goto error;
460 if (selected == nvar) {
461 if (tab->n_dead == nvar) {
462 isl_bool added = add_vertex(&list, bset, tab);
463 if (added < 0)
464 goto error;
465 if (added)
466 n_vertices++;
468 init = 0;
469 continue;
471 ++level;
472 init = 1;
475 isl_mat_free(facets);
476 free(selection);
477 free(snap);
479 isl_tab_free(tab);
481 vertices = vertices_from_list(bset, n_vertices, list);
483 vertices = compute_chambers(bset, vertices);
485 return vertices;
486 error:
487 free_vertex_list(list);
488 isl_mat_free(facets);
489 free(selection);
490 free(snap);
491 isl_tab_free(tab);
492 isl_basic_set_free(bset);
493 return NULL;
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) {
506 next = list->next;
507 isl_basic_set_free(list->c.dom);
508 free(list->c.vertices);
509 free(list);
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,
517 struct isl_tab *tab)
519 int i;
521 if (!bset || !tab)
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]);
526 switch (type) {
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)
540 int i;
541 isl_ctx *ctx;
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);
546 if (!vertices->c)
547 goto error;
548 vertices->n_chambers = n_chambers;
550 for (i = 0; list; list = next, i++) {
551 next = list->next;
552 vertices->c[i] = list->c;
553 free(list);
556 return vertices;
557 error:
558 isl_vertices_free(vertices);
559 free_chamber_list(list);
560 return NULL;
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)
570 int i;
571 struct isl_tab_undo *snap;
573 if (bset->n_eq > 0)
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)
585 continue;
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;
592 if (tab->n_dead) {
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)
604 int n_frozen;
605 int i, j;
606 int n_vertices = 0;
607 struct isl_tab_undo *snap;
608 struct isl_chamber_list *c = NULL;
610 for (i = 0; i < vertices->n_vertices; ++i)
611 if (selection[i])
612 n_vertices++;
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;
618 n_frozen = i;
620 if (isl_tab_detect_redundant(tab) < 0)
621 return -1;
623 c = isl_calloc_type(tab->mat->ctx, struct isl_chamber_list);
624 if (!c)
625 goto error;
626 c->c.vertices = isl_alloc_array(tab->mat->ctx, int, n_vertices);
627 if (n_vertices && !c->c.vertices)
628 goto error;
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);
635 if (!c->c.dom)
636 goto error;
638 c->c.n_vertices = n_vertices;
640 for (i = 0, j = 0; i < vertices->n_vertices; ++i)
641 if (selection[i]) {
642 c->c.vertices[j] = i;
643 j++;
646 c->next = *list;
647 *list = c;
649 for (i = 0; i < n_frozen; ++i)
650 tab->con[i].frozen = 1;
652 if (isl_tab_rollback(tab, snap) < 0)
653 return -1;
655 return 0;
656 error:
657 free_chamber_list(c);
658 return -1;
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)
670 while (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);
676 free(todo);
678 todo = next;
682 static struct isl_facet_todo *create_todo(struct isl_tab *tab, int con)
684 int i;
685 int n_frozen;
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;
693 n_frozen = i;
695 if (isl_tab_detect_redundant(tab) < 0)
696 return NULL;
698 todo = isl_calloc_type(tab->mat->ctx, struct isl_facet_todo);
699 if (!todo)
700 return NULL;
702 todo->constraint = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
703 if (!todo->constraint)
704 goto error;
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);
712 if (!todo->bset)
713 goto error;
714 ISL_F_SET(todo->bset, ISL_BASIC_SET_NO_REDUNDANT);
715 todo->tab = isl_tab_dup(tab);
716 if (!todo->tab)
717 goto error;
719 for (i = 0; i < n_frozen; ++i)
720 tab->con[i].frozen = 1;
722 if (isl_tab_rollback(tab, snap) < 0)
723 goto error;
725 return todo;
726 error:
727 free_todo(todo);
728 return NULL;
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)
736 int i;
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)
744 continue;
745 if (tab->con[i].is_redundant)
746 continue;
748 if (isl_tab_select_facet(tab, i) < 0)
749 return -1;
751 todo = create_todo(tab, i);
752 if (!todo)
753 return -1;
755 todo->next = *next;
756 *next = todo;
758 if (isl_tab_rollback(tab, snap) < 0)
759 return -1;
762 return 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) {
772 int eq;
773 eq = isl_basic_set_plain_is_equal(todo->bset, (*list)->bset);
774 if (eq < 0)
775 return -1;
776 if (!eq)
777 continue;
778 todo = *list;
779 *list = todo->next;
780 todo->next = NULL;
781 free_todo(todo);
782 return 1;
785 return 0;
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)
794 int i;
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) {
801 int drop;
803 if (tab->con[i].frozen)
804 continue;
805 if (tab->con[i].is_redundant)
806 continue;
808 if (isl_tab_select_facet(tab, i) < 0)
809 return -1;
811 todo = create_todo(tab, i);
812 if (!todo)
813 return -1;
815 drop = has_opposite(todo, &first->next);
816 if (drop < 0)
817 return -1;
819 if (drop)
820 free_todo(todo);
821 else {
822 todo->next = first->next;
823 first->next = todo;
826 if (isl_tab_rollback(tab, snap) < 0)
827 return -1;
830 return 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)
858 int i;
859 isl_ctx *ctx;
860 isl_vec *sample = NULL;
861 struct isl_tab *tab = NULL;
862 struct isl_tab_undo *snap;
863 int *selection = NULL;
864 int n_chambers = 0;
865 struct isl_chamber_list *list = NULL;
866 struct isl_facet_todo *todo = NULL;
868 if (!bset || !vertices)
869 goto error;
871 ctx = isl_vertices_get_ctx(vertices);
872 selection = isl_alloc_array(ctx, int, vertices->n_vertices);
873 if (vertices->n_vertices && !selection)
874 goto error;
876 bset = isl_basic_set_params(bset);
878 tab = isl_tab_from_basic_set(bset, 1);
879 if (!tab)
880 goto error;
881 for (i = 0; i < bset->n_ineq; ++i)
882 if (isl_tab_freeze_constraint(tab, i) < 0)
883 goto error;
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)
893 goto error;
894 if (!selection[i])
895 continue;
896 selection[i] = can_intersect(tab, vertices->v[i].dom);
897 if (selection[i] < 0)
898 goto error;
901 if (isl_tab_detect_redundant(tab) < 0)
902 goto error;
904 if (add_chamber(&list, vertices, tab, selection) < 0)
905 goto error;
906 n_chambers++;
908 if (init_todo(&todo, tab) < 0)
909 goto error;
911 while (todo) {
912 struct isl_facet_todo *next;
914 if (isl_tab_rollback(tab, snap) < 0)
915 goto error;
917 if (isl_tab_add_ineq(tab, todo->constraint->el) < 0)
918 goto error;
919 if (isl_tab_freeze_constraint(tab, tab->n_con - 1) < 0)
920 goto error;
922 for (i = 0; i < vertices->n_vertices; ++i) {
923 selection[i] = bset_covers_tab(vertices->v[i].dom,
924 todo->tab);
925 if (selection[i] < 0)
926 goto error;
927 if (!selection[i])
928 continue;
929 selection[i] = can_intersect(tab, vertices->v[i].dom);
930 if (selection[i] < 0)
931 goto error;
934 if (isl_tab_detect_redundant(tab) < 0)
935 goto error;
937 if (add_chamber(&list, vertices, tab, selection) < 0)
938 goto error;
939 n_chambers++;
941 if (update_todo(todo, tab) < 0)
942 goto error;
944 next = todo->next;
945 todo->next = NULL;
946 free_todo(todo);
947 todo = next;
950 isl_vec_free(sample);
952 isl_tab_free(tab);
953 free(selection);
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;
962 return vertices;
963 error:
964 free_chamber_list(list);
965 free_todo(todo);
966 isl_vec_free(sample);
967 isl_tab_free(tab);
968 free(selection);
969 if (!tab)
970 isl_basic_set_free(bset);
971 isl_vertices_free(vertices);
972 return NULL;
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;
991 if (!vertex)
992 return NULL;
994 v = &vertex->vertices->v[vertex->id];
995 if (!v->dom) {
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;
1012 if (!vertex)
1013 return NULL;
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,
1022 int id)
1024 isl_ctx *ctx;
1025 isl_vertex *vertex;
1027 if (!vertices)
1028 return NULL;
1030 ctx = isl_vertices_get_ctx(vertices);
1031 vertex = isl_alloc_type(ctx, isl_vertex);
1032 if (!vertex)
1033 goto error;
1035 vertex->vertices = vertices;
1036 vertex->id = id;
1038 return vertex;
1039 error:
1040 isl_vertices_free(vertices);
1041 return NULL;
1044 __isl_null isl_vertex *isl_vertex_free(__isl_take isl_vertex *vertex)
1046 if (!vertex)
1047 return NULL;
1048 isl_vertices_free(vertex->vertices);
1049 free(vertex);
1051 return NULL;
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)
1067 int i;
1068 isl_cell *cell = NULL;
1070 if (!vertices || !dom)
1071 goto error;
1073 cell = isl_calloc_type(dom->ctx, isl_cell);
1074 if (!cell)
1075 goto error;
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)
1080 goto error;
1081 for (i = 0; i < cell->n_vertices; ++i)
1082 cell->ids[i] = vertices->c[id].vertices[i];
1083 cell->vertices = vertices;
1084 cell->dom = dom;
1086 return cell;
1087 error:
1088 isl_cell_free(cell);
1089 isl_vertices_free(vertices);
1090 isl_basic_set_free(dom);
1091 return NULL;
1094 __isl_null isl_cell *isl_cell_free(__isl_take isl_cell *cell)
1096 if (!cell)
1097 return NULL;
1099 isl_vertices_free(cell->vertices);
1100 free(cell->ids);
1101 isl_basic_set_free(cell->dom);
1102 free(cell);
1104 return NULL;
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)
1113 int i;
1114 isl_vec *c = NULL;
1115 struct isl_tab *tab;
1116 isl_size total;
1118 total = isl_basic_set_dim(bset, isl_dim_all);
1119 if (total < 0)
1120 return NULL;
1121 tab = isl_tab_alloc(bset->ctx, bset->n_eq + bset->n_ineq + 1,
1122 1 + total, 0);
1123 if (!tab)
1124 return NULL;
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)
1128 goto error;
1129 return tab;
1132 c = isl_vec_alloc(bset->ctx, 1 + 1 + total);
1133 if (!c)
1134 goto error;
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)
1140 goto error;
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)
1147 goto error;
1148 if (tab->empty) {
1149 isl_vec_free(c);
1150 return tab;
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)
1157 goto error;
1159 isl_vec_free(c);
1160 return tab;
1161 error:
1162 isl_vec_free(c);
1163 isl_tab_free(tab);
1164 return NULL;
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)
1173 isl_vec *vec;
1174 struct isl_tab *tab;
1176 tab = tab_for_shifted_cone(bset);
1177 vec = isl_tab_get_sample_value(tab);
1178 isl_tab_free(tab);
1179 if (!vec)
1180 return NULL;
1182 isl_seq_cpy(vec->el, vec->el + 1, vec->size - 1);
1183 vec->size--;
1185 return vec;
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)
1200 int i;
1201 isl_vec *vec;
1202 isl_cell *cell;
1204 if (!vertices)
1205 return isl_stat_error;
1207 if (vertices->n_chambers == 0)
1208 return isl_stat_ok;
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);
1214 if (!cell)
1215 return isl_stat_error;
1216 return fn(cell, user);
1219 vec = isl_basic_set_interior_point(vertices->c[0].dom);
1220 if (!vec)
1221 return isl_stat_error;
1223 for (i = 0; i < vertices->n_chambers; ++i) {
1224 int r;
1225 isl_basic_set *dom = isl_basic_set_copy(vertices->c[i].dom);
1226 if (i)
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);
1230 if (!cell)
1231 goto error;
1232 r = fn(cell, user);
1233 if (r < 0)
1234 goto error;
1237 isl_vec_free(vec);
1239 return isl_stat_ok;
1240 error:
1241 isl_vec_free(vec);
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)
1248 int i;
1249 isl_cell *cell;
1251 if (!vertices)
1252 return isl_stat_error;
1254 if (vertices->n_chambers == 0)
1255 return isl_stat_ok;
1257 for (i = 0; i < vertices->n_chambers; ++i) {
1258 isl_stat r;
1259 isl_basic_set *dom = isl_basic_set_copy(vertices->c[i].dom);
1261 cell = isl_cell_alloc(isl_vertices_copy(vertices), dom, i);
1262 if (!cell)
1263 return isl_stat_error;
1265 r = fn(cell, user);
1266 if (r < 0)
1267 return isl_stat_error;
1270 return isl_stat_ok;
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)
1276 int i;
1277 isl_vertex *vertex;
1279 if (!vertices)
1280 return isl_stat_error;
1282 if (vertices->n_vertices == 0)
1283 return isl_stat_ok;
1285 for (i = 0; i < vertices->n_vertices; ++i) {
1286 isl_stat r;
1288 vertex = isl_vertex_alloc(isl_vertices_copy(vertices), i);
1289 if (!vertex)
1290 return isl_stat_error;
1292 r = fn(vertex, user);
1293 if (r < 0)
1294 return isl_stat_error;
1297 return isl_stat_ok;
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)
1303 int i;
1304 isl_vertex *vertex;
1306 if (!cell)
1307 return isl_stat_error;
1309 if (cell->n_vertices == 0)
1310 return isl_stat_ok;
1312 for (i = 0; i < cell->n_vertices; ++i) {
1313 isl_stat r;
1315 vertex = isl_vertex_alloc(isl_vertices_copy(cell->vertices),
1316 cell->ids[i]);
1317 if (!vertex)
1318 return isl_stat_error;
1320 r = fn(vertex, user);
1321 if (r < 0)
1322 return isl_stat_error;
1325 return isl_stat_ok;
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)
1341 int i;
1342 isl_morph *param_morph = NULL;
1344 if (!morph || !vertices)
1345 goto error;
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)
1359 goto error;
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)
1366 goto error;
1369 isl_morph_free(param_morph);
1370 isl_morph_free(morph);
1371 return vertices;
1372 error:
1373 isl_morph_free(param_morph);
1374 isl_morph_free(morph);
1375 isl_vertices_free(vertices);
1376 return NULL;
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)
1386 int i;
1387 isl_ctx *ctx;
1388 struct isl_cell *simplex;
1390 ctx = isl_cell_get_ctx(cell);
1392 simplex = isl_calloc_type(ctx, struct isl_cell);
1393 if (!simplex)
1394 return isl_stat_error;
1395 simplex->vertices = isl_vertices_copy(cell->vertices);
1396 if (!simplex->vertices)
1397 goto error;
1398 simplex->dom = isl_basic_set_copy(cell->dom);
1399 if (!simplex->dom)
1400 goto error;
1401 simplex->n_vertices = n_simplex + n_other;
1402 simplex->ids = isl_alloc_array(ctx, int, simplex->n_vertices);
1403 if (!simplex->ids)
1404 goto error;
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);
1412 error:
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
1423 * to zero.
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
1427 * it up here.
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)
1432 int i;
1433 isl_int m;
1435 isl_seq_cpy(v->el, bset->ineq[facet], v->size);
1437 isl_int_init(m);
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);
1442 isl_int_clear(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)
1461 int i, j, k;
1462 isl_size d, nparam;
1463 int *ids;
1464 isl_ctx *ctx;
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);
1483 if (!ids)
1484 goto error;
1485 for (i = 0; i < bset->n_ineq; ++i) {
1486 if (isl_seq_first_non_zero(bset->ineq[i] + 1 + nparam, d) == -1)
1487 continue;
1488 if (vertex_on_facet(vertex, bset, i, v))
1489 continue;
1491 for (j = 1, k = 0; j < n_other; ++j) {
1492 isl_basic_set *ov;
1493 ov = cell->vertices->v[other_ids[j]].vertex;
1494 if (vertex_on_facet(ov, bset, i, v))
1495 ids[k++] = other_ids[j];
1497 if (k == 0)
1498 continue;
1500 if (triangulate(cell, v, simplex_ids, n_simplex + 1,
1501 ids, k, fn, user) < 0)
1502 goto error;
1504 free(ids);
1506 return isl_stat_ok;
1507 error:
1508 free(ids);
1509 return isl_stat_error;
1512 /* Triangulate the given cell and call "fn" on each of the resulting
1513 * simplices.
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)
1518 isl_size d, total;
1519 isl_stat r;
1520 isl_ctx *ctx;
1521 isl_vec *v = NULL;
1522 int *simplex_ids = NULL;
1524 if (!cell)
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);
1537 if (!simplex_ids)
1538 goto error;
1540 v = isl_vec_alloc(ctx, 1 + total);
1541 if (!v)
1542 goto error;
1544 r = triangulate(cell, v, simplex_ids, 0,
1545 cell->ids, cell->n_vertices, fn, user);
1547 isl_vec_free(v);
1548 free(simplex_ids);
1550 isl_cell_free(cell);
1552 return r;
1553 error:
1554 free(simplex_ids);
1555 isl_vec_free(v);
1556 isl_cell_free(cell);
1557 return isl_stat_error;