isl_scheduler.c: compute_split_schedule: drop redundant variable
[isl.git] / isl_space.c
blob688bc6018fd301fc5723d14c8b4f698410f3542a
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2013-2014 Ecole Normale Superieure
6 * Use of this software is governed by the MIT license
8 * Written by Sven Verdoolaege, K.U.Leuven, Departement
9 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
10 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
11 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
12 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
15 #include <stdlib.h>
16 #include <string.h>
17 #include <isl_space_private.h>
18 #include <isl_id_private.h>
19 #include <isl_reordering.h>
21 isl_ctx *isl_space_get_ctx(__isl_keep isl_space *dim)
23 return dim ? dim->ctx : NULL;
26 __isl_give isl_space *isl_space_alloc(isl_ctx *ctx,
27 unsigned nparam, unsigned n_in, unsigned n_out)
29 isl_space *dim;
31 dim = isl_alloc_type(ctx, struct isl_space);
32 if (!dim)
33 return NULL;
35 dim->ctx = ctx;
36 isl_ctx_ref(ctx);
37 dim->ref = 1;
38 dim->nparam = nparam;
39 dim->n_in = n_in;
40 dim->n_out = n_out;
42 dim->tuple_id[0] = NULL;
43 dim->tuple_id[1] = NULL;
45 dim->nested[0] = NULL;
46 dim->nested[1] = NULL;
48 dim->n_id = 0;
49 dim->ids = NULL;
51 return dim;
54 /* Mark the space as being that of a set, by setting the domain tuple
55 * to isl_id_none.
57 static __isl_give isl_space *mark_as_set(__isl_take isl_space *space)
59 space = isl_space_cow(space);
60 if (!space)
61 return NULL;
62 space = isl_space_set_tuple_id(space, isl_dim_in, &isl_id_none);
63 return space;
66 /* Is the space that of a set?
68 isl_bool isl_space_is_set(__isl_keep isl_space *space)
70 if (!space)
71 return isl_bool_error;
72 if (space->n_in != 0 || space->nested[0])
73 return isl_bool_false;
74 if (space->tuple_id[0] != &isl_id_none)
75 return isl_bool_false;
76 return isl_bool_true;
79 /* Is the given space that of a map?
81 isl_bool isl_space_is_map(__isl_keep isl_space *space)
83 if (!space)
84 return isl_bool_error;
85 return space->tuple_id[0] != &isl_id_none &&
86 space->tuple_id[1] != &isl_id_none;
89 __isl_give isl_space *isl_space_set_alloc(isl_ctx *ctx,
90 unsigned nparam, unsigned dim)
92 isl_space *space;
93 space = isl_space_alloc(ctx, nparam, 0, dim);
94 space = mark_as_set(space);
95 return space;
98 /* Mark the space as being that of a parameter domain, by setting
99 * both tuples to isl_id_none.
101 static __isl_give isl_space *mark_as_params(isl_space *space)
103 if (!space)
104 return NULL;
105 space = isl_space_set_tuple_id(space, isl_dim_in, &isl_id_none);
106 space = isl_space_set_tuple_id(space, isl_dim_out, &isl_id_none);
107 return space;
110 /* Is the space that of a parameter domain?
112 isl_bool isl_space_is_params(__isl_keep isl_space *space)
114 if (!space)
115 return isl_bool_error;
116 if (space->n_in != 0 || space->nested[0] ||
117 space->n_out != 0 || space->nested[1])
118 return isl_bool_false;
119 if (space->tuple_id[0] != &isl_id_none)
120 return isl_bool_false;
121 if (space->tuple_id[1] != &isl_id_none)
122 return isl_bool_false;
123 return isl_bool_true;
126 /* Create a space for a parameter domain.
128 __isl_give isl_space *isl_space_params_alloc(isl_ctx *ctx, unsigned nparam)
130 isl_space *space;
131 space = isl_space_alloc(ctx, nparam, 0, 0);
132 space = mark_as_params(space);
133 return space;
136 static unsigned global_pos(__isl_keep isl_space *dim,
137 enum isl_dim_type type, unsigned pos)
139 struct isl_ctx *ctx = dim->ctx;
141 switch (type) {
142 case isl_dim_param:
143 isl_assert(ctx, pos < dim->nparam,
144 return isl_space_dim(dim, isl_dim_all));
145 return pos;
146 case isl_dim_in:
147 isl_assert(ctx, pos < dim->n_in,
148 return isl_space_dim(dim, isl_dim_all));
149 return pos + dim->nparam;
150 case isl_dim_out:
151 isl_assert(ctx, pos < dim->n_out,
152 return isl_space_dim(dim, isl_dim_all));
153 return pos + dim->nparam + dim->n_in;
154 default:
155 isl_assert(ctx, 0, return isl_space_dim(dim, isl_dim_all));
157 return isl_space_dim(dim, isl_dim_all);
160 /* Extend length of ids array to the total number of dimensions.
162 static __isl_give isl_space *extend_ids(__isl_take isl_space *dim)
164 isl_id **ids;
165 int i;
167 if (isl_space_dim(dim, isl_dim_all) <= dim->n_id)
168 return dim;
170 if (!dim->ids) {
171 dim->ids = isl_calloc_array(dim->ctx,
172 isl_id *, isl_space_dim(dim, isl_dim_all));
173 if (!dim->ids)
174 goto error;
175 } else {
176 ids = isl_realloc_array(dim->ctx, dim->ids,
177 isl_id *, isl_space_dim(dim, isl_dim_all));
178 if (!ids)
179 goto error;
180 dim->ids = ids;
181 for (i = dim->n_id; i < isl_space_dim(dim, isl_dim_all); ++i)
182 dim->ids[i] = NULL;
185 dim->n_id = isl_space_dim(dim, isl_dim_all);
187 return dim;
188 error:
189 isl_space_free(dim);
190 return NULL;
193 static __isl_give isl_space *set_id(__isl_take isl_space *dim,
194 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
196 dim = isl_space_cow(dim);
198 if (!dim)
199 goto error;
201 pos = global_pos(dim, type, pos);
202 if (pos == isl_space_dim(dim, isl_dim_all))
203 goto error;
205 if (pos >= dim->n_id) {
206 if (!id)
207 return dim;
208 dim = extend_ids(dim);
209 if (!dim)
210 goto error;
213 dim->ids[pos] = id;
215 return dim;
216 error:
217 isl_id_free(id);
218 isl_space_free(dim);
219 return NULL;
222 static __isl_keep isl_id *get_id(__isl_keep isl_space *dim,
223 enum isl_dim_type type, unsigned pos)
225 if (!dim)
226 return NULL;
228 pos = global_pos(dim, type, pos);
229 if (pos == isl_space_dim(dim, isl_dim_all))
230 return NULL;
231 if (pos >= dim->n_id)
232 return NULL;
233 return dim->ids[pos];
236 static unsigned offset(__isl_keep isl_space *dim, enum isl_dim_type type)
238 switch (type) {
239 case isl_dim_param: return 0;
240 case isl_dim_in: return dim->nparam;
241 case isl_dim_out: return dim->nparam + dim->n_in;
242 default: return 0;
246 static unsigned n(__isl_keep isl_space *dim, enum isl_dim_type type)
248 switch (type) {
249 case isl_dim_param: return dim->nparam;
250 case isl_dim_in: return dim->n_in;
251 case isl_dim_out: return dim->n_out;
252 case isl_dim_all: return dim->nparam + dim->n_in + dim->n_out;
253 default: return 0;
257 unsigned isl_space_dim(__isl_keep isl_space *dim, enum isl_dim_type type)
259 if (!dim)
260 return 0;
261 return n(dim, type);
264 unsigned isl_space_offset(__isl_keep isl_space *dim, enum isl_dim_type type)
266 if (!dim)
267 return 0;
268 return offset(dim, type);
271 static __isl_give isl_space *copy_ids(__isl_take isl_space *dst,
272 enum isl_dim_type dst_type, unsigned offset, __isl_keep isl_space *src,
273 enum isl_dim_type src_type)
275 int i;
276 isl_id *id;
278 if (!dst)
279 return NULL;
281 for (i = 0; i < n(src, src_type); ++i) {
282 id = get_id(src, src_type, i);
283 if (!id)
284 continue;
285 dst = set_id(dst, dst_type, offset + i, isl_id_copy(id));
286 if (!dst)
287 return NULL;
289 return dst;
292 __isl_take isl_space *isl_space_dup(__isl_keep isl_space *dim)
294 isl_space *dup;
295 if (!dim)
296 return NULL;
297 dup = isl_space_alloc(dim->ctx, dim->nparam, dim->n_in, dim->n_out);
298 if (!dup)
299 return NULL;
300 if (dim->tuple_id[0] &&
301 !(dup->tuple_id[0] = isl_id_copy(dim->tuple_id[0])))
302 goto error;
303 if (dim->tuple_id[1] &&
304 !(dup->tuple_id[1] = isl_id_copy(dim->tuple_id[1])))
305 goto error;
306 if (dim->nested[0] && !(dup->nested[0] = isl_space_copy(dim->nested[0])))
307 goto error;
308 if (dim->nested[1] && !(dup->nested[1] = isl_space_copy(dim->nested[1])))
309 goto error;
310 if (!dim->ids)
311 return dup;
312 dup = copy_ids(dup, isl_dim_param, 0, dim, isl_dim_param);
313 dup = copy_ids(dup, isl_dim_in, 0, dim, isl_dim_in);
314 dup = copy_ids(dup, isl_dim_out, 0, dim, isl_dim_out);
315 return dup;
316 error:
317 isl_space_free(dup);
318 return NULL;
321 __isl_give isl_space *isl_space_cow(__isl_take isl_space *dim)
323 if (!dim)
324 return NULL;
326 if (dim->ref == 1)
327 return dim;
328 dim->ref--;
329 return isl_space_dup(dim);
332 __isl_give isl_space *isl_space_copy(__isl_keep isl_space *dim)
334 if (!dim)
335 return NULL;
337 dim->ref++;
338 return dim;
341 __isl_null isl_space *isl_space_free(__isl_take isl_space *space)
343 int i;
345 if (!space)
346 return NULL;
348 if (--space->ref > 0)
349 return NULL;
351 isl_id_free(space->tuple_id[0]);
352 isl_id_free(space->tuple_id[1]);
354 isl_space_free(space->nested[0]);
355 isl_space_free(space->nested[1]);
357 for (i = 0; i < space->n_id; ++i)
358 isl_id_free(space->ids[i]);
359 free(space->ids);
360 isl_ctx_deref(space->ctx);
362 free(space);
364 return NULL;
367 /* Check if "s" is a valid dimension or tuple name.
368 * We currently only forbid names that look like a number.
370 * s is assumed to be non-NULL.
372 static int name_ok(isl_ctx *ctx, const char *s)
374 char *p;
375 long dummy;
377 dummy = strtol(s, &p, 0);
378 if (p != s)
379 isl_die(ctx, isl_error_invalid, "name looks like a number",
380 return 0);
382 return 1;
385 /* Is it possible for the given dimension type to have a tuple id?
387 static int space_can_have_id(__isl_keep isl_space *space,
388 enum isl_dim_type type)
390 if (!space)
391 return 0;
392 if (isl_space_is_params(space))
393 isl_die(space->ctx, isl_error_invalid,
394 "parameter spaces don't have tuple ids", return 0);
395 if (isl_space_is_set(space) && type != isl_dim_set)
396 isl_die(space->ctx, isl_error_invalid,
397 "set spaces can only have a set id", return 0);
398 if (type != isl_dim_in && type != isl_dim_out)
399 isl_die(space->ctx, isl_error_invalid,
400 "only input, output and set tuples can have ids",
401 return 0);
403 return 1;
406 /* Does the tuple have an id?
408 isl_bool isl_space_has_tuple_id(__isl_keep isl_space *dim,
409 enum isl_dim_type type)
411 if (!space_can_have_id(dim, type))
412 return isl_bool_error;
413 return dim->tuple_id[type - isl_dim_in] != NULL;
416 __isl_give isl_id *isl_space_get_tuple_id(__isl_keep isl_space *dim,
417 enum isl_dim_type type)
419 int has_id;
421 if (!dim)
422 return NULL;
423 has_id = isl_space_has_tuple_id(dim, type);
424 if (has_id < 0)
425 return NULL;
426 if (!has_id)
427 isl_die(dim->ctx, isl_error_invalid,
428 "tuple has no id", return NULL);
429 return isl_id_copy(dim->tuple_id[type - isl_dim_in]);
432 __isl_give isl_space *isl_space_set_tuple_id(__isl_take isl_space *dim,
433 enum isl_dim_type type, __isl_take isl_id *id)
435 dim = isl_space_cow(dim);
436 if (!dim || !id)
437 goto error;
438 if (type != isl_dim_in && type != isl_dim_out)
439 isl_die(dim->ctx, isl_error_invalid,
440 "only input, output and set tuples can have names",
441 goto error);
443 isl_id_free(dim->tuple_id[type - isl_dim_in]);
444 dim->tuple_id[type - isl_dim_in] = id;
446 return dim;
447 error:
448 isl_id_free(id);
449 isl_space_free(dim);
450 return NULL;
453 __isl_give isl_space *isl_space_reset_tuple_id(__isl_take isl_space *dim,
454 enum isl_dim_type type)
456 dim = isl_space_cow(dim);
457 if (!dim)
458 return NULL;
459 if (type != isl_dim_in && type != isl_dim_out)
460 isl_die(dim->ctx, isl_error_invalid,
461 "only input, output and set tuples can have names",
462 goto error);
464 isl_id_free(dim->tuple_id[type - isl_dim_in]);
465 dim->tuple_id[type - isl_dim_in] = NULL;
467 return dim;
468 error:
469 isl_space_free(dim);
470 return NULL;
473 /* Set the id of the given dimension of "space" to "id".
474 * If the dimension already has an id, then it is replaced.
475 * If the dimension is a parameter, then we need to change it
476 * in the nested spaces (if any) as well.
478 __isl_give isl_space *isl_space_set_dim_id(__isl_take isl_space *space,
479 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
481 space = isl_space_cow(space);
482 if (!space || !id)
483 goto error;
485 if (type == isl_dim_param) {
486 int i;
488 for (i = 0; i < 2; ++i) {
489 if (!space->nested[i])
490 continue;
491 space->nested[i] =
492 isl_space_set_dim_id(space->nested[i],
493 type, pos, isl_id_copy(id));
494 if (!space->nested[i])
495 goto error;
499 isl_id_free(get_id(space, type, pos));
500 return set_id(space, type, pos, id);
501 error:
502 isl_id_free(id);
503 isl_space_free(space);
504 return NULL;
507 /* Reset the id of the given dimension of "space".
508 * If the dimension already has an id, then it is removed.
509 * If the dimension is a parameter, then we need to reset it
510 * in the nested spaces (if any) as well.
512 __isl_give isl_space *isl_space_reset_dim_id(__isl_take isl_space *space,
513 enum isl_dim_type type, unsigned pos)
515 space = isl_space_cow(space);
516 if (!space)
517 goto error;
519 if (type == isl_dim_param) {
520 int i;
522 for (i = 0; i < 2; ++i) {
523 if (!space->nested[i])
524 continue;
525 space->nested[i] =
526 isl_space_reset_dim_id(space->nested[i],
527 type, pos);
528 if (!space->nested[i])
529 goto error;
533 isl_id_free(get_id(space, type, pos));
534 return set_id(space, type, pos, NULL);
535 error:
536 isl_space_free(space);
537 return NULL;
540 isl_bool isl_space_has_dim_id(__isl_keep isl_space *dim,
541 enum isl_dim_type type, unsigned pos)
543 if (!dim)
544 return isl_bool_error;
545 return get_id(dim, type, pos) != NULL;
548 __isl_give isl_id *isl_space_get_dim_id(__isl_keep isl_space *dim,
549 enum isl_dim_type type, unsigned pos)
551 if (!dim)
552 return NULL;
553 if (!get_id(dim, type, pos))
554 isl_die(dim->ctx, isl_error_invalid,
555 "dim has no id", return NULL);
556 return isl_id_copy(get_id(dim, type, pos));
559 __isl_give isl_space *isl_space_set_tuple_name(__isl_take isl_space *dim,
560 enum isl_dim_type type, const char *s)
562 isl_id *id;
564 if (!dim)
565 return NULL;
567 if (!s)
568 return isl_space_reset_tuple_id(dim, type);
570 if (!name_ok(dim->ctx, s))
571 goto error;
573 id = isl_id_alloc(dim->ctx, s, NULL);
574 return isl_space_set_tuple_id(dim, type, id);
575 error:
576 isl_space_free(dim);
577 return NULL;
580 /* Does the tuple have a name?
582 isl_bool isl_space_has_tuple_name(__isl_keep isl_space *space,
583 enum isl_dim_type type)
585 isl_id *id;
587 if (!space_can_have_id(space, type))
588 return isl_bool_error;
589 id = space->tuple_id[type - isl_dim_in];
590 return id && id->name;
593 const char *isl_space_get_tuple_name(__isl_keep isl_space *dim,
594 enum isl_dim_type type)
596 isl_id *id;
597 if (!dim)
598 return NULL;
599 if (type != isl_dim_in && type != isl_dim_out)
600 return NULL;
601 id = dim->tuple_id[type - isl_dim_in];
602 return id ? id->name : NULL;
605 __isl_give isl_space *isl_space_set_dim_name(__isl_take isl_space *dim,
606 enum isl_dim_type type, unsigned pos,
607 const char *s)
609 isl_id *id;
611 if (!dim)
612 return NULL;
613 if (!s)
614 return isl_space_reset_dim_id(dim, type, pos);
615 if (!name_ok(dim->ctx, s))
616 goto error;
617 id = isl_id_alloc(dim->ctx, s, NULL);
618 return isl_space_set_dim_id(dim, type, pos, id);
619 error:
620 isl_space_free(dim);
621 return NULL;
624 /* Does the given dimension have a name?
626 isl_bool isl_space_has_dim_name(__isl_keep isl_space *space,
627 enum isl_dim_type type, unsigned pos)
629 isl_id *id;
631 if (!space)
632 return isl_bool_error;
633 id = get_id(space, type, pos);
634 return id && id->name;
637 __isl_keep const char *isl_space_get_dim_name(__isl_keep isl_space *dim,
638 enum isl_dim_type type, unsigned pos)
640 isl_id *id = get_id(dim, type, pos);
641 return id ? id->name : NULL;
644 int isl_space_find_dim_by_id(__isl_keep isl_space *dim, enum isl_dim_type type,
645 __isl_keep isl_id *id)
647 int i;
648 int offset;
649 int n;
651 if (!dim || !id)
652 return -1;
654 offset = isl_space_offset(dim, type);
655 n = isl_space_dim(dim, type);
656 for (i = 0; i < n && offset + i < dim->n_id; ++i)
657 if (dim->ids[offset + i] == id)
658 return i;
660 return -1;
663 int isl_space_find_dim_by_name(__isl_keep isl_space *space,
664 enum isl_dim_type type, const char *name)
666 int i;
667 int offset;
668 int n;
670 if (!space || !name)
671 return -1;
673 offset = isl_space_offset(space, type);
674 n = isl_space_dim(space, type);
675 for (i = 0; i < n && offset + i < space->n_id; ++i)
676 if (space->ids[offset + i]->name &&
677 !strcmp(space->ids[offset + i]->name, name))
678 return i;
680 return -1;
683 /* Reset the user pointer on all identifiers of parameters and tuples
684 * of "space".
686 __isl_give isl_space *isl_space_reset_user(__isl_take isl_space *space)
688 int i;
689 isl_ctx *ctx;
690 isl_id *id;
691 const char *name;
693 if (!space)
694 return NULL;
696 ctx = isl_space_get_ctx(space);
698 for (i = 0; i < space->nparam && i < space->n_id; ++i) {
699 if (!isl_id_get_user(space->ids[i]))
700 continue;
701 space = isl_space_cow(space);
702 if (!space)
703 return NULL;
704 name = isl_id_get_name(space->ids[i]);
705 id = isl_id_alloc(ctx, name, NULL);
706 isl_id_free(space->ids[i]);
707 space->ids[i] = id;
708 if (!id)
709 return isl_space_free(space);
712 for (i = 0; i < 2; ++i) {
713 if (!space->tuple_id[i])
714 continue;
715 if (!isl_id_get_user(space->tuple_id[i]))
716 continue;
717 space = isl_space_cow(space);
718 if (!space)
719 return NULL;
720 name = isl_id_get_name(space->tuple_id[i]);
721 id = isl_id_alloc(ctx, name, NULL);
722 isl_id_free(space->tuple_id[i]);
723 space->tuple_id[i] = id;
724 if (!id)
725 return isl_space_free(space);
728 for (i = 0; i < 2; ++i) {
729 if (!space->nested[i])
730 continue;
731 space = isl_space_cow(space);
732 if (!space)
733 return NULL;
734 space->nested[i] = isl_space_reset_user(space->nested[i]);
735 if (!space->nested[i])
736 return isl_space_free(space);
739 return space;
742 static __isl_keep isl_id *tuple_id(__isl_keep isl_space *dim,
743 enum isl_dim_type type)
745 if (!dim)
746 return NULL;
747 if (type == isl_dim_in)
748 return dim->tuple_id[0];
749 if (type == isl_dim_out)
750 return dim->tuple_id[1];
751 return NULL;
754 static __isl_keep isl_space *nested(__isl_keep isl_space *dim,
755 enum isl_dim_type type)
757 if (!dim)
758 return NULL;
759 if (type == isl_dim_in)
760 return dim->nested[0];
761 if (type == isl_dim_out)
762 return dim->nested[1];
763 return NULL;
766 /* Are the two spaces the same, apart from positions and names of parameters?
768 static int isl_space_has_equal_tuples(__isl_keep isl_space *space1,
769 __isl_keep isl_space *space2)
771 if (!space1 || !space2)
772 return -1;
773 if (space1 == space2)
774 return 1;
775 return isl_space_tuple_is_equal(space1, isl_dim_in,
776 space2, isl_dim_in) &&
777 isl_space_tuple_is_equal(space1, isl_dim_out,
778 space2, isl_dim_out);
781 /* Check if the tuple of type "type1" of "space1" is the same as
782 * the tuple of type "type2" of "space2".
784 * That is, check if the tuples have the same identifier, the same dimension
785 * and the same internal structure.
786 * The identifiers of the dimensions inside the tuples do not affect the result.
788 * Note that this function only checks the tuples themselves.
789 * If nested tuples are involved, then we need to be careful not
790 * to have result affected by possibly differing parameters
791 * in those nested tuples.
793 isl_bool isl_space_tuple_is_equal(__isl_keep isl_space *space1,
794 enum isl_dim_type type1, __isl_keep isl_space *space2,
795 enum isl_dim_type type2)
797 isl_id *id1, *id2;
798 isl_space *nested1, *nested2;
800 if (!space1 || !space2)
801 return isl_bool_error;
803 if (space1 == space2 && type1 == type2)
804 return isl_bool_true;
806 if (n(space1, type1) != n(space2, type2))
807 return isl_bool_false;
808 id1 = tuple_id(space1, type1);
809 id2 = tuple_id(space2, type2);
810 if (!id1 ^ !id2)
811 return isl_bool_false;
812 if (id1 && id1 != id2)
813 return isl_bool_false;
814 nested1 = nested(space1, type1);
815 nested2 = nested(space2, type2);
816 if (!nested1 ^ !nested2)
817 return isl_bool_false;
818 if (nested1 && !isl_space_has_equal_tuples(nested1, nested2))
819 return isl_bool_false;
820 return isl_bool_true;
823 /* This is the old, undocumented, name for isl_space_tuple_is_equal.
824 * It will be removed at some point.
826 int isl_space_tuple_match(__isl_keep isl_space *space1, enum isl_dim_type type1,
827 __isl_keep isl_space *space2, enum isl_dim_type type2)
829 return isl_space_tuple_is_equal(space1, type1, space2, type2);
832 static int match(__isl_keep isl_space *dim1, enum isl_dim_type dim1_type,
833 __isl_keep isl_space *dim2, enum isl_dim_type dim2_type)
835 int i;
837 if (dim1 == dim2 && dim1_type == dim2_type)
838 return 1;
840 if (!isl_space_tuple_is_equal(dim1, dim1_type, dim2, dim2_type))
841 return 0;
843 if (!dim1->ids && !dim2->ids)
844 return 1;
846 for (i = 0; i < n(dim1, dim1_type); ++i) {
847 if (get_id(dim1, dim1_type, i) != get_id(dim2, dim2_type, i))
848 return 0;
850 return 1;
853 int isl_space_match(__isl_keep isl_space *dim1, enum isl_dim_type dim1_type,
854 __isl_keep isl_space *dim2, enum isl_dim_type dim2_type)
856 if (!dim1 || !dim2)
857 return -1;
859 return match(dim1, dim1_type, dim2, dim2_type);
862 static void get_ids(__isl_keep isl_space *dim, enum isl_dim_type type,
863 unsigned first, unsigned n, __isl_keep isl_id **ids)
865 int i;
867 for (i = 0; i < n ; ++i)
868 ids[i] = get_id(dim, type, first + i);
871 __isl_give isl_space *isl_space_extend(__isl_take isl_space *dim,
872 unsigned nparam, unsigned n_in, unsigned n_out)
874 isl_id **ids = NULL;
876 if (!dim)
877 return NULL;
878 if (dim->nparam == nparam && dim->n_in == n_in && dim->n_out == n_out)
879 return dim;
881 isl_assert(dim->ctx, dim->nparam <= nparam, goto error);
882 isl_assert(dim->ctx, dim->n_in <= n_in, goto error);
883 isl_assert(dim->ctx, dim->n_out <= n_out, goto error);
885 dim = isl_space_cow(dim);
886 if (!dim)
887 goto error;
889 if (dim->ids) {
890 ids = isl_calloc_array(dim->ctx, isl_id *,
891 nparam + n_in + n_out);
892 if (!ids)
893 goto error;
894 get_ids(dim, isl_dim_param, 0, dim->nparam, ids);
895 get_ids(dim, isl_dim_in, 0, dim->n_in, ids + nparam);
896 get_ids(dim, isl_dim_out, 0, dim->n_out, ids + nparam + n_in);
897 free(dim->ids);
898 dim->ids = ids;
899 dim->n_id = nparam + n_in + n_out;
901 dim->nparam = nparam;
902 dim->n_in = n_in;
903 dim->n_out = n_out;
905 return dim;
906 error:
907 free(ids);
908 isl_space_free(dim);
909 return NULL;
912 __isl_give isl_space *isl_space_add_dims(__isl_take isl_space *dim,
913 enum isl_dim_type type, unsigned n)
915 dim = isl_space_reset(dim, type);
916 if (!dim)
917 return NULL;
918 switch (type) {
919 case isl_dim_param:
920 dim = isl_space_extend(dim,
921 dim->nparam + n, dim->n_in, dim->n_out);
922 if (dim && dim->nested[0] &&
923 !(dim->nested[0] = isl_space_add_dims(dim->nested[0],
924 isl_dim_param, n)))
925 goto error;
926 if (dim && dim->nested[1] &&
927 !(dim->nested[1] = isl_space_add_dims(dim->nested[1],
928 isl_dim_param, n)))
929 goto error;
930 return dim;
931 case isl_dim_in:
932 return isl_space_extend(dim,
933 dim->nparam, dim->n_in + n, dim->n_out);
934 case isl_dim_out:
935 return isl_space_extend(dim,
936 dim->nparam, dim->n_in, dim->n_out + n);
937 default:
938 isl_die(dim->ctx, isl_error_invalid,
939 "cannot add dimensions of specified type", goto error);
941 error:
942 isl_space_free(dim);
943 return NULL;
946 static int valid_dim_type(enum isl_dim_type type)
948 switch (type) {
949 case isl_dim_param:
950 case isl_dim_in:
951 case isl_dim_out:
952 return 1;
953 default:
954 return 0;
958 /* Insert "n" dimensions of type "type" at position "pos".
959 * If we are inserting parameters, then they are also inserted in
960 * any nested spaces.
962 __isl_give isl_space *isl_space_insert_dims(__isl_take isl_space *dim,
963 enum isl_dim_type type, unsigned pos, unsigned n)
965 isl_id **ids = NULL;
967 if (!dim)
968 return NULL;
969 if (n == 0)
970 return isl_space_reset(dim, type);
972 if (!valid_dim_type(type))
973 isl_die(dim->ctx, isl_error_invalid,
974 "cannot insert dimensions of specified type",
975 goto error);
977 isl_assert(dim->ctx, pos <= isl_space_dim(dim, type), goto error);
979 dim = isl_space_cow(dim);
980 if (!dim)
981 return NULL;
983 if (dim->ids) {
984 enum isl_dim_type t, o = isl_dim_param;
985 int off;
986 int s[3];
987 ids = isl_calloc_array(dim->ctx, isl_id *,
988 dim->nparam + dim->n_in + dim->n_out + n);
989 if (!ids)
990 goto error;
991 off = 0;
992 s[isl_dim_param - o] = dim->nparam;
993 s[isl_dim_in - o] = dim->n_in;
994 s[isl_dim_out - o] = dim->n_out;
995 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
996 if (t != type) {
997 get_ids(dim, t, 0, s[t - o], ids + off);
998 off += s[t - o];
999 } else {
1000 get_ids(dim, t, 0, pos, ids + off);
1001 off += pos + n;
1002 get_ids(dim, t, pos, s[t - o] - pos, ids + off);
1003 off += s[t - o] - pos;
1006 free(dim->ids);
1007 dim->ids = ids;
1008 dim->n_id = dim->nparam + dim->n_in + dim->n_out + n;
1010 switch (type) {
1011 case isl_dim_param: dim->nparam += n; break;
1012 case isl_dim_in: dim->n_in += n; break;
1013 case isl_dim_out: dim->n_out += n; break;
1014 default: ;
1016 dim = isl_space_reset(dim, type);
1018 if (type == isl_dim_param) {
1019 if (dim && dim->nested[0] &&
1020 !(dim->nested[0] = isl_space_insert_dims(dim->nested[0],
1021 isl_dim_param, pos, n)))
1022 goto error;
1023 if (dim && dim->nested[1] &&
1024 !(dim->nested[1] = isl_space_insert_dims(dim->nested[1],
1025 isl_dim_param, pos, n)))
1026 goto error;
1029 return dim;
1030 error:
1031 isl_space_free(dim);
1032 return NULL;
1035 __isl_give isl_space *isl_space_move_dims(__isl_take isl_space *dim,
1036 enum isl_dim_type dst_type, unsigned dst_pos,
1037 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
1039 int i;
1041 if (!dim)
1042 return NULL;
1043 if (n == 0) {
1044 dim = isl_space_reset(dim, src_type);
1045 return isl_space_reset(dim, dst_type);
1048 isl_assert(dim->ctx, src_pos + n <= isl_space_dim(dim, src_type),
1049 goto error);
1051 if (dst_type == src_type && dst_pos == src_pos)
1052 return dim;
1054 isl_assert(dim->ctx, dst_type != src_type, goto error);
1056 dim = isl_space_reset(dim, src_type);
1057 dim = isl_space_reset(dim, dst_type);
1059 dim = isl_space_cow(dim);
1060 if (!dim)
1061 return NULL;
1063 if (dim->ids) {
1064 isl_id **ids;
1065 enum isl_dim_type t, o = isl_dim_param;
1066 int off;
1067 int s[3];
1068 ids = isl_calloc_array(dim->ctx, isl_id *,
1069 dim->nparam + dim->n_in + dim->n_out);
1070 if (!ids)
1071 goto error;
1072 off = 0;
1073 s[isl_dim_param - o] = dim->nparam;
1074 s[isl_dim_in - o] = dim->n_in;
1075 s[isl_dim_out - o] = dim->n_out;
1076 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
1077 if (t == dst_type) {
1078 get_ids(dim, t, 0, dst_pos, ids + off);
1079 off += dst_pos;
1080 get_ids(dim, src_type, src_pos, n, ids + off);
1081 off += n;
1082 get_ids(dim, t, dst_pos, s[t - o] - dst_pos,
1083 ids + off);
1084 off += s[t - o] - dst_pos;
1085 } else if (t == src_type) {
1086 get_ids(dim, t, 0, src_pos, ids + off);
1087 off += src_pos;
1088 get_ids(dim, t, src_pos + n,
1089 s[t - o] - src_pos - n, ids + off);
1090 off += s[t - o] - src_pos - n;
1091 } else {
1092 get_ids(dim, t, 0, s[t - o], ids + off);
1093 off += s[t - o];
1096 free(dim->ids);
1097 dim->ids = ids;
1098 dim->n_id = dim->nparam + dim->n_in + dim->n_out;
1101 switch (dst_type) {
1102 case isl_dim_param: dim->nparam += n; break;
1103 case isl_dim_in: dim->n_in += n; break;
1104 case isl_dim_out: dim->n_out += n; break;
1105 default: ;
1108 switch (src_type) {
1109 case isl_dim_param: dim->nparam -= n; break;
1110 case isl_dim_in: dim->n_in -= n; break;
1111 case isl_dim_out: dim->n_out -= n; break;
1112 default: ;
1115 if (dst_type != isl_dim_param && src_type != isl_dim_param)
1116 return dim;
1118 for (i = 0; i < 2; ++i) {
1119 if (!dim->nested[i])
1120 continue;
1121 dim->nested[i] = isl_space_replace(dim->nested[i],
1122 isl_dim_param, dim);
1123 if (!dim->nested[i])
1124 goto error;
1127 return dim;
1128 error:
1129 isl_space_free(dim);
1130 return NULL;
1133 __isl_give isl_space *isl_space_join(__isl_take isl_space *left,
1134 __isl_take isl_space *right)
1136 isl_space *dim;
1138 if (!left || !right)
1139 goto error;
1141 isl_assert(left->ctx, match(left, isl_dim_param, right, isl_dim_param),
1142 goto error);
1143 isl_assert(left->ctx,
1144 isl_space_tuple_is_equal(left, isl_dim_out, right, isl_dim_in),
1145 goto error);
1147 dim = isl_space_alloc(left->ctx, left->nparam, left->n_in, right->n_out);
1148 if (!dim)
1149 goto error;
1151 dim = copy_ids(dim, isl_dim_param, 0, left, isl_dim_param);
1152 dim = copy_ids(dim, isl_dim_in, 0, left, isl_dim_in);
1153 dim = copy_ids(dim, isl_dim_out, 0, right, isl_dim_out);
1155 if (dim && left->tuple_id[0] &&
1156 !(dim->tuple_id[0] = isl_id_copy(left->tuple_id[0])))
1157 goto error;
1158 if (dim && right->tuple_id[1] &&
1159 !(dim->tuple_id[1] = isl_id_copy(right->tuple_id[1])))
1160 goto error;
1161 if (dim && left->nested[0] &&
1162 !(dim->nested[0] = isl_space_copy(left->nested[0])))
1163 goto error;
1164 if (dim && right->nested[1] &&
1165 !(dim->nested[1] = isl_space_copy(right->nested[1])))
1166 goto error;
1168 isl_space_free(left);
1169 isl_space_free(right);
1171 return dim;
1172 error:
1173 isl_space_free(left);
1174 isl_space_free(right);
1175 return NULL;
1178 /* Given two map spaces { A -> C } and { B -> D }, construct the space
1179 * { [A -> B] -> [C -> D] }.
1180 * Given two set spaces { A } and { B }, construct the space { [A -> B] }.
1182 __isl_give isl_space *isl_space_product(__isl_take isl_space *left,
1183 __isl_take isl_space *right)
1185 isl_space *dom1, *dom2, *nest1, *nest2;
1186 int is_set;
1188 if (!left || !right)
1189 goto error;
1191 is_set = isl_space_is_set(left);
1192 if (is_set != isl_space_is_set(right))
1193 isl_die(isl_space_get_ctx(left), isl_error_invalid,
1194 "expecting either two set spaces or two map spaces",
1195 goto error);
1196 if (is_set)
1197 return isl_space_range_product(left, right);
1199 isl_assert(left->ctx, match(left, isl_dim_param, right, isl_dim_param),
1200 goto error);
1202 dom1 = isl_space_domain(isl_space_copy(left));
1203 dom2 = isl_space_domain(isl_space_copy(right));
1204 nest1 = isl_space_wrap(isl_space_join(isl_space_reverse(dom1), dom2));
1206 dom1 = isl_space_range(left);
1207 dom2 = isl_space_range(right);
1208 nest2 = isl_space_wrap(isl_space_join(isl_space_reverse(dom1), dom2));
1210 return isl_space_join(isl_space_reverse(nest1), nest2);
1211 error:
1212 isl_space_free(left);
1213 isl_space_free(right);
1214 return NULL;
1217 /* Given two spaces { A -> C } and { B -> C }, construct the space
1218 * { [A -> B] -> C }
1220 __isl_give isl_space *isl_space_domain_product(__isl_take isl_space *left,
1221 __isl_take isl_space *right)
1223 isl_space *ran, *dom1, *dom2, *nest;
1225 if (!left || !right)
1226 goto error;
1228 if (!match(left, isl_dim_param, right, isl_dim_param))
1229 isl_die(left->ctx, isl_error_invalid,
1230 "parameters need to match", goto error);
1231 if (!isl_space_tuple_is_equal(left, isl_dim_out, right, isl_dim_out))
1232 isl_die(left->ctx, isl_error_invalid,
1233 "ranges need to match", goto error);
1235 ran = isl_space_range(isl_space_copy(left));
1237 dom1 = isl_space_domain(left);
1238 dom2 = isl_space_domain(right);
1239 nest = isl_space_wrap(isl_space_join(isl_space_reverse(dom1), dom2));
1241 return isl_space_join(isl_space_reverse(nest), ran);
1242 error:
1243 isl_space_free(left);
1244 isl_space_free(right);
1245 return NULL;
1248 __isl_give isl_space *isl_space_range_product(__isl_take isl_space *left,
1249 __isl_take isl_space *right)
1251 isl_space *dom, *ran1, *ran2, *nest;
1253 if (!left || !right)
1254 goto error;
1256 isl_assert(left->ctx, match(left, isl_dim_param, right, isl_dim_param),
1257 goto error);
1258 if (!isl_space_tuple_is_equal(left, isl_dim_in, right, isl_dim_in))
1259 isl_die(left->ctx, isl_error_invalid,
1260 "domains need to match", goto error);
1262 dom = isl_space_domain(isl_space_copy(left));
1264 ran1 = isl_space_range(left);
1265 ran2 = isl_space_range(right);
1266 nest = isl_space_wrap(isl_space_join(isl_space_reverse(ran1), ran2));
1268 return isl_space_join(isl_space_reverse(dom), nest);
1269 error:
1270 isl_space_free(left);
1271 isl_space_free(right);
1272 return NULL;
1275 /* Given a space of the form [A -> B] -> [C -> D], return the space A -> C.
1277 __isl_give isl_space *isl_space_factor_domain(__isl_take isl_space *space)
1279 space = isl_space_domain_factor_domain(space);
1280 space = isl_space_range_factor_domain(space);
1281 return space;
1284 /* Given a space of the form [A -> B] -> [C -> D], return the space B -> D.
1286 __isl_give isl_space *isl_space_factor_range(__isl_take isl_space *space)
1288 space = isl_space_domain_factor_range(space);
1289 space = isl_space_range_factor_range(space);
1290 return space;
1293 /* Given a space of the form [A -> B] -> C, return the space A -> C.
1295 __isl_give isl_space *isl_space_domain_factor_domain(
1296 __isl_take isl_space *space)
1298 isl_space *nested;
1299 isl_space *domain;
1301 if (!space)
1302 return NULL;
1303 if (!isl_space_domain_is_wrapping(space))
1304 isl_die(isl_space_get_ctx(space), isl_error_invalid,
1305 "domain not a product", return isl_space_free(space));
1307 nested = space->nested[0];
1308 domain = isl_space_copy(space);
1309 domain = isl_space_drop_dims(domain, isl_dim_in,
1310 nested->n_in, nested->n_out);
1311 if (!domain)
1312 return isl_space_free(space);
1313 if (nested->tuple_id[0]) {
1314 domain->tuple_id[0] = isl_id_copy(nested->tuple_id[0]);
1315 if (!domain->tuple_id[0])
1316 goto error;
1318 if (nested->nested[0]) {
1319 domain->nested[0] = isl_space_copy(nested->nested[0]);
1320 if (!domain->nested[0])
1321 goto error;
1324 isl_space_free(space);
1325 return domain;
1326 error:
1327 isl_space_free(space);
1328 isl_space_free(domain);
1329 return NULL;
1332 /* Given a space of the form [A -> B] -> C, return the space B -> C.
1334 __isl_give isl_space *isl_space_domain_factor_range(
1335 __isl_take isl_space *space)
1337 isl_space *nested;
1338 isl_space *range;
1340 if (!space)
1341 return NULL;
1342 if (!isl_space_domain_is_wrapping(space))
1343 isl_die(isl_space_get_ctx(space), isl_error_invalid,
1344 "domain not a product", return isl_space_free(space));
1346 nested = space->nested[0];
1347 range = isl_space_copy(space);
1348 range = isl_space_drop_dims(range, isl_dim_in, 0, nested->n_in);
1349 if (!range)
1350 return isl_space_free(space);
1351 if (nested->tuple_id[1]) {
1352 range->tuple_id[0] = isl_id_copy(nested->tuple_id[1]);
1353 if (!range->tuple_id[0])
1354 goto error;
1356 if (nested->nested[1]) {
1357 range->nested[0] = isl_space_copy(nested->nested[1]);
1358 if (!range->nested[0])
1359 goto error;
1362 isl_space_free(space);
1363 return range;
1364 error:
1365 isl_space_free(space);
1366 isl_space_free(range);
1367 return NULL;
1370 /* Given a space of the form A -> [B -> C], return the space A -> B.
1372 __isl_give isl_space *isl_space_range_factor_domain(
1373 __isl_take isl_space *space)
1375 isl_space *nested;
1376 isl_space *domain;
1378 if (!space)
1379 return NULL;
1380 if (!isl_space_range_is_wrapping(space))
1381 isl_die(isl_space_get_ctx(space), isl_error_invalid,
1382 "range not a product", return isl_space_free(space));
1384 nested = space->nested[1];
1385 domain = isl_space_copy(space);
1386 domain = isl_space_drop_dims(domain, isl_dim_out,
1387 nested->n_in, nested->n_out);
1388 if (!domain)
1389 return isl_space_free(space);
1390 if (nested->tuple_id[0]) {
1391 domain->tuple_id[1] = isl_id_copy(nested->tuple_id[0]);
1392 if (!domain->tuple_id[1])
1393 goto error;
1395 if (nested->nested[0]) {
1396 domain->nested[1] = isl_space_copy(nested->nested[0]);
1397 if (!domain->nested[1])
1398 goto error;
1401 isl_space_free(space);
1402 return domain;
1403 error:
1404 isl_space_free(space);
1405 isl_space_free(domain);
1406 return NULL;
1409 /* Given a space of the form A -> [B -> C], return the space A -> C.
1411 __isl_give isl_space *isl_space_range_factor_range(
1412 __isl_take isl_space *space)
1414 isl_space *nested;
1415 isl_space *range;
1417 if (!space)
1418 return NULL;
1419 if (!isl_space_range_is_wrapping(space))
1420 isl_die(isl_space_get_ctx(space), isl_error_invalid,
1421 "range not a product", return isl_space_free(space));
1423 nested = space->nested[1];
1424 range = isl_space_copy(space);
1425 range = isl_space_drop_dims(range, isl_dim_out, 0, nested->n_in);
1426 if (!range)
1427 return isl_space_free(space);
1428 if (nested->tuple_id[1]) {
1429 range->tuple_id[1] = isl_id_copy(nested->tuple_id[1]);
1430 if (!range->tuple_id[1])
1431 goto error;
1433 if (nested->nested[1]) {
1434 range->nested[1] = isl_space_copy(nested->nested[1]);
1435 if (!range->nested[1])
1436 goto error;
1439 isl_space_free(space);
1440 return range;
1441 error:
1442 isl_space_free(space);
1443 isl_space_free(range);
1444 return NULL;
1447 __isl_give isl_space *isl_space_map_from_set(__isl_take isl_space *dim)
1449 isl_ctx *ctx;
1450 isl_id **ids = NULL;
1452 if (!dim)
1453 return NULL;
1454 ctx = isl_space_get_ctx(dim);
1455 if (!isl_space_is_set(dim))
1456 isl_die(ctx, isl_error_invalid, "not a set space", goto error);
1457 dim = isl_space_cow(dim);
1458 if (!dim)
1459 return NULL;
1460 if (dim->ids) {
1461 ids = isl_calloc_array(dim->ctx, isl_id *,
1462 dim->nparam + dim->n_out + dim->n_out);
1463 if (!ids)
1464 goto error;
1465 get_ids(dim, isl_dim_param, 0, dim->nparam, ids);
1466 get_ids(dim, isl_dim_out, 0, dim->n_out, ids + dim->nparam);
1468 dim->n_in = dim->n_out;
1469 if (ids) {
1470 free(dim->ids);
1471 dim->ids = ids;
1472 dim->n_id = dim->nparam + dim->n_out + dim->n_out;
1473 dim = copy_ids(dim, isl_dim_out, 0, dim, isl_dim_in);
1475 isl_id_free(dim->tuple_id[0]);
1476 dim->tuple_id[0] = isl_id_copy(dim->tuple_id[1]);
1477 isl_space_free(dim->nested[0]);
1478 dim->nested[0] = isl_space_copy(dim->nested[1]);
1479 return dim;
1480 error:
1481 isl_space_free(dim);
1482 return NULL;
1485 __isl_give isl_space *isl_space_map_from_domain_and_range(
1486 __isl_take isl_space *domain, __isl_take isl_space *range)
1488 if (!domain || !range)
1489 goto error;
1490 if (!isl_space_is_set(domain))
1491 isl_die(isl_space_get_ctx(domain), isl_error_invalid,
1492 "domain is not a set space", goto error);
1493 if (!isl_space_is_set(range))
1494 isl_die(isl_space_get_ctx(range), isl_error_invalid,
1495 "range is not a set space", goto error);
1496 return isl_space_join(isl_space_reverse(domain), range);
1497 error:
1498 isl_space_free(domain);
1499 isl_space_free(range);
1500 return NULL;
1503 static __isl_give isl_space *set_ids(__isl_take isl_space *dim,
1504 enum isl_dim_type type,
1505 unsigned first, unsigned n, __isl_take isl_id **ids)
1507 int i;
1509 for (i = 0; i < n ; ++i)
1510 dim = set_id(dim, type, first + i, ids[i]);
1512 return dim;
1515 __isl_give isl_space *isl_space_reverse(__isl_take isl_space *dim)
1517 unsigned t;
1518 isl_space *nested;
1519 isl_id **ids = NULL;
1520 isl_id *id;
1522 if (!dim)
1523 return NULL;
1524 if (match(dim, isl_dim_in, dim, isl_dim_out))
1525 return dim;
1527 dim = isl_space_cow(dim);
1528 if (!dim)
1529 return NULL;
1531 id = dim->tuple_id[0];
1532 dim->tuple_id[0] = dim->tuple_id[1];
1533 dim->tuple_id[1] = id;
1535 nested = dim->nested[0];
1536 dim->nested[0] = dim->nested[1];
1537 dim->nested[1] = nested;
1539 if (dim->ids) {
1540 int n_id = dim->n_in + dim->n_out;
1541 ids = isl_alloc_array(dim->ctx, isl_id *, n_id);
1542 if (n_id && !ids)
1543 goto error;
1544 get_ids(dim, isl_dim_in, 0, dim->n_in, ids);
1545 get_ids(dim, isl_dim_out, 0, dim->n_out, ids + dim->n_in);
1548 t = dim->n_in;
1549 dim->n_in = dim->n_out;
1550 dim->n_out = t;
1552 if (dim->ids) {
1553 dim = set_ids(dim, isl_dim_out, 0, dim->n_out, ids);
1554 dim = set_ids(dim, isl_dim_in, 0, dim->n_in, ids + dim->n_out);
1555 free(ids);
1558 return dim;
1559 error:
1560 free(ids);
1561 isl_space_free(dim);
1562 return NULL;
1565 __isl_give isl_space *isl_space_drop_dims(__isl_take isl_space *dim,
1566 enum isl_dim_type type, unsigned first, unsigned num)
1568 int i;
1570 if (!dim)
1571 return NULL;
1573 if (num == 0)
1574 return isl_space_reset(dim, type);
1576 if (!valid_dim_type(type))
1577 isl_die(dim->ctx, isl_error_invalid,
1578 "cannot drop dimensions of specified type", goto error);
1580 if (first + num > n(dim, type) || first + num < first)
1581 isl_die(isl_space_get_ctx(dim), isl_error_invalid,
1582 "index out of bounds", return isl_space_free(dim));
1583 dim = isl_space_cow(dim);
1584 if (!dim)
1585 goto error;
1586 if (dim->ids) {
1587 dim = extend_ids(dim);
1588 if (!dim)
1589 goto error;
1590 for (i = 0; i < num; ++i)
1591 isl_id_free(get_id(dim, type, first + i));
1592 for (i = first+num; i < n(dim, type); ++i)
1593 set_id(dim, type, i - num, get_id(dim, type, i));
1594 switch (type) {
1595 case isl_dim_param:
1596 get_ids(dim, isl_dim_in, 0, dim->n_in,
1597 dim->ids + offset(dim, isl_dim_in) - num);
1598 case isl_dim_in:
1599 get_ids(dim, isl_dim_out, 0, dim->n_out,
1600 dim->ids + offset(dim, isl_dim_out) - num);
1601 default:
1604 dim->n_id -= num;
1606 switch (type) {
1607 case isl_dim_param: dim->nparam -= num; break;
1608 case isl_dim_in: dim->n_in -= num; break;
1609 case isl_dim_out: dim->n_out -= num; break;
1610 default: ;
1612 dim = isl_space_reset(dim, type);
1613 if (type == isl_dim_param) {
1614 if (dim && dim->nested[0] &&
1615 !(dim->nested[0] = isl_space_drop_dims(dim->nested[0],
1616 isl_dim_param, first, num)))
1617 goto error;
1618 if (dim && dim->nested[1] &&
1619 !(dim->nested[1] = isl_space_drop_dims(dim->nested[1],
1620 isl_dim_param, first, num)))
1621 goto error;
1623 return dim;
1624 error:
1625 isl_space_free(dim);
1626 return NULL;
1629 __isl_give isl_space *isl_space_drop_inputs(__isl_take isl_space *dim,
1630 unsigned first, unsigned n)
1632 if (!dim)
1633 return NULL;
1634 return isl_space_drop_dims(dim, isl_dim_in, first, n);
1637 __isl_give isl_space *isl_space_drop_outputs(__isl_take isl_space *dim,
1638 unsigned first, unsigned n)
1640 if (!dim)
1641 return NULL;
1642 return isl_space_drop_dims(dim, isl_dim_out, first, n);
1645 __isl_give isl_space *isl_space_domain(__isl_take isl_space *dim)
1647 if (!dim)
1648 return NULL;
1649 dim = isl_space_drop_outputs(dim, 0, dim->n_out);
1650 dim = isl_space_reverse(dim);
1651 dim = mark_as_set(dim);
1652 return dim;
1655 __isl_give isl_space *isl_space_from_domain(__isl_take isl_space *dim)
1657 if (!dim)
1658 return NULL;
1659 if (!isl_space_is_set(dim))
1660 isl_die(isl_space_get_ctx(dim), isl_error_invalid,
1661 "not a set space", goto error);
1662 dim = isl_space_reverse(dim);
1663 dim = isl_space_reset(dim, isl_dim_out);
1664 return dim;
1665 error:
1666 isl_space_free(dim);
1667 return NULL;
1670 __isl_give isl_space *isl_space_range(__isl_take isl_space *dim)
1672 if (!dim)
1673 return NULL;
1674 dim = isl_space_drop_inputs(dim, 0, dim->n_in);
1675 dim = mark_as_set(dim);
1676 return dim;
1679 __isl_give isl_space *isl_space_from_range(__isl_take isl_space *dim)
1681 if (!dim)
1682 return NULL;
1683 if (!isl_space_is_set(dim))
1684 isl_die(isl_space_get_ctx(dim), isl_error_invalid,
1685 "not a set space", goto error);
1686 return isl_space_reset(dim, isl_dim_in);
1687 error:
1688 isl_space_free(dim);
1689 return NULL;
1692 /* Given a map space A -> B, return the map space [A -> B] -> A.
1694 __isl_give isl_space *isl_space_domain_map(__isl_take isl_space *space)
1696 isl_space *domain;
1698 domain = isl_space_from_range(isl_space_domain(isl_space_copy(space)));
1699 space = isl_space_from_domain(isl_space_wrap(space));
1700 space = isl_space_join(space, domain);
1702 return space;
1705 /* Given a map space A -> B, return the map space [A -> B] -> B.
1707 __isl_give isl_space *isl_space_range_map(__isl_take isl_space *space)
1709 isl_space *range;
1711 range = isl_space_from_range(isl_space_range(isl_space_copy(space)));
1712 space = isl_space_from_domain(isl_space_wrap(space));
1713 space = isl_space_join(space, range);
1715 return space;
1718 __isl_give isl_space *isl_space_params(__isl_take isl_space *space)
1720 if (isl_space_is_params(space))
1721 return space;
1722 space = isl_space_drop_dims(space,
1723 isl_dim_in, 0, isl_space_dim(space, isl_dim_in));
1724 space = isl_space_drop_dims(space,
1725 isl_dim_out, 0, isl_space_dim(space, isl_dim_out));
1726 space = mark_as_params(space);
1727 return space;
1730 __isl_give isl_space *isl_space_set_from_params(__isl_take isl_space *space)
1732 if (!space)
1733 return NULL;
1734 if (!isl_space_is_params(space))
1735 isl_die(isl_space_get_ctx(space), isl_error_invalid,
1736 "not a parameter space", goto error);
1737 return isl_space_reset(space, isl_dim_set);
1738 error:
1739 isl_space_free(space);
1740 return NULL;
1743 __isl_give isl_space *isl_space_as_set_space(__isl_take isl_space *dim)
1745 dim = isl_space_cow(dim);
1746 if (!dim)
1747 return NULL;
1749 dim->n_out += dim->n_in;
1750 dim->n_in = 0;
1751 dim = isl_space_reset(dim, isl_dim_in);
1752 dim = isl_space_reset(dim, isl_dim_out);
1754 return dim;
1757 __isl_give isl_space *isl_space_underlying(__isl_take isl_space *dim,
1758 unsigned n_div)
1760 int i;
1762 if (!dim)
1763 return NULL;
1764 if (n_div == 0 &&
1765 dim->nparam == 0 && dim->n_in == 0 && dim->n_id == 0)
1766 return isl_space_reset(isl_space_reset(dim, isl_dim_in), isl_dim_out);
1767 dim = isl_space_cow(dim);
1768 if (!dim)
1769 return NULL;
1770 dim->n_out += dim->nparam + dim->n_in + n_div;
1771 dim->nparam = 0;
1772 dim->n_in = 0;
1774 for (i = 0; i < dim->n_id; ++i)
1775 isl_id_free(get_id(dim, isl_dim_out, i));
1776 dim->n_id = 0;
1777 dim = isl_space_reset(dim, isl_dim_in);
1778 dim = isl_space_reset(dim, isl_dim_out);
1780 return dim;
1783 /* Are the two spaces the same, including positions and names of parameters?
1785 isl_bool isl_space_is_equal(__isl_keep isl_space *dim1,
1786 __isl_keep isl_space *dim2)
1788 if (!dim1 || !dim2)
1789 return isl_bool_error;
1790 if (dim1 == dim2)
1791 return isl_bool_true;
1792 return match(dim1, isl_dim_param, dim2, isl_dim_param) &&
1793 isl_space_tuple_is_equal(dim1, isl_dim_in, dim2, isl_dim_in) &&
1794 isl_space_tuple_is_equal(dim1, isl_dim_out, dim2, isl_dim_out);
1797 /* Is space1 equal to the domain of space2?
1799 * In the internal version we also allow space2 to be the space of a set,
1800 * provided space1 is a parameter space.
1802 isl_bool isl_space_is_domain_internal(__isl_keep isl_space *space1,
1803 __isl_keep isl_space *space2)
1805 if (!space1 || !space2)
1806 return isl_bool_error;
1807 if (!isl_space_is_set(space1))
1808 return isl_bool_false;
1809 return match(space1, isl_dim_param, space2, isl_dim_param) &&
1810 isl_space_tuple_is_equal(space1, isl_dim_set,
1811 space2, isl_dim_in);
1814 /* Is space1 equal to the domain of space2?
1816 isl_bool isl_space_is_domain(__isl_keep isl_space *space1,
1817 __isl_keep isl_space *space2)
1819 if (!space2)
1820 return isl_bool_error;
1821 if (!isl_space_is_map(space2))
1822 return isl_bool_false;
1823 return isl_space_is_domain_internal(space1, space2);
1826 /* Is space1 equal to the range of space2?
1828 * In the internal version, space2 is allowed to be the space of a set,
1829 * in which case it should be equal to space1.
1831 isl_bool isl_space_is_range_internal(__isl_keep isl_space *space1,
1832 __isl_keep isl_space *space2)
1834 if (!space1 || !space2)
1835 return isl_bool_error;
1836 if (!isl_space_is_set(space1))
1837 return isl_bool_false;
1838 return match(space1, isl_dim_param, space2, isl_dim_param) &&
1839 isl_space_tuple_is_equal(space1, isl_dim_set,
1840 space2, isl_dim_out);
1843 /* Is space1 equal to the range of space2?
1845 isl_bool isl_space_is_range(__isl_keep isl_space *space1,
1846 __isl_keep isl_space *space2)
1848 if (!space2)
1849 return isl_bool_error;
1850 if (!isl_space_is_map(space2))
1851 return isl_bool_false;
1852 return isl_space_is_range_internal(space1, space2);
1855 int isl_space_compatible(__isl_keep isl_space *dim1,
1856 __isl_keep isl_space *dim2)
1858 return dim1->nparam == dim2->nparam &&
1859 dim1->n_in + dim1->n_out == dim2->n_in + dim2->n_out;
1862 static uint32_t isl_hash_dim(uint32_t hash, __isl_keep isl_space *dim)
1864 int i;
1865 isl_id *id;
1867 if (!dim)
1868 return hash;
1870 isl_hash_byte(hash, dim->nparam % 256);
1871 isl_hash_byte(hash, dim->n_in % 256);
1872 isl_hash_byte(hash, dim->n_out % 256);
1874 for (i = 0; i < dim->nparam; ++i) {
1875 id = get_id(dim, isl_dim_param, i);
1876 hash = isl_hash_id(hash, id);
1879 id = tuple_id(dim, isl_dim_in);
1880 hash = isl_hash_id(hash, id);
1881 id = tuple_id(dim, isl_dim_out);
1882 hash = isl_hash_id(hash, id);
1884 hash = isl_hash_dim(hash, dim->nested[0]);
1885 hash = isl_hash_dim(hash, dim->nested[1]);
1887 return hash;
1890 uint32_t isl_space_get_hash(__isl_keep isl_space *dim)
1892 uint32_t hash;
1894 if (!dim)
1895 return 0;
1897 hash = isl_hash_init();
1898 hash = isl_hash_dim(hash, dim);
1900 return hash;
1903 isl_bool isl_space_is_wrapping(__isl_keep isl_space *dim)
1905 if (!dim)
1906 return isl_bool_error;
1908 if (!isl_space_is_set(dim))
1909 return isl_bool_false;
1911 return dim->nested[1] != NULL;
1914 /* Is "space" the space of a map where the domain is a wrapped map space?
1916 isl_bool isl_space_domain_is_wrapping(__isl_keep isl_space *space)
1918 if (!space)
1919 return isl_bool_error;
1921 if (isl_space_is_set(space))
1922 return isl_bool_false;
1924 return space->nested[0] != NULL;
1927 /* Is "space" the space of a map where the range is a wrapped map space?
1929 isl_bool isl_space_range_is_wrapping(__isl_keep isl_space *space)
1931 if (!space)
1932 return isl_bool_error;
1934 if (isl_space_is_set(space))
1935 return isl_bool_false;
1937 return space->nested[1] != NULL;
1940 __isl_give isl_space *isl_space_wrap(__isl_take isl_space *dim)
1942 isl_space *wrap;
1944 if (!dim)
1945 return NULL;
1947 wrap = isl_space_set_alloc(dim->ctx,
1948 dim->nparam, dim->n_in + dim->n_out);
1950 wrap = copy_ids(wrap, isl_dim_param, 0, dim, isl_dim_param);
1951 wrap = copy_ids(wrap, isl_dim_set, 0, dim, isl_dim_in);
1952 wrap = copy_ids(wrap, isl_dim_set, dim->n_in, dim, isl_dim_out);
1954 if (!wrap)
1955 goto error;
1957 wrap->nested[1] = dim;
1959 return wrap;
1960 error:
1961 isl_space_free(dim);
1962 return NULL;
1965 __isl_give isl_space *isl_space_unwrap(__isl_take isl_space *dim)
1967 isl_space *unwrap;
1969 if (!dim)
1970 return NULL;
1972 if (!isl_space_is_wrapping(dim))
1973 isl_die(dim->ctx, isl_error_invalid, "not a wrapping space",
1974 goto error);
1976 unwrap = isl_space_copy(dim->nested[1]);
1977 isl_space_free(dim);
1979 return unwrap;
1980 error:
1981 isl_space_free(dim);
1982 return NULL;
1985 int isl_space_is_named_or_nested(__isl_keep isl_space *dim, enum isl_dim_type type)
1987 if (type != isl_dim_in && type != isl_dim_out)
1988 return 0;
1989 if (!dim)
1990 return -1;
1991 if (dim->tuple_id[type - isl_dim_in])
1992 return 1;
1993 if (dim->nested[type - isl_dim_in])
1994 return 1;
1995 return 0;
1998 int isl_space_may_be_set(__isl_keep isl_space *dim)
2000 if (!dim)
2001 return -1;
2002 if (isl_space_is_set(dim))
2003 return 1;
2004 if (isl_space_dim(dim, isl_dim_in) != 0)
2005 return 0;
2006 if (isl_space_is_named_or_nested(dim, isl_dim_in))
2007 return 0;
2008 return 1;
2011 __isl_give isl_space *isl_space_reset(__isl_take isl_space *dim,
2012 enum isl_dim_type type)
2014 if (!isl_space_is_named_or_nested(dim, type))
2015 return dim;
2017 dim = isl_space_cow(dim);
2018 if (!dim)
2019 return NULL;
2021 isl_id_free(dim->tuple_id[type - isl_dim_in]);
2022 dim->tuple_id[type - isl_dim_in] = NULL;
2023 isl_space_free(dim->nested[type - isl_dim_in]);
2024 dim->nested[type - isl_dim_in] = NULL;
2026 return dim;
2029 __isl_give isl_space *isl_space_flatten(__isl_take isl_space *dim)
2031 if (!dim)
2032 return NULL;
2033 if (!dim->nested[0] && !dim->nested[1])
2034 return dim;
2036 if (dim->nested[0])
2037 dim = isl_space_reset(dim, isl_dim_in);
2038 if (dim && dim->nested[1])
2039 dim = isl_space_reset(dim, isl_dim_out);
2041 return dim;
2044 __isl_give isl_space *isl_space_flatten_domain(__isl_take isl_space *dim)
2046 if (!dim)
2047 return NULL;
2048 if (!dim->nested[0])
2049 return dim;
2051 return isl_space_reset(dim, isl_dim_in);
2054 __isl_give isl_space *isl_space_flatten_range(__isl_take isl_space *dim)
2056 if (!dim)
2057 return NULL;
2058 if (!dim->nested[1])
2059 return dim;
2061 return isl_space_reset(dim, isl_dim_out);
2064 /* Replace the dimensions of the given type of dst by those of src.
2066 __isl_give isl_space *isl_space_replace(__isl_take isl_space *dst,
2067 enum isl_dim_type type, __isl_keep isl_space *src)
2069 dst = isl_space_cow(dst);
2071 if (!dst || !src)
2072 goto error;
2074 dst = isl_space_drop_dims(dst, type, 0, isl_space_dim(dst, type));
2075 dst = isl_space_add_dims(dst, type, isl_space_dim(src, type));
2076 dst = copy_ids(dst, type, 0, src, type);
2078 if (dst && type == isl_dim_param) {
2079 int i;
2080 for (i = 0; i <= 1; ++i) {
2081 if (!dst->nested[i])
2082 continue;
2083 dst->nested[i] = isl_space_replace(dst->nested[i],
2084 type, src);
2085 if (!dst->nested[i])
2086 goto error;
2090 return dst;
2091 error:
2092 isl_space_free(dst);
2093 return NULL;
2096 /* Given a dimension specification "dim" of a set, create a dimension
2097 * specification for the lift of the set. In particular, the result
2098 * is of the form [dim -> local[..]], with n_local variables in the
2099 * range of the wrapped map.
2101 __isl_give isl_space *isl_space_lift(__isl_take isl_space *dim, unsigned n_local)
2103 isl_space *local_dim;
2105 if (!dim)
2106 return NULL;
2108 local_dim = isl_space_dup(dim);
2109 local_dim = isl_space_drop_dims(local_dim, isl_dim_set, 0, dim->n_out);
2110 local_dim = isl_space_add_dims(local_dim, isl_dim_set, n_local);
2111 local_dim = isl_space_set_tuple_name(local_dim, isl_dim_set, "local");
2112 dim = isl_space_join(isl_space_from_domain(dim),
2113 isl_space_from_range(local_dim));
2114 dim = isl_space_wrap(dim);
2115 dim = isl_space_set_tuple_name(dim, isl_dim_set, "lifted");
2117 return dim;
2120 isl_bool isl_space_can_zip(__isl_keep isl_space *dim)
2122 if (!dim)
2123 return isl_bool_error;
2125 return dim->nested[0] && dim->nested[1];
2128 __isl_give isl_space *isl_space_zip(__isl_take isl_space *dim)
2130 isl_space *dom, *ran;
2131 isl_space *dom_dom, *dom_ran, *ran_dom, *ran_ran;
2133 if (!isl_space_can_zip(dim))
2134 isl_die(dim->ctx, isl_error_invalid, "dim cannot be zipped",
2135 goto error);
2137 if (!dim)
2138 return NULL;
2139 dom = isl_space_unwrap(isl_space_domain(isl_space_copy(dim)));
2140 ran = isl_space_unwrap(isl_space_range(dim));
2141 dom_dom = isl_space_domain(isl_space_copy(dom));
2142 dom_ran = isl_space_range(dom);
2143 ran_dom = isl_space_domain(isl_space_copy(ran));
2144 ran_ran = isl_space_range(ran);
2145 dom = isl_space_join(isl_space_from_domain(dom_dom),
2146 isl_space_from_range(ran_dom));
2147 ran = isl_space_join(isl_space_from_domain(dom_ran),
2148 isl_space_from_range(ran_ran));
2149 return isl_space_join(isl_space_from_domain(isl_space_wrap(dom)),
2150 isl_space_from_range(isl_space_wrap(ran)));
2151 error:
2152 isl_space_free(dim);
2153 return NULL;
2156 /* Can we apply isl_space_curry to "space"?
2157 * That is, does it have a nested relation in its domain?
2159 isl_bool isl_space_can_curry(__isl_keep isl_space *space)
2161 if (!space)
2162 return isl_bool_error;
2164 return !!space->nested[0];
2167 /* Given a space (A -> B) -> C, return the corresponding space
2168 * A -> (B -> C).
2170 __isl_give isl_space *isl_space_curry(__isl_take isl_space *space)
2172 isl_space *dom, *ran;
2173 isl_space *dom_dom, *dom_ran;
2175 if (!space)
2176 return NULL;
2178 if (!isl_space_can_curry(space))
2179 isl_die(space->ctx, isl_error_invalid,
2180 "space cannot be curried", goto error);
2182 dom = isl_space_unwrap(isl_space_domain(isl_space_copy(space)));
2183 ran = isl_space_range(space);
2184 dom_dom = isl_space_domain(isl_space_copy(dom));
2185 dom_ran = isl_space_range(dom);
2186 ran = isl_space_join(isl_space_from_domain(dom_ran),
2187 isl_space_from_range(ran));
2188 return isl_space_join(isl_space_from_domain(dom_dom),
2189 isl_space_from_range(isl_space_wrap(ran)));
2190 error:
2191 isl_space_free(space);
2192 return NULL;
2195 /* Can we apply isl_space_uncurry to "space"?
2196 * That is, does it have a nested relation in its range?
2198 isl_bool isl_space_can_uncurry(__isl_keep isl_space *space)
2200 if (!space)
2201 return isl_bool_error;
2203 return !!space->nested[1];
2206 /* Given a space A -> (B -> C), return the corresponding space
2207 * (A -> B) -> C.
2209 __isl_give isl_space *isl_space_uncurry(__isl_take isl_space *space)
2211 isl_space *dom, *ran;
2212 isl_space *ran_dom, *ran_ran;
2214 if (!space)
2215 return NULL;
2217 if (!isl_space_can_uncurry(space))
2218 isl_die(space->ctx, isl_error_invalid,
2219 "space cannot be uncurried",
2220 return isl_space_free(space));
2222 dom = isl_space_domain(isl_space_copy(space));
2223 ran = isl_space_unwrap(isl_space_range(space));
2224 ran_dom = isl_space_domain(isl_space_copy(ran));
2225 ran_ran = isl_space_range(ran);
2226 dom = isl_space_join(isl_space_from_domain(dom),
2227 isl_space_from_range(ran_dom));
2228 return isl_space_join(isl_space_from_domain(isl_space_wrap(dom)),
2229 isl_space_from_range(ran_ran));
2232 int isl_space_has_named_params(__isl_keep isl_space *dim)
2234 int i;
2235 unsigned off;
2237 if (!dim)
2238 return -1;
2239 if (dim->nparam == 0)
2240 return 1;
2241 off = isl_space_offset(dim, isl_dim_param);
2242 if (off + dim->nparam > dim->n_id)
2243 return 0;
2244 for (i = 0; i < dim->nparam; ++i)
2245 if (!dim->ids[off + i])
2246 return 0;
2247 return 1;
2250 /* Align the initial parameters of dim1 to match the order in dim2.
2252 __isl_give isl_space *isl_space_align_params(__isl_take isl_space *dim1,
2253 __isl_take isl_space *dim2)
2255 isl_reordering *exp;
2257 if (!isl_space_has_named_params(dim1) || !isl_space_has_named_params(dim2))
2258 isl_die(isl_space_get_ctx(dim1), isl_error_invalid,
2259 "parameter alignment requires named parameters",
2260 goto error);
2262 dim2 = isl_space_params(dim2);
2263 exp = isl_parameter_alignment_reordering(dim1, dim2);
2264 exp = isl_reordering_extend_space(exp, dim1);
2265 isl_space_free(dim2);
2266 if (!exp)
2267 return NULL;
2268 dim1 = isl_space_copy(exp->dim);
2269 isl_reordering_free(exp);
2270 return dim1;
2271 error:
2272 isl_space_free(dim1);
2273 isl_space_free(dim2);
2274 return NULL;
2277 /* Given the space of set (domain), construct a space for a map
2278 * with as domain the given space and as range the range of "model".
2280 __isl_give isl_space *isl_space_extend_domain_with_range(
2281 __isl_take isl_space *space, __isl_take isl_space *model)
2283 if (!model)
2284 goto error;
2286 space = isl_space_from_domain(space);
2287 space = isl_space_add_dims(space, isl_dim_out,
2288 isl_space_dim(model, isl_dim_out));
2289 if (isl_space_has_tuple_id(model, isl_dim_out))
2290 space = isl_space_set_tuple_id(space, isl_dim_out,
2291 isl_space_get_tuple_id(model, isl_dim_out));
2292 if (!space)
2293 goto error;
2294 if (model->nested[1]) {
2295 isl_space *nested = isl_space_copy(model->nested[1]);
2296 int n_nested, n_space;
2297 nested = isl_space_align_params(nested, isl_space_copy(space));
2298 n_nested = isl_space_dim(nested, isl_dim_param);
2299 n_space = isl_space_dim(space, isl_dim_param);
2300 if (n_nested > n_space)
2301 nested = isl_space_drop_dims(nested, isl_dim_param,
2302 n_space, n_nested - n_space);
2303 if (!nested)
2304 goto error;
2305 space->nested[1] = nested;
2307 isl_space_free(model);
2308 return space;
2309 error:
2310 isl_space_free(model);
2311 isl_space_free(space);
2312 return NULL;
2315 /* Compare the "type" dimensions of two isl_spaces.
2317 * The order is fairly arbitrary.
2319 static int isl_space_cmp_type(__isl_keep isl_space *space1,
2320 __isl_keep isl_space *space2, enum isl_dim_type type)
2322 int cmp;
2323 isl_space *nested1, *nested2;
2325 if (isl_space_dim(space1, type) != isl_space_dim(space2, type))
2326 return isl_space_dim(space1, type) -
2327 isl_space_dim(space2, type);
2329 cmp = isl_id_cmp(tuple_id(space1, type), tuple_id(space2, type));
2330 if (cmp != 0)
2331 return cmp;
2333 nested1 = nested(space1, type);
2334 nested2 = nested(space2, type);
2335 if (!nested1 != !nested2)
2336 return !nested1 - !nested2;
2338 if (nested1)
2339 return isl_space_cmp(nested1, nested2);
2341 return 0;
2344 /* Compare two isl_spaces.
2346 * The order is fairly arbitrary.
2348 int isl_space_cmp(__isl_keep isl_space *space1, __isl_keep isl_space *space2)
2350 int i;
2351 int cmp;
2353 if (space1 == space2)
2354 return 0;
2355 if (!space1)
2356 return -1;
2357 if (!space2)
2358 return 1;
2360 cmp = isl_space_cmp_type(space1, space2, isl_dim_param);
2361 if (cmp != 0)
2362 return cmp;
2363 cmp = isl_space_cmp_type(space1, space2, isl_dim_in);
2364 if (cmp != 0)
2365 return cmp;
2366 cmp = isl_space_cmp_type(space1, space2, isl_dim_out);
2367 if (cmp != 0)
2368 return cmp;
2370 if (!space1->ids && !space2->ids)
2371 return 0;
2373 for (i = 0; i < n(space1, isl_dim_param); ++i) {
2374 cmp = isl_id_cmp(get_id(space1, isl_dim_param, i),
2375 get_id(space2, isl_dim_param, i));
2376 if (cmp != 0)
2377 return cmp;
2380 return 0;