add isl_*_list_insert
[platform/upstream/isl.git] / isl_local_space.c
index 8a27e81..96703f9 100644 (file)
@@ -1,11 +1,13 @@
 /*
  * Copyright 2011      INRIA Saclay
+ * Copyright 2012      Ecole Normale Superieure
  *
- * Use of this software is governed by the GNU LGPLv2.1 license
+ * Use of this software is governed by the MIT license
  *
  * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
  * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
  * 91893 Orsay, France
+ * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
  */
 
 #include <isl_ctx_private.h>
@@ -27,7 +29,7 @@ __isl_give isl_local_space *isl_local_space_alloc_div(__isl_take isl_space *dim,
        isl_ctx *ctx;
        isl_local_space *ls = NULL;
 
-       if (!dim)
+       if (!dim || !div)
                goto error;
 
        ctx = isl_space_get_ctx(dim);
@@ -41,6 +43,7 @@ __isl_give isl_local_space *isl_local_space_alloc_div(__isl_take isl_space *dim,
 
        return ls;
 error:
+       isl_mat_free(div);
        isl_space_free(dim);
        isl_local_space_free(ls);
        return NULL;
@@ -175,12 +178,32 @@ unsigned isl_local_space_offset(__isl_keep isl_local_space *ls,
        }
 }
 
+/* Does the given dimension have a name?
+ */
+int isl_local_space_has_dim_name(__isl_keep isl_local_space *ls,
+       enum isl_dim_type type, unsigned pos)
+{
+       return ls ? isl_space_has_dim_name(ls->dim, type, pos) : -1;
+}
+
 const char *isl_local_space_get_dim_name(__isl_keep isl_local_space *ls,
        enum isl_dim_type type, unsigned pos)
 {
        return ls ? isl_space_get_dim_name(ls->dim, type, pos) : NULL;
 }
 
+int isl_local_space_has_dim_id(__isl_keep isl_local_space *ls,
+       enum isl_dim_type type, unsigned pos)
+{
+       return ls ? isl_space_has_dim_id(ls->dim, type, pos) : -1;
+}
+
+__isl_give isl_id *isl_local_space_get_dim_id(__isl_keep isl_local_space *ls,
+       enum isl_dim_type type, unsigned pos)
+{
+       return ls ? isl_space_get_dim_id(ls->dim, type, pos) : NULL;
+}
+
 __isl_give isl_aff *isl_local_space_get_div(__isl_keep isl_local_space *ls,
        int pos)
 {
@@ -380,23 +403,87 @@ static void expand_row(__isl_keep isl_mat *dst, int d,
 
 /* Compare (known) divs.
  * Return non-zero if at least one of the two divs is unknown.
+ * In particular, if both divs are unknown, we respect their
+ * current order.  Otherwise, we sort the known div after the unknown
+ * div only if the known div depends on the unknown div.
  */
-static int cmp_row(__isl_keep isl_mat *div, int i, int j)
+static int cmp_row(isl_int *row_i, isl_int *row_j, int i, int j,
+       unsigned n_row, unsigned n_col)
 {
        int li, lj;
+       int unknown_i, unknown_j;
 
-       if (isl_int_is_zero(div->row[j][0]))
-               return -1;
-       if (isl_int_is_zero(div->row[i][0]))
-               return 1;
+       unknown_i = isl_int_is_zero(row_i[0]);
+       unknown_j = isl_int_is_zero(row_j[0]);
 
-       li = isl_seq_last_non_zero(div->row[i], div->n_col);
-       lj = isl_seq_last_non_zero(div->row[j], div->n_col);
+       if (unknown_i && unknown_j)
+               return i - j;
+
+       if (unknown_i)
+               li = n_col - n_row + i;
+       else
+               li = isl_seq_last_non_zero(row_i, n_col);
+       if (unknown_j)
+               lj = n_col - n_row + j;
+       else
+               lj = isl_seq_last_non_zero(row_j, n_col);
 
        if (li != lj)
                return li - lj;
 
-       return isl_seq_cmp(div->row[i], div->row[j], div->n_col);
+       return isl_seq_cmp(row_i, row_j, n_col);
+}
+
+/* Call cmp_row for divs in a matrix.
+ */
+static int mat_cmp_row(__isl_keep isl_mat *div, int i, int j)
+{
+       return cmp_row(div->row[i], div->row[j], i, j, div->n_row, div->n_col);
+}
+
+/* Call cmp_row for divs in a basic map.
+ */
+static int bmap_cmp_row(__isl_keep isl_basic_map *bmap, int i, int j,
+       unsigned total)
+{
+       return cmp_row(bmap->div[i], bmap->div[j], i, j, bmap->n_div, total);
+}
+
+/* Sort the divs in "bmap".
+ *
+ * We first make sure divs are placed after divs on which they depend.
+ * Then we perform a simple insertion sort based on the same ordering
+ * that is used in isl_merge_divs.
+ */
+__isl_give isl_basic_map *isl_basic_map_sort_divs(
+       __isl_take isl_basic_map *bmap)
+{
+       int i, j;
+       unsigned total;
+
+       bmap = isl_basic_map_order_divs(bmap);
+       if (!bmap)
+               return NULL;
+       if (bmap->n_div <= 1)
+               return bmap;
+
+       total = 2 + isl_basic_map_total_dim(bmap);
+       for (i = 1; i < bmap->n_div; ++i) {
+               for (j = i - 1; j >= 0; --j) {
+                       if (bmap_cmp_row(bmap, j, j + 1, total) <= 0)
+                               break;
+                       isl_basic_map_swap_div(bmap, j, j + 1);
+               }
+       }
+
+       return bmap;
+}
+
+/* Sort the divs in the basic maps of "map".
+ */
+__isl_give isl_map *isl_map_sort_divs(__isl_take isl_map *map)
+{
+       return isl_map_inline_foreach_basic_map(map, &isl_basic_map_sort_divs);
 }
 
 /* Combine the two lists of divs into a single list.
@@ -412,8 +499,12 @@ __isl_give isl_mat *isl_merge_divs(__isl_keep isl_mat *div1,
 {
        int i, j, k;
        isl_mat *div = NULL;
-       unsigned d = div1->n_col - div1->n_row;
+       unsigned d;
 
+       if (!div1 || !div2)
+               return NULL;
+
+       d = div1->n_col - div1->n_row;
        div = isl_mat_alloc(div1->ctx, 1 + div1->n_row + div2->n_row,
                                d + div1->n_row + div2->n_row);
        if (!div)
@@ -425,7 +516,7 @@ __isl_give isl_mat *isl_merge_divs(__isl_keep isl_mat *div1,
                expand_row(div, k, div1, i, exp1);
                expand_row(div, k + 1, div2, j, exp2);
 
-               cmp = cmp_row(div, k, k + 1);
+               cmp = mat_cmp_row(div, k, k + 1);
                if (cmp == 0) {
                        exp1[i++] = k;
                        exp2[j++] = k;
@@ -532,6 +623,21 @@ __isl_give isl_local_space *isl_local_space_domain(
        return ls;
 }
 
+__isl_give isl_local_space *isl_local_space_range(
+       __isl_take isl_local_space *ls)
+{
+       ls = isl_local_space_drop_dims(ls, isl_dim_in,
+                                       0, isl_local_space_dim(ls, isl_dim_in));
+       ls = isl_local_space_cow(ls);
+       if (!ls)
+               return NULL;
+
+       ls->dim = isl_space_range(ls->dim);
+       if (!ls->dim)
+               return isl_local_space_free(ls);
+       return ls;
+}
+
 /* Construct a local space for a map that has the given local
  * space as domain and that has a zero-dimensional range.
  */