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