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