isl_pip_basic_map_compute_divs: remove some equalities first
[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 static struct isl_map *extremum_on(
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         if (!bmap || !dom)
411                 goto error;
412
413         ctx = bmap->ctx;
414         isl_assert(ctx, isl_basic_map_compatible_domain(bmap, dom), goto error);
415         nparam = isl_basic_map_n_param(bmap);
416         n_in = isl_basic_map_n_in(bmap);
417         n_out = isl_basic_map_n_out(bmap);
418
419         domain = isl_basic_map_to_pip(bmap, nparam + n_in, 0, dom->n_div);
420         if (!domain)
421                 goto error;
422         context = isl_basic_map_to_pip((struct isl_basic_map *)dom, 0, 0, 0);
423         if (!context)
424                 goto error;
425
426         options = pip_options_init();
427         options->Simplify = 1;
428         options->Maximize = max;
429         options->Urs_unknowns = -1;
430         options->Urs_parms = -1;
431         sol = pip_solve(domain, context, -1, options);
432
433         if (sol) {
434                 struct isl_basic_set *copy;
435                 copy = isl_basic_set_copy(dom);
436                 map = isl_map_from_quast(ctx, sol,
437                                 isl_dim_copy(bmap->dim), copy, empty);
438         } else {
439                 map = isl_map_empty_like_basic_map(bmap);
440                 if (empty)
441                         *empty = NULL;
442         }
443         if (!map)
444                 goto error;
445         if (map->n == 0 && empty) {
446                 isl_set_free(*empty);
447                 *empty = isl_set_from_basic_set(dom);
448         } else
449                 isl_basic_set_free(dom);
450         isl_basic_map_free(bmap);
451
452         pip_quast_free(sol);
453         pip_options_free(options);
454         pip_matrix_free(domain);
455         pip_matrix_free(context);
456
457         return map;
458 error:
459         if (domain)
460                 pip_matrix_free(domain);
461         if (context)
462                 pip_matrix_free(context);
463         isl_basic_map_free(bmap);
464         isl_basic_set_free(dom);
465         return NULL;
466 }
467
468 struct isl_map *isl_pip_basic_map_lexmax(
469                 struct isl_basic_map *bmap, struct isl_basic_set *dom,
470                 struct isl_set **empty)
471 {
472         return extremum_on(bmap, dom, empty, 1);
473 }
474
475 struct isl_map *isl_pip_basic_map_lexmin(
476                 struct isl_basic_map *bmap, struct isl_basic_set *dom,
477                 struct isl_set **empty)
478 {
479         return extremum_on(bmap, dom, empty, 0);
480 }
481
482 /* Project the given basic set onto its parameter domain, possibly introducing
483  * new, explicit, existential variables in the constraints.
484  * The input has parameters and output variables.
485  * The output has the same parameters, but no output variables, only
486  * explicit existentially quantified variables.
487  */
488 static struct isl_set *compute_divs_no_eq(struct isl_basic_set *bset)
489 {
490         PipMatrix *domain = NULL, *context = NULL;
491         PipOptions      *options;
492         PipQuast        *sol;
493         struct isl_ctx  *ctx;
494         struct isl_dim  *dim;
495         struct isl_map  *map;
496         struct isl_set  *set;
497         struct isl_basic_set    *dom;
498         unsigned         nparam;
499
500         if (!bset)
501                 goto error;
502
503         ctx = bset->ctx;
504         nparam = isl_basic_set_n_param(bset);
505
506         domain = isl_basic_set_to_pip(bset, nparam, 0, 0);
507         if (!domain)
508                 goto error;
509         context = pip_matrix_alloc(0, nparam + 2);
510         if (!context)
511                 goto error;
512
513         options = pip_options_init();
514         options->Simplify = 1;
515         options->Urs_unknowns = -1;
516         options->Urs_parms = -1;
517         sol = pip_solve(domain, context, -1, options);
518
519         dom = isl_basic_set_alloc(ctx, nparam, 0, 0, 0, 0);
520         map = isl_map_from_quast(ctx, sol, isl_dim_copy(dom->dim), dom, NULL);
521         set = (struct isl_set *)map;
522
523         pip_quast_free(sol);
524         pip_options_free(options);
525         pip_matrix_free(domain);
526         pip_matrix_free(context);
527
528         isl_basic_set_free(bset);
529
530         return set;
531 error:
532         if (domain)
533                 pip_matrix_free(domain);
534         if (context)
535                 pip_matrix_free(context);
536         isl_basic_set_free(dom);
537         isl_basic_set_free(bset);
538         return NULL;
539 }
540
541 static struct isl_map *isl_map_reset_dim(struct isl_map *map,
542         struct isl_dim *dim)
543 {
544         int i;
545
546         if (!map || !dim)
547                 goto error;
548
549         for (i = 0; i < map->n; ++i) {
550                 isl_dim_free(map->p[i]->dim);
551                 map->p[i]->dim = isl_dim_copy(dim);
552         }
553         isl_dim_free(map->dim);
554         map->dim = dim;
555
556         return map;
557 error:
558         isl_map_free(map);
559         isl_dim_free(dim);
560         return NULL;
561 }
562
563 static struct isl_set *isl_set_reset_dim(struct isl_set *set,
564         struct isl_dim *dim)
565 {
566         return (struct isl_set *) isl_map_reset_dim((struct isl_map *)set, dim);
567 }
568
569 /* Given a matrix M (mat) and a size n (size), replace mat
570  * by the matrix
571  *
572  *              [ M 0 ]
573  *              [ 0 I ]
574  *
575  * where I is an n x n identity matrix.
576  */
577 static struct isl_mat *append_identity(struct isl_ctx *ctx,
578         struct isl_mat *mat, unsigned size)
579 {
580         int i;
581         unsigned n_row, n_col;
582
583         n_row = mat->n_row;
584         n_col = mat->n_col;
585         mat = isl_mat_extend(ctx, mat, n_row + size, n_col + size);
586         if (!mat)
587                 return NULL;
588         for (i = 0; i < n_row; ++i)
589                 isl_seq_clr(mat->row[i] + n_col, size);
590         for (i = 0; i < size; ++i) {
591                 isl_seq_clr(mat->row[n_row + i], n_col + size);
592                 isl_int_set_si(mat->row[n_row + i][n_col + i], 1);
593         }
594         return mat;
595 }
596
597 /* Apply a preimage specified by "mat" on the parameters of "bset".
598  */
599 static struct isl_basic_set *basic_set_parameter_preimage(
600         struct isl_basic_set *bset, struct isl_mat *mat)
601 {
602         unsigned nparam, n_out;
603
604         if (!bset || !mat)
605                 goto error;
606
607         bset->dim = isl_dim_cow(bset->dim);
608         if (!bset->dim)
609                 goto error;
610
611         nparam = isl_basic_set_dim(bset, isl_dim_param);
612         n_out = isl_basic_set_dim(bset, isl_dim_set);
613
614         isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
615
616         mat = append_identity(bset->ctx, mat, n_out);
617         if (!mat)
618                 goto error;
619
620         bset->dim->nparam = 0;
621         bset->dim->n_out += nparam;
622         bset = isl_basic_set_preimage(bset, mat);
623         if (bset) {
624                 bset->dim->nparam = bset->dim->n_out - n_out;
625                 bset->dim->n_out = n_out;
626         }
627         return bset;
628 error:
629         isl_mat_free(bset ? bset->ctx : NULL, mat);
630         isl_basic_set_free(bset);
631         return NULL;
632 }
633
634 /* Apply a preimage specified by "mat" on the parameters of "set".
635  */
636 static struct isl_set *set_parameter_preimage(
637         struct isl_set *set, struct isl_mat *mat)
638 {
639         struct isl_dim *dim = NULL;
640         unsigned nparam, n_out;
641
642         if (!set || !mat)
643                 goto error;
644
645         dim = isl_dim_copy(set->dim);
646         dim = isl_dim_cow(dim);
647         if (!dim)
648                 goto error;
649
650         nparam = isl_set_dim(set, isl_dim_param);
651         n_out = isl_set_dim(set, isl_dim_set);
652
653         isl_assert(set->ctx, mat->n_row == 1 + nparam, goto error);
654
655         mat = append_identity(set->ctx, mat, n_out);
656         if (!mat)
657                 goto error;
658
659         dim->nparam = 0;
660         dim->n_out += nparam;
661         isl_set_reset_dim(set, dim);
662         set = isl_set_preimage(set, mat);
663         if (!set)
664                 goto error2;
665         dim = isl_dim_copy(set->dim);
666         dim = isl_dim_cow(dim);
667         if (!dim)
668                 goto error2;
669         dim->nparam = dim->n_out - n_out;
670         dim->n_out = n_out;
671         isl_set_reset_dim(set, dim);
672         return set;
673 error:
674         isl_dim_free(dim);
675         isl_mat_free(set ? set->ctx : NULL, mat);
676 error2:
677         isl_set_free(set);
678         return NULL;
679 }
680
681 /* Intersect the basic set "bset" with the affine space specified by the
682  * equalities in "eq".
683  */
684 static struct isl_basic_set *basic_set_append_equalities(
685         struct isl_basic_set *bset, struct isl_mat *eq)
686 {
687         int i, k;
688         unsigned len;
689
690         if (!bset || !eq)
691                 goto error;
692
693         bset = isl_basic_set_extend_dim(bset, isl_dim_copy(bset->dim), 0,
694                                         eq->n_row, 0);
695         if (!bset)
696                 goto error;
697
698         len = 1 + isl_dim_total(bset->dim) + bset->extra;
699         for (i = 0; i < eq->n_row; ++i) {
700                 k = isl_basic_set_alloc_equality(bset);
701                 if (k < 0)
702                         goto error;
703                 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
704                 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
705         }
706         isl_mat_free(bset->ctx, eq);
707
708         return bset;
709 error:
710         isl_mat_free(bset ? bset->ctx : NULL, eq);
711         isl_basic_set_free(bset);
712         return NULL;
713 }
714
715 /* Intersect the set "set" with the affine space specified by the
716  * equalities in "eq".
717  */
718 static struct isl_set *set_append_equalities(struct isl_set *set,
719         struct isl_mat *eq)
720 {
721         int i;
722
723         if (!set || !eq)
724                 goto error;
725
726         for (i = 0; i < set->n; ++i) {
727                 set->p[i] = basic_set_append_equalities(set->p[i],
728                                         isl_mat_copy(set->ctx, eq));
729                 if (!set->p[i])
730                         goto error;
731         }
732         isl_mat_free(set->ctx, eq);
733         return set;
734 error:
735         isl_mat_free(set ? set->ctx : NULL, eq);
736         isl_set_free(set);
737         return NULL;
738 }
739
740 /* Project the given basic set onto its parameter domain, possibly introducing
741  * new, explicit, existential variables in the constraints.
742  * The input has parameters and output variables.
743  * The output has the same parameters, but no output variables, only
744  * explicit existentially quantified variables.
745  *
746  * The actual projection is performed by pip, but pip doesn't seem
747  * to like equalities very much, so we first remove the equalities
748  * among the parameters by performing a variable compression on
749  * the parameters.  Afterward, an inverse transformation is performed
750  * and the equalities among the parameters are inserted back in.
751  */
752 static struct isl_set *compute_divs(struct isl_basic_set *bset)
753 {
754         int i, j;
755         struct isl_mat *eq;
756         struct isl_mat *T, *T2;
757         struct isl_set *set;
758         unsigned nparam, n_out;
759
760         bset = isl_basic_set_cow(bset);
761         if (!bset)
762                 return NULL;
763
764         if (bset->n_eq == 0)
765                 return compute_divs_no_eq(bset);
766
767         isl_basic_set_gauss(bset, NULL);
768
769         nparam = isl_basic_set_dim(bset, isl_dim_param);
770         n_out = isl_basic_set_dim(bset, isl_dim_out);
771
772         for (i = 0, j = n_out - 1; i < bset->n_eq && j >= 0; --j) {
773                 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
774                         ++i;
775         }
776         if (i == bset->n_eq)
777                 return compute_divs_no_eq(bset);
778
779         eq = isl_mat_sub_alloc(bset->ctx, bset->eq, i, bset->n_eq - i,
780                 0, 1 + nparam);
781         eq = isl_mat_cow(bset->ctx, eq);
782         T = isl_mat_variable_compression(bset->ctx,
783                 isl_mat_copy(bset->ctx, eq), &T2);
784         bset = basic_set_parameter_preimage(bset, T);
785
786         set = compute_divs_no_eq(bset);
787         set = set_parameter_preimage(set, T2);
788         set = set_append_equalities(set, eq);
789         return set;
790 }
791
792 /* Compute an explicit representation for all the existentially
793  * quantified variables.
794  * The input and output dimensions are first turned into parameters
795  * and the existential variables into output dimensions.
796  * compute_divs then returns a map with the same parameters and
797  * no input or output dimensions and the dimension specification
798  * is reset to that of the input.
799  */
800 struct isl_map *isl_pip_basic_map_compute_divs(struct isl_basic_map *bmap)
801 {
802         struct isl_basic_set *bset;
803         struct isl_set *set;
804         struct isl_map *map;
805         struct isl_dim *dim, *orig_dim = NULL;
806         unsigned         nparam;
807         unsigned         n_in;
808         unsigned         n_out;
809
810         bmap = isl_basic_map_cow(bmap);
811         if (!bmap)
812                 return NULL;
813
814         nparam = isl_basic_map_dim(bmap, isl_dim_param);
815         n_in = isl_basic_map_dim(bmap, isl_dim_in);
816         n_out = isl_basic_map_dim(bmap, isl_dim_out);
817         dim = isl_dim_set_alloc(bmap->ctx, nparam + n_in + n_out, bmap->n_div);
818         if (!dim)
819                 goto error;
820
821         orig_dim = bmap->dim;
822         bmap->dim = dim;
823         bmap->extra -= bmap->n_div;
824         bmap->n_div = 0;
825         bset = (struct isl_basic_set *)bmap;
826
827         set = compute_divs(bset);
828         map = (struct isl_map *)set;
829         map = isl_map_reset_dim(map, orig_dim);
830
831         return map;
832 error:
833         isl_basic_map_free(bmap);
834         return NULL;
835 }