isl_basic_set_compute_vertices: store input basic set in result
[platform/upstream/isl.git] / isl_vertices.c
1 /*
2  * Copyright 2010      INRIA Saclay
3  *
4  * Use of this software is governed by the GNU LGPLv2.1 license
5  *
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  */
10
11 #include <isl_set.h>
12 #include <isl_seq.h>
13 #include <isl_tab.h>
14 #include <isl_map_private.h>
15 #include <isl_dim_private.h>
16 #include <isl_morph.h>
17 #include <isl_vertices_private.h>
18 #include <isl_mat_private.h>
19
20 #define SELECTED        1
21 #define DESELECTED      -1
22 #define UNSELECTED      0
23
24 static __isl_give isl_vertices *compute_chambers(__isl_take isl_basic_set *bset,
25         __isl_take isl_vertices *vertices);
26
27 __isl_give isl_vertices *isl_vertices_copy(__isl_keep isl_vertices *vertices)
28 {
29         if (!vertices)
30                 return NULL;
31
32         vertices->ref++;
33         return vertices;
34 }
35
36 void isl_vertices_free(__isl_take isl_vertices *vertices)
37 {
38         int i;
39
40         if (!vertices)
41                 return;
42
43         if (--vertices->ref > 0)
44                 return;
45
46         for (i = 0; i < vertices->n_vertices; ++i) {
47                 isl_basic_set_free(vertices->v[i].vertex);
48                 isl_basic_set_free(vertices->v[i].dom);
49         }
50         free(vertices->v);
51
52         for (i = 0; i < vertices->n_chambers; ++i) {
53                 free(vertices->c[i].vertices);
54                 isl_basic_set_free(vertices->c[i].dom);
55         }
56         free(vertices->c);
57
58         isl_basic_set_free(vertices->bset);
59         free(vertices);
60 }
61
62 struct isl_vertex_list {
63         struct isl_vertex v;
64         struct isl_vertex_list *next;
65 };
66
67 static void free_vertex_list(struct isl_vertex_list *list)
68 {
69         struct isl_vertex_list *next;
70
71         for (; list; list = next) {
72                 next = list->next;
73                 isl_basic_set_free(list->v.vertex);
74                 isl_basic_set_free(list->v.dom);
75                 free(list);
76         }
77 }
78
79 static __isl_give isl_vertices *vertices_from_list(__isl_keep isl_basic_set *bset,
80         int n_vertices, struct isl_vertex_list *list)
81 {
82         int i;
83         struct isl_vertex_list *next;
84         isl_vertices *vertices;
85
86         vertices = isl_calloc_type(bset->ctx, isl_vertices);
87         if (!vertices)
88                 goto error;
89         vertices->ref = 1;
90         vertices->bset = isl_basic_set_copy(bset);
91         vertices->v = isl_alloc_array(bset->ctx, struct isl_vertex, n_vertices);
92         if (!vertices->v)
93                 goto error;
94         vertices->n_vertices = n_vertices;
95
96         for (i = 0; list; list = next, i++) {
97                 next = list->next;
98                 vertices->v[i] = list->v;
99                 free(list);
100         }
101
102         return vertices;
103 error:
104         free(vertices);
105         free_vertex_list(list);
106         return NULL;
107 }
108
109 /* Prepend a vertex to the linked list "list" based on the equalities in "tab".
110  */
111 static int add_vertex(struct isl_vertex_list **list,
112         __isl_keep isl_basic_set *bset, struct isl_tab *tab)
113 {
114         unsigned nvar;
115         unsigned nparam;
116         struct isl_vertex_list *v = NULL;
117
118         if (isl_tab_detect_implicit_equalities(tab) < 0)
119                 return -1;
120
121         nvar = isl_basic_set_dim(bset, isl_dim_set);
122         nparam = isl_basic_set_dim(bset, isl_dim_param);
123
124         v = isl_calloc_type(tab->mat->ctx, struct isl_vertex_list);
125         if (!v)
126                 goto error;
127
128         v->v.vertex = isl_basic_set_copy(bset);
129         v->v.vertex = isl_basic_set_cow(v->v.vertex);
130         v->v.vertex = isl_basic_set_update_from_tab(v->v.vertex, tab);
131         v->v.vertex = isl_basic_set_simplify(v->v.vertex);
132         v->v.vertex = isl_basic_set_finalize(v->v.vertex);
133         if (!v->v.vertex)
134                 goto error;
135         isl_assert(bset->ctx, v->v.vertex->n_eq >= nvar, goto error);
136         v->v.dom = isl_basic_set_copy(v->v.vertex);
137         v->v.dom = isl_basic_set_project_out(v->v.dom, isl_dim_set, 0, nvar);
138         if (!v->v.dom)
139                 goto error;
140
141         v->next = *list;
142         *list = v;
143
144         return 0;
145 error:
146         free_vertex_list(v);
147         return -1;
148 }
149
150 /* Compute the parametric vertices and the chamber decomposition
151  * of an empty parametric polytope.
152  */
153 static __isl_give isl_vertices *vertices_empty(__isl_keep isl_basic_set *bset)
154 {
155         isl_vertices *vertices;
156         unsigned nparam;
157
158         if (!bset)
159                 return NULL;
160
161         nparam = isl_basic_set_dim(bset, isl_dim_param);
162
163         vertices = isl_calloc_type(bset->ctx, isl_vertices);
164         if (!vertices)
165                 return NULL;
166         vertices->bset = isl_basic_set_copy(bset);
167         vertices->ref = 1;
168
169         vertices->n_vertices = 0;
170         vertices->n_chambers = 0;
171
172         return vertices;
173 }
174
175 /* Compute the parametric vertices and the chamber decomposition
176  * of the parametric polytope defined using the same constraints
177  * as "bset" in the 0D case.
178  * There is exactly one 0D vertex and a single chamber containing
179  * the vertex.
180  */
181 static __isl_give isl_vertices *vertices_0D(__isl_keep isl_basic_set *bset)
182 {
183         isl_vertices *vertices;
184         unsigned nparam;
185
186         if (!bset)
187                 return NULL;
188
189         nparam = isl_basic_set_dim(bset, isl_dim_param);
190
191         vertices = isl_calloc_type(bset->ctx, isl_vertices);
192         if (!vertices)
193                 return NULL;
194         vertices->ref = 1;
195         vertices->bset = isl_basic_set_copy(bset);
196
197         vertices->v = isl_calloc_array(bset->ctx, struct isl_vertex, 1);
198         if (!vertices->v)
199                 goto error;
200         vertices->n_vertices = 1;
201         vertices->v[0].vertex = isl_basic_set_copy(bset);
202         if (!vertices->v[0].vertex)
203                 goto error;
204
205         vertices->c = isl_calloc_array(bset->ctx, struct isl_chamber, 1);
206         if (!vertices->c)
207                 goto error;
208         vertices->n_chambers = 1;
209         vertices->c[0].n_vertices = 1;
210         vertices->c[0].vertices = isl_calloc_array(bset->ctx, int, 1);
211         if (!vertices->c[0].vertices)
212                 goto error;
213         vertices->c[0].dom = isl_basic_set_copy(bset);
214         if (!vertices->c[0].dom)
215                 goto error;
216
217         return vertices;
218 error:
219         isl_vertices_free(vertices);
220         return NULL;
221 }
222
223 static int isl_mat_rank(__isl_keep isl_mat *mat)
224 {
225         int row, col;
226         isl_mat *H;
227
228         H = isl_mat_left_hermite(isl_mat_copy(mat), 0, NULL, NULL);
229         if (!H)
230                 return -1;
231
232         for (col = 0; col < H->n_col; ++col) {
233                 for (row = 0; row < H->n_row; ++row)
234                         if (!isl_int_is_zero(H->row[row][col]))
235                                 break;
236                 if (row == H->n_row)
237                         break;
238         }
239
240         isl_mat_free(H);
241
242         return col;
243 }
244
245 /* Is the row pointed to by "f" linearly independent of the "n" first
246  * rows in "facets"?
247  */
248 static int is_independent(__isl_keep isl_mat *facets, int n, isl_int *f)
249 {
250         int rank;
251
252         if (isl_seq_first_non_zero(f, facets->n_col) < 0)
253                 return 0;
254
255         isl_seq_cpy(facets->row[n], f, facets->n_col);
256         facets->n_row = n + 1;
257         rank = isl_mat_rank(facets);
258         if (rank < 0)
259                 return -1;
260
261         return rank == n + 1;
262 }
263
264 /* Check whether we can select constraint "level", given the current selection
265  * reflected by facets in "tab", the rows of "facets" and the earlier
266  * "selected" elements of "selection".
267  *
268  * If the constraint is (strictly) redundant in the tableau, selecting it would
269  * result in an empty tableau, so it can't be selected.
270  * If the set variable part of the constraint is not linearly indepedent
271  * of the set variable parts of the already selected constraints,
272  * the constraint cannot be selected.
273  * If selecting the constraint results in an empty tableau, the constraint
274  * cannot be selected.
275  * Finally, if selecting the constraint results in some explicitly
276  * deselected constraints turning into equalities, then the corresponding
277  * vertices have already been generated, so the constraint cannot be selected.
278  */
279 static int can_select(__isl_keep isl_basic_set *bset, int level,
280         struct isl_tab *tab, __isl_keep isl_mat *facets, int selected,
281         int *selection)
282 {
283         int i;
284         int indep;
285         unsigned ovar;
286         struct isl_tab_undo *snap;
287
288         if (isl_tab_is_redundant(tab, level))
289                 return 0;
290
291         ovar = isl_dim_offset(bset->dim, isl_dim_set);
292
293         indep = is_independent(facets, selected, bset->ineq[level] + 1 + ovar);
294         if (indep < 0)
295                 return -1;
296         if (!indep)
297                 return 0;
298
299         snap = isl_tab_snap(tab);
300         if (isl_tab_select_facet(tab, level) < 0)
301                 return -1;
302
303         if (tab->empty) {
304                 if (isl_tab_rollback(tab, snap) < 0)
305                         return -1;
306                 return 0;
307         }
308
309         for (i = 0; i < level; ++i) {
310                 int sgn;
311
312                 if (selection[i] != DESELECTED)
313                         continue;
314
315                 if (isl_tab_is_equality(tab, i))
316                         sgn = 0;
317                 else if (isl_tab_is_redundant(tab, i))
318                         sgn = 1;
319                 else
320                         sgn = isl_tab_sign_of_max(tab, i);
321                 if (sgn < -1)
322                         return -1;
323                 if (sgn <= 0) {
324                         if (isl_tab_rollback(tab, snap) < 0)
325                                 return -1;
326                         return 0;
327                 }
328         }
329
330         return 1;
331 }
332
333 /* Compute the parametric vertices and the chamber decomposition
334  * of a parametric polytope that is not full-dimensional.
335  *
336  * Simply map the parametric polytope to a lower dimensional space
337  * and map the resulting vertices back.
338  */
339 static __isl_give isl_vertices *lower_dim_vertices(
340         __isl_keep isl_basic_set *bset)
341 {
342         isl_morph *morph;
343         isl_vertices *vertices;
344
345         bset = isl_basic_set_copy(bset);
346         morph = isl_basic_set_full_compression(bset);
347         bset = isl_morph_basic_set(isl_morph_copy(morph), bset);
348
349         vertices = isl_basic_set_compute_vertices(bset);
350         isl_basic_set_free(bset);
351
352         morph = isl_morph_inverse(morph);
353
354         vertices = isl_morph_vertices(morph, vertices);
355
356         return vertices;
357 }
358
359 /* Compute the parametric vertices and the chamber decomposition
360  * of the parametric polytope defined using the same constraints
361  * as "bset".  "bset" is assumed to have no existentially quantified
362  * variables.
363  *
364  * The vertices themselves are computed in a fairly simplistic way.
365  * We simply run through all combinations of d constraints,
366  * with d the number of set variables, and check if those d constraints
367  * define a vertex.  To avoid the generation of duplicate vertices,
368  * which we may happen if a vertex is defined by more that d constraints,
369  * we make sure we only generate the vertex for the d constraints with
370  * smallest index.
371  *
372  * We set up a tableau and keep track of which facets have been
373  * selected.  The tableau is marked strict_redundant so that we can be
374  * sure that any constraint that is marked redundant (and that is not
375  * also marked zero) is not an equality.
376  * If a constraint is marked DESELECTED, it means the constraint was
377  * SELECTED before (in combination with the same selection of earlier
378  * constraints).  If such a deselected constraint turns out to be an
379  * equality, then any vertex that may still be found with the current
380  * selection has already been generated when the constraint was selected.
381  * A constraint is marked UNSELECTED when there is no way selecting
382  * the constraint could lead to a vertex (in combination with the current
383  * selection of earlier constraints).
384  *
385  * The set variable coefficients of the selected constraints are stored
386  * in the facets matrix.
387  */
388 __isl_give isl_vertices *isl_basic_set_compute_vertices(
389         __isl_keep isl_basic_set *bset)
390 {
391         struct isl_tab *tab;
392         int level;
393         int init;
394         unsigned nvar;
395         int *selection;
396         int selected;
397         struct isl_tab_undo **snap;
398         isl_mat *facets;
399         struct isl_vertex_list *list = NULL;
400         int n_vertices = 0;
401         isl_vertices *vertices;
402
403         if (!bset)
404                 return NULL;
405
406         if (isl_basic_set_fast_is_empty(bset))
407                 return vertices_empty(bset);
408
409         if (bset->n_eq != 0)
410                 return lower_dim_vertices(bset);
411
412         isl_assert(bset->ctx, isl_basic_set_dim(bset, isl_dim_div) == 0,
413                 return NULL);
414
415         if (isl_basic_set_dim(bset, isl_dim_set) == 0)
416                 return vertices_0D(bset);
417
418         nvar = isl_basic_set_dim(bset, isl_dim_set);
419
420         bset = isl_basic_set_copy(bset);
421         bset = isl_basic_set_set_rational(bset);
422         if (!bset)
423                 return NULL;
424
425         tab = isl_tab_from_basic_set(bset);
426         if (!tab)
427                 goto error;
428         tab->strict_redundant = 1;
429
430         if (tab->empty) {
431                 vertices = vertices_empty(bset);
432                 isl_basic_set_free(bset);
433                 isl_tab_free(tab);
434                 return vertices;
435         }
436
437         selection = isl_alloc_array(bset->ctx, int, bset->n_ineq);
438         snap = isl_alloc_array(bset->ctx, struct isl_tab_undo *, bset->n_ineq);
439         facets = isl_mat_alloc(bset->ctx, nvar, nvar);
440         if (!selection || !snap || !facets)
441                 goto error;
442
443         level = 0;
444         init = 1;
445         selected = 0;
446
447         while (level >= 0) {
448                 if (level >= bset->n_ineq ||
449                     (!init && selection[level] != SELECTED)) {
450                         --level;
451                         init = 0;
452                         continue;
453                 }
454                 if (init) {
455                         int ok;
456                         snap[level] = isl_tab_snap(tab);
457                         ok = can_select(bset, level, tab, facets, selected,
458                                         selection);
459                         if (ok < 0)
460                                 goto error;
461                         if (ok) {
462                                 selection[level] = SELECTED;
463                                 selected++;
464                         } else
465                                 selection[level] = UNSELECTED;
466                 } else {
467                         selection[level] = DESELECTED;
468                         selected--;
469                         if (isl_tab_rollback(tab, snap[level]) < 0)
470                                 goto error;
471                 }
472                 if (selected == nvar) {
473                         if (tab->n_dead == nvar) {
474                                 if (add_vertex(&list, bset, tab) < 0)
475                                         goto error;
476                                 n_vertices++;
477                         }
478                         init = 0;
479                         continue;
480                 }
481                 ++level;
482                 init = 1;
483         }
484
485         isl_mat_free(facets);
486         free(selection);
487         free(snap);
488
489         isl_tab_free(tab);
490
491         vertices = vertices_from_list(bset, n_vertices, list);
492
493         vertices = compute_chambers(bset, vertices);
494
495         return vertices;
496 error:
497         isl_mat_free(facets);
498         free(selection);
499         free(snap);
500         isl_tab_free(tab);
501         isl_basic_set_free(bset);
502         return NULL;
503 }
504
505 struct isl_chamber_list {
506         struct isl_chamber c;
507         struct isl_chamber_list *next;
508 };
509
510 static void free_chamber_list(struct isl_chamber_list *list)
511 {
512         struct isl_chamber_list *next;
513
514         for (; list; list = next) {
515                 next = list->next;
516                 isl_basic_set_free(list->c.dom);
517                 free(list->c.vertices);
518                 free(list);
519         }
520 }
521
522 /* Check whether the basic set "bset" is a superset of the basic set described
523  * by "tab", i.e., check whether all constraints of "bset" are redundant.
524  */
525 static int bset_covers_tab(__isl_keep isl_basic_set *bset, struct isl_tab *tab)
526 {
527         int i;
528
529         if (!bset || !tab)
530                 return -1;
531
532         for (i = 0; i < bset->n_ineq; ++i) {
533                 enum isl_ineq_type type = isl_tab_ineq_type(tab, bset->ineq[i]);
534                 switch (type) {
535                 case isl_ineq_error:            return -1;
536                 case isl_ineq_redundant:        continue;
537                 default:                        return 0;
538                 }
539         }
540
541         return 1;
542 }
543
544 static __isl_give isl_vertices *vertices_add_chambers(
545         __isl_take isl_vertices *vertices, int n_chambers,
546         struct isl_chamber_list *list)
547 {
548         int i;
549         struct isl_chamber_list *next;
550
551         vertices->c = isl_alloc_array(vertices->ctx, struct isl_chamber, n_chambers);
552         if (!vertices->c)
553                 goto error;
554         vertices->n_chambers = n_chambers;
555
556         for (i = 0; list; list = next, i++) {
557                 next = list->next;
558                 vertices->c[i] = list->c;
559                 free(list);
560         }
561
562         return vertices;
563 error:
564         isl_vertices_free(vertices);
565         free_chamber_list(list);
566         return NULL;
567 }
568
569 /* Can "tab" be intersected with "bset" without resulting in
570  * a lower-dimensional set.
571  */
572 static int can_intersect(struct isl_tab *tab, __isl_keep isl_basic_set *bset)
573 {
574         int i;
575         struct isl_tab_undo *snap;
576
577         if (isl_tab_extend_cons(tab, bset->n_ineq) < 0)
578                 return -1;
579
580         snap = isl_tab_snap(tab);
581
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 -1;
587         }
588
589         if (isl_tab_detect_implicit_equalities(tab) < 0)
590                 return -1;
591         if (tab->n_dead) {
592                 if (isl_tab_rollback(tab, snap) < 0)
593                         return -1;
594                 return 0;
595         }
596
597         return 1;
598 }
599
600 static int add_chamber(struct isl_chamber_list **list,
601         __isl_keep isl_vertices *vertices, struct isl_tab *tab, int *selection)
602 {
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;
608
609         for (i = 0; i < vertices->n_vertices; ++i)
610                 if (selection[i])
611                         n_vertices++;
612
613         snap = isl_tab_snap(tab);
614
615         for (i = 0; i < tab->n_con && tab->con[i].frozen; ++i)
616                 tab->con[i].frozen = 0;
617         n_frozen = i;
618
619         if (isl_tab_detect_redundant(tab) < 0)
620                 return -1;
621
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 (!c->c.vertices)
627                 goto error;
628         c->c.dom = isl_basic_set_from_basic_map(isl_basic_map_copy(tab->bmap));
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;
636
637         c->c.n_vertices = n_vertices;
638
639         for (i = 0, j = 0; i < vertices->n_vertices; ++i)
640                 if (selection[i]) {
641                         c->c.vertices[j] = i;
642                         j++;
643                 }
644
645         c->next = *list;
646         *list = c;
647
648         for (i = 0; i < n_frozen; ++i)
649                 tab->con[i].frozen = 1;
650
651         if (isl_tab_rollback(tab, snap) < 0)
652                 return -1;
653
654         return 0;
655 error:
656         free_chamber_list(c);
657         return -1;
658 }
659
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;
665 };
666
667 static void free_todo(struct isl_facet_todo *todo)
668 {
669         while (todo) {
670                 struct isl_facet_todo *next = todo->next;
671
672                 isl_tab_free(todo->tab);
673                 isl_basic_set_free(todo->bset);
674                 isl_vec_free(todo->constraint);
675                 free(todo);
676
677                 todo = next;
678         }
679 }
680
681 static struct isl_facet_todo *create_todo(struct isl_tab *tab, int con)
682 {
683         int i;
684         int n_frozen;
685         struct isl_tab_undo *snap;
686         struct isl_facet_todo *todo;
687
688         snap = isl_tab_snap(tab);
689
690         for (i = 0; i < tab->n_con && tab->con[i].frozen; ++i)
691                 tab->con[i].frozen = 0;
692         n_frozen = i;
693
694         if (isl_tab_detect_redundant(tab) < 0)
695                 return NULL;
696
697         todo = isl_calloc_type(tab->mat->ctx, struct isl_facet_todo);
698         if (!todo)
699                 return NULL;
700
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_from_basic_map(isl_basic_map_copy(tab->bmap));
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_NORMALIZED);
714         todo->tab = isl_tab_dup(tab);
715         if (!todo->tab)
716                 goto error;
717
718         for (i = 0; i < n_frozen; ++i)
719                 tab->con[i].frozen = 1;
720
721         if (isl_tab_rollback(tab, snap) < 0)
722                 goto error;
723
724         return todo;
725 error:
726         free_todo(todo);
727         return NULL;
728 }
729
730 /* Create todo items for all interior facets of the chamber represented
731  * by "tab" and collect them in "next".
732  */
733 static int init_todo(struct isl_facet_todo **next, struct isl_tab *tab)
734 {
735         int i;
736         struct isl_tab_undo *snap;
737         struct isl_facet_todo *todo;
738
739         snap = isl_tab_snap(tab);
740
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;
746
747                 if (isl_tab_select_facet(tab, i) < 0)
748                         return -1;
749
750                 todo = create_todo(tab, i);
751                 if (!todo)
752                         return -1;
753
754                 todo->next = *next;
755                 *next = todo;
756
757                 if (isl_tab_rollback(tab, snap) < 0)
758                         return -1;
759         }
760
761         return 0;
762 }
763
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.
766  */
767 static int has_opposite(struct isl_facet_todo *todo,
768         struct isl_facet_todo **list)
769 {
770         for (; *list; list = &(*list)->next) {
771                 int eq;
772                 eq = isl_basic_set_fast_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;
782         }
783
784         return 0;
785 }
786
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.
790  */
791 static int update_todo(struct isl_facet_todo *first, struct isl_tab *tab)
792 {
793         int i;
794         struct isl_tab_undo *snap;
795         struct isl_facet_todo *todo;
796
797         snap = isl_tab_snap(tab);
798
799         for (i = 0; i < tab->n_con; ++i) {
800                 int drop;
801
802                 if (tab->con[i].frozen)
803                         continue;
804                 if (tab->con[i].is_redundant)
805                         continue;
806
807                 if (isl_tab_select_facet(tab, i) < 0)
808                         return -1;
809
810                 todo = create_todo(tab, i);
811                 if (!todo)
812                         return -1;
813
814                 drop = has_opposite(todo, &first->next);
815                 if (drop < 0)
816                         return -1;
817
818                 if (drop)
819                         free_todo(todo);
820                 else {
821                         todo->next = first->next;
822                         first->next = todo;
823                 }
824
825                 if (isl_tab_rollback(tab, snap) < 0)
826                         return -1;
827         }
828
829         return 0;
830 }
831
832 /* Compute the chamber decomposition of the parametric polytope respresented
833  * by "bset" given the parametric vertices and their activity domains.
834  *
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.
839  *
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.
844  *
845  * For each of 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.
853  */
854 static __isl_give isl_vertices *compute_chambers(__isl_take isl_basic_set *bset,
855         __isl_take isl_vertices *vertices)
856 {
857         int i;
858         isl_vec *sample = NULL;
859         struct isl_tab *tab = NULL;
860         struct isl_tab_undo *snap;
861         unsigned nvar;
862         int *selection = NULL;
863         int n_chambers = 0;
864         struct isl_chamber_list *list = NULL;
865         struct isl_facet_todo *todo = NULL;
866
867         if (!bset || !vertices)
868                 goto error;
869
870         selection = isl_alloc_array(vertices->ctx, int, vertices->n_vertices);
871         if (!selection)
872                 goto error;
873
874         nvar = isl_basic_set_dim(bset, isl_dim_set);
875         bset = isl_basic_set_project_out(bset, isl_dim_set, 0, nvar);
876
877         tab = isl_tab_from_basic_set(bset);
878         for (i = 0; i < bset->n_ineq; ++i)
879                 if (isl_tab_freeze_constraint(tab, i) < 0)
880                         goto error;
881         if (isl_tab_track_bset(tab, bset) < 0)
882                 goto error;
883
884         snap = isl_tab_snap(tab);
885
886         sample = isl_tab_get_sample_value(tab);
887
888         for (i = 0; i < vertices->n_vertices; ++i) {
889                 selection[i] = isl_basic_set_contains(vertices->v[i].dom, sample);
890                 if (selection[i] < 0)
891                         goto error;
892                 if (!selection[i])
893                         continue;
894                 selection[i] = can_intersect(tab, vertices->v[i].dom);
895                 if (selection[i] < 0)
896                         goto error;
897         }
898
899         if (isl_tab_detect_redundant(tab) < 0)
900                 goto error;
901
902         if (add_chamber(&list, vertices, tab, selection) < 0)
903                 goto error;
904         n_chambers++;
905
906         if (init_todo(&todo, tab) < 0)
907                 goto error;
908
909         while (todo) {
910                 struct isl_facet_todo *next;
911
912                 if (isl_tab_rollback(tab, snap) < 0)
913                         goto error;
914
915                 if (isl_tab_add_ineq(tab, todo->constraint->el) < 0)
916                         goto error;
917                 if (isl_tab_freeze_constraint(tab, tab->n_con - 1) < 0)
918                         goto error;
919
920                 for (i = 0; i < vertices->n_vertices; ++i) {
921                         selection[i] = bset_covers_tab(vertices->v[i].dom,
922                                                         todo->tab);
923                         if (selection[i] < 0)
924                                 goto error;
925                         if (!selection[i])
926                                 continue;
927                         selection[i] = can_intersect(tab, vertices->v[i].dom);
928                         if (selection[i] < 0)
929                                 goto error;
930                 }
931
932                 if (isl_tab_detect_redundant(tab) < 0)
933                         goto error;
934
935                 if (add_chamber(&list, vertices, tab, selection) < 0)
936                         goto error;
937                 n_chambers++;
938
939                 if (update_todo(todo, tab) < 0)
940                         goto error;
941
942                 next = todo->next;
943                 todo->next = NULL;
944                 free_todo(todo);
945                 todo = next;
946         }
947
948         isl_vec_free(sample);
949
950         isl_tab_free(tab);
951         free(selection);
952
953         vertices = vertices_add_chambers(vertices, n_chambers, list);
954
955         for (i = 0; vertices && i < vertices->n_vertices; ++i) {
956                 isl_basic_set_free(vertices->v[i].dom);
957                 vertices->v[i].dom = NULL;
958         }
959
960         return vertices;
961 error:
962         free_chamber_list(list);
963         free_todo(todo);
964         isl_vec_free(sample);
965         isl_tab_free(tab);
966         free(selection);
967         if (!tab)
968                 isl_basic_set_free(bset);
969         isl_vertices_free(vertices);
970         return NULL;
971 }
972
973 isl_ctx *isl_vertex_get_ctx(__isl_keep isl_vertex *vertex)
974 {
975         return vertex ? isl_vertices_get_ctx(vertex->vertices) : NULL;
976 }
977
978 int isl_vertex_get_id(__isl_keep isl_vertex *vertex)
979 {
980         return vertex ? vertex->id : -1;
981 }
982
983 __isl_give isl_basic_set *isl_vertex_get_domain(__isl_keep isl_vertex *vertex)
984 {
985         struct isl_vertex *v;
986
987         if (!vertex)
988                 return NULL;
989
990         v = &vertex->vertices->v[vertex->id];
991         if (!v->dom) {
992                 unsigned nvar;
993                 nvar = isl_basic_set_dim(v->vertex, isl_dim_set);
994                 v->dom = isl_basic_set_copy(v->vertex);
995                 v->dom = isl_basic_set_project_out(v->dom, isl_dim_set, 0, nvar);
996         }
997
998         return isl_basic_set_copy(v->dom);
999 }
1000
1001 __isl_give isl_basic_set *isl_vertex_get_expr(__isl_keep isl_vertex *vertex)
1002 {
1003         struct isl_vertex *v;
1004
1005         if (!vertex)
1006                 return NULL;
1007
1008         v = &vertex->vertices->v[vertex->id];
1009
1010         return isl_basic_set_copy(v->vertex);
1011 }
1012
1013 static __isl_give isl_vertex *isl_vertex_alloc(__isl_take isl_vertices *vertices,
1014         int id)
1015 {
1016         isl_vertex *vertex;
1017
1018         if (!vertices)
1019                 return NULL;
1020
1021         vertex = isl_alloc_type(vertices->ctx, isl_vertex);
1022         if (!vertex)
1023                 goto error;
1024
1025         vertex->vertices = vertices;
1026         vertex->id = id;
1027
1028         return vertex;
1029 error:
1030         isl_vertices_free(vertices);
1031         return NULL;
1032 }
1033
1034 void isl_vertex_free(__isl_take isl_vertex *vertex)
1035 {
1036         if (!vertex)
1037                 return;
1038         isl_vertices_free(vertex->vertices);
1039         free(vertex);
1040 }
1041
1042 __isl_give isl_basic_set *isl_basic_set_set_integral(__isl_take isl_basic_set *bset)
1043 {
1044         if (!bset)
1045                 return NULL;
1046
1047         if (!ISL_F_ISSET(bset, ISL_BASIC_MAP_RATIONAL))
1048                 return bset;
1049
1050         bset = isl_basic_set_cow(bset);
1051         if (!bset)
1052                 return NULL;
1053
1054         ISL_F_CLR(bset, ISL_BASIC_MAP_RATIONAL);
1055
1056         return isl_basic_set_finalize(bset);
1057 }
1058
1059 isl_ctx *isl_cell_get_ctx(__isl_keep isl_cell *cell)
1060 {
1061         return cell ? cell->dom->ctx : NULL;
1062 }
1063
1064 __isl_give isl_basic_set *isl_cell_get_domain(__isl_keep isl_cell *cell)
1065 {
1066         return cell ? isl_basic_set_copy(cell->dom) : NULL;
1067 }
1068
1069 static __isl_give isl_cell *isl_cell_alloc(__isl_take isl_vertices *vertices,
1070         __isl_take isl_basic_set *dom, int id)
1071 {
1072         int i;
1073         isl_cell *cell = NULL;
1074
1075         if (!vertices || !dom)
1076                 goto error;
1077
1078         cell = isl_calloc_type(dom->ctx, isl_cell);
1079         if (!cell)
1080                 goto error;
1081
1082         cell->n_vertices = vertices->c[id].n_vertices;
1083         cell->ids = isl_alloc_array(dom->ctx, int, cell->n_vertices);
1084         if (!cell->ids)
1085                 goto error;
1086         for (i = 0; i < cell->n_vertices; ++i)
1087                 cell->ids[i] = vertices->c[id].vertices[i];
1088         cell->vertices = vertices;
1089         cell->dom = dom;
1090
1091         return cell;
1092 error:
1093         isl_cell_free(cell);
1094         isl_vertices_free(vertices);
1095         isl_basic_set_free(dom);
1096         return NULL;
1097 }
1098
1099 void isl_cell_free(__isl_take isl_cell *cell)
1100 {
1101         if (!cell)
1102                 return;
1103
1104         isl_vertices_free(cell->vertices);
1105         free(cell->ids);
1106         isl_basic_set_free(cell->dom);
1107         free(cell);
1108 }
1109
1110 /* Create a tableau of the cone obtained by first homogenizing the given
1111  * polytope and then making all inequalities strict by setting the
1112  * constant term to -1.
1113  */
1114 static struct isl_tab *tab_for_shifted_cone(__isl_keep isl_basic_set *bset)
1115 {
1116         int i;
1117         isl_vec *c = NULL;
1118         struct isl_tab *tab;
1119
1120         if (!bset)
1121                 return NULL;
1122         tab = isl_tab_alloc(bset->ctx, bset->n_ineq + 1,
1123                             1 + isl_basic_set_total_dim(bset), 0);
1124         if (!tab)
1125                 return NULL;
1126         tab->rational = ISL_F_ISSET(bset, ISL_BASIC_SET_RATIONAL);
1127         if (ISL_F_ISSET(bset, ISL_BASIC_MAP_EMPTY)) {
1128                 if (isl_tab_mark_empty(tab) < 0)
1129                         goto error;
1130                 return tab;
1131         }
1132
1133         c = isl_vec_alloc(bset->ctx, 1 + 1 + isl_basic_set_total_dim(bset));
1134         if (!c)
1135                 goto error;
1136
1137         isl_int_set_si(c->el[0], 0);
1138         for (i = 0; i < bset->n_eq; ++i) {
1139                 isl_seq_cpy(c->el + 1, bset->eq[i], c->size - 1);
1140                 if (isl_tab_add_eq(tab, c->el) < 0)
1141                         goto error;
1142         }
1143
1144         isl_int_set_si(c->el[0], -1);
1145         for (i = 0; i < bset->n_ineq; ++i) {
1146                 isl_seq_cpy(c->el + 1, bset->ineq[i], c->size - 1);
1147                 if (isl_tab_add_ineq(tab, c->el) < 0)
1148                         goto error;
1149                 if (tab->empty) {
1150                         isl_vec_free(c);
1151                         return tab;
1152                 }
1153         }
1154
1155         isl_seq_clr(c->el + 1, c->size - 1);
1156         isl_int_set_si(c->el[1], 1);
1157         if (isl_tab_add_ineq(tab, c->el) < 0)
1158                 goto error;
1159
1160         isl_vec_free(c);
1161         return tab;
1162 error:
1163         isl_vec_free(c);
1164         isl_tab_free(tab);
1165         return NULL;
1166 }
1167
1168 /* Compute an interior point of "bset" by selecting an interior
1169  * point in homogeneous space and projecting the point back down.
1170  */
1171 static __isl_give isl_vec *isl_basic_set_interior_point(
1172         __isl_keep isl_basic_set *bset)
1173 {
1174         isl_vec *vec;
1175         struct isl_tab *tab;
1176
1177         tab = tab_for_shifted_cone(bset);
1178         vec = isl_tab_get_sample_value(tab);
1179         isl_tab_free(tab);
1180         if (!vec)
1181                 return NULL;
1182
1183         isl_seq_cpy(vec->el, vec->el + 1, vec->size - 1);
1184         vec->size--;
1185
1186         return vec;
1187 }
1188
1189 /* Call "fn" on all chambers of the parametric polytope with the shared
1190  * facets of neighboring chambers only appearing in one of the chambers.
1191  *
1192  * We pick an interior point from one of the chambers and then make
1193  * all constraints that do not satisfy this point strict.
1194  */
1195 int isl_vertices_foreach_disjoint_cell(__isl_keep isl_vertices *vertices,
1196         int (*fn)(__isl_take isl_cell *cell, void *user), void *user)
1197 {
1198         int i, j;
1199         isl_vec *vec;
1200         isl_int v;
1201         isl_cell *cell;
1202
1203         if (!vertices)
1204                 return -1;
1205
1206         if (vertices->n_chambers == 0)
1207                 return 0;
1208
1209         if (vertices->n_chambers == 1) {
1210                 isl_basic_set *dom = isl_basic_set_copy(vertices->c[0].dom);
1211                 dom = isl_basic_set_set_integral(dom);
1212                 cell = isl_cell_alloc(isl_vertices_copy(vertices), dom, 0);
1213                 if (!cell)
1214                         return -1;
1215                 return fn(cell, user);
1216         }
1217
1218         vec = isl_basic_set_interior_point(vertices->c[0].dom);
1219         if (!vec)
1220                 return -1;
1221
1222         isl_int_init(v);
1223
1224         for (i = 0; i < vertices->n_chambers; ++i) {
1225                 int r;
1226                 isl_basic_set *dom = isl_basic_set_copy(vertices->c[i].dom);
1227                 dom = isl_basic_set_cow(dom);
1228                 if (!dom)
1229                         goto error;
1230                 for (j = 0; i && j < dom->n_ineq; ++j) {
1231                         isl_seq_inner_product(vec->el, dom->ineq[j], vec->size,
1232                                                 &v);
1233                         if (!isl_int_is_neg(v))
1234                                 continue;
1235                         isl_int_sub_ui(dom->ineq[j][0], dom->ineq[j][0], 1);
1236                 }
1237                 dom = isl_basic_set_set_integral(dom);
1238                 cell = isl_cell_alloc(isl_vertices_copy(vertices), dom, i);
1239                 if (!cell)
1240                         goto error;
1241                 r = fn(cell, user);
1242                 if (r < 0)
1243                         goto error;
1244         }
1245
1246         isl_int_clear(v);
1247         isl_vec_free(vec);
1248
1249         return 0;
1250 error:
1251         isl_int_clear(v);
1252         isl_vec_free(vec);
1253         return -1;
1254 }
1255
1256 int isl_vertices_foreach_cell(__isl_keep isl_vertices *vertices,
1257         int (*fn)(__isl_take isl_cell *cell, void *user), void *user)
1258 {
1259         int i;
1260         isl_cell *cell;
1261
1262         if (!vertices)
1263                 return -1;
1264
1265         if (vertices->n_chambers == 0)
1266                 return 0;
1267
1268         for (i = 0; i < vertices->n_chambers; ++i) {
1269                 int r;
1270                 isl_basic_set *dom = isl_basic_set_copy(vertices->c[i].dom);
1271
1272                 cell = isl_cell_alloc(isl_vertices_copy(vertices), dom, i);
1273                 if (!cell)
1274                         return -1;
1275
1276                 r = fn(cell, user);
1277                 if (r < 0)
1278                         return -1;
1279         }
1280
1281         return 0;
1282 }
1283
1284 int isl_vertices_foreach_vertex(__isl_keep isl_vertices *vertices,
1285         int (*fn)(__isl_take isl_vertex *vertex, void *user), void *user)
1286 {
1287         int i;
1288         isl_vertex *vertex;
1289
1290         if (!vertices)
1291                 return -1;
1292
1293         if (vertices->n_vertices == 0)
1294                 return 0;
1295
1296         for (i = 0; i < vertices->n_vertices; ++i) {
1297                 int r;
1298
1299                 vertex = isl_vertex_alloc(isl_vertices_copy(vertices), i);
1300                 if (!vertex)
1301                         return -1;
1302
1303                 r = fn(vertex, user);
1304                 if (r < 0)
1305                         return -1;
1306         }
1307
1308         return 0;
1309 }
1310
1311 int isl_cell_foreach_vertex(__isl_keep isl_cell *cell,
1312         int (*fn)(__isl_take isl_vertex *vertex, void *user), void *user)
1313 {
1314         int i;
1315         isl_vertex *vertex;
1316
1317         if (!cell)
1318                 return -1;
1319
1320         if (cell->n_vertices == 0)
1321                 return 0;
1322
1323         for (i = 0; i < cell->n_vertices; ++i) {
1324                 int r;
1325
1326                 vertex = isl_vertex_alloc(isl_vertices_copy(cell->vertices),
1327                                           cell->ids[i]);
1328                 if (!vertex)
1329                         return -1;
1330
1331                 r = fn(vertex, user);
1332                 if (r < 0)
1333                         return -1;
1334         }
1335
1336         return 0;
1337 }
1338
1339 isl_ctx *isl_vertices_get_ctx(__isl_keep isl_vertices *vertices)
1340 {
1341         return vertices ? vertices->bset->ctx : NULL;
1342 }
1343
1344 int isl_vertices_get_n_vertices(__isl_keep isl_vertices *vertices)
1345 {
1346         return vertices ? vertices->n_vertices : -1;
1347 }
1348
1349 __isl_give isl_vertices *isl_morph_vertices(__isl_take isl_morph *morph,
1350         __isl_take isl_vertices *vertices)
1351 {
1352         int i;
1353         isl_morph *param_morph = NULL;
1354
1355         if (!morph || !vertices)
1356                 goto error;
1357
1358         isl_assert(vertices->bset->ctx, vertices->ref == 1, goto error);
1359
1360         param_morph = isl_morph_copy(morph);
1361         param_morph = isl_morph_remove_dom_dims(param_morph, isl_dim_set,
1362                                     0, isl_morph_dom_dim(morph, isl_dim_set));
1363         param_morph = isl_morph_remove_ran_dims(param_morph, isl_dim_set,
1364                                     0, isl_morph_ran_dim(morph, isl_dim_set));
1365
1366         for (i = 0; i < vertices->n_vertices; ++i) {
1367                 vertices->v[i].dom = isl_morph_basic_set(
1368                         isl_morph_copy(param_morph), vertices->v[i].dom);
1369                 vertices->v[i].vertex = isl_morph_basic_set(
1370                         isl_morph_copy(morph), vertices->v[i].vertex);
1371                 if (!vertices->v[i].vertex)
1372                         goto error;
1373         }
1374
1375         for (i = 0; i < vertices->n_chambers; ++i) {
1376                 vertices->c[i].dom = isl_morph_basic_set(
1377                         isl_morph_copy(param_morph), vertices->c[i].dom);
1378                 if (!vertices->c[i].dom)
1379                         goto error;
1380         }
1381
1382         isl_morph_free(param_morph);
1383         isl_morph_free(morph);
1384         return vertices;
1385 error:
1386         isl_morph_free(param_morph);
1387         isl_morph_free(morph);
1388         isl_vertices_free(vertices);
1389         return NULL;
1390 }