introduce isl_dim structure for representing shared dimension information
[platform/upstream/isl.git] / isl_map_piplib.c
1 #include "isl_set.h"
2 #include "isl_map.h"
3 #include "isl_seq.h"
4 #include "isl_piplib.h"
5 #include "isl_map_piplib.h"
6 #include "isl_map_private.h"
7
8 static void copy_values_from(isl_int *dst, Entier *src, unsigned n)
9 {
10         int i;
11
12         for (i = 0; i < n; ++i)
13                 entier_assign(dst[i], src[i]);
14 }
15
16 static void add_value(isl_int *dst, Entier *src)
17 {
18         mpz_add(*dst, *dst, *src);
19 }
20
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)
24 {
25         int i;
26
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) {
32                 int p = pos[i];
33                 add_value(&dst[1+nparam+n_in+n_out+p],
34                           &src->the_vector[n_in+nparam+i]);
35         }
36 }
37
38 static int add_inequality(struct isl_ctx *ctx,
39                    struct isl_basic_map *bmap, int *pos, PipVector *vec)
40 {
41         unsigned nparam = isl_basic_map_n_param(bmap);
42         unsigned n_in = isl_basic_map_n_in(bmap);
43         unsigned n_out = isl_basic_map_n_out(bmap);
44         unsigned n_div = isl_basic_map_n_div(bmap);
45         int i = isl_basic_map_alloc_inequality(bmap);
46         if (i < 0)
47                 return -1;
48         copy_constraint_from(bmap->ineq[i], vec,
49             nparam, n_in, n_out, n_div, pos);
50
51         return i;
52 }
53
54 /* For a div d = floor(f/m), add the constraints
55  *
56  *              f - m d >= 0
57  *              -(f-(n-1)) + m d >= 0
58  *
59  * Note that the second constraint is the negation of
60  *
61  *              f - m d >= n
62  */
63 static int add_div_constraints(struct isl_ctx *ctx,
64         struct isl_basic_map *bmap, int *pos, PipNewparm *p, unsigned div)
65 {
66         int i, j;
67         unsigned total = isl_basic_map_total_dim(bmap);
68         unsigned div_pos = 1 + total - bmap->n_div + div;
69
70         i = add_inequality(ctx, bmap, pos, p->vector);
71         if (i < 0)
72                 return -1;
73         copy_values_from(&bmap->ineq[i][div_pos], &p->deno, 1);
74         isl_int_neg(bmap->ineq[i][div_pos], bmap->ineq[i][div_pos]);
75
76         j = isl_basic_map_alloc_inequality(bmap);
77         if (j < 0)
78                 return -1;
79         isl_seq_neg(bmap->ineq[j], bmap->ineq[i], 1 + total);
80         isl_int_add(bmap->ineq[j][0], bmap->ineq[j][0], bmap->ineq[j][div_pos]);
81         isl_int_sub_ui(bmap->ineq[j][0], bmap->ineq[j][0], 1);
82         return j;
83 }
84
85 static int add_equality(struct isl_ctx *ctx,
86                    struct isl_basic_map *bmap, int *pos,
87                    unsigned var, PipVector *vec)
88 {
89         int i;
90         unsigned nparam = isl_basic_map_n_param(bmap);
91         unsigned n_in = isl_basic_map_n_in(bmap);
92         unsigned n_out = isl_basic_map_n_out(bmap);
93
94         isl_assert(ctx, var < n_out, return -1);
95
96         i = isl_basic_map_alloc_equality(bmap);
97         if (i < 0)
98                 return -1;
99         copy_constraint_from(bmap->eq[i], vec,
100             nparam, n_in, n_out, bmap->extra, pos);
101         isl_int_set_si(bmap->eq[i][1+nparam+n_in+var], -1);
102
103         return i;
104 }
105
106 static int find_div(struct isl_ctx *ctx,
107                    struct isl_basic_map *bmap, int *pos, PipNewparm *p)
108 {
109         int i, j;
110         unsigned nparam = isl_basic_map_n_param(bmap);
111         unsigned n_in = isl_basic_map_n_in(bmap);
112         unsigned n_out = isl_basic_map_n_out(bmap);
113
114         i = isl_basic_map_alloc_div(bmap);
115         if (i < 0)
116                 return -1;
117
118         copy_constraint_from(bmap->div[i]+1, p->vector,
119             nparam, n_in, n_out, bmap->extra, pos);
120
121         copy_values_from(bmap->div[i], &p->deno, 1);
122         for (j = 0; j < i; ++j)
123                 if (isl_seq_eq(bmap->div[i], bmap->div[j],
124                                 1+1+isl_basic_map_total_dim(bmap)+j)) {
125                         isl_basic_map_free_div(bmap, 1);
126                         return j;
127                 }
128
129         if (add_div_constraints(ctx, bmap, pos, p, i) < 0)
130                 return -1;
131
132         return i;
133 }
134
135 /* Count some properties of a quast
136  * - maximal number of new parameters
137  * - maximal depth
138  * - total number of solutions
139  * - total number of empty branches
140  */
141 static void quast_count(PipQuast *q, int *maxnew, int depth, int *maxdepth,
142                         int *sol, int *nosol)
143 {
144         PipNewparm *p;
145
146         for (p = q->newparm; p; p = p->next)
147                 if (p->rank > *maxnew)
148                         *maxnew = p->rank;
149         if (q->condition) {
150                 if (++depth > *maxdepth)
151                         *maxdepth = depth;
152                 quast_count(q->next_else, maxnew, depth, maxdepth, sol, nosol);
153                 quast_count(q->next_then, maxnew, depth, maxdepth, sol, nosol);
154         } else {
155                 if (q->list)
156                         ++(*sol);
157                 else
158                         ++(*nosol);
159         }
160 }
161
162 /*
163  * pos: array of length bmap->set.extra, mapping each of the existential
164  *              variables PIP proposes to an existential variable in bmap
165  * bmap: collects the currently active constraints
166  * rest: collects the empty leaves of the quast (if not NULL)
167  */
168 struct scan_data {
169         struct isl_ctx                  *ctx;
170         struct isl_basic_map            *bmap;
171         struct isl_set                  **rest;
172         int        *pos;
173 };
174
175 /*
176  * New existentially quantified variables are places after the existing ones.
177  */
178 static struct isl_map *scan_quast_r(struct scan_data *data, PipQuast *q,
179                                     struct isl_map *map)
180 {
181         PipNewparm *p;
182         struct isl_basic_map *bmap = data->bmap;
183         unsigned old_n_div = bmap->n_div;
184         unsigned nparam = isl_basic_map_n_param(bmap);
185         unsigned n_in = isl_basic_map_n_in(bmap);
186         unsigned n_out = isl_basic_map_n_out(bmap);
187
188         if (!map)
189                 goto error;
190
191         for (p = q->newparm; p; p = p->next) {
192                 int pos;
193                 unsigned pip_param = nparam + n_in;
194
195                 pos = find_div(data->ctx, bmap, data->pos, p);
196                 if (pos < 0)
197                         goto error;
198                 data->pos[p->rank - pip_param] = pos;
199         }
200
201         if (q->condition) {
202                 int pos = add_inequality(data->ctx, bmap, data->pos,
203                                          q->condition);
204                 if (pos < 0)
205                         goto error;
206                 map = scan_quast_r(data, q->next_then, map);
207
208                 if (isl_inequality_negate(bmap, pos))
209                         goto error;
210                 map = scan_quast_r(data, q->next_else, map);
211
212                 if (isl_basic_map_free_inequality(bmap, 1))
213                         goto error;
214         } else if (q->list) {
215                 PipList *l;
216                 int j;
217                 /* if bmap->n_out is zero, we are only interested in the domains
218                  * where a solution exists and not in the actual solution
219                  */
220                 for (j = 0, l = q->list; j < n_out && l; ++j, l = l->next)
221                         if (add_equality(data->ctx, bmap, data->pos, j,
222                                                 l->vector) < 0)
223                                 goto error;
224                 map = isl_map_add(map, isl_basic_map_copy(bmap));
225                 if (isl_basic_map_free_equality(bmap, n_out))
226                         goto error;
227         } else if (map->n && data->rest) {
228                 /* not interested in rest if no sol */
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);
232                 if (!bset)
233                         goto error;
234                 *data->rest = isl_set_add(*data->rest, bset);
235         }
236
237         if (isl_basic_map_free_inequality(bmap, 2*(bmap->n_div - old_n_div)))
238                 goto error;
239         if (isl_basic_map_free_div(bmap, bmap->n_div - old_n_div))
240                 goto error;
241         return map;
242 error:
243         isl_map_free(map);
244         return NULL;
245 }
246
247 /*
248  * Returns a map with "context" as domain and as range the first
249  * "keep" variables in the quast lists.
250  */
251 static struct isl_map *isl_map_from_quast(struct isl_ctx *ctx, PipQuast *q,
252                 unsigned keep,
253                 struct isl_basic_set *context,
254                 struct isl_set **rest)
255 {
256         int             pip_param;
257         int             nexist;
258         int             max_depth;
259         int             n_sol, n_nosol;
260         struct scan_data        data;
261         struct isl_map          *map;
262         struct isl_dim          *dims;
263         unsigned                nparam;
264         unsigned                dim;
265
266         data.ctx = ctx;
267         data.rest = rest;
268         data.bmap = NULL;
269         data.pos = NULL;
270
271         if (!context)
272                 goto error;
273
274         dim = isl_basic_set_n_dim(context);
275         nparam = isl_basic_set_n_param(context);
276         pip_param = nparam + dim;
277
278         max_depth = 0;
279         n_sol = 0;
280         n_nosol = 0;
281         nexist = pip_param-1;
282         quast_count(q, &nexist, 0, &max_depth, &n_sol, &n_nosol);
283         nexist -= pip_param-1;
284
285         if (rest) {
286                 *rest = isl_set_alloc(data.ctx, nparam, dim, n_nosol,
287                                         ISL_MAP_DISJOINT);
288                 if (!*rest)
289                         goto error;
290         }
291         map = isl_map_alloc(data.ctx, nparam, dim, keep, n_sol,
292                                 ISL_MAP_DISJOINT);
293         if (!map)
294                 goto error;
295
296         dims = isl_dim_reverse(isl_dim_copy(context->dim));
297         data.bmap = isl_basic_map_from_basic_set(context, dims);
298         data.bmap = isl_basic_map_extend(data.bmap,
299             nparam, dim, keep, nexist, keep, max_depth+2*nexist);
300         if (!data.bmap)
301                 goto error;
302
303         if (data.bmap->extra) {
304                 int i;
305                 data.pos = isl_alloc_array(ctx, int, data.bmap->extra);
306                 if (!data.pos)
307                         goto error;
308                 for (i = 0; i < data.bmap->n_div; ++i)
309                         data.pos[i] = i;
310         }
311
312         map = scan_quast_r(&data, q, map);
313         map = isl_map_finalize(map);
314         if (!map)
315                 goto error;
316         if (rest) {
317                 *rest = isl_set_finalize(*rest);
318                 if (!*rest)
319                         goto error;
320         }
321         isl_basic_map_free(data.bmap);
322         if (data.pos)
323                 free(data.pos);
324         return map;
325 error:
326         if (data.pos)
327                 free(data.pos);
328         isl_basic_map_free(data.bmap);
329         isl_map_free(map);
330         if (rest) {
331                 isl_set_free(*rest);
332                 *rest = NULL;
333         }
334         return NULL;
335 }
336
337 static void copy_values_to(Entier *dst, isl_int *src, unsigned n)
338 {
339         int i;
340
341         for (i = 0; i < n; ++i)
342                 entier_assign(dst[i], src[i]);
343 }
344
345 static void copy_constraint_to(Entier *dst, isl_int *src,
346                 unsigned pip_param, unsigned pip_var,
347                 unsigned extra_front, unsigned extra_back)
348 {
349         copy_values_to(dst+1+extra_front+pip_var+pip_param+extra_back, src, 1);
350         copy_values_to(dst+1+extra_front+pip_var, src+1, pip_param);
351         copy_values_to(dst+1+extra_front, src+1+pip_param, pip_var);
352 }
353
354 PipMatrix *isl_basic_map_to_pip(struct isl_basic_map *bmap, unsigned pip_param,
355                          unsigned extra_front, unsigned extra_back)
356 {
357         int i;
358         unsigned nrow;
359         unsigned ncol;
360         PipMatrix *M;
361         unsigned off;
362         unsigned pip_var = isl_basic_map_total_dim(bmap) - pip_param;
363
364         nrow = extra_front + bmap->n_eq + bmap->n_ineq;
365         ncol = 1 + extra_front + pip_var + pip_param + extra_back + 1;
366         M = pip_matrix_alloc(nrow, ncol);
367         if (!M)
368                 return NULL;
369
370         off = extra_front;
371         for (i = 0; i < bmap->n_eq; ++i) {
372                 entier_set_si(M->p[off+i][0], 0);
373                 copy_constraint_to(M->p[off+i], bmap->eq[i],
374                                    pip_param, pip_var, extra_front, extra_back);
375         }
376         off += bmap->n_eq;
377         for (i = 0; i < bmap->n_ineq; ++i) {
378                 entier_set_si(M->p[off+i][0], 1);
379                 copy_constraint_to(M->p[off+i], bmap->ineq[i],
380                                    pip_param, pip_var, extra_front, extra_back);
381         }
382         return M;
383 }
384
385 static struct isl_map *extremum_on(
386                 struct isl_basic_map *bmap, struct isl_basic_set *dom,
387                 struct isl_set **empty, int max)
388 {
389         PipOptions      *options;
390         PipQuast        *sol;
391         struct isl_map  *map;
392         struct isl_ctx  *ctx;
393         PipMatrix *domain = NULL, *context = NULL;
394         unsigned         nparam, n_in, n_out;
395
396         if (!bmap || !dom)
397                 goto error;
398
399         ctx = bmap->ctx;
400         isl_assert(ctx, isl_basic_map_compatible_domain(bmap, dom), goto error);
401         nparam = isl_basic_map_n_param(bmap);
402         n_in = isl_basic_map_n_in(bmap);
403         n_out = isl_basic_map_n_out(bmap);
404
405         domain = isl_basic_map_to_pip(bmap, nparam + n_in, 0, dom->n_div);
406         if (!domain)
407                 goto error;
408         context = isl_basic_map_to_pip((struct isl_basic_map *)dom, 0, 0, 0);
409         if (!context)
410                 goto error;
411
412         options = pip_options_init();
413         options->Simplify = 1;
414         options->Maximize = max;
415         options->Urs_unknowns = -1;
416         options->Urs_parms = -1;
417         sol = pip_solve(domain, context, -1, options);
418
419         if (sol) {
420                 struct isl_basic_set *copy;
421                 copy = isl_basic_set_copy(dom);
422                 map = isl_map_from_quast(ctx, sol, n_out, copy, empty);
423         } else {
424                 map = isl_map_empty_like_basic_map(bmap);
425                 if (empty)
426                         *empty = NULL;
427         }
428         if (!map)
429                 goto error;
430         if (map->n == 0 && empty) {
431                 isl_set_free(*empty);
432                 *empty = isl_set_from_basic_set(dom);
433         } else
434                 isl_basic_set_free(dom);
435         isl_basic_map_free(bmap);
436
437         pip_quast_free(sol);
438         pip_options_free(options);
439         pip_matrix_free(domain);
440         pip_matrix_free(context);
441
442         return map;
443 error:
444         if (domain)
445                 pip_matrix_free(domain);
446         if (context)
447                 pip_matrix_free(context);
448         isl_basic_map_free(bmap);
449         isl_basic_set_free(dom);
450         return NULL;
451 }
452
453 struct isl_map *isl_pip_basic_map_lexmax(
454                 struct isl_basic_map *bmap, struct isl_basic_set *dom,
455                 struct isl_set **empty)
456 {
457         return extremum_on(bmap, dom, empty, 1);
458 }
459
460 struct isl_map *isl_pip_basic_map_lexmin(
461                 struct isl_basic_map *bmap, struct isl_basic_set *dom,
462                 struct isl_set **empty)
463 {
464         return extremum_on(bmap, dom, empty, 0);
465 }
466
467 struct isl_map *isl_pip_basic_map_compute_divs(struct isl_basic_map *bmap)
468 {
469         PipMatrix *domain = NULL, *context = NULL;
470         PipOptions      *options;
471         PipQuast        *sol;
472         struct isl_ctx  *ctx;
473         struct isl_dim  *dim;
474         struct isl_map  *map;
475         struct isl_set  *set;
476         struct isl_basic_set    *dom;
477         unsigned         nparam;
478         unsigned         n_in;
479         unsigned         n_out;
480
481         if (!bmap)
482                 goto error;
483
484         ctx = bmap->ctx;
485         nparam = isl_basic_map_n_param(bmap);
486         n_in = isl_basic_map_n_in(bmap);
487         n_out = isl_basic_map_n_out(bmap);
488
489         domain = isl_basic_map_to_pip(bmap, nparam + n_in + n_out, 0, 0);
490         if (!domain)
491                 goto error;
492         context = pip_matrix_alloc(0, nparam + n_in + n_out + 2);
493         if (!context)
494                 goto error;
495
496         options = pip_options_init();
497         options->Simplify = 1;
498         options->Urs_unknowns = -1;
499         options->Urs_parms = -1;
500         sol = pip_solve(domain, context, -1, options);
501
502         dom = isl_basic_set_alloc(ctx, nparam, n_in + n_out, 0, 0, 0);
503         map = isl_map_from_quast(ctx, sol, 0, dom, NULL);
504
505         pip_quast_free(sol);
506         pip_options_free(options);
507         pip_matrix_free(domain);
508         pip_matrix_free(context);
509
510         dim = isl_dim_copy(bmap->dim);
511         isl_basic_map_free(bmap);
512
513         set = isl_map_domain(map);
514
515         return isl_map_from_set(set, dim);
516 error:
517         if (domain)
518                 pip_matrix_free(domain);
519         if (context)
520                 pip_matrix_free(context);
521         isl_basic_set_free(dom);
522         isl_basic_map_free(bmap);
523         return NULL;
524 }