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