/*
* Copyright 2011 INRIA Saclay
+ * Copyright 2012 Ecole Normale Superieure
*
* Use of this software is governed by the GNU LGPLv2.1 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>
isl_ctx *ctx;
isl_local_space *ls = NULL;
- if (!dim)
+ if (!dim || !div)
goto error;
ctx = isl_space_get_ctx(dim);
return ls;
error:
+ isl_mat_free(div);
isl_space_free(dim);
isl_local_space_free(ls);
return NULL;
}
}
+/* 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)
{
/* 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]);
+
+ if (unknown_i && unknown_j)
+ return i - j;
- 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)
+ 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.
{
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)
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;
return div;
}
+/* Construct a local space that contains all the divs in either
+ * "ls1" or "ls2".
+ */
+__isl_give isl_local_space *isl_local_space_intersect(
+ __isl_take isl_local_space *ls1, __isl_take isl_local_space *ls2)
+{
+ isl_ctx *ctx;
+ int *exp1 = NULL;
+ int *exp2 = NULL;
+ isl_mat *div;
+
+ if (!ls1 || !ls2)
+ goto error;
+
+ ctx = isl_local_space_get_ctx(ls1);
+ if (!isl_space_is_equal(ls1->dim, ls2->dim))
+ isl_die(ctx, isl_error_invalid,
+ "spaces should be identical", goto error);
+
+ if (ls2->div->n_row == 0) {
+ isl_local_space_free(ls2);
+ return ls1;
+ }
+
+ if (ls1->div->n_row == 0) {
+ isl_local_space_free(ls1);
+ return ls2;
+ }
+
+ exp1 = isl_alloc_array(ctx, int, ls1->div->n_row);
+ exp2 = isl_alloc_array(ctx, int, ls2->div->n_row);
+ if (!exp1 || !exp2)
+ goto error;
+
+ div = isl_merge_divs(ls1->div, ls2->div, exp1, exp2);
+ if (!div)
+ goto error;
+
+ free(exp1);
+ free(exp2);
+ isl_local_space_free(ls2);
+ isl_mat_free(ls1->div);
+ ls1->div = div;
+
+ return ls1;
+error:
+ free(exp1);
+ free(exp2);
+ isl_local_space_free(ls1);
+ isl_local_space_free(ls2);
+ return NULL;
+}
+
int isl_local_space_divs_known(__isl_keep isl_local_space *ls)
{
int i;
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.
*/
return active;
}
+
+/* Given a local space "ls" of a set, create a local space
+ * for the lift of the set. In particular, the result
+ * is of the form [dim -> local[..]], with ls->div->n_row variables in the
+ * range of the wrapped map.
+ */
+__isl_give isl_local_space *isl_local_space_lift(
+ __isl_take isl_local_space *ls)
+{
+ ls = isl_local_space_cow(ls);
+ if (!ls)
+ return NULL;
+
+ ls->dim = isl_space_lift(ls->dim, ls->div->n_row);
+ ls->div = isl_mat_drop_rows(ls->div, 0, ls->div->n_row);
+ if (!ls->dim || !ls->div)
+ return isl_local_space_free(ls);
+
+ return ls;
+}
+
+/* Construct a basic map that maps a set living in local space "ls"
+ * to the corresponding lifted local space.
+ */
+__isl_give isl_basic_map *isl_local_space_lifting(
+ __isl_take isl_local_space *ls)
+{
+ isl_basic_map *lifting;
+ isl_basic_set *bset;
+
+ if (!ls)
+ return NULL;
+ if (!isl_local_space_is_set(ls))
+ isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
+ "lifting only defined on set spaces",
+ return isl_local_space_free(ls));
+
+ bset = isl_basic_set_from_local_space(ls);
+ lifting = isl_basic_set_unwrap(isl_basic_set_lift(bset));
+ lifting = isl_basic_map_domain_map(lifting);
+ lifting = isl_basic_map_reverse(lifting);
+
+ return lifting;
+}