drop isl_basic_set_drop_equality
[isl.git] / isl_vertices.c
blob64236e7cc6608e8a7326d0bb9eab36ba1f0c7e43
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 void isl_vertices_free(__isl_take isl_vertices *vertices)
40 int i;
42 if (!vertices)
43 return;
45 if (--vertices->ref > 0)
46 return;
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);
64 struct isl_vertex_list {
65 struct isl_vertex v;
66 struct isl_vertex_list *next;
69 static void free_vertex_list(struct isl_vertex_list *list)
71 struct isl_vertex_list *next;
73 for (; list; list = next) {
74 next = list->next;
75 isl_basic_set_free(list->v.vertex);
76 isl_basic_set_free(list->v.dom);
77 free(list);
81 static __isl_give isl_vertices *vertices_from_list(__isl_keep isl_basic_set *bset,
82 int n_vertices, struct isl_vertex_list *list)
84 int i;
85 struct isl_vertex_list *next;
86 isl_vertices *vertices;
88 vertices = isl_calloc_type(bset->ctx, isl_vertices);
89 if (!vertices)
90 goto error;
91 vertices->ref = 1;
92 vertices->bset = isl_basic_set_copy(bset);
93 vertices->v = isl_alloc_array(bset->ctx, struct isl_vertex, n_vertices);
94 if (n_vertices && !vertices->v)
95 goto error;
96 vertices->n_vertices = n_vertices;
98 for (i = 0; list; list = next, i++) {
99 next = list->next;
100 vertices->v[i] = list->v;
101 free(list);
104 return vertices;
105 error:
106 isl_vertices_free(vertices);
107 free_vertex_list(list);
108 return NULL;
111 /* Prepend a vertex to the linked list "list" based on the equalities in "tab".
112 * Return isl_bool_true if the vertex was actually added and
113 * isl_bool_false otherwise.
114 * In particular, vertices with a lower-dimensional activity domain are
115 * not added to the list because they would not be included in any chamber.
116 * Return isl_bool_error on error.
118 static isl_bool add_vertex(struct isl_vertex_list **list,
119 __isl_keep isl_basic_set *bset, struct isl_tab *tab)
121 unsigned nvar;
122 struct isl_vertex_list *v = NULL;
124 if (isl_tab_detect_implicit_equalities(tab) < 0)
125 return isl_bool_error;
127 nvar = isl_basic_set_dim(bset, isl_dim_set);
129 v = isl_calloc_type(tab->mat->ctx, struct isl_vertex_list);
130 if (!v)
131 goto error;
133 v->v.vertex = isl_basic_set_copy(bset);
134 v->v.vertex = isl_basic_set_cow(v->v.vertex);
135 v->v.vertex = isl_basic_set_update_from_tab(v->v.vertex, tab);
136 v->v.vertex = isl_basic_set_simplify(v->v.vertex);
137 v->v.vertex = isl_basic_set_finalize(v->v.vertex);
138 if (!v->v.vertex)
139 goto error;
140 isl_assert(bset->ctx, v->v.vertex->n_eq >= nvar, goto error);
141 v->v.dom = isl_basic_set_copy(v->v.vertex);
142 v->v.dom = isl_basic_set_params(v->v.dom);
143 if (!v->v.dom)
144 goto error;
146 if (v->v.dom->n_eq > 0) {
147 free_vertex_list(v);
148 return isl_bool_false;
151 v->next = *list;
152 *list = v;
154 return isl_bool_true;
155 error:
156 free_vertex_list(v);
157 return isl_bool_error;
160 /* Compute the parametric vertices and the chamber decomposition
161 * of an empty parametric polytope.
163 static __isl_give isl_vertices *vertices_empty(__isl_keep isl_basic_set *bset)
165 isl_vertices *vertices;
167 if (!bset)
168 return NULL;
170 vertices = isl_calloc_type(bset->ctx, isl_vertices);
171 if (!vertices)
172 return NULL;
173 vertices->bset = isl_basic_set_copy(bset);
174 vertices->ref = 1;
176 vertices->n_vertices = 0;
177 vertices->n_chambers = 0;
179 return vertices;
182 /* Compute the parametric vertices and the chamber decomposition
183 * of the parametric polytope defined using the same constraints
184 * as "bset" in the 0D case.
185 * There is exactly one 0D vertex and a single chamber containing
186 * the vertex.
188 static __isl_give isl_vertices *vertices_0D(__isl_keep isl_basic_set *bset)
190 isl_vertices *vertices;
192 if (!bset)
193 return NULL;
195 vertices = isl_calloc_type(bset->ctx, isl_vertices);
196 if (!vertices)
197 return NULL;
198 vertices->ref = 1;
199 vertices->bset = isl_basic_set_copy(bset);
201 vertices->v = isl_calloc_array(bset->ctx, struct isl_vertex, 1);
202 if (!vertices->v)
203 goto error;
204 vertices->n_vertices = 1;
205 vertices->v[0].vertex = isl_basic_set_copy(bset);
206 vertices->v[0].dom = isl_basic_set_params(isl_basic_set_copy(bset));
207 if (!vertices->v[0].vertex || !vertices->v[0].dom)
208 goto error;
210 vertices->c = isl_calloc_array(bset->ctx, struct isl_chamber, 1);
211 if (!vertices->c)
212 goto error;
213 vertices->n_chambers = 1;
214 vertices->c[0].n_vertices = 1;
215 vertices->c[0].vertices = isl_calloc_array(bset->ctx, int, 1);
216 if (!vertices->c[0].vertices)
217 goto error;
218 vertices->c[0].dom = isl_basic_set_copy(vertices->v[0].dom);
219 if (!vertices->c[0].dom)
220 goto error;
222 return vertices;
223 error:
224 isl_vertices_free(vertices);
225 return NULL;
228 static int isl_mat_rank(__isl_keep isl_mat *mat)
230 int row, col;
231 isl_mat *H;
233 H = isl_mat_left_hermite(isl_mat_copy(mat), 0, NULL, NULL);
234 if (!H)
235 return -1;
237 for (col = 0; col < H->n_col; ++col) {
238 for (row = 0; row < H->n_row; ++row)
239 if (!isl_int_is_zero(H->row[row][col]))
240 break;
241 if (row == H->n_row)
242 break;
245 isl_mat_free(H);
247 return col;
250 /* Is the row pointed to by "f" linearly independent of the "n" first
251 * rows in "facets"?
253 static int is_independent(__isl_keep isl_mat *facets, int n, isl_int *f)
255 int rank;
257 if (isl_seq_first_non_zero(f, facets->n_col) < 0)
258 return 0;
260 isl_seq_cpy(facets->row[n], f, facets->n_col);
261 facets->n_row = n + 1;
262 rank = isl_mat_rank(facets);
263 if (rank < 0)
264 return -1;
266 return rank == n + 1;
269 /* Check whether we can select constraint "level", given the current selection
270 * reflected by facets in "tab", the rows of "facets" and the earlier
271 * "selected" elements of "selection".
273 * If the constraint is (strictly) redundant in the tableau, selecting it would
274 * result in an empty tableau, so it can't be selected.
275 * If the set variable part of the constraint is not linearly independent
276 * of the set variable parts of the already selected constraints,
277 * the constraint cannot be selected.
278 * If selecting the constraint results in an empty tableau, the constraint
279 * cannot be selected.
280 * Finally, if selecting the constraint results in some explicitly
281 * deselected constraints turning into equalities, then the corresponding
282 * vertices have already been generated, so the constraint cannot be selected.
284 static int can_select(__isl_keep isl_basic_set *bset, int level,
285 struct isl_tab *tab, __isl_keep isl_mat *facets, int selected,
286 int *selection)
288 int i;
289 int indep;
290 unsigned ovar;
291 struct isl_tab_undo *snap;
293 if (isl_tab_is_redundant(tab, level))
294 return 0;
296 ovar = isl_space_offset(bset->dim, isl_dim_set);
298 indep = is_independent(facets, selected, bset->ineq[level] + 1 + ovar);
299 if (indep < 0)
300 return -1;
301 if (!indep)
302 return 0;
304 snap = isl_tab_snap(tab);
305 if (isl_tab_select_facet(tab, level) < 0)
306 return -1;
308 if (tab->empty) {
309 if (isl_tab_rollback(tab, snap) < 0)
310 return -1;
311 return 0;
314 for (i = 0; i < level; ++i) {
315 int sgn;
317 if (selection[i] != DESELECTED)
318 continue;
320 if (isl_tab_is_equality(tab, i))
321 sgn = 0;
322 else if (isl_tab_is_redundant(tab, i))
323 sgn = 1;
324 else
325 sgn = isl_tab_sign_of_max(tab, i);
326 if (sgn < -1)
327 return -1;
328 if (sgn <= 0) {
329 if (isl_tab_rollback(tab, snap) < 0)
330 return -1;
331 return 0;
335 return 1;
338 /* Compute the parametric vertices and the chamber decomposition
339 * of a parametric polytope that is not full-dimensional.
341 * Simply map the parametric polytope to a lower dimensional space
342 * and map the resulting vertices back.
344 static __isl_give isl_vertices *lower_dim_vertices(
345 __isl_keep isl_basic_set *bset)
347 isl_morph *morph;
348 isl_vertices *vertices;
350 bset = isl_basic_set_copy(bset);
351 morph = isl_basic_set_full_compression(bset);
352 bset = isl_morph_basic_set(isl_morph_copy(morph), bset);
354 vertices = isl_basic_set_compute_vertices(bset);
355 isl_basic_set_free(bset);
357 morph = isl_morph_inverse(morph);
359 vertices = isl_morph_vertices(morph, vertices);
361 return vertices;
364 /* Compute the parametric vertices and the chamber decomposition
365 * of the parametric polytope defined using the same constraints
366 * as "bset". "bset" is assumed to have no existentially quantified
367 * variables.
369 * The vertices themselves are computed in a fairly simplistic way.
370 * We simply run through all combinations of d constraints,
371 * with d the number of set variables, and check if those d constraints
372 * define a vertex. To avoid the generation of duplicate vertices,
373 * which we may happen if a vertex is defined by more that d constraints,
374 * we make sure we only generate the vertex for the d constraints with
375 * smallest index.
377 * We set up a tableau and keep track of which facets have been
378 * selected. The tableau is marked strict_redundant so that we can be
379 * sure that any constraint that is marked redundant (and that is not
380 * also marked zero) is not an equality.
381 * If a constraint is marked DESELECTED, it means the constraint was
382 * SELECTED before (in combination with the same selection of earlier
383 * constraints). If such a deselected constraint turns out to be an
384 * equality, then any vertex that may still be found with the current
385 * selection has already been generated when the constraint was selected.
386 * A constraint is marked UNSELECTED when there is no way selecting
387 * the constraint could lead to a vertex (in combination with the current
388 * selection of earlier constraints).
390 * The set variable coefficients of the selected constraints are stored
391 * in the facets matrix.
393 __isl_give isl_vertices *isl_basic_set_compute_vertices(
394 __isl_keep isl_basic_set *bset)
396 struct isl_tab *tab;
397 int level;
398 int init;
399 unsigned nvar;
400 int *selection = NULL;
401 int selected;
402 struct isl_tab_undo **snap = NULL;
403 isl_mat *facets = NULL;
404 struct isl_vertex_list *list = NULL;
405 int n_vertices = 0;
406 isl_vertices *vertices;
408 if (!bset)
409 return NULL;
411 if (isl_basic_set_plain_is_empty(bset))
412 return vertices_empty(bset);
414 if (bset->n_eq != 0)
415 return lower_dim_vertices(bset);
417 isl_assert(bset->ctx, isl_basic_set_dim(bset, isl_dim_div) == 0,
418 return NULL);
420 if (isl_basic_set_dim(bset, isl_dim_set) == 0)
421 return vertices_0D(bset);
423 nvar = isl_basic_set_dim(bset, isl_dim_set);
425 bset = isl_basic_set_copy(bset);
426 bset = isl_basic_set_set_rational(bset);
427 if (!bset)
428 return NULL;
430 tab = isl_tab_from_basic_set(bset, 0);
431 if (!tab)
432 goto error;
433 tab->strict_redundant = 1;
435 if (tab->empty) {
436 vertices = vertices_empty(bset);
437 isl_basic_set_free(bset);
438 isl_tab_free(tab);
439 return vertices;
442 selection = isl_alloc_array(bset->ctx, int, bset->n_ineq);
443 snap = isl_alloc_array(bset->ctx, struct isl_tab_undo *, bset->n_ineq);
444 facets = isl_mat_alloc(bset->ctx, nvar, nvar);
445 if ((bset->n_ineq && (!selection || !snap)) || !facets)
446 goto error;
448 level = 0;
449 init = 1;
450 selected = 0;
452 while (level >= 0) {
453 if (level >= bset->n_ineq ||
454 (!init && selection[level] != SELECTED)) {
455 --level;
456 init = 0;
457 continue;
459 if (init) {
460 int ok;
461 snap[level] = isl_tab_snap(tab);
462 ok = can_select(bset, level, tab, facets, selected,
463 selection);
464 if (ok < 0)
465 goto error;
466 if (ok) {
467 selection[level] = SELECTED;
468 selected++;
469 } else
470 selection[level] = UNSELECTED;
471 } else {
472 selection[level] = DESELECTED;
473 selected--;
474 if (isl_tab_rollback(tab, snap[level]) < 0)
475 goto error;
477 if (selected == nvar) {
478 if (tab->n_dead == nvar) {
479 isl_bool added = add_vertex(&list, bset, tab);
480 if (added < 0)
481 goto error;
482 if (added)
483 n_vertices++;
485 init = 0;
486 continue;
488 ++level;
489 init = 1;
492 isl_mat_free(facets);
493 free(selection);
494 free(snap);
496 isl_tab_free(tab);
498 vertices = vertices_from_list(bset, n_vertices, list);
500 vertices = compute_chambers(bset, vertices);
502 return vertices;
503 error:
504 free_vertex_list(list);
505 isl_mat_free(facets);
506 free(selection);
507 free(snap);
508 isl_tab_free(tab);
509 isl_basic_set_free(bset);
510 return NULL;
513 struct isl_chamber_list {
514 struct isl_chamber c;
515 struct isl_chamber_list *next;
518 static void free_chamber_list(struct isl_chamber_list *list)
520 struct isl_chamber_list *next;
522 for (; list; list = next) {
523 next = list->next;
524 isl_basic_set_free(list->c.dom);
525 free(list->c.vertices);
526 free(list);
530 /* Check whether the basic set "bset" is a superset of the basic set described
531 * by "tab", i.e., check whether all constraints of "bset" are redundant.
533 static isl_bool bset_covers_tab(__isl_keep isl_basic_set *bset,
534 struct isl_tab *tab)
536 int i;
538 if (!bset || !tab)
539 return isl_bool_error;
541 for (i = 0; i < bset->n_ineq; ++i) {
542 enum isl_ineq_type type = isl_tab_ineq_type(tab, bset->ineq[i]);
543 switch (type) {
544 case isl_ineq_error: return isl_bool_error;
545 case isl_ineq_redundant: continue;
546 default: return isl_bool_false;
550 return isl_bool_true;
553 static __isl_give isl_vertices *vertices_add_chambers(
554 __isl_take isl_vertices *vertices, int n_chambers,
555 struct isl_chamber_list *list)
557 int i;
558 isl_ctx *ctx;
559 struct isl_chamber_list *next;
561 ctx = isl_vertices_get_ctx(vertices);
562 vertices->c = isl_alloc_array(ctx, struct isl_chamber, n_chambers);
563 if (!vertices->c)
564 goto error;
565 vertices->n_chambers = n_chambers;
567 for (i = 0; list; list = next, i++) {
568 next = list->next;
569 vertices->c[i] = list->c;
570 free(list);
573 return vertices;
574 error:
575 isl_vertices_free(vertices);
576 free_chamber_list(list);
577 return NULL;
580 /* Can "tab" be intersected with "bset" without resulting in
581 * a lower-dimensional set.
582 * "bset" itself is assumed to be full-dimensional.
584 static isl_bool can_intersect(struct isl_tab *tab,
585 __isl_keep isl_basic_set *bset)
587 int i;
588 struct isl_tab_undo *snap;
590 if (bset->n_eq > 0)
591 isl_die(isl_basic_set_get_ctx(bset), isl_error_internal,
592 "expecting full-dimensional input",
593 return isl_bool_error);
595 if (isl_tab_extend_cons(tab, bset->n_ineq) < 0)
596 return isl_bool_error;
598 snap = isl_tab_snap(tab);
600 for (i = 0; i < bset->n_ineq; ++i) {
601 if (isl_tab_ineq_type(tab, bset->ineq[i]) == isl_ineq_redundant)
602 continue;
603 if (isl_tab_add_ineq(tab, bset->ineq[i]) < 0)
604 return isl_bool_error;
607 if (isl_tab_detect_implicit_equalities(tab) < 0)
608 return isl_bool_error;
609 if (tab->n_dead) {
610 if (isl_tab_rollback(tab, snap) < 0)
611 return isl_bool_error;
612 return isl_bool_false;
615 return isl_bool_true;
618 static int add_chamber(struct isl_chamber_list **list,
619 __isl_keep isl_vertices *vertices, struct isl_tab *tab, int *selection)
621 int n_frozen;
622 int i, j;
623 int n_vertices = 0;
624 struct isl_tab_undo *snap;
625 struct isl_chamber_list *c = NULL;
627 for (i = 0; i < vertices->n_vertices; ++i)
628 if (selection[i])
629 n_vertices++;
631 snap = isl_tab_snap(tab);
633 for (i = 0; i < tab->n_con && tab->con[i].frozen; ++i)
634 tab->con[i].frozen = 0;
635 n_frozen = i;
637 if (isl_tab_detect_redundant(tab) < 0)
638 return -1;
640 c = isl_calloc_type(tab->mat->ctx, struct isl_chamber_list);
641 if (!c)
642 goto error;
643 c->c.vertices = isl_alloc_array(tab->mat->ctx, int, n_vertices);
644 if (n_vertices && !c->c.vertices)
645 goto error;
646 c->c.dom = isl_basic_set_copy(isl_tab_peek_bset(tab));
647 c->c.dom = isl_basic_set_set_rational(c->c.dom);
648 c->c.dom = isl_basic_set_cow(c->c.dom);
649 c->c.dom = isl_basic_set_update_from_tab(c->c.dom, tab);
650 c->c.dom = isl_basic_set_simplify(c->c.dom);
651 c->c.dom = isl_basic_set_finalize(c->c.dom);
652 if (!c->c.dom)
653 goto error;
655 c->c.n_vertices = n_vertices;
657 for (i = 0, j = 0; i < vertices->n_vertices; ++i)
658 if (selection[i]) {
659 c->c.vertices[j] = i;
660 j++;
663 c->next = *list;
664 *list = c;
666 for (i = 0; i < n_frozen; ++i)
667 tab->con[i].frozen = 1;
669 if (isl_tab_rollback(tab, snap) < 0)
670 return -1;
672 return 0;
673 error:
674 free_chamber_list(c);
675 return -1;
678 struct isl_facet_todo {
679 struct isl_tab *tab; /* A tableau representation of the facet */
680 isl_basic_set *bset; /* A normalized basic set representation */
681 isl_vec *constraint; /* Constraint pointing to the other side */
682 struct isl_facet_todo *next;
685 static void free_todo(struct isl_facet_todo *todo)
687 while (todo) {
688 struct isl_facet_todo *next = todo->next;
690 isl_tab_free(todo->tab);
691 isl_basic_set_free(todo->bset);
692 isl_vec_free(todo->constraint);
693 free(todo);
695 todo = next;
699 static struct isl_facet_todo *create_todo(struct isl_tab *tab, int con)
701 int i;
702 int n_frozen;
703 struct isl_tab_undo *snap;
704 struct isl_facet_todo *todo;
706 snap = isl_tab_snap(tab);
708 for (i = 0; i < tab->n_con && tab->con[i].frozen; ++i)
709 tab->con[i].frozen = 0;
710 n_frozen = i;
712 if (isl_tab_detect_redundant(tab) < 0)
713 return NULL;
715 todo = isl_calloc_type(tab->mat->ctx, struct isl_facet_todo);
716 if (!todo)
717 return NULL;
719 todo->constraint = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
720 if (!todo->constraint)
721 goto error;
722 isl_seq_neg(todo->constraint->el, tab->bmap->ineq[con], 1 + tab->n_var);
723 todo->bset = isl_basic_set_copy(isl_tab_peek_bset(tab));
724 todo->bset = isl_basic_set_set_rational(todo->bset);
725 todo->bset = isl_basic_set_cow(todo->bset);
726 todo->bset = isl_basic_set_update_from_tab(todo->bset, tab);
727 todo->bset = isl_basic_set_simplify(todo->bset);
728 todo->bset = isl_basic_set_sort_constraints(todo->bset);
729 if (!todo->bset)
730 goto error;
731 ISL_F_SET(todo->bset, ISL_BASIC_SET_NORMALIZED);
732 todo->tab = isl_tab_dup(tab);
733 if (!todo->tab)
734 goto error;
736 for (i = 0; i < n_frozen; ++i)
737 tab->con[i].frozen = 1;
739 if (isl_tab_rollback(tab, snap) < 0)
740 goto error;
742 return todo;
743 error:
744 free_todo(todo);
745 return NULL;
748 /* Create todo items for all interior facets of the chamber represented
749 * by "tab" and collect them in "next".
751 static int init_todo(struct isl_facet_todo **next, struct isl_tab *tab)
753 int i;
754 struct isl_tab_undo *snap;
755 struct isl_facet_todo *todo;
757 snap = isl_tab_snap(tab);
759 for (i = 0; i < tab->n_con; ++i) {
760 if (tab->con[i].frozen)
761 continue;
762 if (tab->con[i].is_redundant)
763 continue;
765 if (isl_tab_select_facet(tab, i) < 0)
766 return -1;
768 todo = create_todo(tab, i);
769 if (!todo)
770 return -1;
772 todo->next = *next;
773 *next = todo;
775 if (isl_tab_rollback(tab, snap) < 0)
776 return -1;
779 return 0;
782 /* Does the linked list contain a todo item that is the opposite of "todo".
783 * If so, return 1 and remove the opposite todo item.
785 static int has_opposite(struct isl_facet_todo *todo,
786 struct isl_facet_todo **list)
788 for (; *list; list = &(*list)->next) {
789 int eq;
790 eq = isl_basic_set_plain_is_equal(todo->bset, (*list)->bset);
791 if (eq < 0)
792 return -1;
793 if (!eq)
794 continue;
795 todo = *list;
796 *list = todo->next;
797 todo->next = NULL;
798 free_todo(todo);
799 return 1;
802 return 0;
805 /* Create todo items for all interior facets of the chamber represented
806 * by "tab" and collect them in first->next, taking care to cancel
807 * opposite todo items.
809 static int update_todo(struct isl_facet_todo *first, struct isl_tab *tab)
811 int i;
812 struct isl_tab_undo *snap;
813 struct isl_facet_todo *todo;
815 snap = isl_tab_snap(tab);
817 for (i = 0; i < tab->n_con; ++i) {
818 int drop;
820 if (tab->con[i].frozen)
821 continue;
822 if (tab->con[i].is_redundant)
823 continue;
825 if (isl_tab_select_facet(tab, i) < 0)
826 return -1;
828 todo = create_todo(tab, i);
829 if (!todo)
830 return -1;
832 drop = has_opposite(todo, &first->next);
833 if (drop < 0)
834 return -1;
836 if (drop)
837 free_todo(todo);
838 else {
839 todo->next = first->next;
840 first->next = todo;
843 if (isl_tab_rollback(tab, snap) < 0)
844 return -1;
847 return 0;
850 /* Compute the chamber decomposition of the parametric polytope respresented
851 * by "bset" given the parametric vertices and their activity domains.
853 * We are only interested in full-dimensional chambers.
854 * Each of these chambers is the intersection of the activity domains of
855 * one or more vertices and the union of all chambers is equal to the
856 * projection of the entire parametric polytope onto the parameter space.
858 * We first create an initial chamber by intersecting as many activity
859 * domains as possible without ending up with an empty or lower-dimensional
860 * set. As a minor optimization, we only consider those activity domains
861 * that contain some arbitrary point.
863 * For each of the interior facets of the chamber, we construct a todo item,
864 * containing the facet and a constraint containing the other side of the facet,
865 * for constructing the chamber on the other side.
866 * While their are any todo items left, we pick a todo item and
867 * create the required chamber by intersecting all activity domains
868 * that contain the facet and have a full-dimensional intersection with
869 * the other side of the facet. For each of the interior facets, we
870 * again create todo items, taking care to cancel opposite todo items.
872 static __isl_give isl_vertices *compute_chambers(__isl_take isl_basic_set *bset,
873 __isl_take isl_vertices *vertices)
875 int i;
876 isl_ctx *ctx;
877 isl_vec *sample = NULL;
878 struct isl_tab *tab = NULL;
879 struct isl_tab_undo *snap;
880 int *selection = NULL;
881 int n_chambers = 0;
882 struct isl_chamber_list *list = NULL;
883 struct isl_facet_todo *todo = NULL;
885 if (!bset || !vertices)
886 goto error;
888 ctx = isl_vertices_get_ctx(vertices);
889 selection = isl_alloc_array(ctx, int, vertices->n_vertices);
890 if (vertices->n_vertices && !selection)
891 goto error;
893 bset = isl_basic_set_params(bset);
895 tab = isl_tab_from_basic_set(bset, 1);
896 if (!tab)
897 goto error;
898 for (i = 0; i < bset->n_ineq; ++i)
899 if (isl_tab_freeze_constraint(tab, i) < 0)
900 goto error;
901 isl_basic_set_free(bset);
903 snap = isl_tab_snap(tab);
905 sample = isl_tab_get_sample_value(tab);
907 for (i = 0; i < vertices->n_vertices; ++i) {
908 selection[i] = isl_basic_set_contains(vertices->v[i].dom, sample);
909 if (selection[i] < 0)
910 goto error;
911 if (!selection[i])
912 continue;
913 selection[i] = can_intersect(tab, vertices->v[i].dom);
914 if (selection[i] < 0)
915 goto error;
918 if (isl_tab_detect_redundant(tab) < 0)
919 goto error;
921 if (add_chamber(&list, vertices, tab, selection) < 0)
922 goto error;
923 n_chambers++;
925 if (init_todo(&todo, tab) < 0)
926 goto error;
928 while (todo) {
929 struct isl_facet_todo *next;
931 if (isl_tab_rollback(tab, snap) < 0)
932 goto error;
934 if (isl_tab_add_ineq(tab, todo->constraint->el) < 0)
935 goto error;
936 if (isl_tab_freeze_constraint(tab, tab->n_con - 1) < 0)
937 goto error;
939 for (i = 0; i < vertices->n_vertices; ++i) {
940 selection[i] = bset_covers_tab(vertices->v[i].dom,
941 todo->tab);
942 if (selection[i] < 0)
943 goto error;
944 if (!selection[i])
945 continue;
946 selection[i] = can_intersect(tab, vertices->v[i].dom);
947 if (selection[i] < 0)
948 goto error;
951 if (isl_tab_detect_redundant(tab) < 0)
952 goto error;
954 if (add_chamber(&list, vertices, tab, selection) < 0)
955 goto error;
956 n_chambers++;
958 if (update_todo(todo, tab) < 0)
959 goto error;
961 next = todo->next;
962 todo->next = NULL;
963 free_todo(todo);
964 todo = next;
967 isl_vec_free(sample);
969 isl_tab_free(tab);
970 free(selection);
972 vertices = vertices_add_chambers(vertices, n_chambers, list);
974 for (i = 0; vertices && i < vertices->n_vertices; ++i) {
975 isl_basic_set_free(vertices->v[i].dom);
976 vertices->v[i].dom = NULL;
979 return vertices;
980 error:
981 free_chamber_list(list);
982 free_todo(todo);
983 isl_vec_free(sample);
984 isl_tab_free(tab);
985 free(selection);
986 if (!tab)
987 isl_basic_set_free(bset);
988 isl_vertices_free(vertices);
989 return NULL;
992 isl_ctx *isl_vertex_get_ctx(__isl_keep isl_vertex *vertex)
994 return vertex ? isl_vertices_get_ctx(vertex->vertices) : NULL;
997 int isl_vertex_get_id(__isl_keep isl_vertex *vertex)
999 return vertex ? vertex->id : -1;
1002 __isl_give isl_basic_set *isl_basic_set_set_integral(__isl_take isl_basic_set *bset)
1004 if (!bset)
1005 return NULL;
1007 if (!ISL_F_ISSET(bset, ISL_BASIC_MAP_RATIONAL))
1008 return bset;
1010 bset = isl_basic_set_cow(bset);
1011 if (!bset)
1012 return NULL;
1014 ISL_F_CLR(bset, ISL_BASIC_MAP_RATIONAL);
1016 return isl_basic_set_finalize(bset);
1019 /* Return the activity domain of the vertex "vertex".
1021 __isl_give isl_basic_set *isl_vertex_get_domain(__isl_keep isl_vertex *vertex)
1023 struct isl_vertex *v;
1025 if (!vertex)
1026 return NULL;
1028 v = &vertex->vertices->v[vertex->id];
1029 if (!v->dom) {
1030 v->dom = isl_basic_set_copy(v->vertex);
1031 v->dom = isl_basic_set_params(v->dom);
1032 v->dom = isl_basic_set_set_integral(v->dom);
1035 return isl_basic_set_copy(v->dom);
1038 /* Return a multiple quasi-affine expression describing the vertex "vertex"
1039 * in terms of the parameters,
1041 __isl_give isl_multi_aff *isl_vertex_get_expr(__isl_keep isl_vertex *vertex)
1043 struct isl_vertex *v;
1044 isl_basic_set *bset;
1046 if (!vertex)
1047 return NULL;
1049 v = &vertex->vertices->v[vertex->id];
1051 bset = isl_basic_set_copy(v->vertex);
1052 return isl_multi_aff_from_basic_set_equalities(bset);
1055 static __isl_give isl_vertex *isl_vertex_alloc(__isl_take isl_vertices *vertices,
1056 int id)
1058 isl_ctx *ctx;
1059 isl_vertex *vertex;
1061 if (!vertices)
1062 return NULL;
1064 ctx = isl_vertices_get_ctx(vertices);
1065 vertex = isl_alloc_type(ctx, isl_vertex);
1066 if (!vertex)
1067 goto error;
1069 vertex->vertices = vertices;
1070 vertex->id = id;
1072 return vertex;
1073 error:
1074 isl_vertices_free(vertices);
1075 return NULL;
1078 void isl_vertex_free(__isl_take isl_vertex *vertex)
1080 if (!vertex)
1081 return;
1082 isl_vertices_free(vertex->vertices);
1083 free(vertex);
1086 isl_ctx *isl_cell_get_ctx(__isl_keep isl_cell *cell)
1088 return cell ? cell->dom->ctx : NULL;
1091 __isl_give isl_basic_set *isl_cell_get_domain(__isl_keep isl_cell *cell)
1093 return cell ? isl_basic_set_copy(cell->dom) : NULL;
1096 static __isl_give isl_cell *isl_cell_alloc(__isl_take isl_vertices *vertices,
1097 __isl_take isl_basic_set *dom, int id)
1099 int i;
1100 isl_cell *cell = NULL;
1102 if (!vertices || !dom)
1103 goto error;
1105 cell = isl_calloc_type(dom->ctx, isl_cell);
1106 if (!cell)
1107 goto error;
1109 cell->n_vertices = vertices->c[id].n_vertices;
1110 cell->ids = isl_alloc_array(dom->ctx, int, cell->n_vertices);
1111 if (cell->n_vertices && !cell->ids)
1112 goto error;
1113 for (i = 0; i < cell->n_vertices; ++i)
1114 cell->ids[i] = vertices->c[id].vertices[i];
1115 cell->vertices = vertices;
1116 cell->dom = dom;
1118 return cell;
1119 error:
1120 isl_cell_free(cell);
1121 isl_vertices_free(vertices);
1122 isl_basic_set_free(dom);
1123 return NULL;
1126 void isl_cell_free(__isl_take isl_cell *cell)
1128 if (!cell)
1129 return;
1131 isl_vertices_free(cell->vertices);
1132 free(cell->ids);
1133 isl_basic_set_free(cell->dom);
1134 free(cell);
1137 /* Create a tableau of the cone obtained by first homogenizing the given
1138 * polytope and then making all inequalities strict by setting the
1139 * constant term to -1.
1141 static struct isl_tab *tab_for_shifted_cone(__isl_keep isl_basic_set *bset)
1143 int i;
1144 isl_vec *c = NULL;
1145 struct isl_tab *tab;
1147 if (!bset)
1148 return NULL;
1149 tab = isl_tab_alloc(bset->ctx, bset->n_eq + bset->n_ineq + 1,
1150 1 + isl_basic_set_total_dim(bset), 0);
1151 if (!tab)
1152 return NULL;
1153 tab->rational = ISL_F_ISSET(bset, ISL_BASIC_SET_RATIONAL);
1154 if (ISL_F_ISSET(bset, ISL_BASIC_MAP_EMPTY)) {
1155 if (isl_tab_mark_empty(tab) < 0)
1156 goto error;
1157 return tab;
1160 c = isl_vec_alloc(bset->ctx, 1 + 1 + isl_basic_set_total_dim(bset));
1161 if (!c)
1162 goto error;
1164 isl_int_set_si(c->el[0], 0);
1165 for (i = 0; i < bset->n_eq; ++i) {
1166 isl_seq_cpy(c->el + 1, bset->eq[i], c->size - 1);
1167 if (isl_tab_add_eq(tab, c->el) < 0)
1168 goto error;
1171 isl_int_set_si(c->el[0], -1);
1172 for (i = 0; i < bset->n_ineq; ++i) {
1173 isl_seq_cpy(c->el + 1, bset->ineq[i], c->size - 1);
1174 if (isl_tab_add_ineq(tab, c->el) < 0)
1175 goto error;
1176 if (tab->empty) {
1177 isl_vec_free(c);
1178 return tab;
1182 isl_seq_clr(c->el + 1, c->size - 1);
1183 isl_int_set_si(c->el[1], 1);
1184 if (isl_tab_add_ineq(tab, c->el) < 0)
1185 goto error;
1187 isl_vec_free(c);
1188 return tab;
1189 error:
1190 isl_vec_free(c);
1191 isl_tab_free(tab);
1192 return NULL;
1195 /* Compute an interior point of "bset" by selecting an interior
1196 * point in homogeneous space and projecting the point back down.
1198 static __isl_give isl_vec *isl_basic_set_interior_point(
1199 __isl_keep isl_basic_set *bset)
1201 isl_vec *vec;
1202 struct isl_tab *tab;
1204 tab = tab_for_shifted_cone(bset);
1205 vec = isl_tab_get_sample_value(tab);
1206 isl_tab_free(tab);
1207 if (!vec)
1208 return NULL;
1210 isl_seq_cpy(vec->el, vec->el + 1, vec->size - 1);
1211 vec->size--;
1213 return vec;
1216 /* Call "fn" on all chambers of the parametric polytope with the shared
1217 * facets of neighboring chambers only appearing in one of the chambers.
1219 * We pick an interior point from one of the chambers and then make
1220 * all constraints that do not satisfy this point strict.
1221 * For constraints that saturate the interior point, the sign
1222 * of the first non-zero coefficient is used to determine which
1223 * of the two (internal) constraints should be tightened.
1225 isl_stat isl_vertices_foreach_disjoint_cell(__isl_keep isl_vertices *vertices,
1226 isl_stat (*fn)(__isl_take isl_cell *cell, void *user), void *user)
1228 int i;
1229 isl_vec *vec;
1230 isl_cell *cell;
1232 if (!vertices)
1233 return isl_stat_error;
1235 if (vertices->n_chambers == 0)
1236 return isl_stat_ok;
1238 if (vertices->n_chambers == 1) {
1239 isl_basic_set *dom = isl_basic_set_copy(vertices->c[0].dom);
1240 dom = isl_basic_set_set_integral(dom);
1241 cell = isl_cell_alloc(isl_vertices_copy(vertices), dom, 0);
1242 if (!cell)
1243 return isl_stat_error;
1244 return fn(cell, user);
1247 vec = isl_basic_set_interior_point(vertices->c[0].dom);
1248 if (!vec)
1249 return isl_stat_error;
1251 for (i = 0; i < vertices->n_chambers; ++i) {
1252 int r;
1253 isl_basic_set *dom = isl_basic_set_copy(vertices->c[i].dom);
1254 if (i)
1255 dom = isl_basic_set_tighten_outward(dom, vec);
1256 dom = isl_basic_set_set_integral(dom);
1257 cell = isl_cell_alloc(isl_vertices_copy(vertices), dom, i);
1258 if (!cell)
1259 goto error;
1260 r = fn(cell, user);
1261 if (r < 0)
1262 goto error;
1265 isl_vec_free(vec);
1267 return isl_stat_ok;
1268 error:
1269 isl_vec_free(vec);
1270 return isl_stat_error;
1273 isl_stat isl_vertices_foreach_cell(__isl_keep isl_vertices *vertices,
1274 isl_stat (*fn)(__isl_take isl_cell *cell, void *user), void *user)
1276 int i;
1277 isl_cell *cell;
1279 if (!vertices)
1280 return isl_stat_error;
1282 if (vertices->n_chambers == 0)
1283 return isl_stat_ok;
1285 for (i = 0; i < vertices->n_chambers; ++i) {
1286 isl_stat r;
1287 isl_basic_set *dom = isl_basic_set_copy(vertices->c[i].dom);
1289 cell = isl_cell_alloc(isl_vertices_copy(vertices), dom, i);
1290 if (!cell)
1291 return isl_stat_error;
1293 r = fn(cell, user);
1294 if (r < 0)
1295 return isl_stat_error;
1298 return isl_stat_ok;
1301 isl_stat isl_vertices_foreach_vertex(__isl_keep isl_vertices *vertices,
1302 isl_stat (*fn)(__isl_take isl_vertex *vertex, void *user), void *user)
1304 int i;
1305 isl_vertex *vertex;
1307 if (!vertices)
1308 return isl_stat_error;
1310 if (vertices->n_vertices == 0)
1311 return isl_stat_ok;
1313 for (i = 0; i < vertices->n_vertices; ++i) {
1314 isl_stat r;
1316 vertex = isl_vertex_alloc(isl_vertices_copy(vertices), 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_stat isl_cell_foreach_vertex(__isl_keep isl_cell *cell,
1329 isl_stat (*fn)(__isl_take isl_vertex *vertex, void *user), void *user)
1331 int i;
1332 isl_vertex *vertex;
1334 if (!cell)
1335 return isl_stat_error;
1337 if (cell->n_vertices == 0)
1338 return isl_stat_ok;
1340 for (i = 0; i < cell->n_vertices; ++i) {
1341 isl_stat r;
1343 vertex = isl_vertex_alloc(isl_vertices_copy(cell->vertices),
1344 cell->ids[i]);
1345 if (!vertex)
1346 return isl_stat_error;
1348 r = fn(vertex, user);
1349 if (r < 0)
1350 return isl_stat_error;
1353 return isl_stat_ok;
1356 isl_ctx *isl_vertices_get_ctx(__isl_keep isl_vertices *vertices)
1358 return vertices ? vertices->bset->ctx : NULL;
1361 int isl_vertices_get_n_vertices(__isl_keep isl_vertices *vertices)
1363 return vertices ? vertices->n_vertices : -1;
1366 __isl_give isl_vertices *isl_morph_vertices(__isl_take isl_morph *morph,
1367 __isl_take isl_vertices *vertices)
1369 int i;
1370 isl_morph *param_morph = NULL;
1372 if (!morph || !vertices)
1373 goto error;
1375 isl_assert(vertices->bset->ctx, vertices->ref == 1, goto error);
1377 param_morph = isl_morph_copy(morph);
1378 param_morph = isl_morph_dom_params(param_morph);
1379 param_morph = isl_morph_ran_params(param_morph);
1381 for (i = 0; i < vertices->n_vertices; ++i) {
1382 vertices->v[i].dom = isl_morph_basic_set(
1383 isl_morph_copy(param_morph), vertices->v[i].dom);
1384 vertices->v[i].vertex = isl_morph_basic_set(
1385 isl_morph_copy(morph), vertices->v[i].vertex);
1386 if (!vertices->v[i].vertex)
1387 goto error;
1390 for (i = 0; i < vertices->n_chambers; ++i) {
1391 vertices->c[i].dom = isl_morph_basic_set(
1392 isl_morph_copy(param_morph), vertices->c[i].dom);
1393 if (!vertices->c[i].dom)
1394 goto error;
1397 isl_morph_free(param_morph);
1398 isl_morph_free(morph);
1399 return vertices;
1400 error:
1401 isl_morph_free(param_morph);
1402 isl_morph_free(morph);
1403 isl_vertices_free(vertices);
1404 return NULL;
1407 /* Construct a simplex isl_cell spanned by the vertices with indices in
1408 * "simplex_ids" and "other_ids" and call "fn" on this isl_cell.
1410 static isl_stat call_on_simplex(__isl_keep isl_cell *cell,
1411 int *simplex_ids, int n_simplex, int *other_ids, int n_other,
1412 isl_stat (*fn)(__isl_take isl_cell *simplex, void *user), void *user)
1414 int i;
1415 isl_ctx *ctx;
1416 struct isl_cell *simplex;
1418 ctx = isl_cell_get_ctx(cell);
1420 simplex = isl_calloc_type(ctx, struct isl_cell);
1421 if (!simplex)
1422 return isl_stat_error;
1423 simplex->vertices = isl_vertices_copy(cell->vertices);
1424 if (!simplex->vertices)
1425 goto error;
1426 simplex->dom = isl_basic_set_copy(cell->dom);
1427 if (!simplex->dom)
1428 goto error;
1429 simplex->n_vertices = n_simplex + n_other;
1430 simplex->ids = isl_alloc_array(ctx, int, simplex->n_vertices);
1431 if (!simplex->ids)
1432 goto error;
1434 for (i = 0; i < n_simplex; ++i)
1435 simplex->ids[i] = simplex_ids[i];
1436 for (i = 0; i < n_other; ++i)
1437 simplex->ids[n_simplex + i] = other_ids[i];
1439 return fn(simplex, user);
1440 error:
1441 isl_cell_free(simplex);
1442 return isl_stat_error;
1445 /* Check whether the parametric vertex described by "vertex"
1446 * lies on the facet corresponding to constraint "facet" of "bset".
1447 * The isl_vec "v" is a temporary vector than can be used by this function.
1449 * We eliminate the variables from the facet constraint using the
1450 * equalities defining the vertex and check if the result is identical
1451 * to zero.
1453 * It would probably be better to keep track of the constraints defining
1454 * a vertex during the vertex construction so that we could simply look
1455 * it up here.
1457 static int vertex_on_facet(__isl_keep isl_basic_set *vertex,
1458 __isl_keep isl_basic_set *bset, int facet, __isl_keep isl_vec *v)
1460 int i;
1461 isl_int m;
1463 isl_seq_cpy(v->el, bset->ineq[facet], v->size);
1465 isl_int_init(m);
1466 for (i = 0; i < vertex->n_eq; ++i) {
1467 int k = isl_seq_last_non_zero(vertex->eq[i], v->size);
1468 isl_seq_elim(v->el, vertex->eq[i], k, v->size, &m);
1470 isl_int_clear(m);
1472 return isl_seq_first_non_zero(v->el, v->size) == -1;
1475 /* Triangulate the polytope spanned by the vertices with ids
1476 * in "simplex_ids" and "other_ids" and call "fn" on each of
1477 * the resulting simplices.
1478 * If the input polytope is already a simplex, we simply call "fn".
1479 * Otherwise, we pick a point from "other_ids" and add it to "simplex_ids".
1480 * Then we consider each facet of "bset" that does not contain the point
1481 * we just picked, but does contain some of the other points in "other_ids"
1482 * and call ourselves recursively on the polytope spanned by the new
1483 * "simplex_ids" and those points in "other_ids" that lie on the facet.
1485 static isl_stat triangulate(__isl_keep isl_cell *cell, __isl_keep isl_vec *v,
1486 int *simplex_ids, int n_simplex, int *other_ids, int n_other,
1487 isl_stat (*fn)(__isl_take isl_cell *simplex, void *user), void *user)
1489 int i, j, k;
1490 int d, nparam;
1491 int *ids;
1492 isl_ctx *ctx;
1493 isl_basic_set *vertex;
1494 isl_basic_set *bset;
1496 ctx = isl_cell_get_ctx(cell);
1497 d = isl_basic_set_dim(cell->vertices->bset, isl_dim_set);
1498 nparam = isl_basic_set_dim(cell->vertices->bset, isl_dim_param);
1500 if (n_simplex + n_other == d + 1)
1501 return call_on_simplex(cell, simplex_ids, n_simplex,
1502 other_ids, n_other, fn, user);
1504 simplex_ids[n_simplex] = other_ids[0];
1505 vertex = cell->vertices->v[other_ids[0]].vertex;
1506 bset = cell->vertices->bset;
1508 ids = isl_alloc_array(ctx, int, n_other - 1);
1509 if (!ids)
1510 goto error;
1511 for (i = 0; i < bset->n_ineq; ++i) {
1512 if (isl_seq_first_non_zero(bset->ineq[i] + 1 + nparam, d) == -1)
1513 continue;
1514 if (vertex_on_facet(vertex, bset, i, v))
1515 continue;
1517 for (j = 1, k = 0; j < n_other; ++j) {
1518 isl_basic_set *ov;
1519 ov = cell->vertices->v[other_ids[j]].vertex;
1520 if (vertex_on_facet(ov, bset, i, v))
1521 ids[k++] = other_ids[j];
1523 if (k == 0)
1524 continue;
1526 if (triangulate(cell, v, simplex_ids, n_simplex + 1,
1527 ids, k, fn, user) < 0)
1528 goto error;
1530 free(ids);
1532 return isl_stat_ok;
1533 error:
1534 free(ids);
1535 return isl_stat_error;
1538 /* Triangulate the given cell and call "fn" on each of the resulting
1539 * simplices.
1541 isl_stat isl_cell_foreach_simplex(__isl_take isl_cell *cell,
1542 isl_stat (*fn)(__isl_take isl_cell *simplex, void *user), void *user)
1544 int d, total;
1545 int r;
1546 isl_ctx *ctx;
1547 isl_vec *v = NULL;
1548 int *simplex_ids = NULL;
1550 if (!cell)
1551 return isl_stat_error;
1553 d = isl_basic_set_dim(cell->vertices->bset, isl_dim_set);
1554 total = isl_basic_set_total_dim(cell->vertices->bset);
1556 if (cell->n_vertices == d + 1)
1557 return fn(cell, user);
1559 ctx = isl_cell_get_ctx(cell);
1560 simplex_ids = isl_alloc_array(ctx, int, d + 1);
1561 if (!simplex_ids)
1562 goto error;
1564 v = isl_vec_alloc(ctx, 1 + total);
1565 if (!v)
1566 goto error;
1568 r = triangulate(cell, v, simplex_ids, 0,
1569 cell->ids, cell->n_vertices, fn, user);
1571 isl_vec_free(v);
1572 free(simplex_ids);
1574 isl_cell_free(cell);
1576 return r;
1577 error:
1578 free(simplex_ids);
1579 isl_vec_free(v);
1580 isl_cell_free(cell);
1581 return isl_stat_error;