4 #include "isl_piplib.h"
5 #include "isl_map_piplib.h"
6 #include "isl_map_private.h"
8 static void copy_values_from(isl_int *dst, Entier *src, unsigned n)
12 for (i = 0; i < n; ++i)
13 entier_assign(dst[i], src[i]);
16 static void add_value(isl_int *dst, Entier *src)
18 mpz_add(*dst, *dst, *src);
21 static void copy_constraint_from(isl_int *dst, PipVector *src,
22 unsigned nparam, unsigned n_in, unsigned n_out,
23 unsigned extra, int *pos)
27 copy_values_from(dst, src->the_vector+src->nb_elements-1, 1);
28 copy_values_from(dst+1, src->the_vector, nparam+n_in);
29 isl_seq_clr(dst+1+nparam+n_in, n_out);
30 isl_seq_clr(dst+1+nparam+n_in+n_out, extra);
31 for (i = 0; i + n_in + nparam < src->nb_elements-1; ++i) {
33 add_value(&dst[1+nparam+n_in+n_out+p],
34 &src->the_vector[n_in+nparam+i]);
38 static int add_inequality(struct isl_ctx *ctx,
39 struct isl_basic_map *bmap, int *pos, PipVector *vec)
41 int i = isl_basic_map_alloc_inequality(bmap);
44 copy_constraint_from(bmap->ineq[i], vec,
45 bmap->nparam, bmap->n_in, bmap->n_out, bmap->n_div, pos);
50 /* For a div d = floor(f/m), add the constraints
53 * -(f-(n-1)) + m d >= 0
55 * Note that the second constraint is the negation of
59 static int add_div_constraints(struct isl_ctx *ctx,
60 struct isl_basic_map *bmap, int *pos, PipNewparm *p, unsigned div)
63 unsigned div_pos = 1 + bmap->nparam + bmap->n_in + bmap->n_out + div;
64 unsigned total = bmap->nparam + bmap->n_in + bmap->n_out + bmap->extra;
66 i = add_inequality(ctx, bmap, pos, p->vector);
69 copy_values_from(&bmap->ineq[i][div_pos], &p->deno, 1);
70 isl_int_neg(bmap->ineq[i][div_pos], bmap->ineq[i][div_pos]);
72 j = isl_basic_map_alloc_inequality(bmap);
75 isl_seq_neg(bmap->ineq[j], bmap->ineq[i], 1 + total);
76 isl_int_add(bmap->ineq[j][0], bmap->ineq[j][0], bmap->ineq[j][div_pos]);
77 isl_int_sub_ui(bmap->ineq[j][0], bmap->ineq[j][0], 1);
81 static int add_equality(struct isl_ctx *ctx,
82 struct isl_basic_map *bmap, int *pos,
83 unsigned var, PipVector *vec)
87 isl_assert(ctx, var < bmap->n_out, return -1);
89 i = isl_basic_map_alloc_equality(bmap);
92 copy_constraint_from(bmap->eq[i], vec,
93 bmap->nparam, bmap->n_in, bmap->n_out, bmap->extra, pos);
94 isl_int_set_si(bmap->eq[i][1+bmap->nparam+bmap->n_in+var], -1);
99 static int find_div(struct isl_ctx *ctx,
100 struct isl_basic_map *bmap, int *pos, PipNewparm *p)
104 i = isl_basic_map_alloc_div(bmap);
108 copy_constraint_from(bmap->div[i]+1, p->vector,
109 bmap->nparam, bmap->n_in, bmap->n_out, bmap->extra, pos);
111 copy_values_from(bmap->div[i], &p->deno, 1);
112 for (j = 0; j < i; ++j)
113 if (isl_seq_eq(bmap->div[i], bmap->div[j],
114 1+1+bmap->nparam+bmap->n_in+bmap->n_out+j)) {
115 isl_basic_map_free_div(bmap, 1);
119 if (add_div_constraints(ctx, bmap, pos, p, i) < 0)
125 /* Count some properties of a quast
126 * - maximal number of new parameters
128 * - total number of solutions
129 * - total number of empty branches
131 static void quast_count(PipQuast *q, int *maxnew, int depth, int *maxdepth,
132 int *sol, int *nosol)
136 for (p = q->newparm; p; p = p->next)
137 if (p->rank > *maxnew)
140 if (++depth > *maxdepth)
142 quast_count(q->next_else, maxnew, depth, maxdepth, sol, nosol);
143 quast_count(q->next_then, maxnew, depth, maxdepth, sol, nosol);
153 * pos: array of length bmap->set.extra, mapping each of the existential
154 * variables PIP proposes to an existential variable in bmap
155 * bmap: collects the currently active constraints
156 * rest: collects the empty leaves of the quast (if not NULL)
160 struct isl_basic_map *bmap;
161 struct isl_set **rest;
166 * New existentially quantified variables are places after the existing ones.
168 static struct isl_map *scan_quast_r(struct scan_data *data, PipQuast *q,
172 struct isl_basic_map *bmap = data->bmap;
173 unsigned old_n_div = bmap->n_div;
178 for (p = q->newparm; p; p = p->next) {
180 unsigned pip_param = bmap->nparam + bmap->n_in;
182 pos = find_div(data->ctx, bmap, data->pos, p);
185 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(bmap, pos))
197 map = scan_quast_r(data, q->next_else, map);
199 if (isl_basic_map_free_inequality(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(map, isl_basic_map_copy(bmap));
212 if (isl_basic_map_free_equality(bmap, bmap->n_out))
214 } else if (map->n && data->rest) {
215 /* not interested in rest if no sol */
216 struct isl_basic_set *bset;
217 bset = isl_basic_set_from_basic_map(isl_basic_map_copy(bmap));
218 bset = isl_basic_set_drop_dims(bset, bmap->n_in, bmap->n_out);
221 *data->rest = isl_set_add(*data->rest, bset);
224 if (isl_basic_map_free_inequality(bmap, 2*(bmap->n_div - old_n_div)))
226 if (isl_basic_map_free_div(bmap, bmap->n_div - old_n_div))
235 * Returns a map with "context" as domain and as range the first
236 * "keep" variables in the quast lists.
238 static struct isl_map *isl_map_from_quast(struct isl_ctx *ctx, PipQuast *q,
240 struct isl_basic_set *context,
241 struct isl_set **rest)
247 struct scan_data data;
259 nparam = context->nparam;
260 pip_param = nparam + context->dim;
265 nexist = pip_param-1;
266 quast_count(q, &nexist, 0, &max_depth, &n_sol, &n_nosol);
267 nexist -= pip_param-1;
270 *rest = isl_set_alloc(data.ctx, nparam, context->dim, n_nosol,
275 map = isl_map_alloc(data.ctx, nparam, context->dim, keep, n_sol,
280 data.bmap = isl_basic_map_from_basic_set(context,
282 data.bmap = isl_basic_map_extend(data.bmap,
283 nparam, data.bmap->n_in, keep, nexist, keep, max_depth+2*nexist);
287 if (data.bmap->extra) {
289 data.pos = isl_alloc_array(ctx, int, data.bmap->extra);
292 for (i = 0; i < data.bmap->n_div; ++i)
296 map = scan_quast_r(&data, q, map);
297 map = isl_map_finalize(map);
301 *rest = isl_set_finalize(*rest);
305 isl_basic_map_free(data.bmap);
312 isl_basic_map_free(data.bmap);
321 static void copy_values_to(Entier *dst, isl_int *src, unsigned n)
325 for (i = 0; i < n; ++i)
326 entier_assign(dst[i], src[i]);
329 static void copy_constraint_to(Entier *dst, isl_int *src,
330 unsigned pip_param, unsigned pip_var,
331 unsigned extra_front, unsigned extra_back)
333 copy_values_to(dst+1+extra_front+pip_var+pip_param+extra_back, src, 1);
334 copy_values_to(dst+1+extra_front+pip_var, src+1, pip_param);
335 copy_values_to(dst+1+extra_front, src+1+pip_param, pip_var);
338 PipMatrix *isl_basic_map_to_pip(struct isl_basic_map *bmap, unsigned pip_param,
339 unsigned extra_front, unsigned extra_back)
346 unsigned pip_var = bmap->nparam + bmap->n_in + bmap->n_out
347 + bmap->n_div - pip_param;
349 nrow = extra_front + bmap->n_eq + bmap->n_ineq;
350 ncol = 1 + extra_front + pip_var + pip_param + extra_back + 1;
351 M = pip_matrix_alloc(nrow, ncol);
356 for (i = 0; i < bmap->n_eq; ++i) {
357 entier_set_si(M->p[off+i][0], 0);
358 copy_constraint_to(M->p[off+i], bmap->eq[i],
359 pip_param, pip_var, extra_front, extra_back);
362 for (i = 0; i < bmap->n_ineq; ++i) {
363 entier_set_si(M->p[off+i][0], 1);
364 copy_constraint_to(M->p[off+i], bmap->ineq[i],
365 pip_param, pip_var, extra_front, extra_back);
370 static struct isl_map *extremum_on(
371 struct isl_basic_map *bmap, struct isl_basic_set *dom,
372 struct isl_set **empty, int max)
378 PipMatrix *domain = NULL, *context = NULL;
384 isl_assert(ctx, bmap->nparam == dom->nparam, goto error);
385 isl_assert(ctx, bmap->n_in == dom->dim, goto error);
387 domain = isl_basic_map_to_pip(bmap, bmap->nparam + bmap->n_in,
391 context = isl_basic_map_to_pip((struct isl_basic_map *)dom, 0, 0, 0);
395 options = pip_options_init();
396 options->Simplify = 1;
397 options->Maximize = max;
398 options->Urs_unknowns = -1;
399 options->Urs_parms = -1;
400 sol = pip_solve(domain, context, -1, options);
403 struct isl_basic_set *copy;
404 copy = isl_basic_set_copy(dom);
405 map = isl_map_from_quast(ctx, sol, bmap->n_out, copy, empty);
407 map = isl_map_empty(ctx, bmap->nparam, bmap->n_in, bmap->n_out);
413 if (map->n == 0 && empty) {
414 isl_set_free(*empty);
415 *empty = isl_set_from_basic_set(dom);
417 isl_basic_set_free(dom);
418 isl_basic_map_free(bmap);
421 pip_options_free(options);
422 pip_matrix_free(domain);
423 pip_matrix_free(context);
428 pip_matrix_free(domain);
430 pip_matrix_free(context);
431 isl_basic_map_free(bmap);
432 isl_basic_set_free(dom);
436 struct isl_map *isl_pip_basic_map_lexmax(
437 struct isl_basic_map *bmap, struct isl_basic_set *dom,
438 struct isl_set **empty)
440 return extremum_on(bmap, dom, empty, 1);
443 struct isl_map *isl_pip_basic_map_lexmin(
444 struct isl_basic_map *bmap, struct isl_basic_set *dom,
445 struct isl_set **empty)
447 return extremum_on(bmap, dom, empty, 0);
450 struct isl_map *isl_pip_basic_map_compute_divs(struct isl_basic_map *bmap)
452 PipMatrix *domain = NULL, *context = NULL;
458 struct isl_basic_set *dom;
469 domain = isl_basic_map_to_pip(bmap, bmap->nparam + n_in + n_out, 0, 0);
472 context = pip_matrix_alloc(0, bmap->nparam + n_in + n_out + 2);
476 options = pip_options_init();
477 options->Simplify = 1;
478 options->Urs_unknowns = -1;
479 options->Urs_parms = -1;
480 sol = pip_solve(domain, context, -1, options);
482 dom = isl_basic_set_alloc(ctx, bmap->nparam, n_in + n_out, 0, 0, 0);
483 map = isl_map_from_quast(ctx, sol, 0, dom, NULL);
486 pip_options_free(options);
487 pip_matrix_free(domain);
488 pip_matrix_free(context);
490 isl_basic_map_free(bmap);
492 set = isl_map_domain(map);
494 return isl_map_from_set(set, n_in, n_out);
497 pip_matrix_free(domain);
499 pip_matrix_free(context);
500 isl_basic_set_free(dom);
501 isl_basic_map_free(bmap);