isl_set_product: call isl_map_range_product instead of isl_map_product
[platform/upstream/isl.git] / isl_map.c
index 9d0ffbf..7496da7 100644 (file)
--- a/isl_map.c
+++ b/isl_map.c
  */
 
 #include <string.h>
-#include <strings.h>
 #include <isl_ctx_private.h>
 #include <isl_map_private.h>
 #include <isl/blk.h>
 #include "isl_dim_private.h"
 #include "isl_equalities.h"
-#include <isl/list.h>
+#include <isl_list_private.h>
 #include <isl/lp.h>
 #include <isl/seq.h>
 #include <isl/set.h>
@@ -30,6 +29,7 @@
 #include <isl_mat_private.h>
 #include <isl_dim_map.h>
 #include <isl_local_space_private.h>
+#include <isl_aff_private.h>
 
 static unsigned n(struct isl_dim *dim, enum isl_dim_type type)
 {
@@ -161,17 +161,17 @@ unsigned isl_basic_map_total_dim(const struct isl_basic_map *bmap)
 
 unsigned isl_map_n_in(const struct isl_map *map)
 {
-       return map->dim->n_in;
+       return map ? map->dim->n_in : 0;
 }
 
 unsigned isl_map_n_out(const struct isl_map *map)
 {
-       return map->dim->n_out;
+       return map ? map->dim->n_out : 0;
 }
 
 unsigned isl_map_n_param(const struct isl_map *map)
 {
-       return map->dim->nparam;
+       return map ? map->dim->nparam : 0;
 }
 
 int isl_map_compatible_domain(struct isl_map *map, struct isl_set *set)
@@ -349,6 +349,12 @@ error:
        return NULL;
 }
 
+__isl_give isl_basic_set *isl_basic_set_set_tuple_name(
+       __isl_take isl_basic_set *bset, const char *s)
+{
+       return isl_basic_map_set_tuple_name(bset, isl_dim_set, s);
+}
+
 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map *bmap,
        enum isl_dim_type type)
 {
@@ -1384,6 +1390,8 @@ __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
 __isl_give isl_basic_map *isl_basic_map_remove_divs(
        __isl_take isl_basic_map *bmap)
 {
+       if (!bmap)
+               return NULL;
        bmap = isl_basic_map_eliminate_vars(bmap, isl_dim_total(bmap->dim),
                                                bmap->n_div);
        if (!bmap)
@@ -1435,7 +1443,7 @@ struct isl_basic_map *isl_basic_map_remove_dims(struct isl_basic_map *bmap,
                return NULL;
        isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
                        goto error);
-       if (n == 0)
+       if (n == 0 && !isl_dim_is_named_or_nested(bmap->dim, type))
                return bmap;
        bmap = isl_basic_map_eliminate_vars(bmap,
                        isl_basic_map_offset(bmap, type) - 1 + first, n);
@@ -1493,6 +1501,9 @@ __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
                if (!div_involves_vars(bmap, i, first, n))
                        continue;
                bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
+               if (!bmap)
+                       return NULL;
+               i = bmap->n_div;
        }
 
        return bmap;
@@ -2259,11 +2270,13 @@ struct isl_map *isl_map_intersect(struct isl_map *map1, struct isl_map *map2)
        if (!map1 || !map2)
                goto error;
 
-       if (isl_map_plain_is_empty(map1)) {
+       if (isl_map_plain_is_empty(map1) &&
+           isl_dim_equal(map1->dim, map2->dim)) {
                isl_map_free(map2);
                return map1;
        }
-       if (isl_map_plain_is_empty(map2)) {
+       if (isl_map_plain_is_empty(map2) &&
+           isl_dim_equal(map1->dim, map2->dim)) {
                isl_map_free(map1);
                return map2;
        }
@@ -2322,6 +2335,21 @@ struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
                                  (struct isl_map *)set2);
 }
 
+/* The current implementation of isl_map_intersect accepts intersections
+ * with parameter domains, so we can just call that for now.
+ */
+__isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map,
+               __isl_take isl_set *params)
+{
+       return isl_map_intersect(map, params);
+}
+
+__isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set,
+               __isl_take isl_set *params)
+{
+       return isl_map_intersect_params(set, params);
+}
+
 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
 {
        struct isl_dim *dim;
@@ -5015,6 +5043,126 @@ __isl_give isl_set *isl_set_lexmax(__isl_take isl_set *set)
        return (isl_set *)isl_map_lexmax((isl_map *)set);
 }
 
