isl_convex_hull: fix memory leak in error path
[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(struct isl_set *set);
10
11 static 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 /* Compute the convex hull of a basic map, by removing the redundant
23  * constraints.  If the minimal value along the normal of a constraint
24  * is the same if the constraint is removed, then the constraint is redundant.
25  *
26  * Alternatively, we could have intersected the basic map with the
27  * corresponding equality and the checked if the dimension was that
28  * of a facet.
29  */
30 struct isl_basic_map *isl_basic_map_convex_hull(struct isl_basic_map *bmap)
31 {
32         int i;
33         isl_int opt_n;
34         isl_int opt_d;
35         struct isl_ctx *ctx;
36
37         bmap = isl_basic_map_implicit_equalities(bmap);
38         if (!bmap)
39                 return NULL;
40
41         if (F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
42                 return bmap;
43         if (F_ISSET(bmap, ISL_BASIC_MAP_NO_REDUNDANT))
44                 return bmap;
45
46         ctx = bmap->ctx;
47         isl_int_init(opt_n);
48         isl_int_init(opt_d);
49         for (i = bmap->n_ineq-1; i >= 0; --i) {
50                 enum isl_lp_result res;
51                 swap_ineq(bmap, i, bmap->n_ineq-1);
52                 bmap->n_ineq--;
53                 res = isl_solve_lp(bmap, 0,
54                         bmap->ineq[bmap->n_ineq]+1, ctx->one, &opt_n, &opt_d);
55                 bmap->n_ineq++;
56                 swap_ineq(bmap, i, bmap->n_ineq-1);
57                 if (res == isl_lp_unbounded)
58                         continue;
59                 if (res == isl_lp_error)
60                         goto error;
61                 if (res == isl_lp_empty) {
62                         bmap = isl_basic_map_set_to_empty(bmap);
63                         break;
64                 }
65                 isl_int_addmul(opt_n, opt_d, bmap->ineq[i][0]);
66                 if (!isl_int_is_neg(opt_n))
67                         isl_basic_map_drop_inequality(bmap, i);
68         }
69         isl_int_clear(opt_n);
70         isl_int_clear(opt_d);
71
72         F_SET(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
73         return bmap;
74 error:
75         isl_int_clear(opt_n);
76         isl_int_clear(opt_d);
77         isl_basic_map_free(bmap);
78         return NULL;
79 }
80
81 struct isl_basic_set *isl_basic_set_convex_hull(struct isl_basic_set *bset)
82 {
83         return (struct isl_basic_set *)
84                 isl_basic_map_convex_hull((struct isl_basic_map *)bset);
85 }
86
87 /* Check if "c" is a direction with a lower bound in "set" that is independent
88  * of the previously found "n" bounds in "dirs".
89  * If so, add it to the list, with the negative of the lower bound
90  * in the constant position, i.e., such that c correspond to a bounding
91  * hyperplane (but not necessarily a facet).
92  */
93 static int is_independent_bound(struct isl_ctx *ctx,
94         struct isl_set *set, isl_int *c,
95         struct isl_mat *dirs, int n)
96 {
97         int first;
98         int i = 0, j;
99         isl_int opt;
100         isl_int opt_denom;
101
102         isl_seq_cpy(dirs->row[n]+1, c+1, dirs->n_col-1);
103         if (n != 0) {
104                 int pos = isl_seq_first_non_zero(dirs->row[n]+1, dirs->n_col-1);
105                 if (pos < 0)
106                         return 0;
107                 for (i = 0; i < n; ++i) {
108                         int pos_i;
109                         pos_i = isl_seq_first_non_zero(dirs->row[i]+1, dirs->n_col-1);
110                         if (pos_i < pos)
111                                 continue;
112                         if (pos_i > pos)
113                                 break;
114                         isl_seq_elim(dirs->row[n]+1, dirs->row[i]+1, pos,
115                                         dirs->n_col-1, NULL);
116                         pos = isl_seq_first_non_zero(dirs->row[n]+1, dirs->n_col-1);
117                         if (pos < 0)
118                                 return 0;
119                 }
120         }
121
122         isl_int_init(opt);
123         isl_int_init(opt_denom);
124         first = 1;
125         for (j = 0; j < set->n; ++j) {
126                 enum isl_lp_result res;
127
128                 if (F_ISSET(set->p[j], ISL_BASIC_MAP_EMPTY))
129                         continue;
130
131                 res = isl_solve_lp((struct isl_basic_map*)set->p[j],
132                                 0, dirs->row[n]+1, ctx->one, &opt, &opt_denom);
133                 if (res == isl_lp_unbounded)
134                         break;
135                 if (res == isl_lp_error)
136                         goto error;
137                 if (res == isl_lp_empty) {
138                         set->p[j] = isl_basic_set_set_to_empty(set->p[j]);
139                         if (!set->p[j])
140                                 goto error;
141                         continue;
142                 }
143                 if (!isl_int_is_one(opt_denom))
144                         isl_seq_scale(dirs->row[n], dirs->row[n], opt_denom,
145                                         dirs->n_col);
146                 if (first || isl_int_lt(opt, dirs->row[n][0]))
147                         isl_int_set(dirs->row[n][0], opt);
148                 first = 0;
149         }
150         isl_int_clear(opt);
151         isl_int_clear(opt_denom);
152         if (j < set->n)
153                 return 0;
154         isl_int_neg(dirs->row[n][0], dirs->row[n][0]);
155         if (i < n) {
156                 int k;
157                 isl_int *t = dirs->row[n];
158                 for (k = n; k > i; --k)
159                         dirs->row[k] = dirs->row[k-1];
160                 dirs->row[i] = t;
161         }
162         return 1;
163 error:
164         isl_int_clear(opt);
165         isl_int_clear(opt_denom);
166         return -1;
167 }
168
169 /* Compute and return a maximal set of linearly independent bounds
170  * on the set "set", based on the constraints of the basic sets
171  * in "set".
172  */
173 static struct isl_mat *independent_bounds(struct isl_ctx *ctx,
174         struct isl_set *set)
175 {
176         int i, j, n;
177         struct isl_mat *dirs = NULL;
178
179         dirs = isl_mat_alloc(ctx, set->dim, 1+set->dim);
180         if (!dirs)
181                 goto error;
182
183         n = 0;
184         for (i = 0; n < set->dim && i < set->n; ++i) {
185                 int f;
186                 struct isl_basic_set *bset = set->p[i];
187
188                 for (j = 0; n < set->dim && j < bset->n_eq; ++j) {
189                         f = is_independent_bound(ctx, set, bset->eq[j],
190                                                 dirs, n);
191                         if (f < 0)
192                                 goto error;
193                         if (f) {
194                                 ++n;
195                                 continue;
196                         }
197                         isl_seq_neg(bset->eq[j], bset->eq[j], 1+set->dim);
198                         f = is_independent_bound(ctx, set, bset->eq[j],
199                                                 dirs, n);
200                         isl_seq_neg(bset->eq[j], bset->eq[j], 1+set->dim);
201                         if (f < 0)
202                                 goto error;
203                         if (f)
204                                 ++n;
205                 }
206                 for (j = 0; n < set->dim && j < bset->n_ineq; ++j) {
207                         f = is_independent_bound(ctx, set, bset->ineq[j],
208                                                 dirs, n);
209                         if (f < 0)
210                                 goto error;
211                         if (f)
212                                 ++n;
213                 }
214         }
215         dirs->n_row = n;
216         return dirs;
217 error:
218         isl_mat_free(ctx, dirs);
219         return NULL;
220 }
221
222 static struct isl_basic_set *isl_basic_set_set_rational(
223         struct isl_basic_set *bset)
224 {
225         if (!bset)
226                 return NULL;
227
228         if (F_ISSET(bset, ISL_BASIC_MAP_RATIONAL))
229                 return bset;
230
231         bset = isl_basic_set_cow(bset);
232         if (!bset)
233                 return NULL;
234
235         F_SET(bset, ISL_BASIC_MAP_RATIONAL);
236
237         return bset;
238 }
239
240 static struct isl_set *isl_set_set_rational(struct isl_set *set)
241 {
242         int i;
243
244         set = isl_set_cow(set);
245         if (!set)
246                 return NULL;
247         for (i = 0; i < set->n; ++i) {
248                 set->p[i] = isl_basic_set_set_rational(set->p[i]);
249                 if (!set->p[i])
250                         goto error;
251         }
252         return set;
253 error:
254         isl_set_free(set);
255         return NULL;
256 }
257
258 static struct isl_basic_set *isl_basic_set_add_equality(struct isl_ctx *ctx,
259         struct isl_basic_set *bset, isl_int *c)
260 {
261         int i;
262         unsigned total;
263
264         isl_assert(ctx, bset->nparam == 0, goto error);
265         isl_assert(ctx, bset->n_div == 0, goto error);
266         bset = isl_basic_set_extend(bset, 0, bset->dim, 0, 1, 0);
267         i = isl_basic_set_alloc_equality(bset);
268         if (i < 0)
269                 goto error;
270         isl_seq_cpy(bset->eq[i], c, 1 + bset->dim);
271         return bset;
272 error:
273         isl_basic_set_free(bset);
274         return NULL;
275 }
276
277 static struct isl_set *isl_set_add_equality(struct isl_ctx *ctx,
278         struct isl_set *set, isl_int *c)
279 {
280         int i;
281
282         set = isl_set_cow(set);
283         if (!set)
284                 return NULL;
285         for (i = 0; i < set->n; ++i) {
286                 set->p[i] = isl_basic_set_add_equality(ctx, set->p[i], c);
287                 if (!set->p[i])
288                         goto error;
289         }
290         return set;
291 error:
292         isl_set_free(set);
293         return NULL;
294 }
295
296 /* Given a union of basic sets, construct the constraints for wrapping
297  * a facet around one of its ridges.
298  * In particular, if each of n the d-dimensional basic sets i in "set"
299  * contains the origin, satisfies the constraints x_1 >= 0 and x_2 >= 0
300  * and is defined by the constraints
301  *                                  [ 1 ]
302  *                              A_i [ x ]  >= 0
303  *
304  * then the resulting set is of dimension n*(1+d) and has as contraints
305  *
306  *                                  [ a_i ]
307  *                              A_i [ x_i ] >= 0
308  *
309  *                                    a_i   >= 0
310  *
311  *                      \sum_i x_{i,1} = 1
312  */
313 static struct isl_basic_set *wrap_constraints(struct isl_ctx *ctx,
314                                                         struct isl_set *set)
315 {
316         struct isl_basic_set *lp;
317         unsigned n_eq;
318         unsigned n_ineq;
319         int i, j, k;
320         unsigned dim;
321
322         if (!set)
323                 return NULL;
324
325         dim = 1 + set->dim;
326         n_eq = 1;
327         n_ineq = set->n;
328         for (i = 0; i < set->n; ++i) {
329                 n_eq += set->p[i]->n_eq;
330                 n_ineq += set->p[i]->n_ineq;
331         }
332         lp = isl_basic_set_alloc(ctx, 0, dim * set->n, 0, n_eq, n_ineq);
333         if (!lp)
334                 return NULL;
335         k = isl_basic_set_alloc_equality(lp);
336         isl_int_set_si(lp->eq[k][0], -1);
337         for (i = 0; i < set->n; ++i) {
338                 isl_int_set_si(lp->eq[k][1+dim*i], 0);
339                 isl_int_set_si(lp->eq[k][1+dim*i+1], 1);
340                 isl_seq_clr(lp->eq[k]+1+dim*i+2, dim-2);
341         }
342         for (i = 0; i < set->n; ++i) {
343                 k = isl_basic_set_alloc_inequality(lp);
344                 isl_seq_clr(lp->ineq[k], 1+lp->dim);
345                 isl_int_set_si(lp->ineq[k][1+dim*i], 1);
346
347                 for (j = 0; j < set->p[i]->n_eq; ++j) {
348                         k = isl_basic_set_alloc_equality(lp);
349                         isl_seq_clr(lp->eq[k], 1+dim*i);
350                         isl_seq_cpy(lp->eq[k]+1+dim*i, set->p[i]->eq[j], dim);
351                         isl_seq_clr(lp->eq[k]+1+dim*(i+1), dim*(set->n-i-1));
352                 }
353
354                 for (j = 0; j < set->p[i]->n_ineq; ++j) {
355                         k = isl_basic_set_alloc_inequality(lp);
356                         isl_seq_clr(lp->ineq[k], 1+dim*i);
357                         isl_seq_cpy(lp->ineq[k]+1+dim*i, set->p[i]->ineq[j], dim);
358                         isl_seq_clr(lp->ineq[k]+1+dim*(i+1), dim*(set->n-i-1));
359                 }
360         }
361         return lp;
362 }
363
364 /* Given a facet "facet" of the convex hull of "set" and a facet "ridge"
365  * of that facet, compute the other facet of the convex hull that contains
366  * the ridge.
367  *
368  * We first transform the set such that the facet constraint becomes
369  *
370  *                      x_1 >= 0
371  *
372  * I.e., the facet is
373  *
374  *                      x_1 = 0
375  *
376  * and on that facet, the constraint that defines the ridge is
377  *
378  *                      x_2 >= 0
379  *
380  * (This transformation is not strictly needed, all that is needed is
381  * that the ridge contains the origin.)
382  *
383  * Since the ridge contains the origin, the cone of the convex hull
384  * will be of the form
385  *
386  *                      x_1 >= 0
387  *                      x_2 >= a x_1
388  *
389  * with this second constraint defining the new facet.
390  * The constant a is obtained by settting x_1 in the cone of the
391  * convex hull to 1 and minimizing x_2.
392  * Now, each element in the cone of the convex hull is the sum
393  * of elements in the cones of the basic sets.
394  * If a_i is the dilation factor of basic set i, then the problem
395  * we need to solve is
396  *
397  *                      min \sum_i x_{i,2}
398  *                      st
399  *                              \sum_i x_{i,1} = 1
400  *                                  a_i   >= 0
401  *                                [ a_i ]
402  *                              A [ x_i ] >= 0
403  *
404  * with
405  *                                  [  1  ]
406  *                              A_i [ x_i ] >= 0
407  *
408  * the constraints of each (transformed) basic set.
409  * If a = n/d, then the consstraint defining the new facet (in the transformed
410  * space) is
411  *
412  *                      -n x_1 + d x_2 >= 0
413  *
414  * In the original space, we need to take the same combination of the
415  * corresponding constraints "facet" and "ridge".
416  */
417 static isl_int *wrap_facet(struct isl_ctx *ctx, struct isl_set *set,
418         isl_int *facet, isl_int *ridge)
419 {
420         int i;
421         struct isl_mat *T = NULL;
422         struct isl_basic_set *lp = NULL;
423         struct isl_vec *obj;
424         enum isl_lp_result res;
425         isl_int num, den;
426         unsigned dim;
427
428         set = isl_set_copy(set);
429
430         dim = 1 + set->dim;
431         T = isl_mat_alloc(ctx, 3, 1 + set->dim);
432         if (!T)
433                 goto error;
434         isl_int_set_si(T->row[0][0], 1);
435         isl_seq_clr(T->row[0]+1, set->dim);
436         isl_seq_cpy(T->row[1], facet, 1+set->dim);
437         isl_seq_cpy(T->row[2], ridge, 1+set->dim);
438         T = isl_mat_right_inverse(ctx, T);
439         set = isl_set_preimage(ctx, set, T);
440         T = NULL;
441         lp = wrap_constraints(ctx, set);
442         obj = isl_vec_alloc(ctx, dim*set->n);
443         if (!obj)
444                 goto error;
445         for (i = 0; i < set->n; ++i) {
446                 isl_seq_clr(obj->block.data+dim*i, 2);
447                 isl_int_set_si(obj->block.data[dim*i+2], 1);
448                 isl_seq_clr(obj->block.data+dim*i+3, dim-3);
449         }
450         isl_int_init(num);
451         isl_int_init(den);
452         res = isl_solve_lp((struct isl_basic_map *)lp, 0,
453                                         obj->block.data, ctx->one, &num, &den);
454         if (res == isl_lp_ok) {
455                 isl_int_neg(num, num);
456                 isl_seq_combine(facet, num, facet, den, ridge, dim);
457         }
458         isl_int_clear(num);
459         isl_int_clear(den);
460         isl_vec_free(ctx, obj);
461         isl_basic_set_free(lp);
462         isl_set_free(set);
463         return (res == isl_lp_ok) ? facet : NULL;
464 error:
465         isl_basic_set_free(lp);
466         isl_mat_free(ctx, T);
467         isl_set_free(set);
468         return NULL;
469 }
470
471 /* Given a direction of a constraint, compute the constant term
472  * such that the resulting constraint is a bounding constraint
473  * of the set "set" (which just happens to be a face of the
474  * original set).
475  */
476 static int compute_bound_on_face(struct isl_ctx *ctx,
477         struct isl_set *set, isl_int *c)
478 {
479         int first = 1;
480         int j;
481         isl_int opt;
482         isl_int opt_denom;
483
484         isl_int_init(opt);
485         isl_int_init(opt_denom);
486         for (j = 0; j < set->n; ++j) {
487                 enum isl_lp_result res;
488
489                 if (F_ISSET(set->p[j], ISL_BASIC_MAP_EMPTY))
490                         continue;
491
492                 res = isl_solve_lp((struct isl_basic_map*)set->p[j],
493                                         0, c+1, ctx->one, &opt, &opt_denom);
494                 if (res == isl_lp_unbounded)
495                         goto error;
496                 if (res == isl_lp_error)
497                         goto error;
498                 if (res == isl_lp_empty) {
499                         set->p[j] = isl_basic_set_set_to_empty(set->p[j]);
500                         if (!set->p[j])
501                                 goto error;
502                         continue;
503                 }
504                 if (!isl_int_is_one(opt_denom))
505                         isl_seq_scale(c, c, opt_denom, 1+set->dim);
506                 if (first || isl_int_lt(opt, c[0]))
507                         isl_int_set(c[0], opt);
508                 first = 0;
509         }
510         isl_assert(ctx, !first, goto error);
511         isl_int_clear(opt);
512         isl_int_clear(opt_denom);
513         isl_int_neg(c[0], c[0]);
514         return 0;
515 error:
516         isl_int_clear(opt);
517         isl_int_clear(opt_denom);
518         return -1;
519 }
520
521 /* Given a set of d linearly independent bounding constraints of the
522  * convex hull of "set", compute the constraint of a facet of "set".
523  *
524  * We first compute the intersection with the first bounding hyperplane
525  * and shift the second bounding constraint to be a bounding constraint
526  * of the resulting face.  We then wrap around the next bounding constraint
527  * and continue the process until all bounding constraints have been
528  * taken into account.
529  * The resulting linear combination of the bounding constraints will
530  * correspond to a facet of the convex hull.
531  */
532 static struct isl_mat *initial_facet_constraint(struct isl_ctx *ctx,
533         struct isl_set *set, struct isl_mat *bounds)
534 {
535         struct isl_set *face = NULL;
536         int i;
537
538         isl_assert(ctx, set->n > 0, goto error);
539         isl_assert(ctx, bounds->n_row == set->dim, goto error);
540
541         face = isl_set_copy(set);
542         if (!face)
543                 goto error;
544         for (i = 1; i < set->dim; ++i) {
545                 face = isl_set_add_equality(ctx, face, bounds->row[i-1]);
546                 if (compute_bound_on_face(ctx, face, bounds->row[i]) < 0)
547                         goto error;
548                 if (!wrap_facet(ctx, set, bounds->row[0], bounds->row[i]))
549                         goto error;
550         }
551         isl_set_free(face);
552         return bounds;
553 error:
554         isl_set_free(face);
555         isl_mat_free(ctx, bounds);
556         return NULL;
557 }
558
559 /* Given the bounding constraint "c" of a facet of the convex hull of "set",
560  * compute a hyperplane description of the facet, i.e., compute the facets
561  * of the facet.
562  *
563  * We compute an affine transformation that transforms the constraint
564  *
565  *                        [ 1 ]
566  *                      c [ x ] = 0
567  *
568  * to the constraint
569  *
570  *                         z_1  = 0
571  *
572  * by computing the right inverse U of a matrix that starts with the rows
573  *
574  *                      [ 1 0 ]
575  *                      [  c  ]
576  *
577  * Then
578  *                      [ 1 ]     [ 1 ]
579  *                      [ x ] = U [ z ]
580  * and
581  *                      [ 1 ]     [ 1 ]
582  *                      [ z ] = Q [ x ]
583  *
584  * with Q = U^{-1}
585  * Since z_1 is zero, we can drop this variable as well as the corresponding
586  * column of U to obtain
587  *
588  *                      [ 1 ]      [ 1  ]
589  *                      [ x ] = U' [ z' ]
590  * and
591  *                      [ 1  ]      [ 1 ]
592  *                      [ z' ] = Q' [ x ]
593  *
594  * with Q' equal to Q, but without the corresponding row.
595  * After computing the facets of the facet in the z' space,
596  * we convert them back to the x space through Q.
597  */
598 static struct isl_basic_set *compute_facet(struct isl_ctx *ctx,
599         struct isl_set *set, isl_int *c)
600 {
601         struct isl_mat *m, *U, *Q;
602         struct isl_basic_set *facet;
603
604         set = isl_set_copy(set);
605         m = isl_mat_alloc(ctx, 2, 1 + set->dim);
606         if (!m)
607                 goto error;
608         isl_int_set_si(m->row[0][0], 1);
609         isl_seq_clr(m->row[0]+1, set->dim);
610         isl_seq_cpy(m->row[1], c, 1+set->dim);
611         m = isl_mat_left_hermite(ctx, m, 0, &U, &Q);
612         if (!m)
613                 goto error;
614         U = isl_mat_drop_cols(ctx, U, 1, 1);
615         Q = isl_mat_drop_rows(ctx, Q, 1, 1);
616         set = isl_set_preimage(ctx, set, U);
617         facet = uset_convex_hull(set);
618         facet = isl_basic_set_preimage(ctx, facet, Q);
619         isl_mat_free(ctx, m);
620         return facet;
621 error:
622         isl_set_free(set);
623         return NULL;
624 }
625
626 /* Given an initial facet constraint, compute the remaining facets.
627  * We do this by running through all facets found so far and computing
628  * the adjacent facets through wrapping, adding those facets that we
629  * hadn't already found before.
630  *
631  * This function can still be significantly optimized by checking which of
632  * the facets of the basic sets are also facets of the convex hull and
633  * using all the facets so far to help in constructing the facets of the
634  * facets
635  * and/or
636  * using the technique in section "3.1 Ridge Generation" of
637  * "Extended Convex Hull" by Fukuda et al.
638  */
639 static struct isl_basic_set *extend(struct isl_ctx *ctx, struct isl_set *set,
640         struct isl_mat *initial)
641 {
642         int i, j, f;
643         int k;
644         struct isl_basic_set *hull = NULL;
645         struct isl_basic_set *facet = NULL;
646         unsigned n_ineq;
647         unsigned total;
648
649         isl_assert(ctx, set->n > 0, goto error);
650
651         n_ineq = 1;
652         for (i = 0; i < set->n; ++i) {
653                 n_ineq += set->p[i]->n_eq;
654                 n_ineq += set->p[i]->n_ineq;
655         }
656         isl_assert(ctx, 1 + set->dim == initial->n_col, goto error);
657         hull = isl_basic_set_alloc(ctx, 0, set->dim, 0,
658                     0, n_ineq + 2 * set->p[0]->n_div);
659         if (!hull)
660                 goto error;
661         k = isl_basic_set_alloc_inequality(hull);
662         if (k < 0)
663                 goto error;
664         isl_seq_cpy(hull->ineq[k], initial->row[0], initial->n_col);
665         for (i = 0; i < hull->n_ineq; ++i) {
666                 facet = compute_facet(ctx, set, hull->ineq[i]);
667                 if (!facet)
668                         goto error;
669                 for (j = 0; j < facet->n_ineq; ++j) {
670                         k = isl_basic_set_alloc_inequality(hull);
671                         if (k < 0)
672                                 goto error;
673                         isl_seq_cpy(hull->ineq[k], hull->ineq[i], 1+hull->dim);
674                         if (!wrap_facet(ctx, set, hull->ineq[k], facet->ineq[j]))
675                                 goto error;
676                         for (f = 0; f < k; ++f)
677                                 if (isl_seq_eq(hull->ineq[f], hull->ineq[k],
678                                                 1+hull->dim))
679                                         break;
680                         if (f < k)
681                                 isl_basic_set_free_inequality(hull, 1);
682                 }
683                 isl_basic_set_free(facet);
684         }
685         hull = isl_basic_set_simplify(hull);
686         hull = isl_basic_set_finalize(hull);
687         return hull;
688 error:
689         isl_basic_set_free(facet);
690         isl_basic_set_free(hull);
691         return NULL;
692 }
693
694 /* Special case for computing the convex hull of a one dimensional set.
695  * We simply collect the lower and upper bounds of each basic set
696  * and the biggest of those.
697  */
698 static struct isl_basic_set *convex_hull_1d(struct isl_ctx *ctx,
699         struct isl_set *set)
700 {
701         struct isl_mat *c = NULL;
702         isl_int *lower = NULL;
703         isl_int *upper = NULL;
704         int i, j, k;
705         isl_int a, b;
706         struct isl_basic_set *hull;
707
708         for (i = 0; i < set->n; ++i) {
709                 set->p[i] = isl_basic_set_simplify(set->p[i]);
710                 if (!set->p[i])
711                         goto error;
712         }
713         set = isl_set_remove_empty_parts(set);
714         if (!set)
715                 goto error;
716         isl_assert(ctx, set->n > 0, goto error);
717         c = isl_mat_alloc(ctx, 2, 2);
718         if (!c)
719                 goto error;
720
721         if (set->p[0]->n_eq > 0) {
722                 isl_assert(ctx, set->p[0]->n_eq == 1, goto error);
723                 lower = c->row[0];
724                 upper = c->row[1];
725                 if (isl_int_is_pos(set->p[0]->eq[0][1])) {
726                         isl_seq_cpy(lower, set->p[0]->eq[0], 2);
727                         isl_seq_neg(upper, set->p[0]->eq[0], 2);
728                 } else {
729                         isl_seq_neg(lower, set->p[0]->eq[0], 2);
730                         isl_seq_cpy(upper, set->p[0]->eq[0], 2);
731                 }
732         } else {
733                 for (j = 0; j < set->p[0]->n_ineq; ++j) {
734                         if (isl_int_is_pos(set->p[0]->ineq[j][1])) {
735                                 lower = c->row[0];
736                                 isl_seq_cpy(lower, set->p[0]->ineq[j], 2);
737                         } else {
738                                 upper = c->row[1];
739                                 isl_seq_cpy(upper, set->p[0]->ineq[j], 2);
740                         }
741                 }
742         }
743
744         isl_int_init(a);
745         isl_int_init(b);
746         for (i = 0; i < set->n; ++i) {
747                 struct isl_basic_set *bset = set->p[i];
748                 int has_lower = 0;
749                 int has_upper = 0;
750
751                 for (j = 0; j < bset->n_eq; ++j) {
752                         has_lower = 1;
753                         has_upper = 1;
754                         if (lower) {
755                                 isl_int_mul(a, lower[0], bset->eq[j][1]);
756                                 isl_int_mul(b, lower[1], bset->eq[j][0]);
757                                 if (isl_int_lt(a, b) && isl_int_is_pos(bset->eq[j][1]))
758                                         isl_seq_cpy(lower, bset->eq[j], 2);
759                                 if (isl_int_gt(a, b) && isl_int_is_neg(bset->eq[j][1]))
760                                         isl_seq_neg(lower, bset->eq[j], 2);
761                         }
762                         if (upper) {
763                                 isl_int_mul(a, upper[0], bset->eq[j][1]);
764                                 isl_int_mul(b, upper[1], bset->eq[j][0]);
765                                 if (isl_int_lt(a, b) && isl_int_is_pos(bset->eq[j][1]))
766                                         isl_seq_neg(upper, bset->eq[j], 2);
767                                 if (isl_int_gt(a, b) && isl_int_is_neg(bset->eq[j][1]))
768                                         isl_seq_cpy(upper, bset->eq[j], 2);
769                         }
770                 }
771                 for (j = 0; j < bset->n_ineq; ++j) {
772                         if (isl_int_is_pos(bset->ineq[j][1]))
773                                 has_lower = 1;
774                         if (isl_int_is_neg(bset->ineq[j][1]))
775                                 has_upper = 1;
776                         if (lower && isl_int_is_pos(bset->ineq[j][1])) {
777                                 isl_int_mul(a, lower[0], bset->ineq[j][1]);
778                                 isl_int_mul(b, lower[1], bset->ineq[j][0]);
779                                 if (isl_int_lt(a, b))
780                                         isl_seq_cpy(lower, bset->ineq[j], 2);
781                         }
782                         if (upper && isl_int_is_neg(bset->ineq[j][1])) {
783                                 isl_int_mul(a, upper[0], bset->ineq[j][1]);
784                                 isl_int_mul(b, upper[1], bset->ineq[j][0]);
785                                 if (isl_int_gt(a, b))
786                                         isl_seq_cpy(upper, bset->ineq[j], 2);
787                         }
788                 }
789                 if (!has_lower)
790                         lower = NULL;
791                 if (!has_upper)
792                         upper = NULL;
793         }
794         isl_int_clear(a);
795         isl_int_clear(b);
796
797         hull = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
798         if (!hull)
799                 goto error;
800         if (lower) {
801                 k = isl_basic_set_alloc_inequality(hull);
802                 isl_seq_cpy(hull->ineq[k], lower, 2);
803         }
804         if (upper) {
805                 k = isl_basic_set_alloc_inequality(hull);
806                 isl_seq_cpy(hull->ineq[k], upper, 2);
807         }
808         hull = isl_basic_set_finalize(hull);
809         isl_set_free(set);
810         isl_mat_free(ctx, c);
811         return hull;
812 error:
813         isl_set_free(set);
814         isl_mat_free(ctx, c);
815         return NULL;
816 }
817
818 /* Project out final n dimensions using Fourier-Motzkin */
819 static struct isl_set *set_project_out(struct isl_ctx *ctx,
820         struct isl_set *set, unsigned n)
821 {
822         return isl_set_remove_dims(set, set->dim - n, n);
823 }
824
825 /* If the number of linearly independent bounds we found is smaller
826  * than the dimension, then the convex hull will have a lineality space,
827  * so we may as well project out this lineality space.
828  * We first transform the set such that the first variables correspond
829  * to the directions of the linearly independent bounds and then
830  * project out the remaining variables.
831  */
832 static struct isl_basic_set *modulo_lineality(struct isl_ctx *ctx,
833         struct isl_set *set, struct isl_mat *bounds)
834 {
835         int i, j;
836         unsigned old_dim, new_dim;
837         struct isl_mat *H = NULL, *U = NULL, *Q = NULL;
838         struct isl_basic_set *hull;
839
840         old_dim = set->dim;
841         new_dim = bounds->n_row;
842         H = isl_mat_sub_alloc(ctx, bounds->row, 0, bounds->n_row, 1, set->dim);
843         H = isl_mat_left_hermite(ctx, H, 0, &U, &Q);
844         if (!H)
845                 goto error;
846         U = isl_mat_lin_to_aff(ctx, U);
847         Q = isl_mat_lin_to_aff(ctx, Q);
848         Q->n_row = 1 + new_dim;
849         isl_mat_free(ctx, H);
850         set = isl_set_preimage(ctx, set, U);
851         set = set_project_out(ctx, set, old_dim - new_dim);
852         hull = uset_convex_hull(set);
853         hull = isl_basic_set_preimage(ctx, hull, Q);
854         isl_mat_free(ctx, bounds);
855         return hull;
856 error:
857         isl_mat_free(ctx, bounds);
858         isl_mat_free(ctx, Q);
859         isl_set_free(set);
860         return NULL;
861 }
862
863 /* This is the core procedure, where "set" is a "pure" set, i.e.,
864  * without parameters or divs and where the convex hull of set is
865  * known to be full-dimensional.
866  */
867 static struct isl_basic_set *uset_convex_hull(struct isl_set *set)
868 {
869         int i;
870         struct isl_basic_set *convex_hull = NULL;
871         struct isl_mat *bounds;
872
873         if (set->dim == 0) {
874                 convex_hull = isl_basic_set_universe(set->ctx, 0, 0);
875                 isl_set_free(set);
876                 return convex_hull;
877         }
878
879         set = isl_set_set_rational(set);
880
881         if (!set)
882                 goto error;
883         for (i = 0; i < set->n; ++i) {
884                 set->p[i] = isl_basic_set_convex_hull(set->p[i]);
885                 if (!set->p[i])
886                         goto error;
887         }
888         set = isl_set_remove_empty_parts(set);
889         if (!set)
890                 goto error;
891         if (set->n == 1) {
892                 convex_hull = isl_basic_set_copy(set->p[0]);
893                 isl_set_free(set);
894                 return convex_hull;
895         }
896         if (set->dim == 1)
897                 return convex_hull_1d(set->ctx, set);
898
899         bounds = independent_bounds(set->ctx, set);
900         if (!bounds)
901                 goto error;
902         if (bounds->n_row < set->dim)
903                 return modulo_lineality(set->ctx, set, bounds);
904         bounds = initial_facet_constraint(set->ctx, set, bounds);
905         if (!bounds)
906                 goto error;
907         convex_hull = extend(set->ctx, set, bounds);
908         isl_mat_free(set->ctx, bounds);
909         isl_set_free(set);
910
911         return convex_hull;
912 error:
913         isl_set_free(set);
914         return NULL;
915 }
916
917 /* Compute the convex hull of set "set" with affine hull "affine_hull",
918  * We first remove the equalities (transforming the set), compute the
919  * convex hull of the transformed set and then add the equalities back
920  * (after performing the inverse transformation.
921  */
922 static struct isl_basic_set *modulo_affine_hull(struct isl_ctx *ctx,
923         struct isl_set *set, struct isl_basic_set *affine_hull)
924 {
925         struct isl_mat *T;
926         struct isl_mat *T2;
927         struct isl_basic_set *dummy;
928         struct isl_basic_set *convex_hull;
929
930         dummy = isl_basic_set_remove_equalities(
931                         isl_basic_set_copy(affine_hull), &T, &T2);
932         if (!dummy)
933                 goto error;
934         isl_basic_set_free(dummy);
935         set = isl_set_preimage(ctx, set, T);
936         convex_hull = uset_convex_hull(set);
937         convex_hull = isl_basic_set_preimage(ctx, convex_hull, T2);
938         convex_hull = isl_basic_set_intersect(convex_hull, affine_hull);
939         return convex_hull;
940 error:
941         isl_basic_set_free(affine_hull);
942         isl_set_free(set);
943         return NULL;
944 }
945
946 /* Compute the convex hull of a map.
947  *
948  * The implementation was inspired by "Extended Convex Hull" by Fukuda et al.,
949  * specifically, the wrapping of facets to obtain new facets.
950  */
951 struct isl_basic_map *isl_map_convex_hull(struct isl_map *map)
952 {
953         struct isl_basic_set *bset;
954         struct isl_basic_set *affine_hull = NULL;
955         struct isl_basic_map *convex_hull = NULL;
956         struct isl_set *set = NULL;
957         struct isl_ctx *ctx;
958
959         if (!map)
960                 goto error;
961
962         ctx = map->ctx;
963         if (map->n == 0) {
964                 convex_hull = isl_basic_map_empty(ctx,
965                                             map->nparam, map->n_in, map->n_out);
966                 isl_map_free(map);
967                 return convex_hull;
968         }
969
970         set = isl_map_underlying_set(isl_map_copy(map));
971         if (!set)
972                 goto error;
973
974         affine_hull = isl_set_affine_hull(isl_set_copy(set));
975         if (!affine_hull)
976                 goto error;
977         if (affine_hull->n_eq != 0)
978                 bset = modulo_affine_hull(ctx, set, affine_hull);
979         else {
980                 isl_basic_set_free(affine_hull);
981                 bset = uset_convex_hull(set);
982         }
983
984         convex_hull = isl_basic_map_overlying_set(bset,
985                         isl_basic_map_copy(map->p[0]));
986
987         isl_map_free(map);
988         return convex_hull;
989 error:
990         isl_set_free(set);
991         isl_map_free(map);
992         return NULL;
993 }
994
995 struct isl_basic_set *isl_set_convex_hull(struct isl_set *set)
996 {
997         return (struct isl_basic_set *)
998                 isl_map_convex_hull((struct isl_map *)set);
999 }