isl_basic_set_opt: avoid invalid access on error path
[platform/upstream/isl.git] / isl_space.c
index ee45615..e6f957c 100644 (file)
@@ -2,7 +2,7 @@
  * Copyright 2008-2009 Katholieke Universiteit Leuven
  * Copyright 2010      INRIA Saclay
  *
- * Use of this software is governed by the GNU LGPLv2.1 license
+ * Use of this software is governed by the MIT license
  *
  * Written by Sven Verdoolaege, K.U.Leuven, Departement
  * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
@@ -73,6 +73,16 @@ int isl_space_is_set(__isl_keep isl_space *space)
        return 1;
 }
 
+/* Is the given space that of a map?
+ */
+int isl_space_is_map(__isl_keep isl_space *space)
+{
+       if (!space)
+               return -1;
+       return space->tuple_id[0] != &isl_id_none &&
+               space->tuple_id[1] != &isl_id_none;
+}
+
 __isl_give isl_space *isl_space_set_alloc(isl_ctx *ctx,
                        unsigned nparam, unsigned dim)
 {
@@ -282,6 +292,8 @@ __isl_take isl_space *isl_space_dup(__isl_keep isl_space *dim)
        if (!dim)
                return NULL;
        dup = isl_space_alloc(dim->ctx, dim->nparam, dim->n_in, dim->n_out);
+       if (!dup)
+               return NULL;
        if (dim->tuple_id[0] &&
            !(dup->tuple_id[0] = isl_id_copy(dim->tuple_id[0])))
                goto error;
@@ -323,15 +335,15 @@ __isl_give isl_space *isl_space_copy(__isl_keep isl_space *dim)
        return dim;
 }
 
-void isl_space_free(__isl_take isl_space *dim)
+void *isl_space_free(__isl_take isl_space *dim)
 {
        int i;
 
        if (!dim)
-               return;
+               return NULL;
 
        if (--dim->ref > 0)
-               return;
+               return NULL;
 
        isl_id_free(dim->tuple_id[0]);
        isl_id_free(dim->tuple_id[1]);
@@ -345,8 +357,15 @@ void isl_space_free(__isl_take isl_space *dim)
        isl_ctx_deref(dim->ctx);
        
        free(dim);
+
+       return NULL;
 }
 
