3 #include "isl_piplib.h"
4 #include "isl_map_piplib.h"
6 static void copy_values_from(isl_int *dst, Entier *src, unsigned n)
10 for (i = 0; i < n; ++i)
11 entier_assign(dst[i], src[i]);
14 static void add_value(isl_int *dst, Entier *src)
16 mpz_add(*dst, *dst, *src);
19 static void copy_constraint_from(isl_int *dst, PipVector *src,
20 unsigned nparam, unsigned n_in, unsigned n_out,
21 unsigned extra, int *pos)
25 copy_values_from(dst, src->the_vector+src->nb_elements-1, 1);
26 copy_values_from(dst+1, src->the_vector, nparam+n_in);
27 isl_seq_clr(dst+1+nparam+n_in, n_out);
28 isl_seq_clr(dst+1+nparam+n_in+n_out, extra);
29 for (i = 0; i + n_in + nparam < src->nb_elements-1; ++i) {
31 add_value(&dst[1+nparam+n_in+n_out+p],
32 &src->the_vector[n_in+nparam+i]);
36 static int add_inequality(struct isl_ctx *ctx,
37 struct isl_basic_map *bmap, int *pos, PipVector *vec)
39 int i = isl_basic_map_alloc_inequality(ctx, bmap);
42 copy_constraint_from(bmap->ineq[i], vec,
43 bmap->nparam, bmap->n_in, bmap->n_out, bmap->extra, pos);
48 /* For a div d = floor(f/m), add the constraints
51 * -(f-(n-1)) + m d >= 0
53 * Note that the second constraint is the negation of
57 static int add_div_constraints(struct isl_ctx *ctx,
58 struct isl_basic_map *bmap, int *pos, PipNewparm *p, unsigned div)
61 unsigned div_pos = 1 + bmap->nparam + bmap->n_in + bmap->n_out + div;
62 unsigned total = bmap->nparam + bmap->n_in + bmap->n_out + bmap->extra;
64 i = add_inequality(ctx, bmap, pos, p->vector);
67 copy_values_from(&bmap->ineq[i][div_pos], &p->deno, 1);
68 isl_int_neg(bmap->ineq[i][div_pos], bmap->ineq[i][div_pos]);
70 j = isl_basic_map_alloc_inequality(ctx, bmap);
73 isl_seq_neg(bmap->ineq[j], bmap->ineq[i], 1 + total);
74 isl_int_add(bmap->ineq[j][0], bmap->ineq[j][0], bmap->ineq[j][div_pos]);
75 isl_int_sub_ui(bmap->ineq[j][0], bmap->ineq[j][0], 1);
79 static int add_equality(struct isl_ctx *ctx,
80 struct isl_basic_map *bmap, int *pos,
81 unsigned var, PipVector *vec)
85 isl_assert(ctx, var < bmap->n_out, return -1);
87 i = isl_basic_map_alloc_equality(ctx, bmap);
90 copy_constraint_from(bmap->eq[i], vec,
91 bmap->nparam, bmap->n_in, bmap->n_out, bmap->extra, pos);
92 isl_int_set_si(bmap->eq[i][1+bmap->nparam+bmap->n_in+var], -1);
97 static int find_div(struct isl_ctx *ctx,
98 struct isl_basic_map *bmap, int *pos, PipNewparm *p)
102 i = isl_basic_map_alloc_div(ctx, bmap);
106 copy_constraint_from(bmap->div[i]+1, p->vector,
107 bmap->nparam, bmap->n_in, bmap->n_out, bmap->extra, pos);
109 copy_values_from(bmap->div[i], &p->deno, 1);
110 for (j = 0; j < i; ++j)
111 if (isl_seq_eq(bmap->div[i], bmap->div[j],
112 1+1+bmap->nparam+bmap->n_in+bmap->n_out+j)) {
113 isl_basic_map_free_div(ctx, bmap, 1);
117 if (add_div_constraints(ctx, bmap, pos, p, i) < 0)
123 /* Count some properties of a quast
124 * - maximal number of new parameters
126 * - total number of solutions
127 * - total number of empty branches
129 static void quast_count(PipQuast *q, int *maxnew, int depth, int *maxdepth,
130 int *sol, int *nosol)
134 for (p = q->newparm; p; p = p->next)
135 if (p->rank > *maxnew)
138 if (++depth > *maxdepth)
140 quast_count(q->next_else, maxnew, depth, maxdepth, sol, nosol);
141 quast_count(q->next_then, maxnew, depth, maxdepth, sol, nosol);
151 * pos: array of length bmap->set.extra, mapping each of the existential
152 * variables PIP proposes to an existential variable in bmap
153 * bmap: collects the currently active constraints
154 * rest: collects the empty leaves of the quast (if not NULL)
158 struct isl_basic_map *bmap;
159 struct isl_set **rest;
164 * New existentially quantified variables are places after the existing ones.
166 static struct isl_map *scan_quast_r(struct scan_data *data, PipQuast *q,
171 struct isl_basic_map *bmap = data->bmap;
172 unsigned old_n_div = bmap->n_div;
177 for (p = q->newparm; p; p = p->next) {
179 unsigned pip_param = bmap->nparam + bmap->n_in;
181 pos = find_div(data->ctx, bmap, data->pos, p);
184 data->pos[p->rank - pip_param] = pos;
189 int pos = add_inequality(data->ctx, bmap, data->pos,
193 map = scan_quast_r(data, q->next_then, map);
195 if (isl_inequality_negate(data->ctx, bmap, pos))
197 map = scan_quast_r(data, q->next_else, map);
199 if (isl_basic_map_free_inequality(data->ctx, bmap, 1))
201 } else if (q->list) {
204 /* if bmap->n_out is zero, we are only interested in the domains
205 * where a solution exists and not in the actual solution
207 for (j = 0, l = q->list; j < bmap->n_out && l; ++j, l = l->next)
208 if (add_equality(data->ctx, bmap, data->pos, j,
211 map = isl_map_add(data->ctx, map,
212 isl_basic_map_copy(data->ctx, bmap));
213 if (isl_basic_map_free_equality(data->ctx, bmap,
216 } else if (map->n && data->rest) {
217 /* not interested in rest if no sol */
218 struct isl_basic_set *bset;
219 bset = isl_basic_set_from_basic_map(data->ctx,
220 isl_basic_map_copy(data->ctx, bmap));
221 bset = isl_basic_set_drop_vars(data->ctx, bset,
222 bmap->n_in, bmap->n_out);
225 *data->rest = isl_set_add(data->ctx, *data->rest, bset);
228 if (isl_basic_map_free_inequality(data->ctx, bmap,
229 2*(bmap->n_div - old_n_div)))
231 if (isl_basic_map_free_div(data->ctx, bmap,
232 bmap->n_div - old_n_div))
236 isl_map_free(data->ctx, map);
241 * Returns a map with "context" as domain and as range the first
242 * "keep" variables in the quast lists.
244 static struct isl_map *isl_map_from_quast(struct isl_ctx *ctx, PipQuast *q,
246 struct isl_basic_set *context,
247 struct isl_set **rest)
253 struct scan_data data;
265 nparam = context->nparam;
266 pip_param = nparam + context->dim;
271 nexist = pip_param-1;
272 quast_count(q, &nexist, 0, &max_depth, &n_sol, &n_nosol);
273 nexist -= pip_param-1;
276 *rest = isl_set_alloc(data.ctx, nparam, context->dim, n_nosol,
281 map = isl_map_alloc(data.ctx, nparam, context->dim, keep, n_sol,
286 data.bmap = isl_basic_map_from_basic_set(ctx, context,
288 data.bmap = isl_basic_map_extend(data.ctx, data.bmap,
289 nparam, data.bmap->n_in, keep, nexist, keep, max_depth+2*nexist);
293 if (data.bmap->extra) {
295 data.pos = isl_alloc_array(ctx, int, data.bmap->extra);
298 for (i = 0; i < data.bmap->n_div; ++i)
302 map = scan_quast_r(&data, q, map);
303 map = isl_map_finalize(ctx, map);
307 *rest = isl_set_finalize(ctx, *rest);
311 isl_basic_map_free(data.ctx, data.bmap);
318 isl_basic_map_free(data.ctx, data.bmap);
319 isl_map_free(ctx, map);
321 isl_set_free(ctx, *rest);
327 static void copy_values_to(Entier *dst, isl_int *src, unsigned n)
331 for (i = 0; i < n; ++i)
332 entier_assign(dst[i], src[i]);
335 static void copy_constraint_to(Entier *dst, isl_int *src,
336 unsigned pip_param, unsigned pip_var,
337 unsigned extra_front, unsigned extra_back)
339 copy_values_to(dst+1+extra_front+pip_var+pip_param+extra_back, src, 1);
340 copy_values_to(dst+1+extra_front+pip_var, src+1, pip_param);
341 copy_values_to(dst+1+extra_front, src+1+pip_param, pip_var);
344 PipMatrix *isl_basic_map_to_pip(struct isl_basic_map *bmap, unsigned pip_param,
345 unsigned extra_front, unsigned extra_back)
352 unsigned pip_var = bmap->nparam + bmap->n_in + bmap->n_out
353 + bmap->n_div - pip_param;
354 unsigned pip_dim = pip_var - bmap->n_div;
356 nrow = extra_front + bmap->n_eq + bmap->n_ineq;
357 ncol = 1 + extra_front + pip_var + pip_param + extra_back + 1;
358 M = pip_matrix_alloc(nrow, ncol);
363 for (i = 0; i < bmap->n_eq; ++i) {
364 entier_set_si(M->p[off+i][0], 0);
365 copy_constraint_to(M->p[off+i], bmap->eq[i],
366 pip_param, pip_var, extra_front, extra_back);
369 for (i = 0; i < bmap->n_ineq; ++i) {
370 entier_set_si(M->p[off+i][0], 1);
371 copy_constraint_to(M->p[off+i], bmap->ineq[i],
372 pip_param, pip_var, extra_front, extra_back);
377 static struct isl_map *extremum_on(struct isl_ctx *ctx,
378 struct isl_basic_map *bmap, struct isl_basic_set *dom,
379 struct isl_set **empty, int max)
384 PipMatrix *domain = NULL, *context = NULL;
389 isl_assert(ctx, bmap->nparam == dom->nparam, goto error);
390 isl_assert(ctx, bmap->n_in == dom->dim, goto error);
392 domain = isl_basic_map_to_pip(bmap, bmap->nparam + bmap->n_in,
396 context = isl_basic_map_to_pip((struct isl_basic_map *)dom, 0, 0, 0);
400 options = pip_options_init();
401 options->Simplify = 1;
402 options->Maximize = max;
403 options->Urs_unknowns = -1;
404 options->Urs_parms = -1;
405 sol = pip_solve(domain, context, -1, options);
408 struct isl_basic_set *copy;
409 copy = isl_basic_set_copy(ctx, dom);
410 map = isl_map_from_quast(ctx, sol, bmap->n_out, copy, empty);
412 map = isl_map_empty(ctx, bmap->nparam, bmap->n_in, bmap->n_out);
418 if (map->n == 0 && empty) {
419 isl_set_free(ctx, *empty);
420 *empty = isl_set_from_basic_set(ctx, dom);
422 isl_basic_set_free(ctx, dom);
423 isl_basic_map_free(ctx, bmap);
426 pip_options_free(options);
427 pip_matrix_free(domain);
428 pip_matrix_free(context);
433 pip_matrix_free(domain);
435 pip_matrix_free(context);
436 isl_basic_map_free(ctx, bmap);
437 isl_basic_set_free(ctx, dom);
441 struct isl_map *isl_pip_basic_map_lexmax(struct isl_ctx *ctx,
442 struct isl_basic_map *bmap, struct isl_basic_set *dom,
443 struct isl_set **empty)
445 return extremum_on(ctx, bmap, dom, empty, 1);
448 struct isl_map *isl_pip_basic_map_lexmin(struct isl_ctx *ctx,
449 struct isl_basic_map *bmap, struct isl_basic_set *dom,
450 struct isl_set **empty)
452 return extremum_on(ctx, bmap, dom, empty, 0);
455 struct isl_map *isl_pip_basic_map_compute_divs(struct isl_ctx *ctx,
456 struct isl_basic_map *bmap)
458 PipMatrix *domain = NULL, *context = NULL;
463 struct isl_basic_set *dom;
473 domain = isl_basic_map_to_pip(bmap, bmap->nparam + n_in + n_out, 0, 0);
476 context = pip_matrix_alloc(0, bmap->nparam + n_in + n_out + 2);
480 options = pip_options_init();
481 options->Simplify = 1;
482 options->Urs_unknowns = -1;
483 options->Urs_parms = -1;
484 sol = pip_solve(domain, context, -1, options);
486 dom = isl_basic_set_alloc(ctx, bmap->nparam, n_in + n_out, 0, 0, 0);
487 map = isl_map_from_quast(ctx, sol, 0, dom, NULL);
490 pip_options_free(options);
491 pip_matrix_free(domain);
492 pip_matrix_free(context);
494 isl_basic_map_free(ctx, bmap);
496 set = isl_map_domain(ctx, map);
498 return isl_map_from_set(ctx, set, n_in, n_out);
501 pip_matrix_free(domain);
503 pip_matrix_free(context);
504 isl_basic_set_free(ctx, dom);
505 isl_basic_map_free(ctx, bmap);