isl_band: use isl_union_pw_multi_aff to represent partial schedule
[platform/upstream/isl.git] / isl_space.c
index 88b9626..aae979a 100644 (file)
@@ -360,20 +360,33 @@ static int name_ok(isl_ctx *ctx, const char *s)
        return 1;
 }
 
-int isl_space_has_tuple_id(__isl_keep isl_space *dim, enum isl_dim_type type)
+/* Is it possible for the given dimension type to have a tuple id?
+ */
+static int space_can_have_id(__isl_keep isl_space *space,
+       enum isl_dim_type type)
 {
-       if (!dim)
-               return -1;
-       if (isl_space_is_params(dim))
-               isl_die(dim->ctx, isl_error_invalid,
-                       "parameter spaces don't have tuple ids", return -1);
-       if (isl_space_is_set(dim) && type != isl_dim_set)
-               isl_die(dim->ctx, isl_error_invalid,
-                       "set spaces can only have a set id", return -1);
+       if (!space)
+               return 0;
+       if (isl_space_is_params(space))
+               isl_die(space->ctx, isl_error_invalid,
+                       "parameter spaces don't have tuple ids", return 0);
+       if (isl_space_is_set(space) && type != isl_dim_set)
+               isl_die(space->ctx, isl_error_invalid,
+                       "set spaces can only have a set id", return 0);
        if (type != isl_dim_in && type != isl_dim_out)
-               isl_die(dim->ctx, isl_error_invalid,
+               isl_die(space->ctx, isl_error_invalid,
                        "only input, output and set tuples can have ids",
-                       return -1);
+                       return 0);
+
+       return 1;
+}
+
+/* Does the tuple have an id?
+ */
+int isl_space_has_tuple_id(__isl_keep isl_space *dim, enum isl_dim_type type)
+{
+       if (!space_can_have_id(dim, type))
+               return -1;
        return dim->tuple_id[type - isl_dim_in] != NULL;
 }
 
@@ -434,17 +447,37 @@ error:
        return NULL;
 }
 
-__isl_give isl_space *isl_space_set_dim_id(__isl_take isl_space *dim,
+/* Set the id of the given dimension of "space" to "id".
+ * If the dimension already has an id, then it is replaced.
+ * If the dimension is a parameter, then we need to change it
+ * in the nested spaces (if any) as well.
+ */
+__isl_give isl_space *isl_space_set_dim_id(__isl_take isl_space *space,
        enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
 {
-       dim = isl_space_cow(dim);
-       if (!dim || !id)
+       space = isl_space_cow(space);
+       if (!space || !id)
                goto error;
-       isl_id_free(get_id(dim, type, pos));
-       return set_id(dim, type, pos, id);
+
+       if (type == isl_dim_param) {
+               int i;
+
+               for (i = 0; i < 2; ++i) {
+                       if (!space->nested[i])
+                               continue;
+                       space->nested[i] =
+                               isl_space_set_dim_id(space->nested[i],
+                                               type, pos, isl_id_copy(id));
+                       if (!space->nested[i])
+                               goto error;
+               }
+       }
+
+       isl_id_free(get_id(space, type, pos));
+       return set_id(space, type, pos, id);
 error:
        isl_id_free(id);
-       isl_space_free(dim);
+       isl_space_free(space);
        return NULL;
 }
 
@@ -488,6 +521,19 @@ error:
        return NULL;
 }
 
+/* Does the tuple have a name?
+ */
+int isl_space_has_tuple_name(__isl_keep isl_space *space,
+       enum isl_dim_type type)
+{
+       isl_id *id;
+
+       if (!space_can_have_id(space, type))
+               return -1;
+       id = space->tuple_id[type - isl_dim_in];
+       return id && id->name;
+}
+
 const char *isl_space_get_tuple_name(__isl_keep isl_space *dim,
         enum isl_dim_type type)
 {
@@ -517,6 +563,19 @@ error:
        return NULL;
 }
 
+/* Does the given dimension have a name?
+ */
+int isl_space_has_dim_name(__isl_keep isl_space *space,
+       enum isl_dim_type type, unsigned pos)
+{
+       isl_id *id;
+
+       if (!space)
+               return -1;
+       id = get_id(space, type, pos);
+       return id && id->name;
+}
+
 __isl_keep const char *isl_space_get_dim_name(__isl_keep isl_space *dim,
                                 enum isl_dim_type type, unsigned pos)
 {
@@ -1618,7 +1677,7 @@ __isl_give isl_space *isl_space_zip(__isl_take isl_space *dim)
                        goto error);
 
        if (!dim)
-               return 0;
+               return NULL;
        dom = isl_space_unwrap(isl_space_domain(isl_space_copy(dim)));
        ran = isl_space_unwrap(isl_space_range(dim));
        dom_dom = isl_space_domain(isl_space_copy(dom));
@@ -1636,6 +1695,45 @@ error:
        return NULL;
 }
 
+/* Can we apply isl_space_curry to "space"?
+ * That is, does it have a nested relation in its domain?
+ */
+int isl_space_can_curry(__isl_keep isl_space *space)
+{
+       if (!space)
+               return -1;
+
+       return !!space->nested[0];
+}
+
+/* Given a space (A -> B) -> C, return the corresponding space
+ * A -> (B -> C).
+ */
+__isl_give isl_space *isl_space_curry(__isl_take isl_space *space)
+{
+       isl_space *dom, *ran;
+       isl_space *dom_dom, *dom_ran;
+
+       if (!space)
+               return NULL;
+
+       if (!isl_space_can_curry(space))
+               isl_die(space->ctx, isl_error_invalid,
+                       "space cannot be curried", goto error);
+
+       dom = isl_space_unwrap(isl_space_domain(isl_space_copy(space)));
+       ran = isl_space_range(space);
+       dom_dom = isl_space_domain(isl_space_copy(dom));
+       dom_ran = isl_space_range(dom);
+       ran = isl_space_join(isl_space_from_domain(dom_ran),
+                          isl_space_from_range(ran));
+       return isl_space_join(isl_space_from_domain(dom_dom),
+                           isl_space_from_range(isl_space_wrap(ran)));
+error:
+       isl_space_free(space);
+       return NULL;
+}
+
 int isl_space_has_named_params(__isl_keep isl_space *dim)
 {
        int i;