configure.ac: move gmp detection to a separate file
[isl.git] / isl_vertices.c
blobc17224d08bbeac5a6e908ccefde85f8425a64226
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".
113 static int add_vertex(struct isl_vertex_list **list,
114 __isl_keep isl_basic_set *bset, struct isl_tab *tab)
116 unsigned nvar;
117 unsigned nparam;
118 struct isl_vertex_list *v = NULL;
120 if (isl_tab_detect_implicit_equalities(tab) < 0)
121 return -1;
123 nvar = isl_basic_set_dim(bset, isl_dim_set);
124 nparam = isl_basic_set_dim(bset, isl_dim_param);
126 v = isl_calloc_type(tab->mat->ctx, struct isl_vertex_list);
127 if (!v)
128 goto error;
130 v->v.vertex = isl_basic_set_copy(bset);
131 v->v.vertex = isl_basic_set_cow(v->v.vertex);
132 v->v.vertex = isl_basic_set_update_from_tab(v->v.vertex, tab);
133 v->v.vertex = isl_basic_set_simplify(v->v.vertex);
134 v->v.vertex = isl_basic_set_finalize(v->v.vertex);
135 if (!v->v.vertex)
136 goto error;
137 isl_assert(bset->ctx, v->v.vertex->n_eq >= nvar, goto error);
138 v->v.dom = isl_basic_set_copy(v->v.vertex);
139 v->v.dom = isl_basic_set_params(v->v.dom);
140 if (!v->v.dom)
141 goto error;
143 v->next = *list;
144 *list = v;
146 return 0;
147 error:
148 free_vertex_list(v);
149 return -1;
152 /* Compute the parametric vertices and the chamber decomposition
153 * of an empty parametric polytope.
155 static __isl_give isl_vertices *vertices_empty(__isl_keep isl_basic_set *bset)
157 isl_vertices *vertices;
158 unsigned nparam;
160 if (!bset)
161 return NULL;
163 nparam = isl_basic_set_dim(bset, isl_dim_param);
165 vertices = isl_calloc_type(bset->ctx, isl_vertices);
166 if (!vertices)
167 return NULL;
168 vertices->bset = isl_basic_set_copy(bset);
169 vertices->ref = 1;
171 vertices->n_vertices = 0;
172 vertices->n_chambers = 0;
174 return vertices;
177 /* Compute the parametric vertices and the chamber decomposition
178 * of the parametric polytope defined using the same constraints
179 * as "bset" in the 0D case.
180 * There is exactly one 0D vertex and a single chamber containing
181 * the vertex.
183 static __isl_give isl_vertices *vertices_0D(__isl_keep isl_basic_set *bset)
185 isl_vertices *vertices;
186 unsigned nparam;
188 if (!bset)
189 return NULL;
191 nparam = isl_basic_set_dim(bset, isl_dim_param);
193 vertices = isl_calloc_type(bset->ctx, isl_vertices);
194 if (!vertices)
195 return NULL;
196 vertices->ref = 1;
197 vertices->bset = isl_basic_set_copy(bset);
199 vertices->v = isl_calloc_array(bset->ctx, struct isl_vertex, 1);
200 if (!vertices->v)
201 goto error;
202 vertices->n_vertices = 1;
203 vertices->v[0].vertex = isl_basic_set_copy(bset);
204 vertices->v[0].dom = isl_basic_set_params(isl_basic_set_copy(bset));
205 if (!vertices->v[0].vertex || !vertices->v[0].dom)
206 goto error;
208 vertices->c = isl_calloc_array(bset->ctx, struct isl_chamber, 1);
209 if (!vertices->c)
210 goto error;
211 vertices->n_chambers = 1;
212 vertices->c[0].n_vertices = 1;
213 vertices->c[0].vertices = isl_calloc_array(bset->ctx, int, 1);
214 if (!vertices->c[0].vertices)
215 goto error;
216 vertices->c[0].dom = isl_basic_set_copy(vertices->v[0].dom);
217 if (!vertices->c[0].dom)
218 goto error;
220 return vertices;
221 error:
222 isl_vertices_free(vertices);
223 return NULL;
226 static int isl_mat_rank(__isl_keep isl_mat *mat)
228 int row, col;
229 isl_mat *H;
231 H = isl_mat_left_hermite(isl_mat_copy(mat), 0, NULL, NULL);
232 if (!H)
233 return -1;
235 for (col = 0; col < H->n_col; ++col) {
236 for (row = 0; row < H->n_row; ++row)
237 if (!isl_int_is_zero(H->row[row][col]))
238 break;
239 if (row == H->n_row)
240 break;
243 isl_mat_free(H);
245 return col;
248 /* Is the row pointed to by "f" linearly independent of the "n" first
249 * rows in "facets"?
251 static int is_independent(__isl_keep isl_mat *facets, int n, isl_int *f)
253 int rank;
255 if (isl_seq_first_non_zero(f, facets->n_col) < 0)
256 return 0;
258 isl_seq_cpy(facets->row[n], f, facets->n_col);
259 facets->n_row = n + 1;
260 rank = isl_mat_rank(facets);
261 if (rank < 0)
262 return -1;
264 return rank == n + 1;
267 /* Check whether we can select constraint "level", given the current selection
268 * reflected by facets in "tab", the rows of "facets" and the earlier
269 * "selected" elements of "selection".
271 * If the constraint is (strictly) redundant in the tableau, selecting it would
272 * result in an empty tableau, so it can't be selected.
273 * If the set variable part of the constraint is not linearly indepedent
274 * of the set variable parts of the already selected constraints,
275 * the constraint cannot be selected.
276 * If selecting the constraint results in an empty tableau, the constraint
277 * cannot be selected.
278 * Finally, if selecting the constraint results in some explicitly
279 * deselected constraints turning into equalities, then the corresponding
280 * vertices have already been generated, so the constraint cannot be selected.
282 static int can_select(__isl_keep isl_basic_set *bset, int level,
283 struct isl_tab *tab, __isl_keep isl_mat *facets, int selected,
284 int *selection)
286 int i;
287 int indep;
288 unsigned ovar;
289 struct isl_tab_undo *snap;
291 if (isl_tab_is_redundant(tab, level))
292 return 0;
294 ovar = isl_space_offset(bset->dim, isl_dim_set);
296 indep = is_independent(facets, selected, bset->ineq[level] + 1 + ovar);
297 if (indep < 0)
298 return -1;
299 if (!indep)
300 return 0;
302 snap = isl_tab_snap(tab);
303 if (isl_tab_select_facet(tab, level) < 0)
304 return -1;
306 if (tab->empty) {
307 if (isl_tab_rollback(tab, snap) < 0)
308 return -1;
309 return 0;
312 for (i = 0; i < level; ++i) {
313 int sgn;
315 if (selection[i] != DESELECTED)
316 continue;
318 if (isl_tab_is_equality(tab, i))
319 sgn = 0;
320 else if (isl_tab_is_redundant(tab, i))
321 sgn = 1;
322 else
323 sgn = isl_tab_sign_of_max(tab, i);
324 if (sgn < -1)
325 return -1;
326 if (sgn <= 0) {
327 if (isl_tab_rollback(tab, snap) < 0)
328 return -1;
329 return 0;
333 return 1;
336 /* Compute the parametric vertices and the chamber decomposition
337 * of a parametric polytope that is not full-dimensional.
339 * Simply map the parametric polytope to a lower dimensional space
340 * and map the resulting vertices back.
342 static __isl_give isl_vertices *lower_dim_vertices(
343 __isl_keep isl_basic_set *bset)
345 isl_morph *morph;
346 isl_vertices *vertices;
348 bset = isl_basic_set_copy(bset);
349 morph = isl_basic_set_full_compression(bset);
350 bset = isl_morph_basic_set(isl_morph_copy(morph), bset);
352 vertices = isl_basic_set_compute_vertices(bset);
353 isl_basic_set_free(bset);
355 morph = isl_morph_inverse(morph);
357 vertices = isl_morph_vertices(morph, vertices);
359 return vertices;
362 /* Compute the parametric vertices and the chamber decomposition
363 * of the parametric polytope defined using the same constraints
364 * as "bset". "bset" is assumed to have no existentially quantified
365 * variables.
367 * The vertices themselves are computed in a fairly simplistic way.
368 * We simply run through all combinations of d constraints,
369 * with d the number of set variables, and check if those d constraints
370 * define a vertex. To avoid the generation of duplicate vertices,
371 * which we may happen if a vertex is defined by more that d constraints,
372 * we make sure we only generate the vertex for the d constraints with
373 * smallest index.
375 * We set up a tableau and keep track of which facets have been
376 * selected. The tableau is marked strict_redundant so that we can be
377 * sure that any constraint that is marked redundant (and that is not
378 * also marked zero) is not an equality.
379 * If a constraint is marked DESELECTED, it means the constraint was
380 * SELECTED before (in combination with the same selection of earlier
381 * constraints). If such a deselected constraint turns out to be an
382 * equality, then any vertex that may still be found with the current
383 * selection has already been generated when the constraint was selected.
384 * A constraint is marked UNSELECTED when there is no way selecting
385 * the constraint could lead to a vertex (in combination with the current
386 * selection of earlier constraints).
388 * The set variable coefficients of the selected constraints are stored
389 * in the facets matrix.
391 __isl_give isl_vertices *isl_basic_set_compute_vertices(
392 __isl_keep isl_basic_set *bset)
394 struct isl_tab *tab;
395 int level;
396 int init;
397 unsigned nvar;
398 int *selection = NULL;
399 int selected;
400 struct isl_tab_undo **snap = NULL;
401 isl_mat *facets = NULL;
402 struct isl_vertex_list *list = NULL;
403 int n_vertices = 0;
404 isl_vertices *vertices;
406 if (!bset)
407 return NULL;
409 if (isl_basic_set_plain_is_empty(bset))
410 return vertices_empty(bset);
412 if (bset->n_eq != 0)
413 return lower_dim_vertices(bset);
415 isl_assert(bset->ctx, isl_basic_set_dim(bset, isl_dim_div) == 0,
416 return NULL);
418 if (isl_basic_set_dim(bset, isl_dim_set) == 0)
419 return vertices_0D(bset);
421 nvar = isl_basic_set_dim(bset, isl_dim_set);
423 bset = isl_basic_set_copy(bset);
424 bset = isl_basic_set_set_rational(bset);
425 if (!bset)
426 return NULL;
428 tab = isl_tab_from_basic_set(bset, 0);
429 if (!tab)
430 goto error;
431 tab->strict_redundant = 1;
433 if (tab->empty) {
434 vertices = vertices_empty(bset);
435 isl_basic_set_free(bset);
436 isl_tab_free(tab);
437 return vertices;
440 selection = isl_alloc_array(bset->ctx, int, bset->n_ineq);
441 snap = isl_alloc_array(bset->ctx, struct isl_tab_undo *, bset->n_ineq);
442 facets = isl_mat_alloc(bset->ctx, nvar, nvar);
443 if ((bset->n_ineq && (!selection || !snap)) || !facets)
444 goto error;
446 level = 0;
447 init = 1;
448 selected = 0;
450 while (level >= 0) {
451 if (level >= bset->n_ineq ||
452 (!init && selection[level] != SELECTED)) {
453 --level;
454 init = 0;
455 continue;
457 if (init) {
458 int ok;
459 snap[level] = isl_tab_snap(tab);
460 ok = can_select(bset, level, tab, facets, selected,
461 selection);
462 if (ok < 0)
463 goto error;
464 if (ok) {
465 selection[level] = SELECTED;
466 selected++;
467 } else
468 selection[level] = UNSELECTED;
469 } else {
470 selection[level] = DESELECTED;
471 selected--;
472 if (isl_tab_rollback(tab, snap[level]) < 0)
473 goto error;
475 if (selected == nvar) {
476 if (tab->n_dead == nvar) {
477 if (add_vertex(&list, bset, tab) < 0)
478 goto error;
479 n_vertices++;
481 init = 0;
482 continue;
484 ++level;
485 init = 1;
488 isl_mat_free(facets);
489 free(selection);
490 free(snap);
492 isl_tab_free(tab);
494 vertices = vertices_from_list(bset, n_vertices, list);
496 vertices = compute_chambers(bset, vertices);
498 return vertices;
499 error:
500 free_vertex_list(list);
501 isl_mat_free(facets);
502 free(selection);
503 free(snap);
504 isl_tab_free(tab);
505 isl_basic_set_free(bset);
506 return NULL;
509 struct isl_chamber_list {
510 struct isl_chamber c;
511 struct isl_chamber_list *next;
514 static void free_chamber_list(struct isl_chamber_list *list)
516 struct isl_chamber_list *next;
518 for (; list; list = next) {
519 next = list->next;
520 isl_basic_set_free(list->c.dom);
521 free(list->c.vertices);
522 free(list);
526 /* Check whether the basic set "bset" is a superset of the basic set described
527 * by "tab", i.e., check whether all constraints of "bset" are redundant.
529 static int bset_covers_tab(__isl_keep isl_basic_set *bset, struct isl_tab *tab)
531 int i;
533 if (!bset || !tab)
534 return -1;
536 for (i = 0; i < bset->n_ineq; ++i) {
537 enum isl_ineq_type type = isl_tab_ineq_type(tab, bset->ineq[i]);
538 switch (type) {
539 case isl_ineq_error: return -1;
540 case isl_ineq_redundant: continue;
541 default: return 0;
545 return 1;
548 static __isl_give isl_vertices *vertices_add_chambers(
549 __isl_take isl_vertices *vertices, int n_chambers,
550 struct isl_chamber_list *list)
552 int i;
553 isl_ctx *ctx;
554 struct isl_chamber_list *next;
556 ctx = isl_vertices_get_ctx(vertices);
557 vertices->c = isl_alloc_array(ctx, struct isl_chamber, n_chambers);
558 if (!vertices->c)
559 goto error;
560 vertices->n_chambers = n_chambers;
562 for (i = 0; list; list = next, i++) {
563 next = list->next;
564 vertices->c[i] = list->c;
565 free(list);
568 return vertices;
569 error:
570 isl_vertices_free(vertices);
571 free_chamber_list(list);
572 return NULL;
575 /* Can "tab" be intersected with "bset" without resulting in
576 * a lower-dimensional set.
578 static int can_intersect(struct isl_tab *tab, __isl_keep isl_basic_set *bset)
580 int i;
581 struct isl_tab_undo *snap;
583 if (isl_tab_extend_cons(tab, bset->n_ineq) < 0)
584 return -1;
586 snap = isl_tab_snap(tab);
588 for (i = 0; i < bset->n_ineq; ++i) {
589 if (isl_tab_ineq_type(tab, bset->ineq[i]) == isl_ineq_redundant)
590 continue;
591 if (isl_tab_add_ineq(tab, bset->ineq[i]) < 0)
592 return -1;
595 if (isl_tab_detect_implicit_equalities(tab) < 0)
596 return -1;
597 if (tab->n_dead) {
598 if (isl_tab_rollback(tab, snap) < 0)
599 return -1;
600 return 0;
603 return 1;
606 static int add_chamber(struct isl_chamber_list **list,
607 __isl_keep isl_vertices *vertices, struct isl_tab *tab, int *selection)
609 int n_frozen;
610 int i, j;
611 int n_vertices = 0;
612 struct isl_tab_undo *snap;
613 struct isl_chamber_list *c = NULL;
615 for (i = 0; i < vertices->n_vertices; ++i)
616 if (selection[i])
617 n_vertices++;
619 snap = isl_tab_snap(tab);
621 for (i = 0; i < tab->n_con && tab->con[i].frozen; ++i)
622 tab->con[i].frozen = 0;
623 n_frozen = i;
625 if (isl_tab_detect_redundant(tab) < 0)
626 return -1;
628 c = isl_calloc_type(tab->mat->ctx, struct isl_chamber_list);
629 if (!c)
630 goto error;
631 c->c.vertices = isl_alloc_array(tab->mat->ctx, int, n_vertices);
632 if (n_vertices && !c->c.vertices)
633 goto error;
634 c->c.dom = isl_basic_set_from_basic_map(isl_basic_map_copy(tab->bmap));
635 c->c.dom = isl_basic_set_set_rational(c->c.dom);
636 c->c.dom = isl_basic_set_cow(c->c.dom);
637 c->c.dom = isl_basic_set_update_from_tab(c->c.dom, tab);
638 c->c.dom = isl_basic_set_simplify(c->c.dom);
639 c->c.dom = isl_basic_set_finalize(c->c.dom);
640 if (!c->c.dom)
641 goto error;
643 c->c.n_vertices = n_vertices;
645 for (i = 0, j = 0; i < vertices->n_vertices; ++i)
646 if (selection[i]) {
647 c->c.vertices[j] = i;
648 j++;
651 c->next = *list;
652 *list = c;
654 for (i = 0; i < n_frozen; ++i)
655 tab->con[i].frozen = 1;
657 if (isl_tab_rollback(tab, snap) < 0)
658 return -1;
660 return 0;
661 error:
662 free_chamber_list(c);
663 return -1;
666 struct isl_facet_todo {
667 struct isl_tab *tab; /* A tableau representation of the facet */
668 isl_basic_set *bset; /* A normalized basic set representation */
669 isl_vec *constraint; /* Constraint pointing to the other side */
670 struct isl_facet_todo *next;
673 static void free_todo(struct isl_facet_todo *todo)
675 while (todo) {
676 struct isl_facet_todo *next = todo->next;
678 isl_tab_free(todo->tab);
679 isl_basic_set_free(todo->bset);
680 isl_vec_free(todo->constraint);
681 free(todo);
683 todo = next;
687 static struct isl_facet_todo *create_todo(struct isl_tab *tab, int con)
689 int i;
690 int n_frozen;
691 struct isl_tab_undo *snap;
692 struct isl_facet_todo *todo;
694 snap = isl_tab_snap(tab);
696 for (i = 0; i < tab->n_con && tab->con[i].frozen; ++i)
697 tab->con[i].frozen = 0;
698 n_frozen = i;
700 if (isl_tab_detect_redundant(tab) < 0)
701 return NULL;
703 todo = isl_calloc_type(tab->mat->ctx, struct isl_facet_todo);
704 if (!todo)
705 return NULL;
707 todo->constraint = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
708 if (!todo->constraint)
709 goto error;
710 isl_seq_neg(todo->constraint->el, tab->bmap->ineq[con], 1 + tab->n_var);
711 todo->bset = isl_basic_set_from_basic_map(isl_basic_map_copy(tab->bmap));
712 todo->bset = isl_basic_set_set_rational(todo->bset);
713 todo->bset = isl_basic_set_cow(todo->bset);
714 todo->bset = isl_basic_set_update_from_tab(todo->bset, tab);
715 todo->bset = isl_basic_set_simplify(todo->bset);
716 todo->bset = isl_basic_set_sort_constraints(todo->bset);
717 if (!todo->bset)
718 goto error;
719 ISL_F_SET(todo->bset, ISL_BASIC_SET_NORMALIZED);
720 todo->tab = isl_tab_dup(tab);
721 if (!todo->tab)
722 goto error;
724 for (i = 0; i < n_frozen; ++i)
725 tab->con[i].frozen = 1;
727 if (isl_tab_rollback(tab, snap) < 0)
728 goto error;
730 return todo;
731 error:
732 free_todo(todo);
733 return NULL;
736 /* Create todo items for all interior facets of the chamber represented
737 * by "tab" and collect them in "next".
739 static int init_todo(struct isl_facet_todo **next, struct isl_tab *tab)
741 int i;
742 struct isl_tab_undo *snap;
743 struct isl_facet_todo *todo;
745 snap = isl_tab_snap(tab);
747 for (i = 0; i < tab->n_con; ++i) {
748 if (tab->con[i].frozen)
749 continue;
750 if (tab->con[i].is_redundant)
751 continue;
753 if (isl_tab_select_facet(tab, i) < 0)
754 return -1;
756 todo = create_todo(tab, i);
757 if (!todo)
758 return -1;
760 todo->next = *next;
761 *next = todo;
763 if (isl_tab_rollback(tab, snap) < 0)
764 return -1;
767 return 0;
770 /* Does the linked list contain a todo item that is the opposite of "todo".
771 * If so, return 1 and remove the opposite todo item.
773 static int has_opposite(struct isl_facet_todo *todo,
774 struct isl_facet_todo **list)
776 for (; *list; list = &(*list)->next) {
777 int eq;
778 eq = isl_basic_set_plain_is_equal(todo->bset, (*list)->bset);
779 if (eq < 0)
780 return -1;
781 if (!eq)
782 continue;
783 todo = *list;
784 *list = todo->next;
785 todo->next = NULL;
786 free_todo(todo);
787 return 1;
790 return 0;
793 /* Create todo items for all interior facets of the chamber represented
794 * by "tab" and collect them in first->next, taking care to cancel
795 * opposite todo items.
797 static int update_todo(struct isl_facet_todo *first, struct isl_tab *tab)
799 int i;
800 struct isl_tab_undo *snap;
801 struct isl_facet_todo *todo;
803 snap = isl_tab_snap(tab);
805 for (i = 0; i < tab->n_con; ++i) {
806 int drop;
808 if (tab->con[i].frozen)
809 continue;
810 if (tab->con[i].is_redundant)
811 continue;
813 if (isl_tab_select_facet(tab, i) < 0)
814 return -1;
816 todo = create_todo(tab, i);
817 if (!todo)
818 return -1;
820 drop = has_opposite(todo, &first->next);
821 if (drop < 0)
822 return -1;
824 if (drop)
825 free_todo(todo);
826 else {
827 todo->next = first->next;
828 first->next = todo;
831 if (isl_tab_rollback(tab, snap) < 0)
832 return -1;
835 return 0;
838 /* Compute the chamber decomposition of the parametric polytope respresented
839 * by "bset" given the parametric vertices and their activity domains.
841 * We are only interested in full-dimensional chambers.
842 * Each of these chambers is the intersection of the activity domains of
843 * one or more vertices and the union of all chambers is equal to the
844 * projection of the entire parametric polytope onto the parameter space.
846 * We first create an initial chamber by intersecting as many activity
847 * domains as possible without ending up with an empty or lower-dimensional
848 * set. As a minor optimization, we only consider those activity domains
849 * that contain some arbitrary point.
851 * For each of interior facets of the chamber, we construct a todo item,
852 * containing the facet and a constraint containing the other side of the facet,
853 * for constructing the chamber on the other side.
854 * While their are any todo items left, we pick a todo item and
855 * create the required chamber by intersecting all activity domains
856 * that contain the facet and have a full-dimensional intersection with
857 * the other side of the facet. For each of the interior facets, we
858 * again create todo items, taking care to cancel opposite todo items.
860 static __isl_give isl_vertices *compute_chambers(__isl_take isl_basic_set *bset,
861 __isl_take isl_vertices *vertices)
863 int i;
864 isl_ctx *ctx;
865 isl_vec *sample = NULL;
866 struct isl_tab *tab = NULL;
867 struct isl_tab_undo *snap;
868 int *selection = NULL;
869 int n_chambers = 0;
870 struct isl_chamber_list *list = NULL;
871 struct isl_facet_todo *todo = NULL;
873 if (!bset || !vertices)
874 goto error;
876 ctx = isl_vertices_get_ctx(vertices);
877 selection = isl_alloc_array(ctx, int, vertices->n_vertices);
878 if (vertices->n_vertices && !selection)
879 goto error;
881 bset = isl_basic_set_params(bset);
883 tab = isl_tab_from_basic_set(bset, 1);
884 if (!tab)
885 goto error;
886 for (i = 0; i < bset->n_ineq; ++i)
887 if (isl_tab_freeze_constraint(tab, i) < 0)
888 goto error;
889 isl_basic_set_free(bset);
891 snap = isl_tab_snap(tab);
893 sample = isl_tab_get_sample_value(tab);
895 for (i = 0; i < vertices->n_vertices; ++i) {
896 selection[i] = isl_basic_set_contains(vertices->v[i].dom, sample);
897 if (selection[i] < 0)
898 goto error;
899 if (!selection[i])
900 continue;
901 selection[i] = can_intersect(tab, vertices->v[i].dom);
902 if (selection[i] < 0)
903 goto error;
906 if (isl_tab_detect_redundant(tab) < 0)
907 goto error;
909 if (add_chamber(&list, vertices, tab, selection) < 0)
910 goto error;
911 n_chambers++;
913 if (init_todo(&todo, tab) < 0)
914 goto error;
916 while (todo) {
917 struct isl_facet_todo *next;
919 if (isl_tab_rollback(tab, snap) < 0)
920 goto error;
922 if (isl_tab_add_ineq(tab, todo->constraint->el) < 0)
923 goto error;
924 if (isl_tab_freeze_constraint(tab, tab->n_con - 1) < 0)
925 goto error;
927 for (i = 0; i < vertices->n_vertices; ++i) {
928 selection[i] = bset_covers_tab(vertices->v[i].dom,
929 todo->tab);
930 if (selection[i] < 0)
931 goto error;
932 if (!selection[i])
933 continue;
934 selection[i] = can_intersect(tab, vertices->v[i].dom);
935 if (selection[i] < 0)
936 goto error;
939 if (isl_tab_detect_redundant(tab) < 0)
940 goto error;
942 if (add_chamber(&list, vertices, tab, selection) < 0)
943 goto error;
944 n_chambers++;
946 if (update_todo(todo, tab) < 0)
947 goto error;
949 next = todo->next;
950 todo->next = NULL;
951 free_todo(todo);
952 todo = next;
955 isl_vec_free(sample);
957 isl_tab_free(tab);
958 free(selection);
960 vertices = vertices_add_chambers(vertices, n_chambers, list);
962 for (i = 0; vertices && i < vertices->n_vertices; ++i) {
963 isl_basic_set_free(vertices->v[i].dom);
964 vertices->v[i].dom = NULL;
967 return vertices;
968 error:
969 free_chamber_list(list);
970 free_todo(todo);
971 isl_vec_free(sample);
972 isl_tab_free(tab);
973 free(selection);
974 if (!tab)
975 isl_basic_set_free(bset);
976 isl_vertices_free(vertices);
977 return NULL;
980 isl_ctx *isl_vertex_get_ctx(__isl_keep isl_vertex *vertex)
982 return vertex ? isl_vertices_get_ctx(vertex->vertices) : NULL;
985 int isl_vertex_get_id(__isl_keep isl_vertex *vertex)
987 return vertex ? vertex->id : -1;
990 __isl_give isl_basic_set *isl_basic_set_set_integral(__isl_take isl_basic_set *bset)
992 if (!bset)
993 return NULL;
995 if (!ISL_F_ISSET(bset, ISL_BASIC_MAP_RATIONAL))
996 return bset;
998 bset = isl_basic_set_cow(bset);
999 if (!bset)
1000 return NULL;
1002 ISL_F_CLR(bset, ISL_BASIC_MAP_RATIONAL);
1004 return isl_basic_set_finalize(bset);
1007 /* Return the activity domain of the vertex "vertex".
1009 __isl_give isl_basic_set *isl_vertex_get_domain(__isl_keep isl_vertex *vertex)
1011 struct isl_vertex *v;
1013 if (!vertex)
1014 return NULL;
1016 v = &vertex->vertices->v[vertex->id];
1017 if (!v->dom) {
1018 v->dom = isl_basic_set_copy(v->vertex);
1019 v->dom = isl_basic_set_params(v->dom);
1020 v->dom = isl_basic_set_set_integral(v->dom);
1023 return isl_basic_set_copy(v->dom);
1026 /* Return a multiple quasi-affine expression describing the vertex "vertex"
1027 * in terms of the parameters,
1029 __isl_give isl_multi_aff *isl_vertex_get_expr(__isl_keep isl_vertex *vertex)
1031 struct isl_vertex *v;
1032 isl_basic_set *bset;
1034 if (!vertex)
1035 return NULL;
1037 v = &vertex->vertices->v[vertex->id];
1039 bset = isl_basic_set_copy(v->vertex);
1040 return isl_multi_aff_from_basic_set_equalities(bset);
1043 static __isl_give isl_vertex *isl_vertex_alloc(__isl_take isl_vertices *vertices,
1044 int id)
1046 isl_ctx *ctx;
1047 isl_vertex *vertex;
1049 if (!vertices)
1050 return NULL;
1052 ctx = isl_vertices_get_ctx(vertices);
1053 vertex = isl_alloc_type(ctx, isl_vertex);
1054 if (!vertex)
1055 goto error;
1057 vertex->vertices = vertices;
1058 vertex->id = id;
1060 return vertex;
1061 error:
1062 isl_vertices_free(vertices);
1063 return NULL;
1066 void isl_vertex_free(__isl_take isl_vertex *vertex)
1068 if (!vertex)
1069 return;
1070 isl_vertices_free(vertex->vertices);
1071 free(vertex);
1074 isl_ctx *isl_cell_get_ctx(__isl_keep isl_cell *cell)
1076 return cell ? cell->dom->ctx : NULL;
1079 __isl_give isl_basic_set *isl_cell_get_domain(__isl_keep isl_cell *cell)
1081 return cell ? isl_basic_set_copy(cell->dom) : NULL;
1084 static __isl_give isl_cell *isl_cell_alloc(__isl_take isl_vertices *vertices,
1085 __isl_take isl_basic_set *dom, int id)
1087 int i;
1088 isl_cell *cell = NULL;
1090 if (!vertices || !dom)
1091 goto error;
1093 cell = isl_calloc_type(dom->ctx, isl_cell);
1094 if (!cell)
1095 goto error;
1097 cell->n_vertices = vertices->c[id].n_vertices;
1098 cell->ids = isl_alloc_array(dom->ctx, int, cell->n_vertices);
1099 if (cell->n_vertices && !cell->ids)
1100 goto error;
1101 for (i = 0; i < cell->n_vertices; ++i)
1102 cell->ids[i] = vertices->c[id].vertices[i];
1103 cell->vertices = vertices;
1104 cell->dom = dom;
1106 return cell;
1107 error:
1108 isl_cell_free(cell);
1109 isl_vertices_free(vertices);
1110 isl_basic_set_free(dom);
1111 return NULL;
1114 void isl_cell_free(__isl_take isl_cell *cell)
1116 if (!cell)
1117 return;
1119 isl_vertices_free(cell->vertices);
1120 free(cell->ids);
1121 isl_basic_set_free(cell->dom);
1122 free(cell);
1125 /* Create a tableau of the cone obtained by first homogenizing the given
1126 * polytope and then making all inequalities strict by setting the
1127 * constant term to -1.
1129 static struct isl_tab *tab_for_shifted_cone(__isl_keep isl_basic_set *bset)
1131 int i;
1132 isl_vec *c = NULL;
1133 struct isl_tab *tab;
1135 if (!bset)
1136 return NULL;
1137 tab = isl_tab_alloc(bset->ctx, bset->n_ineq + 1,
1138 1 + isl_basic_set_total_dim(bset), 0);
1139 if (!tab)
1140 return NULL;
1141 tab->rational = ISL_F_ISSET(bset, ISL_BASIC_SET_RATIONAL);
1142 if (ISL_F_ISSET(bset, ISL_BASIC_MAP_EMPTY)) {
1143 if (isl_tab_mark_empty(tab) < 0)
1144 goto error;
1145 return tab;
1148 c = isl_vec_alloc(bset->ctx, 1 + 1 + isl_basic_set_total_dim(bset));
1149 if (!c)
1150 goto error;
1152 isl_int_set_si(c->el[0], 0);
1153 for (i = 0; i < bset->n_eq; ++i) {
1154 isl_seq_cpy(c->el + 1, bset->eq[i], c->size - 1);
1155 if (isl_tab_add_eq(tab, c->el) < 0)
1156 goto error;
1159 isl_int_set_si(c->el[0], -1);
1160 for (i = 0; i < bset->n_ineq; ++i) {
1161 isl_seq_cpy(c->el + 1, bset->ineq[i], c->size - 1);
1162 if (isl_tab_add_ineq(tab, c->el) < 0)
1163 goto error;
1164 if (tab->empty) {
1165 isl_vec_free(c);
1166 return tab;
1170 isl_seq_clr(c->el + 1, c->size - 1);
1171 isl_int_set_si(c->el[1], 1);
1172 if (isl_tab_add_ineq(tab, c->el) < 0)
1173 goto error;
1175 isl_vec_free(c);
1176 return tab;
1177 error:
1178 isl_vec_free(c);
1179 isl_tab_free(tab);
1180 return NULL;
1183 /* Compute an interior point of "bset" by selecting an interior
1184 * point in homogeneous space and projecting the point back down.
1186 static __isl_give isl_vec *isl_basic_set_interior_point(
1187 __isl_keep isl_basic_set *bset)
1189 isl_vec *vec;
1190 struct isl_tab *tab;
1192 tab = tab_for_shifted_cone(bset);
1193 vec = isl_tab_get_sample_value(tab);
1194 isl_tab_free(tab);
1195 if (!vec)
1196 return NULL;
1198 isl_seq_cpy(vec->el, vec->el + 1, vec->size - 1);
1199 vec->size--;
1201 return vec;
1204 /* Call "fn" on all chambers of the parametric polytope with the shared
1205 * facets of neighboring chambers only appearing in one of the chambers.
1207 * We pick an interior point from one of the chambers and then make
1208 * all constraints that do not satisfy this point strict.
1210 int isl_vertices_foreach_disjoint_cell(__isl_keep isl_vertices *vertices,
1211 int (*fn)(__isl_take isl_cell *cell, void *user), void *user)
1213 int i, j;
1214 isl_vec *vec;
1215 isl_int v;
1216 isl_cell *cell;
1218 if (!vertices)
1219 return -1;
1221 if (vertices->n_chambers == 0)
1222 return 0;
1224 if (vertices->n_chambers == 1) {
1225 isl_basic_set *dom = isl_basic_set_copy(vertices->c[0].dom);
1226 dom = isl_basic_set_set_integral(dom);
1227 cell = isl_cell_alloc(isl_vertices_copy(vertices), dom, 0);
1228 if (!cell)
1229 return -1;
1230 return fn(cell, user);
1233 vec = isl_basic_set_interior_point(vertices->c[0].dom);
1234 if (!vec)
1235 return -1;
1237 isl_int_init(v);
1239 for (i = 0; i < vertices->n_chambers; ++i) {
1240 int r;
1241 isl_basic_set *dom = isl_basic_set_copy(vertices->c[i].dom);
1242 dom = isl_basic_set_cow(dom);
1243 if (!dom)
1244 goto error;
1245 for (j = 0; i && j < dom->n_ineq; ++j) {
1246 isl_seq_inner_product(vec->el, dom->ineq[j], vec->size,
1247 &v);
1248 if (!isl_int_is_neg(v))
1249 continue;
1250 isl_int_sub_ui(dom->ineq[j][0], dom->ineq[j][0], 1);
1252 dom = isl_basic_set_set_integral(dom);
1253 cell = isl_cell_alloc(isl_vertices_copy(vertices), dom, i);
1254 if (!cell)
1255 goto error;
1256 r = fn(cell, user);
1257 if (r < 0)
1258 goto error;
1261 isl_int_clear(v);
1262 isl_vec_free(vec);
1264 return 0;
1265 error:
1266 isl_int_clear(v);
1267 isl_vec_free(vec);
1268 return -1;
1271 int isl_vertices_foreach_cell(__isl_keep isl_vertices *vertices,
1272 int (*fn)(__isl_take isl_cell *cell, void *user), void *user)
1274 int i;
1275 isl_cell *cell;
1277 if (!vertices)
1278 return -1;
1280 if (vertices->n_chambers == 0)
1281 return 0;
1283 for (i = 0; i < vertices->n_chambers; ++i) {
1284 int r;
1285 isl_basic_set *dom = isl_basic_set_copy(vertices->c[i].dom);
1287 cell = isl_cell_alloc(isl_vertices_copy(vertices), dom, i);
1288 if (!cell)
1289 return -1;
1291 r = fn(cell, user);
1292 if (r < 0)
1293 return -1;
1296 return 0;
1299 int isl_vertices_foreach_vertex(__isl_keep isl_vertices *vertices,
1300 int (*fn)(__isl_take isl_vertex *vertex, void *user), void *user)
1302 int i;
1303 isl_vertex *vertex;
1305 if (!vertices)
1306 return -1;
1308 if (vertices->n_vertices == 0)
1309 return 0;
1311 for (i = 0; i < vertices->n_vertices; ++i) {
1312 int r;
1314 vertex = isl_vertex_alloc(isl_vertices_copy(vertices), i);
1315 if (!vertex)
1316 return -1;
1318 r = fn(vertex, user);
1319 if (r < 0)
1320 return -1;
1323 return 0;
1326 int isl_cell_foreach_vertex(__isl_keep isl_cell *cell,
1327 int (*fn)(__isl_take isl_vertex *vertex, void *user), void *user)
1329 int i;
1330 isl_vertex *vertex;
1332 if (!cell)
1333 return -1;
1335 if (cell->n_vertices == 0)
1336 return 0;
1338 for (i = 0; i < cell->n_vertices; ++i) {
1339 int r;
1341 vertex = isl_vertex_alloc(isl_vertices_copy(cell->vertices),
1342 cell->ids[i]);
1343 if (!vertex)
1344 return -1;
1346 r = fn(vertex, user);
1347 if (r < 0)
1348 return -1;
1351 return 0;
1354 isl_ctx *isl_vertices_get_ctx(__isl_keep isl_vertices *vertices)
1356 return vertices ? vertices->bset->ctx : NULL;
1359 int isl_vertices_get_n_vertices(__isl_keep isl_vertices *vertices)
1361 return vertices ? vertices->n_vertices : -1;
1364 __isl_give isl_vertices *isl_morph_vertices(__isl_take isl_morph *morph,
1365 __isl_take isl_vertices *vertices)
1367 int i;
1368 isl_morph *param_morph = NULL;
1370 if (!morph || !vertices)
1371 goto error;
1373 isl_assert(vertices->bset->ctx, vertices->ref == 1, goto error);
1375 param_morph = isl_morph_copy(morph);
1376 param_morph = isl_morph_dom_params(param_morph);
1377 param_morph = isl_morph_ran_params(param_morph);
1379 for (i = 0; i < vertices->n_vertices; ++i) {
1380 vertices->v[i].dom = isl_morph_basic_set(
1381 isl_morph_copy(param_morph), vertices->v[i].dom);
1382 vertices->v[i].vertex = isl_morph_basic_set(
1383 isl_morph_copy(morph), vertices->v[i].vertex);
1384 if (!vertices->v[i].vertex)
1385 goto error;
1388 for (i = 0; i < vertices->n_chambers; ++i) {
1389 vertices->c[i].dom = isl_morph_basic_set(
1390 isl_morph_copy(param_morph), vertices->c[i].dom);
1391 if (!vertices->c[i].dom)
1392 goto error;
1395 isl_morph_free(param_morph);
1396 isl_morph_free(morph);
1397 return vertices;
1398 error:
1399 isl_morph_free(param_morph);
1400 isl_morph_free(morph);
1401 isl_vertices_free(vertices);
1402 return NULL;
1405 /* Construct a simplex isl_cell spanned by the vertices with indices in
1406 * "simplex_ids" and "other_ids" and call "fn" on this isl_cell.
1408 static int call_on_simplex(__isl_keep isl_cell *cell,
1409 int *simplex_ids, int n_simplex, int *other_ids, int n_other,
1410 int (*fn)(__isl_take isl_cell *simplex, void *user), void *user)
1412 int i;
1413 isl_ctx *ctx;
1414 struct isl_cell *simplex;
1416 ctx = isl_cell_get_ctx(cell);
1418 simplex = isl_calloc_type(ctx, struct isl_cell);
1419 if (!simplex)
1420 return -1;
1421 simplex->vertices = isl_vertices_copy(cell->vertices);
1422 if (!simplex->vertices)
1423 goto error;
1424 simplex->dom = isl_basic_set_copy(cell->dom);
1425 if (!simplex->dom)
1426 goto error;
1427 simplex->n_vertices = n_simplex + n_other;
1428 simplex->ids = isl_alloc_array(ctx, int, simplex->n_vertices);
1429 if (!simplex->ids)
1430 goto error;
1432 for (i = 0; i < n_simplex; ++i)
1433 simplex->ids[i] = simplex_ids[i];
1434 for (i = 0; i < n_other; ++i)
1435 simplex->ids[n_simplex + i] = other_ids[i];
1437 return fn(simplex, user);
1438 error:
1439 isl_cell_free(simplex);
1440 return -1;
1443 /* Check whether the parametric vertex described by "vertex"
1444 * lies on the facet corresponding to constraint "facet" of "bset".
1445 * The isl_vec "v" is a temporary vector than can be used by this function.
1447 * We eliminate the variables from the facet constraint using the
1448 * equalities defining the vertex and check if the result is identical
1449 * to zero.
1451 * It would probably be better to keep track of the constraints defining
1452 * a vertex during the vertex construction so that we could simply look
1453 * it up here.
1455 static int vertex_on_facet(__isl_keep isl_basic_set *vertex,
1456 __isl_keep isl_basic_set *bset, int facet, __isl_keep isl_vec *v)
1458 int i;
1459 isl_int m;
1461 isl_seq_cpy(v->el, bset->ineq[facet], v->size);
1463 isl_int_init(m);
1464 for (i = 0; i < vertex->n_eq; ++i) {
1465 int k = isl_seq_last_non_zero(vertex->eq[i], v->size);
1466 isl_seq_elim(v->el, vertex->eq[i], k, v->size, &m);
1468 isl_int_clear(m);
1470 return isl_seq_first_non_zero(v->el, v->size) == -1;
1473 /* Triangulate the polytope spanned by the vertices with ids
1474 * in "simplex_ids" and "other_ids" and call "fn" on each of
1475 * the resulting simplices.
1476 * If the input polytope is already a simplex, we simply call "fn".
1477 * Otherwise, we pick a point from "other_ids" and add it to "simplex_ids".
1478 * Then we consider each facet of "bset" that does not contain the point
1479 * we just picked, but does contain some of the other points in "other_ids"
1480 * and call ourselves recursively on the polytope spanned by the new
1481 * "simplex_ids" and those points in "other_ids" that lie on the facet.
1483 static int triangulate(__isl_keep isl_cell *cell, __isl_keep isl_vec *v,
1484 int *simplex_ids, int n_simplex, int *other_ids, int n_other,
1485 int (*fn)(__isl_take isl_cell *simplex, void *user), void *user)
1487 int i, j, k;
1488 int d, nparam;
1489 int *ids;
1490 isl_ctx *ctx;
1491 isl_basic_set *vertex;
1492 isl_basic_set *bset;
1494 ctx = isl_cell_get_ctx(cell);
1495 d = isl_basic_set_dim(cell->vertices->bset, isl_dim_set);
1496 nparam = isl_basic_set_dim(cell->vertices->bset, isl_dim_param);
1498 if (n_simplex + n_other == d + 1)
1499 return call_on_simplex(cell, simplex_ids, n_simplex,
1500 other_ids, n_other, fn, user);
1502 simplex_ids[n_simplex] = other_ids[0];
1503 vertex = cell->vertices->v[other_ids[0]].vertex;
1504 bset = cell->vertices->bset;
1506 ids = isl_alloc_array(ctx, int, n_other - 1);
1507 for (i = 0; i < bset->n_ineq; ++i) {
1508 if (isl_seq_first_non_zero(bset->ineq[i] + 1 + nparam, d) == -1)
1509 continue;
1510 if (vertex_on_facet(vertex, bset, i, v))
1511 continue;
1513 for (j = 1, k = 0; j < n_other; ++j) {
1514 isl_basic_set *ov;
1515 ov = cell->vertices->v[other_ids[j]].vertex;
1516 if (vertex_on_facet(ov, bset, i, v))
1517 ids[k++] = other_ids[j];
1519 if (k == 0)
1520 continue;
1522 if (triangulate(cell, v, simplex_ids, n_simplex + 1,
1523 ids, k, fn, user) < 0)
1524 goto error;
1526 free(ids);
1528 return 0;
1529 error:
1530 free(ids);
1531 return -1;
1534 /* Triangulate the given cell and call "fn" on each of the resulting
1535 * simplices.
1537 int isl_cell_foreach_simplex(__isl_take isl_cell *cell,
1538 int (*fn)(__isl_take isl_cell *simplex, void *user), void *user)
1540 int d, total;
1541 int r;
1542 isl_ctx *ctx;
1543 isl_vec *v = NULL;
1544 int *simplex_ids = NULL;
1546 if (!cell)
1547 return -1;
1549 d = isl_basic_set_dim(cell->vertices->bset, isl_dim_set);
1550 total = isl_basic_set_total_dim(cell->vertices->bset);
1552 if (cell->n_vertices == d + 1)
1553 return fn(cell, user);
1555 ctx = isl_cell_get_ctx(cell);
1556 simplex_ids = isl_alloc_array(ctx, int, d + 1);
1557 if (!simplex_ids)
1558 goto error;
1560 v = isl_vec_alloc(ctx, 1 + total);
1561 if (!v)
1562 goto error;
1564 r = triangulate(cell, v, simplex_ids, 0,
1565 cell->ids, cell->n_vertices, fn, user);
1567 isl_vec_free(v);
1568 free(simplex_ids);
1570 isl_cell_free(cell);
1572 return r;
1573 error:
1574 free(simplex_ids);
1575 isl_vec_free(v);
1576 isl_cell_free(cell);
1577 return -1;