isl_space_extend: detect overflow in total number of dimensions
[isl.git] / isl_space.c
blob8e56359b1e142e54a68cc40fc326e4f15a275644
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2013-2014 Ecole Normale Superieure
6 * Use of this software is governed by the MIT license
8 * Written by Sven Verdoolaege, K.U.Leuven, Departement
9 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
10 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
11 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
12 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
15 #include <stdlib.h>
16 #include <string.h>
17 #include <isl_space_private.h>
18 #include <isl_id_private.h>
19 #include <isl_reordering.h>
21 isl_ctx *isl_space_get_ctx(__isl_keep isl_space *dim)
23 return dim ? dim->ctx : NULL;
26 __isl_give isl_space *isl_space_alloc(isl_ctx *ctx,
27 unsigned nparam, unsigned n_in, unsigned n_out)
29 isl_space *dim;
31 dim = isl_alloc_type(ctx, struct isl_space);
32 if (!dim)
33 return NULL;
35 dim->ctx = ctx;
36 isl_ctx_ref(ctx);
37 dim->ref = 1;
38 dim->nparam = nparam;
39 dim->n_in = n_in;
40 dim->n_out = n_out;
42 dim->tuple_id[0] = NULL;
43 dim->tuple_id[1] = NULL;
45 dim->nested[0] = NULL;
46 dim->nested[1] = NULL;
48 dim->n_id = 0;
49 dim->ids = NULL;
51 return dim;
54 /* Mark the space as being that of a set, by setting the domain tuple
55 * to isl_id_none.
57 static __isl_give isl_space *mark_as_set(__isl_take isl_space *space)
59 space = isl_space_cow(space);
60 if (!space)
61 return NULL;
62 space = isl_space_set_tuple_id(space, isl_dim_in, &isl_id_none);
63 return space;
66 /* Is the space that of a set?
68 int isl_space_is_set(__isl_keep isl_space *space)
70 if (!space)
71 return -1;
72 if (space->n_in != 0 || space->nested[0])
73 return 0;
74 if (space->tuple_id[0] != &isl_id_none)
75 return 0;
76 return 1;
79 /* Is the given space that of a map?
81 int isl_space_is_map(__isl_keep isl_space *space)
83 if (!space)
84 return -1;
85 return space->tuple_id[0] != &isl_id_none &&
86 space->tuple_id[1] != &isl_id_none;
89 __isl_give isl_space *isl_space_set_alloc(isl_ctx *ctx,
90 unsigned nparam, unsigned dim)
92 isl_space *space;
93 space = isl_space_alloc(ctx, nparam, 0, dim);
94 space = mark_as_set(space);
95 return space;
98 /* Mark the space as being that of a parameter domain, by setting
99 * both tuples to isl_id_none.
101 static __isl_give isl_space *mark_as_params(isl_space *space)
103 if (!space)
104 return NULL;
105 space = isl_space_set_tuple_id(space, isl_dim_in, &isl_id_none);
106 space = isl_space_set_tuple_id(space, isl_dim_out, &isl_id_none);
107 return space;
110 /* Is the space that of a parameter domain?
112 int isl_space_is_params(__isl_keep isl_space *space)
114 if (!space)
115 return -1;
116 if (space->n_in != 0 || space->nested[0] ||
117 space->n_out != 0 || space->nested[1])
118 return 0;
119 if (space->tuple_id[0] != &isl_id_none)
120 return 0;
121 if (space->tuple_id[1] != &isl_id_none)
122 return 0;
123 return 1;
126 /* Create a space for a parameter domain.
128 __isl_give isl_space *isl_space_params_alloc(isl_ctx *ctx, unsigned nparam)
130 isl_space *space;
131 space = isl_space_alloc(ctx, nparam, 0, 0);
132 space = mark_as_params(space);
133 return space;
136 static unsigned global_pos(__isl_keep isl_space *dim,
137 enum isl_dim_type type, unsigned pos)
139 struct isl_ctx *ctx = dim->ctx;
141 switch (type) {
142 case isl_dim_param:
143 isl_assert(ctx, pos < dim->nparam,
144 return isl_space_dim(dim, isl_dim_all));
145 return pos;
146 case isl_dim_in:
147 isl_assert(ctx, pos < dim->n_in,
148 return isl_space_dim(dim, isl_dim_all));
149 return pos + dim->nparam;
150 case isl_dim_out:
151 isl_assert(ctx, pos < dim->n_out,
152 return isl_space_dim(dim, isl_dim_all));
153 return pos + dim->nparam + dim->n_in;
154 default:
155 isl_assert(ctx, 0, return isl_space_dim(dim, isl_dim_all));
157 return isl_space_dim(dim, isl_dim_all);
160 /* Extend length of ids array to the total number of dimensions.
162 static __isl_give isl_space *extend_ids(__isl_take isl_space *dim)
164 isl_id **ids;
165 int i;
167 if (isl_space_dim(dim, isl_dim_all) <= dim->n_id)
168 return dim;
170 if (!dim->ids) {
171 dim->ids = isl_calloc_array(dim->ctx,
172 isl_id *, isl_space_dim(dim, isl_dim_all));
173 if (!dim->ids)
174 goto error;
175 } else {
176 ids = isl_realloc_array(dim->ctx, dim->ids,
177 isl_id *, isl_space_dim(dim, isl_dim_all));
178 if (!ids)
179 goto error;
180 dim->ids = ids;
181 for (i = dim->n_id; i < isl_space_dim(dim, isl_dim_all); ++i)
182 dim->ids[i] = NULL;
185 dim->n_id = isl_space_dim(dim, isl_dim_all);
187 return dim;
188 error:
189 isl_space_free(dim);
190 return NULL;
193 static __isl_give isl_space *set_id(__isl_take isl_space *dim,
194 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
196 dim = isl_space_cow(dim);
198 if (!dim)
199 goto error;
201 pos = global_pos(dim, type, pos);
202 if (pos == isl_space_dim(dim, isl_dim_all))
203 goto error;
205 if (pos >= dim->n_id) {
206 if (!id)
207 return dim;
208 dim = extend_ids(dim);
209 if (!dim)
210 goto error;
213 dim->ids[pos] = id;
215 return dim;
216 error:
217 isl_id_free(id);
218 isl_space_free(dim);
219 return NULL;
222 static __isl_keep isl_id *get_id(__isl_keep isl_space *dim,
223 enum isl_dim_type type, unsigned pos)
225 if (!dim)
226 return NULL;
228 pos = global_pos(dim, type, pos);
229 if (pos == isl_space_dim(dim, isl_dim_all))
230 return NULL;
231 if (pos >= dim->n_id)
232 return NULL;
233 return dim->ids[pos];
236 static unsigned offset(__isl_keep isl_space *dim, enum isl_dim_type type)
238 switch (type) {
239 case isl_dim_param: return 0;
240 case isl_dim_in: return dim->nparam;
241 case isl_dim_out: return dim->nparam + dim->n_in;
242 default: return 0;
246 static unsigned n(__isl_keep isl_space *dim, enum isl_dim_type type)
248 switch (type) {
249 case isl_dim_param: return dim->nparam;
250 case isl_dim_in: return dim->n_in;
251 case isl_dim_out: return dim->n_out;
252 case isl_dim_all: return dim->nparam + dim->n_in + dim->n_out;
253 default: return 0;
257 unsigned isl_space_dim(__isl_keep isl_space *dim, enum isl_dim_type type)
259 if (!dim)
260 return 0;
261 return n(dim, type);
264 unsigned isl_space_offset(__isl_keep isl_space *dim, enum isl_dim_type type)
266 if (!dim)
267 return 0;
268 return offset(dim, type);
271 static __isl_give isl_space *copy_ids(__isl_take isl_space *dst,
272 enum isl_dim_type dst_type, unsigned offset, __isl_keep isl_space *src,
273 enum isl_dim_type src_type)
275 int i;
276 isl_id *id;
278 if (!dst)
279 return NULL;
281 for (i = 0; i < n(src, src_type); ++i) {
282 id = get_id(src, src_type, i);
283 if (!id)
284 continue;
285 dst = set_id(dst, dst_type, offset + i, isl_id_copy(id));
286 if (!dst)
287 return NULL;
289 return dst;
292 __isl_take isl_space *isl_space_dup(__isl_keep isl_space *dim)
294 isl_space *dup;
295 if (!dim)
296 return NULL;
297 dup = isl_space_alloc(dim->ctx, dim->nparam, dim->n_in, dim->n_out);
298 if (!dup)
299 return NULL;
300 if (dim->tuple_id[0] &&
301 !(dup->tuple_id[0] = isl_id_copy(dim->tuple_id[0])))
302 goto error;
303 if (dim->tuple_id[1] &&
304 !(dup->tuple_id[1] = isl_id_copy(dim->tuple_id[1])))
305 goto error;
306 if (dim->nested[0] && !(dup->nested[0] = isl_space_copy(dim->nested[0])))
307 goto error;
308 if (dim->nested[1] && !(dup->nested[1] = isl_space_copy(dim->nested[1])))
309 goto error;
310 if (!dim->ids)
311 return dup;
312 dup = copy_ids(dup, isl_dim_param, 0, dim, isl_dim_param);
313 dup = copy_ids(dup, isl_dim_in, 0, dim, isl_dim_in);
314 dup = copy_ids(dup, isl_dim_out, 0, dim, isl_dim_out);
315 return dup;
316 error:
317 isl_space_free(dup);
318 return NULL;
321 __isl_give isl_space *isl_space_cow(__isl_take isl_space *dim)
323 if (!dim)
324 return NULL;
326 if (dim->ref == 1)
327 return dim;
328 dim->ref--;
329 return isl_space_dup(dim);
332 __isl_give isl_space *isl_space_copy(__isl_keep isl_space *dim)
334 if (!dim)
335 return NULL;
337 dim->ref++;
338 return dim;
341 __isl_null isl_space *isl_space_free(__isl_take isl_space *space)
343 int i;
345 if (!space)
346 return NULL;
348 if (--space->ref > 0)
349 return NULL;
351 isl_id_free(space->tuple_id[0]);
352 isl_id_free(space->tuple_id[1]);
354 isl_space_free(space->nested[0]);
355 isl_space_free(space->nested[1]);
357 for (i = 0; i < space->n_id; ++i)
358 isl_id_free(space->ids[i]);
359 free(space->ids);
360 isl_ctx_deref(space->ctx);
362 free(space);
364 return NULL;
367 /* Check if "s" is a valid dimension or tuple name.
368 * We currently only forbid names that look like a number.
370 * s is assumed to be non-NULL.
372 static int name_ok(isl_ctx *ctx, const char *s)
374 char *p;
375 long dummy;
377 dummy = strtol(s, &p, 0);
378 if (p != s)
379 isl_die(ctx, isl_error_invalid, "name looks like a number",
380 return 0);
382 return 1;
385 /* Is it possible for the given dimension type to have a tuple id?
387 static int space_can_have_id(__isl_keep isl_space *space,
388 enum isl_dim_type type)
390 if (!space)
391 return 0;
392 if (isl_space_is_params(space))
393 isl_die(space->ctx, isl_error_invalid,
394 "parameter spaces don't have tuple ids", return 0);
395 if (isl_space_is_set(space) && type != isl_dim_set)
396 isl_die(space->ctx, isl_error_invalid,
397 "set spaces can only have a set id", return 0);
398 if (type != isl_dim_in && type != isl_dim_out)
399 isl_die(space->ctx, isl_error_invalid,
400 "only input, output and set tuples can have ids",
401 return 0);
403 return 1;
406 /* Does the tuple have an id?
408 int isl_space_has_tuple_id(__isl_keep isl_space *dim, enum isl_dim_type type)
410 if (!space_can_have_id(dim, type))
411 return -1;
412 return dim->tuple_id[type - isl_dim_in] != NULL;
415 __isl_give isl_id *isl_space_get_tuple_id(__isl_keep isl_space *dim,
416 enum isl_dim_type type)
418 int has_id;
420 if (!dim)
421 return NULL;
422 has_id = isl_space_has_tuple_id(dim, type);
423 if (has_id < 0)
424 return NULL;
425 if (!has_id)
426 isl_die(dim->ctx, isl_error_invalid,
427 "tuple has no id", return NULL);
428 return isl_id_copy(dim->tuple_id[type - isl_dim_in]);
431 __isl_give isl_space *isl_space_set_tuple_id(__isl_take isl_space *dim,
432 enum isl_dim_type type, __isl_take isl_id *id)
434 dim = isl_space_cow(dim);
435 if (!dim || !id)
436 goto error;
437 if (type != isl_dim_in && type != isl_dim_out)
438 isl_die(dim->ctx, isl_error_invalid,
439 "only input, output and set tuples can have names",
440 goto error);
442 isl_id_free(dim->tuple_id[type - isl_dim_in]);
443 dim->tuple_id[type - isl_dim_in] = id;
445 return dim;
446 error:
447 isl_id_free(id);
448 isl_space_free(dim);
449 return NULL;
452 __isl_give isl_space *isl_space_reset_tuple_id(__isl_take isl_space *dim,
453 enum isl_dim_type type)
455 dim = isl_space_cow(dim);
456 if (!dim)
457 return NULL;
458 if (type != isl_dim_in && type != isl_dim_out)
459 isl_die(dim->ctx, isl_error_invalid,
460 "only input, output and set tuples can have names",
461 goto error);
463 isl_id_free(dim->tuple_id[type - isl_dim_in]);
464 dim->tuple_id[type - isl_dim_in] = NULL;
466 return dim;
467 error:
468 isl_space_free(dim);
469 return NULL;
472 /* Set the id of the given dimension of "space" to "id".
473 * If the dimension already has an id, then it is replaced.
474 * If the dimension is a parameter, then we need to change it
475 * in the nested spaces (if any) as well.
477 __isl_give isl_space *isl_space_set_dim_id(__isl_take isl_space *space,
478 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
480 space = isl_space_cow(space);
481 if (!space || !id)
482 goto error;
484 if (type == isl_dim_param) {
485 int i;
487 for (i = 0; i < 2; ++i) {
488 if (!space->nested[i])
489 continue;
490 space->nested[i] =
491 isl_space_set_dim_id(space->nested[i],
492 type, pos, isl_id_copy(id));
493 if (!space->nested[i])
494 goto error;
498 isl_id_free(get_id(space, type, pos));
499 return set_id(space, type, pos, id);
500 error:
501 isl_id_free(id);
502 isl_space_free(space);
503 return NULL;
506 /* Reset the id of the given dimension of "space".
507 * If the dimension already has an id, then it is removed.
508 * If the dimension is a parameter, then we need to reset it
509 * in the nested spaces (if any) as well.
511 __isl_give isl_space *isl_space_reset_dim_id(__isl_take isl_space *space,
512 enum isl_dim_type type, unsigned pos)
514 space = isl_space_cow(space);
515 if (!space)
516 goto error;
518 if (type == isl_dim_param) {
519 int i;
521 for (i = 0; i < 2; ++i) {
522 if (!space->nested[i])
523 continue;
524 space->nested[i] =
525 isl_space_reset_dim_id(space->nested[i],
526 type, pos);
527 if (!space->nested[i])
528 goto error;
532 isl_id_free(get_id(space, type, pos));
533 return set_id(space, type, pos, NULL);
534 error:
535 isl_space_free(space);
536 return NULL;
539 int isl_space_has_dim_id(__isl_keep isl_space *dim,
540 enum isl_dim_type type, unsigned pos)
542 if (!dim)
543 return -1;
544 return get_id(dim, type, pos) != NULL;
547 __isl_give isl_id *isl_space_get_dim_id(__isl_keep isl_space *dim,
548 enum isl_dim_type type, unsigned pos)
550 if (!dim)
551 return NULL;
552 if (!get_id(dim, type, pos))
553 isl_die(dim->ctx, isl_error_invalid,
554 "dim has no id", return NULL);
555 return isl_id_copy(get_id(dim, type, pos));
558 __isl_give isl_space *isl_space_set_tuple_name(__isl_take isl_space *dim,
559 enum isl_dim_type type, const char *s)
561 isl_id *id;
563 if (!dim)
564 return NULL;
566 if (!s)
567 return isl_space_reset_tuple_id(dim, type);
569 if (!name_ok(dim->ctx, s))
570 goto error;
572 id = isl_id_alloc(dim->ctx, s, NULL);
573 return isl_space_set_tuple_id(dim, type, id);
574 error:
575 isl_space_free(dim);
576 return NULL;
579 /* Does the tuple have a name?
581 int isl_space_has_tuple_name(__isl_keep isl_space *space,
582 enum isl_dim_type type)
584 isl_id *id;
586 if (!space_can_have_id(space, type))
587 return -1;
588 id = space->tuple_id[type - isl_dim_in];
589 return id && id->name;
592 const char *isl_space_get_tuple_name(__isl_keep isl_space *dim,
593 enum isl_dim_type type)
595 isl_id *id;
596 if (!dim)
597 return NULL;
598 if (type != isl_dim_in && type != isl_dim_out)
599 return NULL;
600 id = dim->tuple_id[type - isl_dim_in];
601 return id ? id->name : NULL;
604 __isl_give isl_space *isl_space_set_dim_name(__isl_take isl_space *dim,
605 enum isl_dim_type type, unsigned pos,
606 const char *s)
608 isl_id *id;
610 if (!dim)
611 return NULL;
612 if (!s)
613 return isl_space_reset_dim_id(dim, type, pos);
614 if (!name_ok(dim->ctx, s))
615 goto error;
616 id = isl_id_alloc(dim->ctx, s, NULL);
617 return isl_space_set_dim_id(dim, type, pos, id);
618 error:
619 isl_space_free(dim);
620 return NULL;
623 /* Does the given dimension have a name?
625 int isl_space_has_dim_name(__isl_keep isl_space *space,
626 enum isl_dim_type type, unsigned pos)
628 isl_id *id;
630 if (!space)
631 return -1;
632 id = get_id(space, type, pos);
633 return id && id->name;
636 __isl_keep const char *isl_space_get_dim_name(__isl_keep isl_space *dim,
637 enum isl_dim_type type, unsigned pos)
639 isl_id *id = get_id(dim, type, pos);
640 return id ? id->name : NULL;
643 int isl_space_find_dim_by_id(__isl_keep isl_space *dim, enum isl_dim_type type,
644 __isl_keep isl_id *id)
646 int i;
647 int offset;
648 int n;
650 if (!dim || !id)
651 return -1;
653 offset = isl_space_offset(dim, type);
654 n = isl_space_dim(dim, type);
655 for (i = 0; i < n && offset + i < dim->n_id; ++i)
656 if (dim->ids[offset + i] == id)
657 return i;
659 return -1;
662 int isl_space_find_dim_by_name(__isl_keep isl_space *space,
663 enum isl_dim_type type, const char *name)
665 int i;
666 int offset;
667 int n;
669 if (!space || !name)
670 return -1;
672 offset = isl_space_offset(space, type);
673 n = isl_space_dim(space, type);
674 for (i = 0; i < n && offset + i < space->n_id; ++i)
675 if (space->ids[offset + i]->name &&
676 !strcmp(space->ids[offset + i]->name, name))
677 return i;
679 return -1;
682 /* Reset the user pointer on all identifiers of parameters and tuples
683 * of "space".
685 __isl_give isl_space *isl_space_reset_user(__isl_take isl_space *space)
687 int i;
688 isl_ctx *ctx;
689 isl_id *id;
690 const char *name;
692 if (!space)
693 return NULL;
695 ctx = isl_space_get_ctx(space);
697 for (i = 0; i < space->nparam && i < space->n_id; ++i) {
698 if (!isl_id_get_user(space->ids[i]))
699 continue;
700 space = isl_space_cow(space);
701 if (!space)
702 return NULL;
703 name = isl_id_get_name(space->ids[i]);
704 id = isl_id_alloc(ctx, name, NULL);
705 isl_id_free(space->ids[i]);
706 space->ids[i] = id;
707 if (!id)
708 return isl_space_free(space);
711 for (i = 0; i < 2; ++i) {
712 if (!space->tuple_id[i])
713 continue;
714 if (!isl_id_get_user(space->tuple_id[i]))
715 continue;
716 space = isl_space_cow(space);
717 if (!space)
718 return NULL;
719 name = isl_id_get_name(space->tuple_id[i]);
720 id = isl_id_alloc(ctx, name, NULL);
721 isl_id_free(space->tuple_id[i]);
722 space->tuple_id[i] = id;
723 if (!id)
724 return isl_space_free(space);
727 for (i = 0; i < 2; ++i) {
728 if (!space->nested[i])
729 continue;
730 space = isl_space_cow(space);
731 if (!space)
732 return NULL;
733 space->nested[i] = isl_space_reset_user(space->nested[i]);
734 if (!space->nested[i])
735 return isl_space_free(space);
738 return space;
741 static __isl_keep isl_id *tuple_id(__isl_keep isl_space *dim,
742 enum isl_dim_type type)
744 if (!dim)
745 return NULL;
746 if (type == isl_dim_in)
747 return dim->tuple_id[0];
748 if (type == isl_dim_out)
749 return dim->tuple_id[1];
750 return NULL;
753 static __isl_keep isl_space *nested(__isl_keep isl_space *dim,
754 enum isl_dim_type type)
756 if (!dim)
757 return NULL;
758 if (type == isl_dim_in)
759 return dim->nested[0];
760 if (type == isl_dim_out)
761 return dim->nested[1];
762 return NULL;
765 /* Are the two spaces the same, apart from positions and names of parameters?
767 static int isl_space_has_equal_tuples(__isl_keep isl_space *space1,
768 __isl_keep isl_space *space2)
770 if (!space1 || !space2)
771 return -1;
772 if (space1 == space2)
773 return 1;
774 return isl_space_tuple_is_equal(space1, isl_dim_in,
775 space2, isl_dim_in) &&
776 isl_space_tuple_is_equal(space1, isl_dim_out,
777 space2, isl_dim_out);
780 /* Check if the tuple of type "type1" of "space1" is the same as
781 * the tuple of type "type2" of "space2".
783 * That is, check if the tuples have the same identifier, the same dimension
784 * and the same internal structure.
785 * The identifiers of the dimensions inside the tuples do not affect the result.
787 * Note that this function only checks the tuples themselves.
788 * If nested tuples are involved, then we need to be careful not
789 * to have result affected by possibly differing parameters
790 * in those nested tuples.
792 int isl_space_tuple_is_equal(__isl_keep isl_space *space1,
793 enum isl_dim_type type1, __isl_keep isl_space *space2,
794 enum isl_dim_type type2)
796 isl_id *id1, *id2;
797 isl_space *nested1, *nested2;
799 if (!space1 || !space2)
800 return -1;
802 if (space1 == space2 && type1 == type2)
803 return 1;
805 if (n(space1, type1) != n(space2, type2))
806 return 0;
807 id1 = tuple_id(space1, type1);
808 id2 = tuple_id(space2, type2);
809 if (!id1 ^ !id2)
810 return 0;
811 if (id1 && id1 != id2)
812 return 0;
813 nested1 = nested(space1, type1);
814 nested2 = nested(space2, type2);
815 if (!nested1 ^ !nested2)
816 return 0;
817 if (nested1 && !isl_space_has_equal_tuples(nested1, nested2))
818 return 0;
819 return 1;
822 /* This is the old, undocumented, name for isl_space_tuple_is_equal.
823 * It will be removed at some point.
825 int isl_space_tuple_match(__isl_keep isl_space *space1, enum isl_dim_type type1,
826 __isl_keep isl_space *space2, enum isl_dim_type type2)
828 return isl_space_tuple_is_equal(space1, type1, space2, type2);
831 static int match(__isl_keep isl_space *dim1, enum isl_dim_type dim1_type,
832 __isl_keep isl_space *dim2, enum isl_dim_type dim2_type)
834 int i;
836 if (dim1 == dim2 && dim1_type == dim2_type)
837 return 1;
839 if (!isl_space_tuple_is_equal(dim1, dim1_type, dim2, dim2_type))
840 return 0;
842 if (!dim1->ids && !dim2->ids)
843 return 1;
845 for (i = 0; i < n(dim1, dim1_type); ++i) {
846 if (get_id(dim1, dim1_type, i) != get_id(dim2, dim2_type, i))
847 return 0;
849 return 1;
852 int isl_space_match(__isl_keep isl_space *dim1, enum isl_dim_type dim1_type,
853 __isl_keep isl_space *dim2, enum isl_dim_type dim2_type)
855 if (!dim1 || !dim2)
856 return -1;
858 return match(dim1, dim1_type, dim2, dim2_type);
861 static void get_ids(__isl_keep isl_space *dim, enum isl_dim_type type,
862 unsigned first, unsigned n, __isl_keep isl_id **ids)
864 int i;
866 for (i = 0; i < n ; ++i)
867 ids[i] = get_id(dim, type, first + i);
870 __isl_give isl_space *isl_space_extend(__isl_take isl_space *space,
871 unsigned nparam, unsigned n_in, unsigned n_out)
873 isl_id **ids = NULL;
875 if (!space)
876 return NULL;
877 if (space->nparam == nparam &&
878 space->n_in == n_in && space->n_out == n_out)
879 return space;
881 isl_assert(space->ctx, space->nparam <= nparam, goto error);
882 isl_assert(space->ctx, space->n_in <= n_in, goto error);
883 isl_assert(space->ctx, space->n_out <= n_out, goto error);
885 space = isl_space_cow(space);
886 if (!space)
887 goto error;
889 if (space->ids) {
890 unsigned n;
891 n = nparam + n_in + n_out;
892 if (n < nparam || n < n_in || n < n_out)
893 isl_die(isl_space_get_ctx(space), isl_error_invalid,
894 "overflow in total number of dimensions",
895 goto error);
896 ids = isl_calloc_array(space->ctx, isl_id *, n);
897 if (!ids)
898 goto error;
899 get_ids(space, isl_dim_param, 0, space->nparam, ids);
900 get_ids(space, isl_dim_in, 0, space->n_in, ids + nparam);
901 get_ids(space, isl_dim_out, 0, space->n_out,
902 ids + nparam + n_in);
903 free(space->ids);
904 space->ids = ids;
905 space->n_id = nparam + n_in + n_out;
907 space->nparam = nparam;
908 space->n_in = n_in;
909 space->n_out = n_out;
911 return space;
912 error:
913 free(ids);
914 isl_space_free(space);
915 return NULL;
918 __isl_give isl_space *isl_space_add_dims(__isl_take isl_space *dim,
919 enum isl_dim_type type, unsigned n)
921 dim = isl_space_reset(dim, type);
922 if (!dim)
923 return NULL;
924 switch (type) {
925 case isl_dim_param:
926 dim = isl_space_extend(dim,
927 dim->nparam + n, dim->n_in, dim->n_out);
928 if (dim && dim->nested[0] &&
929 !(dim->nested[0] = isl_space_add_dims(dim->nested[0],
930 isl_dim_param, n)))
931 goto error;
932 if (dim && dim->nested[1] &&
933 !(dim->nested[1] = isl_space_add_dims(dim->nested[1],
934 isl_dim_param, n)))
935 goto error;
936 return dim;
937 case isl_dim_in:
938 return isl_space_extend(dim,
939 dim->nparam, dim->n_in + n, dim->n_out);
940 case isl_dim_out:
941 return isl_space_extend(dim,
942 dim->nparam, dim->n_in, dim->n_out + n);
943 default:
944 isl_die(dim->ctx, isl_error_invalid,
945 "cannot add dimensions of specified type", goto error);
947 error:
948 isl_space_free(dim);
949 return NULL;
952 static int valid_dim_type(enum isl_dim_type type)
954 switch (type) {
955 case isl_dim_param:
956 case isl_dim_in:
957 case isl_dim_out:
958 return 1;
959 default:
960 return 0;
964 /* Insert "n" dimensions of type "type" at position "pos".
965 * If we are inserting parameters, then they are also inserted in
966 * any nested spaces.
968 __isl_give isl_space *isl_space_insert_dims(__isl_take isl_space *dim,
969 enum isl_dim_type type, unsigned pos, unsigned n)
971 isl_id **ids = NULL;
973 if (!dim)
974 return NULL;
975 if (n == 0)
976 return isl_space_reset(dim, type);
978 if (!valid_dim_type(type))
979 isl_die(dim->ctx, isl_error_invalid,
980 "cannot insert dimensions of specified type",
981 goto error);
983 isl_assert(dim->ctx, pos <= isl_space_dim(dim, type), goto error);
985 dim = isl_space_cow(dim);
986 if (!dim)
987 return NULL;
989 if (dim->ids) {
990 enum isl_dim_type t, o = isl_dim_param;
991 int off;
992 int s[3];
993 ids = isl_calloc_array(dim->ctx, isl_id *,
994 dim->nparam + dim->n_in + dim->n_out + n);
995 if (!ids)
996 goto error;
997 off = 0;
998 s[isl_dim_param - o] = dim->nparam;
999 s[isl_dim_in - o] = dim->n_in;
1000 s[isl_dim_out - o] = dim->n_out;
1001 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
1002 if (t != type) {
1003 get_ids(dim, t, 0, s[t - o], ids + off);
1004 off += s[t - o];
1005 } else {
1006 get_ids(dim, t, 0, pos, ids + off);
1007 off += pos + n;
1008 get_ids(dim, t, pos, s[t - o] - pos, ids + off);
1009 off += s[t - o] - pos;
1012 free(dim->ids);
1013 dim->ids = ids;
1014 dim->n_id = dim->nparam + dim->n_in + dim->n_out + n;
1016 switch (type) {
1017 case isl_dim_param: dim->nparam += n; break;
1018 case isl_dim_in: dim->n_in += n; break;
1019 case isl_dim_out: dim->n_out += n; break;
1020 default: ;
1022 dim = isl_space_reset(dim, type);
1024 if (type == isl_dim_param) {
1025 if (dim && dim->nested[0] &&
1026 !(dim->nested[0] = isl_space_insert_dims(dim->nested[0],
1027 isl_dim_param, pos, n)))
1028 goto error;
1029 if (dim && dim->nested[1] &&
1030 !(dim->nested[1] = isl_space_insert_dims(dim->nested[1],
1031 isl_dim_param, pos, n)))
1032 goto error;
1035 return dim;
1036 error:
1037 isl_space_free(dim);
1038 return NULL;
1041 __isl_give isl_space *isl_space_move_dims(__isl_take isl_space *dim,
1042 enum isl_dim_type dst_type, unsigned dst_pos,
1043 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
1045 int i;
1047 if (!dim)
1048 return NULL;
1049 if (n == 0) {
1050 dim = isl_space_reset(dim, src_type);
1051 return isl_space_reset(dim, dst_type);
1054 isl_assert(dim->ctx, src_pos + n <= isl_space_dim(dim, src_type),
1055 goto error);
1057 if (dst_type == src_type && dst_pos == src_pos)
1058 return dim;
1060 isl_assert(dim->ctx, dst_type != src_type, goto error);
1062 dim = isl_space_reset(dim, src_type);
1063 dim = isl_space_reset(dim, dst_type);
1065 dim = isl_space_cow(dim);
1066 if (!dim)
1067 return NULL;
1069 if (dim->ids) {
1070 isl_id **ids;
1071 enum isl_dim_type t, o = isl_dim_param;
1072 int off;
1073 int s[3];
1074 ids = isl_calloc_array(dim->ctx, isl_id *,
1075 dim->nparam + dim->n_in + dim->n_out);
1076 if (!ids)
1077 goto error;
1078 off = 0;
1079 s[isl_dim_param - o] = dim->nparam;
1080 s[isl_dim_in - o] = dim->n_in;
1081 s[isl_dim_out - o] = dim->n_out;
1082 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
1083 if (t == dst_type) {
1084 get_ids(dim, t, 0, dst_pos, ids + off);
1085 off += dst_pos;
1086 get_ids(dim, src_type, src_pos, n, ids + off);
1087 off += n;
1088 get_ids(dim, t, dst_pos, s[t - o] - dst_pos,
1089 ids + off);
1090 off += s[t - o] - dst_pos;
1091 } else if (t == src_type) {
1092 get_ids(dim, t, 0, src_pos, ids + off);
1093 off += src_pos;
1094 get_ids(dim, t, src_pos + n,
1095 s[t - o] - src_pos - n, ids + off);
1096 off += s[t - o] - src_pos - n;
1097 } else {
1098 get_ids(dim, t, 0, s[t - o], ids + off);
1099 off += s[t - o];
1102 free(dim->ids);
1103 dim->ids = ids;
1104 dim->n_id = dim->nparam + dim->n_in + dim->n_out;
1107 switch (dst_type) {
1108 case isl_dim_param: dim->nparam += n; break;
1109 case isl_dim_in: dim->n_in += n; break;
1110 case isl_dim_out: dim->n_out += n; break;
1111 default: ;
1114 switch (src_type) {
1115 case isl_dim_param: dim->nparam -= n; break;
1116 case isl_dim_in: dim->n_in -= n; break;
1117 case isl_dim_out: dim->n_out -= n; break;
1118 default: ;
1121 if (dst_type != isl_dim_param && src_type != isl_dim_param)
1122 return dim;
1124 for (i = 0; i < 2; ++i) {
1125 if (!dim->nested[i])
1126 continue;
1127 dim->nested[i] = isl_space_replace(dim->nested[i],
1128 isl_dim_param, dim);
1129 if (!dim->nested[i])
1130 goto error;
1133 return dim;
1134 error:
1135 isl_space_free(dim);
1136 return NULL;
1139 __isl_give isl_space *isl_space_join(__isl_take isl_space *left,
1140 __isl_take isl_space *right)
1142 isl_space *dim;
1144 if (!left || !right)
1145 goto error;
1147 isl_assert(left->ctx, match(left, isl_dim_param, right, isl_dim_param),
1148 goto error);
1149 isl_assert(left->ctx,
1150 isl_space_tuple_is_equal(left, isl_dim_out, right, isl_dim_in),
1151 goto error);
1153 dim = isl_space_alloc(left->ctx, left->nparam, left->n_in, right->n_out);
1154 if (!dim)
1155 goto error;
1157 dim = copy_ids(dim, isl_dim_param, 0, left, isl_dim_param);
1158 dim = copy_ids(dim, isl_dim_in, 0, left, isl_dim_in);
1159 dim = copy_ids(dim, isl_dim_out, 0, right, isl_dim_out);
1161 if (dim && left->tuple_id[0] &&
1162 !(dim->tuple_id[0] = isl_id_copy(left->tuple_id[0])))
1163 goto error;
1164 if (dim && right->tuple_id[1] &&
1165 !(dim->tuple_id[1] = isl_id_copy(right->tuple_id[1])))
1166 goto error;
1167 if (dim && left->nested[0] &&
1168 !(dim->nested[0] = isl_space_copy(left->nested[0])))
1169 goto error;
1170 if (dim && right->nested[1] &&
1171 !(dim->nested[1] = isl_space_copy(right->nested[1])))
1172 goto error;
1174 isl_space_free(left);
1175 isl_space_free(right);
1177 return dim;
1178 error:
1179 isl_space_free(left);
1180 isl_space_free(right);
1181 return NULL;
1184 /* Given two map spaces { A -> C } and { B -> D }, construct the space
1185 * { [A -> B] -> [C -> D] }.
1186 * Given two set spaces { A } and { B }, construct the space { [A -> B] }.
1188 __isl_give isl_space *isl_space_product(__isl_take isl_space *left,
1189 __isl_take isl_space *right)
1191 isl_space *dom1, *dom2, *nest1, *nest2;
1192 int is_set;
1194 if (!left || !right)
1195 goto error;
1197 is_set = isl_space_is_set(left);
1198 if (is_set != isl_space_is_set(right))
1199 isl_die(isl_space_get_ctx(left), isl_error_invalid,
1200 "expecting either two set spaces or two map spaces",
1201 goto error);
1202 if (is_set)
1203 return isl_space_range_product(left, right);
1205 isl_assert(left->ctx, match(left, isl_dim_param, right, isl_dim_param),
1206 goto error);
1208 dom1 = isl_space_domain(isl_space_copy(left));
1209 dom2 = isl_space_domain(isl_space_copy(right));
1210 nest1 = isl_space_wrap(isl_space_join(isl_space_reverse(dom1), dom2));
1212 dom1 = isl_space_range(left);
1213 dom2 = isl_space_range(right);
1214 nest2 = isl_space_wrap(isl_space_join(isl_space_reverse(dom1), dom2));
1216 return isl_space_join(isl_space_reverse(nest1), nest2);
1217 error:
1218 isl_space_free(left);
1219 isl_space_free(right);
1220 return NULL;
1223 /* Given two spaces { A -> C } and { B -> C }, construct the space
1224 * { [A -> B] -> C }
1226 __isl_give isl_space *isl_space_domain_product(__isl_take isl_space *left,
1227 __isl_take isl_space *right)
1229 isl_space *ran, *dom1, *dom2, *nest;
1231 if (!left || !right)
1232 goto error;
1234 if (!match(left, isl_dim_param, right, isl_dim_param))
1235 isl_die(left->ctx, isl_error_invalid,
1236 "parameters need to match", goto error);
1237 if (!isl_space_tuple_is_equal(left, isl_dim_out, right, isl_dim_out))
1238 isl_die(left->ctx, isl_error_invalid,
1239 "ranges need to match", goto error);
1241 ran = isl_space_range(isl_space_copy(left));
1243 dom1 = isl_space_domain(left);
1244 dom2 = isl_space_domain(right);
1245 nest = isl_space_wrap(isl_space_join(isl_space_reverse(dom1), dom2));
1247 return isl_space_join(isl_space_reverse(nest), ran);
1248 error:
1249 isl_space_free(left);
1250 isl_space_free(right);
1251 return NULL;
1254 __isl_give isl_space *isl_space_range_product(__isl_take isl_space *left,
1255 __isl_take isl_space *right)
1257 isl_space *dom, *ran1, *ran2, *nest;
1259 if (!left || !right)
1260 goto error;
1262 isl_assert(left->ctx, match(left, isl_dim_param, right, isl_dim_param),
1263 goto error);
1264 if (!isl_space_tuple_is_equal(left, isl_dim_in, right, isl_dim_in))
1265 isl_die(left->ctx, isl_error_invalid,
1266 "domains need to match", goto error);
1268 dom = isl_space_domain(isl_space_copy(left));
1270 ran1 = isl_space_range(left);
1271 ran2 = isl_space_range(right);
1272 nest = isl_space_wrap(isl_space_join(isl_space_reverse(ran1), ran2));
1274 return isl_space_join(isl_space_reverse(dom), nest);
1275 error:
1276 isl_space_free(left);
1277 isl_space_free(right);
1278 return NULL;
1281 /* Given a space of the form [A -> B] -> C, return the space A -> C.
1283 __isl_give isl_space *isl_space_domain_factor_domain(
1284 __isl_take isl_space *space)
1286 isl_space *nested;
1287 isl_space *domain;
1289 if (!space)
1290 return NULL;
1291 if (!isl_space_domain_is_wrapping(space))
1292 isl_die(isl_space_get_ctx(space), isl_error_invalid,
1293 "domain not a product", return isl_space_free(space));
1295 nested = space->nested[0];
1296 domain = isl_space_copy(space);
1297 domain = isl_space_drop_dims(space, isl_dim_in,
1298 nested->n_in, nested->n_out);
1299 if (!domain)
1300 return isl_space_free(space);
1301 if (nested->tuple_id[0]) {
1302 domain->tuple_id[0] = isl_id_copy(nested->tuple_id[0]);
1303 if (!domain->tuple_id[0])
1304 goto error;
1306 if (nested->nested[0]) {
1307 domain->nested[0] = isl_space_copy(nested->nested[0]);
1308 if (!domain->nested[0])
1309 goto error;
1312 isl_space_free(space);
1313 return domain;
1314 error:
1315 isl_space_free(space);
1316 isl_space_free(domain);
1317 return NULL;
1320 /* Given a space of the form A -> [B -> C], return the space A -> B.
1322 __isl_give isl_space *isl_space_range_factor_domain(
1323 __isl_take isl_space *space)
1325 isl_space *nested;
1326 isl_space *domain;
1328 if (!space)
1329 return NULL;
1330 if (!isl_space_range_is_wrapping(space))
1331 isl_die(isl_space_get_ctx(space), isl_error_invalid,
1332 "range not a product", return isl_space_free(space));
1334 nested = space->nested[1];
1335 domain = isl_space_copy(space);
1336 domain = isl_space_drop_dims(space, isl_dim_out,
1337 nested->n_in, nested->n_out);
1338 if (!domain)
1339 return isl_space_free(space);
1340 if (nested->tuple_id[0]) {
1341 domain->tuple_id[1] = isl_id_copy(nested->tuple_id[0]);
1342 if (!domain->tuple_id[1])
1343 goto error;
1345 if (nested->nested[0]) {
1346 domain->nested[1] = isl_space_copy(nested->nested[0]);
1347 if (!domain->nested[1])
1348 goto error;
1351 isl_space_free(space);
1352 return domain;
1353 error:
1354 isl_space_free(space);
1355 isl_space_free(domain);
1356 return NULL;
1359 /* Given a space of the form A -> [B -> C], return the space A -> C.
1361 __isl_give isl_space *isl_space_range_factor_range(
1362 __isl_take isl_space *space)
1364 isl_space *nested;
1365 isl_space *range;
1367 if (!space)
1368 return NULL;
1369 if (!isl_space_range_is_wrapping(space))
1370 isl_die(isl_space_get_ctx(space), isl_error_invalid,
1371 "range not a product", return isl_space_free(space));
1373 nested = space->nested[1];
1374 range = isl_space_copy(space);
1375 range = isl_space_drop_dims(space, isl_dim_out, 0, nested->n_in);
1376 if (!range)
1377 return isl_space_free(space);
1378 if (nested->tuple_id[1]) {
1379 range->tuple_id[1] = isl_id_copy(nested->tuple_id[1]);
1380 if (!range->tuple_id[1])
1381 goto error;
1383 if (nested->nested[1]) {
1384 range->nested[1] = isl_space_copy(nested->nested[1]);
1385 if (!range->nested[1])
1386 goto error;
1389 isl_space_free(space);
1390 return range;
1391 error:
1392 isl_space_free(space);
1393 isl_space_free(range);
1394 return NULL;
1397 __isl_give isl_space *isl_space_map_from_set(__isl_take isl_space *dim)
1399 isl_ctx *ctx;
1400 isl_id **ids = NULL;
1402 if (!dim)
1403 return NULL;
1404 ctx = isl_space_get_ctx(dim);
1405 if (!isl_space_is_set(dim))
1406 isl_die(ctx, isl_error_invalid, "not a set space", goto error);
1407 dim = isl_space_cow(dim);
1408 if (!dim)
1409 return NULL;
1410 if (dim->ids) {
1411 ids = isl_calloc_array(dim->ctx, isl_id *,
1412 dim->nparam + dim->n_out + dim->n_out);
1413 if (!ids)
1414 goto error;
1415 get_ids(dim, isl_dim_param, 0, dim->nparam, ids);
1416 get_ids(dim, isl_dim_out, 0, dim->n_out, ids + dim->nparam);
1418 dim->n_in = dim->n_out;
1419 if (ids) {
1420 free(dim->ids);
1421 dim->ids = ids;
1422 dim->n_id = dim->nparam + dim->n_out + dim->n_out;
1423 dim = copy_ids(dim, isl_dim_out, 0, dim, isl_dim_in);
1425 isl_id_free(dim->tuple_id[0]);
1426 dim->tuple_id[0] = isl_id_copy(dim->tuple_id[1]);
1427 isl_space_free(dim->nested[0]);
1428 dim->nested[0] = isl_space_copy(dim->nested[1]);
1429 return dim;
1430 error:
1431 isl_space_free(dim);
1432 return NULL;
1435 __isl_give isl_space *isl_space_map_from_domain_and_range(
1436 __isl_take isl_space *domain, __isl_take isl_space *range)
1438 if (!domain || !range)
1439 goto error;
1440 if (!isl_space_is_set(domain))
1441 isl_die(isl_space_get_ctx(domain), isl_error_invalid,
1442 "domain is not a set space", goto error);
1443 if (!isl_space_is_set(range))
1444 isl_die(isl_space_get_ctx(range), isl_error_invalid,
1445 "range is not a set space", goto error);
1446 return isl_space_join(isl_space_reverse(domain), range);
1447 error:
1448 isl_space_free(domain);
1449 isl_space_free(range);
1450 return NULL;
1453 static __isl_give isl_space *set_ids(__isl_take isl_space *dim,
1454 enum isl_dim_type type,
1455 unsigned first, unsigned n, __isl_take isl_id **ids)
1457 int i;
1459 for (i = 0; i < n ; ++i)
1460 dim = set_id(dim, type, first + i, ids[i]);
1462 return dim;
1465 __isl_give isl_space *isl_space_reverse(__isl_take isl_space *dim)
1467 unsigned t;
1468 isl_space *nested;
1469 isl_id **ids = NULL;
1470 isl_id *id;
1472 if (!dim)
1473 return NULL;
1474 if (match(dim, isl_dim_in, dim, isl_dim_out))
1475 return dim;
1477 dim = isl_space_cow(dim);
1478 if (!dim)
1479 return NULL;
1481 id = dim->tuple_id[0];
1482 dim->tuple_id[0] = dim->tuple_id[1];
1483 dim->tuple_id[1] = id;
1485 nested = dim->nested[0];
1486 dim->nested[0] = dim->nested[1];
1487 dim->nested[1] = nested;
1489 if (dim->ids) {
1490 int n_id = dim->n_in + dim->n_out;
1491 ids = isl_alloc_array(dim->ctx, isl_id *, n_id);
1492 if (n_id && !ids)
1493 goto error;
1494 get_ids(dim, isl_dim_in, 0, dim->n_in, ids);
1495 get_ids(dim, isl_dim_out, 0, dim->n_out, ids + dim->n_in);
1498 t = dim->n_in;
1499 dim->n_in = dim->n_out;
1500 dim->n_out = t;
1502 if (dim->ids) {
1503 dim = set_ids(dim, isl_dim_out, 0, dim->n_out, ids);
1504 dim = set_ids(dim, isl_dim_in, 0, dim->n_in, ids + dim->n_out);
1505 free(ids);
1508 return dim;
1509 error:
1510 free(ids);
1511 isl_space_free(dim);
1512 return NULL;
1515 __isl_give isl_space *isl_space_drop_dims(__isl_take isl_space *dim,
1516 enum isl_dim_type type, unsigned first, unsigned num)
1518 int i;
1520 if (!dim)
1521 return NULL;
1523 if (num == 0)
1524 return isl_space_reset(dim, type);
1526 if (!valid_dim_type(type))
1527 isl_die(dim->ctx, isl_error_invalid,
1528 "cannot drop dimensions of specified type", goto error);
1530 if (first + num > n(dim, type) || first + num < first)
1531 isl_die(isl_space_get_ctx(dim), isl_error_invalid,
1532 "index out of bounds", return isl_space_free(dim));
1533 dim = isl_space_cow(dim);
1534 if (!dim)
1535 goto error;
1536 if (dim->ids) {
1537 dim = extend_ids(dim);
1538 if (!dim)
1539 goto error;
1540 for (i = 0; i < num; ++i)
1541 isl_id_free(get_id(dim, type, first + i));
1542 for (i = first+num; i < n(dim, type); ++i)
1543 set_id(dim, type, i - num, get_id(dim, type, i));
1544 switch (type) {
1545 case isl_dim_param:
1546 get_ids(dim, isl_dim_in, 0, dim->n_in,
1547 dim->ids + offset(dim, isl_dim_in) - num);
1548 case isl_dim_in:
1549 get_ids(dim, isl_dim_out, 0, dim->n_out,
1550 dim->ids + offset(dim, isl_dim_out) - num);
1551 default:
1554 dim->n_id -= num;
1556 switch (type) {
1557 case isl_dim_param: dim->nparam -= num; break;
1558 case isl_dim_in: dim->n_in -= num; break;
1559 case isl_dim_out: dim->n_out -= num; break;
1560 default: ;
1562 dim = isl_space_reset(dim, type);
1563 if (type == isl_dim_param) {
1564 if (dim && dim->nested[0] &&
1565 !(dim->nested[0] = isl_space_drop_dims(dim->nested[0],
1566 isl_dim_param, first, num)))
1567 goto error;
1568 if (dim && dim->nested[1] &&
1569 !(dim->nested[1] = isl_space_drop_dims(dim->nested[1],
1570 isl_dim_param, first, num)))
1571 goto error;
1573 return dim;
1574 error:
1575 isl_space_free(dim);
1576 return NULL;
1579 __isl_give isl_space *isl_space_drop_inputs(__isl_take isl_space *dim,
1580 unsigned first, unsigned n)
1582 if (!dim)
1583 return NULL;
1584 return isl_space_drop_dims(dim, isl_dim_in, first, n);
1587 __isl_give isl_space *isl_space_drop_outputs(__isl_take isl_space *dim,
1588 unsigned first, unsigned n)
1590 if (!dim)
1591 return NULL;
1592 return isl_space_drop_dims(dim, isl_dim_out, first, n);
1595 __isl_give isl_space *isl_space_domain(__isl_take isl_space *dim)
1597 if (!dim)
1598 return NULL;
1599 dim = isl_space_drop_outputs(dim, 0, dim->n_out);
1600 dim = isl_space_reverse(dim);
1601 dim = mark_as_set(dim);
1602 return dim;
1605 __isl_give isl_space *isl_space_from_domain(__isl_take isl_space *dim)
1607 if (!dim)
1608 return NULL;
1609 if (!isl_space_is_set(dim))
1610 isl_die(isl_space_get_ctx(dim), isl_error_invalid,
1611 "not a set space", goto error);
1612 dim = isl_space_reverse(dim);
1613 dim = isl_space_reset(dim, isl_dim_out);
1614 return dim;
1615 error:
1616 isl_space_free(dim);
1617 return NULL;
1620 __isl_give isl_space *isl_space_range(__isl_take isl_space *dim)
1622 if (!dim)
1623 return NULL;
1624 dim = isl_space_drop_inputs(dim, 0, dim->n_in);
1625 dim = mark_as_set(dim);
1626 return dim;
1629 __isl_give isl_space *isl_space_from_range(__isl_take isl_space *dim)
1631 if (!dim)
1632 return NULL;
1633 if (!isl_space_is_set(dim))
1634 isl_die(isl_space_get_ctx(dim), isl_error_invalid,
1635 "not a set space", goto error);
1636 return isl_space_reset(dim, isl_dim_in);
1637 error:
1638 isl_space_free(dim);
1639 return NULL;
1642 /* Given a map space A -> B, return the map space [A -> B] -> A.
1644 __isl_give isl_space *isl_space_domain_map(__isl_take isl_space *space)
1646 isl_space *domain;
1648 domain = isl_space_from_range(isl_space_domain(isl_space_copy(space)));
1649 space = isl_space_from_domain(isl_space_wrap(space));
1650 space = isl_space_join(space, domain);
1652 return space;
1655 /* Given a map space A -> B, return the map space [A -> B] -> B.
1657 __isl_give isl_space *isl_space_range_map(__isl_take isl_space *space)
1659 isl_space *range;
1661 range = isl_space_from_range(isl_space_range(isl_space_copy(space)));
1662 space = isl_space_from_domain(isl_space_wrap(space));
1663 space = isl_space_join(space, range);
1665 return space;
1668 __isl_give isl_space *isl_space_params(__isl_take isl_space *space)
1670 if (isl_space_is_params(space))
1671 return space;
1672 space = isl_space_drop_dims(space,
1673 isl_dim_in, 0, isl_space_dim(space, isl_dim_in));
1674 space = isl_space_drop_dims(space,
1675 isl_dim_out, 0, isl_space_dim(space, isl_dim_out));
1676 space = mark_as_params(space);
1677 return space;
1680 __isl_give isl_space *isl_space_set_from_params(__isl_take isl_space *space)
1682 if (!space)
1683 return NULL;
1684 if (!isl_space_is_params(space))
1685 isl_die(isl_space_get_ctx(space), isl_error_invalid,
1686 "not a parameter space", goto error);
1687 return isl_space_reset(space, isl_dim_set);
1688 error:
1689 isl_space_free(space);
1690 return NULL;
1693 __isl_give isl_space *isl_space_as_set_space(__isl_take isl_space *dim)
1695 dim = isl_space_cow(dim);
1696 if (!dim)
1697 return NULL;
1699 dim->n_out += dim->n_in;
1700 dim->n_in = 0;
1701 dim = isl_space_reset(dim, isl_dim_in);
1702 dim = isl_space_reset(dim, isl_dim_out);
1704 return dim;
1707 __isl_give isl_space *isl_space_underlying(__isl_take isl_space *dim,
1708 unsigned n_div)
1710 int i;
1712 if (!dim)
1713 return NULL;
1714 if (n_div == 0 &&
1715 dim->nparam == 0 && dim->n_in == 0 && dim->n_id == 0)
1716 return isl_space_reset(isl_space_reset(dim, isl_dim_in), isl_dim_out);
1717 dim = isl_space_cow(dim);
1718 if (!dim)
1719 return NULL;
1720 dim->n_out += dim->nparam + dim->n_in + n_div;
1721 dim->nparam = 0;
1722 dim->n_in = 0;
1724 for (i = 0; i < dim->n_id; ++i)
1725 isl_id_free(get_id(dim, isl_dim_out, i));
1726 dim->n_id = 0;
1727 dim = isl_space_reset(dim, isl_dim_in);
1728 dim = isl_space_reset(dim, isl_dim_out);
1730 return dim;
1733 /* Are the two spaces the same, including positions and names of parameters?
1735 int isl_space_is_equal(__isl_keep isl_space *dim1, __isl_keep isl_space *dim2)
1737 if (!dim1 || !dim2)
1738 return -1;
1739 if (dim1 == dim2)
1740 return 1;
1741 return match(dim1, isl_dim_param, dim2, isl_dim_param) &&
1742 isl_space_tuple_is_equal(dim1, isl_dim_in, dim2, isl_dim_in) &&
1743 isl_space_tuple_is_equal(dim1, isl_dim_out, dim2, isl_dim_out);
1746 /* Is space1 equal to the domain of space2?
1748 * In the internal version we also allow space2 to be the space of a set,
1749 * provided space1 is a parameter space.
1751 int isl_space_is_domain_internal(__isl_keep isl_space *space1,
1752 __isl_keep isl_space *space2)
1754 if (!space1 || !space2)
1755 return -1;
1756 if (!isl_space_is_set(space1))
1757 return 0;
1758 return match(space1, isl_dim_param, space2, isl_dim_param) &&
1759 isl_space_tuple_is_equal(space1, isl_dim_set,
1760 space2, isl_dim_in);
1763 /* Is space1 equal to the domain of space2?
1765 int isl_space_is_domain(__isl_keep isl_space *space1,
1766 __isl_keep isl_space *space2)
1768 if (!space2)
1769 return -1;
1770 if (!isl_space_is_map(space2))
1771 return 0;
1772 return isl_space_is_domain_internal(space1, space2);
1775 /* Is space1 equal to the range of space2?
1777 * In the internal version, space2 is allowed to be the space of a set,
1778 * in which case it should be equal to space1.
1780 int isl_space_is_range_internal(__isl_keep isl_space *space1,
1781 __isl_keep isl_space *space2)
1783 if (!space1 || !space2)
1784 return -1;
1785 if (!isl_space_is_set(space1))
1786 return 0;
1787 return match(space1, isl_dim_param, space2, isl_dim_param) &&
1788 isl_space_tuple_is_equal(space1, isl_dim_set,
1789 space2, isl_dim_out);
1792 /* Is space1 equal to the range of space2?
1794 int isl_space_is_range(__isl_keep isl_space *space1,
1795 __isl_keep isl_space *space2)
1797 if (!space2)
1798 return -1;
1799 if (!isl_space_is_map(space2))
1800 return 0;
1801 return isl_space_is_range_internal(space1, space2);
1804 int isl_space_compatible(__isl_keep isl_space *dim1,
1805 __isl_keep isl_space *dim2)
1807 return dim1->nparam == dim2->nparam &&
1808 dim1->n_in + dim1->n_out == dim2->n_in + dim2->n_out;
1811 static uint32_t isl_hash_dim(uint32_t hash, __isl_keep isl_space *dim)
1813 int i;
1814 isl_id *id;
1816 if (!dim)
1817 return hash;
1819 isl_hash_byte(hash, dim->nparam % 256);
1820 isl_hash_byte(hash, dim->n_in % 256);
1821 isl_hash_byte(hash, dim->n_out % 256);
1823 for (i = 0; i < dim->nparam; ++i) {
1824 id = get_id(dim, isl_dim_param, i);
1825 hash = isl_hash_id(hash, id);
1828 id = tuple_id(dim, isl_dim_in);
1829 hash = isl_hash_id(hash, id);
1830 id = tuple_id(dim, isl_dim_out);
1831 hash = isl_hash_id(hash, id);
1833 hash = isl_hash_dim(hash, dim->nested[0]);
1834 hash = isl_hash_dim(hash, dim->nested[1]);
1836 return hash;
1839 uint32_t isl_space_get_hash(__isl_keep isl_space *dim)
1841 uint32_t hash;
1843 if (!dim)
1844 return 0;
1846 hash = isl_hash_init();
1847 hash = isl_hash_dim(hash, dim);
1849 return hash;
1852 int isl_space_is_wrapping(__isl_keep isl_space *dim)
1854 if (!dim)
1855 return -1;
1857 if (!isl_space_is_set(dim))
1858 return 0;
1860 return dim->nested[1] != NULL;
1863 /* Is "space" the space of a map where the domain is a wrapped map space?
1865 int isl_space_domain_is_wrapping(__isl_keep isl_space *space)
1867 if (!space)
1868 return -1;
1870 if (isl_space_is_set(space))
1871 return 0;
1873 return space->nested[0] != NULL;
1876 /* Is "space" the space of a map where the range is a wrapped map space?
1878 int isl_space_range_is_wrapping(__isl_keep isl_space *space)
1880 if (!space)
1881 return -1;
1883 if (isl_space_is_set(space))
1884 return 0;
1886 return space->nested[1] != NULL;
1889 __isl_give isl_space *isl_space_wrap(__isl_take isl_space *dim)
1891 isl_space *wrap;
1893 if (!dim)
1894 return NULL;
1896 wrap = isl_space_set_alloc(dim->ctx,
1897 dim->nparam, dim->n_in + dim->n_out);
1899 wrap = copy_ids(wrap, isl_dim_param, 0, dim, isl_dim_param);
1900 wrap = copy_ids(wrap, isl_dim_set, 0, dim, isl_dim_in);
1901 wrap = copy_ids(wrap, isl_dim_set, dim->n_in, dim, isl_dim_out);
1903 if (!wrap)
1904 goto error;
1906 wrap->nested[1] = dim;
1908 return wrap;
1909 error:
1910 isl_space_free(dim);
1911 return NULL;
1914 __isl_give isl_space *isl_space_unwrap(__isl_take isl_space *dim)
1916 isl_space *unwrap;
1918 if (!dim)
1919 return NULL;
1921 if (!isl_space_is_wrapping(dim))
1922 isl_die(dim->ctx, isl_error_invalid, "not a wrapping space",
1923 goto error);
1925 unwrap = isl_space_copy(dim->nested[1]);
1926 isl_space_free(dim);
1928 return unwrap;
1929 error:
1930 isl_space_free(dim);
1931 return NULL;
1934 int isl_space_is_named_or_nested(__isl_keep isl_space *dim, enum isl_dim_type type)
1936 if (type != isl_dim_in && type != isl_dim_out)
1937 return 0;
1938 if (!dim)
1939 return -1;
1940 if (dim->tuple_id[type - isl_dim_in])
1941 return 1;
1942 if (dim->nested[type - isl_dim_in])
1943 return 1;
1944 return 0;
1947 int isl_space_may_be_set(__isl_keep isl_space *dim)
1949 if (!dim)
1950 return -1;
1951 if (isl_space_is_set(dim))
1952 return 1;
1953 if (isl_space_dim(dim, isl_dim_in) != 0)
1954 return 0;
1955 if (isl_space_is_named_or_nested(dim, isl_dim_in))
1956 return 0;
1957 return 1;
1960 __isl_give isl_space *isl_space_reset(__isl_take isl_space *dim,
1961 enum isl_dim_type type)
1963 if (!isl_space_is_named_or_nested(dim, type))
1964 return dim;
1966 dim = isl_space_cow(dim);
1967 if (!dim)
1968 return NULL;
1970 isl_id_free(dim->tuple_id[type - isl_dim_in]);
1971 dim->tuple_id[type - isl_dim_in] = NULL;
1972 isl_space_free(dim->nested[type - isl_dim_in]);
1973 dim->nested[type - isl_dim_in] = NULL;
1975 return dim;
1978 __isl_give isl_space *isl_space_flatten(__isl_take isl_space *dim)
1980 if (!dim)
1981 return NULL;
1982 if (!dim->nested[0] && !dim->nested[1])
1983 return dim;
1985 if (dim->nested[0])
1986 dim = isl_space_reset(dim, isl_dim_in);
1987 if (dim && dim->nested[1])
1988 dim = isl_space_reset(dim, isl_dim_out);
1990 return dim;
1993 __isl_give isl_space *isl_space_flatten_domain(__isl_take isl_space *dim)
1995 if (!dim)
1996 return NULL;
1997 if (!dim->nested[0])
1998 return dim;
2000 return isl_space_reset(dim, isl_dim_in);
2003 __isl_give isl_space *isl_space_flatten_range(__isl_take isl_space *dim)
2005 if (!dim)
2006 return NULL;
2007 if (!dim->nested[1])
2008 return dim;
2010 return isl_space_reset(dim, isl_dim_out);
2013 /* Replace the dimensions of the given type of dst by those of src.
2015 __isl_give isl_space *isl_space_replace(__isl_take isl_space *dst,
2016 enum isl_dim_type type, __isl_keep isl_space *src)
2018 dst = isl_space_cow(dst);
2020 if (!dst || !src)
2021 goto error;
2023 dst = isl_space_drop_dims(dst, type, 0, isl_space_dim(dst, type));
2024 dst = isl_space_add_dims(dst, type, isl_space_dim(src, type));
2025 dst = copy_ids(dst, type, 0, src, type);
2027 if (dst && type == isl_dim_param) {
2028 int i;
2029 for (i = 0; i <= 1; ++i) {
2030 if (!dst->nested[i])
2031 continue;
2032 dst->nested[i] = isl_space_replace(dst->nested[i],
2033 type, src);
2034 if (!dst->nested[i])
2035 goto error;
2039 return dst;
2040 error:
2041 isl_space_free(dst);
2042 return NULL;
2045 /* Given a dimension specification "dim" of a set, create a dimension
2046 * specification for the lift of the set. In particular, the result
2047 * is of the form [dim -> local[..]], with n_local variables in the
2048 * range of the wrapped map.
2050 __isl_give isl_space *isl_space_lift(__isl_take isl_space *dim, unsigned n_local)
2052 isl_space *local_dim;
2054 if (!dim)
2055 return NULL;
2057 local_dim = isl_space_dup(dim);
2058 local_dim = isl_space_drop_dims(local_dim, isl_dim_set, 0, dim->n_out);
2059 local_dim = isl_space_add_dims(local_dim, isl_dim_set, n_local);
2060 local_dim = isl_space_set_tuple_name(local_dim, isl_dim_set, "local");
2061 dim = isl_space_join(isl_space_from_domain(dim),
2062 isl_space_from_range(local_dim));
2063 dim = isl_space_wrap(dim);
2064 dim = isl_space_set_tuple_name(dim, isl_dim_set, "lifted");
2066 return dim;
2069 int isl_space_can_zip(__isl_keep isl_space *dim)
2071 if (!dim)
2072 return -1;
2074 return dim->nested[0] && dim->nested[1];
2077 __isl_give isl_space *isl_space_zip(__isl_take isl_space *dim)
2079 isl_space *dom, *ran;
2080 isl_space *dom_dom, *dom_ran, *ran_dom, *ran_ran;
2082 if (!isl_space_can_zip(dim))
2083 isl_die(dim->ctx, isl_error_invalid, "dim cannot be zipped",
2084 goto error);
2086 if (!dim)
2087 return NULL;
2088 dom = isl_space_unwrap(isl_space_domain(isl_space_copy(dim)));
2089 ran = isl_space_unwrap(isl_space_range(dim));
2090 dom_dom = isl_space_domain(isl_space_copy(dom));
2091 dom_ran = isl_space_range(dom);
2092 ran_dom = isl_space_domain(isl_space_copy(ran));
2093 ran_ran = isl_space_range(ran);
2094 dom = isl_space_join(isl_space_from_domain(dom_dom),
2095 isl_space_from_range(ran_dom));
2096 ran = isl_space_join(isl_space_from_domain(dom_ran),
2097 isl_space_from_range(ran_ran));
2098 return isl_space_join(isl_space_from_domain(isl_space_wrap(dom)),
2099 isl_space_from_range(isl_space_wrap(ran)));
2100 error:
2101 isl_space_free(dim);
2102 return NULL;
2105 /* Can we apply isl_space_curry to "space"?
2106 * That is, does it have a nested relation in its domain?
2108 int isl_space_can_curry(__isl_keep isl_space *space)
2110 if (!space)
2111 return -1;
2113 return !!space->nested[0];
2116 /* Given a space (A -> B) -> C, return the corresponding space
2117 * A -> (B -> C).
2119 __isl_give isl_space *isl_space_curry(__isl_take isl_space *space)
2121 isl_space *dom, *ran;
2122 isl_space *dom_dom, *dom_ran;
2124 if (!space)
2125 return NULL;
2127 if (!isl_space_can_curry(space))
2128 isl_die(space->ctx, isl_error_invalid,
2129 "space cannot be curried", goto error);
2131 dom = isl_space_unwrap(isl_space_domain(isl_space_copy(space)));
2132 ran = isl_space_range(space);
2133 dom_dom = isl_space_domain(isl_space_copy(dom));
2134 dom_ran = isl_space_range(dom);
2135 ran = isl_space_join(isl_space_from_domain(dom_ran),
2136 isl_space_from_range(ran));
2137 return isl_space_join(isl_space_from_domain(dom_dom),
2138 isl_space_from_range(isl_space_wrap(ran)));
2139 error:
2140 isl_space_free(space);
2141 return NULL;
2144 /* Can we apply isl_space_uncurry to "space"?
2145 * That is, does it have a nested relation in its range?
2147 int isl_space_can_uncurry(__isl_keep isl_space *space)
2149 if (!space)
2150 return -1;
2152 return !!space->nested[1];
2155 /* Given a space A -> (B -> C), return the corresponding space
2156 * (A -> B) -> C.
2158 __isl_give isl_space *isl_space_uncurry(__isl_take isl_space *space)
2160 isl_space *dom, *ran;
2161 isl_space *ran_dom, *ran_ran;
2163 if (!space)
2164 return NULL;
2166 if (!isl_space_can_uncurry(space))
2167 isl_die(space->ctx, isl_error_invalid,
2168 "space cannot be uncurried",
2169 return isl_space_free(space));
2171 dom = isl_space_domain(isl_space_copy(space));
2172 ran = isl_space_unwrap(isl_space_range(space));
2173 ran_dom = isl_space_domain(isl_space_copy(ran));
2174 ran_ran = isl_space_range(ran);
2175 dom = isl_space_join(isl_space_from_domain(dom),
2176 isl_space_from_range(ran_dom));
2177 return isl_space_join(isl_space_from_domain(isl_space_wrap(dom)),
2178 isl_space_from_range(ran_ran));
2181 int isl_space_has_named_params(__isl_keep isl_space *dim)
2183 int i;
2184 unsigned off;
2186 if (!dim)
2187 return -1;
2188 if (dim->nparam == 0)
2189 return 1;
2190 off = isl_space_offset(dim, isl_dim_param);
2191 if (off + dim->nparam > dim->n_id)
2192 return 0;
2193 for (i = 0; i < dim->nparam; ++i)
2194 if (!dim->ids[off + i])
2195 return 0;
2196 return 1;
2199 /* Align the initial parameters of dim1 to match the order in dim2.
2201 __isl_give isl_space *isl_space_align_params(__isl_take isl_space *dim1,
2202 __isl_take isl_space *dim2)
2204 isl_reordering *exp;
2206 if (!isl_space_has_named_params(dim1) || !isl_space_has_named_params(dim2))
2207 isl_die(isl_space_get_ctx(dim1), isl_error_invalid,
2208 "parameter alignment requires named parameters",
2209 goto error);
2211 dim2 = isl_space_params(dim2);
2212 exp = isl_parameter_alignment_reordering(dim1, dim2);
2213 exp = isl_reordering_extend_space(exp, dim1);
2214 isl_space_free(dim2);
2215 if (!exp)
2216 return NULL;
2217 dim1 = isl_space_copy(exp->dim);
2218 isl_reordering_free(exp);
2219 return dim1;
2220 error:
2221 isl_space_free(dim1);
2222 isl_space_free(dim2);
2223 return NULL;
2226 /* Given the space of set (domain), construct a space for a map
2227 * with as domain the given space and as range the range of "model".
2229 __isl_give isl_space *isl_space_extend_domain_with_range(
2230 __isl_take isl_space *space, __isl_take isl_space *model)
2232 if (!model)
2233 goto error;
2235 space = isl_space_from_domain(space);
2236 space = isl_space_add_dims(space, isl_dim_out,
2237 isl_space_dim(model, isl_dim_out));
2238 if (isl_space_has_tuple_id(model, isl_dim_out))
2239 space = isl_space_set_tuple_id(space, isl_dim_out,
2240 isl_space_get_tuple_id(model, isl_dim_out));
2241 if (!space)
2242 goto error;
2243 if (model->nested[1]) {
2244 isl_space *nested = isl_space_copy(model->nested[1]);
2245 int n_nested, n_space;
2246 nested = isl_space_align_params(nested, isl_space_copy(space));
2247 n_nested = isl_space_dim(nested, isl_dim_param);
2248 n_space = isl_space_dim(space, isl_dim_param);
2249 if (n_nested > n_space)
2250 nested = isl_space_drop_dims(nested, isl_dim_param,
2251 n_space, n_nested - n_space);
2252 if (!nested)
2253 goto error;
2254 space->nested[1] = nested;
2256 isl_space_free(model);
2257 return space;
2258 error:
2259 isl_space_free(model);
2260 isl_space_free(space);
2261 return NULL;
2264 /* Compare the "type" dimensions of two isl_spaces.
2266 * The order is fairly arbitrary.
2268 static int isl_space_cmp_type(__isl_keep isl_space *space1,
2269 __isl_keep isl_space *space2, enum isl_dim_type type)
2271 int cmp;
2272 isl_space *nested1, *nested2;
2274 if (isl_space_dim(space1, type) != isl_space_dim(space2, type))
2275 return isl_space_dim(space1, type) -
2276 isl_space_dim(space2, type);
2278 cmp = isl_id_cmp(tuple_id(space1, type), tuple_id(space2, type));
2279 if (cmp != 0)
2280 return cmp;
2282 nested1 = nested(space1, type);
2283 nested2 = nested(space2, type);
2284 if (!nested1 != !nested2)
2285 return !nested1 - !nested2;
2287 if (nested1)
2288 return isl_space_cmp(nested1, nested2);
2290 return 0;
2293 /* Compare two isl_spaces.
2295 * The order is fairly arbitrary.
2297 int isl_space_cmp(__isl_keep isl_space *space1, __isl_keep isl_space *space2)
2299 int i;
2300 int cmp;
2302 if (space1 == space2)
2303 return 0;
2304 if (!space1)
2305 return -1;
2306 if (!space2)
2307 return 1;
2309 cmp = isl_space_cmp_type(space1, space2, isl_dim_param);
2310 if (cmp != 0)
2311 return cmp;
2312 cmp = isl_space_cmp_type(space1, space2, isl_dim_in);
2313 if (cmp != 0)
2314 return cmp;
2315 cmp = isl_space_cmp_type(space1, space2, isl_dim_out);
2316 if (cmp != 0)
2317 return cmp;
2319 if (!space1->ids && !space2->ids)
2320 return 0;
2322 for (i = 0; i < n(space1, isl_dim_param); ++i) {
2323 cmp = isl_id_cmp(get_id(space1, isl_dim_param, i),
2324 get_id(space2, isl_dim_param, i));
2325 if (cmp != 0)
2326 return cmp;
2329 return 0;