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