test both context handling mechanisms of isl_pip
[platform/upstream/isl.git] / isl_fold.c
index b8de1e6..3e3599f 100644 (file)
@@ -13,8 +13,9 @@
 #include <isl_point_private.h>
 #include <isl_dim_private.h>
 #include <isl_map_private.h>
-#include <isl_lp.h>
-#include <isl_seq.h>
+#include <isl/lp.h>
+#include <isl/seq.h>
+#include <isl_mat_private.h>
 
 static __isl_give isl_qpolynomial_fold *qpolynomial_fold_alloc(
        enum isl_fold type, __isl_take isl_dim *dim, int n)
@@ -88,6 +89,32 @@ int isl_qpolynomial_fold_involves_dims(__isl_keep isl_qpolynomial_fold *fold,
        return 0;
 }
 
+__isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_set_dim_name(
+       __isl_take isl_qpolynomial_fold *fold,
+       enum isl_dim_type type, unsigned pos, const char *s)
+{
+       int i;
+
+       fold = isl_qpolynomial_fold_cow(fold);
+       if (!fold)
+               return NULL;
+       fold->dim = isl_dim_set_name(fold->dim, type, pos, s);
+       if (!fold->dim)
+               goto error;
+
+       for (i = 0; i < fold->n; ++i) {
+               fold->qp[i] = isl_qpolynomial_set_dim_name(fold->qp[i],
+                                                           type, pos, s);
+               if (!fold->qp[i])
+                       goto error;
+       }
+
+       return fold;
+error:
+       isl_qpolynomial_fold_free(fold);
+       return NULL;
+}
+
 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_drop_dims(
        __isl_take isl_qpolynomial_fold *fold,
        enum isl_dim_type type, unsigned first, unsigned n)
