update .gitignore
[platform/upstream/isl.git] / isl_fold.c
index 036c296..3286bd7 100644 (file)
@@ -8,9 +8,10 @@
  * 91893 Orsay, France 
  */
 
+#include <isl_union_map_private.h>
 #include <isl_polynomial_private.h>
 #include <isl_point_private.h>
-#include <isl_dim.h>
+#include <isl_dim_private.h>
 #include <isl_map_private.h>
 #include <isl_lp.h>
 #include <isl_seq.h>
@@ -24,7 +25,7 @@ static __isl_give isl_qpolynomial_fold *qpolynomial_fold_alloc(
                goto error;
 
        isl_assert(dim->ctx, n >= 0, goto error);
-       fold = isl_alloc(dim->ctx, struct isl_qpolynomial_fold,
+       fold = isl_calloc(dim->ctx, struct isl_qpolynomial_fold,
                        sizeof(struct isl_qpolynomial_fold) +
                        (n - 1) * sizeof(struct isl_qpolynomial *));
        if (!fold)
@@ -42,6 +43,22 @@ error:
        return NULL;
 }
 
+__isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_reset_dim(
+       __isl_take isl_qpolynomial_fold *fold, __isl_take isl_dim *dim)
+{
+       if (!fold || !dim)
+               goto error;
+
+       isl_dim_free(fold->dim);
+       fold->dim = dim;
+
+       return fold;
+error:
+       isl_qpolynomial_fold_free(fold);
+       isl_dim_free(dim);
+       return NULL;
+}
+
 int isl_qpolynomial_fold_involves_dims(__isl_keep isl_qpolynomial_fold *fold,
        enum isl_dim_type type, unsigned first, unsigned n)
 {
@@ -333,6 +350,33 @@ error:
        return NULL;
 }
 
+__isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_substitute_equalities(
+       __isl_take isl_qpolynomial_fold *fold, __isl_take isl_basic_set *eq)
+{
+       int i;
+
+       if (!fold || !eq)
+               goto error;
+
+       fold = isl_qpolynomial_fold_cow(fold);
+       if (!fold)
+               return NULL;
+
+       for (i = 0; i < fold->n; ++i) {
+               fold->qp[i] = isl_qpolynomial_substitute_equalities(fold->qp[i],
+                                                       isl_basic_set_copy(eq));
+               if (!fold->qp[i])
+                       goto error;
+       }
+
+       isl_basic_set_free(eq);
+       return fold;
+error:
+       isl_basic_set_free(eq);
+       isl_qpolynomial_fold_free(fold);
+       return NULL;
+}
+
 #undef PW
 #define PW isl_pw_qpolynomial_fold
 #undef EL
@@ -346,6 +390,15 @@ error:
 
 #include <isl_pw_templ.c>
 
+#undef UNION
+#define UNION isl_union_pw_qpolynomial_fold
+#undef PART
+#define PART isl_pw_qpolynomial_fold
+#undef PARTS
+#define PARTS pw_qpolynomial_fold
+
+#include <isl_union_templ.c>
+
 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_empty(enum isl_fold type,
        __isl_take isl_dim *dim)
 {
@@ -397,6 +450,7 @@ __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_dup(
        if (!dup)
                return NULL;
        
+       dup->n = fold->n;
        for (i = 0; i < fold->n; ++i) {
                dup->qp[i] = isl_qpolynomial_copy(fold->qp[i]);
                if (!dup->qp[i])
@@ -629,3 +683,253 @@ error:
        isl_qpolynomial_fold_free(fold);
        return NULL;
 }
+
+/* Check whether for each quasi-polynomial in "fold2" there is
+ * a quasi-polynomial in "fold1" that dominates it on "set".
+ */
+static int qpolynomial_fold_covers_on_domain(__isl_keep isl_set *set,
+       __isl_keep isl_qpolynomial_fold *fold1,
+       __isl_keep isl_qpolynomial_fold *fold2)
+{
+       int i, j;
+       int covers;
+
+       if (!set || !fold1 || !fold2)
+               return -1;
+
+       covers = fold1->type == isl_fold_max ? 1 : -1;
+
+       for (i = 0; i < fold2->n; ++i) {
+               for (j = 0; j < fold1->n; ++j) {
+                       isl_qpolynomial *d;
+                       int sgn;
+
+                       d = isl_qpolynomial_sub(
+                               isl_qpolynomial_copy(fold1->qp[j]),
+                               isl_qpolynomial_copy(fold2->qp[i]));
+                       sgn = isl_qpolynomial_sign(set, d);
+                       isl_qpolynomial_free(d);
+                       if (sgn == covers)
+                               break;
+               }
+               if (j >= fold1->n)
+                       return 0;
+       }
+
+       return 1;
+}
+
+/* Check whether "pwf1" dominated "pwf2", i.e., the domain of "pwf1" contains
+ * that of "pwf2" and on each cell, the corresponding fold from pwf1 dominates
+ * that of pwf2.
+ */
+int isl_pw_qpolynomial_fold_covers(__isl_keep isl_pw_qpolynomial_fold *pwf1,
+       __isl_keep isl_pw_qpolynomial_fold *pwf2)
+{
+       int i, j;
+       isl_set *dom1, *dom2;
+       int is_subset;
+
+       if (!pwf1 || !pwf2)
+               return -1;
+
+       if (pwf2->n == 0)
+               return 1;
+       if (pwf1->n == 0)
+               return 0;
+
+       dom1 = isl_pw_qpolynomial_fold_domain(isl_pw_qpolynomial_fold_copy(pwf1));
+       dom2 = isl_pw_qpolynomial_fold_domain(isl_pw_qpolynomial_fold_copy(pwf2));
+       is_subset = isl_set_is_subset(dom2, dom1);
+       isl_set_free(dom1);
+       isl_set_free(dom2);
+
+       if (is_subset < 0 || !is_subset)
+               return is_subset;
+
+       for (i = 0; i < pwf2->n; ++i) {
+               for (j = 0; j < pwf1->n; ++j) {
+                       int is_empty;
+                       isl_set *common;
+                       int covers;
+
+                       common = isl_set_intersect(isl_set_copy(pwf1->p[j].set),
+                                                  isl_set_copy(pwf2->p[i].set));
+                       is_empty = isl_set_is_empty(common);
+                       if (is_empty < 0 || is_empty) {
+                               isl_set_free(common);
+                               if (is_empty < 0)
+                                       return -1;
+                               continue;
+                       }
+                       covers = qpolynomial_fold_covers_on_domain(common,
+                                       pwf1->p[j].fold, pwf2->p[i].fold);
+                       isl_set_free(common);
+                       if (covers < 0 || !covers)
+                               return covers;
+               }
+       }
+
+       return 1;
+}
+
+__isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_morph(
+       __isl_take isl_qpolynomial_fold *fold, __isl_take isl_morph *morph)
+{
+       int i;
+       isl_ctx *ctx;
+
+       if (!fold || !morph)
+               goto error;
+
+       ctx = fold->dim->ctx;
+       isl_assert(ctx, isl_dim_equal(fold->dim, morph->dom->dim), goto error);
+
+       fold = isl_qpolynomial_fold_cow(fold);
+       if (!fold)
+               goto error;
+
+       isl_dim_free(fold->dim);
+       fold->dim = isl_dim_copy(morph->ran->dim);
+       if (!fold->dim)
+               goto error;
+
+       for (i = 0; i < fold->n; ++i) {
+               fold->qp[i] = isl_qpolynomial_morph(fold->qp[i],
+                                               isl_morph_copy(morph));
+               if (!fold->qp[i])
+                       goto error;
+       }
+
+       isl_morph_free(morph);
+
+       return fold;
+error:
+       isl_qpolynomial_fold_free(fold);
+       isl_morph_free(morph);
+       return NULL;
+}
+
+enum isl_fold isl_qpolynomial_fold_get_type(__isl_keep isl_qpolynomial_fold *fold)
+{
+       if (!fold)
+               return isl_fold_list;
+       return fold->type;
+}
+
+__isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_lift(
+       __isl_take isl_qpolynomial_fold *fold, __isl_take isl_dim *dim)
+{
+       int i;
+       isl_ctx *ctx;
+
+       if (!fold || !dim)
+               goto error;
+
+       if (isl_dim_equal(fold->dim, dim)) {
+               isl_dim_free(dim);
+               return fold;
+       }
+
+       fold = isl_qpolynomial_fold_cow(fold);
+       if (!fold)
+               goto error;
+
+       isl_dim_free(fold->dim);
+       fold->dim = isl_dim_copy(dim);
+       if (!fold->dim)
+               goto error;
+
+       for (i = 0; i < fold->n; ++i) {
+               fold->qp[i] = isl_qpolynomial_lift(fold->qp[i],
+                                               isl_dim_copy(dim));
+               if (!fold->qp[i])
+                       goto error;
+       }
+
+       isl_dim_free(dim);
+
+       return fold;
+error:
+       isl_qpolynomial_fold_free(fold);
+       isl_dim_free(dim);
+       return NULL;
+}
+
+int isl_qpolynomial_fold_foreach_qpolynomial(
+       __isl_keep isl_qpolynomial_fold *fold,
+       int (*fn)(__isl_take isl_qpolynomial *qp, void *user), void *user)
+{
+       int i;
+
+       if (!fold)
+               return -1;
+
+       for (i = 0; i < fold->n; ++i)
+               if (fn(isl_qpolynomial_copy(fold->qp[i]), user) < 0)
+                       return -1;
+
+       return 0;
+}
+
+__isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_move_dims(
+       __isl_take isl_qpolynomial_fold *fold,
+       enum isl_dim_type dst_type, unsigned dst_pos,
+       enum isl_dim_type src_type, unsigned src_pos, unsigned n)
+{
+       int i;
+
+       if (n == 0)
+               return fold;
+
+       fold = isl_qpolynomial_fold_cow(fold);
+       if (!fold)
+               return NULL;
+
+       fold->dim = isl_dim_move(fold->dim, dst_type, dst_pos,
+                                               src_type, src_pos, n);
+       if (!fold->dim)
+               goto error;
+
+       for (i = 0; i < fold->n; ++i) {
+               fold->qp[i] = isl_qpolynomial_move_dims(fold->qp[i],
+                               dst_type, dst_pos, src_type, src_pos, n);
+               if (!fold->qp[i])
+                       goto error;
+       }
+
+       return fold;
+error:
+       isl_qpolynomial_fold_free(fold);
+       return NULL;
+}
+
+/* For each 0 <= i < "n", replace variable "first" + i of type "type"
+ * in fold->qp[k] by subs[i].
+ */
+__isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_substitute(
+       __isl_take isl_qpolynomial_fold *fold,
+       enum isl_dim_type type, unsigned first, unsigned n,
+       __isl_keep isl_qpolynomial **subs)
+{
+       int i;
+
+       if (n == 0)
+               return fold;
+
+       fold = isl_qpolynomial_fold_cow(fold);
+       if (!fold)
+               return NULL;
+
+       for (i = 0; i < fold->n; ++i) {
+               fold->qp[i] = isl_qpolynomial_substitute(fold->qp[i],
+                               type, first, n, subs);
+               if (!fold->qp[i])
+                       goto error;
+       }
+
+       return fold;
+error:
+       isl_qpolynomial_fold_free(fold);
+       return NULL;
+}