isl_tab: keep track of isl_basic_map instead of isl_basic_set
[platform/upstream/isl.git] / isl_affine_hull.c
1 #include "isl_ctx.h"
2 #include "isl_seq.h"
3 #include "isl_set.h"
4 #include "isl_lp.h"
5 #include "isl_map.h"
6 #include "isl_map_private.h"
7 #include "isl_equalities.h"
8 #include "isl_sample.h"
9 #include "isl_tab.h"
10
11 struct isl_basic_map *isl_basic_map_implicit_equalities(
12                                                 struct isl_basic_map *bmap)
13 {
14         struct isl_tab *tab;
15
16         if (!bmap)
17                 return bmap;
18
19         bmap = isl_basic_map_gauss(bmap, NULL);
20         if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
21                 return bmap;
22         if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NO_IMPLICIT))
23                 return bmap;
24         if (bmap->n_ineq <= 1)
25                 return bmap;
26
27         tab = isl_tab_from_basic_map(bmap);
28         tab = isl_tab_detect_implicit_equalities(tab);
29         bmap = isl_basic_map_update_from_tab(bmap, tab);
30         isl_tab_free(tab);
31         bmap = isl_basic_map_gauss(bmap, NULL);
32         ISL_F_SET(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
33         return bmap;
34 }
35
36 struct isl_basic_set *isl_basic_set_implicit_equalities(
37                                                 struct isl_basic_set *bset)
38 {
39         return (struct isl_basic_set *)
40                 isl_basic_map_implicit_equalities((struct isl_basic_map*)bset);
41 }
42
43 struct isl_map *isl_map_implicit_equalities(struct isl_map *map)
44 {
45         int i;
46
47         if (!map)
48                 return map;
49
50         for (i = 0; i < map->n; ++i) {
51                 map->p[i] = isl_basic_map_implicit_equalities(map->p[i]);
52                 if (!map->p[i])
53                         goto error;
54         }
55
56         return map;
57 error:
58         isl_map_free(map);
59         return NULL;
60 }
61
62 /* Make eq[row][col] of both bmaps equal so we can add the row
63  * add the column to the common matrix.
64  * Note that because of the echelon form, the columns of row row
65  * after column col are zero.
66  */
67 static void set_common_multiple(
68         struct isl_basic_set *bset1, struct isl_basic_set *bset2,
69         unsigned row, unsigned col)
70 {
71         isl_int m, c;
72
73         if (isl_int_eq(bset1->eq[row][col], bset2->eq[row][col]))
74                 return;
75
76         isl_int_init(c);
77         isl_int_init(m);
78         isl_int_lcm(m, bset1->eq[row][col], bset2->eq[row][col]);
79         isl_int_divexact(c, m, bset1->eq[row][col]);
80         isl_seq_scale(bset1->eq[row], bset1->eq[row], c, col+1);
81         isl_int_divexact(c, m, bset2->eq[row][col]);
82         isl_seq_scale(bset2->eq[row], bset2->eq[row], c, col+1);
83         isl_int_clear(c);
84         isl_int_clear(m);
85 }
86
87 /* Delete a given equality, moving all the following equalities one up.
88  */
89 static void delete_row(struct isl_basic_set *bset, unsigned row)
90 {
91         isl_int *t;
92         int r;
93
94         t = bset->eq[row];
95         bset->n_eq--;
96         for (r = row; r < bset->n_eq; ++r)
97                 bset->eq[r] = bset->eq[r+1];
98         bset->eq[bset->n_eq] = t;
99 }
100
101 /* Make first row entries in column col of bset1 identical to
102  * those of bset2, using the fact that entry bset1->eq[row][col]=a
103  * is non-zero.  Initially, these elements of bset1 are all zero.
104  * For each row i < row, we set
105  *              A[i] = a * A[i] + B[i][col] * A[row]
106  *              B[i] = a * B[i]
107  * so that
108  *              A[i][col] = B[i][col] = a * old(B[i][col])
109  */
110 static void construct_column(
111         struct isl_basic_set *bset1, struct isl_basic_set *bset2,
112         unsigned row, unsigned col)
113 {
114         int r;
115         isl_int a;
116         isl_int b;
117         unsigned total;
118
119         isl_int_init(a);
120         isl_int_init(b);
121         total = 1 + isl_basic_set_n_dim(bset1);
122         for (r = 0; r < row; ++r) {
123                 if (isl_int_is_zero(bset2->eq[r][col]))
124                         continue;
125                 isl_int_gcd(b, bset2->eq[r][col], bset1->eq[row][col]);
126                 isl_int_divexact(a, bset1->eq[row][col], b);
127                 isl_int_divexact(b, bset2->eq[r][col], b);
128                 isl_seq_combine(bset1->eq[r], a, bset1->eq[r],
129                                               b, bset1->eq[row], total);
130                 isl_seq_scale(bset2->eq[r], bset2->eq[r], a, total);
131         }
132         isl_int_clear(a);
133         isl_int_clear(b);
134         delete_row(bset1, row);
135 }
136
137 /* Make first row entries in column col of bset1 identical to
138  * those of bset2, using only these entries of the two matrices.
139  * Let t be the last row with different entries.
140  * For each row i < t, we set
141  *      A[i] = (A[t][col]-B[t][col]) * A[i] + (B[i][col]-A[i][col) * A[t]
142  *      B[i] = (A[t][col]-B[t][col]) * B[i] + (B[i][col]-A[i][col) * B[t]
143  * so that
144  *      A[i][col] = B[i][col] = old(A[t][col]*B[i][col]-A[i][col]*B[t][col])
145  */
146 static int transform_column(
147         struct isl_basic_set *bset1, struct isl_basic_set *bset2,
148         unsigned row, unsigned col)
149 {
150         int i, t;
151         isl_int a, b, g;
152         unsigned total;
153
154         for (t = row-1; t >= 0; --t)
155                 if (isl_int_ne(bset1->eq[t][col], bset2->eq[t][col]))
156                         break;
157         if (t < 0)
158                 return 0;
159
160         total = 1 + isl_basic_set_n_dim(bset1);
161         isl_int_init(a);
162         isl_int_init(b);
163         isl_int_init(g);
164         isl_int_sub(b, bset1->eq[t][col], bset2->eq[t][col]);
165         for (i = 0; i < t; ++i) {
166                 isl_int_sub(a, bset2->eq[i][col], bset1->eq[i][col]);
167                 isl_int_gcd(g, a, b);
168                 isl_int_divexact(a, a, g);
169                 isl_int_divexact(g, b, g);
170                 isl_seq_combine(bset1->eq[i], g, bset1->eq[i], a, bset1->eq[t],
171                                 total);
172                 isl_seq_combine(bset2->eq[i], g, bset2->eq[i], a, bset2->eq[t],
173                                 total);
174         }
175         isl_int_clear(a);
176         isl_int_clear(b);
177         isl_int_clear(g);
178         delete_row(bset1, t);
179         delete_row(bset2, t);
180         return 1;
181 }
182
183 /* The implementation is based on Section 5.2 of Michael Karr,
184  * "Affine Relationships Among Variables of a Program",
185  * except that the echelon form we use starts from the last column
186  * and that we are dealing with integer coefficients.
187  */
188 static struct isl_basic_set *affine_hull(
189         struct isl_basic_set *bset1, struct isl_basic_set *bset2)
190 {
191         unsigned total;
192         int col;
193         int row;
194
195         total = 1 + isl_basic_set_n_dim(bset1);
196
197         row = 0;
198         for (col = total-1; col >= 0; --col) {
199                 int is_zero1 = row >= bset1->n_eq ||
200                         isl_int_is_zero(bset1->eq[row][col]);
201                 int is_zero2 = row >= bset2->n_eq ||
202                         isl_int_is_zero(bset2->eq[row][col]);
203                 if (!is_zero1 && !is_zero2) {
204                         set_common_multiple(bset1, bset2, row, col);
205                         ++row;
206                 } else if (!is_zero1 && is_zero2) {
207                         construct_column(bset1, bset2, row, col);
208                 } else if (is_zero1 && !is_zero2) {
209                         construct_column(bset2, bset1, row, col);
210                 } else {
211                         if (transform_column(bset1, bset2, row, col))
212                                 --row;
213                 }
214         }
215         isl_basic_set_free(bset2);
216         isl_assert(bset1->ctx, row == bset1->n_eq, goto error);
217         bset1 = isl_basic_set_normalize_constraints(bset1);
218         return bset1;
219 error:
220         isl_basic_set_free(bset1);
221         return NULL;
222 }
223
224 /* Find an integer point in the set represented by "tab"
225  * that lies outside of the equality "eq" e(x) = 0.
226  * If "up" is true, look for a point satisfying e(x) - 1 >= 0.
227  * Otherwise, look for a point satisfying -e(x) - 1 >= 0 (i.e., e(x) <= -1).
228  * The point, if found, is returned.
229  * If no point can be found, a zero-length vector is returned.
230  *
231  * Before solving an ILP problem, we first check if simply
232  * adding the normal of the constraint to one of the known
233  * integer points in the basic set represented by "tab"
234  * yields another point inside the basic set.
235  *
236  * The caller of this function ensures that the tableau is bounded or
237  * that tab->basis and tab->n_unbounded have been set appropriately.
238  */
239 static struct isl_vec *outside_point(struct isl_tab *tab, isl_int *eq, int up)
240 {
241         struct isl_ctx *ctx;
242         struct isl_vec *sample = NULL;
243         struct isl_tab_undo *snap;
244         unsigned dim;
245         int k;
246
247         if (!tab)
248                 return NULL;
249         ctx = tab->mat->ctx;
250
251         dim = tab->n_var;
252         sample = isl_vec_alloc(ctx, 1 + dim);
253         if (!sample)
254                 return NULL;
255         isl_int_set_si(sample->el[0], 1);
256         isl_seq_combine(sample->el + 1,
257                 ctx->one, tab->bmap->sample->el + 1,
258                 up ? ctx->one : ctx->negone, eq + 1, dim);
259         if (isl_basic_map_contains(tab->bmap, sample))
260                 return sample;
261         isl_vec_free(sample);
262         sample = NULL;
263
264         snap = isl_tab_snap(tab);
265
266         if (!up)
267                 isl_seq_neg(eq, eq, 1 + dim);
268         isl_int_sub_ui(eq[0], eq[0], 1);
269
270         if (isl_tab_extend_cons(tab, 1) < 0)
271                 goto error;
272         if (isl_tab_add_ineq(tab, eq) < 0)
273                 goto error;
274
275         sample = isl_tab_sample(tab);
276
277         isl_int_add_ui(eq[0], eq[0], 1);
278         if (!up)
279                 isl_seq_neg(eq, eq, 1 + dim);
280
281         if (isl_tab_rollback(tab, snap) < 0)
282                 goto error;
283
284         return sample;
285 error:
286         isl_vec_free(sample);
287         return NULL;
288 }
289
290 struct isl_basic_set *isl_basic_set_recession_cone(struct isl_basic_set *bset)
291 {
292         int i;
293
294         bset = isl_basic_set_cow(bset);
295         if (!bset)
296                 return NULL;
297         isl_assert(bset->ctx, bset->n_div == 0, goto error);
298
299         for (i = 0; i < bset->n_eq; ++i)
300                 isl_int_set_si(bset->eq[i][0], 0);
301
302         for (i = 0; i < bset->n_ineq; ++i)
303                 isl_int_set_si(bset->ineq[i][0], 0);
304
305         ISL_F_CLR(bset, ISL_BASIC_SET_NO_IMPLICIT);
306         return isl_basic_set_implicit_equalities(bset);
307 error:
308         isl_basic_set_free(bset);
309         return NULL;
310 }
311
312 /* Extend an initial (under-)approximation of the affine hull of basic
313  * set represented by the tableau "tab"
314  * by looking for points that do not satisfy one of the equalities
315  * in the current approximation and adding them to that approximation
316  * until no such points can be found any more.
317  *
318  * The caller of this function ensures that "tab" is bounded or
319  * that tab->basis and tab->n_unbounded have been set appropriately.
320  */
321 static struct isl_basic_set *extend_affine_hull(struct isl_tab *tab,
322         struct isl_basic_set *hull)
323 {
324         int i, j, k;
325         unsigned dim;
326
327         if (!tab || !hull)
328                 goto error;
329
330         dim = tab->n_var;
331
332         if (isl_tab_extend_cons(tab, 2 * dim + 1) < 0)
333                 goto error;
334
335         for (i = 0; i < dim; ++i) {
336                 struct isl_vec *sample;
337                 struct isl_basic_set *point;
338                 for (j = 0; j < hull->n_eq; ++j) {
339                         sample = outside_point(tab, hull->eq[j], 1);
340                         if (!sample)
341                                 goto error;
342                         if (sample->size > 0)
343                                 break;
344                         isl_vec_free(sample);
345                         sample = outside_point(tab, hull->eq[j], 0);
346                         if (!sample)
347                                 goto error;
348                         if (sample->size > 0)
349                                 break;
350                         isl_vec_free(sample);
351
352                         tab = isl_tab_add_eq(tab, hull->eq[j]);
353                         if (!tab)
354                                 goto error;
355                 }
356                 if (j == hull->n_eq)
357                         break;
358                 if (tab->samples)
359                         tab = isl_tab_add_sample(tab, isl_vec_copy(sample));
360                 if (!tab)
361                         goto error;
362                 point = isl_basic_set_from_vec(sample);
363                 hull = affine_hull(hull, point);
364         }
365
366         return hull;
367 error:
368         isl_basic_set_free(hull);
369         return NULL;
370 }
371
372 /* Drop all constraints in bset that involve any of the dimensions
373  * first to first+n-1.
374  */
375 static struct isl_basic_set *drop_constraints_involving
376         (struct isl_basic_set *bset, unsigned first, unsigned n)
377 {
378         int i;
379
380         if (!bset)
381                 return NULL;
382
383         bset = isl_basic_set_cow(bset);
384
385         for (i = bset->n_eq - 1; i >= 0; --i) {
386                 if (isl_seq_first_non_zero(bset->eq[i] + 1 + first, n) == -1)
387                         continue;
388                 isl_basic_set_drop_equality(bset, i);
389         }
390
391         for (i = bset->n_ineq - 1; i >= 0; --i) {
392                 if (isl_seq_first_non_zero(bset->ineq[i] + 1 + first, n) == -1)
393                         continue;
394                 isl_basic_set_drop_inequality(bset, i);
395         }
396
397         return bset;
398 }
399
400 /* Look for all equalities satisfied by the integer points in bset,
401  * which is assumed to be bounded.
402  *
403  * The equalities are obtained by successively looking for
404  * a point that is affinely independent of the points found so far.
405  * In particular, for each equality satisfied by the points so far,
406  * we check if there is any point on a hyperplane parallel to the
407  * corresponding hyperplane shifted by at least one (in either direction).
408  */
409 static struct isl_basic_set *uset_affine_hull_bounded(struct isl_basic_set *bset)
410 {
411         struct isl_vec *sample = NULL;
412         struct isl_basic_set *hull;
413         struct isl_tab *tab = NULL;
414         unsigned dim;
415
416         if (isl_basic_set_fast_is_empty(bset))
417                 return bset;
418
419         dim = isl_basic_set_n_dim(bset);
420
421         if (bset->sample && bset->sample->size == 1 + dim) {
422                 int contains = isl_basic_set_contains(bset, bset->sample);
423                 if (contains < 0)
424                         goto error;
425                 if (contains) {
426                         if (dim == 0)
427                                 return bset;
428                         sample = isl_vec_copy(bset->sample);
429                 } else {
430                         isl_vec_free(bset->sample);
431                         bset->sample = NULL;
432                 }
433         }
434
435         tab = isl_tab_from_basic_set(bset);
436         if (!tab)
437                 goto error;
438         if (tab->empty) {
439                 isl_tab_free(tab);
440                 isl_vec_free(sample);
441                 return isl_basic_set_set_to_empty(bset);
442         }
443         if (isl_tab_track_bset(tab, isl_basic_set_copy(bset)) < 0)
444                 goto error;
445
446         if (!sample) {
447                 struct isl_tab_undo *snap;
448                 snap = isl_tab_snap(tab);
449                 sample = isl_tab_sample(tab);
450                 if (isl_tab_rollback(tab, snap) < 0)
451                         goto error;
452                 isl_vec_free(tab->bmap->sample);
453                 tab->bmap->sample = isl_vec_copy(sample);
454         }
455
456         if (!sample)
457                 goto error;
458         if (sample->size == 0) {
459                 isl_tab_free(tab);
460                 isl_vec_free(sample);
461                 return isl_basic_set_set_to_empty(bset);
462         }
463
464         hull = isl_basic_set_from_vec(sample);
465
466         isl_basic_set_free(bset);
467         hull = extend_affine_hull(tab, hull);
468         isl_tab_free(tab);
469
470         return hull;
471 error:
472         isl_vec_free(sample);
473         isl_tab_free(tab);
474         isl_basic_set_free(bset);
475         return NULL;
476 }
477
478 /* Given an unbounded tableau and an integer point satisfying the tableau,
479  * construct an intial affine hull containing the recession cone
480  * shifted to the given point.
481  *
482  * The unbounded directions are taken from the last rows of the basis,
483  * which is assumed to have been initialized appropriately.
484  */
485 static __isl_give isl_basic_set *initial_hull(struct isl_tab *tab,
486         __isl_take isl_vec *vec)
487 {
488         int i;
489         int k;
490         struct isl_basic_set *bset = NULL;
491         struct isl_ctx *ctx;
492         unsigned dim;
493
494         if (!vec || !tab)
495                 return NULL;
496         ctx = vec->ctx;
497         isl_assert(ctx, vec->size != 0, goto error);
498
499         bset = isl_basic_set_alloc(ctx, 0, vec->size - 1, 0, vec->size - 1, 0);
500         if (!bset)
501                 goto error;
502         dim = isl_basic_set_n_dim(bset) - tab->n_unbounded;
503         for (i = 0; i < dim; ++i) {
504                 k = isl_basic_set_alloc_equality(bset);
505                 if (k < 0)
506                         goto error;
507                 isl_seq_cpy(bset->eq[k] + 1, tab->basis->row[1 + i] + 1,
508                             vec->size - 1);
509                 isl_seq_inner_product(bset->eq[k] + 1, vec->el +1,
510                                       vec->size - 1, &bset->eq[k][0]);
511                 isl_int_neg(bset->eq[k][0], bset->eq[k][0]);
512         }
513         bset->sample = vec;
514         bset = isl_basic_set_gauss(bset, NULL);
515
516         return bset;
517 error:
518         isl_basic_set_free(bset);
519         isl_vec_free(vec);
520         return NULL;
521 }
522
523 /* Given a tableau of a set and a tableau of the corresponding
524  * recession cone, detect and add all equalities to the tableau.
525  * If the tableau is bounded, then we can simply keep the
526  * tableau in its state after the return from extend_affine_hull.
527  * However, if the tableau is unbounded, then
528  * isl_tab_set_initial_basis_with_cone will add some additional
529  * constraints to the tableau that have to be removed again.
530  * In this case, we therefore rollback to the state before
531  * any constraints were added and then add the eqaulities back in.
532  */
533 struct isl_tab *isl_tab_detect_equalities(struct isl_tab *tab,
534         struct isl_tab *tab_cone)
535 {
536         int j;
537         struct isl_vec *sample;
538         struct isl_basic_set *hull;
539         struct isl_tab_undo *snap;
540
541         if (!tab || !tab_cone)
542                 goto error;
543
544         snap = isl_tab_snap(tab);
545
546         isl_mat_free(tab->basis);
547         tab->basis = NULL;
548
549         isl_assert(tab->mat->ctx, tab->bmap, goto error);
550         isl_assert(tab->mat->ctx, tab->samples, goto error);
551         isl_assert(tab->mat->ctx, tab->samples->n_col == 1 + tab->n_var, goto error);
552         isl_assert(tab->mat->ctx, tab->n_sample > tab->n_outside, goto error);
553
554         if (isl_tab_set_initial_basis_with_cone(tab, tab_cone) < 0)
555                 goto error;
556
557         sample = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
558         if (!sample)
559                 goto error;
560
561         isl_seq_cpy(sample->el, tab->samples->row[tab->n_outside], sample->size);
562
563         isl_vec_free(tab->bmap->sample);
564         tab->bmap->sample = isl_vec_copy(sample);
565
566         if (tab->n_unbounded == 0)
567                 hull = isl_basic_set_from_vec(isl_vec_copy(sample));
568         else
569                 hull = initial_hull(tab, isl_vec_copy(sample));
570
571         for (j = tab->n_outside + 1; j < tab->n_sample; ++j) {
572                 isl_seq_cpy(sample->el, tab->samples->row[j], sample->size);
573                 hull = affine_hull(hull,
574                                 isl_basic_set_from_vec(isl_vec_copy(sample)));
575         }
576
577         isl_vec_free(sample);
578
579         hull = extend_affine_hull(tab, hull);
580         if (!hull)
581                 goto error;
582
583         if (tab->n_unbounded == 0) {
584                 isl_basic_set_free(hull);
585                 return tab;
586         }
587
588         if (isl_tab_rollback(tab, snap) < 0)
589                 goto error;
590
591         if (hull->n_eq > tab->n_zero) {
592                 for (j = 0; j < hull->n_eq; ++j) {
593                         isl_seq_normalize(tab->mat->ctx, hull->eq[j], 1 + tab->n_var);
594                         tab = isl_tab_add_eq(tab, hull->eq[j]);
595                 }
596         }
597
598         isl_basic_set_free(hull);
599
600         return tab;
601 error:
602         isl_tab_free(tab);
603         return NULL;
604 }
605
606 /* Compute the affine hull of "bset", where "cone" is the recession cone
607  * of "bset".
608  *
609  * We first compute a unimodular transformation that puts the unbounded
610  * directions in the last dimensions.  In particular, we take a transformation
611  * that maps all equalities to equalities (in HNF) on the first dimensions.
612  * Let x be the original dimensions and y the transformed, with y_1 bounded
613  * and y_2 unbounded.
614  *
615  *             [ y_1 ]                  [ y_1 ]   [ Q_1 ]
616  *      x = U  [ y_2 ]                  [ y_2 ] = [ Q_2 ] x
617  *
618  * Let's call the input basic set S.  We compute S' = preimage(S, U)
619  * and drop the final dimensions including any constraints involving them.
620  * This results in set S''.
621  * Then we compute the affine hull A'' of S''.
622  * Let F y_1 >= g be the constraint system of A''.  In the transformed
623  * space the y_2 are unbounded, so we can add them back without any constraints,
624  * resulting in
625  *
626  *                      [ y_1 ]
627  *              [ F 0 ] [ y_2 ] >= g
628  * or
629  *                      [ Q_1 ]
630  *              [ F 0 ] [ Q_2 ] x >= g
631  * or
632  *              F Q_1 x >= g
633  *
634  * The affine hull in the original space is then obtained as
635  * A = preimage(A'', Q_1).
636  */
637 static struct isl_basic_set *affine_hull_with_cone(struct isl_basic_set *bset,
638         struct isl_basic_set *cone)
639 {
640         unsigned total;
641         unsigned cone_dim;
642         struct isl_basic_set *hull;
643         struct isl_mat *M, *U, *Q;
644
645         if (!bset || !cone)
646                 goto error;
647
648         total = isl_basic_set_total_dim(cone);
649         cone_dim = total - cone->n_eq;
650
651         M = isl_mat_sub_alloc(bset->ctx, cone->eq, 0, cone->n_eq, 1, total);
652         M = isl_mat_left_hermite(M, 0, &U, &Q);
653         if (!M)
654                 goto error;
655         isl_mat_free(M);
656
657         U = isl_mat_lin_to_aff(U);
658         bset = isl_basic_set_preimage(bset, isl_mat_copy(U));
659
660         bset = drop_constraints_involving(bset, total - cone_dim, cone_dim);
661         bset = isl_basic_set_drop_dims(bset, total - cone_dim, cone_dim);
662
663         Q = isl_mat_lin_to_aff(Q);
664         Q = isl_mat_drop_rows(Q, 1 + total - cone_dim, cone_dim);
665
666         if (bset && bset->sample && bset->sample->size == 1 + total)
667                 bset->sample = isl_mat_vec_product(isl_mat_copy(Q), bset->sample);
668
669         hull = uset_affine_hull_bounded(bset);
670
671         if (!hull)
672                 isl_mat_free(U);
673         else {
674                 struct isl_vec *sample = isl_vec_copy(hull->sample);
675                 U = isl_mat_drop_cols(U, 1 + total - cone_dim, cone_dim);
676                 if (sample && sample->size > 0)
677                         sample = isl_mat_vec_product(U, sample);
678                 else
679                         isl_mat_free(U);
680                 hull = isl_basic_set_preimage(hull, Q);
681                 isl_vec_free(hull->sample);
682                 hull->sample = sample;
683         }
684
685         isl_basic_set_free(cone);
686
687         return hull;
688 error:
689         isl_basic_set_free(bset);
690         isl_basic_set_free(cone);
691         return NULL;
692 }
693
694 /* Look for all equalities satisfied by the integer points in bset,
695  * which is assumed not to have any explicit equalities.
696  *
697  * The equalities are obtained by successively looking for
698  * a point that is affinely independent of the points found so far.
699  * In particular, for each equality satisfied by the points so far,
700  * we check if there is any point on a hyperplane parallel to the
701  * corresponding hyperplane shifted by at least one (in either direction).
702  *
703  * Before looking for any outside points, we first compute the recession
704  * cone.  The directions of this recession cone will always be part
705  * of the affine hull, so there is no need for looking for any points
706  * in these directions.
707  * In particular, if the recession cone is full-dimensional, then
708  * the affine hull is simply the whole universe.
709  */
710 static struct isl_basic_set *uset_affine_hull(struct isl_basic_set *bset)
711 {
712         struct isl_basic_set *cone;
713
714         if (isl_basic_set_fast_is_empty(bset))
715                 return bset;
716
717         cone = isl_basic_set_recession_cone(isl_basic_set_copy(bset));
718         if (!cone)
719                 goto error;
720         if (cone->n_eq == 0) {
721                 struct isl_basic_set *hull;
722                 isl_basic_set_free(cone);
723                 hull = isl_basic_set_universe_like(bset);
724                 isl_basic_set_free(bset);
725                 return hull;
726         }
727
728         if (cone->n_eq < isl_basic_set_total_dim(cone))
729                 return affine_hull_with_cone(bset, cone);
730
731         isl_basic_set_free(cone);
732         return uset_affine_hull_bounded(bset);
733 error:
734         isl_basic_set_free(bset);
735         return NULL;
736 }
737
738 /* Look for all equalities satisfied by the integer points in bmap
739  * that are independent of the equalities already explicitly available
740  * in bmap.
741  *
742  * We first remove all equalities already explicitly available,
743  * then look for additional equalities in the reduced space
744  * and then transform the result to the original space.
745  * The original equalities are _not_ added to this set.  This is
746  * the responsibility of the calling function.
747  * The resulting basic set has all meaning about the dimensions removed.
748  * In particular, dimensions that correspond to existential variables
749  * in bmap and that are found to be fixed are not removed.
750  */
751 static struct isl_basic_set *equalities_in_underlying_set(
752                                                 struct isl_basic_map *bmap)
753 {
754         struct isl_mat *T1 = NULL;
755         struct isl_mat *T2 = NULL;
756         struct isl_basic_set *bset = NULL;
757         struct isl_basic_set *hull = NULL;
758
759         bset = isl_basic_map_underlying_set(bmap);
760         if (!bset)
761                 return NULL;
762         if (bset->n_eq)
763                 bset = isl_basic_set_remove_equalities(bset, &T1, &T2);
764         if (!bset)
765                 goto error;
766
767         hull = uset_affine_hull(bset);
768         if (!T2)
769                 return hull;
770
771         if (!hull)
772                 isl_mat_free(T1);
773         else {
774                 struct isl_vec *sample = isl_vec_copy(hull->sample);
775                 if (sample && sample->size > 0)
776                         sample = isl_mat_vec_product(T1, sample);
777                 else
778                         isl_mat_free(T1);
779                 hull = isl_basic_set_preimage(hull, T2);
780                 isl_vec_free(hull->sample);
781                 hull->sample = sample;
782         }
783
784         return hull;
785 error:
786         isl_mat_free(T2);
787         isl_basic_set_free(bset);
788         isl_basic_set_free(hull);
789         return NULL;
790 }
791
792 /* Detect and make explicit all equalities satisfied by the (integer)
793  * points in bmap.
794  */
795 struct isl_basic_map *isl_basic_map_detect_equalities(
796                                                 struct isl_basic_map *bmap)
797 {
798         int i, j;
799         struct isl_basic_set *hull = NULL;
800
801         if (!bmap)
802                 return NULL;
803         if (bmap->n_ineq == 0)
804                 return bmap;
805         if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
806                 return bmap;
807         if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_ALL_EQUALITIES))
808                 return bmap;
809         if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
810                 return isl_basic_map_implicit_equalities(bmap);
811
812         hull = equalities_in_underlying_set(isl_basic_map_copy(bmap));
813         if (!hull)
814                 goto error;
815         if (ISL_F_ISSET(hull, ISL_BASIC_SET_EMPTY)) {
816                 isl_basic_set_free(hull);
817                 return isl_basic_map_set_to_empty(bmap);
818         }
819         bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim), 0,
820                                         hull->n_eq, 0);
821         for (i = 0; i < hull->n_eq; ++i) {
822                 j = isl_basic_map_alloc_equality(bmap);
823                 if (j < 0)
824                         goto error;
825                 isl_seq_cpy(bmap->eq[j], hull->eq[i],
826                                 1 + isl_basic_set_total_dim(hull));
827         }
828         isl_vec_free(bmap->sample);
829         bmap->sample = isl_vec_copy(hull->sample);
830         isl_basic_set_free(hull);
831         ISL_F_SET(bmap, ISL_BASIC_MAP_NO_IMPLICIT | ISL_BASIC_MAP_ALL_EQUALITIES);
832         bmap = isl_basic_map_simplify(bmap);
833         return isl_basic_map_finalize(bmap);
834 error:
835         isl_basic_set_free(hull);
836         isl_basic_map_free(bmap);
837         return NULL;
838 }
839
840 __isl_give isl_basic_set *isl_basic_set_detect_equalities(
841                                                 __isl_take isl_basic_set *bset)
842 {
843         return (isl_basic_set *)
844                 isl_basic_map_detect_equalities((isl_basic_map *)bset);
845 }
846
847 struct isl_map *isl_map_detect_equalities(struct isl_map *map)
848 {
849         struct isl_basic_map *bmap;
850         int i;
851
852         if (!map)
853                 return NULL;
854
855         for (i = 0; i < map->n; ++i) {
856                 bmap = isl_basic_map_copy(map->p[i]);
857                 bmap = isl_basic_map_detect_equalities(bmap);
858                 if (!bmap)
859                         goto error;
860                 isl_basic_map_free(map->p[i]);
861                 map->p[i] = bmap;
862         }
863
864         return map;
865 error:
866         isl_map_free(map);
867         return NULL;
868 }
869
870 __isl_give isl_set *isl_set_detect_equalities(__isl_take isl_set *set)
871 {
872         return (isl_set *)isl_map_detect_equalities((isl_map *)set);
873 }
874
875 /* After computing the rational affine hull (by detecting the implicit
876  * equalities), we compute the additional equalities satisfied by
877  * the integer points (if any) and add the original equalities back in.
878  */
879 struct isl_basic_map *isl_basic_map_affine_hull(struct isl_basic_map *bmap)
880 {
881         bmap = isl_basic_map_detect_equalities(bmap);
882         bmap = isl_basic_map_cow(bmap);
883         isl_basic_map_free_inequality(bmap, bmap->n_ineq);
884         return bmap;
885 }
886
887 struct isl_basic_set *isl_basic_set_affine_hull(struct isl_basic_set *bset)
888 {
889         return (struct isl_basic_set *)
890                 isl_basic_map_affine_hull((struct isl_basic_map *)bset);
891 }
892
893 struct isl_basic_map *isl_map_affine_hull(struct isl_map *map)
894 {
895         int i;
896         struct isl_basic_map *model = NULL;
897         struct isl_basic_map *hull = NULL;
898         struct isl_set *set;
899
900         if (!map)
901                 return NULL;
902
903         if (map->n == 0) {
904                 hull = isl_basic_map_empty_like_map(map);
905                 isl_map_free(map);
906                 return hull;
907         }
908
909         map = isl_map_detect_equalities(map);
910         map = isl_map_align_divs(map);
911         if (!map)
912                 return NULL;
913         model = isl_basic_map_copy(map->p[0]);
914         set = isl_map_underlying_set(map);
915         set = isl_set_cow(set);
916         if (!set)
917                 goto error;
918
919         for (i = 0; i < set->n; ++i) {
920                 set->p[i] = isl_basic_set_cow(set->p[i]);
921                 set->p[i] = isl_basic_set_affine_hull(set->p[i]);
922                 set->p[i] = isl_basic_set_gauss(set->p[i], NULL);
923                 if (!set->p[i])
924                         goto error;
925         }
926         set = isl_set_remove_empty_parts(set);
927         if (set->n == 0) {
928                 hull = isl_basic_map_empty_like(model);
929                 isl_basic_map_free(model);
930         } else {
931                 struct isl_basic_set *bset;
932                 while (set->n > 1) {
933                         set->p[0] = affine_hull(set->p[0], set->p[--set->n]);
934                         if (!set->p[0])
935                                 goto error;
936                 }
937                 bset = isl_basic_set_copy(set->p[0]);
938                 hull = isl_basic_map_overlying_set(bset, model);
939         }
940         isl_set_free(set);
941         hull = isl_basic_map_simplify(hull);
942         return isl_basic_map_finalize(hull);
943 error:
944         isl_basic_map_free(model);
945         isl_set_free(set);
946         return NULL;
947 }
948
949 struct isl_basic_set *isl_set_affine_hull(struct isl_set *set)
950 {
951         return (struct isl_basic_set *)
952                 isl_map_affine_hull((struct isl_map *)set);
953 }