AST printing callbacks: pass isl_ast_print_options
[isl.git] / isl_space.c
blobeaad1a6127b6ed426daa842f24bdc96d12f7bc0e
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
5 * Use of this software is governed by the MIT license
7 * Written by Sven Verdoolaege, K.U.Leuven, Departement
8 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
9 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
10 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
13 #include <stdlib.h>
14 #include <isl_space_private.h>
15 #include <isl_id_private.h>
16 #include <isl_reordering.h>
18 isl_ctx *isl_space_get_ctx(__isl_keep isl_space *dim)
20 return dim ? dim->ctx : NULL;
23 __isl_give isl_space *isl_space_alloc(isl_ctx *ctx,
24 unsigned nparam, unsigned n_in, unsigned n_out)
26 isl_space *dim;
28 dim = isl_alloc_type(ctx, struct isl_space);
29 if (!dim)
30 return NULL;
32 dim->ctx = ctx;
33 isl_ctx_ref(ctx);
34 dim->ref = 1;
35 dim->nparam = nparam;
36 dim->n_in = n_in;
37 dim->n_out = n_out;
39 dim->tuple_id[0] = NULL;
40 dim->tuple_id[1] = NULL;
42 dim->nested[0] = NULL;
43 dim->nested[1] = NULL;
45 dim->n_id = 0;
46 dim->ids = NULL;
48 return dim;
51 /* Mark the space as being that of a set, by setting the domain tuple
52 * to isl_id_none.
54 static __isl_give isl_space *mark_as_set(__isl_take isl_space *space)
56 space = isl_space_cow(space);
57 if (!space)
58 return NULL;
59 space = isl_space_set_tuple_id(space, isl_dim_in, &isl_id_none);
60 return space;
63 /* Is the space that of a set?
65 int isl_space_is_set(__isl_keep isl_space *space)
67 if (!space)
68 return -1;
69 if (space->n_in != 0 || space->nested[0])
70 return 0;
71 if (space->tuple_id[0] != &isl_id_none)
72 return 0;
73 return 1;
76 /* Is the given space that of a map?
78 int isl_space_is_map(__isl_keep isl_space *space)
80 if (!space)
81 return -1;
82 return space->tuple_id[0] != &isl_id_none &&
83 space->tuple_id[1] != &isl_id_none;
86 __isl_give isl_space *isl_space_set_alloc(isl_ctx *ctx,
87 unsigned nparam, unsigned dim)
89 isl_space *space;
90 space = isl_space_alloc(ctx, nparam, 0, dim);
91 space = mark_as_set(space);
92 return space;
95 /* Mark the space as being that of a parameter domain, by setting
96 * both tuples to isl_id_none.
98 static __isl_give isl_space *mark_as_params(isl_space *space)
100 if (!space)
101 return NULL;
102 space = isl_space_set_tuple_id(space, isl_dim_in, &isl_id_none);
103 space = isl_space_set_tuple_id(space, isl_dim_out, &isl_id_none);
104 return space;
107 /* Is the space that of a parameter domain?
109 int isl_space_is_params(__isl_keep isl_space *space)
111 if (!space)
112 return -1;
113 if (space->n_in != 0 || space->nested[0] ||
114 space->n_out != 0 || space->nested[1])
115 return 0;
116 if (space->tuple_id[0] != &isl_id_none)
117 return 0;
118 if (space->tuple_id[1] != &isl_id_none)
119 return 0;
120 return 1;
123 /* Create a space for a parameter domain.
125 __isl_give isl_space *isl_space_params_alloc(isl_ctx *ctx, unsigned nparam)
127 isl_space *space;
128 space = isl_space_alloc(ctx, nparam, 0, 0);
129 space = mark_as_params(space);
130 return space;
133 static unsigned global_pos(__isl_keep isl_space *dim,
134 enum isl_dim_type type, unsigned pos)
136 struct isl_ctx *ctx = dim->ctx;
138 switch (type) {
139 case isl_dim_param:
140 isl_assert(ctx, pos < dim->nparam,
141 return isl_space_dim(dim, isl_dim_all));
142 return pos;
143 case isl_dim_in:
144 isl_assert(ctx, pos < dim->n_in,
145 return isl_space_dim(dim, isl_dim_all));
146 return pos + dim->nparam;
147 case isl_dim_out:
148 isl_assert(ctx, pos < dim->n_out,
149 return isl_space_dim(dim, isl_dim_all));
150 return pos + dim->nparam + dim->n_in;
151 default:
152 isl_assert(ctx, 0, return isl_space_dim(dim, isl_dim_all));
154 return isl_space_dim(dim, isl_dim_all);
157 /* Extend length of ids array to the total number of dimensions.
159 static __isl_give isl_space *extend_ids(__isl_take isl_space *dim)
161 isl_id **ids;
162 int i;
164 if (isl_space_dim(dim, isl_dim_all) <= dim->n_id)
165 return dim;
167 if (!dim->ids) {
168 dim->ids = isl_calloc_array(dim->ctx,
169 isl_id *, isl_space_dim(dim, isl_dim_all));
170 if (!dim->ids)
171 goto error;
172 } else {
173 ids = isl_realloc_array(dim->ctx, dim->ids,
174 isl_id *, isl_space_dim(dim, isl_dim_all));
175 if (!ids)
176 goto error;
177 dim->ids = ids;
178 for (i = dim->n_id; i < isl_space_dim(dim, isl_dim_all); ++i)
179 dim->ids[i] = NULL;
182 dim->n_id = isl_space_dim(dim, isl_dim_all);
184 return dim;
185 error:
186 isl_space_free(dim);
187 return NULL;
190 static __isl_give isl_space *set_id(__isl_take isl_space *dim,
191 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
193 dim = isl_space_cow(dim);
195 if (!dim)
196 goto error;
198 pos = global_pos(dim, type, pos);
199 if (pos == isl_space_dim(dim, isl_dim_all))
200 goto error;
202 if (pos >= dim->n_id) {
203 if (!id)
204 return dim;
205 dim = extend_ids(dim);
206 if (!dim)
207 goto error;
210 dim->ids[pos] = id;
212 return dim;
213 error:
214 isl_id_free(id);
215 isl_space_free(dim);
216 return NULL;
219 static __isl_keep isl_id *get_id(__isl_keep isl_space *dim,
220 enum isl_dim_type type, unsigned pos)
222 if (!dim)
223 return NULL;
225 pos = global_pos(dim, type, pos);
226 if (pos == isl_space_dim(dim, isl_dim_all))
227 return NULL;
228 if (pos >= dim->n_id)
229 return NULL;
230 return dim->ids[pos];
233 static unsigned offset(__isl_keep isl_space *dim, enum isl_dim_type type)
235 switch (type) {
236 case isl_dim_param: return 0;
237 case isl_dim_in: return dim->nparam;
238 case isl_dim_out: return dim->nparam + dim->n_in;
239 default: return 0;
243 static unsigned n(__isl_keep isl_space *dim, enum isl_dim_type type)
245 switch (type) {
246 case isl_dim_param: return dim->nparam;
247 case isl_dim_in: return dim->n_in;
248 case isl_dim_out: return dim->n_out;
249 case isl_dim_all: return dim->nparam + dim->n_in + dim->n_out;
250 default: return 0;
254 unsigned isl_space_dim(__isl_keep isl_space *dim, enum isl_dim_type type)
256 if (!dim)
257 return 0;
258 return n(dim, type);
261 unsigned isl_space_offset(__isl_keep isl_space *dim, enum isl_dim_type type)
263 if (!dim)
264 return 0;
265 return offset(dim, type);
268 static __isl_give isl_space *copy_ids(__isl_take isl_space *dst,
269 enum isl_dim_type dst_type, unsigned offset, __isl_keep isl_space *src,
270 enum isl_dim_type src_type)
272 int i;
273 isl_id *id;
275 if (!dst)
276 return NULL;
278 for (i = 0; i < n(src, src_type); ++i) {
279 id = get_id(src, src_type, i);
280 if (!id)
281 continue;
282 dst = set_id(dst, dst_type, offset + i, isl_id_copy(id));
283 if (!dst)
284 return NULL;
286 return dst;
289 __isl_take isl_space *isl_space_dup(__isl_keep isl_space *dim)
291 isl_space *dup;
292 if (!dim)
293 return NULL;
294 dup = isl_space_alloc(dim->ctx, dim->nparam, dim->n_in, dim->n_out);
295 if (dim->tuple_id[0] &&
296 !(dup->tuple_id[0] = isl_id_copy(dim->tuple_id[0])))
297 goto error;
298 if (dim->tuple_id[1] &&
299 !(dup->tuple_id[1] = isl_id_copy(dim->tuple_id[1])))
300 goto error;
301 if (dim->nested[0] && !(dup->nested[0] = isl_space_copy(dim->nested[0])))
302 goto error;
303 if (dim->nested[1] && !(dup->nested[1] = isl_space_copy(dim->nested[1])))
304 goto error;
305 if (!dim->ids)
306 return dup;
307 dup = copy_ids(dup, isl_dim_param, 0, dim, isl_dim_param);
308 dup = copy_ids(dup, isl_dim_in, 0, dim, isl_dim_in);
309 dup = copy_ids(dup, isl_dim_out, 0, dim, isl_dim_out);
310 return dup;
311 error:
312 isl_space_free(dup);
313 return NULL;
316 __isl_give isl_space *isl_space_cow(__isl_take isl_space *dim)
318 if (!dim)
319 return NULL;
321 if (dim->ref == 1)
322 return dim;
323 dim->ref--;
324 return isl_space_dup(dim);
327 __isl_give isl_space *isl_space_copy(__isl_keep isl_space *dim)
329 if (!dim)
330 return NULL;
332 dim->ref++;
333 return dim;
336 void *isl_space_free(__isl_take isl_space *dim)
338 int i;
340 if (!dim)
341 return NULL;
343 if (--dim->ref > 0)
344 return NULL;
346 isl_id_free(dim->tuple_id[0]);
347 isl_id_free(dim->tuple_id[1]);
349 isl_space_free(dim->nested[0]);
350 isl_space_free(dim->nested[1]);
352 for (i = 0; i < dim->n_id; ++i)
353 isl_id_free(dim->ids[i]);
354 free(dim->ids);
355 isl_ctx_deref(dim->ctx);
357 free(dim);
359 return NULL;
362 /* Check if "s" is a valid dimension or tuple name.
363 * We currently only forbid names that look like a number.
365 * s is assumed to be non-NULL.
367 static int name_ok(isl_ctx *ctx, const char *s)
369 char *p;
370 long dummy;
372 dummy = strtol(s, &p, 0);
373 if (p != s)
374 isl_die(ctx, isl_error_invalid, "name looks like a number",
375 return 0);
377 return 1;
380 /* Is it possible for the given dimension type to have a tuple id?
382 static int space_can_have_id(__isl_keep isl_space *space,
383 enum isl_dim_type type)
385 if (!space)
386 return 0;
387 if (isl_space_is_params(space))
388 isl_die(space->ctx, isl_error_invalid,
389 "parameter spaces don't have tuple ids", return 0);
390 if (isl_space_is_set(space) && type != isl_dim_set)
391 isl_die(space->ctx, isl_error_invalid,
392 "set spaces can only have a set id", return 0);
393 if (type != isl_dim_in && type != isl_dim_out)
394 isl_die(space->ctx, isl_error_invalid,
395 "only input, output and set tuples can have ids",
396 return 0);
398 return 1;
401 /* Does the tuple have an id?
403 int isl_space_has_tuple_id(__isl_keep isl_space *dim, enum isl_dim_type type)
405 if (!space_can_have_id(dim, type))
406 return -1;
407 return dim->tuple_id[type - isl_dim_in] != NULL;
410 __isl_give isl_id *isl_space_get_tuple_id(__isl_keep isl_space *dim,
411 enum isl_dim_type type)
413 int has_id;
415 if (!dim)
416 return NULL;
417 has_id = isl_space_has_tuple_id(dim, type);
418 if (has_id < 0)
419 return NULL;
420 if (!has_id)
421 isl_die(dim->ctx, isl_error_invalid,
422 "tuple has no id", return NULL);
423 return isl_id_copy(dim->tuple_id[type - isl_dim_in]);
426 __isl_give isl_space *isl_space_set_tuple_id(__isl_take isl_space *dim,
427 enum isl_dim_type type, __isl_take isl_id *id)
429 dim = isl_space_cow(dim);
430 if (!dim || !id)
431 goto error;
432 if (type != isl_dim_in && type != isl_dim_out)
433 isl_die(dim->ctx, isl_error_invalid,
434 "only input, output and set tuples can have names",
435 goto error);
437 isl_id_free(dim->tuple_id[type - isl_dim_in]);
438 dim->tuple_id[type - isl_dim_in] = id;
440 return dim;
441 error:
442 isl_id_free(id);
443 isl_space_free(dim);
444 return NULL;
447 __isl_give isl_space *isl_space_reset_tuple_id(__isl_take isl_space *dim,
448 enum isl_dim_type type)
450 dim = isl_space_cow(dim);
451 if (!dim)
452 return NULL;
453 if (type != isl_dim_in && type != isl_dim_out)
454 isl_die(dim->ctx, isl_error_invalid,
455 "only input, output and set tuples can have names",
456 goto error);
458 isl_id_free(dim->tuple_id[type - isl_dim_in]);
459 dim->tuple_id[type - isl_dim_in] = NULL;
461 return dim;
462 error:
463 isl_space_free(dim);
464 return NULL;
467 /* Set the id of the given dimension of "space" to "id".
468 * If the dimension already has an id, then it is replaced.
469 * If the dimension is a parameter, then we need to change it
470 * in the nested spaces (if any) as well.
472 __isl_give isl_space *isl_space_set_dim_id(__isl_take isl_space *space,
473 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
475 space = isl_space_cow(space);
476 if (!space || !id)
477 goto error;
479 if (type == isl_dim_param) {
480 int i;
482 for (i = 0; i < 2; ++i) {
483 if (!space->nested[i])
484 continue;
485 space->nested[i] =
486 isl_space_set_dim_id(space->nested[i],
487 type, pos, isl_id_copy(id));
488 if (!space->nested[i])
489 goto error;
493 isl_id_free(get_id(space, type, pos));
494 return set_id(space, type, pos, id);
495 error:
496 isl_id_free(id);
497 isl_space_free(space);
498 return NULL;
501 /* Reset the id of the given dimension of "space".
502 * If the dimension already has an id, then it is removed.
503 * If the dimension is a parameter, then we need to reset it
504 * in the nested spaces (if any) as well.
506 __isl_give isl_space *isl_space_reset_dim_id(__isl_take isl_space *space,
507 enum isl_dim_type type, unsigned pos)
509 space = isl_space_cow(space);
510 if (!space)
511 goto error;
513 if (type == isl_dim_param) {
514 int i;
516 for (i = 0; i < 2; ++i) {
517 if (!space->nested[i])
518 continue;
519 space->nested[i] =
520 isl_space_reset_dim_id(space->nested[i],
521 type, pos);
522 if (!space->nested[i])
523 goto error;
527 isl_id_free(get_id(space, type, pos));
528 return set_id(space, type, pos, NULL);
529 error:
530 isl_space_free(space);
531 return NULL;
534 int isl_space_has_dim_id(__isl_keep isl_space *dim,
535 enum isl_dim_type type, unsigned pos)
537 if (!dim)
538 return -1;
539 return get_id(dim, type, pos) != NULL;
542 __isl_give isl_id *isl_space_get_dim_id(__isl_keep isl_space *dim,
543 enum isl_dim_type type, unsigned pos)
545 if (!dim)
546 return NULL;
547 if (!get_id(dim, type, pos))
548 isl_die(dim->ctx, isl_error_invalid,
549 "dim has no id", return NULL);
550 return isl_id_copy(get_id(dim, type, pos));
553 __isl_give isl_space *isl_space_set_tuple_name(__isl_take isl_space *dim,
554 enum isl_dim_type type, const char *s)
556 isl_id *id;
558 if (!dim)
559 return NULL;
561 if (!s)
562 return isl_space_reset_tuple_id(dim, type);
564 if (!name_ok(dim->ctx, s))
565 goto error;
567 id = isl_id_alloc(dim->ctx, s, NULL);
568 return isl_space_set_tuple_id(dim, type, id);
569 error:
570 isl_space_free(dim);
571 return NULL;
574 /* Does the tuple have a name?
576 int isl_space_has_tuple_name(__isl_keep isl_space *space,
577 enum isl_dim_type type)
579 isl_id *id;
581 if (!space_can_have_id(space, type))
582 return -1;
583 id = space->tuple_id[type - isl_dim_in];
584 return id && id->name;
587 const char *isl_space_get_tuple_name(__isl_keep isl_space *dim,
588 enum isl_dim_type type)
590 isl_id *id;
591 if (!dim)
592 return NULL;
593 if (type != isl_dim_in && type != isl_dim_out)
594 return NULL;
595 id = dim->tuple_id[type - isl_dim_in];
596 return id ? id->name : NULL;
599 __isl_give isl_space *isl_space_set_dim_name(__isl_take isl_space *dim,
600 enum isl_dim_type type, unsigned pos,
601 const char *s)
603 isl_id *id;
605 if (!dim)
606 return NULL;
607 if (!s)
608 return isl_space_reset_dim_id(dim, type, pos);
609 if (!name_ok(dim->ctx, s))
610 goto error;
611 id = isl_id_alloc(dim->ctx, s, NULL);
612 return isl_space_set_dim_id(dim, type, pos, id);
613 error:
614 isl_space_free(dim);
615 return NULL;
618 /* Does the given dimension have a name?
620 int isl_space_has_dim_name(__isl_keep isl_space *space,
621 enum isl_dim_type type, unsigned pos)
623 isl_id *id;
625 if (!space)
626 return -1;
627 id = get_id(space, type, pos);
628 return id && id->name;
631 __isl_keep const char *isl_space_get_dim_name(__isl_keep isl_space *dim,
632 enum isl_dim_type type, unsigned pos)
634 isl_id *id = get_id(dim, type, pos);
635 return id ? id->name : NULL;
638 int isl_space_find_dim_by_id(__isl_keep isl_space *dim, enum isl_dim_type type,
639 __isl_keep isl_id *id)
641 int i;
642 int offset;
643 int n;
645 if (!dim || !id)
646 return -1;
648 offset = isl_space_offset(dim, type);
649 n = isl_space_dim(dim, type);
650 for (i = 0; i < n && offset + i < dim->n_id; ++i)
651 if (dim->ids[offset + i] == id)
652 return i;
654 return -1;
657 int isl_space_find_dim_by_name(__isl_keep isl_space *space,
658 enum isl_dim_type type, const char *name)
660 int i;
661 int offset;
662 int n;
664 if (!space || !name)
665 return -1;
667 offset = isl_space_offset(space, type);
668 n = isl_space_dim(space, type);
669 for (i = 0; i < n && offset + i < space->n_id; ++i)
670 if (space->ids[offset + i]->name &&
671 !strcmp(space->ids[offset + i]->name, name))
672 return i;
674 return -1;
677 static __isl_keep isl_id *tuple_id(__isl_keep isl_space *dim,
678 enum isl_dim_type type)
680 if (!dim)
681 return NULL;
682 if (type == isl_dim_in)
683 return dim->tuple_id[0];
684 if (type == isl_dim_out)
685 return dim->tuple_id[1];
686 return NULL;
689 static __isl_keep isl_space *nested(__isl_keep isl_space *dim,
690 enum isl_dim_type type)
692 if (!dim)
693 return NULL;
694 if (type == isl_dim_in)
695 return dim->nested[0];
696 if (type == isl_dim_out)
697 return dim->nested[1];
698 return NULL;
701 int isl_space_tuple_match(__isl_keep isl_space *dim1, enum isl_dim_type dim1_type,
702 __isl_keep isl_space *dim2, enum isl_dim_type dim2_type)
704 isl_id *id1, *id2;
705 isl_space *nested1, *nested2;
707 if (!dim1 || !dim2)
708 return -1;
710 if (dim1 == dim2 && dim1_type == dim2_type)
711 return 1;
713 if (n(dim1, dim1_type) != n(dim2, dim2_type))
714 return 0;
715 id1 = tuple_id(dim1, dim1_type);
716 id2 = tuple_id(dim2, dim2_type);
717 if (!id1 ^ !id2)
718 return 0;
719 if (id1 && id1 != id2)
720 return 0;
721 nested1 = nested(dim1, dim1_type);
722 nested2 = nested(dim2, dim2_type);
723 if (!nested1 ^ !nested2)
724 return 0;
725 if (nested1 && !isl_space_is_equal(nested1, nested2))
726 return 0;
727 return 1;
730 static int match(__isl_keep isl_space *dim1, enum isl_dim_type dim1_type,
731 __isl_keep isl_space *dim2, enum isl_dim_type dim2_type)
733 int i;
735 if (dim1 == dim2 && dim1_type == dim2_type)
736 return 1;
738 if (!isl_space_tuple_match(dim1, dim1_type, dim2, dim2_type))
739 return 0;
741 if (!dim1->ids && !dim2->ids)
742 return 1;
744 for (i = 0; i < n(dim1, dim1_type); ++i) {
745 if (get_id(dim1, dim1_type, i) != get_id(dim2, dim2_type, i))
746 return 0;
748 return 1;
751 int isl_space_match(__isl_keep isl_space *dim1, enum isl_dim_type dim1_type,
752 __isl_keep isl_space *dim2, enum isl_dim_type dim2_type)
754 if (!dim1 || !dim2)
755 return -1;
757 return match(dim1, dim1_type, dim2, dim2_type);
760 static void get_ids(__isl_keep isl_space *dim, enum isl_dim_type type,
761 unsigned first, unsigned n, __isl_keep isl_id **ids)
763 int i;
765 for (i = 0; i < n ; ++i)
766 ids[i] = get_id(dim, type, first + i);
769 __isl_give isl_space *isl_space_extend(__isl_take isl_space *dim,
770 unsigned nparam, unsigned n_in, unsigned n_out)
772 isl_id **ids = NULL;
774 if (!dim)
775 return NULL;
776 if (dim->nparam == nparam && dim->n_in == n_in && dim->n_out == n_out)
777 return dim;
779 isl_assert(dim->ctx, dim->nparam <= nparam, goto error);
780 isl_assert(dim->ctx, dim->n_in <= n_in, goto error);
781 isl_assert(dim->ctx, dim->n_out <= n_out, goto error);
783 dim = isl_space_cow(dim);
785 if (dim->ids) {
786 ids = isl_calloc_array(dim->ctx, isl_id *,
787 nparam + n_in + n_out);
788 if (!ids)
789 goto error;
790 get_ids(dim, isl_dim_param, 0, dim->nparam, ids);
791 get_ids(dim, isl_dim_in, 0, dim->n_in, ids + nparam);
792 get_ids(dim, isl_dim_out, 0, dim->n_out, ids + nparam + n_in);
793 free(dim->ids);
794 dim->ids = ids;
795 dim->n_id = nparam + n_in + n_out;
797 dim->nparam = nparam;
798 dim->n_in = n_in;
799 dim->n_out = n_out;
801 return dim;
802 error:
803 free(ids);
804 isl_space_free(dim);
805 return NULL;
808 __isl_give isl_space *isl_space_add_dims(__isl_take isl_space *dim,
809 enum isl_dim_type type, unsigned n)
811 if (!dim)
812 return NULL;
813 dim = isl_space_reset(dim, type);
814 switch (type) {
815 case isl_dim_param:
816 dim = isl_space_extend(dim,
817 dim->nparam + n, dim->n_in, dim->n_out);
818 if (dim && dim->nested[0] &&
819 !(dim->nested[0] = isl_space_add_dims(dim->nested[0],
820 isl_dim_param, n)))
821 goto error;
822 if (dim && dim->nested[1] &&
823 !(dim->nested[1] = isl_space_add_dims(dim->nested[1],
824 isl_dim_param, n)))
825 goto error;
826 return dim;
827 case isl_dim_in:
828 return isl_space_extend(dim,
829 dim->nparam, dim->n_in + n, dim->n_out);
830 case isl_dim_out:
831 return isl_space_extend(dim,
832 dim->nparam, dim->n_in, dim->n_out + n);
833 default:
834 isl_die(dim->ctx, isl_error_invalid,
835 "cannot add dimensions of specified type", goto error);
837 error:
838 isl_space_free(dim);
839 return NULL;
842 static int valid_dim_type(enum isl_dim_type type)
844 switch (type) {
845 case isl_dim_param:
846 case isl_dim_in:
847 case isl_dim_out:
848 return 1;
849 default:
850 return 0;
854 /* Insert "n" dimensions of type "type" at position "pos".
855 * If we are inserting parameters, then they are also inserted in
856 * any nested spaces.
858 __isl_give isl_space *isl_space_insert_dims(__isl_take isl_space *dim,
859 enum isl_dim_type type, unsigned pos, unsigned n)
861 isl_id **ids = NULL;
863 if (!dim)
864 return NULL;
865 if (n == 0)
866 return isl_space_reset(dim, type);
868 if (!valid_dim_type(type))
869 isl_die(dim->ctx, isl_error_invalid,
870 "cannot insert dimensions of specified type",
871 goto error);
873 isl_assert(dim->ctx, pos <= isl_space_dim(dim, type), goto error);
875 dim = isl_space_cow(dim);
876 if (!dim)
877 return NULL;
879 if (dim->ids) {
880 enum isl_dim_type t, o = isl_dim_param;
881 int off;
882 int s[3];
883 ids = isl_calloc_array(dim->ctx, isl_id *,
884 dim->nparam + dim->n_in + dim->n_out + n);
885 if (!ids)
886 goto error;
887 off = 0;
888 s[isl_dim_param - o] = dim->nparam;
889 s[isl_dim_in - o] = dim->n_in;
890 s[isl_dim_out - o] = dim->n_out;
891 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
892 if (t != type) {
893 get_ids(dim, t, 0, s[t - o], ids + off);
894 off += s[t - o];
895 } else {
896 get_ids(dim, t, 0, pos, ids + off);
897 off += pos + n;
898 get_ids(dim, t, pos, s[t - o] - pos, ids + off);
899 off += s[t - o] - pos;
902 free(dim->ids);
903 dim->ids = ids;
904 dim->n_id = dim->nparam + dim->n_in + dim->n_out + n;
906 switch (type) {
907 case isl_dim_param: dim->nparam += n; break;
908 case isl_dim_in: dim->n_in += n; break;
909 case isl_dim_out: dim->n_out += n; break;
910 default: ;
912 dim = isl_space_reset(dim, type);
914 if (type == isl_dim_param) {
915 if (dim && dim->nested[0] &&
916 !(dim->nested[0] = isl_space_insert_dims(dim->nested[0],
917 isl_dim_param, pos, n)))
918 goto error;
919 if (dim && dim->nested[1] &&
920 !(dim->nested[1] = isl_space_insert_dims(dim->nested[1],
921 isl_dim_param, pos, n)))
922 goto error;
925 return dim;
926 error:
927 isl_space_free(dim);
928 return NULL;
931 __isl_give isl_space *isl_space_move_dims(__isl_take isl_space *dim,
932 enum isl_dim_type dst_type, unsigned dst_pos,
933 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
935 int i;
937 if (!dim)
938 return NULL;
939 if (n == 0)
940 return dim;
942 isl_assert(dim->ctx, src_pos + n <= isl_space_dim(dim, src_type),
943 goto error);
945 if (dst_type == src_type && dst_pos == src_pos)
946 return dim;
948 isl_assert(dim->ctx, dst_type != src_type, goto error);
950 dim = isl_space_reset(dim, src_type);
951 dim = isl_space_reset(dim, dst_type);
953 dim = isl_space_cow(dim);
954 if (!dim)
955 return NULL;
957 if (dim->ids) {
958 isl_id **ids;
959 enum isl_dim_type t, o = isl_dim_param;
960 int off;
961 int s[3];
962 ids = isl_calloc_array(dim->ctx, isl_id *,
963 dim->nparam + dim->n_in + dim->n_out);
964 if (!ids)
965 goto error;
966 off = 0;
967 s[isl_dim_param - o] = dim->nparam;
968 s[isl_dim_in - o] = dim->n_in;
969 s[isl_dim_out - o] = dim->n_out;
970 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
971 if (t == dst_type) {
972 get_ids(dim, t, 0, dst_pos, ids + off);
973 off += dst_pos;
974 get_ids(dim, src_type, src_pos, n, ids + off);
975 off += n;
976 get_ids(dim, t, dst_pos, s[t - o] - dst_pos,
977 ids + off);
978 off += s[t - o] - dst_pos;
979 } else if (t == src_type) {
980 get_ids(dim, t, 0, src_pos, ids + off);
981 off += src_pos;
982 get_ids(dim, t, src_pos + n,
983 s[t - o] - src_pos - n, ids + off);
984 off += s[t - o] - src_pos - n;
985 } else {
986 get_ids(dim, t, 0, s[t - o], ids + off);
987 off += s[t - o];
990 free(dim->ids);
991 dim->ids = ids;
992 dim->n_id = dim->nparam + dim->n_in + dim->n_out;
995 switch (dst_type) {
996 case isl_dim_param: dim->nparam += n; break;
997 case isl_dim_in: dim->n_in += n; break;
998 case isl_dim_out: dim->n_out += n; break;
999 default: ;
1002 switch (src_type) {
1003 case isl_dim_param: dim->nparam -= n; break;
1004 case isl_dim_in: dim->n_in -= n; break;
1005 case isl_dim_out: dim->n_out -= n; break;
1006 default: ;
1009 if (dst_type != isl_dim_param && src_type != isl_dim_param)
1010 return dim;
1012 for (i = 0; i < 2; ++i) {
1013 if (!dim->nested[i])
1014 continue;
1015 dim->nested[i] = isl_space_replace(dim->nested[i],
1016 isl_dim_param, dim);
1017 if (!dim->nested[i])
1018 goto error;
1021 return dim;
1022 error:
1023 isl_space_free(dim);
1024 return NULL;
1027 __isl_give isl_space *isl_space_join(__isl_take isl_space *left,
1028 __isl_take isl_space *right)
1030 isl_space *dim;
1032 if (!left || !right)
1033 goto error;
1035 isl_assert(left->ctx, match(left, isl_dim_param, right, isl_dim_param),
1036 goto error);
1037 isl_assert(left->ctx,
1038 isl_space_tuple_match(left, isl_dim_out, right, isl_dim_in),
1039 goto error);
1041 dim = isl_space_alloc(left->ctx, left->nparam, left->n_in, right->n_out);
1042 if (!dim)
1043 goto error;
1045 dim = copy_ids(dim, isl_dim_param, 0, left, isl_dim_param);
1046 dim = copy_ids(dim, isl_dim_in, 0, left, isl_dim_in);
1047 dim = copy_ids(dim, isl_dim_out, 0, right, isl_dim_out);
1049 if (dim && left->tuple_id[0] &&
1050 !(dim->tuple_id[0] = isl_id_copy(left->tuple_id[0])))
1051 goto error;
1052 if (dim && right->tuple_id[1] &&
1053 !(dim->tuple_id[1] = isl_id_copy(right->tuple_id[1])))
1054 goto error;
1055 if (dim && left->nested[0] &&
1056 !(dim->nested[0] = isl_space_copy(left->nested[0])))
1057 goto error;
1058 if (dim && right->nested[1] &&
1059 !(dim->nested[1] = isl_space_copy(right->nested[1])))
1060 goto error;
1062 isl_space_free(left);
1063 isl_space_free(right);
1065 return dim;
1066 error:
1067 isl_space_free(left);
1068 isl_space_free(right);
1069 return NULL;
1072 __isl_give isl_space *isl_space_product(__isl_take isl_space *left,
1073 __isl_take isl_space *right)
1075 isl_space *dom1, *dom2, *nest1, *nest2;
1077 if (!left || !right)
1078 goto error;
1080 isl_assert(left->ctx, match(left, isl_dim_param, right, isl_dim_param),
1081 goto error);
1083 dom1 = isl_space_domain(isl_space_copy(left));
1084 dom2 = isl_space_domain(isl_space_copy(right));
1085 nest1 = isl_space_wrap(isl_space_join(isl_space_reverse(dom1), dom2));
1087 dom1 = isl_space_range(left);
1088 dom2 = isl_space_range(right);
1089 nest2 = isl_space_wrap(isl_space_join(isl_space_reverse(dom1), dom2));
1091 return isl_space_join(isl_space_reverse(nest1), nest2);
1092 error:
1093 isl_space_free(left);
1094 isl_space_free(right);
1095 return NULL;
1098 /* Given two spaces { A -> C } and { B -> C }, construct the space
1099 * { [A -> B] -> C }
1101 __isl_give isl_space *isl_space_domain_product(__isl_take isl_space *left,
1102 __isl_take isl_space *right)
1104 isl_space *ran, *dom1, *dom2, *nest;
1106 if (!left || !right)
1107 goto error;
1109 if (!match(left, isl_dim_param, right, isl_dim_param))
1110 isl_die(left->ctx, isl_error_invalid,
1111 "parameters need to match", goto error);
1112 if (!isl_space_tuple_match(left, isl_dim_out, right, isl_dim_out))
1113 isl_die(left->ctx, isl_error_invalid,
1114 "ranges need to match", goto error);
1116 ran = isl_space_range(isl_space_copy(left));
1118 dom1 = isl_space_domain(left);
1119 dom2 = isl_space_domain(right);
1120 nest = isl_space_wrap(isl_space_join(isl_space_reverse(dom1), dom2));
1122 return isl_space_join(isl_space_reverse(nest), ran);
1123 error:
1124 isl_space_free(left);
1125 isl_space_free(right);
1126 return NULL;
1129 __isl_give isl_space *isl_space_range_product(__isl_take isl_space *left,
1130 __isl_take isl_space *right)
1132 isl_space *dom, *ran1, *ran2, *nest;
1134 if (!left || !right)
1135 goto error;
1137 isl_assert(left->ctx, match(left, isl_dim_param, right, isl_dim_param),
1138 goto error);
1139 if (!isl_space_tuple_match(left, isl_dim_in, right, isl_dim_in))
1140 isl_die(left->ctx, isl_error_invalid,
1141 "domains need to match", goto error);
1143 dom = isl_space_domain(isl_space_copy(left));
1145 ran1 = isl_space_range(left);
1146 ran2 = isl_space_range(right);
1147 nest = isl_space_wrap(isl_space_join(isl_space_reverse(ran1), ran2));
1149 return isl_space_join(isl_space_reverse(dom), nest);
1150 error:
1151 isl_space_free(left);
1152 isl_space_free(right);
1153 return NULL;
1156 __isl_give isl_space *isl_space_map_from_set(__isl_take isl_space *dim)
1158 isl_ctx *ctx;
1159 isl_id **ids = NULL;
1161 if (!dim)
1162 return NULL;
1163 ctx = isl_space_get_ctx(dim);
1164 if (!isl_space_is_set(dim))
1165 isl_die(ctx, isl_error_invalid, "not a set space", goto error);
1166 dim = isl_space_cow(dim);
1167 if (!dim)
1168 return NULL;
1169 if (dim->ids) {
1170 ids = isl_calloc_array(dim->ctx, isl_id *,
1171 dim->nparam + dim->n_out + dim->n_out);
1172 if (!ids)
1173 goto error;
1174 get_ids(dim, isl_dim_param, 0, dim->nparam, ids);
1175 get_ids(dim, isl_dim_out, 0, dim->n_out, ids + dim->nparam);
1177 dim->n_in = dim->n_out;
1178 if (ids) {
1179 free(dim->ids);
1180 dim->ids = ids;
1181 dim->n_id = dim->nparam + dim->n_out + dim->n_out;
1182 dim = copy_ids(dim, isl_dim_out, 0, dim, isl_dim_in);
1184 isl_id_free(dim->tuple_id[0]);
1185 dim->tuple_id[0] = isl_id_copy(dim->tuple_id[1]);
1186 isl_space_free(dim->nested[0]);
1187 dim->nested[0] = isl_space_copy(dim->nested[1]);
1188 return dim;
1189 error:
1190 isl_space_free(dim);
1191 return NULL;
1194 __isl_give isl_space *isl_space_map_from_domain_and_range(
1195 __isl_take isl_space *domain, __isl_take isl_space *range)
1197 if (!domain || !range)
1198 goto error;
1199 if (!isl_space_is_set(domain))
1200 isl_die(isl_space_get_ctx(domain), isl_error_invalid,
1201 "domain is not a set space", goto error);
1202 if (!isl_space_is_set(range))
1203 isl_die(isl_space_get_ctx(range), isl_error_invalid,
1204 "range is not a set space", goto error);
1205 return isl_space_join(isl_space_reverse(domain), range);
1206 error:
1207 isl_space_free(domain);
1208 isl_space_free(range);
1209 return NULL;
1212 static __isl_give isl_space *set_ids(__isl_take isl_space *dim,
1213 enum isl_dim_type type,
1214 unsigned first, unsigned n, __isl_take isl_id **ids)
1216 int i;
1218 for (i = 0; i < n ; ++i)
1219 dim = set_id(dim, type, first + i, ids[i]);
1221 return dim;
1224 __isl_give isl_space *isl_space_reverse(__isl_take isl_space *dim)
1226 unsigned t;
1227 isl_space *nested;
1228 isl_id **ids = NULL;
1229 isl_id *id;
1231 if (!dim)
1232 return NULL;
1233 if (match(dim, isl_dim_in, dim, isl_dim_out))
1234 return dim;
1236 dim = isl_space_cow(dim);
1237 if (!dim)
1238 return NULL;
1240 id = dim->tuple_id[0];
1241 dim->tuple_id[0] = dim->tuple_id[1];
1242 dim->tuple_id[1] = id;
1244 nested = dim->nested[0];
1245 dim->nested[0] = dim->nested[1];
1246 dim->nested[1] = nested;
1248 if (dim->ids) {
1249 ids = isl_alloc_array(dim->ctx, isl_id *,
1250 dim->n_in + dim->n_out);
1251 if (!ids)
1252 goto error;
1253 get_ids(dim, isl_dim_in, 0, dim->n_in, ids);
1254 get_ids(dim, isl_dim_out, 0, dim->n_out, ids + dim->n_in);
1257 t = dim->n_in;
1258 dim->n_in = dim->n_out;
1259 dim->n_out = t;
1261 if (dim->ids) {
1262 dim = set_ids(dim, isl_dim_out, 0, dim->n_out, ids);
1263 dim = set_ids(dim, isl_dim_in, 0, dim->n_in, ids + dim->n_out);
1264 free(ids);
1267 return dim;
1268 error:
1269 free(ids);
1270 isl_space_free(dim);
1271 return NULL;
1274 __isl_give isl_space *isl_space_drop_dims(__isl_take isl_space *dim,
1275 enum isl_dim_type type, unsigned first, unsigned num)
1277 int i;
1279 if (!dim)
1280 return NULL;
1282 if (num == 0)
1283 return isl_space_reset(dim, type);
1285 if (!valid_dim_type(type))
1286 isl_die(dim->ctx, isl_error_invalid,
1287 "cannot drop dimensions of specified type", goto error);
1289 isl_assert(dim->ctx, first + num <= n(dim, type), goto error);
1290 dim = isl_space_cow(dim);
1291 if (!dim)
1292 goto error;
1293 if (dim->ids) {
1294 dim = extend_ids(dim);
1295 if (!dim)
1296 goto error;
1297 for (i = 0; i < num; ++i)
1298 isl_id_free(get_id(dim, type, first + i));
1299 for (i = first+num; i < n(dim, type); ++i)
1300 set_id(dim, type, i - num, get_id(dim, type, i));
1301 switch (type) {
1302 case isl_dim_param:
1303 get_ids(dim, isl_dim_in, 0, dim->n_in,
1304 dim->ids + offset(dim, isl_dim_in) - num);
1305 case isl_dim_in:
1306 get_ids(dim, isl_dim_out, 0, dim->n_out,
1307 dim->ids + offset(dim, isl_dim_out) - num);
1308 default:
1311 dim->n_id -= num;
1313 switch (type) {
1314 case isl_dim_param: dim->nparam -= num; break;
1315 case isl_dim_in: dim->n_in -= num; break;
1316 case isl_dim_out: dim->n_out -= num; break;
1317 default: ;
1319 dim = isl_space_reset(dim, type);
1320 if (type == isl_dim_param) {
1321 if (dim && dim->nested[0] &&
1322 !(dim->nested[0] = isl_space_drop_dims(dim->nested[0],
1323 isl_dim_param, first, num)))
1324 goto error;
1325 if (dim && dim->nested[1] &&
1326 !(dim->nested[1] = isl_space_drop_dims(dim->nested[1],
1327 isl_dim_param, first, num)))
1328 goto error;
1330 return dim;
1331 error:
1332 isl_space_free(dim);
1333 return NULL;
1336 __isl_give isl_space *isl_space_drop_inputs(__isl_take isl_space *dim,
1337 unsigned first, unsigned n)
1339 if (!dim)
1340 return NULL;
1341 return isl_space_drop_dims(dim, isl_dim_in, first, n);
1344 __isl_give isl_space *isl_space_drop_outputs(__isl_take isl_space *dim,
1345 unsigned first, unsigned n)
1347 if (!dim)
1348 return NULL;
1349 return isl_space_drop_dims(dim, isl_dim_out, first, n);
1352 __isl_give isl_space *isl_space_domain(__isl_take isl_space *dim)
1354 if (!dim)
1355 return NULL;
1356 dim = isl_space_drop_outputs(dim, 0, dim->n_out);
1357 dim = isl_space_reverse(dim);
1358 dim = mark_as_set(dim);
1359 return dim;
1362 __isl_give isl_space *isl_space_from_domain(__isl_take isl_space *dim)
1364 if (!dim)
1365 return NULL;
1366 if (!isl_space_is_set(dim))
1367 isl_die(isl_space_get_ctx(dim), isl_error_invalid,
1368 "not a set space", goto error);
1369 dim = isl_space_reverse(dim);
1370 dim = isl_space_reset(dim, isl_dim_out);
1371 return dim;
1372 error:
1373 isl_space_free(dim);
1374 return NULL;
1377 __isl_give isl_space *isl_space_range(__isl_take isl_space *dim)
1379 if (!dim)
1380 return NULL;
1381 dim = isl_space_drop_inputs(dim, 0, dim->n_in);
1382 dim = mark_as_set(dim);
1383 return dim;
1386 __isl_give isl_space *isl_space_from_range(__isl_take isl_space *dim)
1388 if (!dim)
1389 return NULL;
1390 if (!isl_space_is_set(dim))
1391 isl_die(isl_space_get_ctx(dim), isl_error_invalid,
1392 "not a set space", goto error);
1393 return isl_space_reset(dim, isl_dim_in);
1394 error:
1395 isl_space_free(dim);
1396 return NULL;
1399 __isl_give isl_space *isl_space_params(__isl_take isl_space *space)
1401 if (isl_space_is_params(space))
1402 return space;
1403 space = isl_space_drop_dims(space,
1404 isl_dim_in, 0, isl_space_dim(space, isl_dim_in));
1405 space = isl_space_drop_dims(space,
1406 isl_dim_out, 0, isl_space_dim(space, isl_dim_out));
1407 space = mark_as_params(space);
1408 return space;
1411 __isl_give isl_space *isl_space_set_from_params(__isl_take isl_space *space)
1413 if (!space)
1414 return NULL;
1415 if (!isl_space_is_params(space))
1416 isl_die(isl_space_get_ctx(space), isl_error_invalid,
1417 "not a parameter space", goto error);
1418 return isl_space_reset(space, isl_dim_set);
1419 error:
1420 isl_space_free(space);
1421 return NULL;
1424 __isl_give isl_space *isl_space_as_set_space(__isl_take isl_space *dim)
1426 dim = isl_space_cow(dim);
1427 if (!dim)
1428 return NULL;
1430 dim->n_out += dim->n_in;
1431 dim->n_in = 0;
1432 dim = isl_space_reset(dim, isl_dim_in);
1433 dim = isl_space_reset(dim, isl_dim_out);
1435 return dim;
1438 __isl_give isl_space *isl_space_underlying(__isl_take isl_space *dim,
1439 unsigned n_div)
1441 int i;
1443 if (!dim)
1444 return NULL;
1445 if (n_div == 0 &&
1446 dim->nparam == 0 && dim->n_in == 0 && dim->n_id == 0)
1447 return isl_space_reset(isl_space_reset(dim, isl_dim_in), isl_dim_out);
1448 dim = isl_space_cow(dim);
1449 if (!dim)
1450 return NULL;
1451 dim->n_out += dim->nparam + dim->n_in + n_div;
1452 dim->nparam = 0;
1453 dim->n_in = 0;
1455 for (i = 0; i < dim->n_id; ++i)
1456 isl_id_free(get_id(dim, isl_dim_out, i));
1457 dim->n_id = 0;
1458 dim = isl_space_reset(dim, isl_dim_in);
1459 dim = isl_space_reset(dim, isl_dim_out);
1461 return dim;
1464 /* Are the two spaces the same, including positions and names of parameters?
1466 int isl_space_is_equal(__isl_keep isl_space *dim1, __isl_keep isl_space *dim2)
1468 if (!dim1 || !dim2)
1469 return -1;
1470 if (dim1 == dim2)
1471 return 1;
1472 return match(dim1, isl_dim_param, dim2, isl_dim_param) &&
1473 isl_space_tuple_match(dim1, isl_dim_in, dim2, isl_dim_in) &&
1474 isl_space_tuple_match(dim1, isl_dim_out, dim2, isl_dim_out);
1477 /* Is space1 equal to the domain of space2?
1479 * In the internal version we also allow space2 to be the space of a set,
1480 * provided space1 is a parameter space.
1482 int isl_space_is_domain_internal(__isl_keep isl_space *space1,
1483 __isl_keep isl_space *space2)
1485 if (!space1 || !space2)
1486 return -1;
1487 if (!isl_space_is_set(space1))
1488 return 0;
1489 return match(space1, isl_dim_param, space2, isl_dim_param) &&
1490 isl_space_tuple_match(space1, isl_dim_set, space2, isl_dim_in);
1493 /* Is space1 equal to the domain of space2?
1495 int isl_space_is_domain(__isl_keep isl_space *space1,
1496 __isl_keep isl_space *space2)
1498 if (!space2)
1499 return -1;
1500 if (!isl_space_is_map(space2))
1501 return 0;
1502 return isl_space_is_domain_internal(space1, space2);
1505 /* Is space1 equal to the range of space2?
1507 * In the internal version, space2 is allowed to be the space of a set,
1508 * in which case it should be equal to space1.
1510 int isl_space_is_range_internal(__isl_keep isl_space *space1,
1511 __isl_keep isl_space *space2)
1513 if (!space1 || !space2)
1514 return -1;
1515 if (!isl_space_is_set(space1))
1516 return 0;
1517 return match(space1, isl_dim_param, space2, isl_dim_param) &&
1518 isl_space_tuple_match(space1, isl_dim_set, space2, isl_dim_out);
1521 /* Is space1 equal to the range of space2?
1523 int isl_space_is_range(__isl_keep isl_space *space1,
1524 __isl_keep isl_space *space2)
1526 if (!space2)
1527 return -1;
1528 if (!isl_space_is_map(space2))
1529 return 0;
1530 return isl_space_is_range_internal(space1, space2);
1533 int isl_space_compatible(__isl_keep isl_space *dim1,
1534 __isl_keep isl_space *dim2)
1536 return dim1->nparam == dim2->nparam &&
1537 dim1->n_in + dim1->n_out == dim2->n_in + dim2->n_out;
1540 static uint32_t isl_hash_dim(uint32_t hash, __isl_keep isl_space *dim)
1542 int i;
1543 isl_id *id;
1545 if (!dim)
1546 return hash;
1548 hash = isl_hash_builtin(hash, dim->nparam);
1549 hash = isl_hash_builtin(hash, dim->n_in);
1550 hash = isl_hash_builtin(hash, dim->n_out);
1552 for (i = 0; i < dim->nparam; ++i) {
1553 id = get_id(dim, isl_dim_param, i);
1554 hash = isl_hash_id(hash, id);
1557 id = tuple_id(dim, isl_dim_in);
1558 hash = isl_hash_id(hash, id);
1559 id = tuple_id(dim, isl_dim_out);
1560 hash = isl_hash_id(hash, id);
1562 hash = isl_hash_dim(hash, dim->nested[0]);
1563 hash = isl_hash_dim(hash, dim->nested[1]);
1565 return hash;
1568 uint32_t isl_space_get_hash(__isl_keep isl_space *dim)
1570 uint32_t hash;
1572 if (!dim)
1573 return 0;
1575 hash = isl_hash_init();
1576 hash = isl_hash_dim(hash, dim);
1578 return hash;
1581 int isl_space_is_wrapping(__isl_keep isl_space *dim)
1583 if (!dim)
1584 return -1;
1586 if (!isl_space_is_set(dim))
1587 return 0;
1589 return dim->nested[1] != NULL;
1592 __isl_give isl_space *isl_space_wrap(__isl_take isl_space *dim)
1594 isl_space *wrap;
1596 if (!dim)
1597 return NULL;
1599 wrap = isl_space_set_alloc(dim->ctx,
1600 dim->nparam, dim->n_in + dim->n_out);
1602 wrap = copy_ids(wrap, isl_dim_param, 0, dim, isl_dim_param);
1603 wrap = copy_ids(wrap, isl_dim_set, 0, dim, isl_dim_in);
1604 wrap = copy_ids(wrap, isl_dim_set, dim->n_in, dim, isl_dim_out);
1606 if (!wrap)
1607 goto error;
1609 wrap->nested[1] = dim;
1611 return wrap;
1612 error:
1613 isl_space_free(dim);
1614 return NULL;
1617 __isl_give isl_space *isl_space_unwrap(__isl_take isl_space *dim)
1619 isl_space *unwrap;
1621 if (!dim)
1622 return NULL;
1624 if (!isl_space_is_wrapping(dim))
1625 isl_die(dim->ctx, isl_error_invalid, "not a wrapping space",
1626 goto error);
1628 unwrap = isl_space_copy(dim->nested[1]);
1629 isl_space_free(dim);
1631 return unwrap;
1632 error:
1633 isl_space_free(dim);
1634 return NULL;
1637 int isl_space_is_named_or_nested(__isl_keep isl_space *dim, enum isl_dim_type type)
1639 if (type != isl_dim_in && type != isl_dim_out)
1640 return 0;
1641 if (!dim)
1642 return -1;
1643 if (dim->tuple_id[type - isl_dim_in])
1644 return 1;
1645 if (dim->nested[type - isl_dim_in])
1646 return 1;
1647 return 0;
1650 int isl_space_may_be_set(__isl_keep isl_space *dim)
1652 if (!dim)
1653 return -1;
1654 if (isl_space_is_set(dim))
1655 return 1;
1656 if (isl_space_dim(dim, isl_dim_in) != 0)
1657 return 0;
1658 if (isl_space_is_named_or_nested(dim, isl_dim_in))
1659 return 0;
1660 return 1;
1663 __isl_give isl_space *isl_space_reset(__isl_take isl_space *dim,
1664 enum isl_dim_type type)
1666 if (!isl_space_is_named_or_nested(dim, type))
1667 return dim;
1669 dim = isl_space_cow(dim);
1670 if (!dim)
1671 return NULL;
1673 isl_id_free(dim->tuple_id[type - isl_dim_in]);
1674 dim->tuple_id[type - isl_dim_in] = NULL;
1675 isl_space_free(dim->nested[type - isl_dim_in]);
1676 dim->nested[type - isl_dim_in] = NULL;
1678 return dim;
1681 __isl_give isl_space *isl_space_flatten(__isl_take isl_space *dim)
1683 if (!dim)
1684 return NULL;
1685 if (!dim->nested[0] && !dim->nested[1])
1686 return dim;
1688 if (dim->nested[0])
1689 dim = isl_space_reset(dim, isl_dim_in);
1690 if (dim && dim->nested[1])
1691 dim = isl_space_reset(dim, isl_dim_out);
1693 return dim;
1696 __isl_give isl_space *isl_space_flatten_domain(__isl_take isl_space *dim)
1698 if (!dim)
1699 return NULL;
1700 if (!dim->nested[0])
1701 return dim;
1703 return isl_space_reset(dim, isl_dim_in);
1706 __isl_give isl_space *isl_space_flatten_range(__isl_take isl_space *dim)
1708 if (!dim)
1709 return NULL;
1710 if (!dim->nested[1])
1711 return dim;
1713 return isl_space_reset(dim, isl_dim_out);
1716 /* Replace the dimensions of the given type of dst by those of src.
1718 __isl_give isl_space *isl_space_replace(__isl_take isl_space *dst,
1719 enum isl_dim_type type, __isl_keep isl_space *src)
1721 dst = isl_space_cow(dst);
1723 if (!dst || !src)
1724 goto error;
1726 dst = isl_space_drop_dims(dst, type, 0, isl_space_dim(dst, type));
1727 dst = isl_space_add_dims(dst, type, isl_space_dim(src, type));
1728 dst = copy_ids(dst, type, 0, src, type);
1730 if (dst && type == isl_dim_param) {
1731 int i;
1732 for (i = 0; i <= 1; ++i) {
1733 if (!dst->nested[i])
1734 continue;
1735 dst->nested[i] = isl_space_replace(dst->nested[i],
1736 type, src);
1737 if (!dst->nested[i])
1738 goto error;
1742 return dst;
1743 error:
1744 isl_space_free(dst);
1745 return NULL;
1748 /* Given a dimension specification "dim" of a set, create a dimension
1749 * specification for the lift of the set. In particular, the result
1750 * is of the form [dim -> local[..]], with n_local variables in the
1751 * range of the wrapped map.
1753 __isl_give isl_space *isl_space_lift(__isl_take isl_space *dim, unsigned n_local)
1755 isl_space *local_dim;
1757 if (!dim)
1758 return NULL;
1760 local_dim = isl_space_dup(dim);
1761 local_dim = isl_space_drop_dims(local_dim, isl_dim_set, 0, dim->n_out);
1762 local_dim = isl_space_add_dims(local_dim, isl_dim_set, n_local);
1763 local_dim = isl_space_set_tuple_name(local_dim, isl_dim_set, "local");
1764 dim = isl_space_join(isl_space_from_domain(dim),
1765 isl_space_from_range(local_dim));
1766 dim = isl_space_wrap(dim);
1767 dim = isl_space_set_tuple_name(dim, isl_dim_set, "lifted");
1769 return dim;
1772 int isl_space_can_zip(__isl_keep isl_space *dim)
1774 if (!dim)
1775 return -1;
1777 return dim->nested[0] && dim->nested[1];
1780 __isl_give isl_space *isl_space_zip(__isl_take isl_space *dim)
1782 isl_space *dom, *ran;
1783 isl_space *dom_dom, *dom_ran, *ran_dom, *ran_ran;
1785 if (!isl_space_can_zip(dim))
1786 isl_die(dim->ctx, isl_error_invalid, "dim cannot be zipped",
1787 goto error);
1789 if (!dim)
1790 return NULL;
1791 dom = isl_space_unwrap(isl_space_domain(isl_space_copy(dim)));
1792 ran = isl_space_unwrap(isl_space_range(dim));
1793 dom_dom = isl_space_domain(isl_space_copy(dom));
1794 dom_ran = isl_space_range(dom);
1795 ran_dom = isl_space_domain(isl_space_copy(ran));
1796 ran_ran = isl_space_range(ran);
1797 dom = isl_space_join(isl_space_from_domain(dom_dom),
1798 isl_space_from_range(ran_dom));
1799 ran = isl_space_join(isl_space_from_domain(dom_ran),
1800 isl_space_from_range(ran_ran));
1801 return isl_space_join(isl_space_from_domain(isl_space_wrap(dom)),
1802 isl_space_from_range(isl_space_wrap(ran)));
1803 error:
1804 isl_space_free(dim);
1805 return NULL;
1808 /* Can we apply isl_space_curry to "space"?
1809 * That is, does it have a nested relation in its domain?
1811 int isl_space_can_curry(__isl_keep isl_space *space)
1813 if (!space)
1814 return -1;
1816 return !!space->nested[0];
1819 /* Given a space (A -> B) -> C, return the corresponding space
1820 * A -> (B -> C).
1822 __isl_give isl_space *isl_space_curry(__isl_take isl_space *space)
1824 isl_space *dom, *ran;
1825 isl_space *dom_dom, *dom_ran;
1827 if (!space)
1828 return NULL;
1830 if (!isl_space_can_curry(space))
1831 isl_die(space->ctx, isl_error_invalid,
1832 "space cannot be curried", goto error);
1834 dom = isl_space_unwrap(isl_space_domain(isl_space_copy(space)));
1835 ran = isl_space_range(space);
1836 dom_dom = isl_space_domain(isl_space_copy(dom));
1837 dom_ran = isl_space_range(dom);
1838 ran = isl_space_join(isl_space_from_domain(dom_ran),
1839 isl_space_from_range(ran));
1840 return isl_space_join(isl_space_from_domain(dom_dom),
1841 isl_space_from_range(isl_space_wrap(ran)));
1842 error:
1843 isl_space_free(space);
1844 return NULL;
1847 /* Can we apply isl_space_uncurry to "space"?
1848 * That is, does it have a nested relation in its range?
1850 int isl_space_can_uncurry(__isl_keep isl_space *space)
1852 if (!space)
1853 return -1;
1855 return !!space->nested[1];
1858 /* Given a space A -> (B -> C), return the corresponding space
1859 * (A -> B) -> C.
1861 __isl_give isl_space *isl_space_uncurry(__isl_take isl_space *space)
1863 isl_space *dom, *ran;
1864 isl_space *ran_dom, *ran_ran;
1866 if (!space)
1867 return NULL;
1869 if (!isl_space_can_uncurry(space))
1870 isl_die(space->ctx, isl_error_invalid,
1871 "space cannot be uncurried",
1872 return isl_space_free(space));
1874 dom = isl_space_domain(isl_space_copy(space));
1875 ran = isl_space_unwrap(isl_space_range(space));
1876 ran_dom = isl_space_domain(isl_space_copy(ran));
1877 ran_ran = isl_space_range(ran);
1878 dom = isl_space_join(isl_space_from_domain(dom),
1879 isl_space_from_range(ran_dom));
1880 return isl_space_join(isl_space_from_domain(isl_space_wrap(dom)),
1881 isl_space_from_range(ran_ran));
1884 int isl_space_has_named_params(__isl_keep isl_space *dim)
1886 int i;
1887 unsigned off;
1889 if (!dim)
1890 return -1;
1891 if (dim->nparam == 0)
1892 return 1;
1893 off = isl_space_offset(dim, isl_dim_param);
1894 if (off + dim->nparam > dim->n_id)
1895 return 0;
1896 for (i = 0; i < dim->nparam; ++i)
1897 if (!dim->ids[off + i])
1898 return 0;
1899 return 1;
1902 /* Align the initial parameters of dim1 to match the order in dim2.
1904 __isl_give isl_space *isl_space_align_params(__isl_take isl_space *dim1,
1905 __isl_take isl_space *dim2)
1907 isl_reordering *exp;
1909 if (!isl_space_has_named_params(dim1) || !isl_space_has_named_params(dim2))
1910 isl_die(isl_space_get_ctx(dim1), isl_error_invalid,
1911 "parameter alignment requires named parameters",
1912 goto error);
1914 dim2 = isl_space_params(dim2);
1915 exp = isl_parameter_alignment_reordering(dim1, dim2);
1916 exp = isl_reordering_extend_space(exp, dim1);
1917 isl_space_free(dim2);
1918 if (!exp)
1919 return NULL;
1920 dim1 = isl_space_copy(exp->dim);
1921 isl_reordering_free(exp);
1922 return dim1;
1923 error:
1924 isl_space_free(dim1);
1925 isl_space_free(dim2);
1926 return NULL;
1929 /* Given the space of set (domain), construct a space for a map
1930 * with as domain the given space and as range the range of "model".
1932 __isl_give isl_space *isl_space_extend_domain_with_range(
1933 __isl_take isl_space *space, __isl_take isl_space *model)
1935 if (!model)
1936 goto error;
1938 space = isl_space_from_domain(space);
1939 space = isl_space_add_dims(space, isl_dim_out,
1940 isl_space_dim(model, isl_dim_out));
1941 if (isl_space_has_tuple_id(model, isl_dim_out))
1942 space = isl_space_set_tuple_id(space, isl_dim_out,
1943 isl_space_get_tuple_id(model, isl_dim_out));
1944 if (!space)
1945 goto error;
1946 if (model->nested[1]) {
1947 isl_space *nested = isl_space_copy(model->nested[1]);
1948 int n_nested, n_space;
1949 nested = isl_space_align_params(nested, isl_space_copy(space));
1950 n_nested = isl_space_dim(nested, isl_dim_param);
1951 n_space = isl_space_dim(space, isl_dim_param);
1952 if (n_nested > n_space)
1953 nested = isl_space_drop_dims(nested, isl_dim_param,
1954 n_space, n_nested - n_space);
1955 if (!nested)
1956 goto error;
1957 space->nested[1] = nested;
1959 isl_space_free(model);
1960 return space;
1961 error:
1962 isl_space_free(model);
1963 isl_space_free(space);
1964 return NULL;