isl_pw_qpolynomial_bound: handle isl_pw_qpolynomials with wrapped domains
[platform/upstream/isl.git] / isl_bound.c
index 40e4d5e..f3b3173 100644 (file)
@@ -9,17 +9,43 @@
  */
 
 #include <isl_bound.h>
+#include <isl_bernstein.h>
 #include <isl_range.h>
 #include <isl_polynomial_private.h>
 
+/* Compute a bound on the polynomial defined over the parametric polytope
+ * using either range propagation or bernstein expansion and
+ * store the result in bound->pwf and bound->pwf_tight.
+ * Since bernstein expansion requires bounded domains, we apply
+ * range propagation on unbounded domains.  Otherwise, we respect the choice
+ * of the user.
+ */
 static int compressed_guarded_poly_bound(__isl_take isl_basic_set *bset,
        __isl_take isl_qpolynomial *poly, void *user)
 {
        struct isl_bound *bound = (struct isl_bound *)user;
-       return isl_qpolynomial_bound_on_domain_range(bset, poly, bound);
+       int bounded;
+
+       if (!bset || !poly)
+               goto error;
+
+       if (bset->ctx->opt->bound == ISL_BOUND_RANGE)
+               return isl_qpolynomial_bound_on_domain_range(bset, poly, bound);
+
+       bounded = isl_basic_set_is_bounded(bset);
+       if (bounded < 0)
+               goto error;
+       if (bounded)
+               return isl_qpolynomial_bound_on_domain_bernstein(bset, poly, bound);
+       else
+               return isl_qpolynomial_bound_on_domain_range(bset, poly, bound);
+error:
+       isl_basic_set_free(bset);
+       isl_qpolynomial_free(poly);
+       return -1;
 }
 
-static int guarded_poly_bound(__isl_take isl_basic_set *bset,
+static int unwrapped_guarded_poly_bound(__isl_take isl_basic_set *bset,
        __isl_take isl_qpolynomial *poly, void *user)
 {
        struct isl_bound *bound = (struct isl_bound *)user;
@@ -77,6 +103,56 @@ error:
        return -1;
 }
 
+static int guarded_poly_bound(__isl_take isl_basic_set *bset,
+       __isl_take isl_qpolynomial *poly, void *user)
+{
+       struct isl_bound *bound = (struct isl_bound *)user;
+       isl_dim *dim;
+       isl_dim *target_dim;
+       isl_pw_qpolynomial_fold *top_pwf;
+       isl_pw_qpolynomial_fold *top_pwf_tight;
+       int nparam;
+       int n_in;
+       int r;
+
+       if (!isl_basic_set_is_wrapping(bset))
+               return unwrapped_guarded_poly_bound(bset, poly, user);
+
+       target_dim = isl_basic_set_get_dim(bset);
+       target_dim = isl_dim_unwrap(target_dim);
+       target_dim = isl_dim_domain(target_dim);
+
+       nparam = isl_dim_size(target_dim, isl_dim_param);
+       n_in = isl_dim_size(target_dim, isl_dim_set);
+
+       bset = isl_basic_set_move_dims(bset, isl_dim_param, nparam,
+                                       isl_dim_set, 0, n_in);
+       poly = isl_qpolynomial_move_dims(poly, isl_dim_param, nparam,
+                                       isl_dim_set, 0, n_in);
+
+       dim = isl_basic_set_get_dim(bset);
+       dim = isl_dim_drop(dim, isl_dim_set, 0, isl_dim_size(dim, isl_dim_set));
+
+       top_pwf = bound->pwf;
+       top_pwf_tight = bound->pwf_tight;
+
+       bound->pwf = isl_pw_qpolynomial_fold_zero(isl_dim_copy(dim));
+       bound->pwf_tight = isl_pw_qpolynomial_fold_zero(dim);
+
+       r = unwrapped_guarded_poly_bound(bset, poly, user);
+
+       bound->pwf = isl_pw_qpolynomial_fold_reset_dim(bound->pwf,
+                                                   isl_dim_copy(target_dim));
+       bound->pwf_tight = isl_pw_qpolynomial_fold_reset_dim(bound->pwf_tight,
+                                                   target_dim);
+
+       bound->pwf = isl_pw_qpolynomial_fold_add(top_pwf, bound->pwf);
+       bound->pwf_tight = isl_pw_qpolynomial_fold_add(top_pwf_tight,
+                                                       bound->pwf_tight);
+
+       return r;
+}
+
 static int guarded_qp(__isl_take isl_qpolynomial *qp, void *user)
 {
        struct isl_bound *bound = (struct isl_bound *)user;
@@ -155,7 +231,17 @@ __isl_give isl_pw_qpolynomial_fold *isl_pw_qpolynomial_fold_bound(
                return pwf;
        }
 
-       dim = isl_dim_drop(dim, isl_dim_set, 0, nvar);
+       if (isl_dim_is_wrapping(dim)) {
+               dim = isl_dim_unwrap(dim);
+               nvar = isl_dim_size(dim, isl_dim_out);
+               dim = isl_dim_domain(dim);
+               if (nvar == 0) {
+                       if (tight)
+                               *tight = 1;
+                       return isl_pw_qpolynomial_fold_reset_dim(pwf, dim);
+               }
+       } else
+               dim = isl_dim_drop(dim, isl_dim_set, 0, nvar);
 
        bound.pwf = isl_pw_qpolynomial_fold_zero(isl_dim_copy(dim));
        bound.pwf_tight = isl_pw_qpolynomial_fold_zero(isl_dim_copy(dim));
@@ -199,3 +285,52 @@ __isl_give isl_pw_qpolynomial_fold *isl_pw_qpolynomial_bound(
        pwf = isl_pw_qpolynomial_fold_from_pw_qpolynomial(type, pwqp);
        return isl_pw_qpolynomial_fold_bound(pwf, tight);
 }
+
+struct isl_union_bound_data {
+       enum isl_fold type;
+       int tight;
+       isl_union_pw_qpolynomial_fold *res;
+};
+
+static int bound_pw(__isl_take isl_pw_qpolynomial *pwqp, void *user)
+{
+       struct isl_union_bound_data *data = user;
+       isl_pw_qpolynomial_fold *pwf;
+
+       pwf = isl_pw_qpolynomial_bound(pwqp, data->type,
+                                       data->tight ? &data->tight : NULL);
+       data->res = isl_union_pw_qpolynomial_fold_add_pw_qpolynomial_fold(
+                                                               data->res, pwf);
+
+       return 0;
+}
+
+__isl_give isl_union_pw_qpolynomial_fold *isl_union_pw_qpolynomial_bound(
+       __isl_take isl_union_pw_qpolynomial *upwqp,
+       enum isl_fold type, int *tight)
+{
+       isl_dim *dim;
+       struct isl_union_bound_data data = { type, 1, NULL };
+
+       if (!upwqp)
+               return NULL;
+
+       if (!tight)
+               data.tight = 0;
+
+       dim = isl_union_pw_qpolynomial_get_dim(upwqp);
+       data.res = isl_union_pw_qpolynomial_fold_zero(dim);
+       if (isl_union_pw_qpolynomial_foreach_pw_qpolynomial(upwqp,
+                                                   &bound_pw, &data) < 0)
+               goto error;
+
+       isl_union_pw_qpolynomial_free(upwqp);
+       if (tight)
+               *tight = data.tight;
+
+       return data.res;
+error:
+       isl_union_pw_qpolynomial_free(upwqp);
+       isl_union_pw_qpolynomial_fold_free(data.res);
+       return NULL;
+}