isl_basic_map_normalize_constraints: avoid NULL pointer dereference
[isl.git] / isl_affine_hull.c
blobc92b95286254445c7fa1fe5966b0b8017160f1ae
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
4 * Use of this software is governed by the GNU LGPLv2.1 license
6 * Written by Sven Verdoolaege, K.U.Leuven, Departement
7 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
8 */
10 #include "isl_ctx.h"
11 #include "isl_seq.h"
12 #include "isl_set.h"
13 #include "isl_lp.h"
14 #include "isl_map.h"
15 #include "isl_map_private.h"
16 #include "isl_equalities.h"
17 #include "isl_sample.h"
18 #include "isl_tab.h"
20 struct isl_basic_map *isl_basic_map_implicit_equalities(
21 struct isl_basic_map *bmap)
23 struct isl_tab *tab;
25 if (!bmap)
26 return bmap;
28 bmap = isl_basic_map_gauss(bmap, NULL);
29 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
30 return bmap;
31 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NO_IMPLICIT))
32 return bmap;
33 if (bmap->n_ineq <= 1)
34 return bmap;
36 tab = isl_tab_from_basic_map(bmap);
37 if (isl_tab_detect_implicit_equalities(tab) < 0)
38 goto error;
39 bmap = isl_basic_map_update_from_tab(bmap, tab);
40 isl_tab_free(tab);
41 bmap = isl_basic_map_gauss(bmap, NULL);
42 ISL_F_SET(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
43 return bmap;
44 error:
45 isl_tab_free(tab);
46 isl_basic_map_free(bmap);
47 return NULL;
50 struct isl_basic_set *isl_basic_set_implicit_equalities(
51 struct isl_basic_set *bset)
53 return (struct isl_basic_set *)
54 isl_basic_map_implicit_equalities((struct isl_basic_map*)bset);
57 struct isl_map *isl_map_implicit_equalities(struct isl_map *map)
59 int i;
61 if (!map)
62 return map;
64 for (i = 0; i < map->n; ++i) {
65 map->p[i] = isl_basic_map_implicit_equalities(map->p[i]);
66 if (!map->p[i])
67 goto error;
70 return map;
71 error:
72 isl_map_free(map);
73 return NULL;
76 /* Make eq[row][col] of both bmaps equal so we can add the row
77 * add the column to the common matrix.
78 * Note that because of the echelon form, the columns of row row
79 * after column col are zero.
81 static void set_common_multiple(
82 struct isl_basic_set *bset1, struct isl_basic_set *bset2,
83 unsigned row, unsigned col)
85 isl_int m, c;
87 if (isl_int_eq(bset1->eq[row][col], bset2->eq[row][col]))
88 return;
90 isl_int_init(c);
91 isl_int_init(m);
92 isl_int_lcm(m, bset1->eq[row][col], bset2->eq[row][col]);
93 isl_int_divexact(c, m, bset1->eq[row][col]);
94 isl_seq_scale(bset1->eq[row], bset1->eq[row], c, col+1);
95 isl_int_divexact(c, m, bset2->eq[row][col]);
96 isl_seq_scale(bset2->eq[row], bset2->eq[row], c, col+1);
97 isl_int_clear(c);
98 isl_int_clear(m);
101 /* Delete a given equality, moving all the following equalities one up.
103 static void delete_row(struct isl_basic_set *bset, unsigned row)
105 isl_int *t;
106 int r;
108 t = bset->eq[row];
109 bset->n_eq--;
110 for (r = row; r < bset->n_eq; ++r)
111 bset->eq[r] = bset->eq[r+1];
112 bset->eq[bset->n_eq] = t;
115 /* Make first row entries in column col of bset1 identical to
116 * those of bset2, using the fact that entry bset1->eq[row][col]=a
117 * is non-zero. Initially, these elements of bset1 are all zero.
118 * For each row i < row, we set
119 * A[i] = a * A[i] + B[i][col] * A[row]
120 * B[i] = a * B[i]
121 * so that
122 * A[i][col] = B[i][col] = a * old(B[i][col])
124 static void construct_column(
125 struct isl_basic_set *bset1, struct isl_basic_set *bset2,
126 unsigned row, unsigned col)
128 int r;
129 isl_int a;
130 isl_int b;
131 unsigned total;
133 isl_int_init(a);
134 isl_int_init(b);
135 total = 1 + isl_basic_set_n_dim(bset1);
136 for (r = 0; r < row; ++r) {
137 if (isl_int_is_zero(bset2->eq[r][col]))
138 continue;
139 isl_int_gcd(b, bset2->eq[r][col], bset1->eq[row][col]);
140 isl_int_divexact(a, bset1->eq[row][col], b);
141 isl_int_divexact(b, bset2->eq[r][col], b);
142 isl_seq_combine(bset1->eq[r], a, bset1->eq[r],
143 b, bset1->eq[row], total);
144 isl_seq_scale(bset2->eq[r], bset2->eq[r], a, total);
146 isl_int_clear(a);
147 isl_int_clear(b);
148 delete_row(bset1, row);
151 /* Make first row entries in column col of bset1 identical to
152 * those of bset2, using only these entries of the two matrices.
153 * Let t be the last row with different entries.
154 * For each row i < t, we set
155 * A[i] = (A[t][col]-B[t][col]) * A[i] + (B[i][col]-A[i][col) * A[t]
156 * B[i] = (A[t][col]-B[t][col]) * B[i] + (B[i][col]-A[i][col) * B[t]
157 * so that
158 * A[i][col] = B[i][col] = old(A[t][col]*B[i][col]-A[i][col]*B[t][col])
160 static int transform_column(
161 struct isl_basic_set *bset1, struct isl_basic_set *bset2,
162 unsigned row, unsigned col)
164 int i, t;
165 isl_int a, b, g;
166 unsigned total;
168 for (t = row-1; t >= 0; --t)
169 if (isl_int_ne(bset1->eq[t][col], bset2->eq[t][col]))
170 break;
171 if (t < 0)
172 return 0;
174 total = 1 + isl_basic_set_n_dim(bset1);
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 total);
186 isl_seq_combine(bset2->eq[i], g, bset2->eq[i], a, bset2->eq[t],
187 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 1;
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 struct isl_basic_set *affine_hull(
203 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
205 unsigned total;
206 int col;
207 int row;
209 total = 1 + isl_basic_set_n_dim(bset1);
211 row = 0;
212 for (col = total-1; col >= 0; --col) {
213 int is_zero1 = row >= bset1->n_eq ||
214 isl_int_is_zero(bset1->eq[row][col]);
215 int is_zero2 = row >= bset2->n_eq ||
216 isl_int_is_zero(bset2->eq[row][col]);
217 if (!is_zero1 && !is_zero2) {
218 set_common_multiple(bset1, bset2, row, col);
219 ++row;
220 } else if (!is_zero1 && is_zero2) {
221 construct_column(bset1, bset2, row, col);
222 } else if (is_zero1 && !is_zero2) {
223 construct_column(bset2, bset1, row, col);
224 } else {
225 if (transform_column(bset1, bset2, row, col))
226 --row;
229 isl_basic_set_free(bset2);
230 isl_assert(bset1->ctx, row == bset1->n_eq, goto error);
231 bset1 = isl_basic_set_normalize_constraints(bset1);
232 return bset1;
233 error:
234 isl_basic_set_free(bset1);
235 return NULL;
238 /* Find an integer point in the set represented by "tab"
239 * that lies outside of the equality "eq" e(x) = 0.
240 * If "up" is true, look for a point satisfying e(x) - 1 >= 0.
241 * Otherwise, look for a point satisfying -e(x) - 1 >= 0 (i.e., e(x) <= -1).
242 * The point, if found, is returned.
243 * If no point can be found, a zero-length vector is returned.
245 * Before solving an ILP problem, we first check if simply
246 * adding the normal of the constraint to one of the known
247 * integer points in the basic set represented by "tab"
248 * yields another point inside the basic set.
250 * The caller of this function ensures that the tableau is bounded or
251 * that tab->basis and tab->n_unbounded have been set appropriately.
253 static struct isl_vec *outside_point(struct isl_tab *tab, isl_int *eq, int up)
255 struct isl_ctx *ctx;
256 struct isl_vec *sample = NULL;
257 struct isl_tab_undo *snap;
258 unsigned dim;
260 if (!tab)
261 return NULL;
262 ctx = tab->mat->ctx;
264 dim = tab->n_var;
265 sample = isl_vec_alloc(ctx, 1 + dim);
266 if (!sample)
267 return NULL;
268 isl_int_set_si(sample->el[0], 1);
269 isl_seq_combine(sample->el + 1,
270 ctx->one, tab->bmap->sample->el + 1,
271 up ? ctx->one : ctx->negone, eq + 1, dim);
272 if (isl_basic_map_contains(tab->bmap, sample))
273 return sample;
274 isl_vec_free(sample);
275 sample = NULL;
277 snap = isl_tab_snap(tab);
279 if (!up)
280 isl_seq_neg(eq, eq, 1 + dim);
281 isl_int_sub_ui(eq[0], eq[0], 1);
283 if (isl_tab_extend_cons(tab, 1) < 0)
284 goto error;
285 if (isl_tab_add_ineq(tab, eq) < 0)
286 goto error;
288 sample = isl_tab_sample(tab);
290 isl_int_add_ui(eq[0], eq[0], 1);
291 if (!up)
292 isl_seq_neg(eq, eq, 1 + dim);
294 if (isl_tab_rollback(tab, snap) < 0)
295 goto error;
297 return sample;
298 error:
299 isl_vec_free(sample);
300 return NULL;
303 struct isl_basic_set *isl_basic_set_recession_cone(struct isl_basic_set *bset)
305 int i;
307 bset = isl_basic_set_cow(bset);
308 if (!bset)
309 return NULL;
310 isl_assert(bset->ctx, bset->n_div == 0, goto error);
312 for (i = 0; i < bset->n_eq; ++i)
313 isl_int_set_si(bset->eq[i][0], 0);
315 for (i = 0; i < bset->n_ineq; ++i)
316 isl_int_set_si(bset->ineq[i][0], 0);
318 ISL_F_CLR(bset, ISL_BASIC_SET_NO_IMPLICIT);
319 return isl_basic_set_implicit_equalities(bset);
320 error:
321 isl_basic_set_free(bset);
322 return NULL;
325 __isl_give isl_set *isl_set_recession_cone(__isl_take isl_set *set)
327 int i;
329 if (!set)
330 return NULL;
331 if (set->n == 0)
332 return set;
334 set = isl_set_remove_divs(set);
335 set = isl_set_cow(set);
336 if (!set)
337 return NULL;
339 for (i = 0; i < set->n; ++i) {
340 set->p[i] = isl_basic_set_recession_cone(set->p[i]);
341 if (!set->p[i])
342 goto error;
345 return set;
346 error:
347 isl_set_free(set);
348 return NULL;
351 /* Extend an initial (under-)approximation of the affine hull of basic
352 * set represented by the tableau "tab"
353 * by looking for points that do not satisfy one of the equalities
354 * in the current approximation and adding them to that approximation
355 * until no such points can be found any more.
357 * The caller of this function ensures that "tab" is bounded or
358 * that tab->basis and tab->n_unbounded have been set appropriately.
360 static struct isl_basic_set *extend_affine_hull(struct isl_tab *tab,
361 struct isl_basic_set *hull)
363 int i, j;
364 unsigned dim;
366 if (!tab || !hull)
367 goto error;
369 dim = tab->n_var;
371 if (isl_tab_extend_cons(tab, 2 * dim + 1) < 0)
372 goto error;
374 for (i = 0; i < dim; ++i) {
375 struct isl_vec *sample;
376 struct isl_basic_set *point;
377 for (j = 0; j < hull->n_eq; ++j) {
378 sample = outside_point(tab, hull->eq[j], 1);
379 if (!sample)
380 goto error;
381 if (sample->size > 0)
382 break;
383 isl_vec_free(sample);
384 sample = outside_point(tab, hull->eq[j], 0);
385 if (!sample)
386 goto error;
387 if (sample->size > 0)
388 break;
389 isl_vec_free(sample);
391 tab = isl_tab_add_eq(tab, hull->eq[j]);
392 if (!tab)
393 goto error;
395 if (j == hull->n_eq)
396 break;
397 if (tab->samples)
398 tab = isl_tab_add_sample(tab, isl_vec_copy(sample));
399 if (!tab)
400 goto error;
401 point = isl_basic_set_from_vec(sample);
402 hull = affine_hull(hull, point);
405 return hull;
406 error:
407 isl_basic_set_free(hull);
408 return NULL;
411 /* Drop all constraints in bset that involve any of the dimensions
412 * first to first+n-1.
414 static struct isl_basic_set *drop_constraints_involving
415 (struct isl_basic_set *bset, unsigned first, unsigned n)
417 int i;
419 if (!bset)
420 return NULL;
422 bset = isl_basic_set_cow(bset);
424 for (i = bset->n_eq - 1; i >= 0; --i) {
425 if (isl_seq_first_non_zero(bset->eq[i] + 1 + first, n) == -1)
426 continue;
427 isl_basic_set_drop_equality(bset, i);
430 for (i = bset->n_ineq - 1; i >= 0; --i) {
431 if (isl_seq_first_non_zero(bset->ineq[i] + 1 + first, n) == -1)
432 continue;
433 isl_basic_set_drop_inequality(bset, i);
436 return bset;
439 /* Look for all equalities satisfied by the integer points in bset,
440 * which is assumed to be bounded.
442 * The equalities are obtained by successively looking for
443 * a point that is affinely independent of the points found so far.
444 * In particular, for each equality satisfied by the points so far,
445 * we check if there is any point on a hyperplane parallel to the
446 * corresponding hyperplane shifted by at least one (in either direction).
448 static struct isl_basic_set *uset_affine_hull_bounded(struct isl_basic_set *bset)
450 struct isl_vec *sample = NULL;
451 struct isl_basic_set *hull;
452 struct isl_tab *tab = NULL;
453 unsigned dim;
455 if (isl_basic_set_fast_is_empty(bset))
456 return bset;
458 dim = isl_basic_set_n_dim(bset);
460 if (bset->sample && bset->sample->size == 1 + dim) {
461 int contains = isl_basic_set_contains(bset, bset->sample);
462 if (contains < 0)
463 goto error;
464 if (contains) {
465 if (dim == 0)
466 return bset;
467 sample = isl_vec_copy(bset->sample);
468 } else {
469 isl_vec_free(bset->sample);
470 bset->sample = NULL;
474 tab = isl_tab_from_basic_set(bset);
475 if (!tab)
476 goto error;
477 if (tab->empty) {
478 isl_tab_free(tab);
479 isl_vec_free(sample);
480 return isl_basic_set_set_to_empty(bset);
482 if (isl_tab_track_bset(tab, isl_basic_set_copy(bset)) < 0)
483 goto error;
485 if (!sample) {
486 struct isl_tab_undo *snap;
487 snap = isl_tab_snap(tab);
488 sample = isl_tab_sample(tab);
489 if (isl_tab_rollback(tab, snap) < 0)
490 goto error;
491 isl_vec_free(tab->bmap->sample);
492 tab->bmap->sample = isl_vec_copy(sample);
495 if (!sample)
496 goto error;
497 if (sample->size == 0) {
498 isl_tab_free(tab);
499 isl_vec_free(sample);
500 return isl_basic_set_set_to_empty(bset);
503 hull = isl_basic_set_from_vec(sample);
505 isl_basic_set_free(bset);
506 hull = extend_affine_hull(tab, hull);
507 isl_tab_free(tab);
509 return hull;
510 error:
511 isl_vec_free(sample);
512 isl_tab_free(tab);
513 isl_basic_set_free(bset);
514 return NULL;
517 /* Given an unbounded tableau and an integer point satisfying the tableau,
518 * construct an intial affine hull containing the recession cone
519 * shifted to the given point.
521 * The unbounded directions are taken from the last rows of the basis,
522 * which is assumed to have been initialized appropriately.
524 static __isl_give isl_basic_set *initial_hull(struct isl_tab *tab,
525 __isl_take isl_vec *vec)
527 int i;
528 int k;
529 struct isl_basic_set *bset = NULL;
530 struct isl_ctx *ctx;
531 unsigned dim;
533 if (!vec || !tab)
534 return NULL;
535 ctx = vec->ctx;
536 isl_assert(ctx, vec->size != 0, goto error);
538 bset = isl_basic_set_alloc(ctx, 0, vec->size - 1, 0, vec->size - 1, 0);
539 if (!bset)
540 goto error;
541 dim = isl_basic_set_n_dim(bset) - tab->n_unbounded;
542 for (i = 0; i < dim; ++i) {
543 k = isl_basic_set_alloc_equality(bset);
544 if (k < 0)
545 goto error;
546 isl_seq_cpy(bset->eq[k] + 1, tab->basis->row[1 + i] + 1,
547 vec->size - 1);
548 isl_seq_inner_product(bset->eq[k] + 1, vec->el +1,
549 vec->size - 1, &bset->eq[k][0]);
550 isl_int_neg(bset->eq[k][0], bset->eq[k][0]);
552 bset->sample = vec;
553 bset = isl_basic_set_gauss(bset, NULL);
555 return bset;
556 error:
557 isl_basic_set_free(bset);
558 isl_vec_free(vec);
559 return NULL;
562 /* Given a tableau of a set and a tableau of the corresponding
563 * recession cone, detect and add all equalities to the tableau.
564 * If the tableau is bounded, then we can simply keep the
565 * tableau in its state after the return from extend_affine_hull.
566 * However, if the tableau is unbounded, then
567 * isl_tab_set_initial_basis_with_cone will add some additional
568 * constraints to the tableau that have to be removed again.
569 * In this case, we therefore rollback to the state before
570 * any constraints were added and then add the eqaulities back in.
572 struct isl_tab *isl_tab_detect_equalities(struct isl_tab *tab,
573 struct isl_tab *tab_cone)
575 int j;
576 struct isl_vec *sample;
577 struct isl_basic_set *hull;
578 struct isl_tab_undo *snap;
580 if (!tab || !tab_cone)
581 goto error;
583 snap = isl_tab_snap(tab);
585 isl_mat_free(tab->basis);
586 tab->basis = NULL;
588 isl_assert(tab->mat->ctx, tab->bmap, goto error);
589 isl_assert(tab->mat->ctx, tab->samples, goto error);
590 isl_assert(tab->mat->ctx, tab->samples->n_col == 1 + tab->n_var, goto error);
591 isl_assert(tab->mat->ctx, tab->n_sample > tab->n_outside, goto error);
593 if (isl_tab_set_initial_basis_with_cone(tab, tab_cone) < 0)
594 goto error;
596 sample = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
597 if (!sample)
598 goto error;
600 isl_seq_cpy(sample->el, tab->samples->row[tab->n_outside], sample->size);
602 isl_vec_free(tab->bmap->sample);
603 tab->bmap->sample = isl_vec_copy(sample);
605 if (tab->n_unbounded == 0)
606 hull = isl_basic_set_from_vec(isl_vec_copy(sample));
607 else
608 hull = initial_hull(tab, isl_vec_copy(sample));
610 for (j = tab->n_outside + 1; j < tab->n_sample; ++j) {
611 isl_seq_cpy(sample->el, tab->samples->row[j], sample->size);
612 hull = affine_hull(hull,
613 isl_basic_set_from_vec(isl_vec_copy(sample)));
616 isl_vec_free(sample);
618 hull = extend_affine_hull(tab, hull);
619 if (!hull)
620 goto error;
622 if (tab->n_unbounded == 0) {
623 isl_basic_set_free(hull);
624 return tab;
627 if (isl_tab_rollback(tab, snap) < 0)
628 goto error;
630 if (hull->n_eq > tab->n_zero) {
631 for (j = 0; j < hull->n_eq; ++j) {
632 isl_seq_normalize(tab->mat->ctx, hull->eq[j], 1 + tab->n_var);
633 tab = isl_tab_add_eq(tab, hull->eq[j]);
637 isl_basic_set_free(hull);
639 return tab;
640 error:
641 isl_tab_free(tab);
642 return NULL;
645 /* Compute the affine hull of "bset", where "cone" is the recession cone
646 * of "bset".
648 * We first compute a unimodular transformation that puts the unbounded
649 * directions in the last dimensions. In particular, we take a transformation
650 * that maps all equalities to equalities (in HNF) on the first dimensions.
651 * Let x be the original dimensions and y the transformed, with y_1 bounded
652 * and y_2 unbounded.
654 * [ y_1 ] [ y_1 ] [ Q_1 ]
655 * x = U [ y_2 ] [ y_2 ] = [ Q_2 ] x
657 * Let's call the input basic set S. We compute S' = preimage(S, U)
658 * and drop the final dimensions including any constraints involving them.
659 * This results in set S''.
660 * Then we compute the affine hull A'' of S''.
661 * Let F y_1 >= g be the constraint system of A''. In the transformed
662 * space the y_2 are unbounded, so we can add them back without any constraints,
663 * resulting in
665 * [ y_1 ]
666 * [ F 0 ] [ y_2 ] >= g
667 * or
668 * [ Q_1 ]
669 * [ F 0 ] [ Q_2 ] x >= g
670 * or
671 * F Q_1 x >= g
673 * The affine hull in the original space is then obtained as
674 * A = preimage(A'', Q_1).
676 static struct isl_basic_set *affine_hull_with_cone(struct isl_basic_set *bset,
677 struct isl_basic_set *cone)
679 unsigned total;
680 unsigned cone_dim;
681 struct isl_basic_set *hull;
682 struct isl_mat *M, *U, *Q;
684 if (!bset || !cone)
685 goto error;
687 total = isl_basic_set_total_dim(cone);
688 cone_dim = total - cone->n_eq;
690 M = isl_mat_sub_alloc(bset->ctx, cone->eq, 0, cone->n_eq, 1, total);
691 M = isl_mat_left_hermite(M, 0, &U, &Q);
692 if (!M)
693 goto error;
694 isl_mat_free(M);
696 U = isl_mat_lin_to_aff(U);
697 bset = isl_basic_set_preimage(bset, isl_mat_copy(U));
699 bset = drop_constraints_involving(bset, total - cone_dim, cone_dim);
700 bset = isl_basic_set_drop_dims(bset, total - cone_dim, cone_dim);
702 Q = isl_mat_lin_to_aff(Q);
703 Q = isl_mat_drop_rows(Q, 1 + total - cone_dim, cone_dim);
705 if (bset && bset->sample && bset->sample->size == 1 + total)
706 bset->sample = isl_mat_vec_product(isl_mat_copy(Q), bset->sample);
708 hull = uset_affine_hull_bounded(bset);
710 if (!hull)
711 isl_mat_free(U);
712 else {
713 struct isl_vec *sample = isl_vec_copy(hull->sample);
714 U = isl_mat_drop_cols(U, 1 + total - cone_dim, cone_dim);
715 if (sample && sample->size > 0)
716 sample = isl_mat_vec_product(U, sample);
717 else
718 isl_mat_free(U);
719 hull = isl_basic_set_preimage(hull, Q);
720 isl_vec_free(hull->sample);
721 hull->sample = sample;
724 isl_basic_set_free(cone);
726 return hull;
727 error:
728 isl_basic_set_free(bset);
729 isl_basic_set_free(cone);
730 return NULL;
733 /* Look for all equalities satisfied by the integer points in bset,
734 * which is assumed not to have any explicit equalities.
736 * The equalities are obtained by successively looking for
737 * a point that is affinely independent of the points found so far.
738 * In particular, for each equality satisfied by the points so far,
739 * we check if there is any point on a hyperplane parallel to the
740 * corresponding hyperplane shifted by at least one (in either direction).
742 * Before looking for any outside points, we first compute the recession
743 * cone. The directions of this recession cone will always be part
744 * of the affine hull, so there is no need for looking for any points
745 * in these directions.
746 * In particular, if the recession cone is full-dimensional, then
747 * the affine hull is simply the whole universe.
749 static struct isl_basic_set *uset_affine_hull(struct isl_basic_set *bset)
751 struct isl_basic_set *cone;
753 if (isl_basic_set_fast_is_empty(bset))
754 return bset;
756 cone = isl_basic_set_recession_cone(isl_basic_set_copy(bset));
757 if (!cone)
758 goto error;
759 if (cone->n_eq == 0) {
760 struct isl_basic_set *hull;
761 isl_basic_set_free(cone);
762 hull = isl_basic_set_universe_like(bset);
763 isl_basic_set_free(bset);
764 return hull;
767 if (cone->n_eq < isl_basic_set_total_dim(cone))
768 return affine_hull_with_cone(bset, cone);
770 isl_basic_set_free(cone);
771 return uset_affine_hull_bounded(bset);
772 error:
773 isl_basic_set_free(bset);
774 return NULL;
777 /* Look for all equalities satisfied by the integer points in bmap
778 * that are independent of the equalities already explicitly available
779 * in bmap.
781 * We first remove all equalities already explicitly available,
782 * then look for additional equalities in the reduced space
783 * and then transform the result to the original space.
784 * The original equalities are _not_ added to this set. This is
785 * the responsibility of the calling function.
786 * The resulting basic set has all meaning about the dimensions removed.
787 * In particular, dimensions that correspond to existential variables
788 * in bmap and that are found to be fixed are not removed.
790 static struct isl_basic_set *equalities_in_underlying_set(
791 struct isl_basic_map *bmap)
793 struct isl_mat *T1 = NULL;
794 struct isl_mat *T2 = NULL;
795 struct isl_basic_set *bset = NULL;
796 struct isl_basic_set *hull = NULL;
798 bset = isl_basic_map_underlying_set(bmap);
799 if (!bset)
800 return NULL;
801 if (bset->n_eq)
802 bset = isl_basic_set_remove_equalities(bset, &T1, &T2);
803 if (!bset)
804 goto error;
806 hull = uset_affine_hull(bset);
807 if (!T2)
808 return hull;
810 if (!hull)
811 isl_mat_free(T1);
812 else {
813 struct isl_vec *sample = isl_vec_copy(hull->sample);
814 if (sample && sample->size > 0)
815 sample = isl_mat_vec_product(T1, sample);
816 else
817 isl_mat_free(T1);
818 hull = isl_basic_set_preimage(hull, T2);
819 isl_vec_free(hull->sample);
820 hull->sample = sample;
823 return hull;
824 error:
825 isl_mat_free(T2);
826 isl_basic_set_free(bset);
827 isl_basic_set_free(hull);
828 return NULL;
831 /* Detect and make explicit all equalities satisfied by the (integer)
832 * points in bmap.
834 struct isl_basic_map *isl_basic_map_detect_equalities(
835 struct isl_basic_map *bmap)
837 int i, j;
838 struct isl_basic_set *hull = NULL;
840 if (!bmap)
841 return NULL;
842 if (bmap->n_ineq == 0)
843 return bmap;
844 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
845 return bmap;
846 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_ALL_EQUALITIES))
847 return bmap;
848 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
849 return isl_basic_map_implicit_equalities(bmap);
851 hull = equalities_in_underlying_set(isl_basic_map_copy(bmap));
852 if (!hull)
853 goto error;
854 if (ISL_F_ISSET(hull, ISL_BASIC_SET_EMPTY)) {
855 isl_basic_set_free(hull);
856 return isl_basic_map_set_to_empty(bmap);
858 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim), 0,
859 hull->n_eq, 0);
860 for (i = 0; i < hull->n_eq; ++i) {
861 j = isl_basic_map_alloc_equality(bmap);
862 if (j < 0)
863 goto error;
864 isl_seq_cpy(bmap->eq[j], hull->eq[i],
865 1 + isl_basic_set_total_dim(hull));
867 isl_vec_free(bmap->sample);
868 bmap->sample = isl_vec_copy(hull->sample);
869 isl_basic_set_free(hull);
870 ISL_F_SET(bmap, ISL_BASIC_MAP_NO_IMPLICIT | ISL_BASIC_MAP_ALL_EQUALITIES);
871 bmap = isl_basic_map_simplify(bmap);
872 return isl_basic_map_finalize(bmap);
873 error:
874 isl_basic_set_free(hull);
875 isl_basic_map_free(bmap);
876 return NULL;
879 __isl_give isl_basic_set *isl_basic_set_detect_equalities(
880 __isl_take isl_basic_set *bset)
882 return (isl_basic_set *)
883 isl_basic_map_detect_equalities((isl_basic_map *)bset);
886 struct isl_map *isl_map_detect_equalities(struct isl_map *map)
888 struct isl_basic_map *bmap;
889 int i;
891 if (!map)
892 return NULL;
894 for (i = 0; i < map->n; ++i) {
895 bmap = isl_basic_map_copy(map->p[i]);
896 bmap = isl_basic_map_detect_equalities(bmap);
897 if (!bmap)
898 goto error;
899 isl_basic_map_free(map->p[i]);
900 map->p[i] = bmap;
903 return map;
904 error:
905 isl_map_free(map);
906 return NULL;
909 __isl_give isl_set *isl_set_detect_equalities(__isl_take isl_set *set)
911 return (isl_set *)isl_map_detect_equalities((isl_map *)set);
914 /* After computing the rational affine hull (by detecting the implicit
915 * equalities), we compute the additional equalities satisfied by
916 * the integer points (if any) and add the original equalities back in.
918 struct isl_basic_map *isl_basic_map_affine_hull(struct isl_basic_map *bmap)
920 bmap = isl_basic_map_detect_equalities(bmap);
921 bmap = isl_basic_map_cow(bmap);
922 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
923 return bmap;
926 struct isl_basic_set *isl_basic_set_affine_hull(struct isl_basic_set *bset)
928 return (struct isl_basic_set *)
929 isl_basic_map_affine_hull((struct isl_basic_map *)bset);
932 struct isl_basic_map *isl_map_affine_hull(struct isl_map *map)
934 int i;
935 struct isl_basic_map *model = NULL;
936 struct isl_basic_map *hull = NULL;
937 struct isl_set *set;
939 map = isl_map_detect_equalities(map);
940 map = isl_map_align_divs(map);
942 if (!map)
943 return NULL;
945 if (map->n == 0) {
946 hull = isl_basic_map_empty_like_map(map);
947 isl_map_free(map);
948 return hull;
951 model = isl_basic_map_copy(map->p[0]);
952 set = isl_map_underlying_set(map);
953 set = isl_set_cow(set);
954 if (!set)
955 goto error;
957 for (i = 0; i < set->n; ++i) {
958 set->p[i] = isl_basic_set_cow(set->p[i]);
959 set->p[i] = isl_basic_set_affine_hull(set->p[i]);
960 set->p[i] = isl_basic_set_gauss(set->p[i], NULL);
961 if (!set->p[i])
962 goto error;
964 set = isl_set_remove_empty_parts(set);
965 if (set->n == 0) {
966 hull = isl_basic_map_empty_like(model);
967 isl_basic_map_free(model);
968 } else {
969 struct isl_basic_set *bset;
970 while (set->n > 1) {
971 set->p[0] = affine_hull(set->p[0], set->p[--set->n]);
972 if (!set->p[0])
973 goto error;
975 bset = isl_basic_set_copy(set->p[0]);
976 hull = isl_basic_map_overlying_set(bset, model);
978 isl_set_free(set);
979 hull = isl_basic_map_simplify(hull);
980 return isl_basic_map_finalize(hull);
981 error:
982 isl_basic_map_free(model);
983 isl_set_free(set);
984 return NULL;
987 struct isl_basic_set *isl_set_affine_hull(struct isl_set *set)
989 return (struct isl_basic_set *)
990 isl_map_affine_hull((struct isl_map *)set);