extract out shared isl_space_check_range
[isl.git] / isl_space.c
blobe4be128e314ab9a97c98befc56ac25a3875bc0ea
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 *space)
23 return space ? space->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 *space;
31 space = isl_alloc_type(ctx, struct isl_space);
32 if (!space)
33 return NULL;
35 space->ctx = ctx;
36 isl_ctx_ref(ctx);
37 space->ref = 1;
38 space->nparam = nparam;
39 space->n_in = n_in;
40 space->n_out = n_out;
42 space->tuple_id[0] = NULL;
43 space->tuple_id[1] = NULL;
45 space->nested[0] = NULL;
46 space->nested[1] = NULL;
48 space->n_id = 0;
49 space->ids = NULL;
51 return space;
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 isl_bool isl_space_is_set(__isl_keep isl_space *space)
70 if (!space)
71 return isl_bool_error;
72 if (space->n_in != 0 || space->nested[0])
73 return isl_bool_false;
74 if (space->tuple_id[0] != &isl_id_none)
75 return isl_bool_false;
76 return isl_bool_true;
79 /* Check that "space" is a set space.
81 isl_stat isl_space_check_is_set(__isl_keep isl_space *space)
83 isl_bool is_set;
85 is_set = isl_space_is_set(space);
86 if (is_set < 0)
87 return isl_stat_error;
88 if (!is_set)
89 isl_die(isl_space_get_ctx(space), isl_error_invalid,
90 "space is not a set", return isl_stat_error);
91 return isl_stat_ok;
94 /* Is the given space that of a map?
96 isl_bool isl_space_is_map(__isl_keep isl_space *space)
98 if (!space)
99 return isl_bool_error;
100 return space->tuple_id[0] != &isl_id_none &&
101 space->tuple_id[1] != &isl_id_none;
104 __isl_give isl_space *isl_space_set_alloc(isl_ctx *ctx,
105 unsigned nparam, unsigned dim)
107 isl_space *space;
108 space = isl_space_alloc(ctx, nparam, 0, dim);
109 space = mark_as_set(space);
110 return space;
113 /* Mark the space as being that of a parameter domain, by setting
114 * both tuples to isl_id_none.
116 static __isl_give isl_space *mark_as_params(isl_space *space)
118 if (!space)
119 return NULL;
120 space = isl_space_set_tuple_id(space, isl_dim_in, &isl_id_none);
121 space = isl_space_set_tuple_id(space, isl_dim_out, &isl_id_none);
122 return space;
125 /* Is the space that of a parameter domain?
127 isl_bool isl_space_is_params(__isl_keep isl_space *space)
129 if (!space)
130 return isl_bool_error;
131 if (space->n_in != 0 || space->nested[0] ||
132 space->n_out != 0 || space->nested[1])
133 return isl_bool_false;
134 if (space->tuple_id[0] != &isl_id_none)
135 return isl_bool_false;
136 if (space->tuple_id[1] != &isl_id_none)
137 return isl_bool_false;
138 return isl_bool_true;
141 /* Create a space for a parameter domain.
143 __isl_give isl_space *isl_space_params_alloc(isl_ctx *ctx, unsigned nparam)
145 isl_space *space;
146 space = isl_space_alloc(ctx, nparam, 0, 0);
147 space = mark_as_params(space);
148 return space;
151 static unsigned global_pos(__isl_keep isl_space *space,
152 enum isl_dim_type type, unsigned pos)
154 struct isl_ctx *ctx = space->ctx;
156 if (isl_space_check_range(space, type, pos, 1) < 0)
157 return isl_space_dim(space, isl_dim_all);
159 switch (type) {
160 case isl_dim_param:
161 return pos;
162 case isl_dim_in:
163 return pos + space->nparam;
164 case isl_dim_out:
165 return pos + space->nparam + space->n_in;
166 default:
167 isl_assert(ctx, 0, return isl_space_dim(space, isl_dim_all));
169 return isl_space_dim(space, isl_dim_all);
172 /* Extend length of ids array to the total number of dimensions.
174 static __isl_give isl_space *extend_ids(__isl_take isl_space *space)
176 isl_id **ids;
177 int i;
179 if (isl_space_dim(space, isl_dim_all) <= space->n_id)
180 return space;
182 if (!space->ids) {
183 space->ids = isl_calloc_array(space->ctx,
184 isl_id *, isl_space_dim(space, isl_dim_all));
185 if (!space->ids)
186 goto error;
187 } else {
188 ids = isl_realloc_array(space->ctx, space->ids,
189 isl_id *, isl_space_dim(space, isl_dim_all));
190 if (!ids)
191 goto error;
192 space->ids = ids;
193 for (i = space->n_id; i < isl_space_dim(space, isl_dim_all); ++i)
194 space->ids[i] = NULL;
197 space->n_id = isl_space_dim(space, isl_dim_all);
199 return space;
200 error:
201 isl_space_free(space);
202 return NULL;
205 static __isl_give isl_space *set_id(__isl_take isl_space *space,
206 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
208 space = isl_space_cow(space);
210 if (!space)
211 goto error;
213 pos = global_pos(space, type, pos);
214 if (pos == isl_space_dim(space, isl_dim_all))
215 goto error;
217 if (pos >= space->n_id) {
218 if (!id)
219 return space;
220 space = extend_ids(space);
221 if (!space)
222 goto error;
225 space->ids[pos] = id;
227 return space;
228 error:
229 isl_id_free(id);
230 isl_space_free(space);
231 return NULL;
234 static __isl_keep isl_id *get_id(__isl_keep isl_space *space,
235 enum isl_dim_type type, unsigned pos)
237 if (!space)
238 return NULL;
240 pos = global_pos(space, type, pos);
241 if (pos == isl_space_dim(space, isl_dim_all))
242 return NULL;
243 if (pos >= space->n_id)
244 return NULL;
245 return space->ids[pos];
248 static unsigned offset(__isl_keep isl_space *space, enum isl_dim_type type)
250 switch (type) {
251 case isl_dim_param: return 0;
252 case isl_dim_in: return space->nparam;
253 case isl_dim_out: return space->nparam + space->n_in;
254 default: return 0;
258 static unsigned n(__isl_keep isl_space *space, enum isl_dim_type type)
260 switch (type) {
261 case isl_dim_param: return space->nparam;
262 case isl_dim_in: return space->n_in;
263 case isl_dim_out: return space->n_out;
264 case isl_dim_all:
265 return space->nparam + space->n_in + space->n_out;
266 default: return 0;
270 unsigned isl_space_dim(__isl_keep isl_space *space, enum isl_dim_type type)
272 if (!space)
273 return 0;
274 return n(space, type);
277 unsigned isl_space_offset(__isl_keep isl_space *dim, enum isl_dim_type type)
279 if (!dim)
280 return 0;
281 return offset(dim, type);
284 static __isl_give isl_space *copy_ids(__isl_take isl_space *dst,
285 enum isl_dim_type dst_type, unsigned offset, __isl_keep isl_space *src,
286 enum isl_dim_type src_type)
288 int i;
289 isl_id *id;
291 if (!dst)
292 return NULL;
294 for (i = 0; i < n(src, src_type); ++i) {
295 id = get_id(src, src_type, i);
296 if (!id)
297 continue;
298 dst = set_id(dst, dst_type, offset + i, isl_id_copy(id));
299 if (!dst)
300 return NULL;
302 return dst;
305 __isl_take isl_space *isl_space_dup(__isl_keep isl_space *space)
307 isl_space *dup;
308 if (!space)
309 return NULL;
310 dup = isl_space_alloc(space->ctx,
311 space->nparam, space->n_in, space->n_out);
312 if (!dup)
313 return NULL;
314 if (space->tuple_id[0] &&
315 !(dup->tuple_id[0] = isl_id_copy(space->tuple_id[0])))
316 goto error;
317 if (space->tuple_id[1] &&
318 !(dup->tuple_id[1] = isl_id_copy(space->tuple_id[1])))
319 goto error;
320 if (space->nested[0] &&
321 !(dup->nested[0] = isl_space_copy(space->nested[0])))
322 goto error;
323 if (space->nested[1] &&
324 !(dup->nested[1] = isl_space_copy(space->nested[1])))
325 goto error;
326 if (!space->ids)
327 return dup;
328 dup = copy_ids(dup, isl_dim_param, 0, space, isl_dim_param);
329 dup = copy_ids(dup, isl_dim_in, 0, space, isl_dim_in);
330 dup = copy_ids(dup, isl_dim_out, 0, space, isl_dim_out);
331 return dup;
332 error:
333 isl_space_free(dup);
334 return NULL;
337 __isl_give isl_space *isl_space_cow(__isl_take isl_space *space)
339 if (!space)
340 return NULL;
342 if (space->ref == 1)
343 return space;
344 space->ref--;
345 return isl_space_dup(space);
348 __isl_give isl_space *isl_space_copy(__isl_keep isl_space *dim)
350 if (!dim)
351 return NULL;
353 dim->ref++;
354 return dim;
357 __isl_null isl_space *isl_space_free(__isl_take isl_space *space)
359 int i;
361 if (!space)
362 return NULL;
364 if (--space->ref > 0)
365 return NULL;
367 isl_id_free(space->tuple_id[0]);
368 isl_id_free(space->tuple_id[1]);
370 isl_space_free(space->nested[0]);
371 isl_space_free(space->nested[1]);
373 for (i = 0; i < space->n_id; ++i)
374 isl_id_free(space->ids[i]);
375 free(space->ids);
376 isl_ctx_deref(space->ctx);
378 free(space);
380 return NULL;
383 /* Check if "s" is a valid dimension or tuple name.
384 * We currently only forbid names that look like a number.
386 * s is assumed to be non-NULL.
388 static int name_ok(isl_ctx *ctx, const char *s)
390 char *p;
391 long dummy;
393 dummy = strtol(s, &p, 0);
394 if (p != s)
395 isl_die(ctx, isl_error_invalid, "name looks like a number",
396 return 0);
398 return 1;
401 /* Is it possible for the given dimension type to have a tuple id?
403 static int space_can_have_id(__isl_keep isl_space *space,
404 enum isl_dim_type type)
406 if (!space)
407 return 0;
408 if (isl_space_is_params(space))
409 isl_die(space->ctx, isl_error_invalid,
410 "parameter spaces don't have tuple ids", return 0);
411 if (isl_space_is_set(space) && type != isl_dim_set)
412 isl_die(space->ctx, isl_error_invalid,
413 "set spaces can only have a set id", return 0);
414 if (type != isl_dim_in && type != isl_dim_out)
415 isl_die(space->ctx, isl_error_invalid,
416 "only input, output and set tuples can have ids",
417 return 0);
419 return 1;
422 /* Does the tuple have an id?
424 isl_bool isl_space_has_tuple_id(__isl_keep isl_space *space,
425 enum isl_dim_type type)
427 if (!space_can_have_id(space, type))
428 return isl_bool_error;
429 return space->tuple_id[type - isl_dim_in] != NULL;
432 __isl_give isl_id *isl_space_get_tuple_id(__isl_keep isl_space *space,
433 enum isl_dim_type type)
435 int has_id;
437 if (!space)
438 return NULL;
439 has_id = isl_space_has_tuple_id(space, type);
440 if (has_id < 0)
441 return NULL;
442 if (!has_id)
443 isl_die(space->ctx, isl_error_invalid,
444 "tuple has no id", return NULL);
445 return isl_id_copy(space->tuple_id[type - isl_dim_in]);
448 __isl_give isl_space *isl_space_set_tuple_id(__isl_take isl_space *space,
449 enum isl_dim_type type, __isl_take isl_id *id)
451 space = isl_space_cow(space);
452 if (!space || !id)
453 goto error;
454 if (type != isl_dim_in && type != isl_dim_out)
455 isl_die(space->ctx, isl_error_invalid,
456 "only input, output and set tuples can have names",
457 goto error);
459 isl_id_free(space->tuple_id[type - isl_dim_in]);
460 space->tuple_id[type - isl_dim_in] = id;
462 return space;
463 error:
464 isl_id_free(id);
465 isl_space_free(space);
466 return NULL;
469 __isl_give isl_space *isl_space_reset_tuple_id(__isl_take isl_space *space,
470 enum isl_dim_type type)
472 space = isl_space_cow(space);
473 if (!space)
474 return NULL;
475 if (type != isl_dim_in && type != isl_dim_out)
476 isl_die(space->ctx, isl_error_invalid,
477 "only input, output and set tuples can have names",
478 goto error);
480 isl_id_free(space->tuple_id[type - isl_dim_in]);
481 space->tuple_id[type - isl_dim_in] = NULL;
483 return space;
484 error:
485 isl_space_free(space);
486 return NULL;
489 /* Set the id of the given dimension of "space" to "id".
490 * If the dimension already has an id, then it is replaced.
491 * If the dimension is a parameter, then we need to change it
492 * in the nested spaces (if any) as well.
494 __isl_give isl_space *isl_space_set_dim_id(__isl_take isl_space *space,
495 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
497 space = isl_space_cow(space);
498 if (!space || !id)
499 goto error;
501 if (type == isl_dim_param) {
502 int i;
504 for (i = 0; i < 2; ++i) {
505 if (!space->nested[i])
506 continue;
507 space->nested[i] =
508 isl_space_set_dim_id(space->nested[i],
509 type, pos, isl_id_copy(id));
510 if (!space->nested[i])
511 goto error;
515 isl_id_free(get_id(space, type, pos));
516 return set_id(space, type, pos, id);
517 error:
518 isl_id_free(id);
519 isl_space_free(space);
520 return NULL;
523 /* Reset the id of the given dimension of "space".
524 * If the dimension already has an id, then it is removed.
525 * If the dimension is a parameter, then we need to reset it
526 * in the nested spaces (if any) as well.
528 __isl_give isl_space *isl_space_reset_dim_id(__isl_take isl_space *space,
529 enum isl_dim_type type, unsigned pos)
531 space = isl_space_cow(space);
532 if (!space)
533 goto error;
535 if (type == isl_dim_param) {
536 int i;
538 for (i = 0; i < 2; ++i) {
539 if (!space->nested[i])
540 continue;
541 space->nested[i] =
542 isl_space_reset_dim_id(space->nested[i],
543 type, pos);
544 if (!space->nested[i])
545 goto error;
549 isl_id_free(get_id(space, type, pos));
550 return set_id(space, type, pos, NULL);
551 error:
552 isl_space_free(space);
553 return NULL;
556 isl_bool isl_space_has_dim_id(__isl_keep isl_space *space,
557 enum isl_dim_type type, unsigned pos)
559 if (!space)
560 return isl_bool_error;
561 return get_id(space, type, pos) != NULL;
564 __isl_give isl_id *isl_space_get_dim_id(__isl_keep isl_space *space,
565 enum isl_dim_type type, unsigned pos)
567 if (!space)
568 return NULL;
569 if (!get_id(space, type, pos))
570 isl_die(space->ctx, isl_error_invalid,
571 "dim has no id", return NULL);
572 return isl_id_copy(get_id(space, type, pos));
575 __isl_give isl_space *isl_space_set_tuple_name(__isl_take isl_space *space,
576 enum isl_dim_type type, const char *s)
578 isl_id *id;
580 if (!space)
581 return NULL;
583 if (!s)
584 return isl_space_reset_tuple_id(space, type);
586 if (!name_ok(space->ctx, s))
587 goto error;
589 id = isl_id_alloc(space->ctx, s, NULL);
590 return isl_space_set_tuple_id(space, type, id);
591 error:
592 isl_space_free(space);
593 return NULL;
596 /* Does the tuple have a name?
598 isl_bool isl_space_has_tuple_name(__isl_keep isl_space *space,
599 enum isl_dim_type type)
601 isl_id *id;
603 if (!space_can_have_id(space, type))
604 return isl_bool_error;
605 id = space->tuple_id[type - isl_dim_in];
606 return id && id->name;
609 __isl_keep const char *isl_space_get_tuple_name(__isl_keep isl_space *space,
610 enum isl_dim_type type)
612 isl_id *id;
613 if (!space)
614 return NULL;
615 if (type != isl_dim_in && type != isl_dim_out)
616 return NULL;
617 id = space->tuple_id[type - isl_dim_in];
618 return id ? id->name : NULL;
621 __isl_give isl_space *isl_space_set_dim_name(__isl_take isl_space *space,
622 enum isl_dim_type type, unsigned pos,
623 const char *s)
625 isl_id *id;
627 if (!space)
628 return NULL;
629 if (!s)
630 return isl_space_reset_dim_id(space, type, pos);
631 if (!name_ok(space->ctx, s))
632 goto error;
633 id = isl_id_alloc(space->ctx, s, NULL);
634 return isl_space_set_dim_id(space, type, pos, id);
635 error:
636 isl_space_free(space);
637 return NULL;
640 /* Does the given dimension have a name?
642 isl_bool isl_space_has_dim_name(__isl_keep isl_space *space,
643 enum isl_dim_type type, unsigned pos)
645 isl_id *id;
647 if (!space)
648 return isl_bool_error;
649 id = get_id(space, type, pos);
650 return id && id->name;
653 __isl_keep const char *isl_space_get_dim_name(__isl_keep isl_space *space,
654 enum isl_dim_type type, unsigned pos)
656 isl_id *id = get_id(space, type, pos);
657 return id ? id->name : NULL;
660 int isl_space_find_dim_by_id(__isl_keep isl_space *space,
661 enum isl_dim_type type, __isl_keep isl_id *id)
663 int i;
664 int offset;
665 int n;
667 if (!space || !id)
668 return -1;
670 offset = isl_space_offset(space, type);
671 n = isl_space_dim(space, type);
672 for (i = 0; i < n && offset + i < space->n_id; ++i)
673 if (space->ids[offset + i] == id)
674 return i;
676 return -1;
679 int isl_space_find_dim_by_name(__isl_keep isl_space *space,
680 enum isl_dim_type type, const char *name)
682 int i;
683 int offset;
684 int n;
686 if (!space || !name)
687 return -1;
689 offset = isl_space_offset(space, type);
690 n = isl_space_dim(space, type);
691 for (i = 0; i < n && offset + i < space->n_id; ++i) {
692 isl_id *id = get_id(space, type, i);
693 if (id && id->name && !strcmp(id->name, name))
694 return i;
697 return -1;
700 /* Reset the user pointer on all identifiers of parameters and tuples
701 * of "space".
703 __isl_give isl_space *isl_space_reset_user(__isl_take isl_space *space)
705 int i;
706 isl_ctx *ctx;
707 isl_id *id;
708 const char *name;
710 if (!space)
711 return NULL;
713 ctx = isl_space_get_ctx(space);
715 for (i = 0; i < space->nparam && i < space->n_id; ++i) {
716 if (!isl_id_get_user(space->ids[i]))
717 continue;
718 space = isl_space_cow(space);
719 if (!space)
720 return NULL;
721 name = isl_id_get_name(space->ids[i]);
722 id = isl_id_alloc(ctx, name, NULL);
723 isl_id_free(space->ids[i]);
724 space->ids[i] = id;
725 if (!id)
726 return isl_space_free(space);
729 for (i = 0; i < 2; ++i) {
730 if (!space->tuple_id[i])
731 continue;
732 if (!isl_id_get_user(space->tuple_id[i]))
733 continue;
734 space = isl_space_cow(space);
735 if (!space)
736 return NULL;
737 name = isl_id_get_name(space->tuple_id[i]);
738 id = isl_id_alloc(ctx, name, NULL);
739 isl_id_free(space->tuple_id[i]);
740 space->tuple_id[i] = id;
741 if (!id)
742 return isl_space_free(space);
745 for (i = 0; i < 2; ++i) {
746 if (!space->nested[i])
747 continue;
748 space = isl_space_cow(space);
749 if (!space)
750 return NULL;
751 space->nested[i] = isl_space_reset_user(space->nested[i]);
752 if (!space->nested[i])
753 return isl_space_free(space);
756 return space;
759 static __isl_keep isl_id *tuple_id(__isl_keep isl_space *dim,
760 enum isl_dim_type type)
762 if (!dim)
763 return NULL;
764 if (type == isl_dim_in)
765 return dim->tuple_id[0];
766 if (type == isl_dim_out)
767 return dim->tuple_id[1];
768 return NULL;
771 static __isl_keep isl_space *nested(__isl_keep isl_space *dim,
772 enum isl_dim_type type)
774 if (!dim)
775 return NULL;
776 if (type == isl_dim_in)
777 return dim->nested[0];
778 if (type == isl_dim_out)
779 return dim->nested[1];
780 return NULL;
783 /* Are the two spaces the same, apart from positions and names of parameters?
785 isl_bool isl_space_has_equal_tuples(__isl_keep isl_space *space1,
786 __isl_keep isl_space *space2)
788 if (!space1 || !space2)
789 return isl_bool_error;
790 if (space1 == space2)
791 return isl_bool_true;
792 return isl_space_tuple_is_equal(space1, isl_dim_in,
793 space2, isl_dim_in) &&
794 isl_space_tuple_is_equal(space1, isl_dim_out,
795 space2, isl_dim_out);
798 /* Check if the tuple of type "type1" of "space1" is the same as
799 * the tuple of type "type2" of "space2".
801 * That is, check if the tuples have the same identifier, the same dimension
802 * and the same internal structure.
803 * The identifiers of the dimensions inside the tuples do not affect the result.
805 * Note that this function only checks the tuples themselves.
806 * If nested tuples are involved, then we need to be careful not
807 * to have result affected by possibly differing parameters
808 * in those nested tuples.
810 isl_bool isl_space_tuple_is_equal(__isl_keep isl_space *space1,
811 enum isl_dim_type type1, __isl_keep isl_space *space2,
812 enum isl_dim_type type2)
814 isl_id *id1, *id2;
815 isl_space *nested1, *nested2;
817 if (!space1 || !space2)
818 return isl_bool_error;
820 if (space1 == space2 && type1 == type2)
821 return isl_bool_true;
823 if (n(space1, type1) != n(space2, type2))
824 return isl_bool_false;
825 id1 = tuple_id(space1, type1);
826 id2 = tuple_id(space2, type2);
827 if (!id1 ^ !id2)
828 return isl_bool_false;
829 if (id1 && id1 != id2)
830 return isl_bool_false;
831 nested1 = nested(space1, type1);
832 nested2 = nested(space2, type2);
833 if (!nested1 ^ !nested2)
834 return isl_bool_false;
835 if (nested1 && !isl_space_has_equal_tuples(nested1, nested2))
836 return isl_bool_false;
837 return isl_bool_true;
840 static isl_bool match(__isl_keep isl_space *space1, enum isl_dim_type type1,
841 __isl_keep isl_space *space2, enum isl_dim_type type2)
843 int i;
845 if (space1 == space2 && type1 == type2)
846 return isl_bool_true;
848 if (!isl_space_tuple_is_equal(space1, type1, space2, type2))
849 return isl_bool_false;
851 if (!space1->ids && !space2->ids)
852 return isl_bool_true;
854 for (i = 0; i < n(space1, type1); ++i) {
855 if (get_id(space1, type1, i) != get_id(space2, type2, i))
856 return isl_bool_false;
858 return isl_bool_true;
861 /* Do "space1" and "space2" have the same parameters?
863 isl_bool isl_space_has_equal_params(__isl_keep isl_space *space1,
864 __isl_keep isl_space *space2)
866 if (!space1 || !space2)
867 return isl_bool_error;
869 return match(space1, isl_dim_param, space2, isl_dim_param);
872 /* Do "space1" and "space2" have the same identifiers for all
873 * the tuple variables?
875 isl_bool isl_space_has_equal_ids(__isl_keep isl_space *space1,
876 __isl_keep isl_space *space2)
878 isl_bool equal;
880 if (!space1 || !space2)
881 return isl_bool_error;
883 equal = match(space1, isl_dim_in, space2, isl_dim_in);
884 if (equal < 0 || !equal)
885 return equal;
886 return match(space1, isl_dim_out, space2, isl_dim_out);
889 isl_bool isl_space_match(__isl_keep isl_space *space1, enum isl_dim_type type1,
890 __isl_keep isl_space *space2, enum isl_dim_type type2)
892 if (!space1 || !space2)
893 return isl_bool_error;
895 return match(space1, type1, space2, type2);
898 static void get_ids(__isl_keep isl_space *dim, enum isl_dim_type type,
899 unsigned first, unsigned n, __isl_keep isl_id **ids)
901 int i;
903 for (i = 0; i < n ; ++i)
904 ids[i] = get_id(dim, type, first + i);
907 static __isl_give isl_space *space_extend(__isl_take isl_space *space,
908 unsigned nparam, unsigned n_in, unsigned n_out)
910 isl_id **ids = NULL;
912 if (!space)
913 return NULL;
914 if (space->nparam == nparam &&
915 space->n_in == n_in && space->n_out == n_out)
916 return space;
918 isl_assert(space->ctx, space->nparam <= nparam, goto error);
919 isl_assert(space->ctx, space->n_in <= n_in, goto error);
920 isl_assert(space->ctx, space->n_out <= n_out, goto error);
922 space = isl_space_cow(space);
923 if (!space)
924 goto error;
926 if (space->ids) {
927 unsigned n;
928 n = nparam + n_in + n_out;
929 if (n < nparam || n < n_in || n < n_out)
930 isl_die(isl_space_get_ctx(space), isl_error_invalid,
931 "overflow in total number of dimensions",
932 goto error);
933 ids = isl_calloc_array(space->ctx, isl_id *, n);
934 if (!ids)
935 goto error;
936 get_ids(space, isl_dim_param, 0, space->nparam, ids);
937 get_ids(space, isl_dim_in, 0, space->n_in, ids + nparam);
938 get_ids(space, isl_dim_out, 0, space->n_out,
939 ids + nparam + n_in);
940 free(space->ids);
941 space->ids = ids;
942 space->n_id = nparam + n_in + n_out;
944 space->nparam = nparam;
945 space->n_in = n_in;
946 space->n_out = n_out;
948 return space;
949 error:
950 free(ids);
951 isl_space_free(space);
952 return NULL;
955 __isl_give isl_space *isl_space_extend(__isl_take isl_space *space,
956 unsigned nparam, unsigned n_in, unsigned n_out)
958 return space_extend(space, nparam, n_in, n_out);
961 __isl_give isl_space *isl_space_add_dims(__isl_take isl_space *space,
962 enum isl_dim_type type, unsigned n)
964 space = isl_space_reset(space, type);
965 if (!space)
966 return NULL;
967 switch (type) {
968 case isl_dim_param:
969 space = space_extend(space,
970 space->nparam + n, space->n_in, space->n_out);
971 if (space && space->nested[0] &&
972 !(space->nested[0] = isl_space_add_dims(space->nested[0],
973 isl_dim_param, n)))
974 goto error;
975 if (space && space->nested[1] &&
976 !(space->nested[1] = isl_space_add_dims(space->nested[1],
977 isl_dim_param, n)))
978 goto error;
979 return space;
980 case isl_dim_in:
981 return space_extend(space,
982 space->nparam, space->n_in + n, space->n_out);
983 case isl_dim_out:
984 return space_extend(space,
985 space->nparam, space->n_in, space->n_out + n);
986 default:
987 isl_die(space->ctx, isl_error_invalid,
988 "cannot add dimensions of specified type", goto error);
990 error:
991 isl_space_free(space);
992 return NULL;
995 /* Add a parameter with identifier "id" to "space", provided
996 * it does not already appear in "space".
998 __isl_give isl_space *isl_space_add_param_id(__isl_take isl_space *space,
999 __isl_take isl_id *id)
1001 int pos;
1003 if (!space || !id)
1004 goto error;
1006 if (isl_space_find_dim_by_id(space, isl_dim_param, id) >= 0) {
1007 isl_id_free(id);
1008 return space;
1011 pos = isl_space_dim(space, isl_dim_param);
1012 space = isl_space_add_dims(space, isl_dim_param, 1);
1013 space = isl_space_set_dim_id(space, isl_dim_param, pos, id);
1015 return space;
1016 error:
1017 isl_space_free(space);
1018 isl_id_free(id);
1019 return NULL;
1022 static int valid_dim_type(enum isl_dim_type type)
1024 switch (type) {
1025 case isl_dim_param:
1026 case isl_dim_in:
1027 case isl_dim_out:
1028 return 1;
1029 default:
1030 return 0;
1034 #undef TYPE
1035 #define TYPE isl_space
1036 #include "check_type_range_templ.c"
1038 /* Insert "n" dimensions of type "type" at position "pos".
1039 * If we are inserting parameters, then they are also inserted in
1040 * any nested spaces.
1042 __isl_give isl_space *isl_space_insert_dims(__isl_take isl_space *space,
1043 enum isl_dim_type type, unsigned pos, unsigned n)
1045 isl_ctx *ctx;
1046 isl_id **ids = NULL;
1048 if (!space)
1049 return NULL;
1050 if (n == 0)
1051 return isl_space_reset(space, type);
1053 ctx = isl_space_get_ctx(space);
1054 if (!valid_dim_type(type))
1055 isl_die(ctx, isl_error_invalid,
1056 "cannot insert dimensions of specified type",
1057 goto error);
1059 if (isl_space_check_range(space, type, pos, 0) < 0)
1060 return isl_space_free(space);
1062 space = isl_space_cow(space);
1063 if (!space)
1064 return NULL;
1066 if (space->ids) {
1067 enum isl_dim_type t, o = isl_dim_param;
1068 int off;
1069 int s[3];
1070 ids = isl_calloc_array(ctx, isl_id *,
1071 space->nparam + space->n_in + space->n_out + n);
1072 if (!ids)
1073 goto error;
1074 off = 0;
1075 s[isl_dim_param - o] = space->nparam;
1076 s[isl_dim_in - o] = space->n_in;
1077 s[isl_dim_out - o] = space->n_out;
1078 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
1079 if (t != type) {
1080 get_ids(space, t, 0, s[t - o], ids + off);
1081 off += s[t - o];
1082 } else {
1083 get_ids(space, t, 0, pos, ids + off);
1084 off += pos + n;
1085 get_ids(space, t, pos, s[t - o] - pos,
1086 ids + off);
1087 off += s[t - o] - pos;
1090 free(space->ids);
1091 space->ids = ids;
1092 space->n_id = space->nparam + space->n_in + space->n_out + n;
1094 switch (type) {
1095 case isl_dim_param: space->nparam += n; break;
1096 case isl_dim_in: space->n_in += n; break;
1097 case isl_dim_out: space->n_out += n; break;
1098 default: ;
1100 space = isl_space_reset(space, type);
1102 if (type == isl_dim_param) {
1103 if (space && space->nested[0] &&
1104 !(space->nested[0] = isl_space_insert_dims(space->nested[0],
1105 isl_dim_param, pos, n)))
1106 goto error;
1107 if (space && space->nested[1] &&
1108 !(space->nested[1] = isl_space_insert_dims(space->nested[1],
1109 isl_dim_param, pos, n)))
1110 goto error;
1113 return space;
1114 error:
1115 isl_space_free(space);
1116 return NULL;
1119 __isl_give isl_space *isl_space_move_dims(__isl_take isl_space *space,
1120 enum isl_dim_type dst_type, unsigned dst_pos,
1121 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
1123 int i;
1125 space = isl_space_reset(space, src_type);
1126 space = isl_space_reset(space, dst_type);
1127 if (!space)
1128 return NULL;
1129 if (n == 0)
1130 return space;
1132 if (isl_space_check_range(space, src_type, src_pos, n) < 0)
1133 return isl_space_free(space);
1135 if (dst_type == src_type && dst_pos == src_pos)
1136 return space;
1138 isl_assert(space->ctx, dst_type != src_type, goto error);
1140 space = isl_space_cow(space);
1141 if (!space)
1142 return NULL;
1144 if (space->ids) {
1145 isl_id **ids;
1146 enum isl_dim_type t, o = isl_dim_param;
1147 int off;
1148 int s[3];
1149 ids = isl_calloc_array(space->ctx, isl_id *,
1150 space->nparam + space->n_in + space->n_out);
1151 if (!ids)
1152 goto error;
1153 off = 0;
1154 s[isl_dim_param - o] = space->nparam;
1155 s[isl_dim_in - o] = space->n_in;
1156 s[isl_dim_out - o] = space->n_out;
1157 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
1158 if (t == dst_type) {
1159 get_ids(space, t, 0, dst_pos, ids + off);
1160 off += dst_pos;
1161 get_ids(space, src_type, src_pos, n, ids + off);
1162 off += n;
1163 get_ids(space, t, dst_pos, s[t - o] - dst_pos,
1164 ids + off);
1165 off += s[t - o] - dst_pos;
1166 } else if (t == src_type) {
1167 get_ids(space, t, 0, src_pos, ids + off);
1168 off += src_pos;
1169 get_ids(space, t, src_pos + n,
1170 s[t - o] - src_pos - n, ids + off);
1171 off += s[t - o] - src_pos - n;
1172 } else {
1173 get_ids(space, t, 0, s[t - o], ids + off);
1174 off += s[t - o];
1177 free(space->ids);
1178 space->ids = ids;
1179 space->n_id = space->nparam + space->n_in + space->n_out;
1182 switch (dst_type) {
1183 case isl_dim_param: space->nparam += n; break;
1184 case isl_dim_in: space->n_in += n; break;
1185 case isl_dim_out: space->n_out += n; break;
1186 default: ;
1189 switch (src_type) {
1190 case isl_dim_param: space->nparam -= n; break;
1191 case isl_dim_in: space->n_in -= n; break;
1192 case isl_dim_out: space->n_out -= n; break;
1193 default: ;
1196 if (dst_type != isl_dim_param && src_type != isl_dim_param)
1197 return space;
1199 for (i = 0; i < 2; ++i) {
1200 if (!space->nested[i])
1201 continue;
1202 space->nested[i] = isl_space_replace_params(space->nested[i],
1203 space);
1204 if (!space->nested[i])
1205 goto error;
1208 return space;
1209 error:
1210 isl_space_free(space);
1211 return NULL;
1214 /* Check that "space1" and "space2" have the same parameters,
1215 * reporting an error if they do not.
1217 isl_stat isl_space_check_equal_params(__isl_keep isl_space *space1,
1218 __isl_keep isl_space *space2)
1220 isl_bool equal;
1222 equal = isl_space_has_equal_params(space1, space2);
1223 if (equal < 0)
1224 return isl_stat_error;
1225 if (!equal)
1226 isl_die(isl_space_get_ctx(space1), isl_error_invalid,
1227 "parameters need to match", return isl_stat_error);
1228 return isl_stat_ok;
1231 __isl_give isl_space *isl_space_join(__isl_take isl_space *left,
1232 __isl_take isl_space *right)
1234 isl_space *space;
1236 if (isl_space_check_equal_params(left, right) < 0)
1237 goto error;
1239 isl_assert(left->ctx,
1240 isl_space_tuple_is_equal(left, isl_dim_out, right, isl_dim_in),
1241 goto error);
1243 space = isl_space_alloc(left->ctx,
1244 left->nparam, left->n_in, right->n_out);
1245 if (!space)
1246 goto error;
1248 space = copy_ids(space, isl_dim_param, 0, left, isl_dim_param);
1249 space = copy_ids(space, isl_dim_in, 0, left, isl_dim_in);
1250 space = copy_ids(space, isl_dim_out, 0, right, isl_dim_out);
1252 if (space && left->tuple_id[0] &&
1253 !(space->tuple_id[0] = isl_id_copy(left->tuple_id[0])))
1254 goto error;
1255 if (space && right->tuple_id[1] &&
1256 !(space->tuple_id[1] = isl_id_copy(right->tuple_id[1])))
1257 goto error;
1258 if (space && left->nested[0] &&
1259 !(space->nested[0] = isl_space_copy(left->nested[0])))
1260 goto error;
1261 if (space && right->nested[1] &&
1262 !(space->nested[1] = isl_space_copy(right->nested[1])))
1263 goto error;
1265 isl_space_free(left);
1266 isl_space_free(right);
1268 return space;
1269 error:
1270 isl_space_free(left);
1271 isl_space_free(right);
1272 return NULL;
1275 /* Given two map spaces { A -> C } and { B -> D }, construct the space
1276 * { [A -> B] -> [C -> D] }.
1277 * Given two set spaces { A } and { B }, construct the space { [A -> B] }.
1279 __isl_give isl_space *isl_space_product(__isl_take isl_space *left,
1280 __isl_take isl_space *right)
1282 isl_space *dom1, *dom2, *nest1, *nest2;
1283 int is_set;
1285 if (!left || !right)
1286 goto error;
1288 is_set = isl_space_is_set(left);
1289 if (is_set != isl_space_is_set(right))
1290 isl_die(isl_space_get_ctx(left), isl_error_invalid,
1291 "expecting either two set spaces or two map spaces",
1292 goto error);
1293 if (is_set)
1294 return isl_space_range_product(left, right);
1296 if (isl_space_check_equal_params(left, right) < 0)
1297 goto error;
1299 dom1 = isl_space_domain(isl_space_copy(left));
1300 dom2 = isl_space_domain(isl_space_copy(right));
1301 nest1 = isl_space_wrap(isl_space_join(isl_space_reverse(dom1), dom2));
1303 dom1 = isl_space_range(left);
1304 dom2 = isl_space_range(right);
1305 nest2 = isl_space_wrap(isl_space_join(isl_space_reverse(dom1), dom2));
1307 return isl_space_join(isl_space_reverse(nest1), nest2);
1308 error:
1309 isl_space_free(left);
1310 isl_space_free(right);
1311 return NULL;
1314 /* Given two spaces { A -> C } and { B -> C }, construct the space
1315 * { [A -> B] -> C }
1317 __isl_give isl_space *isl_space_domain_product(__isl_take isl_space *left,
1318 __isl_take isl_space *right)
1320 isl_space *ran, *dom1, *dom2, *nest;
1322 if (isl_space_check_equal_params(left, right) < 0)
1323 goto error;
1325 if (!isl_space_tuple_is_equal(left, isl_dim_out, right, isl_dim_out))
1326 isl_die(left->ctx, isl_error_invalid,
1327 "ranges need to match", goto error);
1329 ran = isl_space_range(isl_space_copy(left));
1331 dom1 = isl_space_domain(left);
1332 dom2 = isl_space_domain(right);
1333 nest = isl_space_wrap(isl_space_join(isl_space_reverse(dom1), dom2));
1335 return isl_space_join(isl_space_reverse(nest), ran);
1336 error:
1337 isl_space_free(left);
1338 isl_space_free(right);
1339 return NULL;
1342 __isl_give isl_space *isl_space_range_product(__isl_take isl_space *left,
1343 __isl_take isl_space *right)
1345 isl_space *dom, *ran1, *ran2, *nest;
1347 if (isl_space_check_equal_params(left, right) < 0)
1348 goto error;
1350 if (!isl_space_tuple_is_equal(left, isl_dim_in, right, isl_dim_in))
1351 isl_die(left->ctx, isl_error_invalid,
1352 "domains need to match", goto error);
1354 dom = isl_space_domain(isl_space_copy(left));
1356 ran1 = isl_space_range(left);
1357 ran2 = isl_space_range(right);
1358 nest = isl_space_wrap(isl_space_join(isl_space_reverse(ran1), ran2));
1360 return isl_space_join(isl_space_reverse(dom), nest);
1361 error:
1362 isl_space_free(left);
1363 isl_space_free(right);
1364 return NULL;
1367 /* Given a space of the form [A -> B] -> C, return the space A -> C.
1369 __isl_give isl_space *isl_space_domain_factor_domain(
1370 __isl_take isl_space *space)
1372 isl_space *nested;
1373 isl_space *domain;
1375 if (!space)
1376 return NULL;
1377 if (!isl_space_domain_is_wrapping(space))
1378 isl_die(isl_space_get_ctx(space), isl_error_invalid,
1379 "domain not a product", return isl_space_free(space));
1381 nested = space->nested[0];
1382 domain = isl_space_copy(space);
1383 domain = isl_space_drop_dims(domain, isl_dim_in,
1384 nested->n_in, nested->n_out);
1385 if (!domain)
1386 return isl_space_free(space);
1387 if (nested->tuple_id[0]) {
1388 domain->tuple_id[0] = isl_id_copy(nested->tuple_id[0]);
1389 if (!domain->tuple_id[0])
1390 goto error;
1392 if (nested->nested[0]) {
1393 domain->nested[0] = isl_space_copy(nested->nested[0]);
1394 if (!domain->nested[0])
1395 goto error;
1398 isl_space_free(space);
1399 return domain;
1400 error:
1401 isl_space_free(space);
1402 isl_space_free(domain);
1403 return NULL;
1406 /* Given a space of the form [A -> B] -> C, return the space B -> C.
1408 __isl_give isl_space *isl_space_domain_factor_range(
1409 __isl_take isl_space *space)
1411 isl_space *nested;
1412 isl_space *range;
1414 if (!space)
1415 return NULL;
1416 if (!isl_space_domain_is_wrapping(space))
1417 isl_die(isl_space_get_ctx(space), isl_error_invalid,
1418 "domain not a product", return isl_space_free(space));
1420 nested = space->nested[0];
1421 range = isl_space_copy(space);
1422 range = isl_space_drop_dims(range, isl_dim_in, 0, nested->n_in);
1423 if (!range)
1424 return isl_space_free(space);
1425 if (nested->tuple_id[1]) {
1426 range->tuple_id[0] = isl_id_copy(nested->tuple_id[1]);
1427 if (!range->tuple_id[0])
1428 goto error;
1430 if (nested->nested[1]) {
1431 range->nested[0] = isl_space_copy(nested->nested[1]);
1432 if (!range->nested[0])
1433 goto error;
1436 isl_space_free(space);
1437 return range;
1438 error:
1439 isl_space_free(space);
1440 isl_space_free(range);
1441 return NULL;
1444 /* Internal function that selects the domain of the map that is
1445 * embedded in either a set space or the range of a map space.
1446 * In particular, given a space of the form [A -> B], return the space A.
1447 * Given a space of the form A -> [B -> C], return the space A -> B.
1449 static __isl_give isl_space *range_factor_domain(__isl_take isl_space *space)
1451 isl_space *nested;
1452 isl_space *domain;
1454 if (!space)
1455 return NULL;
1457 nested = space->nested[1];
1458 domain = isl_space_copy(space);
1459 domain = isl_space_drop_dims(domain, isl_dim_out,
1460 nested->n_in, nested->n_out);
1461 if (!domain)
1462 return isl_space_free(space);
1463 if (nested->tuple_id[0]) {
1464 domain->tuple_id[1] = isl_id_copy(nested->tuple_id[0]);
1465 if (!domain->tuple_id[1])
1466 goto error;
1468 if (nested->nested[0]) {
1469 domain->nested[1] = isl_space_copy(nested->nested[0]);
1470 if (!domain->nested[1])
1471 goto error;
1474 isl_space_free(space);
1475 return domain;
1476 error:
1477 isl_space_free(space);
1478 isl_space_free(domain);
1479 return NULL;
1482 /* Given a space of the form A -> [B -> C], return the space A -> B.
1484 __isl_give isl_space *isl_space_range_factor_domain(
1485 __isl_take isl_space *space)
1487 if (!space)
1488 return NULL;
1489 if (!isl_space_range_is_wrapping(space))
1490 isl_die(isl_space_get_ctx(space), isl_error_invalid,
1491 "range not a product", return isl_space_free(space));
1493 return range_factor_domain(space);
1496 /* Given a space of the form [A -> B], return the space A.
1498 static __isl_give isl_space *set_factor_domain(__isl_take isl_space *space)
1500 if (!space)
1501 return NULL;
1502 if (!isl_space_is_wrapping(space))
1503 isl_die(isl_space_get_ctx(space), isl_error_invalid,
1504 "not a product", return isl_space_free(space));
1506 return range_factor_domain(space);
1509 /* Given a space of the form [A -> B] -> [C -> D], return the space A -> C.
1510 * Given a space of the form [A -> B], return the space A.
1512 __isl_give isl_space *isl_space_factor_domain(__isl_take isl_space *space)
1514 if (!space)
1515 return NULL;
1516 if (isl_space_is_set(space))
1517 return set_factor_domain(space);
1518 space = isl_space_domain_factor_domain(space);
1519 space = isl_space_range_factor_domain(space);
1520 return space;
1523 /* Internal function that selects the range of the map that is
1524 * embedded in either a set space or the range of a map space.
1525 * In particular, given a space of the form [A -> B], return the space B.
1526 * Given a space of the form A -> [B -> C], return the space A -> C.
1528 static __isl_give isl_space *range_factor_range(__isl_take isl_space *space)
1530 isl_space *nested;
1531 isl_space *range;
1533 if (!space)
1534 return NULL;
1536 nested = space->nested[1];
1537 range = isl_space_copy(space);
1538 range = isl_space_drop_dims(range, isl_dim_out, 0, nested->n_in);
1539 if (!range)
1540 return isl_space_free(space);
1541 if (nested->tuple_id[1]) {
1542 range->tuple_id[1] = isl_id_copy(nested->tuple_id[1]);
1543 if (!range->tuple_id[1])
1544 goto error;
1546 if (nested->nested[1]) {
1547 range->nested[1] = isl_space_copy(nested->nested[1]);
1548 if (!range->nested[1])
1549 goto error;
1552 isl_space_free(space);
1553 return range;
1554 error:
1555 isl_space_free(space);
1556 isl_space_free(range);
1557 return NULL;
1560 /* Given a space of the form A -> [B -> C], return the space A -> C.
1562 __isl_give isl_space *isl_space_range_factor_range(
1563 __isl_take isl_space *space)
1565 if (!space)
1566 return NULL;
1567 if (!isl_space_range_is_wrapping(space))
1568 isl_die(isl_space_get_ctx(space), isl_error_invalid,
1569 "range not a product", return isl_space_free(space));
1571 return range_factor_range(space);
1574 /* Given a space of the form [A -> B], return the space B.
1576 static __isl_give isl_space *set_factor_range(__isl_take isl_space *space)
1578 if (!space)
1579 return NULL;
1580 if (!isl_space_is_wrapping(space))
1581 isl_die(isl_space_get_ctx(space), isl_error_invalid,
1582 "not a product", return isl_space_free(space));
1584 return range_factor_range(space);
1587 /* Given a space of the form [A -> B] -> [C -> D], return the space B -> D.
1588 * Given a space of the form [A -> B], return the space B.
1590 __isl_give isl_space *isl_space_factor_range(__isl_take isl_space *space)
1592 if (!space)
1593 return NULL;
1594 if (isl_space_is_set(space))
1595 return set_factor_range(space);
1596 space = isl_space_domain_factor_range(space);
1597 space = isl_space_range_factor_range(space);
1598 return space;
1601 __isl_give isl_space *isl_space_map_from_set(__isl_take isl_space *space)
1603 isl_ctx *ctx;
1604 isl_id **ids = NULL;
1605 int n_id;
1607 if (!space)
1608 return NULL;
1609 ctx = isl_space_get_ctx(space);
1610 if (!isl_space_is_set(space))
1611 isl_die(ctx, isl_error_invalid, "not a set space", goto error);
1612 space = isl_space_cow(space);
1613 if (!space)
1614 return NULL;
1615 n_id = space->nparam + space->n_out + space->n_out;
1616 if (n_id > 0 && space->ids) {
1617 ids = isl_calloc_array(space->ctx, isl_id *, n_id);
1618 if (!ids)
1619 goto error;
1620 get_ids(space, isl_dim_param, 0, space->nparam, ids);
1621 get_ids(space, isl_dim_out, 0, space->n_out,
1622 ids + space->nparam);
1624 space->n_in = space->n_out;
1625 if (ids) {
1626 free(space->ids);
1627 space->ids = ids;
1628 space->n_id = n_id;
1629 space = copy_ids(space, isl_dim_out, 0, space, isl_dim_in);
1631 isl_id_free(space->tuple_id[0]);
1632 space->tuple_id[0] = isl_id_copy(space->tuple_id[1]);
1633 isl_space_free(space->nested[0]);
1634 space->nested[0] = isl_space_copy(space->nested[1]);
1635 return space;
1636 error:
1637 isl_space_free(space);
1638 return NULL;
1641 __isl_give isl_space *isl_space_map_from_domain_and_range(
1642 __isl_take isl_space *domain, __isl_take isl_space *range)
1644 if (!domain || !range)
1645 goto error;
1646 if (!isl_space_is_set(domain))
1647 isl_die(isl_space_get_ctx(domain), isl_error_invalid,
1648 "domain is not a set space", goto error);
1649 if (!isl_space_is_set(range))
1650 isl_die(isl_space_get_ctx(range), isl_error_invalid,
1651 "range is not a set space", goto error);
1652 return isl_space_join(isl_space_reverse(domain), range);
1653 error:
1654 isl_space_free(domain);
1655 isl_space_free(range);
1656 return NULL;
1659 static __isl_give isl_space *set_ids(__isl_take isl_space *dim,
1660 enum isl_dim_type type,
1661 unsigned first, unsigned n, __isl_take isl_id **ids)
1663 int i;
1665 for (i = 0; i < n ; ++i)
1666 dim = set_id(dim, type, first + i, ids[i]);
1668 return dim;
1671 __isl_give isl_space *isl_space_reverse(__isl_take isl_space *space)
1673 unsigned t;
1674 isl_space *nested;
1675 isl_id **ids = NULL;
1676 isl_id *id;
1678 if (!space)
1679 return NULL;
1680 if (match(space, isl_dim_in, space, isl_dim_out))
1681 return space;
1683 space = isl_space_cow(space);
1684 if (!space)
1685 return NULL;
1687 id = space->tuple_id[0];
1688 space->tuple_id[0] = space->tuple_id[1];
1689 space->tuple_id[1] = id;
1691 nested = space->nested[0];
1692 space->nested[0] = space->nested[1];
1693 space->nested[1] = nested;
1695 if (space->ids) {
1696 int n_id = space->n_in + space->n_out;
1697 ids = isl_alloc_array(space->ctx, isl_id *, n_id);
1698 if (n_id && !ids)
1699 goto error;
1700 get_ids(space, isl_dim_in, 0, space->n_in, ids);
1701 get_ids(space, isl_dim_out, 0, space->n_out, ids + space->n_in);
1704 t = space->n_in;
1705 space->n_in = space->n_out;
1706 space->n_out = t;
1708 if (space->ids) {
1709 space = set_ids(space, isl_dim_out, 0, space->n_out, ids);
1710 space = set_ids(space, isl_dim_in, 0, space->n_in,
1711 ids + space->n_out);
1712 free(ids);
1715 return space;
1716 error:
1717 free(ids);
1718 isl_space_free(space);
1719 return NULL;
1722 __isl_give isl_space *isl_space_drop_dims(__isl_take isl_space *space,
1723 enum isl_dim_type type, unsigned first, unsigned num)
1725 int i;
1727 if (!space)
1728 return NULL;
1730 if (num == 0)
1731 return isl_space_reset(space, type);
1733 if (!valid_dim_type(type))
1734 isl_die(space->ctx, isl_error_invalid,
1735 "cannot drop dimensions of specified type", goto error);
1737 if (isl_space_check_range(space, type, first, num) < 0)
1738 return isl_space_free(space);
1739 space = isl_space_cow(space);
1740 if (!space)
1741 goto error;
1742 if (space->ids) {
1743 space = extend_ids(space);
1744 if (!space)
1745 goto error;
1746 for (i = 0; i < num; ++i)
1747 isl_id_free(get_id(space, type, first + i));
1748 for (i = first+num; i < n(space, type); ++i)
1749 set_id(space, type, i - num, get_id(space, type, i));
1750 switch (type) {
1751 case isl_dim_param:
1752 get_ids(space, isl_dim_in, 0, space->n_in,
1753 space->ids + offset(space, isl_dim_in) - num);
1754 case isl_dim_in:
1755 get_ids(space, isl_dim_out, 0, space->n_out,
1756 space->ids + offset(space, isl_dim_out) - num);
1757 default:
1760 space->n_id -= num;
1762 switch (type) {
1763 case isl_dim_param: space->nparam -= num; break;
1764 case isl_dim_in: space->n_in -= num; break;
1765 case isl_dim_out: space->n_out -= num; break;
1766 default: ;
1768 space = isl_space_reset(space, type);
1769 if (type == isl_dim_param) {
1770 if (space && space->nested[0] &&
1771 !(space->nested[0] = isl_space_drop_dims(space->nested[0],
1772 isl_dim_param, first, num)))
1773 goto error;
1774 if (space && space->nested[1] &&
1775 !(space->nested[1] = isl_space_drop_dims(space->nested[1],
1776 isl_dim_param, first, num)))
1777 goto error;
1779 return space;
1780 error:
1781 isl_space_free(space);
1782 return NULL;
1785 __isl_give isl_space *isl_space_drop_inputs(__isl_take isl_space *dim,
1786 unsigned first, unsigned n)
1788 if (!dim)
1789 return NULL;
1790 return isl_space_drop_dims(dim, isl_dim_in, first, n);
1793 __isl_give isl_space *isl_space_drop_outputs(__isl_take isl_space *dim,
1794 unsigned first, unsigned n)
1796 if (!dim)
1797 return NULL;
1798 return isl_space_drop_dims(dim, isl_dim_out, first, n);
1801 __isl_give isl_space *isl_space_domain(__isl_take isl_space *space)
1803 if (!space)
1804 return NULL;
1805 space = isl_space_drop_dims(space, isl_dim_out, 0, space->n_out);
1806 space = isl_space_reverse(space);
1807 space = mark_as_set(space);
1808 return space;
1811 __isl_give isl_space *isl_space_from_domain(__isl_take isl_space *space)
1813 if (!space)
1814 return NULL;
1815 if (!isl_space_is_set(space))
1816 isl_die(isl_space_get_ctx(space), isl_error_invalid,
1817 "not a set space", goto error);
1818 space = isl_space_reverse(space);
1819 space = isl_space_reset(space, isl_dim_out);
1820 return space;
1821 error:
1822 isl_space_free(space);
1823 return NULL;
1826 __isl_give isl_space *isl_space_range(__isl_take isl_space *space)
1828 if (!space)
1829 return NULL;
1830 space = isl_space_drop_dims(space, isl_dim_in, 0, space->n_in);
1831 space = mark_as_set(space);
1832 return space;
1835 __isl_give isl_space *isl_space_from_range(__isl_take isl_space *space)
1837 if (!space)
1838 return NULL;
1839 if (!isl_space_is_set(space))
1840 isl_die(isl_space_get_ctx(space), isl_error_invalid,
1841 "not a set space", goto error);
1842 return isl_space_reset(space, isl_dim_in);
1843 error:
1844 isl_space_free(space);
1845 return NULL;
1848 /* Given a map space A -> B, return the map space [A -> B] -> A.
1850 __isl_give isl_space *isl_space_domain_map(__isl_take isl_space *space)
1852 isl_space *domain;
1854 domain = isl_space_from_range(isl_space_domain(isl_space_copy(space)));
1855 space = isl_space_from_domain(isl_space_wrap(space));
1856 space = isl_space_join(space, domain);
1858 return space;
1861 /* Given a map space A -> B, return the map space [A -> B] -> B.
1863 __isl_give isl_space *isl_space_range_map(__isl_take isl_space *space)
1865 isl_space *range;
1867 range = isl_space_from_range(isl_space_range(isl_space_copy(space)));
1868 space = isl_space_from_domain(isl_space_wrap(space));
1869 space = isl_space_join(space, range);
1871 return space;
1874 __isl_give isl_space *isl_space_params(__isl_take isl_space *space)
1876 if (isl_space_is_params(space))
1877 return space;
1878 space = isl_space_drop_dims(space,
1879 isl_dim_in, 0, isl_space_dim(space, isl_dim_in));
1880 space = isl_space_drop_dims(space,
1881 isl_dim_out, 0, isl_space_dim(space, isl_dim_out));
1882 space = mark_as_params(space);
1883 return space;
1886 __isl_give isl_space *isl_space_set_from_params(__isl_take isl_space *space)
1888 if (!space)
1889 return NULL;
1890 if (!isl_space_is_params(space))
1891 isl_die(isl_space_get_ctx(space), isl_error_invalid,
1892 "not a parameter space", goto error);
1893 return isl_space_reset(space, isl_dim_set);
1894 error:
1895 isl_space_free(space);
1896 return NULL;
1899 __isl_give isl_space *isl_space_underlying(__isl_take isl_space *space,
1900 unsigned n_div)
1902 int i;
1903 isl_bool is_set;
1905 is_set = isl_space_is_set(space);
1906 if (is_set < 0)
1907 return isl_space_free(space);
1908 if (n_div == 0 && is_set &&
1909 space->nparam == 0 && space->n_in == 0 && space->n_id == 0)
1910 return isl_space_reset(space, isl_dim_out);
1911 space = isl_space_cow(space);
1912 if (!space)
1913 return NULL;
1914 space->n_out += space->nparam + space->n_in + n_div;
1915 space->nparam = 0;
1916 space->n_in = 0;
1918 for (i = 0; i < space->n_id; ++i)
1919 isl_id_free(get_id(space, isl_dim_out, i));
1920 space->n_id = 0;
1921 space = isl_space_reset(space, isl_dim_in);
1922 space = isl_space_reset(space, isl_dim_out);
1923 space = mark_as_set(space);
1925 return space;
1928 /* Are the two spaces the same, including positions and names of parameters?
1930 isl_bool isl_space_is_equal(__isl_keep isl_space *space1,
1931 __isl_keep isl_space *space2)
1933 isl_bool equal;
1935 if (!space1 || !space2)
1936 return isl_bool_error;
1937 if (space1 == space2)
1938 return isl_bool_true;
1939 equal = isl_space_has_equal_params(space1, space2);
1940 if (equal < 0 || !equal)
1941 return equal;
1942 return isl_space_has_equal_tuples(space1, space2);
1945 /* Do the tuples of "space1" correspond to those of the domain of "space2"?
1946 * That is, is "space1" eqaul to the domain of "space2", ignoring parameters.
1948 * "space2" is allowed to be a set space, in which case "space1"
1949 * should be a parameter space.
1951 isl_bool isl_space_has_domain_tuples(__isl_keep isl_space *space1,
1952 __isl_keep isl_space *space2)
1954 isl_bool is_set;
1956 is_set = isl_space_is_set(space1);
1957 if (is_set < 0 || !is_set)
1958 return is_set;
1959 return isl_space_tuple_is_equal(space1, isl_dim_set,
1960 space2, isl_dim_in);
1963 /* Is space1 equal to the domain of space2?
1965 * In the internal version we also allow space2 to be the space of a set,
1966 * provided space1 is a parameter space.
1968 isl_bool isl_space_is_domain_internal(__isl_keep isl_space *space1,
1969 __isl_keep isl_space *space2)
1971 isl_bool equal_params;
1973 if (!space1 || !space2)
1974 return isl_bool_error;
1975 equal_params = isl_space_has_equal_params(space1, space2);
1976 if (equal_params < 0 || !equal_params)
1977 return equal_params;
1978 return isl_space_has_domain_tuples(space1, space2);
1981 /* Is space1 equal to the domain of space2?
1983 isl_bool isl_space_is_domain(__isl_keep isl_space *space1,
1984 __isl_keep isl_space *space2)
1986 if (!space2)
1987 return isl_bool_error;
1988 if (!isl_space_is_map(space2))
1989 return isl_bool_false;
1990 return isl_space_is_domain_internal(space1, space2);
1993 /* Is space1 equal to the range of space2?
1995 * In the internal version, space2 is allowed to be the space of a set,
1996 * in which case it should be equal to space1.
1998 isl_bool isl_space_is_range_internal(__isl_keep isl_space *space1,
1999 __isl_keep isl_space *space2)
2001 isl_bool equal_params;
2003 if (!space1 || !space2)
2004 return isl_bool_error;
2005 if (!isl_space_is_set(space1))
2006 return isl_bool_false;
2007 equal_params = isl_space_has_equal_params(space1, space2);
2008 if (equal_params < 0 || !equal_params)
2009 return equal_params;
2010 return isl_space_tuple_is_equal(space1, isl_dim_set,
2011 space2, isl_dim_out);
2014 /* Is space1 equal to the range of space2?
2016 isl_bool isl_space_is_range(__isl_keep isl_space *space1,
2017 __isl_keep isl_space *space2)
2019 if (!space2)
2020 return isl_bool_error;
2021 if (!isl_space_is_map(space2))
2022 return isl_bool_false;
2023 return isl_space_is_range_internal(space1, space2);
2026 /* Update "hash" by hashing in the parameters of "space".
2028 static uint32_t isl_hash_params(uint32_t hash, __isl_keep isl_space *space)
2030 int i;
2031 isl_id *id;
2033 if (!space)
2034 return hash;
2036 isl_hash_byte(hash, space->nparam % 256);
2038 for (i = 0; i < space->nparam; ++i) {
2039 id = get_id(space, isl_dim_param, i);
2040 hash = isl_hash_id(hash, id);
2043 return hash;
2046 /* Update "hash" by hashing in the tuples of "space".
2047 * Changes in this function should be reflected in isl_hash_tuples_domain.
2049 static uint32_t isl_hash_tuples(uint32_t hash, __isl_keep isl_space *space)
2051 isl_id *id;
2053 if (!space)
2054 return hash;
2056 isl_hash_byte(hash, space->n_in % 256);
2057 isl_hash_byte(hash, space->n_out % 256);
2059 id = tuple_id(space, isl_dim_in);
2060 hash = isl_hash_id(hash, id);
2061 id = tuple_id(space, isl_dim_out);
2062 hash = isl_hash_id(hash, id);
2064 hash = isl_hash_tuples(hash, space->nested[0]);
2065 hash = isl_hash_tuples(hash, space->nested[1]);
2067 return hash;
2070 /* Update "hash" by hashing in the domain tuple of "space".
2071 * The result of this function is equal to the result of applying
2072 * isl_hash_tuples to the domain of "space".
2074 static uint32_t isl_hash_tuples_domain(uint32_t hash,
2075 __isl_keep isl_space *space)
2077 isl_id *id;
2079 if (!space)
2080 return hash;
2082 isl_hash_byte(hash, 0);
2083 isl_hash_byte(hash, space->n_in % 256);
2085 hash = isl_hash_id(hash, &isl_id_none);
2086 id = tuple_id(space, isl_dim_in);
2087 hash = isl_hash_id(hash, id);
2089 hash = isl_hash_tuples(hash, space->nested[0]);
2091 return hash;
2094 /* Return a hash value that digests the tuples of "space",
2095 * i.e., that ignores the parameters.
2097 uint32_t isl_space_get_tuple_hash(__isl_keep isl_space *space)
2099 uint32_t hash;
2101 if (!space)
2102 return 0;
2104 hash = isl_hash_init();
2105 hash = isl_hash_tuples(hash, space);
2107 return hash;
2110 uint32_t isl_space_get_hash(__isl_keep isl_space *space)
2112 uint32_t hash;
2114 if (!space)
2115 return 0;
2117 hash = isl_hash_init();
2118 hash = isl_hash_params(hash, space);
2119 hash = isl_hash_tuples(hash, space);
2121 return hash;
2124 /* Return the hash value of the domain of "space".
2125 * That is, isl_space_get_domain_hash(space) is equal to
2126 * isl_space_get_hash(isl_space_domain(space)).
2128 uint32_t isl_space_get_domain_hash(__isl_keep isl_space *space)
2130 uint32_t hash;
2132 if (!space)
2133 return 0;
2135 hash = isl_hash_init();
2136 hash = isl_hash_params(hash, space);
2137 hash = isl_hash_tuples_domain(hash, space);
2139 return hash;
2142 isl_bool isl_space_is_wrapping(__isl_keep isl_space *space)
2144 if (!space)
2145 return isl_bool_error;
2147 if (!isl_space_is_set(space))
2148 return isl_bool_false;
2150 return space->nested[1] != NULL;
2153 /* Is "space" the space of a map where the domain is a wrapped map space?
2155 isl_bool isl_space_domain_is_wrapping(__isl_keep isl_space *space)
2157 if (!space)
2158 return isl_bool_error;
2160 if (isl_space_is_set(space))
2161 return isl_bool_false;
2163 return space->nested[0] != NULL;
2166 /* Is "space" the space of a map where the range is a wrapped map space?
2168 isl_bool isl_space_range_is_wrapping(__isl_keep isl_space *space)
2170 if (!space)
2171 return isl_bool_error;
2173 if (isl_space_is_set(space))
2174 return isl_bool_false;
2176 return space->nested[1] != NULL;
2179 /* Is "space" a product of two spaces?
2180 * That is, is it a wrapping set space or a map space
2181 * with wrapping domain and range?
2183 isl_bool isl_space_is_product(__isl_keep isl_space *space)
2185 isl_bool is_set;
2186 isl_bool is_product;
2188 is_set = isl_space_is_set(space);
2189 if (is_set < 0)
2190 return isl_bool_error;
2191 if (is_set)
2192 return isl_space_is_wrapping(space);
2193 is_product = isl_space_domain_is_wrapping(space);
2194 if (is_product < 0 || !is_product)
2195 return is_product;
2196 return isl_space_range_is_wrapping(space);
2199 __isl_give isl_space *isl_space_wrap(__isl_take isl_space *space)
2201 isl_space *wrap;
2203 if (!space)
2204 return NULL;
2206 wrap = isl_space_set_alloc(space->ctx,
2207 space->nparam, space->n_in + space->n_out);
2209 wrap = copy_ids(wrap, isl_dim_param, 0, space, isl_dim_param);
2210 wrap = copy_ids(wrap, isl_dim_set, 0, space, isl_dim_in);
2211 wrap = copy_ids(wrap, isl_dim_set, space->n_in, space, isl_dim_out);
2213 if (!wrap)
2214 goto error;
2216 wrap->nested[1] = space;
2218 return wrap;
2219 error:
2220 isl_space_free(space);
2221 return NULL;
2224 __isl_give isl_space *isl_space_unwrap(__isl_take isl_space *space)
2226 isl_space *unwrap;
2228 if (!space)
2229 return NULL;
2231 if (!isl_space_is_wrapping(space))
2232 isl_die(space->ctx, isl_error_invalid, "not a wrapping space",
2233 goto error);
2235 unwrap = isl_space_copy(space->nested[1]);
2236 isl_space_free(space);
2238 return unwrap;
2239 error:
2240 isl_space_free(space);
2241 return NULL;
2244 isl_bool isl_space_is_named_or_nested(__isl_keep isl_space *space,
2245 enum isl_dim_type type)
2247 if (type != isl_dim_in && type != isl_dim_out)
2248 return isl_bool_false;
2249 if (!space)
2250 return isl_bool_error;
2251 if (space->tuple_id[type - isl_dim_in])
2252 return isl_bool_true;
2253 if (space->nested[type - isl_dim_in])
2254 return isl_bool_true;
2255 return isl_bool_false;
2258 isl_bool isl_space_may_be_set(__isl_keep isl_space *space)
2260 isl_bool nested;
2262 if (!space)
2263 return isl_bool_error;
2264 if (isl_space_is_set(space))
2265 return isl_bool_true;
2266 if (isl_space_dim(space, isl_dim_in) != 0)
2267 return isl_bool_false;
2268 nested = isl_space_is_named_or_nested(space, isl_dim_in);
2269 if (nested < 0 || nested)
2270 return isl_bool_not(nested);
2271 return isl_bool_true;
2274 __isl_give isl_space *isl_space_reset(__isl_take isl_space *space,
2275 enum isl_dim_type type)
2277 if (!isl_space_is_named_or_nested(space, type))
2278 return space;
2280 space = isl_space_cow(space);
2281 if (!space)
2282 return NULL;
2284 isl_id_free(space->tuple_id[type - isl_dim_in]);
2285 space->tuple_id[type - isl_dim_in] = NULL;
2286 isl_space_free(space->nested[type - isl_dim_in]);
2287 space->nested[type - isl_dim_in] = NULL;
2289 return space;
2292 __isl_give isl_space *isl_space_flatten(__isl_take isl_space *space)
2294 if (!space)
2295 return NULL;
2296 if (!space->nested[0] && !space->nested[1])
2297 return space;
2299 if (space->nested[0])
2300 space = isl_space_reset(space, isl_dim_in);
2301 if (space && space->nested[1])
2302 space = isl_space_reset(space, isl_dim_out);
2304 return space;
2307 __isl_give isl_space *isl_space_flatten_domain(__isl_take isl_space *space)
2309 if (!space)
2310 return NULL;
2311 if (!space->nested[0])
2312 return space;
2314 return isl_space_reset(space, isl_dim_in);
2317 __isl_give isl_space *isl_space_flatten_range(__isl_take isl_space *space)
2319 if (!space)
2320 return NULL;
2321 if (!space->nested[1])
2322 return space;
2324 return isl_space_reset(space, isl_dim_out);
2327 /* Replace the parameters of dst by those of src.
2329 __isl_give isl_space *isl_space_replace_params(__isl_take isl_space *dst,
2330 __isl_keep isl_space *src)
2332 isl_bool equal_params;
2333 enum isl_dim_type type = isl_dim_param;
2335 equal_params = isl_space_has_equal_params(dst, src);
2336 if (equal_params < 0)
2337 return isl_space_free(dst);
2338 if (equal_params)
2339 return dst;
2341 dst = isl_space_cow(dst);
2343 if (!dst || !src)
2344 goto error;
2346 dst = isl_space_drop_dims(dst, type, 0, isl_space_dim(dst, type));
2347 dst = isl_space_add_dims(dst, type, isl_space_dim(src, type));
2348 dst = copy_ids(dst, type, 0, src, type);
2350 if (dst) {
2351 int i;
2352 for (i = 0; i <= 1; ++i) {
2353 if (!dst->nested[i])
2354 continue;
2355 dst->nested[i] = isl_space_replace_params(
2356 dst->nested[i], src);
2357 if (!dst->nested[i])
2358 goto error;
2362 return dst;
2363 error:
2364 isl_space_free(dst);
2365 return NULL;
2368 /* Given a dimension specification "dim" of a set, create a dimension
2369 * specification for the lift of the set. In particular, the result
2370 * is of the form [dim -> local[..]], with n_local variables in the
2371 * range of the wrapped map.
2373 __isl_give isl_space *isl_space_lift(__isl_take isl_space *space,
2374 unsigned n_local)
2376 isl_space *local_space;
2378 if (!space)
2379 return NULL;
2381 local_space = isl_space_dup(space);
2382 local_space = isl_space_drop_dims(local_space, isl_dim_set, 0,
2383 space->n_out);
2384 local_space = isl_space_add_dims(local_space, isl_dim_set, n_local);
2385 local_space = isl_space_set_tuple_name(local_space,
2386 isl_dim_set, "local");
2387 space = isl_space_join(isl_space_from_domain(space),
2388 isl_space_from_range(local_space));
2389 space = isl_space_wrap(space);
2390 space = isl_space_set_tuple_name(space, isl_dim_set, "lifted");
2392 return space;
2395 isl_bool isl_space_can_zip(__isl_keep isl_space *space)
2397 isl_bool is_set;
2399 is_set = isl_space_is_set(space);
2400 if (is_set < 0)
2401 return isl_bool_error;
2402 if (is_set)
2403 return isl_bool_false;
2404 return isl_space_is_product(space);
2407 __isl_give isl_space *isl_space_zip(__isl_take isl_space *space)
2409 isl_space *dom, *ran;
2410 isl_space *dom_dom, *dom_ran, *ran_dom, *ran_ran;
2412 if (!isl_space_can_zip(space))
2413 isl_die(space->ctx, isl_error_invalid, "dim cannot be zipped",
2414 goto error);
2416 if (!space)
2417 return NULL;
2418 dom = isl_space_unwrap(isl_space_domain(isl_space_copy(space)));
2419 ran = isl_space_unwrap(isl_space_range(space));
2420 dom_dom = isl_space_domain(isl_space_copy(dom));
2421 dom_ran = isl_space_range(dom);
2422 ran_dom = isl_space_domain(isl_space_copy(ran));
2423 ran_ran = isl_space_range(ran);
2424 dom = isl_space_join(isl_space_from_domain(dom_dom),
2425 isl_space_from_range(ran_dom));
2426 ran = isl_space_join(isl_space_from_domain(dom_ran),
2427 isl_space_from_range(ran_ran));
2428 return isl_space_join(isl_space_from_domain(isl_space_wrap(dom)),
2429 isl_space_from_range(isl_space_wrap(ran)));
2430 error:
2431 isl_space_free(space);
2432 return NULL;
2435 /* Can we apply isl_space_curry to "space"?
2436 * That is, does it have a nested relation in its domain?
2438 isl_bool isl_space_can_curry(__isl_keep isl_space *space)
2440 if (!space)
2441 return isl_bool_error;
2443 return !!space->nested[0];
2446 /* Given a space (A -> B) -> C, return the corresponding space
2447 * A -> (B -> C).
2449 __isl_give isl_space *isl_space_curry(__isl_take isl_space *space)
2451 isl_space *dom, *ran;
2452 isl_space *dom_dom, *dom_ran;
2454 if (!space)
2455 return NULL;
2457 if (!isl_space_can_curry(space))
2458 isl_die(space->ctx, isl_error_invalid,
2459 "space cannot be curried", goto error);
2461 dom = isl_space_unwrap(isl_space_domain(isl_space_copy(space)));
2462 ran = isl_space_range(space);
2463 dom_dom = isl_space_domain(isl_space_copy(dom));
2464 dom_ran = isl_space_range(dom);
2465 ran = isl_space_join(isl_space_from_domain(dom_ran),
2466 isl_space_from_range(ran));
2467 return isl_space_join(isl_space_from_domain(dom_dom),
2468 isl_space_from_range(isl_space_wrap(ran)));
2469 error:
2470 isl_space_free(space);
2471 return NULL;
2474 /* Can isl_space_range_curry be applied to "space"?
2475 * That is, does it have a nested relation in its range,
2476 * the domain of which is itself a nested relation?
2478 isl_bool isl_space_can_range_curry(__isl_keep isl_space *space)
2480 isl_bool can;
2482 if (!space)
2483 return isl_bool_error;
2484 can = isl_space_range_is_wrapping(space);
2485 if (can < 0 || !can)
2486 return can;
2487 return isl_space_can_curry(space->nested[1]);
2490 /* Given a space A -> ((B -> C) -> D), return the corresponding space
2491 * A -> (B -> (C -> D)).
2493 __isl_give isl_space *isl_space_range_curry(__isl_take isl_space *space)
2495 if (!space)
2496 return NULL;
2498 if (!isl_space_can_range_curry(space))
2499 isl_die(isl_space_get_ctx(space), isl_error_invalid,
2500 "space range cannot be curried",
2501 return isl_space_free(space));
2503 space = isl_space_cow(space);
2504 if (!space)
2505 return NULL;
2506 space->nested[1] = isl_space_curry(space->nested[1]);
2507 if (!space->nested[1])
2508 return isl_space_free(space);
2510 return space;
2513 /* Can we apply isl_space_uncurry to "space"?
2514 * That is, does it have a nested relation in its range?
2516 isl_bool isl_space_can_uncurry(__isl_keep isl_space *space)
2518 if (!space)
2519 return isl_bool_error;
2521 return !!space->nested[1];
2524 /* Given a space A -> (B -> C), return the corresponding space
2525 * (A -> B) -> C.
2527 __isl_give isl_space *isl_space_uncurry(__isl_take isl_space *space)
2529 isl_space *dom, *ran;
2530 isl_space *ran_dom, *ran_ran;
2532 if (!space)
2533 return NULL;
2535 if (!isl_space_can_uncurry(space))
2536 isl_die(space->ctx, isl_error_invalid,
2537 "space cannot be uncurried",
2538 return isl_space_free(space));
2540 dom = isl_space_domain(isl_space_copy(space));
2541 ran = isl_space_unwrap(isl_space_range(space));
2542 ran_dom = isl_space_domain(isl_space_copy(ran));
2543 ran_ran = isl_space_range(ran);
2544 dom = isl_space_join(isl_space_from_domain(dom),
2545 isl_space_from_range(ran_dom));
2546 return isl_space_join(isl_space_from_domain(isl_space_wrap(dom)),
2547 isl_space_from_range(ran_ran));
2550 isl_bool isl_space_has_named_params(__isl_keep isl_space *space)
2552 int i;
2553 unsigned off;
2555 if (!space)
2556 return isl_bool_error;
2557 if (space->nparam == 0)
2558 return isl_bool_true;
2559 off = isl_space_offset(space, isl_dim_param);
2560 if (off + space->nparam > space->n_id)
2561 return isl_bool_false;
2562 for (i = 0; i < space->nparam; ++i)
2563 if (!space->ids[off + i])
2564 return isl_bool_false;
2565 return isl_bool_true;
2568 /* Check that "space" has only named parameters, reporting an error
2569 * if it does not.
2571 isl_stat isl_space_check_named_params(__isl_keep isl_space *space)
2573 isl_bool named;
2575 named = isl_space_has_named_params(space);
2576 if (named < 0)
2577 return isl_stat_error;
2578 if (!named)
2579 isl_die(isl_space_get_ctx(space), isl_error_invalid,
2580 "unexpected unnamed parameters", return isl_stat_error);
2582 return isl_stat_ok;
2585 /* Align the initial parameters of space1 to match the order in space2.
2587 __isl_give isl_space *isl_space_align_params(__isl_take isl_space *space1,
2588 __isl_take isl_space *space2)
2590 isl_reordering *exp;
2592 if (isl_space_check_named_params(space1) < 0 ||
2593 isl_space_check_named_params(space2) < 0)
2594 goto error;
2596 exp = isl_parameter_alignment_reordering(space1, space2);
2597 exp = isl_reordering_extend_space(exp, space1);
2598 isl_space_free(space2);
2599 space1 = isl_reordering_get_space(exp);
2600 isl_reordering_free(exp);
2601 return space1;
2602 error:
2603 isl_space_free(space1);
2604 isl_space_free(space2);
2605 return NULL;
2608 /* Given the space of set (domain), construct a space for a map
2609 * with as domain the given space and as range the range of "model".
2611 __isl_give isl_space *isl_space_extend_domain_with_range(
2612 __isl_take isl_space *space, __isl_take isl_space *model)
2614 if (!model)
2615 goto error;
2617 space = isl_space_from_domain(space);
2618 space = isl_space_add_dims(space, isl_dim_out,
2619 isl_space_dim(model, isl_dim_out));
2620 if (isl_space_has_tuple_id(model, isl_dim_out))
2621 space = isl_space_set_tuple_id(space, isl_dim_out,
2622 isl_space_get_tuple_id(model, isl_dim_out));
2623 if (!space)
2624 goto error;
2625 if (model->nested[1]) {
2626 isl_space *nested = isl_space_copy(model->nested[1]);
2627 int n_nested, n_space;
2628 nested = isl_space_align_params(nested, isl_space_copy(space));
2629 n_nested = isl_space_dim(nested, isl_dim_param);
2630 n_space = isl_space_dim(space, isl_dim_param);
2631 if (n_nested > n_space)
2632 nested = isl_space_drop_dims(nested, isl_dim_param,
2633 n_space, n_nested - n_space);
2634 if (!nested)
2635 goto error;
2636 space->nested[1] = nested;
2638 isl_space_free(model);
2639 return space;
2640 error:
2641 isl_space_free(model);
2642 isl_space_free(space);
2643 return NULL;
2646 /* Compare the "type" dimensions of two isl_spaces.
2648 * The order is fairly arbitrary.
2650 static int isl_space_cmp_type(__isl_keep isl_space *space1,
2651 __isl_keep isl_space *space2, enum isl_dim_type type)
2653 int cmp;
2654 isl_space *nested1, *nested2;
2656 if (isl_space_dim(space1, type) != isl_space_dim(space2, type))
2657 return isl_space_dim(space1, type) -
2658 isl_space_dim(space2, type);
2660 cmp = isl_id_cmp(tuple_id(space1, type), tuple_id(space2, type));
2661 if (cmp != 0)
2662 return cmp;
2664 nested1 = nested(space1, type);
2665 nested2 = nested(space2, type);
2666 if (!nested1 != !nested2)
2667 return !nested1 - !nested2;
2669 if (nested1)
2670 return isl_space_cmp(nested1, nested2);
2672 return 0;
2675 /* Compare two isl_spaces.
2677 * The order is fairly arbitrary.
2679 int isl_space_cmp(__isl_keep isl_space *space1, __isl_keep isl_space *space2)
2681 int i;
2682 int cmp;
2684 if (space1 == space2)
2685 return 0;
2686 if (!space1)
2687 return -1;
2688 if (!space2)
2689 return 1;
2691 cmp = isl_space_cmp_type(space1, space2, isl_dim_param);
2692 if (cmp != 0)
2693 return cmp;
2694 cmp = isl_space_cmp_type(space1, space2, isl_dim_in);
2695 if (cmp != 0)
2696 return cmp;
2697 cmp = isl_space_cmp_type(space1, space2, isl_dim_out);
2698 if (cmp != 0)
2699 return cmp;
2701 if (!space1->ids && !space2->ids)
2702 return 0;
2704 for (i = 0; i < n(space1, isl_dim_param); ++i) {
2705 cmp = isl_id_cmp(get_id(space1, isl_dim_param, i),
2706 get_id(space2, isl_dim_param, i));
2707 if (cmp != 0)
2708 return cmp;
2711 return 0;