5 #include "isl_piplib.h"
6 #include "isl_map_piplib.h"
7 #include "isl_map_private.h"
9 static void copy_values_from(isl_int *dst, Entier *src, unsigned n)
13 for (i = 0; i < n; ++i)
14 entier_assign(dst[i], src[i]);
17 static void add_value(isl_int *dst, Entier *src)
19 mpz_add(*dst, *dst, *src);
22 static void copy_constraint_from(isl_int *dst, PipVector *src,
23 unsigned nparam, unsigned n_in, unsigned n_out,
24 unsigned extra, int *pos)
28 copy_values_from(dst, src->the_vector+src->nb_elements-1, 1);
29 copy_values_from(dst+1, src->the_vector, nparam+n_in);
30 isl_seq_clr(dst+1+nparam+n_in, n_out);
31 isl_seq_clr(dst+1+nparam+n_in+n_out, extra);
32 for (i = 0; i + n_in + nparam < src->nb_elements-1; ++i) {
34 add_value(&dst[1+nparam+n_in+n_out+p],
35 &src->the_vector[n_in+nparam+i]);
39 static int add_inequality(struct isl_ctx *ctx,
40 struct isl_basic_map *bmap, int *pos, PipVector *vec)
42 unsigned nparam = isl_basic_map_n_param(bmap);
43 unsigned n_in = isl_basic_map_n_in(bmap);
44 unsigned n_out = isl_basic_map_n_out(bmap);
45 unsigned n_div = isl_basic_map_n_div(bmap);
46 int i = isl_basic_map_alloc_inequality(bmap);
49 copy_constraint_from(bmap->ineq[i], vec,
50 nparam, n_in, n_out, n_div, pos);
55 /* For a div d = floor(f/m), add the constraints
58 * -(f-(n-1)) + m d >= 0
60 * Note that the second constraint is the negation of
64 static int add_div_constraints(struct isl_ctx *ctx,
65 struct isl_basic_map *bmap, int *pos, PipNewparm *p, unsigned div)
68 unsigned total = isl_basic_map_total_dim(bmap);
69 unsigned div_pos = 1 + total - bmap->n_div + div;
71 i = add_inequality(ctx, bmap, pos, p->vector);
74 copy_values_from(&bmap->ineq[i][div_pos], &p->deno, 1);
75 isl_int_neg(bmap->ineq[i][div_pos], bmap->ineq[i][div_pos]);
77 j = isl_basic_map_alloc_inequality(bmap);
80 isl_seq_neg(bmap->ineq[j], bmap->ineq[i], 1 + total);
81 isl_int_add(bmap->ineq[j][0], bmap->ineq[j][0], bmap->ineq[j][div_pos]);
82 isl_int_sub_ui(bmap->ineq[j][0], bmap->ineq[j][0], 1);
86 static int add_equality(struct isl_ctx *ctx,
87 struct isl_basic_map *bmap, int *pos,
88 unsigned var, PipVector *vec)
91 unsigned nparam = isl_basic_map_n_param(bmap);
92 unsigned n_in = isl_basic_map_n_in(bmap);
93 unsigned n_out = isl_basic_map_n_out(bmap);
95 isl_assert(ctx, var < n_out, return -1);
97 i = isl_basic_map_alloc_equality(bmap);
100 copy_constraint_from(bmap->eq[i], vec,
101 nparam, n_in, n_out, bmap->extra, pos);
102 isl_int_set_si(bmap->eq[i][1+nparam+n_in+var], -1);
107 static int find_div(struct isl_ctx *ctx,
108 struct isl_basic_map *bmap, int *pos, PipNewparm *p)
111 unsigned nparam = isl_basic_map_n_param(bmap);
112 unsigned n_in = isl_basic_map_n_in(bmap);
113 unsigned n_out = isl_basic_map_n_out(bmap);
115 i = isl_basic_map_alloc_div(bmap);
119 copy_constraint_from(bmap->div[i]+1, p->vector,
120 nparam, n_in, n_out, bmap->extra, pos);
122 copy_values_from(bmap->div[i], &p->deno, 1);
123 for (j = 0; j < i; ++j)
124 if (isl_seq_eq(bmap->div[i], bmap->div[j],
125 1+1+isl_basic_map_total_dim(bmap)+j)) {
126 isl_basic_map_free_div(bmap, 1);
130 if (add_div_constraints(ctx, bmap, pos, p, i) < 0)
136 /* Count some properties of a quast
137 * - maximal number of new parameters
139 * - total number of solutions
140 * - total number of empty branches
142 static void quast_count(PipQuast *q, int *maxnew, int depth, int *maxdepth,
143 int *sol, int *nosol)
147 for (p = q->newparm; p; p = p->next)
148 if (p->rank > *maxnew)
151 if (++depth > *maxdepth)
153 quast_count(q->next_else, maxnew, depth, maxdepth, sol, nosol);
154 quast_count(q->next_then, maxnew, depth, maxdepth, sol, nosol);
164 * pos: array of length bmap->set.extra, mapping each of the existential
165 * variables PIP proposes to an existential variable in bmap
166 * bmap: collects the currently active constraints
167 * rest: collects the empty leaves of the quast (if not NULL)
171 struct isl_basic_map *bmap;
172 struct isl_set **rest;
177 * New existentially quantified variables are places after the existing ones.
179 static struct isl_map *scan_quast_r(struct scan_data *data, PipQuast *q,
183 struct isl_basic_map *bmap = data->bmap;
184 unsigned old_n_div = bmap->n_div;
185 unsigned nparam = isl_basic_map_n_param(bmap);
186 unsigned n_in = isl_basic_map_n_in(bmap);
187 unsigned n_out = isl_basic_map_n_out(bmap);
192 for (p = q->newparm; p; p = p->next) {
194 unsigned pip_param = nparam + n_in;
196 pos = find_div(data->ctx, bmap, data->pos, p);
199 data->pos[p->rank - pip_param] = pos;
203 int pos = add_inequality(data->ctx, bmap, data->pos,
207 map = scan_quast_r(data, q->next_then, map);
209 if (isl_inequality_negate(bmap, pos))
211 map = scan_quast_r(data, q->next_else, map);
213 if (isl_basic_map_free_inequality(bmap, 1))
215 } else if (q->list) {
218 /* if bmap->n_out is zero, we are only interested in the domains
219 * where a solution exists and not in the actual solution
221 for (j = 0, l = q->list; j < n_out && l; ++j, l = l->next)
222 if (add_equality(data->ctx, bmap, data->pos, j,
225 map = isl_map_add(map, isl_basic_map_copy(bmap));
226 if (isl_basic_map_free_equality(bmap, n_out))
228 } else if (data->rest) {
229 struct isl_basic_set *bset;
230 bset = isl_basic_set_from_basic_map(isl_basic_map_copy(bmap));
231 bset = isl_basic_set_drop_dims(bset, n_in, n_out);
234 *data->rest = isl_set_add(*data->rest, bset);
237 if (isl_basic_map_free_inequality(bmap, 2*(bmap->n_div - old_n_div)))
239 if (isl_basic_map_free_div(bmap, bmap->n_div - old_n_div))
248 * Returns a map of dimension "keep_dim" with "context" as domain and
249 * as range the first "isl_dim_size(keep_dim, isl_dim_out)" variables
250 * in the quast lists.
252 static struct isl_map *isl_map_from_quast(struct isl_ctx *ctx, PipQuast *q,
253 struct isl_dim *keep_dim,
254 struct isl_basic_set *context,
255 struct isl_set **rest)
261 struct scan_data data;
262 struct isl_map *map = NULL;
263 struct isl_dim *dims;
273 if (!context || !keep_dim)
276 dim = isl_basic_set_n_dim(context);
277 nparam = isl_basic_set_n_param(context);
278 keep = isl_dim_size(keep_dim, isl_dim_out);
279 pip_param = nparam + dim;
284 nexist = pip_param-1;
285 quast_count(q, &nexist, 0, &max_depth, &n_sol, &n_nosol);
286 nexist -= pip_param-1;
289 *rest = isl_set_alloc_dim(isl_dim_copy(context->dim), n_nosol,
294 map = isl_map_alloc_dim(isl_dim_copy(keep_dim), n_sol,
299 dims = isl_dim_reverse(isl_dim_copy(context->dim));
300 data.bmap = isl_basic_map_from_basic_set(context, dims);
301 data.bmap = isl_basic_map_extend_dim(data.bmap,
302 keep_dim, nexist, keep, max_depth+2*nexist);
306 if (data.bmap->extra) {
308 data.pos = isl_alloc_array(ctx, int, data.bmap->extra);
311 for (i = 0; i < data.bmap->n_div; ++i)
315 map = scan_quast_r(&data, q, map);
316 map = isl_map_finalize(map);
320 *rest = isl_set_finalize(*rest);
324 isl_basic_map_free(data.bmap);
329 isl_basic_set_free(context);
330 isl_dim_free(keep_dim);
334 isl_basic_map_free(data.bmap);
343 static void copy_values_to(Entier *dst, isl_int *src, unsigned n)
347 for (i = 0; i < n; ++i)
348 entier_assign(dst[i], src[i]);
351 static void copy_constraint_to(Entier *dst, isl_int *src,
352 unsigned pip_param, unsigned pip_var,
353 unsigned extra_front, unsigned extra_back)
355 copy_values_to(dst+1+extra_front+pip_var+pip_param+extra_back, src, 1);
356 copy_values_to(dst+1+extra_front+pip_var, src+1, pip_param);
357 copy_values_to(dst+1+extra_front, src+1+pip_param, pip_var);
360 PipMatrix *isl_basic_map_to_pip(struct isl_basic_map *bmap, unsigned pip_param,
361 unsigned extra_front, unsigned extra_back)
368 unsigned pip_var = isl_basic_map_total_dim(bmap) - pip_param;
370 nrow = extra_front + bmap->n_eq + bmap->n_ineq;
371 ncol = 1 + extra_front + pip_var + pip_param + extra_back + 1;
372 M = pip_matrix_alloc(nrow, ncol);
377 for (i = 0; i < bmap->n_eq; ++i) {
378 entier_set_si(M->p[off+i][0], 0);
379 copy_constraint_to(M->p[off+i], bmap->eq[i],
380 pip_param, pip_var, extra_front, extra_back);
383 for (i = 0; i < bmap->n_ineq; ++i) {
384 entier_set_si(M->p[off+i][0], 1);
385 copy_constraint_to(M->p[off+i], bmap->ineq[i],
386 pip_param, pip_var, extra_front, extra_back);
391 PipMatrix *isl_basic_set_to_pip(struct isl_basic_set *bset, unsigned pip_param,
392 unsigned extra_front, unsigned extra_back)
394 return isl_basic_map_to_pip((struct isl_basic_map *)bset,
395 pip_param, extra_front, extra_back);
398 struct isl_map *isl_pip_basic_map_lexopt(
399 struct isl_basic_map *bmap, struct isl_basic_set *dom,
400 struct isl_set **empty, int max)
406 PipMatrix *domain = NULL, *context = NULL;
407 unsigned nparam, n_in, n_out;
409 bmap = isl_basic_map_detect_equalities(bmap);
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);
422 context = isl_basic_map_to_pip((struct isl_basic_map *)dom, 0, 0, 0);
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);
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);
439 map = isl_map_empty_like_basic_map(bmap);
445 if (map->n == 0 && empty) {
446 isl_set_free(*empty);
447 *empty = isl_set_from_basic_set(dom);
449 isl_basic_set_free(dom);
450 isl_basic_map_free(bmap);
453 pip_options_free(options);
454 pip_matrix_free(domain);
455 pip_matrix_free(context);
460 pip_matrix_free(domain);
462 pip_matrix_free(context);
463 isl_basic_map_free(bmap);
464 isl_basic_set_free(dom);