isl_ast.c: fix typo in comment
[isl.git] / isl_affine_hull.c
blob6973f15692161d313833d4928246e4d94f4cd560
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2012 Ecole Normale Superieure
6 * Use of this software is governed by the MIT license
8 * Written by Sven Verdoolaege, K.U.Leuven, Departement
9 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
10 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
11 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
12 * and Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
15 #include <isl_ctx_private.h>
16 #include <isl_map_private.h>
17 #include <isl_seq.h>
18 #include <isl/set.h>
19 #include <isl/lp.h>
20 #include <isl/map.h>
21 #include "isl_equalities.h"
22 #include "isl_sample.h"
23 #include "isl_tab.h"
24 #include <isl_mat_private.h>
25 #include <isl_vec_private.h>
27 #include <bset_to_bmap.c>
28 #include <bset_from_bmap.c>
29 #include <set_to_map.c>
30 #include <set_from_map.c>
32 __isl_give isl_basic_map *isl_basic_map_implicit_equalities(
33 __isl_take isl_basic_map *bmap)
35 struct isl_tab *tab;
37 if (!bmap)
38 return bmap;
40 bmap = isl_basic_map_gauss(bmap, NULL);
41 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
42 return bmap;
43 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NO_IMPLICIT))
44 return bmap;
45 if (bmap->n_ineq <= 1)
46 return bmap;
48 tab = isl_tab_from_basic_map(bmap, 0);
49 if (isl_tab_detect_implicit_equalities(tab) < 0)
50 goto error;
51 bmap = isl_basic_map_update_from_tab(bmap, tab);
52 isl_tab_free(tab);
53 bmap = isl_basic_map_gauss(bmap, NULL);
54 ISL_F_SET(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
55 return bmap;
56 error:
57 isl_tab_free(tab);
58 isl_basic_map_free(bmap);
59 return NULL;
62 struct isl_basic_set *isl_basic_set_implicit_equalities(
63 struct isl_basic_set *bset)
65 return bset_from_bmap(
66 isl_basic_map_implicit_equalities(bset_to_bmap(bset)));
69 /* Make eq[row][col] of both bmaps equal so we can add the row
70 * add the column to the common matrix.
71 * Note that because of the echelon form, the columns of row row
72 * after column col are zero.
74 static void set_common_multiple(
75 struct isl_basic_set *bset1, struct isl_basic_set *bset2,
76 unsigned row, unsigned col)
78 isl_int m, c;
80 if (isl_int_eq(bset1->eq[row][col], bset2->eq[row][col]))
81 return;
83 isl_int_init(c);
84 isl_int_init(m);
85 isl_int_lcm(m, bset1->eq[row][col], bset2->eq[row][col]);
86 isl_int_divexact(c, m, bset1->eq[row][col]);
87 isl_seq_scale(bset1->eq[row], bset1->eq[row], c, col+1);
88 isl_int_divexact(c, m, bset2->eq[row][col]);
89 isl_seq_scale(bset2->eq[row], bset2->eq[row], c, col+1);
90 isl_int_clear(c);
91 isl_int_clear(m);
94 /* Delete a given equality, moving all the following equalities one up.
96 static void delete_row(struct isl_basic_set *bset, unsigned row)
98 isl_int *t;
99 int r;
101 t = bset->eq[row];
102 bset->n_eq--;
103 for (r = row; r < bset->n_eq; ++r)
104 bset->eq[r] = bset->eq[r+1];
105 bset->eq[bset->n_eq] = t;
108 /* Make first row entries in column col of bset1 identical to
109 * those of bset2, using the fact that entry bset1->eq[row][col]=a
110 * is non-zero. Initially, these elements of bset1 are all zero.
111 * For each row i < row, we set
112 * A[i] = a * A[i] + B[i][col] * A[row]
113 * B[i] = a * B[i]
114 * so that
115 * A[i][col] = B[i][col] = a * old(B[i][col])
117 static isl_stat construct_column(
118 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
119 unsigned row, unsigned col)
121 int r;
122 isl_int a;
123 isl_int b;
124 isl_size total;
126 total = isl_basic_set_dim(bset1, isl_dim_set);
127 if (total < 0)
128 return isl_stat_error;
130 isl_int_init(a);
131 isl_int_init(b);
132 for (r = 0; r < row; ++r) {
133 if (isl_int_is_zero(bset2->eq[r][col]))
134 continue;
135 isl_int_gcd(b, bset2->eq[r][col], bset1->eq[row][col]);
136 isl_int_divexact(a, bset1->eq[row][col], b);
137 isl_int_divexact(b, bset2->eq[r][col], b);
138 isl_seq_combine(bset1->eq[r], a, bset1->eq[r],
139 b, bset1->eq[row], 1 + total);
140 isl_seq_scale(bset2->eq[r], bset2->eq[r], a, 1 + total);
142 isl_int_clear(a);
143 isl_int_clear(b);
144 delete_row(bset1, row);
146 return isl_stat_ok;
149 /* Make first row entries in column col of bset1 identical to
150 * those of bset2, using only these entries of the two matrices.
151 * Let t be the last row with different entries.
152 * For each row i < t, we set
153 * A[i] = (A[t][col]-B[t][col]) * A[i] + (B[i][col]-A[i][col) * A[t]
154 * B[i] = (A[t][col]-B[t][col]) * B[i] + (B[i][col]-A[i][col) * B[t]
155 * so that
156 * A[i][col] = B[i][col] = old(A[t][col]*B[i][col]-A[i][col]*B[t][col])
158 static isl_bool transform_column(
159 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
160 unsigned row, unsigned col)
162 int i, t;
163 isl_int a, b, g;
164 isl_size total;
166 for (t = row-1; t >= 0; --t)
167 if (isl_int_ne(bset1->eq[t][col], bset2->eq[t][col]))
168 break;
169 if (t < 0)
170 return isl_bool_false;
172 total = isl_basic_set_dim(bset1, isl_dim_set);
173 if (total < 0)
174 return isl_bool_error;
175 isl_int_init(a);
176 isl_int_init(b);
177 isl_int_init(g);
178 isl_int_sub(b, bset1->eq[t][col], bset2->eq[t][col]);
179 for (i = 0; i < t; ++i) {
180 isl_int_sub(a, bset2->eq[i][col], bset1->eq[i][col]);
181 isl_int_gcd(g, a, b);
182 isl_int_divexact(a, a, g);
183 isl_int_divexact(g, b, g);
184 isl_seq_combine(bset1->eq[i], g, bset1->eq[i], a, bset1->eq[t],
185 1 + total);
186 isl_seq_combine(bset2->eq[i], g, bset2->eq[i], a, bset2->eq[t],
187 1 + total);
189 isl_int_clear(a);
190 isl_int_clear(b);
191 isl_int_clear(g);
192 delete_row(bset1, t);
193 delete_row(bset2, t);
194 return isl_bool_true;
197 /* The implementation is based on Section 5.2 of Michael Karr,
198 * "Affine Relationships Among Variables of a Program",
199 * except that the echelon form we use starts from the last column
200 * and that we are dealing with integer coefficients.
202 static __isl_give isl_basic_set *affine_hull(
203 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
205 isl_size dim;
206 unsigned total;
207 int col;
208 int row;
210 dim = isl_basic_set_dim(bset1, isl_dim_set);
211 if (dim < 0 || !bset2)
212 goto error;
214 total = 1 + dim;
216 row = 0;
217 for (col = total-1; col >= 0; --col) {
218 int is_zero1 = row >= bset1->n_eq ||
219 isl_int_is_zero(bset1->eq[row][col]);
220 int is_zero2 = row >= bset2->n_eq ||
221 isl_int_is_zero(bset2->eq[row][col]);
222 if (!is_zero1 && !is_zero2) {
223 set_common_multiple(bset1, bset2, row, col);
224 ++row;
225 } else if (!is_zero1 && is_zero2) {
226 if (construct_column(bset1, bset2, row, col) < 0)
227 goto error;
228 } else if (is_zero1 && !is_zero2) {
229 if (construct_column(bset2, bset1, row, col) < 0)
230 goto error;
231 } else {
232 isl_bool transform;
234 transform = transform_column(bset1, bset2, row, col);
235 if (transform < 0)
236 goto error;
237 if (transform)
238 --row;
241 isl_assert(bset1->ctx, row == bset1->n_eq, goto error);
242 isl_basic_set_free(bset2);
243 bset1 = isl_basic_set_normalize_constraints(bset1);
244 return bset1;
245 error:
246 isl_basic_set_free(bset1);
247 isl_basic_set_free(bset2);
248 return NULL;
251 /* Find an integer point in the set represented by "tab"
252 * that lies outside of the equality "eq" e(x) = 0.
253 * If "up" is true, look for a point satisfying e(x) - 1 >= 0.
254 * Otherwise, look for a point satisfying -e(x) - 1 >= 0 (i.e., e(x) <= -1).
255 * The point, if found, is returned.
256 * If no point can be found, a zero-length vector is returned.
258 * Before solving an ILP problem, we first check if simply
259 * adding the normal of the constraint to one of the known
260 * integer points in the basic set represented by "tab"
261 * yields another point inside the basic set.
263 * The caller of this function ensures that the tableau is bounded or
264 * that tab->basis and tab->n_unbounded have been set appropriately.
266 static struct isl_vec *outside_point(struct isl_tab *tab, isl_int *eq, int up)
268 struct isl_ctx *ctx;
269 struct isl_vec *sample = NULL;
270 struct isl_tab_undo *snap;
271 unsigned dim;
273 if (!tab)
274 return NULL;
275 ctx = tab->mat->ctx;
277 dim = tab->n_var;
278 sample = isl_vec_alloc(ctx, 1 + dim);
279 if (!sample)
280 return NULL;
281 isl_int_set_si(sample->el[0], 1);
282 isl_seq_combine(sample->el + 1,
283 ctx->one, tab->bmap->sample->el + 1,
284 up ? ctx->one : ctx->negone, eq + 1, dim);
285 if (isl_basic_map_contains(tab->bmap, sample))
286 return sample;
287 isl_vec_free(sample);
288 sample = NULL;
290 snap = isl_tab_snap(tab);
292 if (!up)
293 isl_seq_neg(eq, eq, 1 + dim);
294 isl_int_sub_ui(eq[0], eq[0], 1);
296 if (isl_tab_extend_cons(tab, 1) < 0)
297 goto error;
298 if (isl_tab_add_ineq(tab, eq) < 0)
299 goto error;
301 sample = isl_tab_sample(tab);
303 isl_int_add_ui(eq[0], eq[0], 1);
304 if (!up)
305 isl_seq_neg(eq, eq, 1 + dim);
307 if (sample && isl_tab_rollback(tab, snap) < 0)
308 goto error;
310 return sample;
311 error:
312 isl_vec_free(sample);
313 return NULL;
316 __isl_give isl_basic_set *isl_basic_set_recession_cone(
317 __isl_take isl_basic_set *bset)
319 int i;
321 bset = isl_basic_set_cow(bset);
322 if (isl_basic_set_check_no_locals(bset) < 0)
323 return isl_basic_set_free(bset);
325 for (i = 0; i < bset->n_eq; ++i)
326 isl_int_set_si(bset->eq[i][0], 0);
328 for (i = 0; i < bset->n_ineq; ++i)
329 isl_int_set_si(bset->ineq[i][0], 0);
331 ISL_F_CLR(bset, ISL_BASIC_SET_NO_IMPLICIT);
332 return isl_basic_set_implicit_equalities(bset);
335 /* Move "sample" to a point that is one up (or down) from the original
336 * point in dimension "pos".
338 static void adjacent_point(__isl_keep isl_vec *sample, int pos, int up)
340 if (up)
341 isl_int_add_ui(sample->el[1 + pos], sample->el[1 + pos], 1);
342 else
343 isl_int_sub_ui(sample->el[1 + pos], sample->el[1 + pos], 1);
346 /* Check if any points that are adjacent to "sample" also belong to "bset".
347 * If so, add them to "hull" and return the updated hull.
349 * Before checking whether and adjacent point belongs to "bset", we first
350 * check whether it already belongs to "hull" as this test is typically
351 * much cheaper.
353 static __isl_give isl_basic_set *add_adjacent_points(
354 __isl_take isl_basic_set *hull, __isl_take isl_vec *sample,
355 __isl_keep isl_basic_set *bset)
357 int i, up;
358 isl_size dim;
360 dim = isl_basic_set_dim(hull, isl_dim_set);
361 if (!sample || dim < 0)
362 goto error;
364 for (i = 0; i < dim; ++i) {
365 for (up = 0; up <= 1; ++up) {
366 int contains;
367 isl_basic_set *point;
369 adjacent_point(sample, i, up);
370 contains = isl_basic_set_contains(hull, sample);
371 if (contains < 0)
372 goto error;
373 if (contains) {
374 adjacent_point(sample, i, !up);
375 continue;
377 contains = isl_basic_set_contains(bset, sample);
378 if (contains < 0)
379 goto error;
380 if (contains) {
381 point = isl_basic_set_from_vec(
382 isl_vec_copy(sample));
383 hull = affine_hull(hull, point);
385 adjacent_point(sample, i, !up);
386 if (contains)
387 break;
391 isl_vec_free(sample);
393 return hull;
394 error:
395 isl_vec_free(sample);
396 isl_basic_set_free(hull);
397 return NULL;
400 /* Extend an initial (under-)approximation of the affine hull of basic
401 * set represented by the tableau "tab"
402 * by looking for points that do not satisfy one of the equalities
403 * in the current approximation and adding them to that approximation
404 * until no such points can be found any more.
406 * The caller of this function ensures that "tab" is bounded or
407 * that tab->basis and tab->n_unbounded have been set appropriately.
409 * "bset" may be either NULL or the basic set represented by "tab".
410 * If "bset" is not NULL, we check for any point we find if any
411 * of its adjacent points also belong to "bset".
413 static __isl_give isl_basic_set *extend_affine_hull(struct isl_tab *tab,
414 __isl_take isl_basic_set *hull, __isl_keep isl_basic_set *bset)
416 int i, j;
417 unsigned dim;
419 if (!tab || !hull)
420 goto error;
422 dim = tab->n_var;
424 if (isl_tab_extend_cons(tab, 2 * dim + 1) < 0)
425 goto error;
427 for (i = 0; i < dim; ++i) {
428 struct isl_vec *sample;
429 struct isl_basic_set *point;
430 for (j = 0; j < hull->n_eq; ++j) {
431 sample = outside_point(tab, hull->eq[j], 1);
432 if (!sample)
433 goto error;
434 if (sample->size > 0)
435 break;
436 isl_vec_free(sample);
437 sample = outside_point(tab, hull->eq[j], 0);
438 if (!sample)
439 goto error;
440 if (sample->size > 0)
441 break;
442 isl_vec_free(sample);
444 if (isl_tab_add_eq(tab, hull->eq[j]) < 0)
445 goto error;
447 if (j == hull->n_eq)
448 break;
449 if (tab->samples &&
450 isl_tab_add_sample(tab, isl_vec_copy(sample)) < 0)
451 hull = isl_basic_set_free(hull);
452 if (bset)
453 hull = add_adjacent_points(hull, isl_vec_copy(sample),
454 bset);
455 point = isl_basic_set_from_vec(sample);
456 hull = affine_hull(hull, point);
457 if (!hull)
458 return NULL;
461 return hull;
462 error:
463 isl_basic_set_free(hull);
464 return NULL;
467 /* Construct an initial underapproximation of the hull of "bset"
468 * from "sample" and any of its adjacent points that also belong to "bset".
470 static __isl_give isl_basic_set *initialize_hull(__isl_keep isl_basic_set *bset,
471 __isl_take isl_vec *sample)
473 isl_basic_set *hull;
475 hull = isl_basic_set_from_vec(isl_vec_copy(sample));
476 hull = add_adjacent_points(hull, sample, bset);
478 return hull;
481 /* Look for all equalities satisfied by the integer points in bset,
482 * which is assumed to be bounded.
484 * The equalities are obtained by successively looking for
485 * a point that is affinely independent of the points found so far.
486 * In particular, for each equality satisfied by the points so far,
487 * we check if there is any point on a hyperplane parallel to the
488 * corresponding hyperplane shifted by at least one (in either direction).
490 static __isl_give isl_basic_set *uset_affine_hull_bounded(
491 __isl_take isl_basic_set *bset)
493 struct isl_vec *sample = NULL;
494 struct isl_basic_set *hull;
495 struct isl_tab *tab = NULL;
496 isl_size dim;
498 if (isl_basic_set_plain_is_empty(bset))
499 return bset;
501 dim = isl_basic_set_dim(bset, isl_dim_set);
502 if (dim < 0)
503 return isl_basic_set_free(bset);
505 if (bset->sample && bset->sample->size == 1 + dim) {
506 int contains = isl_basic_set_contains(bset, bset->sample);
507 if (contains < 0)
508 goto error;
509 if (contains) {
510 if (dim == 0)
511 return bset;
512 sample = isl_vec_copy(bset->sample);
513 } else {
514 isl_vec_free(bset->sample);
515 bset->sample = NULL;
519 tab = isl_tab_from_basic_set(bset, 1);
520 if (!tab)
521 goto error;
522 if (tab->empty) {
523 isl_tab_free(tab);
524 isl_vec_free(sample);
525 return isl_basic_set_set_to_empty(bset);
528 if (!sample) {
529 struct isl_tab_undo *snap;
530 snap = isl_tab_snap(tab);
531 sample = isl_tab_sample(tab);
532 if (isl_tab_rollback(tab, snap) < 0)
533 goto error;
534 isl_vec_free(tab->bmap->sample);
535 tab->bmap->sample = isl_vec_copy(sample);
538 if (!sample)
539 goto error;
540 if (sample->size == 0) {
541 isl_tab_free(tab);
542 isl_vec_free(sample);
543 return isl_basic_set_set_to_empty(bset);
546 hull = initialize_hull(bset, sample);
548 hull = extend_affine_hull(tab, hull, bset);
549 isl_basic_set_free(bset);
550 isl_tab_free(tab);
552 return hull;
553 error:
554 isl_vec_free(sample);
555 isl_tab_free(tab);
556 isl_basic_set_free(bset);
557 return NULL;
560 /* Given an unbounded tableau and an integer point satisfying the tableau,
561 * construct an initial affine hull containing the recession cone
562 * shifted to the given point.
564 * The unbounded directions are taken from the last rows of the basis,
565 * which is assumed to have been initialized appropriately.
567 static __isl_give isl_basic_set *initial_hull(struct isl_tab *tab,
568 __isl_take isl_vec *vec)
570 int i;
571 int k;
572 struct isl_basic_set *bset = NULL;
573 struct isl_ctx *ctx;
574 isl_size dim;
576 if (!vec || !tab)
577 return NULL;
578 ctx = vec->ctx;
579 isl_assert(ctx, vec->size != 0, goto error);
581 bset = isl_basic_set_alloc(ctx, 0, vec->size - 1, 0, vec->size - 1, 0);
582 dim = isl_basic_set_dim(bset, isl_dim_set);
583 if (dim < 0)
584 goto error;
585 dim -= tab->n_unbounded;
586 for (i = 0; i < dim; ++i) {
587 k = isl_basic_set_alloc_equality(bset);
588 if (k < 0)
589 goto error;
590 isl_seq_cpy(bset->eq[k] + 1, tab->basis->row[1 + i] + 1,
591 vec->size - 1);
592 isl_seq_inner_product(bset->eq[k] + 1, vec->el +1,
593 vec->size - 1, &bset->eq[k][0]);
594 isl_int_neg(bset->eq[k][0], bset->eq[k][0]);
596 bset->sample = vec;
597 bset = isl_basic_set_gauss(bset, NULL);
599 return bset;
600 error:
601 isl_basic_set_free(bset);
602 isl_vec_free(vec);
603 return NULL;
606 /* Given a tableau of a set and a tableau of the corresponding
607 * recession cone, detect and add all equalities to the tableau.
608 * If the tableau is bounded, then we can simply keep the
609 * tableau in its state after the return from extend_affine_hull.
610 * However, if the tableau is unbounded, then
611 * isl_tab_set_initial_basis_with_cone will add some additional
612 * constraints to the tableau that have to be removed again.
613 * In this case, we therefore rollback to the state before
614 * any constraints were added and then add the equalities back in.
616 struct isl_tab *isl_tab_detect_equalities(struct isl_tab *tab,
617 struct isl_tab *tab_cone)
619 int j;
620 struct isl_vec *sample;
621 struct isl_basic_set *hull = NULL;
622 struct isl_tab_undo *snap;
624 if (!tab || !tab_cone)
625 goto error;
627 snap = isl_tab_snap(tab);
629 isl_mat_free(tab->basis);
630 tab->basis = NULL;
632 isl_assert(tab->mat->ctx, tab->bmap, goto error);
633 isl_assert(tab->mat->ctx, tab->samples, goto error);
634 isl_assert(tab->mat->ctx, tab->samples->n_col == 1 + tab->n_var, goto error);
635 isl_assert(tab->mat->ctx, tab->n_sample > tab->n_outside, goto error);
637 if (isl_tab_set_initial_basis_with_cone(tab, tab_cone) < 0)
638 goto error;
640 sample = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
641 if (!sample)
642 goto error;
644 isl_seq_cpy(sample->el, tab->samples->row[tab->n_outside], sample->size);
646 isl_vec_free(tab->bmap->sample);
647 tab->bmap->sample = isl_vec_copy(sample);
649 if (tab->n_unbounded == 0)
650 hull = isl_basic_set_from_vec(isl_vec_copy(sample));
651 else
652 hull = initial_hull(tab, isl_vec_copy(sample));
654 for (j = tab->n_outside + 1; j < tab->n_sample; ++j) {
655 isl_seq_cpy(sample->el, tab->samples->row[j], sample->size);
656 hull = affine_hull(hull,
657 isl_basic_set_from_vec(isl_vec_copy(sample)));
660 isl_vec_free(sample);
662 hull = extend_affine_hull(tab, hull, NULL);
663 if (!hull)
664 goto error;
666 if (tab->n_unbounded == 0) {
667 isl_basic_set_free(hull);
668 return tab;
671 if (isl_tab_rollback(tab, snap) < 0)
672 goto error;
674 if (hull->n_eq > tab->n_zero) {
675 for (j = 0; j < hull->n_eq; ++j) {
676 isl_seq_normalize(tab->mat->ctx, hull->eq[j], 1 + tab->n_var);
677 if (isl_tab_add_eq(tab, hull->eq[j]) < 0)
678 goto error;
682 isl_basic_set_free(hull);
684 return tab;
685 error:
686 isl_basic_set_free(hull);
687 isl_tab_free(tab);
688 return NULL;
691 /* Compute the affine hull of "bset", where "cone" is the recession cone
692 * of "bset".
694 * We first compute a unimodular transformation that puts the unbounded
695 * directions in the last dimensions. In particular, we take a transformation
696 * that maps all equalities to equalities (in HNF) on the first dimensions.
697 * Let x be the original dimensions and y the transformed, with y_1 bounded
698 * and y_2 unbounded.
700 * [ y_1 ] [ y_1 ] [ Q_1 ]
701 * x = U [ y_2 ] [ y_2 ] = [ Q_2 ] x
703 * Let's call the input basic set S. We compute S' = preimage(S, U)
704 * and drop the final dimensions including any constraints involving them.
705 * This results in set S''.
706 * Then we compute the affine hull A'' of S''.
707 * Let F y_1 >= g be the constraint system of A''. In the transformed
708 * space the y_2 are unbounded, so we can add them back without any constraints,
709 * resulting in
711 * [ y_1 ]
712 * [ F 0 ] [ y_2 ] >= g
713 * or
714 * [ Q_1 ]
715 * [ F 0 ] [ Q_2 ] x >= g
716 * or
717 * F Q_1 x >= g
719 * The affine hull in the original space is then obtained as
720 * A = preimage(A'', Q_1).
722 static struct isl_basic_set *affine_hull_with_cone(struct isl_basic_set *bset,
723 struct isl_basic_set *cone)
725 isl_size total;
726 unsigned cone_dim;
727 struct isl_basic_set *hull;
728 struct isl_mat *M, *U, *Q;
730 total = isl_basic_set_dim(cone, isl_dim_all);
731 if (!bset || total < 0)
732 goto error;
734 cone_dim = total - cone->n_eq;
736 M = isl_mat_sub_alloc6(bset->ctx, cone->eq, 0, cone->n_eq, 1, total);
737 M = isl_mat_left_hermite(M, 0, &U, &Q);
738 if (!M)
739 goto error;
740 isl_mat_free(M);
742 U = isl_mat_lin_to_aff(U);
743 bset = isl_basic_set_preimage(bset, isl_mat_copy(U));
745 bset = isl_basic_set_drop_constraints_involving(bset, total - cone_dim,
746 cone_dim);
747 bset = isl_basic_set_drop_dims(bset, total - cone_dim, cone_dim);
749 Q = isl_mat_lin_to_aff(Q);
750 Q = isl_mat_drop_rows(Q, 1 + total - cone_dim, cone_dim);
752 if (bset && bset->sample && bset->sample->size == 1 + total)
753 bset->sample = isl_mat_vec_product(isl_mat_copy(Q), bset->sample);
755 hull = uset_affine_hull_bounded(bset);
757 if (!hull) {
758 isl_mat_free(Q);
759 isl_mat_free(U);
760 } else {
761 struct isl_vec *sample = isl_vec_copy(hull->sample);
762 U = isl_mat_drop_cols(U, 1 + total - cone_dim, cone_dim);
763 if (sample && sample->size > 0)
764 sample = isl_mat_vec_product(U, sample);
765 else
766 isl_mat_free(U);
767 hull = isl_basic_set_preimage(hull, Q);
768 if (hull) {
769 isl_vec_free(hull->sample);
770 hull->sample = sample;
771 } else
772 isl_vec_free(sample);
775 isl_basic_set_free(cone);
777 return hull;
778 error:
779 isl_basic_set_free(bset);
780 isl_basic_set_free(cone);
781 return NULL;
784 /* Look for all equalities satisfied by the integer points in bset,
785 * which is assumed not to have any explicit equalities.
787 * The equalities are obtained by successively looking for
788 * a point that is affinely independent of the points found so far.
789 * In particular, for each equality satisfied by the points so far,
790 * we check if there is any point on a hyperplane parallel to the
791 * corresponding hyperplane shifted by at least one (in either direction).
793 * Before looking for any outside points, we first compute the recession
794 * cone. The directions of this recession cone will always be part
795 * of the affine hull, so there is no need for looking for any points
796 * in these directions.
797 * In particular, if the recession cone is full-dimensional, then
798 * the affine hull is simply the whole universe.
800 static struct isl_basic_set *uset_affine_hull(struct isl_basic_set *bset)
802 struct isl_basic_set *cone;
803 isl_size total;
805 if (isl_basic_set_plain_is_empty(bset))
806 return bset;
808 cone = isl_basic_set_recession_cone(isl_basic_set_copy(bset));
809 if (!cone)
810 goto error;
811 if (cone->n_eq == 0) {
812 isl_space *space;
813 space = isl_basic_set_get_space(bset);
814 isl_basic_set_free(cone);
815 isl_basic_set_free(bset);
816 return isl_basic_set_universe(space);
819 total = isl_basic_set_dim(cone, isl_dim_all);
820 if (total < 0)
821 bset = isl_basic_set_free(bset);
822 if (cone->n_eq < total)
823 return affine_hull_with_cone(bset, cone);
825 isl_basic_set_free(cone);
826 return uset_affine_hull_bounded(bset);
827 error:
828 isl_basic_set_free(bset);
829 return NULL;
832 /* Look for all equalities satisfied by the integer points in bmap
833 * that are independent of the equalities already explicitly available
834 * in bmap.
836 * We first remove all equalities already explicitly available,
837 * then look for additional equalities in the reduced space
838 * and then transform the result to the original space.
839 * The original equalities are _not_ added to this set. This is
840 * the responsibility of the calling function.
841 * The resulting basic set has all meaning about the dimensions removed.
842 * In particular, dimensions that correspond to existential variables
843 * in bmap and that are found to be fixed are not removed.
845 static struct isl_basic_set *equalities_in_underlying_set(
846 struct isl_basic_map *bmap)
848 struct isl_mat *T1 = NULL;
849 struct isl_mat *T2 = NULL;
850 struct isl_basic_set *bset = NULL;
851 struct isl_basic_set *hull = NULL;
853 bset = isl_basic_map_underlying_set(bmap);
854 if (!bset)
855 return NULL;
856 if (bset->n_eq)
857 bset = isl_basic_set_remove_equalities(bset, &T1, &T2);
858 if (!bset)
859 goto error;
861 hull = uset_affine_hull(bset);
862 if (!T2)
863 return hull;
865 if (!hull) {
866 isl_mat_free(T1);
867 isl_mat_free(T2);
868 } else {
869 struct isl_vec *sample = isl_vec_copy(hull->sample);
870 if (sample && sample->size > 0)
871 sample = isl_mat_vec_product(T1, sample);
872 else
873 isl_mat_free(T1);
874 hull = isl_basic_set_preimage(hull, T2);
875 if (hull) {
876 isl_vec_free(hull->sample);
877 hull->sample = sample;
878 } else
879 isl_vec_free(sample);
882 return hull;
883 error:
884 isl_mat_free(T1);
885 isl_mat_free(T2);
886 isl_basic_set_free(bset);
887 isl_basic_set_free(hull);
888 return NULL;
891 /* Detect and make explicit all equalities satisfied by the (integer)
892 * points in bmap.
894 __isl_give isl_basic_map *isl_basic_map_detect_equalities(
895 __isl_take isl_basic_map *bmap)
897 int i, j;
898 isl_size total;
899 struct isl_basic_set *hull = NULL;
901 if (!bmap)
902 return NULL;
903 if (bmap->n_ineq == 0)
904 return bmap;
905 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
906 return bmap;
907 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_ALL_EQUALITIES))
908 return bmap;
909 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
910 return isl_basic_map_implicit_equalities(bmap);
912 hull = equalities_in_underlying_set(isl_basic_map_copy(bmap));
913 if (!hull)
914 goto error;
915 if (ISL_F_ISSET(hull, ISL_BASIC_SET_EMPTY)) {
916 isl_basic_set_free(hull);
917 return isl_basic_map_set_to_empty(bmap);
919 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim), 0,
920 hull->n_eq, 0);
921 total = isl_basic_set_dim(hull, isl_dim_all);
922 if (total < 0)
923 goto error;
924 for (i = 0; i < hull->n_eq; ++i) {
925 j = isl_basic_map_alloc_equality(bmap);
926 if (j < 0)
927 goto error;
928 isl_seq_cpy(bmap->eq[j], hull->eq[i], 1 + total);
930 isl_vec_free(bmap->sample);
931 bmap->sample = isl_vec_copy(hull->sample);
932 isl_basic_set_free(hull);
933 ISL_F_SET(bmap, ISL_BASIC_MAP_NO_IMPLICIT | ISL_BASIC_MAP_ALL_EQUALITIES);
934 bmap = isl_basic_map_simplify(bmap);
935 return isl_basic_map_finalize(bmap);
936 error:
937 isl_basic_set_free(hull);
938 isl_basic_map_free(bmap);
939 return NULL;
942 __isl_give isl_basic_set *isl_basic_set_detect_equalities(
943 __isl_take isl_basic_set *bset)
945 return bset_from_bmap(
946 isl_basic_map_detect_equalities(bset_to_bmap(bset)));
949 __isl_give isl_map *isl_map_detect_equalities(__isl_take isl_map *map)
951 return isl_map_inline_foreach_basic_map(map,
952 &isl_basic_map_detect_equalities);
955 __isl_give isl_set *isl_set_detect_equalities(__isl_take isl_set *set)
957 return set_from_map(isl_map_detect_equalities(set_to_map(set)));
960 /* Return the superset of "bmap" described by the equalities
961 * satisfied by "bmap" that are already known.
963 __isl_give isl_basic_map *isl_basic_map_plain_affine_hull(
964 __isl_take isl_basic_map *bmap)
966 bmap = isl_basic_map_cow(bmap);
967 if (bmap)
968 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
969 bmap = isl_basic_map_finalize(bmap);
970 return bmap;
973 /* Return the superset of "bset" described by the equalities
974 * satisfied by "bset" that are already known.
976 __isl_give isl_basic_set *isl_basic_set_plain_affine_hull(
977 __isl_take isl_basic_set *bset)
979 return isl_basic_map_plain_affine_hull(bset);
982 /* After computing the rational affine hull (by detecting the implicit
983 * equalities), we compute the additional equalities satisfied by
984 * the integer points (if any) and add the original equalities back in.
986 __isl_give isl_basic_map *isl_basic_map_affine_hull(
987 __isl_take isl_basic_map *bmap)
989 bmap = isl_basic_map_detect_equalities(bmap);
990 bmap = isl_basic_map_plain_affine_hull(bmap);
991 return bmap;
994 struct isl_basic_set *isl_basic_set_affine_hull(struct isl_basic_set *bset)
996 return bset_from_bmap(isl_basic_map_affine_hull(bset_to_bmap(bset)));
999 /* Given a rational affine matrix "M", add stride constraints to "bmap"
1000 * that ensure that
1002 * M(x)
1004 * is an integer vector. The variables x include all the variables
1005 * of "bmap" except the unknown divs.
1007 * If d is the common denominator of M, then we need to impose that
1009 * d M(x) = 0 mod d
1011 * or
1013 * exists alpha : d M(x) = d alpha
1015 * This function is similar to add_strides in isl_morph.c
1017 static __isl_give isl_basic_map *add_strides(__isl_take isl_basic_map *bmap,
1018 __isl_keep isl_mat *M, int n_known)
1020 int i, div, k;
1021 isl_int gcd;
1023 if (isl_int_is_one(M->row[0][0]))
1024 return bmap;
1026 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
1027 M->n_row - 1, M->n_row - 1, 0);
1029 isl_int_init(gcd);
1030 for (i = 1; i < M->n_row; ++i) {
1031 isl_seq_gcd(M->row[i], M->n_col, &gcd);
1032 if (isl_int_is_divisible_by(gcd, M->row[0][0]))
1033 continue;
1034 div = isl_basic_map_alloc_div(bmap);
1035 if (div < 0)
1036 goto error;
1037 isl_int_set_si(bmap->div[div][0], 0);
1038 k = isl_basic_map_alloc_equality(bmap);
1039 if (k < 0)
1040 goto error;
1041 isl_seq_cpy(bmap->eq[k], M->row[i], M->n_col);
1042 isl_seq_clr(bmap->eq[k] + M->n_col, bmap->n_div - n_known);
1043 isl_int_set(bmap->eq[k][M->n_col - n_known + div],
1044 M->row[0][0]);
1046 isl_int_clear(gcd);
1048 return bmap;
1049 error:
1050 isl_int_clear(gcd);
1051 isl_basic_map_free(bmap);
1052 return NULL;
1055 /* If there are any equalities that involve (multiple) unknown divs,
1056 * then extract the stride information encoded by those equalities
1057 * and make it explicitly available in "bmap".
1059 * We first sort the divs so that the unknown divs appear last and
1060 * then we count how many equalities involve these divs.
1062 * Let these equalities be of the form
1064 * A(x) + B y = 0
1066 * where y represents the unknown divs and x the remaining variables.
1067 * Let [H 0] be the Hermite Normal Form of B, i.e.,
1069 * B = [H 0] Q
1071 * Then x is a solution of the equalities iff
1073 * H^-1 A(x) (= - [I 0] Q y)
1075 * is an integer vector. Let d be the common denominator of H^-1.
1076 * We impose
1078 * d H^-1 A(x) = d alpha
1080 * in add_strides, with alpha fresh existentially quantified variables.
1082 static __isl_give isl_basic_map *isl_basic_map_make_strides_explicit(
1083 __isl_take isl_basic_map *bmap)
1085 isl_bool known;
1086 int n_known;
1087 int n, n_col;
1088 isl_size v_div;
1089 isl_ctx *ctx;
1090 isl_mat *A, *B, *M;
1092 known = isl_basic_map_divs_known(bmap);
1093 if (known < 0)
1094 return isl_basic_map_free(bmap);
1095 if (known)
1096 return bmap;
1097 bmap = isl_basic_map_sort_divs(bmap);
1098 bmap = isl_basic_map_gauss(bmap, NULL);
1099 if (!bmap)
1100 return NULL;
1102 for (n_known = 0; n_known < bmap->n_div; ++n_known)
1103 if (isl_int_is_zero(bmap->div[n_known][0]))
1104 break;
1105 ctx = isl_basic_map_get_ctx(bmap);
1106 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
1107 if (v_div < 0)
1108 return isl_basic_map_free(bmap);
1109 for (n = 0; n < bmap->n_eq; ++n)
1110 if (isl_seq_first_non_zero(bmap->eq[n] + 1 + v_div + n_known,
1111 bmap->n_div - n_known) == -1)
1112 break;
1113 if (n == 0)
1114 return bmap;
1115 B = isl_mat_sub_alloc6(ctx, bmap->eq, 0, n, 0, 1 + v_div + n_known);
1116 n_col = bmap->n_div - n_known;
1117 A = isl_mat_sub_alloc6(ctx, bmap->eq, 0, n, 1 + v_div + n_known, n_col);
1118 A = isl_mat_left_hermite(A, 0, NULL, NULL);
1119 A = isl_mat_drop_cols(A, n, n_col - n);
1120 A = isl_mat_lin_to_aff(A);
1121 A = isl_mat_right_inverse(A);
1122 B = isl_mat_insert_zero_rows(B, 0, 1);
1123 B = isl_mat_set_element_si(B, 0, 0, 1);
1124 M = isl_mat_product(A, B);
1125 if (!M)
1126 return isl_basic_map_free(bmap);
1127 bmap = add_strides(bmap, M, n_known);
1128 bmap = isl_basic_map_gauss(bmap, NULL);
1129 isl_mat_free(M);
1131 return bmap;
1134 /* Compute the affine hull of each basic map in "map" separately
1135 * and make all stride information explicit so that we can remove
1136 * all unknown divs without losing this information.
1137 * The result is also guaranteed to be gaussed.
1139 * In simple cases where a div is determined by an equality,
1140 * calling isl_basic_map_gauss is enough to make the stride information
1141 * explicit, as it will derive an explicit representation for the div
1142 * from the equality. If, however, the stride information
1143 * is encoded through multiple unknown divs then we need to make
1144 * some extra effort in isl_basic_map_make_strides_explicit.
1146 static __isl_give isl_map *isl_map_local_affine_hull(__isl_take isl_map *map)
1148 int i;
1150 map = isl_map_cow(map);
1151 if (!map)
1152 return NULL;
1154 for (i = 0; i < map->n; ++i) {
1155 map->p[i] = isl_basic_map_affine_hull(map->p[i]);
1156 map->p[i] = isl_basic_map_gauss(map->p[i], NULL);
1157 map->p[i] = isl_basic_map_make_strides_explicit(map->p[i]);
1158 if (!map->p[i])
1159 return isl_map_free(map);
1162 return map;
1165 static __isl_give isl_set *isl_set_local_affine_hull(__isl_take isl_set *set)
1167 return isl_map_local_affine_hull(set);
1170 /* Return an empty basic map living in the same space as "map".
1172 static __isl_give isl_basic_map *replace_map_by_empty_basic_map(
1173 __isl_take isl_map *map)
1175 isl_space *space;
1177 space = isl_map_get_space(map);
1178 isl_map_free(map);
1179 return isl_basic_map_empty(space);
1182 /* Compute the affine hull of "map".
1184 * We first compute the affine hull of each basic map separately.
1185 * Then we align the divs and recompute the affine hulls of the basic
1186 * maps since some of them may now have extra divs.
1187 * In order to avoid performing parametric integer programming to
1188 * compute explicit expressions for the divs, possible leading to
1189 * an explosion in the number of basic maps, we first drop all unknown
1190 * divs before aligning the divs. Note that isl_map_local_affine_hull tries
1191 * to make sure that all stride information is explicitly available
1192 * in terms of known divs. This involves calling isl_basic_set_gauss,
1193 * which is also needed because affine_hull assumes its input has been gaussed,
1194 * while isl_map_affine_hull may be called on input that has not been gaussed,
1195 * in particular from initial_facet_constraint.
1196 * Similarly, align_divs may reorder some divs so that we need to
1197 * gauss the result again.
1198 * Finally, we combine the individual affine hulls into a single
1199 * affine hull.
1201 __isl_give isl_basic_map *isl_map_affine_hull(__isl_take isl_map *map)
1203 struct isl_basic_map *model = NULL;
1204 struct isl_basic_map *hull = NULL;
1205 struct isl_set *set;
1206 isl_basic_set *bset;
1208 map = isl_map_detect_equalities(map);
1209 map = isl_map_local_affine_hull(map);
1210 map = isl_map_remove_empty_parts(map);
1211 map = isl_map_remove_unknown_divs(map);
1212 map = isl_map_align_divs_internal(map);
1214 if (!map)
1215 return NULL;
1217 if (map->n == 0)
1218 return replace_map_by_empty_basic_map(map);
1220 model = isl_basic_map_copy(map->p[0]);
1221 set = isl_map_underlying_set(map);
1222 set = isl_set_cow(set);
1223 set = isl_set_local_affine_hull(set);
1224 if (!set)
1225 goto error;
1227 while (set->n > 1)
1228 set->p[0] = affine_hull(set->p[0], set->p[--set->n]);
1230 bset = isl_basic_set_copy(set->p[0]);
1231 hull = isl_basic_map_overlying_set(bset, model);
1232 isl_set_free(set);
1233 hull = isl_basic_map_simplify(hull);
1234 return isl_basic_map_finalize(hull);
1235 error:
1236 isl_basic_map_free(model);
1237 isl_set_free(set);
1238 return NULL;
1241 struct isl_basic_set *isl_set_affine_hull(struct isl_set *set)
1243 return bset_from_bmap(isl_map_affine_hull(set_to_map(set)));