isl_basic_set_total_dim: implement in terms of isl_basic_map_total_dim
[isl.git] / isl_vertices.c
blobc361af33d94a068c9fe061d6a341fe3ff404e742
1 /*
2 * Copyright 2010 INRIA Saclay
4 * Use of this software is governed by the MIT license
6 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
7 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
8 * 91893 Orsay, France
9 */
11 #include <isl_map_private.h>
12 #include <isl_aff_private.h>
13 #include <isl/set.h>
14 #include <isl_seq.h>
15 #include <isl_tab.h>
16 #include <isl_space_private.h>
17 #include <isl_morph.h>
18 #include <isl_vertices_private.h>
19 #include <isl_mat_private.h>
20 #include <isl_vec_private.h>
22 #define SELECTED 1
23 #define DESELECTED -1
24 #define UNSELECTED 0
26 static __isl_give isl_vertices *compute_chambers(__isl_take isl_basic_set *bset,
27 __isl_take isl_vertices *vertices);
29 __isl_give isl_vertices *isl_vertices_copy(__isl_keep isl_vertices *vertices)
31 if (!vertices)
32 return NULL;
34 vertices->ref++;
35 return vertices;
38 __isl_null isl_vertices *isl_vertices_free(__isl_take isl_vertices *vertices)
40 int i;
42 if (!vertices)
43 return NULL;
45 if (--vertices->ref > 0)
46 return NULL;
48 for (i = 0; i < vertices->n_vertices; ++i) {
49 isl_basic_set_free(vertices->v[i].vertex);
50 isl_basic_set_free(vertices->v[i].dom);
52 free(vertices->v);
54 for (i = 0; i < vertices->n_chambers; ++i) {
55 free(vertices->c[i].vertices);
56 isl_basic_set_free(vertices->c[i].dom);
58 free(vertices->c);
60 isl_basic_set_free(vertices->bset);
61 free(vertices);
63 return NULL;
66 struct isl_vertex_list {
67 struct isl_vertex v;
68 struct isl_vertex_list *next;
71 static struct isl_vertex_list *free_vertex_list(struct isl_vertex_list *list)
73 struct isl_vertex_list *next;
75 for (; list; list = next) {
76 next = list->next;
77 isl_basic_set_free(list->v.vertex);
78 isl_basic_set_free(list->v.dom);
79 free(list);
82 return NULL;
85 static __isl_give isl_vertices *vertices_from_list(__isl_keep isl_basic_set *bset,
86 int n_vertices, struct isl_vertex_list *list)
88 int i;
89 struct isl_vertex_list *next;
90 isl_vertices *vertices;
92 vertices = isl_calloc_type(bset->ctx, isl_vertices);
93 if (!vertices)
94 goto error;
95 vertices->ref = 1;
96 vertices->bset = isl_basic_set_copy(bset);
97 vertices->v = isl_alloc_array(bset->ctx, struct isl_vertex, n_vertices);
98 if (n_vertices && !vertices->v)
99 goto error;
100 vertices->n_vertices = n_vertices;
102 for (i = 0; list; list = next, i++) {
103 next = list->next;
104 vertices->v[i] = list->v;
105 free(list);
108 return vertices;
109 error:
110 isl_vertices_free(vertices);
111 free_vertex_list(list);
112 return NULL;
115 /* Prepend a vertex to the linked list "list" based on the equalities in "tab".
116 * Return isl_bool_true if the vertex was actually added and
117 * isl_bool_false otherwise.
118 * In particular, vertices with a lower-dimensional activity domain are
119 * not added to the list because they would not be included in any chamber.
120 * Return isl_bool_error on error.
122 static isl_bool add_vertex(struct isl_vertex_list **list,
123 __isl_keep isl_basic_set *bset, struct isl_tab *tab)
125 unsigned nvar;
126 struct isl_vertex_list *v = NULL;
128 if (isl_tab_detect_implicit_equalities(tab) < 0)
129 return isl_bool_error;
131 nvar = isl_basic_set_dim(bset, isl_dim_set);
133 v = isl_calloc_type(tab->mat->ctx, struct isl_vertex_list);
134 if (!v)
135 goto error;
137 v->v.vertex = isl_basic_set_copy(bset);
138 v->v.vertex = isl_basic_set_cow(v->v.vertex);
139 v->v.vertex = isl_basic_set_update_from_tab(v->v.vertex, tab);
140 v->v.vertex = isl_basic_set_simplify(v->v.vertex);
141 v->v.vertex = isl_basic_set_finalize(v->v.vertex);
142 if (!v->v.vertex)
143 goto error;
144 isl_assert(bset->ctx, v->v.vertex->n_eq >= nvar, goto error);
145 v->v.dom = isl_basic_set_copy(v->v.vertex);
146 v->v.dom = isl_basic_set_params(v->v.dom);
147 if (!v->v.dom)
148 goto error;
150 if (v->v.dom->n_eq > 0) {
151 free_vertex_list(v);
152 return isl_bool_false;
155 v->next = *list;
156 *list = v;
158 return isl_bool_true;
159 error:
160 free_vertex_list(v);
161 return isl_bool_error;
164 /* Compute the parametric vertices and the chamber decomposition
165 * of an empty parametric polytope.
167 static __isl_give isl_vertices *vertices_empty(__isl_keep isl_basic_set *bset)
169 isl_vertices *vertices;
171 if (!bset)
172 return NULL;
174 vertices = isl_calloc_type(bset->ctx, isl_vertices);
175 if (!vertices)
176 return NULL;
177 vertices->bset = isl_basic_set_copy(bset);
178 vertices->ref = 1;
180 vertices->n_vertices = 0;
181 vertices->n_chambers = 0;
183 return vertices;
186 /* Compute the parametric vertices and the chamber decomposition
187 * of the parametric polytope defined using the same constraints
188 * as "bset" in the 0D case.
189 * There is exactly one 0D vertex and a single chamber containing
190 * the vertex.
192 static __isl_give isl_vertices *vertices_0D(__isl_keep isl_basic_set *bset)
194 isl_vertices *vertices;
196 if (!bset)
197 return NULL;
199 vertices = isl_calloc_type(bset->ctx, isl_vertices);
200 if (!vertices)
201 return NULL;
202 vertices->ref = 1;
203 vertices->bset = isl_basic_set_copy(bset);
205 vertices->v = isl_calloc_array(bset->ctx, struct isl_vertex, 1);
206 if (!vertices->v)
207 goto error;
208 vertices->n_vertices = 1;
209 vertices->v[0].vertex = isl_basic_set_copy(bset);
210 vertices->v[0].dom = isl_basic_set_params(isl_basic_set_copy(bset));
211 if (!vertices->v[0].vertex || !vertices->v[0].dom)
212 goto error;
214 vertices->c = isl_calloc_array(bset->ctx, struct isl_chamber, 1);
215 if (!vertices->c)
216 goto error;
217 vertices->n_chambers = 1;
218 vertices->c[0].n_vertices = 1;
219 vertices->c[0].vertices = isl_calloc_array(bset->ctx, int, 1);
220 if (!vertices->c[0].vertices)
221 goto error;
222 vertices->c[0].dom = isl_basic_set_copy(vertices->v[0].dom);
223 if (!vertices->c[0].dom)
224 goto error;
226 return vertices;
227 error:
228 isl_vertices_free(vertices);
229 return NULL;
232 /* Is the row pointed to by "f" linearly independent of the "n" first
233 * rows in "facets"?
235 static int is_independent(__isl_keep isl_mat *facets, int n, isl_int *f)
237 int rank;
239 if (isl_seq_first_non_zero(f, facets->n_col) < 0)
240 return 0;
242 isl_seq_cpy(facets->row[n], f, facets->n_col);
243 facets->n_row = n + 1;
244 rank = isl_mat_rank(facets);
245 if (rank < 0)
246 return -1;
248 return rank == n + 1;
251 /* Check whether we can select constraint "level", given the current selection
252 * reflected by facets in "tab", the rows of "facets" and the earlier
253 * "selected" elements of "selection".
255 * If the constraint is (strictly) redundant in the tableau, selecting it would
256 * result in an empty tableau, so it can't be selected.
257 * If the set variable part of the constraint is not linearly independent
258 * of the set variable parts of the already selected constraints,
259 * the constraint cannot be selected.
260 * If selecting the constraint results in an empty tableau, the constraint
261 * cannot be selected.
262 * Finally, if selecting the constraint results in some explicitly
263 * deselected constraints turning into equalities, then the corresponding
264 * vertices have already been generated, so the constraint cannot be selected.
266 static int can_select(__isl_keep isl_basic_set *bset, int level,
267 struct isl_tab *tab, __isl_keep isl_mat *facets, int selected,
268 int *selection)
270 int i;
271 int indep;
272 unsigned ovar;
273 struct isl_tab_undo *snap;
275 if (isl_tab_is_redundant(tab, level))
276 return 0;
278 ovar = isl_space_offset(bset->dim, isl_dim_set);
280 indep = is_independent(facets, selected, bset->ineq[level] + 1 + ovar);
281 if (indep < 0)
282 return -1;
283 if (!indep)
284 return 0;
286 snap = isl_tab_snap(tab);
287 if (isl_tab_select_facet(tab, level) < 0)
288 return -1;
290 if (tab->empty) {
291 if (isl_tab_rollback(tab, snap) < 0)
292 return -1;
293 return 0;
296 for (i = 0; i < level; ++i) {
297 int sgn;
299 if (selection[i] != DESELECTED)
300 continue;
302 if (isl_tab_is_equality(tab, i))
303 sgn = 0;
304 else if (isl_tab_is_redundant(tab, i))
305 sgn = 1;
306 else
307 sgn = isl_tab_sign_of_max(tab, i);
308 if (sgn < -1)
309 return -1;
310 if (sgn <= 0) {
311 if (isl_tab_rollback(tab, snap) < 0)
312 return -1;
313 return 0;
317 return 1;
320 /* Compute the parametric vertices and the chamber decomposition
321 * of a parametric polytope that is not full-dimensional.
323 * Simply map the parametric polytope to a lower dimensional space
324 * and map the resulting vertices back.
326 static __isl_give isl_vertices *lower_dim_vertices(
327 __isl_keep isl_basic_set *bset)
329 isl_morph *morph;
330 isl_vertices *vertices;
332 bset = isl_basic_set_copy(bset);
333 morph = isl_basic_set_full_compression(bset);
334 bset = isl_morph_basic_set(isl_morph_copy(morph), bset);
336 vertices = isl_basic_set_compute_vertices(bset);
337 isl_basic_set_free(bset);
339 morph = isl_morph_inverse(morph);
341 vertices = isl_morph_vertices(morph, vertices);
343 return vertices;
346 /* Compute the parametric vertices and the chamber decomposition
347 * of the parametric polytope defined using the same constraints
348 * as "bset". "bset" is assumed to have no existentially quantified
349 * variables.
351 * The vertices themselves are computed in a fairly simplistic way.
352 * We simply run through all combinations of d constraints,
353 * with d the number of set variables, and check if those d constraints
354 * define a vertex. To avoid the generation of duplicate vertices,
355 * which we may happen if a vertex is defined by more that d constraints,
356 * we make sure we only generate the vertex for the d constraints with
357 * smallest index.
359 * We set up a tableau and keep track of which facets have been
360 * selected. The tableau is marked strict_redundant so that we can be
361 * sure that any constraint that is marked redundant (and that is not
362 * also marked zero) is not an equality.
363 * If a constraint is marked DESELECTED, it means the constraint was
364 * SELECTED before (in combination with the same selection of earlier
365 * constraints). If such a deselected constraint turns out to be an
366 * equality, then any vertex that may still be found with the current
367 * selection has already been generated when the constraint was selected.
368 * A constraint is marked UNSELECTED when there is no way selecting
369 * the constraint could lead to a vertex (in combination with the current
370 * selection of earlier constraints).
372 * The set variable coefficients of the selected constraints are stored
373 * in the facets matrix.
375 __isl_give isl_vertices *isl_basic_set_compute_vertices(
376 __isl_keep isl_basic_set *bset)
378 struct isl_tab *tab;
379 int level;
380 int init;
381 unsigned nvar;
382 int *selection = NULL;
383 int selected;
384 struct isl_tab_undo **snap = NULL;
385 isl_mat *facets = NULL;
386 struct isl_vertex_list *list = NULL;
387 int n_vertices = 0;
388 isl_vertices *vertices;
390 if (!bset)
391 return NULL;
393 if (isl_basic_set_plain_is_empty(bset))
394 return vertices_empty(bset);
396 if (bset->n_eq != 0)
397 return lower_dim_vertices(bset);
399 isl_assert(bset->ctx, isl_basic_set_dim(bset, isl_dim_div) == 0,
400 return NULL);
402 if (isl_basic_set_dim(bset, isl_dim_set) == 0)
403 return vertices_0D(bset);
405 nvar = isl_basic_set_dim(bset, isl_dim_set);
407 bset = isl_basic_set_copy(bset);
408 bset = isl_basic_set_set_rational(bset);
409 if (!bset)
410 return NULL;
412 tab = isl_tab_from_basic_set(bset, 0);
413 if (!tab)
414 goto error;
415 tab->strict_redundant = 1;
417 if (tab->empty) {
418 vertices = vertices_empty(bset);
419 isl_basic_set_free(bset);
420 isl_tab_free(tab);
421 return vertices;
424 selection = isl_alloc_array(bset->ctx, int, bset->n_ineq);
425 snap = isl_alloc_array(bset->ctx, struct isl_tab_undo *, bset->n_ineq);
426 facets = isl_mat_alloc(bset->ctx, nvar, nvar);
427 if ((bset->n_ineq && (!selection || !snap)) || !facets)
428 goto error;
430 level = 0;
431 init = 1;
432 selected = 0;
434 while (level >= 0) {
435 if (level >= bset->n_ineq ||
436 (!init && selection[level] != SELECTED)) {
437 --level;
438 init = 0;
439 continue;
441 if (init) {
442 int ok;
443 snap[level] = isl_tab_snap(tab);
444 ok = can_select(bset, level, tab, facets, selected,
445 selection);
446 if (ok < 0)
447 goto error;
448 if (ok) {
449 selection[level] = SELECTED;
450 selected++;
451 } else
452 selection[level] = UNSELECTED;
453 } else {
454 selection[level] = DESELECTED;
455 selected--;
456 if (isl_tab_rollback(tab, snap[level]) < 0)
457 goto error;
459 if (selected == nvar) {
460 if (tab->n_dead == nvar) {
461 isl_bool added = add_vertex(&list, bset, tab);
462 if (added < 0)
463 goto error;
464 if (added)
465 n_vertices++;
467 init = 0;
468 continue;
470 ++level;
471 init = 1;
474 isl_mat_free(facets);
475 free(selection);
476 free(snap);
478 isl_tab_free(tab);
480 vertices = vertices_from_list(bset, n_vertices, list);
482 vertices = compute_chambers(bset, vertices);
484 return vertices;
485 error:
486 free_vertex_list(list);
487 isl_mat_free(facets);
488 free(selection);
489 free(snap);
490 isl_tab_free(tab);
491 isl_basic_set_free(bset);
492 return NULL;
495 struct isl_chamber_list {
496 struct isl_chamber c;
497 struct isl_chamber_list *next;
500 static void free_chamber_list(struct isl_chamber_list *list)
502 struct isl_chamber_list *next;
504 for (; list; list = next) {
505 next = list->next;
506 isl_basic_set_free(list->c.dom);
507 free(list->c.vertices);
508 free(list);
512 /* Check whether the basic set "bset" is a superset of the basic set described
513 * by "tab", i.e., check whether all constraints of "bset" are redundant.
515 static isl_bool bset_covers_tab(__isl_keep isl_basic_set *bset,
516 struct isl_tab *tab)
518 int i;
520 if (!bset || !tab)
521 return isl_bool_error;
523 for (i = 0; i < bset->n_ineq; ++i) {
524 enum isl_ineq_type type = isl_tab_ineq_type(tab, bset->ineq[i]);
525 switch (type) {
526 case isl_ineq_error: return isl_bool_error;
527 case isl_ineq_redundant: continue;
528 default: return isl_bool_false;
532 return isl_bool_true;
535 static __isl_give isl_vertices *vertices_add_chambers(
536 __isl_take isl_vertices *vertices, int n_chambers,
537 struct isl_chamber_list *list)
539 int i;
540 isl_ctx *ctx;
541 struct isl_chamber_list *next;
543 ctx = isl_vertices_get_ctx(vertices);
544 vertices->c = isl_alloc_array(ctx, struct isl_chamber, n_chambers);
545 if (!vertices->c)
546 goto error;
547 vertices->n_chambers = n_chambers;
549 for (i = 0; list; list = next, i++) {
550 next = list->next;
551 vertices->c[i] = list->c;
552 free(list);
555 return vertices;
556 error:
557 isl_vertices_free(vertices);
558 free_chamber_list(list);
559 return NULL;
562 /* Can "tab" be intersected with "bset" without resulting in
563 * a lower-dimensional set.
564 * "bset" itself is assumed to be full-dimensional.
566 static isl_bool can_intersect(struct isl_tab *tab,
567 __isl_keep isl_basic_set *bset)
569 int i;
570 struct isl_tab_undo *snap;
572 if (bset->n_eq > 0)
573 isl_die(isl_basic_set_get_ctx(bset), isl_error_internal,
574 "expecting full-dimensional input",
575 return isl_bool_error);
577 if (isl_tab_extend_cons(tab, bset->n_ineq) < 0)
578 return isl_bool_error;
580 snap = isl_tab_snap(tab);
582 for (i = 0; i < bset->n_ineq; ++i) {
583 if (isl_tab_ineq_type(tab, bset->ineq[i]) == isl_ineq_redundant)
584 continue;
585 if (isl_tab_add_ineq(tab, bset->ineq[i]) < 0)
586 return isl_bool_error;
589 if (isl_tab_detect_implicit_equalities(tab) < 0)
590 return isl_bool_error;
591 if (tab->n_dead) {
592 if (isl_tab_rollback(tab, snap) < 0)
593 return isl_bool_error;
594 return isl_bool_false;
597 return isl_bool_true;
600 static int add_chamber(struct isl_chamber_list **list,
601 __isl_keep isl_vertices *vertices, struct isl_tab *tab, int *selection)
603 int n_frozen;
604 int i, j;
605 int n_vertices = 0;
606 struct isl_tab_undo *snap;
607 struct isl_chamber_list *c = NULL;
609 for (i = 0; i < vertices->n_vertices; ++i)
610 if (selection[i])
611 n_vertices++;
613 snap = isl_tab_snap(tab);
615 for (i = 0; i < tab->n_con && tab->con[i].frozen; ++i)
616 tab->con[i].frozen = 0;
617 n_frozen = i;
619 if (isl_tab_detect_redundant(tab) < 0)
620 return -1;
622 c = isl_calloc_type(tab->mat->ctx, struct isl_chamber_list);
623 if (!c)
624 goto error;
625 c->c.vertices = isl_alloc_array(tab->mat->ctx, int, n_vertices);
626 if (n_vertices && !c->c.vertices)
627 goto error;
628 c->c.dom = isl_basic_set_copy(isl_tab_peek_bset(tab));
629 c->c.dom = isl_basic_set_set_rational(c->c.dom);
630 c->c.dom = isl_basic_set_cow(c->c.dom);
631 c->c.dom = isl_basic_set_update_from_tab(c->c.dom, tab);
632 c->c.dom = isl_basic_set_simplify(c->c.dom);
633 c->c.dom = isl_basic_set_finalize(c->c.dom);
634 if (!c->c.dom)
635 goto error;
637 c->c.n_vertices = n_vertices;
639 for (i = 0, j = 0; i < vertices->n_vertices; ++i)
640 if (selection[i]) {
641 c->c.vertices[j] = i;
642 j++;
645 c->next = *list;
646 *list = c;
648 for (i = 0; i < n_frozen; ++i)
649 tab->con[i].frozen = 1;
651 if (isl_tab_rollback(tab, snap) < 0)
652 return -1;
654 return 0;
655 error:
656 free_chamber_list(c);
657 return -1;
660 struct isl_facet_todo {
661 struct isl_tab *tab; /* A tableau representation of the facet */
662 isl_basic_set *bset; /* A normalized basic set representation */
663 isl_vec *constraint; /* Constraint pointing to the other side */
664 struct isl_facet_todo *next;
667 static void free_todo(struct isl_facet_todo *todo)
669 while (todo) {
670 struct isl_facet_todo *next = todo->next;
672 isl_tab_free(todo->tab);
673 isl_basic_set_free(todo->bset);
674 isl_vec_free(todo->constraint);
675 free(todo);
677 todo = next;
681 static struct isl_facet_todo *create_todo(struct isl_tab *tab, int con)
683 int i;
684 int n_frozen;
685 struct isl_tab_undo *snap;
686 struct isl_facet_todo *todo;
688 snap = isl_tab_snap(tab);
690 for (i = 0; i < tab->n_con && tab->con[i].frozen; ++i)
691 tab->con[i].frozen = 0;
692 n_frozen = i;
694 if (isl_tab_detect_redundant(tab) < 0)
695 return NULL;
697 todo = isl_calloc_type(tab->mat->ctx, struct isl_facet_todo);
698 if (!todo)
699 return NULL;
701 todo->constraint = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
702 if (!todo->constraint)
703 goto error;
704 isl_seq_neg(todo->constraint->el, tab->bmap->ineq[con], 1 + tab->n_var);
705 todo->bset = isl_basic_set_copy(isl_tab_peek_bset(tab));
706 todo->bset = isl_basic_set_set_rational(todo->bset);
707 todo->bset = isl_basic_set_cow(todo->bset);
708 todo->bset = isl_basic_set_update_from_tab(todo->bset, tab);
709 todo->bset = isl_basic_set_simplify(todo->bset);
710 todo->bset = isl_basic_set_sort_constraints(todo->bset);
711 if (!todo->bset)
712 goto error;
713 ISL_F_SET(todo->bset, ISL_BASIC_SET_NO_REDUNDANT);
714 todo->tab = isl_tab_dup(tab);
715 if (!todo->tab)
716 goto error;
718 for (i = 0; i < n_frozen; ++i)
719 tab->con[i].frozen = 1;
721 if (isl_tab_rollback(tab, snap) < 0)
722 goto error;
724 return todo;
725 error:
726 free_todo(todo);
727 return NULL;
730 /* Create todo items for all interior facets of the chamber represented
731 * by "tab" and collect them in "next".
733 static int init_todo(struct isl_facet_todo **next, struct isl_tab *tab)
735 int i;
736 struct isl_tab_undo *snap;
737 struct isl_facet_todo *todo;
739 snap = isl_tab_snap(tab);
741 for (i = 0; i < tab->n_con; ++i) {
742 if (tab->con[i].frozen)
743 continue;
744 if (tab->con[i].is_redundant)
745 continue;
747 if (isl_tab_select_facet(tab, i) < 0)
748 return -1;
750 todo = create_todo(tab, i);
751 if (!todo)
752 return -1;
754 todo->next = *next;
755 *next = todo;
757 if (isl_tab_rollback(tab, snap) < 0)
758 return -1;
761 return 0;
764 /* Does the linked list contain a todo item that is the opposite of "todo".
765 * If so, return 1 and remove the opposite todo item.
767 static int has_opposite(struct isl_facet_todo *todo,
768 struct isl_facet_todo **list)
770 for (; *list; list = &(*list)->next) {
771 int eq;
772 eq = isl_basic_set_plain_is_equal(todo->bset, (*list)->bset);
773 if (eq < 0)
774 return -1;
775 if (!eq)
776 continue;
777 todo = *list;
778 *list = todo->next;
779 todo->next = NULL;
780 free_todo(todo);
781 return 1;
784 return 0;
787 /* Create todo items for all interior facets of the chamber represented
788 * by "tab" and collect them in first->next, taking care to cancel
789 * opposite todo items.
791 static int update_todo(struct isl_facet_todo *first, struct isl_tab *tab)
793 int i;
794 struct isl_tab_undo *snap;
795 struct isl_facet_todo *todo;
797 snap = isl_tab_snap(tab);
799 for (i = 0; i < tab->n_con; ++i) {
800 int drop;
802 if (tab->con[i].frozen)
803 continue;
804 if (tab->con[i].is_redundant)
805 continue;
807 if (isl_tab_select_facet(tab, i) < 0)
808 return -1;
810 todo = create_todo(tab, i);
811 if (!todo)
812 return -1;
814 drop = has_opposite(todo, &first->next);
815 if (drop < 0)
816 return -1;
818 if (drop)
819 free_todo(todo);
820 else {
821 todo->next = first->next;
822 first->next = todo;
825 if (isl_tab_rollback(tab, snap) < 0)
826 return -1;
829 return 0;
832 /* Compute the chamber decomposition of the parametric polytope respresented
833 * by "bset" given the parametric vertices and their activity domains.
835 * We are only interested in full-dimensional chambers.
836 * Each of these chambers is the intersection of the activity domains of
837 * one or more vertices and the union of all chambers is equal to the
838 * projection of the entire parametric polytope onto the parameter space.
840 * We first create an initial chamber by intersecting as many activity
841 * domains as possible without ending up with an empty or lower-dimensional
842 * set. As a minor optimization, we only consider those activity domains
843 * that contain some arbitrary point.
845 * For each of the interior facets of the chamber, we construct a todo item,
846 * containing the facet and a constraint containing the other side of the facet,
847 * for constructing the chamber on the other side.
848 * While their are any todo items left, we pick a todo item and
849 * create the required chamber by intersecting all activity domains
850 * that contain the facet and have a full-dimensional intersection with
851 * the other side of the facet. For each of the interior facets, we
852 * again create todo items, taking care to cancel opposite todo items.
854 static __isl_give isl_vertices *compute_chambers(__isl_take isl_basic_set *bset,
855 __isl_take isl_vertices *vertices)
857 int i;
858 isl_ctx *ctx;
859 isl_vec *sample = NULL;
860 struct isl_tab *tab = NULL;
861 struct isl_tab_undo *snap;
862 int *selection = NULL;
863 int n_chambers = 0;
864 struct isl_chamber_list *list = NULL;
865 struct isl_facet_todo *todo = NULL;
867 if (!bset || !vertices)
868 goto error;
870 ctx = isl_vertices_get_ctx(vertices);
871 selection = isl_alloc_array(ctx, int, vertices->n_vertices);
872 if (vertices->n_vertices && !selection)
873 goto error;
875 bset = isl_basic_set_params(bset);
877 tab = isl_tab_from_basic_set(bset, 1);
878 if (!tab)
879 goto error;
880 for (i = 0; i < bset->n_ineq; ++i)
881 if (isl_tab_freeze_constraint(tab, i) < 0)
882 goto error;
883 isl_basic_set_free(bset);
885 snap = isl_tab_snap(tab);
887 sample = isl_tab_get_sample_value(tab);
889 for (i = 0; i < vertices->n_vertices; ++i) {
890 selection[i] = isl_basic_set_contains(vertices->v[i].dom, sample);
891 if (selection[i] < 0)
892 goto error;
893 if (!selection[i])
894 continue;
895 selection[i] = can_intersect(tab, vertices->v[i].dom);
896 if (selection[i] < 0)
897 goto error;
900 if (isl_tab_detect_redundant(tab) < 0)
901 goto error;
903 if (add_chamber(&list, vertices, tab, selection) < 0)
904 goto error;
905 n_chambers++;
907 if (init_todo(&todo, tab) < 0)
908 goto error;
910 while (todo) {
911 struct isl_facet_todo *next;
913 if (isl_tab_rollback(tab, snap) < 0)
914 goto error;
916 if (isl_tab_add_ineq(tab, todo->constraint->el) < 0)
917 goto error;
918 if (isl_tab_freeze_constraint(tab, tab->n_con - 1) < 0)
919 goto error;
921 for (i = 0; i < vertices->n_vertices; ++i) {
922 selection[i] = bset_covers_tab(vertices->v[i].dom,
923 todo->tab);
924 if (selection[i] < 0)
925 goto error;
926 if (!selection[i])
927 continue;
928 selection[i] = can_intersect(tab, vertices->v[i].dom);
929 if (selection[i] < 0)
930 goto error;
933 if (isl_tab_detect_redundant(tab) < 0)
934 goto error;
936 if (add_chamber(&list, vertices, tab, selection) < 0)
937 goto error;
938 n_chambers++;
940 if (update_todo(todo, tab) < 0)
941 goto error;
943 next = todo->next;
944 todo->next = NULL;
945 free_todo(todo);
946 todo = next;
949 isl_vec_free(sample);
951 isl_tab_free(tab);
952 free(selection);
954 vertices = vertices_add_chambers(vertices, n_chambers, list);
956 for (i = 0; vertices && i < vertices->n_vertices; ++i) {
957 isl_basic_set_free(vertices->v[i].dom);
958 vertices->v[i].dom = NULL;
961 return vertices;
962 error:
963 free_chamber_list(list);
964 free_todo(todo);
965 isl_vec_free(sample);
966 isl_tab_free(tab);
967 free(selection);
968 if (!tab)
969 isl_basic_set_free(bset);
970 isl_vertices_free(vertices);
971 return NULL;
974 isl_ctx *isl_vertex_get_ctx(__isl_keep isl_vertex *vertex)
976 return vertex ? isl_vertices_get_ctx(vertex->vertices) : NULL;
979 int isl_vertex_get_id(__isl_keep isl_vertex *vertex)
981 return vertex ? vertex->id : -1;
984 /* Return the activity domain of the vertex "vertex".
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 v->dom = isl_basic_set_copy(v->vertex);
996 v->dom = isl_basic_set_params(v->dom);
997 v->dom = isl_basic_set_set_integral(v->dom);
1000 return isl_basic_set_copy(v->dom);
1003 /* Return a multiple quasi-affine expression describing the vertex "vertex"
1004 * in terms of the parameters,
1006 __isl_give isl_multi_aff *isl_vertex_get_expr(__isl_keep isl_vertex *vertex)
1008 struct isl_vertex *v;
1009 isl_basic_set *bset;
1011 if (!vertex)
1012 return NULL;
1014 v = &vertex->vertices->v[vertex->id];
1016 bset = isl_basic_set_copy(v->vertex);
1017 return isl_multi_aff_from_basic_set_equalities(bset);
1020 static __isl_give isl_vertex *isl_vertex_alloc(__isl_take isl_vertices *vertices,
1021 int id)
1023 isl_ctx *ctx;
1024 isl_vertex *vertex;
1026 if (!vertices)
1027 return NULL;
1029 ctx = isl_vertices_get_ctx(vertices);
1030 vertex = isl_alloc_type(ctx, isl_vertex);
1031 if (!vertex)
1032 goto error;
1034 vertex->vertices = vertices;
1035 vertex->id = id;
1037 return vertex;
1038 error:
1039 isl_vertices_free(vertices);
1040 return NULL;
1043 __isl_null isl_vertex *isl_vertex_free(__isl_take isl_vertex *vertex)
1045 if (!vertex)
1046 return NULL;
1047 isl_vertices_free(vertex->vertices);
1048 free(vertex);
1050 return NULL;
1053 isl_ctx *isl_cell_get_ctx(__isl_keep isl_cell *cell)
1055 return cell ? cell->dom->ctx : NULL;
1058 __isl_give isl_basic_set *isl_cell_get_domain(__isl_keep isl_cell *cell)
1060 return cell ? isl_basic_set_copy(cell->dom) : NULL;
1063 static __isl_give isl_cell *isl_cell_alloc(__isl_take isl_vertices *vertices,
1064 __isl_take isl_basic_set *dom, int id)
1066 int i;
1067 isl_cell *cell = NULL;
1069 if (!vertices || !dom)
1070 goto error;
1072 cell = isl_calloc_type(dom->ctx, isl_cell);
1073 if (!cell)
1074 goto error;
1076 cell->n_vertices = vertices->c[id].n_vertices;
1077 cell->ids = isl_alloc_array(dom->ctx, int, cell->n_vertices);
1078 if (cell->n_vertices && !cell->ids)
1079 goto error;
1080 for (i = 0; i < cell->n_vertices; ++i)
1081 cell->ids[i] = vertices->c[id].vertices[i];
1082 cell->vertices = vertices;
1083 cell->dom = dom;
1085 return cell;
1086 error:
1087 isl_cell_free(cell);
1088 isl_vertices_free(vertices);
1089 isl_basic_set_free(dom);
1090 return NULL;
1093 __isl_null isl_cell *isl_cell_free(__isl_take isl_cell *cell)
1095 if (!cell)
1096 return NULL;
1098 isl_vertices_free(cell->vertices);
1099 free(cell->ids);
1100 isl_basic_set_free(cell->dom);
1101 free(cell);
1103 return NULL;
1106 /* Create a tableau of the cone obtained by first homogenizing the given
1107 * polytope and then making all inequalities strict by setting the
1108 * constant term to -1.
1110 static struct isl_tab *tab_for_shifted_cone(__isl_keep isl_basic_set *bset)
1112 int i;
1113 isl_vec *c = NULL;
1114 struct isl_tab *tab;
1116 if (!bset)
1117 return NULL;
1118 tab = isl_tab_alloc(bset->ctx, bset->n_eq + bset->n_ineq + 1,
1119 1 + isl_basic_set_total_dim(bset), 0);
1120 if (!tab)
1121 return NULL;
1122 tab->rational = ISL_F_ISSET(bset, ISL_BASIC_SET_RATIONAL);
1123 if (ISL_F_ISSET(bset, ISL_BASIC_MAP_EMPTY)) {
1124 if (isl_tab_mark_empty(tab) < 0)
1125 goto error;
1126 return tab;
1129 c = isl_vec_alloc(bset->ctx, 1 + 1 + isl_basic_set_total_dim(bset));
1130 if (!c)
1131 goto error;
1133 isl_int_set_si(c->el[0], 0);
1134 for (i = 0; i < bset->n_eq; ++i) {
1135 isl_seq_cpy(c->el + 1, bset->eq[i], c->size - 1);
1136 if (isl_tab_add_eq(tab, c->el) < 0)
1137 goto error;
1140 isl_int_set_si(c->el[0], -1);
1141 for (i = 0; i < bset->n_ineq; ++i) {
1142 isl_seq_cpy(c->el + 1, bset->ineq[i], c->size - 1);
1143 if (isl_tab_add_ineq(tab, c->el) < 0)
1144 goto error;
1145 if (tab->empty) {
1146 isl_vec_free(c);
1147 return tab;
1151 isl_seq_clr(c->el + 1, c->size - 1);
1152 isl_int_set_si(c->el[1], 1);
1153 if (isl_tab_add_ineq(tab, c->el) < 0)
1154 goto error;
1156 isl_vec_free(c);
1157 return tab;
1158 error:
1159 isl_vec_free(c);
1160 isl_tab_free(tab);
1161 return NULL;
1164 /* Compute an interior point of "bset" by selecting an interior
1165 * point in homogeneous space and projecting the point back down.
1167 static __isl_give isl_vec *isl_basic_set_interior_point(
1168 __isl_keep isl_basic_set *bset)
1170 isl_vec *vec;
1171 struct isl_tab *tab;
1173 tab = tab_for_shifted_cone(bset);
1174 vec = isl_tab_get_sample_value(tab);
1175 isl_tab_free(tab);
1176 if (!vec)
1177 return NULL;
1179 isl_seq_cpy(vec->el, vec->el + 1, vec->size - 1);
1180 vec->size--;
1182 return vec;
1185 /* Call "fn" on all chambers of the parametric polytope with the shared
1186 * facets of neighboring chambers only appearing in one of the chambers.
1188 * We pick an interior point from one of the chambers and then make
1189 * all constraints that do not satisfy this point strict.
1190 * For constraints that saturate the interior point, the sign
1191 * of the first non-zero coefficient is used to determine which
1192 * of the two (internal) constraints should be tightened.
1194 isl_stat isl_vertices_foreach_disjoint_cell(__isl_keep isl_vertices *vertices,
1195 isl_stat (*fn)(__isl_take isl_cell *cell, void *user), void *user)
1197 int i;
1198 isl_vec *vec;
1199 isl_cell *cell;
1201 if (!vertices)
1202 return isl_stat_error;
1204 if (vertices->n_chambers == 0)
1205 return isl_stat_ok;
1207 if (vertices->n_chambers == 1) {
1208 isl_basic_set *dom = isl_basic_set_copy(vertices->c[0].dom);
1209 dom = isl_basic_set_set_integral(dom);
1210 cell = isl_cell_alloc(isl_vertices_copy(vertices), dom, 0);
1211 if (!cell)
1212 return isl_stat_error;
1213 return fn(cell, user);
1216 vec = isl_basic_set_interior_point(vertices->c[0].dom);
1217 if (!vec)
1218 return isl_stat_error;
1220 for (i = 0; i < vertices->n_chambers; ++i) {
1221 int r;
1222 isl_basic_set *dom = isl_basic_set_copy(vertices->c[i].dom);
1223 if (i)
1224 dom = isl_basic_set_tighten_outward(dom, vec);
1225 dom = isl_basic_set_set_integral(dom);
1226 cell = isl_cell_alloc(isl_vertices_copy(vertices), dom, i);
1227 if (!cell)
1228 goto error;
1229 r = fn(cell, user);
1230 if (r < 0)
1231 goto error;
1234 isl_vec_free(vec);
1236 return isl_stat_ok;
1237 error:
1238 isl_vec_free(vec);
1239 return isl_stat_error;
1242 isl_stat isl_vertices_foreach_cell(__isl_keep isl_vertices *vertices,
1243 isl_stat (*fn)(__isl_take isl_cell *cell, void *user), void *user)
1245 int i;
1246 isl_cell *cell;
1248 if (!vertices)
1249 return isl_stat_error;
1251 if (vertices->n_chambers == 0)
1252 return isl_stat_ok;
1254 for (i = 0; i < vertices->n_chambers; ++i) {
1255 isl_stat r;
1256 isl_basic_set *dom = isl_basic_set_copy(vertices->c[i].dom);
1258 cell = isl_cell_alloc(isl_vertices_copy(vertices), dom, i);
1259 if (!cell)
1260 return isl_stat_error;
1262 r = fn(cell, user);
1263 if (r < 0)
1264 return isl_stat_error;
1267 return isl_stat_ok;
1270 isl_stat isl_vertices_foreach_vertex(__isl_keep isl_vertices *vertices,
1271 isl_stat (*fn)(__isl_take isl_vertex *vertex, void *user), void *user)
1273 int i;
1274 isl_vertex *vertex;
1276 if (!vertices)
1277 return isl_stat_error;
1279 if (vertices->n_vertices == 0)
1280 return isl_stat_ok;
1282 for (i = 0; i < vertices->n_vertices; ++i) {
1283 isl_stat r;
1285 vertex = isl_vertex_alloc(isl_vertices_copy(vertices), i);
1286 if (!vertex)
1287 return isl_stat_error;
1289 r = fn(vertex, user);
1290 if (r < 0)
1291 return isl_stat_error;
1294 return isl_stat_ok;
1297 isl_stat isl_cell_foreach_vertex(__isl_keep isl_cell *cell,
1298 isl_stat (*fn)(__isl_take isl_vertex *vertex, void *user), void *user)
1300 int i;
1301 isl_vertex *vertex;
1303 if (!cell)
1304 return isl_stat_error;
1306 if (cell->n_vertices == 0)
1307 return isl_stat_ok;
1309 for (i = 0; i < cell->n_vertices; ++i) {
1310 isl_stat r;
1312 vertex = isl_vertex_alloc(isl_vertices_copy(cell->vertices),
1313 cell->ids[i]);
1314 if (!vertex)
1315 return isl_stat_error;
1317 r = fn(vertex, user);
1318 if (r < 0)
1319 return isl_stat_error;
1322 return isl_stat_ok;
1325 isl_ctx *isl_vertices_get_ctx(__isl_keep isl_vertices *vertices)
1327 return vertices ? vertices->bset->ctx : NULL;
1330 int isl_vertices_get_n_vertices(__isl_keep isl_vertices *vertices)
1332 return vertices ? vertices->n_vertices : -1;
1335 __isl_give isl_vertices *isl_morph_vertices(__isl_take isl_morph *morph,
1336 __isl_take isl_vertices *vertices)
1338 int i;
1339 isl_morph *param_morph = NULL;
1341 if (!morph || !vertices)
1342 goto error;
1344 isl_assert(vertices->bset->ctx, vertices->ref == 1, goto error);
1346 param_morph = isl_morph_copy(morph);
1347 param_morph = isl_morph_dom_params(param_morph);
1348 param_morph = isl_morph_ran_params(param_morph);
1350 for (i = 0; i < vertices->n_vertices; ++i) {
1351 vertices->v[i].dom = isl_morph_basic_set(
1352 isl_morph_copy(param_morph), vertices->v[i].dom);
1353 vertices->v[i].vertex = isl_morph_basic_set(
1354 isl_morph_copy(morph), vertices->v[i].vertex);
1355 if (!vertices->v[i].vertex)
1356 goto error;
1359 for (i = 0; i < vertices->n_chambers; ++i) {
1360 vertices->c[i].dom = isl_morph_basic_set(
1361 isl_morph_copy(param_morph), vertices->c[i].dom);
1362 if (!vertices->c[i].dom)
1363 goto error;
1366 isl_morph_free(param_morph);
1367 isl_morph_free(morph);
1368 return vertices;
1369 error:
1370 isl_morph_free(param_morph);
1371 isl_morph_free(morph);
1372 isl_vertices_free(vertices);
1373 return NULL;
1376 /* Construct a simplex isl_cell spanned by the vertices with indices in
1377 * "simplex_ids" and "other_ids" and call "fn" on this isl_cell.
1379 static isl_stat call_on_simplex(__isl_keep isl_cell *cell,
1380 int *simplex_ids, int n_simplex, int *other_ids, int n_other,
1381 isl_stat (*fn)(__isl_take isl_cell *simplex, void *user), void *user)
1383 int i;
1384 isl_ctx *ctx;
1385 struct isl_cell *simplex;
1387 ctx = isl_cell_get_ctx(cell);
1389 simplex = isl_calloc_type(ctx, struct isl_cell);
1390 if (!simplex)
1391 return isl_stat_error;
1392 simplex->vertices = isl_vertices_copy(cell->vertices);
1393 if (!simplex->vertices)
1394 goto error;
1395 simplex->dom = isl_basic_set_copy(cell->dom);
1396 if (!simplex->dom)
1397 goto error;
1398 simplex->n_vertices = n_simplex + n_other;
1399 simplex->ids = isl_alloc_array(ctx, int, simplex->n_vertices);
1400 if (!simplex->ids)
1401 goto error;
1403 for (i = 0; i < n_simplex; ++i)
1404 simplex->ids[i] = simplex_ids[i];
1405 for (i = 0; i < n_other; ++i)
1406 simplex->ids[n_simplex + i] = other_ids[i];
1408 return fn(simplex, user);
1409 error:
1410 isl_cell_free(simplex);
1411 return isl_stat_error;
1414 /* Check whether the parametric vertex described by "vertex"
1415 * lies on the facet corresponding to constraint "facet" of "bset".
1416 * The isl_vec "v" is a temporary vector than can be used by this function.
1418 * We eliminate the variables from the facet constraint using the
1419 * equalities defining the vertex and check if the result is identical
1420 * to zero.
1422 * It would probably be better to keep track of the constraints defining
1423 * a vertex during the vertex construction so that we could simply look
1424 * it up here.
1426 static int vertex_on_facet(__isl_keep isl_basic_set *vertex,
1427 __isl_keep isl_basic_set *bset, int facet, __isl_keep isl_vec *v)
1429 int i;
1430 isl_int m;
1432 isl_seq_cpy(v->el, bset->ineq[facet], v->size);
1434 isl_int_init(m);
1435 for (i = 0; i < vertex->n_eq; ++i) {
1436 int k = isl_seq_last_non_zero(vertex->eq[i], v->size);
1437 isl_seq_elim(v->el, vertex->eq[i], k, v->size, &m);
1439 isl_int_clear(m);
1441 return isl_seq_first_non_zero(v->el, v->size) == -1;
1444 /* Triangulate the polytope spanned by the vertices with ids
1445 * in "simplex_ids" and "other_ids" and call "fn" on each of
1446 * the resulting simplices.
1447 * If the input polytope is already a simplex, we simply call "fn".
1448 * Otherwise, we pick a point from "other_ids" and add it to "simplex_ids".
1449 * Then we consider each facet of "bset" that does not contain the point
1450 * we just picked, but does contain some of the other points in "other_ids"
1451 * and call ourselves recursively on the polytope spanned by the new
1452 * "simplex_ids" and those points in "other_ids" that lie on the facet.
1454 static isl_stat triangulate(__isl_keep isl_cell *cell, __isl_keep isl_vec *v,
1455 int *simplex_ids, int n_simplex, int *other_ids, int n_other,
1456 isl_stat (*fn)(__isl_take isl_cell *simplex, void *user), void *user)
1458 int i, j, k;
1459 int d, nparam;
1460 int *ids;
1461 isl_ctx *ctx;
1462 isl_basic_set *vertex;
1463 isl_basic_set *bset;
1465 ctx = isl_cell_get_ctx(cell);
1466 d = isl_basic_set_dim(cell->vertices->bset, isl_dim_set);
1467 nparam = isl_basic_set_dim(cell->vertices->bset, isl_dim_param);
1469 if (n_simplex + n_other == d + 1)
1470 return call_on_simplex(cell, simplex_ids, n_simplex,
1471 other_ids, n_other, fn, user);
1473 simplex_ids[n_simplex] = other_ids[0];
1474 vertex = cell->vertices->v[other_ids[0]].vertex;
1475 bset = cell->vertices->bset;
1477 ids = isl_alloc_array(ctx, int, n_other - 1);
1478 if (!ids)
1479 goto error;
1480 for (i = 0; i < bset->n_ineq; ++i) {
1481 if (isl_seq_first_non_zero(bset->ineq[i] + 1 + nparam, d) == -1)
1482 continue;
1483 if (vertex_on_facet(vertex, bset, i, v))
1484 continue;
1486 for (j = 1, k = 0; j < n_other; ++j) {
1487 isl_basic_set *ov;
1488 ov = cell->vertices->v[other_ids[j]].vertex;
1489 if (vertex_on_facet(ov, bset, i, v))
1490 ids[k++] = other_ids[j];
1492 if (k == 0)
1493 continue;
1495 if (triangulate(cell, v, simplex_ids, n_simplex + 1,
1496 ids, k, fn, user) < 0)
1497 goto error;
1499 free(ids);
1501 return isl_stat_ok;
1502 error:
1503 free(ids);
1504 return isl_stat_error;
1507 /* Triangulate the given cell and call "fn" on each of the resulting
1508 * simplices.
1510 isl_stat isl_cell_foreach_simplex(__isl_take isl_cell *cell,
1511 isl_stat (*fn)(__isl_take isl_cell *simplex, void *user), void *user)
1513 int d, total;
1514 isl_stat r;
1515 isl_ctx *ctx;
1516 isl_vec *v = NULL;
1517 int *simplex_ids = NULL;
1519 if (!cell)
1520 return isl_stat_error;
1522 d = isl_basic_set_dim(cell->vertices->bset, isl_dim_set);
1523 total = isl_basic_set_total_dim(cell->vertices->bset);
1525 if (cell->n_vertices == d + 1)
1526 return fn(cell, user);
1528 ctx = isl_cell_get_ctx(cell);
1529 simplex_ids = isl_alloc_array(ctx, int, d + 1);
1530 if (!simplex_ids)
1531 goto error;
1533 v = isl_vec_alloc(ctx, 1 + total);
1534 if (!v)
1535 goto error;
1537 r = triangulate(cell, v, simplex_ids, 0,
1538 cell->ids, cell->n_vertices, fn, user);
1540 isl_vec_free(v);
1541 free(simplex_ids);
1543 isl_cell_free(cell);
1545 return r;
1546 error:
1547 free(simplex_ids);
1548 isl_vec_free(v);
1549 isl_cell_free(cell);
1550 return isl_stat_error;