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