+/* Construct a map that equates the two given dimensions in the given space.
+ */
+static __isl_give isl_map *equate(__isl_take isl_dim *dim,
+       enum isl_dim_type src_type, int src_pos,
+       enum isl_dim_type dst_type, int dst_pos)
+{
+       isl_basic_map *bmap;
+       int k;
+
+       bmap = isl_basic_map_alloc_dim(dim, 0, 1, 0);
+       k = isl_basic_map_alloc_equality(bmap);
+       if (k < 0)
+               goto error;
+       isl_seq_clr(bmap->eq[k], 1 + isl_basic_map_total_dim(bmap));
+       src_pos += isl_basic_map_offset(bmap, src_type);
+       dst_pos += isl_basic_map_offset(bmap, dst_type);
+       isl_int_set_si(bmap->eq[k][src_pos], 1);
+       isl_int_set_si(bmap->eq[k][dst_pos], -1);
+
+       return isl_map_from_basic_map(bmap);
+error:
+       isl_basic_map_free(bmap);
+       return NULL;
+}
+
+/* Extract the first and only affine expression from list
+ * and then add it to *pwaff with the given dom.
+ * This domain is known to be disjoint from other domains
+ * because of the way isl_basic_set_foreach_lexmax works.
+ */
+static int update_dim_max(__isl_take isl_basic_set *dom,
+       __isl_take isl_aff_list *list, void *user)
+{
+       isl_ctx *ctx = isl_basic_set_get_ctx(dom);
+       isl_aff *aff;
+       isl_pw_aff **pwaff = user;
+       isl_pw_aff *pwaff_i;
+
+       if (isl_aff_list_n_aff(list) != 1)
+               isl_die(ctx, isl_error_internal,
+                       "expecting single element list", goto error);
+
+       aff = isl_aff_list_get_aff(list, 0);
+       pwaff_i = isl_pw_aff_alloc(isl_set_from_basic_set(dom), aff);
+
+       *pwaff = isl_pw_aff_add_disjoint(*pwaff, pwaff_i);
+
+       isl_aff_list_free(list);
+
+       return 0;
+error:
+       isl_basic_set_free(dom);
+       isl_aff_list_free(list);
+       return -1;
+}
+
+/* Given a one-dimensional basic set, compute the maximum of that
+ * dimension as an isl_pw_aff.
+ *
+ * The isl_pw_aff is constructed by having isl_basic_set_foreach_lexmax
+ * call update_dim_max on each leaf of the result.
+ */
+static __isl_give isl_pw_aff *basic_set_dim_max(__isl_keep isl_basic_set *bset)
+{
+       isl_dim *dim = isl_basic_set_get_dim(bset);
+       isl_pw_aff *pwaff;
+       int r;
+
+       dim = isl_dim_domain(isl_dim_from_range(dim));
+       pwaff = isl_pw_aff_empty(dim);
+
+       r = isl_basic_set_foreach_lexmax(bset, &update_dim_max, &pwaff);
+       if (r < 0)
+               return isl_pw_aff_free(pwaff);
+
+       return pwaff;
+}
+
+/* Compute the maximum of the given set dimension as a function of the
+ * parameters, but independently of the other set dimensions.
+ *
+ * We first project the set onto the given dimension and then compute
+ * the "lexicographic" maximum in each basic set, combining the results
+ * using isl_pw_aff_max.
+ */
+__isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
+{
+       int i;
+       isl_map *map;
+       isl_pw_aff *pwaff;
+
+       map = isl_map_from_domain(set);
+       map = isl_map_add_dims(map, isl_dim_out, 1);
+       map = isl_map_intersect(map,
+               equate(isl_map_get_dim(map), isl_dim_in, pos,
+                                            isl_dim_out, 0));
+       set = isl_map_range(map);
+       if (!set)
+               return NULL;
+
+       if (set->n == 0) {
+               isl_dim *dim = isl_set_get_dim(set);
+               dim = isl_dim_domain(isl_dim_from_range(dim));
+               isl_set_free(set);
+               return isl_pw_aff_empty(dim);
+       }
+
+       pwaff = basic_set_dim_max(set->p[0]);
+       for (i = 1; i < set->n; ++i) {
+               isl_pw_aff *pwaff_i;
+
+               pwaff_i = basic_set_dim_max(set->p[i]);
+               pwaff = isl_pw_aff_max(pwaff, pwaff_i);
+       }
+
+       isl_set_free(set);
+
+       return pwaff;
+}
+
 /* Apply a preimage specified by "mat" on the parameters of "bset".
  * bset is assumed to have only parameters and divs.
  */
