isl_space_tuple_match: also ignore parameters in nested spaces
[isl.git] / isl_space.c
blob53ad82bf112cf29959fab25d583d1ddecfea5890
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2013-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 __isl_null isl_space *isl_space_free(__isl_take isl_space *space)
343 int i;
345 if (!space)
346 return NULL;
348 if (--space->ref > 0)
349 return NULL;
351 isl_id_free(space->tuple_id[0]);
352 isl_id_free(space->tuple_id[1]);
354 isl_space_free(space->nested[0]);
355 isl_space_free(space->nested[1]);
357 for (i = 0; i < space->n_id; ++i)
358 isl_id_free(space->ids[i]);
359 free(space->ids);
360 isl_ctx_deref(space->ctx);
362 free(space);
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 /* Reset the user pointer on all identifiers of parameters and tuples
683 * of "space".
685 __isl_give isl_space *isl_space_reset_user(__isl_take isl_space *space)
687 int i;
688 isl_ctx *ctx;
689 isl_id *id;
690 const char *name;
692 if (!space)
693 return NULL;
695 ctx = isl_space_get_ctx(space);
697 for (i = 0; i < space->nparam && i < space->n_id; ++i) {
698 if (!isl_id_get_user(space->ids[i]))
699 continue;
700 space = isl_space_cow(space);
701 if (!space)
702 return NULL;
703 name = isl_id_get_name(space->ids[i]);
704 id = isl_id_alloc(ctx, name, NULL);
705 isl_id_free(space->ids[i]);
706 space->ids[i] = id;
707 if (!id)
708 return isl_space_free(space);
711 for (i = 0; i < 2; ++i) {
712 if (!space->tuple_id[i])
713 continue;
714 if (!isl_id_get_user(space->tuple_id[i]))
715 continue;
716 space = isl_space_cow(space);
717 if (!space)
718 return NULL;
719 name = isl_id_get_name(space->tuple_id[i]);
720 id = isl_id_alloc(ctx, name, NULL);
721 isl_id_free(space->tuple_id[i]);
722 space->tuple_id[i] = id;
723 if (!id)
724 return isl_space_free(space);
727 for (i = 0; i < 2; ++i) {
728 if (!space->nested[i])
729 continue;
730 space = isl_space_cow(space);
731 if (!space)
732 return NULL;
733 space->nested[i] = isl_space_reset_user(space->nested[i]);
734 if (!space->nested[i])
735 return isl_space_free(space);
738 return space;
741 static __isl_keep isl_id *tuple_id(__isl_keep isl_space *dim,
742 enum isl_dim_type type)
744 if (!dim)
745 return NULL;
746 if (type == isl_dim_in)
747 return dim->tuple_id[0];
748 if (type == isl_dim_out)
749 return dim->tuple_id[1];
750 return NULL;
753 static __isl_keep isl_space *nested(__isl_keep isl_space *dim,
754 enum isl_dim_type type)
756 if (!dim)
757 return NULL;
758 if (type == isl_dim_in)
759 return dim->nested[0];
760 if (type == isl_dim_out)
761 return dim->nested[1];
762 return NULL;
765 /* Are the two spaces the same, apart from positions and names of parameters?
767 static int isl_space_has_equal_tuples(__isl_keep isl_space *space1,
768 __isl_keep isl_space *space2)
770 if (!space1 || !space2)
771 return -1;
772 if (space1 == space2)
773 return 1;
774 return isl_space_tuple_match(space1, isl_dim_in, space2, isl_dim_in) &&
775 isl_space_tuple_match(space1, isl_dim_out, space2, isl_dim_out);
778 /* Check if the tuple of type "type1" of "space1" is the same as
779 * the tuple of type "type2" of "space2".
781 * That is, check if the tuples have the same identifier, the same dimension
782 * and the same internal structure.
783 * The identifiers of the dimensions inside the tuples do not affect the result.
785 * Note that this function only checks the tuples themselves.
786 * If nested tuples are involved, then we need to be careful not
787 * to have result affected by possibly differing parameters
788 * in those nested tuples.
790 int isl_space_tuple_match(__isl_keep isl_space *space1, enum isl_dim_type type1,
791 __isl_keep isl_space *space2, enum isl_dim_type type2)
793 isl_id *id1, *id2;
794 isl_space *nested1, *nested2;
796 if (!space1 || !space2)
797 return -1;
799 if (space1 == space2 && type1 == type2)
800 return 1;
802 if (n(space1, type1) != n(space2, type2))
803 return 0;
804 id1 = tuple_id(space1, type1);
805 id2 = tuple_id(space2, type2);
806 if (!id1 ^ !id2)
807 return 0;
808 if (id1 && id1 != id2)
809 return 0;
810 nested1 = nested(space1, type1);
811 nested2 = nested(space2, type2);
812 if (!nested1 ^ !nested2)
813 return 0;
814 if (nested1 && !isl_space_has_equal_tuples(nested1, nested2))
815 return 0;
816 return 1;
819 static int match(__isl_keep isl_space *dim1, enum isl_dim_type dim1_type,
820 __isl_keep isl_space *dim2, enum isl_dim_type dim2_type)
822 int i;
824 if (dim1 == dim2 && dim1_type == dim2_type)
825 return 1;
827 if (!isl_space_tuple_match(dim1, dim1_type, dim2, dim2_type))
828 return 0;
830 if (!dim1->ids && !dim2->ids)
831 return 1;
833 for (i = 0; i < n(dim1, dim1_type); ++i) {
834 if (get_id(dim1, dim1_type, i) != get_id(dim2, dim2_type, i))
835 return 0;
837 return 1;
840 int isl_space_match(__isl_keep isl_space *dim1, enum isl_dim_type dim1_type,
841 __isl_keep isl_space *dim2, enum isl_dim_type dim2_type)
843 if (!dim1 || !dim2)
844 return -1;
846 return match(dim1, dim1_type, dim2, dim2_type);
849 static void get_ids(__isl_keep isl_space *dim, enum isl_dim_type type,
850 unsigned first, unsigned n, __isl_keep isl_id **ids)
852 int i;
854 for (i = 0; i < n ; ++i)
855 ids[i] = get_id(dim, type, first + i);
858 __isl_give isl_space *isl_space_extend(__isl_take isl_space *dim,
859 unsigned nparam, unsigned n_in, unsigned n_out)
861 isl_id **ids = NULL;
863 if (!dim)
864 return NULL;
865 if (dim->nparam == nparam && dim->n_in == n_in && dim->n_out == n_out)
866 return dim;
868 isl_assert(dim->ctx, dim->nparam <= nparam, goto error);
869 isl_assert(dim->ctx, dim->n_in <= n_in, goto error);
870 isl_assert(dim->ctx, dim->n_out <= n_out, goto error);
872 dim = isl_space_cow(dim);
873 if (!dim)
874 goto error;
876 if (dim->ids) {
877 ids = isl_calloc_array(dim->ctx, isl_id *,
878 nparam + n_in + n_out);
879 if (!ids)
880 goto error;
881 get_ids(dim, isl_dim_param, 0, dim->nparam, ids);
882 get_ids(dim, isl_dim_in, 0, dim->n_in, ids + nparam);
883 get_ids(dim, isl_dim_out, 0, dim->n_out, ids + nparam + n_in);
884 free(dim->ids);
885 dim->ids = ids;
886 dim->n_id = nparam + n_in + n_out;
888 dim->nparam = nparam;
889 dim->n_in = n_in;
890 dim->n_out = n_out;
892 return dim;
893 error:
894 free(ids);
895 isl_space_free(dim);
896 return NULL;
899 __isl_give isl_space *isl_space_add_dims(__isl_take isl_space *dim,
900 enum isl_dim_type type, unsigned n)
902 if (!dim)
903 return NULL;
904 dim = isl_space_reset(dim, type);
905 switch (type) {
906 case isl_dim_param:
907 dim = isl_space_extend(dim,
908 dim->nparam + n, dim->n_in, dim->n_out);
909 if (dim && dim->nested[0] &&
910 !(dim->nested[0] = isl_space_add_dims(dim->nested[0],
911 isl_dim_param, n)))
912 goto error;
913 if (dim && dim->nested[1] &&
914 !(dim->nested[1] = isl_space_add_dims(dim->nested[1],
915 isl_dim_param, n)))
916 goto error;
917 return dim;
918 case isl_dim_in:
919 return isl_space_extend(dim,
920 dim->nparam, dim->n_in + n, dim->n_out);
921 case isl_dim_out:
922 return isl_space_extend(dim,
923 dim->nparam, dim->n_in, dim->n_out + n);
924 default:
925 isl_die(dim->ctx, isl_error_invalid,
926 "cannot add dimensions of specified type", goto error);
928 error:
929 isl_space_free(dim);
930 return NULL;
933 static int valid_dim_type(enum isl_dim_type type)
935 switch (type) {
936 case isl_dim_param:
937 case isl_dim_in:
938 case isl_dim_out:
939 return 1;
940 default:
941 return 0;
945 /* Insert "n" dimensions of type "type" at position "pos".
946 * If we are inserting parameters, then they are also inserted in
947 * any nested spaces.
949 __isl_give isl_space *isl_space_insert_dims(__isl_take isl_space *dim,
950 enum isl_dim_type type, unsigned pos, unsigned n)
952 isl_id **ids = NULL;
954 if (!dim)
955 return NULL;
956 if (n == 0)
957 return isl_space_reset(dim, type);
959 if (!valid_dim_type(type))
960 isl_die(dim->ctx, isl_error_invalid,
961 "cannot insert dimensions of specified type",
962 goto error);
964 isl_assert(dim->ctx, pos <= isl_space_dim(dim, type), goto error);
966 dim = isl_space_cow(dim);
967 if (!dim)
968 return NULL;
970 if (dim->ids) {
971 enum isl_dim_type t, o = isl_dim_param;
972 int off;
973 int s[3];
974 ids = isl_calloc_array(dim->ctx, isl_id *,
975 dim->nparam + dim->n_in + dim->n_out + n);
976 if (!ids)
977 goto error;
978 off = 0;
979 s[isl_dim_param - o] = dim->nparam;
980 s[isl_dim_in - o] = dim->n_in;
981 s[isl_dim_out - o] = dim->n_out;
982 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
983 if (t != type) {
984 get_ids(dim, t, 0, s[t - o], ids + off);
985 off += s[t - o];
986 } else {
987 get_ids(dim, t, 0, pos, ids + off);
988 off += pos + n;
989 get_ids(dim, t, pos, s[t - o] - pos, ids + off);
990 off += s[t - o] - pos;
993 free(dim->ids);
994 dim->ids = ids;
995 dim->n_id = dim->nparam + dim->n_in + dim->n_out + n;
997 switch (type) {
998 case isl_dim_param: dim->nparam += n; break;
999 case isl_dim_in: dim->n_in += n; break;
1000 case isl_dim_out: dim->n_out += n; break;
1001 default: ;
1003 dim = isl_space_reset(dim, type);
1005 if (type == isl_dim_param) {
1006 if (dim && dim->nested[0] &&
1007 !(dim->nested[0] = isl_space_insert_dims(dim->nested[0],
1008 isl_dim_param, pos, n)))
1009 goto error;
1010 if (dim && dim->nested[1] &&
1011 !(dim->nested[1] = isl_space_insert_dims(dim->nested[1],
1012 isl_dim_param, pos, n)))
1013 goto error;
1016 return dim;
1017 error:
1018 isl_space_free(dim);
1019 return NULL;
1022 __isl_give isl_space *isl_space_move_dims(__isl_take isl_space *dim,
1023 enum isl_dim_type dst_type, unsigned dst_pos,
1024 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
1026 int i;
1028 if (!dim)
1029 return NULL;
1030 if (n == 0) {
1031 dim = isl_space_reset(dim, src_type);
1032 return isl_space_reset(dim, dst_type);
1035 isl_assert(dim->ctx, src_pos + n <= isl_space_dim(dim, src_type),
1036 goto error);
1038 if (dst_type == src_type && dst_pos == src_pos)
1039 return dim;
1041 isl_assert(dim->ctx, dst_type != src_type, goto error);
1043 dim = isl_space_reset(dim, src_type);
1044 dim = isl_space_reset(dim, dst_type);
1046 dim = isl_space_cow(dim);
1047 if (!dim)
1048 return NULL;
1050 if (dim->ids) {
1051 isl_id **ids;
1052 enum isl_dim_type t, o = isl_dim_param;
1053 int off;
1054 int s[3];
1055 ids = isl_calloc_array(dim->ctx, isl_id *,
1056 dim->nparam + dim->n_in + dim->n_out);
1057 if (!ids)
1058 goto error;
1059 off = 0;
1060 s[isl_dim_param - o] = dim->nparam;
1061 s[isl_dim_in - o] = dim->n_in;
1062 s[isl_dim_out - o] = dim->n_out;
1063 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
1064 if (t == dst_type) {
1065 get_ids(dim, t, 0, dst_pos, ids + off);
1066 off += dst_pos;
1067 get_ids(dim, src_type, src_pos, n, ids + off);
1068 off += n;
1069 get_ids(dim, t, dst_pos, s[t - o] - dst_pos,
1070 ids + off);
1071 off += s[t - o] - dst_pos;
1072 } else if (t == src_type) {
1073 get_ids(dim, t, 0, src_pos, ids + off);
1074 off += src_pos;
1075 get_ids(dim, t, src_pos + n,
1076 s[t - o] - src_pos - n, ids + off);
1077 off += s[t - o] - src_pos - n;
1078 } else {
1079 get_ids(dim, t, 0, s[t - o], ids + off);
1080 off += s[t - o];
1083 free(dim->ids);
1084 dim->ids = ids;
1085 dim->n_id = dim->nparam + dim->n_in + dim->n_out;
1088 switch (dst_type) {
1089 case isl_dim_param: dim->nparam += n; break;
1090 case isl_dim_in: dim->n_in += n; break;
1091 case isl_dim_out: dim->n_out += n; break;
1092 default: ;
1095 switch (src_type) {
1096 case isl_dim_param: dim->nparam -= n; break;
1097 case isl_dim_in: dim->n_in -= n; break;
1098 case isl_dim_out: dim->n_out -= n; break;
1099 default: ;
1102 if (dst_type != isl_dim_param && src_type != isl_dim_param)
1103 return dim;
1105 for (i = 0; i < 2; ++i) {
1106 if (!dim->nested[i])
1107 continue;
1108 dim->nested[i] = isl_space_replace(dim->nested[i],
1109 isl_dim_param, dim);
1110 if (!dim->nested[i])
1111 goto error;
1114 return dim;
1115 error:
1116 isl_space_free(dim);
1117 return NULL;
1120 __isl_give isl_space *isl_space_join(__isl_take isl_space *left,
1121 __isl_take isl_space *right)
1123 isl_space *dim;
1125 if (!left || !right)
1126 goto error;
1128 isl_assert(left->ctx, match(left, isl_dim_param, right, isl_dim_param),
1129 goto error);
1130 isl_assert(left->ctx,
1131 isl_space_tuple_match(left, isl_dim_out, right, isl_dim_in),
1132 goto error);
1134 dim = isl_space_alloc(left->ctx, left->nparam, left->n_in, right->n_out);
1135 if (!dim)
1136 goto error;
1138 dim = copy_ids(dim, isl_dim_param, 0, left, isl_dim_param);
1139 dim = copy_ids(dim, isl_dim_in, 0, left, isl_dim_in);
1140 dim = copy_ids(dim, isl_dim_out, 0, right, isl_dim_out);
1142 if (dim && left->tuple_id[0] &&
1143 !(dim->tuple_id[0] = isl_id_copy(left->tuple_id[0])))
1144 goto error;
1145 if (dim && right->tuple_id[1] &&
1146 !(dim->tuple_id[1] = isl_id_copy(right->tuple_id[1])))
1147 goto error;
1148 if (dim && left->nested[0] &&
1149 !(dim->nested[0] = isl_space_copy(left->nested[0])))
1150 goto error;
1151 if (dim && right->nested[1] &&
1152 !(dim->nested[1] = isl_space_copy(right->nested[1])))
1153 goto error;
1155 isl_space_free(left);
1156 isl_space_free(right);
1158 return dim;
1159 error:
1160 isl_space_free(left);
1161 isl_space_free(right);
1162 return NULL;
1165 /* Given two map spaces { A -> C } and { B -> D }, construct the space
1166 * { [A -> B] -> [C -> D] }.
1167 * Given two set spaces { A } and { B }, construct the space { [A -> B] }.
1169 __isl_give isl_space *isl_space_product(__isl_take isl_space *left,
1170 __isl_take isl_space *right)
1172 isl_space *dom1, *dom2, *nest1, *nest2;
1173 int is_set;
1175 if (!left || !right)
1176 goto error;
1178 is_set = isl_space_is_set(left);
1179 if (is_set != isl_space_is_set(right))
1180 isl_die(isl_space_get_ctx(left), isl_error_invalid,
1181 "expecting either two set spaces or two map spaces",
1182 goto error);
1183 if (is_set)
1184 return isl_space_range_product(left, right);
1186 isl_assert(left->ctx, match(left, isl_dim_param, right, isl_dim_param),
1187 goto error);
1189 dom1 = isl_space_domain(isl_space_copy(left));
1190 dom2 = isl_space_domain(isl_space_copy(right));
1191 nest1 = isl_space_wrap(isl_space_join(isl_space_reverse(dom1), dom2));
1193 dom1 = isl_space_range(left);
1194 dom2 = isl_space_range(right);
1195 nest2 = isl_space_wrap(isl_space_join(isl_space_reverse(dom1), dom2));
1197 return isl_space_join(isl_space_reverse(nest1), nest2);
1198 error:
1199 isl_space_free(left);
1200 isl_space_free(right);
1201 return NULL;
1204 /* Given two spaces { A -> C } and { B -> C }, construct the space
1205 * { [A -> B] -> C }
1207 __isl_give isl_space *isl_space_domain_product(__isl_take isl_space *left,
1208 __isl_take isl_space *right)
1210 isl_space *ran, *dom1, *dom2, *nest;
1212 if (!left || !right)
1213 goto error;
1215 if (!match(left, isl_dim_param, right, isl_dim_param))
1216 isl_die(left->ctx, isl_error_invalid,
1217 "parameters need to match", goto error);
1218 if (!isl_space_tuple_match(left, isl_dim_out, right, isl_dim_out))
1219 isl_die(left->ctx, isl_error_invalid,
1220 "ranges need to match", goto error);
1222 ran = isl_space_range(isl_space_copy(left));
1224 dom1 = isl_space_domain(left);
1225 dom2 = isl_space_domain(right);
1226 nest = isl_space_wrap(isl_space_join(isl_space_reverse(dom1), dom2));
1228 return isl_space_join(isl_space_reverse(nest), ran);
1229 error:
1230 isl_space_free(left);
1231 isl_space_free(right);
1232 return NULL;
1235 __isl_give isl_space *isl_space_range_product(__isl_take isl_space *left,
1236 __isl_take isl_space *right)
1238 isl_space *dom, *ran1, *ran2, *nest;
1240 if (!left || !right)
1241 goto error;
1243 isl_assert(left->ctx, match(left, isl_dim_param, right, isl_dim_param),
1244 goto error);
1245 if (!isl_space_tuple_match(left, isl_dim_in, right, isl_dim_in))
1246 isl_die(left->ctx, isl_error_invalid,
1247 "domains need to match", goto error);
1249 dom = isl_space_domain(isl_space_copy(left));
1251 ran1 = isl_space_range(left);
1252 ran2 = isl_space_range(right);
1253 nest = isl_space_wrap(isl_space_join(isl_space_reverse(ran1), ran2));
1255 return isl_space_join(isl_space_reverse(dom), nest);
1256 error:
1257 isl_space_free(left);
1258 isl_space_free(right);
1259 return NULL;
1262 /* Given a space of the form [A -> B] -> C, return the space A -> C.
1264 __isl_give isl_space *isl_space_domain_factor_domain(
1265 __isl_take isl_space *space)
1267 isl_space *nested;
1268 isl_space *domain;
1270 if (!space)
1271 return NULL;
1272 if (!isl_space_domain_is_wrapping(space))
1273 isl_die(isl_space_get_ctx(space), isl_error_invalid,
1274 "domain not a product", return isl_space_free(space));
1276 nested = space->nested[0];
1277 domain = isl_space_copy(space);
1278 domain = isl_space_drop_dims(space, isl_dim_in,
1279 nested->n_in, nested->n_out);
1280 if (!domain)
1281 return isl_space_free(space);
1282 if (nested->tuple_id[0]) {
1283 domain->tuple_id[0] = isl_id_copy(nested->tuple_id[0]);
1284 if (!domain->tuple_id[0])
1285 goto error;
1287 if (nested->nested[0]) {
1288 domain->nested[0] = isl_space_copy(nested->nested[0]);
1289 if (!domain->nested[0])
1290 goto error;
1293 isl_space_free(space);
1294 return domain;
1295 error:
1296 isl_space_free(space);
1297 isl_space_free(domain);
1298 return NULL;
1301 /* Given a space of the form A -> [B -> C], return the space A -> B.
1303 __isl_give isl_space *isl_space_range_factor_domain(
1304 __isl_take isl_space *space)
1306 isl_space *nested;
1307 isl_space *domain;
1309 if (!space)
1310 return NULL;
1311 if (!isl_space_range_is_wrapping(space))
1312 isl_die(isl_space_get_ctx(space), isl_error_invalid,
1313 "range not a product", return isl_space_free(space));
1315 nested = space->nested[1];
1316 domain = isl_space_copy(space);
1317 domain = isl_space_drop_dims(space, isl_dim_out,
1318 nested->n_in, nested->n_out);
1319 if (!domain)
1320 return isl_space_free(space);
1321 if (nested->tuple_id[0]) {
1322 domain->tuple_id[1] = isl_id_copy(nested->tuple_id[0]);
1323 if (!domain->tuple_id[1])
1324 goto error;
1326 if (nested->nested[0]) {
1327 domain->nested[1] = isl_space_copy(nested->nested[0]);
1328 if (!domain->nested[1])
1329 goto error;
1332 isl_space_free(space);
1333 return domain;
1334 error:
1335 isl_space_free(space);
1336 isl_space_free(domain);
1337 return NULL;
1340 /* Given a space of the form A -> [B -> C], return the space A -> C.
1342 __isl_give isl_space *isl_space_range_factor_range(
1343 __isl_take isl_space *space)
1345 isl_space *nested;
1346 isl_space *range;
1348 if (!space)
1349 return NULL;
1350 if (!isl_space_range_is_wrapping(space))
1351 isl_die(isl_space_get_ctx(space), isl_error_invalid,
1352 "range not a product", return isl_space_free(space));
1354 nested = space->nested[1];
1355 range = isl_space_copy(space);
1356 range = isl_space_drop_dims(space, isl_dim_out, 0, nested->n_in);
1357 if (!range)
1358 return isl_space_free(space);
1359 if (nested->tuple_id[1]) {
1360 range->tuple_id[1] = isl_id_copy(nested->tuple_id[1]);
1361 if (!range->tuple_id[1])
1362 goto error;
1364 if (nested->nested[1]) {
1365 range->nested[1] = isl_space_copy(nested->nested[1]);
1366 if (!range->nested[1])
1367 goto error;
1370 isl_space_free(space);
1371 return range;
1372 error:
1373 isl_space_free(space);
1374 isl_space_free(range);
1375 return NULL;
1378 __isl_give isl_space *isl_space_map_from_set(__isl_take isl_space *dim)
1380 isl_ctx *ctx;
1381 isl_id **ids = NULL;
1383 if (!dim)
1384 return NULL;
1385 ctx = isl_space_get_ctx(dim);
1386 if (!isl_space_is_set(dim))
1387 isl_die(ctx, isl_error_invalid, "not a set space", goto error);
1388 dim = isl_space_cow(dim);
1389 if (!dim)
1390 return NULL;
1391 if (dim->ids) {
1392 ids = isl_calloc_array(dim->ctx, isl_id *,
1393 dim->nparam + dim->n_out + dim->n_out);
1394 if (!ids)
1395 goto error;
1396 get_ids(dim, isl_dim_param, 0, dim->nparam, ids);
1397 get_ids(dim, isl_dim_out, 0, dim->n_out, ids + dim->nparam);
1399 dim->n_in = dim->n_out;
1400 if (ids) {
1401 free(dim->ids);
1402 dim->ids = ids;
1403 dim->n_id = dim->nparam + dim->n_out + dim->n_out;
1404 dim = copy_ids(dim, isl_dim_out, 0, dim, isl_dim_in);
1406 isl_id_free(dim->tuple_id[0]);
1407 dim->tuple_id[0] = isl_id_copy(dim->tuple_id[1]);
1408 isl_space_free(dim->nested[0]);
1409 dim->nested[0] = isl_space_copy(dim->nested[1]);
1410 return dim;
1411 error:
1412 isl_space_free(dim);
1413 return NULL;
1416 __isl_give isl_space *isl_space_map_from_domain_and_range(
1417 __isl_take isl_space *domain, __isl_take isl_space *range)
1419 if (!domain || !range)
1420 goto error;
1421 if (!isl_space_is_set(domain))
1422 isl_die(isl_space_get_ctx(domain), isl_error_invalid,
1423 "domain is not a set space", goto error);
1424 if (!isl_space_is_set(range))
1425 isl_die(isl_space_get_ctx(range), isl_error_invalid,
1426 "range is not a set space", goto error);
1427 return isl_space_join(isl_space_reverse(domain), range);
1428 error:
1429 isl_space_free(domain);
1430 isl_space_free(range);
1431 return NULL;
1434 static __isl_give isl_space *set_ids(__isl_take isl_space *dim,
1435 enum isl_dim_type type,
1436 unsigned first, unsigned n, __isl_take isl_id **ids)
1438 int i;
1440 for (i = 0; i < n ; ++i)
1441 dim = set_id(dim, type, first + i, ids[i]);
1443 return dim;
1446 __isl_give isl_space *isl_space_reverse(__isl_take isl_space *dim)
1448 unsigned t;
1449 isl_space *nested;
1450 isl_id **ids = NULL;
1451 isl_id *id;
1453 if (!dim)
1454 return NULL;
1455 if (match(dim, isl_dim_in, dim, isl_dim_out))
1456 return dim;
1458 dim = isl_space_cow(dim);
1459 if (!dim)
1460 return NULL;
1462 id = dim->tuple_id[0];
1463 dim->tuple_id[0] = dim->tuple_id[1];
1464 dim->tuple_id[1] = id;
1466 nested = dim->nested[0];
1467 dim->nested[0] = dim->nested[1];
1468 dim->nested[1] = nested;
1470 if (dim->ids) {
1471 int n_id = dim->n_in + dim->n_out;
1472 ids = isl_alloc_array(dim->ctx, isl_id *, n_id);
1473 if (n_id && !ids)
1474 goto error;
1475 get_ids(dim, isl_dim_in, 0, dim->n_in, ids);
1476 get_ids(dim, isl_dim_out, 0, dim->n_out, ids + dim->n_in);
1479 t = dim->n_in;
1480 dim->n_in = dim->n_out;
1481 dim->n_out = t;
1483 if (dim->ids) {
1484 dim = set_ids(dim, isl_dim_out, 0, dim->n_out, ids);
1485 dim = set_ids(dim, isl_dim_in, 0, dim->n_in, ids + dim->n_out);
1486 free(ids);
1489 return dim;
1490 error:
1491 free(ids);
1492 isl_space_free(dim);
1493 return NULL;
1496 __isl_give isl_space *isl_space_drop_dims(__isl_take isl_space *dim,
1497 enum isl_dim_type type, unsigned first, unsigned num)
1499 int i;
1501 if (!dim)
1502 return NULL;
1504 if (num == 0)
1505 return isl_space_reset(dim, type);
1507 if (!valid_dim_type(type))
1508 isl_die(dim->ctx, isl_error_invalid,
1509 "cannot drop dimensions of specified type", goto error);
1511 if (first + num > n(dim, type) || first + num < first)
1512 isl_die(isl_space_get_ctx(dim), isl_error_invalid,
1513 "index out of bounds", return isl_space_free(dim));
1514 dim = isl_space_cow(dim);
1515 if (!dim)
1516 goto error;
1517 if (dim->ids) {
1518 dim = extend_ids(dim);
1519 if (!dim)
1520 goto error;
1521 for (i = 0; i < num; ++i)
1522 isl_id_free(get_id(dim, type, first + i));
1523 for (i = first+num; i < n(dim, type); ++i)
1524 set_id(dim, type, i - num, get_id(dim, type, i));
1525 switch (type) {
1526 case isl_dim_param:
1527 get_ids(dim, isl_dim_in, 0, dim->n_in,
1528 dim->ids + offset(dim, isl_dim_in) - num);
1529 case isl_dim_in:
1530 get_ids(dim, isl_dim_out, 0, dim->n_out,
1531 dim->ids + offset(dim, isl_dim_out) - num);
1532 default:
1535 dim->n_id -= num;
1537 switch (type) {
1538 case isl_dim_param: dim->nparam -= num; break;
1539 case isl_dim_in: dim->n_in -= num; break;
1540 case isl_dim_out: dim->n_out -= num; break;
1541 default: ;
1543 dim = isl_space_reset(dim, type);
1544 if (type == isl_dim_param) {
1545 if (dim && dim->nested[0] &&
1546 !(dim->nested[0] = isl_space_drop_dims(dim->nested[0],
1547 isl_dim_param, first, num)))
1548 goto error;
1549 if (dim && dim->nested[1] &&
1550 !(dim->nested[1] = isl_space_drop_dims(dim->nested[1],
1551 isl_dim_param, first, num)))
1552 goto error;
1554 return dim;
1555 error:
1556 isl_space_free(dim);
1557 return NULL;
1560 __isl_give isl_space *isl_space_drop_inputs(__isl_take isl_space *dim,
1561 unsigned first, unsigned n)
1563 if (!dim)
1564 return NULL;
1565 return isl_space_drop_dims(dim, isl_dim_in, first, n);
1568 __isl_give isl_space *isl_space_drop_outputs(__isl_take isl_space *dim,
1569 unsigned first, unsigned n)
1571 if (!dim)
1572 return NULL;
1573 return isl_space_drop_dims(dim, isl_dim_out, first, n);
1576 __isl_give isl_space *isl_space_domain(__isl_take isl_space *dim)
1578 if (!dim)
1579 return NULL;
1580 dim = isl_space_drop_outputs(dim, 0, dim->n_out);
1581 dim = isl_space_reverse(dim);
1582 dim = mark_as_set(dim);
1583 return dim;
1586 __isl_give isl_space *isl_space_from_domain(__isl_take isl_space *dim)
1588 if (!dim)
1589 return NULL;
1590 if (!isl_space_is_set(dim))
1591 isl_die(isl_space_get_ctx(dim), isl_error_invalid,
1592 "not a set space", goto error);
1593 dim = isl_space_reverse(dim);
1594 dim = isl_space_reset(dim, isl_dim_out);
1595 return dim;
1596 error:
1597 isl_space_free(dim);
1598 return NULL;
1601 __isl_give isl_space *isl_space_range(__isl_take isl_space *dim)
1603 if (!dim)
1604 return NULL;
1605 dim = isl_space_drop_inputs(dim, 0, dim->n_in);
1606 dim = mark_as_set(dim);
1607 return dim;
1610 __isl_give isl_space *isl_space_from_range(__isl_take isl_space *dim)
1612 if (!dim)
1613 return NULL;
1614 if (!isl_space_is_set(dim))
1615 isl_die(isl_space_get_ctx(dim), isl_error_invalid,
1616 "not a set space", goto error);
1617 return isl_space_reset(dim, isl_dim_in);
1618 error:
1619 isl_space_free(dim);
1620 return NULL;
1623 /* Given a map space A -> B, return the map space [A -> B] -> A.
1625 __isl_give isl_space *isl_space_domain_map(__isl_take isl_space *space)
1627 isl_space *domain;
1629 domain = isl_space_from_range(isl_space_domain(isl_space_copy(space)));
1630 space = isl_space_from_domain(isl_space_wrap(space));
1631 space = isl_space_join(space, domain);
1633 return space;
1636 /* Given a map space A -> B, return the map space [A -> B] -> B.
1638 __isl_give isl_space *isl_space_range_map(__isl_take isl_space *space)
1640 isl_space *range;
1642 range = isl_space_from_range(isl_space_range(isl_space_copy(space)));
1643 space = isl_space_from_domain(isl_space_wrap(space));
1644 space = isl_space_join(space, range);
1646 return space;
1649 __isl_give isl_space *isl_space_params(__isl_take isl_space *space)
1651 if (isl_space_is_params(space))
1652 return space;
1653 space = isl_space_drop_dims(space,
1654 isl_dim_in, 0, isl_space_dim(space, isl_dim_in));
1655 space = isl_space_drop_dims(space,
1656 isl_dim_out, 0, isl_space_dim(space, isl_dim_out));
1657 space = mark_as_params(space);
1658 return space;
1661 __isl_give isl_space *isl_space_set_from_params(__isl_take isl_space *space)
1663 if (!space)
1664 return NULL;
1665 if (!isl_space_is_params(space))
1666 isl_die(isl_space_get_ctx(space), isl_error_invalid,
1667 "not a parameter space", goto error);
1668 return isl_space_reset(space, isl_dim_set);
1669 error:
1670 isl_space_free(space);
1671 return NULL;
1674 __isl_give isl_space *isl_space_as_set_space(__isl_take isl_space *dim)
1676 dim = isl_space_cow(dim);
1677 if (!dim)
1678 return NULL;
1680 dim->n_out += dim->n_in;
1681 dim->n_in = 0;
1682 dim = isl_space_reset(dim, isl_dim_in);
1683 dim = isl_space_reset(dim, isl_dim_out);
1685 return dim;
1688 __isl_give isl_space *isl_space_underlying(__isl_take isl_space *dim,
1689 unsigned n_div)
1691 int i;
1693 if (!dim)
1694 return NULL;
1695 if (n_div == 0 &&
1696 dim->nparam == 0 && dim->n_in == 0 && dim->n_id == 0)
1697 return isl_space_reset(isl_space_reset(dim, isl_dim_in), isl_dim_out);
1698 dim = isl_space_cow(dim);
1699 if (!dim)
1700 return NULL;
1701 dim->n_out += dim->nparam + dim->n_in + n_div;
1702 dim->nparam = 0;
1703 dim->n_in = 0;
1705 for (i = 0; i < dim->n_id; ++i)
1706 isl_id_free(get_id(dim, isl_dim_out, i));
1707 dim->n_id = 0;
1708 dim = isl_space_reset(dim, isl_dim_in);
1709 dim = isl_space_reset(dim, isl_dim_out);
1711 return dim;
1714 /* Are the two spaces the same, including positions and names of parameters?
1716 int isl_space_is_equal(__isl_keep isl_space *dim1, __isl_keep isl_space *dim2)
1718 if (!dim1 || !dim2)
1719 return -1;
1720 if (dim1 == dim2)
1721 return 1;
1722 return match(dim1, isl_dim_param, dim2, isl_dim_param) &&
1723 isl_space_tuple_match(dim1, isl_dim_in, dim2, isl_dim_in) &&
1724 isl_space_tuple_match(dim1, isl_dim_out, dim2, isl_dim_out);
1727 /* Is space1 equal to the domain of space2?
1729 * In the internal version we also allow space2 to be the space of a set,
1730 * provided space1 is a parameter space.
1732 int isl_space_is_domain_internal(__isl_keep isl_space *space1,
1733 __isl_keep isl_space *space2)
1735 if (!space1 || !space2)
1736 return -1;
1737 if (!isl_space_is_set(space1))
1738 return 0;
1739 return match(space1, isl_dim_param, space2, isl_dim_param) &&
1740 isl_space_tuple_match(space1, isl_dim_set, space2, isl_dim_in);
1743 /* Is space1 equal to the domain of space2?
1745 int isl_space_is_domain(__isl_keep isl_space *space1,
1746 __isl_keep isl_space *space2)
1748 if (!space2)
1749 return -1;
1750 if (!isl_space_is_map(space2))
1751 return 0;
1752 return isl_space_is_domain_internal(space1, space2);
1755 /* Is space1 equal to the range of space2?
1757 * In the internal version, space2 is allowed to be the space of a set,
1758 * in which case it should be equal to space1.
1760 int isl_space_is_range_internal(__isl_keep isl_space *space1,
1761 __isl_keep isl_space *space2)
1763 if (!space1 || !space2)
1764 return -1;
1765 if (!isl_space_is_set(space1))
1766 return 0;
1767 return match(space1, isl_dim_param, space2, isl_dim_param) &&
1768 isl_space_tuple_match(space1, isl_dim_set, space2, isl_dim_out);
1771 /* Is space1 equal to the range of space2?
1773 int isl_space_is_range(__isl_keep isl_space *space1,
1774 __isl_keep isl_space *space2)
1776 if (!space2)
1777 return -1;
1778 if (!isl_space_is_map(space2))
1779 return 0;
1780 return isl_space_is_range_internal(space1, space2);
1783 int isl_space_compatible(__isl_keep isl_space *dim1,
1784 __isl_keep isl_space *dim2)
1786 return dim1->nparam == dim2->nparam &&
1787 dim1->n_in + dim1->n_out == dim2->n_in + dim2->n_out;
1790 static uint32_t isl_hash_dim(uint32_t hash, __isl_keep isl_space *dim)
1792 int i;
1793 isl_id *id;
1795 if (!dim)
1796 return hash;
1798 isl_hash_byte(hash, dim->nparam % 256);
1799 isl_hash_byte(hash, dim->n_in % 256);
1800 isl_hash_byte(hash, dim->n_out % 256);
1802 for (i = 0; i < dim->nparam; ++i) {
1803 id = get_id(dim, isl_dim_param, i);
1804 hash = isl_hash_id(hash, id);
1807 id = tuple_id(dim, isl_dim_in);
1808 hash = isl_hash_id(hash, id);
1809 id = tuple_id(dim, isl_dim_out);
1810 hash = isl_hash_id(hash, id);
1812 hash = isl_hash_dim(hash, dim->nested[0]);
1813 hash = isl_hash_dim(hash, dim->nested[1]);
1815 return hash;
1818 uint32_t isl_space_get_hash(__isl_keep isl_space *dim)
1820 uint32_t hash;
1822 if (!dim)
1823 return 0;
1825 hash = isl_hash_init();
1826 hash = isl_hash_dim(hash, dim);
1828 return hash;
1831 int isl_space_is_wrapping(__isl_keep isl_space *dim)
1833 if (!dim)
1834 return -1;
1836 if (!isl_space_is_set(dim))
1837 return 0;
1839 return dim->nested[1] != NULL;
1842 /* Is "space" the space of a map where the domain is a wrapped map space?
1844 int isl_space_domain_is_wrapping(__isl_keep isl_space *space)
1846 if (!space)
1847 return -1;
1849 if (isl_space_is_set(space))
1850 return 0;
1852 return space->nested[0] != NULL;
1855 /* Is "space" the space of a map where the range is a wrapped map space?
1857 int isl_space_range_is_wrapping(__isl_keep isl_space *space)
1859 if (!space)
1860 return -1;
1862 if (isl_space_is_set(space))
1863 return 0;
1865 return space->nested[1] != NULL;
1868 __isl_give isl_space *isl_space_wrap(__isl_take isl_space *dim)
1870 isl_space *wrap;
1872 if (!dim)
1873 return NULL;
1875 wrap = isl_space_set_alloc(dim->ctx,
1876 dim->nparam, dim->n_in + dim->n_out);
1878 wrap = copy_ids(wrap, isl_dim_param, 0, dim, isl_dim_param);
1879 wrap = copy_ids(wrap, isl_dim_set, 0, dim, isl_dim_in);
1880 wrap = copy_ids(wrap, isl_dim_set, dim->n_in, dim, isl_dim_out);
1882 if (!wrap)
1883 goto error;
1885 wrap->nested[1] = dim;
1887 return wrap;
1888 error:
1889 isl_space_free(dim);
1890 return NULL;
1893 __isl_give isl_space *isl_space_unwrap(__isl_take isl_space *dim)
1895 isl_space *unwrap;
1897 if (!dim)
1898 return NULL;
1900 if (!isl_space_is_wrapping(dim))
1901 isl_die(dim->ctx, isl_error_invalid, "not a wrapping space",
1902 goto error);
1904 unwrap = isl_space_copy(dim->nested[1]);
1905 isl_space_free(dim);
1907 return unwrap;
1908 error:
1909 isl_space_free(dim);
1910 return NULL;
1913 int isl_space_is_named_or_nested(__isl_keep isl_space *dim, enum isl_dim_type type)
1915 if (type != isl_dim_in && type != isl_dim_out)
1916 return 0;
1917 if (!dim)
1918 return -1;
1919 if (dim->tuple_id[type - isl_dim_in])
1920 return 1;
1921 if (dim->nested[type - isl_dim_in])
1922 return 1;
1923 return 0;
1926 int isl_space_may_be_set(__isl_keep isl_space *dim)
1928 if (!dim)
1929 return -1;
1930 if (isl_space_is_set(dim))
1931 return 1;
1932 if (isl_space_dim(dim, isl_dim_in) != 0)
1933 return 0;
1934 if (isl_space_is_named_or_nested(dim, isl_dim_in))
1935 return 0;
1936 return 1;
1939 __isl_give isl_space *isl_space_reset(__isl_take isl_space *dim,
1940 enum isl_dim_type type)
1942 if (!isl_space_is_named_or_nested(dim, type))
1943 return dim;
1945 dim = isl_space_cow(dim);
1946 if (!dim)
1947 return NULL;
1949 isl_id_free(dim->tuple_id[type - isl_dim_in]);
1950 dim->tuple_id[type - isl_dim_in] = NULL;
1951 isl_space_free(dim->nested[type - isl_dim_in]);
1952 dim->nested[type - isl_dim_in] = NULL;
1954 return dim;
1957 __isl_give isl_space *isl_space_flatten(__isl_take isl_space *dim)
1959 if (!dim)
1960 return NULL;
1961 if (!dim->nested[0] && !dim->nested[1])
1962 return dim;
1964 if (dim->nested[0])
1965 dim = isl_space_reset(dim, isl_dim_in);
1966 if (dim && dim->nested[1])
1967 dim = isl_space_reset(dim, isl_dim_out);
1969 return dim;
1972 __isl_give isl_space *isl_space_flatten_domain(__isl_take isl_space *dim)
1974 if (!dim)
1975 return NULL;
1976 if (!dim->nested[0])
1977 return dim;
1979 return isl_space_reset(dim, isl_dim_in);
1982 __isl_give isl_space *isl_space_flatten_range(__isl_take isl_space *dim)
1984 if (!dim)
1985 return NULL;
1986 if (!dim->nested[1])
1987 return dim;
1989 return isl_space_reset(dim, isl_dim_out);
1992 /* Replace the dimensions of the given type of dst by those of src.
1994 __isl_give isl_space *isl_space_replace(__isl_take isl_space *dst,
1995 enum isl_dim_type type, __isl_keep isl_space *src)
1997 dst = isl_space_cow(dst);
1999 if (!dst || !src)
2000 goto error;
2002 dst = isl_space_drop_dims(dst, type, 0, isl_space_dim(dst, type));
2003 dst = isl_space_add_dims(dst, type, isl_space_dim(src, type));
2004 dst = copy_ids(dst, type, 0, src, type);
2006 if (dst && type == isl_dim_param) {
2007 int i;
2008 for (i = 0; i <= 1; ++i) {
2009 if (!dst->nested[i])
2010 continue;
2011 dst->nested[i] = isl_space_replace(dst->nested[i],
2012 type, src);
2013 if (!dst->nested[i])
2014 goto error;
2018 return dst;
2019 error:
2020 isl_space_free(dst);
2021 return NULL;
2024 /* Given a dimension specification "dim" of a set, create a dimension
2025 * specification for the lift of the set. In particular, the result
2026 * is of the form [dim -> local[..]], with n_local variables in the
2027 * range of the wrapped map.
2029 __isl_give isl_space *isl_space_lift(__isl_take isl_space *dim, unsigned n_local)
2031 isl_space *local_dim;
2033 if (!dim)
2034 return NULL;
2036 local_dim = isl_space_dup(dim);
2037 local_dim = isl_space_drop_dims(local_dim, isl_dim_set, 0, dim->n_out);
2038 local_dim = isl_space_add_dims(local_dim, isl_dim_set, n_local);
2039 local_dim = isl_space_set_tuple_name(local_dim, isl_dim_set, "local");
2040 dim = isl_space_join(isl_space_from_domain(dim),
2041 isl_space_from_range(local_dim));
2042 dim = isl_space_wrap(dim);
2043 dim = isl_space_set_tuple_name(dim, isl_dim_set, "lifted");
2045 return dim;
2048 int isl_space_can_zip(__isl_keep isl_space *dim)
2050 if (!dim)
2051 return -1;
2053 return dim->nested[0] && dim->nested[1];
2056 __isl_give isl_space *isl_space_zip(__isl_take isl_space *dim)
2058 isl_space *dom, *ran;
2059 isl_space *dom_dom, *dom_ran, *ran_dom, *ran_ran;
2061 if (!isl_space_can_zip(dim))
2062 isl_die(dim->ctx, isl_error_invalid, "dim cannot be zipped",
2063 goto error);
2065 if (!dim)
2066 return NULL;
2067 dom = isl_space_unwrap(isl_space_domain(isl_space_copy(dim)));
2068 ran = isl_space_unwrap(isl_space_range(dim));
2069 dom_dom = isl_space_domain(isl_space_copy(dom));
2070 dom_ran = isl_space_range(dom);
2071 ran_dom = isl_space_domain(isl_space_copy(ran));
2072 ran_ran = isl_space_range(ran);
2073 dom = isl_space_join(isl_space_from_domain(dom_dom),
2074 isl_space_from_range(ran_dom));
2075 ran = isl_space_join(isl_space_from_domain(dom_ran),
2076 isl_space_from_range(ran_ran));
2077 return isl_space_join(isl_space_from_domain(isl_space_wrap(dom)),
2078 isl_space_from_range(isl_space_wrap(ran)));
2079 error:
2080 isl_space_free(dim);
2081 return NULL;
2084 /* Can we apply isl_space_curry to "space"?
2085 * That is, does it have a nested relation in its domain?
2087 int isl_space_can_curry(__isl_keep isl_space *space)
2089 if (!space)
2090 return -1;
2092 return !!space->nested[0];
2095 /* Given a space (A -> B) -> C, return the corresponding space
2096 * A -> (B -> C).
2098 __isl_give isl_space *isl_space_curry(__isl_take isl_space *space)
2100 isl_space *dom, *ran;
2101 isl_space *dom_dom, *dom_ran;
2103 if (!space)
2104 return NULL;
2106 if (!isl_space_can_curry(space))
2107 isl_die(space->ctx, isl_error_invalid,
2108 "space cannot be curried", goto error);
2110 dom = isl_space_unwrap(isl_space_domain(isl_space_copy(space)));
2111 ran = isl_space_range(space);
2112 dom_dom = isl_space_domain(isl_space_copy(dom));
2113 dom_ran = isl_space_range(dom);
2114 ran = isl_space_join(isl_space_from_domain(dom_ran),
2115 isl_space_from_range(ran));
2116 return isl_space_join(isl_space_from_domain(dom_dom),
2117 isl_space_from_range(isl_space_wrap(ran)));
2118 error:
2119 isl_space_free(space);
2120 return NULL;
2123 /* Can we apply isl_space_uncurry to "space"?
2124 * That is, does it have a nested relation in its range?
2126 int isl_space_can_uncurry(__isl_keep isl_space *space)
2128 if (!space)
2129 return -1;
2131 return !!space->nested[1];
2134 /* Given a space A -> (B -> C), return the corresponding space
2135 * (A -> B) -> C.
2137 __isl_give isl_space *isl_space_uncurry(__isl_take isl_space *space)
2139 isl_space *dom, *ran;
2140 isl_space *ran_dom, *ran_ran;
2142 if (!space)
2143 return NULL;
2145 if (!isl_space_can_uncurry(space))
2146 isl_die(space->ctx, isl_error_invalid,
2147 "space cannot be uncurried",
2148 return isl_space_free(space));
2150 dom = isl_space_domain(isl_space_copy(space));
2151 ran = isl_space_unwrap(isl_space_range(space));
2152 ran_dom = isl_space_domain(isl_space_copy(ran));
2153 ran_ran = isl_space_range(ran);
2154 dom = isl_space_join(isl_space_from_domain(dom),
2155 isl_space_from_range(ran_dom));
2156 return isl_space_join(isl_space_from_domain(isl_space_wrap(dom)),
2157 isl_space_from_range(ran_ran));
2160 int isl_space_has_named_params(__isl_keep isl_space *dim)
2162 int i;
2163 unsigned off;
2165 if (!dim)
2166 return -1;
2167 if (dim->nparam == 0)
2168 return 1;
2169 off = isl_space_offset(dim, isl_dim_param);
2170 if (off + dim->nparam > dim->n_id)
2171 return 0;
2172 for (i = 0; i < dim->nparam; ++i)
2173 if (!dim->ids[off + i])
2174 return 0;
2175 return 1;
2178 /* Align the initial parameters of dim1 to match the order in dim2.
2180 __isl_give isl_space *isl_space_align_params(__isl_take isl_space *dim1,
2181 __isl_take isl_space *dim2)
2183 isl_reordering *exp;
2185 if (!isl_space_has_named_params(dim1) || !isl_space_has_named_params(dim2))
2186 isl_die(isl_space_get_ctx(dim1), isl_error_invalid,
2187 "parameter alignment requires named parameters",
2188 goto error);
2190 dim2 = isl_space_params(dim2);
2191 exp = isl_parameter_alignment_reordering(dim1, dim2);
2192 exp = isl_reordering_extend_space(exp, dim1);
2193 isl_space_free(dim2);
2194 if (!exp)
2195 return NULL;
2196 dim1 = isl_space_copy(exp->dim);
2197 isl_reordering_free(exp);
2198 return dim1;
2199 error:
2200 isl_space_free(dim1);
2201 isl_space_free(dim2);
2202 return NULL;
2205 /* Given the space of set (domain), construct a space for a map
2206 * with as domain the given space and as range the range of "model".
2208 __isl_give isl_space *isl_space_extend_domain_with_range(
2209 __isl_take isl_space *space, __isl_take isl_space *model)
2211 if (!model)
2212 goto error;
2214 space = isl_space_from_domain(space);
2215 space = isl_space_add_dims(space, isl_dim_out,
2216 isl_space_dim(model, isl_dim_out));
2217 if (isl_space_has_tuple_id(model, isl_dim_out))
2218 space = isl_space_set_tuple_id(space, isl_dim_out,
2219 isl_space_get_tuple_id(model, isl_dim_out));
2220 if (!space)
2221 goto error;
2222 if (model->nested[1]) {
2223 isl_space *nested = isl_space_copy(model->nested[1]);
2224 int n_nested, n_space;
2225 nested = isl_space_align_params(nested, isl_space_copy(space));
2226 n_nested = isl_space_dim(nested, isl_dim_param);
2227 n_space = isl_space_dim(space, isl_dim_param);
2228 if (n_nested > n_space)
2229 nested = isl_space_drop_dims(nested, isl_dim_param,
2230 n_space, n_nested - n_space);
2231 if (!nested)
2232 goto error;
2233 space->nested[1] = nested;
2235 isl_space_free(model);
2236 return space;
2237 error:
2238 isl_space_free(model);
2239 isl_space_free(space);
2240 return NULL;
2243 /* Compare the "type" dimensions of two isl_spaces.
2245 * The order is fairly arbitrary.
2247 static int isl_space_cmp_type(__isl_keep isl_space *space1,
2248 __isl_keep isl_space *space2, enum isl_dim_type type)
2250 int cmp;
2251 isl_space *nested1, *nested2;
2253 if (isl_space_dim(space1, type) != isl_space_dim(space2, type))
2254 return isl_space_dim(space1, type) -
2255 isl_space_dim(space2, type);
2257 cmp = isl_id_cmp(tuple_id(space1, type), tuple_id(space2, type));
2258 if (cmp != 0)
2259 return cmp;
2261 nested1 = nested(space1, type);
2262 nested2 = nested(space2, type);
2263 if (!nested1 != !nested2)
2264 return !nested1 - !nested2;
2266 if (nested1)
2267 return isl_space_cmp(nested1, nested2);
2269 return 0;
2272 /* Compare two isl_spaces.
2274 * The order is fairly arbitrary.
2276 int isl_space_cmp(__isl_keep isl_space *space1, __isl_keep isl_space *space2)
2278 int i;
2279 int cmp;
2281 if (space1 == space2)
2282 return 0;
2283 if (!space1)
2284 return -1;
2285 if (!space2)
2286 return 1;
2288 cmp = isl_space_cmp_type(space1, space2, isl_dim_param);
2289 if (cmp != 0)
2290 return cmp;
2291 cmp = isl_space_cmp_type(space1, space2, isl_dim_in);
2292 if (cmp != 0)
2293 return cmp;
2294 cmp = isl_space_cmp_type(space1, space2, isl_dim_out);
2295 if (cmp != 0)
2296 return cmp;
2298 if (!space1->ids && !space2->ids)
2299 return 0;
2301 for (i = 0; i < n(space1, isl_dim_param); ++i) {
2302 cmp = isl_id_cmp(get_id(space1, isl_dim_param, i),
2303 get_id(space2, isl_dim_param, i));
2304 if (cmp != 0)
2305 return cmp;
2308 return 0;