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