isl_map_simple_hull: use hash tables and tableaus
[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_bounded(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_set *set)
352 {
353         struct isl_basic_set *lp;
354         unsigned n_eq;
355         unsigned n_ineq;
356         int i, j, k;
357         unsigned dim, lp_dim;
358
359         if (!set)
360                 return NULL;
361
362         dim = 1 + isl_set_n_dim(set);
363         n_eq = 1;
364         n_ineq = set->n;
365         for (i = 0; i < set->n; ++i) {
366                 n_eq += set->p[i]->n_eq;
367                 n_ineq += set->p[i]->n_ineq;
368         }
369         lp = isl_basic_set_alloc(set->ctx, 0, dim * set->n, 0, n_eq, n_ineq);
370         if (!lp)
371                 return NULL;
372         lp_dim = isl_basic_set_n_dim(lp);
373         k = isl_basic_set_alloc_equality(lp);
374         isl_int_set_si(lp->eq[k][0], -1);
375         for (i = 0; i < set->n; ++i) {
376                 isl_int_set_si(lp->eq[k][1+dim*i], 0);
377                 isl_int_set_si(lp->eq[k][1+dim*i+1], 1);
378                 isl_seq_clr(lp->eq[k]+1+dim*i+2, dim-2);
379         }
380         for (i = 0; i < set->n; ++i) {
381                 k = isl_basic_set_alloc_inequality(lp);
382                 isl_seq_clr(lp->ineq[k], 1+lp_dim);
383                 isl_int_set_si(lp->ineq[k][1+dim*i], 1);
384
385                 for (j = 0; j < set->p[i]->n_eq; ++j) {
386                         k = isl_basic_set_alloc_equality(lp);
387                         isl_seq_clr(lp->eq[k], 1+dim*i);
388                         isl_seq_cpy(lp->eq[k]+1+dim*i, set->p[i]->eq[j], dim);
389                         isl_seq_clr(lp->eq[k]+1+dim*(i+1), dim*(set->n-i-1));
390                 }
391
392                 for (j = 0; j < set->p[i]->n_ineq; ++j) {
393                         k = isl_basic_set_alloc_inequality(lp);
394                         isl_seq_clr(lp->ineq[k], 1+dim*i);
395                         isl_seq_cpy(lp->ineq[k]+1+dim*i, set->p[i]->ineq[j], dim);
396                         isl_seq_clr(lp->ineq[k]+1+dim*(i+1), dim*(set->n-i-1));
397                 }
398         }
399         return lp;
400 }
401
402 /* Given a facet "facet" of the convex hull of "set" and a facet "ridge"
403  * of that facet, compute the other facet of the convex hull that contains
404  * the ridge.
405  *
406  * We first transform the set such that the facet constraint becomes
407  *
408  *                      x_1 >= 0
409  *
410  * I.e., the facet lies in
411  *
412  *                      x_1 = 0
413  *
414  * and on that facet, the constraint that defines the ridge is
415  *
416  *                      x_2 >= 0
417  *
418  * (This transformation is not strictly needed, all that is needed is
419  * that the ridge contains the origin.)
420  *
421  * Since the ridge contains the origin, the cone of the convex hull
422  * will be of the form
423  *
424  *                      x_1 >= 0
425  *                      x_2 >= a x_1
426  *
427  * with this second constraint defining the new facet.
428  * The constant a is obtained by settting x_1 in the cone of the
429  * convex hull to 1 and minimizing x_2.
430  * Now, each element in the cone of the convex hull is the sum
431  * of elements in the cones of the basic sets.
432  * If a_i is the dilation factor of basic set i, then the problem
433  * we need to solve is
434  *
435  *                      min \sum_i x_{i,2}
436  *                      st
437  *                              \sum_i x_{i,1} = 1
438  *                                  a_i   >= 0
439  *                                [ a_i ]
440  *                              A [ x_i ] >= 0
441  *
442  * with
443  *                                  [  1  ]
444  *                              A_i [ x_i ] >= 0
445  *
446  * the constraints of each (transformed) basic set.
447  * If a = n/d, then the constraint defining the new facet (in the transformed
448  * space) is
449  *
450  *                      -n x_1 + d x_2 >= 0
451  *
452  * In the original space, we need to take the same combination of the
453  * corresponding constraints "facet" and "ridge".
454  *
455  * If a = -infty = "-1/0", then we just return the original facet constraint.
456  * This means that the facet is unbounded, but has a bounded intersection
457  * with the union of sets.
458  */
459 static isl_int *wrap_facet(struct isl_set *set, isl_int *facet, isl_int *ridge)
460 {
461         int i;
462         struct isl_mat *T = NULL;
463         struct isl_basic_set *lp = NULL;
464         struct isl_vec *obj;
465         enum isl_lp_result res;
466         isl_int num, den;
467         unsigned dim;
468
469         set = isl_set_copy(set);
470
471         dim = 1 + isl_set_n_dim(set);
472         T = isl_mat_alloc(set->ctx, 3, dim);
473         if (!T)
474                 goto error;
475         isl_int_set_si(T->row[0][0], 1);
476         isl_seq_clr(T->row[0]+1, dim - 1);
477         isl_seq_cpy(T->row[1], facet, dim);
478         isl_seq_cpy(T->row[2], ridge, dim);
479         T = isl_mat_right_inverse(set->ctx, T);
480         set = isl_set_preimage(set, T);
481         T = NULL;
482         if (!set)
483                 goto error;
484         lp = wrap_constraints(set);
485         obj = isl_vec_alloc(set->ctx, dim*set->n);
486         if (!obj)
487                 goto error;
488         for (i = 0; i < set->n; ++i) {
489                 isl_seq_clr(obj->block.data+dim*i, 2);
490                 isl_int_set_si(obj->block.data[dim*i+2], 1);
491                 isl_seq_clr(obj->block.data+dim*i+3, dim-3);
492         }
493         isl_int_init(num);
494         isl_int_init(den);
495         res = isl_solve_lp((struct isl_basic_map *)lp, 0,
496                                     obj->block.data, set->ctx->one, &num, &den);
497         if (res == isl_lp_ok) {
498                 isl_int_neg(num, num);
499                 isl_seq_combine(facet, num, facet, den, ridge, dim);
500         }
501         isl_int_clear(num);
502         isl_int_clear(den);
503         isl_vec_free(set->ctx, obj);
504         isl_basic_set_free(lp);
505         isl_set_free(set);
506         isl_assert(set->ctx, res == isl_lp_ok || res == isl_lp_unbounded, 
507                    return NULL);
508         return facet;
509 error:
510         isl_basic_set_free(lp);
511         isl_mat_free(set->ctx, T);
512         isl_set_free(set);
513         return NULL;
514 }
515
516 /* Given a set of d linearly independent bounding constraints of the
517  * convex hull of "set", compute the constraint of a facet of "set".
518  *
519  * We first compute the intersection with the first bounding hyperplane
520  * and remove the component corresponding to this hyperplane from
521  * other bounds (in homogeneous space).
522  * We then wrap around one of the remaining bounding constraints
523  * and continue the process until all bounding constraints have been
524  * taken into account.
525  * The resulting linear combination of the bounding constraints will
526  * correspond to a facet of the convex hull.
527  */
528 static struct isl_mat *initial_facet_constraint(struct isl_ctx *ctx,
529         struct isl_set *set, struct isl_mat *bounds)
530 {
531         struct isl_set *slice = NULL;
532         struct isl_basic_set *face = NULL;
533         struct isl_mat *m, *U, *Q;
534         int i;
535         unsigned dim = isl_set_n_dim(set);
536
537         isl_assert(ctx, set->n > 0, goto error);
538         isl_assert(ctx, bounds->n_row == dim, goto error);
539
540         while (bounds->n_row > 1) {
541                 slice = isl_set_copy(set);
542                 slice = isl_set_add_equality(ctx, slice, bounds->row[0]);
543                 face = isl_set_affine_hull(slice);
544                 if (!face)
545                         goto error;
546                 if (face->n_eq == 1) {
547                         isl_basic_set_free(face);
548                         break;
549                 }
550                 m = isl_mat_alloc(ctx, 1 + face->n_eq, 1 + dim);
551                 if (!m)
552                         goto error;
553                 isl_int_set_si(m->row[0][0], 1);
554                 isl_seq_clr(m->row[0]+1, dim);
555                 for (i = 0; i < face->n_eq; ++i)
556                         isl_seq_cpy(m->row[1 + i], face->eq[i], 1 + dim);
557                 U = isl_mat_right_inverse(ctx, m);
558                 Q = isl_mat_right_inverse(ctx, isl_mat_copy(ctx, U));
559                 U = isl_mat_drop_cols(ctx, U, 1 + face->n_eq,
560                                                 dim - face->n_eq);
561                 Q = isl_mat_drop_rows(ctx, Q, 1 + face->n_eq,
562                                                 dim - face->n_eq);
563                 U = isl_mat_drop_cols(ctx, U, 0, 1);
564                 Q = isl_mat_drop_rows(ctx, Q, 0, 1);
565                 bounds = isl_mat_product(ctx, bounds, U);
566                 bounds = isl_mat_product(ctx, bounds, Q);
567                 while (isl_seq_first_non_zero(bounds->row[bounds->n_row-1],
568                                               bounds->n_col) == -1) {
569                         bounds->n_row--;
570                         isl_assert(ctx, bounds->n_row > 1, goto error);
571                 }
572                 if (!wrap_facet(set, bounds->row[0],
573                                           bounds->row[bounds->n_row-1]))
574                         goto error;
575                 isl_basic_set_free(face);
576                 bounds->n_row--;
577         }
578         return bounds;
579 error:
580         isl_basic_set_free(face);
581         isl_mat_free(ctx, bounds);
582         return NULL;
583 }
584
585 /* Given the bounding constraint "c" of a facet of the convex hull of "set",
586  * compute a hyperplane description of the facet, i.e., compute the facets
587  * of the facet.
588  *
589  * We compute an affine transformation that transforms the constraint
590  *
591  *                        [ 1 ]
592  *                      c [ x ] = 0
593  *
594  * to the constraint
595  *
596  *                         z_1  = 0
597  *
598  * by computing the right inverse U of a matrix that starts with the rows
599  *
600  *                      [ 1 0 ]
601  *                      [  c  ]
602  *
603  * Then
604  *                      [ 1 ]     [ 1 ]
605  *                      [ x ] = U [ z ]
606  * and
607  *                      [ 1 ]     [ 1 ]
608  *                      [ z ] = Q [ x ]
609  *
610  * with Q = U^{-1}
611  * Since z_1 is zero, we can drop this variable as well as the corresponding
612  * column of U to obtain
613  *
614  *                      [ 1 ]      [ 1  ]
615  *                      [ x ] = U' [ z' ]
616  * and
617  *                      [ 1  ]      [ 1 ]
618  *                      [ z' ] = Q' [ x ]
619  *
620  * with Q' equal to Q, but without the corresponding row.
621  * After computing the facets of the facet in the z' space,
622  * we convert them back to the x space through Q.
623  */
624 static struct isl_basic_set *compute_facet(struct isl_set *set, isl_int *c)
625 {
626         struct isl_mat *m, *U, *Q;
627         struct isl_basic_set *facet = NULL;
628         struct isl_ctx *ctx;
629         unsigned dim;
630
631         ctx = set->ctx;
632         set = isl_set_copy(set);
633         dim = isl_set_n_dim(set);
634         m = isl_mat_alloc(set->ctx, 2, 1 + dim);
635         if (!m)
636                 goto error;
637         isl_int_set_si(m->row[0][0], 1);
638         isl_seq_clr(m->row[0]+1, dim);
639         isl_seq_cpy(m->row[1], c, 1+dim);
640         U = isl_mat_right_inverse(set->ctx, m);
641         Q = isl_mat_right_inverse(set->ctx, isl_mat_copy(set->ctx, U));
642         U = isl_mat_drop_cols(set->ctx, U, 1, 1);
643         Q = isl_mat_drop_rows(set->ctx, Q, 1, 1);
644         set = isl_set_preimage(set, U);
645         facet = uset_convex_hull_wrap_bounded(set);
646         facet = isl_basic_set_preimage(facet, Q);
647         isl_assert(ctx, facet->n_eq == 0, goto error);
648         return facet;
649 error:
650         isl_basic_set_free(facet);
651         isl_set_free(set);
652         return NULL;
653 }
654
655 /* Given an initial facet constraint, compute the remaining facets.
656  * We do this by running through all facets found so far and computing
657  * the adjacent facets through wrapping, adding those facets that we
658  * hadn't already found before.
659  *
660  * For each facet we have found so far, we first compute its facets
661  * in the resulting convex hull.  That is, we compute the ridges
662  * of the resulting convex hull contained in the facet.
663  * We also compute the corresponding facet in the current approximation
664  * of the convex hull.  There is no need to wrap around the ridges
665  * in this facet since that would result in a facet that is already
666  * present in the current approximation.
667  *
668  * This function can still be significantly optimized by checking which of
669  * the facets of the basic sets are also facets of the convex hull and
670  * using all the facets so far to help in constructing the facets of the
671  * facets
672  * and/or
673  * using the technique in section "3.1 Ridge Generation" of
674  * "Extended Convex Hull" by Fukuda et al.
675  */
676 static struct isl_basic_set *extend(struct isl_basic_set *hull,
677         struct isl_set *set)
678 {
679         int i, j, f;
680         int k;
681         struct isl_basic_set *facet = NULL;
682         struct isl_basic_set *hull_facet = NULL;
683         unsigned total;
684         unsigned dim;
685
686         isl_assert(set->ctx, set->n > 0, goto error);
687
688         dim = isl_set_n_dim(set);
689
690         for (i = 0; i < hull->n_ineq; ++i) {
691                 facet = compute_facet(set, hull->ineq[i]);
692                 facet = isl_basic_set_add_equality(facet->ctx, facet, hull->ineq[i]);
693                 facet = isl_basic_set_gauss(facet, NULL);
694                 facet = isl_basic_set_normalize_constraints(facet);
695                 hull_facet = isl_basic_set_copy(hull);
696                 hull_facet = isl_basic_set_add_equality(hull_facet->ctx, hull_facet, hull->ineq[i]);
697                 hull_facet = isl_basic_set_gauss(hull_facet, NULL);
698                 hull_facet = isl_basic_set_normalize_constraints(hull_facet);
699                 if (!facet)
700                         goto error;
701                 if (facet->n_ineq + hull->n_ineq > hull->c_size)
702                         hull = isl_basic_set_extend_dim(hull,
703                                 isl_dim_copy(hull->dim), 0, 0, facet->n_ineq);
704                 for (j = 0; j < facet->n_ineq; ++j) {
705                         for (f = 0; f < hull_facet->n_ineq; ++f)
706                                 if (isl_seq_eq(facet->ineq[j],
707                                                 hull_facet->ineq[f], 1 + dim))
708                                         break;
709                         if (f < hull_facet->n_ineq)
710                                 continue;
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+dim);
715                         if (!wrap_facet(set, hull->ineq[k], facet->ineq[j]))
716                                 goto error;
717                 }
718                 isl_basic_set_free(hull_facet);
719                 isl_basic_set_free(facet);
720         }
721         hull = isl_basic_set_simplify(hull);
722         hull = isl_basic_set_finalize(hull);
723         return hull;
724 error:
725         isl_basic_set_free(hull_facet);
726         isl_basic_set_free(facet);
727         isl_basic_set_free(hull);
728         return NULL;
729 }
730
731 /* Special case for computing the convex hull of a one dimensional set.
732  * We simply collect the lower and upper bounds of each basic set
733  * and the biggest of those.
734  */
735 static struct isl_basic_set *convex_hull_1d(struct isl_ctx *ctx,
736         struct isl_set *set)
737 {
738         struct isl_mat *c = NULL;
739         isl_int *lower = NULL;
740         isl_int *upper = NULL;
741         int i, j, k;
742         isl_int a, b;
743         struct isl_basic_set *hull;
744
745         for (i = 0; i < set->n; ++i) {
746                 set->p[i] = isl_basic_set_simplify(set->p[i]);
747                 if (!set->p[i])
748                         goto error;
749         }
750         set = isl_set_remove_empty_parts(set);
751         if (!set)
752                 goto error;
753         isl_assert(ctx, set->n > 0, goto error);
754         c = isl_mat_alloc(ctx, 2, 2);
755         if (!c)
756                 goto error;
757
758         if (set->p[0]->n_eq > 0) {
759                 isl_assert(ctx, set->p[0]->n_eq == 1, goto error);
760                 lower = c->row[0];
761                 upper = c->row[1];
762                 if (isl_int_is_pos(set->p[0]->eq[0][1])) {
763                         isl_seq_cpy(lower, set->p[0]->eq[0], 2);
764                         isl_seq_neg(upper, set->p[0]->eq[0], 2);
765                 } else {
766                         isl_seq_neg(lower, set->p[0]->eq[0], 2);
767                         isl_seq_cpy(upper, set->p[0]->eq[0], 2);
768                 }
769         } else {
770                 for (j = 0; j < set->p[0]->n_ineq; ++j) {
771                         if (isl_int_is_pos(set->p[0]->ineq[j][1])) {
772                                 lower = c->row[0];
773                                 isl_seq_cpy(lower, set->p[0]->ineq[j], 2);
774                         } else {
775                                 upper = c->row[1];
776                                 isl_seq_cpy(upper, set->p[0]->ineq[j], 2);
777                         }
778                 }
779         }
780
781         isl_int_init(a);
782         isl_int_init(b);
783         for (i = 0; i < set->n; ++i) {
784                 struct isl_basic_set *bset = set->p[i];
785                 int has_lower = 0;
786                 int has_upper = 0;
787
788                 for (j = 0; j < bset->n_eq; ++j) {
789                         has_lower = 1;
790                         has_upper = 1;
791                         if (lower) {
792                                 isl_int_mul(a, lower[0], bset->eq[j][1]);
793                                 isl_int_mul(b, lower[1], bset->eq[j][0]);
794                                 if (isl_int_lt(a, b) && isl_int_is_pos(bset->eq[j][1]))
795                                         isl_seq_cpy(lower, bset->eq[j], 2);
796                                 if (isl_int_gt(a, b) && isl_int_is_neg(bset->eq[j][1]))
797                                         isl_seq_neg(lower, bset->eq[j], 2);
798                         }
799                         if (upper) {
800                                 isl_int_mul(a, upper[0], bset->eq[j][1]);
801                                 isl_int_mul(b, upper[1], bset->eq[j][0]);
802                                 if (isl_int_lt(a, b) && isl_int_is_pos(bset->eq[j][1]))
803                                         isl_seq_neg(upper, bset->eq[j], 2);
804                                 if (isl_int_gt(a, b) && isl_int_is_neg(bset->eq[j][1]))
805                                         isl_seq_cpy(upper, bset->eq[j], 2);
806                         }
807                 }
808                 for (j = 0; j < bset->n_ineq; ++j) {
809                         if (isl_int_is_pos(bset->ineq[j][1]))
810                                 has_lower = 1;
811                         if (isl_int_is_neg(bset->ineq[j][1]))
812                                 has_upper = 1;
813                         if (lower && isl_int_is_pos(bset->ineq[j][1])) {
814                                 isl_int_mul(a, lower[0], bset->ineq[j][1]);
815                                 isl_int_mul(b, lower[1], bset->ineq[j][0]);
816                                 if (isl_int_lt(a, b))
817                                         isl_seq_cpy(lower, bset->ineq[j], 2);
818                         }
819                         if (upper && isl_int_is_neg(bset->ineq[j][1])) {
820                                 isl_int_mul(a, upper[0], bset->ineq[j][1]);
821                                 isl_int_mul(b, upper[1], bset->ineq[j][0]);
822                                 if (isl_int_gt(a, b))
823                                         isl_seq_cpy(upper, bset->ineq[j], 2);
824                         }
825                 }
826                 if (!has_lower)
827                         lower = NULL;
828                 if (!has_upper)
829                         upper = NULL;
830         }
831         isl_int_clear(a);
832         isl_int_clear(b);
833
834         hull = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
835         hull = isl_basic_set_set_rational(hull);
836         if (!hull)
837                 goto error;
838         if (lower) {
839                 k = isl_basic_set_alloc_inequality(hull);
840                 isl_seq_cpy(hull->ineq[k], lower, 2);
841         }
842         if (upper) {
843                 k = isl_basic_set_alloc_inequality(hull);
844                 isl_seq_cpy(hull->ineq[k], upper, 2);
845         }
846         hull = isl_basic_set_finalize(hull);
847         isl_set_free(set);
848         isl_mat_free(ctx, c);
849         return hull;
850 error:
851         isl_set_free(set);
852         isl_mat_free(ctx, c);
853         return NULL;
854 }
855
856 /* Project out final n dimensions using Fourier-Motzkin */
857 static struct isl_set *set_project_out(struct isl_ctx *ctx,
858         struct isl_set *set, unsigned n)
859 {
860         return isl_set_remove_dims(set, isl_set_n_dim(set) - n, n);
861 }
862
863 static struct isl_basic_set *convex_hull_0d(struct isl_set *set)
864 {
865         struct isl_basic_set *convex_hull;
866
867         if (!set)
868                 return NULL;
869
870         if (isl_set_is_empty(set))
871                 convex_hull = isl_basic_set_empty(isl_dim_copy(set->dim));
872         else
873                 convex_hull = isl_basic_set_universe(isl_dim_copy(set->dim));
874         isl_set_free(set);
875         return convex_hull;
876 }
877
878 /* Compute the convex hull of a pair of basic sets without any parameters or
879  * integer divisions using Fourier-Motzkin elimination.
880  * The convex hull is the set of all points that can be written as
881  * the sum of points from both basic sets (in homogeneous coordinates).
882  * We set up the constraints in a space with dimensions for each of
883  * the three sets and then project out the dimensions corresponding
884  * to the two original basic sets, retaining only those corresponding
885  * to the convex hull.
886  */
887 static struct isl_basic_set *convex_hull_pair(struct isl_basic_set *bset1,
888         struct isl_basic_set *bset2)
889 {
890         int i, j, k;
891         struct isl_basic_set *bset[2];
892         struct isl_basic_set *hull = NULL;
893         unsigned dim;
894
895         if (!bset1 || !bset2)
896                 goto error;
897
898         dim = isl_basic_set_n_dim(bset1);
899         hull = isl_basic_set_alloc(bset1->ctx, 0, 2 + 3 * dim, 0,
900                                 1 + dim + bset1->n_eq + bset2->n_eq,
901                                 2 + bset1->n_ineq + bset2->n_ineq);
902         bset[0] = bset1;
903         bset[1] = bset2;
904         for (i = 0; i < 2; ++i) {
905                 for (j = 0; j < bset[i]->n_eq; ++j) {
906                         k = isl_basic_set_alloc_equality(hull);
907                         if (k < 0)
908                                 goto error;
909                         isl_seq_clr(hull->eq[k], (i+1) * (1+dim));
910                         isl_seq_clr(hull->eq[k]+(i+2)*(1+dim), (1-i)*(1+dim));
911                         isl_seq_cpy(hull->eq[k]+(i+1)*(1+dim), bset[i]->eq[j],
912                                         1+dim);
913                 }
914                 for (j = 0; j < bset[i]->n_ineq; ++j) {
915                         k = isl_basic_set_alloc_inequality(hull);
916                         if (k < 0)
917                                 goto error;
918                         isl_seq_clr(hull->ineq[k], (i+1) * (1+dim));
919                         isl_seq_clr(hull->ineq[k]+(i+2)*(1+dim), (1-i)*(1+dim));
920                         isl_seq_cpy(hull->ineq[k]+(i+1)*(1+dim),
921                                         bset[i]->ineq[j], 1+dim);
922                 }
923                 k = isl_basic_set_alloc_inequality(hull);
924                 if (k < 0)
925                         goto error;
926                 isl_seq_clr(hull->ineq[k], 1+2+3*dim);
927                 isl_int_set_si(hull->ineq[k][(i+1)*(1+dim)], 1);
928         }
929         for (j = 0; j < 1+dim; ++j) {
930                 k = isl_basic_set_alloc_equality(hull);
931                 if (k < 0)
932                         goto error;
933                 isl_seq_clr(hull->eq[k], 1+2+3*dim);
934                 isl_int_set_si(hull->eq[k][j], -1);
935                 isl_int_set_si(hull->eq[k][1+dim+j], 1);
936                 isl_int_set_si(hull->eq[k][2*(1+dim)+j], 1);
937         }
938         hull = isl_basic_set_set_rational(hull);
939         hull = isl_basic_set_remove_dims(hull, dim, 2*(1+dim));
940         hull = isl_basic_set_convex_hull(hull);
941         isl_basic_set_free(bset1);
942         isl_basic_set_free(bset2);
943         return hull;
944 error:
945         isl_basic_set_free(bset1);
946         isl_basic_set_free(bset2);
947         isl_basic_set_free(hull);
948         return NULL;
949 }
950
951 /* Compute the convex hull of a set without any parameters or
952  * integer divisions using Fourier-Motzkin elimination.
953  * In each step, we combined two basic sets until only one
954  * basic set is left.
955  */
956 static struct isl_basic_set *uset_convex_hull_elim(struct isl_set *set)
957 {
958         struct isl_basic_set *convex_hull = NULL;
959
960         convex_hull = isl_set_copy_basic_set(set);
961         set = isl_set_drop_basic_set(set, convex_hull);
962         if (!set)
963                 goto error;
964         while (set->n > 0) {
965                 struct isl_basic_set *t;
966                 t = isl_set_copy_basic_set(set);
967                 if (!t)
968                         goto error;
969                 set = isl_set_drop_basic_set(set, t);
970                 if (!set)
971                         goto error;
972                 convex_hull = convex_hull_pair(convex_hull, t);
973         }
974         isl_set_free(set);
975         return convex_hull;
976 error:
977         isl_set_free(set);
978         isl_basic_set_free(convex_hull);
979         return NULL;
980 }
981
982 /* Compute an initial hull for wrapping containing a single initial
983  * facet by first computing bounds on the set and then using these
984  * bounds to construct an initial facet.
985  * This function is a remnant of an older implementation where the
986  * bounds were also used to check whether the set was bounded.
987  * Since this function will now only be called when we know the
988  * set to be bounded, the initial facet should probably be constructed 
989  * by simply using the coordinate directions instead.
990  */
991 static struct isl_basic_set *initial_hull(struct isl_basic_set *hull,
992         struct isl_set *set)
993 {
994         struct isl_mat *bounds = NULL;
995         unsigned dim;
996         int k;
997
998         if (!hull)
999                 goto error;
1000         bounds = independent_bounds(set->ctx, set);
1001         if (!bounds)
1002                 goto error;
1003         isl_assert(set->ctx, bounds->n_row == isl_set_n_dim(set), goto error);
1004         bounds = initial_facet_constraint(set->ctx, set, bounds);
1005         if (!bounds)
1006                 goto error;
1007         k = isl_basic_set_alloc_inequality(hull);
1008         if (k < 0)
1009                 goto error;
1010         dim = isl_set_n_dim(set);
1011         isl_assert(set->ctx, 1 + dim == bounds->n_col, goto error);
1012         isl_seq_cpy(hull->ineq[k], bounds->row[0], bounds->n_col);
1013         isl_mat_free(set->ctx, bounds);
1014
1015         return hull;
1016 error:
1017         isl_basic_set_free(hull);
1018         isl_mat_free(set->ctx, bounds);
1019         return NULL;
1020 }
1021
1022 struct max_constraint {
1023         struct isl_mat *c;
1024         int             count;
1025         int             ineq;
1026 };
1027
1028 static int max_constraint_equal(const void *entry, const void *val)
1029 {
1030         struct max_constraint *a = (struct max_constraint *)entry;
1031         isl_int *b = (isl_int *)val;
1032
1033         return isl_seq_eq(a->c->row[0] + 1, b, a->c->n_col - 1);
1034 }
1035
1036 static void update_constraint(struct isl_ctx *ctx, struct isl_hash_table *table,
1037         isl_int *con, unsigned len, int n, int ineq)
1038 {
1039         struct isl_hash_table_entry *entry;
1040         struct max_constraint *c;
1041         uint32_t c_hash;
1042
1043         c_hash = isl_seq_hash(con + 1, len, isl_hash_init());
1044         entry = isl_hash_table_find(ctx, table, c_hash, max_constraint_equal,
1045                         con + 1, 0);
1046         if (!entry)
1047                 return;
1048         c = entry->data;
1049         if (c->count < n) {
1050                 isl_hash_table_remove(ctx, table, entry);
1051                 return;
1052         }
1053         c->count++;
1054         if (isl_int_gt(c->c->row[0][0], con[0]))
1055                 return;
1056         if (isl_int_eq(c->c->row[0][0], con[0])) {
1057                 if (ineq)
1058                         c->ineq = ineq;
1059                 return;
1060         }
1061         c->c = isl_mat_cow(ctx, c->c);
1062         isl_int_set(c->c->row[0][0], con[0]);
1063         c->ineq = ineq;
1064 }
1065
1066 /* Check whether the constraint hash table "table" constains the constraint
1067  * "con".
1068  */
1069 static int has_constraint(struct isl_ctx *ctx, struct isl_hash_table *table,
1070         isl_int *con, unsigned len, int n)
1071 {
1072         struct isl_hash_table_entry *entry;
1073         struct max_constraint *c;
1074         uint32_t c_hash;
1075
1076         c_hash = isl_seq_hash(con + 1, len, isl_hash_init());
1077         entry = isl_hash_table_find(ctx, table, c_hash, max_constraint_equal,
1078                         con + 1, 0);
1079         if (!entry)
1080                 return 0;
1081         c = entry->data;
1082         if (c->count < n)
1083                 return 0;
1084         return isl_int_eq(c->c->row[0][0], con[0]);
1085 }
1086
1087 /* Check for inequality constraints of a basic set without equalities
1088  * such that the same or more stringent copies of the constraint appear
1089  * in all of the basic sets.  Such constraints are necessarily facet
1090  * constraints of the convex hull.
1091  *
1092  * If the resulting basic set is by chance identical to one of
1093  * the basic sets in "set", then we know that this basic set contains
1094  * all other basic sets and is therefore the convex hull of set.
1095  * In this case we set *is_hull to 1.
1096  */
1097 static struct isl_basic_set *common_constraints(struct isl_basic_set *hull,
1098         struct isl_set *set, int *is_hull)
1099 {
1100         int i, j, s, n;
1101         int min_constraints;
1102         int best;
1103         struct max_constraint *constraints = NULL;
1104         struct isl_hash_table *table = NULL;
1105         unsigned total;
1106
1107         *is_hull = 0;
1108
1109         for (i = 0; i < set->n; ++i)
1110                 if (set->p[i]->n_eq == 0)
1111                         break;
1112         if (i >= set->n)
1113                 return hull;
1114         min_constraints = set->p[i]->n_ineq;
1115         best = i;
1116         for (i = best + 1; i < set->n; ++i) {
1117                 if (set->p[i]->n_eq != 0)
1118                         continue;
1119                 if (set->p[i]->n_ineq >= min_constraints)
1120                         continue;
1121                 min_constraints = set->p[i]->n_ineq;
1122                 best = i;
1123         }
1124         constraints = isl_calloc_array(hull->ctx, struct max_constraint,
1125                                         min_constraints);
1126         if (!constraints)
1127                 return hull;
1128         table = isl_alloc_type(hull->ctx, struct isl_hash_table);
1129         if (isl_hash_table_init(hull->ctx, table, min_constraints))
1130                 goto error;
1131
1132         total = isl_dim_total(set->dim);
1133         for (i = 0; i < set->p[best]->n_ineq; ++i) {
1134                 constraints[i].c = isl_mat_sub_alloc(hull->ctx,
1135                         set->p[best]->ineq + i, 0, 1, 0, 1 + total);
1136                 if (!constraints[i].c)
1137                         goto error;
1138                 constraints[i].ineq = 1;
1139         }
1140         for (i = 0; i < min_constraints; ++i) {
1141                 struct isl_hash_table_entry *entry;
1142                 uint32_t c_hash;
1143                 c_hash = isl_seq_hash(constraints[i].c->row[0] + 1, total,
1144                                         isl_hash_init());
1145                 entry = isl_hash_table_find(hull->ctx, table, c_hash,
1146                         max_constraint_equal, constraints[i].c->row[0] + 1, 1);
1147                 if (!entry)
1148                         goto error;
1149                 isl_assert(hull->ctx, !entry->data, goto error);
1150                 entry->data = &constraints[i];
1151         }
1152
1153         n = 0;
1154         for (s = 0; s < set->n; ++s) {
1155                 if (s == best)
1156                         continue;
1157
1158                 for (i = 0; i < set->p[s]->n_eq; ++i) {
1159                         isl_int *eq = set->p[s]->eq[i];
1160                         for (j = 0; j < 2; ++j) {
1161                                 isl_seq_neg(eq, eq, 1 + total);
1162                                 update_constraint(hull->ctx, table,
1163                                                             eq, total, n, 0);
1164                         }
1165                 }
1166                 for (i = 0; i < set->p[s]->n_ineq; ++i) {
1167                         isl_int *ineq = set->p[s]->ineq[i];
1168                         update_constraint(hull->ctx, table, ineq, total, n,
1169                                 set->p[s]->n_eq == 0);
1170                 }
1171                 ++n;
1172         }
1173
1174         for (i = 0; i < min_constraints; ++i) {
1175                 if (constraints[i].count < n)
1176                         continue;
1177                 if (!constraints[i].ineq)
1178                         continue;
1179                 j = isl_basic_set_alloc_inequality(hull);
1180                 if (j < 0)
1181                         goto error;
1182                 isl_seq_cpy(hull->ineq[j], constraints[i].c->row[0], 1 + total);
1183         }
1184
1185         for (s = 0; s < set->n; ++s) {
1186                 if (set->p[s]->n_eq)
1187                         continue;
1188                 if (set->p[s]->n_ineq != hull->n_ineq)
1189                         continue;
1190                 for (i = 0; i < set->p[s]->n_ineq; ++i) {
1191                         isl_int *ineq = set->p[s]->ineq[i];
1192                         if (!has_constraint(hull->ctx, table, ineq, total, n))
1193                                 break;
1194                 }
1195                 if (i == set->p[s]->n_ineq)
1196                         *is_hull = 1;
1197         }
1198
1199         isl_hash_table_clear(table);
1200         for (i = 0; i < min_constraints; ++i)
1201                 isl_mat_free(hull->ctx, constraints[i].c);
1202         free(constraints);
1203         free(table);
1204         return hull;
1205 error:
1206         isl_hash_table_clear(table);
1207         free(table);
1208         if (constraints)
1209                 for (i = 0; i < min_constraints; ++i)
1210                         isl_mat_free(hull->ctx, constraints[i].c);
1211         free(constraints);
1212         return hull;
1213 }
1214
1215 /* Create a template for the convex hull of "set" and fill it up
1216  * obvious facet constraints, if any.  If the result happens to
1217  * be the convex hull of "set" then *is_hull is set to 1.
1218  */
1219 static struct isl_basic_set *proto_hull(struct isl_set *set, int *is_hull)
1220 {
1221         struct isl_basic_set *hull;
1222         unsigned n_ineq;
1223         int i;
1224
1225         n_ineq = 1;
1226         for (i = 0; i < set->n; ++i) {
1227                 n_ineq += set->p[i]->n_eq;
1228                 n_ineq += set->p[i]->n_ineq;
1229         }
1230         hull = isl_basic_set_alloc_dim(isl_dim_copy(set->dim), 0, 0, n_ineq);
1231         hull = isl_basic_set_set_rational(hull);
1232         if (!hull)
1233                 return NULL;
1234         return common_constraints(hull, set, is_hull);
1235 }
1236
1237 static struct isl_basic_set *uset_convex_hull_wrap(struct isl_set *set)
1238 {
1239         struct isl_basic_set *hull;
1240         int is_hull;
1241
1242         hull = proto_hull(set, &is_hull);
1243         if (hull && !is_hull) {
1244                 if (hull->n_ineq == 0)
1245                         hull = initial_hull(hull, set);
1246                 hull = extend(hull, set);
1247         }
1248         isl_set_free(set);
1249
1250         return hull;
1251 }
1252
1253 static int isl_basic_set_is_bounded(struct isl_basic_set *bset)
1254 {
1255         struct isl_tab *tab;
1256         int bounded;
1257
1258         tab = isl_tab_from_recession_cone((struct isl_basic_map *)bset);
1259         bounded = isl_tab_cone_is_bounded(bset->ctx, tab);
1260         isl_tab_free(bset->ctx, tab);
1261         return bounded;
1262 }
1263
1264 static int isl_set_is_bounded(struct isl_set *set)
1265 {
1266         int i;
1267
1268         for (i = 0; i < set->n; ++i) {
1269                 int bounded = isl_basic_set_is_bounded(set->p[i]);
1270                 if (!bounded || bounded < 0)
1271                         return bounded;
1272         }
1273         return 1;
1274 }
1275
1276 /* Compute the convex hull of a set without any parameters or
1277  * integer divisions.  Depending on whether the set is bounded,
1278  * we pass control to the wrapping based convex hull or
1279  * the Fourier-Motzkin elimination based convex hull.
1280  * We also handle a few special cases before checking the boundedness.
1281  */
1282 static struct isl_basic_set *uset_convex_hull(struct isl_set *set)
1283 {
1284         int i;
1285         struct isl_basic_set *convex_hull = NULL;
1286
1287         if (isl_set_n_dim(set) == 0)
1288                 return convex_hull_0d(set);
1289
1290         set = isl_set_set_rational(set);
1291
1292         if (!set)
1293                 goto error;
1294         set = isl_set_normalize(set);
1295         if (!set)
1296                 return NULL;
1297         if (set->n == 1) {
1298                 convex_hull = isl_basic_set_copy(set->p[0]);
1299                 isl_set_free(set);
1300                 return convex_hull;
1301         }
1302         if (isl_set_n_dim(set) == 1)
1303                 return convex_hull_1d(set->ctx, set);
1304
1305         if (!isl_set_is_bounded(set))
1306                 return uset_convex_hull_elim(set);
1307
1308         return uset_convex_hull_wrap(set);
1309 error:
1310         isl_set_free(set);
1311         isl_basic_set_free(convex_hull);
1312         return NULL;
1313 }
1314
1315 /* This is the core procedure, where "set" is a "pure" set, i.e.,
1316  * without parameters or divs and where the convex hull of set is
1317  * known to be full-dimensional.
1318  */
1319 static struct isl_basic_set *uset_convex_hull_wrap_bounded(struct isl_set *set)
1320 {
1321         int i;
1322         struct isl_basic_set *convex_hull = NULL;
1323
1324         if (isl_set_n_dim(set) == 0) {
1325                 convex_hull = isl_basic_set_universe(isl_dim_copy(set->dim));
1326                 isl_set_free(set);
1327                 convex_hull = isl_basic_set_set_rational(convex_hull);
1328                 return convex_hull;
1329         }
1330
1331         set = isl_set_set_rational(set);
1332
1333         if (!set)
1334                 goto error;
1335         set = isl_set_normalize(set);
1336         if (!set)
1337                 goto error;
1338         if (set->n == 1) {
1339                 convex_hull = isl_basic_set_copy(set->p[0]);
1340                 isl_set_free(set);
1341                 return convex_hull;
1342         }
1343         if (isl_set_n_dim(set) == 1)
1344                 return convex_hull_1d(set->ctx, set);
1345
1346         return uset_convex_hull_wrap(set);
1347 error:
1348         isl_set_free(set);
1349         return NULL;
1350 }
1351
1352 /* Compute the convex hull of set "set" with affine hull "affine_hull",
1353  * We first remove the equalities (transforming the set), compute the
1354  * convex hull of the transformed set and then add the equalities back
1355  * (after performing the inverse transformation.
1356  */
1357 static struct isl_basic_set *modulo_affine_hull(struct isl_ctx *ctx,
1358         struct isl_set *set, struct isl_basic_set *affine_hull)
1359 {
1360         struct isl_mat *T;
1361         struct isl_mat *T2;
1362         struct isl_basic_set *dummy;
1363         struct isl_basic_set *convex_hull;
1364
1365         dummy = isl_basic_set_remove_equalities(
1366                         isl_basic_set_copy(affine_hull), &T, &T2);
1367         if (!dummy)
1368                 goto error;
1369         isl_basic_set_free(dummy);
1370         set = isl_set_preimage(set, T);
1371         convex_hull = uset_convex_hull(set);
1372         convex_hull = isl_basic_set_preimage(convex_hull, T2);
1373         convex_hull = isl_basic_set_intersect(convex_hull, affine_hull);
1374         return convex_hull;
1375 error:
1376         isl_basic_set_free(affine_hull);
1377         isl_set_free(set);
1378         return NULL;
1379 }
1380
1381 /* Compute the convex hull of a map.
1382  *
1383  * The implementation was inspired by "Extended Convex Hull" by Fukuda et al.,
1384  * specifically, the wrapping of facets to obtain new facets.
1385  */
1386 struct isl_basic_map *isl_map_convex_hull(struct isl_map *map)
1387 {
1388         struct isl_basic_set *bset;
1389         struct isl_basic_map *model = NULL;
1390         struct isl_basic_set *affine_hull = NULL;
1391         struct isl_basic_map *convex_hull = NULL;
1392         struct isl_set *set = NULL;
1393         struct isl_ctx *ctx;
1394
1395         if (!map)
1396                 goto error;
1397
1398         ctx = map->ctx;
1399         if (map->n == 0) {
1400                 convex_hull = isl_basic_map_empty_like_map(map);
1401                 isl_map_free(map);
1402                 return convex_hull;
1403         }
1404
1405         map = isl_map_align_divs(map);
1406         model = isl_basic_map_copy(map->p[0]);
1407         set = isl_map_underlying_set(map);
1408         if (!set)
1409                 goto error;
1410
1411         affine_hull = isl_set_affine_hull(isl_set_copy(set));
1412         if (!affine_hull)
1413                 goto error;
1414         if (affine_hull->n_eq != 0)
1415                 bset = modulo_affine_hull(ctx, set, affine_hull);
1416         else {
1417                 isl_basic_set_free(affine_hull);
1418                 bset = uset_convex_hull(set);
1419         }
1420
1421         convex_hull = isl_basic_map_overlying_set(bset, model);
1422
1423         ISL_F_CLR(convex_hull, ISL_BASIC_MAP_RATIONAL);
1424         return convex_hull;
1425 error:
1426         isl_set_free(set);
1427         isl_basic_map_free(model);
1428         return NULL;
1429 }
1430
1431 struct isl_basic_set *isl_set_convex_hull(struct isl_set *set)
1432 {
1433         return (struct isl_basic_set *)
1434                 isl_map_convex_hull((struct isl_map *)set);
1435 }
1436
1437 struct sh_data_entry {
1438         struct isl_hash_table   *table;
1439         struct isl_tab          *tab;
1440 };
1441
1442 /* Holds the data needed during the simple hull computation.
1443  * In particular,
1444  *      n               the number of basic sets in the original set
1445  *      hull_table      a hash table of already computed constraints
1446  *                      in the simple hull
1447  *      p               for each basic set,
1448  *              table           a hash table of the constraints
1449  *              tab             the tableau corresponding to the basic set
1450  */
1451 struct sh_data {
1452         struct isl_ctx          *ctx;
1453         unsigned                n;
1454         struct isl_hash_table   *hull_table;
1455         struct sh_data_entry    p[0];
1456 };
1457
1458 static void sh_data_free(struct sh_data *data)
1459 {
1460         int i;
1461
1462         if (!data)
1463                 return;
1464         isl_hash_table_free(data->ctx, data->hull_table);
1465         for (i = 0; i < data->n; ++i) {
1466                 isl_hash_table_free(data->ctx, data->p[i].table);
1467                 isl_tab_free(data->ctx, data->p[i].tab);
1468         }
1469         free(data);
1470 }
1471
1472 struct ineq_cmp_data {
1473         unsigned        len;
1474         isl_int         *p;
1475 };
1476
1477 static int has_ineq(const void *entry, const void *val)
1478 {
1479         isl_int *row = (isl_int *)entry;
1480         struct ineq_cmp_data *v = (struct ineq_cmp_data *)val;
1481
1482         return isl_seq_eq(row + 1, v->p + 1, v->len) ||
1483                isl_seq_is_neg(row + 1, v->p + 1, v->len);
1484 }
1485
1486 static int hash_ineq(struct isl_ctx *ctx, struct isl_hash_table *table,
1487                         isl_int *ineq, unsigned len)
1488 {
1489         uint32_t c_hash;
1490         struct ineq_cmp_data v;
1491         struct isl_hash_table_entry *entry;
1492
1493         v.len = len;
1494         v.p = ineq;
1495         c_hash = isl_seq_hash(ineq + 1, len, isl_hash_init());
1496         entry = isl_hash_table_find(ctx, table, c_hash, has_ineq, &v, 1);
1497         if (!entry)
1498                 return - 1;
1499         entry->data = ineq;
1500         return 0;
1501 }
1502
1503 /* Fill hash table "table" with the constraints of "bset".
1504  * Equalities are added as two inequalities.
1505  * The value in the hash table is a pointer to the (in)equality of "bset".
1506  */
1507 static int hash_basic_set(struct isl_hash_table *table,
1508                                 struct isl_basic_set *bset)
1509 {
1510         int i, j;
1511         unsigned dim = isl_basic_set_total_dim(bset);
1512
1513         for (i = 0; i < bset->n_eq; ++i) {
1514                 for (j = 0; j < 2; ++j) {
1515                         isl_seq_neg(bset->eq[i], bset->eq[i], 1 + dim);
1516                         if (hash_ineq(bset->ctx, table, bset->eq[i], dim) < 0)
1517                                 return -1;
1518                 }
1519         }
1520         for (i = 0; i < bset->n_ineq; ++i) {
1521                 if (hash_ineq(bset->ctx, table, bset->ineq[i], dim) < 0)
1522                         return -1;
1523         }
1524         return 0;
1525 }
1526
1527 static struct sh_data *sh_data_alloc(struct isl_set *set, unsigned n_ineq)
1528 {
1529         struct sh_data *data;
1530         int i;
1531
1532         data = isl_calloc(set->ctx, struct sh_data,
1533                 sizeof(struct sh_data) + set->n * sizeof(struct sh_data_entry));
1534         if (!data)
1535                 return NULL;
1536         data->ctx = set->ctx;
1537         data->n = set->n;
1538         data->hull_table = isl_hash_table_alloc(set->ctx, n_ineq);
1539         if (!data->hull_table)
1540                 goto error;
1541         for (i = 0; i < set->n; ++i) {
1542                 data->p[i].table = isl_hash_table_alloc(set->ctx,
1543                                     2 * set->p[i]->n_eq + set->p[i]->n_ineq);
1544                 if (!data->p[i].table)
1545                         goto error;
1546                 if (hash_basic_set(data->p[i].table, set->p[i]) < 0)
1547                         goto error;
1548         }
1549         return data;
1550 error:
1551         sh_data_free(data);
1552         return NULL;
1553 }
1554
1555 /* Check if inequality "ineq" is a bound for basic set "j" or if
1556  * it can be relaxed (by increasing the constant term) to become
1557  * a bound for that basic set.  In the latter case, the constant
1558  * term is updated.
1559  * Return 1 if "ineq" is a bound
1560  *        0 if "ineq" may attain arbitrarily small values on basic set "j"
1561  *       -1 if some error occurred
1562  */
1563 static int is_bound(struct sh_data *data, struct isl_set *set, int j,
1564                         isl_int *ineq)
1565 {
1566         enum isl_lp_result res;
1567         isl_int opt;
1568
1569         if (!data->p[j].tab) {
1570                 data->p[j].tab = isl_tab_from_basic_set(set->p[j]);
1571                 if (!data->p[j].tab)
1572                         return -1;
1573         }
1574
1575         isl_int_init(opt);
1576
1577         res = isl_tab_min(data->ctx, data->p[j].tab, ineq, data->ctx->one,
1578                                 &opt, NULL);
1579         if (res == isl_lp_ok && isl_int_is_neg(opt))
1580                 isl_int_sub(ineq[0], ineq[0], opt);
1581
1582         isl_int_clear(opt);
1583
1584         return res == isl_lp_ok ? 1 :
1585                res == isl_lp_unbounded ? 0 : -1;
1586 }
1587
1588 /* Check if inequality "ineq" from basic set "i" can be relaxed to
1589  * become a bound on the whole set.  If so, add the (relaxed) inequality
1590  * to "hull".
1591  *
1592  * We first check if "hull" already contains a translate of the inequality.
1593  * If so, we are done.
1594  * Then, we check if any of the previous basic sets contains a translate
1595  * of the inequality.  If so, then we have already considered this
1596  * inequality and we are done.
1597  * Otherwise, for each basic set other than "i", we check if the inequality
1598  * is a bound on the basic set.
1599  * For previous basic sets, we know that they do not contain a translate
1600  * of the inequality, so we directly call is_bound.
1601  * For following basic sets, we first check if a translate of the
1602  * inequality appears in its description and if so directly update
1603  * the inequality accordingly.
1604  */
1605 static struct isl_basic_set *add_bound(struct isl_basic_set *hull,
1606         struct sh_data *data, struct isl_set *set, int i, isl_int *ineq)
1607 {
1608         uint32_t c_hash;
1609         struct ineq_cmp_data v;
1610         struct isl_hash_table_entry *entry;
1611         int j, k;
1612
1613         if (!hull)
1614                 return NULL;
1615
1616         v.len = isl_basic_set_total_dim(hull);
1617         v.p = ineq;
1618         c_hash = isl_seq_hash(ineq + 1, v.len, isl_hash_init());
1619
1620         entry = isl_hash_table_find(hull->ctx, data->hull_table, c_hash,
1621                                         has_ineq, &v, 0);
1622         if (entry)
1623                 return hull;
1624
1625         for (j = 0; j < i; ++j) {
1626                 entry = isl_hash_table_find(hull->ctx, data->p[j].table,
1627                                                 c_hash, has_ineq, &v, 0);
1628                 if (entry)
1629                         break;
1630         }
1631         if (j < i)
1632                 return hull;
1633
1634         k = isl_basic_set_alloc_inequality(hull);
1635         isl_seq_cpy(hull->ineq[k], ineq, 1 + v.len);
1636         if (k < 0)
1637                 goto error;
1638
1639         for (j = 0; j < i; ++j) {
1640                 int bound;
1641                 bound = is_bound(data, set, j, hull->ineq[k]);
1642                 if (bound < 0)
1643                         goto error;
1644                 if (!bound)
1645                         break;
1646         }
1647         if (j < i) {
1648                 isl_basic_set_free_inequality(hull, 1);
1649                 return hull;
1650         }
1651
1652         for (j = i + 1; j < set->n; ++j) {
1653                 int bound, neg;
1654                 isl_int *ineq_j;
1655                 entry = isl_hash_table_find(hull->ctx, data->p[j].table,
1656                                                 c_hash, has_ineq, &v, 0);
1657                 if (entry) {
1658                         ineq_j = entry->data;
1659                         neg = isl_seq_is_neg(ineq_j + 1,
1660                                              hull->ineq[k] + 1, v.len);
1661                         if (neg)
1662                                 isl_int_neg(ineq_j[0], ineq_j[0]);
1663                         if (isl_int_gt(ineq_j[0], hull->ineq[k][0]))
1664                                 isl_int_set(hull->ineq[k][0], ineq_j[0]);
1665                         if (neg)
1666                                 isl_int_neg(ineq_j[0], ineq_j[0]);
1667                         continue;
1668                 }
1669                 bound = is_bound(data, set, j, hull->ineq[k]);
1670                 if (bound < 0)
1671                         goto error;
1672                 if (!bound)
1673                         break;
1674         }
1675         if (j < set->n) {
1676                 isl_basic_set_free_inequality(hull, 1);
1677                 return hull;
1678         }
1679
1680         entry = isl_hash_table_find(hull->ctx, data->hull_table, c_hash,
1681                                         has_ineq, &v, 1);
1682         if (!entry)
1683                 goto error;
1684         entry->data = hull->ineq[k];
1685
1686         return hull;
1687 error:
1688         isl_basic_set_free(hull);
1689         return NULL;
1690 }
1691
1692 /* Check if any inequality from basic set "i" can be relaxed to
1693  * become a bound on the whole set.  If so, add the (relaxed) inequality
1694  * to "hull".
1695  */
1696 static struct isl_basic_set *add_bounds(struct isl_basic_set *bset,
1697         struct sh_data *data, struct isl_set *set, int i)
1698 {
1699         int j, k;
1700         unsigned dim = isl_basic_set_total_dim(bset);
1701
1702         for (j = 0; j < set->p[i]->n_eq; ++j) {
1703                 for (k = 0; k < 2; ++k) {
1704                         isl_seq_neg(set->p[i]->eq[j], set->p[i]->eq[j], 1+dim);
1705                         add_bound(bset, data, set, i, set->p[i]->eq[j]);
1706                 }
1707         }
1708         for (j = 0; j < set->p[i]->n_ineq; ++j)
1709                 add_bound(bset, data, set, i, set->p[i]->ineq[j]);
1710         return bset;
1711 }
1712
1713 /* Compute a superset of the convex hull of set that is described
1714  * by only translates of the constraints in the constituents of set.
1715  */
1716 static struct isl_basic_set *uset_simple_hull(struct isl_set *set)
1717 {
1718         struct sh_data *data = NULL;
1719         struct isl_basic_set *hull = NULL;
1720         unsigned n_ineq;
1721         int i, j;
1722
1723         if (!set)
1724                 return NULL;
1725
1726         n_ineq = 0;
1727         for (i = 0; i < set->n; ++i) {
1728                 if (!set->p[i])
1729                         goto error;
1730                 n_ineq += 2 * set->p[i]->n_eq + set->p[i]->n_ineq;
1731         }
1732
1733         hull = isl_set_affine_hull(isl_set_copy(set));
1734         if (!hull)
1735                 goto error;
1736         hull = isl_basic_set_extend_dim(hull, isl_dim_copy(hull->dim),
1737                                         0, 0, n_ineq);
1738         if (!hull)
1739                 goto error;
1740
1741         data = sh_data_alloc(set, n_ineq);
1742         if (!data)
1743                 goto error;
1744         if (hash_basic_set(data->hull_table, hull) < 0)
1745                 goto error;
1746
1747         for (i = 0; i < set->n; ++i)
1748                 hull = add_bounds(hull, data, set, i);
1749
1750         hull = isl_basic_set_convex_hull(hull);
1751
1752         sh_data_free(data);
1753         isl_set_free(set);
1754
1755         return hull;
1756 error:
1757         sh_data_free(data);
1758         isl_basic_set_free(hull);
1759         isl_set_free(set);
1760         return NULL;
1761 }
1762
1763 /* Compute a superset of the convex hull of map that is described
1764  * by only translates of the constraints in the constituents of map.
1765  */
1766 struct isl_basic_map *isl_map_simple_hull(struct isl_map *map)
1767 {
1768         struct isl_set *set = NULL;
1769         struct isl_basic_map *model = NULL;
1770         struct isl_basic_map *hull;
1771         struct isl_basic_set *bset = NULL;
1772
1773         if (!map)
1774                 return NULL;
1775         if (map->n == 0) {
1776                 hull = isl_basic_map_empty_like_map(map);
1777                 isl_map_free(map);
1778                 return hull;
1779         }
1780         if (map->n == 1) {
1781                 hull = isl_basic_map_copy(map->p[0]);
1782                 isl_map_free(map);
1783                 return hull;
1784         }
1785
1786         map = isl_map_align_divs(map);
1787         model = isl_basic_map_copy(map->p[0]);
1788
1789         set = isl_map_underlying_set(map);
1790
1791         bset = uset_simple_hull(set);
1792
1793         hull = isl_basic_map_overlying_set(bset, model);
1794
1795         return hull;
1796 }
1797
1798 struct isl_basic_set *isl_set_simple_hull(struct isl_set *set)
1799 {
1800         return (struct isl_basic_set *)
1801                 isl_map_simple_hull((struct isl_map *)set);
1802 }