@@ -5253,7 +5401,7 @@ error:
        return NULL;
 }
 
-static int basic_map_divs_known(__isl_keep isl_basic_map *bmap)
+int isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
 {
        int i;
        unsigned off;
@@ -5279,7 +5427,7 @@ static int map_divs_known(__isl_keep isl_map *map)
                return -1;
 
        for (i = 0; i < map->n; ++i) {
-               int known = basic_map_divs_known(map->p[i]);
+               int known = isl_basic_map_divs_known(map->p[i]);
                if (known <= 0)
                        return known;
        }
@@ -5297,7 +5445,7 @@ struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
        int known;
        struct isl_map *map;
 
-       known = basic_map_divs_known(bmap);
+       known = isl_basic_map_divs_known(bmap);
        if (known < 0)
                goto error;
        if (known)
@@ -5305,7 +5453,7 @@ struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
 
        bmap = isl_basic_map_drop_redundant_divs(bmap);
 
-       known = basic_map_divs_known(bmap);
+       known = isl_basic_map_divs_known(bmap);
        if (known < 0)
                goto error;
        if (known)
@@ -7356,6 +7504,16 @@ error:
        return NULL;
 }
 
+__isl_give isl_basic_map *isl_basic_map_flat_range_product(
+       __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
+{
+       isl_basic_map *prod;
+
+       prod = isl_basic_map_range_product(bmap1, bmap2);
+       prod = isl_basic_map_flatten_range(prod);
+       return prod;
+}
+
 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
        __isl_take isl_map *map2,
        __isl_give isl_dim *(*dim_product)(__isl_take isl_dim *left,
@@ -7426,8 +7584,7 @@ __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
  */
 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
 {
-       return (struct isl_set *)isl_map_product((struct isl_map *)set1,
-                                                (struct isl_map *)set2);
+       return isl_map_range_product(set1, set2);
 }
 
 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
@@ -7445,6 +7602,18 @@ __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
                                &isl_basic_map_range_product);
 }
 
+/* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
+ */
+__isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
+       __isl_take isl_map *map2)
+{
+       isl_map *prod;
+
+       prod = isl_map_range_product(map1, map2);
+       prod = isl_map_flatten_range(prod);
+       return prod;
+}
+
 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
 {
        int i;
@@ -8261,6 +8430,31 @@ __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
        return (isl_basic_set *)isl_basic_map_flatten((isl_basic_map *)bset);
 }
 
