add isl_set_coalesce test case
[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_aff_private.h>
17 #include <isl/seq.h>
18
19 isl_ctx *isl_local_space_get_ctx(__isl_keep isl_local_space *ls)
20 {
21         return ls ? ls->dim->ctx : NULL;
22 }
23
24 __isl_give isl_local_space *isl_local_space_alloc_div(__isl_take isl_space *dim,
25         __isl_take isl_mat *div)
26 {
27         isl_ctx *ctx;
28         isl_local_space *ls = NULL;
29
30         if (!dim || !div)
31                 goto error;
32
33         ctx = isl_space_get_ctx(dim);
34         ls = isl_calloc_type(ctx, struct isl_local_space);
35         if (!ls)
36                 goto error;
37
38         ls->ref = 1;
39         ls->dim = dim;
40         ls->div = div;
41
42         return ls;
43 error:
44         isl_mat_free(div);
45         isl_space_free(dim);
46         isl_local_space_free(ls);
47         return NULL;
48 }
49
50 __isl_give isl_local_space *isl_local_space_alloc(__isl_take isl_space *dim,
51         unsigned n_div)
52 {
53         isl_ctx *ctx;
54         isl_mat *div;
55         unsigned total;
56
57         if (!dim)
58                 return NULL;
59
60         total = isl_space_dim(dim, isl_dim_all);
61
62         ctx = isl_space_get_ctx(dim);
63         div = isl_mat_alloc(ctx, n_div, 1 + 1 + total + n_div);
64         return isl_local_space_alloc_div(dim, div);
65 }
66
67 __isl_give isl_local_space *isl_local_space_from_space(__isl_take isl_space *dim)
68 {
69         return isl_local_space_alloc(dim, 0);
70 }
71
72 __isl_give isl_local_space *isl_local_space_copy(__isl_keep isl_local_space *ls)
73 {
74         if (!ls)
75                 return NULL;
76
77         ls->ref++;
78         return ls;
79 }
80
81 __isl_give isl_local_space *isl_local_space_dup(__isl_keep isl_local_space *ls)
82 {
83         if (!ls)
84                 return NULL;
85
86         return isl_local_space_alloc_div(isl_space_copy(ls->dim),
87                                          isl_mat_copy(ls->div));
88
89 }
90
91 __isl_give isl_local_space *isl_local_space_cow(__isl_take isl_local_space *ls)
92 {
93         if (!ls)
94                 return NULL;
95
96         if (ls->ref == 1)
97                 return ls;
98         ls->ref--;
99         return isl_local_space_dup(ls);
100 }
101
102 void *isl_local_space_free(__isl_take isl_local_space *ls)
103 {
104         if (!ls)
105                 return NULL;
106
107         if (--ls->ref > 0)
108                 return NULL;
109
110         isl_space_free(ls->dim);
111         isl_mat_free(ls->div);
112
113         free(ls);
114
115         return NULL;
116 }
117
118 /* Is the local space that of a set?
119  */
120 int isl_local_space_is_set(__isl_keep isl_local_space *ls)
121 {
122         return ls ? isl_space_is_set(ls->dim) : -1;
123 }
124
125 /* Return true if the two local spaces are identical, with identical
126  * expressions for the integer divisions.
127  */
128 int isl_local_space_is_equal(__isl_keep isl_local_space *ls1,
129         __isl_keep isl_local_space *ls2)
130 {
131         int equal;
132
133         if (!ls1 || !ls2)
134                 return -1;
135
136         equal = isl_space_is_equal(ls1->dim, ls2->dim);
137         if (equal < 0 || !equal)
138                 return equal;
139
140         if (!isl_local_space_divs_known(ls1))
141                 return 0;
142         if (!isl_local_space_divs_known(ls2))
143                 return 0;
144
145         return isl_mat_is_equal(ls1->div, ls2->div);
146 }
147
148 int isl_local_space_dim(__isl_keep isl_local_space *ls,
149         enum isl_dim_type type)
150 {
151         if (!ls)
152                 return 0;
153         if (type == isl_dim_div)
154                 return ls->div->n_row;
155         if (type == isl_dim_all)
156                 return isl_space_dim(ls->dim, isl_dim_all) + ls->div->n_row;
157         return isl_space_dim(ls->dim, type);
158 }
159
160 unsigned isl_local_space_offset(__isl_keep isl_local_space *ls,
161         enum isl_dim_type type)
162 {
163         isl_space *dim;
164
165         if (!ls)
166                 return 0;
167
168         dim = ls->dim;
169         switch (type) {
170         case isl_dim_cst:       return 0;
171         case isl_dim_param:     return 1;
172         case isl_dim_in:        return 1 + dim->nparam;
173         case isl_dim_out:       return 1 + dim->nparam + dim->n_in;
174         case isl_dim_div:       return 1 + dim->nparam + dim->n_in + dim->n_out;
175         default:                return 0;
176         }
177 }
178
179 const char *isl_local_space_get_dim_name(__isl_keep isl_local_space *ls,
180         enum isl_dim_type type, unsigned pos)
181 {
182         return ls ? isl_space_get_dim_name(ls->dim, type, pos) : NULL;
183 }
184
185 __isl_give isl_aff *isl_local_space_get_div(__isl_keep isl_local_space *ls,
186         int pos)
187 {
188         isl_aff *aff;
189
190         if (!ls)
191                 return NULL;
192
193         if (pos < 0 || pos >= ls->div->n_row)
194                 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
195                         "index out of bounds", return NULL);
196
197         if (isl_int_is_zero(ls->div->row[pos][0]))
198                 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
199                         "expression of div unknown", return NULL);
200
201         aff = isl_aff_alloc(isl_local_space_copy(ls));
202         if (!aff)
203                 return NULL;
204         isl_seq_cpy(aff->v->el, ls->div->row[pos], aff->v->size);
205         return aff;
206 }
207
208 __isl_give isl_space *isl_local_space_get_space(__isl_keep isl_local_space *ls)
209 {
210         if (!ls)
211                 return NULL;
212
213         return isl_space_copy(ls->dim);
214 }
215
216 __isl_give isl_local_space *isl_local_space_set_dim_name(
217         __isl_take isl_local_space *ls,
218         enum isl_dim_type type, unsigned pos, const char *s)
219 {
220         ls = isl_local_space_cow(ls);
221         if (!ls)
222                 return NULL;
223         ls->dim = isl_space_set_dim_name(ls->dim, type, pos, s);
224         if (!ls->dim)
225                 return isl_local_space_free(ls);
226
227         return ls;
228 }
229
230 __isl_give isl_local_space *isl_local_space_set_dim_id(
231         __isl_take isl_local_space *ls,
232         enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
233 {
234         ls = isl_local_space_cow(ls);
235         if (!ls)
236                 return isl_id_free(id);
237         ls->dim = isl_space_set_dim_id(ls->dim, type, pos, id);
238         if (!ls->dim)
239                 return isl_local_space_free(ls);
240
241         return ls;
242 }
243
244 __isl_give isl_local_space *isl_local_space_reset_space(
245         __isl_take isl_local_space *ls, __isl_take isl_space *dim)
246 {
247         ls = isl_local_space_cow(ls);
248         if (!ls || !dim)
249                 goto error;
250
251         isl_space_free(ls->dim);
252         ls->dim = dim;
253
254         return ls;
255 error:
256         isl_local_space_free(ls);
257         isl_space_free(dim);
258         return NULL;
259 }
260
261 /* Reorder the columns of the given div definitions according to the
262  * given reordering.
263  * The order of the divs themselves is assumed not to change.
264  */
265 static __isl_give isl_mat *reorder_divs(__isl_take isl_mat *div,
266         __isl_take isl_reordering *r)
267 {
268         int i, j;
269         isl_mat *mat;
270         int extra;
271
272         if (!div || !r)
273                 goto error;
274
275         extra = isl_space_dim(r->dim, isl_dim_all) + div->n_row - r->len;
276         mat = isl_mat_alloc(div->ctx, div->n_row, div->n_col + extra);
277         if (!mat)
278                 goto error;
279
280         for (i = 0; i < div->n_row; ++i) {
281                 isl_seq_cpy(mat->row[i], div->row[i], 2);
282                 isl_seq_clr(mat->row[i] + 2, mat->n_col - 2);
283                 for (j = 0; j < r->len; ++j)
284                         isl_int_set(mat->row[i][2 + r->pos[j]],
285                                     div->row[i][2 + j]);
286         }
287
288         isl_reordering_free(r);
289         isl_mat_free(div);
290         return mat;
291 error:
292         isl_reordering_free(r);
293         isl_mat_free(div);
294         return NULL;
295 }
296
297 /* Reorder the dimensions of "ls" according to the given reordering.
298  * The reordering r is assumed to have been extended with the local
299  * variables, leaving them in the same order.
300  */
301 __isl_give isl_local_space *isl_local_space_realign(
302         __isl_take isl_local_space *ls, __isl_take isl_reordering *r)
303 {
304         ls = isl_local_space_cow(ls);
305         if (!ls || !r)
306                 goto error;
307
308         ls->div = reorder_divs(ls->div, isl_reordering_copy(r));
309         if (!ls->div)
310                 goto error;
311
312         ls = isl_local_space_reset_space(ls, isl_space_copy(r->dim));
313
314         isl_reordering_free(r);
315         return ls;
316 error:
317         isl_local_space_free(ls);
318         isl_reordering_free(r);
319         return NULL;
320 }
321
322 __isl_give isl_local_space *isl_local_space_add_div(
323         __isl_take isl_local_space *ls, __isl_take isl_vec *div)
324 {
325         ls = isl_local_space_cow(ls);
326         if (!ls || !div)
327                 goto error;
328
329         if (ls->div->n_col != div->size)
330                 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
331                         "incompatible dimensions", goto error);
332
333         ls->div = isl_mat_add_zero_cols(ls->div, 1);
334         ls->div = isl_mat_add_rows(ls->div, 1);
335         if (!ls->div)
336                 goto error;
337
338         isl_seq_cpy(ls->div->row[ls->div->n_row - 1], div->el, div->size);
339         isl_int_set_si(ls->div->row[ls->div->n_row - 1][div->size], 0);
340
341         isl_vec_free(div);
342         return ls;
343 error:
344         isl_local_space_free(ls);
345         isl_vec_free(div);
346         return NULL;
347 }
348
349 __isl_give isl_local_space *isl_local_space_replace_divs(
350         __isl_take isl_local_space *ls, __isl_take isl_mat *div)
351 {
352         ls = isl_local_space_cow(ls);
353
354         if (!ls || !div)
355                 goto error;
356
357         isl_mat_free(ls->div);
358         ls->div = div;
359         return ls;
360 error:
361         isl_mat_free(div);
362         isl_local_space_free(ls);
363         return NULL;
364 }
365
366 /* Copy row "s" of "src" to row "d" of "dst", applying the expansion
367  * defined by "exp".
368  */
369 static void expand_row(__isl_keep isl_mat *dst, int d,
370         __isl_keep isl_mat *src, int s, int *exp)
371 {
372         int i;
373         unsigned c = src->n_col - src->n_row;
374
375         isl_seq_cpy(dst->row[d], src->row[s], c);
376         isl_seq_clr(dst->row[d] + c, dst->n_col - c);
377
378         for (i = 0; i < s; ++i)
379                 isl_int_set(dst->row[d][c + exp[i]], src->row[s][c + i]);
380 }
381
382 /* Compare (known) divs.
383  * Return non-zero if at least one of the two divs is unknown.
384  */
385 static int cmp_row(__isl_keep isl_mat *div, int i, int j)
386 {
387         int li, lj;
388
389         if (isl_int_is_zero(div->row[j][0]))
390                 return -1;
391         if (isl_int_is_zero(div->row[i][0]))
392                 return 1;
393
394         li = isl_seq_last_non_zero(div->row[i], div->n_col);
395         lj = isl_seq_last_non_zero(div->row[j], div->n_col);
396
397         if (li != lj)
398                 return li - lj;
399
400         return isl_seq_cmp(div->row[i], div->row[j], div->n_col);
401 }
402
403 /* Combine the two lists of divs into a single list.
404  * For each row i in div1, exp1[i] is set to the position of the corresponding
405  * row in the result.  Similarly for div2 and exp2.
406  * This function guarantees
407  *      exp1[i] >= i
408  *      exp1[i+1] > exp1[i]
409  * For optimal merging, the two input list should have been sorted.
410  */
411 __isl_give isl_mat *isl_merge_divs(__isl_keep isl_mat *div1,
412         __isl_keep isl_mat *div2, int *exp1, int *exp2)
413 {
414         int i, j, k;
415         isl_mat *div = NULL;
416         unsigned d;
417
418         if (!div1 || !div2)
419                 return NULL;
420
421         d = div1->n_col - div1->n_row;
422         div = isl_mat_alloc(div1->ctx, 1 + div1->n_row + div2->n_row,
423                                 d + div1->n_row + div2->n_row);
424         if (!div)
425                 return NULL;
426
427         for (i = 0, j = 0, k = 0; i < div1->n_row && j < div2->n_row; ++k) {
428                 int cmp;
429
430                 expand_row(div, k, div1, i, exp1);
431                 expand_row(div, k + 1, div2, j, exp2);
432
433                 cmp = cmp_row(div, k, k + 1);
434                 if (cmp == 0) {
435                         exp1[i++] = k;
436                         exp2[j++] = k;
437                 } else if (cmp < 0) {
438                         exp1[i++] = k;
439                 } else {
440                         exp2[j++] = k;
441                         isl_seq_cpy(div->row[k], div->row[k + 1], div->n_col);
442                 }
443         }
444         for (; i < div1->n_row; ++i, ++k) {
445                 expand_row(div, k, div1, i, exp1);
446                 exp1[i] = k;
447         }
448         for (; j < div2->n_row; ++j, ++k) {
449                 expand_row(div, k, div2, j, exp2);
450                 exp2[j] = k;
451         }
452
453         div->n_row = k;
454         div->n_col = d + k;
455
456         return div;
457 }
458
459 /* Construct a local space that contains all the divs in either
460  * "ls1" or "ls2".
461  */
462 __isl_give isl_local_space *isl_local_space_intersect(
463         __isl_take isl_local_space *ls1, __isl_take isl_local_space *ls2)
464 {
465         isl_ctx *ctx;
466         int *exp1 = NULL;
467         int *exp2 = NULL;
468         isl_mat *div;
469
470         if (!ls1 || !ls2)
471                 goto error;
472
473         ctx = isl_local_space_get_ctx(ls1);
474         if (!isl_space_is_equal(ls1->dim, ls2->dim))
475                 isl_die(ctx, isl_error_invalid,
476                         "spaces should be identical", goto error);
477
478         if (ls2->div->n_row == 0) {
479                 isl_local_space_free(ls2);
480                 return ls1;
481         }
482
483         if (ls1->div->n_row == 0) {
484                 isl_local_space_free(ls1);
485                 return ls2;
486         }
487
488         exp1 = isl_alloc_array(ctx, int, ls1->div->n_row);
489         exp2 = isl_alloc_array(ctx, int, ls2->div->n_row);
490         if (!exp1 || !exp2)
491                 goto error;
492
493         div = isl_merge_divs(ls1->div, ls2->div, exp1, exp2);
494         if (!div)
495                 goto error;
496
497         free(exp1);
498         free(exp2);
499         isl_local_space_free(ls2);
500         isl_mat_free(ls1->div);
501         ls1->div = div;
502
503         return ls1;
504 error:
505         free(exp1);
506         free(exp2);
507         isl_local_space_free(ls1);
508         isl_local_space_free(ls2);
509         return NULL;
510 }
511
512 int isl_local_space_divs_known(__isl_keep isl_local_space *ls)
513 {
514         int i;
515
516         if (!ls)
517                 return -1;
518
519         for (i = 0; i < ls->div->n_row; ++i)
520                 if (isl_int_is_zero(ls->div->row[i][0]))
521                         return 0;
522
523         return 1;
524 }
525
526 __isl_give isl_local_space *isl_local_space_domain(
527         __isl_take isl_local_space *ls)
528 {
529         ls = isl_local_space_drop_dims(ls, isl_dim_out,
530                                         0, isl_local_space_dim(ls, isl_dim_out));
531         ls = isl_local_space_cow(ls);
532         if (!ls)
533                 return NULL;
534         ls->dim = isl_space_domain(ls->dim);
535         if (!ls->dim)
536                 return isl_local_space_free(ls);
537         return ls;
538 }
539
540 __isl_give isl_local_space *isl_local_space_range(
541         __isl_take isl_local_space *ls)
542 {
543         ls = isl_local_space_drop_dims(ls, isl_dim_in,
544                                         0, isl_local_space_dim(ls, isl_dim_in));
545         ls = isl_local_space_cow(ls);
546         if (!ls)
547                 return NULL;
548
549         ls->dim = isl_space_range(ls->dim);
550         if (!ls->dim)
551                 return isl_local_space_free(ls);
552         return ls;
553 }
554
555 /* Construct a local space for a map that has the given local
556  * space as domain and that has a zero-dimensional range.
557  */
558 __isl_give isl_local_space *isl_local_space_from_domain(
559         __isl_take isl_local_space *ls)
560 {
561         ls = isl_local_space_cow(ls);
562         if (!ls)
563                 return NULL;
564         ls->dim = isl_space_from_domain(ls->dim);
565         if (!ls->dim)
566                 return isl_local_space_free(ls);
567         return ls;
568 }
569
570 __isl_give isl_local_space *isl_local_space_add_dims(
571         __isl_take isl_local_space *ls, enum isl_dim_type type, unsigned n)
572 {
573         int pos;
574
575         if (!ls)
576                 return NULL;
577         pos = isl_local_space_dim(ls, type);
578         return isl_local_space_insert_dims(ls, type, pos, n);
579 }
580
581 /* Remove common factor of non-constant terms and denominator.
582  */
583 static void normalize_div(__isl_keep isl_local_space *ls, int div)
584 {
585         isl_ctx *ctx = ls->div->ctx;
586         unsigned total = ls->div->n_col - 2;
587
588         isl_seq_gcd(ls->div->row[div] + 2, total, &ctx->normalize_gcd);
589         isl_int_gcd(ctx->normalize_gcd,
590                     ctx->normalize_gcd, ls->div->row[div][0]);
591         if (isl_int_is_one(ctx->normalize_gcd))
592                 return;
593
594         isl_seq_scale_down(ls->div->row[div] + 2, ls->div->row[div] + 2,
595                             ctx->normalize_gcd, total);
596         isl_int_divexact(ls->div->row[div][0], ls->div->row[div][0],
597                             ctx->normalize_gcd);
598         isl_int_fdiv_q(ls->div->row[div][1], ls->div->row[div][1],
599                             ctx->normalize_gcd);
600 }
601
602 /* Exploit the equalities in "eq" to simplify the expressions of
603  * the integer divisions in "ls".
604  * The integer divisions in "ls" are assumed to appear as regular
605  * dimensions in "eq".
606  */
607 __isl_give isl_local_space *isl_local_space_substitute_equalities(
608         __isl_take isl_local_space *ls, __isl_take isl_basic_set *eq)
609 {
610         int i, j, k;
611         unsigned total;
612         unsigned n_div;
613
614         ls = isl_local_space_cow(ls);
615         if (!ls || !eq)
616                 goto error;
617
618         total = isl_space_dim(eq->dim, isl_dim_all);
619         if (isl_local_space_dim(ls, isl_dim_all) != total)
620                 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
621                         "dimensions don't match", goto error);
622         total++;
623         n_div = eq->n_div;
624         for (i = 0; i < eq->n_eq; ++i) {
625                 j = isl_seq_last_non_zero(eq->eq[i], total + n_div);
626                 if (j < 0 || j == 0 || j >= total)
627                         continue;
628
629                 for (k = 0; k < ls->div->n_row; ++k) {
630                         if (isl_int_is_zero(ls->div->row[k][1 + j]))
631                                 continue;
632                         isl_seq_elim(ls->div->row[k] + 1, eq->eq[i], j, total,
633                                         &ls->div->row[k][0]);
634                         normalize_div(ls, k);
635                 }
636         }
637
638         isl_basic_set_free(eq);
639         return ls;
640 error:
641         isl_basic_set_free(eq);
642         isl_local_space_free(ls);
643         return NULL;
644 }
645
646 /* Plug in "subs" for dimension "type", "pos" in the integer divisions
647  * of "ls".
648  *
649  * Let i be the dimension to replace and let "subs" be of the form
650  *
651  *      f/d
652  *
653  * Any integer division with a non-zero coefficient for i,
654  *
655  *      floor((a i + g)/m)
656  *
657  * is replaced by
658  *
659  *      floor((a f + d g)/(m d))
660  */
661 __isl_give isl_local_space *isl_local_space_substitute(
662         __isl_take isl_local_space *ls,
663         enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
664 {
665         int i;
666         isl_int v;
667
668         ls = isl_local_space_cow(ls);
669         if (!ls || !subs)
670                 return isl_local_space_free(ls);
671
672         if (!isl_space_is_equal(ls->dim, subs->ls->dim))
673                 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
674                         "spaces don't match", return isl_local_space_free(ls));
675         if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
676                 isl_die(isl_local_space_get_ctx(ls), isl_error_unsupported,
677                         "cannot handle divs yet",
678                         return isl_local_space_free(ls));
679
680         pos += isl_local_space_offset(ls, type);
681
682         isl_int_init(v);
683         for (i = 0; i < ls->div->n_row; ++i) {
684                 if (isl_int_is_zero(ls->div->row[i][1 + pos]))
685                         continue;
686                 isl_int_set(v, ls->div->row[i][1 + pos]);
687                 isl_int_set_si(ls->div->row[i][1 + pos], 0);
688                 isl_seq_combine(ls->div->row[i] + 1,
689                                 subs->v->el[0], ls->div->row[i] + 1,
690                                 v, subs->v->el + 1, subs->v->size - 1);
691                 isl_int_mul(ls->div->row[i][0],
692                             ls->div->row[i][0], subs->v->el[0]);
693                 normalize_div(ls, i);
694         }
695         isl_int_clear(v);
696
697         return ls;
698 }
699
700 int isl_local_space_is_named_or_nested(__isl_keep isl_local_space *ls,
701         enum isl_dim_type type)
702 {
703         if (!ls)
704                 return -1;
705         return isl_space_is_named_or_nested(ls->dim, type);
706 }
707
708 __isl_give isl_local_space *isl_local_space_drop_dims(
709         __isl_take isl_local_space *ls,
710         enum isl_dim_type type, unsigned first, unsigned n)
711 {
712         isl_ctx *ctx;
713
714         if (!ls)
715                 return NULL;
716         if (n == 0 && !isl_local_space_is_named_or_nested(ls, type))
717                 return ls;
718
719         ctx = isl_local_space_get_ctx(ls);
720         if (first + n > isl_local_space_dim(ls, type))
721                 isl_die(ctx, isl_error_invalid, "range out of bounds",
722                         return isl_local_space_free(ls));
723
724         ls = isl_local_space_cow(ls);
725         if (!ls)
726                 return NULL;
727
728         if (type == isl_dim_div) {
729                 ls->div = isl_mat_drop_rows(ls->div, first, n);
730         } else {
731                 ls->dim = isl_space_drop_dims(ls->dim, type, first, n);
732                 if (!ls->dim)
733                         return isl_local_space_free(ls);
734         }
735
736         first += 1 + isl_local_space_offset(ls, type);
737         ls->div = isl_mat_drop_cols(ls->div, first, n);
738         if (!ls->div)
739                 return isl_local_space_free(ls);
740
741         return ls;
742 }
743
744 __isl_give isl_local_space *isl_local_space_insert_dims(
745         __isl_take isl_local_space *ls,
746         enum isl_dim_type type, unsigned first, unsigned n)
747 {
748         isl_ctx *ctx;
749
750         if (!ls)
751                 return NULL;
752         if (n == 0 && !isl_local_space_is_named_or_nested(ls, type))
753                 return ls;
754
755         ctx = isl_local_space_get_ctx(ls);
756         if (first > isl_local_space_dim(ls, type))
757                 isl_die(ctx, isl_error_invalid, "position out of bounds",
758                         return isl_local_space_free(ls));
759
760         ls = isl_local_space_cow(ls);
761         if (!ls)
762                 return NULL;
763
764         if (type == isl_dim_div) {
765                 ls->div = isl_mat_insert_zero_rows(ls->div, first, n);
766         } else {
767                 ls->dim = isl_space_insert_dims(ls->dim, type, first, n);
768                 if (!ls->dim)
769                         return isl_local_space_free(ls);
770         }
771
772         first += 1 + isl_local_space_offset(ls, type);
773         ls->div = isl_mat_insert_zero_cols(ls->div, first, n);
774         if (!ls->div)
775                 return isl_local_space_free(ls);
776
777         return ls;
778 }
779
780 /* Check if the constraints pointed to by "constraint" is a div
781  * constraint corresponding to div "div" in "ls".
782  *
783  * That is, if div = floor(f/m), then check if the constraint is
784  *
785  *              f - m d >= 0
786  * or
787  *              -(f-(m-1)) + m d >= 0
788  */
789 int isl_local_space_is_div_constraint(__isl_keep isl_local_space *ls,
790         isl_int *constraint, unsigned div)
791 {
792         unsigned pos;
793
794         if (!ls)
795                 return -1;
796
797         if (isl_int_is_zero(ls->div->row[div][0]))
798                 return 0;
799
800         pos = isl_local_space_offset(ls, isl_dim_div) + div;
801
802         if (isl_int_eq(constraint[pos], ls->div->row[div][0])) {
803                 int neg;
804                 isl_int_sub(ls->div->row[div][1],
805                                 ls->div->row[div][1], ls->div->row[div][0]);
806                 isl_int_add_ui(ls->div->row[div][1], ls->div->row[div][1], 1);
807                 neg = isl_seq_is_neg(constraint, ls->div->row[div]+1, pos);
808                 isl_int_sub_ui(ls->div->row[div][1], ls->div->row[div][1], 1);
809                 isl_int_add(ls->div->row[div][1],
810                                 ls->div->row[div][1], ls->div->row[div][0]);
811                 if (!neg)
812                         return 0;
813                 if (isl_seq_first_non_zero(constraint+pos+1,
814                                             ls->div->n_row-div-1) != -1)
815                         return 0;
816         } else if (isl_int_abs_eq(constraint[pos], ls->div->row[div][0])) {
817                 if (!isl_seq_eq(constraint, ls->div->row[div]+1, pos))
818                         return 0;
819                 if (isl_seq_first_non_zero(constraint+pos+1,
820                                             ls->div->n_row-div-1) != -1)
821                         return 0;
822         } else
823                 return 0;
824
825         return 1;
826 }
827
828 /*
829  * Set active[i] to 1 if the dimension at position i is involved
830  * in the linear expression l.
831  */
832 int *isl_local_space_get_active(__isl_keep isl_local_space *ls, isl_int *l)
833 {
834         int i, j;
835         isl_ctx *ctx;
836         int *active = NULL;
837         unsigned total;
838         unsigned offset;
839
840         ctx = isl_local_space_get_ctx(ls);
841         total = isl_local_space_dim(ls, isl_dim_all);
842         active = isl_calloc_array(ctx, int, total);
843         if (!active)
844                 return NULL;
845
846         for (i = 0; i < total; ++i)
847                 active[i] = !isl_int_is_zero(l[i]);
848
849         offset = isl_local_space_offset(ls, isl_dim_div) - 1;
850         for (i = ls->div->n_row - 1; i >= 0; --i) {
851                 if (!active[offset + i])
852                         continue;
853                 for (j = 0; j < total; ++j)
854                         active[j] |= !isl_int_is_zero(ls->div->row[i][2 + j]);
855         }
856
857         return active;
858 }
859
860 /* Given a local space "ls" of a set, create a local space
861  * for the lift of the set.  In particular, the result
862  * is of the form [dim -> local[..]], with ls->div->n_row variables in the
863  * range of the wrapped map.
864  */
865 __isl_give isl_local_space *isl_local_space_lift(
866         __isl_take isl_local_space *ls)
867 {
868         ls = isl_local_space_cow(ls);
869         if (!ls)
870                 return NULL;
871
872         ls->dim = isl_space_lift(ls->dim, ls->div->n_row);
873         ls->div = isl_mat_drop_rows(ls->div, 0, ls->div->n_row);
874         if (!ls->dim || !ls->div)
875                 return isl_local_space_free(ls);
876
877         return ls;
878 }
879
880 /* Construct a basic map that maps a set living in local space "ls"
881  * to the corresponding lifted local space.
882  */
883 __isl_give isl_basic_map *isl_local_space_lifting(
884         __isl_take isl_local_space *ls)
885 {
886         isl_basic_map *lifting;
887         isl_basic_set *bset;
888
889         if (!ls)
890                 return NULL;
891         if (!isl_local_space_is_set(ls))
892                 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
893                         "lifting only defined on set spaces",
894                         return isl_local_space_free(ls));
895
896         bset = isl_basic_set_from_local_space(ls);
897         lifting = isl_basic_set_unwrap(isl_basic_set_lift(bset));
898         lifting = isl_basic_map_domain_map(lifting);
899         lifting = isl_basic_map_reverse(lifting);
900
901         return lifting;
902 }