add isl_ast_expr_access
[isl.git] / isl_space.c
blob943b4c4a7e46bac42e90e159736697f7c889e810
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2014 Ecole Normale Superieure
6 * Use of this software is governed by the MIT license
8 * Written by Sven Verdoolaege, K.U.Leuven, Departement
9 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
10 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
11 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
12 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
15 #include <stdlib.h>
16 #include <string.h>
17 #include <isl_space_private.h>
18 #include <isl_id_private.h>
19 #include <isl_reordering.h>
21 isl_ctx *isl_space_get_ctx(__isl_keep isl_space *dim)
23 return dim ? dim->ctx : NULL;
26 __isl_give isl_space *isl_space_alloc(isl_ctx *ctx,
27 unsigned nparam, unsigned n_in, unsigned n_out)
29 isl_space *dim;
31 dim = isl_alloc_type(ctx, struct isl_space);
32 if (!dim)
33 return NULL;
35 dim->ctx = ctx;
36 isl_ctx_ref(ctx);
37 dim->ref = 1;
38 dim->nparam = nparam;
39 dim->n_in = n_in;
40 dim->n_out = n_out;
42 dim->tuple_id[0] = NULL;
43 dim->tuple_id[1] = NULL;
45 dim->nested[0] = NULL;
46 dim->nested[1] = NULL;
48 dim->n_id = 0;
49 dim->ids = NULL;
51 return dim;
54 /* Mark the space as being that of a set, by setting the domain tuple
55 * to isl_id_none.
57 static __isl_give isl_space *mark_as_set(__isl_take isl_space *space)
59 space = isl_space_cow(space);
60 if (!space)
61 return NULL;
62 space = isl_space_set_tuple_id(space, isl_dim_in, &isl_id_none);
63 return space;
66 /* Is the space that of a set?
68 int isl_space_is_set(__isl_keep isl_space *space)
70 if (!space)
71 return -1;
72 if (space->n_in != 0 || space->nested[0])
73 return 0;
74 if (space->tuple_id[0] != &isl_id_none)
75 return 0;
76 return 1;
79 /* Is the given space that of a map?
81 int isl_space_is_map(__isl_keep isl_space *space)
83 if (!space)
84 return -1;
85 return space->tuple_id[0] != &isl_id_none &&
86 space->tuple_id[1] != &isl_id_none;
89 __isl_give isl_space *isl_space_set_alloc(isl_ctx *ctx,
90 unsigned nparam, unsigned dim)
92 isl_space *space;
93 space = isl_space_alloc(ctx, nparam, 0, dim);
94 space = mark_as_set(space);
95 return space;
98 /* Mark the space as being that of a parameter domain, by setting
99 * both tuples to isl_id_none.
101 static __isl_give isl_space *mark_as_params(isl_space *space)
103 if (!space)
104 return NULL;
105 space = isl_space_set_tuple_id(space, isl_dim_in, &isl_id_none);
106 space = isl_space_set_tuple_id(space, isl_dim_out, &isl_id_none);
107 return space;
110 /* Is the space that of a parameter domain?
112 int isl_space_is_params(__isl_keep isl_space *space)
114 if (!space)
115 return -1;
116 if (space->n_in != 0 || space->nested[0] ||
117 space->n_out != 0 || space->nested[1])
118 return 0;
119 if (space->tuple_id[0] != &isl_id_none)
120 return 0;
121 if (space->tuple_id[1] != &isl_id_none)
122 return 0;
123 return 1;
126 /* Create a space for a parameter domain.
128 __isl_give isl_space *isl_space_params_alloc(isl_ctx *ctx, unsigned nparam)
130 isl_space *space;
131 space = isl_space_alloc(ctx, nparam, 0, 0);
132 space = mark_as_params(space);
133 return space;
136 static unsigned global_pos(__isl_keep isl_space *dim,
137 enum isl_dim_type type, unsigned pos)
139 struct isl_ctx *ctx = dim->ctx;
141 switch (type) {
142 case isl_dim_param:
143 isl_assert(ctx, pos < dim->nparam,
144 return isl_space_dim(dim, isl_dim_all));
145 return pos;
146 case isl_dim_in:
147 isl_assert(ctx, pos < dim->n_in,
148 return isl_space_dim(dim, isl_dim_all));
149 return pos + dim->nparam;
150 case isl_dim_out:
151 isl_assert(ctx, pos < dim->n_out,
152 return isl_space_dim(dim, isl_dim_all));
153 return pos + dim->nparam + dim->n_in;
154 default:
155 isl_assert(ctx, 0, return isl_space_dim(dim, isl_dim_all));
157 return isl_space_dim(dim, isl_dim_all);
160 /* Extend length of ids array to the total number of dimensions.
162 static __isl_give isl_space *extend_ids(__isl_take isl_space *dim)
164 isl_id **ids;
165 int i;
167 if (isl_space_dim(dim, isl_dim_all) <= dim->n_id)
168 return dim;
170 if (!dim->ids) {
171 dim->ids = isl_calloc_array(dim->ctx,
172 isl_id *, isl_space_dim(dim, isl_dim_all));
173 if (!dim->ids)
174 goto error;
175 } else {
176 ids = isl_realloc_array(dim->ctx, dim->ids,
177 isl_id *, isl_space_dim(dim, isl_dim_all));
178 if (!ids)
179 goto error;
180 dim->ids = ids;
181 for (i = dim->n_id; i < isl_space_dim(dim, isl_dim_all); ++i)
182 dim->ids[i] = NULL;
185 dim->n_id = isl_space_dim(dim, isl_dim_all);
187 return dim;
188 error:
189 isl_space_free(dim);
190 return NULL;
193 static __isl_give isl_space *set_id(__isl_take isl_space *dim,
194 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
196 dim = isl_space_cow(dim);
198 if (!dim)
199 goto error;
201 pos = global_pos(dim, type, pos);
202 if (pos == isl_space_dim(dim, isl_dim_all))
203 goto error;
205 if (pos >= dim->n_id) {
206 if (!id)
207 return dim;
208 dim = extend_ids(dim);
209 if (!dim)
210 goto error;
213 dim->ids[pos] = id;
215 return dim;
216 error:
217 isl_id_free(id);
218 isl_space_free(dim);
219 return NULL;
222 static __isl_keep isl_id *get_id(__isl_keep isl_space *dim,
223 enum isl_dim_type type, unsigned pos)
225 if (!dim)
226 return NULL;
228 pos = global_pos(dim, type, pos);
229 if (pos == isl_space_dim(dim, isl_dim_all))
230 return NULL;
231 if (pos >= dim->n_id)
232 return NULL;
233 return dim->ids[pos];
236 static unsigned offset(__isl_keep isl_space *dim, enum isl_dim_type type)
238 switch (type) {
239 case isl_dim_param: return 0;
240 case isl_dim_in: return dim->nparam;
241 case isl_dim_out: return dim->nparam + dim->n_in;
242 default: return 0;
246 static unsigned n(__isl_keep isl_space *dim, enum isl_dim_type type)
248 switch (type) {
249 case isl_dim_param: return dim->nparam;
250 case isl_dim_in: return dim->n_in;
251 case isl_dim_out: return dim->n_out;
252 case isl_dim_all: return dim->nparam + dim->n_in + dim->n_out;
253 default: return 0;
257 unsigned isl_space_dim(__isl_keep isl_space *dim, enum isl_dim_type type)
259 if (!dim)
260 return 0;
261 return n(dim, type);
264 unsigned isl_space_offset(__isl_keep isl_space *dim, enum isl_dim_type type)
266 if (!dim)
267 return 0;
268 return offset(dim, type);
271 static __isl_give isl_space *copy_ids(__isl_take isl_space *dst,
272 enum isl_dim_type dst_type, unsigned offset, __isl_keep isl_space *src,
273 enum isl_dim_type src_type)
275 int i;
276 isl_id *id;
278 if (!dst)
279 return NULL;
281 for (i = 0; i < n(src, src_type); ++i) {
282 id = get_id(src, src_type, i);
283 if (!id)
284 continue;
285 dst = set_id(dst, dst_type, offset + i, isl_id_copy(id));
286 if (!dst)
287 return NULL;
289 return dst;
292 __isl_take isl_space *isl_space_dup(__isl_keep isl_space *dim)
294 isl_space *dup;
295 if (!dim)
296 return NULL;
297 dup = isl_space_alloc(dim->ctx, dim->nparam, dim->n_in, dim->n_out);
298 if (!dup)
299 return NULL;
300 if (dim->tuple_id[0] &&
301 !(dup->tuple_id[0] = isl_id_copy(dim->tuple_id[0])))
302 goto error;
303 if (dim->tuple_id[1] &&
304 !(dup->tuple_id[1] = isl_id_copy(dim->tuple_id[1])))
305 goto error;
306 if (dim->nested[0] && !(dup->nested[0] = isl_space_copy(dim->nested[0])))
307 goto error;
308 if (dim->nested[1] && !(dup->nested[1] = isl_space_copy(dim->nested[1])))
309 goto error;
310 if (!dim->ids)
311 return dup;
312 dup = copy_ids(dup, isl_dim_param, 0, dim, isl_dim_param);
313 dup = copy_ids(dup, isl_dim_in, 0, dim, isl_dim_in);
314 dup = copy_ids(dup, isl_dim_out, 0, dim, isl_dim_out);
315 return dup;
316 error:
317 isl_space_free(dup);
318 return NULL;
321 __isl_give isl_space *isl_space_cow(__isl_take isl_space *dim)
323 if (!dim)
324 return NULL;
326 if (dim->ref == 1)
327 return dim;
328 dim->ref--;
329 return isl_space_dup(dim);
332 __isl_give isl_space *isl_space_copy(__isl_keep isl_space *dim)
334 if (!dim)
335 return NULL;
337 dim->ref++;
338 return dim;
341 void *isl_space_free(__isl_take isl_space *dim)
343 int i;
345 if (!dim)
346 return NULL;
348 if (--dim->ref > 0)
349 return NULL;
351 isl_id_free(dim->tuple_id[0]);
352 isl_id_free(dim->tuple_id[1]);
354 isl_space_free(dim->nested[0]);
355 isl_space_free(dim->nested[1]);
357 for (i = 0; i < dim->n_id; ++i)
358 isl_id_free(dim->ids[i]);
359 free(dim->ids);
360 isl_ctx_deref(dim->ctx);
362 free(dim);
364 return NULL;
367 /* Check if "s" is a valid dimension or tuple name.
368 * We currently only forbid names that look like a number.
370 * s is assumed to be non-NULL.
372 static int name_ok(isl_ctx *ctx, const char *s)
374 char *p;
375 long dummy;
377 dummy = strtol(s, &p, 0);
378 if (p != s)
379 isl_die(ctx, isl_error_invalid, "name looks like a number",
380 return 0);
382 return 1;
385 /* Is it possible for the given dimension type to have a tuple id?
387 static int space_can_have_id(__isl_keep isl_space *space,
388 enum isl_dim_type type)
390 if (!space)
391 return 0;
392 if (isl_space_is_params(space))
393 isl_die(space->ctx, isl_error_invalid,
394 "parameter spaces don't have tuple ids", return 0);
395 if (isl_space_is_set(space) && type != isl_dim_set)
396 isl_die(space->ctx, isl_error_invalid,
397 "set spaces can only have a set id", return 0);
398 if (type != isl_dim_in && type != isl_dim_out)
399 isl_die(space->ctx, isl_error_invalid,
400 "only input, output and set tuples can have ids",
401 return 0);
403 return 1;
406 /* Does the tuple have an id?
408 int isl_space_has_tuple_id(__isl_keep isl_space *dim, enum isl_dim_type type)
410 if (!space_can_have_id(dim, type))
411 return -1;
412 return dim->tuple_id[type - isl_dim_in] != NULL;
415 __isl_give isl_id *isl_space_get_tuple_id(__isl_keep isl_space *dim,
416 enum isl_dim_type type)
418 int has_id;
420 if (!dim)
421 return NULL;
422 has_id = isl_space_has_tuple_id(dim, type);
423 if (has_id < 0)
424 return NULL;
425 if (!has_id)
426 isl_die(dim->ctx, isl_error_invalid,
427 "tuple has no id", return NULL);
428 return isl_id_copy(dim->tuple_id[type - isl_dim_in]);
431 __isl_give isl_space *isl_space_set_tuple_id(__isl_take isl_space *dim,
432 enum isl_dim_type type, __isl_take isl_id *id)
434 dim = isl_space_cow(dim);
435 if (!dim || !id)
436 goto error;
437 if (type != isl_dim_in && type != isl_dim_out)
438 isl_die(dim->ctx, isl_error_invalid,
439 "only input, output and set tuples can have names",
440 goto error);
442 isl_id_free(dim->tuple_id[type - isl_dim_in]);
443 dim->tuple_id[type - isl_dim_in] = id;
445 return dim;
446 error:
447 isl_id_free(id);
448 isl_space_free(dim);
449 return NULL;
452 __isl_give isl_space *isl_space_reset_tuple_id(__isl_take isl_space *dim,
453 enum isl_dim_type type)
455 dim = isl_space_cow(dim);
456 if (!dim)
457 return NULL;
458 if (type != isl_dim_in && type != isl_dim_out)
459 isl_die(dim->ctx, isl_error_invalid,
460 "only input, output and set tuples can have names",
461 goto error);
463 isl_id_free(dim->tuple_id[type - isl_dim_in]);
464 dim->tuple_id[type - isl_dim_in] = NULL;
466 return dim;
467 error:
468 isl_space_free(dim);
469 return NULL;
472 /* Set the id of the given dimension of "space" to "id".
473 * If the dimension already has an id, then it is replaced.
474 * If the dimension is a parameter, then we need to change it
475 * in the nested spaces (if any) as well.
477 __isl_give isl_space *isl_space_set_dim_id(__isl_take isl_space *space,
478 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
480 space = isl_space_cow(space);
481 if (!space || !id)
482 goto error;
484 if (type == isl_dim_param) {
485 int i;
487 for (i = 0; i < 2; ++i) {
488 if (!space->nested[i])
489 continue;
490 space->nested[i] =
491 isl_space_set_dim_id(space->nested[i],
492 type, pos, isl_id_copy(id));
493 if (!space->nested[i])
494 goto error;
498 isl_id_free(get_id(space, type, pos));
499 return set_id(space, type, pos, id);
500 error:
501 isl_id_free(id);
502 isl_space_free(space);
503 return NULL;
506 /* Reset the id of the given dimension of "space".
507 * If the dimension already has an id, then it is removed.
508 * If the dimension is a parameter, then we need to reset it
509 * in the nested spaces (if any) as well.
511 __isl_give isl_space *isl_space_reset_dim_id(__isl_take isl_space *space,
512 enum isl_dim_type type, unsigned pos)
514 space = isl_space_cow(space);
515 if (!space)
516 goto error;
518 if (type == isl_dim_param) {
519 int i;
521 for (i = 0; i < 2; ++i) {
522 if (!space->nested[i])
523 continue;
524 space->nested[i] =
525 isl_space_reset_dim_id(space->nested[i],
526 type, pos);
527 if (!space->nested[i])
528 goto error;
532 isl_id_free(get_id(space, type, pos));
533 return set_id(space, type, pos, NULL);
534 error:
535 isl_space_free(space);
536 return NULL;
539 int isl_space_has_dim_id(__isl_keep isl_space *dim,
540 enum isl_dim_type type, unsigned pos)
542 if (!dim)
543 return -1;
544 return get_id(dim, type, pos) != NULL;
547 __isl_give isl_id *isl_space_get_dim_id(__isl_keep isl_space *dim,
548 enum isl_dim_type type, unsigned pos)
550 if (!dim)
551 return NULL;
552 if (!get_id(dim, type, pos))
553 isl_die(dim->ctx, isl_error_invalid,
554 "dim has no id", return NULL);
555 return isl_id_copy(get_id(dim, type, pos));
558 __isl_give isl_space *isl_space_set_tuple_name(__isl_take isl_space *dim,
559 enum isl_dim_type type, const char *s)
561 isl_id *id;
563 if (!dim)
564 return NULL;
566 if (!s)
567 return isl_space_reset_tuple_id(dim, type);
569 if (!name_ok(dim->ctx, s))
570 goto error;
572 id = isl_id_alloc(dim->ctx, s, NULL);
573 return isl_space_set_tuple_id(dim, type, id);
574 error:
575 isl_space_free(dim);
576 return NULL;
579 /* Does the tuple have a name?
581 int isl_space_has_tuple_name(__isl_keep isl_space *space,
582 enum isl_dim_type type)
584 isl_id *id;
586 if (!space_can_have_id(space, type))
587 return -1;
588 id = space->tuple_id[type - isl_dim_in];
589 return id && id->name;
592 const char *isl_space_get_tuple_name(__isl_keep isl_space *dim,
593 enum isl_dim_type type)
595 isl_id *id;
596 if (!dim)
597 return NULL;
598 if (type != isl_dim_in && type != isl_dim_out)
599 return NULL;
600 id = dim->tuple_id[type - isl_dim_in];
601 return id ? id->name : NULL;
604 __isl_give isl_space *isl_space_set_dim_name(__isl_take isl_space *dim,
605 enum isl_dim_type type, unsigned pos,
606 const char *s)
608 isl_id *id;
610 if (!dim)
611 return NULL;
612 if (!s)
613 return isl_space_reset_dim_id(dim, type, pos);
614 if (!name_ok(dim->ctx, s))
615 goto error;
616 id = isl_id_alloc(dim->ctx, s, NULL);
617 return isl_space_set_dim_id(dim, type, pos, id);
618 error:
619 isl_space_free(dim);
620 return NULL;
623 /* Does the given dimension have a name?
625 int isl_space_has_dim_name(__isl_keep isl_space *space,
626 enum isl_dim_type type, unsigned pos)
628 isl_id *id;
630 if (!space)
631 return -1;
632 id = get_id(space, type, pos);
633 return id && id->name;
636 __isl_keep const char *isl_space_get_dim_name(__isl_keep isl_space *dim,
637 enum isl_dim_type type, unsigned pos)
639 isl_id *id = get_id(dim, type, pos);
640 return id ? id->name : NULL;
643 int isl_space_find_dim_by_id(__isl_keep isl_space *dim, enum isl_dim_type type,
644 __isl_keep isl_id *id)
646 int i;
647 int offset;
648 int n;
650 if (!dim || !id)
651 return -1;
653 offset = isl_space_offset(dim, type);
654 n = isl_space_dim(dim, type);
655 for (i = 0; i < n && offset + i < dim->n_id; ++i)
656 if (dim->ids[offset + i] == id)
657 return i;
659 return -1;
662 int isl_space_find_dim_by_name(__isl_keep isl_space *space,
663 enum isl_dim_type type, const char *name)
665 int i;
666 int offset;
667 int n;
669 if (!space || !name)
670 return -1;
672 offset = isl_space_offset(space, type);
673 n = isl_space_dim(space, type);
674 for (i = 0; i < n && offset + i < space->n_id; ++i)
675 if (space->ids[offset + i]->name &&
676 !strcmp(space->ids[offset + i]->name, name))
677 return i;
679 return -1;
682 static __isl_keep isl_id *tuple_id(__isl_keep isl_space *dim,
683 enum isl_dim_type type)
685 if (!dim)
686 return NULL;
687 if (type == isl_dim_in)
688 return dim->tuple_id[0];
689 if (type == isl_dim_out)
690 return dim->tuple_id[1];
691 return NULL;
694 static __isl_keep isl_space *nested(__isl_keep isl_space *dim,
695 enum isl_dim_type type)
697 if (!dim)
698 return NULL;
699 if (type == isl_dim_in)
700 return dim->nested[0];
701 if (type == isl_dim_out)
702 return dim->nested[1];
703 return NULL;
706 int isl_space_tuple_match(__isl_keep isl_space *dim1, enum isl_dim_type dim1_type,
707 __isl_keep isl_space *dim2, enum isl_dim_type dim2_type)
709 isl_id *id1, *id2;
710 isl_space *nested1, *nested2;
712 if (!dim1 || !dim2)
713 return -1;
715 if (dim1 == dim2 && dim1_type == dim2_type)
716 return 1;
718 if (n(dim1, dim1_type) != n(dim2, dim2_type))
719 return 0;
720 id1 = tuple_id(dim1, dim1_type);
721 id2 = tuple_id(dim2, dim2_type);
722 if (!id1 ^ !id2)
723 return 0;
724 if (id1 && id1 != id2)
725 return 0;
726 nested1 = nested(dim1, dim1_type);
727 nested2 = nested(dim2, dim2_type);
728 if (!nested1 ^ !nested2)
729 return 0;
730 if (nested1 && !isl_space_is_equal(nested1, nested2))
731 return 0;
732 return 1;
735 static int match(__isl_keep isl_space *dim1, enum isl_dim_type dim1_type,
736 __isl_keep isl_space *dim2, enum isl_dim_type dim2_type)
738 int i;
740 if (dim1 == dim2 && dim1_type == dim2_type)
741 return 1;
743 if (!isl_space_tuple_match(dim1, dim1_type, dim2, dim2_type))
744 return 0;
746 if (!dim1->ids && !dim2->ids)
747 return 1;
749 for (i = 0; i < n(dim1, dim1_type); ++i) {
750 if (get_id(dim1, dim1_type, i) != get_id(dim2, dim2_type, i))
751 return 0;
753 return 1;
756 int isl_space_match(__isl_keep isl_space *dim1, enum isl_dim_type dim1_type,
757 __isl_keep isl_space *dim2, enum isl_dim_type dim2_type)
759 if (!dim1 || !dim2)
760 return -1;
762 return match(dim1, dim1_type, dim2, dim2_type);
765 static void get_ids(__isl_keep isl_space *dim, enum isl_dim_type type,
766 unsigned first, unsigned n, __isl_keep isl_id **ids)
768 int i;
770 for (i = 0; i < n ; ++i)
771 ids[i] = get_id(dim, type, first + i);
774 __isl_give isl_space *isl_space_extend(__isl_take isl_space *dim,
775 unsigned nparam, unsigned n_in, unsigned n_out)
777 isl_id **ids = NULL;
779 if (!dim)
780 return NULL;
781 if (dim->nparam == nparam && dim->n_in == n_in && dim->n_out == n_out)
782 return dim;
784 isl_assert(dim->ctx, dim->nparam <= nparam, goto error);
785 isl_assert(dim->ctx, dim->n_in <= n_in, goto error);
786 isl_assert(dim->ctx, dim->n_out <= n_out, goto error);
788 dim = isl_space_cow(dim);
789 if (!dim)
790 goto error;
792 if (dim->ids) {
793 ids = isl_calloc_array(dim->ctx, isl_id *,
794 nparam + n_in + n_out);
795 if (!ids)
796 goto error;
797 get_ids(dim, isl_dim_param, 0, dim->nparam, ids);
798 get_ids(dim, isl_dim_in, 0, dim->n_in, ids + nparam);
799 get_ids(dim, isl_dim_out, 0, dim->n_out, ids + nparam + n_in);
800 free(dim->ids);
801 dim->ids = ids;
802 dim->n_id = nparam + n_in + n_out;
804 dim->nparam = nparam;
805 dim->n_in = n_in;
806 dim->n_out = n_out;
808 return dim;
809 error:
810 free(ids);
811 isl_space_free(dim);
812 return NULL;
815 __isl_give isl_space *isl_space_add_dims(__isl_take isl_space *dim,
816 enum isl_dim_type type, unsigned n)
818 if (!dim)
819 return NULL;
820 dim = isl_space_reset(dim, type);
821 switch (type) {
822 case isl_dim_param:
823 dim = isl_space_extend(dim,
824 dim->nparam + n, dim->n_in, dim->n_out);
825 if (dim && dim->nested[0] &&
826 !(dim->nested[0] = isl_space_add_dims(dim->nested[0],
827 isl_dim_param, n)))
828 goto error;
829 if (dim && dim->nested[1] &&
830 !(dim->nested[1] = isl_space_add_dims(dim->nested[1],
831 isl_dim_param, n)))
832 goto error;
833 return dim;
834 case isl_dim_in:
835 return isl_space_extend(dim,
836 dim->nparam, dim->n_in + n, dim->n_out);
837 case isl_dim_out:
838 return isl_space_extend(dim,
839 dim->nparam, dim->n_in, dim->n_out + n);
840 default:
841 isl_die(dim->ctx, isl_error_invalid,
842 "cannot add dimensions of specified type", goto error);
844 error:
845 isl_space_free(dim);
846 return NULL;
849 static int valid_dim_type(enum isl_dim_type type)
851 switch (type) {
852 case isl_dim_param:
853 case isl_dim_in:
854 case isl_dim_out:
855 return 1;
856 default:
857 return 0;
861 /* Insert "n" dimensions of type "type" at position "pos".
862 * If we are inserting parameters, then they are also inserted in
863 * any nested spaces.
865 __isl_give isl_space *isl_space_insert_dims(__isl_take isl_space *dim,
866 enum isl_dim_type type, unsigned pos, unsigned n)
868 isl_id **ids = NULL;
870 if (!dim)
871 return NULL;
872 if (n == 0)
873 return isl_space_reset(dim, type);
875 if (!valid_dim_type(type))
876 isl_die(dim->ctx, isl_error_invalid,
877 "cannot insert dimensions of specified type",
878 goto error);
880 isl_assert(dim->ctx, pos <= isl_space_dim(dim, type), goto error);
882 dim = isl_space_cow(dim);
883 if (!dim)
884 return NULL;
886 if (dim->ids) {
887 enum isl_dim_type t, o = isl_dim_param;
888 int off;
889 int s[3];
890 ids = isl_calloc_array(dim->ctx, isl_id *,
891 dim->nparam + dim->n_in + dim->n_out + n);
892 if (!ids)
893 goto error;
894 off = 0;
895 s[isl_dim_param - o] = dim->nparam;
896 s[isl_dim_in - o] = dim->n_in;
897 s[isl_dim_out - o] = dim->n_out;
898 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
899 if (t != type) {
900 get_ids(dim, t, 0, s[t - o], ids + off);
901 off += s[t - o];
902 } else {
903 get_ids(dim, t, 0, pos, ids + off);
904 off += pos + n;
905 get_ids(dim, t, pos, s[t - o] - pos, ids + off);
906 off += s[t - o] - pos;
909 free(dim->ids);
910 dim->ids = ids;
911 dim->n_id = dim->nparam + dim->n_in + dim->n_out + n;
913 switch (type) {
914 case isl_dim_param: dim->nparam += n; break;
915 case isl_dim_in: dim->n_in += n; break;
916 case isl_dim_out: dim->n_out += n; break;
917 default: ;
919 dim = isl_space_reset(dim, type);
921 if (type == isl_dim_param) {
922 if (dim && dim->nested[0] &&
923 !(dim->nested[0] = isl_space_insert_dims(dim->nested[0],
924 isl_dim_param, pos, n)))
925 goto error;
926 if (dim && dim->nested[1] &&
927 !(dim->nested[1] = isl_space_insert_dims(dim->nested[1],
928 isl_dim_param, pos, n)))
929 goto error;
932 return dim;
933 error:
934 isl_space_free(dim);
935 return NULL;
938 __isl_give isl_space *isl_space_move_dims(__isl_take isl_space *dim,
939 enum isl_dim_type dst_type, unsigned dst_pos,
940 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
942 int i;
944 if (!dim)
945 return NULL;
946 if (n == 0) {
947 dim = isl_space_reset(dim, src_type);
948 return isl_space_reset(dim, dst_type);
951 isl_assert(dim->ctx, src_pos + n <= isl_space_dim(dim, src_type),
952 goto error);
954 if (dst_type == src_type && dst_pos == src_pos)
955 return dim;
957 isl_assert(dim->ctx, dst_type != src_type, goto error);
959 dim = isl_space_reset(dim, src_type);
960 dim = isl_space_reset(dim, dst_type);
962 dim = isl_space_cow(dim);
963 if (!dim)
964 return NULL;
966 if (dim->ids) {
967 isl_id **ids;
968 enum isl_dim_type t, o = isl_dim_param;
969 int off;
970 int s[3];
971 ids = isl_calloc_array(dim->ctx, isl_id *,
972 dim->nparam + dim->n_in + dim->n_out);
973 if (!ids)
974 goto error;
975 off = 0;
976 s[isl_dim_param - o] = dim->nparam;
977 s[isl_dim_in - o] = dim->n_in;
978 s[isl_dim_out - o] = dim->n_out;
979 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
980 if (t == dst_type) {
981 get_ids(dim, t, 0, dst_pos, ids + off);
982 off += dst_pos;
983 get_ids(dim, src_type, src_pos, n, ids + off);
984 off += n;
985 get_ids(dim, t, dst_pos, s[t - o] - dst_pos,
986 ids + off);
987 off += s[t - o] - dst_pos;
988 } else if (t == src_type) {
989 get_ids(dim, t, 0, src_pos, ids + off);
990 off += src_pos;
991 get_ids(dim, t, src_pos + n,
992 s[t - o] - src_pos - n, ids + off);
993 off += s[t - o] - src_pos - n;
994 } else {
995 get_ids(dim, t, 0, s[t - o], ids + off);
996 off += s[t - o];
999 free(dim->ids);
1000 dim->ids = ids;
1001 dim->n_id = dim->nparam + dim->n_in + dim->n_out;
1004 switch (dst_type) {
1005 case isl_dim_param: dim->nparam += n; break;
1006 case isl_dim_in: dim->n_in += n; break;
1007 case isl_dim_out: dim->n_out += n; break;
1008 default: ;
1011 switch (src_type) {
1012 case isl_dim_param: dim->nparam -= n; break;
1013 case isl_dim_in: dim->n_in -= n; break;
1014 case isl_dim_out: dim->n_out -= n; break;
1015 default: ;
1018 if (dst_type != isl_dim_param && src_type != isl_dim_param)
1019 return dim;
1021 for (i = 0; i < 2; ++i) {
1022 if (!dim->nested[i])
1023 continue;
1024 dim->nested[i] = isl_space_replace(dim->nested[i],
1025 isl_dim_param, dim);
1026 if (!dim->nested[i])
1027 goto error;
1030 return dim;
1031 error:
1032 isl_space_free(dim);
1033 return NULL;
1036 __isl_give isl_space *isl_space_join(__isl_take isl_space *left,
1037 __isl_take isl_space *right)
1039 isl_space *dim;
1041 if (!left || !right)
1042 goto error;
1044 isl_assert(left->ctx, match(left, isl_dim_param, right, isl_dim_param),
1045 goto error);
1046 isl_assert(left->ctx,
1047 isl_space_tuple_match(left, isl_dim_out, right, isl_dim_in),
1048 goto error);
1050 dim = isl_space_alloc(left->ctx, left->nparam, left->n_in, right->n_out);
1051 if (!dim)
1052 goto error;
1054 dim = copy_ids(dim, isl_dim_param, 0, left, isl_dim_param);
1055 dim = copy_ids(dim, isl_dim_in, 0, left, isl_dim_in);
1056 dim = copy_ids(dim, isl_dim_out, 0, right, isl_dim_out);
1058 if (dim && left->tuple_id[0] &&
1059 !(dim->tuple_id[0] = isl_id_copy(left->tuple_id[0])))
1060 goto error;
1061 if (dim && right->tuple_id[1] &&
1062 !(dim->tuple_id[1] = isl_id_copy(right->tuple_id[1])))
1063 goto error;
1064 if (dim && left->nested[0] &&
1065 !(dim->nested[0] = isl_space_copy(left->nested[0])))
1066 goto error;
1067 if (dim && right->nested[1] &&
1068 !(dim->nested[1] = isl_space_copy(right->nested[1])))
1069 goto error;
1071 isl_space_free(left);
1072 isl_space_free(right);
1074 return dim;
1075 error:
1076 isl_space_free(left);
1077 isl_space_free(right);
1078 return NULL;
1081 __isl_give isl_space *isl_space_product(__isl_take isl_space *left,
1082 __isl_take isl_space *right)
1084 isl_space *dom1, *dom2, *nest1, *nest2;
1086 if (!left || !right)
1087 goto error;
1089 isl_assert(left->ctx, match(left, isl_dim_param, right, isl_dim_param),
1090 goto error);
1092 dom1 = isl_space_domain(isl_space_copy(left));
1093 dom2 = isl_space_domain(isl_space_copy(right));
1094 nest1 = isl_space_wrap(isl_space_join(isl_space_reverse(dom1), dom2));
1096 dom1 = isl_space_range(left);
1097 dom2 = isl_space_range(right);
1098 nest2 = isl_space_wrap(isl_space_join(isl_space_reverse(dom1), dom2));
1100 return isl_space_join(isl_space_reverse(nest1), nest2);
1101 error:
1102 isl_space_free(left);
1103 isl_space_free(right);
1104 return NULL;
1107 /* Given two spaces { A -> C } and { B -> C }, construct the space
1108 * { [A -> B] -> C }
1110 __isl_give isl_space *isl_space_domain_product(__isl_take isl_space *left,
1111 __isl_take isl_space *right)
1113 isl_space *ran, *dom1, *dom2, *nest;
1115 if (!left || !right)
1116 goto error;
1118 if (!match(left, isl_dim_param, right, isl_dim_param))
1119 isl_die(left->ctx, isl_error_invalid,
1120 "parameters need to match", goto error);
1121 if (!isl_space_tuple_match(left, isl_dim_out, right, isl_dim_out))
1122 isl_die(left->ctx, isl_error_invalid,
1123 "ranges need to match", goto error);
1125 ran = isl_space_range(isl_space_copy(left));
1127 dom1 = isl_space_domain(left);
1128 dom2 = isl_space_domain(right);
1129 nest = isl_space_wrap(isl_space_join(isl_space_reverse(dom1), dom2));
1131 return isl_space_join(isl_space_reverse(nest), ran);
1132 error:
1133 isl_space_free(left);
1134 isl_space_free(right);
1135 return NULL;
1138 __isl_give isl_space *isl_space_range_product(__isl_take isl_space *left,
1139 __isl_take isl_space *right)
1141 isl_space *dom, *ran1, *ran2, *nest;
1143 if (!left || !right)
1144 goto error;
1146 isl_assert(left->ctx, match(left, isl_dim_param, right, isl_dim_param),
1147 goto error);
1148 if (!isl_space_tuple_match(left, isl_dim_in, right, isl_dim_in))
1149 isl_die(left->ctx, isl_error_invalid,
1150 "domains need to match", goto error);
1152 dom = isl_space_domain(isl_space_copy(left));
1154 ran1 = isl_space_range(left);
1155 ran2 = isl_space_range(right);
1156 nest = isl_space_wrap(isl_space_join(isl_space_reverse(ran1), ran2));
1158 return isl_space_join(isl_space_reverse(dom), nest);
1159 error:
1160 isl_space_free(left);
1161 isl_space_free(right);
1162 return NULL;
1165 __isl_give isl_space *isl_space_map_from_set(__isl_take isl_space *dim)
1167 isl_ctx *ctx;
1168 isl_id **ids = NULL;
1170 if (!dim)
1171 return NULL;
1172 ctx = isl_space_get_ctx(dim);
1173 if (!isl_space_is_set(dim))
1174 isl_die(ctx, isl_error_invalid, "not a set space", goto error);
1175 dim = isl_space_cow(dim);
1176 if (!dim)
1177 return NULL;
1178 if (dim->ids) {
1179 ids = isl_calloc_array(dim->ctx, isl_id *,
1180 dim->nparam + dim->n_out + dim->n_out);
1181 if (!ids)
1182 goto error;
1183 get_ids(dim, isl_dim_param, 0, dim->nparam, ids);
1184 get_ids(dim, isl_dim_out, 0, dim->n_out, ids + dim->nparam);
1186 dim->n_in = dim->n_out;
1187 if (ids) {
1188 free(dim->ids);
1189 dim->ids = ids;
1190 dim->n_id = dim->nparam + dim->n_out + dim->n_out;
1191 dim = copy_ids(dim, isl_dim_out, 0, dim, isl_dim_in);
1193 isl_id_free(dim->tuple_id[0]);
1194 dim->tuple_id[0] = isl_id_copy(dim->tuple_id[1]);
1195 isl_space_free(dim->nested[0]);
1196 dim->nested[0] = isl_space_copy(dim->nested[1]);
1197 return dim;
1198 error:
1199 isl_space_free(dim);
1200 return NULL;
1203 __isl_give isl_space *isl_space_map_from_domain_and_range(
1204 __isl_take isl_space *domain, __isl_take isl_space *range)
1206 if (!domain || !range)
1207 goto error;
1208 if (!isl_space_is_set(domain))
1209 isl_die(isl_space_get_ctx(domain), isl_error_invalid,
1210 "domain is not a set space", goto error);
1211 if (!isl_space_is_set(range))
1212 isl_die(isl_space_get_ctx(range), isl_error_invalid,
1213 "range is not a set space", goto error);
1214 return isl_space_join(isl_space_reverse(domain), range);
1215 error:
1216 isl_space_free(domain);
1217 isl_space_free(range);
1218 return NULL;
1221 static __isl_give isl_space *set_ids(__isl_take isl_space *dim,
1222 enum isl_dim_type type,
1223 unsigned first, unsigned n, __isl_take isl_id **ids)
1225 int i;
1227 for (i = 0; i < n ; ++i)
1228 dim = set_id(dim, type, first + i, ids[i]);
1230 return dim;
1233 __isl_give isl_space *isl_space_reverse(__isl_take isl_space *dim)
1235 unsigned t;
1236 isl_space *nested;
1237 isl_id **ids = NULL;
1238 isl_id *id;
1240 if (!dim)
1241 return NULL;
1242 if (match(dim, isl_dim_in, dim, isl_dim_out))
1243 return dim;
1245 dim = isl_space_cow(dim);
1246 if (!dim)
1247 return NULL;
1249 id = dim->tuple_id[0];
1250 dim->tuple_id[0] = dim->tuple_id[1];
1251 dim->tuple_id[1] = id;
1253 nested = dim->nested[0];
1254 dim->nested[0] = dim->nested[1];
1255 dim->nested[1] = nested;
1257 if (dim->ids) {
1258 int n_id = dim->n_in + dim->n_out;
1259 ids = isl_alloc_array(dim->ctx, isl_id *, n_id);
1260 if (n_id && !ids)
1261 goto error;
1262 get_ids(dim, isl_dim_in, 0, dim->n_in, ids);
1263 get_ids(dim, isl_dim_out, 0, dim->n_out, ids + dim->n_in);
1266 t = dim->n_in;
1267 dim->n_in = dim->n_out;
1268 dim->n_out = t;
1270 if (dim->ids) {
1271 dim = set_ids(dim, isl_dim_out, 0, dim->n_out, ids);
1272 dim = set_ids(dim, isl_dim_in, 0, dim->n_in, ids + dim->n_out);
1273 free(ids);
1276 return dim;
1277 error:
1278 free(ids);
1279 isl_space_free(dim);
1280 return NULL;
1283 __isl_give isl_space *isl_space_drop_dims(__isl_take isl_space *dim,
1284 enum isl_dim_type type, unsigned first, unsigned num)
1286 int i;
1288 if (!dim)
1289 return NULL;
1291 if (num == 0)
1292 return isl_space_reset(dim, type);
1294 if (!valid_dim_type(type))
1295 isl_die(dim->ctx, isl_error_invalid,
1296 "cannot drop dimensions of specified type", goto error);
1298 isl_assert(dim->ctx, first + num <= n(dim, type), goto error);
1299 dim = isl_space_cow(dim);
1300 if (!dim)
1301 goto error;
1302 if (dim->ids) {
1303 dim = extend_ids(dim);
1304 if (!dim)
1305 goto error;
1306 for (i = 0; i < num; ++i)
1307 isl_id_free(get_id(dim, type, first + i));
1308 for (i = first+num; i < n(dim, type); ++i)
1309 set_id(dim, type, i - num, get_id(dim, type, i));
1310 switch (type) {
1311 case isl_dim_param:
1312 get_ids(dim, isl_dim_in, 0, dim->n_in,
1313 dim->ids + offset(dim, isl_dim_in) - num);
1314 case isl_dim_in:
1315 get_ids(dim, isl_dim_out, 0, dim->n_out,
1316 dim->ids + offset(dim, isl_dim_out) - num);
1317 default:
1320 dim->n_id -= num;
1322 switch (type) {
1323 case isl_dim_param: dim->nparam -= num; break;
1324 case isl_dim_in: dim->n_in -= num; break;
1325 case isl_dim_out: dim->n_out -= num; break;
1326 default: ;
1328 dim = isl_space_reset(dim, type);
1329 if (type == isl_dim_param) {
1330 if (dim && dim->nested[0] &&
1331 !(dim->nested[0] = isl_space_drop_dims(dim->nested[0],
1332 isl_dim_param, first, num)))
1333 goto error;
1334 if (dim && dim->nested[1] &&
1335 !(dim->nested[1] = isl_space_drop_dims(dim->nested[1],
1336 isl_dim_param, first, num)))
1337 goto error;
1339 return dim;
1340 error:
1341 isl_space_free(dim);
1342 return NULL;
1345 __isl_give isl_space *isl_space_drop_inputs(__isl_take isl_space *dim,
1346 unsigned first, unsigned n)
1348 if (!dim)
1349 return NULL;
1350 return isl_space_drop_dims(dim, isl_dim_in, first, n);
1353 __isl_give isl_space *isl_space_drop_outputs(__isl_take isl_space *dim,
1354 unsigned first, unsigned n)
1356 if (!dim)
1357 return NULL;
1358 return isl_space_drop_dims(dim, isl_dim_out, first, n);
1361 __isl_give isl_space *isl_space_domain(__isl_take isl_space *dim)
1363 if (!dim)
1364 return NULL;
1365 dim = isl_space_drop_outputs(dim, 0, dim->n_out);
1366 dim = isl_space_reverse(dim);
1367 dim = mark_as_set(dim);
1368 return dim;
1371 __isl_give isl_space *isl_space_from_domain(__isl_take isl_space *dim)
1373 if (!dim)
1374 return NULL;
1375 if (!isl_space_is_set(dim))
1376 isl_die(isl_space_get_ctx(dim), isl_error_invalid,
1377 "not a set space", goto error);
1378 dim = isl_space_reverse(dim);
1379 dim = isl_space_reset(dim, isl_dim_out);
1380 return dim;
1381 error:
1382 isl_space_free(dim);
1383 return NULL;
1386 __isl_give isl_space *isl_space_range(__isl_take isl_space *dim)
1388 if (!dim)
1389 return NULL;
1390 dim = isl_space_drop_inputs(dim, 0, dim->n_in);
1391 dim = mark_as_set(dim);
1392 return dim;
1395 __isl_give isl_space *isl_space_from_range(__isl_take isl_space *dim)
1397 if (!dim)
1398 return NULL;
1399 if (!isl_space_is_set(dim))
1400 isl_die(isl_space_get_ctx(dim), isl_error_invalid,
1401 "not a set space", goto error);
1402 return isl_space_reset(dim, isl_dim_in);
1403 error:
1404 isl_space_free(dim);
1405 return NULL;
1408 /* Given a map space A -> B, return the map space [A -> B] -> A.
1410 __isl_give isl_space *isl_space_domain_map(__isl_take isl_space *space)
1412 isl_space *domain;
1414 domain = isl_space_from_range(isl_space_domain(isl_space_copy(space)));
1415 space = isl_space_from_domain(isl_space_wrap(space));
1416 space = isl_space_join(space, domain);
1418 return space;
1421 /* Given a map space A -> B, return the map space [A -> B] -> B.
1423 __isl_give isl_space *isl_space_range_map(__isl_take isl_space *space)
1425 isl_space *range;
1427 range = isl_space_from_range(isl_space_range(isl_space_copy(space)));
1428 space = isl_space_from_domain(isl_space_wrap(space));
1429 space = isl_space_join(space, range);
1431 return space;
1434 __isl_give isl_space *isl_space_params(__isl_take isl_space *space)
1436 if (isl_space_is_params(space))
1437 return space;
1438 space = isl_space_drop_dims(space,
1439 isl_dim_in, 0, isl_space_dim(space, isl_dim_in));
1440 space = isl_space_drop_dims(space,
1441 isl_dim_out, 0, isl_space_dim(space, isl_dim_out));
1442 space = mark_as_params(space);
1443 return space;
1446 __isl_give isl_space *isl_space_set_from_params(__isl_take isl_space *space)
1448 if (!space)
1449 return NULL;
1450 if (!isl_space_is_params(space))
1451 isl_die(isl_space_get_ctx(space), isl_error_invalid,
1452 "not a parameter space", goto error);
1453 return isl_space_reset(space, isl_dim_set);
1454 error:
1455 isl_space_free(space);
1456 return NULL;
1459 __isl_give isl_space *isl_space_as_set_space(__isl_take isl_space *dim)
1461 dim = isl_space_cow(dim);
1462 if (!dim)
1463 return NULL;
1465 dim->n_out += dim->n_in;
1466 dim->n_in = 0;
1467 dim = isl_space_reset(dim, isl_dim_in);
1468 dim = isl_space_reset(dim, isl_dim_out);
1470 return dim;
1473 __isl_give isl_space *isl_space_underlying(__isl_take isl_space *dim,
1474 unsigned n_div)
1476 int i;
1478 if (!dim)
1479 return NULL;
1480 if (n_div == 0 &&
1481 dim->nparam == 0 && dim->n_in == 0 && dim->n_id == 0)
1482 return isl_space_reset(isl_space_reset(dim, isl_dim_in), isl_dim_out);
1483 dim = isl_space_cow(dim);
1484 if (!dim)
1485 return NULL;
1486 dim->n_out += dim->nparam + dim->n_in + n_div;
1487 dim->nparam = 0;
1488 dim->n_in = 0;
1490 for (i = 0; i < dim->n_id; ++i)
1491 isl_id_free(get_id(dim, isl_dim_out, i));
1492 dim->n_id = 0;
1493 dim = isl_space_reset(dim, isl_dim_in);
1494 dim = isl_space_reset(dim, isl_dim_out);
1496 return dim;
1499 /* Are the two spaces the same, including positions and names of parameters?
1501 int isl_space_is_equal(__isl_keep isl_space *dim1, __isl_keep isl_space *dim2)
1503 if (!dim1 || !dim2)
1504 return -1;
1505 if (dim1 == dim2)
1506 return 1;
1507 return match(dim1, isl_dim_param, dim2, isl_dim_param) &&
1508 isl_space_tuple_match(dim1, isl_dim_in, dim2, isl_dim_in) &&
1509 isl_space_tuple_match(dim1, isl_dim_out, dim2, isl_dim_out);
1512 /* Is space1 equal to the domain of space2?
1514 * In the internal version we also allow space2 to be the space of a set,
1515 * provided space1 is a parameter space.
1517 int isl_space_is_domain_internal(__isl_keep isl_space *space1,
1518 __isl_keep isl_space *space2)
1520 if (!space1 || !space2)
1521 return -1;
1522 if (!isl_space_is_set(space1))
1523 return 0;
1524 return match(space1, isl_dim_param, space2, isl_dim_param) &&
1525 isl_space_tuple_match(space1, isl_dim_set, space2, isl_dim_in);
1528 /* Is space1 equal to the domain of space2?
1530 int isl_space_is_domain(__isl_keep isl_space *space1,
1531 __isl_keep isl_space *space2)
1533 if (!space2)
1534 return -1;
1535 if (!isl_space_is_map(space2))
1536 return 0;
1537 return isl_space_is_domain_internal(space1, space2);
1540 /* Is space1 equal to the range of space2?
1542 * In the internal version, space2 is allowed to be the space of a set,
1543 * in which case it should be equal to space1.
1545 int isl_space_is_range_internal(__isl_keep isl_space *space1,
1546 __isl_keep isl_space *space2)
1548 if (!space1 || !space2)
1549 return -1;
1550 if (!isl_space_is_set(space1))
1551 return 0;
1552 return match(space1, isl_dim_param, space2, isl_dim_param) &&
1553 isl_space_tuple_match(space1, isl_dim_set, space2, isl_dim_out);
1556 /* Is space1 equal to the range of space2?
1558 int isl_space_is_range(__isl_keep isl_space *space1,
1559 __isl_keep isl_space *space2)
1561 if (!space2)
1562 return -1;
1563 if (!isl_space_is_map(space2))
1564 return 0;
1565 return isl_space_is_range_internal(space1, space2);
1568 int isl_space_compatible(__isl_keep isl_space *dim1,
1569 __isl_keep isl_space *dim2)
1571 return dim1->nparam == dim2->nparam &&
1572 dim1->n_in + dim1->n_out == dim2->n_in + dim2->n_out;
1575 static uint32_t isl_hash_dim(uint32_t hash, __isl_keep isl_space *dim)
1577 int i;
1578 isl_id *id;
1580 if (!dim)
1581 return hash;
1583 isl_hash_byte(hash, dim->nparam % 256);
1584 isl_hash_byte(hash, dim->n_in % 256);
1585 isl_hash_byte(hash, dim->n_out % 256);
1587 for (i = 0; i < dim->nparam; ++i) {
1588 id = get_id(dim, isl_dim_param, i);
1589 hash = isl_hash_id(hash, id);
1592 id = tuple_id(dim, isl_dim_in);
1593 hash = isl_hash_id(hash, id);
1594 id = tuple_id(dim, isl_dim_out);
1595 hash = isl_hash_id(hash, id);
1597 hash = isl_hash_dim(hash, dim->nested[0]);
1598 hash = isl_hash_dim(hash, dim->nested[1]);
1600 return hash;
1603 uint32_t isl_space_get_hash(__isl_keep isl_space *dim)
1605 uint32_t hash;
1607 if (!dim)
1608 return 0;
1610 hash = isl_hash_init();
1611 hash = isl_hash_dim(hash, dim);
1613 return hash;
1616 int isl_space_is_wrapping(__isl_keep isl_space *dim)
1618 if (!dim)
1619 return -1;
1621 if (!isl_space_is_set(dim))
1622 return 0;
1624 return dim->nested[1] != NULL;
1627 __isl_give isl_space *isl_space_wrap(__isl_take isl_space *dim)
1629 isl_space *wrap;
1631 if (!dim)
1632 return NULL;
1634 wrap = isl_space_set_alloc(dim->ctx,
1635 dim->nparam, dim->n_in + dim->n_out);
1637 wrap = copy_ids(wrap, isl_dim_param, 0, dim, isl_dim_param);
1638 wrap = copy_ids(wrap, isl_dim_set, 0, dim, isl_dim_in);
1639 wrap = copy_ids(wrap, isl_dim_set, dim->n_in, dim, isl_dim_out);
1641 if (!wrap)
1642 goto error;
1644 wrap->nested[1] = dim;
1646 return wrap;
1647 error:
1648 isl_space_free(dim);
1649 return NULL;
1652 __isl_give isl_space *isl_space_unwrap(__isl_take isl_space *dim)
1654 isl_space *unwrap;
1656 if (!dim)
1657 return NULL;
1659 if (!isl_space_is_wrapping(dim))
1660 isl_die(dim->ctx, isl_error_invalid, "not a wrapping space",
1661 goto error);
1663 unwrap = isl_space_copy(dim->nested[1]);
1664 isl_space_free(dim);
1666 return unwrap;
1667 error:
1668 isl_space_free(dim);
1669 return NULL;
1672 int isl_space_is_named_or_nested(__isl_keep isl_space *dim, enum isl_dim_type type)
1674 if (type != isl_dim_in && type != isl_dim_out)
1675 return 0;
1676 if (!dim)
1677 return -1;
1678 if (dim->tuple_id[type - isl_dim_in])
1679 return 1;
1680 if (dim->nested[type - isl_dim_in])
1681 return 1;
1682 return 0;
1685 int isl_space_may_be_set(__isl_keep isl_space *dim)
1687 if (!dim)
1688 return -1;
1689 if (isl_space_is_set(dim))
1690 return 1;
1691 if (isl_space_dim(dim, isl_dim_in) != 0)
1692 return 0;
1693 if (isl_space_is_named_or_nested(dim, isl_dim_in))
1694 return 0;
1695 return 1;
1698 __isl_give isl_space *isl_space_reset(__isl_take isl_space *dim,
1699 enum isl_dim_type type)
1701 if (!isl_space_is_named_or_nested(dim, type))
1702 return dim;
1704 dim = isl_space_cow(dim);
1705 if (!dim)
1706 return NULL;
1708 isl_id_free(dim->tuple_id[type - isl_dim_in]);
1709 dim->tuple_id[type - isl_dim_in] = NULL;
1710 isl_space_free(dim->nested[type - isl_dim_in]);
1711 dim->nested[type - isl_dim_in] = NULL;
1713 return dim;
1716 __isl_give isl_space *isl_space_flatten(__isl_take isl_space *dim)
1718 if (!dim)
1719 return NULL;
1720 if (!dim->nested[0] && !dim->nested[1])
1721 return dim;
1723 if (dim->nested[0])
1724 dim = isl_space_reset(dim, isl_dim_in);
1725 if (dim && dim->nested[1])
1726 dim = isl_space_reset(dim, isl_dim_out);
1728 return dim;
1731 __isl_give isl_space *isl_space_flatten_domain(__isl_take isl_space *dim)
1733 if (!dim)
1734 return NULL;
1735 if (!dim->nested[0])
1736 return dim;
1738 return isl_space_reset(dim, isl_dim_in);
1741 __isl_give isl_space *isl_space_flatten_range(__isl_take isl_space *dim)
1743 if (!dim)
1744 return NULL;
1745 if (!dim->nested[1])
1746 return dim;
1748 return isl_space_reset(dim, isl_dim_out);
1751 /* Replace the dimensions of the given type of dst by those of src.
1753 __isl_give isl_space *isl_space_replace(__isl_take isl_space *dst,
1754 enum isl_dim_type type, __isl_keep isl_space *src)
1756 dst = isl_space_cow(dst);
1758 if (!dst || !src)
1759 goto error;
1761 dst = isl_space_drop_dims(dst, type, 0, isl_space_dim(dst, type));
1762 dst = isl_space_add_dims(dst, type, isl_space_dim(src, type));
1763 dst = copy_ids(dst, type, 0, src, type);
1765 if (dst && type == isl_dim_param) {
1766 int i;
1767 for (i = 0; i <= 1; ++i) {
1768 if (!dst->nested[i])
1769 continue;
1770 dst->nested[i] = isl_space_replace(dst->nested[i],
1771 type, src);
1772 if (!dst->nested[i])
1773 goto error;
1777 return dst;
1778 error:
1779 isl_space_free(dst);
1780 return NULL;
1783 /* Given a dimension specification "dim" of a set, create a dimension
1784 * specification for the lift of the set. In particular, the result
1785 * is of the form [dim -> local[..]], with n_local variables in the
1786 * range of the wrapped map.
1788 __isl_give isl_space *isl_space_lift(__isl_take isl_space *dim, unsigned n_local)
1790 isl_space *local_dim;
1792 if (!dim)
1793 return NULL;
1795 local_dim = isl_space_dup(dim);
1796 local_dim = isl_space_drop_dims(local_dim, isl_dim_set, 0, dim->n_out);
1797 local_dim = isl_space_add_dims(local_dim, isl_dim_set, n_local);
1798 local_dim = isl_space_set_tuple_name(local_dim, isl_dim_set, "local");
1799 dim = isl_space_join(isl_space_from_domain(dim),
1800 isl_space_from_range(local_dim));
1801 dim = isl_space_wrap(dim);
1802 dim = isl_space_set_tuple_name(dim, isl_dim_set, "lifted");
1804 return dim;
1807 int isl_space_can_zip(__isl_keep isl_space *dim)
1809 if (!dim)
1810 return -1;
1812 return dim->nested[0] && dim->nested[1];
1815 __isl_give isl_space *isl_space_zip(__isl_take isl_space *dim)
1817 isl_space *dom, *ran;
1818 isl_space *dom_dom, *dom_ran, *ran_dom, *ran_ran;
1820 if (!isl_space_can_zip(dim))
1821 isl_die(dim->ctx, isl_error_invalid, "dim cannot be zipped",
1822 goto error);
1824 if (!dim)
1825 return NULL;
1826 dom = isl_space_unwrap(isl_space_domain(isl_space_copy(dim)));
1827 ran = isl_space_unwrap(isl_space_range(dim));
1828 dom_dom = isl_space_domain(isl_space_copy(dom));
1829 dom_ran = isl_space_range(dom);
1830 ran_dom = isl_space_domain(isl_space_copy(ran));
1831 ran_ran = isl_space_range(ran);
1832 dom = isl_space_join(isl_space_from_domain(dom_dom),
1833 isl_space_from_range(ran_dom));
1834 ran = isl_space_join(isl_space_from_domain(dom_ran),
1835 isl_space_from_range(ran_ran));
1836 return isl_space_join(isl_space_from_domain(isl_space_wrap(dom)),
1837 isl_space_from_range(isl_space_wrap(ran)));
1838 error:
1839 isl_space_free(dim);
1840 return NULL;
1843 /* Can we apply isl_space_curry to "space"?
1844 * That is, does it have a nested relation in its domain?
1846 int isl_space_can_curry(__isl_keep isl_space *space)
1848 if (!space)
1849 return -1;
1851 return !!space->nested[0];
1854 /* Given a space (A -> B) -> C, return the corresponding space
1855 * A -> (B -> C).
1857 __isl_give isl_space *isl_space_curry(__isl_take isl_space *space)
1859 isl_space *dom, *ran;
1860 isl_space *dom_dom, *dom_ran;
1862 if (!space)
1863 return NULL;
1865 if (!isl_space_can_curry(space))
1866 isl_die(space->ctx, isl_error_invalid,
1867 "space cannot be curried", goto error);
1869 dom = isl_space_unwrap(isl_space_domain(isl_space_copy(space)));
1870 ran = isl_space_range(space);
1871 dom_dom = isl_space_domain(isl_space_copy(dom));
1872 dom_ran = isl_space_range(dom);
1873 ran = isl_space_join(isl_space_from_domain(dom_ran),
1874 isl_space_from_range(ran));
1875 return isl_space_join(isl_space_from_domain(dom_dom),
1876 isl_space_from_range(isl_space_wrap(ran)));
1877 error:
1878 isl_space_free(space);
1879 return NULL;
1882 /* Can we apply isl_space_uncurry to "space"?
1883 * That is, does it have a nested relation in its range?
1885 int isl_space_can_uncurry(__isl_keep isl_space *space)
1887 if (!space)
1888 return -1;
1890 return !!space->nested[1];
1893 /* Given a space A -> (B -> C), return the corresponding space
1894 * (A -> B) -> C.
1896 __isl_give isl_space *isl_space_uncurry(__isl_take isl_space *space)
1898 isl_space *dom, *ran;
1899 isl_space *ran_dom, *ran_ran;
1901 if (!space)
1902 return NULL;
1904 if (!isl_space_can_uncurry(space))
1905 isl_die(space->ctx, isl_error_invalid,
1906 "space cannot be uncurried",
1907 return isl_space_free(space));
1909 dom = isl_space_domain(isl_space_copy(space));
1910 ran = isl_space_unwrap(isl_space_range(space));
1911 ran_dom = isl_space_domain(isl_space_copy(ran));
1912 ran_ran = isl_space_range(ran);
1913 dom = isl_space_join(isl_space_from_domain(dom),
1914 isl_space_from_range(ran_dom));
1915 return isl_space_join(isl_space_from_domain(isl_space_wrap(dom)),
1916 isl_space_from_range(ran_ran));
1919 int isl_space_has_named_params(__isl_keep isl_space *dim)
1921 int i;
1922 unsigned off;
1924 if (!dim)
1925 return -1;
1926 if (dim->nparam == 0)
1927 return 1;
1928 off = isl_space_offset(dim, isl_dim_param);
1929 if (off + dim->nparam > dim->n_id)
1930 return 0;
1931 for (i = 0; i < dim->nparam; ++i)
1932 if (!dim->ids[off + i])
1933 return 0;
1934 return 1;
1937 /* Align the initial parameters of dim1 to match the order in dim2.
1939 __isl_give isl_space *isl_space_align_params(__isl_take isl_space *dim1,
1940 __isl_take isl_space *dim2)
1942 isl_reordering *exp;
1944 if (!isl_space_has_named_params(dim1) || !isl_space_has_named_params(dim2))
1945 isl_die(isl_space_get_ctx(dim1), isl_error_invalid,
1946 "parameter alignment requires named parameters",
1947 goto error);
1949 dim2 = isl_space_params(dim2);
1950 exp = isl_parameter_alignment_reordering(dim1, dim2);
1951 exp = isl_reordering_extend_space(exp, dim1);
1952 isl_space_free(dim2);
1953 if (!exp)
1954 return NULL;
1955 dim1 = isl_space_copy(exp->dim);
1956 isl_reordering_free(exp);
1957 return dim1;
1958 error:
1959 isl_space_free(dim1);
1960 isl_space_free(dim2);
1961 return NULL;
1964 /* Given the space of set (domain), construct a space for a map
1965 * with as domain the given space and as range the range of "model".
1967 __isl_give isl_space *isl_space_extend_domain_with_range(
1968 __isl_take isl_space *space, __isl_take isl_space *model)
1970 if (!model)
1971 goto error;
1973 space = isl_space_from_domain(space);
1974 space = isl_space_add_dims(space, isl_dim_out,
1975 isl_space_dim(model, isl_dim_out));
1976 if (isl_space_has_tuple_id(model, isl_dim_out))
1977 space = isl_space_set_tuple_id(space, isl_dim_out,
1978 isl_space_get_tuple_id(model, isl_dim_out));
1979 if (!space)
1980 goto error;
1981 if (model->nested[1]) {
1982 isl_space *nested = isl_space_copy(model->nested[1]);
1983 int n_nested, n_space;
1984 nested = isl_space_align_params(nested, isl_space_copy(space));
1985 n_nested = isl_space_dim(nested, isl_dim_param);
1986 n_space = isl_space_dim(space, isl_dim_param);
1987 if (n_nested > n_space)
1988 nested = isl_space_drop_dims(nested, isl_dim_param,
1989 n_space, n_nested - n_space);
1990 if (!nested)
1991 goto error;
1992 space->nested[1] = nested;
1994 isl_space_free(model);
1995 return space;
1996 error:
1997 isl_space_free(model);
1998 isl_space_free(space);
1999 return NULL;