19ae70202a65c8621f4568bb1fa4df2921f76b68
[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 = div1->n_col - div1->n_row;
417
418         div = isl_mat_alloc(div1->ctx, 1 + div1->n_row + div2->n_row,
419                                 d + div1->n_row + div2->n_row);
420         if (!div)
421                 return NULL;
422
423         for (i = 0, j = 0, k = 0; i < div1->n_row && j < div2->n_row; ++k) {
424                 int cmp;
425
426                 expand_row(div, k, div1, i, exp1);
427                 expand_row(div, k + 1, div2, j, exp2);
428
429                 cmp = cmp_row(div, k, k + 1);
430                 if (cmp == 0) {
431                         exp1[i++] = k;
432                         exp2[j++] = k;
433                 } else if (cmp < 0) {
434                         exp1[i++] = k;
435                 } else {
436                         exp2[j++] = k;
437                         isl_seq_cpy(div->row[k], div->row[k + 1], div->n_col);
438                 }
439         }
440         for (; i < div1->n_row; ++i, ++k) {
441                 expand_row(div, k, div1, i, exp1);
442                 exp1[i] = k;
443         }
444         for (; j < div2->n_row; ++j, ++k) {
445                 expand_row(div, k, div2, j, exp2);
446                 exp2[j] = k;
447         }
448
449         div->n_row = k;
450         div->n_col = d + k;
451
452         return div;
453 }
454
455 /* Construct a local space that contains all the divs in either
456  * "ls1" or "ls2".
457  */
458 __isl_give isl_local_space *isl_local_space_intersect(
459         __isl_take isl_local_space *ls1, __isl_take isl_local_space *ls2)
460 {
461         isl_ctx *ctx;
462         int *exp1 = NULL;
463         int *exp2 = NULL;
464         isl_mat *div;
465
466         if (!ls1 || !ls2)
467                 goto error;
468
469         ctx = isl_local_space_get_ctx(ls1);
470         if (!isl_space_is_equal(ls1->dim, ls2->dim))
471                 isl_die(ctx, isl_error_invalid,
472                         "spaces should be identical", goto error);
473
474         if (ls2->div->n_row == 0) {
475                 isl_local_space_free(ls2);
476                 return ls1;
477         }
478
479         if (ls1->div->n_row == 0) {
480                 isl_local_space_free(ls1);
481                 return ls2;
482         }
483
484         exp1 = isl_alloc_array(ctx, int, ls1->div->n_row);
485         exp2 = isl_alloc_array(ctx, int, ls2->div->n_row);
486         if (!exp1 || !exp2)
487                 goto error;
488
489         div = isl_merge_divs(ls1->div, ls2->div, exp1, exp2);
490         if (!div)
491                 goto error;
492
493         free(exp1);
494         free(exp2);
495         isl_local_space_free(ls2);
496         isl_mat_free(ls1->div);
497         ls1->div = div;
498
499         return ls1;
500 error:
501         free(exp1);
502         free(exp2);
503         isl_local_space_free(ls1);
504         isl_local_space_free(ls2);
505         return NULL;
506 }
507
508 int isl_local_space_divs_known(__isl_keep isl_local_space *ls)
509 {
510         int i;
511
512         if (!ls)
513                 return -1;
514
515         for (i = 0; i < ls->div->n_row; ++i)
516                 if (isl_int_is_zero(ls->div->row[i][0]))
517                         return 0;
518
519         return 1;
520 }
521
522 __isl_give isl_local_space *isl_local_space_domain(
523         __isl_take isl_local_space *ls)
524 {
525         ls = isl_local_space_drop_dims(ls, isl_dim_out,
526                                         0, isl_local_space_dim(ls, isl_dim_out));
527         ls = isl_local_space_cow(ls);
528         if (!ls)
529                 return NULL;
530         ls->dim = isl_space_domain(ls->dim);
531         if (!ls->dim)
532                 return isl_local_space_free(ls);
533         return ls;
534 }
535
536 __isl_give isl_local_space *isl_local_space_range(
537         __isl_take isl_local_space *ls)
538 {
539         ls = isl_local_space_drop_dims(ls, isl_dim_in,
540                                         0, isl_local_space_dim(ls, isl_dim_in));
541         ls = isl_local_space_cow(ls);
542         if (!ls)
543                 return NULL;
544
545         ls->dim = isl_space_range(ls->dim);
546         if (!ls->dim)
547                 return isl_local_space_free(ls);
548         return ls;
549 }
550
551 /* Construct a local space for a map that has the given local
552  * space as domain and that has a zero-dimensional range.
553  */
554 __isl_give isl_local_space *isl_local_space_from_domain(
555         __isl_take isl_local_space *ls)
556 {
557         ls = isl_local_space_cow(ls);
558         if (!ls)
559                 return NULL;
560         ls->dim = isl_space_from_domain(ls->dim);
561         if (!ls->dim)
562                 return isl_local_space_free(ls);
563         return ls;
564 }
565
566 __isl_give isl_local_space *isl_local_space_add_dims(
567         __isl_take isl_local_space *ls, enum isl_dim_type type, unsigned n)
568 {
569         int pos;
570
571         if (!ls)
572                 return NULL;
573         pos = isl_local_space_dim(ls, type);
574         return isl_local_space_insert_dims(ls, type, pos, n);
575 }
576
577 /* Remove common factor of non-constant terms and denominator.
578  */
579 static void normalize_div(__isl_keep isl_local_space *ls, int div)
580 {
581         isl_ctx *ctx = ls->div->ctx;
582         unsigned total = ls->div->n_col - 2;
583
584         isl_seq_gcd(ls->div->row[div] + 2, total, &ctx->normalize_gcd);
585         isl_int_gcd(ctx->normalize_gcd,
586                     ctx->normalize_gcd, ls->div->row[div][0]);
587         if (isl_int_is_one(ctx->normalize_gcd))
588                 return;
589
590         isl_seq_scale_down(ls->div->row[div] + 2, ls->div->row[div] + 2,
591                             ctx->normalize_gcd, total);
592         isl_int_divexact(ls->div->row[div][0], ls->div->row[div][0],
593                             ctx->normalize_gcd);
594         isl_int_fdiv_q(ls->div->row[div][1], ls->div->row[div][1],
595                             ctx->normalize_gcd);
596 }
597
598 /* Exploit the equalities in "eq" to simplify the expressions of
599  * the integer divisions in "ls".
600  * The integer divisions in "ls" are assumed to appear as regular
601  * dimensions in "eq".
602  */
603 __isl_give isl_local_space *isl_local_space_substitute_equalities(
604         __isl_take isl_local_space *ls, __isl_take isl_basic_set *eq)
605 {
606         int i, j, k;
607         unsigned total;
608         unsigned n_div;
609
610         ls = isl_local_space_cow(ls);
611         if (!ls || !eq)
612                 goto error;
613
614         total = isl_space_dim(eq->dim, isl_dim_all);
615         if (isl_local_space_dim(ls, isl_dim_all) != total)
616                 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
617                         "dimensions don't match", goto error);
618         total++;
619         n_div = eq->n_div;
620         for (i = 0; i < eq->n_eq; ++i) {
621                 j = isl_seq_last_non_zero(eq->eq[i], total + n_div);
622                 if (j < 0 || j == 0 || j >= total)
623                         continue;
624
625                 for (k = 0; k < ls->div->n_row; ++k) {
626                         if (isl_int_is_zero(ls->div->row[k][1 + j]))
627                                 continue;
628                         isl_seq_elim(ls->div->row[k] + 1, eq->eq[i], j, total,
629                                         &ls->div->row[k][0]);
630                         normalize_div(ls, k);
631                 }
632         }
633
634         isl_basic_set_free(eq);
635         return ls;
636 error:
637         isl_basic_set_free(eq);
638         isl_local_space_free(ls);
639         return NULL;
640 }
641
642 /* Plug in "subs" for dimension "type", "pos" in the integer divisions
643  * of "ls".
644  *
645  * Let i be the dimension to replace and let "subs" be of the form
646  *
647  *      f/d
648  *
649  * Any integer division with a non-zero coefficient for i,
650  *
651  *      floor((a i + g)/m)
652  *
653  * is replaced by
654  *
655  *      floor((a f + d g)/(m d))
656  */
657 __isl_give isl_local_space *isl_local_space_substitute(
658         __isl_take isl_local_space *ls,
659         enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
660 {
661         int i;
662         isl_int v;
663
664         ls = isl_local_space_cow(ls);
665         if (!ls || !subs)
666                 return isl_local_space_free(ls);
667
668         if (!isl_space_is_equal(ls->dim, subs->ls->dim))
669                 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
670                         "spaces don't match", return isl_local_space_free(ls));
671         if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
672                 isl_die(isl_local_space_get_ctx(ls), isl_error_unsupported,
673                         "cannot handle divs yet",
674                         return isl_local_space_free(ls));
675
676         pos += isl_local_space_offset(ls, type);
677
678         isl_int_init(v);
679         for (i = 0; i < ls->div->n_row; ++i) {
680                 if (isl_int_is_zero(ls->div->row[i][1 + pos]))
681                         continue;
682                 isl_int_set(v, ls->div->row[i][1 + pos]);
683                 isl_int_set_si(ls->div->row[i][1 + pos], 0);
684                 isl_seq_combine(ls->div->row[i] + 1,
685                                 subs->v->el[0], ls->div->row[i] + 1,
686                                 v, subs->v->el + 1, subs->v->size - 1);
687                 isl_int_mul(ls->div->row[i][0],
688                             ls->div->row[i][0], subs->v->el[0]);
689                 normalize_div(ls, i);
690         }
691         isl_int_clear(v);
692
693         return ls;
694 }
695
696 int isl_local_space_is_named_or_nested(__isl_keep isl_local_space *ls,
697         enum isl_dim_type type)
698 {
699         if (!ls)
700                 return -1;
701         return isl_space_is_named_or_nested(ls->dim, type);
702 }
703
704 __isl_give isl_local_space *isl_local_space_drop_dims(
705         __isl_take isl_local_space *ls,
706         enum isl_dim_type type, unsigned first, unsigned n)
707 {
708         isl_ctx *ctx;
709
710         if (!ls)
711                 return NULL;
712         if (n == 0 && !isl_local_space_is_named_or_nested(ls, type))
713                 return ls;
714
715         ctx = isl_local_space_get_ctx(ls);
716         if (first + n > isl_local_space_dim(ls, type))
717                 isl_die(ctx, isl_error_invalid, "range out of bounds",
718                         return isl_local_space_free(ls));
719
720         ls = isl_local_space_cow(ls);
721         if (!ls)
722                 return NULL;
723
724         if (type == isl_dim_div) {
725                 ls->div = isl_mat_drop_rows(ls->div, first, n);
726         } else {
727                 ls->dim = isl_space_drop_dims(ls->dim, type, first, n);
728                 if (!ls->dim)
729                         return isl_local_space_free(ls);
730         }
731
732         first += 1 + isl_local_space_offset(ls, type);
733         ls->div = isl_mat_drop_cols(ls->div, first, n);
734         if (!ls->div)
735                 return isl_local_space_free(ls);
736
737         return ls;
738 }
739
740 __isl_give isl_local_space *isl_local_space_insert_dims(
741         __isl_take isl_local_space *ls,
742         enum isl_dim_type type, unsigned first, unsigned n)
743 {
744         isl_ctx *ctx;
745
746         if (!ls)
747                 return NULL;
748         if (n == 0 && !isl_local_space_is_named_or_nested(ls, type))
749                 return ls;
750
751         ctx = isl_local_space_get_ctx(ls);
752         if (first > isl_local_space_dim(ls, type))
753                 isl_die(ctx, isl_error_invalid, "position out of bounds",
754                         return isl_local_space_free(ls));
755
756         ls = isl_local_space_cow(ls);
757         if (!ls)
758                 return NULL;
759
760         if (type == isl_dim_div) {
761                 ls->div = isl_mat_insert_zero_rows(ls->div, first, n);
762         } else {
763                 ls->dim = isl_space_insert_dims(ls->dim, type, first, n);
764                 if (!ls->dim)
765                         return isl_local_space_free(ls);
766         }
767
768         first += 1 + isl_local_space_offset(ls, type);
769         ls->div = isl_mat_insert_zero_cols(ls->div, first, n);
770         if (!ls->div)
771                 return isl_local_space_free(ls);
772
773         return ls;
774 }
775
776 /* Check if the constraints pointed to by "constraint" is a div
777  * constraint corresponding to div "div" in "ls".
778  *
779  * That is, if div = floor(f/m), then check if the constraint is
780  *
781  *              f - m d >= 0
782  * or
783  *              -(f-(m-1)) + m d >= 0
784  */
785 int isl_local_space_is_div_constraint(__isl_keep isl_local_space *ls,
786         isl_int *constraint, unsigned div)
787 {
788         unsigned pos;
789
790         if (!ls)
791                 return -1;
792
793         if (isl_int_is_zero(ls->div->row[div][0]))
794                 return 0;
795
796         pos = isl_local_space_offset(ls, isl_dim_div) + div;
797
798         if (isl_int_eq(constraint[pos], ls->div->row[div][0])) {
799                 int neg;
800                 isl_int_sub(ls->div->row[div][1],
801                                 ls->div->row[div][1], ls->div->row[div][0]);
802                 isl_int_add_ui(ls->div->row[div][1], ls->div->row[div][1], 1);
803                 neg = isl_seq_is_neg(constraint, ls->div->row[div]+1, pos);
804                 isl_int_sub_ui(ls->div->row[div][1], ls->div->row[div][1], 1);
805                 isl_int_add(ls->div->row[div][1],
806                                 ls->div->row[div][1], ls->div->row[div][0]);
807                 if (!neg)
808                         return 0;
809                 if (isl_seq_first_non_zero(constraint+pos+1,
810                                             ls->div->n_row-div-1) != -1)
811                         return 0;
812         } else if (isl_int_abs_eq(constraint[pos], ls->div->row[div][0])) {
813                 if (!isl_seq_eq(constraint, ls->div->row[div]+1, pos))
814                         return 0;
815                 if (isl_seq_first_non_zero(constraint+pos+1,
816                                             ls->div->n_row-div-1) != -1)
817                         return 0;
818         } else
819                 return 0;
820
821         return 1;
822 }
823
824 /*
825  * Set active[i] to 1 if the dimension at position i is involved
826  * in the linear expression l.
827  */
828 int *isl_local_space_get_active(__isl_keep isl_local_space *ls, isl_int *l)
829 {
830         int i, j;
831         isl_ctx *ctx;
832         int *active = NULL;
833         unsigned total;
834         unsigned offset;
835
836         ctx = isl_local_space_get_ctx(ls);
837         total = isl_local_space_dim(ls, isl_dim_all);
838         active = isl_calloc_array(ctx, int, total);
839         if (!active)
840                 return NULL;
841
842         for (i = 0; i < total; ++i)
843                 active[i] = !isl_int_is_zero(l[i]);
844
845         offset = isl_local_space_offset(ls, isl_dim_div) - 1;
846         for (i = ls->div->n_row - 1; i >= 0; --i) {
847                 if (!active[offset + i])
848                         continue;
849                 for (j = 0; j < total; ++j)
850                         active[j] |= !isl_int_is_zero(ls->div->row[i][2 + j]);
851         }
852
853         return active;
854 }
855
856 /* Given a local space "ls" of a set, create a local space
857  * for the lift of the set.  In particular, the result
858  * is of the form [dim -> local[..]], with ls->div->n_row variables in the
859  * range of the wrapped map.
860  */
861 __isl_give isl_local_space *isl_local_space_lift(
862         __isl_take isl_local_space *ls)
863 {
864         ls = isl_local_space_cow(ls);
865         if (!ls)
866                 return NULL;
867
868         ls->dim = isl_space_lift(ls->dim, ls->div->n_row);
869         ls->div = isl_mat_drop_rows(ls->div, 0, ls->div->n_row);
870         if (!ls->dim || !ls->div)
871                 return isl_local_space_free(ls);
872
873         return ls;
874 }
875
876 /* Construct a basic map that maps a set living in local space "ls"
877  * to the corresponding lifted local space.
878  */
879 __isl_give isl_basic_map *isl_local_space_lifting(
880         __isl_take isl_local_space *ls)
881 {
882         isl_basic_map *lifting;
883         isl_basic_set *bset;
884
885         if (!ls)
886                 return NULL;
887         if (!isl_local_space_is_set(ls))
888                 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
889                         "lifting only defined on set spaces",
890                         return isl_local_space_free(ls));
891
892         bset = isl_basic_set_from_local_space(ls);
893         lifting = isl_basic_set_unwrap(isl_basic_set_lift(bset));
894         lifting = isl_basic_map_domain_map(lifting);
895         lifting = isl_basic_map_reverse(lifting);
896
897         return lifting;
898 }