isl_schedule_tree.c: print_tree_band: use isl_bool for local variable
[isl.git] / isl_affine_hull.c
blob146182731c34345030252dc434a6a6b6f33eeeff
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 void 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 unsigned total;
126 isl_int_init(a);
127 isl_int_init(b);
128 total = 1 + isl_basic_set_n_dim(bset1);
129 for (r = 0; r < row; ++r) {
130 if (isl_int_is_zero(bset2->eq[r][col]))
131 continue;
132 isl_int_gcd(b, bset2->eq[r][col], bset1->eq[row][col]);
133 isl_int_divexact(a, bset1->eq[row][col], b);
134 isl_int_divexact(b, bset2->eq[r][col], b);
135 isl_seq_combine(bset1->eq[r], a, bset1->eq[r],
136 b, bset1->eq[row], total);
137 isl_seq_scale(bset2->eq[r], bset2->eq[r], a, total);
139 isl_int_clear(a);
140 isl_int_clear(b);
141 delete_row(bset1, row);
144 /* Make first row entries in column col of bset1 identical to
145 * those of bset2, using only these entries of the two matrices.
146 * Let t be the last row with different entries.
147 * For each row i < t, we set
148 * A[i] = (A[t][col]-B[t][col]) * A[i] + (B[i][col]-A[i][col) * A[t]
149 * B[i] = (A[t][col]-B[t][col]) * B[i] + (B[i][col]-A[i][col) * B[t]
150 * so that
151 * A[i][col] = B[i][col] = old(A[t][col]*B[i][col]-A[i][col]*B[t][col])
153 static int transform_column(
154 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
155 unsigned row, unsigned col)
157 int i, t;
158 isl_int a, b, g;
159 unsigned total;
161 for (t = row-1; t >= 0; --t)
162 if (isl_int_ne(bset1->eq[t][col], bset2->eq[t][col]))
163 break;
164 if (t < 0)
165 return 0;
167 total = 1 + isl_basic_set_n_dim(bset1);
168 isl_int_init(a);
169 isl_int_init(b);
170 isl_int_init(g);
171 isl_int_sub(b, bset1->eq[t][col], bset2->eq[t][col]);
172 for (i = 0; i < t; ++i) {
173 isl_int_sub(a, bset2->eq[i][col], bset1->eq[i][col]);
174 isl_int_gcd(g, a, b);
175 isl_int_divexact(a, a, g);
176 isl_int_divexact(g, b, g);
177 isl_seq_combine(bset1->eq[i], g, bset1->eq[i], a, bset1->eq[t],
178 total);
179 isl_seq_combine(bset2->eq[i], g, bset2->eq[i], a, bset2->eq[t],
180 total);
182 isl_int_clear(a);
183 isl_int_clear(b);
184 isl_int_clear(g);
185 delete_row(bset1, t);
186 delete_row(bset2, t);
187 return 1;
190 /* The implementation is based on Section 5.2 of Michael Karr,
191 * "Affine Relationships Among Variables of a Program",
192 * except that the echelon form we use starts from the last column
193 * and that we are dealing with integer coefficients.
195 static __isl_give isl_basic_set *affine_hull(
196 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
198 unsigned total;
199 int col;
200 int row;
202 if (!bset1 || !bset2)
203 goto error;
205 total = 1 + isl_basic_set_n_dim(bset1);
207 row = 0;
208 for (col = total-1; col >= 0; --col) {
209 int is_zero1 = row >= bset1->n_eq ||
210 isl_int_is_zero(bset1->eq[row][col]);
211 int is_zero2 = row >= bset2->n_eq ||
212 isl_int_is_zero(bset2->eq[row][col]);
213 if (!is_zero1 && !is_zero2) {
214 set_common_multiple(bset1, bset2, row, col);
215 ++row;
216 } else if (!is_zero1 && is_zero2) {
217 construct_column(bset1, bset2, row, col);
218 } else if (is_zero1 && !is_zero2) {
219 construct_column(bset2, bset1, row, col);
220 } else {
221 if (transform_column(bset1, bset2, row, col))
222 --row;
225 isl_assert(bset1->ctx, row == bset1->n_eq, goto error);
226 isl_basic_set_free(bset2);
227 bset1 = isl_basic_set_normalize_constraints(bset1);
228 return bset1;
229 error:
230 isl_basic_set_free(bset1);
231 isl_basic_set_free(bset2);
232 return NULL;
235 /* Find an integer point in the set represented by "tab"
236 * that lies outside of the equality "eq" e(x) = 0.
237 * If "up" is true, look for a point satisfying e(x) - 1 >= 0.
238 * Otherwise, look for a point satisfying -e(x) - 1 >= 0 (i.e., e(x) <= -1).
239 * The point, if found, is returned.
240 * If no point can be found, a zero-length vector is returned.
242 * Before solving an ILP problem, we first check if simply
243 * adding the normal of the constraint to one of the known
244 * integer points in the basic set represented by "tab"
245 * yields another point inside the basic set.
247 * The caller of this function ensures that the tableau is bounded or
248 * that tab->basis and tab->n_unbounded have been set appropriately.
250 static struct isl_vec *outside_point(struct isl_tab *tab, isl_int *eq, int up)
252 struct isl_ctx *ctx;
253 struct isl_vec *sample = NULL;
254 struct isl_tab_undo *snap;
255 unsigned dim;
257 if (!tab)
258 return NULL;
259 ctx = tab->mat->ctx;
261 dim = tab->n_var;
262 sample = isl_vec_alloc(ctx, 1 + dim);
263 if (!sample)
264 return NULL;
265 isl_int_set_si(sample->el[0], 1);
266 isl_seq_combine(sample->el + 1,
267 ctx->one, tab->bmap->sample->el + 1,
268 up ? ctx->one : ctx->negone, eq + 1, dim);
269 if (isl_basic_map_contains(tab->bmap, sample))
270 return sample;
271 isl_vec_free(sample);
272 sample = NULL;
274 snap = isl_tab_snap(tab);
276 if (!up)
277 isl_seq_neg(eq, eq, 1 + dim);
278 isl_int_sub_ui(eq[0], eq[0], 1);
280 if (isl_tab_extend_cons(tab, 1) < 0)
281 goto error;
282 if (isl_tab_add_ineq(tab, eq) < 0)
283 goto error;
285 sample = isl_tab_sample(tab);
287 isl_int_add_ui(eq[0], eq[0], 1);
288 if (!up)
289 isl_seq_neg(eq, eq, 1 + dim);
291 if (sample && isl_tab_rollback(tab, snap) < 0)
292 goto error;
294 return sample;
295 error:
296 isl_vec_free(sample);
297 return NULL;
300 __isl_give isl_basic_set *isl_basic_set_recession_cone(
301 __isl_take isl_basic_set *bset)
303 int i;
305 bset = isl_basic_set_cow(bset);
306 if (!bset)
307 return NULL;
308 isl_assert(bset->ctx, bset->n_div == 0, goto error);
310 for (i = 0; i < bset->n_eq; ++i)
311 isl_int_set_si(bset->eq[i][0], 0);
313 for (i = 0; i < bset->n_ineq; ++i)
314 isl_int_set_si(bset->ineq[i][0], 0);
316 ISL_F_CLR(bset, ISL_BASIC_SET_NO_IMPLICIT);
317 return isl_basic_set_implicit_equalities(bset);
318 error:
319 isl_basic_set_free(bset);
320 return NULL;
323 /* Move "sample" to a point that is one up (or down) from the original
324 * point in dimension "pos".
326 static void adjacent_point(__isl_keep isl_vec *sample, int pos, int up)
328 if (up)
329 isl_int_add_ui(sample->el[1 + pos], sample->el[1 + pos], 1);
330 else
331 isl_int_sub_ui(sample->el[1 + pos], sample->el[1 + pos], 1);
334 /* Check if any points that are adjacent to "sample" also belong to "bset".
335 * If so, add them to "hull" and return the updated hull.
337 * Before checking whether and adjacent point belongs to "bset", we first
338 * check whether it already belongs to "hull" as this test is typically
339 * much cheaper.
341 static __isl_give isl_basic_set *add_adjacent_points(
342 __isl_take isl_basic_set *hull, __isl_take isl_vec *sample,
343 __isl_keep isl_basic_set *bset)
345 int i, up;
346 int dim;
348 if (!sample)
349 goto error;
351 dim = isl_basic_set_dim(hull, isl_dim_set);
353 for (i = 0; i < dim; ++i) {
354 for (up = 0; up <= 1; ++up) {
355 int contains;
356 isl_basic_set *point;
358 adjacent_point(sample, i, up);
359 contains = isl_basic_set_contains(hull, sample);
360 if (contains < 0)
361 goto error;
362 if (contains) {
363 adjacent_point(sample, i, !up);
364 continue;
366 contains = isl_basic_set_contains(bset, sample);
367 if (contains < 0)
368 goto error;
369 if (contains) {
370 point = isl_basic_set_from_vec(
371 isl_vec_copy(sample));
372 hull = affine_hull(hull, point);
374 adjacent_point(sample, i, !up);
375 if (contains)
376 break;
380 isl_vec_free(sample);
382 return hull;
383 error:
384 isl_vec_free(sample);
385 isl_basic_set_free(hull);
386 return NULL;
389 /* Extend an initial (under-)approximation of the affine hull of basic
390 * set represented by the tableau "tab"
391 * by looking for points that do not satisfy one of the equalities
392 * in the current approximation and adding them to that approximation
393 * until no such points can be found any more.
395 * The caller of this function ensures that "tab" is bounded or
396 * that tab->basis and tab->n_unbounded have been set appropriately.
398 * "bset" may be either NULL or the basic set represented by "tab".
399 * If "bset" is not NULL, we check for any point we find if any
400 * of its adjacent points also belong to "bset".
402 static __isl_give isl_basic_set *extend_affine_hull(struct isl_tab *tab,
403 __isl_take isl_basic_set *hull, __isl_keep isl_basic_set *bset)
405 int i, j;
406 unsigned dim;
408 if (!tab || !hull)
409 goto error;
411 dim = tab->n_var;
413 if (isl_tab_extend_cons(tab, 2 * dim + 1) < 0)
414 goto error;
416 for (i = 0; i < dim; ++i) {
417 struct isl_vec *sample;
418 struct isl_basic_set *point;
419 for (j = 0; j < hull->n_eq; ++j) {
420 sample = outside_point(tab, hull->eq[j], 1);
421 if (!sample)
422 goto error;
423 if (sample->size > 0)
424 break;
425 isl_vec_free(sample);
426 sample = outside_point(tab, hull->eq[j], 0);
427 if (!sample)
428 goto error;
429 if (sample->size > 0)
430 break;
431 isl_vec_free(sample);
433 if (isl_tab_add_eq(tab, hull->eq[j]) < 0)
434 goto error;
436 if (j == hull->n_eq)
437 break;
438 if (tab->samples &&
439 isl_tab_add_sample(tab, isl_vec_copy(sample)) < 0)
440 hull = isl_basic_set_free(hull);
441 if (bset)
442 hull = add_adjacent_points(hull, isl_vec_copy(sample),
443 bset);
444 point = isl_basic_set_from_vec(sample);
445 hull = affine_hull(hull, point);
446 if (!hull)
447 return NULL;
450 return hull;
451 error:
452 isl_basic_set_free(hull);
453 return NULL;
456 /* Construct an initial underapproximation of the hull of "bset"
457 * from "sample" and any of its adjacent points that also belong to "bset".
459 static __isl_give isl_basic_set *initialize_hull(__isl_keep isl_basic_set *bset,
460 __isl_take isl_vec *sample)
462 isl_basic_set *hull;
464 hull = isl_basic_set_from_vec(isl_vec_copy(sample));
465 hull = add_adjacent_points(hull, sample, bset);
467 return hull;
470 /* Look for all equalities satisfied by the integer points in bset,
471 * which is assumed to be bounded.
473 * The equalities are obtained by successively looking for
474 * a point that is affinely independent of the points found so far.
475 * In particular, for each equality satisfied by the points so far,
476 * we check if there is any point on a hyperplane parallel to the
477 * corresponding hyperplane shifted by at least one (in either direction).
479 static __isl_give isl_basic_set *uset_affine_hull_bounded(
480 __isl_take isl_basic_set *bset)
482 struct isl_vec *sample = NULL;
483 struct isl_basic_set *hull;
484 struct isl_tab *tab = NULL;
485 unsigned dim;
487 if (isl_basic_set_plain_is_empty(bset))
488 return bset;
490 dim = isl_basic_set_n_dim(bset);
492 if (bset->sample && bset->sample->size == 1 + dim) {
493 int contains = isl_basic_set_contains(bset, bset->sample);
494 if (contains < 0)
495 goto error;
496 if (contains) {
497 if (dim == 0)
498 return bset;
499 sample = isl_vec_copy(bset->sample);
500 } else {
501 isl_vec_free(bset->sample);
502 bset->sample = NULL;
506 tab = isl_tab_from_basic_set(bset, 1);
507 if (!tab)
508 goto error;
509 if (tab->empty) {
510 isl_tab_free(tab);
511 isl_vec_free(sample);
512 return isl_basic_set_set_to_empty(bset);
515 if (!sample) {
516 struct isl_tab_undo *snap;
517 snap = isl_tab_snap(tab);
518 sample = isl_tab_sample(tab);
519 if (isl_tab_rollback(tab, snap) < 0)
520 goto error;
521 isl_vec_free(tab->bmap->sample);
522 tab->bmap->sample = isl_vec_copy(sample);
525 if (!sample)
526 goto error;
527 if (sample->size == 0) {
528 isl_tab_free(tab);
529 isl_vec_free(sample);
530 return isl_basic_set_set_to_empty(bset);
533 hull = initialize_hull(bset, sample);
535 hull = extend_affine_hull(tab, hull, bset);
536 isl_basic_set_free(bset);
537 isl_tab_free(tab);
539 return hull;
540 error:
541 isl_vec_free(sample);
542 isl_tab_free(tab);
543 isl_basic_set_free(bset);
544 return NULL;
547 /* Given an unbounded tableau and an integer point satisfying the tableau,
548 * construct an initial affine hull containing the recession cone
549 * shifted to the given point.
551 * The unbounded directions are taken from the last rows of the basis,
552 * which is assumed to have been initialized appropriately.
554 static __isl_give isl_basic_set *initial_hull(struct isl_tab *tab,
555 __isl_take isl_vec *vec)
557 int i;
558 int k;
559 struct isl_basic_set *bset = NULL;
560 struct isl_ctx *ctx;
561 unsigned dim;
563 if (!vec || !tab)
564 return NULL;
565 ctx = vec->ctx;
566 isl_assert(ctx, vec->size != 0, goto error);
568 bset = isl_basic_set_alloc(ctx, 0, vec->size - 1, 0, vec->size - 1, 0);
569 if (!bset)
570 goto error;
571 dim = isl_basic_set_n_dim(bset) - tab->n_unbounded;
572 for (i = 0; i < dim; ++i) {
573 k = isl_basic_set_alloc_equality(bset);
574 if (k < 0)
575 goto error;
576 isl_seq_cpy(bset->eq[k] + 1, tab->basis->row[1 + i] + 1,
577 vec->size - 1);
578 isl_seq_inner_product(bset->eq[k] + 1, vec->el +1,
579 vec->size - 1, &bset->eq[k][0]);
580 isl_int_neg(bset->eq[k][0], bset->eq[k][0]);
582 bset->sample = vec;
583 bset = isl_basic_set_gauss(bset, NULL);
585 return bset;
586 error:
587 isl_basic_set_free(bset);
588 isl_vec_free(vec);
589 return NULL;
592 /* Given a tableau of a set and a tableau of the corresponding
593 * recession cone, detect and add all equalities to the tableau.
594 * If the tableau is bounded, then we can simply keep the
595 * tableau in its state after the return from extend_affine_hull.
596 * However, if the tableau is unbounded, then
597 * isl_tab_set_initial_basis_with_cone will add some additional
598 * constraints to the tableau that have to be removed again.
599 * In this case, we therefore rollback to the state before
600 * any constraints were added and then add the equalities back in.
602 struct isl_tab *isl_tab_detect_equalities(struct isl_tab *tab,
603 struct isl_tab *tab_cone)
605 int j;
606 struct isl_vec *sample;
607 struct isl_basic_set *hull = NULL;
608 struct isl_tab_undo *snap;
610 if (!tab || !tab_cone)
611 goto error;
613 snap = isl_tab_snap(tab);
615 isl_mat_free(tab->basis);
616 tab->basis = NULL;
618 isl_assert(tab->mat->ctx, tab->bmap, goto error);
619 isl_assert(tab->mat->ctx, tab->samples, goto error);
620 isl_assert(tab->mat->ctx, tab->samples->n_col == 1 + tab->n_var, goto error);
621 isl_assert(tab->mat->ctx, tab->n_sample > tab->n_outside, goto error);
623 if (isl_tab_set_initial_basis_with_cone(tab, tab_cone) < 0)
624 goto error;
626 sample = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
627 if (!sample)
628 goto error;
630 isl_seq_cpy(sample->el, tab->samples->row[tab->n_outside], sample->size);
632 isl_vec_free(tab->bmap->sample);
633 tab->bmap->sample = isl_vec_copy(sample);
635 if (tab->n_unbounded == 0)
636 hull = isl_basic_set_from_vec(isl_vec_copy(sample));
637 else
638 hull = initial_hull(tab, isl_vec_copy(sample));
640 for (j = tab->n_outside + 1; j < tab->n_sample; ++j) {
641 isl_seq_cpy(sample->el, tab->samples->row[j], sample->size);
642 hull = affine_hull(hull,
643 isl_basic_set_from_vec(isl_vec_copy(sample)));
646 isl_vec_free(sample);
648 hull = extend_affine_hull(tab, hull, NULL);
649 if (!hull)
650 goto error;
652 if (tab->n_unbounded == 0) {
653 isl_basic_set_free(hull);
654 return tab;
657 if (isl_tab_rollback(tab, snap) < 0)
658 goto error;
660 if (hull->n_eq > tab->n_zero) {
661 for (j = 0; j < hull->n_eq; ++j) {
662 isl_seq_normalize(tab->mat->ctx, hull->eq[j], 1 + tab->n_var);
663 if (isl_tab_add_eq(tab, hull->eq[j]) < 0)
664 goto error;
668 isl_basic_set_free(hull);
670 return tab;
671 error:
672 isl_basic_set_free(hull);
673 isl_tab_free(tab);
674 return NULL;
677 /* Compute the affine hull of "bset", where "cone" is the recession cone
678 * of "bset".
680 * We first compute a unimodular transformation that puts the unbounded
681 * directions in the last dimensions. In particular, we take a transformation
682 * that maps all equalities to equalities (in HNF) on the first dimensions.
683 * Let x be the original dimensions and y the transformed, with y_1 bounded
684 * and y_2 unbounded.
686 * [ y_1 ] [ y_1 ] [ Q_1 ]
687 * x = U [ y_2 ] [ y_2 ] = [ Q_2 ] x
689 * Let's call the input basic set S. We compute S' = preimage(S, U)
690 * and drop the final dimensions including any constraints involving them.
691 * This results in set S''.
692 * Then we compute the affine hull A'' of S''.
693 * Let F y_1 >= g be the constraint system of A''. In the transformed
694 * space the y_2 are unbounded, so we can add them back without any constraints,
695 * resulting in
697 * [ y_1 ]
698 * [ F 0 ] [ y_2 ] >= g
699 * or
700 * [ Q_1 ]
701 * [ F 0 ] [ Q_2 ] x >= g
702 * or
703 * F Q_1 x >= g
705 * The affine hull in the original space is then obtained as
706 * A = preimage(A'', Q_1).
708 static struct isl_basic_set *affine_hull_with_cone(struct isl_basic_set *bset,
709 struct isl_basic_set *cone)
711 unsigned total;
712 unsigned cone_dim;
713 struct isl_basic_set *hull;
714 struct isl_mat *M, *U, *Q;
716 if (!bset || !cone)
717 goto error;
719 total = isl_basic_set_total_dim(cone);
720 cone_dim = total - cone->n_eq;
722 M = isl_mat_sub_alloc6(bset->ctx, cone->eq, 0, cone->n_eq, 1, total);
723 M = isl_mat_left_hermite(M, 0, &U, &Q);
724 if (!M)
725 goto error;
726 isl_mat_free(M);
728 U = isl_mat_lin_to_aff(U);
729 bset = isl_basic_set_preimage(bset, isl_mat_copy(U));
731 bset = isl_basic_set_drop_constraints_involving(bset, total - cone_dim,
732 cone_dim);
733 bset = isl_basic_set_drop_dims(bset, total - cone_dim, cone_dim);
735 Q = isl_mat_lin_to_aff(Q);
736 Q = isl_mat_drop_rows(Q, 1 + total - cone_dim, cone_dim);
738 if (bset && bset->sample && bset->sample->size == 1 + total)
739 bset->sample = isl_mat_vec_product(isl_mat_copy(Q), bset->sample);
741 hull = uset_affine_hull_bounded(bset);
743 if (!hull) {
744 isl_mat_free(Q);
745 isl_mat_free(U);
746 } else {
747 struct isl_vec *sample = isl_vec_copy(hull->sample);
748 U = isl_mat_drop_cols(U, 1 + total - cone_dim, cone_dim);
749 if (sample && sample->size > 0)
750 sample = isl_mat_vec_product(U, sample);
751 else
752 isl_mat_free(U);
753 hull = isl_basic_set_preimage(hull, Q);
754 if (hull) {
755 isl_vec_free(hull->sample);
756 hull->sample = sample;
757 } else
758 isl_vec_free(sample);
761 isl_basic_set_free(cone);
763 return hull;
764 error:
765 isl_basic_set_free(bset);
766 isl_basic_set_free(cone);
767 return NULL;
770 /* Look for all equalities satisfied by the integer points in bset,
771 * which is assumed not to have any explicit equalities.
773 * The equalities are obtained by successively looking for
774 * a point that is affinely independent of the points found so far.
775 * In particular, for each equality satisfied by the points so far,
776 * we check if there is any point on a hyperplane parallel to the
777 * corresponding hyperplane shifted by at least one (in either direction).
779 * Before looking for any outside points, we first compute the recession
780 * cone. The directions of this recession cone will always be part
781 * of the affine hull, so there is no need for looking for any points
782 * in these directions.
783 * In particular, if the recession cone is full-dimensional, then
784 * the affine hull is simply the whole universe.
786 static struct isl_basic_set *uset_affine_hull(struct isl_basic_set *bset)
788 struct isl_basic_set *cone;
790 if (isl_basic_set_plain_is_empty(bset))
791 return bset;
793 cone = isl_basic_set_recession_cone(isl_basic_set_copy(bset));
794 if (!cone)
795 goto error;
796 if (cone->n_eq == 0) {
797 isl_space *space;
798 space = isl_basic_set_get_space(bset);
799 isl_basic_set_free(cone);
800 isl_basic_set_free(bset);
801 return isl_basic_set_universe(space);
804 if (cone->n_eq < isl_basic_set_total_dim(cone))
805 return affine_hull_with_cone(bset, cone);
807 isl_basic_set_free(cone);
808 return uset_affine_hull_bounded(bset);
809 error:
810 isl_basic_set_free(bset);
811 return NULL;
814 /* Look for all equalities satisfied by the integer points in bmap
815 * that are independent of the equalities already explicitly available
816 * in bmap.
818 * We first remove all equalities already explicitly available,
819 * then look for additional equalities in the reduced space
820 * and then transform the result to the original space.
821 * The original equalities are _not_ added to this set. This is
822 * the responsibility of the calling function.
823 * The resulting basic set has all meaning about the dimensions removed.
824 * In particular, dimensions that correspond to existential variables
825 * in bmap and that are found to be fixed are not removed.
827 static struct isl_basic_set *equalities_in_underlying_set(
828 struct isl_basic_map *bmap)
830 struct isl_mat *T1 = NULL;
831 struct isl_mat *T2 = NULL;
832 struct isl_basic_set *bset = NULL;
833 struct isl_basic_set *hull = NULL;
835 bset = isl_basic_map_underlying_set(bmap);
836 if (!bset)
837 return NULL;
838 if (bset->n_eq)
839 bset = isl_basic_set_remove_equalities(bset, &T1, &T2);
840 if (!bset)
841 goto error;
843 hull = uset_affine_hull(bset);
844 if (!T2)
845 return hull;
847 if (!hull) {
848 isl_mat_free(T1);
849 isl_mat_free(T2);
850 } else {
851 struct isl_vec *sample = isl_vec_copy(hull->sample);
852 if (sample && sample->size > 0)
853 sample = isl_mat_vec_product(T1, sample);
854 else
855 isl_mat_free(T1);
856 hull = isl_basic_set_preimage(hull, T2);
857 if (hull) {
858 isl_vec_free(hull->sample);
859 hull->sample = sample;
860 } else
861 isl_vec_free(sample);
864 return hull;
865 error:
866 isl_mat_free(T1);
867 isl_mat_free(T2);
868 isl_basic_set_free(bset);
869 isl_basic_set_free(hull);
870 return NULL;
873 /* Detect and make explicit all equalities satisfied by the (integer)
874 * points in bmap.
876 __isl_give isl_basic_map *isl_basic_map_detect_equalities(
877 __isl_take isl_basic_map *bmap)
879 int i, j;
880 struct isl_basic_set *hull = NULL;
882 if (!bmap)
883 return NULL;
884 if (bmap->n_ineq == 0)
885 return bmap;
886 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
887 return bmap;
888 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_ALL_EQUALITIES))
889 return bmap;
890 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
891 return isl_basic_map_implicit_equalities(bmap);
893 hull = equalities_in_underlying_set(isl_basic_map_copy(bmap));
894 if (!hull)
895 goto error;
896 if (ISL_F_ISSET(hull, ISL_BASIC_SET_EMPTY)) {
897 isl_basic_set_free(hull);
898 return isl_basic_map_set_to_empty(bmap);
900 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim), 0,
901 hull->n_eq, 0);
902 for (i = 0; i < hull->n_eq; ++i) {
903 j = isl_basic_map_alloc_equality(bmap);
904 if (j < 0)
905 goto error;
906 isl_seq_cpy(bmap->eq[j], hull->eq[i],
907 1 + isl_basic_set_total_dim(hull));
909 isl_vec_free(bmap->sample);
910 bmap->sample = isl_vec_copy(hull->sample);
911 isl_basic_set_free(hull);
912 ISL_F_SET(bmap, ISL_BASIC_MAP_NO_IMPLICIT | ISL_BASIC_MAP_ALL_EQUALITIES);
913 bmap = isl_basic_map_simplify(bmap);
914 return isl_basic_map_finalize(bmap);
915 error:
916 isl_basic_set_free(hull);
917 isl_basic_map_free(bmap);
918 return NULL;
921 __isl_give isl_basic_set *isl_basic_set_detect_equalities(
922 __isl_take isl_basic_set *bset)
924 return bset_from_bmap(
925 isl_basic_map_detect_equalities(bset_to_bmap(bset)));
928 __isl_give isl_map *isl_map_detect_equalities(__isl_take isl_map *map)
930 return isl_map_inline_foreach_basic_map(map,
931 &isl_basic_map_detect_equalities);
934 __isl_give isl_set *isl_set_detect_equalities(__isl_take isl_set *set)
936 return set_from_map(isl_map_detect_equalities(set_to_map(set)));
939 /* Return the superset of "bmap" described by the equalities
940 * satisfied by "bmap" that are already known.
942 __isl_give isl_basic_map *isl_basic_map_plain_affine_hull(
943 __isl_take isl_basic_map *bmap)
945 bmap = isl_basic_map_cow(bmap);
946 if (bmap)
947 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
948 bmap = isl_basic_map_finalize(bmap);
949 return bmap;
952 /* Return the superset of "bset" described by the equalities
953 * satisfied by "bset" that are already known.
955 __isl_give isl_basic_set *isl_basic_set_plain_affine_hull(
956 __isl_take isl_basic_set *bset)
958 return isl_basic_map_plain_affine_hull(bset);
961 /* After computing the rational affine hull (by detecting the implicit
962 * equalities), we compute the additional equalities satisfied by
963 * the integer points (if any) and add the original equalities back in.
965 __isl_give isl_basic_map *isl_basic_map_affine_hull(
966 __isl_take isl_basic_map *bmap)
968 bmap = isl_basic_map_detect_equalities(bmap);
969 bmap = isl_basic_map_plain_affine_hull(bmap);
970 return bmap;
973 struct isl_basic_set *isl_basic_set_affine_hull(struct isl_basic_set *bset)
975 return bset_from_bmap(isl_basic_map_affine_hull(bset_to_bmap(bset)));
978 /* Given a rational affine matrix "M", add stride constraints to "bmap"
979 * that ensure that
981 * M(x)
983 * is an integer vector. The variables x include all the variables
984 * of "bmap" except the unknown divs.
986 * If d is the common denominator of M, then we need to impose that
988 * d M(x) = 0 mod d
990 * or
992 * exists alpha : d M(x) = d alpha
994 * This function is similar to add_strides in isl_morph.c
996 static __isl_give isl_basic_map *add_strides(__isl_take isl_basic_map *bmap,
997 __isl_keep isl_mat *M, int n_known)
999 int i, div, k;
1000 isl_int gcd;
1002 if (isl_int_is_one(M->row[0][0]))
1003 return bmap;
1005 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
1006 M->n_row - 1, M->n_row - 1, 0);
1008 isl_int_init(gcd);
1009 for (i = 1; i < M->n_row; ++i) {
1010 isl_seq_gcd(M->row[i], M->n_col, &gcd);
1011 if (isl_int_is_divisible_by(gcd, M->row[0][0]))
1012 continue;
1013 div = isl_basic_map_alloc_div(bmap);
1014 if (div < 0)
1015 goto error;
1016 isl_int_set_si(bmap->div[div][0], 0);
1017 k = isl_basic_map_alloc_equality(bmap);
1018 if (k < 0)
1019 goto error;
1020 isl_seq_cpy(bmap->eq[k], M->row[i], M->n_col);
1021 isl_seq_clr(bmap->eq[k] + M->n_col, bmap->n_div - n_known);
1022 isl_int_set(bmap->eq[k][M->n_col - n_known + div],
1023 M->row[0][0]);
1025 isl_int_clear(gcd);
1027 return bmap;
1028 error:
1029 isl_int_clear(gcd);
1030 isl_basic_map_free(bmap);
1031 return NULL;
1034 /* If there are any equalities that involve (multiple) unknown divs,
1035 * then extract the stride information encoded by those equalities
1036 * and make it explicitly available in "bmap".
1038 * We first sort the divs so that the unknown divs appear last and
1039 * then we count how many equalities involve these divs.
1041 * Let these equalities be of the form
1043 * A(x) + B y = 0
1045 * where y represents the unknown divs and x the remaining variables.
1046 * Let [H 0] be the Hermite Normal Form of B, i.e.,
1048 * B = [H 0] Q
1050 * Then x is a solution of the equalities iff
1052 * H^-1 A(x) (= - [I 0] Q y)
1054 * is an integer vector. Let d be the common denominator of H^-1.
1055 * We impose
1057 * d H^-1 A(x) = d alpha
1059 * in add_strides, with alpha fresh existentially quantified variables.
1061 static __isl_give isl_basic_map *isl_basic_map_make_strides_explicit(
1062 __isl_take isl_basic_map *bmap)
1064 int known;
1065 int n_known;
1066 int n, n_col;
1067 int total;
1068 isl_ctx *ctx;
1069 isl_mat *A, *B, *M;
1071 known = isl_basic_map_divs_known(bmap);
1072 if (known < 0)
1073 return isl_basic_map_free(bmap);
1074 if (known)
1075 return bmap;
1076 bmap = isl_basic_map_sort_divs(bmap);
1077 bmap = isl_basic_map_gauss(bmap, NULL);
1078 if (!bmap)
1079 return NULL;
1081 for (n_known = 0; n_known < bmap->n_div; ++n_known)
1082 if (isl_int_is_zero(bmap->div[n_known][0]))
1083 break;
1084 ctx = isl_basic_map_get_ctx(bmap);
1085 total = isl_space_dim(bmap->dim, isl_dim_all);
1086 for (n = 0; n < bmap->n_eq; ++n)
1087 if (isl_seq_first_non_zero(bmap->eq[n] + 1 + total + n_known,
1088 bmap->n_div - n_known) == -1)
1089 break;
1090 if (n == 0)
1091 return bmap;
1092 B = isl_mat_sub_alloc6(ctx, bmap->eq, 0, n, 0, 1 + total + n_known);
1093 n_col = bmap->n_div - n_known;
1094 A = isl_mat_sub_alloc6(ctx, bmap->eq, 0, n, 1 + total + n_known, n_col);
1095 A = isl_mat_left_hermite(A, 0, NULL, NULL);
1096 A = isl_mat_drop_cols(A, n, n_col - n);
1097 A = isl_mat_lin_to_aff(A);
1098 A = isl_mat_right_inverse(A);
1099 B = isl_mat_insert_zero_rows(B, 0, 1);
1100 B = isl_mat_set_element_si(B, 0, 0, 1);
1101 M = isl_mat_product(A, B);
1102 if (!M)
1103 return isl_basic_map_free(bmap);
1104 bmap = add_strides(bmap, M, n_known);
1105 bmap = isl_basic_map_gauss(bmap, NULL);
1106 isl_mat_free(M);
1108 return bmap;
1111 /* Compute the affine hull of each basic map in "map" separately
1112 * and make all stride information explicit so that we can remove
1113 * all unknown divs without losing this information.
1114 * The result is also guaranteed to be gaussed.
1116 * In simple cases where a div is determined by an equality,
1117 * calling isl_basic_map_gauss is enough to make the stride information
1118 * explicit, as it will derive an explicit representation for the div
1119 * from the equality. If, however, the stride information
1120 * is encoded through multiple unknown divs then we need to make
1121 * some extra effort in isl_basic_map_make_strides_explicit.
1123 static __isl_give isl_map *isl_map_local_affine_hull(__isl_take isl_map *map)
1125 int i;
1127 map = isl_map_cow(map);
1128 if (!map)
1129 return NULL;
1131 for (i = 0; i < map->n; ++i) {
1132 map->p[i] = isl_basic_map_affine_hull(map->p[i]);
1133 map->p[i] = isl_basic_map_gauss(map->p[i], NULL);
1134 map->p[i] = isl_basic_map_make_strides_explicit(map->p[i]);
1135 if (!map->p[i])
1136 return isl_map_free(map);
1139 return map;
1142 static __isl_give isl_set *isl_set_local_affine_hull(__isl_take isl_set *set)
1144 return isl_map_local_affine_hull(set);
1147 /* Return an empty basic map living in the same space as "map".
1149 static __isl_give isl_basic_map *replace_map_by_empty_basic_map(
1150 __isl_take isl_map *map)
1152 isl_space *space;
1154 space = isl_map_get_space(map);
1155 isl_map_free(map);
1156 return isl_basic_map_empty(space);
1159 /* Compute the affine hull of "map".
1161 * We first compute the affine hull of each basic map separately.
1162 * Then we align the divs and recompute the affine hulls of the basic
1163 * maps since some of them may now have extra divs.
1164 * In order to avoid performing parametric integer programming to
1165 * compute explicit expressions for the divs, possible leading to
1166 * an explosion in the number of basic maps, we first drop all unknown
1167 * divs before aligning the divs. Note that isl_map_local_affine_hull tries
1168 * to make sure that all stride information is explicitly available
1169 * in terms of known divs. This involves calling isl_basic_set_gauss,
1170 * which is also needed because affine_hull assumes its input has been gaussed,
1171 * while isl_map_affine_hull may be called on input that has not been gaussed,
1172 * in particular from initial_facet_constraint.
1173 * Similarly, align_divs may reorder some divs so that we need to
1174 * gauss the result again.
1175 * Finally, we combine the individual affine hulls into a single
1176 * affine hull.
1178 __isl_give isl_basic_map *isl_map_affine_hull(__isl_take isl_map *map)
1180 struct isl_basic_map *model = NULL;
1181 struct isl_basic_map *hull = NULL;
1182 struct isl_set *set;
1183 isl_basic_set *bset;
1185 map = isl_map_detect_equalities(map);
1186 map = isl_map_local_affine_hull(map);
1187 map = isl_map_remove_empty_parts(map);
1188 map = isl_map_remove_unknown_divs(map);
1189 map = isl_map_align_divs_internal(map);
1191 if (!map)
1192 return NULL;
1194 if (map->n == 0)
1195 return replace_map_by_empty_basic_map(map);
1197 model = isl_basic_map_copy(map->p[0]);
1198 set = isl_map_underlying_set(map);
1199 set = isl_set_cow(set);
1200 set = isl_set_local_affine_hull(set);
1201 if (!set)
1202 goto error;
1204 while (set->n > 1)
1205 set->p[0] = affine_hull(set->p[0], set->p[--set->n]);
1207 bset = isl_basic_set_copy(set->p[0]);
1208 hull = isl_basic_map_overlying_set(bset, model);
1209 isl_set_free(set);
1210 hull = isl_basic_map_simplify(hull);
1211 return isl_basic_map_finalize(hull);
1212 error:
1213 isl_basic_map_free(model);
1214 isl_set_free(set);
1215 return NULL;
1218 struct isl_basic_set *isl_set_affine_hull(struct isl_set *set)
1220 return bset_from_bmap(isl_map_affine_hull(set_to_map(set)));