add isl_aff_align_params
[isl.git] / isl_vertices.c
blob77915fb862207a858de6797b318ba18a7716d63a
1 /*
2 * Copyright 2010 INRIA Saclay
4 * Use of this software is governed by the GNU LGPLv2.1 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/set.h>
13 #include <isl/seq.h>
14 #include <isl_tab.h>
15 #include <isl_space_private.h>
16 #include <isl_morph.h>
17 #include <isl_vertices_private.h>
18 #include <isl_mat_private.h>
20 #define SELECTED 1
21 #define DESELECTED -1
22 #define UNSELECTED 0
24 static __isl_give isl_vertices *compute_chambers(__isl_take isl_basic_set *bset,
25 __isl_take isl_vertices *vertices);
27 __isl_give isl_vertices *isl_vertices_copy(__isl_keep isl_vertices *vertices)
29 if (!vertices)
30 return NULL;
32 vertices->ref++;
33 return vertices;
36 void isl_vertices_free(__isl_take isl_vertices *vertices)
38 int i;
40 if (!vertices)
41 return;
43 if (--vertices->ref > 0)
44 return;
46 for (i = 0; i < vertices->n_vertices; ++i) {
47 isl_basic_set_free(vertices->v[i].vertex);
48 isl_basic_set_free(vertices->v[i].dom);
50 free(vertices->v);
52 for (i = 0; i < vertices->n_chambers; ++i) {
53 free(vertices->c[i].vertices);
54 isl_basic_set_free(vertices->c[i].dom);
56 free(vertices->c);
58 isl_basic_set_free(vertices->bset);
59 free(vertices);
62 struct isl_vertex_list {
63 struct isl_vertex v;
64 struct isl_vertex_list *next;
67 static void free_vertex_list(struct isl_vertex_list *list)
69 struct isl_vertex_list *next;
71 for (; list; list = next) {
72 next = list->next;
73 isl_basic_set_free(list->v.vertex);
74 isl_basic_set_free(list->v.dom);
75 free(list);
79 static __isl_give isl_vertices *vertices_from_list(__isl_keep isl_basic_set *bset,
80 int n_vertices, struct isl_vertex_list *list)
82 int i;
83 struct isl_vertex_list *next;
84 isl_vertices *vertices;
86 vertices = isl_calloc_type(bset->ctx, isl_vertices);
87 if (!vertices)
88 goto error;
89 vertices->ref = 1;
90 vertices->bset = isl_basic_set_copy(bset);
91 vertices->v = isl_alloc_array(bset->ctx, struct isl_vertex, n_vertices);
92 if (!vertices->v)
93 goto error;
94 vertices->n_vertices = n_vertices;
96 for (i = 0; list; list = next, i++) {
97 next = list->next;
98 vertices->v[i] = list->v;
99 free(list);
102 return vertices;
103 error:
104 free(vertices);
105 free_vertex_list(list);
106 return NULL;
109 /* Prepend a vertex to the linked list "list" based on the equalities in "tab".
111 static int add_vertex(struct isl_vertex_list **list,
112 __isl_keep isl_basic_set *bset, struct isl_tab *tab)
114 unsigned nvar;
115 unsigned nparam;
116 struct isl_vertex_list *v = NULL;
118 if (isl_tab_detect_implicit_equalities(tab) < 0)
119 return -1;
121 nvar = isl_basic_set_dim(bset, isl_dim_set);
122 nparam = isl_basic_set_dim(bset, isl_dim_param);
124 v = isl_calloc_type(tab->mat->ctx, struct isl_vertex_list);
125 if (!v)
126 goto error;
128 v->v.vertex = isl_basic_set_copy(bset);
129 v->v.vertex = isl_basic_set_cow(v->v.vertex);
130 v->v.vertex = isl_basic_set_update_from_tab(v->v.vertex, tab);
131 v->v.vertex = isl_basic_set_simplify(v->v.vertex);
132 v->v.vertex = isl_basic_set_finalize(v->v.vertex);
133 if (!v->v.vertex)
134 goto error;
135 isl_assert(bset->ctx, v->v.vertex->n_eq >= nvar, goto error);
136 v->v.dom = isl_basic_set_copy(v->v.vertex);
137 v->v.dom = isl_basic_set_project_out(v->v.dom, isl_dim_set, 0, nvar);
138 if (!v->v.dom)
139 goto error;
141 v->next = *list;
142 *list = v;
144 return 0;
145 error:
146 free_vertex_list(v);
147 return -1;
150 /* Compute the parametric vertices and the chamber decomposition
151 * of an empty parametric polytope.
153 static __isl_give isl_vertices *vertices_empty(__isl_keep isl_basic_set *bset)
155 isl_vertices *vertices;
156 unsigned nparam;
158 if (!bset)
159 return NULL;
161 nparam = isl_basic_set_dim(bset, isl_dim_param);
163 vertices = isl_calloc_type(bset->ctx, isl_vertices);
164 if (!vertices)
165 return NULL;
166 vertices->bset = isl_basic_set_copy(bset);
167 vertices->ref = 1;
169 vertices->n_vertices = 0;
170 vertices->n_chambers = 0;
172 return vertices;
175 /* Compute the parametric vertices and the chamber decomposition
176 * of the parametric polytope defined using the same constraints
177 * as "bset" in the 0D case.
178 * There is exactly one 0D vertex and a single chamber containing
179 * the vertex.
181 static __isl_give isl_vertices *vertices_0D(__isl_keep isl_basic_set *bset)
183 isl_vertices *vertices;
184 unsigned nparam;
186 if (!bset)
187 return NULL;
189 nparam = isl_basic_set_dim(bset, isl_dim_param);
191 vertices = isl_calloc_type(bset->ctx, isl_vertices);
192 if (!vertices)
193 return NULL;
194 vertices->ref = 1;
195 vertices->bset = isl_basic_set_copy(bset);
197 vertices->v = isl_calloc_array(bset->ctx, struct isl_vertex, 1);
198 if (!vertices->v)
199 goto error;
200 vertices->n_vertices = 1;
201 vertices->v[0].vertex = isl_basic_set_copy(bset);
202 vertices->v[0].dom = isl_basic_set_params(isl_basic_set_copy(bset));
203 if (!vertices->v[0].vertex || !vertices->v[0].dom)
204 goto error;
206 vertices->c = isl_calloc_array(bset->ctx, struct isl_chamber, 1);
207 if (!vertices->c)
208 goto error;
209 vertices->n_chambers = 1;
210 vertices->c[0].n_vertices = 1;
211 vertices->c[0].vertices = isl_calloc_array(bset->ctx, int, 1);
212 if (!vertices->c[0].vertices)
213 goto error;
214 vertices->c[0].dom = isl_basic_set_copy(vertices->v[0].dom);
215 if (!vertices->c[0].dom)
216 goto error;
218 return vertices;
219 error:
220 isl_vertices_free(vertices);
221 return NULL;
224 static int isl_mat_rank(__isl_keep isl_mat *mat)
226 int row, col;
227 isl_mat *H;
229 H = isl_mat_left_hermite(isl_mat_copy(mat), 0, NULL, NULL);
230 if (!H)
231 return -1;
233 for (col = 0; col < H->n_col; ++col) {
234 for (row = 0; row < H->n_row; ++row)
235 if (!isl_int_is_zero(H->row[row][col]))
236 break;
237 if (row == H->n_row)
238 break;
241 isl_mat_free(H);
243 return col;
246 /* Is the row pointed to by "f" linearly independent of the "n" first
247 * rows in "facets"?
249 static int is_independent(__isl_keep isl_mat *facets, int n, isl_int *f)
251 int rank;
253 if (isl_seq_first_non_zero(f, facets->n_col) < 0)
254 return 0;
256 isl_seq_cpy(facets->row[n], f, facets->n_col);
257 facets->n_row = n + 1;
258 rank = isl_mat_rank(facets);
259 if (rank < 0)
260 return -1;
262 return rank == n + 1;
265 /* Check whether we can select constraint "level", given the current selection
266 * reflected by facets in "tab", the rows of "facets" and the earlier
267 * "selected" elements of "selection".
269 * If the constraint is (strictly) redundant in the tableau, selecting it would
270 * result in an empty tableau, so it can't be selected.
271 * If the set variable part of the constraint is not linearly indepedent
272 * of the set variable parts of the already selected constraints,
273 * the constraint cannot be selected.
274 * If selecting the constraint results in an empty tableau, the constraint
275 * cannot be selected.
276 * Finally, if selecting the constraint results in some explicitly
277 * deselected constraints turning into equalities, then the corresponding
278 * vertices have already been generated, so the constraint cannot be selected.
280 static int can_select(__isl_keep isl_basic_set *bset, int level,
281 struct isl_tab *tab, __isl_keep isl_mat *facets, int selected,
282 int *selection)
284 int i;
285 int indep;
286 unsigned ovar;
287 struct isl_tab_undo *snap;
289 if (isl_tab_is_redundant(tab, level))
290 return 0;
292 ovar = isl_space_offset(bset->dim, isl_dim_set);
294 indep = is_independent(facets, selected, bset->ineq[level] + 1 + ovar);
295 if (indep < 0)
296 return -1;
297 if (!indep)
298 return 0;
300 snap = isl_tab_snap(tab);
301 if (isl_tab_select_facet(tab, level) < 0)
302 return -1;
304 if (tab->empty) {
305 if (isl_tab_rollback(tab, snap) < 0)
306 return -1;
307 return 0;
310 for (i = 0; i < level; ++i) {
311 int sgn;
313 if (selection[i] != DESELECTED)
314 continue;
316 if (isl_tab_is_equality(tab, i))
317 sgn = 0;
318 else if (isl_tab_is_redundant(tab, i))
319 sgn = 1;
320 else
321 sgn = isl_tab_sign_of_max(tab, i);
322 if (sgn < -1)
323 return -1;
324 if (sgn <= 0) {
325 if (isl_tab_rollback(tab, snap) < 0)
326 return -1;
327 return 0;
331 return 1;
334 /* Compute the parametric vertices and the chamber decomposition
335 * of a parametric polytope that is not full-dimensional.
337 * Simply map the parametric polytope to a lower dimensional space
338 * and map the resulting vertices back.
340 static __isl_give isl_vertices *lower_dim_vertices(
341 __isl_keep isl_basic_set *bset)
343 isl_morph *morph;
344 isl_vertices *vertices;
346 bset = isl_basic_set_copy(bset);
347 morph = isl_basic_set_full_compression(bset);
348 bset = isl_morph_basic_set(isl_morph_copy(morph), bset);
350 vertices = isl_basic_set_compute_vertices(bset);
351 isl_basic_set_free(bset);
353 morph = isl_morph_inverse(morph);
355 vertices = isl_morph_vertices(morph, vertices);
357 return vertices;
360 /* Compute the parametric vertices and the chamber decomposition
361 * of the parametric polytope defined using the same constraints
362 * as "bset". "bset" is assumed to have no existentially quantified
363 * variables.
365 * The vertices themselves are computed in a fairly simplistic way.
366 * We simply run through all combinations of d constraints,
367 * with d the number of set variables, and check if those d constraints
368 * define a vertex. To avoid the generation of duplicate vertices,
369 * which we may happen if a vertex is defined by more that d constraints,
370 * we make sure we only generate the vertex for the d constraints with
371 * smallest index.
373 * We set up a tableau and keep track of which facets have been
374 * selected. The tableau is marked strict_redundant so that we can be
375 * sure that any constraint that is marked redundant (and that is not
376 * also marked zero) is not an equality.
377 * If a constraint is marked DESELECTED, it means the constraint was
378 * SELECTED before (in combination with the same selection of earlier
379 * constraints). If such a deselected constraint turns out to be an
380 * equality, then any vertex that may still be found with the current
381 * selection has already been generated when the constraint was selected.
382 * A constraint is marked UNSELECTED when there is no way selecting
383 * the constraint could lead to a vertex (in combination with the current
384 * selection of earlier constraints).
386 * The set variable coefficients of the selected constraints are stored
387 * in the facets matrix.
389 __isl_give isl_vertices *isl_basic_set_compute_vertices(
390 __isl_keep isl_basic_set *bset)
392 struct isl_tab *tab;
393 int level;
394 int init;
395 unsigned nvar;
396 int *selection = NULL;
397 int selected;
398 struct isl_tab_undo **snap = NULL;
399 isl_mat *facets = NULL;
400 struct isl_vertex_list *list = NULL;
401 int n_vertices = 0;
402 isl_vertices *vertices;
404 if (!bset)
405 return NULL;
407 if (isl_basic_set_plain_is_empty(bset))
408 return vertices_empty(bset);
410 if (bset->n_eq != 0)
411 return lower_dim_vertices(bset);
413 isl_assert(bset->ctx, isl_basic_set_dim(bset, isl_dim_div) == 0,
414 return NULL);
416 if (isl_basic_set_dim(bset, isl_dim_set) == 0)
417 return vertices_0D(bset);
419 nvar = isl_basic_set_dim(bset, isl_dim_set);
421 bset = isl_basic_set_copy(bset);
422 bset = isl_basic_set_set_rational(bset);
423 if (!bset)
424 return NULL;
426 tab = isl_tab_from_basic_set(bset);
427 if (!tab)
428 goto error;
429 tab->strict_redundant = 1;
431 if (tab->empty) {
432 vertices = vertices_empty(bset);
433 isl_basic_set_free(bset);
434 isl_tab_free(tab);
435 return vertices;
438 selection = isl_alloc_array(bset->ctx, int, bset->n_ineq);
439 snap = isl_alloc_array(bset->ctx, struct isl_tab_undo *, bset->n_ineq);
440 facets = isl_mat_alloc(bset->ctx, nvar, nvar);
441 if (!selection || !snap || !facets)
442 goto error;
444 level = 0;
445 init = 1;
446 selected = 0;
448 while (level >= 0) {
449 if (level >= bset->n_ineq ||
450 (!init && selection[level] != SELECTED)) {
451 --level;
452 init = 0;
453 continue;
455 if (init) {
456 int ok;
457 snap[level] = isl_tab_snap(tab);
458 ok = can_select(bset, level, tab, facets, selected,
459 selection);
460 if (ok < 0)
461 goto error;
462 if (ok) {
463 selection[level] = SELECTED;
464 selected++;
465 } else
466 selection[level] = UNSELECTED;
467 } else {
468 selection[level] = DESELECTED;
469 selected--;
470 if (isl_tab_rollback(tab, snap[level]) < 0)
471 goto error;
473 if (selected == nvar) {
474 if (tab->n_dead == nvar) {
475 if (add_vertex(&list, bset, tab) < 0)
476 goto error;
477 n_vertices++;
479 init = 0;
480 continue;
482 ++level;
483 init = 1;
486 isl_mat_free(facets);
487 free(selection);
488 free(snap);
490 isl_tab_free(tab);
492 vertices = vertices_from_list(bset, n_vertices, list);
494 vertices = compute_chambers(bset, vertices);
496 return vertices;
497 error:
498 isl_mat_free(facets);
499 free(selection);
500 free(snap);
501 isl_tab_free(tab);
502 isl_basic_set_free(bset);
503 return NULL;
506 struct isl_chamber_list {
507 struct isl_chamber c;
508 struct isl_chamber_list *next;
511 static void free_chamber_list(struct isl_chamber_list *list)
513 struct isl_chamber_list *next;
515 for (; list; list = next) {
516 next = list->next;
517 isl_basic_set_free(list->c.dom);
518 free(list->c.vertices);
519 free(list);
523 /* Check whether the basic set "bset" is a superset of the basic set described
524 * by "tab", i.e., check whether all constraints of "bset" are redundant.
526 static int bset_covers_tab(__isl_keep isl_basic_set *bset, struct isl_tab *tab)
528 int i;
530 if (!bset || !tab)
531 return -1;
533 for (i = 0; i < bset->n_ineq; ++i) {
534 enum isl_ineq_type type = isl_tab_ineq_type(tab, bset->ineq[i]);
535 switch (type) {
536 case isl_ineq_error: return -1;
537 case isl_ineq_redundant: continue;
538 default: return 0;
542 return 1;
545 static __isl_give isl_vertices *vertices_add_chambers(
546 __isl_take isl_vertices *vertices, int n_chambers,
547 struct isl_chamber_list *list)
549 int i;
550 isl_ctx *ctx;
551 struct isl_chamber_list *next;
553 ctx = isl_vertices_get_ctx(vertices);
554 vertices->c = isl_alloc_array(ctx, struct isl_chamber, n_chambers);
555 if (!vertices->c)
556 goto error;
557 vertices->n_chambers = n_chambers;
559 for (i = 0; list; list = next, i++) {
560 next = list->next;
561 vertices->c[i] = list->c;
562 free(list);
565 return vertices;
566 error:
567 isl_vertices_free(vertices);
568 free_chamber_list(list);
569 return NULL;
572 /* Can "tab" be intersected with "bset" without resulting in
573 * a lower-dimensional set.
575 static int can_intersect(struct isl_tab *tab, __isl_keep isl_basic_set *bset)
577 int i;
578 struct isl_tab_undo *snap;
580 if (isl_tab_extend_cons(tab, bset->n_ineq) < 0)
581 return -1;
583 snap = isl_tab_snap(tab);
585 for (i = 0; i < bset->n_ineq; ++i) {
586 if (isl_tab_ineq_type(tab, bset->ineq[i]) == isl_ineq_redundant)
587 continue;
588 if (isl_tab_add_ineq(tab, bset->ineq[i]) < 0)
589 return -1;
592 if (isl_tab_detect_implicit_equalities(tab) < 0)
593 return -1;
594 if (tab->n_dead) {
595 if (isl_tab_rollback(tab, snap) < 0)
596 return -1;
597 return 0;
600 return 1;
603 static int add_chamber(struct isl_chamber_list **list,
604 __isl_keep isl_vertices *vertices, struct isl_tab *tab, int *selection)
606 int n_frozen;
607 int i, j;
608 int n_vertices = 0;
609 struct isl_tab_undo *snap;
610 struct isl_chamber_list *c = NULL;
612 for (i = 0; i < vertices->n_vertices; ++i)
613 if (selection[i])
614 n_vertices++;
616 snap = isl_tab_snap(tab);
618 for (i = 0; i < tab->n_con && tab->con[i].frozen; ++i)
619 tab->con[i].frozen = 0;
620 n_frozen = i;
622 if (isl_tab_detect_redundant(tab) < 0)
623 return -1;
625 c = isl_calloc_type(tab->mat->ctx, struct isl_chamber_list);
626 if (!c)
627 goto error;
628 c->c.vertices = isl_alloc_array(tab->mat->ctx, int, n_vertices);
629 if (!c->c.vertices)
630 goto error;
631 c->c.dom = isl_basic_set_from_basic_map(isl_basic_map_copy(tab->bmap));
632 c->c.dom = isl_basic_set_set_rational(c->c.dom);
633 c->c.dom = isl_basic_set_cow(c->c.dom);
634 c->c.dom = isl_basic_set_update_from_tab(c->c.dom, tab);
635 c->c.dom = isl_basic_set_simplify(c->c.dom);
636 c->c.dom = isl_basic_set_finalize(c->c.dom);
637 if (!c->c.dom)
638 goto error;
640 c->c.n_vertices = n_vertices;
642 for (i = 0, j = 0; i < vertices->n_vertices; ++i)
643 if (selection[i]) {
644 c->c.vertices[j] = i;
645 j++;
648 c->next = *list;
649 *list = c;
651 for (i = 0; i < n_frozen; ++i)
652 tab->con[i].frozen = 1;
654 if (isl_tab_rollback(tab, snap) < 0)
655 return -1;
657 return 0;
658 error:
659 free_chamber_list(c);
660 return -1;
663 struct isl_facet_todo {
664 struct isl_tab *tab; /* A tableau representation of the facet */
665 isl_basic_set *bset; /* A normalized basic set representation */
666 isl_vec *constraint; /* Constraint pointing to the other side */
667 struct isl_facet_todo *next;
670 static void free_todo(struct isl_facet_todo *todo)
672 while (todo) {
673 struct isl_facet_todo *next = todo->next;
675 isl_tab_free(todo->tab);
676 isl_basic_set_free(todo->bset);
677 isl_vec_free(todo->constraint);
678 free(todo);
680 todo = next;
684 static struct isl_facet_todo *create_todo(struct isl_tab *tab, int con)
686 int i;
687 int n_frozen;
688 struct isl_tab_undo *snap;
689 struct isl_facet_todo *todo;
691 snap = isl_tab_snap(tab);
693 for (i = 0; i < tab->n_con && tab->con[i].frozen; ++i)
694 tab->con[i].frozen = 0;
695 n_frozen = i;
697 if (isl_tab_detect_redundant(tab) < 0)
698 return NULL;
700 todo = isl_calloc_type(tab->mat->ctx, struct isl_facet_todo);
701 if (!todo)
702 return NULL;
704 todo->constraint = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
705 if (!todo->constraint)
706 goto error;
707 isl_seq_neg(todo->constraint->el, tab->bmap->ineq[con], 1 + tab->n_var);
708 todo->bset = isl_basic_set_from_basic_map(isl_basic_map_copy(tab->bmap));
709 todo->bset = isl_basic_set_set_rational(todo->bset);
710 todo->bset = isl_basic_set_cow(todo->bset);
711 todo->bset = isl_basic_set_update_from_tab(todo->bset, tab);
712 todo->bset = isl_basic_set_simplify(todo->bset);
713 todo->bset = isl_basic_set_sort_constraints(todo->bset);
714 if (!todo->bset)
715 goto error;
716 ISL_F_SET(todo->bset, ISL_BASIC_SET_NORMALIZED);
717 todo->tab = isl_tab_dup(tab);
718 if (!todo->tab)
719 goto error;
721 for (i = 0; i < n_frozen; ++i)
722 tab->con[i].frozen = 1;
724 if (isl_tab_rollback(tab, snap) < 0)
725 goto error;
727 return todo;
728 error:
729 free_todo(todo);
730 return NULL;
733 /* Create todo items for all interior facets of the chamber represented
734 * by "tab" and collect them in "next".
736 static int init_todo(struct isl_facet_todo **next, struct isl_tab *tab)
738 int i;
739 struct isl_tab_undo *snap;
740 struct isl_facet_todo *todo;
742 snap = isl_tab_snap(tab);
744 for (i = 0; i < tab->n_con; ++i) {
745 if (tab->con[i].frozen)
746 continue;
747 if (tab->con[i].is_redundant)
748 continue;
750 if (isl_tab_select_facet(tab, i) < 0)
751 return -1;
753 todo = create_todo(tab, i);
754 if (!todo)
755 return -1;
757 todo->next = *next;
758 *next = todo;
760 if (isl_tab_rollback(tab, snap) < 0)
761 return -1;
764 return 0;
767 /* Does the linked list contain a todo item that is the opposite of "todo".
768 * If so, return 1 and remove the opposite todo item.
770 static int has_opposite(struct isl_facet_todo *todo,
771 struct isl_facet_todo **list)
773 for (; *list; list = &(*list)->next) {
774 int eq;
775 eq = isl_basic_set_plain_is_equal(todo->bset, (*list)->bset);
776 if (eq < 0)
777 return -1;
778 if (!eq)
779 continue;
780 todo = *list;
781 *list = todo->next;
782 todo->next = NULL;
783 free_todo(todo);
784 return 1;
787 return 0;
790 /* Create todo items for all interior facets of the chamber represented
791 * by "tab" and collect them in first->next, taking care to cancel
792 * opposite todo items.
794 static int update_todo(struct isl_facet_todo *first, struct isl_tab *tab)
796 int i;
797 struct isl_tab_undo *snap;
798 struct isl_facet_todo *todo;
800 snap = isl_tab_snap(tab);
802 for (i = 0; i < tab->n_con; ++i) {
803 int drop;
805 if (tab->con[i].frozen)
806 continue;
807 if (tab->con[i].is_redundant)
808 continue;
810 if (isl_tab_select_facet(tab, i) < 0)
811 return -1;
813 todo = create_todo(tab, i);
814 if (!todo)
815 return -1;
817 drop = has_opposite(todo, &first->next);
818 if (drop < 0)
819 return -1;
821 if (drop)
822 free_todo(todo);
823 else {
824 todo->next = first->next;
825 first->next = todo;
828 if (isl_tab_rollback(tab, snap) < 0)
829 return -1;
832 return 0;
835 /* Compute the chamber decomposition of the parametric polytope respresented
836 * by "bset" given the parametric vertices and their activity domains.
838 * We are only interested in full-dimensional chambers.
839 * Each of these chambers is the intersection of the activity domains of
840 * one or more vertices and the union of all chambers is equal to the
841 * projection of the entire parametric polytope onto the parameter space.
843 * We first create an initial chamber by intersecting as many activity
844 * domains as possible without ending up with an empty or lower-dimensional
845 * set. As a minor optimization, we only consider those activity domains
846 * that contain some arbitrary point.
848 * For each of interior facets of the chamber, we construct a todo item,
849 * containing the facet and a constraint containing the other side of the facet,
850 * for constructing the chamber on the other side.
851 * While their are any todo items left, we pick a todo item and
852 * create the required chamber by intersecting all activity domains
853 * that contain the facet and have a full-dimensional intersection with
854 * the other side of the facet. For each of the interior facets, we
855 * again create todo items, taking care to cancel opposite todo items.
857 static __isl_give isl_vertices *compute_chambers(__isl_take isl_basic_set *bset,
858 __isl_take isl_vertices *vertices)
860 int i;
861 isl_ctx *ctx;
862 isl_vec *sample = NULL;
863 struct isl_tab *tab = NULL;
864 struct isl_tab_undo *snap;
865 int *selection = NULL;
866 int n_chambers = 0;
867 struct isl_chamber_list *list = NULL;
868 struct isl_facet_todo *todo = NULL;
870 if (!bset || !vertices)
871 goto error;
873 ctx = isl_vertices_get_ctx(vertices);
874 selection = isl_alloc_array(ctx, int, vertices->n_vertices);
875 if (!selection)
876 goto error;
878 bset = isl_basic_set_params(bset);
880 tab = isl_tab_from_basic_set(bset);
881 for (i = 0; i < bset->n_ineq; ++i)
882 if (isl_tab_freeze_constraint(tab, i) < 0)
883 goto error;
884 if (isl_tab_track_bset(tab, bset) < 0)
885 goto error;
887 snap = isl_tab_snap(tab);
889 sample = isl_tab_get_sample_value(tab);
891 for (i = 0; i < vertices->n_vertices; ++i) {
892 selection[i] = isl_basic_set_contains(vertices->v[i].dom, sample);
893 if (selection[i] < 0)
894 goto error;
895 if (!selection[i])
896 continue;
897 selection[i] = can_intersect(tab, vertices->v[i].dom);
898 if (selection[i] < 0)
899 goto error;
902 if (isl_tab_detect_redundant(tab) < 0)
903 goto error;
905 if (add_chamber(&list, vertices, tab, selection) < 0)
906 goto error;
907 n_chambers++;
909 if (init_todo(&todo, tab) < 0)
910 goto error;
912 while (todo) {
913 struct isl_facet_todo *next;
915 if (isl_tab_rollback(tab, snap) < 0)
916 goto error;
918 if (isl_tab_add_ineq(tab, todo->constraint->el) < 0)
919 goto error;
920 if (isl_tab_freeze_constraint(tab, tab->n_con - 1) < 0)
921 goto error;
923 for (i = 0; i < vertices->n_vertices; ++i) {
924 selection[i] = bset_covers_tab(vertices->v[i].dom,
925 todo->tab);
926 if (selection[i] < 0)
927 goto error;
928 if (!selection[i])
929 continue;
930 selection[i] = can_intersect(tab, vertices->v[i].dom);
931 if (selection[i] < 0)
932 goto error;
935 if (isl_tab_detect_redundant(tab) < 0)
936 goto error;
938 if (add_chamber(&list, vertices, tab, selection) < 0)
939 goto error;
940 n_chambers++;
942 if (update_todo(todo, tab) < 0)
943 goto error;
945 next = todo->next;
946 todo->next = NULL;
947 free_todo(todo);
948 todo = next;
951 isl_vec_free(sample);
953 isl_tab_free(tab);
954 free(selection);
956 vertices = vertices_add_chambers(vertices, n_chambers, list);
958 for (i = 0; vertices && i < vertices->n_vertices; ++i) {
959 isl_basic_set_free(vertices->v[i].dom);
960 vertices->v[i].dom = NULL;
963 return vertices;
964 error:
965 free_chamber_list(list);
966 free_todo(todo);
967 isl_vec_free(sample);
968 isl_tab_free(tab);
969 free(selection);
970 if (!tab)
971 isl_basic_set_free(bset);
972 isl_vertices_free(vertices);
973 return NULL;
976 isl_ctx *isl_vertex_get_ctx(__isl_keep isl_vertex *vertex)
978 return vertex ? isl_vertices_get_ctx(vertex->vertices) : NULL;
981 int isl_vertex_get_id(__isl_keep isl_vertex *vertex)
983 return vertex ? vertex->id : -1;
986 __isl_give isl_basic_set *isl_vertex_get_domain(__isl_keep isl_vertex *vertex)
988 struct isl_vertex *v;
990 if (!vertex)
991 return NULL;
993 v = &vertex->vertices->v[vertex->id];
994 if (!v->dom) {
995 unsigned nvar;
996 nvar = isl_basic_set_dim(v->vertex, isl_dim_set);
997 v->dom = isl_basic_set_copy(v->vertex);
998 v->dom = isl_basic_set_project_out(v->dom, isl_dim_set, 0, nvar);
1001 return isl_basic_set_copy(v->dom);
1004 __isl_give isl_basic_set *isl_vertex_get_expr(__isl_keep isl_vertex *vertex)
1006 struct isl_vertex *v;
1008 if (!vertex)
1009 return NULL;
1011 v = &vertex->vertices->v[vertex->id];
1013 return isl_basic_set_copy(v->vertex);
1016 static __isl_give isl_vertex *isl_vertex_alloc(__isl_take isl_vertices *vertices,
1017 int id)
1019 isl_ctx *ctx;
1020 isl_vertex *vertex;
1022 if (!vertices)
1023 return NULL;
1025 ctx = isl_vertices_get_ctx(vertices);
1026 vertex = isl_alloc_type(ctx, isl_vertex);
1027 if (!vertex)
1028 goto error;
1030 vertex->vertices = vertices;
1031 vertex->id = id;
1033 return vertex;
1034 error:
1035 isl_vertices_free(vertices);
1036 return NULL;
1039 void isl_vertex_free(__isl_take isl_vertex *vertex)
1041 if (!vertex)
1042 return;
1043 isl_vertices_free(vertex->vertices);
1044 free(vertex);
1047 __isl_give isl_basic_set *isl_basic_set_set_integral(__isl_take isl_basic_set *bset)
1049 if (!bset)
1050 return NULL;
1052 if (!ISL_F_ISSET(bset, ISL_BASIC_MAP_RATIONAL))
1053 return bset;
1055 bset = isl_basic_set_cow(bset);
1056 if (!bset)
1057 return NULL;
1059 ISL_F_CLR(bset, ISL_BASIC_MAP_RATIONAL);
1061 return isl_basic_set_finalize(bset);
1064 isl_ctx *isl_cell_get_ctx(__isl_keep isl_cell *cell)
1066 return cell ? cell->dom->ctx : NULL;
1069 __isl_give isl_basic_set *isl_cell_get_domain(__isl_keep isl_cell *cell)
1071 return cell ? isl_basic_set_copy(cell->dom) : NULL;
1074 static __isl_give isl_cell *isl_cell_alloc(__isl_take isl_vertices *vertices,
1075 __isl_take isl_basic_set *dom, int id)
1077 int i;
1078 isl_cell *cell = NULL;
1080 if (!vertices || !dom)
1081 goto error;
1083 cell = isl_calloc_type(dom->ctx, isl_cell);
1084 if (!cell)
1085 goto error;
1087 cell->n_vertices = vertices->c[id].n_vertices;
1088 cell->ids = isl_alloc_array(dom->ctx, int, cell->n_vertices);
1089 if (!cell->ids)
1090 goto error;
1091 for (i = 0; i < cell->n_vertices; ++i)
1092 cell->ids[i] = vertices->c[id].vertices[i];
1093 cell->vertices = vertices;
1094 cell->dom = dom;
1096 return cell;
1097 error:
1098 isl_cell_free(cell);
1099 isl_vertices_free(vertices);
1100 isl_basic_set_free(dom);
1101 return NULL;
1104 void isl_cell_free(__isl_take isl_cell *cell)
1106 if (!cell)
1107 return;
1109 isl_vertices_free(cell->vertices);
1110 free(cell->ids);
1111 isl_basic_set_free(cell->dom);
1112 free(cell);
1115 /* Create a tableau of the cone obtained by first homogenizing the given
1116 * polytope and then making all inequalities strict by setting the
1117 * constant term to -1.
1119 static struct isl_tab *tab_for_shifted_cone(__isl_keep isl_basic_set *bset)
1121 int i;
1122 isl_vec *c = NULL;
1123 struct isl_tab *tab;
1125 if (!bset)
1126 return NULL;
1127 tab = isl_tab_alloc(bset->ctx, bset->n_ineq + 1,
1128 1 + isl_basic_set_total_dim(bset), 0);
1129 if (!tab)
1130 return NULL;
1131 tab->rational = ISL_F_ISSET(bset, ISL_BASIC_SET_RATIONAL);
1132 if (ISL_F_ISSET(bset, ISL_BASIC_MAP_EMPTY)) {
1133 if (isl_tab_mark_empty(tab) < 0)
1134 goto error;
1135 return tab;
1138 c = isl_vec_alloc(bset->ctx, 1 + 1 + isl_basic_set_total_dim(bset));
1139 if (!c)
1140 goto error;
1142 isl_int_set_si(c->el[0], 0);
1143 for (i = 0; i < bset->n_eq; ++i) {
1144 isl_seq_cpy(c->el + 1, bset->eq[i], c->size - 1);
1145 if (isl_tab_add_eq(tab, c->el) < 0)
1146 goto error;
1149 isl_int_set_si(c->el[0], -1);
1150 for (i = 0; i < bset->n_ineq; ++i) {
1151 isl_seq_cpy(c->el + 1, bset->ineq[i], c->size - 1);
1152 if (isl_tab_add_ineq(tab, c->el) < 0)
1153 goto error;
1154 if (tab->empty) {
1155 isl_vec_free(c);
1156 return tab;
1160 isl_seq_clr(c->el + 1, c->size - 1);
1161 isl_int_set_si(c->el[1], 1);
1162 if (isl_tab_add_ineq(tab, c->el) < 0)
1163 goto error;
1165 isl_vec_free(c);
1166 return tab;
1167 error:
1168 isl_vec_free(c);
1169 isl_tab_free(tab);
1170 return NULL;
1173 /* Compute an interior point of "bset" by selecting an interior
1174 * point in homogeneous space and projecting the point back down.
1176 static __isl_give isl_vec *isl_basic_set_interior_point(
1177 __isl_keep isl_basic_set *bset)
1179 isl_vec *vec;
1180 struct isl_tab *tab;
1182 tab = tab_for_shifted_cone(bset);
1183 vec = isl_tab_get_sample_value(tab);
1184 isl_tab_free(tab);
1185 if (!vec)
1186 return NULL;
1188 isl_seq_cpy(vec->el, vec->el + 1, vec->size - 1);
1189 vec->size--;
1191 return vec;
1194 /* Call "fn" on all chambers of the parametric polytope with the shared
1195 * facets of neighboring chambers only appearing in one of the chambers.
1197 * We pick an interior point from one of the chambers and then make
1198 * all constraints that do not satisfy this point strict.
1200 int isl_vertices_foreach_disjoint_cell(__isl_keep isl_vertices *vertices,
1201 int (*fn)(__isl_take isl_cell *cell, void *user), void *user)
1203 int i, j;
1204 isl_vec *vec;
1205 isl_int v;
1206 isl_cell *cell;
1208 if (!vertices)
1209 return -1;
1211 if (vertices->n_chambers == 0)
1212 return 0;
1214 if (vertices->n_chambers == 1) {
1215 isl_basic_set *dom = isl_basic_set_copy(vertices->c[0].dom);
1216 dom = isl_basic_set_set_integral(dom);
1217 cell = isl_cell_alloc(isl_vertices_copy(vertices), dom, 0);
1218 if (!cell)
1219 return -1;
1220 return fn(cell, user);
1223 vec = isl_basic_set_interior_point(vertices->c[0].dom);
1224 if (!vec)
1225 return -1;
1227 isl_int_init(v);
1229 for (i = 0; i < vertices->n_chambers; ++i) {
1230 int r;
1231 isl_basic_set *dom = isl_basic_set_copy(vertices->c[i].dom);
1232 dom = isl_basic_set_cow(dom);
1233 if (!dom)
1234 goto error;
1235 for (j = 0; i && j < dom->n_ineq; ++j) {
1236 isl_seq_inner_product(vec->el, dom->ineq[j], vec->size,
1237 &v);
1238 if (!isl_int_is_neg(v))
1239 continue;
1240 isl_int_sub_ui(dom->ineq[j][0], dom->ineq[j][0], 1);
1242 dom = isl_basic_set_set_integral(dom);
1243 cell = isl_cell_alloc(isl_vertices_copy(vertices), dom, i);
1244 if (!cell)
1245 goto error;
1246 r = fn(cell, user);
1247 if (r < 0)
1248 goto error;
1251 isl_int_clear(v);
1252 isl_vec_free(vec);
1254 return 0;
1255 error:
1256 isl_int_clear(v);
1257 isl_vec_free(vec);
1258 return -1;
1261 int isl_vertices_foreach_cell(__isl_keep isl_vertices *vertices,
1262 int (*fn)(__isl_take isl_cell *cell, void *user), void *user)
1264 int i;
1265 isl_cell *cell;
1267 if (!vertices)
1268 return -1;
1270 if (vertices->n_chambers == 0)
1271 return 0;
1273 for (i = 0; i < vertices->n_chambers; ++i) {
1274 int r;
1275 isl_basic_set *dom = isl_basic_set_copy(vertices->c[i].dom);
1277 cell = isl_cell_alloc(isl_vertices_copy(vertices), dom, i);
1278 if (!cell)
1279 return -1;
1281 r = fn(cell, user);
1282 if (r < 0)
1283 return -1;
1286 return 0;
1289 int isl_vertices_foreach_vertex(__isl_keep isl_vertices *vertices,
1290 int (*fn)(__isl_take isl_vertex *vertex, void *user), void *user)
1292 int i;
1293 isl_vertex *vertex;
1295 if (!vertices)
1296 return -1;
1298 if (vertices->n_vertices == 0)
1299 return 0;
1301 for (i = 0; i < vertices->n_vertices; ++i) {
1302 int r;
1304 vertex = isl_vertex_alloc(isl_vertices_copy(vertices), i);
1305 if (!vertex)
1306 return -1;
1308 r = fn(vertex, user);
1309 if (r < 0)
1310 return -1;
1313 return 0;
1316 int isl_cell_foreach_vertex(__isl_keep isl_cell *cell,
1317 int (*fn)(__isl_take isl_vertex *vertex, void *user), void *user)
1319 int i;
1320 isl_vertex *vertex;
1322 if (!cell)
1323 return -1;
1325 if (cell->n_vertices == 0)
1326 return 0;
1328 for (i = 0; i < cell->n_vertices; ++i) {
1329 int r;
1331 vertex = isl_vertex_alloc(isl_vertices_copy(cell->vertices),
1332 cell->ids[i]);
1333 if (!vertex)
1334 return -1;
1336 r = fn(vertex, user);
1337 if (r < 0)
1338 return -1;
1341 return 0;
1344 isl_ctx *isl_vertices_get_ctx(__isl_keep isl_vertices *vertices)
1346 return vertices ? vertices->bset->ctx : NULL;
1349 int isl_vertices_get_n_vertices(__isl_keep isl_vertices *vertices)
1351 return vertices ? vertices->n_vertices : -1;
1354 __isl_give isl_vertices *isl_morph_vertices(__isl_take isl_morph *morph,
1355 __isl_take isl_vertices *vertices)
1357 int i;
1358 isl_morph *param_morph = NULL;
1360 if (!morph || !vertices)
1361 goto error;
1363 isl_assert(vertices->bset->ctx, vertices->ref == 1, goto error);
1365 param_morph = isl_morph_copy(morph);
1366 param_morph = isl_morph_dom_params(param_morph);
1367 param_morph = isl_morph_ran_params(param_morph);
1369 for (i = 0; i < vertices->n_vertices; ++i) {
1370 vertices->v[i].dom = isl_morph_basic_set(
1371 isl_morph_copy(param_morph), vertices->v[i].dom);
1372 vertices->v[i].vertex = isl_morph_basic_set(
1373 isl_morph_copy(morph), vertices->v[i].vertex);
1374 if (!vertices->v[i].vertex)
1375 goto error;
1378 for (i = 0; i < vertices->n_chambers; ++i) {
1379 vertices->c[i].dom = isl_morph_basic_set(
1380 isl_morph_copy(param_morph), vertices->c[i].dom);
1381 if (!vertices->c[i].dom)
1382 goto error;
1385 isl_morph_free(param_morph);
1386 isl_morph_free(morph);
1387 return vertices;
1388 error:
1389 isl_morph_free(param_morph);
1390 isl_morph_free(morph);
1391 isl_vertices_free(vertices);
1392 return NULL;
1395 /* Construct a simplex isl_cell spanned by the vertices with indices in
1396 * "simplex_ids" and "other_ids" and call "fn" on this isl_cell.
1398 static int call_on_simplex(__isl_keep isl_cell *cell,
1399 int *simplex_ids, int n_simplex, int *other_ids, int n_other,
1400 int (*fn)(__isl_take isl_cell *simplex, void *user), void *user)
1402 int i;
1403 isl_ctx *ctx;
1404 struct isl_cell *simplex;
1406 ctx = isl_cell_get_ctx(cell);
1408 simplex = isl_calloc_type(ctx, struct isl_cell);
1409 if (!simplex)
1410 return -1;
1411 simplex->vertices = isl_vertices_copy(cell->vertices);
1412 if (!simplex->vertices)
1413 goto error;
1414 simplex->dom = isl_basic_set_copy(cell->dom);
1415 if (!simplex->dom)
1416 goto error;
1417 simplex->n_vertices = n_simplex + n_other;
1418 simplex->ids = isl_alloc_array(ctx, int, simplex->n_vertices);
1419 if (!simplex->ids)
1420 goto error;
1422 for (i = 0; i < n_simplex; ++i)
1423 simplex->ids[i] = simplex_ids[i];
1424 for (i = 0; i < n_other; ++i)
1425 simplex->ids[n_simplex + i] = other_ids[i];
1427 return fn(simplex, user);
1428 error:
1429 isl_cell_free(simplex);
1430 return -1;
1433 /* Check whether the parametric vertex described by "vertex"
1434 * lies on the facet corresponding to constraint "facet" of "bset".
1435 * The isl_vec "v" is a temporary vector than can be used by this function.
1437 * We eliminate the variables from the facet constraint using the
1438 * equalities defining the vertex and check if the result is identical
1439 * to zero.
1441 * It would probably be better to keep track of the constraints defining
1442 * a vertex during the vertex construction so that we could simply look
1443 * it up here.
1445 static int vertex_on_facet(__isl_keep isl_basic_set *vertex,
1446 __isl_keep isl_basic_set *bset, int facet, __isl_keep isl_vec *v)
1448 int i;
1449 isl_int m;
1451 isl_seq_cpy(v->el, bset->ineq[facet], v->size);
1453 isl_int_init(m);
1454 for (i = 0; i < vertex->n_eq; ++i) {
1455 int k = isl_seq_last_non_zero(vertex->eq[i], v->size);
1456 isl_seq_elim(v->el, vertex->eq[i], k, v->size, &m);
1458 isl_int_clear(m);
1460 return isl_seq_first_non_zero(v->el, v->size) == -1;
1463 /* Triangulate the polytope spanned by the vertices with ids
1464 * in "simplex_ids" and "other_ids" and call "fn" on each of
1465 * the resulting simplices.
1466 * If the input polytope is already a simplex, we simply call "fn".
1467 * Otherwise, we pick a point from "other_ids" and add it to "simplex_ids".
1468 * Then we consider each facet of "bset" that does not contain the point
1469 * we just picked, but does contain some of the other points in "other_ids"
1470 * and call ourselves recursively on the polytope spanned by the new
1471 * "simplex_ids" and those points in "other_ids" that lie on the facet.
1473 static int triangulate(__isl_keep isl_cell *cell, __isl_keep isl_vec *v,
1474 int *simplex_ids, int n_simplex, int *other_ids, int n_other,
1475 int (*fn)(__isl_take isl_cell *simplex, void *user), void *user)
1477 int i, j, k;
1478 int d, nparam;
1479 int *ids;
1480 isl_ctx *ctx;
1481 isl_basic_set *vertex;
1482 isl_basic_set *bset;
1484 ctx = isl_cell_get_ctx(cell);
1485 d = isl_basic_set_dim(cell->vertices->bset, isl_dim_set);
1486 nparam = isl_basic_set_dim(cell->vertices->bset, isl_dim_param);
1488 if (n_simplex + n_other == d + 1)
1489 return call_on_simplex(cell, simplex_ids, n_simplex,
1490 other_ids, n_other, fn, user);
1492 simplex_ids[n_simplex] = other_ids[0];
1493 vertex = cell->vertices->v[other_ids[0]].vertex;
1494 bset = cell->vertices->bset;
1496 ids = isl_alloc_array(ctx, int, n_other - 1);
1497 for (i = 0; i < bset->n_ineq; ++i) {
1498 if (isl_seq_first_non_zero(bset->ineq[i] + 1 + nparam, d) == -1)
1499 continue;
1500 if (vertex_on_facet(vertex, bset, i, v))
1501 continue;
1503 for (j = 1, k = 0; j < n_other; ++j) {
1504 isl_basic_set *ov;
1505 ov = cell->vertices->v[other_ids[j]].vertex;
1506 if (vertex_on_facet(ov, bset, i, v))
1507 ids[k++] = other_ids[j];
1509 if (k == 0)
1510 continue;
1512 if (triangulate(cell, v, simplex_ids, n_simplex + 1,
1513 ids, k, fn, user) < 0)
1514 goto error;
1516 free(ids);
1518 return 0;
1519 error:
1520 free(ids);
1521 return -1;
1524 /* Triangulate the given cell and call "fn" on each of the resulting
1525 * simplices.
1527 int isl_cell_foreach_simplex(__isl_take isl_cell *cell,
1528 int (*fn)(__isl_take isl_cell *simplex, void *user), void *user)
1530 int d, total;
1531 int r;
1532 isl_ctx *ctx;
1533 isl_vec *v = NULL;
1534 int *simplex_ids = NULL;
1536 if (!cell)
1537 return -1;
1539 d = isl_basic_set_dim(cell->vertices->bset, isl_dim_set);
1540 total = isl_basic_set_total_dim(cell->vertices->bset);
1542 if (cell->n_vertices == d + 1)
1543 return fn(cell, user);
1545 ctx = isl_cell_get_ctx(cell);
1546 simplex_ids = isl_alloc_array(ctx, int, d + 1);
1547 if (!simplex_ids)
1548 goto error;
1550 v = isl_vec_alloc(ctx, 1 + total);
1551 if (!v)
1552 goto error;
1554 r = triangulate(cell, v, simplex_ids, 0,
1555 cell->ids, cell->n_vertices, fn, user);
1557 isl_vec_free(v);
1558 free(simplex_ids);
1560 isl_cell_free(cell);
1562 return r;
1563 error:
1564 free(simplex_ids);
1565 isl_vec_free(v);
1566 isl_cell_free(cell);
1567 return -1;