51602f882010aa265fbfc7d87f81aa2f67b9c2d4
[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_dim_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_dim *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_dim_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_dim_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_dim *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_dim_total(dim);
59
60         ctx = isl_dim_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_dim(__isl_take isl_dim *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_dim_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_dim_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_dim_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_dim_size(ls->dim, isl_dim_all) + ls->div->n_row;
148         return isl_dim_size(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_dim *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_dim_get_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_dim *isl_local_space_get_dim(__isl_keep isl_local_space *ls)
197 {
198         if (!ls)
199                 return NULL;
200
201         return isl_dim_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_dim_set_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_dim(
219         __isl_take isl_local_space *ls, __isl_take isl_dim *dim)
220 {
221         ls = isl_local_space_cow(ls);
222         if (!ls || !dim)
223                 goto error;
224
225         isl_dim_free(ls->dim);
226         ls->dim = dim;
227
228         return ls;
229 error:
230         isl_local_space_free(ls);
231         isl_dim_free(dim);
232         return NULL;
233 }
234
235 __isl_give isl_local_space *isl_local_space_add_div(
236         __isl_take isl_local_space *ls, __isl_take isl_vec *div)
237 {
238         ls = isl_local_space_cow(ls);
239         if (!ls || !div)
240                 goto error;
241
242         if (ls->div->n_col != div->size)
243                 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
244                         "incompatible dimensions", goto error);
245
246         ls->div = isl_mat_add_zero_cols(ls->div, 1);
247         ls->div = isl_mat_add_rows(ls->div, 1);
248         if (!ls->div)
249                 goto error;
250
251         isl_seq_cpy(ls->div->row[ls->div->n_row - 1], div->el, div->size);
252         isl_int_set_si(ls->div->row[ls->div->n_row - 1][div->size], 0);
253
254         isl_vec_free(div);
255         return ls;
256 error:
257         isl_local_space_free(ls);
258         isl_vec_free(div);
259         return NULL;
260 }
261
262 __isl_give isl_local_space *isl_local_space_replace_divs(
263         __isl_take isl_local_space *ls, __isl_take isl_mat *div)
264 {
265         ls = isl_local_space_cow(ls);
266
267         if (!ls || !div)
268                 goto error;
269
270         isl_mat_free(ls->div);
271         ls->div = div;
272         return ls;
273 error:
274         isl_mat_free(div);
275         isl_local_space_free(ls);
276         return NULL;
277 }
278
279 /* Copy row "s" of "src" to row "d" of "dst", applying the expansion
280  * defined by "exp".
281  */
282 static void expand_row(__isl_keep isl_mat *dst, int d,
283         __isl_keep isl_mat *src, int s, int *exp)
284 {
285         int i;
286         unsigned c = src->n_col - src->n_row;
287
288         isl_seq_cpy(dst->row[d], src->row[s], c);
289         isl_seq_clr(dst->row[d] + c, dst->n_col - c);
290
291         for (i = 0; i < s; ++i)
292                 isl_int_set(dst->row[d][c + exp[i]], src->row[s][c + i]);
293 }
294
295 /* Compare (known) divs.
296  * Return non-zero if at least one of the two divs is unknown.
297  */
298 static int cmp_row(__isl_keep isl_mat *div, int i, int j)
299 {
300         int li, lj;
301
302         if (isl_int_is_zero(div->row[j][0]))
303                 return -1;
304         if (isl_int_is_zero(div->row[i][0]))
305                 return 1;
306
307         li = isl_seq_last_non_zero(div->row[i], div->n_col);
308         lj = isl_seq_last_non_zero(div->row[j], div->n_col);
309
310         if (li != lj)
311                 return li - lj;
312
313         return isl_seq_cmp(div->row[i], div->row[j], div->n_col);
314 }
315
316 /* Combine the two lists of divs into a single list.
317  * For each row i in div1, exp1[i] is set to the position of the corresponding
318  * row in the result.  Similarly for div2 and exp2.
319  * This function guarantees
320  *      exp1[i] >= i
321  *      exp1[i+1] > exp1[i]
322  * For optimal merging, the two input list should have been sorted.
323  */
324 __isl_give isl_mat *isl_merge_divs(__isl_keep isl_mat *div1,
325         __isl_keep isl_mat *div2, int *exp1, int *exp2)
326 {
327         int i, j, k;
328         isl_mat *div = NULL;
329         unsigned d = div1->n_col - div1->n_row;
330
331         div = isl_mat_alloc(div1->ctx, 1 + div1->n_row + div2->n_row,
332                                 d + div1->n_row + div2->n_row);
333         if (!div)
334                 return NULL;
335
336         for (i = 0, j = 0, k = 0; i < div1->n_row && j < div2->n_row; ++k) {
337                 int cmp;
338
339                 expand_row(div, k, div1, i, exp1);
340                 expand_row(div, k + 1, div2, j, exp2);
341
342                 cmp = cmp_row(div, k, k + 1);
343                 if (cmp == 0) {
344                         exp1[i++] = k;
345                         exp2[j++] = k;
346                 } else if (cmp < 0) {
347                         exp1[i++] = k;
348                 } else {
349                         exp2[j++] = k;
350                         isl_seq_cpy(div->row[k], div->row[k + 1], div->n_col);
351                 }
352         }
353         for (; i < div1->n_row; ++i, ++k) {
354                 expand_row(div, k, div1, i, exp1);
355                 exp1[i] = k;
356         }
357         for (; j < div2->n_row; ++j, ++k) {
358                 expand_row(div, k, div2, j, exp2);
359                 exp2[j] = k;
360         }
361
362         div->n_row = k;
363         div->n_col = d + k;
364
365         return div;
366 }
367
368 int isl_local_space_divs_known(__isl_keep isl_local_space *ls)
369 {
370         int i;
371
372         if (!ls)
373                 return -1;
374
375         for (i = 0; i < ls->div->n_row; ++i)
376                 if (isl_int_is_zero(ls->div->row[i][0]))
377                         return 0;
378
379         return 1;
380 }
381
382 /* Construct a local space for a map that has the given local
383  * space as domain and that has a zero-dimensional range.
384  */
385 __isl_give isl_local_space *isl_local_space_from_domain(
386         __isl_take isl_local_space *ls)
387 {
388         ls = isl_local_space_cow(ls);
389         if (!ls)
390                 return NULL;
391         ls->dim = isl_dim_from_domain(ls->dim);
392         if (!ls->dim)
393                 return isl_local_space_free(ls);
394         return ls;
395 }
396
397 __isl_give isl_local_space *isl_local_space_add_dims(
398         __isl_take isl_local_space *ls, enum isl_dim_type type, unsigned n)
399 {
400         int pos;
401
402         if (!ls)
403                 return NULL;
404         pos = isl_local_space_dim(ls, type);
405         return isl_local_space_insert_dims(ls, type, pos, n);
406 }
407
408 /* Remove common factor of non-constant terms and denominator.
409  */
410 static void normalize_div(__isl_keep isl_local_space *ls, int div)
411 {
412         isl_ctx *ctx = ls->div->ctx;
413         unsigned total = ls->div->n_col - 2;
414
415         isl_seq_gcd(ls->div->row[div] + 2, total, &ctx->normalize_gcd);
416         isl_int_gcd(ctx->normalize_gcd,
417                     ctx->normalize_gcd, ls->div->row[div][0]);
418         if (isl_int_is_one(ctx->normalize_gcd))
419                 return;
420
421         isl_seq_scale_down(ls->div->row[div] + 2, ls->div->row[div] + 2,
422                             ctx->normalize_gcd, total);
423         isl_int_divexact(ls->div->row[div][0], ls->div->row[div][0],
424                             ctx->normalize_gcd);
425         isl_int_fdiv_q(ls->div->row[div][1], ls->div->row[div][1],
426                             ctx->normalize_gcd);
427 }
428
429 /* Exploit the equalities in "eq" to simplify the expressions of
430  * the integer divisions in "ls".
431  * The integer divisions in "ls" are assumed to appear as regular
432  * dimensions in "eq".
433  */
434 __isl_give isl_local_space *isl_local_space_substitute_equalities(
435         __isl_take isl_local_space *ls, __isl_take isl_basic_set *eq)
436 {
437         int i, j, k;
438         unsigned total;
439         unsigned n_div;
440
441         ls = isl_local_space_cow(ls);
442         if (!ls || !eq)
443                 goto error;
444
445         total = isl_dim_total(eq->dim);
446         if (isl_local_space_dim(ls, isl_dim_all) != total)
447                 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
448                         "dimensions don't match", goto error);
449         total++;
450         n_div = eq->n_div;
451         for (i = 0; i < eq->n_eq; ++i) {
452                 j = isl_seq_last_non_zero(eq->eq[i], total + n_div);
453                 if (j < 0 || j == 0 || j >= total)
454                         continue;
455
456                 for (k = 0; k < ls->div->n_row; ++k) {
457                         if (isl_int_is_zero(ls->div->row[k][1 + j]))
458                                 continue;
459                         isl_seq_elim(ls->div->row[k] + 1, eq->eq[i], j, total,
460                                         &ls->div->row[k][0]);
461                         normalize_div(ls, k);
462                 }
463         }
464
465         isl_basic_set_free(eq);
466         return ls;
467 error:
468         isl_basic_set_free(eq);
469         isl_local_space_free(ls);
470         return NULL;
471 }
472
473 int isl_local_space_is_named_or_nested(__isl_keep isl_local_space *ls,
474         enum isl_dim_type type)
475 {
476         if (!ls)
477                 return -1;
478         return isl_dim_is_named_or_nested(ls->dim, type);
479 }
480
481 __isl_give isl_local_space *isl_local_space_drop_dims(
482         __isl_take isl_local_space *ls,
483         enum isl_dim_type type, unsigned first, unsigned n)
484 {
485         isl_ctx *ctx;
486
487         if (!ls)
488                 return NULL;
489         if (n == 0 && !isl_local_space_is_named_or_nested(ls, type))
490                 return ls;
491
492         ctx = isl_local_space_get_ctx(ls);
493         if (first + n > isl_local_space_dim(ls, type))
494                 isl_die(ctx, isl_error_invalid, "range out of bounds",
495                         return isl_local_space_free(ls));
496
497         ls = isl_local_space_cow(ls);
498         if (!ls)
499                 return NULL;
500
501         if (type == isl_dim_div) {
502                 ls->div = isl_mat_drop_rows(ls->div, first, n);
503         } else {
504                 ls->dim = isl_dim_drop(ls->dim, type, first, n);
505                 if (!ls->dim)
506                         return isl_local_space_free(ls);
507         }
508
509         first += 1 + isl_local_space_offset(ls, type);
510         ls->div = isl_mat_drop_cols(ls->div, first, n);
511         if (!ls->div)
512                 return isl_local_space_free(ls);
513
514         return ls;
515 }
516
517 __isl_give isl_local_space *isl_local_space_insert_dims(
518         __isl_take isl_local_space *ls,
519         enum isl_dim_type type, unsigned first, unsigned n)
520 {
521         isl_ctx *ctx;
522
523         if (!ls)
524                 return NULL;
525         if (n == 0 && !isl_local_space_is_named_or_nested(ls, type))
526                 return ls;
527
528         ctx = isl_local_space_get_ctx(ls);
529         if (first > isl_local_space_dim(ls, type))
530                 isl_die(ctx, isl_error_invalid, "position out of bounds",
531                         return isl_local_space_free(ls));
532
533         ls = isl_local_space_cow(ls);
534         if (!ls)
535                 return NULL;
536
537         if (type == isl_dim_div) {
538                 ls->div = isl_mat_insert_zero_rows(ls->div, first, n);
539         } else {
540                 ls->dim = isl_dim_insert(ls->dim, type, first, n);
541                 if (!ls->dim)
542                         return isl_local_space_free(ls);
543         }
544
545         first += 1 + isl_local_space_offset(ls, type);
546         ls->div = isl_mat_insert_zero_cols(ls->div, first, n);
547         if (!ls->div)
548                 return isl_local_space_free(ls);
549
550         return ls;
551 }