isl_pip_basic_map_compute_divs: remove some equalities first
[isl.git] / isl_map_piplib.c
blob25d223b201f16e91979d05ac208348781525aeaf
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"
10 static void copy_values_from(isl_int *dst, Entier *src, unsigned n)
12 int i;
14 for (i = 0; i < n; ++i)
15 entier_assign(dst[i], src[i]);
18 static void add_value(isl_int *dst, Entier *src)
20 mpz_add(*dst, *dst, *src);
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)
27 int i;
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]);
40 static int add_inequality(struct isl_ctx *ctx,
41 struct isl_basic_map *bmap, int *pos, PipVector *vec)
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);
53 return i;
56 /* For a div d = floor(f/m), add the constraints
58 * f - m d >= 0
59 * -(f-(n-1)) + m d >= 0
61 * Note that the second constraint is the negation of
63 * f - m d >= n
65 static int add_div_constraints(struct isl_ctx *ctx,
66 struct isl_basic_map *bmap, int *pos, PipNewparm *p, unsigned div)
68 int i, j;
69 unsigned total = isl_basic_map_total_dim(bmap);
70 unsigned div_pos = 1 + total - bmap->n_div + div;
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]);
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;
87 static int add_equality(struct isl_ctx *ctx,
88 struct isl_basic_map *bmap, int *pos,
89 unsigned var, PipVector *vec)
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);
96 isl_assert(ctx, var < n_out, return -1);
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);
105 return i;
108 static int find_div(struct isl_ctx *ctx,
109 struct isl_basic_map *bmap, int *pos, PipNewparm *p)
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);
116 i = isl_basic_map_alloc_div(bmap);
117 if (i < 0)
118 return -1;
120 copy_constraint_from(bmap->div[i]+1, p->vector,
121 nparam, n_in, n_out, bmap->extra, pos);
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;
131 if (add_div_constraints(ctx, bmap, pos, p, i) < 0)
132 return -1;
134 return i;
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
143 static void quast_count(PipQuast *q, int *maxnew, int depth, int *maxdepth,
144 int *sol, int *nosol)
146 PipNewparm *p;
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);
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)
170 struct scan_data {
171 struct isl_ctx *ctx;
172 struct isl_basic_map *bmap;
173 struct isl_set **rest;
174 int *pos;
178 * New existentially quantified variables are places after the existing ones.
180 static struct isl_map *scan_quast_r(struct scan_data *data, PipQuast *q,
181 struct isl_map *map)
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);
190 if (!map)
191 goto error;
193 for (p = q->newparm; p; p = p->next) {
194 int pos;
195 unsigned pip_param = nparam + n_in;
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;
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);
210 if (isl_inequality_negate(bmap, pos))
211 goto error;
212 map = scan_quast_r(data, q->next_else, map);
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
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);
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;
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.
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)
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;
269 data.ctx = ctx;
270 data.rest = rest;
271 data.bmap = NULL;
272 data.pos = NULL;
274 if (!context || !keep_dim)
275 goto error;
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;
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;
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;
295 map = isl_map_alloc_dim(isl_dim_copy(keep_dim), n_sol,
296 ISL_MAP_DISJOINT);
297 if (!map)
298 goto error;
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;
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;
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;
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;
341 return NULL;
344 static void copy_values_to(Entier *dst, isl_int *src, unsigned n)
346 int i;
348 for (i = 0; i < n; ++i)
349 entier_assign(dst[i], src[i]);
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)
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);
361 PipMatrix *isl_basic_map_to_pip(struct isl_basic_map *bmap, unsigned pip_param,
362 unsigned extra_front, unsigned extra_back)
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;
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;
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);
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);
389 return M;
392 PipMatrix *isl_basic_set_to_pip(struct isl_basic_set *bset, unsigned pip_param,
393 unsigned extra_front, unsigned extra_back)
395 return isl_basic_map_to_pip((struct isl_basic_map *)bset,
396 pip_param, extra_front, extra_back);
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)
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;
410 if (!bmap || !dom)
411 goto error;
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);
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;
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);
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;
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);
452 pip_quast_free(sol);
453 pip_options_free(options);
454 pip_matrix_free(domain);
455 pip_matrix_free(context);
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;
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)
472 return extremum_on(bmap, dom, empty, 1);
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)
479 return extremum_on(bmap, dom, empty, 0);
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.
488 static struct isl_set *compute_divs_no_eq(struct isl_basic_set *bset)
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;
500 if (!bset)
501 goto error;
503 ctx = bset->ctx;
504 nparam = isl_basic_set_n_param(bset);
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;
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);
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;
523 pip_quast_free(sol);
524 pip_options_free(options);
525 pip_matrix_free(domain);
526 pip_matrix_free(context);
528 isl_basic_set_free(bset);
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;
541 static struct isl_map *isl_map_reset_dim(struct isl_map *map,
542 struct isl_dim *dim)
544 int i;
546 if (!map || !dim)
547 goto error;
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);
553 isl_dim_free(map->dim);
554 map->dim = dim;
556 return map;
557 error:
558 isl_map_free(map);
559 isl_dim_free(dim);
560 return NULL;
563 static struct isl_set *isl_set_reset_dim(struct isl_set *set,
564 struct isl_dim *dim)
566 return (struct isl_set *) isl_map_reset_dim((struct isl_map *)set, dim);
569 /* Given a matrix M (mat) and a size n (size), replace mat
570 * by the matrix
572 * [ M 0 ]
573 * [ 0 I ]
575 * where I is an n x n identity matrix.
577 static struct isl_mat *append_identity(struct isl_ctx *ctx,
578 struct isl_mat *mat, unsigned size)
580 int i;
581 unsigned n_row, n_col;
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);
594 return mat;
597 /* Apply a preimage specified by "mat" on the parameters of "bset".
599 static struct isl_basic_set *basic_set_parameter_preimage(
600 struct isl_basic_set *bset, struct isl_mat *mat)
602 unsigned nparam, n_out;
604 if (!bset || !mat)
605 goto error;
607 bset->dim = isl_dim_cow(bset->dim);
608 if (!bset->dim)
609 goto error;
611 nparam = isl_basic_set_dim(bset, isl_dim_param);
612 n_out = isl_basic_set_dim(bset, isl_dim_set);
614 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
616 mat = append_identity(bset->ctx, mat, n_out);
617 if (!mat)
618 goto error;
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;
627 return bset;
628 error:
629 isl_mat_free(bset ? bset->ctx : NULL, mat);
630 isl_basic_set_free(bset);
631 return NULL;
634 /* Apply a preimage specified by "mat" on the parameters of "set".
636 static struct isl_set *set_parameter_preimage(
637 struct isl_set *set, struct isl_mat *mat)
639 struct isl_dim *dim = NULL;
640 unsigned nparam, n_out;
642 if (!set || !mat)
643 goto error;
645 dim = isl_dim_copy(set->dim);
646 dim = isl_dim_cow(dim);
647 if (!dim)
648 goto error;
650 nparam = isl_set_dim(set, isl_dim_param);
651 n_out = isl_set_dim(set, isl_dim_set);
653 isl_assert(set->ctx, mat->n_row == 1 + nparam, goto error);
655 mat = append_identity(set->ctx, mat, n_out);
656 if (!mat)
657 goto error;
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;
681 /* Intersect the basic set "bset" with the affine space specified by the
682 * equalities in "eq".
684 static struct isl_basic_set *basic_set_append_equalities(
685 struct isl_basic_set *bset, struct isl_mat *eq)
687 int i, k;
688 unsigned len;
690 if (!bset || !eq)
691 goto error;
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;
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);
706 isl_mat_free(bset->ctx, eq);
708 return bset;
709 error:
710 isl_mat_free(bset ? bset->ctx : NULL, eq);
711 isl_basic_set_free(bset);
712 return NULL;
715 /* Intersect the set "set" with the affine space specified by the
716 * equalities in "eq".
718 static struct isl_set *set_append_equalities(struct isl_set *set,
719 struct isl_mat *eq)
721 int i;
723 if (!set || !eq)
724 goto error;
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;
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;
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.
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.
752 static struct isl_set *compute_divs(struct isl_basic_set *bset)
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;
760 bset = isl_basic_set_cow(bset);
761 if (!bset)
762 return NULL;
764 if (bset->n_eq == 0)
765 return compute_divs_no_eq(bset);
767 isl_basic_set_gauss(bset, NULL);
769 nparam = isl_basic_set_dim(bset, isl_dim_param);
770 n_out = isl_basic_set_dim(bset, isl_dim_out);
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;
776 if (i == bset->n_eq)
777 return compute_divs_no_eq(bset);
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);
786 set = compute_divs_no_eq(bset);
787 set = set_parameter_preimage(set, T2);
788 set = set_append_equalities(set, eq);
789 return set;
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.
800 struct isl_map *isl_pip_basic_map_compute_divs(struct isl_basic_map *bmap)
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;
810 bmap = isl_basic_map_cow(bmap);
811 if (!bmap)
812 return NULL;
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;
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;
827 set = compute_divs(bset);
828 map = (struct isl_map *)set;
829 map = isl_map_reset_dim(map, orig_dim);
831 return map;
832 error:
833 isl_basic_map_free(bmap);
834 return NULL;