isl_map_simplify.c: has_multiple_var_equality: return isl_bool
[isl.git] / isl_space.c
blob72f9c5d3731486ffa6a88c68e8b5c8eef93dadd0
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 /* Remove all parameters from "space".
1803 __isl_give isl_space *isl_space_drop_all_params(__isl_take isl_space *space)
1805 unsigned nparam;
1807 nparam = isl_space_dim(space, isl_dim_param);
1808 return isl_space_drop_dims(space, isl_dim_param, 0, nparam);
1811 __isl_give isl_space *isl_space_domain(__isl_take isl_space *space)
1813 if (!space)
1814 return NULL;
1815 space = isl_space_drop_dims(space, isl_dim_out, 0, space->n_out);
1816 space = isl_space_reverse(space);
1817 space = mark_as_set(space);
1818 return space;
1821 __isl_give isl_space *isl_space_from_domain(__isl_take isl_space *space)
1823 if (!space)
1824 return NULL;
1825 if (!isl_space_is_set(space))
1826 isl_die(isl_space_get_ctx(space), isl_error_invalid,
1827 "not a set space", goto error);
1828 space = isl_space_reverse(space);
1829 space = isl_space_reset(space, isl_dim_out);
1830 return space;
1831 error:
1832 isl_space_free(space);
1833 return NULL;
1836 __isl_give isl_space *isl_space_range(__isl_take isl_space *space)
1838 if (!space)
1839 return NULL;
1840 space = isl_space_drop_dims(space, isl_dim_in, 0, space->n_in);
1841 space = mark_as_set(space);
1842 return space;
1845 __isl_give isl_space *isl_space_from_range(__isl_take isl_space *space)
1847 if (!space)
1848 return NULL;
1849 if (!isl_space_is_set(space))
1850 isl_die(isl_space_get_ctx(space), isl_error_invalid,
1851 "not a set space", goto error);
1852 return isl_space_reset(space, isl_dim_in);
1853 error:
1854 isl_space_free(space);
1855 return NULL;
1858 /* Given a map space A -> B, return the map space [A -> B] -> A.
1860 __isl_give isl_space *isl_space_domain_map(__isl_take isl_space *space)
1862 isl_space *domain;
1864 domain = isl_space_from_range(isl_space_domain(isl_space_copy(space)));
1865 space = isl_space_from_domain(isl_space_wrap(space));
1866 space = isl_space_join(space, domain);
1868 return space;
1871 /* Given a map space A -> B, return the map space [A -> B] -> B.
1873 __isl_give isl_space *isl_space_range_map(__isl_take isl_space *space)
1875 isl_space *range;
1877 range = isl_space_from_range(isl_space_range(isl_space_copy(space)));
1878 space = isl_space_from_domain(isl_space_wrap(space));
1879 space = isl_space_join(space, range);
1881 return space;
1884 __isl_give isl_space *isl_space_params(__isl_take isl_space *space)
1886 if (isl_space_is_params(space))
1887 return space;
1888 space = isl_space_drop_dims(space,
1889 isl_dim_in, 0, isl_space_dim(space, isl_dim_in));
1890 space = isl_space_drop_dims(space,
1891 isl_dim_out, 0, isl_space_dim(space, isl_dim_out));
1892 space = mark_as_params(space);
1893 return space;
1896 __isl_give isl_space *isl_space_set_from_params(__isl_take isl_space *space)
1898 if (!space)
1899 return NULL;
1900 if (!isl_space_is_params(space))
1901 isl_die(isl_space_get_ctx(space), isl_error_invalid,
1902 "not a parameter space", goto error);
1903 return isl_space_reset(space, isl_dim_set);
1904 error:
1905 isl_space_free(space);
1906 return NULL;
1909 __isl_give isl_space *isl_space_underlying(__isl_take isl_space *space,
1910 unsigned n_div)
1912 int i;
1913 isl_bool is_set;
1915 is_set = isl_space_is_set(space);
1916 if (is_set < 0)
1917 return isl_space_free(space);
1918 if (n_div == 0 && is_set &&
1919 space->nparam == 0 && space->n_in == 0 && space->n_id == 0)
1920 return isl_space_reset(space, isl_dim_out);
1921 space = isl_space_cow(space);
1922 if (!space)
1923 return NULL;
1924 space->n_out += space->nparam + space->n_in + n_div;
1925 space->nparam = 0;
1926 space->n_in = 0;
1928 for (i = 0; i < space->n_id; ++i)
1929 isl_id_free(get_id(space, isl_dim_out, i));
1930 space->n_id = 0;
1931 space = isl_space_reset(space, isl_dim_in);
1932 space = isl_space_reset(space, isl_dim_out);
1933 space = mark_as_set(space);
1935 return space;
1938 /* Are the two spaces the same, including positions and names of parameters?
1940 isl_bool isl_space_is_equal(__isl_keep isl_space *space1,
1941 __isl_keep isl_space *space2)
1943 isl_bool equal;
1945 if (!space1 || !space2)
1946 return isl_bool_error;
1947 if (space1 == space2)
1948 return isl_bool_true;
1949 equal = isl_space_has_equal_params(space1, space2);
1950 if (equal < 0 || !equal)
1951 return equal;
1952 return isl_space_has_equal_tuples(space1, space2);
1955 /* Do the tuples of "space1" correspond to those of the domain of "space2"?
1956 * That is, is "space1" eqaul to the domain of "space2", ignoring parameters.
1958 * "space2" is allowed to be a set space, in which case "space1"
1959 * should be a parameter space.
1961 isl_bool isl_space_has_domain_tuples(__isl_keep isl_space *space1,
1962 __isl_keep isl_space *space2)
1964 isl_bool is_set;
1966 is_set = isl_space_is_set(space1);
1967 if (is_set < 0 || !is_set)
1968 return is_set;
1969 return isl_space_tuple_is_equal(space1, isl_dim_set,
1970 space2, isl_dim_in);
1973 /* Is space1 equal to the domain of space2?
1975 * In the internal version we also allow space2 to be the space of a set,
1976 * provided space1 is a parameter space.
1978 isl_bool isl_space_is_domain_internal(__isl_keep isl_space *space1,
1979 __isl_keep isl_space *space2)
1981 isl_bool equal_params;
1983 if (!space1 || !space2)
1984 return isl_bool_error;
1985 equal_params = isl_space_has_equal_params(space1, space2);
1986 if (equal_params < 0 || !equal_params)
1987 return equal_params;
1988 return isl_space_has_domain_tuples(space1, space2);
1991 /* Is space1 equal to the domain of space2?
1993 isl_bool isl_space_is_domain(__isl_keep isl_space *space1,
1994 __isl_keep isl_space *space2)
1996 if (!space2)
1997 return isl_bool_error;
1998 if (!isl_space_is_map(space2))
1999 return isl_bool_false;
2000 return isl_space_is_domain_internal(space1, space2);
2003 /* Is space1 equal to the range of space2?
2005 * In the internal version, space2 is allowed to be the space of a set,
2006 * in which case it should be equal to space1.
2008 isl_bool isl_space_is_range_internal(__isl_keep isl_space *space1,
2009 __isl_keep isl_space *space2)
2011 isl_bool equal_params;
2013 if (!space1 || !space2)
2014 return isl_bool_error;
2015 if (!isl_space_is_set(space1))
2016 return isl_bool_false;
2017 equal_params = isl_space_has_equal_params(space1, space2);
2018 if (equal_params < 0 || !equal_params)
2019 return equal_params;
2020 return isl_space_tuple_is_equal(space1, isl_dim_set,
2021 space2, isl_dim_out);
2024 /* Is space1 equal to the range of space2?
2026 isl_bool isl_space_is_range(__isl_keep isl_space *space1,
2027 __isl_keep isl_space *space2)
2029 if (!space2)
2030 return isl_bool_error;
2031 if (!isl_space_is_map(space2))
2032 return isl_bool_false;
2033 return isl_space_is_range_internal(space1, space2);
2036 /* Update "hash" by hashing in the parameters of "space".
2038 static uint32_t isl_hash_params(uint32_t hash, __isl_keep isl_space *space)
2040 int i;
2041 isl_id *id;
2043 if (!space)
2044 return hash;
2046 isl_hash_byte(hash, space->nparam % 256);
2048 for (i = 0; i < space->nparam; ++i) {
2049 id = get_id(space, isl_dim_param, i);
2050 hash = isl_hash_id(hash, id);
2053 return hash;
2056 /* Update "hash" by hashing in the tuples of "space".
2057 * Changes in this function should be reflected in isl_hash_tuples_domain.
2059 static uint32_t isl_hash_tuples(uint32_t hash, __isl_keep isl_space *space)
2061 isl_id *id;
2063 if (!space)
2064 return hash;
2066 isl_hash_byte(hash, space->n_in % 256);
2067 isl_hash_byte(hash, space->n_out % 256);
2069 id = tuple_id(space, isl_dim_in);
2070 hash = isl_hash_id(hash, id);
2071 id = tuple_id(space, isl_dim_out);
2072 hash = isl_hash_id(hash, id);
2074 hash = isl_hash_tuples(hash, space->nested[0]);
2075 hash = isl_hash_tuples(hash, space->nested[1]);
2077 return hash;
2080 /* Update "hash" by hashing in the domain tuple of "space".
2081 * The result of this function is equal to the result of applying
2082 * isl_hash_tuples to the domain of "space".
2084 static uint32_t isl_hash_tuples_domain(uint32_t hash,
2085 __isl_keep isl_space *space)
2087 isl_id *id;
2089 if (!space)
2090 return hash;
2092 isl_hash_byte(hash, 0);
2093 isl_hash_byte(hash, space->n_in % 256);
2095 hash = isl_hash_id(hash, &isl_id_none);
2096 id = tuple_id(space, isl_dim_in);
2097 hash = isl_hash_id(hash, id);
2099 hash = isl_hash_tuples(hash, space->nested[0]);
2101 return hash;
2104 /* Return a hash value that digests the tuples of "space",
2105 * i.e., that ignores the parameters.
2107 uint32_t isl_space_get_tuple_hash(__isl_keep isl_space *space)
2109 uint32_t hash;
2111 if (!space)
2112 return 0;
2114 hash = isl_hash_init();
2115 hash = isl_hash_tuples(hash, space);
2117 return hash;
2120 uint32_t isl_space_get_hash(__isl_keep isl_space *space)
2122 uint32_t hash;
2124 if (!space)
2125 return 0;
2127 hash = isl_hash_init();
2128 hash = isl_hash_params(hash, space);
2129 hash = isl_hash_tuples(hash, space);
2131 return hash;
2134 /* Return the hash value of the domain of "space".
2135 * That is, isl_space_get_domain_hash(space) is equal to
2136 * isl_space_get_hash(isl_space_domain(space)).
2138 uint32_t isl_space_get_domain_hash(__isl_keep isl_space *space)
2140 uint32_t hash;
2142 if (!space)
2143 return 0;
2145 hash = isl_hash_init();
2146 hash = isl_hash_params(hash, space);
2147 hash = isl_hash_tuples_domain(hash, space);
2149 return hash;
2152 /* Is "space" the space of a set wrapping a map space?
2154 isl_bool isl_space_is_wrapping(__isl_keep isl_space *space)
2156 if (!space)
2157 return isl_bool_error;
2159 if (!isl_space_is_set(space))
2160 return isl_bool_false;
2162 return space->nested[1] != NULL;
2165 /* Is "space" the space of a map where the domain is a wrapped map space?
2167 isl_bool isl_space_domain_is_wrapping(__isl_keep isl_space *space)
2169 if (!space)
2170 return isl_bool_error;
2172 if (isl_space_is_set(space))
2173 return isl_bool_false;
2175 return space->nested[0] != NULL;
2178 /* Is "space" the space of a map where the range is a wrapped map space?
2180 isl_bool isl_space_range_is_wrapping(__isl_keep isl_space *space)
2182 if (!space)
2183 return isl_bool_error;
2185 if (isl_space_is_set(space))
2186 return isl_bool_false;
2188 return space->nested[1] != NULL;
2191 /* Is "space" a product of two spaces?
2192 * That is, is it a wrapping set space or a map space
2193 * with wrapping domain and range?
2195 isl_bool isl_space_is_product(__isl_keep isl_space *space)
2197 isl_bool is_set;
2198 isl_bool is_product;
2200 is_set = isl_space_is_set(space);
2201 if (is_set < 0)
2202 return isl_bool_error;
2203 if (is_set)
2204 return isl_space_is_wrapping(space);
2205 is_product = isl_space_domain_is_wrapping(space);
2206 if (is_product < 0 || !is_product)
2207 return is_product;
2208 return isl_space_range_is_wrapping(space);
2211 __isl_give isl_space *isl_space_wrap(__isl_take isl_space *space)
2213 isl_space *wrap;
2215 if (!space)
2216 return NULL;
2218 wrap = isl_space_set_alloc(space->ctx,
2219 space->nparam, space->n_in + space->n_out);
2221 wrap = copy_ids(wrap, isl_dim_param, 0, space, isl_dim_param);
2222 wrap = copy_ids(wrap, isl_dim_set, 0, space, isl_dim_in);
2223 wrap = copy_ids(wrap, isl_dim_set, space->n_in, space, isl_dim_out);
2225 if (!wrap)
2226 goto error;
2228 wrap->nested[1] = space;
2230 return wrap;
2231 error:
2232 isl_space_free(space);
2233 return NULL;
2236 __isl_give isl_space *isl_space_unwrap(__isl_take isl_space *space)
2238 isl_space *unwrap;
2240 if (!space)
2241 return NULL;
2243 if (!isl_space_is_wrapping(space))
2244 isl_die(space->ctx, isl_error_invalid, "not a wrapping space",
2245 goto error);
2247 unwrap = isl_space_copy(space->nested[1]);
2248 isl_space_free(space);
2250 return unwrap;
2251 error:
2252 isl_space_free(space);
2253 return NULL;
2256 isl_bool isl_space_is_named_or_nested(__isl_keep isl_space *space,
2257 enum isl_dim_type type)
2259 if (type != isl_dim_in && type != isl_dim_out)
2260 return isl_bool_false;
2261 if (!space)
2262 return isl_bool_error;
2263 if (space->tuple_id[type - isl_dim_in])
2264 return isl_bool_true;
2265 if (space->nested[type - isl_dim_in])
2266 return isl_bool_true;
2267 return isl_bool_false;
2270 isl_bool isl_space_may_be_set(__isl_keep isl_space *space)
2272 isl_bool nested;
2274 if (!space)
2275 return isl_bool_error;
2276 if (isl_space_is_set(space))
2277 return isl_bool_true;
2278 if (isl_space_dim(space, isl_dim_in) != 0)
2279 return isl_bool_false;
2280 nested = isl_space_is_named_or_nested(space, isl_dim_in);
2281 if (nested < 0 || nested)
2282 return isl_bool_not(nested);
2283 return isl_bool_true;
2286 __isl_give isl_space *isl_space_reset(__isl_take isl_space *space,
2287 enum isl_dim_type type)
2289 if (!isl_space_is_named_or_nested(space, type))
2290 return space;
2292 space = isl_space_cow(space);
2293 if (!space)
2294 return NULL;
2296 isl_id_free(space->tuple_id[type - isl_dim_in]);
2297 space->tuple_id[type - isl_dim_in] = NULL;
2298 isl_space_free(space->nested[type - isl_dim_in]);
2299 space->nested[type - isl_dim_in] = NULL;
2301 return space;
2304 __isl_give isl_space *isl_space_flatten(__isl_take isl_space *space)
2306 if (!space)
2307 return NULL;
2308 if (!space->nested[0] && !space->nested[1])
2309 return space;
2311 if (space->nested[0])
2312 space = isl_space_reset(space, isl_dim_in);
2313 if (space && space->nested[1])
2314 space = isl_space_reset(space, isl_dim_out);
2316 return space;
2319 __isl_give isl_space *isl_space_flatten_domain(__isl_take isl_space *space)
2321 if (!space)
2322 return NULL;
2323 if (!space->nested[0])
2324 return space;
2326 return isl_space_reset(space, isl_dim_in);
2329 __isl_give isl_space *isl_space_flatten_range(__isl_take isl_space *space)
2331 if (!space)
2332 return NULL;
2333 if (!space->nested[1])
2334 return space;
2336 return isl_space_reset(space, isl_dim_out);
2339 /* Replace the parameters of dst by those of src.
2341 __isl_give isl_space *isl_space_replace_params(__isl_take isl_space *dst,
2342 __isl_keep isl_space *src)
2344 isl_bool equal_params;
2345 enum isl_dim_type type = isl_dim_param;
2347 equal_params = isl_space_has_equal_params(dst, src);
2348 if (equal_params < 0)
2349 return isl_space_free(dst);
2350 if (equal_params)
2351 return dst;
2353 dst = isl_space_cow(dst);
2355 if (!dst || !src)
2356 goto error;
2358 dst = isl_space_drop_dims(dst, type, 0, isl_space_dim(dst, type));
2359 dst = isl_space_add_dims(dst, type, isl_space_dim(src, type));
2360 dst = copy_ids(dst, type, 0, src, type);
2362 if (dst) {
2363 int i;
2364 for (i = 0; i <= 1; ++i) {
2365 if (!dst->nested[i])
2366 continue;
2367 dst->nested[i] = isl_space_replace_params(
2368 dst->nested[i], src);
2369 if (!dst->nested[i])
2370 goto error;
2374 return dst;
2375 error:
2376 isl_space_free(dst);
2377 return NULL;
2380 /* Given a dimension specification "dim" of a set, create a dimension
2381 * specification for the lift of the set. In particular, the result
2382 * is of the form [dim -> local[..]], with n_local variables in the
2383 * range of the wrapped map.
2385 __isl_give isl_space *isl_space_lift(__isl_take isl_space *space,
2386 unsigned n_local)
2388 isl_space *local_space;
2390 if (!space)
2391 return NULL;
2393 local_space = isl_space_dup(space);
2394 local_space = isl_space_drop_dims(local_space, isl_dim_set, 0,
2395 space->n_out);
2396 local_space = isl_space_add_dims(local_space, isl_dim_set, n_local);
2397 local_space = isl_space_set_tuple_name(local_space,
2398 isl_dim_set, "local");
2399 space = isl_space_join(isl_space_from_domain(space),
2400 isl_space_from_range(local_space));
2401 space = isl_space_wrap(space);
2402 space = isl_space_set_tuple_name(space, isl_dim_set, "lifted");
2404 return space;
2407 isl_bool isl_space_can_zip(__isl_keep isl_space *space)
2409 isl_bool is_set;
2411 is_set = isl_space_is_set(space);
2412 if (is_set < 0)
2413 return isl_bool_error;
2414 if (is_set)
2415 return isl_bool_false;
2416 return isl_space_is_product(space);
2419 __isl_give isl_space *isl_space_zip(__isl_take isl_space *space)
2421 isl_space *dom, *ran;
2422 isl_space *dom_dom, *dom_ran, *ran_dom, *ran_ran;
2424 if (!isl_space_can_zip(space))
2425 isl_die(space->ctx, isl_error_invalid, "dim cannot be zipped",
2426 goto error);
2428 if (!space)
2429 return NULL;
2430 dom = isl_space_unwrap(isl_space_domain(isl_space_copy(space)));
2431 ran = isl_space_unwrap(isl_space_range(space));
2432 dom_dom = isl_space_domain(isl_space_copy(dom));
2433 dom_ran = isl_space_range(dom);
2434 ran_dom = isl_space_domain(isl_space_copy(ran));
2435 ran_ran = isl_space_range(ran);
2436 dom = isl_space_join(isl_space_from_domain(dom_dom),
2437 isl_space_from_range(ran_dom));
2438 ran = isl_space_join(isl_space_from_domain(dom_ran),
2439 isl_space_from_range(ran_ran));
2440 return isl_space_join(isl_space_from_domain(isl_space_wrap(dom)),
2441 isl_space_from_range(isl_space_wrap(ran)));
2442 error:
2443 isl_space_free(space);
2444 return NULL;
2447 /* Can we apply isl_space_curry to "space"?
2448 * That is, does is it have a map space with a nested relation in its domain?
2450 isl_bool isl_space_can_curry(__isl_keep isl_space *space)
2452 return isl_space_domain_is_wrapping(space);
2455 /* Given a space (A -> B) -> C, return the corresponding space
2456 * A -> (B -> C).
2458 __isl_give isl_space *isl_space_curry(__isl_take isl_space *space)
2460 isl_space *dom, *ran;
2461 isl_space *dom_dom, *dom_ran;
2463 if (!space)
2464 return NULL;
2466 if (!isl_space_can_curry(space))
2467 isl_die(space->ctx, isl_error_invalid,
2468 "space cannot be curried", goto error);
2470 dom = isl_space_unwrap(isl_space_domain(isl_space_copy(space)));
2471 ran = isl_space_range(space);
2472 dom_dom = isl_space_domain(isl_space_copy(dom));
2473 dom_ran = isl_space_range(dom);
2474 ran = isl_space_join(isl_space_from_domain(dom_ran),
2475 isl_space_from_range(ran));
2476 return isl_space_join(isl_space_from_domain(dom_dom),
2477 isl_space_from_range(isl_space_wrap(ran)));
2478 error:
2479 isl_space_free(space);
2480 return NULL;
2483 /* Can isl_space_range_curry be applied to "space"?
2484 * That is, does it have a nested relation in its range,
2485 * the domain of which is itself a nested relation?
2487 isl_bool isl_space_can_range_curry(__isl_keep isl_space *space)
2489 isl_bool can;
2491 if (!space)
2492 return isl_bool_error;
2493 can = isl_space_range_is_wrapping(space);
2494 if (can < 0 || !can)
2495 return can;
2496 return isl_space_can_curry(space->nested[1]);
2499 /* Given a space A -> ((B -> C) -> D), return the corresponding space
2500 * A -> (B -> (C -> D)).
2502 __isl_give isl_space *isl_space_range_curry(__isl_take isl_space *space)
2504 if (!space)
2505 return NULL;
2507 if (!isl_space_can_range_curry(space))
2508 isl_die(isl_space_get_ctx(space), isl_error_invalid,
2509 "space range cannot be curried",
2510 return isl_space_free(space));
2512 space = isl_space_cow(space);
2513 if (!space)
2514 return NULL;
2515 space->nested[1] = isl_space_curry(space->nested[1]);
2516 if (!space->nested[1])
2517 return isl_space_free(space);
2519 return space;
2522 /* Can we apply isl_space_uncurry to "space"?
2523 * That is, does it have a map space with a nested relation in its range?
2525 isl_bool isl_space_can_uncurry(__isl_keep isl_space *space)
2527 return isl_space_range_is_wrapping(space);
2530 /* Given a space A -> (B -> C), return the corresponding space
2531 * (A -> B) -> C.
2533 __isl_give isl_space *isl_space_uncurry(__isl_take isl_space *space)
2535 isl_space *dom, *ran;
2536 isl_space *ran_dom, *ran_ran;
2538 if (!space)
2539 return NULL;
2541 if (!isl_space_can_uncurry(space))
2542 isl_die(space->ctx, isl_error_invalid,
2543 "space cannot be uncurried",
2544 return isl_space_free(space));
2546 dom = isl_space_domain(isl_space_copy(space));
2547 ran = isl_space_unwrap(isl_space_range(space));
2548 ran_dom = isl_space_domain(isl_space_copy(ran));
2549 ran_ran = isl_space_range(ran);
2550 dom = isl_space_join(isl_space_from_domain(dom),
2551 isl_space_from_range(ran_dom));
2552 return isl_space_join(isl_space_from_domain(isl_space_wrap(dom)),
2553 isl_space_from_range(ran_ran));
2556 isl_bool isl_space_has_named_params(__isl_keep isl_space *space)
2558 int i;
2559 unsigned off;
2561 if (!space)
2562 return isl_bool_error;
2563 if (space->nparam == 0)
2564 return isl_bool_true;
2565 off = isl_space_offset(space, isl_dim_param);
2566 if (off + space->nparam > space->n_id)
2567 return isl_bool_false;
2568 for (i = 0; i < space->nparam; ++i)
2569 if (!space->ids[off + i])
2570 return isl_bool_false;
2571 return isl_bool_true;
2574 /* Check that "space" has only named parameters, reporting an error
2575 * if it does not.
2577 isl_stat isl_space_check_named_params(__isl_keep isl_space *space)
2579 isl_bool named;
2581 named = isl_space_has_named_params(space);
2582 if (named < 0)
2583 return isl_stat_error;
2584 if (!named)
2585 isl_die(isl_space_get_ctx(space), isl_error_invalid,
2586 "unexpected unnamed parameters", return isl_stat_error);
2588 return isl_stat_ok;
2591 /* Align the initial parameters of space1 to match the order in space2.
2593 __isl_give isl_space *isl_space_align_params(__isl_take isl_space *space1,
2594 __isl_take isl_space *space2)
2596 isl_reordering *exp;
2598 if (isl_space_check_named_params(space1) < 0 ||
2599 isl_space_check_named_params(space2) < 0)
2600 goto error;
2602 exp = isl_parameter_alignment_reordering(space1, space2);
2603 exp = isl_reordering_extend_space(exp, space1);
2604 isl_space_free(space2);
2605 space1 = isl_reordering_get_space(exp);
2606 isl_reordering_free(exp);
2607 return space1;
2608 error:
2609 isl_space_free(space1);
2610 isl_space_free(space2);
2611 return NULL;
2614 /* Given the space of set (domain), construct a space for a map
2615 * with as domain the given space and as range the range of "model".
2617 __isl_give isl_space *isl_space_extend_domain_with_range(
2618 __isl_take isl_space *space, __isl_take isl_space *model)
2620 if (!model)
2621 goto error;
2623 space = isl_space_from_domain(space);
2624 space = isl_space_add_dims(space, isl_dim_out,
2625 isl_space_dim(model, isl_dim_out));
2626 if (isl_space_has_tuple_id(model, isl_dim_out))
2627 space = isl_space_set_tuple_id(space, isl_dim_out,
2628 isl_space_get_tuple_id(model, isl_dim_out));
2629 if (!space)
2630 goto error;
2631 if (model->nested[1]) {
2632 isl_space *nested = isl_space_copy(model->nested[1]);
2633 int n_nested, n_space;
2634 nested = isl_space_align_params(nested, isl_space_copy(space));
2635 n_nested = isl_space_dim(nested, isl_dim_param);
2636 n_space = isl_space_dim(space, isl_dim_param);
2637 if (n_nested > n_space)
2638 nested = isl_space_drop_dims(nested, isl_dim_param,
2639 n_space, n_nested - n_space);
2640 if (!nested)
2641 goto error;
2642 space->nested[1] = nested;
2644 isl_space_free(model);
2645 return space;
2646 error:
2647 isl_space_free(model);
2648 isl_space_free(space);
2649 return NULL;
2652 /* Compare the "type" dimensions of two isl_spaces.
2654 * The order is fairly arbitrary.
2656 static int isl_space_cmp_type(__isl_keep isl_space *space1,
2657 __isl_keep isl_space *space2, enum isl_dim_type type)
2659 int cmp;
2660 isl_space *nested1, *nested2;
2662 if (isl_space_dim(space1, type) != isl_space_dim(space2, type))
2663 return isl_space_dim(space1, type) -
2664 isl_space_dim(space2, type);
2666 cmp = isl_id_cmp(tuple_id(space1, type), tuple_id(space2, type));
2667 if (cmp != 0)
2668 return cmp;
2670 nested1 = nested(space1, type);
2671 nested2 = nested(space2, type);
2672 if (!nested1 != !nested2)
2673 return !nested1 - !nested2;
2675 if (nested1)
2676 return isl_space_cmp(nested1, nested2);
2678 return 0;
2681 /* Compare two isl_spaces.
2683 * The order is fairly arbitrary.
2685 int isl_space_cmp(__isl_keep isl_space *space1, __isl_keep isl_space *space2)
2687 int i;
2688 int cmp;
2690 if (space1 == space2)
2691 return 0;
2692 if (!space1)
2693 return -1;
2694 if (!space2)
2695 return 1;
2697 cmp = isl_space_cmp_type(space1, space2, isl_dim_param);
2698 if (cmp != 0)
2699 return cmp;
2700 cmp = isl_space_cmp_type(space1, space2, isl_dim_in);
2701 if (cmp != 0)
2702 return cmp;
2703 cmp = isl_space_cmp_type(space1, space2, isl_dim_out);
2704 if (cmp != 0)
2705 return cmp;
2707 if (!space1->ids && !space2->ids)
2708 return 0;
2710 for (i = 0; i < n(space1, isl_dim_param); ++i) {
2711 cmp = isl_id_cmp(get_id(space1, isl_dim_param, i),
2712 get_id(space2, isl_dim_param, i));
2713 if (cmp != 0)
2714 return cmp;
2717 return 0;