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