wrap_facet: missing 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         if (F_ISSET(bset, ISL_BASIC_SET_EMPTY))
265                 return bset;
266
267         isl_assert(ctx, bset->nparam == 0, goto error);
268         isl_assert(ctx, bset->n_div == 0, goto error);
269         bset = isl_basic_set_extend(bset, 0, bset->dim, 0, 1, 0);
270         i = isl_basic_set_alloc_equality(bset);
271         if (i < 0)
272                 goto error;
273         isl_seq_cpy(bset->eq[i], c, 1 + bset->dim);
274         return bset;
275 error:
276         isl_basic_set_free(bset);
277         return NULL;
278 }
279
280 static struct isl_set *isl_set_add_equality(struct isl_ctx *ctx,
281         struct isl_set *set, isl_int *c)
282 {
283         int i;
284
285         set = isl_set_cow(set);
286         if (!set)
287                 return NULL;
288         for (i = 0; i < set->n; ++i) {
289                 set->p[i] = isl_basic_set_add_equality(ctx, set->p[i], c);
290                 if (!set->p[i])
291                         goto error;
292         }
293         return set;
294 error:
295         isl_set_free(set);
296         return NULL;
297 }
298
299 /* Given a union of basic sets, construct the constraints for wrapping
300  * a facet around one of its ridges.
301  * In particular, if each of n the d-dimensional basic sets i in "set"
302  * contains the origin, satisfies the constraints x_1 >= 0 and x_2 >= 0
303  * and is defined by the constraints
304  *                                  [ 1 ]
305  *                              A_i [ x ]  >= 0
306  *
307  * then the resulting set is of dimension n*(1+d) and has as contraints
308  *
309  *                                  [ a_i ]
310  *                              A_i [ x_i ] >= 0
311  *
312  *                                    a_i   >= 0
313  *
314  *                      \sum_i x_{i,1} = 1
315  */
316 static struct isl_basic_set *wrap_constraints(struct isl_ctx *ctx,
317                                                         struct isl_set *set)
318 {
319         struct isl_basic_set *lp;
320         unsigned n_eq;
321         unsigned n_ineq;
322         int i, j, k;
323         unsigned dim;
324
325         if (!set)
326                 return NULL;
327
328         dim = 1 + set->dim;
329         n_eq = 1;
330         n_ineq = set->n;
331         for (i = 0; i < set->n; ++i) {
332                 n_eq += set->p[i]->n_eq;
333                 n_ineq += set->p[i]->n_ineq;
334         }
335         lp = isl_basic_set_alloc(ctx, 0, dim * set->n, 0, n_eq, n_ineq);
336         if (!lp)
337                 return NULL;
338         k = isl_basic_set_alloc_equality(lp);
339         isl_int_set_si(lp->eq[k][0], -1);
340         for (i = 0; i < set->n; ++i) {
341                 isl_int_set_si(lp->eq[k][1+dim*i], 0);
342                 isl_int_set_si(lp->eq[k][1+dim*i+1], 1);
343                 isl_seq_clr(lp->eq[k]+1+dim*i+2, dim-2);
344         }
345         for (i = 0; i < set->n; ++i) {
346                 k = isl_basic_set_alloc_inequality(lp);
347                 isl_seq_clr(lp->ineq[k], 1+lp->dim);
348                 isl_int_set_si(lp->ineq[k][1+dim*i], 1);
349
350                 for (j = 0; j < set->p[i]->n_eq; ++j) {
351                         k = isl_basic_set_alloc_equality(lp);
352                         isl_seq_clr(lp->eq[k], 1+dim*i);
353                         isl_seq_cpy(lp->eq[k]+1+dim*i, set->p[i]->eq[j], dim);
354                         isl_seq_clr(lp->eq[k]+1+dim*(i+1), dim*(set->n-i-1));
355                 }
356
357                 for (j = 0; j < set->p[i]->n_ineq; ++j) {
358                         k = isl_basic_set_alloc_inequality(lp);
359                         isl_seq_clr(lp->ineq[k], 1+dim*i);
360                         isl_seq_cpy(lp->ineq[k]+1+dim*i, set->p[i]->ineq[j], dim);
361                         isl_seq_clr(lp->ineq[k]+1+dim*(i+1), dim*(set->n-i-1));
362                 }
363         }
364         return lp;
365 }
366
367 /* Given a facet "facet" of the convex hull of "set" and a facet "ridge"
368  * of that facet, compute the other facet of the convex hull that contains
369  * the ridge.
370  *
371  * We first transform the set such that the facet constraint becomes
372  *
373  *                      x_1 >= 0
374  *
375  * I.e., the facet is
376  *
377  *                      x_1 = 0
378  *
379  * and on that facet, the constraint that defines the ridge is
380  *
381  *                      x_2 >= 0
382  *
383  * (This transformation is not strictly needed, all that is needed is
384  * that the ridge contains the origin.)
385  *
386  * Since the ridge contains the origin, the cone of the convex hull
387  * will be of the form
388  *
389  *                      x_1 >= 0
390  *                      x_2 >= a x_1
391  *
392  * with this second constraint defining the new facet.
393  * The constant a is obtained by settting x_1 in the cone of the
394  * convex hull to 1 and minimizing x_2.
395  * Now, each element in the cone of the convex hull is the sum
396  * of elements in the cones of the basic sets.
397  * If a_i is the dilation factor of basic set i, then the problem
398  * we need to solve is
399  *
400  *                      min \sum_i x_{i,2}
401  *                      st
402  *                              \sum_i x_{i,1} = 1
403  *                                  a_i   >= 0
404  *                                [ a_i ]
405  *                              A [ x_i ] >= 0
406  *
407  * with
408  *                                  [  1  ]
409  *                              A_i [ x_i ] >= 0
410  *
411  * the constraints of each (transformed) basic set.
412  * If a = n/d, then the consstraint defining the new facet (in the transformed
413  * space) is
414  *
415  *                      -n x_1 + d x_2 >= 0
416  *
417  * In the original space, we need to take the same combination of the
418  * corresponding constraints "facet" and "ridge".
419  */
420 static isl_int *wrap_facet(struct isl_ctx *ctx, struct isl_set *set,
421         isl_int *facet, isl_int *ridge)
422 {
423         int i;
424         struct isl_mat *T = NULL;
425         struct isl_basic_set *lp = NULL;
426         struct isl_vec *obj;
427         enum isl_lp_result res;
428         isl_int num, den;
429         unsigned dim;
430
431         set = isl_set_copy(set);
432
433         dim = 1 + set->dim;
434         T = isl_mat_alloc(ctx, 3, 1 + set->dim);
435         if (!T)
436                 goto error;
437         isl_int_set_si(T->row[0][0], 1);
438         isl_seq_clr(T->row[0]+1, set->dim);
439         isl_seq_cpy(T->row[1], facet, 1+set->dim);
440         isl_seq_cpy(T->row[2], ridge, 1+set->dim);
441         T = isl_mat_right_inverse(ctx, T);
442         set = isl_set_preimage(ctx, set, T);
443         T = NULL;
444         if (!set)
445                 goto error;
446         lp = wrap_constraints(ctx, set);
447         obj = isl_vec_alloc(ctx, dim*set->n);
448         if (!obj)
449                 goto error;
450         for (i = 0; i < set->n; ++i) {
451                 isl_seq_clr(obj->block.data+dim*i, 2);
452                 isl_int_set_si(obj->block.data[dim*i+2], 1);
453                 isl_seq_clr(obj->block.data+dim*i+3, dim-3);
454         }
455         isl_int_init(num);
456         isl_int_init(den);
457         res = isl_solve_lp((struct isl_basic_map *)lp, 0,
458                                         obj->block.data, ctx->one, &num, &den);
459         if (res == isl_lp_ok) {
460                 isl_int_neg(num, num);
461                 isl_seq_combine(facet, num, facet, den, ridge, dim);
462         }
463         isl_int_clear(num);
464         isl_int_clear(den);
465         isl_vec_free(ctx, obj);
466         isl_basic_set_free(lp);
467         isl_set_free(set);
468         isl_assert(ctx, res == isl_lp_ok, return NULL);
469         return facet;
470 error:
471         isl_basic_set_free(lp);
472         isl_mat_free(ctx, T);
473         isl_set_free(set);
474         return NULL;
475 }
476
477 /* Given a set of d linearly independent bounding constraints of the
478  * convex hull of "set", compute the constraint of a facet of "set".
479  *
480  * We first compute the intersection with the first bounding hyperplane
481  * and remove the component corresponding to this hyperplane from
482  * other bounds (in homogeneous space).
483  * We then wrap around one of the remaining bounding constraints
484  * and continue the process until all bounding constraints have been
485  * taken into account.
486  * The resulting linear combination of the bounding constraints will
487  * correspond to a facet of the convex hull.
488  */
489 static struct isl_mat *initial_facet_constraint(struct isl_ctx *ctx,
490         struct isl_set *set, struct isl_mat *bounds)
491 {
492         struct isl_set *slice = NULL;
493         struct isl_basic_set *face = NULL;
494         struct isl_mat *m, *U, *Q;
495         int i;
496
497         isl_assert(ctx, set->n > 0, goto error);
498         isl_assert(ctx, bounds->n_row == set->dim, goto error);
499
500         while (bounds->n_row > 1) {
501                 slice = isl_set_copy(set);
502                 slice = isl_set_add_equality(ctx, slice, bounds->row[0]);
503                 face = isl_set_affine_hull(slice);
504                 if (!face)
505                         goto error;
506                 if (face->n_eq == 1) {
507                         isl_basic_set_free(face);
508                         break;
509                 }
510                 m = isl_mat_alloc(ctx, 1 + face->n_eq, 1 + face->dim);
511                 if (!m)
512                         goto error;
513                 isl_int_set_si(m->row[0][0], 1);
514                 isl_seq_clr(m->row[0]+1, face->dim);
515                 for (i = 0; i < face->n_eq; ++i)
516                         isl_seq_cpy(m->row[1 + i], face->eq[i], 1 + face->dim);
517                 U = isl_mat_right_inverse(ctx, m);
518                 Q = isl_mat_right_inverse(ctx, isl_mat_copy(ctx, U));
519                 U = isl_mat_drop_cols(ctx, U, 1 + face->n_eq,
520                                                 face->dim - face->n_eq);
521                 Q = isl_mat_drop_rows(ctx, Q, 1 + face->n_eq,
522                                                 face->dim - face->n_eq);
523                 U = isl_mat_drop_cols(ctx, U, 0, 1);
524                 Q = isl_mat_drop_rows(ctx, Q, 0, 1);
525                 bounds = isl_mat_product(ctx, bounds, U);
526                 bounds = isl_mat_product(ctx, bounds, Q);
527                 while (isl_seq_first_non_zero(bounds->row[bounds->n_row-1],
528                                               bounds->n_col) == -1) {
529                         bounds->n_row--;
530                         isl_assert(ctx, bounds->n_row > 1, goto error);
531                 }
532                 if (!wrap_facet(ctx, set, bounds->row[0],
533                                           bounds->row[bounds->n_row-1]))
534                         goto error;
535                 isl_basic_set_free(face);
536                 bounds->n_row--;
537         }
538         return bounds;
539 error:
540         isl_basic_set_free(face);
541         isl_mat_free(ctx, bounds);
542         return NULL;
543 }
544
545 /* Given the bounding constraint "c" of a facet of the convex hull of "set",
546  * compute a hyperplane description of the facet, i.e., compute the facets
547  * of the facet.
548  *
549  * We compute an affine transformation that transforms the constraint
550  *
551  *                        [ 1 ]
552  *                      c [ x ] = 0
553  *
554  * to the constraint
555  *
556  *                         z_1  = 0
557  *
558  * by computing the right inverse U of a matrix that starts with the rows
559  *
560  *                      [ 1 0 ]
561  *                      [  c  ]
562  *
563  * Then
564  *                      [ 1 ]     [ 1 ]
565  *                      [ x ] = U [ z ]
566  * and
567  *                      [ 1 ]     [ 1 ]
568  *                      [ z ] = Q [ x ]
569  *
570  * with Q = U^{-1}
571  * Since z_1 is zero, we can drop this variable as well as the corresponding
572  * column of U to obtain
573  *
574  *                      [ 1 ]      [ 1  ]
575  *                      [ x ] = U' [ z' ]
576  * and
577  *                      [ 1  ]      [ 1 ]
578  *                      [ z' ] = Q' [ x ]
579  *
580  * with Q' equal to Q, but without the corresponding row.
581  * After computing the facets of the facet in the z' space,
582  * we convert them back to the x space through Q.
583  */
584 static struct isl_basic_set *compute_facet(struct isl_ctx *ctx,
585         struct isl_set *set, isl_int *c)
586 {
587         struct isl_mat *m, *U, *Q;
588         struct isl_basic_set *facet;
589
590         set = isl_set_copy(set);
591         m = isl_mat_alloc(ctx, 2, 1 + set->dim);
592         if (!m)
593                 goto error;
594         isl_int_set_si(m->row[0][0], 1);
595         isl_seq_clr(m->row[0]+1, set->dim);
596         isl_seq_cpy(m->row[1], c, 1+set->dim);
597         U = isl_mat_right_inverse(ctx, m);
598         Q = isl_mat_right_inverse(ctx, isl_mat_copy(ctx, U));
599         U = isl_mat_drop_cols(ctx, U, 1, 1);
600         Q = isl_mat_drop_rows(ctx, Q, 1, 1);
601         set = isl_set_preimage(ctx, set, U);
602         facet = uset_convex_hull(set);
603         facet = isl_basic_set_preimage(ctx, facet, Q);
604         return facet;
605 error:
606         isl_set_free(set);
607         return NULL;
608 }
609
610 /* Given an initial facet constraint, compute the remaining facets.
611  * We do this by running through all facets found so far and computing
612  * the adjacent facets through wrapping, adding those facets that we
613  * hadn't already found before.
614  *
615  * This function can still be significantly optimized by checking which of
616  * the facets of the basic sets are also facets of the convex hull and
617  * using all the facets so far to help in constructing the facets of the
618  * facets
619  * and/or
620  * using the technique in section "3.1 Ridge Generation" of
621  * "Extended Convex Hull" by Fukuda et al.
622  */
623 static struct isl_basic_set *extend(struct isl_ctx *ctx, struct isl_set *set,
624         struct isl_mat *initial)
625 {
626         int i, j, f;
627         int k;
628         struct isl_basic_set *hull = NULL;
629         struct isl_basic_set *facet = NULL;
630         unsigned n_ineq;
631         unsigned total;
632
633         isl_assert(ctx, set->n > 0, goto error);
634
635         n_ineq = 1;
636         for (i = 0; i < set->n; ++i) {
637                 n_ineq += set->p[i]->n_eq;
638                 n_ineq += set->p[i]->n_ineq;
639         }
640         isl_assert(ctx, 1 + set->dim == initial->n_col, goto error);
641         hull = isl_basic_set_alloc(ctx, 0, set->dim, 0, 0, n_ineq);
642         hull = isl_basic_set_set_rational(hull);
643         if (!hull)
644                 goto error;
645         k = isl_basic_set_alloc_inequality(hull);
646         if (k < 0)
647                 goto error;
648         isl_seq_cpy(hull->ineq[k], initial->row[0], initial->n_col);
649         for (i = 0; i < hull->n_ineq; ++i) {
650                 facet = compute_facet(ctx, set, hull->ineq[i]);
651                 if (!facet)
652                         goto error;
653                 if (facet->n_ineq + hull->n_ineq > n_ineq) {
654                         hull = isl_basic_set_extend(hull,
655                                 hull->nparam, hull->dim, 0, 0, facet->n_ineq);
656                         n_ineq = hull->n_ineq + facet->n_ineq;
657                 }
658                 for (j = 0; j < facet->n_ineq; ++j) {
659                         k = isl_basic_set_alloc_inequality(hull);
660                         if (k < 0)
661                                 goto error;
662                         isl_seq_cpy(hull->ineq[k], hull->ineq[i], 1+hull->dim);
663                         if (!wrap_facet(ctx, set, hull->ineq[k], facet->ineq[j]))
664                                 goto error;
665                         for (f = 0; f < k; ++f)
666                                 if (isl_seq_eq(hull->ineq[f], hull->ineq[k],
667                                                 1+hull->dim))
668                                         break;
669                         if (f < k)
670                                 isl_basic_set_free_inequality(hull, 1);
671                 }
672                 isl_basic_set_free(facet);
673         }
674         hull = isl_basic_set_simplify(hull);
675         hull = isl_basic_set_finalize(hull);
676         return hull;
677 error:
678         isl_basic_set_free(facet);
679         isl_basic_set_free(hull);
680         return NULL;
681 }
682
683 /* Special case for computing the convex hull of a one dimensional set.
684  * We simply collect the lower and upper bounds of each basic set
685  * and the biggest of those.
686  */
687 static struct isl_basic_set *convex_hull_1d(struct isl_ctx *ctx,
688         struct isl_set *set)
689 {
690         struct isl_mat *c = NULL;
691         isl_int *lower = NULL;
692         isl_int *upper = NULL;
693         int i, j, k;
694         isl_int a, b;
695         struct isl_basic_set *hull;
696
697         for (i = 0; i < set->n; ++i) {
698                 set->p[i] = isl_basic_set_simplify(set->p[i]);
699                 if (!set->p[i])
700                         goto error;
701         }
702         set = isl_set_remove_empty_parts(set);
703         if (!set)
704                 goto error;
705         isl_assert(ctx, set->n > 0, goto error);
706         c = isl_mat_alloc(ctx, 2, 2);
707         if (!c)
708                 goto error;
709
710         if (set->p[0]->n_eq > 0) {
711                 isl_assert(ctx, set->p[0]->n_eq == 1, goto error);
712                 lower = c->row[0];
713                 upper = c->row[1];
714                 if (isl_int_is_pos(set->p[0]->eq[0][1])) {
715                         isl_seq_cpy(lower, set->p[0]->eq[0], 2);
716                         isl_seq_neg(upper, set->p[0]->eq[0], 2);
717                 } else {
718                         isl_seq_neg(lower, set->p[0]->eq[0], 2);
719                         isl_seq_cpy(upper, set->p[0]->eq[0], 2);
720                 }
721         } else {
722                 for (j = 0; j < set->p[0]->n_ineq; ++j) {
723                         if (isl_int_is_pos(set->p[0]->ineq[j][1])) {
724                                 lower = c->row[0];
725                                 isl_seq_cpy(lower, set->p[0]->ineq[j], 2);
726                         } else {
727                                 upper = c->row[1];
728                                 isl_seq_cpy(upper, set->p[0]->ineq[j], 2);
729                         }
730                 }
731         }
732
733         isl_int_init(a);
734         isl_int_init(b);
735         for (i = 0; i < set->n; ++i) {
736                 struct isl_basic_set *bset = set->p[i];
737                 int has_lower = 0;
738                 int has_upper = 0;
739
740                 for (j = 0; j < bset->n_eq; ++j) {
741                         has_lower = 1;
742                         has_upper = 1;
743                         if (lower) {
744                                 isl_int_mul(a, lower[0], bset->eq[j][1]);
745                                 isl_int_mul(b, lower[1], bset->eq[j][0]);
746                                 if (isl_int_lt(a, b) && isl_int_is_pos(bset->eq[j][1]))
747                                         isl_seq_cpy(lower, bset->eq[j], 2);
748                                 if (isl_int_gt(a, b) && isl_int_is_neg(bset->eq[j][1]))
749                                         isl_seq_neg(lower, bset->eq[j], 2);
750                         }
751                         if (upper) {
752                                 isl_int_mul(a, upper[0], bset->eq[j][1]);
753                                 isl_int_mul(b, upper[1], bset->eq[j][0]);
754                                 if (isl_int_lt(a, b) && isl_int_is_pos(bset->eq[j][1]))
755                                         isl_seq_neg(upper, bset->eq[j], 2);
756                                 if (isl_int_gt(a, b) && isl_int_is_neg(bset->eq[j][1]))
757                                         isl_seq_cpy(upper, bset->eq[j], 2);
758                         }
759                 }
760                 for (j = 0; j < bset->n_ineq; ++j) {
761                         if (isl_int_is_pos(bset->ineq[j][1]))
762                                 has_lower = 1;
763                         if (isl_int_is_neg(bset->ineq[j][1]))
764                                 has_upper = 1;
765                         if (lower && isl_int_is_pos(bset->ineq[j][1])) {
766                                 isl_int_mul(a, lower[0], bset->ineq[j][1]);
767                                 isl_int_mul(b, lower[1], bset->ineq[j][0]);
768                                 if (isl_int_lt(a, b))
769                                         isl_seq_cpy(lower, bset->ineq[j], 2);
770                         }
771                         if (upper && isl_int_is_neg(bset->ineq[j][1])) {
772                                 isl_int_mul(a, upper[0], bset->ineq[j][1]);
773                                 isl_int_mul(b, upper[1], bset->ineq[j][0]);
774                                 if (isl_int_gt(a, b))
775                                         isl_seq_cpy(upper, bset->ineq[j], 2);
776                         }
777                 }
778                 if (!has_lower)
779                         lower = NULL;
780                 if (!has_upper)
781                         upper = NULL;
782         }
783         isl_int_clear(a);
784         isl_int_clear(b);
785
786         hull = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
787         hull = isl_basic_set_set_rational(hull);
788         if (!hull)
789                 goto error;
790         if (lower) {
791                 k = isl_basic_set_alloc_inequality(hull);
792                 isl_seq_cpy(hull->ineq[k], lower, 2);
793         }
794         if (upper) {
795                 k = isl_basic_set_alloc_inequality(hull);
796                 isl_seq_cpy(hull->ineq[k], upper, 2);
797         }
798         hull = isl_basic_set_finalize(hull);
799         isl_set_free(set);
800         isl_mat_free(ctx, c);
801         return hull;
802 error:
803         isl_set_free(set);
804         isl_mat_free(ctx, c);
805         return NULL;
806 }
807
808 /* Project out final n dimensions using Fourier-Motzkin */
809 static struct isl_set *set_project_out(struct isl_ctx *ctx,
810         struct isl_set *set, unsigned n)
811 {
812         return isl_set_remove_dims(set, set->dim - n, n);
813 }
814
815 /* If the number of linearly independent bounds we found is smaller
816  * than the dimension, then the convex hull will have a lineality space,
817  * so we may as well project out this lineality space.
818  * We first transform the set such that the first variables correspond
819  * to the directions of the linearly independent bounds and then
820  * project out the remaining variables.
821  */
822 static struct isl_basic_set *modulo_lineality(struct isl_ctx *ctx,
823         struct isl_set *set, struct isl_mat *bounds)
824 {
825         int i, j;
826         unsigned old_dim, new_dim;
827         struct isl_mat *H = NULL, *U = NULL, *Q = NULL;
828         struct isl_basic_set *hull;
829
830         old_dim = set->dim;
831         new_dim = bounds->n_row;
832         H = isl_mat_sub_alloc(ctx, bounds->row, 0, bounds->n_row, 1, set->dim);
833         H = isl_mat_left_hermite(ctx, H, 0, &U, &Q);
834         if (!H)
835                 goto error;
836         U = isl_mat_lin_to_aff(ctx, U);
837         Q = isl_mat_lin_to_aff(ctx, Q);
838         Q->n_row = 1 + new_dim;
839         isl_mat_free(ctx, H);
840         set = isl_set_preimage(ctx, set, U);
841         set = set_project_out(ctx, set, old_dim - new_dim);
842         hull = uset_convex_hull(set);
843         hull = isl_basic_set_preimage(ctx, hull, Q);
844         isl_mat_free(ctx, bounds);
845         return hull;
846 error:
847         isl_mat_free(ctx, bounds);
848         isl_mat_free(ctx, Q);
849         isl_set_free(set);
850         return NULL;
851 }
852
853 /* This is the core procedure, where "set" is a "pure" set, i.e.,
854  * without parameters or divs and where the convex hull of set is
855  * known to be full-dimensional.
856  */
857 static struct isl_basic_set *uset_convex_hull(struct isl_set *set)
858 {
859         int i;
860         struct isl_basic_set *convex_hull = NULL;
861         struct isl_mat *bounds;
862
863         if (set->dim == 0) {
864                 convex_hull = isl_basic_set_universe(set->ctx, 0, 0);
865                 isl_set_free(set);
866                 convex_hull = isl_basic_set_set_rational(convex_hull);
867                 return convex_hull;
868         }
869
870         set = isl_set_set_rational(set);
871
872         if (!set)
873                 goto error;
874         for (i = 0; i < set->n; ++i) {
875                 set->p[i] = isl_basic_set_convex_hull(set->p[i]);
876                 if (!set->p[i])
877                         goto error;
878         }
879         set = isl_set_remove_empty_parts(set);
880         if (!set)
881                 goto error;
882         if (set->n == 1) {
883                 convex_hull = isl_basic_set_copy(set->p[0]);
884                 isl_set_free(set);
885                 return convex_hull;
886         }
887         if (set->dim == 1)
888                 return convex_hull_1d(set->ctx, set);
889
890         bounds = independent_bounds(set->ctx, set);
891         if (!bounds)
892                 goto error;
893         if (bounds->n_row < set->dim)
894                 return modulo_lineality(set->ctx, set, bounds);
895         bounds = initial_facet_constraint(set->ctx, set, bounds);
896         if (!bounds)
897                 goto error;
898         convex_hull = extend(set->ctx, set, bounds);
899         isl_mat_free(set->ctx, bounds);
900         isl_set_free(set);
901
902         return convex_hull;
903 error:
904         isl_set_free(set);
905         return NULL;
906 }
907
908 /* Compute the convex hull of set "set" with affine hull "affine_hull",
909  * We first remove the equalities (transforming the set), compute the
910  * convex hull of the transformed set and then add the equalities back
911  * (after performing the inverse transformation.
912  */
913 static struct isl_basic_set *modulo_affine_hull(struct isl_ctx *ctx,
914         struct isl_set *set, struct isl_basic_set *affine_hull)
915 {
916         struct isl_mat *T;
917         struct isl_mat *T2;
918         struct isl_basic_set *dummy;
919         struct isl_basic_set *convex_hull;
920
921         dummy = isl_basic_set_remove_equalities(
922                         isl_basic_set_copy(affine_hull), &T, &T2);
923         if (!dummy)
924                 goto error;
925         isl_basic_set_free(dummy);
926         set = isl_set_preimage(ctx, set, T);
927         convex_hull = uset_convex_hull(set);
928         convex_hull = isl_basic_set_preimage(ctx, convex_hull, T2);
929         convex_hull = isl_basic_set_intersect(convex_hull, affine_hull);
930         return convex_hull;
931 error:
932         isl_basic_set_free(affine_hull);
933         isl_set_free(set);
934         return NULL;
935 }
936
937 /* Compute the convex hull of a map.
938  *
939  * The implementation was inspired by "Extended Convex Hull" by Fukuda et al.,
940  * specifically, the wrapping of facets to obtain new facets.
941  */
942 struct isl_basic_map *isl_map_convex_hull(struct isl_map *map)
943 {
944         struct isl_basic_set *bset;
945         struct isl_basic_set *affine_hull = NULL;
946         struct isl_basic_map *convex_hull = NULL;
947         struct isl_set *set = NULL;
948         struct isl_ctx *ctx;
949
950         if (!map)
951                 goto error;
952
953         ctx = map->ctx;
954         if (map->n == 0) {
955                 convex_hull = isl_basic_map_empty(ctx,
956                                             map->nparam, map->n_in, map->n_out);
957                 isl_map_free(map);
958                 return convex_hull;
959         }
960
961         set = isl_map_underlying_set(isl_map_copy(map));
962         if (!set)
963                 goto error;
964
965         affine_hull = isl_set_affine_hull(isl_set_copy(set));
966         if (!affine_hull)
967                 goto error;
968         if (affine_hull->n_eq != 0)
969                 bset = modulo_affine_hull(ctx, set, affine_hull);
970         else {
971                 isl_basic_set_free(affine_hull);
972                 bset = uset_convex_hull(set);
973         }
974
975         convex_hull = isl_basic_map_overlying_set(bset,
976                         isl_basic_map_copy(map->p[0]));
977
978         isl_map_free(map);
979         return convex_hull;
980 error:
981         isl_set_free(set);
982         isl_map_free(map);
983         return NULL;
984 }
985
986 struct isl_basic_set *isl_set_convex_hull(struct isl_set *set)
987 {
988         return (struct isl_basic_set *)
989                 isl_map_convex_hull((struct isl_map *)set);
990 }