isl_schedule_constraints: split proximity constraints into coincidence/proximity
[isl.git] / isl_vertices.c
blob1e22ad160a0dd2e2b5c00ad3c354e8f9c89cf530
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/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>
19 #include <isl_vec_private.h>
21 #define SELECTED 1
22 #define DESELECTED -1
23 #define UNSELECTED 0
25 static __isl_give isl_vertices *compute_chambers(__isl_take isl_basic_set *bset,
26 __isl_take isl_vertices *vertices);
28 __isl_give isl_vertices *isl_vertices_copy(__isl_keep isl_vertices *vertices)
30 if (!vertices)
31 return NULL;
33 vertices->ref++;
34 return vertices;
37 void isl_vertices_free(__isl_take isl_vertices *vertices)
39 int i;
41 if (!vertices)
42 return;
44 if (--vertices->ref > 0)
45 return;
47 for (i = 0; i < vertices->n_vertices; ++i) {
48 isl_basic_set_free(vertices->v[i].vertex);
49 isl_basic_set_free(vertices->v[i].dom);
51 free(vertices->v);
53 for (i = 0; i < vertices->n_chambers; ++i) {
54 free(vertices->c[i].vertices);
55 isl_basic_set_free(vertices->c[i].dom);
57 free(vertices->c);
59 isl_basic_set_free(vertices->bset);
60 free(vertices);
63 struct isl_vertex_list {
64 struct isl_vertex v;
65 struct isl_vertex_list *next;
68 static void free_vertex_list(struct isl_vertex_list *list)
70 struct isl_vertex_list *next;
72 for (; list; list = next) {
73 next = list->next;
74 isl_basic_set_free(list->v.vertex);
75 isl_basic_set_free(list->v.dom);
76 free(list);
80 static __isl_give isl_vertices *vertices_from_list(__isl_keep isl_basic_set *bset,
81 int n_vertices, struct isl_vertex_list *list)
83 int i;
84 struct isl_vertex_list *next;
85 isl_vertices *vertices;
87 vertices = isl_calloc_type(bset->ctx, isl_vertices);
88 if (!vertices)
89 goto error;
90 vertices->ref = 1;
91 vertices->bset = isl_basic_set_copy(bset);
92 vertices->v = isl_alloc_array(bset->ctx, struct isl_vertex, n_vertices);
93 if (n_vertices && !vertices->v)
94 goto error;
95 vertices->n_vertices = n_vertices;
97 for (i = 0; list; list = next, i++) {
98 next = list->next;
99 vertices->v[i] = list->v;
100 free(list);
103 return vertices;
104 error:
105 isl_vertices_free(vertices);
106 free_vertex_list(list);
107 return NULL;
110 /* Prepend a vertex to the linked list "list" based on the equalities in "tab".
112 static int add_vertex(struct isl_vertex_list **list,
113 __isl_keep isl_basic_set *bset, struct isl_tab *tab)
115 unsigned nvar;
116 unsigned nparam;
117 struct isl_vertex_list *v = NULL;
119 if (isl_tab_detect_implicit_equalities(tab) < 0)
120 return -1;
122 nvar = isl_basic_set_dim(bset, isl_dim_set);
123 nparam = isl_basic_set_dim(bset, isl_dim_param);
125 v = isl_calloc_type(tab->mat->ctx, struct isl_vertex_list);
126 if (!v)
127 goto error;
129 v->v.vertex = isl_basic_set_copy(bset);
130 v->v.vertex = isl_basic_set_cow(v->v.vertex);
131 v->v.vertex = isl_basic_set_update_from_tab(v->v.vertex, tab);
132 v->v.vertex = isl_basic_set_simplify(v->v.vertex);
133 v->v.vertex = isl_basic_set_finalize(v->v.vertex);
134 if (!v->v.vertex)
135 goto error;
136 isl_assert(bset->ctx, v->v.vertex->n_eq >= nvar, goto error);
137 v->v.dom = isl_basic_set_copy(v->v.vertex);
138 v->v.dom = isl_basic_set_params(v->v.dom);
139 if (!v->v.dom)
140 goto error;
142 v->next = *list;
143 *list = v;
145 return 0;
146 error:
147 free_vertex_list(v);
148 return -1;
151 /* Compute the parametric vertices and the chamber decomposition
152 * of an empty parametric polytope.
154 static __isl_give isl_vertices *vertices_empty(__isl_keep isl_basic_set *bset)
156 isl_vertices *vertices;
157 unsigned nparam;
159 if (!bset)
160 return NULL;
162 nparam = isl_basic_set_dim(bset, isl_dim_param);
164 vertices = isl_calloc_type(bset->ctx, isl_vertices);
165 if (!vertices)
166 return NULL;
167 vertices->bset = isl_basic_set_copy(bset);
168 vertices->ref = 1;
170 vertices->n_vertices = 0;
171 vertices->n_chambers = 0;
173 return vertices;
176 /* Compute the parametric vertices and the chamber decomposition
177 * of the parametric polytope defined using the same constraints
178 * as "bset" in the 0D case.
179 * There is exactly one 0D vertex and a single chamber containing
180 * the vertex.
182 static __isl_give isl_vertices *vertices_0D(__isl_keep isl_basic_set *bset)
184 isl_vertices *vertices;
185 unsigned nparam;
187 if (!bset)
188 return NULL;
190 nparam = isl_basic_set_dim(bset, isl_dim_param);
192 vertices = isl_calloc_type(bset->ctx, isl_vertices);
193 if (!vertices)
194 return NULL;
195 vertices->ref = 1;
196 vertices->bset = isl_basic_set_copy(bset);
198 vertices->v = isl_calloc_array(bset->ctx, struct isl_vertex, 1);
199 if (!vertices->v)
200 goto error;
201 vertices->n_vertices = 1;
202 vertices->v[0].vertex = isl_basic_set_copy(bset);
203 vertices->v[0].dom = isl_basic_set_params(isl_basic_set_copy(bset));
204 if (!vertices->v[0].vertex || !vertices->v[0].dom)
205 goto error;
207 vertices->c = isl_calloc_array(bset->ctx, struct isl_chamber, 1);
208 if (!vertices->c)
209 goto error;
210 vertices->n_chambers = 1;
211 vertices->c[0].n_vertices = 1;
212 vertices->c[0].vertices = isl_calloc_array(bset->ctx, int, 1);
213 if (!vertices->c[0].vertices)
214 goto error;
215 vertices->c[0].dom = isl_basic_set_copy(vertices->v[0].dom);
216 if (!vertices->c[0].dom)
217 goto error;
219 return vertices;
220 error:
221 isl_vertices_free(vertices);
222 return NULL;
225 static int isl_mat_rank(__isl_keep isl_mat *mat)
227 int row, col;
228 isl_mat *H;
230 H = isl_mat_left_hermite(isl_mat_copy(mat), 0, NULL, NULL);
231 if (!H)
232 return -1;
234 for (col = 0; col < H->n_col; ++col) {
235 for (row = 0; row < H->n_row; ++row)
236 if (!isl_int_is_zero(H->row[row][col]))
237 break;
238 if (row == H->n_row)
239 break;
242 isl_mat_free(H);
244 return col;
247 /* Is the row pointed to by "f" linearly independent of the "n" first
248 * rows in "facets"?
250 static int is_independent(__isl_keep isl_mat *facets, int n, isl_int *f)
252 int rank;
254 if (isl_seq_first_non_zero(f, facets->n_col) < 0)
255 return 0;
257 isl_seq_cpy(facets->row[n], f, facets->n_col);
258 facets->n_row = n + 1;
259 rank = isl_mat_rank(facets);
260 if (rank < 0)
261 return -1;
263 return rank == n + 1;
266 /* Check whether we can select constraint "level", given the current selection
267 * reflected by facets in "tab", the rows of "facets" and the earlier
268 * "selected" elements of "selection".
270 * If the constraint is (strictly) redundant in the tableau, selecting it would
271 * result in an empty tableau, so it can't be selected.
272 * If the set variable part of the constraint is not linearly indepedent
273 * of the set variable parts of the already selected constraints,
274 * the constraint cannot be selected.
275 * If selecting the constraint results in an empty tableau, the constraint
276 * cannot be selected.
277 * Finally, if selecting the constraint results in some explicitly
278 * deselected constraints turning into equalities, then the corresponding
279 * vertices have already been generated, so the constraint cannot be selected.
281 static int can_select(__isl_keep isl_basic_set *bset, int level,
282 struct isl_tab *tab, __isl_keep isl_mat *facets, int selected,
283 int *selection)
285 int i;
286 int indep;
287 unsigned ovar;
288 struct isl_tab_undo *snap;
290 if (isl_tab_is_redundant(tab, level))
291 return 0;
293 ovar = isl_space_offset(bset->dim, isl_dim_set);
295 indep = is_independent(facets, selected, bset->ineq[level] + 1 + ovar);
296 if (indep < 0)
297 return -1;
298 if (!indep)
299 return 0;
301 snap = isl_tab_snap(tab);
302 if (isl_tab_select_facet(tab, level) < 0)
303 return -1;
305 if (tab->empty) {
306 if (isl_tab_rollback(tab, snap) < 0)
307 return -1;
308 return 0;
311 for (i = 0; i < level; ++i) {
312 int sgn;
314 if (selection[i] != DESELECTED)
315 continue;
317 if (isl_tab_is_equality(tab, i))
318 sgn = 0;
319 else if (isl_tab_is_redundant(tab, i))
320 sgn = 1;
321 else
322 sgn = isl_tab_sign_of_max(tab, i);
323 if (sgn < -1)
324 return -1;
325 if (sgn <= 0) {
326 if (isl_tab_rollback(tab, snap) < 0)
327 return -1;
328 return 0;
332 return 1;
335 /* Compute the parametric vertices and the chamber decomposition
336 * of a parametric polytope that is not full-dimensional.
338 * Simply map the parametric polytope to a lower dimensional space
339 * and map the resulting vertices back.
341 static __isl_give isl_vertices *lower_dim_vertices(
342 __isl_keep isl_basic_set *bset)
344 isl_morph *morph;
345 isl_vertices *vertices;
347 bset = isl_basic_set_copy(bset);
348 morph = isl_basic_set_full_compression(bset);
349 bset = isl_morph_basic_set(isl_morph_copy(morph), bset);
351 vertices = isl_basic_set_compute_vertices(bset);
352 isl_basic_set_free(bset);
354 morph = isl_morph_inverse(morph);
356 vertices = isl_morph_vertices(morph, vertices);
358 return vertices;
361 /* Compute the parametric vertices and the chamber decomposition
362 * of the parametric polytope defined using the same constraints
363 * as "bset". "bset" is assumed to have no existentially quantified
364 * variables.
366 * The vertices themselves are computed in a fairly simplistic way.
367 * We simply run through all combinations of d constraints,
368 * with d the number of set variables, and check if those d constraints
369 * define a vertex. To avoid the generation of duplicate vertices,
370 * which we may happen if a vertex is defined by more that d constraints,
371 * we make sure we only generate the vertex for the d constraints with
372 * smallest index.
374 * We set up a tableau and keep track of which facets have been
375 * selected. The tableau is marked strict_redundant so that we can be
376 * sure that any constraint that is marked redundant (and that is not
377 * also marked zero) is not an equality.
378 * If a constraint is marked DESELECTED, it means the constraint was
379 * SELECTED before (in combination with the same selection of earlier
380 * constraints). If such a deselected constraint turns out to be an
381 * equality, then any vertex that may still be found with the current
382 * selection has already been generated when the constraint was selected.
383 * A constraint is marked UNSELECTED when there is no way selecting
384 * the constraint could lead to a vertex (in combination with the current
385 * selection of earlier constraints).
387 * The set variable coefficients of the selected constraints are stored
388 * in the facets matrix.
390 __isl_give isl_vertices *isl_basic_set_compute_vertices(
391 __isl_keep isl_basic_set *bset)
393 struct isl_tab *tab;
394 int level;
395 int init;
396 unsigned nvar;
397 int *selection = NULL;
398 int selected;
399 struct isl_tab_undo **snap = NULL;
400 isl_mat *facets = NULL;
401 struct isl_vertex_list *list = NULL;
402 int n_vertices = 0;
403 isl_vertices *vertices;
405 if (!bset)
406 return NULL;
408 if (isl_basic_set_plain_is_empty(bset))
409 return vertices_empty(bset);
411 if (bset->n_eq != 0)
412 return lower_dim_vertices(bset);
414 isl_assert(bset->ctx, isl_basic_set_dim(bset, isl_dim_div) == 0,
415 return NULL);
417 if (isl_basic_set_dim(bset, isl_dim_set) == 0)
418 return vertices_0D(bset);
420 nvar = isl_basic_set_dim(bset, isl_dim_set);
422 bset = isl_basic_set_copy(bset);
423 bset = isl_basic_set_set_rational(bset);
424 if (!bset)
425 return NULL;
427 tab = isl_tab_from_basic_set(bset, 0);
428 if (!tab)
429 goto error;
430 tab->strict_redundant = 1;
432 if (tab->empty) {
433 vertices = vertices_empty(bset);
434 isl_basic_set_free(bset);
435 isl_tab_free(tab);
436 return vertices;
439 selection = isl_alloc_array(bset->ctx, int, bset->n_ineq);
440 snap = isl_alloc_array(bset->ctx, struct isl_tab_undo *, bset->n_ineq);
441 facets = isl_mat_alloc(bset->ctx, nvar, nvar);
442 if ((bset->n_ineq && (!selection || !snap)) || !facets)
443 goto error;
445 level = 0;
446 init = 1;
447 selected = 0;
449 while (level >= 0) {
450 if (level >= bset->n_ineq ||
451 (!init && selection[level] != SELECTED)) {
452 --level;
453 init = 0;
454 continue;
456 if (init) {
457 int ok;
458 snap[level] = isl_tab_snap(tab);
459 ok = can_select(bset, level, tab, facets, selected,
460 selection);
461 if (ok < 0)
462 goto error;
463 if (ok) {
464 selection[level] = SELECTED;
465 selected++;
466 } else
467 selection[level] = UNSELECTED;
468 } else {
469 selection[level] = DESELECTED;
470 selected--;
471 if (isl_tab_rollback(tab, snap[level]) < 0)
472 goto error;
474 if (selected == nvar) {
475 if (tab->n_dead == nvar) {
476 if (add_vertex(&list, bset, tab) < 0)
477 goto error;
478 n_vertices++;
480 init = 0;
481 continue;
483 ++level;
484 init = 1;
487 isl_mat_free(facets);
488 free(selection);
489 free(snap);
491 isl_tab_free(tab);
493 vertices = vertices_from_list(bset, n_vertices, list);
495 vertices = compute_chambers(bset, vertices);
497 return vertices;
498 error:
499 free_vertex_list(list);
500 isl_mat_free(facets);
501 free(selection);
502 free(snap);
503 isl_tab_free(tab);
504 isl_basic_set_free(bset);
505 return NULL;
508 struct isl_chamber_list {
509 struct isl_chamber c;
510 struct isl_chamber_list *next;
513 static void free_chamber_list(struct isl_chamber_list *list)
515 struct isl_chamber_list *next;
517 for (; list; list = next) {
518 next = list->next;
519 isl_basic_set_free(list->c.dom);
520 free(list->c.vertices);
521 free(list);
525 /* Check whether the basic set "bset" is a superset of the basic set described
526 * by "tab", i.e., check whether all constraints of "bset" are redundant.
528 static int bset_covers_tab(__isl_keep isl_basic_set *bset, struct isl_tab *tab)
530 int i;
532 if (!bset || !tab)
533 return -1;
535 for (i = 0; i < bset->n_ineq; ++i) {
536 enum isl_ineq_type type = isl_tab_ineq_type(tab, bset->ineq[i]);
537 switch (type) {
538 case isl_ineq_error: return -1;
539 case isl_ineq_redundant: continue;
540 default: return 0;
544 return 1;
547 static __isl_give isl_vertices *vertices_add_chambers(
548 __isl_take isl_vertices *vertices, int n_chambers,
549 struct isl_chamber_list *list)
551 int i;
552 isl_ctx *ctx;
553 struct isl_chamber_list *next;
555 ctx = isl_vertices_get_ctx(vertices);
556 vertices->c = isl_alloc_array(ctx, struct isl_chamber, n_chambers);
557 if (!vertices->c)
558 goto error;
559 vertices->n_chambers = n_chambers;
561 for (i = 0; list; list = next, i++) {
562 next = list->next;
563 vertices->c[i] = list->c;
564 free(list);
567 return vertices;
568 error:
569 isl_vertices_free(vertices);
570 free_chamber_list(list);
571 return NULL;
574 /* Can "tab" be intersected with "bset" without resulting in
575 * a lower-dimensional set.
577 static int can_intersect(struct isl_tab *tab, __isl_keep isl_basic_set *bset)
579 int i;
580 struct isl_tab_undo *snap;
582 if (isl_tab_extend_cons(tab, bset->n_ineq) < 0)
583 return -1;
585 snap = isl_tab_snap(tab);
587 for (i = 0; i < bset->n_ineq; ++i) {
588 if (isl_tab_ineq_type(tab, bset->ineq[i]) == isl_ineq_redundant)
589 continue;
590 if (isl_tab_add_ineq(tab, bset->ineq[i]) < 0)
591 return -1;
594 if (isl_tab_detect_implicit_equalities(tab) < 0)
595 return -1;
596 if (tab->n_dead) {
597 if (isl_tab_rollback(tab, snap) < 0)
598 return -1;
599 return 0;
602 return 1;
605 static int add_chamber(struct isl_chamber_list **list,
606 __isl_keep isl_vertices *vertices, struct isl_tab *tab, int *selection)
608 int n_frozen;
609 int i, j;
610 int n_vertices = 0;
611 struct isl_tab_undo *snap;
612 struct isl_chamber_list *c = NULL;
614 for (i = 0; i < vertices->n_vertices; ++i)
615 if (selection[i])
616 n_vertices++;
618 snap = isl_tab_snap(tab);
620 for (i = 0; i < tab->n_con && tab->con[i].frozen; ++i)
621 tab->con[i].frozen = 0;
622 n_frozen = i;
624 if (isl_tab_detect_redundant(tab) < 0)
625 return -1;
627 c = isl_calloc_type(tab->mat->ctx, struct isl_chamber_list);
628 if (!c)
629 goto error;
630 c->c.vertices = isl_alloc_array(tab->mat->ctx, int, n_vertices);
631 if (n_vertices && !c->c.vertices)
632 goto error;
633 c->c.dom = isl_basic_set_from_basic_map(isl_basic_map_copy(tab->bmap));
634 c->c.dom = isl_basic_set_set_rational(c->c.dom);
635 c->c.dom = isl_basic_set_cow(c->c.dom);
636 c->c.dom = isl_basic_set_update_from_tab(c->c.dom, tab);
637 c->c.dom = isl_basic_set_simplify(c->c.dom);
638 c->c.dom = isl_basic_set_finalize(c->c.dom);
639 if (!c->c.dom)
640 goto error;
642 c->c.n_vertices = n_vertices;
644 for (i = 0, j = 0; i < vertices->n_vertices; ++i)
645 if (selection[i]) {
646 c->c.vertices[j] = i;
647 j++;
650 c->next = *list;
651 *list = c;
653 for (i = 0; i < n_frozen; ++i)
654 tab->con[i].frozen = 1;
656 if (isl_tab_rollback(tab, snap) < 0)
657 return -1;
659 return 0;
660 error:
661 free_chamber_list(c);
662 return -1;
665 struct isl_facet_todo {
666 struct isl_tab *tab; /* A tableau representation of the facet */
667 isl_basic_set *bset; /* A normalized basic set representation */
668 isl_vec *constraint; /* Constraint pointing to the other side */
669 struct isl_facet_todo *next;
672 static void free_todo(struct isl_facet_todo *todo)
674 while (todo) {
675 struct isl_facet_todo *next = todo->next;
677 isl_tab_free(todo->tab);
678 isl_basic_set_free(todo->bset);
679 isl_vec_free(todo->constraint);
680 free(todo);
682 todo = next;
686 static struct isl_facet_todo *create_todo(struct isl_tab *tab, int con)
688 int i;
689 int n_frozen;
690 struct isl_tab_undo *snap;
691 struct isl_facet_todo *todo;
693 snap = isl_tab_snap(tab);
695 for (i = 0; i < tab->n_con && tab->con[i].frozen; ++i)
696 tab->con[i].frozen = 0;
697 n_frozen = i;
699 if (isl_tab_detect_redundant(tab) < 0)
700 return NULL;
702 todo = isl_calloc_type(tab->mat->ctx, struct isl_facet_todo);
703 if (!todo)
704 return NULL;
706 todo->constraint = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
707 if (!todo->constraint)
708 goto error;
709 isl_seq_neg(todo->constraint->el, tab->bmap->ineq[con], 1 + tab->n_var);
710 todo->bset = isl_basic_set_from_basic_map(isl_basic_map_copy(tab->bmap));
711 todo->bset = isl_basic_set_set_rational(todo->bset);
712 todo->bset = isl_basic_set_cow(todo->bset);
713 todo->bset = isl_basic_set_update_from_tab(todo->bset, tab);
714 todo->bset = isl_basic_set_simplify(todo->bset);
715 todo->bset = isl_basic_set_sort_constraints(todo->bset);
716 if (!todo->bset)
717 goto error;
718 ISL_F_SET(todo->bset, ISL_BASIC_SET_NORMALIZED);
719 todo->tab = isl_tab_dup(tab);
720 if (!todo->tab)
721 goto error;
723 for (i = 0; i < n_frozen; ++i)
724 tab->con[i].frozen = 1;
726 if (isl_tab_rollback(tab, snap) < 0)
727 goto error;
729 return todo;
730 error:
731 free_todo(todo);
732 return NULL;
735 /* Create todo items for all interior facets of the chamber represented
736 * by "tab" and collect them in "next".
738 static int init_todo(struct isl_facet_todo **next, struct isl_tab *tab)
740 int i;
741 struct isl_tab_undo *snap;
742 struct isl_facet_todo *todo;
744 snap = isl_tab_snap(tab);
746 for (i = 0; i < tab->n_con; ++i) {
747 if (tab->con[i].frozen)
748 continue;
749 if (tab->con[i].is_redundant)
750 continue;
752 if (isl_tab_select_facet(tab, i) < 0)
753 return -1;
755 todo = create_todo(tab, i);
756 if (!todo)
757 return -1;
759 todo->next = *next;
760 *next = todo;
762 if (isl_tab_rollback(tab, snap) < 0)
763 return -1;
766 return 0;
769 /* Does the linked list contain a todo item that is the opposite of "todo".
770 * If so, return 1 and remove the opposite todo item.
772 static int has_opposite(struct isl_facet_todo *todo,
773 struct isl_facet_todo **list)
775 for (; *list; list = &(*list)->next) {
776 int eq;
777 eq = isl_basic_set_plain_is_equal(todo->bset, (*list)->bset);
778 if (eq < 0)
779 return -1;
780 if (!eq)
781 continue;
782 todo = *list;
783 *list = todo->next;
784 todo->next = NULL;
785 free_todo(todo);
786 return 1;
789 return 0;
792 /* Create todo items for all interior facets of the chamber represented
793 * by "tab" and collect them in first->next, taking care to cancel
794 * opposite todo items.
796 static int update_todo(struct isl_facet_todo *first, struct isl_tab *tab)
798 int i;
799 struct isl_tab_undo *snap;
800 struct isl_facet_todo *todo;
802 snap = isl_tab_snap(tab);
804 for (i = 0; i < tab->n_con; ++i) {
805 int drop;
807 if (tab->con[i].frozen)
808 continue;
809 if (tab->con[i].is_redundant)
810 continue;
812 if (isl_tab_select_facet(tab, i) < 0)
813 return -1;
815 todo = create_todo(tab, i);
816 if (!todo)
817 return -1;
819 drop = has_opposite(todo, &first->next);
820 if (drop < 0)
821 return -1;
823 if (drop)
824 free_todo(todo);
825 else {
826 todo->next = first->next;
827 first->next = todo;
830 if (isl_tab_rollback(tab, snap) < 0)
831 return -1;
834 return 0;
837 /* Compute the chamber decomposition of the parametric polytope respresented
838 * by "bset" given the parametric vertices and their activity domains.
840 * We are only interested in full-dimensional chambers.
841 * Each of these chambers is the intersection of the activity domains of
842 * one or more vertices and the union of all chambers is equal to the
843 * projection of the entire parametric polytope onto the parameter space.
845 * We first create an initial chamber by intersecting as many activity
846 * domains as possible without ending up with an empty or lower-dimensional
847 * set. As a minor optimization, we only consider those activity domains
848 * that contain some arbitrary point.
850 * For each of interior facets of the chamber, we construct a todo item,
851 * containing the facet and a constraint containing the other side of the facet,
852 * for constructing the chamber on the other side.
853 * While their are any todo items left, we pick a todo item and
854 * create the required chamber by intersecting all activity domains
855 * that contain the facet and have a full-dimensional intersection with
856 * the other side of the facet. For each of the interior facets, we
857 * again create todo items, taking care to cancel opposite todo items.
859 static __isl_give isl_vertices *compute_chambers(__isl_take isl_basic_set *bset,
860 __isl_take isl_vertices *vertices)
862 int i;
863 isl_ctx *ctx;
864 isl_vec *sample = NULL;
865 struct isl_tab *tab = NULL;
866 struct isl_tab_undo *snap;
867 int *selection = NULL;
868 int n_chambers = 0;
869 struct isl_chamber_list *list = NULL;
870 struct isl_facet_todo *todo = NULL;
872 if (!bset || !vertices)
873 goto error;
875 ctx = isl_vertices_get_ctx(vertices);
876 selection = isl_alloc_array(ctx, int, vertices->n_vertices);
877 if (vertices->n_vertices && !selection)
878 goto error;
880 bset = isl_basic_set_params(bset);
882 tab = isl_tab_from_basic_set(bset, 1);
883 if (!tab)
884 goto error;
885 for (i = 0; i < bset->n_ineq; ++i)
886 if (isl_tab_freeze_constraint(tab, i) < 0)
887 goto error;
888 isl_basic_set_free(bset);
890 snap = isl_tab_snap(tab);
892 sample = isl_tab_get_sample_value(tab);
894 for (i = 0; i < vertices->n_vertices; ++i) {
895 selection[i] = isl_basic_set_contains(vertices->v[i].dom, sample);
896 if (selection[i] < 0)
897 goto error;
898 if (!selection[i])
899 continue;
900 selection[i] = can_intersect(tab, vertices->v[i].dom);
901 if (selection[i] < 0)
902 goto error;
905 if (isl_tab_detect_redundant(tab) < 0)
906 goto error;
908 if (add_chamber(&list, vertices, tab, selection) < 0)
909 goto error;
910 n_chambers++;
912 if (init_todo(&todo, tab) < 0)
913 goto error;
915 while (todo) {
916 struct isl_facet_todo *next;
918 if (isl_tab_rollback(tab, snap) < 0)
919 goto error;
921 if (isl_tab_add_ineq(tab, todo->constraint->el) < 0)
922 goto error;
923 if (isl_tab_freeze_constraint(tab, tab->n_con - 1) < 0)
924 goto error;
926 for (i = 0; i < vertices->n_vertices; ++i) {
927 selection[i] = bset_covers_tab(vertices->v[i].dom,
928 todo->tab);
929 if (selection[i] < 0)
930 goto error;
931 if (!selection[i])
932 continue;
933 selection[i] = can_intersect(tab, vertices->v[i].dom);
934 if (selection[i] < 0)
935 goto error;
938 if (isl_tab_detect_redundant(tab) < 0)
939 goto error;
941 if (add_chamber(&list, vertices, tab, selection) < 0)
942 goto error;
943 n_chambers++;
945 if (update_todo(todo, tab) < 0)
946 goto error;
948 next = todo->next;
949 todo->next = NULL;
950 free_todo(todo);
951 todo = next;
954 isl_vec_free(sample);
956 isl_tab_free(tab);
957 free(selection);
959 vertices = vertices_add_chambers(vertices, n_chambers, list);
961 for (i = 0; vertices && i < vertices->n_vertices; ++i) {
962 isl_basic_set_free(vertices->v[i].dom);
963 vertices->v[i].dom = NULL;
966 return vertices;
967 error:
968 free_chamber_list(list);
969 free_todo(todo);
970 isl_vec_free(sample);
971 isl_tab_free(tab);
972 free(selection);
973 if (!tab)
974 isl_basic_set_free(bset);
975 isl_vertices_free(vertices);
976 return NULL;
979 isl_ctx *isl_vertex_get_ctx(__isl_keep isl_vertex *vertex)
981 return vertex ? isl_vertices_get_ctx(vertex->vertices) : NULL;
984 int isl_vertex_get_id(__isl_keep isl_vertex *vertex)
986 return vertex ? vertex->id : -1;
989 __isl_give isl_basic_set *isl_vertex_get_domain(__isl_keep isl_vertex *vertex)
991 struct isl_vertex *v;
993 if (!vertex)
994 return NULL;
996 v = &vertex->vertices->v[vertex->id];
997 if (!v->dom) {
998 v->dom = isl_basic_set_copy(v->vertex);
999 v->dom = isl_basic_set_params(v->dom);
1002 return isl_basic_set_copy(v->dom);
1005 __isl_give isl_basic_set *isl_vertex_get_expr(__isl_keep isl_vertex *vertex)
1007 struct isl_vertex *v;
1009 if (!vertex)
1010 return NULL;
1012 v = &vertex->vertices->v[vertex->id];
1014 return isl_basic_set_copy(v->vertex);
1017 static __isl_give isl_vertex *isl_vertex_alloc(__isl_take isl_vertices *vertices,
1018 int id)
1020 isl_ctx *ctx;
1021 isl_vertex *vertex;
1023 if (!vertices)
1024 return NULL;
1026 ctx = isl_vertices_get_ctx(vertices);
1027 vertex = isl_alloc_type(ctx, isl_vertex);
1028 if (!vertex)
1029 goto error;
1031 vertex->vertices = vertices;
1032 vertex->id = id;
1034 return vertex;
1035 error:
1036 isl_vertices_free(vertices);
1037 return NULL;
1040 void isl_vertex_free(__isl_take isl_vertex *vertex)
1042 if (!vertex)
1043 return;
1044 isl_vertices_free(vertex->vertices);
1045 free(vertex);
1048 __isl_give isl_basic_set *isl_basic_set_set_integral(__isl_take isl_basic_set *bset)
1050 if (!bset)
1051 return NULL;
1053 if (!ISL_F_ISSET(bset, ISL_BASIC_MAP_RATIONAL))
1054 return bset;
1056 bset = isl_basic_set_cow(bset);
1057 if (!bset)
1058 return NULL;
1060 ISL_F_CLR(bset, ISL_BASIC_MAP_RATIONAL);
1062 return isl_basic_set_finalize(bset);
1065 isl_ctx *isl_cell_get_ctx(__isl_keep isl_cell *cell)
1067 return cell ? cell->dom->ctx : NULL;
1070 __isl_give isl_basic_set *isl_cell_get_domain(__isl_keep isl_cell *cell)
1072 return cell ? isl_basic_set_copy(cell->dom) : NULL;
1075 static __isl_give isl_cell *isl_cell_alloc(__isl_take isl_vertices *vertices,
1076 __isl_take isl_basic_set *dom, int id)
1078 int i;
1079 isl_cell *cell = NULL;
1081 if (!vertices || !dom)
1082 goto error;
1084 cell = isl_calloc_type(dom->ctx, isl_cell);
1085 if (!cell)
1086 goto error;
1088 cell->n_vertices = vertices->c[id].n_vertices;
1089 cell->ids = isl_alloc_array(dom->ctx, int, cell->n_vertices);
1090 if (cell->n_vertices && !cell->ids)
1091 goto error;
1092 for (i = 0; i < cell->n_vertices; ++i)
1093 cell->ids[i] = vertices->c[id].vertices[i];
1094 cell->vertices = vertices;
1095 cell->dom = dom;
1097 return cell;
1098 error:
1099 isl_cell_free(cell);
1100 isl_vertices_free(vertices);
1101 isl_basic_set_free(dom);
1102 return NULL;
1105 void isl_cell_free(__isl_take isl_cell *cell)
1107 if (!cell)
1108 return;
1110 isl_vertices_free(cell->vertices);
1111 free(cell->ids);
1112 isl_basic_set_free(cell->dom);
1113 free(cell);
1116 /* Create a tableau of the cone obtained by first homogenizing the given
1117 * polytope and then making all inequalities strict by setting the
1118 * constant term to -1.
1120 static struct isl_tab *tab_for_shifted_cone(__isl_keep isl_basic_set *bset)
1122 int i;
1123 isl_vec *c = NULL;
1124 struct isl_tab *tab;
1126 if (!bset)
1127 return NULL;
1128 tab = isl_tab_alloc(bset->ctx, bset->n_ineq + 1,
1129 1 + isl_basic_set_total_dim(bset), 0);
1130 if (!tab)
1131 return NULL;
1132 tab->rational = ISL_F_ISSET(bset, ISL_BASIC_SET_RATIONAL);
1133 if (ISL_F_ISSET(bset, ISL_BASIC_MAP_EMPTY)) {
1134 if (isl_tab_mark_empty(tab) < 0)
1135 goto error;
1136 return tab;
1139 c = isl_vec_alloc(bset->ctx, 1 + 1 + isl_basic_set_total_dim(bset));
1140 if (!c)
1141 goto error;
1143 isl_int_set_si(c->el[0], 0);
1144 for (i = 0; i < bset->n_eq; ++i) {
1145 isl_seq_cpy(c->el + 1, bset->eq[i], c->size - 1);
1146 if (isl_tab_add_eq(tab, c->el) < 0)
1147 goto error;
1150 isl_int_set_si(c->el[0], -1);
1151 for (i = 0; i < bset->n_ineq; ++i) {
1152 isl_seq_cpy(c->el + 1, bset->ineq[i], c->size - 1);
1153 if (isl_tab_add_ineq(tab, c->el) < 0)
1154 goto error;
1155 if (tab->empty) {
1156 isl_vec_free(c);
1157 return tab;
1161 isl_seq_clr(c->el + 1, c->size - 1);
1162 isl_int_set_si(c->el[1], 1);
1163 if (isl_tab_add_ineq(tab, c->el) < 0)
1164 goto error;
1166 isl_vec_free(c);
1167 return tab;
1168 error:
1169 isl_vec_free(c);
1170 isl_tab_free(tab);
1171 return NULL;
1174 /* Compute an interior point of "bset" by selecting an interior
1175 * point in homogeneous space and projecting the point back down.
1177 static __isl_give isl_vec *isl_basic_set_interior_point(
1178 __isl_keep isl_basic_set *bset)
1180 isl_vec *vec;
1181 struct isl_tab *tab;
1183 tab = tab_for_shifted_cone(bset);
1184 vec = isl_tab_get_sample_value(tab);
1185 isl_tab_free(tab);
1186 if (!vec)
1187 return NULL;
1189 isl_seq_cpy(vec->el, vec->el + 1, vec->size - 1);
1190 vec->size--;
1192 return vec;
1195 /* Call "fn" on all chambers of the parametric polytope with the shared
1196 * facets of neighboring chambers only appearing in one of the chambers.
1198 * We pick an interior point from one of the chambers and then make
1199 * all constraints that do not satisfy this point strict.
1201 int isl_vertices_foreach_disjoint_cell(__isl_keep isl_vertices *vertices,
1202 int (*fn)(__isl_take isl_cell *cell, void *user), void *user)
1204 int i, j;
1205 isl_vec *vec;
1206 isl_int v;
1207 isl_cell *cell;
1209 if (!vertices)
1210 return -1;
1212 if (vertices->n_chambers == 0)
1213 return 0;
1215 if (vertices->n_chambers == 1) {
1216 isl_basic_set *dom = isl_basic_set_copy(vertices->c[0].dom);
1217 dom = isl_basic_set_set_integral(dom);
1218 cell = isl_cell_alloc(isl_vertices_copy(vertices), dom, 0);
1219 if (!cell)
1220 return -1;
1221 return fn(cell, user);
1224 vec = isl_basic_set_interior_point(vertices->c[0].dom);
1225 if (!vec)
1226 return -1;
1228 isl_int_init(v);
1230 for (i = 0; i < vertices->n_chambers; ++i) {
1231 int r;
1232 isl_basic_set *dom = isl_basic_set_copy(vertices->c[i].dom);
1233 dom = isl_basic_set_cow(dom);
1234 if (!dom)
1235 goto error;
1236 for (j = 0; i && j < dom->n_ineq; ++j) {
1237 isl_seq_inner_product(vec->el, dom->ineq[j], vec->size,
1238 &v);
1239 if (!isl_int_is_neg(v))
1240 continue;
1241 isl_int_sub_ui(dom->ineq[j][0], dom->ineq[j][0], 1);
1243 dom = isl_basic_set_set_integral(dom);
1244 cell = isl_cell_alloc(isl_vertices_copy(vertices), dom, i);
1245 if (!cell)
1246 goto error;
1247 r = fn(cell, user);
1248 if (r < 0)
1249 goto error;
1252 isl_int_clear(v);
1253 isl_vec_free(vec);
1255 return 0;
1256 error:
1257 isl_int_clear(v);
1258 isl_vec_free(vec);
1259 return -1;
1262 int isl_vertices_foreach_cell(__isl_keep isl_vertices *vertices,
1263 int (*fn)(__isl_take isl_cell *cell, void *user), void *user)
1265 int i;
1266 isl_cell *cell;
1268 if (!vertices)
1269 return -1;
1271 if (vertices->n_chambers == 0)
1272 return 0;
1274 for (i = 0; i < vertices->n_chambers; ++i) {
1275 int r;
1276 isl_basic_set *dom = isl_basic_set_copy(vertices->c[i].dom);
1278 cell = isl_cell_alloc(isl_vertices_copy(vertices), dom, i);
1279 if (!cell)
1280 return -1;
1282 r = fn(cell, user);
1283 if (r < 0)
1284 return -1;
1287 return 0;
1290 int isl_vertices_foreach_vertex(__isl_keep isl_vertices *vertices,
1291 int (*fn)(__isl_take isl_vertex *vertex, void *user), void *user)
1293 int i;
1294 isl_vertex *vertex;
1296 if (!vertices)
1297 return -1;
1299 if (vertices->n_vertices == 0)
1300 return 0;
1302 for (i = 0; i < vertices->n_vertices; ++i) {
1303 int r;
1305 vertex = isl_vertex_alloc(isl_vertices_copy(vertices), i);
1306 if (!vertex)
1307 return -1;
1309 r = fn(vertex, user);
1310 if (r < 0)
1311 return -1;
1314 return 0;
1317 int isl_cell_foreach_vertex(__isl_keep isl_cell *cell,
1318 int (*fn)(__isl_take isl_vertex *vertex, void *user), void *user)
1320 int i;
1321 isl_vertex *vertex;
1323 if (!cell)
1324 return -1;
1326 if (cell->n_vertices == 0)
1327 return 0;
1329 for (i = 0; i < cell->n_vertices; ++i) {
1330 int r;
1332 vertex = isl_vertex_alloc(isl_vertices_copy(cell->vertices),
1333 cell->ids[i]);
1334 if (!vertex)
1335 return -1;
1337 r = fn(vertex, user);
1338 if (r < 0)
1339 return -1;
1342 return 0;
1345 isl_ctx *isl_vertices_get_ctx(__isl_keep isl_vertices *vertices)
1347 return vertices ? vertices->bset->ctx : NULL;
1350 int isl_vertices_get_n_vertices(__isl_keep isl_vertices *vertices)
1352 return vertices ? vertices->n_vertices : -1;
1355 __isl_give isl_vertices *isl_morph_vertices(__isl_take isl_morph *morph,
1356 __isl_take isl_vertices *vertices)
1358 int i;
1359 isl_morph *param_morph = NULL;
1361 if (!morph || !vertices)
1362 goto error;
1364 isl_assert(vertices->bset->ctx, vertices->ref == 1, goto error);
1366 param_morph = isl_morph_copy(morph);
1367 param_morph = isl_morph_dom_params(param_morph);
1368 param_morph = isl_morph_ran_params(param_morph);
1370 for (i = 0; i < vertices->n_vertices; ++i) {
1371 vertices->v[i].dom = isl_morph_basic_set(
1372 isl_morph_copy(param_morph), vertices->v[i].dom);
1373 vertices->v[i].vertex = isl_morph_basic_set(
1374 isl_morph_copy(morph), vertices->v[i].vertex);
1375 if (!vertices->v[i].vertex)
1376 goto error;
1379 for (i = 0; i < vertices->n_chambers; ++i) {
1380 vertices->c[i].dom = isl_morph_basic_set(
1381 isl_morph_copy(param_morph), vertices->c[i].dom);
1382 if (!vertices->c[i].dom)
1383 goto error;
1386 isl_morph_free(param_morph);
1387 isl_morph_free(morph);
1388 return vertices;
1389 error:
1390 isl_morph_free(param_morph);
1391 isl_morph_free(morph);
1392 isl_vertices_free(vertices);
1393 return NULL;
1396 /* Construct a simplex isl_cell spanned by the vertices with indices in
1397 * "simplex_ids" and "other_ids" and call "fn" on this isl_cell.
1399 static int call_on_simplex(__isl_keep isl_cell *cell,
1400 int *simplex_ids, int n_simplex, int *other_ids, int n_other,
1401 int (*fn)(__isl_take isl_cell *simplex, void *user), void *user)
1403 int i;
1404 isl_ctx *ctx;
1405 struct isl_cell *simplex;
1407 ctx = isl_cell_get_ctx(cell);
1409 simplex = isl_calloc_type(ctx, struct isl_cell);
1410 if (!simplex)
1411 return -1;
1412 simplex->vertices = isl_vertices_copy(cell->vertices);
1413 if (!simplex->vertices)
1414 goto error;
1415 simplex->dom = isl_basic_set_copy(cell->dom);
1416 if (!simplex->dom)
1417 goto error;
1418 simplex->n_vertices = n_simplex + n_other;
1419 simplex->ids = isl_alloc_array(ctx, int, simplex->n_vertices);
1420 if (!simplex->ids)
1421 goto error;
1423 for (i = 0; i < n_simplex; ++i)
1424 simplex->ids[i] = simplex_ids[i];
1425 for (i = 0; i < n_other; ++i)
1426 simplex->ids[n_simplex + i] = other_ids[i];
1428 return fn(simplex, user);
1429 error:
1430 isl_cell_free(simplex);
1431 return -1;
1434 /* Check whether the parametric vertex described by "vertex"
1435 * lies on the facet corresponding to constraint "facet" of "bset".
1436 * The isl_vec "v" is a temporary vector than can be used by this function.
1438 * We eliminate the variables from the facet constraint using the
1439 * equalities defining the vertex and check if the result is identical
1440 * to zero.
1442 * It would probably be better to keep track of the constraints defining
1443 * a vertex during the vertex construction so that we could simply look
1444 * it up here.
1446 static int vertex_on_facet(__isl_keep isl_basic_set *vertex,
1447 __isl_keep isl_basic_set *bset, int facet, __isl_keep isl_vec *v)
1449 int i;
1450 isl_int m;
1452 isl_seq_cpy(v->el, bset->ineq[facet], v->size);
1454 isl_int_init(m);
1455 for (i = 0; i < vertex->n_eq; ++i) {
1456 int k = isl_seq_last_non_zero(vertex->eq[i], v->size);
1457 isl_seq_elim(v->el, vertex->eq[i], k, v->size, &m);
1459 isl_int_clear(m);
1461 return isl_seq_first_non_zero(v->el, v->size) == -1;
1464 /* Triangulate the polytope spanned by the vertices with ids
1465 * in "simplex_ids" and "other_ids" and call "fn" on each of
1466 * the resulting simplices.
1467 * If the input polytope is already a simplex, we simply call "fn".
1468 * Otherwise, we pick a point from "other_ids" and add it to "simplex_ids".
1469 * Then we consider each facet of "bset" that does not contain the point
1470 * we just picked, but does contain some of the other points in "other_ids"
1471 * and call ourselves recursively on the polytope spanned by the new
1472 * "simplex_ids" and those points in "other_ids" that lie on the facet.
1474 static int triangulate(__isl_keep isl_cell *cell, __isl_keep isl_vec *v,
1475 int *simplex_ids, int n_simplex, int *other_ids, int n_other,
1476 int (*fn)(__isl_take isl_cell *simplex, void *user), void *user)
1478 int i, j, k;
1479 int d, nparam;
1480 int *ids;
1481 isl_ctx *ctx;
1482 isl_basic_set *vertex;
1483 isl_basic_set *bset;
1485 ctx = isl_cell_get_ctx(cell);
1486 d = isl_basic_set_dim(cell->vertices->bset, isl_dim_set);
1487 nparam = isl_basic_set_dim(cell->vertices->bset, isl_dim_param);
1489 if (n_simplex + n_other == d + 1)
1490 return call_on_simplex(cell, simplex_ids, n_simplex,
1491 other_ids, n_other, fn, user);
1493 simplex_ids[n_simplex] = other_ids[0];
1494 vertex = cell->vertices->v[other_ids[0]].vertex;
1495 bset = cell->vertices->bset;
1497 ids = isl_alloc_array(ctx, int, n_other - 1);
1498 for (i = 0; i < bset->n_ineq; ++i) {
1499 if (isl_seq_first_non_zero(bset->ineq[i] + 1 + nparam, d) == -1)
1500 continue;
1501 if (vertex_on_facet(vertex, bset, i, v))
1502 continue;
1504 for (j = 1, k = 0; j < n_other; ++j) {
1505 isl_basic_set *ov;
1506 ov = cell->vertices->v[other_ids[j]].vertex;
1507 if (vertex_on_facet(ov, bset, i, v))
1508 ids[k++] = other_ids[j];
1510 if (k == 0)
1511 continue;
1513 if (triangulate(cell, v, simplex_ids, n_simplex + 1,
1514 ids, k, fn, user) < 0)
1515 goto error;
1517 free(ids);
1519 return 0;
1520 error:
1521 free(ids);
1522 return -1;
1525 /* Triangulate the given cell and call "fn" on each of the resulting
1526 * simplices.
1528 int isl_cell_foreach_simplex(__isl_take isl_cell *cell,
1529 int (*fn)(__isl_take isl_cell *simplex, void *user), void *user)
1531 int d, total;
1532 int r;
1533 isl_ctx *ctx;
1534 isl_vec *v = NULL;
1535 int *simplex_ids = NULL;
1537 if (!cell)
1538 return -1;
1540 d = isl_basic_set_dim(cell->vertices->bset, isl_dim_set);
1541 total = isl_basic_set_total_dim(cell->vertices->bset);
1543 if (cell->n_vertices == d + 1)
1544 return fn(cell, user);
1546 ctx = isl_cell_get_ctx(cell);
1547 simplex_ids = isl_alloc_array(ctx, int, d + 1);
1548 if (!simplex_ids)
1549 goto error;
1551 v = isl_vec_alloc(ctx, 1 + total);
1552 if (!v)
1553 goto error;
1555 r = triangulate(cell, v, simplex_ids, 0,
1556 cell->ids, cell->n_vertices, fn, user);
1558 isl_vec_free(v);
1559 free(simplex_ids);
1561 isl_cell_free(cell);
1563 return r;
1564 error:
1565 free(simplex_ids);
1566 isl_vec_free(v);
1567 isl_cell_free(cell);
1568 return -1;