isl_pip_basic_map_compute_divs: don't turn divs into output dimensions
[platform/upstream/isl.git] / isl_map_piplib.c
1 #include "isl_set.h"
2 #include "isl_map.h"
3 #include "isl_mat.h"
4 #include "isl_seq.h"
5 #include "isl_piplib.h"
6 #include "isl_map_piplib.h"
7 #include "isl_map_private.h"
8 #include "isl_equalities.h"
9
10 static void copy_values_from(isl_int *dst, Entier *src, unsigned n)
11 {
12         int i;
13
14         for (i = 0; i < n; ++i)
15                 entier_assign(dst[i], src[i]);
16 }
17
18 static void add_value(isl_int *dst, Entier *src)
19 {
20         mpz_add(*dst, *dst, *src);
21 }
22
23 static void copy_constraint_from(isl_int *dst, PipVector *src,
24                 unsigned nparam, unsigned n_in, unsigned n_out,
25                 unsigned extra, int *pos)
26 {
27         int i;
28
29         copy_values_from(dst, src->the_vector+src->nb_elements-1, 1);
30         copy_values_from(dst+1, src->the_vector, nparam+n_in);
31         isl_seq_clr(dst+1+nparam+n_in, n_out);
32         isl_seq_clr(dst+1+nparam+n_in+n_out, extra);
33         for (i = 0; i + n_in + nparam < src->nb_elements-1; ++i) {
34                 int p = pos[i];
35                 add_value(&dst[1+nparam+n_in+n_out+p],
36                           &src->the_vector[n_in+nparam+i]);
37         }
38 }
39
40 static int add_inequality(struct isl_ctx *ctx,
41                    struct isl_basic_map *bmap, int *pos, PipVector *vec)
42 {
43         unsigned nparam = isl_basic_map_n_param(bmap);
44         unsigned n_in = isl_basic_map_n_in(bmap);
45         unsigned n_out = isl_basic_map_n_out(bmap);
46         unsigned n_div = isl_basic_map_n_div(bmap);
47         int i = isl_basic_map_alloc_inequality(bmap);
48         if (i < 0)
49                 return -1;
50         copy_constraint_from(bmap->ineq[i], vec,
51             nparam, n_in, n_out, n_div, pos);
52
53         return i;
54 }
55
56 /* For a div d = floor(f/m), add the constraints
57  *
58  *              f - m d >= 0
59  *              -(f-(n-1)) + m d >= 0
60  *
61  * Note that the second constraint is the negation of
62  *
63  *              f - m d >= n
64  */
65 static int add_div_constraints(struct isl_ctx *ctx,
66         struct isl_basic_map *bmap, int *pos, PipNewparm *p, unsigned div)
67 {
68         int i, j;
69         unsigned total = isl_basic_map_total_dim(bmap);
70         unsigned div_pos = 1 + total - bmap->n_div + div;
71
72         i = add_inequality(ctx, bmap, pos, p->vector);
73         if (i < 0)
74                 return -1;
75         copy_values_from(&bmap->ineq[i][div_pos], &p->deno, 1);
76         isl_int_neg(bmap->ineq[i][div_pos], bmap->ineq[i][div_pos]);
77
78         j = isl_basic_map_alloc_inequality(bmap);
79         if (j < 0)
80                 return -1;
81         isl_seq_neg(bmap->ineq[j], bmap->ineq[i], 1 + total);
82         isl_int_add(bmap->ineq[j][0], bmap->ineq[j][0], bmap->ineq[j][div_pos]);
83         isl_int_sub_ui(bmap->ineq[j][0], bmap->ineq[j][0], 1);
84         return j;
85 }
86
87 static int add_equality(struct isl_ctx *ctx,
88                    struct isl_basic_map *bmap, int *pos,
89                    unsigned var, PipVector *vec)
90 {
91         int i;
92         unsigned nparam = isl_basic_map_n_param(bmap);
93         unsigned n_in = isl_basic_map_n_in(bmap);
94         unsigned n_out = isl_basic_map_n_out(bmap);
95
96         isl_assert(ctx, var < n_out, return -1);
97
98         i = isl_basic_map_alloc_equality(bmap);
99         if (i < 0)
100                 return -1;
101         copy_constraint_from(bmap->eq[i], vec,
102             nparam, n_in, n_out, bmap->extra, pos);
103         isl_int_set_si(bmap->eq[i][1+nparam+n_in+var], -1);
104
105         return i;
106 }
107
108 static int find_div(struct isl_ctx *ctx,
109                    struct isl_basic_map *bmap, int *pos, PipNewparm *p)
110 {
111         int i, j;
112         unsigned nparam = isl_basic_map_n_param(bmap);
113         unsigned n_in = isl_basic_map_n_in(bmap);
114         unsigned n_out = isl_basic_map_n_out(bmap);
115
116         i = isl_basic_map_alloc_div(bmap);
117         if (i < 0)
118                 return -1;
119
120         copy_constraint_from(bmap->div[i]+1, p->vector,
121             nparam, n_in, n_out, bmap->extra, pos);
122
123         copy_values_from(bmap->div[i], &p->deno, 1);
124         for (j = 0; j < i; ++j)
125                 if (isl_seq_eq(bmap->div[i], bmap->div[j],
126                                 1+1+isl_basic_map_total_dim(bmap)+j)) {
127                         isl_basic_map_free_div(bmap, 1);
128                         return j;
129                 }
130
131         if (add_div_constraints(ctx, bmap, pos, p, i) < 0)
132                 return -1;
133
134         return i;
135 }
136
137 /* Count some properties of a quast
138  * - maximal number of new parameters
139  * - maximal depth
140  * - total number of solutions
141  * - total number of empty branches
142  */
143 static void quast_count(PipQuast *q, int *maxnew, int depth, int *maxdepth,
144                         int *sol, int *nosol)
145 {
146         PipNewparm *p;
147
148         for (p = q->newparm; p; p = p->next)
149                 if (p->rank > *maxnew)
150                         *maxnew = p->rank;
151         if (q->condition) {
152                 if (++depth > *maxdepth)
153                         *maxdepth = depth;
154                 quast_count(q->next_else, maxnew, depth, maxdepth, sol, nosol);
155                 quast_count(q->next_then, maxnew, depth, maxdepth, sol, nosol);
156         } else {
157                 if (q->list)
158                         ++(*sol);
159                 else
160                         ++(*nosol);
161         }
162 }
163
164 /*
165  * pos: array of length bmap->set.extra, mapping each of the existential
166  *              variables PIP proposes to an existential variable in bmap
167  * bmap: collects the currently active constraints
168  * rest: collects the empty leaves of the quast (if not NULL)
169  */
170 struct scan_data {
171         struct isl_ctx                  *ctx;
172         struct isl_basic_map            *bmap;
173         struct isl_set                  **rest;
174         int        *pos;
175 };
176
177 /*
178  * New existentially quantified variables are places after the existing ones.
179  */
180 static struct isl_map *scan_quast_r(struct scan_data *data, PipQuast *q,
181                                     struct isl_map *map)
182 {
183         PipNewparm *p;
184         struct isl_basic_map *bmap = data->bmap;
185         unsigned old_n_div = bmap->n_div;
186         unsigned nparam = isl_basic_map_n_param(bmap);
187         unsigned n_in = isl_basic_map_n_in(bmap);
188         unsigned n_out = isl_basic_map_n_out(bmap);
189
190         if (!map)
191                 goto error;
192
193         for (p = q->newparm; p; p = p->next) {
194                 int pos;
195                 unsigned pip_param = nparam + n_in;
196
197                 pos = find_div(data->ctx, bmap, data->pos, p);
198                 if (pos < 0)
199                         goto error;
200                 data->pos[p->rank - pip_param] = pos;
201         }
202
203         if (q->condition) {
204                 int pos = add_inequality(data->ctx, bmap, data->pos,
205                                          q->condition);
206                 if (pos < 0)
207                         goto error;
208                 map = scan_quast_r(data, q->next_then, map);
209
210                 if (isl_inequality_negate(bmap, pos))
211                         goto error;
212                 map = scan_quast_r(data, q->next_else, map);
213
214                 if (isl_basic_map_free_inequality(bmap, 1))
215                         goto error;
216         } else if (q->list) {
217                 PipList *l;
218                 int j;
219                 /* if bmap->n_out is zero, we are only interested in the domains
220                  * where a solution exists and not in the actual solution
221                  */
222                 for (j = 0, l = q->list; j < n_out && l; ++j, l = l->next)
223                         if (add_equality(data->ctx, bmap, data->pos, j,
224                                                 l->vector) < 0)
225                                 goto error;
226                 map = isl_map_add(map, isl_basic_map_copy(bmap));
227                 if (isl_basic_map_free_equality(bmap, n_out))
228                         goto error;
229         } else if (data->rest) {
230                 struct isl_basic_set *bset;
231                 bset = isl_basic_set_from_basic_map(isl_basic_map_copy(bmap));
232                 bset = isl_basic_set_drop_dims(bset, n_in, n_out);
233                 if (!bset)
234                         goto error;
235                 *data->rest = isl_set_add(*data->rest, bset);
236         }
237
238         if (isl_basic_map_free_inequality(bmap, 2*(bmap->n_div - old_n_div)))
239                 goto error;
240         if (isl_basic_map_free_div(bmap, bmap->n_div - old_n_div))
241                 goto error;
242         return map;
243 error:
244         isl_map_free(map);
245         return NULL;
246 }
247
248 /*
249  * Returns a map of dimension "keep_dim" with "context" as domain and
250  * as range the first "isl_dim_size(keep_dim, isl_dim_out)" variables
251  * in the quast lists.
252  */
253 static struct isl_map *isl_map_from_quast(struct isl_ctx *ctx, PipQuast *q,
254                 struct isl_dim *keep_dim,
255                 struct isl_basic_set *context,
256                 struct isl_set **rest)
257 {
258         int             pip_param;
259         int             nexist;
260         int             max_depth;
261         int             n_sol, n_nosol;
262         struct scan_data        data;
263         struct isl_map          *map = NULL;
264         struct isl_dim          *dims;
265         unsigned                nparam;
266         unsigned                dim;
267         unsigned                keep;
268
269         data.ctx = ctx;
270         data.rest = rest;
271         data.bmap = NULL;
272         data.pos = NULL;
273
274         if (!context || !keep_dim)
275                 goto error;
276
277         dim = isl_basic_set_n_dim(context);
278         nparam = isl_basic_set_n_param(context);
279         keep = isl_dim_size(keep_dim, isl_dim_out);
280         pip_param = nparam + dim;
281
282         max_depth = 0;
283         n_sol = 0;
284         n_nosol = 0;
285         nexist = pip_param-1;
286         quast_count(q, &nexist, 0, &max_depth, &n_sol, &n_nosol);
287         nexist -= pip_param-1;
288
289         if (rest) {
290                 *rest = isl_set_alloc_dim(isl_dim_copy(context->dim), n_nosol,
291                                         ISL_MAP_DISJOINT);
292                 if (!*rest)
293                         goto error;
294         }
295         map = isl_map_alloc_dim(isl_dim_copy(keep_dim), n_sol,
296                                 ISL_MAP_DISJOINT);
297         if (!map)
298                 goto error;
299
300         dims = isl_dim_reverse(isl_dim_copy(context->dim));
301         data.bmap = isl_basic_map_from_basic_set(context, dims);
302         data.bmap = isl_basic_map_extend_dim(data.bmap,
303                 keep_dim, nexist, keep, max_depth+2*nexist);
304         if (!data.bmap)
305                 goto error2;
306
307         if (data.bmap->extra) {
308                 int i;
309                 data.pos = isl_alloc_array(ctx, int, data.bmap->extra);
310                 if (!data.pos)
311                         goto error;
312                 for (i = 0; i < data.bmap->n_div; ++i)
313                         data.pos[i] = i;
314         }
315
316         map = scan_quast_r(&data, q, map);
317         map = isl_map_finalize(map);
318         if (!map)
319                 goto error2;
320         if (rest) {
321                 *rest = isl_set_finalize(*rest);
322                 if (!*rest)
323                         goto error2;
324         }
325         isl_basic_map_free(data.bmap);
326         if (data.pos)
327                 free(data.pos);
328         return map;
329 error:
330         isl_basic_set_free(context);
331         isl_dim_free(keep_dim);
332 error2:
333         if (data.pos)
334                 free(data.pos);
335         isl_basic_map_free(data.bmap);
336         isl_map_free(map);
337         if (rest) {
338                 isl_set_free(*rest);
339                 *rest = NULL;
340         }
341         return NULL;
342 }
343
344 static void copy_values_to(Entier *dst, isl_int *src, unsigned n)
345 {
346         int i;
347
348         for (i = 0; i < n; ++i)
349                 entier_assign(dst[i], src[i]);
350 }
351
352 static void copy_constraint_to(Entier *dst, isl_int *src,
353                 unsigned pip_param, unsigned pip_var,
354                 unsigned extra_front, unsigned extra_back)
355 {
356         copy_values_to(dst+1+extra_front+pip_var+pip_param+extra_back, src, 1);
357         copy_values_to(dst+1+extra_front+pip_var, src+1, pip_param);
358         copy_values_to(dst+1+extra_front, src+1+pip_param, pip_var);
359 }
360
361 PipMatrix *isl_basic_map_to_pip(struct isl_basic_map *bmap, unsigned pip_param,
362                          unsigned extra_front, unsigned extra_back)
363 {
364         int i;
365         unsigned nrow;
366         unsigned ncol;
367         PipMatrix *M;
368         unsigned off;
369         unsigned pip_var = isl_basic_map_total_dim(bmap) - pip_param;
370
371         nrow = extra_front + bmap->n_eq + bmap->n_ineq;
372         ncol = 1 + extra_front + pip_var + pip_param + extra_back + 1;
373         M = pip_matrix_alloc(nrow, ncol);
374         if (!M)
375                 return NULL;
376
377         off = extra_front;
378         for (i = 0; i < bmap->n_eq; ++i) {
379                 entier_set_si(M->p[off+i][0], 0);
380                 copy_constraint_to(M->p[off+i], bmap->eq[i],
381                                    pip_param, pip_var, extra_front, extra_back);
382         }
383         off += bmap->n_eq;
384         for (i = 0; i < bmap->n_ineq; ++i) {
385                 entier_set_si(M->p[off+i][0], 1);
386                 copy_constraint_to(M->p[off+i], bmap->ineq[i],
387                                    pip_param, pip_var, extra_front, extra_back);
388         }
389         return M;
390 }
391
392 PipMatrix *isl_basic_set_to_pip(struct isl_basic_set *bset, unsigned pip_param,
393                          unsigned extra_front, unsigned extra_back)
394 {
395         return isl_basic_map_to_pip((struct isl_basic_map *)bset,
396                                         pip_param, extra_front, extra_back);
397 }
398
399 struct isl_map *isl_pip_basic_map_lexopt(
400                 struct isl_basic_map *bmap, struct isl_basic_set *dom,
401                 struct isl_set **empty, int max)
402 {
403         PipOptions      *options;
404         PipQuast        *sol;
405         struct isl_map  *map;
406         struct isl_ctx  *ctx;
407         PipMatrix *domain = NULL, *context = NULL;
408         unsigned         nparam, n_in, n_out;
409
410         bmap = isl_basic_map_detect_equalities(bmap);
411         if (!bmap || !dom)
412                 goto error;
413
414         ctx = bmap->ctx;
415         isl_assert(ctx, isl_basic_map_compatible_domain(bmap, dom), goto error);
416         nparam = isl_basic_map_n_param(bmap);
417         n_in = isl_basic_map_n_in(bmap);
418         n_out = isl_basic_map_n_out(bmap);
419
420         domain = isl_basic_map_to_pip(bmap, nparam + n_in, 0, dom->n_div);
421         if (!domain)
422                 goto error;
423         context = isl_basic_map_to_pip((struct isl_basic_map *)dom, 0, 0, 0);
424         if (!context)
425                 goto error;
426
427         options = pip_options_init();
428         options->Simplify = 1;
429         options->Maximize = max;
430         options->Urs_unknowns = -1;
431         options->Urs_parms = -1;
432         sol = pip_solve(domain, context, -1, options);
433
434         if (sol) {
435                 struct isl_basic_set *copy;
436                 copy = isl_basic_set_copy(dom);
437                 map = isl_map_from_quast(ctx, sol,
438                                 isl_dim_copy(bmap->dim), copy, empty);
439         } else {
440                 map = isl_map_empty_like_basic_map(bmap);
441                 if (empty)
442                         *empty = NULL;
443         }
444         if (!map)
445                 goto error;
446         if (map->n == 0 && empty) {
447                 isl_set_free(*empty);
448                 *empty = isl_set_from_basic_set(dom);
449         } else
450                 isl_basic_set_free(dom);
451         isl_basic_map_free(bmap);
452
453         pip_quast_free(sol);
454         pip_options_free(options);
455         pip_matrix_free(domain);
456         pip_matrix_free(context);
457
458         return map;
459 error:
460         if (domain)
461                 pip_matrix_free(domain);
462         if (context)
463                 pip_matrix_free(context);
464         isl_basic_map_free(bmap);
465         isl_basic_set_free(dom);
466         return NULL;
467 }
468
469 /* Project the given basic set onto its parameter domain, possibly introducing
470  * new, explicit, existential variables in the constraints.
471  * The input has parameters and output variables.
472  * The output has the same parameters, but no output variables, only
473  * explicit existentially quantified variables.
474  */
475 static struct isl_set *compute_divs_no_eq(struct isl_basic_set *bset)
476 {
477         PipMatrix *domain = NULL, *context = NULL;
478         PipOptions      *options;
479         PipQuast        *sol;
480         struct isl_ctx  *ctx;
481         struct isl_dim  *dim;
482         struct isl_map  *map;
483         struct isl_set  *set;
484         struct isl_basic_set    *dom;
485         unsigned         nparam;
486
487         if (!bset)
488                 goto error;
489
490         ctx = bset->ctx;
491         nparam = isl_basic_set_n_param(bset);
492
493         domain = isl_basic_set_to_pip(bset, nparam, 0, 0);
494         if (!domain)
495                 goto error;
496         context = pip_matrix_alloc(0, nparam + 2);
497         if (!context)
498                 goto error;
499
500         options = pip_options_init();
501         options->Simplify = 1;
502         options->Urs_unknowns = -1;
503         options->Urs_parms = -1;
504         sol = pip_solve(domain, context, -1, options);
505
506         dom = isl_basic_set_alloc(ctx, nparam, 0, 0, 0, 0);
507         map = isl_map_from_quast(ctx, sol, isl_dim_copy(dom->dim), dom, NULL);
508         set = (struct isl_set *)map;
509
510         pip_quast_free(sol);
511         pip_options_free(options);
512         pip_matrix_free(domain);
513         pip_matrix_free(context);
514
515         isl_basic_set_free(bset);
516
517         return set;
518 error:
519         if (domain)
520                 pip_matrix_free(domain);
521         if (context)
522                 pip_matrix_free(context);
523         isl_basic_set_free(dom);
524         isl_basic_set_free(bset);
525         return NULL;
526 }
527
528 static struct isl_map *isl_map_reset_dim(struct isl_map *map,
529         struct isl_dim *dim)
530 {
531         int i;
532
533         if (!map || !dim)
534                 goto error;
535
536         for (i = 0; i < map->n; ++i) {
537                 isl_dim_free(map->p[i]->dim);
538                 map->p[i]->dim = isl_dim_copy(dim);
539         }
540         isl_dim_free(map->dim);
541         map->dim = dim;
542
543         return map;
544 error:
545         isl_map_free(map);
546         isl_dim_free(dim);
547         return NULL;
548 }
549
550 static struct isl_set *isl_set_reset_dim(struct isl_set *set,
551         struct isl_dim *dim)
552 {
553         return (struct isl_set *) isl_map_reset_dim((struct isl_map *)set, dim);
554 }
555
556 /* Apply a preimage specified by "mat" on the parameters of "bset".
557  * bset is assumed to have only parameters and divs.
558  */
559 static struct isl_basic_set *basic_set_parameter_preimage(
560         struct isl_basic_set *bset, struct isl_mat *mat)
561 {
562         unsigned nparam;
563
564         if (!bset || !mat)
565                 goto error;
566
567         bset->dim = isl_dim_cow(bset->dim);
568         if (!bset->dim)
569                 goto error;
570
571         nparam = isl_basic_set_dim(bset, isl_dim_param);
572
573         isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
574
575         bset->dim->nparam = 0;
576         bset->dim->n_out = nparam;
577         bset = isl_basic_set_preimage(bset, mat);
578         if (bset) {
579                 bset->dim->nparam = bset->dim->n_out;
580                 bset->dim->n_out = 0;
581         }
582         return bset;
583 error:
584         isl_mat_free(mat);
585         isl_basic_set_free(bset);
586         return NULL;
587 }
588
589 /* Apply a preimage specified by "mat" on the parameters of "set".
590  * set is assumed to have only parameters and divs.
591  */
592 static struct isl_set *set_parameter_preimage(
593         struct isl_set *set, struct isl_mat *mat)
594 {
595         struct isl_dim *dim = NULL;
596         unsigned nparam;
597
598         if (!set || !mat)
599                 goto error;
600
601         dim = isl_dim_copy(set->dim);
602         dim = isl_dim_cow(dim);
603         if (!dim)
604                 goto error;
605
606         nparam = isl_set_dim(set, isl_dim_param);
607
608         isl_assert(set->ctx, mat->n_row == 1 + nparam, goto error);
609
610         dim->nparam = 0;
611         dim->n_out = nparam;
612         isl_set_reset_dim(set, dim);
613         set = isl_set_preimage(set, mat);
614         if (!set)
615                 goto error2;
616         dim = isl_dim_copy(set->dim);
617         dim = isl_dim_cow(dim);
618         if (!dim)
619                 goto error2;
620         dim->nparam = dim->n_out;
621         dim->n_out = 0;
622         isl_set_reset_dim(set, dim);
623         return set;
624 error:
625         isl_dim_free(dim);
626         isl_mat_free(mat);
627 error2:
628         isl_set_free(set);
629         return NULL;
630 }
631
632 /* Intersect the basic set "bset" with the affine space specified by the
633  * equalities in "eq".
634  */
635 static struct isl_basic_set *basic_set_append_equalities(
636         struct isl_basic_set *bset, struct isl_mat *eq)
637 {
638         int i, k;
639         unsigned len;
640
641         if (!bset || !eq)
642                 goto error;
643
644         bset = isl_basic_set_extend_dim(bset, isl_dim_copy(bset->dim), 0,
645                                         eq->n_row, 0);
646         if (!bset)
647                 goto error;
648
649         len = 1 + isl_dim_total(bset->dim) + bset->extra;
650         for (i = 0; i < eq->n_row; ++i) {
651                 k = isl_basic_set_alloc_equality(bset);
652                 if (k < 0)
653                         goto error;
654                 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
655                 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
656         }
657         isl_mat_free(eq);
658
659         return bset;
660 error:
661         isl_mat_free(eq);
662         isl_basic_set_free(bset);
663         return NULL;
664 }
665
666 /* Intersect the set "set" with the affine space specified by the
667  * equalities in "eq".
668  */
669 static struct isl_set *set_append_equalities(struct isl_set *set,
670         struct isl_mat *eq)
671 {
672         int i;
673
674         if (!set || !eq)
675                 goto error;
676
677         for (i = 0; i < set->n; ++i) {
678                 set->p[i] = basic_set_append_equalities(set->p[i],
679                                         isl_mat_copy(eq));
680                 if (!set->p[i])
681                         goto error;
682         }
683         isl_mat_free(eq);
684         return set;
685 error:
686         isl_mat_free(eq);
687         isl_set_free(set);
688         return NULL;
689 }
690
691 /* Project the given basic set onto its parameter domain, possibly introducing
692  * new, explicit, existential variables in the constraints.
693  * The input has parameters and (possibly implicit) existential variables.
694  * The output has the same parameters, but only
695  * explicit existentially quantified variables.
696  *
697  * The actual projection is performed by pip, but pip doesn't seem
698  * to like equalities very much, so we first remove the equalities
699  * among the parameters by performing a variable compression on
700  * the parameters.  Afterward, an inverse transformation is performed
701  * and the equalities among the parameters are inserted back in.
702  */
703 static struct isl_set *compute_divs(struct isl_basic_set *bset)
704 {
705         int i, j;
706         struct isl_mat *eq;
707         struct isl_mat *T, *T2;
708         struct isl_set *set;
709         unsigned nparam, n_div;
710
711         bset = isl_basic_set_cow(bset);
712         if (!bset)
713                 return NULL;
714
715         if (bset->n_eq == 0)
716                 return compute_divs_no_eq(bset);
717
718         isl_basic_set_gauss(bset, NULL);
719
720         nparam = isl_basic_set_dim(bset, isl_dim_param);
721         n_div = isl_basic_set_dim(bset, isl_dim_div);
722
723         for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
724                 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
725                         ++i;
726         }
727         if (i == bset->n_eq)
728                 return compute_divs_no_eq(bset);
729
730         eq = isl_mat_sub_alloc(bset->ctx, bset->eq, i, bset->n_eq - i,
731                 0, 1 + nparam);
732         eq = isl_mat_cow(eq);
733         T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
734         bset = basic_set_parameter_preimage(bset, T);
735
736         set = compute_divs_no_eq(bset);
737         set = set_parameter_preimage(set, T2);
738         set = set_append_equalities(set, eq);
739         return set;
740 }
741
742 /* Compute an explicit representation for all the existentially
743  * quantified variables.
744  * The input and output dimensions are first turned into parameters.
745  * compute_divs then returns a map with the same parameters and
746  * no input or output dimensions and the dimension specification
747  * is reset to that of the input.
748  */
749 struct isl_map *isl_pip_basic_map_compute_divs(struct isl_basic_map *bmap)
750 {
751         struct isl_basic_set *bset;
752         struct isl_set *set;
753         struct isl_map *map;
754         struct isl_dim *dim, *orig_dim = NULL;
755         unsigned         nparam;
756         unsigned         n_in;
757         unsigned         n_out;
758
759         bmap = isl_basic_map_cow(bmap);
760         if (!bmap)
761                 return NULL;
762
763         nparam = isl_basic_map_dim(bmap, isl_dim_param);
764         n_in = isl_basic_map_dim(bmap, isl_dim_in);
765         n_out = isl_basic_map_dim(bmap, isl_dim_out);
766         dim = isl_dim_set_alloc(bmap->ctx, nparam + n_in + n_out, 0);
767         if (!dim)
768                 goto error;
769
770         orig_dim = bmap->dim;
771         bmap->dim = dim;
772         bset = (struct isl_basic_set *)bmap;
773
774         set = compute_divs(bset);
775         map = (struct isl_map *)set;
776         map = isl_map_reset_dim(map, orig_dim);
777
778         return map;
779 error:
780         isl_basic_map_free(bmap);
781         return NULL;
782 }