isl_vertices.c: compute_chambers: avoid invalid access on error path
[platform/upstream/isl.git] / isl_vertices.c
1 /*
2  * Copyright 2010      INRIA Saclay
3  *
4  * Use of this software is governed by the MIT 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_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
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         isl_vertices_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         vertices->v[0].dom = isl_basic_set_params(isl_basic_set_copy(bset));
203         if (!vertices->v[0].vertex || !vertices->v[0].dom)
204                 goto error;
205
206         vertices->c = isl_calloc_array(bset->ctx, struct isl_chamber, 1);
207         if (!vertices->c)
208                 goto error;
209         vertices->n_chambers = 1;
210         vertices->c[0].n_vertices = 1;
211         vertices->c[0].vertices = isl_calloc_array(bset->ctx, int, 1);
212         if (!vertices->c[0].vertices)
213                 goto error;
214         vertices->c[0].dom = isl_basic_set_copy(vertices->v[0].dom);
215         if (!vertices->c[0].dom)
216                 goto error;
217
218         return vertices;
219 error:
220         isl_vertices_free(vertices);
221         return NULL;
222 }
223
224 static int isl_mat_rank(__isl_keep isl_mat *mat)
225 {
226         int row, col;
227         isl_mat *H;
228
229         H = isl_mat_left_hermite(isl_mat_copy(mat), 0, NULL, NULL);
230         if (!H)
231                 return -1;
232
233         for (col = 0; col < H->n_col; ++col) {
234                 for (row = 0; row < H->n_row; ++row)
235                         if (!isl_int_is_zero(H->row[row][col]))
236                                 break;
237                 if (row == H->n_row)
238                         break;
239         }
240
241         isl_mat_free(H);
242
243         return col;
244 }
245
246 /* Is the row pointed to by "f" linearly independent of the "n" first
247  * rows in "facets"?
248  */
249 static int is_independent(__isl_keep isl_mat *facets, int n, isl_int *f)
250 {
251         int rank;
252
253         if (isl_seq_first_non_zero(f, facets->n_col) < 0)
254                 return 0;
255
256         isl_seq_cpy(facets->row[n], f, facets->n_col);
257         facets->n_row = n + 1;
258         rank = isl_mat_rank(facets);
259         if (rank < 0)
260                 return -1;
261
262         return rank == n + 1;
263 }
264
265 /* Check whether we can select constraint "level", given the current selection
266  * reflected by facets in "tab", the rows of "facets" and the earlier
267  * "selected" elements of "selection".
268  *
269  * If the constraint is (strictly) redundant in the tableau, selecting it would
270  * result in an empty tableau, so it can't be selected.
271  * If the set variable part of the constraint is not linearly indepedent
272  * of the set variable parts of the already selected constraints,
273  * the constraint cannot be selected.
274  * If selecting the constraint results in an empty tableau, the constraint
275  * cannot be selected.
276  * Finally, if selecting the constraint results in some explicitly
277  * deselected constraints turning into equalities, then the corresponding
278  * vertices have already been generated, so the constraint cannot be selected.
279  */
280 static int can_select(__isl_keep isl_basic_set *bset, int level,
281         struct isl_tab *tab, __isl_keep isl_mat *facets, int selected,
282         int *selection)
283 {
284         int i;
285         int indep;
286         unsigned ovar;
287         struct isl_tab_undo *snap;
288
289         if (isl_tab_is_redundant(tab, level))
290                 return 0;
291
292         ovar = isl_space_offset(bset->dim, isl_dim_set);
293
294         indep = is_independent(facets, selected, bset->ineq[level] + 1 + ovar);
295         if (indep < 0)
296                 return -1;
297         if (!indep)
298                 return 0;
299
300         snap = isl_tab_snap(tab);
301         if (isl_tab_select_facet(tab, level) < 0)
302                 return -1;
303
304         if (tab->empty) {
305                 if (isl_tab_rollback(tab, snap) < 0)
306                         return -1;
307                 return 0;
308         }
309
310         for (i = 0; i < level; ++i) {
311                 int sgn;
312
313                 if (selection[i] != DESELECTED)
314                         continue;
315
316                 if (isl_tab_is_equality(tab, i))
317                         sgn = 0;
318                 else if (isl_tab_is_redundant(tab, i))
319                         sgn = 1;
320                 else
321                         sgn = isl_tab_sign_of_max(tab, i);
322                 if (sgn < -1)
323                         return -1;
324                 if (sgn <= 0) {
325                         if (isl_tab_rollback(tab, snap) < 0)
326                                 return -1;
327                         return 0;
328                 }
329         }
330
331         return 1;
332 }
333
334 /* Compute the parametric vertices and the chamber decomposition
335  * of a parametric polytope that is not full-dimensional.
336  *
337  * Simply map the parametric polytope to a lower dimensional space
338  * and map the resulting vertices back.
339  */
340 static __isl_give isl_vertices *lower_dim_vertices(
341         __isl_keep isl_basic_set *bset)
342 {
343         isl_morph *morph;
344         isl_vertices *vertices;
345
346         bset = isl_basic_set_copy(bset);
347         morph = isl_basic_set_full_compression(bset);
348         bset = isl_morph_basic_set(isl_morph_copy(morph), bset);
349
350         vertices = isl_basic_set_compute_vertices(bset);
351         isl_basic_set_free(bset);
352
353         morph = isl_morph_inverse(morph);
354
355         vertices = isl_morph_vertices(morph, vertices);
356
357         return vertices;
358 }
359
360 /* Compute the parametric vertices and the chamber decomposition
361  * of the parametric polytope defined using the same constraints
362  * as "bset".  "bset" is assumed to have no existentially quantified
363  * variables.
364  *
365  * The vertices themselves are computed in a fairly simplistic way.
366  * We simply run through all combinations of d constraints,
367  * with d the number of set variables, and check if those d constraints
368  * define a vertex.  To avoid the generation of duplicate vertices,
369  * which we may happen if a vertex is defined by more that d constraints,
370  * we make sure we only generate the vertex for the d constraints with
371  * smallest index.
372  *
373  * We set up a tableau and keep track of which facets have been
374  * selected.  The tableau is marked strict_redundant so that we can be
375  * sure that any constraint that is marked redundant (and that is not
376  * also marked zero) is not an equality.
377  * If a constraint is marked DESELECTED, it means the constraint was
378  * SELECTED before (in combination with the same selection of earlier
379  * constraints).  If such a deselected constraint turns out to be an
380  * equality, then any vertex that may still be found with the current
381  * selection has already been generated when the constraint was selected.
382  * A constraint is marked UNSELECTED when there is no way selecting
383  * the constraint could lead to a vertex (in combination with the current
384  * selection of earlier constraints).
385  *
386  * The set variable coefficients of the selected constraints are stored
387  * in the facets matrix.
388  */
389 __isl_give isl_vertices *isl_basic_set_compute_vertices(
390         __isl_keep isl_basic_set *bset)
391 {
392         struct isl_tab *tab;
393         int level;
394         int init;
395         unsigned nvar;
396         int *selection = NULL;
397         int selected;
398         struct isl_tab_undo **snap = NULL;
399         isl_mat *facets = NULL;
400         struct isl_vertex_list *list = NULL;
401         int n_vertices = 0;
402         isl_vertices *vertices;
403
404         if (!bset)
405                 return NULL;
406
407         if (isl_basic_set_plain_is_empty(bset))
408                 return vertices_empty(bset);
409
410         if (bset->n_eq != 0)
411                 return lower_dim_vertices(bset);
412
413         isl_assert(bset->ctx, isl_basic_set_dim(bset, isl_dim_div) == 0,
414                 return NULL);
415
416         if (isl_basic_set_dim(bset, isl_dim_set) == 0)
417                 return vertices_0D(bset);
418
419         nvar = isl_basic_set_dim(bset, isl_dim_set);
420
421         bset = isl_basic_set_copy(bset);
422         bset = isl_basic_set_set_rational(bset);
423         if (!bset)
424                 return NULL;
425
426         tab = isl_tab_from_basic_set(bset, 0);
427         if (!tab)
428                 goto error;
429         tab->strict_redundant = 1;
430
431         if (tab->empty) {
432                 vertices = vertices_empty(bset);
433                 isl_basic_set_free(bset);
434                 isl_tab_free(tab);
435                 return vertices;
436         }
437
438         selection = isl_alloc_array(bset->ctx, int, bset->n_ineq);
439         snap = isl_alloc_array(bset->ctx, struct isl_tab_undo *, bset->n_ineq);
440         facets = isl_mat_alloc(bset->ctx, nvar, nvar);
441         if (!selection || !snap || !facets)
442                 goto error;
443
444         level = 0;
445         init = 1;
446         selected = 0;
447
448         while (level >= 0) {
449                 if (level >= bset->n_ineq ||
450                     (!init && selection[level] != SELECTED)) {
451                         --level;
452                         init = 0;
453                         continue;
454                 }
455                 if (init) {
456                         int ok;
457                         snap[level] = isl_tab_snap(tab);
458                         ok = can_select(bset, level, tab, facets, selected,
459                                         selection);
460                         if (ok < 0)
461                                 goto error;
462                         if (ok) {
463                                 selection[level] = SELECTED;
464                                 selected++;
465                         } else
466                                 selection[level] = UNSELECTED;
467                 } else {
468                         selection[level] = DESELECTED;
469                         selected--;
470                         if (isl_tab_rollback(tab, snap[level]) < 0)
471                                 goto error;
472                 }
473                 if (selected == nvar) {
474                         if (tab->n_dead == nvar) {
475                                 if (add_vertex(&list, bset, tab) < 0)
476                                         goto error;
477                                 n_vertices++;
478                         }
479                         init = 0;
480                         continue;
481                 }
482                 ++level;
483                 init = 1;
484         }
485
486         isl_mat_free(facets);
487         free(selection);
488         free(snap);
489
490         isl_tab_free(tab);
491
492         vertices = vertices_from_list(bset, n_vertices, list);
493
494         vertices = compute_chambers(bset, vertices);
495
496         return vertices;
497 error:
498         free_vertex_list(list);
499         isl_mat_free(facets);
500         free(selection);
501         free(snap);
502         isl_tab_free(tab);
503         isl_basic_set_free(bset);
504         return NULL;
505 }
506
507 struct isl_chamber_list {
508         struct isl_chamber c;
509         struct isl_chamber_list *next;
510 };
511
512 static void free_chamber_list(struct isl_chamber_list *list)
513 {
514         struct isl_chamber_list *next;
515
516         for (; list; list = next) {
517                 next = list->next;
518                 isl_basic_set_free(list->c.dom);
519                 free(list->c.vertices);
520                 free(list);
521         }
522 }
523
524 /* Check whether the basic set "bset" is a superset of the basic set described
525  * by "tab", i.e., check whether all constraints of "bset" are redundant.
526  */
527 static int bset_covers_tab(__isl_keep isl_basic_set *bset, struct isl_tab *tab)
528 {
529         int i;
530
531         if (!bset || !tab)
532                 return -1;
533
534         for (i = 0; i < bset->n_ineq; ++i) {
535                 enum isl_ineq_type type = isl_tab_ineq_type(tab, bset->ineq[i]);
536                 switch (type) {
537                 case isl_ineq_error:            return -1;
538                 case isl_ineq_redundant:        continue;
539                 default:                        return 0;
540                 }
541         }
542
543         return 1;
544 }
545
546 static __isl_give isl_vertices *vertices_add_chambers(
547         __isl_take isl_vertices *vertices, int n_chambers,
548         struct isl_chamber_list *list)
549 {
550         int i;
551         isl_ctx *ctx;
552         struct isl_chamber_list *next;
553
554         ctx = isl_vertices_get_ctx(vertices);
555         vertices->c = isl_alloc_array(ctx, struct isl_chamber, n_chambers);
556         if (!vertices->c)
557                 goto error;
558         vertices->n_chambers = n_chambers;
559
560         for (i = 0; list; list = next, i++) {
561                 next = list->next;
562                 vertices->c[i] = list->c;
563                 free(list);
564         }
565
566         return vertices;
567 error:
568         isl_vertices_free(vertices);
569         free_chamber_list(list);
570         return NULL;
571 }
572
573 /* Can "tab" be intersected with "bset" without resulting in
574  * a lower-dimensional set.
575  */
576 static int can_intersect(struct isl_tab *tab, __isl_keep isl_basic_set *bset)
577 {
578         int i;
579         struct isl_tab_undo *snap;
580
581         if (isl_tab_extend_cons(tab, bset->n_ineq) < 0)
582                 return -1;
583
584         snap = isl_tab_snap(tab);
585
586         for (i = 0; i < bset->n_ineq; ++i) {
587                 if (isl_tab_ineq_type(tab, bset->ineq[i]) == isl_ineq_redundant)
588                         continue;
589                 if (isl_tab_add_ineq(tab, bset->ineq[i]) < 0)
590                         return -1;
591         }
592
593         if (isl_tab_detect_implicit_equalities(tab) < 0)
594                 return -1;
595         if (tab->n_dead) {
596                 if (isl_tab_rollback(tab, snap) < 0)
597                         return -1;
598                 return 0;
599         }
600
601         return 1;
602 }
603
604 static int add_chamber(struct isl_chamber_list **list,
605         __isl_keep isl_vertices *vertices, struct isl_tab *tab, int *selection)
606 {
607         int n_frozen;
608         int i, j;
609         int n_vertices = 0;
610         struct isl_tab_undo *snap;
611         struct isl_chamber_list *c = NULL;
612
613         for (i = 0; i < vertices->n_vertices; ++i)
614                 if (selection[i])
615                         n_vertices++;
616
617         snap = isl_tab_snap(tab);
618
619         for (i = 0; i < tab->n_con && tab->con[i].frozen; ++i)
620                 tab->con[i].frozen = 0;
621         n_frozen = i;
622
623         if (isl_tab_detect_redundant(tab) < 0)
624                 return -1;
625
626         c = isl_calloc_type(tab->mat->ctx, struct isl_chamber_list);
627         if (!c)
628                 goto error;
629         c->c.vertices = isl_alloc_array(tab->mat->ctx, int, n_vertices);
630         if (!c->c.vertices)
631                 goto error;
632         c->c.dom = isl_basic_set_from_basic_map(isl_basic_map_copy(tab->bmap));
633         c->c.dom = isl_basic_set_set_rational(c->c.dom);
634         c->c.dom = isl_basic_set_cow(c->c.dom);
635         c->c.dom = isl_basic_set_update_from_tab(c->c.dom, tab);
636         c->c.dom = isl_basic_set_simplify(c->c.dom);
637         c->c.dom = isl_basic_set_finalize(c->c.dom);
638         if (!c->c.dom)
639                 goto error;
640
641         c->c.n_vertices = n_vertices;
642
643         for (i = 0, j = 0; i < vertices->n_vertices; ++i)
644                 if (selection[i]) {
645                         c->c.vertices[j] = i;
646                         j++;
647                 }
648
649         c->next = *list;
650         *list = c;
651
652         for (i = 0; i < n_frozen; ++i)
653                 tab->con[i].frozen = 1;
654
655         if (isl_tab_rollback(tab, snap) < 0)
656                 return -1;
657
658         return 0;
659 error:
660         free_chamber_list(c);
661         return -1;
662 }
663
664 struct isl_facet_todo {
665         struct isl_tab *tab;    /* A tableau representation of the facet */
666         isl_basic_set *bset;    /* A normalized basic set representation */
667         isl_vec *constraint;    /* Constraint pointing to the other side */
668         struct isl_facet_todo *next;
669 };
670
671 static void free_todo(struct isl_facet_todo *todo)
672 {
673         while (todo) {
674                 struct isl_facet_todo *next = todo->next;
675
676                 isl_tab_free(todo->tab);
677                 isl_basic_set_free(todo->bset);
678                 isl_vec_free(todo->constraint);
679                 free(todo);
680
681                 todo = next;
682         }
683 }
684
685 static struct isl_facet_todo *create_todo(struct isl_tab *tab, int con)
686 {
687         int i;
688         int n_frozen;
689         struct isl_tab_undo *snap;
690         struct isl_facet_todo *todo;
691
692         snap = isl_tab_snap(tab);
693
694         for (i = 0; i < tab->n_con && tab->con[i].frozen; ++i)
695                 tab->con[i].frozen = 0;
696         n_frozen = i;
697
698         if (isl_tab_detect_redundant(tab) < 0)
699                 return NULL;
700
701         todo = isl_calloc_type(tab->mat->ctx, struct isl_facet_todo);
702         if (!todo)
703                 return NULL;
704
705         todo->constraint = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
706         if (!todo->constraint)
707                 goto error;
708         isl_seq_neg(todo->constraint->el, tab->bmap->ineq[con], 1 + tab->n_var);
709         todo->bset = isl_basic_set_from_basic_map(isl_basic_map_copy(tab->bmap));
710         todo->bset = isl_basic_set_set_rational(todo->bset);
711         todo->bset = isl_basic_set_cow(todo->bset);
712         todo->bset = isl_basic_set_update_from_tab(todo->bset, tab);
713         todo->bset = isl_basic_set_simplify(todo->bset);
714         todo->bset = isl_basic_set_sort_constraints(todo->bset);
715         if (!todo->bset)
716                 goto error;
717         ISL_F_SET(todo->bset, ISL_BASIC_SET_NORMALIZED);
718         todo->tab = isl_tab_dup(tab);
719         if (!todo->tab)
720                 goto error;
721
722         for (i = 0; i < n_frozen; ++i)
723                 tab->con[i].frozen = 1;
724
725         if (isl_tab_rollback(tab, snap) < 0)
726                 goto error;
727
728         return todo;
729 error:
730         free_todo(todo);
731         return NULL;
732 }
733
734 /* Create todo items for all interior facets of the chamber represented
735  * by "tab" and collect them in "next".
736  */
737 static int init_todo(struct isl_facet_todo **next, struct isl_tab *tab)
738 {
739         int i;
740         struct isl_tab_undo *snap;
741         struct isl_facet_todo *todo;
742
743         snap = isl_tab_snap(tab);
744
745         for (i = 0; i < tab->n_con; ++i) {
746                 if (tab->con[i].frozen)
747                         continue;
748                 if (tab->con[i].is_redundant)
749                         continue;
750
751                 if (isl_tab_select_facet(tab, i) < 0)
752                         return -1;
753
754                 todo = create_todo(tab, i);
755                 if (!todo)
756                         return -1;
757
758                 todo->next = *next;
759                 *next = todo;
760
761                 if (isl_tab_rollback(tab, snap) < 0)
762                         return -1;
763         }
764
765         return 0;
766 }
767
768 /* Does the linked list contain a todo item that is the opposite of "todo".
769  * If so, return 1 and remove the opposite todo item.
770  */
771 static int has_opposite(struct isl_facet_todo *todo,
772         struct isl_facet_todo **list)
773 {
774         for (; *list; list = &(*list)->next) {
775                 int eq;
776                 eq = isl_basic_set_plain_is_equal(todo->bset, (*list)->bset);
777                 if (eq < 0)
778                         return -1;
779                 if (!eq)
780                         continue;
781                 todo = *list;
782                 *list = todo->next;
783                 todo->next = NULL;
784                 free_todo(todo);
785                 return 1;
786         }
787
788         return 0;
789 }
790
791 /* Create todo items for all interior facets of the chamber represented
792  * by "tab" and collect them in first->next, taking care to cancel
793  * opposite todo items.
794  */
795 static int update_todo(struct isl_facet_todo *first, struct isl_tab *tab)
796 {
797         int i;
798         struct isl_tab_undo *snap;
799         struct isl_facet_todo *todo;
800
801         snap = isl_tab_snap(tab);
802
803         for (i = 0; i < tab->n_con; ++i) {
804                 int drop;
805
806                 if (tab->con[i].frozen)
807                         continue;
808                 if (tab->con[i].is_redundant)
809                         continue;
810
811                 if (isl_tab_select_facet(tab, i) < 0)
812                         return -1;
813
814                 todo = create_todo(tab, i);
815                 if (!todo)
816                         return -1;
817
818                 drop = has_opposite(todo, &first->next);
819                 if (drop < 0)
820                         return -1;
821
822                 if (drop)
823                         free_todo(todo);
824                 else {
825                         todo->next = first->next;
826                         first->next = todo;
827                 }
828
829                 if (isl_tab_rollback(tab, snap) < 0)
830                         return -1;
831         }
832
833         return 0;
834 }
835
836 /* Compute the chamber decomposition of the parametric polytope respresented
837  * by "bset" given the parametric vertices and their activity domains.
838  *
839  * We are only interested in full-dimensional chambers.
840  * Each of these chambers is the intersection of the activity domains of
841  * one or more vertices and the union of all chambers is equal to the
842  * projection of the entire parametric polytope onto the parameter space.
843  *
844  * We first create an initial chamber by intersecting as many activity
845  * domains as possible without ending up with an empty or lower-dimensional
846  * set.  As a minor optimization, we only consider those activity domains
847  * that contain some arbitrary point.
848  *
849  * For each of interior facets of the chamber, we construct a todo item,
850  * containing the facet and a constraint containing the other side of the facet,
851  * for constructing the chamber on the other side.
852  * While their are any todo items left, we pick a todo item and
853  * create the required chamber by intersecting all activity domains
854  * that contain the facet and have a full-dimensional intersection with
855  * the other side of the facet.  For each of the interior facets, we
856  * again create todo items, taking care to cancel opposite todo items.
857  */
858 static __isl_give isl_vertices *compute_chambers(__isl_take isl_basic_set *bset,
859         __isl_take isl_vertices *vertices)
860 {
861         int i;
862         isl_ctx *ctx;
863         isl_vec *sample = NULL;
864         struct isl_tab *tab = NULL;
865         struct isl_tab_undo *snap;
866         int *selection = NULL;
867         int n_chambers = 0;
868         struct isl_chamber_list *list = NULL;
869         struct isl_facet_todo *todo = NULL;
870
871         if (!bset || !vertices)
872                 goto error;
873
874         ctx = isl_vertices_get_ctx(vertices);
875         selection = isl_alloc_array(ctx, int, vertices->n_vertices);
876         if (!selection)
877                 goto error;
878
879         bset = isl_basic_set_params(bset);
880
881         tab = isl_tab_from_basic_set(bset, 1);
882         if (!tab)
883                 goto error;
884         for (i = 0; i < bset->n_ineq; ++i)
885                 if (isl_tab_freeze_constraint(tab, i) < 0)
886                         goto error;
887         isl_basic_set_free(bset);
888
889         snap = isl_tab_snap(tab);
890
891         sample = isl_tab_get_sample_value(tab);
892
893         for (i = 0; i < vertices->n_vertices; ++i) {
894                 selection[i] = isl_basic_set_contains(vertices->v[i].dom, sample);
895                 if (selection[i] < 0)
896                         goto error;
897                 if (!selection[i])
898                         continue;
899                 selection[i] = can_intersect(tab, vertices->v[i].dom);
900                 if (selection[i] < 0)
901                         goto error;
902         }
903
904         if (isl_tab_detect_redundant(tab) < 0)
905                 goto error;
906
907         if (add_chamber(&list, vertices, tab, selection) < 0)
908                 goto error;
909         n_chambers++;
910
911         if (init_todo(&todo, tab) < 0)
912                 goto error;
913
914         while (todo) {
915                 struct isl_facet_todo *next;
916
917                 if (isl_tab_rollback(tab, snap) < 0)
918                         goto error;
919
920                 if (isl_tab_add_ineq(tab, todo->constraint->el) < 0)
921                         goto error;
922                 if (isl_tab_freeze_constraint(tab, tab->n_con - 1) < 0)
923                         goto error;
924
925                 for (i = 0; i < vertices->n_vertices; ++i) {
926                         selection[i] = bset_covers_tab(vertices->v[i].dom,
927                                                         todo->tab);
928                         if (selection[i] < 0)
929                                 goto error;
930                         if (!selection[i])
931                                 continue;
932                         selection[i] = can_intersect(tab, vertices->v[i].dom);
933                         if (selection[i] < 0)
934                                 goto error;
935                 }
936
937                 if (isl_tab_detect_redundant(tab) < 0)
938                         goto error;
939
940                 if (add_chamber(&list, vertices, tab, selection) < 0)
941                         goto error;
942                 n_chambers++;
943
944                 if (update_todo(todo, tab) < 0)
945                         goto error;
946
947                 next = todo->next;
948                 todo->next = NULL;
949                 free_todo(todo);
950                 todo = next;
951         }
952
953         isl_vec_free(sample);
954
955         isl_tab_free(tab);
956         free(selection);
957
958         vertices = vertices_add_chambers(vertices, n_chambers, list);
959
960         for (i = 0; vertices && i < vertices->n_vertices; ++i) {
961                 isl_basic_set_free(vertices->v[i].dom);
962                 vertices->v[i].dom = NULL;
963         }
964
965         return vertices;
966 error:
967         free_chamber_list(list);
968         free_todo(todo);
969         isl_vec_free(sample);
970         isl_tab_free(tab);
971         free(selection);
972         if (!tab)
973                 isl_basic_set_free(bset);
974         isl_vertices_free(vertices);
975         return NULL;
976 }
977
978 isl_ctx *isl_vertex_get_ctx(__isl_keep isl_vertex *vertex)
979 {
980         return vertex ? isl_vertices_get_ctx(vertex->vertices) : NULL;
981 }
982
983 int isl_vertex_get_id(__isl_keep isl_vertex *vertex)
984 {
985         return vertex ? vertex->id : -1;
986 }
987
988 __isl_give isl_basic_set *isl_vertex_get_domain(__isl_keep isl_vertex *vertex)
989 {
990         struct isl_vertex *v;
991
992         if (!vertex)
993                 return NULL;
994
995         v = &vertex->vertices->v[vertex->id];
996         if (!v->dom) {
997                 unsigned nvar;
998                 nvar = isl_basic_set_dim(v->vertex, isl_dim_set);
999                 v->dom = isl_basic_set_copy(v->vertex);
1000                 v->dom = isl_basic_set_project_out(v->dom, isl_dim_set, 0, nvar);
1001         }
1002
1003         return isl_basic_set_copy(v->dom);
1004 }
1005
1006 __isl_give isl_basic_set *isl_vertex_get_expr(__isl_keep isl_vertex *vertex)
1007 {
1008         struct isl_vertex *v;
1009
1010         if (!vertex)
1011                 return NULL;
1012
1013         v = &vertex->vertices->v[vertex->id];
1014
1015         return isl_basic_set_copy(v->vertex);
1016 }
1017
1018 static __isl_give isl_vertex *isl_vertex_alloc(__isl_take isl_vertices *vertices,
1019         int id)
1020 {
1021         isl_ctx *ctx;
1022         isl_vertex *vertex;
1023
1024         if (!vertices)
1025                 return NULL;
1026
1027         ctx = isl_vertices_get_ctx(vertices);
1028         vertex = isl_alloc_type(ctx, isl_vertex);
1029         if (!vertex)
1030                 goto error;
1031
1032         vertex->vertices = vertices;
1033         vertex->id = id;
1034
1035         return vertex;
1036 error:
1037         isl_vertices_free(vertices);
1038         return NULL;
1039 }
1040
1041 void isl_vertex_free(__isl_take isl_vertex *vertex)
1042 {
1043         if (!vertex)
1044                 return;
1045         isl_vertices_free(vertex->vertices);
1046         free(vertex);
1047 }
1048
1049 __isl_give isl_basic_set *isl_basic_set_set_integral(__isl_take isl_basic_set *bset)
1050 {
1051         if (!bset)
1052                 return NULL;
1053
1054         if (!ISL_F_ISSET(bset, ISL_BASIC_MAP_RATIONAL))
1055                 return bset;
1056
1057         bset = isl_basic_set_cow(bset);
1058         if (!bset)
1059                 return NULL;
1060
1061         ISL_F_CLR(bset, ISL_BASIC_MAP_RATIONAL);
1062
1063         return isl_basic_set_finalize(bset);
1064 }
1065
1066 isl_ctx *isl_cell_get_ctx(__isl_keep isl_cell *cell)
1067 {
1068         return cell ? cell->dom->ctx : NULL;
1069 }
1070
1071 __isl_give isl_basic_set *isl_cell_get_domain(__isl_keep isl_cell *cell)
1072 {
1073         return cell ? isl_basic_set_copy(cell->dom) : NULL;
1074 }
1075
1076 static __isl_give isl_cell *isl_cell_alloc(__isl_take isl_vertices *vertices,
1077         __isl_take isl_basic_set *dom, int id)
1078 {
1079         int i;
1080         isl_cell *cell = NULL;
1081
1082         if (!vertices || !dom)
1083                 goto error;
1084
1085         cell = isl_calloc_type(dom->ctx, isl_cell);
1086         if (!cell)
1087                 goto error;
1088
1089         cell->n_vertices = vertices->c[id].n_vertices;
1090         cell->ids = isl_alloc_array(dom->ctx, int, cell->n_vertices);
1091         if (!cell->ids)
1092                 goto error;
1093         for (i = 0; i < cell->n_vertices; ++i)
1094                 cell->ids[i] = vertices->c[id].vertices[i];
1095         cell->vertices = vertices;
1096         cell->dom = dom;
1097
1098         return cell;
1099 error:
1100         isl_cell_free(cell);
1101         isl_vertices_free(vertices);
1102         isl_basic_set_free(dom);
1103         return NULL;
1104 }
1105
1106 void isl_cell_free(__isl_take isl_cell *cell)
1107 {
1108         if (!cell)
1109                 return;
1110
1111         isl_vertices_free(cell->vertices);
1112         free(cell->ids);
1113         isl_basic_set_free(cell->dom);
1114         free(cell);
1115 }
1116
1117 /* Create a tableau of the cone obtained by first homogenizing the given
1118  * polytope and then making all inequalities strict by setting the
1119  * constant term to -1.
1120  */
1121 static struct isl_tab *tab_for_shifted_cone(__isl_keep isl_basic_set *bset)
1122 {
1123         int i;
1124         isl_vec *c = NULL;
1125         struct isl_tab *tab;
1126
1127         if (!bset)
1128                 return NULL;
1129         tab = isl_tab_alloc(bset->ctx, bset->n_ineq + 1,
1130                             1 + isl_basic_set_total_dim(bset), 0);
1131         if (!tab)
1132                 return NULL;
1133         tab->rational = ISL_F_ISSET(bset, ISL_BASIC_SET_RATIONAL);
1134         if (ISL_F_ISSET(bset, ISL_BASIC_MAP_EMPTY)) {
1135                 if (isl_tab_mark_empty(tab) < 0)
1136                         goto error;
1137                 return tab;
1138         }
1139
1140         c = isl_vec_alloc(bset->ctx, 1 + 1 + isl_basic_set_total_dim(bset));
1141         if (!c)
1142                 goto error;
1143
1144         isl_int_set_si(c->el[0], 0);
1145         for (i = 0; i < bset->n_eq; ++i) {
1146                 isl_seq_cpy(c->el + 1, bset->eq[i], c->size - 1);
1147                 if (isl_tab_add_eq(tab, c->el) < 0)
1148                         goto error;
1149         }
1150
1151         isl_int_set_si(c->el[0], -1);
1152         for (i = 0; i < bset->n_ineq; ++i) {
1153                 isl_seq_cpy(c->el + 1, bset->ineq[i], c->size - 1);
1154                 if (isl_tab_add_ineq(tab, c->el) < 0)
1155                         goto error;
1156                 if (tab->empty) {
1157                         isl_vec_free(c);
1158                         return tab;
1159                 }
1160         }
1161
1162         isl_seq_clr(c->el + 1, c->size - 1);
1163         isl_int_set_si(c->el[1], 1);
1164         if (isl_tab_add_ineq(tab, c->el) < 0)
1165                 goto error;
1166
1167         isl_vec_free(c);
1168         return tab;
1169 error:
1170         isl_vec_free(c);
1171         isl_tab_free(tab);
1172         return NULL;
1173 }
1174
1175 /* Compute an interior point of "bset" by selecting an interior
1176  * point in homogeneous space and projecting the point back down.
1177  */
1178 static __isl_give isl_vec *isl_basic_set_interior_point(
1179         __isl_keep isl_basic_set *bset)
1180 {
1181         isl_vec *vec;
1182         struct isl_tab *tab;
1183
1184         tab = tab_for_shifted_cone(bset);
1185         vec = isl_tab_get_sample_value(tab);
1186         isl_tab_free(tab);
1187         if (!vec)
1188                 return NULL;
1189
1190         isl_seq_cpy(vec->el, vec->el + 1, vec->size - 1);
1191         vec->size--;
1192
1193         return vec;
1194 }
1195
1196 /* Call "fn" on all chambers of the parametric polytope with the shared
1197  * facets of neighboring chambers only appearing in one of the chambers.
1198  *
1199  * We pick an interior point from one of the chambers and then make
1200  * all constraints that do not satisfy this point strict.
1201  */
1202 int isl_vertices_foreach_disjoint_cell(__isl_keep isl_vertices *vertices,
1203         int (*fn)(__isl_take isl_cell *cell, void *user), void *user)
1204 {
1205         int i, j;
1206         isl_vec *vec;
1207         isl_int v;
1208         isl_cell *cell;
1209
1210         if (!vertices)
1211                 return -1;
1212
1213         if (vertices->n_chambers == 0)
1214                 return 0;
1215
1216         if (vertices->n_chambers == 1) {
1217                 isl_basic_set *dom = isl_basic_set_copy(vertices->c[0].dom);
1218                 dom = isl_basic_set_set_integral(dom);
1219                 cell = isl_cell_alloc(isl_vertices_copy(vertices), dom, 0);
1220                 if (!cell)
1221                         return -1;
1222                 return fn(cell, user);
1223         }
1224
1225         vec = isl_basic_set_interior_point(vertices->c[0].dom);
1226         if (!vec)
1227                 return -1;
1228
1229         isl_int_init(v);
1230
1231         for (i = 0; i < vertices->n_chambers; ++i) {
1232                 int r;
1233                 isl_basic_set *dom = isl_basic_set_copy(vertices->c[i].dom);
1234                 dom = isl_basic_set_cow(dom);
1235                 if (!dom)
1236                         goto error;
1237                 for (j = 0; i && j < dom->n_ineq; ++j) {
1238                         isl_seq_inner_product(vec->el, dom->ineq[j], vec->size,
1239                                                 &v);
1240                         if (!isl_int_is_neg(v))
1241                                 continue;
1242                         isl_int_sub_ui(dom->ineq[j][0], dom->ineq[j][0], 1);
1243                 }
1244                 dom = isl_basic_set_set_integral(dom);
1245                 cell = isl_cell_alloc(isl_vertices_copy(vertices), dom, i);
1246                 if (!cell)
1247                         goto error;
1248                 r = fn(cell, user);
1249                 if (r < 0)
1250                         goto error;
1251         }
1252
1253         isl_int_clear(v);
1254         isl_vec_free(vec);
1255
1256         return 0;
1257 error:
1258         isl_int_clear(v);
1259         isl_vec_free(vec);
1260         return -1;
1261 }
1262
1263 int isl_vertices_foreach_cell(__isl_keep isl_vertices *vertices,
1264         int (*fn)(__isl_take isl_cell *cell, void *user), void *user)
1265 {
1266         int i;
1267         isl_cell *cell;
1268
1269         if (!vertices)
1270                 return -1;
1271
1272         if (vertices->n_chambers == 0)
1273                 return 0;
1274
1275         for (i = 0; i < vertices->n_chambers; ++i) {
1276                 int r;
1277                 isl_basic_set *dom = isl_basic_set_copy(vertices->c[i].dom);
1278
1279                 cell = isl_cell_alloc(isl_vertices_copy(vertices), dom, i);
1280                 if (!cell)
1281                         return -1;
1282
1283                 r = fn(cell, user);
1284                 if (r < 0)
1285                         return -1;
1286         }
1287
1288         return 0;
1289 }
1290
1291 int isl_vertices_foreach_vertex(__isl_keep isl_vertices *vertices,
1292         int (*fn)(__isl_take isl_vertex *vertex, void *user), void *user)
1293 {
1294         int i;
1295         isl_vertex *vertex;
1296
1297         if (!vertices)
1298                 return -1;
1299
1300         if (vertices->n_vertices == 0)
1301                 return 0;
1302
1303         for (i = 0; i < vertices->n_vertices; ++i) {
1304                 int r;
1305
1306                 vertex = isl_vertex_alloc(isl_vertices_copy(vertices), i);
1307                 if (!vertex)
1308                         return -1;
1309
1310                 r = fn(vertex, user);
1311                 if (r < 0)
1312                         return -1;
1313         }
1314
1315         return 0;
1316 }
1317
1318 int isl_cell_foreach_vertex(__isl_keep isl_cell *cell,
1319         int (*fn)(__isl_take isl_vertex *vertex, void *user), void *user)
1320 {
1321         int i;
1322         isl_vertex *vertex;
1323
1324         if (!cell)
1325                 return -1;
1326
1327         if (cell->n_vertices == 0)
1328                 return 0;
1329
1330         for (i = 0; i < cell->n_vertices; ++i) {
1331                 int r;
1332
1333                 vertex = isl_vertex_alloc(isl_vertices_copy(cell->vertices),
1334                                           cell->ids[i]);
1335                 if (!vertex)
1336                         return -1;
1337
1338                 r = fn(vertex, user);
1339                 if (r < 0)
1340                         return -1;
1341         }
1342
1343         return 0;
1344 }
1345
1346 isl_ctx *isl_vertices_get_ctx(__isl_keep isl_vertices *vertices)
1347 {
1348         return vertices ? vertices->bset->ctx : NULL;
1349 }
1350
1351 int isl_vertices_get_n_vertices(__isl_keep isl_vertices *vertices)
1352 {
1353         return vertices ? vertices->n_vertices : -1;
1354 }
1355
1356 __isl_give isl_vertices *isl_morph_vertices(__isl_take isl_morph *morph,
1357         __isl_take isl_vertices *vertices)
1358 {
1359         int i;
1360         isl_morph *param_morph = NULL;
1361
1362         if (!morph || !vertices)
1363                 goto error;
1364
1365         isl_assert(vertices->bset->ctx, vertices->ref == 1, goto error);
1366
1367         param_morph = isl_morph_copy(morph);
1368         param_morph = isl_morph_dom_params(param_morph);
1369         param_morph = isl_morph_ran_params(param_morph);
1370
1371         for (i = 0; i < vertices->n_vertices; ++i) {
1372                 vertices->v[i].dom = isl_morph_basic_set(
1373                         isl_morph_copy(param_morph), vertices->v[i].dom);
1374                 vertices->v[i].vertex = isl_morph_basic_set(
1375                         isl_morph_copy(morph), vertices->v[i].vertex);
1376                 if (!vertices->v[i].vertex)
1377                         goto error;
1378         }
1379
1380         for (i = 0; i < vertices->n_chambers; ++i) {
1381                 vertices->c[i].dom = isl_morph_basic_set(
1382                         isl_morph_copy(param_morph), vertices->c[i].dom);
1383                 if (!vertices->c[i].dom)
1384                         goto error;
1385         }
1386
1387         isl_morph_free(param_morph);
1388         isl_morph_free(morph);
1389         return vertices;
1390 error:
1391         isl_morph_free(param_morph);
1392         isl_morph_free(morph);
1393         isl_vertices_free(vertices);
1394         return NULL;
1395 }
1396
1397 /* Construct a simplex isl_cell spanned by the vertices with indices in
1398  * "simplex_ids" and "other_ids" and call "fn" on this isl_cell.
1399  */
1400 static int call_on_simplex(__isl_keep isl_cell *cell,
1401         int *simplex_ids, int n_simplex, int *other_ids, int n_other,
1402         int (*fn)(__isl_take isl_cell *simplex, void *user), void *user)
1403 {
1404         int i;
1405         isl_ctx *ctx;
1406         struct isl_cell *simplex;
1407
1408         ctx = isl_cell_get_ctx(cell);
1409
1410         simplex = isl_calloc_type(ctx, struct isl_cell);
1411         if (!simplex)
1412                 return -1;
1413         simplex->vertices = isl_vertices_copy(cell->vertices);
1414         if (!simplex->vertices)
1415                 goto error;
1416         simplex->dom = isl_basic_set_copy(cell->dom);
1417         if (!simplex->dom)
1418                 goto error;
1419         simplex->n_vertices = n_simplex + n_other;
1420         simplex->ids = isl_alloc_array(ctx, int, simplex->n_vertices);
1421         if (!simplex->ids)
1422                 goto error;
1423
1424         for (i = 0; i < n_simplex; ++i)
1425                 simplex->ids[i] = simplex_ids[i];
1426         for (i = 0; i < n_other; ++i)
1427                 simplex->ids[n_simplex + i] = other_ids[i];
1428
1429         return fn(simplex, user);
1430 error:
1431         isl_cell_free(simplex);
1432         return -1;
1433 }
1434
1435 /* Check whether the parametric vertex described by "vertex"
1436  * lies on the facet corresponding to constraint "facet" of "bset".
1437  * The isl_vec "v" is a temporary vector than can be used by this function.
1438  *
1439  * We eliminate the variables from the facet constraint using the
1440  * equalities defining the vertex and check if the result is identical
1441  * to zero.
1442  *
1443  * It would probably be better to keep track of the constraints defining
1444  * a vertex during the vertex construction so that we could simply look
1445  * it up here.
1446  */
1447 static int vertex_on_facet(__isl_keep isl_basic_set *vertex,
1448         __isl_keep isl_basic_set *bset, int facet, __isl_keep isl_vec *v)
1449 {
1450         int i;
1451         isl_int m;
1452
1453         isl_seq_cpy(v->el, bset->ineq[facet], v->size);
1454
1455         isl_int_init(m);
1456         for (i = 0; i < vertex->n_eq; ++i) {
1457                 int k = isl_seq_last_non_zero(vertex->eq[i], v->size);
1458                 isl_seq_elim(v->el, vertex->eq[i], k, v->size, &m);
1459         }
1460         isl_int_clear(m);
1461
1462         return isl_seq_first_non_zero(v->el, v->size) == -1;
1463 }
1464
1465 /* Triangulate the polytope spanned by the vertices with ids
1466  * in "simplex_ids" and "other_ids" and call "fn" on each of
1467  * the resulting simplices.
1468  * If the input polytope is already a simplex, we simply call "fn".
1469  * Otherwise, we pick a point from "other_ids" and add it to "simplex_ids".
1470  * Then we consider each facet of "bset" that does not contain the point
1471  * we just picked, but does contain some of the other points in "other_ids"
1472  * and call ourselves recursively on the polytope spanned by the new
1473  * "simplex_ids" and those points in "other_ids" that lie on the facet.
1474  */
1475 static int triangulate(__isl_keep isl_cell *cell, __isl_keep isl_vec *v,
1476         int *simplex_ids, int n_simplex, int *other_ids, int n_other,
1477         int (*fn)(__isl_take isl_cell *simplex, void *user), void *user)
1478 {
1479         int i, j, k;
1480         int d, nparam;
1481         int *ids;
1482         isl_ctx *ctx;
1483         isl_basic_set *vertex;
1484         isl_basic_set *bset;
1485
1486         ctx = isl_cell_get_ctx(cell);
1487         d = isl_basic_set_dim(cell->vertices->bset, isl_dim_set);
1488         nparam = isl_basic_set_dim(cell->vertices->bset, isl_dim_param);
1489
1490         if (n_simplex + n_other == d + 1)
1491                 return call_on_simplex(cell, simplex_ids, n_simplex,
1492                                        other_ids, n_other, fn, user);
1493
1494         simplex_ids[n_simplex] = other_ids[0];
1495         vertex = cell->vertices->v[other_ids[0]].vertex;
1496         bset = cell->vertices->bset;
1497
1498         ids = isl_alloc_array(ctx, int, n_other - 1);
1499         for (i = 0; i < bset->n_ineq; ++i) {
1500                 if (isl_seq_first_non_zero(bset->ineq[i] + 1 + nparam, d) == -1)
1501                         continue;
1502                 if (vertex_on_facet(vertex, bset, i, v))
1503                         continue;
1504
1505                 for (j = 1, k = 0; j < n_other; ++j) {
1506                         isl_basic_set *ov;
1507                         ov = cell->vertices->v[other_ids[j]].vertex;
1508                         if (vertex_on_facet(ov, bset, i, v))
1509                                 ids[k++] = other_ids[j];
1510                 }
1511                 if (k == 0)
1512                         continue;
1513
1514                 if (triangulate(cell, v, simplex_ids, n_simplex + 1,
1515                                 ids, k, fn, user) < 0)
1516                         goto error;
1517         }
1518         free(ids);
1519
1520         return 0;
1521 error:
1522         free(ids);
1523         return -1;
1524 }
1525
1526 /* Triangulate the given cell and call "fn" on each of the resulting
1527  * simplices.
1528  */
1529 int isl_cell_foreach_simplex(__isl_take isl_cell *cell,
1530         int (*fn)(__isl_take isl_cell *simplex, void *user), void *user)
1531 {
1532         int d, total;
1533         int r;
1534         isl_ctx *ctx;
1535         isl_vec *v = NULL;
1536         int *simplex_ids = NULL;
1537
1538         if (!cell)
1539                 return -1;
1540
1541         d = isl_basic_set_dim(cell->vertices->bset, isl_dim_set);
1542         total = isl_basic_set_total_dim(cell->vertices->bset);
1543
1544         if (cell->n_vertices == d + 1)
1545                 return fn(cell, user);
1546
1547         ctx = isl_cell_get_ctx(cell);
1548         simplex_ids = isl_alloc_array(ctx, int, d + 1);
1549         if (!simplex_ids)
1550                 goto error;
1551
1552         v = isl_vec_alloc(ctx, 1 + total);
1553         if (!v)
1554                 goto error;
1555
1556         r = triangulate(cell, v, simplex_ids, 0,
1557                         cell->ids, cell->n_vertices, fn, user);
1558
1559         isl_vec_free(v);
1560         free(simplex_ids);
1561
1562         isl_cell_free(cell);
1563
1564         return r;
1565 error:
1566         free(simplex_ids);
1567         isl_vec_free(v);
1568         isl_cell_free(cell);
1569         return -1;
1570 }