add isl_local_space_is_set
[platform/upstream/isl.git] / isl_local_space.c
1 /*
2  * Copyright 2011      INRIA Saclay
3  *
4  * Use of this software is governed by the GNU LGPLv2.1 license
5  *
6  * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
7  * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
8  * 91893 Orsay, France
9  */
10
11 #include <isl_ctx_private.h>
12 #include <isl_map_private.h>
13 #include <isl_local_space_private.h>
14 #include <isl_space_private.h>
15 #include <isl_mat_private.h>
16 #include <isl/seq.h>
17
18 isl_ctx *isl_local_space_get_ctx(__isl_keep isl_local_space *ls)
19 {
20         return ls ? ls->dim->ctx : NULL;
21 }
22
23 __isl_give isl_local_space *isl_local_space_alloc_div(__isl_take isl_space *dim,
24         __isl_take isl_mat *div)
25 {
26         isl_ctx *ctx;
27         isl_local_space *ls = NULL;
28
29         if (!dim)
30                 goto error;
31
32         ctx = isl_space_get_ctx(dim);
33         ls = isl_calloc_type(ctx, struct isl_local_space);
34         if (!ls)
35                 goto error;
36
37         ls->ref = 1;
38         ls->dim = dim;
39         ls->div = div;
40
41         return ls;
42 error:
43         isl_space_free(dim);
44         isl_local_space_free(ls);
45         return NULL;
46 }
47
48 __isl_give isl_local_space *isl_local_space_alloc(__isl_take isl_space *dim,
49         unsigned n_div)
50 {
51         isl_ctx *ctx;
52         isl_mat *div;
53         unsigned total;
54
55         if (!dim)
56                 return NULL;
57
58         total = isl_space_dim(dim, isl_dim_all);
59
60         ctx = isl_space_get_ctx(dim);
61         div = isl_mat_alloc(ctx, n_div, 1 + 1 + total + n_div);
62         return isl_local_space_alloc_div(dim, div);
63 }
64
65 __isl_give isl_local_space *isl_local_space_from_space(__isl_take isl_space *dim)
66 {
67         return isl_local_space_alloc(dim, 0);
68 }
69
70 __isl_give isl_local_space *isl_local_space_copy(__isl_keep isl_local_space *ls)
71 {
72         if (!ls)
73                 return NULL;
74
75         ls->ref++;
76         return ls;
77 }
78
79 __isl_give isl_local_space *isl_local_space_dup(__isl_keep isl_local_space *ls)
80 {
81         if (!ls)
82                 return NULL;
83
84         return isl_local_space_alloc_div(isl_space_copy(ls->dim),
85                                          isl_mat_copy(ls->div));
86
87 }
88
89 __isl_give isl_local_space *isl_local_space_cow(__isl_take isl_local_space *ls)
90 {
91         if (!ls)
92                 return NULL;
93
94         if (ls->ref == 1)
95                 return ls;
96         ls->ref--;
97         return isl_local_space_dup(ls);
98 }
99
100 void *isl_local_space_free(__isl_take isl_local_space *ls)
101 {
102         if (!ls)
103                 return NULL;
104
105         if (--ls->ref > 0)
106                 return NULL;
107
108         isl_space_free(ls->dim);
109         isl_mat_free(ls->div);
110
111         free(ls);
112
113         return NULL;
114 }
115
116 /* Is the local space that of a set?
117  */
118 int isl_local_space_is_set(__isl_keep isl_local_space *ls)
119 {
120         return ls ? isl_space_is_set(ls->dim) : -1;
121 }
122
123 /* Return true if the two local spaces are identical, with identical
124  * expressions for the integer divisions.
125  */
126 int isl_local_space_is_equal(__isl_keep isl_local_space *ls1,
127         __isl_keep isl_local_space *ls2)
128 {
129         int equal;
130
131         if (!ls1 || !ls2)
132                 return -1;
133
134         equal = isl_space_is_equal(ls1->dim, ls2->dim);
135         if (equal < 0 || !equal)
136                 return equal;
137
138         if (!isl_local_space_divs_known(ls1))
139                 return 0;
140         if (!isl_local_space_divs_known(ls2))
141                 return 0;
142
143         return isl_mat_is_equal(ls1->div, ls2->div);
144 }
145
146 int isl_local_space_dim(__isl_keep isl_local_space *ls,
147         enum isl_dim_type type)
148 {
149         if (!ls)
150                 return 0;
151         if (type == isl_dim_div)
152                 return ls->div->n_row;
153         if (type == isl_dim_all)
154                 return isl_space_dim(ls->dim, isl_dim_all) + ls->div->n_row;
155         return isl_space_dim(ls->dim, type);
156 }
157
158 unsigned isl_local_space_offset(__isl_keep isl_local_space *ls,
159         enum isl_dim_type type)
160 {
161         isl_space *dim;
162
163         if (!ls)
164                 return 0;
165
166         dim = ls->dim;
167         switch (type) {
168         case isl_dim_cst:       return 0;
169         case isl_dim_param:     return 1;
170         case isl_dim_in:        return 1 + dim->nparam;
171         case isl_dim_out:       return 1 + dim->nparam + dim->n_in;
172         case isl_dim_div:       return 1 + dim->nparam + dim->n_in + dim->n_out;
173         default:                return 0;
174         }
175 }
176
177 const char *isl_local_space_get_dim_name(__isl_keep isl_local_space *ls,
178         enum isl_dim_type type, unsigned pos)
179 {
180         return ls ? isl_space_get_dim_name(ls->dim, type, pos) : NULL;
181 }
182
183 __isl_give isl_div *isl_local_space_get_div(__isl_keep isl_local_space *ls,
184         int pos)
185 {
186         isl_basic_map *bmap;
187
188         if (!ls)
189                 return NULL;
190
191         if (pos < 0 || pos >= ls->div->n_row)
192                 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
193                         "index out of bounds", return NULL);
194
195         if (isl_int_is_zero(ls->div->row[pos][0]))
196                 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
197                         "expression of div unknown", return NULL);
198
199         bmap = isl_basic_map_from_local_space(isl_local_space_copy(ls));
200         return isl_basic_map_div(bmap, pos);
201 }
202
203 __isl_give isl_space *isl_local_space_get_space(__isl_keep isl_local_space *ls)
204 {
205         if (!ls)
206                 return NULL;
207
208         return isl_space_copy(ls->dim);
209 }
210
211 __isl_give isl_local_space *isl_local_space_set_dim_name(
212         __isl_take isl_local_space *ls,
213         enum isl_dim_type type, unsigned pos, const char *s)
214 {
215         ls = isl_local_space_cow(ls);
216         if (!ls)
217                 return NULL;
218         ls->dim = isl_space_set_dim_name(ls->dim, type, pos, s);
219         if (!ls->dim)
220                 return isl_local_space_free(ls);
221
222         return ls;
223 }
224
225 __isl_give isl_local_space *isl_local_space_reset_space(
226         __isl_take isl_local_space *ls, __isl_take isl_space *dim)
227 {
228         ls = isl_local_space_cow(ls);
229         if (!ls || !dim)
230                 goto error;
231
232         isl_space_free(ls->dim);
233         ls->dim = dim;
234
235         return ls;
236 error:
237         isl_local_space_free(ls);
238         isl_space_free(dim);
239         return NULL;
240 }
241
242 /* Reorder the columns of the given div definitions according to the
243  * given reordering.
244  * The order of the divs themselves is assumed not to change.
245  */
246 static __isl_give isl_mat *reorder_divs(__isl_take isl_mat *div,
247         __isl_take isl_reordering *r)
248 {
249         int i, j;
250         isl_mat *mat;
251         int extra;
252
253         if (!div || !r)
254                 goto error;
255
256         extra = isl_space_dim(r->dim, isl_dim_all) + div->n_row - r->len;
257         mat = isl_mat_alloc(div->ctx, div->n_row, div->n_col + extra);
258         if (!mat)
259                 goto error;
260
261         for (i = 0; i < div->n_row; ++i) {
262                 isl_seq_cpy(mat->row[i], div->row[i], 2);
263                 isl_seq_clr(mat->row[i] + 2, mat->n_col - 2);
264                 for (j = 0; j < r->len; ++j)
265                         isl_int_set(mat->row[i][2 + r->pos[j]],
266                                     div->row[i][2 + j]);
267         }
268
269         isl_reordering_free(r);
270         isl_mat_free(div);
271         return mat;
272 error:
273         isl_reordering_free(r);
274         isl_mat_free(div);
275         return NULL;
276 }
277
278 /* Reorder the dimensions of "ls" according to the given reordering.
279  * The reordering r is assumed to have been extended with the local
280  * variables, leaving them in the same order.
281  */
282 __isl_give isl_local_space *isl_local_space_realign(
283         __isl_take isl_local_space *ls, __isl_take isl_reordering *r)
284 {
285         ls = isl_local_space_cow(ls);
286         if (!ls || !r)
287                 goto error;
288
289         ls->div = reorder_divs(ls->div, isl_reordering_copy(r));
290         if (!ls->div)
291                 goto error;
292
293         ls = isl_local_space_reset_space(ls, isl_space_copy(r->dim));
294
295         isl_reordering_free(r);
296         return ls;
297 error:
298         isl_local_space_free(ls);
299         isl_reordering_free(r);
300         return NULL;
301 }
302
303 __isl_give isl_local_space *isl_local_space_add_div(
304         __isl_take isl_local_space *ls, __isl_take isl_vec *div)
305 {
306         ls = isl_local_space_cow(ls);
307         if (!ls || !div)
308                 goto error;
309
310         if (ls->div->n_col != div->size)
311                 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
312                         "incompatible dimensions", goto error);
313
314         ls->div = isl_mat_add_zero_cols(ls->div, 1);
315         ls->div = isl_mat_add_rows(ls->div, 1);
316         if (!ls->div)
317                 goto error;
318
319         isl_seq_cpy(ls->div->row[ls->div->n_row - 1], div->el, div->size);
320         isl_int_set_si(ls->div->row[ls->div->n_row - 1][div->size], 0);
321
322         isl_vec_free(div);
323         return ls;
324 error:
325         isl_local_space_free(ls);
326         isl_vec_free(div);
327         return NULL;
328 }
329
330 __isl_give isl_local_space *isl_local_space_replace_divs(
331         __isl_take isl_local_space *ls, __isl_take isl_mat *div)
332 {
333         ls = isl_local_space_cow(ls);
334
335         if (!ls || !div)
336                 goto error;
337
338         isl_mat_free(ls->div);
339         ls->div = div;
340         return ls;
341 error:
342         isl_mat_free(div);
343         isl_local_space_free(ls);
344         return NULL;
345 }
346
347 /* Copy row "s" of "src" to row "d" of "dst", applying the expansion
348  * defined by "exp".
349  */
350 static void expand_row(__isl_keep isl_mat *dst, int d,
351         __isl_keep isl_mat *src, int s, int *exp)
352 {
353         int i;
354         unsigned c = src->n_col - src->n_row;
355
356         isl_seq_cpy(dst->row[d], src->row[s], c);
357         isl_seq_clr(dst->row[d] + c, dst->n_col - c);
358
359         for (i = 0; i < s; ++i)
360                 isl_int_set(dst->row[d][c + exp[i]], src->row[s][c + i]);
361 }
362
363 /* Compare (known) divs.
364  * Return non-zero if at least one of the two divs is unknown.
365  */
366 static int cmp_row(__isl_keep isl_mat *div, int i, int j)
367 {
368         int li, lj;
369
370         if (isl_int_is_zero(div->row[j][0]))
371                 return -1;
372         if (isl_int_is_zero(div->row[i][0]))
373                 return 1;
374
375         li = isl_seq_last_non_zero(div->row[i], div->n_col);
376         lj = isl_seq_last_non_zero(div->row[j], div->n_col);
377
378         if (li != lj)
379                 return li - lj;
380
381         return isl_seq_cmp(div->row[i], div->row[j], div->n_col);
382 }
383
384 /* Combine the two lists of divs into a single list.
385  * For each row i in div1, exp1[i] is set to the position of the corresponding
386  * row in the result.  Similarly for div2 and exp2.
387  * This function guarantees
388  *      exp1[i] >= i
389  *      exp1[i+1] > exp1[i]
390  * For optimal merging, the two input list should have been sorted.
391  */
392 __isl_give isl_mat *isl_merge_divs(__isl_keep isl_mat *div1,
393         __isl_keep isl_mat *div2, int *exp1, int *exp2)
394 {
395         int i, j, k;
396         isl_mat *div = NULL;
397         unsigned d = div1->n_col - div1->n_row;
398
399         div = isl_mat_alloc(div1->ctx, 1 + div1->n_row + div2->n_row,
400                                 d + div1->n_row + div2->n_row);
401         if (!div)
402                 return NULL;
403
404         for (i = 0, j = 0, k = 0; i < div1->n_row && j < div2->n_row; ++k) {
405                 int cmp;
406
407                 expand_row(div, k, div1, i, exp1);
408                 expand_row(div, k + 1, div2, j, exp2);
409
410                 cmp = cmp_row(div, k, k + 1);
411                 if (cmp == 0) {
412                         exp1[i++] = k;
413                         exp2[j++] = k;
414                 } else if (cmp < 0) {
415                         exp1[i++] = k;
416                 } else {
417                         exp2[j++] = k;
418                         isl_seq_cpy(div->row[k], div->row[k + 1], div->n_col);
419                 }
420         }
421         for (; i < div1->n_row; ++i, ++k) {
422                 expand_row(div, k, div1, i, exp1);
423                 exp1[i] = k;
424         }
425         for (; j < div2->n_row; ++j, ++k) {
426                 expand_row(div, k, div2, j, exp2);
427                 exp2[j] = k;
428         }
429
430         div->n_row = k;
431         div->n_col = d + k;
432
433         return div;
434 }
435
436 int isl_local_space_divs_known(__isl_keep isl_local_space *ls)
437 {
438         int i;
439
440         if (!ls)
441                 return -1;
442
443         for (i = 0; i < ls->div->n_row; ++i)
444                 if (isl_int_is_zero(ls->div->row[i][0]))
445                         return 0;
446
447         return 1;
448 }
449
450 __isl_give isl_local_space *isl_local_space_domain(
451         __isl_take isl_local_space *ls)
452 {
453         ls = isl_local_space_drop_dims(ls, isl_dim_out,
454                                         0, isl_local_space_dim(ls, isl_dim_out));
455         ls = isl_local_space_cow(ls);
456         if (!ls)
457                 return NULL;
458         ls->dim = isl_space_domain(ls->dim);
459         if (!ls->dim)
460                 return isl_local_space_free(ls);
461         return ls;
462 }
463
464 /* Construct a local space for a map that has the given local
465  * space as domain and that has a zero-dimensional range.
466  */
467 __isl_give isl_local_space *isl_local_space_from_domain(
468         __isl_take isl_local_space *ls)
469 {
470         ls = isl_local_space_cow(ls);
471         if (!ls)
472                 return NULL;
473         ls->dim = isl_space_from_domain(ls->dim);
474         if (!ls->dim)
475                 return isl_local_space_free(ls);
476         return ls;
477 }
478
479 __isl_give isl_local_space *isl_local_space_add_dims(
480         __isl_take isl_local_space *ls, enum isl_dim_type type, unsigned n)
481 {
482         int pos;
483
484         if (!ls)
485                 return NULL;
486         pos = isl_local_space_dim(ls, type);
487         return isl_local_space_insert_dims(ls, type, pos, n);
488 }
489
490 /* Remove common factor of non-constant terms and denominator.
491  */
492 static void normalize_div(__isl_keep isl_local_space *ls, int div)
493 {
494         isl_ctx *ctx = ls->div->ctx;
495         unsigned total = ls->div->n_col - 2;
496
497         isl_seq_gcd(ls->div->row[div] + 2, total, &ctx->normalize_gcd);
498         isl_int_gcd(ctx->normalize_gcd,
499                     ctx->normalize_gcd, ls->div->row[div][0]);
500         if (isl_int_is_one(ctx->normalize_gcd))
501                 return;
502
503         isl_seq_scale_down(ls->div->row[div] + 2, ls->div->row[div] + 2,
504                             ctx->normalize_gcd, total);
505         isl_int_divexact(ls->div->row[div][0], ls->div->row[div][0],
506                             ctx->normalize_gcd);
507         isl_int_fdiv_q(ls->div->row[div][1], ls->div->row[div][1],
508                             ctx->normalize_gcd);
509 }
510
511 /* Exploit the equalities in "eq" to simplify the expressions of
512  * the integer divisions in "ls".
513  * The integer divisions in "ls" are assumed to appear as regular
514  * dimensions in "eq".
515  */
516 __isl_give isl_local_space *isl_local_space_substitute_equalities(
517         __isl_take isl_local_space *ls, __isl_take isl_basic_set *eq)
518 {
519         int i, j, k;
520         unsigned total;
521         unsigned n_div;
522
523         ls = isl_local_space_cow(ls);
524         if (!ls || !eq)
525                 goto error;
526
527         total = isl_space_dim(eq->dim, isl_dim_all);
528         if (isl_local_space_dim(ls, isl_dim_all) != total)
529                 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
530                         "dimensions don't match", goto error);
531         total++;
532         n_div = eq->n_div;
533         for (i = 0; i < eq->n_eq; ++i) {
534                 j = isl_seq_last_non_zero(eq->eq[i], total + n_div);
535                 if (j < 0 || j == 0 || j >= total)
536                         continue;
537
538                 for (k = 0; k < ls->div->n_row; ++k) {
539                         if (isl_int_is_zero(ls->div->row[k][1 + j]))
540                                 continue;
541                         isl_seq_elim(ls->div->row[k] + 1, eq->eq[i], j, total,
542                                         &ls->div->row[k][0]);
543                         normalize_div(ls, k);
544                 }
545         }
546
547         isl_basic_set_free(eq);
548         return ls;
549 error:
550         isl_basic_set_free(eq);
551         isl_local_space_free(ls);
552         return NULL;
553 }
554
555 int isl_local_space_is_named_or_nested(__isl_keep isl_local_space *ls,
556         enum isl_dim_type type)
557 {
558         if (!ls)
559                 return -1;
560         return isl_space_is_named_or_nested(ls->dim, type);
561 }
562
563 __isl_give isl_local_space *isl_local_space_drop_dims(
564         __isl_take isl_local_space *ls,
565         enum isl_dim_type type, unsigned first, unsigned n)
566 {
567         isl_ctx *ctx;
568
569         if (!ls)
570                 return NULL;
571         if (n == 0 && !isl_local_space_is_named_or_nested(ls, type))
572                 return ls;
573
574         ctx = isl_local_space_get_ctx(ls);
575         if (first + n > isl_local_space_dim(ls, type))
576                 isl_die(ctx, isl_error_invalid, "range out of bounds",
577                         return isl_local_space_free(ls));
578
579         ls = isl_local_space_cow(ls);
580         if (!ls)
581                 return NULL;
582
583         if (type == isl_dim_div) {
584                 ls->div = isl_mat_drop_rows(ls->div, first, n);
585         } else {
586                 ls->dim = isl_space_drop_dims(ls->dim, type, first, n);
587                 if (!ls->dim)
588                         return isl_local_space_free(ls);
589         }
590
591         first += 1 + isl_local_space_offset(ls, type);
592         ls->div = isl_mat_drop_cols(ls->div, first, n);
593         if (!ls->div)
594                 return isl_local_space_free(ls);
595
596         return ls;
597 }
598
599 __isl_give isl_local_space *isl_local_space_insert_dims(
600         __isl_take isl_local_space *ls,
601         enum isl_dim_type type, unsigned first, unsigned n)
602 {
603         isl_ctx *ctx;
604
605         if (!ls)
606                 return NULL;
607         if (n == 0 && !isl_local_space_is_named_or_nested(ls, type))
608                 return ls;
609
610         ctx = isl_local_space_get_ctx(ls);
611         if (first > isl_local_space_dim(ls, type))
612                 isl_die(ctx, isl_error_invalid, "position out of bounds",
613                         return isl_local_space_free(ls));
614
615         ls = isl_local_space_cow(ls);
616         if (!ls)
617                 return NULL;
618
619         if (type == isl_dim_div) {
620                 ls->div = isl_mat_insert_zero_rows(ls->div, first, n);
621         } else {
622                 ls->dim = isl_space_insert_dims(ls->dim, type, first, n);
623                 if (!ls->dim)
624                         return isl_local_space_free(ls);
625         }
626
627         first += 1 + isl_local_space_offset(ls, type);
628         ls->div = isl_mat_insert_zero_cols(ls->div, first, n);
629         if (!ls->div)
630                 return isl_local_space_free(ls);
631
632         return ls;
633 }
634
635 /* Check if the constraints pointed to by "constraint" is a div
636  * constraint corresponding to div "div" in "ls".
637  *
638  * That is, if div = floor(f/m), then check if the constraint is
639  *
640  *              f - m d >= 0
641  * or
642  *              -(f-(m-1)) + m d >= 0
643  */
644 int isl_local_space_is_div_constraint(__isl_keep isl_local_space *ls,
645         isl_int *constraint, unsigned div)
646 {
647         unsigned pos;
648
649         if (!ls)
650                 return -1;
651
652         if (isl_int_is_zero(ls->div->row[div][0]))
653                 return 0;
654
655         pos = isl_local_space_offset(ls, isl_dim_div) + div;
656
657         if (isl_int_eq(constraint[pos], ls->div->row[div][0])) {
658                 int neg;
659                 isl_int_sub(ls->div->row[div][1],
660                                 ls->div->row[div][1], ls->div->row[div][0]);
661                 isl_int_add_ui(ls->div->row[div][1], ls->div->row[div][1], 1);
662                 neg = isl_seq_is_neg(constraint, ls->div->row[div]+1, pos);
663                 isl_int_sub_ui(ls->div->row[div][1], ls->div->row[div][1], 1);
664                 isl_int_add(ls->div->row[div][1],
665                                 ls->div->row[div][1], ls->div->row[div][0]);
666                 if (!neg)
667                         return 0;
668                 if (isl_seq_first_non_zero(constraint+pos+1,
669                                             ls->div->n_row-div-1) != -1)
670                         return 0;
671         } else if (isl_int_abs_eq(constraint[pos], ls->div->row[div][0])) {
672                 if (!isl_seq_eq(constraint, ls->div->row[div]+1, pos))
673                         return 0;
674                 if (isl_seq_first_non_zero(constraint+pos+1,
675                                             ls->div->n_row-div-1) != -1)
676                         return 0;
677         } else
678                 return 0;
679
680         return 1;
681 }
682
683 /*
684  * Set active[i] to 1 if the dimension at position i is involved
685  * in the linear expression l.
686  */
687 int *isl_local_space_get_active(__isl_keep isl_local_space *ls, isl_int *l)
688 {
689         int i, j;
690         isl_ctx *ctx;
691         int *active = NULL;
692         unsigned total;
693         unsigned offset;
694
695         ctx = isl_local_space_get_ctx(ls);
696         total = isl_local_space_dim(ls, isl_dim_all);
697         active = isl_calloc_array(ctx, int, total);
698         if (!active)
699                 return NULL;
700
701         for (i = 0; i < total; ++i)
702                 active[i] = !isl_int_is_zero(l[i]);
703
704         offset = isl_local_space_offset(ls, isl_dim_div) - 1;
705         for (i = ls->div->n_row - 1; i >= 0; --i) {
706                 if (!active[offset + i])
707                         continue;
708                 for (j = 0; j < total; ++j)
709                         active[j] |= !isl_int_is_zero(ls->div->row[i][2 + j]);
710         }
711
712         return active;
713 }