0ae2d8940b574a0d63a2e97707bb9a915c6b58a9
[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         return isl_mat_is_equal(ls1->div, ls2->div);
132 }
133
134 int isl_local_space_dim(__isl_keep isl_local_space *ls,
135         enum isl_dim_type type)
136 {
137         if (!ls)
138                 return 0;
139         if (type == isl_dim_div)
140                 return ls->div->n_row;
141         if (type == isl_dim_all)
142                 return isl_dim_size(ls->dim, isl_dim_all) + ls->div->n_row;
143         return isl_dim_size(ls->dim, type);
144 }
145
146 unsigned isl_local_space_offset(__isl_keep isl_local_space *ls,
147         enum isl_dim_type type)
148 {
149         isl_dim *dim;
150
151         if (!ls)
152                 return 0;
153
154         dim = ls->dim;
155         switch (type) {
156         case isl_dim_cst:       return 0;
157         case isl_dim_param:     return 1;
158         case isl_dim_in:        return 1 + dim->nparam;
159         case isl_dim_out:       return 1 + dim->nparam + dim->n_in;
160         case isl_dim_div:       return 1 + dim->nparam + dim->n_in + dim->n_out;
161         default:                return 0;
162         }
163 }
164
165 const char *isl_local_space_get_dim_name(__isl_keep isl_local_space *ls,
166         enum isl_dim_type type, unsigned pos)
167 {
168         return ls ? isl_dim_get_name(ls->dim, type, pos) : NULL;
169 }
170
171 __isl_give isl_div *isl_local_space_get_div(__isl_keep isl_local_space *ls,
172         int pos)
173 {
174         isl_basic_map *bmap;
175
176         if (!ls)
177                 return NULL;
178
179         if (pos < 0 || pos >= ls->div->n_row)
180                 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
181                         "index out of bounds", return NULL);
182
183         if (isl_int_is_zero(ls->div->row[pos][0]))
184                 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
185                         "expression of div unknown", return NULL);
186
187         bmap = isl_basic_map_from_local_space(isl_local_space_copy(ls));
188         return isl_basic_map_div(bmap, pos);
189 }
190
191 __isl_give isl_dim *isl_local_space_get_dim(__isl_keep isl_local_space *ls)
192 {
193         if (!ls)
194                 return NULL;
195
196         return isl_dim_copy(ls->dim);
197 }
198
199 __isl_give isl_local_space *isl_local_space_set_dim_name(
200         __isl_take isl_local_space *ls,
201         enum isl_dim_type type, unsigned pos, const char *s)
202 {
203         ls = isl_local_space_cow(ls);
204         if (!ls)
205                 return NULL;
206         ls->dim = isl_dim_set_name(ls->dim, type, pos, s);
207         if (!ls->dim)
208                 return isl_local_space_free(ls);
209
210         return ls;
211 }
212
213 __isl_give isl_local_space *isl_local_space_add_div(
214         __isl_take isl_local_space *ls, __isl_take isl_vec *div)
215 {
216         ls = isl_local_space_cow(ls);
217         if (!ls || !div)
218                 goto error;
219
220         if (ls->div->n_col != div->size)
221                 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
222                         "incompatible dimensions", goto error);
223
224         ls->div = isl_mat_add_zero_cols(ls->div, 1);
225         ls->div = isl_mat_add_rows(ls->div, 1);
226         if (!ls->div)
227                 goto error;
228
229         isl_seq_cpy(ls->div->row[ls->div->n_row - 1], div->el, div->size);
230         isl_int_set_si(ls->div->row[ls->div->n_row - 1][div->size], 0);
231
232         isl_vec_free(div);
233         return ls;
234 error:
235         isl_local_space_free(ls);
236         isl_vec_free(div);
237         return NULL;
238 }
239
240 __isl_give isl_local_space *isl_local_space_replace_divs(
241         __isl_take isl_local_space *ls, __isl_take isl_mat *div)
242 {
243         ls = isl_local_space_cow(ls);
244
245         if (!ls || !div)
246                 goto error;
247
248         isl_mat_free(ls->div);
249         ls->div = div;
250         return ls;
251 error:
252         isl_mat_free(div);
253         isl_local_space_free(ls);
254         return NULL;
255 }
256
257 /* Copy row "s" of "src" to row "d" of "dst", applying the expansion
258  * defined by "exp".
259  */
260 static void expand_row(__isl_keep isl_mat *dst, int d,
261         __isl_keep isl_mat *src, int s, int *exp)
262 {
263         int i;
264         unsigned c = src->n_col - src->n_row;
265
266         isl_seq_cpy(dst->row[d], src->row[s], c);
267         isl_seq_clr(dst->row[d] + c, dst->n_col - c);
268
269         for (i = 0; i < s; ++i)
270                 isl_int_set(dst->row[d][c + exp[i]], src->row[s][c + i]);
271 }
272
273 /* Compare (known) divs.
274  * Return non-zero if at least one of the two divs is unknown.
275  */
276 static int cmp_row(__isl_keep isl_mat *div, int i, int j)
277 {
278         int li, lj;
279
280         if (isl_int_is_zero(div->row[j][0]))
281                 return -1;
282         if (isl_int_is_zero(div->row[i][0]))
283                 return 1;
284
285         li = isl_seq_last_non_zero(div->row[i], div->n_col);
286         lj = isl_seq_last_non_zero(div->row[j], div->n_col);
287
288         if (li != lj)
289                 return li - lj;
290
291         return isl_seq_cmp(div->row[i], div->row[j], div->n_col);
292 }
293
294 /* Combine the two lists of divs into a single list.
295  * For each row i in div1, exp1[i] is set to the position of the corresponding
296  * row in the result.  Similarly for div2 and exp2.
297  * This function guarantees
298  *      exp1[i] >= i
299  *      exp1[i+1] > exp1[i]
300  * For optimal merging, the two input list should have been sorted.
301  */
302 __isl_give isl_mat *isl_merge_divs(__isl_keep isl_mat *div1,
303         __isl_keep isl_mat *div2, int *exp1, int *exp2)
304 {
305         int i, j, k;
306         isl_mat *div = NULL;
307         unsigned d = div1->n_col - div1->n_row;
308
309         div = isl_mat_alloc(div1->ctx, 1 + div1->n_row + div2->n_row,
310                                 d + div1->n_row + div2->n_row);
311         if (!div)
312                 return NULL;
313
314         for (i = 0, j = 0, k = 0; i < div1->n_row && j < div2->n_row; ++k) {
315                 int cmp;
316
317                 expand_row(div, k, div1, i, exp1);
318                 expand_row(div, k + 1, div2, j, exp2);
319
320                 cmp = cmp_row(div, k, k + 1);
321                 if (cmp == 0) {
322                         exp1[i++] = k;
323                         exp2[j++] = k;
324                 } else if (cmp < 0) {
325                         exp1[i++] = k;
326                 } else {
327                         exp2[j++] = k;
328                         isl_seq_cpy(div->row[k], div->row[k + 1], div->n_col);
329                 }
330         }
331         for (; i < div1->n_row; ++i, ++k) {
332                 expand_row(div, k, div1, i, exp1);
333                 exp1[i] = k;
334         }
335         for (; j < div2->n_row; ++j, ++k) {
336                 expand_row(div, k, div2, j, exp2);
337                 exp2[j] = k;
338         }
339
340         div->n_row = k;
341         div->n_col = d + k;
342
343         return div;
344 }
345
346 int isl_local_space_divs_known(__isl_keep isl_local_space *ls)
347 {
348         int i;
349
350         if (!ls)
351                 return -1;
352
353         for (i = 0; i < ls->div->n_row; ++i)
354                 if (isl_int_is_zero(ls->div->row[i][0]))
355                         return 0;
356
357         return 1;
358 }
359
360 /* Construct a local space for a map that has the given local
361  * space as domain and that has a zero-dimensional range.
362  */
363 __isl_give isl_local_space *isl_local_space_from_domain(
364         __isl_take isl_local_space *ls)
365 {
366         ls = isl_local_space_cow(ls);
367         if (!ls)
368                 return NULL;
369         ls->dim = isl_dim_from_domain(ls->dim);
370         if (!ls->dim)
371                 return isl_local_space_free(ls);
372         return ls;
373 }
374
375 __isl_give isl_local_space *isl_local_space_add_dims(
376         __isl_take isl_local_space *ls, enum isl_dim_type type, unsigned n)
377 {
378         int pos;
379
380         if (!ls)
381                 return NULL;
382         pos = isl_local_space_dim(ls, type);
383         return isl_local_space_insert_dims(ls, type, pos, n);
384 }
385
386 /* Remove common factor of non-constant terms and denominator.
387  */
388 static void normalize_div(__isl_keep isl_local_space *ls, int div)
389 {
390         isl_ctx *ctx = ls->div->ctx;
391         unsigned total = ls->div->n_col - 2;
392
393         isl_seq_gcd(ls->div->row[div] + 2, total, &ctx->normalize_gcd);
394         isl_int_gcd(ctx->normalize_gcd,
395                     ctx->normalize_gcd, ls->div->row[div][0]);
396         if (isl_int_is_one(ctx->normalize_gcd))
397                 return;
398
399         isl_seq_scale_down(ls->div->row[div] + 2, ls->div->row[div] + 2,
400                             ctx->normalize_gcd, total);
401         isl_int_divexact(ls->div->row[div][0], ls->div->row[div][0],
402                             ctx->normalize_gcd);
403         isl_int_fdiv_q(ls->div->row[div][1], ls->div->row[div][1],
404                             ctx->normalize_gcd);
405 }
406
407 /* Exploit the equalities in "eq" to simplify the expressions of
408  * the integer divisions in "ls".
409  * The integer divisions in "ls" are assumed to appear as regular
410  * dimensions in "eq".
411  */
412 __isl_give isl_local_space *isl_local_space_substitute_equalities(
413         __isl_take isl_local_space *ls, __isl_take isl_basic_set *eq)
414 {
415         int i, j, k;
416         unsigned total;
417         unsigned n_div;
418
419         ls = isl_local_space_cow(ls);
420         if (!ls || !eq)
421                 goto error;
422
423         total = isl_dim_total(eq->dim);
424         if (isl_local_space_dim(ls, isl_dim_all) != total)
425                 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
426                         "dimensions don't match", goto error);
427         total++;
428         n_div = eq->n_div;
429         for (i = 0; i < eq->n_eq; ++i) {
430                 j = isl_seq_last_non_zero(eq->eq[i], total + n_div);
431                 if (j < 0 || j == 0 || j >= total)
432                         continue;
433
434                 for (k = 0; k < ls->div->n_row; ++k) {
435                         if (isl_int_is_zero(ls->div->row[k][1 + j]))
436                                 continue;
437                         isl_seq_elim(ls->div->row[k] + 1, eq->eq[i], j, total,
438                                         &ls->div->row[k][0]);
439                         normalize_div(ls, k);
440                 }
441         }
442
443         isl_basic_set_free(eq);
444         return ls;
445 error:
446         isl_basic_set_free(eq);
447         isl_local_space_free(ls);
448         return NULL;
449 }
450
451 int isl_local_space_is_named_or_nested(__isl_keep isl_local_space *ls,
452         enum isl_dim_type type)
453 {
454         if (!ls)
455                 return -1;
456         return isl_dim_is_named_or_nested(ls->dim, type);
457 }
458
459 __isl_give isl_local_space *isl_local_space_drop_dims(
460         __isl_take isl_local_space *ls,
461         enum isl_dim_type type, unsigned first, unsigned n)
462 {
463         isl_ctx *ctx;
464
465         if (!ls)
466                 return NULL;
467         if (n == 0 && !isl_local_space_is_named_or_nested(ls, type))
468                 return ls;
469
470         ctx = isl_local_space_get_ctx(ls);
471         if (first + n > isl_local_space_dim(ls, type))
472                 isl_die(ctx, isl_error_invalid, "range out of bounds",
473                         return isl_local_space_free(ls));
474
475         ls = isl_local_space_cow(ls);
476         if (!ls)
477                 return NULL;
478
479         if (type == isl_dim_div) {
480                 ls->div = isl_mat_drop_rows(ls->div, first, n);
481         } else {
482                 ls->dim = isl_dim_drop(ls->dim, type, first, n);
483                 if (!ls->dim)
484                         return isl_local_space_free(ls);
485         }
486
487         first += 1 + isl_local_space_offset(ls, type);
488         ls->div = isl_mat_drop_cols(ls->div, first, n);
489         if (!ls->div)
490                 return isl_local_space_free(ls);
491
492         return ls;
493 }
494
495 __isl_give isl_local_space *isl_local_space_insert_dims(
496         __isl_take isl_local_space *ls,
497         enum isl_dim_type type, unsigned first, unsigned n)
498 {
499         isl_ctx *ctx;
500
501         if (!ls)
502                 return NULL;
503         if (n == 0 && !isl_local_space_is_named_or_nested(ls, type))
504                 return ls;
505
506         ctx = isl_local_space_get_ctx(ls);
507         if (first > isl_local_space_dim(ls, type))
508                 isl_die(ctx, isl_error_invalid, "position out of bounds",
509                         return isl_local_space_free(ls));
510
511         ls = isl_local_space_cow(ls);
512         if (!ls)
513                 return NULL;
514
515         if (type == isl_dim_div) {
516                 ls->div = isl_mat_insert_zero_rows(ls->div, first, n);
517         } else {
518                 ls->dim = isl_dim_insert(ls->dim, type, first, n);
519                 if (!ls->dim)
520                         return isl_local_space_free(ls);
521         }
522
523         first += 1 + isl_local_space_offset(ls, type);
524         ls->div = isl_mat_insert_zero_cols(ls->div, first, n);
525         if (!ls->div)
526                 return isl_local_space_free(ls);
527
528         return ls;
529 }