42f3e9ffcf648c89073be499307a66da4aaf0b02
[platform/upstream/isl.git] / isl_local_space.c
1 /*
2  * Copyright 2011      INRIA Saclay
3  * Copyright 2012      Ecole Normale Superieure
4  *
5  * Use of this software is governed by the MIT license
6  *
7  * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
8  * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
9  * 91893 Orsay, France
10  * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
11  */
12
13 #include <isl_ctx_private.h>
14 #include <isl_map_private.h>
15 #include <isl_local_space_private.h>
16 #include <isl_space_private.h>
17 #include <isl_mat_private.h>
18 #include <isl_aff_private.h>
19 #include <isl/seq.h>
20
21 isl_ctx *isl_local_space_get_ctx(__isl_keep isl_local_space *ls)
22 {
23         return ls ? ls->dim->ctx : NULL;
24 }
25
26 __isl_give isl_local_space *isl_local_space_alloc_div(__isl_take isl_space *dim,
27         __isl_take isl_mat *div)
28 {
29         isl_ctx *ctx;
30         isl_local_space *ls = NULL;
31
32         if (!dim || !div)
33                 goto error;
34
35         ctx = isl_space_get_ctx(dim);
36         ls = isl_calloc_type(ctx, struct isl_local_space);
37         if (!ls)
38                 goto error;
39
40         ls->ref = 1;
41         ls->dim = dim;
42         ls->div = div;
43
44         return ls;
45 error:
46         isl_mat_free(div);
47         isl_space_free(dim);
48         isl_local_space_free(ls);
49         return NULL;
50 }
51
52 __isl_give isl_local_space *isl_local_space_alloc(__isl_take isl_space *dim,
53         unsigned n_div)
54 {
55         isl_ctx *ctx;
56         isl_mat *div;
57         unsigned total;
58
59         if (!dim)
60                 return NULL;
61
62         total = isl_space_dim(dim, isl_dim_all);
63
64         ctx = isl_space_get_ctx(dim);
65         div = isl_mat_alloc(ctx, n_div, 1 + 1 + total + n_div);
66         return isl_local_space_alloc_div(dim, div);
67 }
68
69 __isl_give isl_local_space *isl_local_space_from_space(__isl_take isl_space *dim)
70 {
71         return isl_local_space_alloc(dim, 0);
72 }
73
74 __isl_give isl_local_space *isl_local_space_copy(__isl_keep isl_local_space *ls)
75 {
76         if (!ls)
77                 return NULL;
78
79         ls->ref++;
80         return ls;
81 }
82
83 __isl_give isl_local_space *isl_local_space_dup(__isl_keep isl_local_space *ls)
84 {
85         if (!ls)
86                 return NULL;
87
88         return isl_local_space_alloc_div(isl_space_copy(ls->dim),
89                                          isl_mat_copy(ls->div));
90
91 }
92
93 __isl_give isl_local_space *isl_local_space_cow(__isl_take isl_local_space *ls)
94 {
95         if (!ls)
96                 return NULL;
97
98         if (ls->ref == 1)
99                 return ls;
100         ls->ref--;
101         return isl_local_space_dup(ls);
102 }
103
104 void *isl_local_space_free(__isl_take isl_local_space *ls)
105 {
106         if (!ls)
107                 return NULL;
108
109         if (--ls->ref > 0)
110                 return NULL;
111
112         isl_space_free(ls->dim);
113         isl_mat_free(ls->div);
114
115         free(ls);
116
117         return NULL;
118 }
119
120 /* Is the local space that of a set?
121  */
122 int isl_local_space_is_set(__isl_keep isl_local_space *ls)
123 {
124         return ls ? isl_space_is_set(ls->dim) : -1;
125 }
126
127 /* Return true if the two local spaces are identical, with identical
128  * expressions for the integer divisions.
129  */
130 int isl_local_space_is_equal(__isl_keep isl_local_space *ls1,
131         __isl_keep isl_local_space *ls2)
132 {
133         int equal;
134
135         if (!ls1 || !ls2)
136                 return -1;
137
138         equal = isl_space_is_equal(ls1->dim, ls2->dim);
139         if (equal < 0 || !equal)
140                 return equal;
141
142         if (!isl_local_space_divs_known(ls1))
143                 return 0;
144         if (!isl_local_space_divs_known(ls2))
145                 return 0;
146
147         return isl_mat_is_equal(ls1->div, ls2->div);
148 }
149
150 int isl_local_space_dim(__isl_keep isl_local_space *ls,
151         enum isl_dim_type type)
152 {
153         if (!ls)
154                 return 0;
155         if (type == isl_dim_div)
156                 return ls->div->n_row;
157         if (type == isl_dim_all)
158                 return isl_space_dim(ls->dim, isl_dim_all) + ls->div->n_row;
159         return isl_space_dim(ls->dim, type);
160 }
161
162 unsigned isl_local_space_offset(__isl_keep isl_local_space *ls,
163         enum isl_dim_type type)
164 {
165         isl_space *dim;
166
167         if (!ls)
168                 return 0;
169
170         dim = ls->dim;
171         switch (type) {
172         case isl_dim_cst:       return 0;
173         case isl_dim_param:     return 1;
174         case isl_dim_in:        return 1 + dim->nparam;
175         case isl_dim_out:       return 1 + dim->nparam + dim->n_in;
176         case isl_dim_div:       return 1 + dim->nparam + dim->n_in + dim->n_out;
177         default:                return 0;
178         }
179 }
180
181 /* Does the given dimension have a name?
182  */
183 int isl_local_space_has_dim_name(__isl_keep isl_local_space *ls,
184         enum isl_dim_type type, unsigned pos)
185 {
186         return ls ? isl_space_has_dim_name(ls->dim, type, pos) : -1;
187 }
188
189 const char *isl_local_space_get_dim_name(__isl_keep isl_local_space *ls,
190         enum isl_dim_type type, unsigned pos)
191 {
192         return ls ? isl_space_get_dim_name(ls->dim, type, pos) : NULL;
193 }
194
195 int isl_local_space_has_dim_id(__isl_keep isl_local_space *ls,
196         enum isl_dim_type type, unsigned pos)
197 {
198         return ls ? isl_space_has_dim_id(ls->dim, type, pos) : -1;
199 }
200
201 __isl_give isl_id *isl_local_space_get_dim_id(__isl_keep isl_local_space *ls,
202         enum isl_dim_type type, unsigned pos)
203 {
204         return ls ? isl_space_get_dim_id(ls->dim, type, pos) : NULL;
205 }
206
207 __isl_give isl_aff *isl_local_space_get_div(__isl_keep isl_local_space *ls,
208         int pos)
209 {
210         isl_aff *aff;
211
212         if (!ls)
213                 return NULL;
214
215         if (pos < 0 || pos >= ls->div->n_row)
216                 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
217                         "index out of bounds", return NULL);
218
219         if (isl_int_is_zero(ls->div->row[pos][0]))
220                 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
221                         "expression of div unknown", return NULL);
222         if (!isl_local_space_is_set(ls))
223                 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
224                         "cannot represent divs of map spaces", return NULL);
225
226         aff = isl_aff_alloc(isl_local_space_copy(ls));
227         if (!aff)
228                 return NULL;
229         isl_seq_cpy(aff->v->el, ls->div->row[pos], aff->v->size);
230         return aff;
231 }
232
233 __isl_give isl_space *isl_local_space_get_space(__isl_keep isl_local_space *ls)
234 {
235         if (!ls)
236                 return NULL;
237
238         return isl_space_copy(ls->dim);
239 }
240
241 __isl_give isl_local_space *isl_local_space_set_dim_name(
242         __isl_take isl_local_space *ls,
243         enum isl_dim_type type, unsigned pos, const char *s)
244 {
245         ls = isl_local_space_cow(ls);
246         if (!ls)
247                 return NULL;
248         ls->dim = isl_space_set_dim_name(ls->dim, type, pos, s);
249         if (!ls->dim)
250                 return isl_local_space_free(ls);
251
252         return ls;
253 }
254
255 __isl_give isl_local_space *isl_local_space_set_dim_id(
256         __isl_take isl_local_space *ls,
257         enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
258 {
259         ls = isl_local_space_cow(ls);
260         if (!ls)
261                 return isl_id_free(id);
262         ls->dim = isl_space_set_dim_id(ls->dim, type, pos, id);
263         if (!ls->dim)
264                 return isl_local_space_free(ls);
265
266         return ls;
267 }
268
269 __isl_give isl_local_space *isl_local_space_reset_space(
270         __isl_take isl_local_space *ls, __isl_take isl_space *dim)
271 {
272         ls = isl_local_space_cow(ls);
273         if (!ls || !dim)
274                 goto error;
275
276         isl_space_free(ls->dim);
277         ls->dim = dim;
278
279         return ls;
280 error:
281         isl_local_space_free(ls);
282         isl_space_free(dim);
283         return NULL;
284 }
285
286 /* Reorder the columns of the given div definitions according to the
287  * given reordering.
288  * The order of the divs themselves is assumed not to change.
289  */
290 static __isl_give isl_mat *reorder_divs(__isl_take isl_mat *div,
291         __isl_take isl_reordering *r)
292 {
293         int i, j;
294         isl_mat *mat;
295         int extra;
296
297         if (!div || !r)
298                 goto error;
299
300         extra = isl_space_dim(r->dim, isl_dim_all) + div->n_row - r->len;
301         mat = isl_mat_alloc(div->ctx, div->n_row, div->n_col + extra);
302         if (!mat)
303                 goto error;
304
305         for (i = 0; i < div->n_row; ++i) {
306                 isl_seq_cpy(mat->row[i], div->row[i], 2);
307                 isl_seq_clr(mat->row[i] + 2, mat->n_col - 2);
308                 for (j = 0; j < r->len; ++j)
309                         isl_int_set(mat->row[i][2 + r->pos[j]],
310                                     div->row[i][2 + j]);
311         }
312
313         isl_reordering_free(r);
314         isl_mat_free(div);
315         return mat;
316 error:
317         isl_reordering_free(r);
318         isl_mat_free(div);
319         return NULL;
320 }
321
322 /* Reorder the dimensions of "ls" according to the given reordering.
323  * The reordering r is assumed to have been extended with the local
324  * variables, leaving them in the same order.
325  */
326 __isl_give isl_local_space *isl_local_space_realign(
327         __isl_take isl_local_space *ls, __isl_take isl_reordering *r)
328 {
329         ls = isl_local_space_cow(ls);
330         if (!ls || !r)
331                 goto error;
332
333         ls->div = reorder_divs(ls->div, isl_reordering_copy(r));
334         if (!ls->div)
335                 goto error;
336
337         ls = isl_local_space_reset_space(ls, isl_space_copy(r->dim));
338
339         isl_reordering_free(r);
340         return ls;
341 error:
342         isl_local_space_free(ls);
343         isl_reordering_free(r);
344         return NULL;
345 }
346
347 __isl_give isl_local_space *isl_local_space_add_div(
348         __isl_take isl_local_space *ls, __isl_take isl_vec *div)
349 {
350         ls = isl_local_space_cow(ls);
351         if (!ls || !div)
352                 goto error;
353
354         if (ls->div->n_col != div->size)
355                 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
356                         "incompatible dimensions", goto error);
357
358         ls->div = isl_mat_add_zero_cols(ls->div, 1);
359         ls->div = isl_mat_add_rows(ls->div, 1);
360         if (!ls->div)
361                 goto error;
362
363         isl_seq_cpy(ls->div->row[ls->div->n_row - 1], div->el, div->size);
364         isl_int_set_si(ls->div->row[ls->div->n_row - 1][div->size], 0);
365
366         isl_vec_free(div);
367         return ls;
368 error:
369         isl_local_space_free(ls);
370         isl_vec_free(div);
371         return NULL;
372 }
373
374 __isl_give isl_local_space *isl_local_space_replace_divs(
375         __isl_take isl_local_space *ls, __isl_take isl_mat *div)
376 {
377         ls = isl_local_space_cow(ls);
378
379         if (!ls || !div)
380                 goto error;
381
382         isl_mat_free(ls->div);
383         ls->div = div;
384         return ls;
385 error:
386         isl_mat_free(div);
387         isl_local_space_free(ls);
388         return NULL;
389 }
390
391 /* Copy row "s" of "src" to row "d" of "dst", applying the expansion
392  * defined by "exp".
393  */
394 static void expand_row(__isl_keep isl_mat *dst, int d,
395         __isl_keep isl_mat *src, int s, int *exp)
396 {
397         int i;
398         unsigned c = src->n_col - src->n_row;
399
400         isl_seq_cpy(dst->row[d], src->row[s], c);
401         isl_seq_clr(dst->row[d] + c, dst->n_col - c);
402
403         for (i = 0; i < s; ++i)
404                 isl_int_set(dst->row[d][c + exp[i]], src->row[s][c + i]);
405 }
406
407 /* Compare (known) divs.
408  * Return non-zero if at least one of the two divs is unknown.
409  * In particular, if both divs are unknown, we respect their
410  * current order.  Otherwise, we sort the known div after the unknown
411  * div only if the known div depends on the unknown div.
412  */
413 static int cmp_row(isl_int *row_i, isl_int *row_j, int i, int j,
414         unsigned n_row, unsigned n_col)
415 {
416         int li, lj;
417         int unknown_i, unknown_j;
418
419         unknown_i = isl_int_is_zero(row_i[0]);
420         unknown_j = isl_int_is_zero(row_j[0]);
421
422         if (unknown_i && unknown_j)
423                 return i - j;
424
425         if (unknown_i)
426                 li = n_col - n_row + i;
427         else
428                 li = isl_seq_last_non_zero(row_i, n_col);
429         if (unknown_j)
430                 lj = n_col - n_row + j;
431         else
432                 lj = isl_seq_last_non_zero(row_j, n_col);
433
434         if (li != lj)
435                 return li - lj;
436
437         return isl_seq_cmp(row_i, row_j, n_col);
438 }
439
440 /* Call cmp_row for divs in a matrix.
441  */
442 int isl_mat_cmp_div(__isl_keep isl_mat *div, int i, int j)
443 {
444         return cmp_row(div->row[i], div->row[j], i, j, div->n_row, div->n_col);
445 }
446
447 /* Call cmp_row for divs in a basic map.
448  */
449 static int bmap_cmp_row(__isl_keep isl_basic_map *bmap, int i, int j,
450         unsigned total)
451 {
452         return cmp_row(bmap->div[i], bmap->div[j], i, j, bmap->n_div, total);
453 }
454
455 /* Sort the divs in "bmap".
456  *
457  * We first make sure divs are placed after divs on which they depend.
458  * Then we perform a simple insertion sort based on the same ordering
459  * that is used in isl_merge_divs.
460  */
461 __isl_give isl_basic_map *isl_basic_map_sort_divs(
462         __isl_take isl_basic_map *bmap)
463 {
464         int i, j;
465         unsigned total;
466
467         bmap = isl_basic_map_order_divs(bmap);
468         if (!bmap)
469                 return NULL;
470         if (bmap->n_div <= 1)
471                 return bmap;
472
473         total = 2 + isl_basic_map_total_dim(bmap);
474         for (i = 1; i < bmap->n_div; ++i) {
475                 for (j = i - 1; j >= 0; --j) {
476                         if (bmap_cmp_row(bmap, j, j + 1, total) <= 0)
477                                 break;
478                         isl_basic_map_swap_div(bmap, j, j + 1);
479                 }
480         }
481
482         return bmap;
483 }
484
485 /* Sort the divs in the basic maps of "map".
486  */
487 __isl_give isl_map *isl_map_sort_divs(__isl_take isl_map *map)
488 {
489         return isl_map_inline_foreach_basic_map(map, &isl_basic_map_sort_divs);
490 }
491
492 /* Combine the two lists of divs into a single list.
493  * For each row i in div1, exp1[i] is set to the position of the corresponding
494  * row in the result.  Similarly for div2 and exp2.
495  * This function guarantees
496  *      exp1[i] >= i
497  *      exp1[i+1] > exp1[i]
498  * For optimal merging, the two input list should have been sorted.
499  */
500 __isl_give isl_mat *isl_merge_divs(__isl_keep isl_mat *div1,
501         __isl_keep isl_mat *div2, int *exp1, int *exp2)
502 {
503         int i, j, k;
504         isl_mat *div = NULL;
505         unsigned d;
506
507         if (!div1 || !div2)
508                 return NULL;
509
510         d = div1->n_col - div1->n_row;
511         div = isl_mat_alloc(div1->ctx, 1 + div1->n_row + div2->n_row,
512                                 d + div1->n_row + div2->n_row);
513         if (!div)
514                 return NULL;
515
516         for (i = 0, j = 0, k = 0; i < div1->n_row && j < div2->n_row; ++k) {
517                 int cmp;
518
519                 expand_row(div, k, div1, i, exp1);
520                 expand_row(div, k + 1, div2, j, exp2);
521
522                 cmp = isl_mat_cmp_div(div, k, k + 1);
523                 if (cmp == 0) {
524                         exp1[i++] = k;
525                         exp2[j++] = k;
526                 } else if (cmp < 0) {
527                         exp1[i++] = k;
528                 } else {
529                         exp2[j++] = k;
530                         isl_seq_cpy(div->row[k], div->row[k + 1], div->n_col);
531                 }
532         }
533         for (; i < div1->n_row; ++i, ++k) {
534                 expand_row(div, k, div1, i, exp1);
535                 exp1[i] = k;
536         }
537         for (; j < div2->n_row; ++j, ++k) {
538                 expand_row(div, k, div2, j, exp2);
539                 exp2[j] = k;
540         }
541
542         div->n_row = k;
543         div->n_col = d + k;
544
545         return div;
546 }
547
548 /* Swap divs "a" and "b" in "ls".
549  */
550 __isl_give isl_local_space *isl_local_space_swap_div(
551         __isl_take isl_local_space *ls, int a, int b)
552 {
553         int offset;
554
555         ls = isl_local_space_cow(ls);
556         if (!ls)
557                 return NULL;
558         if (a < 0 || a >= ls->div->n_row || b < 0 || b >= ls->div->n_row)
559                 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
560                         "index out of bounds", return isl_local_space_free(ls));
561         offset = ls->div->n_col - ls->div->n_row;
562         ls->div = isl_mat_swap_cols(ls->div, offset + a, offset + b);
563         ls->div = isl_mat_swap_rows(ls->div, a, b);
564         if (!ls->div)
565                 return isl_local_space_free(ls);
566         return ls;
567 }
568
569 /* Construct a local space that contains all the divs in either
570  * "ls1" or "ls2".
571  */
572 __isl_give isl_local_space *isl_local_space_intersect(
573         __isl_take isl_local_space *ls1, __isl_take isl_local_space *ls2)
574 {
575         isl_ctx *ctx;
576         int *exp1 = NULL;
577         int *exp2 = NULL;
578         isl_mat *div;
579
580         if (!ls1 || !ls2)
581                 goto error;
582
583         ctx = isl_local_space_get_ctx(ls1);
584         if (!isl_space_is_equal(ls1->dim, ls2->dim))
585                 isl_die(ctx, isl_error_invalid,
586                         "spaces should be identical", goto error);
587
588         if (ls2->div->n_row == 0) {
589                 isl_local_space_free(ls2);
590                 return ls1;
591         }
592
593         if (ls1->div->n_row == 0) {
594                 isl_local_space_free(ls1);
595                 return ls2;
596         }
597
598         exp1 = isl_alloc_array(ctx, int, ls1->div->n_row);
599         exp2 = isl_alloc_array(ctx, int, ls2->div->n_row);
600         if (!exp1 || !exp2)
601                 goto error;
602
603         div = isl_merge_divs(ls1->div, ls2->div, exp1, exp2);
604         if (!div)
605                 goto error;
606
607         free(exp1);
608         free(exp2);
609         isl_local_space_free(ls2);
610         isl_mat_free(ls1->div);
611         ls1->div = div;
612
613         return ls1;
614 error:
615         free(exp1);
616         free(exp2);
617         isl_local_space_free(ls1);
618         isl_local_space_free(ls2);
619         return NULL;
620 }
621
622 int isl_local_space_divs_known(__isl_keep isl_local_space *ls)
623 {
624         int i;
625
626         if (!ls)
627                 return -1;
628
629         for (i = 0; i < ls->div->n_row; ++i)
630                 if (isl_int_is_zero(ls->div->row[i][0]))
631                         return 0;
632
633         return 1;
634 }
635
636 __isl_give isl_local_space *isl_local_space_domain(
637         __isl_take isl_local_space *ls)
638 {
639         ls = isl_local_space_drop_dims(ls, isl_dim_out,
640                                         0, isl_local_space_dim(ls, isl_dim_out));
641         ls = isl_local_space_cow(ls);
642         if (!ls)
643                 return NULL;
644         ls->dim = isl_space_domain(ls->dim);
645         if (!ls->dim)
646                 return isl_local_space_free(ls);
647         return ls;
648 }
649
650 __isl_give isl_local_space *isl_local_space_range(
651         __isl_take isl_local_space *ls)
652 {
653         ls = isl_local_space_drop_dims(ls, isl_dim_in,
654                                         0, isl_local_space_dim(ls, isl_dim_in));
655         ls = isl_local_space_cow(ls);
656         if (!ls)
657                 return NULL;
658
659         ls->dim = isl_space_range(ls->dim);
660         if (!ls->dim)
661                 return isl_local_space_free(ls);
662         return ls;
663 }
664
665 /* Construct a local space for a map that has the given local
666  * space as domain and that has a zero-dimensional range.
667  */
668 __isl_give isl_local_space *isl_local_space_from_domain(
669         __isl_take isl_local_space *ls)
670 {
671         ls = isl_local_space_cow(ls);
672         if (!ls)
673                 return NULL;
674         ls->dim = isl_space_from_domain(ls->dim);
675         if (!ls->dim)
676                 return isl_local_space_free(ls);
677         return ls;
678 }
679
680 __isl_give isl_local_space *isl_local_space_add_dims(
681         __isl_take isl_local_space *ls, enum isl_dim_type type, unsigned n)
682 {
683         int pos;
684
685         if (!ls)
686                 return NULL;
687         pos = isl_local_space_dim(ls, type);
688         return isl_local_space_insert_dims(ls, type, pos, n);
689 }
690
691 /* Remove common factor of non-constant terms and denominator.
692  */
693 static void normalize_div(__isl_keep isl_local_space *ls, int div)
694 {
695         isl_ctx *ctx = ls->div->ctx;
696         unsigned total = ls->div->n_col - 2;
697
698         isl_seq_gcd(ls->div->row[div] + 2, total, &ctx->normalize_gcd);
699         isl_int_gcd(ctx->normalize_gcd,
700                     ctx->normalize_gcd, ls->div->row[div][0]);
701         if (isl_int_is_one(ctx->normalize_gcd))
702                 return;
703
704         isl_seq_scale_down(ls->div->row[div] + 2, ls->div->row[div] + 2,
705                             ctx->normalize_gcd, total);
706         isl_int_divexact(ls->div->row[div][0], ls->div->row[div][0],
707                             ctx->normalize_gcd);
708         isl_int_fdiv_q(ls->div->row[div][1], ls->div->row[div][1],
709                             ctx->normalize_gcd);
710 }
711
712 /* Exploit the equalities in "eq" to simplify the expressions of
713  * the integer divisions in "ls".
714  * The integer divisions in "ls" are assumed to appear as regular
715  * dimensions in "eq".
716  */
717 __isl_give isl_local_space *isl_local_space_substitute_equalities(
718         __isl_take isl_local_space *ls, __isl_take isl_basic_set *eq)
719 {
720         int i, j, k;
721         unsigned total;
722         unsigned n_div;
723
724         ls = isl_local_space_cow(ls);
725         if (!ls || !eq)
726                 goto error;
727
728         total = isl_space_dim(eq->dim, isl_dim_all);
729         if (isl_local_space_dim(ls, isl_dim_all) != total)
730                 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
731                         "dimensions don't match", goto error);
732         total++;
733         n_div = eq->n_div;
734         for (i = 0; i < eq->n_eq; ++i) {
735                 j = isl_seq_last_non_zero(eq->eq[i], total + n_div);
736                 if (j < 0 || j == 0 || j >= total)
737                         continue;
738
739                 for (k = 0; k < ls->div->n_row; ++k) {
740                         if (isl_int_is_zero(ls->div->row[k][1 + j]))
741                                 continue;
742                         isl_seq_elim(ls->div->row[k] + 1, eq->eq[i], j, total,
743                                         &ls->div->row[k][0]);
744                         normalize_div(ls, k);
745                 }
746         }
747
748         isl_basic_set_free(eq);
749         return ls;
750 error:
751         isl_basic_set_free(eq);
752         isl_local_space_free(ls);
753         return NULL;
754 }
755
756 /* Plug in the affine expressions "subs" of length "subs_len" (including
757  * the denominator and the constant term) into the variable at position "pos"
758  * of the "n" div expressions starting at "first".
759  *
760  * Let i be the dimension to replace and let "subs" be of the form
761  *
762  *      f/d
763  *
764  * Any integer division starting at "first" with a non-zero coefficient for i,
765  *
766  *      floor((a i + g)/m)
767  *
768  * is replaced by
769  *
770  *      floor((a f + d g)/(m d))
771  */
772 __isl_give isl_local_space *isl_local_space_substitute_seq(
773         __isl_take isl_local_space *ls,
774         enum isl_dim_type type, unsigned pos, isl_int *subs, int subs_len,
775         int first, int n)
776 {
777         int i;
778         isl_int v;
779
780         if (n == 0)
781                 return ls;
782         ls = isl_local_space_cow(ls);
783         if (!ls)
784                 return NULL;
785         ls->div = isl_mat_cow(ls->div);
786         if (!ls->div)
787                 return isl_local_space_free(ls);
788
789         if (first + n > ls->div->n_row)
790                 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
791                         "index out of bounds", return isl_local_space_free(ls));
792
793         pos += isl_local_space_offset(ls, type);
794
795         isl_int_init(v);
796         for (i = first; i < ls->div->n_row; ++i) {
797                 if (isl_int_is_zero(ls->div->row[i][1 + pos]))
798                         continue;
799                 isl_seq_substitute(ls->div->row[i], pos, subs,
800                         ls->div->n_col, subs_len, v);
801                 normalize_div(ls, i);
802         }
803         isl_int_clear(v);
804
805         return ls;
806 }
807
808 /* Plug in "subs" for dimension "type", "pos" in the integer divisions
809  * of "ls".
810  *
811  * Let i be the dimension to replace and let "subs" be of the form
812  *
813  *      f/d
814  *
815  * Any integer division with a non-zero coefficient for i,
816  *
817  *      floor((a i + g)/m)
818  *
819  * is replaced by
820  *
821  *      floor((a f + d g)/(m d))
822  */
823 __isl_give isl_local_space *isl_local_space_substitute(
824         __isl_take isl_local_space *ls,
825         enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
826 {
827         ls = isl_local_space_cow(ls);
828         if (!ls || !subs)
829                 return isl_local_space_free(ls);
830
831         if (!isl_space_is_equal(ls->dim, subs->ls->dim))
832                 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
833                         "spaces don't match", return isl_local_space_free(ls));
834         if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
835                 isl_die(isl_local_space_get_ctx(ls), isl_error_unsupported,
836                         "cannot handle divs yet",
837                         return isl_local_space_free(ls));
838
839         return isl_local_space_substitute_seq(ls, type, pos, subs->v->el,
840                                             subs->v->size, 0, ls->div->n_row);
841 }
842
843 int isl_local_space_is_named_or_nested(__isl_keep isl_local_space *ls,
844         enum isl_dim_type type)
845 {
846         if (!ls)
847                 return -1;
848         return isl_space_is_named_or_nested(ls->dim, type);
849 }
850
851 __isl_give isl_local_space *isl_local_space_drop_dims(
852         __isl_take isl_local_space *ls,
853         enum isl_dim_type type, unsigned first, unsigned n)
854 {
855         isl_ctx *ctx;
856
857         if (!ls)
858                 return NULL;
859         if (n == 0 && !isl_local_space_is_named_or_nested(ls, type))
860                 return ls;
861
862         ctx = isl_local_space_get_ctx(ls);
863         if (first + n > isl_local_space_dim(ls, type))
864                 isl_die(ctx, isl_error_invalid, "range out of bounds",
865                         return isl_local_space_free(ls));
866
867         ls = isl_local_space_cow(ls);
868         if (!ls)
869                 return NULL;
870
871         if (type == isl_dim_div) {
872                 ls->div = isl_mat_drop_rows(ls->div, first, n);
873         } else {
874                 ls->dim = isl_space_drop_dims(ls->dim, type, first, n);
875                 if (!ls->dim)
876                         return isl_local_space_free(ls);
877         }
878
879         first += 1 + isl_local_space_offset(ls, type);
880         ls->div = isl_mat_drop_cols(ls->div, first, n);
881         if (!ls->div)
882                 return isl_local_space_free(ls);
883
884         return ls;
885 }
886
887 __isl_give isl_local_space *isl_local_space_insert_dims(
888         __isl_take isl_local_space *ls,
889         enum isl_dim_type type, unsigned first, unsigned n)
890 {
891         isl_ctx *ctx;
892
893         if (!ls)
894                 return NULL;
895         if (n == 0 && !isl_local_space_is_named_or_nested(ls, type))
896                 return ls;
897
898         ctx = isl_local_space_get_ctx(ls);
899         if (first > isl_local_space_dim(ls, type))
900                 isl_die(ctx, isl_error_invalid, "position out of bounds",
901                         return isl_local_space_free(ls));
902
903         ls = isl_local_space_cow(ls);
904         if (!ls)
905                 return NULL;
906
907         if (type == isl_dim_div) {
908                 ls->div = isl_mat_insert_zero_rows(ls->div, first, n);
909         } else {
910                 ls->dim = isl_space_insert_dims(ls->dim, type, first, n);
911                 if (!ls->dim)
912                         return isl_local_space_free(ls);
913         }
914
915         first += 1 + isl_local_space_offset(ls, type);
916         ls->div = isl_mat_insert_zero_cols(ls->div, first, n);
917         if (!ls->div)
918                 return isl_local_space_free(ls);
919
920         return ls;
921 }
922
923 /* Check if the constraints pointed to by "constraint" is a div
924  * constraint corresponding to div "div" in "ls".
925  *
926  * That is, if div = floor(f/m), then check if the constraint is
927  *
928  *              f - m d >= 0
929  * or
930  *              -(f-(m-1)) + m d >= 0
931  */
932 int isl_local_space_is_div_constraint(__isl_keep isl_local_space *ls,
933         isl_int *constraint, unsigned div)
934 {
935         unsigned pos;
936
937         if (!ls)
938                 return -1;
939
940         if (isl_int_is_zero(ls->div->row[div][0]))
941                 return 0;
942
943         pos = isl_local_space_offset(ls, isl_dim_div) + div;
944
945         if (isl_int_eq(constraint[pos], ls->div->row[div][0])) {
946                 int neg;
947                 isl_int_sub(ls->div->row[div][1],
948                                 ls->div->row[div][1], ls->div->row[div][0]);
949                 isl_int_add_ui(ls->div->row[div][1], ls->div->row[div][1], 1);
950                 neg = isl_seq_is_neg(constraint, ls->div->row[div]+1, pos);
951                 isl_int_sub_ui(ls->div->row[div][1], ls->div->row[div][1], 1);
952                 isl_int_add(ls->div->row[div][1],
953                                 ls->div->row[div][1], ls->div->row[div][0]);
954                 if (!neg)
955                         return 0;
956                 if (isl_seq_first_non_zero(constraint+pos+1,
957                                             ls->div->n_row-div-1) != -1)
958                         return 0;
959         } else if (isl_int_abs_eq(constraint[pos], ls->div->row[div][0])) {
960                 if (!isl_seq_eq(constraint, ls->div->row[div]+1, pos))
961                         return 0;
962                 if (isl_seq_first_non_zero(constraint+pos+1,
963                                             ls->div->n_row-div-1) != -1)
964                         return 0;
965         } else
966                 return 0;
967
968         return 1;
969 }
970
971 /*
972  * Set active[i] to 1 if the dimension at position i is involved
973  * in the linear expression l.
974  */
975 int *isl_local_space_get_active(__isl_keep isl_local_space *ls, isl_int *l)
976 {
977         int i, j;
978         isl_ctx *ctx;
979         int *active = NULL;
980         unsigned total;
981         unsigned offset;
982
983         ctx = isl_local_space_get_ctx(ls);
984         total = isl_local_space_dim(ls, isl_dim_all);
985         active = isl_calloc_array(ctx, int, total);
986         if (!active)
987                 return NULL;
988
989         for (i = 0; i < total; ++i)
990                 active[i] = !isl_int_is_zero(l[i]);
991
992         offset = isl_local_space_offset(ls, isl_dim_div) - 1;
993         for (i = ls->div->n_row - 1; i >= 0; --i) {
994                 if (!active[offset + i])
995                         continue;
996                 for (j = 0; j < total; ++j)
997                         active[j] |= !isl_int_is_zero(ls->div->row[i][2 + j]);
998         }
999
1000         return active;
1001 }
1002
1003 /* Given a local space "ls" of a set, create a local space
1004  * for the lift of the set.  In particular, the result
1005  * is of the form [dim -> local[..]], with ls->div->n_row variables in the
1006  * range of the wrapped map.
1007  */
1008 __isl_give isl_local_space *isl_local_space_lift(
1009         __isl_take isl_local_space *ls)
1010 {
1011         ls = isl_local_space_cow(ls);
1012         if (!ls)
1013                 return NULL;
1014
1015         ls->dim = isl_space_lift(ls->dim, ls->div->n_row);
1016         ls->div = isl_mat_drop_rows(ls->div, 0, ls->div->n_row);
1017         if (!ls->dim || !ls->div)
1018                 return isl_local_space_free(ls);
1019
1020         return ls;
1021 }
1022
1023 /* Construct a basic map that maps a set living in local space "ls"
1024  * to the corresponding lifted local space.
1025  */
1026 __isl_give isl_basic_map *isl_local_space_lifting(
1027         __isl_take isl_local_space *ls)
1028 {
1029         isl_basic_map *lifting;
1030         isl_basic_set *bset;
1031
1032         if (!ls)
1033                 return NULL;
1034         if (!isl_local_space_is_set(ls))
1035                 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
1036                         "lifting only defined on set spaces",
1037                         return isl_local_space_free(ls));
1038
1039         bset = isl_basic_set_from_local_space(ls);
1040         lifting = isl_basic_set_unwrap(isl_basic_set_lift(bset));
1041         lifting = isl_basic_map_domain_map(lifting);
1042         lifting = isl_basic_map_reverse(lifting);
1043
1044         return lifting;
1045 }
1046
1047 /* Compute the preimage of "ls" under the function represented by "ma".
1048  * In other words, plug in "ma" in "ls".  The result is a local space
1049  * that is part of the domain space of "ma".
1050  *
1051  * If the divs in "ls" are represented as
1052  *
1053  *      floor((a_i(p) + b_i x + c_i(divs))/n_i)
1054  *
1055  * and ma is represented by
1056  *
1057  *      x = D(p) + F(y) + G(divs')
1058  *
1059  * then the resulting divs are
1060  *
1061  *      floor((a_i(p) + b_i D(p) + b_i F(y) + B_i G(divs') + c_i(divs))/n_i)
1062  *
1063  * We first copy over the divs from "ma" and then
1064  * we add the modified divs from "ls".
1065  */
1066 __isl_give isl_local_space *isl_local_space_preimage_multi_aff(
1067         __isl_take isl_local_space *ls, __isl_take isl_multi_aff *ma)
1068 {
1069         int i;
1070         isl_space *space;
1071         isl_local_space *res = NULL;
1072         int n_div_ls, n_div_ma;
1073         isl_int f, c1, c2, g;
1074
1075         ma = isl_multi_aff_align_divs(ma);
1076         if (!ls || !ma)
1077                 goto error;
1078         if (!isl_space_is_range_internal(ls->dim, ma->space))
1079                 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
1080                         "spaces don't match", goto error);
1081
1082         n_div_ls = isl_local_space_dim(ls, isl_dim_div);
1083         n_div_ma = ma->n ? isl_aff_dim(ma->p[0], isl_dim_div) : 0;
1084
1085         space = isl_space_domain(isl_multi_aff_get_space(ma));
1086         res = isl_local_space_alloc(space, n_div_ma + n_div_ls);
1087         if (!res)
1088                 goto error;
1089
1090         if (n_div_ma) {
1091                 isl_mat_free(res->div);
1092                 res->div = isl_mat_copy(ma->p[0]->ls->div);
1093                 res->div = isl_mat_add_zero_cols(res->div, n_div_ls);
1094                 res->div = isl_mat_add_rows(res->div, n_div_ls);
1095                 if (!res->div)
1096                         goto error;
1097         }
1098
1099         isl_int_init(f);
1100         isl_int_init(c1);
1101         isl_int_init(c2);
1102         isl_int_init(g);
1103
1104         for (i = 0; i < ls->div->n_row; ++i) {
1105                 if (isl_int_is_zero(ls->div->row[i][0])) {
1106                         isl_int_set_si(res->div->row[n_div_ma + i][0], 0);
1107                         continue;
1108                 }
1109                 isl_seq_preimage(res->div->row[n_div_ma + i], ls->div->row[i],
1110                                 ma, 0, 0, n_div_ma, n_div_ls, f, c1, c2, g, 1);
1111                 normalize_div(res, n_div_ma + i);
1112         }
1113
1114         isl_int_clear(f);
1115         isl_int_clear(c1);
1116         isl_int_clear(c2);
1117         isl_int_clear(g);
1118
1119         isl_local_space_free(ls);
1120         isl_multi_aff_free(ma);
1121         return res;
1122 error:
1123         isl_local_space_free(ls);
1124         isl_multi_aff_free(ma);
1125         isl_local_space_free(res);
1126         return NULL;
1127 }