isl_poly_is_cst: use isl_bool_ok
[isl.git] / isl_space.c
blob465f9ef3ecc301483fd0d42810f71b945e525d8b
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2013-2014 Ecole Normale Superieure
6 * Use of this software is governed by the MIT license
8 * Written by Sven Verdoolaege, K.U.Leuven, Departement
9 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
10 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
11 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
12 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
15 #include <stdlib.h>
16 #include <string.h>
17 #include <isl_space_private.h>
18 #include <isl_id_private.h>
19 #include <isl_reordering.h>
21 isl_ctx *isl_space_get_ctx(__isl_keep isl_space *space)
23 return space ? space->ctx : NULL;
26 __isl_give isl_space *isl_space_alloc(isl_ctx *ctx,
27 unsigned nparam, unsigned n_in, unsigned n_out)
29 isl_space *space;
31 space = isl_alloc_type(ctx, struct isl_space);
32 if (!space)
33 return NULL;
35 space->ctx = ctx;
36 isl_ctx_ref(ctx);
37 space->ref = 1;
38 space->nparam = nparam;
39 space->n_in = n_in;
40 space->n_out = n_out;
42 space->tuple_id[0] = NULL;
43 space->tuple_id[1] = NULL;
45 space->nested[0] = NULL;
46 space->nested[1] = NULL;
48 space->n_id = 0;
49 space->ids = NULL;
51 return space;
54 /* Mark the space as being that of a set, by setting the domain tuple
55 * to isl_id_none.
57 static __isl_give isl_space *mark_as_set(__isl_take isl_space *space)
59 space = isl_space_cow(space);
60 if (!space)
61 return NULL;
62 space = isl_space_set_tuple_id(space, isl_dim_in, &isl_id_none);
63 return space;
66 /* Is the space that of a set?
68 isl_bool isl_space_is_set(__isl_keep isl_space *space)
70 if (!space)
71 return isl_bool_error;
72 if (space->n_in != 0 || space->nested[0])
73 return isl_bool_false;
74 if (space->tuple_id[0] != &isl_id_none)
75 return isl_bool_false;
76 return isl_bool_true;
79 /* Check that "space" is a set space.
81 isl_stat isl_space_check_is_set(__isl_keep isl_space *space)
83 isl_bool is_set;
85 is_set = isl_space_is_set(space);
86 if (is_set < 0)
87 return isl_stat_error;
88 if (!is_set)
89 isl_die(isl_space_get_ctx(space), isl_error_invalid,
90 "space is not a set", return isl_stat_error);
91 return isl_stat_ok;
94 /* Is the given space that of a map?
96 isl_bool isl_space_is_map(__isl_keep isl_space *space)
98 if (!space)
99 return isl_bool_error;
100 return space->tuple_id[0] != &isl_id_none &&
101 space->tuple_id[1] != &isl_id_none;
104 __isl_give isl_space *isl_space_set_alloc(isl_ctx *ctx,
105 unsigned nparam, unsigned dim)
107 isl_space *space;
108 space = isl_space_alloc(ctx, nparam, 0, dim);
109 space = mark_as_set(space);
110 return space;
113 /* Mark the space as being that of a parameter domain, by setting
114 * both tuples to isl_id_none.
116 static __isl_give isl_space *mark_as_params(isl_space *space)
118 if (!space)
119 return NULL;
120 space = isl_space_set_tuple_id(space, isl_dim_in, &isl_id_none);
121 space = isl_space_set_tuple_id(space, isl_dim_out, &isl_id_none);
122 return space;
125 /* Is the space that of a parameter domain?
127 isl_bool isl_space_is_params(__isl_keep isl_space *space)
129 if (!space)
130 return isl_bool_error;
131 if (space->n_in != 0 || space->nested[0] ||
132 space->n_out != 0 || space->nested[1])
133 return isl_bool_false;
134 if (space->tuple_id[0] != &isl_id_none)
135 return isl_bool_false;
136 if (space->tuple_id[1] != &isl_id_none)
137 return isl_bool_false;
138 return isl_bool_true;
141 /* Create a space for a parameter domain.
143 __isl_give isl_space *isl_space_params_alloc(isl_ctx *ctx, unsigned nparam)
145 isl_space *space;
146 space = isl_space_alloc(ctx, nparam, 0, 0);
147 space = mark_as_params(space);
148 return space;
151 static isl_size global_pos(__isl_keep isl_space *space,
152 enum isl_dim_type type, unsigned pos)
154 if (isl_space_check_range(space, type, pos, 1) < 0)
155 return isl_size_error;
157 switch (type) {
158 case isl_dim_param:
159 return pos;
160 case isl_dim_in:
161 return pos + space->nparam;
162 case isl_dim_out:
163 return pos + space->nparam + space->n_in;
164 default:
165 isl_assert(isl_space_get_ctx(space), 0, return isl_size_error);
167 return isl_size_error;
170 /* Extend length of ids array to the total number of dimensions.
172 static __isl_give isl_space *extend_ids(__isl_take isl_space *space)
174 isl_id **ids;
175 int i;
176 isl_size dim;
178 dim = isl_space_dim(space, isl_dim_all);
179 if (dim < 0)
180 return isl_space_free(space);
181 if (dim <= space->n_id)
182 return space;
184 if (!space->ids) {
185 space->ids = isl_calloc_array(space->ctx, isl_id *, dim);
186 if (!space->ids)
187 goto error;
188 } else {
189 ids = isl_realloc_array(space->ctx, space->ids, isl_id *, dim);
190 if (!ids)
191 goto error;
192 space->ids = ids;
193 for (i = space->n_id; i < dim; ++i)
194 space->ids[i] = NULL;
197 space->n_id = dim;
199 return space;
200 error:
201 isl_space_free(space);
202 return NULL;
205 static __isl_give isl_space *set_id(__isl_take isl_space *space,
206 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
208 isl_size gpos;
210 space = isl_space_cow(space);
212 gpos = global_pos(space, type, pos);
213 if (gpos < 0)
214 goto error;
216 if (gpos >= space->n_id) {
217 if (!id)
218 return space;
219 space = extend_ids(space);
220 if (!space)
221 goto error;
224 space->ids[gpos] = id;
226 return space;
227 error:
228 isl_id_free(id);
229 isl_space_free(space);
230 return NULL;
233 static __isl_keep isl_id *get_id(__isl_keep isl_space *space,
234 enum isl_dim_type type, unsigned pos)
236 isl_size gpos;
238 gpos = global_pos(space, type, pos);
239 if (gpos < 0)
240 return NULL;
241 if (gpos >= space->n_id)
242 return NULL;
243 return space->ids[gpos];
246 static unsigned offset(__isl_keep isl_space *space, enum isl_dim_type type)
248 switch (type) {
249 case isl_dim_param: return 0;
250 case isl_dim_in: return space->nparam;
251 case isl_dim_out: return space->nparam + space->n_in;
252 default: return 0;
256 static unsigned n(__isl_keep isl_space *space, enum isl_dim_type type)
258 switch (type) {
259 case isl_dim_param: return space->nparam;
260 case isl_dim_in: return space->n_in;
261 case isl_dim_out: return space->n_out;
262 case isl_dim_all:
263 return space->nparam + space->n_in + space->n_out;
264 default: return 0;
268 isl_size isl_space_dim(__isl_keep isl_space *space, enum isl_dim_type type)
270 if (!space)
271 return isl_size_error;
272 return n(space, type);
275 unsigned isl_space_offset(__isl_keep isl_space *dim, enum isl_dim_type type)
277 if (!dim)
278 return 0;
279 return offset(dim, type);
282 static __isl_give isl_space *copy_ids(__isl_take isl_space *dst,
283 enum isl_dim_type dst_type, unsigned offset, __isl_keep isl_space *src,
284 enum isl_dim_type src_type)
286 int i;
287 isl_id *id;
289 if (!dst)
290 return NULL;
292 for (i = 0; i < n(src, src_type); ++i) {
293 id = get_id(src, src_type, i);
294 if (!id)
295 continue;
296 dst = set_id(dst, dst_type, offset + i, isl_id_copy(id));
297 if (!dst)
298 return NULL;
300 return dst;
303 __isl_take isl_space *isl_space_dup(__isl_keep isl_space *space)
305 isl_space *dup;
306 if (!space)
307 return NULL;
308 dup = isl_space_alloc(space->ctx,
309 space->nparam, space->n_in, space->n_out);
310 if (!dup)
311 return NULL;
312 if (space->tuple_id[0] &&
313 !(dup->tuple_id[0] = isl_id_copy(space->tuple_id[0])))
314 goto error;
315 if (space->tuple_id[1] &&
316 !(dup->tuple_id[1] = isl_id_copy(space->tuple_id[1])))
317 goto error;
318 if (space->nested[0] &&
319 !(dup->nested[0] = isl_space_copy(space->nested[0])))
320 goto error;
321 if (space->nested[1] &&
322 !(dup->nested[1] = isl_space_copy(space->nested[1])))
323 goto error;
324 if (!space->ids)
325 return dup;
326 dup = copy_ids(dup, isl_dim_param, 0, space, isl_dim_param);
327 dup = copy_ids(dup, isl_dim_in, 0, space, isl_dim_in);
328 dup = copy_ids(dup, isl_dim_out, 0, space, isl_dim_out);
329 return dup;
330 error:
331 isl_space_free(dup);
332 return NULL;
335 __isl_give isl_space *isl_space_cow(__isl_take isl_space *space)
337 if (!space)
338 return NULL;
340 if (space->ref == 1)
341 return space;
342 space->ref--;
343 return isl_space_dup(space);
346 __isl_give isl_space *isl_space_copy(__isl_keep isl_space *dim)
348 if (!dim)
349 return NULL;
351 dim->ref++;
352 return dim;
355 __isl_null isl_space *isl_space_free(__isl_take isl_space *space)
357 int i;
359 if (!space)
360 return NULL;
362 if (--space->ref > 0)
363 return NULL;
365 isl_id_free(space->tuple_id[0]);
366 isl_id_free(space->tuple_id[1]);
368 isl_space_free(space->nested[0]);
369 isl_space_free(space->nested[1]);
371 for (i = 0; i < space->n_id; ++i)
372 isl_id_free(space->ids[i]);
373 free(space->ids);
374 isl_ctx_deref(space->ctx);
376 free(space);
378 return NULL;
381 /* Check if "s" is a valid dimension or tuple name.
382 * We currently only forbid names that look like a number.
384 * s is assumed to be non-NULL.
386 static int name_ok(isl_ctx *ctx, const char *s)
388 char *p;
389 long dummy;
391 dummy = strtol(s, &p, 0);
392 if (p != s)
393 isl_die(ctx, isl_error_invalid, "name looks like a number",
394 return 0);
396 return 1;
399 /* Is it possible for the given dimension type to have a tuple id?
401 static int space_can_have_id(__isl_keep isl_space *space,
402 enum isl_dim_type type)
404 if (!space)
405 return 0;
406 if (isl_space_is_params(space))
407 isl_die(space->ctx, isl_error_invalid,
408 "parameter spaces don't have tuple ids", return 0);
409 if (isl_space_is_set(space) && type != isl_dim_set)
410 isl_die(space->ctx, isl_error_invalid,
411 "set spaces can only have a set id", return 0);
412 if (type != isl_dim_in && type != isl_dim_out)
413 isl_die(space->ctx, isl_error_invalid,
414 "only input, output and set tuples can have ids",
415 return 0);
417 return 1;
420 /* Does the tuple have an id?
422 isl_bool isl_space_has_tuple_id(__isl_keep isl_space *space,
423 enum isl_dim_type type)
425 if (!space_can_have_id(space, type))
426 return isl_bool_error;
427 return isl_bool_ok(space->tuple_id[type - isl_dim_in] != NULL);
430 __isl_give isl_id *isl_space_get_tuple_id(__isl_keep isl_space *space,
431 enum isl_dim_type type)
433 int has_id;
435 if (!space)
436 return NULL;
437 has_id = isl_space_has_tuple_id(space, type);
438 if (has_id < 0)
439 return NULL;
440 if (!has_id)
441 isl_die(space->ctx, isl_error_invalid,
442 "tuple has no id", return NULL);
443 return isl_id_copy(space->tuple_id[type - isl_dim_in]);
446 __isl_give isl_space *isl_space_set_tuple_id(__isl_take isl_space *space,
447 enum isl_dim_type type, __isl_take isl_id *id)
449 space = isl_space_cow(space);
450 if (!space || !id)
451 goto error;
452 if (type != isl_dim_in && type != isl_dim_out)
453 isl_die(space->ctx, isl_error_invalid,
454 "only input, output and set tuples can have names",
455 goto error);
457 isl_id_free(space->tuple_id[type - isl_dim_in]);
458 space->tuple_id[type - isl_dim_in] = id;
460 return space;
461 error:
462 isl_id_free(id);
463 isl_space_free(space);
464 return NULL;
467 __isl_give isl_space *isl_space_reset_tuple_id(__isl_take isl_space *space,
468 enum isl_dim_type type)
470 space = isl_space_cow(space);
471 if (!space)
472 return NULL;
473 if (type != isl_dim_in && type != isl_dim_out)
474 isl_die(space->ctx, isl_error_invalid,
475 "only input, output and set tuples can have names",
476 goto error);
478 isl_id_free(space->tuple_id[type - isl_dim_in]);
479 space->tuple_id[type - isl_dim_in] = NULL;
481 return space;
482 error:
483 isl_space_free(space);
484 return NULL;
487 /* Set the id of the given dimension of "space" to "id".
488 * If the dimension already has an id, then it is replaced.
489 * If the dimension is a parameter, then we need to change it
490 * in the nested spaces (if any) as well.
492 __isl_give isl_space *isl_space_set_dim_id(__isl_take isl_space *space,
493 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
495 space = isl_space_cow(space);
496 if (!space || !id)
497 goto error;
499 if (type == isl_dim_param) {
500 int i;
502 for (i = 0; i < 2; ++i) {
503 if (!space->nested[i])
504 continue;
505 space->nested[i] =
506 isl_space_set_dim_id(space->nested[i],
507 type, pos, isl_id_copy(id));
508 if (!space->nested[i])
509 goto error;
513 isl_id_free(get_id(space, type, pos));
514 return set_id(space, type, pos, id);
515 error:
516 isl_id_free(id);
517 isl_space_free(space);
518 return NULL;
521 /* Reset the id of the given dimension of "space".
522 * If the dimension already has an id, then it is removed.
523 * If the dimension is a parameter, then we need to reset it
524 * in the nested spaces (if any) as well.
526 __isl_give isl_space *isl_space_reset_dim_id(__isl_take isl_space *space,
527 enum isl_dim_type type, unsigned pos)
529 space = isl_space_cow(space);
530 if (!space)
531 goto error;
533 if (type == isl_dim_param) {
534 int i;
536 for (i = 0; i < 2; ++i) {
537 if (!space->nested[i])
538 continue;
539 space->nested[i] =
540 isl_space_reset_dim_id(space->nested[i],
541 type, pos);
542 if (!space->nested[i])
543 goto error;
547 isl_id_free(get_id(space, type, pos));
548 return set_id(space, type, pos, NULL);
549 error:
550 isl_space_free(space);
551 return NULL;
554 isl_bool isl_space_has_dim_id(__isl_keep isl_space *space,
555 enum isl_dim_type type, unsigned pos)
557 if (!space)
558 return isl_bool_error;
559 return isl_bool_ok(get_id(space, type, pos) != NULL);
562 __isl_give isl_id *isl_space_get_dim_id(__isl_keep isl_space *space,
563 enum isl_dim_type type, unsigned pos)
565 if (!space)
566 return NULL;
567 if (!get_id(space, type, pos))
568 isl_die(space->ctx, isl_error_invalid,
569 "dim has no id", return NULL);
570 return isl_id_copy(get_id(space, type, pos));
573 __isl_give isl_space *isl_space_set_tuple_name(__isl_take isl_space *space,
574 enum isl_dim_type type, const char *s)
576 isl_id *id;
578 if (!space)
579 return NULL;
581 if (!s)
582 return isl_space_reset_tuple_id(space, type);
584 if (!name_ok(space->ctx, s))
585 goto error;
587 id = isl_id_alloc(space->ctx, s, NULL);
588 return isl_space_set_tuple_id(space, type, id);
589 error:
590 isl_space_free(space);
591 return NULL;
594 /* Does the tuple have a name?
596 isl_bool isl_space_has_tuple_name(__isl_keep isl_space *space,
597 enum isl_dim_type type)
599 isl_id *id;
601 if (!space_can_have_id(space, type))
602 return isl_bool_error;
603 id = space->tuple_id[type - isl_dim_in];
604 return isl_bool_ok(id && id->name);
607 __isl_keep const char *isl_space_get_tuple_name(__isl_keep isl_space *space,
608 enum isl_dim_type type)
610 isl_id *id;
611 if (!space)
612 return NULL;
613 if (type != isl_dim_in && type != isl_dim_out)
614 return NULL;
615 id = space->tuple_id[type - isl_dim_in];
616 return id ? id->name : NULL;
619 __isl_give isl_space *isl_space_set_dim_name(__isl_take isl_space *space,
620 enum isl_dim_type type, unsigned pos,
621 const char *s)
623 isl_id *id;
625 if (!space)
626 return NULL;
627 if (!s)
628 return isl_space_reset_dim_id(space, type, pos);
629 if (!name_ok(space->ctx, s))
630 goto error;
631 id = isl_id_alloc(space->ctx, s, NULL);
632 return isl_space_set_dim_id(space, type, pos, id);
633 error:
634 isl_space_free(space);
635 return NULL;
638 /* Does the given dimension have a name?
640 isl_bool isl_space_has_dim_name(__isl_keep isl_space *space,
641 enum isl_dim_type type, unsigned pos)
643 isl_id *id;
645 if (!space)
646 return isl_bool_error;
647 id = get_id(space, type, pos);
648 return isl_bool_ok(id && id->name);
651 __isl_keep const char *isl_space_get_dim_name(__isl_keep isl_space *space,
652 enum isl_dim_type type, unsigned pos)
654 isl_id *id = get_id(space, type, pos);
655 return id ? id->name : NULL;
658 int isl_space_find_dim_by_id(__isl_keep isl_space *space,
659 enum isl_dim_type type, __isl_keep isl_id *id)
661 int i;
662 int offset;
663 isl_size n;
665 n = isl_space_dim(space, type);
666 if (n < 0 || !id)
667 return -1;
669 offset = isl_space_offset(space, type);
670 for (i = 0; i < n && offset + i < space->n_id; ++i)
671 if (space->ids[offset + i] == id)
672 return i;
674 return -1;
677 int isl_space_find_dim_by_name(__isl_keep isl_space *space,
678 enum isl_dim_type type, const char *name)
680 int i;
681 int offset;
682 isl_size n;
684 n = isl_space_dim(space, type);
685 if (n < 0 || !name)
686 return -1;
688 offset = isl_space_offset(space, type);
689 for (i = 0; i < n && offset + i < space->n_id; ++i) {
690 isl_id *id = get_id(space, type, i);
691 if (id && id->name && !strcmp(id->name, name))
692 return i;
695 return -1;
698 /* Reset the user pointer on all identifiers of parameters and tuples
699 * of "space".
701 __isl_give isl_space *isl_space_reset_user(__isl_take isl_space *space)
703 int i;
704 isl_ctx *ctx;
705 isl_id *id;
706 const char *name;
708 if (!space)
709 return NULL;
711 ctx = isl_space_get_ctx(space);
713 for (i = 0; i < space->nparam && i < space->n_id; ++i) {
714 if (!isl_id_get_user(space->ids[i]))
715 continue;
716 space = isl_space_cow(space);
717 if (!space)
718 return NULL;
719 name = isl_id_get_name(space->ids[i]);
720 id = isl_id_alloc(ctx, name, NULL);
721 isl_id_free(space->ids[i]);
722 space->ids[i] = id;
723 if (!id)
724 return isl_space_free(space);
727 for (i = 0; i < 2; ++i) {
728 if (!space->tuple_id[i])
729 continue;
730 if (!isl_id_get_user(space->tuple_id[i]))
731 continue;
732 space = isl_space_cow(space);
733 if (!space)
734 return NULL;
735 name = isl_id_get_name(space->tuple_id[i]);
736 id = isl_id_alloc(ctx, name, NULL);
737 isl_id_free(space->tuple_id[i]);
738 space->tuple_id[i] = id;
739 if (!id)
740 return isl_space_free(space);
743 for (i = 0; i < 2; ++i) {
744 if (!space->nested[i])
745 continue;
746 space = isl_space_cow(space);
747 if (!space)
748 return NULL;
749 space->nested[i] = isl_space_reset_user(space->nested[i]);
750 if (!space->nested[i])
751 return isl_space_free(space);
754 return space;
757 static __isl_keep isl_id *tuple_id(__isl_keep isl_space *dim,
758 enum isl_dim_type type)
760 if (!dim)
761 return NULL;
762 if (type == isl_dim_in)
763 return dim->tuple_id[0];
764 if (type == isl_dim_out)
765 return dim->tuple_id[1];
766 return NULL;
769 static __isl_keep isl_space *nested(__isl_keep isl_space *dim,
770 enum isl_dim_type type)
772 if (!dim)
773 return NULL;
774 if (type == isl_dim_in)
775 return dim->nested[0];
776 if (type == isl_dim_out)
777 return dim->nested[1];
778 return NULL;
781 /* Are the two spaces the same, apart from positions and names of parameters?
783 isl_bool isl_space_has_equal_tuples(__isl_keep isl_space *space1,
784 __isl_keep isl_space *space2)
786 if (!space1 || !space2)
787 return isl_bool_error;
788 if (space1 == space2)
789 return isl_bool_true;
790 return isl_space_tuple_is_equal(space1, isl_dim_in,
791 space2, isl_dim_in) &&
792 isl_space_tuple_is_equal(space1, isl_dim_out,
793 space2, isl_dim_out);
796 /* Check if the tuple of type "type1" of "space1" is the same as
797 * the tuple of type "type2" of "space2".
799 * That is, check if the tuples have the same identifier, the same dimension
800 * and the same internal structure.
801 * The identifiers of the dimensions inside the tuples do not affect the result.
803 * Note that this function only checks the tuples themselves.
804 * If nested tuples are involved, then we need to be careful not
805 * to have result affected by possibly differing parameters
806 * in those nested tuples.
808 isl_bool isl_space_tuple_is_equal(__isl_keep isl_space *space1,
809 enum isl_dim_type type1, __isl_keep isl_space *space2,
810 enum isl_dim_type type2)
812 isl_id *id1, *id2;
813 isl_space *nested1, *nested2;
815 if (!space1 || !space2)
816 return isl_bool_error;
818 if (space1 == space2 && type1 == type2)
819 return isl_bool_true;
821 if (n(space1, type1) != n(space2, type2))
822 return isl_bool_false;
823 id1 = tuple_id(space1, type1);
824 id2 = tuple_id(space2, type2);
825 if (!id1 ^ !id2)
826 return isl_bool_false;
827 if (id1 && id1 != id2)
828 return isl_bool_false;
829 nested1 = nested(space1, type1);
830 nested2 = nested(space2, type2);
831 if (!nested1 ^ !nested2)
832 return isl_bool_false;
833 if (nested1 && !isl_space_has_equal_tuples(nested1, nested2))
834 return isl_bool_false;
835 return isl_bool_true;
838 static isl_bool match(__isl_keep isl_space *space1, enum isl_dim_type type1,
839 __isl_keep isl_space *space2, enum isl_dim_type type2)
841 int i;
843 if (space1 == space2 && type1 == type2)
844 return isl_bool_true;
846 if (!isl_space_tuple_is_equal(space1, type1, space2, type2))
847 return isl_bool_false;
849 if (!space1->ids && !space2->ids)
850 return isl_bool_true;
852 for (i = 0; i < n(space1, type1); ++i) {
853 if (get_id(space1, type1, i) != get_id(space2, type2, i))
854 return isl_bool_false;
856 return isl_bool_true;
859 /* Do "space1" and "space2" have the same parameters?
861 isl_bool isl_space_has_equal_params(__isl_keep isl_space *space1,
862 __isl_keep isl_space *space2)
864 if (!space1 || !space2)
865 return isl_bool_error;
867 return match(space1, isl_dim_param, space2, isl_dim_param);
870 /* Do "space1" and "space2" have the same identifiers for all
871 * the tuple variables?
873 isl_bool isl_space_has_equal_ids(__isl_keep isl_space *space1,
874 __isl_keep isl_space *space2)
876 isl_bool equal;
878 if (!space1 || !space2)
879 return isl_bool_error;
881 equal = match(space1, isl_dim_in, space2, isl_dim_in);
882 if (equal < 0 || !equal)
883 return equal;
884 return match(space1, isl_dim_out, space2, isl_dim_out);
887 isl_bool isl_space_match(__isl_keep isl_space *space1, enum isl_dim_type type1,
888 __isl_keep isl_space *space2, enum isl_dim_type type2)
890 if (!space1 || !space2)
891 return isl_bool_error;
893 return match(space1, type1, space2, type2);
896 static void get_ids(__isl_keep isl_space *dim, enum isl_dim_type type,
897 unsigned first, unsigned n, __isl_keep isl_id **ids)
899 int i;
901 for (i = 0; i < n ; ++i)
902 ids[i] = get_id(dim, type, first + i);
905 static __isl_give isl_space *space_extend(__isl_take isl_space *space,
906 unsigned nparam, unsigned n_in, unsigned n_out)
908 isl_id **ids = NULL;
910 if (!space)
911 return NULL;
912 if (space->nparam == nparam &&
913 space->n_in == n_in && space->n_out == n_out)
914 return space;
916 isl_assert(space->ctx, space->nparam <= nparam, goto error);
917 isl_assert(space->ctx, space->n_in <= n_in, goto error);
918 isl_assert(space->ctx, space->n_out <= n_out, goto error);
920 space = isl_space_cow(space);
921 if (!space)
922 goto error;
924 if (space->ids) {
925 unsigned n;
926 n = nparam + n_in + n_out;
927 if (n < nparam || n < n_in || n < n_out)
928 isl_die(isl_space_get_ctx(space), isl_error_invalid,
929 "overflow in total number of dimensions",
930 goto error);
931 ids = isl_calloc_array(space->ctx, isl_id *, n);
932 if (!ids)
933 goto error;
934 get_ids(space, isl_dim_param, 0, space->nparam, ids);
935 get_ids(space, isl_dim_in, 0, space->n_in, ids + nparam);
936 get_ids(space, isl_dim_out, 0, space->n_out,
937 ids + nparam + n_in);
938 free(space->ids);
939 space->ids = ids;
940 space->n_id = nparam + n_in + n_out;
942 space->nparam = nparam;
943 space->n_in = n_in;
944 space->n_out = n_out;
946 return space;
947 error:
948 free(ids);
949 isl_space_free(space);
950 return NULL;
953 __isl_give isl_space *isl_space_extend(__isl_take isl_space *space,
954 unsigned nparam, unsigned n_in, unsigned n_out)
956 return space_extend(space, nparam, n_in, n_out);
959 __isl_give isl_space *isl_space_add_dims(__isl_take isl_space *space,
960 enum isl_dim_type type, unsigned n)
962 space = isl_space_reset(space, type);
963 if (!space)
964 return NULL;
965 switch (type) {
966 case isl_dim_param:
967 space = space_extend(space,
968 space->nparam + n, space->n_in, space->n_out);
969 if (space && space->nested[0] &&
970 !(space->nested[0] = isl_space_add_dims(space->nested[0],
971 isl_dim_param, n)))
972 goto error;
973 if (space && space->nested[1] &&
974 !(space->nested[1] = isl_space_add_dims(space->nested[1],
975 isl_dim_param, n)))
976 goto error;
977 return space;
978 case isl_dim_in:
979 return space_extend(space,
980 space->nparam, space->n_in + n, space->n_out);
981 case isl_dim_out:
982 return space_extend(space,
983 space->nparam, space->n_in, space->n_out + n);
984 default:
985 isl_die(space->ctx, isl_error_invalid,
986 "cannot add dimensions of specified type", goto error);
988 error:
989 isl_space_free(space);
990 return NULL;
993 /* Add a parameter with identifier "id" to "space", provided
994 * it does not already appear in "space".
996 __isl_give isl_space *isl_space_add_param_id(__isl_take isl_space *space,
997 __isl_take isl_id *id)
999 isl_size pos;
1001 if (!space || !id)
1002 goto error;
1004 if (isl_space_find_dim_by_id(space, isl_dim_param, id) >= 0) {
1005 isl_id_free(id);
1006 return space;
1009 pos = isl_space_dim(space, isl_dim_param);
1010 if (pos < 0)
1011 goto error;
1012 space = isl_space_add_dims(space, isl_dim_param, 1);
1013 space = isl_space_set_dim_id(space, isl_dim_param, pos, id);
1015 return space;
1016 error:
1017 isl_space_free(space);
1018 isl_id_free(id);
1019 return NULL;
1022 static int valid_dim_type(enum isl_dim_type type)
1024 switch (type) {
1025 case isl_dim_param:
1026 case isl_dim_in:
1027 case isl_dim_out:
1028 return 1;
1029 default:
1030 return 0;
1034 #undef TYPE
1035 #define TYPE isl_space
1036 #include "check_type_range_templ.c"
1038 /* Insert "n" dimensions of type "type" at position "pos".
1039 * If we are inserting parameters, then they are also inserted in
1040 * any nested spaces.
1042 __isl_give isl_space *isl_space_insert_dims(__isl_take isl_space *space,
1043 enum isl_dim_type type, unsigned pos, unsigned n)
1045 isl_ctx *ctx;
1046 isl_id **ids = NULL;
1048 if (!space)
1049 return NULL;
1050 if (n == 0)
1051 return isl_space_reset(space, type);
1053 ctx = isl_space_get_ctx(space);
1054 if (!valid_dim_type(type))
1055 isl_die(ctx, isl_error_invalid,
1056 "cannot insert dimensions of specified type",
1057 goto error);
1059 if (isl_space_check_range(space, type, pos, 0) < 0)
1060 return isl_space_free(space);
1062 space = isl_space_cow(space);
1063 if (!space)
1064 return NULL;
1066 if (space->ids) {
1067 enum isl_dim_type t, o = isl_dim_param;
1068 int off;
1069 int s[3];
1070 ids = isl_calloc_array(ctx, isl_id *,
1071 space->nparam + space->n_in + space->n_out + n);
1072 if (!ids)
1073 goto error;
1074 off = 0;
1075 s[isl_dim_param - o] = space->nparam;
1076 s[isl_dim_in - o] = space->n_in;
1077 s[isl_dim_out - o] = space->n_out;
1078 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
1079 if (t != type) {
1080 get_ids(space, t, 0, s[t - o], ids + off);
1081 off += s[t - o];
1082 } else {
1083 get_ids(space, t, 0, pos, ids + off);
1084 off += pos + n;
1085 get_ids(space, t, pos, s[t - o] - pos,
1086 ids + off);
1087 off += s[t - o] - pos;
1090 free(space->ids);
1091 space->ids = ids;
1092 space->n_id = space->nparam + space->n_in + space->n_out + n;
1094 switch (type) {
1095 case isl_dim_param: space->nparam += n; break;
1096 case isl_dim_in: space->n_in += n; break;
1097 case isl_dim_out: space->n_out += n; break;
1098 default: ;
1100 space = isl_space_reset(space, type);
1102 if (type == isl_dim_param) {
1103 if (space && space->nested[0] &&
1104 !(space->nested[0] = isl_space_insert_dims(space->nested[0],
1105 isl_dim_param, pos, n)))
1106 goto error;
1107 if (space && space->nested[1] &&
1108 !(space->nested[1] = isl_space_insert_dims(space->nested[1],
1109 isl_dim_param, pos, n)))
1110 goto error;
1113 return space;
1114 error:
1115 isl_space_free(space);
1116 return NULL;
1119 __isl_give isl_space *isl_space_move_dims(__isl_take isl_space *space,
1120 enum isl_dim_type dst_type, unsigned dst_pos,
1121 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
1123 int i;
1125 space = isl_space_reset(space, src_type);
1126 space = isl_space_reset(space, dst_type);
1127 if (!space)
1128 return NULL;
1129 if (n == 0)
1130 return space;
1132 if (isl_space_check_range(space, src_type, src_pos, n) < 0)
1133 return isl_space_free(space);
1135 if (dst_type == src_type && dst_pos == src_pos)
1136 return space;
1138 isl_assert(space->ctx, dst_type != src_type, goto error);
1140 space = isl_space_cow(space);
1141 if (!space)
1142 return NULL;
1144 if (space->ids) {
1145 isl_id **ids;
1146 enum isl_dim_type t, o = isl_dim_param;
1147 int off;
1148 int s[3];
1149 ids = isl_calloc_array(space->ctx, isl_id *,
1150 space->nparam + space->n_in + space->n_out);
1151 if (!ids)
1152 goto error;
1153 off = 0;
1154 s[isl_dim_param - o] = space->nparam;
1155 s[isl_dim_in - o] = space->n_in;
1156 s[isl_dim_out - o] = space->n_out;
1157 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
1158 if (t == dst_type) {
1159 get_ids(space, t, 0, dst_pos, ids + off);
1160 off += dst_pos;
1161 get_ids(space, src_type, src_pos, n, ids + off);
1162 off += n;
1163 get_ids(space, t, dst_pos, s[t - o] - dst_pos,
1164 ids + off);
1165 off += s[t - o] - dst_pos;
1166 } else if (t == src_type) {
1167 get_ids(space, t, 0, src_pos, ids + off);
1168 off += src_pos;
1169 get_ids(space, t, src_pos + n,
1170 s[t - o] - src_pos - n, ids + off);
1171 off += s[t - o] - src_pos - n;
1172 } else {
1173 get_ids(space, t, 0, s[t - o], ids + off);
1174 off += s[t - o];
1177 free(space->ids);
1178 space->ids = ids;
1179 space->n_id = space->nparam + space->n_in + space->n_out;
1182 switch (dst_type) {
1183 case isl_dim_param: space->nparam += n; break;
1184 case isl_dim_in: space->n_in += n; break;
1185 case isl_dim_out: space->n_out += n; break;
1186 default: ;
1189 switch (src_type) {
1190 case isl_dim_param: space->nparam -= n; break;
1191 case isl_dim_in: space->n_in -= n; break;
1192 case isl_dim_out: space->n_out -= n; break;
1193 default: ;
1196 if (dst_type != isl_dim_param && src_type != isl_dim_param)
1197 return space;
1199 for (i = 0; i < 2; ++i) {
1200 if (!space->nested[i])
1201 continue;
1202 space->nested[i] = isl_space_replace_params(space->nested[i],
1203 space);
1204 if (!space->nested[i])
1205 goto error;
1208 return space;
1209 error:
1210 isl_space_free(space);
1211 return NULL;
1214 /* Check that "space1" and "space2" have the same parameters,
1215 * reporting an error if they do not.
1217 isl_stat isl_space_check_equal_params(__isl_keep isl_space *space1,
1218 __isl_keep isl_space *space2)
1220 isl_bool equal;
1222 equal = isl_space_has_equal_params(space1, space2);
1223 if (equal < 0)
1224 return isl_stat_error;
1225 if (!equal)
1226 isl_die(isl_space_get_ctx(space1), isl_error_invalid,
1227 "parameters need to match", return isl_stat_error);
1228 return isl_stat_ok;
1231 __isl_give isl_space *isl_space_join(__isl_take isl_space *left,
1232 __isl_take isl_space *right)
1234 isl_space *space;
1236 if (isl_space_check_equal_params(left, right) < 0)
1237 goto error;
1239 isl_assert(left->ctx,
1240 isl_space_tuple_is_equal(left, isl_dim_out, right, isl_dim_in),
1241 goto error);
1243 space = isl_space_alloc(left->ctx,
1244 left->nparam, left->n_in, right->n_out);
1245 if (!space)
1246 goto error;
1248 space = copy_ids(space, isl_dim_param, 0, left, isl_dim_param);
1249 space = copy_ids(space, isl_dim_in, 0, left, isl_dim_in);
1250 space = copy_ids(space, isl_dim_out, 0, right, isl_dim_out);
1252 if (space && left->tuple_id[0] &&
1253 !(space->tuple_id[0] = isl_id_copy(left->tuple_id[0])))
1254 goto error;
1255 if (space && right->tuple_id[1] &&
1256 !(space->tuple_id[1] = isl_id_copy(right->tuple_id[1])))
1257 goto error;
1258 if (space && left->nested[0] &&
1259 !(space->nested[0] = isl_space_copy(left->nested[0])))
1260 goto error;
1261 if (space && right->nested[1] &&
1262 !(space->nested[1] = isl_space_copy(right->nested[1])))
1263 goto error;
1265 isl_space_free(left);
1266 isl_space_free(right);
1268 return space;
1269 error:
1270 isl_space_free(left);
1271 isl_space_free(right);
1272 return NULL;
1275 /* Given two map spaces { A -> C } and { B -> D }, construct the space
1276 * { [A -> B] -> [C -> D] }.
1277 * Given two set spaces { A } and { B }, construct the space { [A -> B] }.
1279 __isl_give isl_space *isl_space_product(__isl_take isl_space *left,
1280 __isl_take isl_space *right)
1282 isl_space *dom1, *dom2, *nest1, *nest2;
1283 int is_set;
1285 if (!left || !right)
1286 goto error;
1288 is_set = isl_space_is_set(left);
1289 if (is_set != isl_space_is_set(right))
1290 isl_die(isl_space_get_ctx(left), isl_error_invalid,
1291 "expecting either two set spaces or two map spaces",
1292 goto error);
1293 if (is_set)
1294 return isl_space_range_product(left, right);
1296 if (isl_space_check_equal_params(left, right) < 0)
1297 goto error;
1299 dom1 = isl_space_domain(isl_space_copy(left));
1300 dom2 = isl_space_domain(isl_space_copy(right));
1301 nest1 = isl_space_wrap(isl_space_join(isl_space_reverse(dom1), dom2));
1303 dom1 = isl_space_range(left);
1304 dom2 = isl_space_range(right);
1305 nest2 = isl_space_wrap(isl_space_join(isl_space_reverse(dom1), dom2));
1307 return isl_space_join(isl_space_reverse(nest1), nest2);
1308 error:
1309 isl_space_free(left);
1310 isl_space_free(right);
1311 return NULL;
1314 /* Given two spaces { A -> C } and { B -> C }, construct the space
1315 * { [A -> B] -> C }
1317 __isl_give isl_space *isl_space_domain_product(__isl_take isl_space *left,
1318 __isl_take isl_space *right)
1320 isl_space *ran, *dom1, *dom2, *nest;
1322 if (isl_space_check_equal_params(left, right) < 0)
1323 goto error;
1325 if (!isl_space_tuple_is_equal(left, isl_dim_out, right, isl_dim_out))
1326 isl_die(left->ctx, isl_error_invalid,
1327 "ranges need to match", goto error);
1329 ran = isl_space_range(isl_space_copy(left));
1331 dom1 = isl_space_domain(left);
1332 dom2 = isl_space_domain(right);
1333 nest = isl_space_wrap(isl_space_join(isl_space_reverse(dom1), dom2));
1335 return isl_space_join(isl_space_reverse(nest), ran);
1336 error:
1337 isl_space_free(left);
1338 isl_space_free(right);
1339 return NULL;
1342 __isl_give isl_space *isl_space_range_product(__isl_take isl_space *left,
1343 __isl_take isl_space *right)
1345 isl_space *dom, *ran1, *ran2, *nest;
1347 if (isl_space_check_equal_params(left, right) < 0)
1348 goto error;
1350 if (!isl_space_tuple_is_equal(left, isl_dim_in, right, isl_dim_in))
1351 isl_die(left->ctx, isl_error_invalid,
1352 "domains need to match", goto error);
1354 dom = isl_space_domain(isl_space_copy(left));
1356 ran1 = isl_space_range(left);
1357 ran2 = isl_space_range(right);
1358 nest = isl_space_wrap(isl_space_join(isl_space_reverse(ran1), ran2));
1360 return isl_space_join(isl_space_reverse(dom), nest);
1361 error:
1362 isl_space_free(left);
1363 isl_space_free(right);
1364 return NULL;
1367 /* Given a space of the form [A -> B] -> C, return the space A -> C.
1369 __isl_give isl_space *isl_space_domain_factor_domain(
1370 __isl_take isl_space *space)
1372 isl_space *nested;
1373 isl_space *domain;
1375 if (!space)
1376 return NULL;
1377 if (!isl_space_domain_is_wrapping(space))
1378 isl_die(isl_space_get_ctx(space), isl_error_invalid,
1379 "domain not a product", return isl_space_free(space));
1381 nested = space->nested[0];
1382 domain = isl_space_copy(space);
1383 domain = isl_space_drop_dims(domain, isl_dim_in,
1384 nested->n_in, nested->n_out);
1385 if (!domain)
1386 return isl_space_free(space);
1387 if (nested->tuple_id[0]) {
1388 domain->tuple_id[0] = isl_id_copy(nested->tuple_id[0]);
1389 if (!domain->tuple_id[0])
1390 goto error;
1392 if (nested->nested[0]) {
1393 domain->nested[0] = isl_space_copy(nested->nested[0]);
1394 if (!domain->nested[0])
1395 goto error;
1398 isl_space_free(space);
1399 return domain;
1400 error:
1401 isl_space_free(space);
1402 isl_space_free(domain);
1403 return NULL;
1406 /* Given a space of the form [A -> B] -> C, return the space B -> C.
1408 __isl_give isl_space *isl_space_domain_factor_range(
1409 __isl_take isl_space *space)
1411 isl_space *nested;
1412 isl_space *range;
1414 if (!space)
1415 return NULL;
1416 if (!isl_space_domain_is_wrapping(space))
1417 isl_die(isl_space_get_ctx(space), isl_error_invalid,
1418 "domain not a product", return isl_space_free(space));
1420 nested = space->nested[0];
1421 range = isl_space_copy(space);
1422 range = isl_space_drop_dims(range, isl_dim_in, 0, nested->n_in);
1423 if (!range)
1424 return isl_space_free(space);
1425 if (nested->tuple_id[1]) {
1426 range->tuple_id[0] = isl_id_copy(nested->tuple_id[1]);
1427 if (!range->tuple_id[0])
1428 goto error;
1430 if (nested->nested[1]) {
1431 range->nested[0] = isl_space_copy(nested->nested[1]);
1432 if (!range->nested[0])
1433 goto error;
1436 isl_space_free(space);
1437 return range;
1438 error:
1439 isl_space_free(space);
1440 isl_space_free(range);
1441 return NULL;
1444 /* Internal function that selects the domain of the map that is
1445 * embedded in either a set space or the range of a map space.
1446 * In particular, given a space of the form [A -> B], return the space A.
1447 * Given a space of the form A -> [B -> C], return the space A -> B.
1449 static __isl_give isl_space *range_factor_domain(__isl_take isl_space *space)
1451 isl_space *nested;
1452 isl_space *domain;
1454 if (!space)
1455 return NULL;
1457 nested = space->nested[1];
1458 domain = isl_space_copy(space);
1459 domain = isl_space_drop_dims(domain, isl_dim_out,
1460 nested->n_in, nested->n_out);
1461 if (!domain)
1462 return isl_space_free(space);
1463 if (nested->tuple_id[0]) {
1464 domain->tuple_id[1] = isl_id_copy(nested->tuple_id[0]);
1465 if (!domain->tuple_id[1])
1466 goto error;
1468 if (nested->nested[0]) {
1469 domain->nested[1] = isl_space_copy(nested->nested[0]);
1470 if (!domain->nested[1])
1471 goto error;
1474 isl_space_free(space);
1475 return domain;
1476 error:
1477 isl_space_free(space);
1478 isl_space_free(domain);
1479 return NULL;
1482 /* Given a space of the form A -> [B -> C], return the space A -> B.
1484 __isl_give isl_space *isl_space_range_factor_domain(
1485 __isl_take isl_space *space)
1487 if (!space)
1488 return NULL;
1489 if (!isl_space_range_is_wrapping(space))
1490 isl_die(isl_space_get_ctx(space), isl_error_invalid,
1491 "range not a product", return isl_space_free(space));
1493 return range_factor_domain(space);
1496 /* Given a space of the form [A -> B], return the space A.
1498 static __isl_give isl_space *set_factor_domain(__isl_take isl_space *space)
1500 if (!space)
1501 return NULL;
1502 if (!isl_space_is_wrapping(space))
1503 isl_die(isl_space_get_ctx(space), isl_error_invalid,
1504 "not a product", return isl_space_free(space));
1506 return range_factor_domain(space);
1509 /* Given a space of the form [A -> B] -> [C -> D], return the space A -> C.
1510 * Given a space of the form [A -> B], return the space A.
1512 __isl_give isl_space *isl_space_factor_domain(__isl_take isl_space *space)
1514 if (!space)
1515 return NULL;
1516 if (isl_space_is_set(space))
1517 return set_factor_domain(space);
1518 space = isl_space_domain_factor_domain(space);
1519 space = isl_space_range_factor_domain(space);
1520 return space;
1523 /* Internal function that selects the range of the map that is
1524 * embedded in either a set space or the range of a map space.
1525 * In particular, given a space of the form [A -> B], return the space B.
1526 * Given a space of the form A -> [B -> C], return the space A -> C.
1528 static __isl_give isl_space *range_factor_range(__isl_take isl_space *space)
1530 isl_space *nested;
1531 isl_space *range;
1533 if (!space)
1534 return NULL;
1536 nested = space->nested[1];
1537 range = isl_space_copy(space);
1538 range = isl_space_drop_dims(range, isl_dim_out, 0, nested->n_in);
1539 if (!range)
1540 return isl_space_free(space);
1541 if (nested->tuple_id[1]) {
1542 range->tuple_id[1] = isl_id_copy(nested->tuple_id[1]);
1543 if (!range->tuple_id[1])
1544 goto error;
1546 if (nested->nested[1]) {
1547 range->nested[1] = isl_space_copy(nested->nested[1]);
1548 if (!range->nested[1])
1549 goto error;
1552 isl_space_free(space);
1553 return range;
1554 error:
1555 isl_space_free(space);
1556 isl_space_free(range);
1557 return NULL;
1560 /* Given a space of the form A -> [B -> C], return the space A -> C.
1562 __isl_give isl_space *isl_space_range_factor_range(
1563 __isl_take isl_space *space)
1565 if (!space)
1566 return NULL;
1567 if (!isl_space_range_is_wrapping(space))
1568 isl_die(isl_space_get_ctx(space), isl_error_invalid,
1569 "range not a product", return isl_space_free(space));
1571 return range_factor_range(space);
1574 /* Given a space of the form [A -> B], return the space B.
1576 static __isl_give isl_space *set_factor_range(__isl_take isl_space *space)
1578 if (!space)
1579 return NULL;
1580 if (!isl_space_is_wrapping(space))
1581 isl_die(isl_space_get_ctx(space), isl_error_invalid,
1582 "not a product", return isl_space_free(space));
1584 return range_factor_range(space);
1587 /* Given a space of the form [A -> B] -> [C -> D], return the space B -> D.
1588 * Given a space of the form [A -> B], return the space B.
1590 __isl_give isl_space *isl_space_factor_range(__isl_take isl_space *space)
1592 if (!space)
1593 return NULL;
1594 if (isl_space_is_set(space))
1595 return set_factor_range(space);
1596 space = isl_space_domain_factor_range(space);
1597 space = isl_space_range_factor_range(space);
1598 return space;
1601 __isl_give isl_space *isl_space_map_from_set(__isl_take isl_space *space)
1603 isl_ctx *ctx;
1604 isl_id **ids = NULL;
1605 int n_id;
1607 if (!space)
1608 return NULL;
1609 ctx = isl_space_get_ctx(space);
1610 if (!isl_space_is_set(space))
1611 isl_die(ctx, isl_error_invalid, "not a set space", goto error);
1612 space = isl_space_cow(space);
1613 if (!space)
1614 return NULL;
1615 n_id = space->nparam + space->n_out + space->n_out;
1616 if (n_id > 0 && space->ids) {
1617 ids = isl_calloc_array(space->ctx, isl_id *, n_id);
1618 if (!ids)
1619 goto error;
1620 get_ids(space, isl_dim_param, 0, space->nparam, ids);
1621 get_ids(space, isl_dim_out, 0, space->n_out,
1622 ids + space->nparam);
1624 space->n_in = space->n_out;
1625 if (ids) {
1626 free(space->ids);
1627 space->ids = ids;
1628 space->n_id = n_id;
1629 space = copy_ids(space, isl_dim_out, 0, space, isl_dim_in);
1631 isl_id_free(space->tuple_id[0]);
1632 space->tuple_id[0] = isl_id_copy(space->tuple_id[1]);
1633 isl_space_free(space->nested[0]);
1634 space->nested[0] = isl_space_copy(space->nested[1]);
1635 return space;
1636 error:
1637 isl_space_free(space);
1638 return NULL;
1641 __isl_give isl_space *isl_space_map_from_domain_and_range(
1642 __isl_take isl_space *domain, __isl_take isl_space *range)
1644 if (!domain || !range)
1645 goto error;
1646 if (!isl_space_is_set(domain))
1647 isl_die(isl_space_get_ctx(domain), isl_error_invalid,
1648 "domain is not a set space", goto error);
1649 if (!isl_space_is_set(range))
1650 isl_die(isl_space_get_ctx(range), isl_error_invalid,
1651 "range is not a set space", goto error);
1652 return isl_space_join(isl_space_reverse(domain), range);
1653 error:
1654 isl_space_free(domain);
1655 isl_space_free(range);
1656 return NULL;
1659 static __isl_give isl_space *set_ids(__isl_take isl_space *dim,
1660 enum isl_dim_type type,
1661 unsigned first, unsigned n, __isl_take isl_id **ids)
1663 int i;
1665 for (i = 0; i < n ; ++i)
1666 dim = set_id(dim, type, first + i, ids[i]);
1668 return dim;
1671 __isl_give isl_space *isl_space_reverse(__isl_take isl_space *space)
1673 unsigned t;
1674 isl_space *nested;
1675 isl_id **ids = NULL;
1676 isl_id *id;
1678 if (!space)
1679 return NULL;
1680 if (match(space, isl_dim_in, space, isl_dim_out))
1681 return space;
1683 space = isl_space_cow(space);
1684 if (!space)
1685 return NULL;
1687 id = space->tuple_id[0];
1688 space->tuple_id[0] = space->tuple_id[1];
1689 space->tuple_id[1] = id;
1691 nested = space->nested[0];
1692 space->nested[0] = space->nested[1];
1693 space->nested[1] = nested;
1695 if (space->ids) {
1696 int n_id = space->n_in + space->n_out;
1697 ids = isl_alloc_array(space->ctx, isl_id *, n_id);
1698 if (n_id && !ids)
1699 goto error;
1700 get_ids(space, isl_dim_in, 0, space->n_in, ids);
1701 get_ids(space, isl_dim_out, 0, space->n_out, ids + space->n_in);
1704 t = space->n_in;
1705 space->n_in = space->n_out;
1706 space->n_out = t;
1708 if (space->ids) {
1709 space = set_ids(space, isl_dim_out, 0, space->n_out, ids);
1710 space = set_ids(space, isl_dim_in, 0, space->n_in,
1711 ids + space->n_out);
1712 free(ids);
1715 return space;
1716 error:
1717 free(ids);
1718 isl_space_free(space);
1719 return NULL;
1722 __isl_give isl_space *isl_space_drop_dims(__isl_take isl_space *space,
1723 enum isl_dim_type type, unsigned first, unsigned num)
1725 int i;
1727 if (!space)
1728 return NULL;
1730 if (num == 0)
1731 return isl_space_reset(space, type);
1733 if (!valid_dim_type(type))
1734 isl_die(space->ctx, isl_error_invalid,
1735 "cannot drop dimensions of specified type", goto error);
1737 if (isl_space_check_range(space, type, first, num) < 0)
1738 return isl_space_free(space);
1739 space = isl_space_cow(space);
1740 if (!space)
1741 goto error;
1742 if (space->ids) {
1743 space = extend_ids(space);
1744 if (!space)
1745 goto error;
1746 for (i = 0; i < num; ++i)
1747 isl_id_free(get_id(space, type, first + i));
1748 for (i = first+num; i < n(space, type); ++i)
1749 set_id(space, type, i - num, get_id(space, type, i));
1750 switch (type) {
1751 case isl_dim_param:
1752 get_ids(space, isl_dim_in, 0, space->n_in,
1753 space->ids + offset(space, isl_dim_in) - num);
1754 case isl_dim_in:
1755 get_ids(space, isl_dim_out, 0, space->n_out,
1756 space->ids + offset(space, isl_dim_out) - num);
1757 default:
1760 space->n_id -= num;
1762 switch (type) {
1763 case isl_dim_param: space->nparam -= num; break;
1764 case isl_dim_in: space->n_in -= num; break;
1765 case isl_dim_out: space->n_out -= num; break;
1766 default: ;
1768 space = isl_space_reset(space, type);
1769 if (type == isl_dim_param) {
1770 if (space && space->nested[0] &&
1771 !(space->nested[0] = isl_space_drop_dims(space->nested[0],
1772 isl_dim_param, first, num)))
1773 goto error;
1774 if (space && space->nested[1] &&
1775 !(space->nested[1] = isl_space_drop_dims(space->nested[1],
1776 isl_dim_param, first, num)))
1777 goto error;
1779 return space;
1780 error:
1781 isl_space_free(space);
1782 return NULL;
1785 __isl_give isl_space *isl_space_drop_inputs(__isl_take isl_space *dim,
1786 unsigned first, unsigned n)
1788 if (!dim)
1789 return NULL;
1790 return isl_space_drop_dims(dim, isl_dim_in, first, n);
1793 __isl_give isl_space *isl_space_drop_outputs(__isl_take isl_space *dim,
1794 unsigned first, unsigned n)
1796 if (!dim)
1797 return NULL;
1798 return isl_space_drop_dims(dim, isl_dim_out, first, n);
1801 /* Remove all parameters from "space".
1803 __isl_give isl_space *isl_space_drop_all_params(__isl_take isl_space *space)
1805 isl_size nparam;
1807 nparam = isl_space_dim(space, isl_dim_param);
1808 if (nparam < 0)
1809 return isl_space_free(space);
1810 return isl_space_drop_dims(space, isl_dim_param, 0, nparam);
1813 __isl_give isl_space *isl_space_domain(__isl_take isl_space *space)
1815 if (!space)
1816 return NULL;
1817 space = isl_space_drop_dims(space, isl_dim_out, 0, space->n_out);
1818 space = isl_space_reverse(space);
1819 space = mark_as_set(space);
1820 return space;
1823 __isl_give isl_space *isl_space_from_domain(__isl_take isl_space *space)
1825 if (!space)
1826 return NULL;
1827 if (!isl_space_is_set(space))
1828 isl_die(isl_space_get_ctx(space), isl_error_invalid,
1829 "not a set space", goto error);
1830 space = isl_space_reverse(space);
1831 space = isl_space_reset(space, isl_dim_out);
1832 return space;
1833 error:
1834 isl_space_free(space);
1835 return NULL;
1838 __isl_give isl_space *isl_space_range(__isl_take isl_space *space)
1840 if (!space)
1841 return NULL;
1842 space = isl_space_drop_dims(space, isl_dim_in, 0, space->n_in);
1843 space = mark_as_set(space);
1844 return space;
1847 __isl_give isl_space *isl_space_from_range(__isl_take isl_space *space)
1849 if (!space)
1850 return NULL;
1851 if (!isl_space_is_set(space))
1852 isl_die(isl_space_get_ctx(space), isl_error_invalid,
1853 "not a set space", goto error);
1854 return isl_space_reset(space, isl_dim_in);
1855 error:
1856 isl_space_free(space);
1857 return NULL;
1860 /* Given a map space A -> B, return the map space [A -> B] -> A.
1862 __isl_give isl_space *isl_space_domain_map(__isl_take isl_space *space)
1864 isl_space *domain;
1866 domain = isl_space_from_range(isl_space_domain(isl_space_copy(space)));
1867 space = isl_space_from_domain(isl_space_wrap(space));
1868 space = isl_space_join(space, domain);
1870 return space;
1873 /* Given a map space A -> B, return the map space [A -> B] -> B.
1875 __isl_give isl_space *isl_space_range_map(__isl_take isl_space *space)
1877 isl_space *range;
1879 range = isl_space_from_range(isl_space_range(isl_space_copy(space)));
1880 space = isl_space_from_domain(isl_space_wrap(space));
1881 space = isl_space_join(space, range);
1883 return space;
1886 __isl_give isl_space *isl_space_params(__isl_take isl_space *space)
1888 isl_size n_in, n_out;
1890 if (isl_space_is_params(space))
1891 return space;
1892 n_in = isl_space_dim(space, isl_dim_in);
1893 n_out = isl_space_dim(space, isl_dim_out);
1894 if (n_in < 0 || n_out < 0)
1895 return isl_space_free(space);
1896 space = isl_space_drop_dims(space, isl_dim_in, 0, n_in);
1897 space = isl_space_drop_dims(space, isl_dim_out, 0, n_out);
1898 space = mark_as_params(space);
1899 return space;
1902 __isl_give isl_space *isl_space_set_from_params(__isl_take isl_space *space)
1904 if (!space)
1905 return NULL;
1906 if (!isl_space_is_params(space))
1907 isl_die(isl_space_get_ctx(space), isl_error_invalid,
1908 "not a parameter space", goto error);
1909 return isl_space_reset(space, isl_dim_set);
1910 error:
1911 isl_space_free(space);
1912 return NULL;
1915 __isl_give isl_space *isl_space_underlying(__isl_take isl_space *space,
1916 unsigned n_div)
1918 int i;
1919 isl_bool is_set;
1921 is_set = isl_space_is_set(space);
1922 if (is_set < 0)
1923 return isl_space_free(space);
1924 if (n_div == 0 && is_set &&
1925 space->nparam == 0 && space->n_in == 0 && space->n_id == 0)
1926 return isl_space_reset(space, isl_dim_out);
1927 space = isl_space_cow(space);
1928 if (!space)
1929 return NULL;
1930 space->n_out += space->nparam + space->n_in + n_div;
1931 space->nparam = 0;
1932 space->n_in = 0;
1934 for (i = 0; i < space->n_id; ++i)
1935 isl_id_free(get_id(space, isl_dim_out, i));
1936 space->n_id = 0;
1937 space = isl_space_reset(space, isl_dim_in);
1938 space = isl_space_reset(space, isl_dim_out);
1939 space = mark_as_set(space);
1941 return space;
1944 /* Are the two spaces the same, including positions and names of parameters?
1946 isl_bool isl_space_is_equal(__isl_keep isl_space *space1,
1947 __isl_keep isl_space *space2)
1949 isl_bool equal;
1951 if (!space1 || !space2)
1952 return isl_bool_error;
1953 if (space1 == space2)
1954 return isl_bool_true;
1955 equal = isl_space_has_equal_params(space1, space2);
1956 if (equal < 0 || !equal)
1957 return equal;
1958 return isl_space_has_equal_tuples(space1, space2);
1961 /* Do the tuples of "space1" correspond to those of the domain of "space2"?
1962 * That is, is "space1" eqaul to the domain of "space2", ignoring parameters.
1964 * "space2" is allowed to be a set space, in which case "space1"
1965 * should be a parameter space.
1967 isl_bool isl_space_has_domain_tuples(__isl_keep isl_space *space1,
1968 __isl_keep isl_space *space2)
1970 isl_bool is_set;
1972 is_set = isl_space_is_set(space1);
1973 if (is_set < 0 || !is_set)
1974 return is_set;
1975 return isl_space_tuple_is_equal(space1, isl_dim_set,
1976 space2, isl_dim_in);
1979 /* Is space1 equal to the domain of space2?
1981 * In the internal version we also allow space2 to be the space of a set,
1982 * provided space1 is a parameter space.
1984 isl_bool isl_space_is_domain_internal(__isl_keep isl_space *space1,
1985 __isl_keep isl_space *space2)
1987 isl_bool equal_params;
1989 if (!space1 || !space2)
1990 return isl_bool_error;
1991 equal_params = isl_space_has_equal_params(space1, space2);
1992 if (equal_params < 0 || !equal_params)
1993 return equal_params;
1994 return isl_space_has_domain_tuples(space1, space2);
1997 /* Is space1 equal to the domain of space2?
1999 isl_bool isl_space_is_domain(__isl_keep isl_space *space1,
2000 __isl_keep isl_space *space2)
2002 if (!space2)
2003 return isl_bool_error;
2004 if (!isl_space_is_map(space2))
2005 return isl_bool_false;
2006 return isl_space_is_domain_internal(space1, space2);
2009 /* Is space1 equal to the range of space2?
2011 * In the internal version, space2 is allowed to be the space of a set,
2012 * in which case it should be equal to space1.
2014 isl_bool isl_space_is_range_internal(__isl_keep isl_space *space1,
2015 __isl_keep isl_space *space2)
2017 isl_bool equal_params;
2019 if (!space1 || !space2)
2020 return isl_bool_error;
2021 if (!isl_space_is_set(space1))
2022 return isl_bool_false;
2023 equal_params = isl_space_has_equal_params(space1, space2);
2024 if (equal_params < 0 || !equal_params)
2025 return equal_params;
2026 return isl_space_tuple_is_equal(space1, isl_dim_set,
2027 space2, isl_dim_out);
2030 /* Is space1 equal to the range of space2?
2032 isl_bool isl_space_is_range(__isl_keep isl_space *space1,
2033 __isl_keep isl_space *space2)
2035 if (!space2)
2036 return isl_bool_error;
2037 if (!isl_space_is_map(space2))
2038 return isl_bool_false;
2039 return isl_space_is_range_internal(space1, space2);
2042 /* Update "hash" by hashing in the parameters of "space".
2044 static uint32_t isl_hash_params(uint32_t hash, __isl_keep isl_space *space)
2046 int i;
2047 isl_id *id;
2049 if (!space)
2050 return hash;
2052 isl_hash_byte(hash, space->nparam % 256);
2054 for (i = 0; i < space->nparam; ++i) {
2055 id = get_id(space, isl_dim_param, i);
2056 hash = isl_hash_id(hash, id);
2059 return hash;
2062 /* Update "hash" by hashing in the tuples of "space".
2063 * Changes in this function should be reflected in isl_hash_tuples_domain.
2065 static uint32_t isl_hash_tuples(uint32_t hash, __isl_keep isl_space *space)
2067 isl_id *id;
2069 if (!space)
2070 return hash;
2072 isl_hash_byte(hash, space->n_in % 256);
2073 isl_hash_byte(hash, space->n_out % 256);
2075 id = tuple_id(space, isl_dim_in);
2076 hash = isl_hash_id(hash, id);
2077 id = tuple_id(space, isl_dim_out);
2078 hash = isl_hash_id(hash, id);
2080 hash = isl_hash_tuples(hash, space->nested[0]);
2081 hash = isl_hash_tuples(hash, space->nested[1]);
2083 return hash;
2086 /* Update "hash" by hashing in the domain tuple of "space".
2087 * The result of this function is equal to the result of applying
2088 * isl_hash_tuples to the domain of "space".
2090 static uint32_t isl_hash_tuples_domain(uint32_t hash,
2091 __isl_keep isl_space *space)
2093 isl_id *id;
2095 if (!space)
2096 return hash;
2098 isl_hash_byte(hash, 0);
2099 isl_hash_byte(hash, space->n_in % 256);
2101 hash = isl_hash_id(hash, &isl_id_none);
2102 id = tuple_id(space, isl_dim_in);
2103 hash = isl_hash_id(hash, id);
2105 hash = isl_hash_tuples(hash, space->nested[0]);
2107 return hash;
2110 /* Return a hash value that digests the tuples of "space",
2111 * i.e., that ignores the parameters.
2113 uint32_t isl_space_get_tuple_hash(__isl_keep isl_space *space)
2115 uint32_t hash;
2117 if (!space)
2118 return 0;
2120 hash = isl_hash_init();
2121 hash = isl_hash_tuples(hash, space);
2123 return hash;
2126 uint32_t isl_space_get_hash(__isl_keep isl_space *space)
2128 uint32_t hash;
2130 if (!space)
2131 return 0;
2133 hash = isl_hash_init();
2134 hash = isl_hash_params(hash, space);
2135 hash = isl_hash_tuples(hash, space);
2137 return hash;
2140 /* Return the hash value of the domain of "space".
2141 * That is, isl_space_get_domain_hash(space) is equal to
2142 * isl_space_get_hash(isl_space_domain(space)).
2144 uint32_t isl_space_get_domain_hash(__isl_keep isl_space *space)
2146 uint32_t hash;
2148 if (!space)
2149 return 0;
2151 hash = isl_hash_init();
2152 hash = isl_hash_params(hash, space);
2153 hash = isl_hash_tuples_domain(hash, space);
2155 return hash;
2158 /* Is "space" the space of a set wrapping a map space?
2160 isl_bool isl_space_is_wrapping(__isl_keep isl_space *space)
2162 if (!space)
2163 return isl_bool_error;
2165 if (!isl_space_is_set(space))
2166 return isl_bool_false;
2168 return isl_bool_ok(space->nested[1] != NULL);
2171 /* Is "space" the space of a map where the domain is a wrapped map space?
2173 isl_bool isl_space_domain_is_wrapping(__isl_keep isl_space *space)
2175 if (!space)
2176 return isl_bool_error;
2178 if (isl_space_is_set(space))
2179 return isl_bool_false;
2181 return isl_bool_ok(space->nested[0] != NULL);
2184 /* Is "space" the space of a map where the range is a wrapped map space?
2186 isl_bool isl_space_range_is_wrapping(__isl_keep isl_space *space)
2188 if (!space)
2189 return isl_bool_error;
2191 if (isl_space_is_set(space))
2192 return isl_bool_false;
2194 return isl_bool_ok(space->nested[1] != NULL);
2197 /* Is "space" a product of two spaces?
2198 * That is, is it a wrapping set space or a map space
2199 * with wrapping domain and range?
2201 isl_bool isl_space_is_product(__isl_keep isl_space *space)
2203 isl_bool is_set;
2204 isl_bool is_product;
2206 is_set = isl_space_is_set(space);
2207 if (is_set < 0)
2208 return isl_bool_error;
2209 if (is_set)
2210 return isl_space_is_wrapping(space);
2211 is_product = isl_space_domain_is_wrapping(space);
2212 if (is_product < 0 || !is_product)
2213 return is_product;
2214 return isl_space_range_is_wrapping(space);
2217 __isl_give isl_space *isl_space_wrap(__isl_take isl_space *space)
2219 isl_space *wrap;
2221 if (!space)
2222 return NULL;
2224 wrap = isl_space_set_alloc(space->ctx,
2225 space->nparam, space->n_in + space->n_out);
2227 wrap = copy_ids(wrap, isl_dim_param, 0, space, isl_dim_param);
2228 wrap = copy_ids(wrap, isl_dim_set, 0, space, isl_dim_in);
2229 wrap = copy_ids(wrap, isl_dim_set, space->n_in, space, isl_dim_out);
2231 if (!wrap)
2232 goto error;
2234 wrap->nested[1] = space;
2236 return wrap;
2237 error:
2238 isl_space_free(space);
2239 return NULL;
2242 __isl_give isl_space *isl_space_unwrap(__isl_take isl_space *space)
2244 isl_space *unwrap;
2246 if (!space)
2247 return NULL;
2249 if (!isl_space_is_wrapping(space))
2250 isl_die(space->ctx, isl_error_invalid, "not a wrapping space",
2251 goto error);
2253 unwrap = isl_space_copy(space->nested[1]);
2254 isl_space_free(space);
2256 return unwrap;
2257 error:
2258 isl_space_free(space);
2259 return NULL;
2262 isl_bool isl_space_is_named_or_nested(__isl_keep isl_space *space,
2263 enum isl_dim_type type)
2265 if (type != isl_dim_in && type != isl_dim_out)
2266 return isl_bool_false;
2267 if (!space)
2268 return isl_bool_error;
2269 if (space->tuple_id[type - isl_dim_in])
2270 return isl_bool_true;
2271 if (space->nested[type - isl_dim_in])
2272 return isl_bool_true;
2273 return isl_bool_false;
2276 isl_bool isl_space_may_be_set(__isl_keep isl_space *space)
2278 isl_bool nested;
2279 isl_size n_in;
2281 if (!space)
2282 return isl_bool_error;
2283 if (isl_space_is_set(space))
2284 return isl_bool_true;
2285 n_in = isl_space_dim(space, isl_dim_in);
2286 if (n_in < 0)
2287 return isl_bool_error;
2288 if (n_in != 0)
2289 return isl_bool_false;
2290 nested = isl_space_is_named_or_nested(space, isl_dim_in);
2291 if (nested < 0 || nested)
2292 return isl_bool_not(nested);
2293 return isl_bool_true;
2296 __isl_give isl_space *isl_space_reset(__isl_take isl_space *space,
2297 enum isl_dim_type type)
2299 if (!isl_space_is_named_or_nested(space, type))
2300 return space;
2302 space = isl_space_cow(space);
2303 if (!space)
2304 return NULL;
2306 isl_id_free(space->tuple_id[type - isl_dim_in]);
2307 space->tuple_id[type - isl_dim_in] = NULL;
2308 isl_space_free(space->nested[type - isl_dim_in]);
2309 space->nested[type - isl_dim_in] = NULL;
2311 return space;
2314 __isl_give isl_space *isl_space_flatten(__isl_take isl_space *space)
2316 if (!space)
2317 return NULL;
2318 if (!space->nested[0] && !space->nested[1])
2319 return space;
2321 if (space->nested[0])
2322 space = isl_space_reset(space, isl_dim_in);
2323 if (space && space->nested[1])
2324 space = isl_space_reset(space, isl_dim_out);
2326 return space;
2329 __isl_give isl_space *isl_space_flatten_domain(__isl_take isl_space *space)
2331 if (!space)
2332 return NULL;
2333 if (!space->nested[0])
2334 return space;
2336 return isl_space_reset(space, isl_dim_in);
2339 __isl_give isl_space *isl_space_flatten_range(__isl_take isl_space *space)
2341 if (!space)
2342 return NULL;
2343 if (!space->nested[1])
2344 return space;
2346 return isl_space_reset(space, isl_dim_out);
2349 /* Replace the parameters of dst by those of src.
2351 __isl_give isl_space *isl_space_replace_params(__isl_take isl_space *dst,
2352 __isl_keep isl_space *src)
2354 isl_size dst_dim, src_dim;
2355 isl_bool equal_params;
2356 enum isl_dim_type type = isl_dim_param;
2358 equal_params = isl_space_has_equal_params(dst, src);
2359 if (equal_params < 0)
2360 return isl_space_free(dst);
2361 if (equal_params)
2362 return dst;
2364 dst = isl_space_cow(dst);
2366 dst_dim = isl_space_dim(dst, type);
2367 src_dim = isl_space_dim(src, type);
2368 if (dst_dim < 0 || src_dim < 0)
2369 goto error;
2371 dst = isl_space_drop_dims(dst, type, 0, dst_dim);
2372 dst = isl_space_add_dims(dst, type, src_dim);
2373 dst = copy_ids(dst, type, 0, src, type);
2375 if (dst) {
2376 int i;
2377 for (i = 0; i <= 1; ++i) {
2378 if (!dst->nested[i])
2379 continue;
2380 dst->nested[i] = isl_space_replace_params(
2381 dst->nested[i], src);
2382 if (!dst->nested[i])
2383 goto error;
2387 return dst;
2388 error:
2389 isl_space_free(dst);
2390 return NULL;
2393 /* Given a dimension specification "dim" of a set, create a dimension
2394 * specification for the lift of the set. In particular, the result
2395 * is of the form [dim -> local[..]], with n_local variables in the
2396 * range of the wrapped map.
2398 __isl_give isl_space *isl_space_lift(__isl_take isl_space *space,
2399 unsigned n_local)
2401 isl_space *local_space;
2403 if (!space)
2404 return NULL;
2406 local_space = isl_space_dup(space);
2407 local_space = isl_space_drop_dims(local_space, isl_dim_set, 0,
2408 space->n_out);
2409 local_space = isl_space_add_dims(local_space, isl_dim_set, n_local);
2410 local_space = isl_space_set_tuple_name(local_space,
2411 isl_dim_set, "local");
2412 space = isl_space_join(isl_space_from_domain(space),
2413 isl_space_from_range(local_space));
2414 space = isl_space_wrap(space);
2415 space = isl_space_set_tuple_name(space, isl_dim_set, "lifted");
2417 return space;
2420 isl_bool isl_space_can_zip(__isl_keep isl_space *space)
2422 isl_bool is_set;
2424 is_set = isl_space_is_set(space);
2425 if (is_set < 0)
2426 return isl_bool_error;
2427 if (is_set)
2428 return isl_bool_false;
2429 return isl_space_is_product(space);
2432 __isl_give isl_space *isl_space_zip(__isl_take isl_space *space)
2434 isl_space *dom, *ran;
2435 isl_space *dom_dom, *dom_ran, *ran_dom, *ran_ran;
2437 if (!isl_space_can_zip(space))
2438 isl_die(space->ctx, isl_error_invalid, "dim cannot be zipped",
2439 goto error);
2441 if (!space)
2442 return NULL;
2443 dom = isl_space_unwrap(isl_space_domain(isl_space_copy(space)));
2444 ran = isl_space_unwrap(isl_space_range(space));
2445 dom_dom = isl_space_domain(isl_space_copy(dom));
2446 dom_ran = isl_space_range(dom);
2447 ran_dom = isl_space_domain(isl_space_copy(ran));
2448 ran_ran = isl_space_range(ran);
2449 dom = isl_space_join(isl_space_from_domain(dom_dom),
2450 isl_space_from_range(ran_dom));
2451 ran = isl_space_join(isl_space_from_domain(dom_ran),
2452 isl_space_from_range(ran_ran));
2453 return isl_space_join(isl_space_from_domain(isl_space_wrap(dom)),
2454 isl_space_from_range(isl_space_wrap(ran)));
2455 error:
2456 isl_space_free(space);
2457 return NULL;
2460 /* Can we apply isl_space_curry to "space"?
2461 * That is, does is it have a map space with a nested relation in its domain?
2463 isl_bool isl_space_can_curry(__isl_keep isl_space *space)
2465 return isl_space_domain_is_wrapping(space);
2468 /* Given a space (A -> B) -> C, return the corresponding space
2469 * A -> (B -> C).
2471 __isl_give isl_space *isl_space_curry(__isl_take isl_space *space)
2473 isl_space *dom, *ran;
2474 isl_space *dom_dom, *dom_ran;
2476 if (!space)
2477 return NULL;
2479 if (!isl_space_can_curry(space))
2480 isl_die(space->ctx, isl_error_invalid,
2481 "space cannot be curried", goto error);
2483 dom = isl_space_unwrap(isl_space_domain(isl_space_copy(space)));
2484 ran = isl_space_range(space);
2485 dom_dom = isl_space_domain(isl_space_copy(dom));
2486 dom_ran = isl_space_range(dom);
2487 ran = isl_space_join(isl_space_from_domain(dom_ran),
2488 isl_space_from_range(ran));
2489 return isl_space_join(isl_space_from_domain(dom_dom),
2490 isl_space_from_range(isl_space_wrap(ran)));
2491 error:
2492 isl_space_free(space);
2493 return NULL;
2496 /* Can isl_space_range_curry be applied to "space"?
2497 * That is, does it have a nested relation in its range,
2498 * the domain of which is itself a nested relation?
2500 isl_bool isl_space_can_range_curry(__isl_keep isl_space *space)
2502 isl_bool can;
2504 if (!space)
2505 return isl_bool_error;
2506 can = isl_space_range_is_wrapping(space);
2507 if (can < 0 || !can)
2508 return can;
2509 return isl_space_can_curry(space->nested[1]);
2512 /* Given a space A -> ((B -> C) -> D), return the corresponding space
2513 * A -> (B -> (C -> D)).
2515 __isl_give isl_space *isl_space_range_curry(__isl_take isl_space *space)
2517 if (!space)
2518 return NULL;
2520 if (!isl_space_can_range_curry(space))
2521 isl_die(isl_space_get_ctx(space), isl_error_invalid,
2522 "space range cannot be curried",
2523 return isl_space_free(space));
2525 space = isl_space_cow(space);
2526 if (!space)
2527 return NULL;
2528 space->nested[1] = isl_space_curry(space->nested[1]);
2529 if (!space->nested[1])
2530 return isl_space_free(space);
2532 return space;
2535 /* Can we apply isl_space_uncurry to "space"?
2536 * That is, does it have a map space with a nested relation in its range?
2538 isl_bool isl_space_can_uncurry(__isl_keep isl_space *space)
2540 return isl_space_range_is_wrapping(space);
2543 /* Given a space A -> (B -> C), return the corresponding space
2544 * (A -> B) -> C.
2546 __isl_give isl_space *isl_space_uncurry(__isl_take isl_space *space)
2548 isl_space *dom, *ran;
2549 isl_space *ran_dom, *ran_ran;
2551 if (!space)
2552 return NULL;
2554 if (!isl_space_can_uncurry(space))
2555 isl_die(space->ctx, isl_error_invalid,
2556 "space cannot be uncurried",
2557 return isl_space_free(space));
2559 dom = isl_space_domain(isl_space_copy(space));
2560 ran = isl_space_unwrap(isl_space_range(space));
2561 ran_dom = isl_space_domain(isl_space_copy(ran));
2562 ran_ran = isl_space_range(ran);
2563 dom = isl_space_join(isl_space_from_domain(dom),
2564 isl_space_from_range(ran_dom));
2565 return isl_space_join(isl_space_from_domain(isl_space_wrap(dom)),
2566 isl_space_from_range(ran_ran));
2569 isl_bool isl_space_has_named_params(__isl_keep isl_space *space)
2571 int i;
2572 unsigned off;
2574 if (!space)
2575 return isl_bool_error;
2576 if (space->nparam == 0)
2577 return isl_bool_true;
2578 off = isl_space_offset(space, isl_dim_param);
2579 if (off + space->nparam > space->n_id)
2580 return isl_bool_false;
2581 for (i = 0; i < space->nparam; ++i)
2582 if (!space->ids[off + i])
2583 return isl_bool_false;
2584 return isl_bool_true;
2587 /* Check that "space" has only named parameters, reporting an error
2588 * if it does not.
2590 isl_stat isl_space_check_named_params(__isl_keep isl_space *space)
2592 isl_bool named;
2594 named = isl_space_has_named_params(space);
2595 if (named < 0)
2596 return isl_stat_error;
2597 if (!named)
2598 isl_die(isl_space_get_ctx(space), isl_error_invalid,
2599 "unexpected unnamed parameters", return isl_stat_error);
2601 return isl_stat_ok;
2604 /* Align the initial parameters of space1 to match the order in space2.
2606 __isl_give isl_space *isl_space_align_params(__isl_take isl_space *space1,
2607 __isl_take isl_space *space2)
2609 isl_reordering *exp;
2611 if (isl_space_check_named_params(space1) < 0 ||
2612 isl_space_check_named_params(space2) < 0)
2613 goto error;
2615 exp = isl_parameter_alignment_reordering(space1, space2);
2616 exp = isl_reordering_extend_space(exp, space1);
2617 isl_space_free(space2);
2618 space1 = isl_reordering_get_space(exp);
2619 isl_reordering_free(exp);
2620 return space1;
2621 error:
2622 isl_space_free(space1);
2623 isl_space_free(space2);
2624 return NULL;
2627 /* Given the space of set (domain), construct a space for a map
2628 * with as domain the given space and as range the range of "model".
2630 __isl_give isl_space *isl_space_extend_domain_with_range(
2631 __isl_take isl_space *space, __isl_take isl_space *model)
2633 isl_size n_out;
2635 if (!model)
2636 goto error;
2638 space = isl_space_from_domain(space);
2639 n_out = isl_space_dim(model, isl_dim_out);
2640 if (n_out < 0)
2641 goto error;
2642 space = isl_space_add_dims(space, isl_dim_out, n_out);
2643 if (isl_space_has_tuple_id(model, isl_dim_out))
2644 space = isl_space_set_tuple_id(space, isl_dim_out,
2645 isl_space_get_tuple_id(model, isl_dim_out));
2646 if (!space)
2647 goto error;
2648 if (model->nested[1]) {
2649 isl_space *nested = isl_space_copy(model->nested[1]);
2650 isl_size n_nested, n_space;
2651 nested = isl_space_align_params(nested, isl_space_copy(space));
2652 n_nested = isl_space_dim(nested, isl_dim_param);
2653 n_space = isl_space_dim(space, isl_dim_param);
2654 if (n_nested < 0 || n_space < 0)
2655 goto error;
2656 if (n_nested > n_space)
2657 nested = isl_space_drop_dims(nested, isl_dim_param,
2658 n_space, n_nested - n_space);
2659 if (!nested)
2660 goto error;
2661 space->nested[1] = nested;
2663 isl_space_free(model);
2664 return space;
2665 error:
2666 isl_space_free(model);
2667 isl_space_free(space);
2668 return NULL;
2671 /* Compare the "type" dimensions of two isl_spaces.
2673 * The order is fairly arbitrary.
2675 static int isl_space_cmp_type(__isl_keep isl_space *space1,
2676 __isl_keep isl_space *space2, enum isl_dim_type type)
2678 int cmp;
2679 isl_size dim1, dim2;
2680 isl_space *nested1, *nested2;
2682 dim1 = isl_space_dim(space1, type);
2683 dim2 = isl_space_dim(space2, type);
2684 if (dim1 < 0 || dim2 < 0)
2685 return 0;
2686 if (dim1 != dim2)
2687 return dim1 - dim2;
2689 cmp = isl_id_cmp(tuple_id(space1, type), tuple_id(space2, type));
2690 if (cmp != 0)
2691 return cmp;
2693 nested1 = nested(space1, type);
2694 nested2 = nested(space2, type);
2695 if (!nested1 != !nested2)
2696 return !nested1 - !nested2;
2698 if (nested1)
2699 return isl_space_cmp(nested1, nested2);
2701 return 0;
2704 /* Compare two isl_spaces.
2706 * The order is fairly arbitrary.
2708 int isl_space_cmp(__isl_keep isl_space *space1, __isl_keep isl_space *space2)
2710 int i;
2711 int cmp;
2713 if (space1 == space2)
2714 return 0;
2715 if (!space1)
2716 return -1;
2717 if (!space2)
2718 return 1;
2720 cmp = isl_space_cmp_type(space1, space2, isl_dim_param);
2721 if (cmp != 0)
2722 return cmp;
2723 cmp = isl_space_cmp_type(space1, space2, isl_dim_in);
2724 if (cmp != 0)
2725 return cmp;
2726 cmp = isl_space_cmp_type(space1, space2, isl_dim_out);
2727 if (cmp != 0)
2728 return cmp;
2730 if (!space1->ids && !space2->ids)
2731 return 0;
2733 for (i = 0; i < n(space1, isl_dim_param); ++i) {
2734 cmp = isl_id_cmp(get_id(space1, isl_dim_param, i),
2735 get_id(space2, isl_dim_param, i));
2736 if (cmp != 0)
2737 return cmp;
2740 return 0;