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