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