+/* Check if "s" is a valid dimension or tuple name.
+ * We currently only forbid names that look like a number.
+ *
+ * s is assumed to be non-NULL.
+ */
 static int name_ok(isl_ctx *ctx, const char *s)
 {
        char *p;
@@ -764,6 +783,8 @@ __isl_give isl_space *isl_space_extend(__isl_take isl_space *dim,
        isl_assert(dim->ctx, dim->n_out <= n_out, goto error);
 
        dim = isl_space_cow(dim);
+       if (!dim)
+               goto error;
 
        if (dim->ids) {
                ids = isl_calloc_array(dim->ctx, isl_id *,
@@ -834,6 +855,10 @@ static int valid_dim_type(enum isl_dim_type type)
        }
 }
 
+/* Insert "n" dimensions of type "type" at position "pos".
+ * If we are inserting parameters, then they are also inserted in
+ * any nested spaces.
+ */
 __isl_give isl_space *isl_space_insert_dims(__isl_take isl_space *dim,
        enum isl_dim_type type, unsigned pos, unsigned n)
 {
@@ -856,27 +881,26 @@ __isl_give isl_space *isl_space_insert_dims(__isl_take isl_space *dim,
                return NULL;
 
        if (dim->ids) {
-               enum isl_dim_type t;
+               enum isl_dim_type t, o = isl_dim_param;
                int off;
                int s[3];
-               int *size = s - isl_dim_param;
                ids = isl_calloc_array(dim->ctx, isl_id *,
                                     dim->nparam + dim->n_in + dim->n_out + n);
                if (!ids)
                        goto error;
                off = 0;
-               size[isl_dim_param] = dim->nparam;
-               size[isl_dim_in] = dim->n_in;
-               size[isl_dim_out] = dim->n_out;
+               s[isl_dim_param - o] = dim->nparam;
+               s[isl_dim_in - o] = dim->n_in;
+               s[isl_dim_out - o] = dim->n_out;
                for (t = isl_dim_param; t <= isl_dim_out; ++t) {
                        if (t != type) {
-                               get_ids(dim, t, 0, size[t], ids + off);
-                               off += size[t];
+                               get_ids(dim, t, 0, s[t - o], ids + off);
+                               off += s[t - o];
                        } else {
                                get_ids(dim, t, 0, pos, ids + off);
                                off += pos + n;
-                               get_ids(dim, t, pos, size[t] - pos, ids + off);
-                               off += size[t] - pos;
+                               get_ids(dim, t, pos, s[t - o] - pos, ids + off);
+                               off += s[t - o] - pos;
                        }
                }
                free(dim->ids);
@@ -891,6 +915,17 @@ __isl_give isl_space *isl_space_insert_dims(__isl_take isl_space *dim,
        }
        dim = isl_space_reset(dim, type);
 
+       if (type == isl_dim_param) {
+               if (dim && dim->nested[0] &&
+                   !(dim->nested[0] = isl_space_insert_dims(dim->nested[0],
+                                                   isl_dim_param, pos, n)))
+                       goto error;
+               if (dim && dim->nested[1] &&
+                   !(dim->nested[1] = isl_space_insert_dims(dim->nested[1],
+                                                   isl_dim_param, pos, n)))
+                       goto error;
+       }
+
        return dim;
 error:
        isl_space_free(dim);
@@ -925,36 +960,35 @@ __isl_give isl_space *isl_space_move_dims(__isl_take isl_space *dim,
 
        if (dim->ids) {
                isl_id **ids;
-               enum isl_dim_type t;
+               enum isl_dim_type t, o = isl_dim_param;
                int off;
                int s[3];
-               int *size = s - isl_dim_param;
                ids = isl_calloc_array(dim->ctx, isl_id *,
                                         dim->nparam + dim->n_in + dim->n_out);
                if (!ids)
                        goto error;
                off = 0;
-               size[isl_dim_param] = dim->nparam;
-               size[isl_dim_in] = dim->n_in;
-               size[isl_dim_out] = dim->n_out;
+               s[isl_dim_param - o] = dim->nparam;
+               s[isl_dim_in - o] = dim->n_in;
+               s[isl_dim_out - o] = dim->n_out;
                for (t = isl_dim_param; t <= isl_dim_out; ++t) {
                        if (t == dst_type) {
                                get_ids(dim, t, 0, dst_pos, ids + off);
                                off += dst_pos;
                                get_ids(dim, src_type, src_pos, n, ids + off);
                                off += n;
-                               get_ids(dim, t, dst_pos, size[t] - dst_pos,
+                               get_ids(dim, t, dst_pos, s[t - o] - dst_pos,
                                                ids + off);
-                               off += size[t] - dst_pos;
+                               off += s[t - o] - dst_pos;
                        } else if (t == src_type) {
                                get_ids(dim, t, 0, src_pos, ids + off);
                                off += src_pos;
                                get_ids(dim, t, src_pos + n,
-                                           size[t] - src_pos - n, ids + off);
-                               off += size[t] - src_pos - n;
+                                           s[t - o] - src_pos - n, ids + off);
+                               off += s[t - o] - src_pos - n;
                        } else {
-                               get_ids(dim, t, 0, size[t], ids + off);
-                               off += size[t];
+                               get_ids(dim, t, 0, s[t - o], ids + off);
+                               off += s[t - o];
                        }
                }
                free(dim->ids);
@@ -1431,6 +1465,8 @@ __isl_give isl_space *isl_space_underlying(__isl_take isl_space *dim,
        return dim;
 }
 
+/* Are the two spaces the same, including positions and names of parameters?
+ */
 int isl_space_is_equal(__isl_keep isl_space *dim1, __isl_keep isl_space *dim2)
 {
        if (!dim1 || !dim2)
@@ -1443,8 +1479,11 @@ int isl_space_is_equal(__isl_keep isl_space *dim1, __isl_keep isl_space *dim2)
 }
 
 /* Is space1 equal to the domain of space2?
+ *
+ * In the internal version we also allow space2 to be the space of a set,
+ * provided space1 is a parameter space.
  */
-int isl_space_is_domain(__isl_keep isl_space *space1,
+int isl_space_is_domain_internal(__isl_keep isl_space *space1,
        __isl_keep isl_space *space2)
 {
        if (!space1 || !space2)
@@ -1455,6 +1494,46 @@ int isl_space_is_domain(__isl_keep isl_space *space1,
               isl_space_tuple_match(space1, isl_dim_set, space2, isl_dim_in);
 }
 
+/* Is space1 equal to the domain of space2?
+ */
+int isl_space_is_domain(__isl_keep isl_space *space1,
+       __isl_keep isl_space *space2)
+{
+       if (!space2)
+               return -1;
+       if (!isl_space_is_map(space2))
+               return 0;
+       return isl_space_is_domain_internal(space1, space2);
+}
+
+/* Is space1 equal to the range of space2?
+ *
+ * In the internal version, space2 is allowed to be the space of a set,
+ * in which case it should be equal to space1.
+ */
+int isl_space_is_range_internal(__isl_keep isl_space *space1,
+       __isl_keep isl_space *space2)
+{
+       if (!space1 || !space2)
+               return -1;
+       if (!isl_space_is_set(space1))
+               return 0;
+       return match(space1, isl_dim_param, space2, isl_dim_param) &&
+              isl_space_tuple_match(space1, isl_dim_set, space2, isl_dim_out);
+}
+
+/* Is space1 equal to the range of space2?
+ */
+int isl_space_is_range(__isl_keep isl_space *space1,
+       __isl_keep isl_space *space2)
+{
+       if (!space2)
+               return -1;
+       if (!isl_space_is_map(space2))
+               return 0;
+       return isl_space_is_range_internal(space1, space2);
+}
+
 int isl_space_compatible(__isl_keep isl_space *dim1,
        __isl_keep isl_space *dim2)
 {
@@ -1470,9 +1549,9 @@ static uint32_t isl_hash_dim(uint32_t hash, __isl_keep isl_space *dim)
        if (!dim)
                return hash;
 
-       hash = isl_hash_builtin(hash, dim->nparam);
-       hash = isl_hash_builtin(hash, dim->n_in);
-       hash = isl_hash_builtin(hash, dim->n_out);
+       isl_hash_byte(hash, dim->nparam % 256);
+       isl_hash_byte(hash, dim->n_in % 256);
+       isl_hash_byte(hash, dim->n_out % 256);
 
        for (i = 0; i < dim->nparam; ++i) {
                id = get_id(dim, isl_dim_param, i);
@@ -1547,7 +1626,7 @@ __isl_give isl_space *isl_space_unwrap(__isl_take isl_space *dim)
                return NULL;
 
        if (!isl_space_is_wrapping(dim))
-               isl_die(dim->ctx, isl_error_invalid, "not a wrapping dim",
+               isl_die(dim->ctx, isl_error_invalid, "not a wrapping space",
                        goto error);
 
        unwrap = isl_space_copy(dim->nested[1]);
@@ -1769,6 +1848,43 @@ error:
        return NULL;
 }
 
+/* Can we apply isl_space_uncurry to "space"?
+ * That is, does it have a nested relation in its range?
+ */
+int isl_space_can_uncurry(__isl_keep isl_space *space)
+{
+       if (!space)
+               return -1;
+
+       return !!space->nested[1];
+}
+
+/* Given a space A -> (B -> C), return the corresponding space
+ * (A -> B) -> C.
+ */
+__isl_give isl_space *isl_space_uncurry(__isl_take isl_space *space)
+{
+       isl_space *dom, *ran;
+       isl_space *ran_dom, *ran_ran;
+
+       if (!space)
+               return NULL;
+
+       if (!isl_space_can_uncurry(space))
+               isl_die(space->ctx, isl_error_invalid,
+                       "space cannot be uncurried",
+                       return isl_space_free(space));
+
+       dom = isl_space_domain(isl_space_copy(space));
+       ran = isl_space_unwrap(isl_space_range(space));
+       ran_dom = isl_space_domain(isl_space_copy(ran));
+       ran_ran = isl_space_range(ran);
+       dom = isl_space_join(isl_space_from_domain(dom),
+                          isl_space_from_range(ran_dom));
+       return isl_space_join(isl_space_from_domain(isl_space_wrap(dom)),
+                           isl_space_from_range(ran_ran));
+}
+
 int isl_space_has_named_params(__isl_keep isl_space *dim)
 {
        int i;
@@ -1818,16 +1934,36 @@ error:
  * with as domain the given space and as range the range of "model".
  */
 __isl_give isl_space *isl_space_extend_domain_with_range(
-       __isl_take isl_space *domain, __isl_take isl_space *model)
+       __isl_take isl_space *space, __isl_take isl_space *model)
 {
-       isl_space *space;
+       if (!model)
+               goto error;
 
-       space = isl_space_from_domain(domain);
+       space = isl_space_from_domain(space);
        space = isl_space_add_dims(space, isl_dim_out,
                                    isl_space_dim(model, isl_dim_out));
        if (isl_space_has_tuple_id(model, isl_dim_out))
                space = isl_space_set_tuple_id(space, isl_dim_out,
                                isl_space_get_tuple_id(model, isl_dim_out));
+       if (!space)
+               goto error;
+       if (model->nested[1]) {
+               isl_space *nested = isl_space_copy(model->nested[1]);
+               int n_nested, n_space;
+               nested = isl_space_align_params(nested, isl_space_copy(space));
+               n_nested = isl_space_dim(nested, isl_dim_param);
+               n_space = isl_space_dim(space, isl_dim_param);
+               if (n_nested > n_space)
+                       nested = isl_space_drop_dims(nested, isl_dim_param,
+                                               n_space, n_nested - n_space);
+               if (!nested)
+                       goto error;
+               space->nested[1] = nested;
+       }
        isl_space_free(model);
        return space;
+error:
+       isl_space_free(model);
+       isl_space_free(space);
+       return NULL;
 }