isl_*_aff_read_from_str: do not read domains as rational sets
[isl.git] / isl_space.c
blobb2f5ef5b0c6938d40762cf9ac5f572ec2bea96d6
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2013-2014 Ecole Normale Superieure
6 * Use of this software is governed by the MIT license
8 * Written by Sven Verdoolaege, K.U.Leuven, Departement
9 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
10 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
11 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
12 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
15 #include <stdlib.h>
16 #include <string.h>
17 #include <isl_space_private.h>
18 #include <isl_id_private.h>
19 #include <isl_reordering.h>
21 isl_ctx *isl_space_get_ctx(__isl_keep isl_space *dim)
23 return dim ? dim->ctx : NULL;
26 __isl_give isl_space *isl_space_alloc(isl_ctx *ctx,
27 unsigned nparam, unsigned n_in, unsigned n_out)
29 isl_space *dim;
31 dim = isl_alloc_type(ctx, struct isl_space);
32 if (!dim)
33 return NULL;
35 dim->ctx = ctx;
36 isl_ctx_ref(ctx);
37 dim->ref = 1;
38 dim->nparam = nparam;
39 dim->n_in = n_in;
40 dim->n_out = n_out;
42 dim->tuple_id[0] = NULL;
43 dim->tuple_id[1] = NULL;
45 dim->nested[0] = NULL;
46 dim->nested[1] = NULL;
48 dim->n_id = 0;
49 dim->ids = NULL;
51 return dim;
54 /* Mark the space as being that of a set, by setting the domain tuple
55 * to isl_id_none.
57 static __isl_give isl_space *mark_as_set(__isl_take isl_space *space)
59 space = isl_space_cow(space);
60 if (!space)
61 return NULL;
62 space = isl_space_set_tuple_id(space, isl_dim_in, &isl_id_none);
63 return space;
66 /* Is the space that of a set?
68 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 /* Is the given space that of a map?
81 isl_bool isl_space_is_map(__isl_keep isl_space *space)
83 if (!space)
84 return isl_bool_error;
85 return space->tuple_id[0] != &isl_id_none &&
86 space->tuple_id[1] != &isl_id_none;
89 __isl_give isl_space *isl_space_set_alloc(isl_ctx *ctx,
90 unsigned nparam, unsigned dim)
92 isl_space *space;
93 space = isl_space_alloc(ctx, nparam, 0, dim);
94 space = mark_as_set(space);
95 return space;
98 /* Mark the space as being that of a parameter domain, by setting
99 * both tuples to isl_id_none.
101 static __isl_give isl_space *mark_as_params(isl_space *space)
103 if (!space)
104 return NULL;
105 space = isl_space_set_tuple_id(space, isl_dim_in, &isl_id_none);
106 space = isl_space_set_tuple_id(space, isl_dim_out, &isl_id_none);
107 return space;
110 /* Is the space that of a parameter domain?
112 isl_bool isl_space_is_params(__isl_keep isl_space *space)
114 if (!space)
115 return isl_bool_error;
116 if (space->n_in != 0 || space->nested[0] ||
117 space->n_out != 0 || space->nested[1])
118 return isl_bool_false;
119 if (space->tuple_id[0] != &isl_id_none)
120 return isl_bool_false;
121 if (space->tuple_id[1] != &isl_id_none)
122 return isl_bool_false;
123 return isl_bool_true;
126 /* Create a space for a parameter domain.
128 __isl_give isl_space *isl_space_params_alloc(isl_ctx *ctx, unsigned nparam)
130 isl_space *space;
131 space = isl_space_alloc(ctx, nparam, 0, 0);
132 space = mark_as_params(space);
133 return space;
136 static unsigned global_pos(__isl_keep isl_space *dim,
137 enum isl_dim_type type, unsigned pos)
139 struct isl_ctx *ctx = dim->ctx;
141 switch (type) {
142 case isl_dim_param:
143 isl_assert(ctx, pos < dim->nparam,
144 return isl_space_dim(dim, isl_dim_all));
145 return pos;
146 case isl_dim_in:
147 isl_assert(ctx, pos < dim->n_in,
148 return isl_space_dim(dim, isl_dim_all));
149 return pos + dim->nparam;
150 case isl_dim_out:
151 isl_assert(ctx, pos < dim->n_out,
152 return isl_space_dim(dim, isl_dim_all));
153 return pos + dim->nparam + dim->n_in;
154 default:
155 isl_assert(ctx, 0, return isl_space_dim(dim, isl_dim_all));
157 return isl_space_dim(dim, isl_dim_all);
160 /* Extend length of ids array to the total number of dimensions.
162 static __isl_give isl_space *extend_ids(__isl_take isl_space *dim)
164 isl_id **ids;
165 int i;
167 if (isl_space_dim(dim, isl_dim_all) <= dim->n_id)
168 return dim;
170 if (!dim->ids) {
171 dim->ids = isl_calloc_array(dim->ctx,
172 isl_id *, isl_space_dim(dim, isl_dim_all));
173 if (!dim->ids)
174 goto error;
175 } else {
176 ids = isl_realloc_array(dim->ctx, dim->ids,
177 isl_id *, isl_space_dim(dim, isl_dim_all));
178 if (!ids)
179 goto error;
180 dim->ids = ids;
181 for (i = dim->n_id; i < isl_space_dim(dim, isl_dim_all); ++i)
182 dim->ids[i] = NULL;
185 dim->n_id = isl_space_dim(dim, isl_dim_all);
187 return dim;
188 error:
189 isl_space_free(dim);
190 return NULL;
193 static __isl_give isl_space *set_id(__isl_take isl_space *dim,
194 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
196 dim = isl_space_cow(dim);
198 if (!dim)
199 goto error;
201 pos = global_pos(dim, type, pos);
202 if (pos == isl_space_dim(dim, isl_dim_all))
203 goto error;
205 if (pos >= dim->n_id) {
206 if (!id)
207 return dim;
208 dim = extend_ids(dim);
209 if (!dim)
210 goto error;
213 dim->ids[pos] = id;
215 return dim;
216 error:
217 isl_id_free(id);
218 isl_space_free(dim);
219 return NULL;
222 static __isl_keep isl_id *get_id(__isl_keep isl_space *dim,
223 enum isl_dim_type type, unsigned pos)
225 if (!dim)
226 return NULL;
228 pos = global_pos(dim, type, pos);
229 if (pos == isl_space_dim(dim, isl_dim_all))
230 return NULL;
231 if (pos >= dim->n_id)
232 return NULL;
233 return dim->ids[pos];
236 static unsigned offset(__isl_keep isl_space *dim, enum isl_dim_type type)
238 switch (type) {
239 case isl_dim_param: return 0;
240 case isl_dim_in: return dim->nparam;
241 case isl_dim_out: return dim->nparam + dim->n_in;
242 default: return 0;
246 static unsigned n(__isl_keep isl_space *dim, enum isl_dim_type type)
248 switch (type) {
249 case isl_dim_param: return dim->nparam;
250 case isl_dim_in: return dim->n_in;
251 case isl_dim_out: return dim->n_out;
252 case isl_dim_all: return dim->nparam + dim->n_in + dim->n_out;
253 default: return 0;
257 unsigned isl_space_dim(__isl_keep isl_space *dim, enum isl_dim_type type)
259 if (!dim)
260 return 0;
261 return n(dim, type);
264 unsigned isl_space_offset(__isl_keep isl_space *dim, enum isl_dim_type type)
266 if (!dim)
267 return 0;
268 return offset(dim, type);
271 static __isl_give isl_space *copy_ids(__isl_take isl_space *dst,
272 enum isl_dim_type dst_type, unsigned offset, __isl_keep isl_space *src,
273 enum isl_dim_type src_type)
275 int i;
276 isl_id *id;
278 if (!dst)
279 return NULL;
281 for (i = 0; i < n(src, src_type); ++i) {
282 id = get_id(src, src_type, i);
283 if (!id)
284 continue;
285 dst = set_id(dst, dst_type, offset + i, isl_id_copy(id));
286 if (!dst)
287 return NULL;
289 return dst;
292 __isl_take isl_space *isl_space_dup(__isl_keep isl_space *dim)
294 isl_space *dup;
295 if (!dim)
296 return NULL;
297 dup = isl_space_alloc(dim->ctx, dim->nparam, dim->n_in, dim->n_out);
298 if (!dup)
299 return NULL;
300 if (dim->tuple_id[0] &&
301 !(dup->tuple_id[0] = isl_id_copy(dim->tuple_id[0])))
302 goto error;
303 if (dim->tuple_id[1] &&
304 !(dup->tuple_id[1] = isl_id_copy(dim->tuple_id[1])))
305 goto error;
306 if (dim->nested[0] && !(dup->nested[0] = isl_space_copy(dim->nested[0])))
307 goto error;
308 if (dim->nested[1] && !(dup->nested[1] = isl_space_copy(dim->nested[1])))
309 goto error;
310 if (!dim->ids)
311 return dup;
312 dup = copy_ids(dup, isl_dim_param, 0, dim, isl_dim_param);
313 dup = copy_ids(dup, isl_dim_in, 0, dim, isl_dim_in);
314 dup = copy_ids(dup, isl_dim_out, 0, dim, isl_dim_out);
315 return dup;
316 error:
317 isl_space_free(dup);
318 return NULL;
321 __isl_give isl_space *isl_space_cow(__isl_take isl_space *dim)
323 if (!dim)
324 return NULL;
326 if (dim->ref == 1)
327 return dim;
328 dim->ref--;
329 return isl_space_dup(dim);
332 __isl_give isl_space *isl_space_copy(__isl_keep isl_space *dim)
334 if (!dim)
335 return NULL;
337 dim->ref++;
338 return dim;
341 __isl_null isl_space *isl_space_free(__isl_take isl_space *space)
343 int i;
345 if (!space)
346 return NULL;
348 if (--space->ref > 0)
349 return NULL;
351 isl_id_free(space->tuple_id[0]);
352 isl_id_free(space->tuple_id[1]);
354 isl_space_free(space->nested[0]);
355 isl_space_free(space->nested[1]);
357 for (i = 0; i < space->n_id; ++i)
358 isl_id_free(space->ids[i]);
359 free(space->ids);
360 isl_ctx_deref(space->ctx);
362 free(space);
364 return NULL;
367 /* Check if "s" is a valid dimension or tuple name.
368 * We currently only forbid names that look like a number.
370 * s is assumed to be non-NULL.
372 static int name_ok(isl_ctx *ctx, const char *s)
374 char *p;
375 long dummy;
377 dummy = strtol(s, &p, 0);
378 if (p != s)
379 isl_die(ctx, isl_error_invalid, "name looks like a number",
380 return 0);
382 return 1;
385 /* Is it possible for the given dimension type to have a tuple id?
387 static int space_can_have_id(__isl_keep isl_space *space,
388 enum isl_dim_type type)
390 if (!space)
391 return 0;
392 if (isl_space_is_params(space))
393 isl_die(space->ctx, isl_error_invalid,
394 "parameter spaces don't have tuple ids", return 0);
395 if (isl_space_is_set(space) && type != isl_dim_set)
396 isl_die(space->ctx, isl_error_invalid,
397 "set spaces can only have a set id", return 0);
398 if (type != isl_dim_in && type != isl_dim_out)
399 isl_die(space->ctx, isl_error_invalid,
400 "only input, output and set tuples can have ids",
401 return 0);
403 return 1;
406 /* Does the tuple have an id?
408 isl_bool isl_space_has_tuple_id(__isl_keep isl_space *dim,
409 enum isl_dim_type type)
411 if (!space_can_have_id(dim, type))
412 return isl_bool_error;
413 return dim->tuple_id[type - isl_dim_in] != NULL;
416 __isl_give isl_id *isl_space_get_tuple_id(__isl_keep isl_space *dim,
417 enum isl_dim_type type)
419 int has_id;
421 if (!dim)
422 return NULL;
423 has_id = isl_space_has_tuple_id(dim, type);
424 if (has_id < 0)
425 return NULL;
426 if (!has_id)
427 isl_die(dim->ctx, isl_error_invalid,
428 "tuple has no id", return NULL);
429 return isl_id_copy(dim->tuple_id[type - isl_dim_in]);
432 __isl_give isl_space *isl_space_set_tuple_id(__isl_take isl_space *dim,
433 enum isl_dim_type type, __isl_take isl_id *id)
435 dim = isl_space_cow(dim);
436 if (!dim || !id)
437 goto error;
438 if (type != isl_dim_in && type != isl_dim_out)
439 isl_die(dim->ctx, isl_error_invalid,
440 "only input, output and set tuples can have names",
441 goto error);
443 isl_id_free(dim->tuple_id[type - isl_dim_in]);
444 dim->tuple_id[type - isl_dim_in] = id;
446 return dim;
447 error:
448 isl_id_free(id);
449 isl_space_free(dim);
450 return NULL;
453 __isl_give isl_space *isl_space_reset_tuple_id(__isl_take isl_space *dim,
454 enum isl_dim_type type)
456 dim = isl_space_cow(dim);
457 if (!dim)
458 return NULL;
459 if (type != isl_dim_in && type != isl_dim_out)
460 isl_die(dim->ctx, isl_error_invalid,
461 "only input, output and set tuples can have names",
462 goto error);
464 isl_id_free(dim->tuple_id[type - isl_dim_in]);
465 dim->tuple_id[type - isl_dim_in] = NULL;
467 return dim;
468 error:
469 isl_space_free(dim);
470 return NULL;
473 /* Set the id of the given dimension of "space" to "id".
474 * If the dimension already has an id, then it is replaced.
475 * If the dimension is a parameter, then we need to change it
476 * in the nested spaces (if any) as well.
478 __isl_give isl_space *isl_space_set_dim_id(__isl_take isl_space *space,
479 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
481 space = isl_space_cow(space);
482 if (!space || !id)
483 goto error;
485 if (type == isl_dim_param) {
486 int i;
488 for (i = 0; i < 2; ++i) {
489 if (!space->nested[i])
490 continue;
491 space->nested[i] =
492 isl_space_set_dim_id(space->nested[i],
493 type, pos, isl_id_copy(id));
494 if (!space->nested[i])
495 goto error;
499 isl_id_free(get_id(space, type, pos));
500 return set_id(space, type, pos, id);
501 error:
502 isl_id_free(id);
503 isl_space_free(space);
504 return NULL;
507 /* Reset the id of the given dimension of "space".
508 * If the dimension already has an id, then it is removed.
509 * If the dimension is a parameter, then we need to reset it
510 * in the nested spaces (if any) as well.
512 __isl_give isl_space *isl_space_reset_dim_id(__isl_take isl_space *space,
513 enum isl_dim_type type, unsigned pos)
515 space = isl_space_cow(space);
516 if (!space)
517 goto error;
519 if (type == isl_dim_param) {
520 int i;
522 for (i = 0; i < 2; ++i) {
523 if (!space->nested[i])
524 continue;
525 space->nested[i] =
526 isl_space_reset_dim_id(space->nested[i],
527 type, pos);
528 if (!space->nested[i])
529 goto error;
533 isl_id_free(get_id(space, type, pos));
534 return set_id(space, type, pos, NULL);
535 error:
536 isl_space_free(space);
537 return NULL;
540 isl_bool isl_space_has_dim_id(__isl_keep isl_space *dim,
541 enum isl_dim_type type, unsigned pos)
543 if (!dim)
544 return isl_bool_error;
545 return get_id(dim, type, pos) != NULL;
548 __isl_give isl_id *isl_space_get_dim_id(__isl_keep isl_space *dim,
549 enum isl_dim_type type, unsigned pos)
551 if (!dim)
552 return NULL;
553 if (!get_id(dim, type, pos))
554 isl_die(dim->ctx, isl_error_invalid,
555 "dim has no id", return NULL);
556 return isl_id_copy(get_id(dim, type, pos));
559 __isl_give isl_space *isl_space_set_tuple_name(__isl_take isl_space *dim,
560 enum isl_dim_type type, const char *s)
562 isl_id *id;
564 if (!dim)
565 return NULL;
567 if (!s)
568 return isl_space_reset_tuple_id(dim, type);
570 if (!name_ok(dim->ctx, s))
571 goto error;
573 id = isl_id_alloc(dim->ctx, s, NULL);
574 return isl_space_set_tuple_id(dim, type, id);
575 error:
576 isl_space_free(dim);
577 return NULL;
580 /* Does the tuple have a name?
582 isl_bool isl_space_has_tuple_name(__isl_keep isl_space *space,
583 enum isl_dim_type type)
585 isl_id *id;
587 if (!space_can_have_id(space, type))
588 return isl_bool_error;
589 id = space->tuple_id[type - isl_dim_in];
590 return id && id->name;
593 __isl_keep const char *isl_space_get_tuple_name(__isl_keep isl_space *dim,
594 enum isl_dim_type type)
596 isl_id *id;
597 if (!dim)
598 return NULL;
599 if (type != isl_dim_in && type != isl_dim_out)
600 return NULL;
601 id = dim->tuple_id[type - isl_dim_in];
602 return id ? id->name : NULL;
605 __isl_give isl_space *isl_space_set_dim_name(__isl_take isl_space *dim,
606 enum isl_dim_type type, unsigned pos,
607 const char *s)
609 isl_id *id;
611 if (!dim)
612 return NULL;
613 if (!s)
614 return isl_space_reset_dim_id(dim, type, pos);
615 if (!name_ok(dim->ctx, s))
616 goto error;
617 id = isl_id_alloc(dim->ctx, s, NULL);
618 return isl_space_set_dim_id(dim, type, pos, id);
619 error:
620 isl_space_free(dim);
621 return NULL;
624 /* Does the given dimension have a name?
626 isl_bool isl_space_has_dim_name(__isl_keep isl_space *space,
627 enum isl_dim_type type, unsigned pos)
629 isl_id *id;
631 if (!space)
632 return isl_bool_error;
633 id = get_id(space, type, pos);
634 return id && id->name;
637 __isl_keep const char *isl_space_get_dim_name(__isl_keep isl_space *dim,
638 enum isl_dim_type type, unsigned pos)
640 isl_id *id = get_id(dim, type, pos);
641 return id ? id->name : NULL;
644 int isl_space_find_dim_by_id(__isl_keep isl_space *dim, enum isl_dim_type type,
645 __isl_keep isl_id *id)
647 int i;
648 int offset;
649 int n;
651 if (!dim || !id)
652 return -1;
654 offset = isl_space_offset(dim, type);
655 n = isl_space_dim(dim, type);
656 for (i = 0; i < n && offset + i < dim->n_id; ++i)
657 if (dim->ids[offset + i] == id)
658 return i;
660 return -1;
663 int isl_space_find_dim_by_name(__isl_keep isl_space *space,
664 enum isl_dim_type type, const char *name)
666 int i;
667 int offset;
668 int n;
670 if (!space || !name)
671 return -1;
673 offset = isl_space_offset(space, type);
674 n = isl_space_dim(space, type);
675 for (i = 0; i < n && offset + i < space->n_id; ++i) {
676 isl_id *id = get_id(space, type, i);
677 if (id && id->name && !strcmp(id->name, name))
678 return i;
681 return -1;
684 /* Reset the user pointer on all identifiers of parameters and tuples
685 * of "space".
687 __isl_give isl_space *isl_space_reset_user(__isl_take isl_space *space)
689 int i;
690 isl_ctx *ctx;
691 isl_id *id;
692 const char *name;
694 if (!space)
695 return NULL;
697 ctx = isl_space_get_ctx(space);
699 for (i = 0; i < space->nparam && i < space->n_id; ++i) {
700 if (!isl_id_get_user(space->ids[i]))
701 continue;
702 space = isl_space_cow(space);
703 if (!space)
704 return NULL;
705 name = isl_id_get_name(space->ids[i]);
706 id = isl_id_alloc(ctx, name, NULL);
707 isl_id_free(space->ids[i]);
708 space->ids[i] = id;
709 if (!id)
710 return isl_space_free(space);
713 for (i = 0; i < 2; ++i) {
714 if (!space->tuple_id[i])
715 continue;
716 if (!isl_id_get_user(space->tuple_id[i]))
717 continue;
718 space = isl_space_cow(space);
719 if (!space)
720 return NULL;
721 name = isl_id_get_name(space->tuple_id[i]);
722 id = isl_id_alloc(ctx, name, NULL);
723 isl_id_free(space->tuple_id[i]);
724 space->tuple_id[i] = id;
725 if (!id)
726 return isl_space_free(space);
729 for (i = 0; i < 2; ++i) {
730 if (!space->nested[i])
731 continue;
732 space = isl_space_cow(space);
733 if (!space)
734 return NULL;
735 space->nested[i] = isl_space_reset_user(space->nested[i]);
736 if (!space->nested[i])
737 return isl_space_free(space);
740 return space;
743 static __isl_keep isl_id *tuple_id(__isl_keep isl_space *dim,
744 enum isl_dim_type type)
746 if (!dim)
747 return NULL;
748 if (type == isl_dim_in)
749 return dim->tuple_id[0];
750 if (type == isl_dim_out)
751 return dim->tuple_id[1];
752 return NULL;
755 static __isl_keep isl_space *nested(__isl_keep isl_space *dim,
756 enum isl_dim_type type)
758 if (!dim)
759 return NULL;
760 if (type == isl_dim_in)
761 return dim->nested[0];
762 if (type == isl_dim_out)
763 return dim->nested[1];
764 return NULL;
767 /* Are the two spaces the same, apart from positions and names of parameters?
769 isl_bool isl_space_has_equal_tuples(__isl_keep isl_space *space1,
770 __isl_keep isl_space *space2)
772 if (!space1 || !space2)
773 return isl_bool_error;
774 if (space1 == space2)
775 return isl_bool_true;
776 return isl_space_tuple_is_equal(space1, isl_dim_in,
777 space2, isl_dim_in) &&
778 isl_space_tuple_is_equal(space1, isl_dim_out,
779 space2, isl_dim_out);
782 /* Check if the tuple of type "type1" of "space1" is the same as
783 * the tuple of type "type2" of "space2".
785 * That is, check if the tuples have the same identifier, the same dimension
786 * and the same internal structure.
787 * The identifiers of the dimensions inside the tuples do not affect the result.
789 * Note that this function only checks the tuples themselves.
790 * If nested tuples are involved, then we need to be careful not
791 * to have result affected by possibly differing parameters
792 * in those nested tuples.
794 isl_bool isl_space_tuple_is_equal(__isl_keep isl_space *space1,
795 enum isl_dim_type type1, __isl_keep isl_space *space2,
796 enum isl_dim_type type2)
798 isl_id *id1, *id2;
799 isl_space *nested1, *nested2;
801 if (!space1 || !space2)
802 return isl_bool_error;
804 if (space1 == space2 && type1 == type2)
805 return isl_bool_true;
807 if (n(space1, type1) != n(space2, type2))
808 return isl_bool_false;
809 id1 = tuple_id(space1, type1);
810 id2 = tuple_id(space2, type2);
811 if (!id1 ^ !id2)
812 return isl_bool_false;
813 if (id1 && id1 != id2)
814 return isl_bool_false;
815 nested1 = nested(space1, type1);
816 nested2 = nested(space2, type2);
817 if (!nested1 ^ !nested2)
818 return isl_bool_false;
819 if (nested1 && !isl_space_has_equal_tuples(nested1, nested2))
820 return isl_bool_false;
821 return isl_bool_true;
824 /* This is the old, undocumented, name for isl_space_tuple_is_equal.
825 * It will be removed at some point.
827 int isl_space_tuple_match(__isl_keep isl_space *space1, enum isl_dim_type type1,
828 __isl_keep isl_space *space2, enum isl_dim_type type2)
830 return isl_space_tuple_is_equal(space1, type1, space2, type2);
833 static isl_bool match(__isl_keep isl_space *space1, enum isl_dim_type type1,
834 __isl_keep isl_space *space2, enum isl_dim_type type2)
836 int i;
838 if (space1 == space2 && type1 == type2)
839 return isl_bool_true;
841 if (!isl_space_tuple_is_equal(space1, type1, space2, type2))
842 return isl_bool_false;
844 if (!space1->ids && !space2->ids)
845 return isl_bool_true;
847 for (i = 0; i < n(space1, type1); ++i) {
848 if (get_id(space1, type1, i) != get_id(space2, type2, i))
849 return isl_bool_false;
851 return isl_bool_true;
854 /* Do "space1" and "space2" have the same parameters?
856 isl_bool isl_space_has_equal_params(__isl_keep isl_space *space1,
857 __isl_keep isl_space *space2)
859 if (!space1 || !space2)
860 return isl_bool_error;
862 return match(space1, isl_dim_param, space2, isl_dim_param);
865 /* Do "space1" and "space2" have the same identifiers for all
866 * the tuple variables?
868 isl_bool isl_space_has_equal_ids(__isl_keep isl_space *space1,
869 __isl_keep isl_space *space2)
871 isl_bool equal;
873 if (!space1 || !space2)
874 return isl_bool_error;
876 equal = match(space1, isl_dim_in, space2, isl_dim_in);
877 if (equal < 0 || !equal)
878 return equal;
879 return match(space1, isl_dim_out, space2, isl_dim_out);
882 isl_bool isl_space_match(__isl_keep isl_space *space1, enum isl_dim_type type1,
883 __isl_keep isl_space *space2, enum isl_dim_type type2)
885 if (!space1 || !space2)
886 return isl_bool_error;
888 return match(space1, type1, space2, type2);
891 static void get_ids(__isl_keep isl_space *dim, enum isl_dim_type type,
892 unsigned first, unsigned n, __isl_keep isl_id **ids)
894 int i;
896 for (i = 0; i < n ; ++i)
897 ids[i] = get_id(dim, type, first + i);
900 static __isl_give isl_space *space_extend(__isl_take isl_space *space,
901 unsigned nparam, unsigned n_in, unsigned n_out)
903 isl_id **ids = NULL;
905 if (!space)
906 return NULL;
907 if (space->nparam == nparam &&
908 space->n_in == n_in && space->n_out == n_out)
909 return space;
911 isl_assert(space->ctx, space->nparam <= nparam, goto error);
912 isl_assert(space->ctx, space->n_in <= n_in, goto error);
913 isl_assert(space->ctx, space->n_out <= n_out, goto error);
915 space = isl_space_cow(space);
916 if (!space)
917 goto error;
919 if (space->ids) {
920 unsigned n;
921 n = nparam + n_in + n_out;
922 if (n < nparam || n < n_in || n < n_out)
923 isl_die(isl_space_get_ctx(space), isl_error_invalid,
924 "overflow in total number of dimensions",
925 goto error);
926 ids = isl_calloc_array(space->ctx, isl_id *, n);
927 if (!ids)
928 goto error;
929 get_ids(space, isl_dim_param, 0, space->nparam, ids);
930 get_ids(space, isl_dim_in, 0, space->n_in, ids + nparam);
931 get_ids(space, isl_dim_out, 0, space->n_out,
932 ids + nparam + n_in);
933 free(space->ids);
934 space->ids = ids;
935 space->n_id = nparam + n_in + n_out;
937 space->nparam = nparam;
938 space->n_in = n_in;
939 space->n_out = n_out;
941 return space;
942 error:
943 free(ids);
944 isl_space_free(space);
945 return NULL;
948 __isl_give isl_space *isl_space_extend(__isl_take isl_space *space,
949 unsigned nparam, unsigned n_in, unsigned n_out)
951 return space_extend(space, nparam, n_in, n_out);
954 __isl_give isl_space *isl_space_add_dims(__isl_take isl_space *space,
955 enum isl_dim_type type, unsigned n)
957 space = isl_space_reset(space, type);
958 if (!space)
959 return NULL;
960 switch (type) {
961 case isl_dim_param:
962 space = space_extend(space,
963 space->nparam + n, space->n_in, space->n_out);
964 if (space && space->nested[0] &&
965 !(space->nested[0] = isl_space_add_dims(space->nested[0],
966 isl_dim_param, n)))
967 goto error;
968 if (space && space->nested[1] &&
969 !(space->nested[1] = isl_space_add_dims(space->nested[1],
970 isl_dim_param, n)))
971 goto error;
972 return space;
973 case isl_dim_in:
974 return space_extend(space,
975 space->nparam, space->n_in + n, space->n_out);
976 case isl_dim_out:
977 return space_extend(space,
978 space->nparam, space->n_in, space->n_out + n);
979 default:
980 isl_die(space->ctx, isl_error_invalid,
981 "cannot add dimensions of specified type", goto error);
983 error:
984 isl_space_free(space);
985 return NULL;
988 static int valid_dim_type(enum isl_dim_type type)
990 switch (type) {
991 case isl_dim_param:
992 case isl_dim_in:
993 case isl_dim_out:
994 return 1;
995 default:
996 return 0;
1000 /* Insert "n" dimensions of type "type" at position "pos".
1001 * If we are inserting parameters, then they are also inserted in
1002 * any nested spaces.
1004 __isl_give isl_space *isl_space_insert_dims(__isl_take isl_space *dim,
1005 enum isl_dim_type type, unsigned pos, unsigned n)
1007 isl_id **ids = NULL;
1009 if (!dim)
1010 return NULL;
1011 if (n == 0)
1012 return isl_space_reset(dim, type);
1014 if (!valid_dim_type(type))
1015 isl_die(dim->ctx, isl_error_invalid,
1016 "cannot insert dimensions of specified type",
1017 goto error);
1019 isl_assert(dim->ctx, pos <= isl_space_dim(dim, type), goto error);
1021 dim = isl_space_cow(dim);
1022 if (!dim)
1023 return NULL;
1025 if (dim->ids) {
1026 enum isl_dim_type t, o = isl_dim_param;
1027 int off;
1028 int s[3];
1029 ids = isl_calloc_array(dim->ctx, isl_id *,
1030 dim->nparam + dim->n_in + dim->n_out + n);
1031 if (!ids)
1032 goto error;
1033 off = 0;
1034 s[isl_dim_param - o] = dim->nparam;
1035 s[isl_dim_in - o] = dim->n_in;
1036 s[isl_dim_out - o] = dim->n_out;
1037 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
1038 if (t != type) {
1039 get_ids(dim, t, 0, s[t - o], ids + off);
1040 off += s[t - o];
1041 } else {
1042 get_ids(dim, t, 0, pos, ids + off);
1043 off += pos + n;
1044 get_ids(dim, t, pos, s[t - o] - pos, ids + off);
1045 off += s[t - o] - pos;
1048 free(dim->ids);
1049 dim->ids = ids;
1050 dim->n_id = dim->nparam + dim->n_in + dim->n_out + n;
1052 switch (type) {
1053 case isl_dim_param: dim->nparam += n; break;
1054 case isl_dim_in: dim->n_in += n; break;
1055 case isl_dim_out: dim->n_out += n; break;
1056 default: ;
1058 dim = isl_space_reset(dim, type);
1060 if (type == isl_dim_param) {
1061 if (dim && dim->nested[0] &&
1062 !(dim->nested[0] = isl_space_insert_dims(dim->nested[0],
1063 isl_dim_param, pos, n)))
1064 goto error;
1065 if (dim && dim->nested[1] &&
1066 !(dim->nested[1] = isl_space_insert_dims(dim->nested[1],
1067 isl_dim_param, pos, n)))
1068 goto error;
1071 return dim;
1072 error:
1073 isl_space_free(dim);
1074 return NULL;
1077 __isl_give isl_space *isl_space_move_dims(__isl_take isl_space *space,
1078 enum isl_dim_type dst_type, unsigned dst_pos,
1079 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
1081 int i;
1083 space = isl_space_reset(space, src_type);
1084 space = isl_space_reset(space, dst_type);
1085 if (!space)
1086 return NULL;
1087 if (n == 0)
1088 return space;
1090 isl_assert(space->ctx, src_pos + n <= isl_space_dim(space, src_type),
1091 goto error);
1093 if (dst_type == src_type && dst_pos == src_pos)
1094 return space;
1096 isl_assert(space->ctx, dst_type != src_type, goto error);
1098 space = isl_space_cow(space);
1099 if (!space)
1100 return NULL;
1102 if (space->ids) {
1103 isl_id **ids;
1104 enum isl_dim_type t, o = isl_dim_param;
1105 int off;
1106 int s[3];
1107 ids = isl_calloc_array(space->ctx, isl_id *,
1108 space->nparam + space->n_in + space->n_out);
1109 if (!ids)
1110 goto error;
1111 off = 0;
1112 s[isl_dim_param - o] = space->nparam;
1113 s[isl_dim_in - o] = space->n_in;
1114 s[isl_dim_out - o] = space->n_out;
1115 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
1116 if (t == dst_type) {
1117 get_ids(space, t, 0, dst_pos, ids + off);
1118 off += dst_pos;
1119 get_ids(space, src_type, src_pos, n, ids + off);
1120 off += n;
1121 get_ids(space, t, dst_pos, s[t - o] - dst_pos,
1122 ids + off);
1123 off += s[t - o] - dst_pos;
1124 } else if (t == src_type) {
1125 get_ids(space, t, 0, src_pos, ids + off);
1126 off += src_pos;
1127 get_ids(space, t, src_pos + n,
1128 s[t - o] - src_pos - n, ids + off);
1129 off += s[t - o] - src_pos - n;
1130 } else {
1131 get_ids(space, t, 0, s[t - o], ids + off);
1132 off += s[t - o];
1135 free(space->ids);
1136 space->ids = ids;
1137 space->n_id = space->nparam + space->n_in + space->n_out;
1140 switch (dst_type) {
1141 case isl_dim_param: space->nparam += n; break;
1142 case isl_dim_in: space->n_in += n; break;
1143 case isl_dim_out: space->n_out += n; break;
1144 default: ;
1147 switch (src_type) {
1148 case isl_dim_param: space->nparam -= n; break;
1149 case isl_dim_in: space->n_in -= n; break;
1150 case isl_dim_out: space->n_out -= n; break;
1151 default: ;
1154 if (dst_type != isl_dim_param && src_type != isl_dim_param)
1155 return space;
1157 for (i = 0; i < 2; ++i) {
1158 if (!space->nested[i])
1159 continue;
1160 space->nested[i] = isl_space_replace(space->nested[i],
1161 isl_dim_param, space);
1162 if (!space->nested[i])
1163 goto error;
1166 return space;
1167 error:
1168 isl_space_free(space);
1169 return NULL;
1172 /* Check that "space1" and "space2" have the same parameters,
1173 * reporting an error if they do not.
1175 isl_stat isl_space_check_equal_params(__isl_keep isl_space *space1,
1176 __isl_keep isl_space *space2)
1178 isl_bool equal;
1180 equal = isl_space_has_equal_params(space1, space2);
1181 if (equal < 0)
1182 return isl_stat_error;
1183 if (!equal)
1184 isl_die(isl_space_get_ctx(space1), isl_error_invalid,
1185 "parameters need to match", return isl_stat_error);
1186 return isl_stat_ok;
1189 __isl_give isl_space *isl_space_join(__isl_take isl_space *left,
1190 __isl_take isl_space *right)
1192 isl_space *dim;
1194 if (isl_space_check_equal_params(left, right) < 0)
1195 goto error;
1197 isl_assert(left->ctx,
1198 isl_space_tuple_is_equal(left, isl_dim_out, right, isl_dim_in),
1199 goto error);
1201 dim = isl_space_alloc(left->ctx, left->nparam, left->n_in, right->n_out);
1202 if (!dim)
1203 goto error;
1205 dim = copy_ids(dim, isl_dim_param, 0, left, isl_dim_param);
1206 dim = copy_ids(dim, isl_dim_in, 0, left, isl_dim_in);
1207 dim = copy_ids(dim, isl_dim_out, 0, right, isl_dim_out);
1209 if (dim && left->tuple_id[0] &&
1210 !(dim->tuple_id[0] = isl_id_copy(left->tuple_id[0])))
1211 goto error;
1212 if (dim && right->tuple_id[1] &&
1213 !(dim->tuple_id[1] = isl_id_copy(right->tuple_id[1])))
1214 goto error;
1215 if (dim && left->nested[0] &&
1216 !(dim->nested[0] = isl_space_copy(left->nested[0])))
1217 goto error;
1218 if (dim && right->nested[1] &&
1219 !(dim->nested[1] = isl_space_copy(right->nested[1])))
1220 goto error;
1222 isl_space_free(left);
1223 isl_space_free(right);
1225 return dim;
1226 error:
1227 isl_space_free(left);
1228 isl_space_free(right);
1229 return NULL;
1232 /* Given two map spaces { A -> C } and { B -> D }, construct the space
1233 * { [A -> B] -> [C -> D] }.
1234 * Given two set spaces { A } and { B }, construct the space { [A -> B] }.
1236 __isl_give isl_space *isl_space_product(__isl_take isl_space *left,
1237 __isl_take isl_space *right)
1239 isl_space *dom1, *dom2, *nest1, *nest2;
1240 int is_set;
1242 if (!left || !right)
1243 goto error;
1245 is_set = isl_space_is_set(left);
1246 if (is_set != isl_space_is_set(right))
1247 isl_die(isl_space_get_ctx(left), isl_error_invalid,
1248 "expecting either two set spaces or two map spaces",
1249 goto error);
1250 if (is_set)
1251 return isl_space_range_product(left, right);
1253 if (isl_space_check_equal_params(left, right) < 0)
1254 goto error;
1256 dom1 = isl_space_domain(isl_space_copy(left));
1257 dom2 = isl_space_domain(isl_space_copy(right));
1258 nest1 = isl_space_wrap(isl_space_join(isl_space_reverse(dom1), dom2));
1260 dom1 = isl_space_range(left);
1261 dom2 = isl_space_range(right);
1262 nest2 = isl_space_wrap(isl_space_join(isl_space_reverse(dom1), dom2));
1264 return isl_space_join(isl_space_reverse(nest1), nest2);
1265 error:
1266 isl_space_free(left);
1267 isl_space_free(right);
1268 return NULL;
1271 /* Given two spaces { A -> C } and { B -> C }, construct the space
1272 * { [A -> B] -> C }
1274 __isl_give isl_space *isl_space_domain_product(__isl_take isl_space *left,
1275 __isl_take isl_space *right)
1277 isl_space *ran, *dom1, *dom2, *nest;
1279 if (isl_space_check_equal_params(left, right) < 0)
1280 goto error;
1282 if (!isl_space_tuple_is_equal(left, isl_dim_out, right, isl_dim_out))
1283 isl_die(left->ctx, isl_error_invalid,
1284 "ranges need to match", goto error);
1286 ran = isl_space_range(isl_space_copy(left));
1288 dom1 = isl_space_domain(left);
1289 dom2 = isl_space_domain(right);
1290 nest = isl_space_wrap(isl_space_join(isl_space_reverse(dom1), dom2));
1292 return isl_space_join(isl_space_reverse(nest), ran);
1293 error:
1294 isl_space_free(left);
1295 isl_space_free(right);
1296 return NULL;
1299 __isl_give isl_space *isl_space_range_product(__isl_take isl_space *left,
1300 __isl_take isl_space *right)
1302 isl_space *dom, *ran1, *ran2, *nest;
1304 if (isl_space_check_equal_params(left, right) < 0)
1305 goto error;
1307 if (!isl_space_tuple_is_equal(left, isl_dim_in, right, isl_dim_in))
1308 isl_die(left->ctx, isl_error_invalid,
1309 "domains need to match", goto error);
1311 dom = isl_space_domain(isl_space_copy(left));
1313 ran1 = isl_space_range(left);
1314 ran2 = isl_space_range(right);
1315 nest = isl_space_wrap(isl_space_join(isl_space_reverse(ran1), ran2));
1317 return isl_space_join(isl_space_reverse(dom), nest);
1318 error:
1319 isl_space_free(left);
1320 isl_space_free(right);
1321 return NULL;
1324 /* Given a space of the form [A -> B] -> C, return the space A -> C.
1326 __isl_give isl_space *isl_space_domain_factor_domain(
1327 __isl_take isl_space *space)
1329 isl_space *nested;
1330 isl_space *domain;
1332 if (!space)
1333 return NULL;
1334 if (!isl_space_domain_is_wrapping(space))
1335 isl_die(isl_space_get_ctx(space), isl_error_invalid,
1336 "domain not a product", return isl_space_free(space));
1338 nested = space->nested[0];
1339 domain = isl_space_copy(space);
1340 domain = isl_space_drop_dims(domain, isl_dim_in,
1341 nested->n_in, nested->n_out);
1342 if (!domain)
1343 return isl_space_free(space);
1344 if (nested->tuple_id[0]) {
1345 domain->tuple_id[0] = isl_id_copy(nested->tuple_id[0]);
1346 if (!domain->tuple_id[0])
1347 goto error;
1349 if (nested->nested[0]) {
1350 domain->nested[0] = isl_space_copy(nested->nested[0]);
1351 if (!domain->nested[0])
1352 goto error;
1355 isl_space_free(space);
1356 return domain;
1357 error:
1358 isl_space_free(space);
1359 isl_space_free(domain);
1360 return NULL;
1363 /* Given a space of the form [A -> B] -> C, return the space B -> C.
1365 __isl_give isl_space *isl_space_domain_factor_range(
1366 __isl_take isl_space *space)
1368 isl_space *nested;
1369 isl_space *range;
1371 if (!space)
1372 return NULL;
1373 if (!isl_space_domain_is_wrapping(space))
1374 isl_die(isl_space_get_ctx(space), isl_error_invalid,
1375 "domain not a product", return isl_space_free(space));
1377 nested = space->nested[0];
1378 range = isl_space_copy(space);
1379 range = isl_space_drop_dims(range, isl_dim_in, 0, nested->n_in);
1380 if (!range)
1381 return isl_space_free(space);
1382 if (nested->tuple_id[1]) {
1383 range->tuple_id[0] = isl_id_copy(nested->tuple_id[1]);
1384 if (!range->tuple_id[0])
1385 goto error;
1387 if (nested->nested[1]) {
1388 range->nested[0] = isl_space_copy(nested->nested[1]);
1389 if (!range->nested[0])
1390 goto error;
1393 isl_space_free(space);
1394 return range;
1395 error:
1396 isl_space_free(space);
1397 isl_space_free(range);
1398 return NULL;
1401 /* Internal function that selects the domain of the map that is
1402 * embedded in either a set space or the range of a map space.
1403 * In particular, given a space of the form [A -> B], return the space A.
1404 * Given a space of the form A -> [B -> C], return the space A -> B.
1406 static __isl_give isl_space *range_factor_domain(__isl_take isl_space *space)
1408 isl_space *nested;
1409 isl_space *domain;
1411 if (!space)
1412 return NULL;
1414 nested = space->nested[1];
1415 domain = isl_space_copy(space);
1416 domain = isl_space_drop_dims(domain, isl_dim_out,
1417 nested->n_in, nested->n_out);
1418 if (!domain)
1419 return isl_space_free(space);
1420 if (nested->tuple_id[0]) {
1421 domain->tuple_id[1] = isl_id_copy(nested->tuple_id[0]);
1422 if (!domain->tuple_id[1])
1423 goto error;
1425 if (nested->nested[0]) {
1426 domain->nested[1] = isl_space_copy(nested->nested[0]);
1427 if (!domain->nested[1])
1428 goto error;
1431 isl_space_free(space);
1432 return domain;
1433 error:
1434 isl_space_free(space);
1435 isl_space_free(domain);
1436 return NULL;
1439 /* Given a space of the form A -> [B -> C], return the space A -> B.
1441 __isl_give isl_space *isl_space_range_factor_domain(
1442 __isl_take isl_space *space)
1444 if (!space)
1445 return NULL;
1446 if (!isl_space_range_is_wrapping(space))
1447 isl_die(isl_space_get_ctx(space), isl_error_invalid,
1448 "range not a product", return isl_space_free(space));
1450 return range_factor_domain(space);
1453 /* Given a space of the form [A -> B], return the space A.
1455 static __isl_give isl_space *set_factor_domain(__isl_take isl_space *space)
1457 if (!space)
1458 return NULL;
1459 if (!isl_space_is_wrapping(space))
1460 isl_die(isl_space_get_ctx(space), isl_error_invalid,
1461 "not a product", return isl_space_free(space));
1463 return range_factor_domain(space);
1466 /* Given a space of the form [A -> B] -> [C -> D], return the space A -> C.
1467 * Given a space of the form [A -> B], return the space A.
1469 __isl_give isl_space *isl_space_factor_domain(__isl_take isl_space *space)
1471 if (!space)
1472 return NULL;
1473 if (isl_space_is_set(space))
1474 return set_factor_domain(space);
1475 space = isl_space_domain_factor_domain(space);
1476 space = isl_space_range_factor_domain(space);
1477 return space;
1480 /* Internal function that selects the range of the map that is
1481 * embedded in either a set space or the range of a map space.
1482 * In particular, given a space of the form [A -> B], return the space B.
1483 * Given a space of the form A -> [B -> C], return the space A -> C.
1485 static __isl_give isl_space *range_factor_range(__isl_take isl_space *space)
1487 isl_space *nested;
1488 isl_space *range;
1490 if (!space)
1491 return NULL;
1493 nested = space->nested[1];
1494 range = isl_space_copy(space);
1495 range = isl_space_drop_dims(range, isl_dim_out, 0, nested->n_in);
1496 if (!range)
1497 return isl_space_free(space);
1498 if (nested->tuple_id[1]) {
1499 range->tuple_id[1] = isl_id_copy(nested->tuple_id[1]);
1500 if (!range->tuple_id[1])
1501 goto error;
1503 if (nested->nested[1]) {
1504 range->nested[1] = isl_space_copy(nested->nested[1]);
1505 if (!range->nested[1])
1506 goto error;
1509 isl_space_free(space);
1510 return range;
1511 error:
1512 isl_space_free(space);
1513 isl_space_free(range);
1514 return NULL;
1517 /* Given a space of the form A -> [B -> C], return the space A -> C.
1519 __isl_give isl_space *isl_space_range_factor_range(
1520 __isl_take isl_space *space)
1522 if (!space)
1523 return NULL;
1524 if (!isl_space_range_is_wrapping(space))
1525 isl_die(isl_space_get_ctx(space), isl_error_invalid,
1526 "range not a product", return isl_space_free(space));
1528 return range_factor_range(space);
1531 /* Given a space of the form [A -> B], return the space B.
1533 static __isl_give isl_space *set_factor_range(__isl_take isl_space *space)
1535 if (!space)
1536 return NULL;
1537 if (!isl_space_is_wrapping(space))
1538 isl_die(isl_space_get_ctx(space), isl_error_invalid,
1539 "not a product", return isl_space_free(space));
1541 return range_factor_range(space);
1544 /* Given a space of the form [A -> B] -> [C -> D], return the space B -> D.
1545 * Given a space of the form [A -> B], return the space B.
1547 __isl_give isl_space *isl_space_factor_range(__isl_take isl_space *space)
1549 if (!space)
1550 return NULL;
1551 if (isl_space_is_set(space))
1552 return set_factor_range(space);
1553 space = isl_space_domain_factor_range(space);
1554 space = isl_space_range_factor_range(space);
1555 return space;
1558 __isl_give isl_space *isl_space_map_from_set(__isl_take isl_space *space)
1560 isl_ctx *ctx;
1561 isl_id **ids = NULL;
1562 int n_id;
1564 if (!space)
1565 return NULL;
1566 ctx = isl_space_get_ctx(space);
1567 if (!isl_space_is_set(space))
1568 isl_die(ctx, isl_error_invalid, "not a set space", goto error);
1569 space = isl_space_cow(space);
1570 if (!space)
1571 return NULL;
1572 n_id = space->nparam + space->n_out + space->n_out;
1573 if (n_id > 0 && space->ids) {
1574 ids = isl_calloc_array(space->ctx, isl_id *, n_id);
1575 if (!ids)
1576 goto error;
1577 get_ids(space, isl_dim_param, 0, space->nparam, ids);
1578 get_ids(space, isl_dim_out, 0, space->n_out,
1579 ids + space->nparam);
1581 space->n_in = space->n_out;
1582 if (ids) {
1583 free(space->ids);
1584 space->ids = ids;
1585 space->n_id = n_id;
1586 space = copy_ids(space, isl_dim_out, 0, space, isl_dim_in);
1588 isl_id_free(space->tuple_id[0]);
1589 space->tuple_id[0] = isl_id_copy(space->tuple_id[1]);
1590 isl_space_free(space->nested[0]);
1591 space->nested[0] = isl_space_copy(space->nested[1]);
1592 return space;
1593 error:
1594 isl_space_free(space);
1595 return NULL;
1598 __isl_give isl_space *isl_space_map_from_domain_and_range(
1599 __isl_take isl_space *domain, __isl_take isl_space *range)
1601 if (!domain || !range)
1602 goto error;
1603 if (!isl_space_is_set(domain))
1604 isl_die(isl_space_get_ctx(domain), isl_error_invalid,
1605 "domain is not a set space", goto error);
1606 if (!isl_space_is_set(range))
1607 isl_die(isl_space_get_ctx(range), isl_error_invalid,
1608 "range is not a set space", goto error);
1609 return isl_space_join(isl_space_reverse(domain), range);
1610 error:
1611 isl_space_free(domain);
1612 isl_space_free(range);
1613 return NULL;
1616 static __isl_give isl_space *set_ids(__isl_take isl_space *dim,
1617 enum isl_dim_type type,
1618 unsigned first, unsigned n, __isl_take isl_id **ids)
1620 int i;
1622 for (i = 0; i < n ; ++i)
1623 dim = set_id(dim, type, first + i, ids[i]);
1625 return dim;
1628 __isl_give isl_space *isl_space_reverse(__isl_take isl_space *dim)
1630 unsigned t;
1631 isl_space *nested;
1632 isl_id **ids = NULL;
1633 isl_id *id;
1635 if (!dim)
1636 return NULL;
1637 if (match(dim, isl_dim_in, dim, isl_dim_out))
1638 return dim;
1640 dim = isl_space_cow(dim);
1641 if (!dim)
1642 return NULL;
1644 id = dim->tuple_id[0];
1645 dim->tuple_id[0] = dim->tuple_id[1];
1646 dim->tuple_id[1] = id;
1648 nested = dim->nested[0];
1649 dim->nested[0] = dim->nested[1];
1650 dim->nested[1] = nested;
1652 if (dim->ids) {
1653 int n_id = dim->n_in + dim->n_out;
1654 ids = isl_alloc_array(dim->ctx, isl_id *, n_id);
1655 if (n_id && !ids)
1656 goto error;
1657 get_ids(dim, isl_dim_in, 0, dim->n_in, ids);
1658 get_ids(dim, isl_dim_out, 0, dim->n_out, ids + dim->n_in);
1661 t = dim->n_in;
1662 dim->n_in = dim->n_out;
1663 dim->n_out = t;
1665 if (dim->ids) {
1666 dim = set_ids(dim, isl_dim_out, 0, dim->n_out, ids);
1667 dim = set_ids(dim, isl_dim_in, 0, dim->n_in, ids + dim->n_out);
1668 free(ids);
1671 return dim;
1672 error:
1673 free(ids);
1674 isl_space_free(dim);
1675 return NULL;
1678 __isl_give isl_space *isl_space_drop_dims(__isl_take isl_space *dim,
1679 enum isl_dim_type type, unsigned first, unsigned num)
1681 int i;
1683 if (!dim)
1684 return NULL;
1686 if (num == 0)
1687 return isl_space_reset(dim, type);
1689 if (!valid_dim_type(type))
1690 isl_die(dim->ctx, isl_error_invalid,
1691 "cannot drop dimensions of specified type", goto error);
1693 if (first + num > n(dim, type) || first + num < first)
1694 isl_die(isl_space_get_ctx(dim), isl_error_invalid,
1695 "index out of bounds", return isl_space_free(dim));
1696 dim = isl_space_cow(dim);
1697 if (!dim)
1698 goto error;
1699 if (dim->ids) {
1700 dim = extend_ids(dim);
1701 if (!dim)
1702 goto error;
1703 for (i = 0; i < num; ++i)
1704 isl_id_free(get_id(dim, type, first + i));
1705 for (i = first+num; i < n(dim, type); ++i)
1706 set_id(dim, type, i - num, get_id(dim, type, i));
1707 switch (type) {
1708 case isl_dim_param:
1709 get_ids(dim, isl_dim_in, 0, dim->n_in,
1710 dim->ids + offset(dim, isl_dim_in) - num);
1711 case isl_dim_in:
1712 get_ids(dim, isl_dim_out, 0, dim->n_out,
1713 dim->ids + offset(dim, isl_dim_out) - num);
1714 default:
1717 dim->n_id -= num;
1719 switch (type) {
1720 case isl_dim_param: dim->nparam -= num; break;
1721 case isl_dim_in: dim->n_in -= num; break;
1722 case isl_dim_out: dim->n_out -= num; break;
1723 default: ;
1725 dim = isl_space_reset(dim, type);
1726 if (type == isl_dim_param) {
1727 if (dim && dim->nested[0] &&
1728 !(dim->nested[0] = isl_space_drop_dims(dim->nested[0],
1729 isl_dim_param, first, num)))
1730 goto error;
1731 if (dim && dim->nested[1] &&
1732 !(dim->nested[1] = isl_space_drop_dims(dim->nested[1],
1733 isl_dim_param, first, num)))
1734 goto error;
1736 return dim;
1737 error:
1738 isl_space_free(dim);
1739 return NULL;
1742 __isl_give isl_space *isl_space_drop_inputs(__isl_take isl_space *dim,
1743 unsigned first, unsigned n)
1745 if (!dim)
1746 return NULL;
1747 return isl_space_drop_dims(dim, isl_dim_in, first, n);
1750 __isl_give isl_space *isl_space_drop_outputs(__isl_take isl_space *dim,
1751 unsigned first, unsigned n)
1753 if (!dim)
1754 return NULL;
1755 return isl_space_drop_dims(dim, isl_dim_out, first, n);
1758 __isl_give isl_space *isl_space_domain(__isl_take isl_space *space)
1760 if (!space)
1761 return NULL;
1762 space = isl_space_drop_dims(space, isl_dim_out, 0, space->n_out);
1763 space = isl_space_reverse(space);
1764 space = mark_as_set(space);
1765 return space;
1768 __isl_give isl_space *isl_space_from_domain(__isl_take isl_space *dim)
1770 if (!dim)
1771 return NULL;
1772 if (!isl_space_is_set(dim))
1773 isl_die(isl_space_get_ctx(dim), isl_error_invalid,
1774 "not a set space", goto error);
1775 dim = isl_space_reverse(dim);
1776 dim = isl_space_reset(dim, isl_dim_out);
1777 return dim;
1778 error:
1779 isl_space_free(dim);
1780 return NULL;
1783 __isl_give isl_space *isl_space_range(__isl_take isl_space *space)
1785 if (!space)
1786 return NULL;
1787 space = isl_space_drop_dims(space, isl_dim_in, 0, space->n_in);
1788 space = mark_as_set(space);
1789 return space;
1792 __isl_give isl_space *isl_space_from_range(__isl_take isl_space *dim)
1794 if (!dim)
1795 return NULL;
1796 if (!isl_space_is_set(dim))
1797 isl_die(isl_space_get_ctx(dim), isl_error_invalid,
1798 "not a set space", goto error);
1799 return isl_space_reset(dim, isl_dim_in);
1800 error:
1801 isl_space_free(dim);
1802 return NULL;
1805 /* Given a map space A -> B, return the map space [A -> B] -> A.
1807 __isl_give isl_space *isl_space_domain_map(__isl_take isl_space *space)
1809 isl_space *domain;
1811 domain = isl_space_from_range(isl_space_domain(isl_space_copy(space)));
1812 space = isl_space_from_domain(isl_space_wrap(space));
1813 space = isl_space_join(space, domain);
1815 return space;
1818 /* Given a map space A -> B, return the map space [A -> B] -> B.
1820 __isl_give isl_space *isl_space_range_map(__isl_take isl_space *space)
1822 isl_space *range;
1824 range = isl_space_from_range(isl_space_range(isl_space_copy(space)));
1825 space = isl_space_from_domain(isl_space_wrap(space));
1826 space = isl_space_join(space, range);
1828 return space;
1831 __isl_give isl_space *isl_space_params(__isl_take isl_space *space)
1833 if (isl_space_is_params(space))
1834 return space;
1835 space = isl_space_drop_dims(space,
1836 isl_dim_in, 0, isl_space_dim(space, isl_dim_in));
1837 space = isl_space_drop_dims(space,
1838 isl_dim_out, 0, isl_space_dim(space, isl_dim_out));
1839 space = mark_as_params(space);
1840 return space;
1843 __isl_give isl_space *isl_space_set_from_params(__isl_take isl_space *space)
1845 if (!space)
1846 return NULL;
1847 if (!isl_space_is_params(space))
1848 isl_die(isl_space_get_ctx(space), isl_error_invalid,
1849 "not a parameter space", goto error);
1850 return isl_space_reset(space, isl_dim_set);
1851 error:
1852 isl_space_free(space);
1853 return NULL;
1856 __isl_give isl_space *isl_space_underlying(__isl_take isl_space *dim,
1857 unsigned n_div)
1859 int i;
1861 if (!dim)
1862 return NULL;
1863 if (n_div == 0 &&
1864 dim->nparam == 0 && dim->n_in == 0 && dim->n_id == 0)
1865 return isl_space_reset(isl_space_reset(dim, isl_dim_in), isl_dim_out);
1866 dim = isl_space_cow(dim);
1867 if (!dim)
1868 return NULL;
1869 dim->n_out += dim->nparam + dim->n_in + n_div;
1870 dim->nparam = 0;
1871 dim->n_in = 0;
1873 for (i = 0; i < dim->n_id; ++i)
1874 isl_id_free(get_id(dim, isl_dim_out, i));
1875 dim->n_id = 0;
1876 dim = isl_space_reset(dim, isl_dim_in);
1877 dim = isl_space_reset(dim, isl_dim_out);
1879 return dim;
1882 /* Are the two spaces the same, including positions and names of parameters?
1884 isl_bool isl_space_is_equal(__isl_keep isl_space *space1,
1885 __isl_keep isl_space *space2)
1887 isl_bool equal;
1889 if (!space1 || !space2)
1890 return isl_bool_error;
1891 if (space1 == space2)
1892 return isl_bool_true;
1893 equal = isl_space_has_equal_params(space1, space2);
1894 if (equal < 0 || !equal)
1895 return equal;
1896 return isl_space_has_equal_tuples(space1, space2);
1899 /* Is space1 equal to the domain of space2?
1901 * In the internal version we also allow space2 to be the space of a set,
1902 * provided space1 is a parameter space.
1904 isl_bool isl_space_is_domain_internal(__isl_keep isl_space *space1,
1905 __isl_keep isl_space *space2)
1907 isl_bool equal_params;
1909 if (!space1 || !space2)
1910 return isl_bool_error;
1911 if (!isl_space_is_set(space1))
1912 return isl_bool_false;
1913 equal_params = isl_space_has_equal_params(space1, space2);
1914 if (equal_params < 0 || !equal_params)
1915 return equal_params;
1916 return isl_space_tuple_is_equal(space1, isl_dim_set,
1917 space2, isl_dim_in);
1920 /* Is space1 equal to the domain of space2?
1922 isl_bool isl_space_is_domain(__isl_keep isl_space *space1,
1923 __isl_keep isl_space *space2)
1925 if (!space2)
1926 return isl_bool_error;
1927 if (!isl_space_is_map(space2))
1928 return isl_bool_false;
1929 return isl_space_is_domain_internal(space1, space2);
1932 /* Is space1 equal to the range of space2?
1934 * In the internal version, space2 is allowed to be the space of a set,
1935 * in which case it should be equal to space1.
1937 isl_bool isl_space_is_range_internal(__isl_keep isl_space *space1,
1938 __isl_keep isl_space *space2)
1940 isl_bool equal_params;
1942 if (!space1 || !space2)
1943 return isl_bool_error;
1944 if (!isl_space_is_set(space1))
1945 return isl_bool_false;
1946 equal_params = isl_space_has_equal_params(space1, space2);
1947 if (equal_params < 0 || !equal_params)
1948 return equal_params;
1949 return isl_space_tuple_is_equal(space1, isl_dim_set,
1950 space2, isl_dim_out);
1953 /* Is space1 equal to the range of space2?
1955 isl_bool isl_space_is_range(__isl_keep isl_space *space1,
1956 __isl_keep isl_space *space2)
1958 if (!space2)
1959 return isl_bool_error;
1960 if (!isl_space_is_map(space2))
1961 return isl_bool_false;
1962 return isl_space_is_range_internal(space1, space2);
1965 /* Update "hash" by hashing in the parameters of "space".
1967 static uint32_t isl_hash_params(uint32_t hash, __isl_keep isl_space *space)
1969 int i;
1970 isl_id *id;
1972 if (!space)
1973 return hash;
1975 isl_hash_byte(hash, space->nparam % 256);
1977 for (i = 0; i < space->nparam; ++i) {
1978 id = get_id(space, isl_dim_param, i);
1979 hash = isl_hash_id(hash, id);
1982 return hash;
1985 /* Update "hash" by hashing in the tuples of "space".
1986 * Changes in this function should be reflected in isl_hash_tuples_domain.
1988 static uint32_t isl_hash_tuples(uint32_t hash, __isl_keep isl_space *space)
1990 isl_id *id;
1992 if (!space)
1993 return hash;
1995 isl_hash_byte(hash, space->n_in % 256);
1996 isl_hash_byte(hash, space->n_out % 256);
1998 id = tuple_id(space, isl_dim_in);
1999 hash = isl_hash_id(hash, id);
2000 id = tuple_id(space, isl_dim_out);
2001 hash = isl_hash_id(hash, id);
2003 hash = isl_hash_tuples(hash, space->nested[0]);
2004 hash = isl_hash_tuples(hash, space->nested[1]);
2006 return hash;
2009 /* Update "hash" by hashing in the domain tuple of "space".
2010 * The result of this function is equal to the result of applying
2011 * isl_hash_tuples to the domain of "space".
2013 static uint32_t isl_hash_tuples_domain(uint32_t hash,
2014 __isl_keep isl_space *space)
2016 isl_id *id;
2018 if (!space)
2019 return hash;
2021 isl_hash_byte(hash, 0);
2022 isl_hash_byte(hash, space->n_in % 256);
2024 hash = isl_hash_id(hash, &isl_id_none);
2025 id = tuple_id(space, isl_dim_in);
2026 hash = isl_hash_id(hash, id);
2028 hash = isl_hash_tuples(hash, space->nested[0]);
2030 return hash;
2033 /* Return a hash value that digests the tuples of "space",
2034 * i.e., that ignores the parameters.
2036 uint32_t isl_space_get_tuple_hash(__isl_keep isl_space *space)
2038 uint32_t hash;
2040 if (!space)
2041 return 0;
2043 hash = isl_hash_init();
2044 hash = isl_hash_tuples(hash, space);
2046 return hash;
2049 uint32_t isl_space_get_hash(__isl_keep isl_space *space)
2051 uint32_t hash;
2053 if (!space)
2054 return 0;
2056 hash = isl_hash_init();
2057 hash = isl_hash_params(hash, space);
2058 hash = isl_hash_tuples(hash, space);
2060 return hash;
2063 /* Return the hash value of the domain of "space".
2064 * That is, isl_space_get_domain_hash(space) is equal to
2065 * isl_space_get_hash(isl_space_domain(space)).
2067 uint32_t isl_space_get_domain_hash(__isl_keep isl_space *space)
2069 uint32_t hash;
2071 if (!space)
2072 return 0;
2074 hash = isl_hash_init();
2075 hash = isl_hash_params(hash, space);
2076 hash = isl_hash_tuples_domain(hash, space);
2078 return hash;
2081 isl_bool isl_space_is_wrapping(__isl_keep isl_space *dim)
2083 if (!dim)
2084 return isl_bool_error;
2086 if (!isl_space_is_set(dim))
2087 return isl_bool_false;
2089 return dim->nested[1] != NULL;
2092 /* Is "space" the space of a map where the domain is a wrapped map space?
2094 isl_bool isl_space_domain_is_wrapping(__isl_keep isl_space *space)
2096 if (!space)
2097 return isl_bool_error;
2099 if (isl_space_is_set(space))
2100 return isl_bool_false;
2102 return space->nested[0] != NULL;
2105 /* Is "space" the space of a map where the range is a wrapped map space?
2107 isl_bool isl_space_range_is_wrapping(__isl_keep isl_space *space)
2109 if (!space)
2110 return isl_bool_error;
2112 if (isl_space_is_set(space))
2113 return isl_bool_false;
2115 return space->nested[1] != NULL;
2118 /* Is "space" a product of two spaces?
2119 * That is, is it a wrapping set space or a map space
2120 * with wrapping domain and range?
2122 isl_bool isl_space_is_product(__isl_keep isl_space *space)
2124 isl_bool is_set;
2125 isl_bool is_product;
2127 is_set = isl_space_is_set(space);
2128 if (is_set < 0)
2129 return isl_bool_error;
2130 if (is_set)
2131 return isl_space_is_wrapping(space);
2132 is_product = isl_space_domain_is_wrapping(space);
2133 if (is_product < 0 || !is_product)
2134 return is_product;
2135 return isl_space_range_is_wrapping(space);
2138 __isl_give isl_space *isl_space_wrap(__isl_take isl_space *dim)
2140 isl_space *wrap;
2142 if (!dim)
2143 return NULL;
2145 wrap = isl_space_set_alloc(dim->ctx,
2146 dim->nparam, dim->n_in + dim->n_out);
2148 wrap = copy_ids(wrap, isl_dim_param, 0, dim, isl_dim_param);
2149 wrap = copy_ids(wrap, isl_dim_set, 0, dim, isl_dim_in);
2150 wrap = copy_ids(wrap, isl_dim_set, dim->n_in, dim, isl_dim_out);
2152 if (!wrap)
2153 goto error;
2155 wrap->nested[1] = dim;
2157 return wrap;
2158 error:
2159 isl_space_free(dim);
2160 return NULL;
2163 __isl_give isl_space *isl_space_unwrap(__isl_take isl_space *dim)
2165 isl_space *unwrap;
2167 if (!dim)
2168 return NULL;
2170 if (!isl_space_is_wrapping(dim))
2171 isl_die(dim->ctx, isl_error_invalid, "not a wrapping space",
2172 goto error);
2174 unwrap = isl_space_copy(dim->nested[1]);
2175 isl_space_free(dim);
2177 return unwrap;
2178 error:
2179 isl_space_free(dim);
2180 return NULL;
2183 isl_bool isl_space_is_named_or_nested(__isl_keep isl_space *space,
2184 enum isl_dim_type type)
2186 if (type != isl_dim_in && type != isl_dim_out)
2187 return isl_bool_false;
2188 if (!space)
2189 return isl_bool_error;
2190 if (space->tuple_id[type - isl_dim_in])
2191 return isl_bool_true;
2192 if (space->nested[type - isl_dim_in])
2193 return isl_bool_true;
2194 return isl_bool_false;
2197 isl_bool isl_space_may_be_set(__isl_keep isl_space *space)
2199 isl_bool nested;
2201 if (!space)
2202 return isl_bool_error;
2203 if (isl_space_is_set(space))
2204 return isl_bool_true;
2205 if (isl_space_dim(space, isl_dim_in) != 0)
2206 return isl_bool_false;
2207 nested = isl_space_is_named_or_nested(space, isl_dim_in);
2208 if (nested < 0 || nested)
2209 return isl_bool_not(nested);
2210 return isl_bool_true;
2213 __isl_give isl_space *isl_space_reset(__isl_take isl_space *dim,
2214 enum isl_dim_type type)
2216 if (!isl_space_is_named_or_nested(dim, type))
2217 return dim;
2219 dim = isl_space_cow(dim);
2220 if (!dim)
2221 return NULL;
2223 isl_id_free(dim->tuple_id[type - isl_dim_in]);
2224 dim->tuple_id[type - isl_dim_in] = NULL;
2225 isl_space_free(dim->nested[type - isl_dim_in]);
2226 dim->nested[type - isl_dim_in] = NULL;
2228 return dim;
2231 __isl_give isl_space *isl_space_flatten(__isl_take isl_space *dim)
2233 if (!dim)
2234 return NULL;
2235 if (!dim->nested[0] && !dim->nested[1])
2236 return dim;
2238 if (dim->nested[0])
2239 dim = isl_space_reset(dim, isl_dim_in);
2240 if (dim && dim->nested[1])
2241 dim = isl_space_reset(dim, isl_dim_out);
2243 return dim;
2246 __isl_give isl_space *isl_space_flatten_domain(__isl_take isl_space *space)
2248 if (!space)
2249 return NULL;
2250 if (!space->nested[0])
2251 return space;
2253 return isl_space_reset(space, isl_dim_in);
2256 __isl_give isl_space *isl_space_flatten_range(__isl_take isl_space *space)
2258 if (!space)
2259 return NULL;
2260 if (!space->nested[1])
2261 return space;
2263 return isl_space_reset(space, isl_dim_out);
2266 /* Replace the dimensions of the given type of dst by those of src.
2268 __isl_give isl_space *isl_space_replace(__isl_take isl_space *dst,
2269 enum isl_dim_type type, __isl_keep isl_space *src)
2271 dst = isl_space_cow(dst);
2273 if (!dst || !src)
2274 goto error;
2276 dst = isl_space_drop_dims(dst, type, 0, isl_space_dim(dst, type));
2277 dst = isl_space_add_dims(dst, type, isl_space_dim(src, type));
2278 dst = copy_ids(dst, type, 0, src, type);
2280 if (dst && type == isl_dim_param) {
2281 int i;
2282 for (i = 0; i <= 1; ++i) {
2283 if (!dst->nested[i])
2284 continue;
2285 dst->nested[i] = isl_space_replace(dst->nested[i],
2286 type, src);
2287 if (!dst->nested[i])
2288 goto error;
2292 return dst;
2293 error:
2294 isl_space_free(dst);
2295 return NULL;
2298 /* Given a dimension specification "dim" of a set, create a dimension
2299 * specification for the lift of the set. In particular, the result
2300 * is of the form [dim -> local[..]], with n_local variables in the
2301 * range of the wrapped map.
2303 __isl_give isl_space *isl_space_lift(__isl_take isl_space *dim, unsigned n_local)
2305 isl_space *local_dim;
2307 if (!dim)
2308 return NULL;
2310 local_dim = isl_space_dup(dim);
2311 local_dim = isl_space_drop_dims(local_dim, isl_dim_set, 0, dim->n_out);
2312 local_dim = isl_space_add_dims(local_dim, isl_dim_set, n_local);
2313 local_dim = isl_space_set_tuple_name(local_dim, isl_dim_set, "local");
2314 dim = isl_space_join(isl_space_from_domain(dim),
2315 isl_space_from_range(local_dim));
2316 dim = isl_space_wrap(dim);
2317 dim = isl_space_set_tuple_name(dim, isl_dim_set, "lifted");
2319 return dim;
2322 isl_bool isl_space_can_zip(__isl_keep isl_space *space)
2324 isl_bool is_set;
2326 is_set = isl_space_is_set(space);
2327 if (is_set < 0)
2328 return isl_bool_error;
2329 if (is_set)
2330 return isl_bool_false;
2331 return isl_space_is_product(space);
2334 __isl_give isl_space *isl_space_zip(__isl_take isl_space *dim)
2336 isl_space *dom, *ran;
2337 isl_space *dom_dom, *dom_ran, *ran_dom, *ran_ran;
2339 if (!isl_space_can_zip(dim))
2340 isl_die(dim->ctx, isl_error_invalid, "dim cannot be zipped",
2341 goto error);
2343 if (!dim)
2344 return NULL;
2345 dom = isl_space_unwrap(isl_space_domain(isl_space_copy(dim)));
2346 ran = isl_space_unwrap(isl_space_range(dim));
2347 dom_dom = isl_space_domain(isl_space_copy(dom));
2348 dom_ran = isl_space_range(dom);
2349 ran_dom = isl_space_domain(isl_space_copy(ran));
2350 ran_ran = isl_space_range(ran);
2351 dom = isl_space_join(isl_space_from_domain(dom_dom),
2352 isl_space_from_range(ran_dom));
2353 ran = isl_space_join(isl_space_from_domain(dom_ran),
2354 isl_space_from_range(ran_ran));
2355 return isl_space_join(isl_space_from_domain(isl_space_wrap(dom)),
2356 isl_space_from_range(isl_space_wrap(ran)));
2357 error:
2358 isl_space_free(dim);
2359 return NULL;
2362 /* Can we apply isl_space_curry to "space"?
2363 * That is, does it have a nested relation in its domain?
2365 isl_bool isl_space_can_curry(__isl_keep isl_space *space)
2367 if (!space)
2368 return isl_bool_error;
2370 return !!space->nested[0];
2373 /* Given a space (A -> B) -> C, return the corresponding space
2374 * A -> (B -> C).
2376 __isl_give isl_space *isl_space_curry(__isl_take isl_space *space)
2378 isl_space *dom, *ran;
2379 isl_space *dom_dom, *dom_ran;
2381 if (!space)
2382 return NULL;
2384 if (!isl_space_can_curry(space))
2385 isl_die(space->ctx, isl_error_invalid,
2386 "space cannot be curried", goto error);
2388 dom = isl_space_unwrap(isl_space_domain(isl_space_copy(space)));
2389 ran = isl_space_range(space);
2390 dom_dom = isl_space_domain(isl_space_copy(dom));
2391 dom_ran = isl_space_range(dom);
2392 ran = isl_space_join(isl_space_from_domain(dom_ran),
2393 isl_space_from_range(ran));
2394 return isl_space_join(isl_space_from_domain(dom_dom),
2395 isl_space_from_range(isl_space_wrap(ran)));
2396 error:
2397 isl_space_free(space);
2398 return NULL;
2401 /* Can isl_space_range_curry be applied to "space"?
2402 * That is, does it have a nested relation in its range,
2403 * the domain of which is itself a nested relation?
2405 isl_bool isl_space_can_range_curry(__isl_keep isl_space *space)
2407 isl_bool can;
2409 if (!space)
2410 return isl_bool_error;
2411 can = isl_space_range_is_wrapping(space);
2412 if (can < 0 || !can)
2413 return can;
2414 return isl_space_can_curry(space->nested[1]);
2417 /* Given a space A -> ((B -> C) -> D), return the corresponding space
2418 * A -> (B -> (C -> D)).
2420 __isl_give isl_space *isl_space_range_curry(__isl_take isl_space *space)
2422 if (!space)
2423 return NULL;
2425 if (!isl_space_can_range_curry(space))
2426 isl_die(isl_space_get_ctx(space), isl_error_invalid,
2427 "space range cannot be curried",
2428 return isl_space_free(space));
2430 space = isl_space_cow(space);
2431 if (!space)
2432 return NULL;
2433 space->nested[1] = isl_space_curry(space->nested[1]);
2434 if (!space->nested[1])
2435 return isl_space_free(space);
2437 return space;
2440 /* Can we apply isl_space_uncurry to "space"?
2441 * That is, does it have a nested relation in its range?
2443 isl_bool isl_space_can_uncurry(__isl_keep isl_space *space)
2445 if (!space)
2446 return isl_bool_error;
2448 return !!space->nested[1];
2451 /* Given a space A -> (B -> C), return the corresponding space
2452 * (A -> B) -> C.
2454 __isl_give isl_space *isl_space_uncurry(__isl_take isl_space *space)
2456 isl_space *dom, *ran;
2457 isl_space *ran_dom, *ran_ran;
2459 if (!space)
2460 return NULL;
2462 if (!isl_space_can_uncurry(space))
2463 isl_die(space->ctx, isl_error_invalid,
2464 "space cannot be uncurried",
2465 return isl_space_free(space));
2467 dom = isl_space_domain(isl_space_copy(space));
2468 ran = isl_space_unwrap(isl_space_range(space));
2469 ran_dom = isl_space_domain(isl_space_copy(ran));
2470 ran_ran = isl_space_range(ran);
2471 dom = isl_space_join(isl_space_from_domain(dom),
2472 isl_space_from_range(ran_dom));
2473 return isl_space_join(isl_space_from_domain(isl_space_wrap(dom)),
2474 isl_space_from_range(ran_ran));
2477 isl_bool isl_space_has_named_params(__isl_keep isl_space *space)
2479 int i;
2480 unsigned off;
2482 if (!space)
2483 return isl_bool_error;
2484 if (space->nparam == 0)
2485 return isl_bool_true;
2486 off = isl_space_offset(space, isl_dim_param);
2487 if (off + space->nparam > space->n_id)
2488 return isl_bool_false;
2489 for (i = 0; i < space->nparam; ++i)
2490 if (!space->ids[off + i])
2491 return isl_bool_false;
2492 return isl_bool_true;
2495 /* Check that "space" has only named parameters, reporting an error
2496 * if it does not.
2498 isl_stat isl_space_check_named_params(__isl_keep isl_space *space)
2500 isl_bool named;
2502 named = isl_space_has_named_params(space);
2503 if (named < 0)
2504 return isl_stat_error;
2505 if (!named)
2506 isl_die(isl_space_get_ctx(space), isl_error_invalid,
2507 "unaligned unnamed parameters", return isl_stat_error);
2509 return isl_stat_ok;
2512 /* Align the initial parameters of dim1 to match the order in dim2.
2514 __isl_give isl_space *isl_space_align_params(__isl_take isl_space *dim1,
2515 __isl_take isl_space *dim2)
2517 isl_reordering *exp;
2519 if (!isl_space_has_named_params(dim1) || !isl_space_has_named_params(dim2))
2520 isl_die(isl_space_get_ctx(dim1), isl_error_invalid,
2521 "parameter alignment requires named parameters",
2522 goto error);
2524 dim2 = isl_space_params(dim2);
2525 exp = isl_parameter_alignment_reordering(dim1, dim2);
2526 exp = isl_reordering_extend_space(exp, dim1);
2527 isl_space_free(dim2);
2528 if (!exp)
2529 return NULL;
2530 dim1 = isl_space_copy(exp->dim);
2531 isl_reordering_free(exp);
2532 return dim1;
2533 error:
2534 isl_space_free(dim1);
2535 isl_space_free(dim2);
2536 return NULL;
2539 /* Given the space of set (domain), construct a space for a map
2540 * with as domain the given space and as range the range of "model".
2542 __isl_give isl_space *isl_space_extend_domain_with_range(
2543 __isl_take isl_space *space, __isl_take isl_space *model)
2545 if (!model)
2546 goto error;
2548 space = isl_space_from_domain(space);
2549 space = isl_space_add_dims(space, isl_dim_out,
2550 isl_space_dim(model, isl_dim_out));
2551 if (isl_space_has_tuple_id(model, isl_dim_out))
2552 space = isl_space_set_tuple_id(space, isl_dim_out,
2553 isl_space_get_tuple_id(model, isl_dim_out));
2554 if (!space)
2555 goto error;
2556 if (model->nested[1]) {
2557 isl_space *nested = isl_space_copy(model->nested[1]);
2558 int n_nested, n_space;
2559 nested = isl_space_align_params(nested, isl_space_copy(space));
2560 n_nested = isl_space_dim(nested, isl_dim_param);
2561 n_space = isl_space_dim(space, isl_dim_param);
2562 if (n_nested > n_space)
2563 nested = isl_space_drop_dims(nested, isl_dim_param,
2564 n_space, n_nested - n_space);
2565 if (!nested)
2566 goto error;
2567 space->nested[1] = nested;
2569 isl_space_free(model);
2570 return space;
2571 error:
2572 isl_space_free(model);
2573 isl_space_free(space);
2574 return NULL;
2577 /* Compare the "type" dimensions of two isl_spaces.
2579 * The order is fairly arbitrary.
2581 static int isl_space_cmp_type(__isl_keep isl_space *space1,
2582 __isl_keep isl_space *space2, enum isl_dim_type type)
2584 int cmp;
2585 isl_space *nested1, *nested2;
2587 if (isl_space_dim(space1, type) != isl_space_dim(space2, type))
2588 return isl_space_dim(space1, type) -
2589 isl_space_dim(space2, type);
2591 cmp = isl_id_cmp(tuple_id(space1, type), tuple_id(space2, type));
2592 if (cmp != 0)
2593 return cmp;
2595 nested1 = nested(space1, type);
2596 nested2 = nested(space2, type);
2597 if (!nested1 != !nested2)
2598 return !nested1 - !nested2;
2600 if (nested1)
2601 return isl_space_cmp(nested1, nested2);
2603 return 0;
2606 /* Compare two isl_spaces.
2608 * The order is fairly arbitrary.
2610 int isl_space_cmp(__isl_keep isl_space *space1, __isl_keep isl_space *space2)
2612 int i;
2613 int cmp;
2615 if (space1 == space2)
2616 return 0;
2617 if (!space1)
2618 return -1;
2619 if (!space2)
2620 return 1;
2622 cmp = isl_space_cmp_type(space1, space2, isl_dim_param);
2623 if (cmp != 0)
2624 return cmp;
2625 cmp = isl_space_cmp_type(space1, space2, isl_dim_in);
2626 if (cmp != 0)
2627 return cmp;
2628 cmp = isl_space_cmp_type(space1, space2, isl_dim_out);
2629 if (cmp != 0)
2630 return cmp;
2632 if (!space1->ids && !space2->ids)
2633 return 0;
2635 for (i = 0; i < n(space1, isl_dim_param); ++i) {
2636 cmp = isl_id_cmp(get_id(space1, isl_dim_param, i),
2637 get_id(space2, isl_dim_param, i));
2638 if (cmp != 0)
2639 return cmp;
2642 return 0;