rename F_ and FL_ macros to ISL_F_ and ISL_FL_ to avoid name clashes
[platform/upstream/isl.git] / isl_convex_hull.c
1 #include "isl_lp.h"
2 #include "isl_map.h"
3 #include "isl_map_private.h"
4 #include "isl_mat.h"
5 #include "isl_set.h"
6 #include "isl_seq.h"
7 #include "isl_equalities.h"
8
9 static struct isl_basic_set *uset_convex_hull_wrap(struct isl_set *set);
10
11 static void swap_ineq(struct isl_basic_map *bmap, unsigned i, unsigned j)
12 {
13         isl_int *t;
14
15         if (i != j) {
16                 t = bmap->ineq[i];
17                 bmap->ineq[i] = bmap->ineq[j];
18                 bmap->ineq[j] = t;
19         }
20 }
21
22 /* Return 1 if constraint c is redundant with respect to the constraints
23  * in bmap.  If c is a lower [upper] bound in some variable and bmap
24  * does not have a lower [upper] bound in that variable, then c cannot
25  * be redundant and we do not need solve any lp.
26  */
27 int isl_basic_map_constraint_is_redundant(struct isl_basic_map **bmap,
28         isl_int *c, isl_int *opt_n, isl_int *opt_d)
29 {
30         enum isl_lp_result res;
31         unsigned total;
32         int i, j;
33
34         if (!bmap)
35                 return -1;
36
37         total = isl_basic_map_total_dim(*bmap);
38         for (i = 0; i < total; ++i) {
39                 int sign;
40                 if (isl_int_is_zero(c[1+i]))
41                         continue;
42                 sign = isl_int_sgn(c[1+i]);
43                 for (j = 0; j < (*bmap)->n_ineq; ++j)
44                         if (sign == isl_int_sgn((*bmap)->ineq[j][1+i]))
45                                 break;
46                 if (j == (*bmap)->n_ineq)
47                         break;
48         }
49         if (i < total)
50                 return 0;
51
52         res = isl_solve_lp(*bmap, 0, c+1, (*bmap)->ctx->one, opt_n, opt_d);
53         if (res == isl_lp_unbounded)
54                 return 0;
55         if (res == isl_lp_error)
56                 return -1;
57         if (res == isl_lp_empty) {
58                 *bmap = isl_basic_map_set_to_empty(*bmap);
59                 return 0;
60         }
61         if (opt_d)
62                 isl_int_addmul(*opt_n, *opt_d, c[0]);
63         else
64                 isl_int_add(*opt_n, *opt_n, c[0]);
65         return !isl_int_is_neg(*opt_n);
66 }
67
68 int isl_basic_set_constraint_is_redundant(struct isl_basic_set **bset,
69         isl_int *c, isl_int *opt_n, isl_int *opt_d)
70 {
71         return isl_basic_map_constraint_is_redundant(
72                         (struct isl_basic_map **)bset, c, opt_n, opt_d);
73 }
74
75 /* Compute the convex hull of a basic map, by removing the redundant
76  * constraints.  If the minimal value along the normal of a constraint
77  * is the same if the constraint is removed, then the constraint is redundant.
78  *
79  * Alternatively, we could have intersected the basic map with the
80  * corresponding equality and the checked if the dimension was that
81  * of a facet.
82  */
83 struct isl_basic_map *isl_basic_map_convex_hull(struct isl_basic_map *bmap)
84 {
85         int i;
86         isl_int opt_n;
87         isl_int opt_d;
88         struct isl_ctx *ctx;
89
90         bmap = isl_basic_map_implicit_equalities(bmap);
91         if (!bmap)
92                 return NULL;
93
94         if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
95                 return bmap;
96         if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NO_REDUNDANT))
97                 return bmap;
98
99         ctx = bmap->ctx;
100         isl_int_init(opt_n);
101         isl_int_init(opt_d);
102         for (i = bmap->n_ineq-1; i >= 0; --i) {
103                 int redundant;
104                 swap_ineq(bmap, i, bmap->n_ineq-1);
105                 bmap->n_ineq--;
106                 redundant = isl_basic_map_constraint_is_redundant(&bmap,
107                                 bmap->ineq[bmap->n_ineq], &opt_n, &opt_d);
108                 if (redundant == -1)
109                         goto error;
110                 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
111                         break;
112                 bmap->n_ineq++;
113                 swap_ineq(bmap, i, bmap->n_ineq-1);
114                 if (redundant)
115                         isl_basic_map_drop_inequality(bmap, i);
116         }
117         isl_int_clear(opt_n);
118         isl_int_clear(opt_d);
119
120         ISL_F_SET(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
121         return bmap;
122 error:
123         isl_int_clear(opt_n);
124         isl_int_clear(opt_d);
125         isl_basic_map_free(bmap);
126         return NULL;
127 }
128
129 struct isl_basic_set *isl_basic_set_convex_hull(struct isl_basic_set *bset)
130 {
131         return (struct isl_basic_set *)
132                 isl_basic_map_convex_hull((struct isl_basic_map *)bset);
133 }
134
135 /* Check if the set set is bound in the direction of the affine
136  * constraint c and if so, set the constant term such that the
137  * resulting constraint is a bounding constraint for the set.
138  */
139 static int uset_is_bound(struct isl_ctx *ctx, struct isl_set *set,
140         isl_int *c, unsigned len)
141 {
142         int first;
143         int j;
144         isl_int opt;
145         isl_int opt_denom;
146
147         isl_int_init(opt);
148         isl_int_init(opt_denom);
149         first = 1;
150         for (j = 0; j < set->n; ++j) {
151                 enum isl_lp_result res;
152
153                 if (ISL_F_ISSET(set->p[j], ISL_BASIC_SET_EMPTY))
154                         continue;
155
156                 res = isl_solve_lp((struct isl_basic_map*)set->p[j],
157                                 0, c+1, ctx->one, &opt, &opt_denom);
158                 if (res == isl_lp_unbounded)
159                         break;
160                 if (res == isl_lp_error)
161                         goto error;
162                 if (res == isl_lp_empty) {
163                         set->p[j] = isl_basic_set_set_to_empty(set->p[j]);
164                         if (!set->p[j])
165                                 goto error;
166                         continue;
167                 }
168                 if (!isl_int_is_one(opt_denom))
169                         isl_seq_scale(c, c, opt_denom, len);
170                 if (first || isl_int_lt(opt, c[0]))
171                         isl_int_set(c[0], opt);
172                 first = 0;
173         }
174         isl_int_clear(opt);
175         isl_int_clear(opt_denom);
176         isl_int_neg(c[0], c[0]);
177         return j >= set->n;
178 error:
179         isl_int_clear(opt);
180         isl_int_clear(opt_denom);
181         return -1;
182 }
183
184 /* Check if "c" is a direction with both a lower bound and an upper
185  * bound in "set" that is independent of the previously found "n"
186  * bounds in "dirs".
187  * If so, add it to the list, with the negative of the lower bound
188  * in the constant position, i.e., such that c corresponds to a bounding
189  * hyperplane (but not necessarily a facet).
190  */
191 static int is_independent_bound(struct isl_ctx *ctx,
192         struct isl_set *set, isl_int *c,
193         struct isl_mat *dirs, int n)
194 {
195         int is_bound;
196         int i = 0;
197
198         isl_seq_cpy(dirs->row[n]+1, c+1, dirs->n_col-1);
199         if (n != 0) {
200                 int pos = isl_seq_first_non_zero(dirs->row[n]+1, dirs->n_col-1);
201                 if (pos < 0)
202                         return 0;
203                 for (i = 0; i < n; ++i) {
204                         int pos_i;
205                         pos_i = isl_seq_first_non_zero(dirs->row[i]+1, dirs->n_col-1);
206                         if (pos_i < pos)
207                                 continue;
208                         if (pos_i > pos)
209                                 break;
210                         isl_seq_elim(dirs->row[n]+1, dirs->row[i]+1, pos,
211                                         dirs->n_col-1, NULL);
212                         pos = isl_seq_first_non_zero(dirs->row[n]+1, dirs->n_col-1);
213                         if (pos < 0)
214                                 return 0;
215                 }
216         }
217
218         isl_seq_neg(dirs->row[n] + 1, dirs->row[n] + 1, dirs->n_col - 1);
219         is_bound = uset_is_bound(ctx, set, dirs->row[n], dirs->n_col);
220         isl_seq_neg(dirs->row[n] + 1, dirs->row[n] + 1, dirs->n_col - 1);
221         if (is_bound != 1)
222                 return is_bound;
223         is_bound = uset_is_bound(ctx, set, dirs->row[n], dirs->n_col);
224         if (is_bound != 1)
225                 return is_bound;
226         if (i < n) {
227                 int k;
228                 isl_int *t = dirs->row[n];
229                 for (k = n; k > i; --k)
230                         dirs->row[k] = dirs->row[k-1];
231                 dirs->row[i] = t;
232         }
233         return 1;
234 }
235
236 /* Compute and return a maximal set of linearly independent bounds
237  * on the set "set", based on the constraints of the basic sets
238  * in "set".
239  */
240 static struct isl_mat *independent_bounds(struct isl_ctx *ctx,
241         struct isl_set *set)
242 {
243         int i, j, n;
244         struct isl_mat *dirs = NULL;
245         unsigned dim = isl_set_n_dim(set);
246
247         dirs = isl_mat_alloc(ctx, dim, 1+dim);
248         if (!dirs)
249                 goto error;
250
251         n = 0;
252         for (i = 0; n < dim && i < set->n; ++i) {
253                 int f;
254                 struct isl_basic_set *bset = set->p[i];
255
256                 for (j = 0; n < dim && j < bset->n_eq; ++j) {
257                         f = is_independent_bound(ctx, set, bset->eq[j],
258                                                 dirs, n);
259                         if (f < 0)
260                                 goto error;
261                         if (f)
262                                 ++n;
263                 }
264                 for (j = 0; n < dim && j < bset->n_ineq; ++j) {
265                         f = is_independent_bound(ctx, set, bset->ineq[j],
266                                                 dirs, n);
267                         if (f < 0)
268                                 goto error;
269                         if (f)
270                                 ++n;
271                 }
272         }
273         dirs->n_row = n;
274         return dirs;
275 error:
276         isl_mat_free(ctx, dirs);
277         return NULL;
278 }
279
280 static struct isl_basic_set *isl_basic_set_set_rational(
281         struct isl_basic_set *bset)
282 {
283         if (!bset)
284                 return NULL;
285
286         if (ISL_F_ISSET(bset, ISL_BASIC_MAP_RATIONAL))
287                 return bset;
288
289         bset = isl_basic_set_cow(bset);
290         if (!bset)
291                 return NULL;
292
293         ISL_F_SET(bset, ISL_BASIC_MAP_RATIONAL);
294
295         return isl_basic_set_finalize(bset);
296 }
297
298 static struct isl_set *isl_set_set_rational(struct isl_set *set)
299 {
300         int i;
301
302         set = isl_set_cow(set);
303         if (!set)
304                 return NULL;
305         for (i = 0; i < set->n; ++i) {
306                 set->p[i] = isl_basic_set_set_rational(set->p[i]);
307                 if (!set->p[i])
308                         goto error;
309         }
310         return set;
311 error:
312         isl_set_free(set);
313         return NULL;
314 }
315
316 static struct isl_basic_set *isl_basic_set_add_equality(struct isl_ctx *ctx,
317         struct isl_basic_set *bset, isl_int *c)
318 {
319         int i;
320         unsigned total;
321         unsigned dim;
322
323         if (ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY))
324                 return bset;
325
326         isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
327         isl_assert(ctx, bset->n_div == 0, goto error);
328         dim = isl_basic_set_n_dim(bset);
329         bset = isl_basic_set_extend(bset, 0, dim, 0, 1, 0);
330         i = isl_basic_set_alloc_equality(bset);
331         if (i < 0)
332                 goto error;
333         isl_seq_cpy(bset->eq[i], c, 1 + dim);
334         return bset;
335 error:
336         isl_basic_set_free(bset);
337         return NULL;
338 }
339
340 static struct isl_set *isl_set_add_equality(struct isl_ctx *ctx,
341         struct isl_set *set, isl_int *c)
342 {
343         int i;
344
345         set = isl_set_cow(set);
346         if (!set)
347                 return NULL;
348         for (i = 0; i < set->n; ++i) {
349                 set->p[i] = isl_basic_set_add_equality(ctx, set->p[i], c);
350                 if (!set->p[i])
351                         goto error;
352         }
353         return set;
354 error:
355         isl_set_free(set);
356         return NULL;
357 }
358
359 /* Given a union of basic sets, construct the constraints for wrapping
360  * a facet around one of its ridges.
361  * In particular, if each of n the d-dimensional basic sets i in "set"
362  * contains the origin, satisfies the constraints x_1 >= 0 and x_2 >= 0
363  * and is defined by the constraints
364  *                                  [ 1 ]
365  *                              A_i [ x ]  >= 0
366  *
367  * then the resulting set is of dimension n*(1+d) and has as contraints
368  *
369  *                                  [ a_i ]
370  *                              A_i [ x_i ] >= 0
371  *
372  *                                    a_i   >= 0
373  *
374  *                      \sum_i x_{i,1} = 1
375  */
376 static struct isl_basic_set *wrap_constraints(struct isl_ctx *ctx,
377                                                         struct isl_set *set)
378 {
379         struct isl_basic_set *lp;
380         unsigned n_eq;
381         unsigned n_ineq;
382         int i, j, k;
383         unsigned dim, lp_dim;
384
385         if (!set)
386                 return NULL;
387
388         dim = 1 + isl_set_n_dim(set);
389         n_eq = 1;
390         n_ineq = set->n;
391         for (i = 0; i < set->n; ++i) {
392                 n_eq += set->p[i]->n_eq;
393                 n_ineq += set->p[i]->n_ineq;
394         }
395         lp = isl_basic_set_alloc(ctx, 0, dim * set->n, 0, n_eq, n_ineq);
396         if (!lp)
397                 return NULL;
398         lp_dim = isl_basic_set_n_dim(lp);
399         k = isl_basic_set_alloc_equality(lp);
400         isl_int_set_si(lp->eq[k][0], -1);
401         for (i = 0; i < set->n; ++i) {
402                 isl_int_set_si(lp->eq[k][1+dim*i], 0);
403                 isl_int_set_si(lp->eq[k][1+dim*i+1], 1);
404                 isl_seq_clr(lp->eq[k]+1+dim*i+2, dim-2);
405         }
406         for (i = 0; i < set->n; ++i) {
407                 k = isl_basic_set_alloc_inequality(lp);
408                 isl_seq_clr(lp->ineq[k], 1+lp_dim);
409                 isl_int_set_si(lp->ineq[k][1+dim*i], 1);
410
411                 for (j = 0; j < set->p[i]->n_eq; ++j) {
412                         k = isl_basic_set_alloc_equality(lp);
413                         isl_seq_clr(lp->eq[k], 1+dim*i);
414                         isl_seq_cpy(lp->eq[k]+1+dim*i, set->p[i]->eq[j], dim);
415                         isl_seq_clr(lp->eq[k]+1+dim*(i+1), dim*(set->n-i-1));
416                 }
417
418                 for (j = 0; j < set->p[i]->n_ineq; ++j) {
419                         k = isl_basic_set_alloc_inequality(lp);
420                         isl_seq_clr(lp->ineq[k], 1+dim*i);
421                         isl_seq_cpy(lp->ineq[k]+1+dim*i, set->p[i]->ineq[j], dim);
422                         isl_seq_clr(lp->ineq[k]+1+dim*(i+1), dim*(set->n-i-1));
423                 }
424         }
425         return lp;
426 }
427
428 /* Given a facet "facet" of the convex hull of "set" and a facet "ridge"
429  * of that facet, compute the other facet of the convex hull that contains
430  * the ridge.
431  *
432  * We first transform the set such that the facet constraint becomes
433  *
434  *                      x_1 >= 0
435  *
436  * I.e., the facet lies in
437  *
438  *                      x_1 = 0
439  *
440  * and on that facet, the constraint that defines the ridge is
441  *
442  *                      x_2 >= 0
443  *
444  * (This transformation is not strictly needed, all that is needed is
445  * that the ridge contains the origin.)
446  *
447  * Since the ridge contains the origin, the cone of the convex hull
448  * will be of the form
449  *
450  *                      x_1 >= 0
451  *                      x_2 >= a x_1
452  *
453  * with this second constraint defining the new facet.
454  * The constant a is obtained by settting x_1 in the cone of the
455  * convex hull to 1 and minimizing x_2.
456  * Now, each element in the cone of the convex hull is the sum
457  * of elements in the cones of the basic sets.
458  * If a_i is the dilation factor of basic set i, then the problem
459  * we need to solve is
460  *
461  *                      min \sum_i x_{i,2}
462  *                      st
463  *                              \sum_i x_{i,1} = 1
464  *                                  a_i   >= 0
465  *                                [ a_i ]
466  *                              A [ x_i ] >= 0
467  *
468  * with
469  *                                  [  1  ]
470  *                              A_i [ x_i ] >= 0
471  *
472  * the constraints of each (transformed) basic set.
473  * If a = n/d, then the constraint defining the new facet (in the transformed
474  * space) is
475  *
476  *                      -n x_1 + d x_2 >= 0
477  *
478  * In the original space, we need to take the same combination of the
479  * corresponding constraints "facet" and "ridge".
480  *
481  * If a = -infty = "-1/0", then we just return the original facet constraint.
482  * This means that the facet is unbounded, but has a bounded intersection
483  * with the union of sets.
484  */
485 static isl_int *wrap_facet(struct isl_ctx *ctx, struct isl_set *set,
486         isl_int *facet, isl_int *ridge)
487 {
488         int i;
489         struct isl_mat *T = NULL;
490         struct isl_basic_set *lp = NULL;
491         struct isl_vec *obj;
492         enum isl_lp_result res;
493         isl_int num, den;
494         unsigned dim;
495
496         set = isl_set_copy(set);
497
498         dim = 1 + isl_set_n_dim(set);
499         T = isl_mat_alloc(ctx, 3, dim);
500         if (!T)
501                 goto error;
502         isl_int_set_si(T->row[0][0], 1);
503         isl_seq_clr(T->row[0]+1, dim - 1);
504         isl_seq_cpy(T->row[1], facet, dim);
505         isl_seq_cpy(T->row[2], ridge, dim);
506         T = isl_mat_right_inverse(ctx, T);
507         set = isl_set_preimage(ctx, set, T);
508         T = NULL;
509         if (!set)
510                 goto error;
511         lp = wrap_constraints(ctx, set);
512         obj = isl_vec_alloc(ctx, dim*set->n);
513         if (!obj)
514                 goto error;
515         for (i = 0; i < set->n; ++i) {
516                 isl_seq_clr(obj->block.data+dim*i, 2);
517                 isl_int_set_si(obj->block.data[dim*i+2], 1);
518                 isl_seq_clr(obj->block.data+dim*i+3, dim-3);
519         }
520         isl_int_init(num);
521         isl_int_init(den);
522         res = isl_solve_lp((struct isl_basic_map *)lp, 0,
523                                         obj->block.data, ctx->one, &num, &den);
524         if (res == isl_lp_ok) {
525                 isl_int_neg(num, num);
526                 isl_seq_combine(facet, num, facet, den, ridge, dim);
527         }
528         isl_int_clear(num);
529         isl_int_clear(den);
530         isl_vec_free(ctx, obj);
531         isl_basic_set_free(lp);
532         isl_set_free(set);
533         isl_assert(ctx, res == isl_lp_ok || res == isl_lp_unbounded, 
534                    return NULL);
535         return facet;
536 error:
537         isl_basic_set_free(lp);
538         isl_mat_free(ctx, T);
539         isl_set_free(set);
540         return NULL;
541 }
542
543 /* Given a set of d linearly independent bounding constraints of the
544  * convex hull of "set", compute the constraint of a facet of "set".
545  *
546  * We first compute the intersection with the first bounding hyperplane
547  * and remove the component corresponding to this hyperplane from
548  * other bounds (in homogeneous space).
549  * We then wrap around one of the remaining bounding constraints
550  * and continue the process until all bounding constraints have been
551  * taken into account.
552  * The resulting linear combination of the bounding constraints will
553  * correspond to a facet of the convex hull.
554  */
555 static struct isl_mat *initial_facet_constraint(struct isl_ctx *ctx,
556         struct isl_set *set, struct isl_mat *bounds)
557 {
558         struct isl_set *slice = NULL;
559         struct isl_basic_set *face = NULL;
560         struct isl_mat *m, *U, *Q;
561         int i;
562         unsigned dim = isl_set_n_dim(set);
563
564         isl_assert(ctx, set->n > 0, goto error);
565         isl_assert(ctx, bounds->n_row == dim, goto error);
566
567         while (bounds->n_row > 1) {
568                 slice = isl_set_copy(set);
569                 slice = isl_set_add_equality(ctx, slice, bounds->row[0]);
570                 face = isl_set_affine_hull(slice);
571                 if (!face)
572                         goto error;
573                 if (face->n_eq == 1) {
574                         isl_basic_set_free(face);
575                         break;
576                 }
577                 m = isl_mat_alloc(ctx, 1 + face->n_eq, 1 + dim);
578                 if (!m)
579                         goto error;
580                 isl_int_set_si(m->row[0][0], 1);
581                 isl_seq_clr(m->row[0]+1, dim);
582                 for (i = 0; i < face->n_eq; ++i)
583                         isl_seq_cpy(m->row[1 + i], face->eq[i], 1 + dim);
584                 U = isl_mat_right_inverse(ctx, m);
585                 Q = isl_mat_right_inverse(ctx, isl_mat_copy(ctx, U));
586                 U = isl_mat_drop_cols(ctx, U, 1 + face->n_eq,
587                                                 dim - face->n_eq);
588                 Q = isl_mat_drop_rows(ctx, Q, 1 + face->n_eq,
589                                                 dim - face->n_eq);
590                 U = isl_mat_drop_cols(ctx, U, 0, 1);
591                 Q = isl_mat_drop_rows(ctx, Q, 0, 1);
592                 bounds = isl_mat_product(ctx, bounds, U);
593                 bounds = isl_mat_product(ctx, bounds, Q);
594                 while (isl_seq_first_non_zero(bounds->row[bounds->n_row-1],
595                                               bounds->n_col) == -1) {
596                         bounds->n_row--;
597                         isl_assert(ctx, bounds->n_row > 1, goto error);
598                 }
599                 if (!wrap_facet(ctx, set, bounds->row[0],
600                                           bounds->row[bounds->n_row-1]))
601                         goto error;
602                 isl_basic_set_free(face);
603                 bounds->n_row--;
604         }
605         return bounds;
606 error:
607         isl_basic_set_free(face);
608         isl_mat_free(ctx, bounds);
609         return NULL;
610 }
611
612 /* Given the bounding constraint "c" of a facet of the convex hull of "set",
613  * compute a hyperplane description of the facet, i.e., compute the facets
614  * of the facet.
615  *
616  * We compute an affine transformation that transforms the constraint
617  *
618  *                        [ 1 ]
619  *                      c [ x ] = 0
620  *
621  * to the constraint
622  *
623  *                         z_1  = 0
624  *
625  * by computing the right inverse U of a matrix that starts with the rows
626  *
627  *                      [ 1 0 ]
628  *                      [  c  ]
629  *
630  * Then
631  *                      [ 1 ]     [ 1 ]
632  *                      [ x ] = U [ z ]
633  * and
634  *                      [ 1 ]     [ 1 ]
635  *                      [ z ] = Q [ x ]
636  *
637  * with Q = U^{-1}
638  * Since z_1 is zero, we can drop this variable as well as the corresponding
639  * column of U to obtain
640  *
641  *                      [ 1 ]      [ 1  ]
642  *                      [ x ] = U' [ z' ]
643  * and
644  *                      [ 1  ]      [ 1 ]
645  *                      [ z' ] = Q' [ x ]
646  *
647  * with Q' equal to Q, but without the corresponding row.
648  * After computing the facets of the facet in the z' space,
649  * we convert them back to the x space through Q.
650  */
651 static struct isl_basic_set *compute_facet(struct isl_ctx *ctx,
652         struct isl_set *set, isl_int *c)
653 {
654         struct isl_mat *m, *U, *Q;
655         struct isl_basic_set *facet;
656         unsigned dim;
657
658         set = isl_set_copy(set);
659         dim = isl_set_n_dim(set);
660         m = isl_mat_alloc(ctx, 2, 1 + dim);
661         if (!m)
662                 goto error;
663         isl_int_set_si(m->row[0][0], 1);
664         isl_seq_clr(m->row[0]+1, dim);
665         isl_seq_cpy(m->row[1], c, 1+dim);
666         U = isl_mat_right_inverse(ctx, m);
667         Q = isl_mat_right_inverse(ctx, isl_mat_copy(ctx, U));
668         U = isl_mat_drop_cols(ctx, U, 1, 1);
669         Q = isl_mat_drop_rows(ctx, Q, 1, 1);
670         set = isl_set_preimage(ctx, set, U);
671         facet = uset_convex_hull_wrap(set);
672         facet = isl_basic_set_preimage(ctx, facet, Q);
673         return facet;
674 error:
675         isl_set_free(set);
676         return NULL;
677 }
678
679 /* Given an initial facet constraint, compute the remaining facets.
680  * We do this by running through all facets found so far and computing
681  * the adjacent facets through wrapping, adding those facets that we
682  * hadn't already found before.
683  *
684  * This function can still be significantly optimized by checking which of
685  * the facets of the basic sets are also facets of the convex hull and
686  * using all the facets so far to help in constructing the facets of the
687  * facets
688  * and/or
689  * using the technique in section "3.1 Ridge Generation" of
690  * "Extended Convex Hull" by Fukuda et al.
691  */
692 static struct isl_basic_set *extend(struct isl_ctx *ctx, struct isl_set *set,
693         struct isl_mat *initial)
694 {
695         int i, j, f;
696         int k;
697         struct isl_basic_set *hull = NULL;
698         struct isl_basic_set *facet = NULL;
699         unsigned n_ineq;
700         unsigned total;
701         unsigned dim;
702
703         isl_assert(ctx, set->n > 0, goto error);
704
705         n_ineq = 1;
706         for (i = 0; i < set->n; ++i) {
707                 n_ineq += set->p[i]->n_eq;
708                 n_ineq += set->p[i]->n_ineq;
709         }
710         dim = isl_set_n_dim(set);
711         isl_assert(ctx, 1 + dim == initial->n_col, goto error);
712         hull = isl_basic_set_alloc(ctx, 0, dim, 0, 0, n_ineq);
713         hull = isl_basic_set_set_rational(hull);
714         if (!hull)
715                 goto error;
716         k = isl_basic_set_alloc_inequality(hull);
717         if (k < 0)
718                 goto error;
719         isl_seq_cpy(hull->ineq[k], initial->row[0], initial->n_col);
720         for (i = 0; i < hull->n_ineq; ++i) {
721                 facet = compute_facet(ctx, set, hull->ineq[i]);
722                 if (!facet)
723                         goto error;
724                 if (facet->n_ineq + hull->n_ineq > n_ineq) {
725                         hull = isl_basic_set_extend(hull,
726                                 0, dim, 0, 0, facet->n_ineq);
727                         n_ineq = hull->n_ineq + facet->n_ineq;
728                 }
729                 for (j = 0; j < facet->n_ineq; ++j) {
730                         k = isl_basic_set_alloc_inequality(hull);
731                         if (k < 0)
732                                 goto error;
733                         isl_seq_cpy(hull->ineq[k], hull->ineq[i], 1+dim);
734                         if (!wrap_facet(ctx, set, hull->ineq[k], facet->ineq[j]))
735                                 goto error;
736                         for (f = 0; f < k; ++f)
737                                 if (isl_seq_eq(hull->ineq[f], hull->ineq[k],
738                                                 1+dim))
739                                         break;
740                         if (f < k)
741                                 isl_basic_set_free_inequality(hull, 1);
742                 }
743                 isl_basic_set_free(facet);
744         }
745         hull = isl_basic_set_simplify(hull);
746         hull = isl_basic_set_finalize(hull);
747         return hull;
748 error:
749         isl_basic_set_free(facet);
750         isl_basic_set_free(hull);
751         return NULL;
752 }
753
754 /* Special case for computing the convex hull of a one dimensional set.
755  * We simply collect the lower and upper bounds of each basic set
756  * and the biggest of those.
757  */
758 static struct isl_basic_set *convex_hull_1d(struct isl_ctx *ctx,
759         struct isl_set *set)
760 {
761         struct isl_mat *c = NULL;
762         isl_int *lower = NULL;
763         isl_int *upper = NULL;
764         int i, j, k;
765         isl_int a, b;
766         struct isl_basic_set *hull;
767
768         for (i = 0; i < set->n; ++i) {
769                 set->p[i] = isl_basic_set_simplify(set->p[i]);
770                 if (!set->p[i])
771                         goto error;
772         }
773         set = isl_set_remove_empty_parts(set);
774         if (!set)
775                 goto error;
776         isl_assert(ctx, set->n > 0, goto error);
777         c = isl_mat_alloc(ctx, 2, 2);
778         if (!c)
779                 goto error;
780
781         if (set->p[0]->n_eq > 0) {
782                 isl_assert(ctx, set->p[0]->n_eq == 1, goto error);
783                 lower = c->row[0];
784                 upper = c->row[1];
785                 if (isl_int_is_pos(set->p[0]->eq[0][1])) {
786                         isl_seq_cpy(lower, set->p[0]->eq[0], 2);
787                         isl_seq_neg(upper, set->p[0]->eq[0], 2);
788                 } else {
789                         isl_seq_neg(lower, set->p[0]->eq[0], 2);
790                         isl_seq_cpy(upper, set->p[0]->eq[0], 2);
791                 }
792         } else {
793                 for (j = 0; j < set->p[0]->n_ineq; ++j) {
794                         if (isl_int_is_pos(set->p[0]->ineq[j][1])) {
795                                 lower = c->row[0];
796                                 isl_seq_cpy(lower, set->p[0]->ineq[j], 2);
797                         } else {
798                                 upper = c->row[1];
799                                 isl_seq_cpy(upper, set->p[0]->ineq[j], 2);
800                         }
801                 }
802         }
803
804         isl_int_init(a);
805         isl_int_init(b);
806         for (i = 0; i < set->n; ++i) {
807                 struct isl_basic_set *bset = set->p[i];
808                 int has_lower = 0;
809                 int has_upper = 0;
810
811                 for (j = 0; j < bset->n_eq; ++j) {
812                         has_lower = 1;
813                         has_upper = 1;
814                         if (lower) {
815                                 isl_int_mul(a, lower[0], bset->eq[j][1]);
816                                 isl_int_mul(b, lower[1], bset->eq[j][0]);
817                                 if (isl_int_lt(a, b) && isl_int_is_pos(bset->eq[j][1]))
818                                         isl_seq_cpy(lower, bset->eq[j], 2);
819                                 if (isl_int_gt(a, b) && isl_int_is_neg(bset->eq[j][1]))
820                                         isl_seq_neg(lower, bset->eq[j], 2);
821                         }
822                         if (upper) {
823                                 isl_int_mul(a, upper[0], bset->eq[j][1]);
824                                 isl_int_mul(b, upper[1], bset->eq[j][0]);
825                                 if (isl_int_lt(a, b) && isl_int_is_pos(bset->eq[j][1]))
826                                         isl_seq_neg(upper, bset->eq[j], 2);
827                                 if (isl_int_gt(a, b) && isl_int_is_neg(bset->eq[j][1]))
828                                         isl_seq_cpy(upper, bset->eq[j], 2);
829                         }
830                 }
831                 for (j = 0; j < bset->n_ineq; ++j) {
832                         if (isl_int_is_pos(bset->ineq[j][1]))
833                                 has_lower = 1;
834                         if (isl_int_is_neg(bset->ineq[j][1]))
835                                 has_upper = 1;
836                         if (lower && isl_int_is_pos(bset->ineq[j][1])) {
837                                 isl_int_mul(a, lower[0], bset->ineq[j][1]);
838                                 isl_int_mul(b, lower[1], bset->ineq[j][0]);
839                                 if (isl_int_lt(a, b))
840                                         isl_seq_cpy(lower, bset->ineq[j], 2);
841                         }
842                         if (upper && isl_int_is_neg(bset->ineq[j][1])) {
843                                 isl_int_mul(a, upper[0], bset->ineq[j][1]);
844                                 isl_int_mul(b, upper[1], bset->ineq[j][0]);
845                                 if (isl_int_gt(a, b))
846                                         isl_seq_cpy(upper, bset->ineq[j], 2);
847                         }
848                 }
849                 if (!has_lower)
850                         lower = NULL;
851                 if (!has_upper)
852                         upper = NULL;
853         }
854         isl_int_clear(a);
855         isl_int_clear(b);
856
857         hull = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
858         hull = isl_basic_set_set_rational(hull);
859         if (!hull)
860                 goto error;
861         if (lower) {
862                 k = isl_basic_set_alloc_inequality(hull);
863                 isl_seq_cpy(hull->ineq[k], lower, 2);
864         }
865         if (upper) {
866                 k = isl_basic_set_alloc_inequality(hull);
867                 isl_seq_cpy(hull->ineq[k], upper, 2);
868         }
869         hull = isl_basic_set_finalize(hull);
870         isl_set_free(set);
871         isl_mat_free(ctx, c);
872         return hull;
873 error:
874         isl_set_free(set);
875         isl_mat_free(ctx, c);
876         return NULL;
877 }
878
879 /* Project out final n dimensions using Fourier-Motzkin */
880 static struct isl_set *set_project_out(struct isl_ctx *ctx,
881         struct isl_set *set, unsigned n)
882 {
883         return isl_set_remove_dims(set, isl_set_n_dim(set) - n, n);
884 }
885
886 static struct isl_basic_set *convex_hull_0d(struct isl_set *set)
887 {
888         struct isl_basic_set *convex_hull;
889
890         if (!set)
891                 return NULL;
892
893         if (isl_set_is_empty(set))
894                 convex_hull = isl_basic_set_empty(isl_dim_copy(set->dim));
895         else
896                 convex_hull = isl_basic_set_universe(isl_dim_copy(set->dim));
897         isl_set_free(set);
898         return convex_hull;
899 }
900
901 /* Compute the convex hull of a pair of basic sets without any parameters or
902  * integer divisions using Fourier-Motzkin elimination.
903  * The convex hull is the set of all points that can be written as
904  * the sum of points from both basic sets (in homogeneous coordinates).
905  * We set up the constraints in a space with dimensions for each of
906  * the three sets and then project out the dimensions corresponding
907  * to the two original basic sets, retaining only those corresponding
908  * to the convex hull.
909  */
910 static struct isl_basic_set *convex_hull_pair(struct isl_basic_set *bset1,
911         struct isl_basic_set *bset2)
912 {
913         int i, j, k;
914         struct isl_basic_set *bset[2];
915         struct isl_basic_set *hull = NULL;
916         unsigned dim;
917
918         if (!bset1 || !bset2)
919                 goto error;
920
921         dim = isl_basic_set_n_dim(bset1);
922         hull = isl_basic_set_alloc(bset1->ctx, 0, 2 + 3 * dim, 0,
923                                 1 + dim + bset1->n_eq + bset2->n_eq,
924                                 2 + bset1->n_ineq + bset2->n_ineq);
925         bset[0] = bset1;
926         bset[1] = bset2;
927         for (i = 0; i < 2; ++i) {
928                 for (j = 0; j < bset[i]->n_eq; ++j) {
929                         k = isl_basic_set_alloc_equality(hull);
930                         if (k < 0)
931                                 goto error;
932                         isl_seq_clr(hull->eq[k], (i+1) * (1+dim));
933                         isl_seq_clr(hull->eq[k]+(i+2)*(1+dim), (1-i)*(1+dim));
934                         isl_seq_cpy(hull->eq[k]+(i+1)*(1+dim), bset[i]->eq[j],
935                                         1+dim);
936                 }
937                 for (j = 0; j < bset[i]->n_ineq; ++j) {
938                         k = isl_basic_set_alloc_inequality(hull);
939                         if (k < 0)
940                                 goto error;
941                         isl_seq_clr(hull->ineq[k], (i+1) * (1+dim));
942                         isl_seq_clr(hull->ineq[k]+(i+2)*(1+dim), (1-i)*(1+dim));
943                         isl_seq_cpy(hull->ineq[k]+(i+1)*(1+dim),
944                                         bset[i]->ineq[j], 1+dim);
945                 }
946                 k = isl_basic_set_alloc_inequality(hull);
947                 if (k < 0)
948                         goto error;
949                 isl_seq_clr(hull->ineq[k], 1+2+3*dim);
950                 isl_int_set_si(hull->ineq[k][(i+1)*(1+dim)], 1);
951         }
952         for (j = 0; j < 1+dim; ++j) {
953                 k = isl_basic_set_alloc_equality(hull);
954                 if (k < 0)
955                         goto error;
956                 isl_seq_clr(hull->eq[k], 1+2+3*dim);
957                 isl_int_set_si(hull->eq[k][j], -1);
958                 isl_int_set_si(hull->eq[k][1+dim+j], 1);
959                 isl_int_set_si(hull->eq[k][2*(1+dim)+j], 1);
960         }
961         hull = isl_basic_set_set_rational(hull);
962         hull = isl_basic_set_remove_dims(hull, dim, 2*(1+dim));
963         hull = isl_basic_set_convex_hull(hull);
964         isl_basic_set_free(bset1);
965         isl_basic_set_free(bset2);
966         return hull;
967 error:
968         isl_basic_set_free(bset1);
969         isl_basic_set_free(bset2);
970         isl_basic_set_free(hull);
971         return NULL;
972 }
973
974 /* Compute the convex hull of a set without any parameters or
975  * integer divisions using Fourier-Motzkin elimination.
976  * In each step, we combined two basic sets until only one
977  * basic set is left.
978  */
979 static struct isl_basic_set *uset_convex_hull_elim(struct isl_set *set)
980 {
981         struct isl_basic_set *convex_hull = NULL;
982
983         convex_hull = isl_set_copy_basic_set(set);
984         set = isl_set_drop_basic_set(set, convex_hull);
985         if (!set)
986                 goto error;
987         while (set->n > 0) {
988                 struct isl_basic_set *t;
989                 t = isl_set_copy_basic_set(set);
990                 if (!t)
991                         goto error;
992                 set = isl_set_drop_basic_set(set, t);
993                 if (!set)
994                         goto error;
995                 convex_hull = convex_hull_pair(convex_hull, t);
996         }
997         isl_set_free(set);
998         return convex_hull;
999 error:
1000         isl_set_free(set);
1001         isl_basic_set_free(convex_hull);
1002         return NULL;
1003 }
1004
1005 static struct isl_basic_set *uset_convex_hull_wrap_with_bounds(
1006         struct isl_set *set, struct isl_mat *bounds)
1007 {
1008         struct isl_basic_set *convex_hull = NULL;
1009
1010         isl_assert(set->ctx, bounds->n_row == isl_set_n_dim(set), goto error);
1011         bounds = initial_facet_constraint(set->ctx, set, bounds);
1012         if (!bounds)
1013                 goto error;
1014         convex_hull = extend(set->ctx, set, bounds);
1015         isl_mat_free(set->ctx, bounds);
1016         isl_set_free(set);
1017
1018         return convex_hull;
1019 error:
1020         isl_set_free(set);
1021         return NULL;
1022 }
1023
1024 /* Compute the convex hull of a set without any parameters or
1025  * integer divisions.  Depending on whether the set is bounded,
1026  * we pass control to the wrapping based convex hull or
1027  * the Fourier-Motzkin elimination based convex hull.
1028  * We also handle a few special cases before checking the boundedness.
1029  */
1030 static struct isl_basic_set *uset_convex_hull(struct isl_set *set)
1031 {
1032         int i;
1033         struct isl_basic_set *convex_hull = NULL;
1034         struct isl_mat *bounds;
1035
1036         if (isl_set_n_dim(set) == 0)
1037                 return convex_hull_0d(set);
1038
1039         set = isl_set_set_rational(set);
1040
1041         if (!set)
1042                 goto error;
1043         set = isl_set_normalize(set);
1044         if (!set)
1045                 return NULL;
1046         if (set->n == 1) {
1047                 convex_hull = isl_basic_set_copy(set->p[0]);
1048                 isl_set_free(set);
1049                 return convex_hull;
1050         }
1051         if (isl_set_n_dim(set) == 1)
1052                 return convex_hull_1d(set->ctx, set);
1053
1054         bounds = independent_bounds(set->ctx, set);
1055         if (!bounds)
1056                 goto error;
1057         if (bounds->n_row == isl_set_n_dim(set))
1058                 return uset_convex_hull_wrap_with_bounds(set, bounds);
1059         isl_mat_free(set->ctx, bounds);
1060
1061         return uset_convex_hull_elim(set);
1062 error:
1063         isl_set_free(set);
1064         isl_basic_set_free(convex_hull);
1065         return NULL;
1066 }
1067
1068 /* This is the core procedure, where "set" is a "pure" set, i.e.,
1069  * without parameters or divs and where the convex hull of set is
1070  * known to be full-dimensional.
1071  */
1072 static struct isl_basic_set *uset_convex_hull_wrap(struct isl_set *set)
1073 {
1074         int i;
1075         struct isl_basic_set *convex_hull = NULL;
1076         struct isl_mat *bounds;
1077
1078         if (isl_set_n_dim(set) == 0) {
1079                 convex_hull = isl_basic_set_universe(isl_dim_copy(set->dim));
1080                 isl_set_free(set);
1081                 convex_hull = isl_basic_set_set_rational(convex_hull);
1082                 return convex_hull;
1083         }
1084
1085         set = isl_set_set_rational(set);
1086
1087         if (!set)
1088                 goto error;
1089         set = isl_set_normalize(set);
1090         if (!set)
1091                 goto error;
1092         if (set->n == 1) {
1093                 convex_hull = isl_basic_set_copy(set->p[0]);
1094                 isl_set_free(set);
1095                 return convex_hull;
1096         }
1097         if (isl_set_n_dim(set) == 1)
1098                 return convex_hull_1d(set->ctx, set);
1099
1100         bounds = independent_bounds(set->ctx, set);
1101         if (!bounds)
1102                 goto error;
1103         return uset_convex_hull_wrap_with_bounds(set, bounds);
1104 error:
1105         isl_set_free(set);
1106         return NULL;
1107 }
1108
1109 /* Compute the convex hull of set "set" with affine hull "affine_hull",
1110  * We first remove the equalities (transforming the set), compute the
1111  * convex hull of the transformed set and then add the equalities back
1112  * (after performing the inverse transformation.
1113  */
1114 static struct isl_basic_set *modulo_affine_hull(struct isl_ctx *ctx,
1115         struct isl_set *set, struct isl_basic_set *affine_hull)
1116 {
1117         struct isl_mat *T;
1118         struct isl_mat *T2;
1119         struct isl_basic_set *dummy;
1120         struct isl_basic_set *convex_hull;
1121
1122         dummy = isl_basic_set_remove_equalities(
1123                         isl_basic_set_copy(affine_hull), &T, &T2);
1124         if (!dummy)
1125                 goto error;
1126         isl_basic_set_free(dummy);
1127         set = isl_set_preimage(ctx, set, T);
1128         convex_hull = uset_convex_hull(set);
1129         convex_hull = isl_basic_set_preimage(ctx, convex_hull, T2);
1130         convex_hull = isl_basic_set_intersect(convex_hull, affine_hull);
1131         return convex_hull;
1132 error:
1133         isl_basic_set_free(affine_hull);
1134         isl_set_free(set);
1135         return NULL;
1136 }
1137
1138 /* Compute the convex hull of a map.
1139  *
1140  * The implementation was inspired by "Extended Convex Hull" by Fukuda et al.,
1141  * specifically, the wrapping of facets to obtain new facets.
1142  */
1143 struct isl_basic_map *isl_map_convex_hull(struct isl_map *map)
1144 {
1145         struct isl_basic_set *bset;
1146         struct isl_basic_map *model = NULL;
1147         struct isl_basic_set *affine_hull = NULL;
1148         struct isl_basic_map *convex_hull = NULL;
1149         struct isl_set *set = NULL;
1150         struct isl_ctx *ctx;
1151
1152         if (!map)
1153                 goto error;
1154
1155         ctx = map->ctx;
1156         if (map->n == 0) {
1157                 convex_hull = isl_basic_map_empty_like_map(map);
1158                 isl_map_free(map);
1159                 return convex_hull;
1160         }
1161
1162         map = isl_map_align_divs(map);
1163         model = isl_basic_map_copy(map->p[0]);
1164         set = isl_map_underlying_set(map);
1165         if (!set)
1166                 goto error;
1167
1168         affine_hull = isl_set_affine_hull(isl_set_copy(set));
1169         if (!affine_hull)
1170                 goto error;
1171         if (affine_hull->n_eq != 0)
1172                 bset = modulo_affine_hull(ctx, set, affine_hull);
1173         else {
1174                 isl_basic_set_free(affine_hull);
1175                 bset = uset_convex_hull(set);
1176         }
1177
1178         convex_hull = isl_basic_map_overlying_set(bset, model);
1179
1180         ISL_F_CLR(convex_hull, ISL_BASIC_MAP_RATIONAL);
1181         return convex_hull;
1182 error:
1183         isl_set_free(set);
1184         isl_basic_map_free(model);
1185         return NULL;
1186 }
1187
1188 struct isl_basic_set *isl_set_convex_hull(struct isl_set *set)
1189 {
1190         return (struct isl_basic_set *)
1191                 isl_map_convex_hull((struct isl_map *)set);
1192 }
1193
1194 /* Compute a superset of the convex hull of map that is described
1195  * by only translates of the constraints in the constituents of map.
1196  *
1197  * The implementation is not very efficient.  In particular, if
1198  * constraints with the same normal appear in more than one
1199  * basic map, they will be (re)examined each time.
1200  */
1201 struct isl_basic_map *isl_map_simple_hull(struct isl_map *map)
1202 {
1203         struct isl_set *set = NULL;
1204         struct isl_basic_map *model = NULL;
1205         struct isl_basic_map *hull;
1206         struct isl_basic_set *bset = NULL;
1207         int i, j;
1208         unsigned n_ineq;
1209         unsigned dim;
1210
1211         if (!map)
1212                 return NULL;
1213         if (map->n == 0) {
1214                 hull = isl_basic_map_empty_like_map(map);
1215                 isl_map_free(map);
1216                 return hull;
1217         }
1218         if (map->n == 1) {
1219                 hull = isl_basic_map_copy(map->p[0]);
1220                 isl_map_free(map);
1221                 return hull;
1222         }
1223
1224         map = isl_map_align_divs(map);
1225         model = isl_basic_map_copy(map->p[0]);
1226
1227         n_ineq = 0;
1228         for (i = 0; i < map->n; ++i) {
1229                 if (!map->p[i])
1230                         goto error;
1231                 n_ineq += map->p[i]->n_ineq;
1232         }
1233
1234         set = isl_map_underlying_set(map);
1235         if (!set)
1236                 goto error;
1237
1238         bset = isl_set_affine_hull(isl_set_copy(set));
1239         if (!bset)
1240                 goto error;
1241         dim = isl_basic_set_n_dim(bset);
1242         bset = isl_basic_set_extend(bset, 0, dim, 0, 0, n_ineq);
1243         if (!bset)
1244                 goto error;
1245
1246         for (i = 0; i < set->n; ++i) {
1247                 for (j = 0; j < set->p[i]->n_ineq; ++j) {
1248                         int k;
1249                         int is_bound;
1250
1251                         k = isl_basic_set_alloc_inequality(bset);
1252                         if (k < 0)
1253                                 goto error;
1254                         isl_seq_cpy(bset->ineq[k], set->p[i]->ineq[j], 1 + dim);
1255                         is_bound = uset_is_bound(set->ctx, set, bset->ineq[k],
1256                                                         1 + dim);
1257                         if (is_bound < 0)
1258                                 goto error;
1259                         if (!is_bound)
1260                                 isl_basic_set_free_inequality(bset, 1);
1261                 }
1262         }
1263
1264         bset = isl_basic_set_simplify(bset);
1265         bset = isl_basic_set_finalize(bset);
1266         bset = isl_basic_set_convex_hull(bset);
1267
1268         hull = isl_basic_map_overlying_set(bset, model);
1269
1270         isl_set_free(set);
1271         return hull;
1272 error:
1273         isl_basic_set_free(bset);
1274         isl_set_free(set);
1275         isl_basic_map_free(model);
1276         return NULL;
1277 }
1278
1279 struct isl_basic_set *isl_set_simple_hull(struct isl_set *set)
1280 {
1281         return (struct isl_basic_set *)
1282                 isl_map_simple_hull((struct isl_map *)set);
1283 }