@@ -266,7 +293,7 @@ static int isl_qpolynomial_sign(__isl_keep isl_set *set,
                isl_qpolynomial *t;
 
                min = isl_qpolynomial_cst(isl_dim_copy(qp->dim), l);
-               base = isl_qpolynomial_pow(isl_dim_copy(qp->dim),
+               base = isl_qpolynomial_var_pow(isl_dim_copy(qp->dim),
                                                qp->upoly->var, 1);
 
                r = isl_qpolynomial_alloc(isl_dim_copy(qp->dim), 0,
@@ -503,6 +530,33 @@ error:
        return NULL;
 }
 
+__isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_gist(
+       __isl_take isl_qpolynomial_fold *fold, __isl_take isl_set *context)
+{
+       int i;
+
+       if (!fold || !context)
+               goto error;
+
+       fold = isl_qpolynomial_fold_cow(fold);
+       if (!fold)
+               return NULL;
+
+       for (i = 0; i < fold->n; ++i) {
+               fold->qp[i] = isl_qpolynomial_gist(fold->qp[i],
+                                                       isl_set_copy(context));
+               if (!fold->qp[i])
+                       goto error;
+       }
+
+       isl_set_free(context);
+       return fold;
+error:
+       isl_set_free(context);
+       isl_qpolynomial_fold_free(fold);
+       return NULL;
+}
+
 #define HAS_TYPE
 
 #undef PW
@@ -1252,6 +1306,11 @@ __isl_give isl_union_pw_qpolynomial_fold *isl_union_pw_qpolynomial_fold_add_unio
        __isl_take isl_union_pw_qpolynomial_fold *upwf,
        __isl_take isl_union_pw_qpolynomial *upwqp)
 {
+       upwf = isl_union_pw_qpolynomial_fold_align_params(upwf,
+                               isl_union_pw_qpolynomial_get_dim(upwqp));
+       upwqp = isl_union_pw_qpolynomial_align_params(upwqp,
+                               isl_union_pw_qpolynomial_fold_get_dim(upwf));
+
        upwf = isl_union_pw_qpolynomial_fold_cow(upwf);
        if (!upwf || !upwqp)
                goto error;
@@ -1268,3 +1327,170 @@ error:
        isl_union_pw_qpolynomial_free(upwqp);
        return NULL;
 }
+
+static int compatible_range(__isl_keep isl_dim *dim1, __isl_keep isl_dim *dim2)
+{
+       int m;
+       m = isl_dim_match(dim1, isl_dim_param, dim2, isl_dim_param);
+       if (m < 0 || !m)
+               return m;
+       return isl_dim_tuple_match(dim1, isl_dim_out, dim2, isl_dim_set);
+}
+
+/* Compute the intersection of the range of the map and the domain
+ * of the piecewise quasipolynomial reduction and then compute a bound
+ * on the associated quasipolynomial reduction over all elements
+ * in this intersection.
+ *
+ * We first introduce some unconstrained dimensions in the
+ * piecewise quasipolynomial, intersect the resulting domain
+ * with the wrapped map and the compute the sum.
+ */
+__isl_give isl_pw_qpolynomial_fold *isl_map_apply_pw_qpolynomial_fold(
+       __isl_take isl_map *map, __isl_take isl_pw_qpolynomial_fold *pwf,
+       int *tight)
+{
+       isl_ctx *ctx;
+       isl_set *dom;
+       isl_dim *map_dim;
+       isl_dim *pwf_dim;
+       unsigned n_in;
+       int ok;
+
+       ctx = isl_map_get_ctx(map);
+       if (!ctx)
+               goto error;
+
+       map_dim = isl_map_get_dim(map);
+       pwf_dim = isl_pw_qpolynomial_fold_get_dim(pwf);
+       ok = compatible_range(map_dim, pwf_dim);
+       isl_dim_free(map_dim);
+       isl_dim_free(pwf_dim);
+       if (!ok)
+               isl_die(ctx, isl_error_invalid, "incompatible dimensions",
+                       goto error);
+
+       n_in = isl_map_dim(map, isl_dim_in);
+       pwf = isl_pw_qpolynomial_fold_insert_dims(pwf, isl_dim_set, 0, n_in);
+
+       dom = isl_map_wrap(map);
+       pwf = isl_pw_qpolynomial_fold_reset_dim(pwf, isl_set_get_dim(dom));
+
+       pwf = isl_pw_qpolynomial_fold_intersect_domain(pwf, dom);
+       pwf = isl_pw_qpolynomial_fold_bound(pwf, tight);
+       
+       return pwf;
+error:
+       isl_map_free(map);
+       isl_pw_qpolynomial_fold_free(pwf);
+       return NULL;
+}
+
+struct isl_apply_fold_data {
+       isl_union_pw_qpolynomial_fold *upwf;
+       isl_union_pw_qpolynomial_fold *res;
+       isl_map *map;
+       int tight;
+};
+
+static int pw_qpolynomial_fold_apply(__isl_take isl_pw_qpolynomial_fold *pwf,
+       void *user)
+{
+       isl_dim *map_dim;
+       isl_dim *pwf_dim;
+       struct isl_apply_fold_data *data = user;
+       int ok;
+
+       map_dim = isl_map_get_dim(data->map);
+       pwf_dim = isl_pw_qpolynomial_fold_get_dim(pwf);
+       ok = compatible_range(map_dim, pwf_dim);
+       isl_dim_free(map_dim);
+       isl_dim_free(pwf_dim);
+
+       if (ok) {
+               pwf = isl_map_apply_pw_qpolynomial_fold(isl_map_copy(data->map),
+                                   pwf, data->tight ? &data->tight : NULL);
+               data->res = isl_union_pw_qpolynomial_fold_fold_pw_qpolynomial_fold(
+                                                       data->res, pwf);
+       } else
+               isl_pw_qpolynomial_fold_free(pwf);
+
+       return 0;
+}
+
+static int map_apply(__isl_take isl_map *map, void *user)
+{
+       struct isl_apply_fold_data *data = user;
+       int r;
+
+       data->map = map;
+       r = isl_union_pw_qpolynomial_fold_foreach_pw_qpolynomial_fold(
+                               data->upwf, &pw_qpolynomial_fold_apply, data);
+
+       isl_map_free(map);
+       return r;
+}
+
+__isl_give isl_union_pw_qpolynomial_fold *isl_union_map_apply_union_pw_qpolynomial_fold(
+       __isl_take isl_union_map *umap,
+       __isl_take isl_union_pw_qpolynomial_fold *upwf, int *tight)
+{
+       isl_dim *dim;
+       enum isl_fold type;
+       struct isl_apply_fold_data data;
+
+       upwf = isl_union_pw_qpolynomial_fold_align_params(upwf,
+                               isl_union_map_get_dim(umap));
+       umap = isl_union_map_align_params(umap,
+                               isl_union_pw_qpolynomial_fold_get_dim(upwf));
+
+       data.upwf = upwf;
+       data.tight = tight ? 1 : 0;
+       dim = isl_union_pw_qpolynomial_fold_get_dim(upwf);
+       type = isl_union_pw_qpolynomial_fold_get_type(upwf);
+       data.res = isl_union_pw_qpolynomial_fold_zero(dim, type);
+       if (isl_union_map_foreach_map(umap, &map_apply, &data) < 0)
+               goto error;
+
+       isl_union_map_free(umap);
+       isl_union_pw_qpolynomial_fold_free(upwf);
+
+       if (tight)
+               *tight = data.tight;
+
+       return data.res;
+error:
+       isl_union_map_free(umap);
+       isl_union_pw_qpolynomial_fold_free(upwf);
+       isl_union_pw_qpolynomial_fold_free(data.res);
+       return NULL;
+}
+
+/* Reorder the dimension of "fold" according to the given reordering.
+ */
+__isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_realign(
+       __isl_take isl_qpolynomial_fold *fold, __isl_take isl_reordering *r)
+{
+       int i;
+
+       fold = isl_qpolynomial_fold_cow(fold);
+       if (!fold || !r)
+               goto error;
+
+       for (i = 0; i < fold->n; ++i) {
+               fold->qp[i] = isl_qpolynomial_realign(fold->qp[i],
+                                                   isl_reordering_copy(r));
+               if (!fold->qp[i])
+                       goto error;
+       }
+
+       fold = isl_qpolynomial_fold_reset_dim(fold, isl_dim_copy(r->dim));
+
+       isl_reordering_free(r);
+
+       return fold;
+error:
+       isl_qpolynomial_fold_free(fold);
+       isl_reordering_free(r);
+       return NULL;
+}