+__isl_give isl_basic_map *isl_basic_map_flatten_range(
+       __isl_take isl_basic_map *bmap)
+{
+       if (!bmap)
+               return NULL;
+
+       if (!bmap->dim->nested[1])
+               return bmap;
+
+       bmap = isl_basic_map_cow(bmap);
+       if (!bmap)
+               return NULL;
+
+       bmap->dim = isl_dim_flatten_range(bmap->dim);
+       if (!bmap->dim)
+               goto error;
+
+       bmap = isl_basic_map_finalize(bmap);
+
+       return bmap;
+error:
+       isl_basic_map_free(bmap);
+       return NULL;
+}
+
 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
 {
        int i;
@@ -8308,6 +8502,35 @@ __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
        return map;
 }
 
+__isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
+{
+       int i;
+
+       if (!map)
+               return NULL;
+
+       if (!map->dim->nested[1])
+               return map;
+
+       map = isl_map_cow(map);
+       if (!map)
+               return NULL;
+
+       for (i = 0; i < map->n; ++i) {
+               map->p[i] = isl_basic_map_flatten_range(map->p[i]);
+               if (!map->p[i])
+                       goto error;
+       }
+       map->dim = isl_dim_flatten_range(map->dim);
+       if (!map->dim)
+               goto error;
+
+       return map;
+error:
+       isl_map_free(map);
+       return NULL;
+}
+
 /* Reorder the dimensions of "bmap" according to the given dim_map
  * and set the dimension specification to "dim".
  */
@@ -8658,3 +8881,74 @@ error:
        isl_map_free(map);
        return NULL;
 }
+
+/* Construct a basic map mapping the domain of the affine expression
+ * to a one-dimensional range prescribed by the affine expression.
+ */
+__isl_give isl_basic_map *isl_basic_map_from_aff(__isl_take isl_aff *aff)
+{
+       int k;
+       int pos;
+       isl_local_space *ls;
+       isl_basic_map *bmap;
+
+       if (!aff)
+               return NULL;
+
+       ls = isl_aff_get_local_space(aff);
+       ls = isl_local_space_from_domain(ls);
+       ls = isl_local_space_add_dims(ls, isl_dim_out, 1);
+       bmap = isl_basic_map_from_local_space(ls);
+       bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
+       k = isl_basic_map_alloc_equality(bmap);
+       if (k < 0)
+               goto error;
+
+       pos = isl_basic_map_offset(bmap, isl_dim_out);
+       isl_seq_cpy(bmap->eq[k], aff->v->el + 1, pos);
+       isl_int_neg(bmap->eq[k][pos], aff->v->el[0]);
+       isl_seq_cpy(bmap->eq[k] + pos + 1, aff->v->el + 1 + pos,
+                   aff->v->size - (pos + 1));
+
+       isl_aff_free(aff);
+       bmap = isl_basic_map_finalize(bmap);
+       return bmap;
+error:
+       isl_aff_free(aff);
+       isl_basic_map_free(bmap);
+       return NULL;
+}
+
+/* Construct a basic map mapping a domain in the given space to
+ * to an n-dimensional range, with n the number of elements in the list,
+ * where each coordinate in the range is prescribed by the
+ * corresponding affine expression.
+ * The domains of all affine expressions in the list are assumed to match
+ * domain_dim.
+ */
+__isl_give isl_basic_map *isl_basic_map_from_aff_list(
+       __isl_take isl_dim *domain_dim, __isl_take isl_aff_list *list)
+{
+       int i;
+       isl_dim *dim;
+       isl_basic_map *bmap;
+
+       if (!list)
+               return NULL;
+
+       dim = isl_dim_from_domain(domain_dim);
+       bmap = isl_basic_map_universe(dim);
+
+       for (i = 0; i < list->n; ++i) {
+               isl_aff *aff;
+               isl_basic_map *bmap_i;
+
+               aff = isl_aff_copy(list->p[i]);
+               bmap_i = isl_basic_map_from_aff(aff);
+
+               bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
+       }
+
+       isl_aff_list_free(list);
+       return bmap;
+}