isl_pw_qpolynomial_fold_bound: avoid access to freed memory
[platform/upstream/isl.git] / isl_dim.c
index 9cb3d7b..4b5d2c0 100644 (file)
--- a/isl_dim.c
+++ b/isl_dim.c
@@ -1,16 +1,24 @@
 /*
  * Copyright 2008-2009 Katholieke Universiteit Leuven
+ * Copyright 2010      INRIA Saclay
  *
  * Use of this software is governed by the GNU LGPLv2.1 license
  *
  * Written by Sven Verdoolaege, K.U.Leuven, Departement
  * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
+ * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
+ * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France 
  */
 
 #include <stdlib.h>
 #include <isl_dim_private.h>
 #include "isl_name.h"
 
+isl_ctx *isl_dim_get_ctx(__isl_keep isl_dim *dim)
+{
+       return dim ? dim->ctx : NULL;
+}
+
 struct isl_dim *isl_dim_alloc(struct isl_ctx *ctx,
                        unsigned nparam, unsigned n_in, unsigned n_out)
 {
@@ -30,6 +38,9 @@ struct isl_dim *isl_dim_alloc(struct isl_ctx *ctx,
        dim->tuple_name[0] = NULL;
        dim->tuple_name[1] = NULL;
 
+       dim->nested[0] = NULL;
+       dim->nested[1] = NULL;
+
        dim->n_name = 0;
        dim->names = NULL;
 
@@ -107,7 +118,8 @@ static struct isl_dim *set_name(struct isl_dim *dim,
                goto error;
 
        pos = global_pos(dim, type, pos);
-       isl_assert(ctx, pos != isl_dim_total(dim), goto error);
+       if (pos == isl_dim_total(dim))
+               goto error;
 
        if (pos >= dim->n_name) {
                if (!name)
@@ -208,6 +220,10 @@ struct isl_dim *isl_dim_dup(struct isl_dim *dim)
        if (dim->tuple_name[1] &&
            !(dup->tuple_name[1] = isl_name_copy(dim->ctx, dim->tuple_name[1])))
                goto error;
+       if (dim->nested[0] && !(dup->nested[0] = isl_dim_copy(dim->nested[0])))
+               goto error;
+       if (dim->nested[1] && !(dup->nested[1] = isl_dim_copy(dim->nested[1])))
+               goto error;
        if (!dim->names)
                return dup;
        dup = copy_names(dup, isl_dim_param, 0, dim, isl_dim_param);
@@ -252,6 +268,9 @@ void isl_dim_free(struct isl_dim *dim)
        isl_name_free(dim->ctx, dim->tuple_name[0]);
        isl_name_free(dim->ctx, dim->tuple_name[1]);
 
+       isl_dim_free(dim->nested[0]);
+       isl_dim_free(dim->nested[1]);
+
        for (i = 0; i < dim->n_name; ++i)
                isl_name_free(dim->ctx, dim->names[i]);
        free(dim->names);
@@ -354,10 +373,23 @@ static struct isl_name *tuple_name(__isl_keep isl_dim *dim,
        return NULL;
 }
 
+static __isl_keep isl_dim *nested(__isl_keep isl_dim *dim,
+       enum isl_dim_type type)
+{
+       if (!dim)
+               return NULL;
+       if (type == isl_dim_in)
+               return dim->nested[0];
+       if (type == isl_dim_out)
+               return dim->nested[1];
+       return NULL;
+}
+
 int isl_dim_tuple_match(__isl_keep isl_dim *dim1, enum isl_dim_type dim1_type,
                        __isl_keep isl_dim *dim2, enum isl_dim_type dim2_type)
 {
        struct isl_name *name1, *name2;
+       isl_dim *nested1, *nested2;
 
        if (n(dim1, dim1_type) != n(dim2, dim2_type))
                return 0;
@@ -367,6 +399,12 @@ int isl_dim_tuple_match(__isl_keep isl_dim *dim1, enum isl_dim_type dim1_type,
                return 0;
        if (name1 && name1->name != name2->name)
                return 0;
+       nested1 = nested(dim1, dim1_type);
+       nested2 = nested(dim2, dim2_type);
+       if (!nested1 ^ !nested2)
+               return 0;
+       if (nested1 && !isl_dim_equal(nested1, nested2))
+               return 0;
        return 1;
 }
 
@@ -449,18 +487,20 @@ struct isl_dim *isl_dim_add(struct isl_dim *dim, enum isl_dim_type type,
 {
        if (!dim)
                return NULL;
-       if ((type == isl_dim_in || type == isl_dim_out) &&
-           dim->tuple_name[type - isl_dim_in]) {
-               dim = isl_dim_cow(dim);
-               if (!dim)
-                       return NULL;
-               isl_name_free(dim->ctx, dim->tuple_name[type - isl_dim_in]);
-               dim->tuple_name[type - isl_dim_in] = NULL;
-       }
+       dim = isl_dim_reset(dim, type);
        switch (type) {
        case isl_dim_param:
-               return isl_dim_extend(dim,
+               dim = isl_dim_extend(dim,
                                        dim->nparam + n, dim->n_in, dim->n_out);
+               if (dim && dim->nested[0] &&
+                   !(dim->nested[0] = isl_dim_add(dim->nested[0],
+                                                   isl_dim_param, n)))
+                       goto error;
+               if (dim && dim->nested[1] &&
+                   !(dim->nested[1] = isl_dim_add(dim->nested[1],
+                                                   isl_dim_param, n)))
+                       goto error;
+               return dim;
        case isl_dim_in:
                return isl_dim_extend(dim,
                                        dim->nparam, dim->n_in + n, dim->n_out);
@@ -469,6 +509,9 @@ struct isl_dim *isl_dim_add(struct isl_dim *dim, enum isl_dim_type type,
                                        dim->nparam, dim->n_in, dim->n_out + n);
        }
        return dim;
+error:
+       isl_dim_free(dim);
+       return NULL;
 }
 
 __isl_give isl_dim *isl_dim_insert(__isl_take isl_dim *dim,
@@ -478,8 +521,8 @@ __isl_give isl_dim *isl_dim_insert(__isl_take isl_dim *dim,
 
        if (!dim)
                return NULL;
-       if (n == 0 && !isl_dim_get_tuple_name(dim, type))
-               return dim;
+       if (n == 0)
+               return isl_dim_reset(dim, type);
 
        isl_assert(dim->ctx, pos <= isl_dim_size(dim, type), goto error);
 
@@ -490,7 +533,8 @@ __isl_give isl_dim *isl_dim_insert(__isl_take isl_dim *dim,
        if (dim->names) {
                enum isl_dim_type t;
                int off;
-               int size[3];
+               int s[3];
+               int *size = s - isl_dim_param;
                names = isl_calloc_array(dim->ctx, struct isl_name *,
                                     dim->nparam + dim->n_in + dim->n_out + n);
                if (!names)
@@ -519,10 +563,7 @@ __isl_give isl_dim *isl_dim_insert(__isl_take isl_dim *dim,
        case isl_dim_in:        dim->n_in += n; break;
        case isl_dim_out:       dim->n_out += n; break;
        }
-       if (type == isl_dim_in || type == isl_dim_out) {
-               isl_name_free(dim->ctx, dim->tuple_name[type - isl_dim_in]);
-               dim->tuple_name[type - isl_dim_in] = NULL;
-       }
+       dim = isl_dim_reset(dim, type);
 
        return dim;
 error:
@@ -547,6 +588,9 @@ __isl_give isl_dim *isl_dim_move(__isl_take isl_dim *dim,
 
        isl_assert(dim->ctx, dst_type != src_type, goto error);
 
+       dim = isl_dim_reset(dim, src_type);
+       dim = isl_dim_reset(dim, dst_type);
+
        dim = isl_dim_cow(dim);
        if (!dim)
                return NULL;
@@ -555,7 +599,8 @@ __isl_give isl_dim *isl_dim_move(__isl_take isl_dim *dim,
                struct isl_name **names;
                enum isl_dim_type t;
                int off;
-               int size[3];
+               int s[3];
+               int *size = s - isl_dim_param;
                names = isl_calloc_array(dim->ctx, struct isl_name *,
                                         dim->nparam + dim->n_in + dim->n_out);
                if (!names)
@@ -634,6 +679,12 @@ struct isl_dim *isl_dim_join(struct isl_dim *left, struct isl_dim *right)
        if (dim && right->tuple_name[1] &&
            !(dim->tuple_name[1] = isl_name_copy(dim->ctx, right->tuple_name[1])))
                goto error;
+       if (dim && left->nested[0] &&
+           !(dim->nested[0] = isl_dim_copy(left->nested[0])))
+               goto error;
+       if (dim && right->nested[1] &&
+           !(dim->nested[1] = isl_dim_copy(right->nested[1])))
+               goto error;
 
        isl_dim_free(left);
        isl_dim_free(right);
@@ -647,7 +698,7 @@ error:
 
 struct isl_dim *isl_dim_product(struct isl_dim *left, struct isl_dim *right)
 {
-       struct isl_dim *dim;
+       isl_dim *dom1, *dom2, *nest1, *nest2;
 
        if (!left || !right)
                goto error;
@@ -655,21 +706,15 @@ struct isl_dim *isl_dim_product(struct isl_dim *left, struct isl_dim *right)
        isl_assert(left->ctx, match(left, isl_dim_param, right, isl_dim_param),
                        goto error);
 
-       dim = isl_dim_alloc(left->ctx, left->nparam,
-                       left->n_in + right->n_in, left->n_out + right->n_out);
-       if (!dim)
-               goto error;
+       dom1 = isl_dim_domain(isl_dim_copy(left));
+       dom2 = isl_dim_domain(isl_dim_copy(right));
+       nest1 = isl_dim_wrap(isl_dim_join(isl_dim_reverse(dom1), dom2));
 
-       dim = copy_names(dim, isl_dim_param, 0, left, isl_dim_param);
-       dim = copy_names(dim, isl_dim_in, 0, left, isl_dim_in);
-       dim = copy_names(dim, isl_dim_in, left->n_in, right, isl_dim_in);
-       dim = copy_names(dim, isl_dim_out, 0, left, isl_dim_out);
-       dim = copy_names(dim, isl_dim_out, left->n_out, right, isl_dim_out);
+       dom1 = isl_dim_range(left);
+       dom2 = isl_dim_range(right);
+       nest2 = isl_dim_wrap(isl_dim_join(isl_dim_reverse(dom1), dom2));
 
-       isl_dim_free(left);
-       isl_dim_free(right);
-
-       return dim;
+       return isl_dim_join(isl_dim_reverse(nest1), nest2);
 error:
        isl_dim_free(left);
        isl_dim_free(right);
@@ -683,7 +728,7 @@ struct isl_dim *isl_dim_map(struct isl_dim *dim)
        if (!dim)
                return NULL;
        isl_assert(dim->ctx, dim->n_in == 0, goto error);
-       if (dim->n_out == 0 && !dim->tuple_name[1])
+       if (dim->n_out == 0 && !isl_dim_is_named_or_nested(dim, isl_dim_out))
                return dim;
        dim = isl_dim_cow(dim);
        if (!dim)
@@ -705,6 +750,8 @@ struct isl_dim *isl_dim_map(struct isl_dim *dim)
        }
        isl_name_free(dim->ctx, dim->tuple_name[0]);
        dim->tuple_name[0] = isl_name_copy(dim->ctx, dim->tuple_name[1]);
+       isl_dim_free(dim->nested[0]);
+       dim->nested[0] = isl_dim_copy(dim->nested[1]);
        return dim;
 error:
        isl_dim_free(dim);
@@ -725,6 +772,7 @@ static struct isl_dim *set_names(struct isl_dim *dim, enum isl_dim_type type,
 struct isl_dim *isl_dim_reverse(struct isl_dim *dim)
 {
        unsigned t;
+       isl_dim *nested;
        struct isl_name **names = NULL;
        struct isl_name *name;
 
@@ -741,6 +789,10 @@ struct isl_dim *isl_dim_reverse(struct isl_dim *dim)
        dim->tuple_name[0] = dim->tuple_name[1];
        dim->tuple_name[1] = name;
 
+       nested = dim->nested[0];
+       dim->nested[0] = dim->nested[1];
+       dim->nested[1] = nested;
+
        if (dim->names) {
                names = isl_alloc_array(dim->ctx, struct isl_name *,
                                        dim->n_in + dim->n_out);
@@ -776,7 +828,7 @@ struct isl_dim *isl_dim_drop(struct isl_dim *dim, enum isl_dim_type type,
                return NULL;
 
        if (n == 0)
-               return dim;
+               return isl_dim_reset(dim, type);
 
        isl_assert(dim->ctx, first + num <= n(dim, type), goto error);
        dim = isl_dim_cow(dim);
@@ -807,9 +859,16 @@ struct isl_dim *isl_dim_drop(struct isl_dim *dim, enum isl_dim_type type,
        case isl_dim_in:        dim->n_in -= num; break;
        case isl_dim_out:       dim->n_out -= num; break;
        }
-       if (type == isl_dim_in || type == isl_dim_out) {
-               isl_name_free(dim->ctx, dim->tuple_name[type - isl_dim_in]);
-               dim->tuple_name[type - isl_dim_in] = NULL;
+       dim = isl_dim_reset(dim, type);
+       if (type == isl_dim_param) {
+               if (dim && dim->nested[0] &&
+                   !(dim->nested[0] = isl_dim_drop(dim->nested[0],
+                                                   isl_dim_param, first, num)))
+                       goto error;
+               if (dim && dim->nested[1] &&
+                   !(dim->nested[1] = isl_dim_drop(dim->nested[1],
+                                                   isl_dim_param, first, num)))
+                       goto error;
        }
        return dim;
 error:
@@ -841,6 +900,11 @@ struct isl_dim *isl_dim_domain(struct isl_dim *dim)
        return isl_dim_reverse(dim);
 }
 
+__isl_give isl_dim *isl_dim_from_domain(__isl_take isl_dim *dim)
+{
+       return isl_dim_reverse(dim);
+}
+
 struct isl_dim *isl_dim_range(struct isl_dim *dim)
 {
        if (!dim)
@@ -848,6 +912,11 @@ struct isl_dim *isl_dim_range(struct isl_dim *dim)
        return isl_dim_drop_inputs(dim, 0, dim->n_in);
 }
 
+__isl_give isl_dim *isl_dim_from_range(__isl_take isl_dim *dim)
+{
+       return dim;
+}
+
 __isl_give isl_dim *isl_dim_as_set_dim(__isl_take isl_dim *dim)
 {
        dim = isl_dim_cow(dim);
@@ -856,10 +925,8 @@ __isl_give isl_dim *isl_dim_as_set_dim(__isl_take isl_dim *dim)
 
        dim->n_out += dim->n_in;
        dim->n_in = 0;
-       isl_name_free(dim->ctx, dim->tuple_name[0]);
-       isl_name_free(dim->ctx, dim->tuple_name[1]);
-       dim->tuple_name[0] = NULL;
-       dim->tuple_name[1] = NULL;
+       dim = isl_dim_reset(dim, isl_dim_in);
+       dim = isl_dim_reset(dim, isl_dim_out);
 
        return dim;
 }
@@ -871,9 +938,8 @@ struct isl_dim *isl_dim_underlying(struct isl_dim *dim, unsigned n_div)
        if (!dim)
                return NULL;
        if (n_div == 0 &&
-           dim->nparam == 0 && dim->n_in == 0 && dim->n_name == 0 &&
-           !dim->tuple_name[1])
-               return dim;
+           dim->nparam == 0 && dim->n_in == 0 && dim->n_name == 0)
+               return isl_dim_reset(isl_dim_reset(dim, isl_dim_in), isl_dim_out);
        dim = isl_dim_cow(dim);
        if (!dim)
                return NULL;
@@ -884,17 +950,15 @@ struct isl_dim *isl_dim_underlying(struct isl_dim *dim, unsigned n_div)
        for (i = 0; i < dim->n_name; ++i)
                isl_name_free(dim->ctx, get_name(dim, isl_dim_out, i));
        dim->n_name = 0;
-       isl_name_free(dim->ctx, dim->tuple_name[0]);
-       isl_name_free(dim->ctx, dim->tuple_name[1]);
-       dim->tuple_name[0] = NULL;
-       dim->tuple_name[1] = NULL;
+       dim = isl_dim_reset(dim, isl_dim_in);
+       dim = isl_dim_reset(dim, isl_dim_out);
 
        return dim;
 }
 
 unsigned isl_dim_total(struct isl_dim *dim)
 {
-       return dim->nparam + dim->n_in + dim->n_out;
+       return dim ? dim->nparam + dim->n_in + dim->n_out : 0;
 }
 
 int isl_dim_equal(struct isl_dim *dim1, struct isl_dim *dim2)
@@ -910,15 +974,13 @@ int isl_dim_compatible(struct isl_dim *dim1, struct isl_dim *dim2)
               dim1->n_in + dim1->n_out == dim2->n_in + dim2->n_out;
 }
 
-uint32_t isl_dim_get_hash(__isl_keep isl_dim *dim)
+static uint32_t isl_hash_dim(uint32_t hash, __isl_keep isl_dim *dim)
 {
        int i;
-       uint32_t hash;
        struct isl_name *name;
 
        if (!dim)
-               return 0;
-       hash = isl_hash_init();
+               return hash;
 
        hash = isl_hash_builtin(hash, dim->nparam);
        hash = isl_hash_builtin(hash, dim->n_in);
@@ -934,5 +996,154 @@ uint32_t isl_dim_get_hash(__isl_keep isl_dim *dim)
        name = tuple_name(dim, isl_dim_out);
        hash = isl_hash_builtin(hash, name);
 
+       hash = isl_hash_dim(hash, dim->nested[0]);
+       hash = isl_hash_dim(hash, dim->nested[1]);
+
+       return hash;
+}
+
+uint32_t isl_dim_get_hash(__isl_keep isl_dim *dim)
+{
+       uint32_t hash;
+
+       if (!dim)
+               return 0;
+
+       hash = isl_hash_init();
+       hash = isl_hash_dim(hash, dim);
+
        return hash;
 }
+
+int isl_dim_is_wrapping(__isl_keep isl_dim *dim)
+{
+       if (!dim)
+               return -1;
+
+       if (dim->n_in != 0 || dim->tuple_name[0] || dim->nested[0])
+               return 0;
+
+       return dim->nested[1] != NULL;
+}
+
+__isl_give isl_dim *isl_dim_wrap(__isl_take isl_dim *dim)
+{
+       isl_dim *wrap;
+
+       if (!dim)
+               return NULL;
+
+       wrap = isl_dim_alloc(dim->ctx, dim->nparam, 0, dim->n_in + dim->n_out);
+
+       wrap = copy_names(wrap, isl_dim_param, 0, dim, isl_dim_param);
+       wrap = copy_names(wrap, isl_dim_set, 0, dim, isl_dim_in);
+       wrap = copy_names(wrap, isl_dim_set, dim->n_in, dim, isl_dim_out);
+
+       if (!wrap)
+               goto error;
+
+       wrap->nested[1] = dim;
+
+       return wrap;
+error:
+       isl_dim_free(dim);
+       return NULL;
+}
+
+__isl_give isl_dim *isl_dim_unwrap(__isl_take isl_dim *dim)
+{
+       isl_dim *unwrap;
+
+       if (!dim)
+               return NULL;
+
+       if (!isl_dim_is_wrapping(dim))
+               isl_die(dim->ctx, isl_error_invalid, "not a wrapping dim",
+                       goto error);
+
+       unwrap = isl_dim_copy(dim->nested[1]);
+       isl_dim_free(dim);
+
+       return unwrap;
+error:
+       isl_dim_free(dim);
+       return NULL;
+}
+
+int isl_dim_is_named_or_nested(__isl_keep isl_dim *dim, enum isl_dim_type type)
+{
+       if (type != isl_dim_in && type != isl_dim_out)
+               return 0;
+       if (!dim)
+               return -1;
+       if (dim->tuple_name[type - isl_dim_in])
+               return 1;
+       if (dim->nested[type - isl_dim_in])
+               return 1;
+       return 0;
+}
+
+__isl_give isl_dim *isl_dim_reset(__isl_take isl_dim *dim,
+       enum isl_dim_type type)
+{
+       if (!isl_dim_is_named_or_nested(dim, type))
+               return dim;
+
+       dim = isl_dim_cow(dim);
+       if (!dim)
+               return NULL;
+
+       isl_name_free(dim->ctx, dim->tuple_name[type - isl_dim_in]);
+       dim->tuple_name[type - isl_dim_in] = NULL;
+       isl_dim_free(dim->nested[type - isl_dim_in]);
+       dim->nested[type - isl_dim_in] = NULL;
+
+       return dim;
+}
+
+__isl_give isl_dim *isl_dim_flatten(__isl_take isl_dim *dim)
+{
+       if (!dim)
+               return NULL;
+       if (!dim->nested[0] && !dim->nested[1])
+               return dim;
+
+       if (dim->nested[0])
+               dim = isl_dim_reset(dim, isl_dim_in);
+       if (dim && dim->nested[1])
+               dim = isl_dim_reset(dim, isl_dim_out);
+
+       return dim;
+}
+
+/* Replace the dimensions of the given type of dst by those of src.
+ */
+__isl_give isl_dim *isl_dim_replace(__isl_take isl_dim *dst,
+       enum isl_dim_type type, __isl_keep isl_dim *src)
+{
+       dst = isl_dim_cow(dst);
+
+       if (!dst || !src)
+               goto error;
+
+       dst = isl_dim_drop(dst, type, 0, isl_dim_size(dst, type));
+       dst = isl_dim_add(dst, type, isl_dim_size(src, type));
+       dst = copy_names(dst, type, 0, src, type);
+
+       if (dst && type == isl_dim_param) {
+               int i;
+               for (i = 0; i <= 1; ++i) {
+                       if (!dst->nested[i])
+                               continue;
+                       dst->nested[i] = isl_dim_replace(dst->nested[i],
+                                                        type, src);
+                       if (!dst->nested[i])
+                               goto error;
+               }
+       }
+
+       return dst;
+error:
+       isl_dim_free(dst);
+       return NULL;
+}