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