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