Blame isl-0.14/isl_local_space.c

Packit fb9d21
/*
Packit fb9d21
 * Copyright 2011      INRIA Saclay
Packit fb9d21
 * Copyright 2012-2014 Ecole Normale Superieure
Packit fb9d21
 *
Packit fb9d21
 * Use of this software is governed by the MIT license
Packit fb9d21
 *
Packit fb9d21
 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
Packit fb9d21
 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
Packit fb9d21
 * 91893 Orsay, France
Packit fb9d21
 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
Packit fb9d21
 */
Packit fb9d21
Packit fb9d21
#include <isl_ctx_private.h>
Packit fb9d21
#include <isl_map_private.h>
Packit fb9d21
#include <isl_local_space_private.h>
Packit fb9d21
#include <isl_space_private.h>
Packit fb9d21
#include <isl_mat_private.h>
Packit fb9d21
#include <isl_aff_private.h>
Packit fb9d21
#include <isl_vec_private.h>
Packit fb9d21
#include <isl_seq.h>
Packit fb9d21
Packit fb9d21
isl_ctx *isl_local_space_get_ctx(__isl_keep isl_local_space *ls)
Packit fb9d21
{
Packit fb9d21
	return ls ? ls->dim->ctx : NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
__isl_give isl_local_space *isl_local_space_alloc_div(__isl_take isl_space *dim,
Packit fb9d21
	__isl_take isl_mat *div)
Packit fb9d21
{
Packit fb9d21
	isl_ctx *ctx;
Packit fb9d21
	isl_local_space *ls = NULL;
Packit fb9d21
Packit fb9d21
	if (!dim || !div)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	ctx = isl_space_get_ctx(dim);
Packit fb9d21
	ls = isl_calloc_type(ctx, struct isl_local_space);
Packit fb9d21
	if (!ls)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	ls->ref = 1;
Packit fb9d21
	ls->dim = dim;
Packit fb9d21
	ls->div = div;
Packit fb9d21
Packit fb9d21
	return ls;
Packit fb9d21
error:
Packit fb9d21
	isl_mat_free(div);
Packit fb9d21
	isl_space_free(dim);
Packit fb9d21
	isl_local_space_free(ls);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
__isl_give isl_local_space *isl_local_space_alloc(__isl_take isl_space *dim,
Packit fb9d21
	unsigned n_div)
Packit fb9d21
{
Packit fb9d21
	isl_ctx *ctx;
Packit fb9d21
	isl_mat *div;
Packit fb9d21
	unsigned total;
Packit fb9d21
Packit fb9d21
	if (!dim)
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	total = isl_space_dim(dim, isl_dim_all);
Packit fb9d21
Packit fb9d21
	ctx = isl_space_get_ctx(dim);
Packit fb9d21
	div = isl_mat_alloc(ctx, n_div, 1 + 1 + total + n_div);
Packit fb9d21
	return isl_local_space_alloc_div(dim, div);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
__isl_give isl_local_space *isl_local_space_from_space(__isl_take isl_space *dim)
Packit fb9d21
{
Packit fb9d21
	return isl_local_space_alloc(dim, 0);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
__isl_give isl_local_space *isl_local_space_copy(__isl_keep isl_local_space *ls)
Packit fb9d21
{
Packit fb9d21
	if (!ls)
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	ls->ref++;
Packit fb9d21
	return ls;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
__isl_give isl_local_space *isl_local_space_dup(__isl_keep isl_local_space *ls)
Packit fb9d21
{
Packit fb9d21
	if (!ls)
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	return isl_local_space_alloc_div(isl_space_copy(ls->dim),
Packit fb9d21
					 isl_mat_copy(ls->div));
Packit fb9d21
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
__isl_give isl_local_space *isl_local_space_cow(__isl_take isl_local_space *ls)
Packit fb9d21
{
Packit fb9d21
	if (!ls)
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	if (ls->ref == 1)
Packit fb9d21
		return ls;
Packit fb9d21
	ls->ref--;
Packit fb9d21
	return isl_local_space_dup(ls);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
__isl_null isl_local_space *isl_local_space_free(
Packit fb9d21
	__isl_take isl_local_space *ls)
Packit fb9d21
{
Packit fb9d21
	if (!ls)
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	if (--ls->ref > 0)
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	isl_space_free(ls->dim);
Packit fb9d21
	isl_mat_free(ls->div);
Packit fb9d21
Packit fb9d21
	free(ls);
Packit fb9d21
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Is the local space that of a set?
Packit fb9d21
 */
Packit fb9d21
int isl_local_space_is_set(__isl_keep isl_local_space *ls)
Packit fb9d21
{
Packit fb9d21
	return ls ? isl_space_is_set(ls->dim) : -1;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Return true if the two local spaces are identical, with identical
Packit fb9d21
 * expressions for the integer divisions.
Packit fb9d21
 */
Packit fb9d21
int isl_local_space_is_equal(__isl_keep isl_local_space *ls1,
Packit fb9d21
	__isl_keep isl_local_space *ls2)
Packit fb9d21
{
Packit fb9d21
	int equal;
Packit fb9d21
Packit fb9d21
	if (!ls1 || !ls2)
Packit fb9d21
		return -1;
Packit fb9d21
Packit fb9d21
	equal = isl_space_is_equal(ls1->dim, ls2->dim);
Packit fb9d21
	if (equal < 0 || !equal)
Packit fb9d21
		return equal;
Packit fb9d21
Packit fb9d21
	if (!isl_local_space_divs_known(ls1))
Packit fb9d21
		return 0;
Packit fb9d21
	if (!isl_local_space_divs_known(ls2))
Packit fb9d21
		return 0;
Packit fb9d21
Packit fb9d21
	return isl_mat_is_equal(ls1->div, ls2->div);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Compare two isl_local_spaces.
Packit fb9d21
 *
Packit fb9d21
 * Return -1 if "ls1" is "smaller" than "ls2", 1 if "ls1" is "greater"
Packit fb9d21
 * than "ls2" and 0 if they are equal.
Packit fb9d21
 *
Packit fb9d21
 * The order is fairly arbitrary.  We do "prefer" divs that only involve
Packit fb9d21
 * earlier dimensions in the sense that we consider local spaces where
Packit fb9d21
 * the first differing div involves earlier dimensions to be smaller.
Packit fb9d21
 */
Packit fb9d21
int isl_local_space_cmp(__isl_keep isl_local_space *ls1,
Packit fb9d21
	__isl_keep isl_local_space *ls2)
Packit fb9d21
{
Packit fb9d21
	int i;
Packit fb9d21
	int cmp;
Packit fb9d21
	int known1, known2;
Packit fb9d21
	int last1, last2;
Packit fb9d21
	int n_col;
Packit fb9d21
Packit fb9d21
	if (ls1 == ls2)
Packit fb9d21
		return 0;
Packit fb9d21
	if (!ls1)
Packit fb9d21
		return -1;
Packit fb9d21
	if (!ls2)
Packit fb9d21
		return 1;
Packit fb9d21
Packit fb9d21
	cmp = isl_space_cmp(ls1->dim, ls2->dim);
Packit fb9d21
	if (cmp != 0)
Packit fb9d21
		return cmp;
Packit fb9d21
Packit fb9d21
	if (ls1->div->n_row != ls2->div->n_row)
Packit fb9d21
		return ls1->div->n_row - ls2->div->n_row;
Packit fb9d21
Packit fb9d21
	n_col = isl_mat_cols(ls1->div);
Packit fb9d21
	for (i = 0; i < ls1->div->n_row; ++i) {
Packit fb9d21
		known1 = isl_local_space_div_is_known(ls1, i);
Packit fb9d21
		known2 = isl_local_space_div_is_known(ls2, i);
Packit fb9d21
		if (!known1 && !known2)
Packit fb9d21
			continue;
Packit fb9d21
		if (!known1)
Packit fb9d21
			return 1;
Packit fb9d21
		if (!known2)
Packit fb9d21
			return -1;
Packit fb9d21
		last1 = isl_seq_last_non_zero(ls1->div->row[i] + 1, n_col - 1);
Packit fb9d21
		last2 = isl_seq_last_non_zero(ls2->div->row[i] + 1, n_col - 1);
Packit fb9d21
		if (last1 != last2)
Packit fb9d21
			return last1 - last2;
Packit fb9d21
		cmp = isl_seq_cmp(ls1->div->row[i], ls2->div->row[i], n_col);
Packit fb9d21
		if (cmp != 0)
Packit fb9d21
			return cmp;
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	return 0;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
int isl_local_space_dim(__isl_keep isl_local_space *ls,
Packit fb9d21
	enum isl_dim_type type)
Packit fb9d21
{
Packit fb9d21
	if (!ls)
Packit fb9d21
		return 0;
Packit fb9d21
	if (type == isl_dim_div)
Packit fb9d21
		return ls->div->n_row;
Packit fb9d21
	if (type == isl_dim_all)
Packit fb9d21
		return isl_space_dim(ls->dim, isl_dim_all) + ls->div->n_row;
Packit fb9d21
	return isl_space_dim(ls->dim, type);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
unsigned isl_local_space_offset(__isl_keep isl_local_space *ls,
Packit fb9d21
	enum isl_dim_type type)
Packit fb9d21
{
Packit fb9d21
	isl_space *dim;
Packit fb9d21
Packit fb9d21
	if (!ls)
Packit fb9d21
		return 0;
Packit fb9d21
Packit fb9d21
	dim = ls->dim;
Packit fb9d21
	switch (type) {
Packit fb9d21
	case isl_dim_cst:	return 0;
Packit fb9d21
	case isl_dim_param:	return 1;
Packit fb9d21
	case isl_dim_in:	return 1 + dim->nparam;
Packit fb9d21
	case isl_dim_out:	return 1 + dim->nparam + dim->n_in;
Packit fb9d21
	case isl_dim_div:	return 1 + dim->nparam + dim->n_in + dim->n_out;
Packit fb9d21
	default:		return 0;
Packit fb9d21
	}
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Does the given dimension have a name?
Packit fb9d21
 */
Packit fb9d21
int isl_local_space_has_dim_name(__isl_keep isl_local_space *ls,
Packit fb9d21
	enum isl_dim_type type, unsigned pos)
Packit fb9d21
{
Packit fb9d21
	return ls ? isl_space_has_dim_name(ls->dim, type, pos) : -1;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
const char *isl_local_space_get_dim_name(__isl_keep isl_local_space *ls,
Packit fb9d21
	enum isl_dim_type type, unsigned pos)
Packit fb9d21
{
Packit fb9d21
	return ls ? isl_space_get_dim_name(ls->dim, type, pos) : NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
int isl_local_space_has_dim_id(__isl_keep isl_local_space *ls,
Packit fb9d21
	enum isl_dim_type type, unsigned pos)
Packit fb9d21
{
Packit fb9d21
	return ls ? isl_space_has_dim_id(ls->dim, type, pos) : -1;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
__isl_give isl_id *isl_local_space_get_dim_id(__isl_keep isl_local_space *ls,
Packit fb9d21
	enum isl_dim_type type, unsigned pos)
Packit fb9d21
{
Packit fb9d21
	return ls ? isl_space_get_dim_id(ls->dim, type, pos) : NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
__isl_give isl_aff *isl_local_space_get_div(__isl_keep isl_local_space *ls,
Packit fb9d21
	int pos)
Packit fb9d21
{
Packit fb9d21
	isl_aff *aff;
Packit fb9d21
Packit fb9d21
	if (!ls)
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	if (pos < 0 || pos >= ls->div->n_row)
Packit fb9d21
		isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
Packit fb9d21
			"index out of bounds", return NULL);
Packit fb9d21
Packit fb9d21
	if (isl_int_is_zero(ls->div->row[pos][0]))
Packit fb9d21
		isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
Packit fb9d21
			"expression of div unknown", return NULL);
Packit fb9d21
	if (!isl_local_space_is_set(ls))
Packit fb9d21
		isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
Packit fb9d21
			"cannot represent divs of map spaces", return NULL);
Packit fb9d21
Packit fb9d21
	aff = isl_aff_alloc(isl_local_space_copy(ls));
Packit fb9d21
	if (!aff)
Packit fb9d21
		return NULL;
Packit fb9d21
	isl_seq_cpy(aff->v->el, ls->div->row[pos], aff->v->size);
Packit fb9d21
	return aff;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
__isl_give isl_space *isl_local_space_get_space(__isl_keep isl_local_space *ls)
Packit fb9d21
{
Packit fb9d21
	if (!ls)
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	return isl_space_copy(ls->dim);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Replace the identifier of the tuple of type "type" by "id".
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_local_space *isl_local_space_set_tuple_id(
Packit fb9d21
	__isl_take isl_local_space *ls,
Packit fb9d21
	enum isl_dim_type type, __isl_take isl_id *id)
Packit fb9d21
{
Packit fb9d21
	ls = isl_local_space_cow(ls);
Packit fb9d21
	if (!ls)
Packit fb9d21
		goto error;
Packit fb9d21
	ls->dim = isl_space_set_tuple_id(ls->dim, type, id);
Packit fb9d21
	if (!ls->dim)
Packit fb9d21
		return isl_local_space_free(ls);
Packit fb9d21
	return ls;
Packit fb9d21
error:
Packit fb9d21
	isl_id_free(id);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
__isl_give isl_local_space *isl_local_space_set_dim_name(
Packit fb9d21
	__isl_take isl_local_space *ls,
Packit fb9d21
	enum isl_dim_type type, unsigned pos, const char *s)
Packit fb9d21
{
Packit fb9d21
	ls = isl_local_space_cow(ls);
Packit fb9d21
	if (!ls)
Packit fb9d21
		return NULL;
Packit fb9d21
	ls->dim = isl_space_set_dim_name(ls->dim, type, pos, s);
Packit fb9d21
	if (!ls->dim)
Packit fb9d21
		return isl_local_space_free(ls);
Packit fb9d21
Packit fb9d21
	return ls;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
__isl_give isl_local_space *isl_local_space_set_dim_id(
Packit fb9d21
	__isl_take isl_local_space *ls,
Packit fb9d21
	enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
Packit fb9d21
{
Packit fb9d21
	ls = isl_local_space_cow(ls);
Packit fb9d21
	if (!ls)
Packit fb9d21
		goto error;
Packit fb9d21
	ls->dim = isl_space_set_dim_id(ls->dim, type, pos, id);
Packit fb9d21
	if (!ls->dim)
Packit fb9d21
		return isl_local_space_free(ls);
Packit fb9d21
Packit fb9d21
	return ls;
Packit fb9d21
error:
Packit fb9d21
	isl_id_free(id);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
__isl_give isl_local_space *isl_local_space_reset_space(
Packit fb9d21
	__isl_take isl_local_space *ls, __isl_take isl_space *dim)
Packit fb9d21
{
Packit fb9d21
	ls = isl_local_space_cow(ls);
Packit fb9d21
	if (!ls || !dim)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	isl_space_free(ls->dim);
Packit fb9d21
	ls->dim = dim;
Packit fb9d21
Packit fb9d21
	return ls;
Packit fb9d21
error:
Packit fb9d21
	isl_local_space_free(ls);
Packit fb9d21
	isl_space_free(dim);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Reorder the columns of the given div definitions according to the
Packit fb9d21
 * given reordering.
Packit fb9d21
 * The order of the divs themselves is assumed not to change.
Packit fb9d21
 */
Packit fb9d21
static __isl_give isl_mat *reorder_divs(__isl_take isl_mat *div,
Packit fb9d21
	__isl_take isl_reordering *r)
Packit fb9d21
{
Packit fb9d21
	int i, j;
Packit fb9d21
	isl_mat *mat;
Packit fb9d21
	int extra;
Packit fb9d21
Packit fb9d21
	if (!div || !r)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	extra = isl_space_dim(r->dim, isl_dim_all) + div->n_row - r->len;
Packit fb9d21
	mat = isl_mat_alloc(div->ctx, div->n_row, div->n_col + extra);
Packit fb9d21
	if (!mat)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	for (i = 0; i < div->n_row; ++i) {
Packit fb9d21
		isl_seq_cpy(mat->row[i], div->row[i], 2);
Packit fb9d21
		isl_seq_clr(mat->row[i] + 2, mat->n_col - 2);
Packit fb9d21
		for (j = 0; j < r->len; ++j)
Packit fb9d21
			isl_int_set(mat->row[i][2 + r->pos[j]],
Packit fb9d21
				    div->row[i][2 + j]);
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	isl_reordering_free(r);
Packit fb9d21
	isl_mat_free(div);
Packit fb9d21
	return mat;
Packit fb9d21
error:
Packit fb9d21
	isl_reordering_free(r);
Packit fb9d21
	isl_mat_free(div);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Reorder the dimensions of "ls" according to the given reordering.
Packit fb9d21
 * The reordering r is assumed to have been extended with the local
Packit fb9d21
 * variables, leaving them in the same order.
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_local_space *isl_local_space_realign(
Packit fb9d21
	__isl_take isl_local_space *ls, __isl_take isl_reordering *r)
Packit fb9d21
{
Packit fb9d21
	ls = isl_local_space_cow(ls);
Packit fb9d21
	if (!ls || !r)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	ls->div = reorder_divs(ls->div, isl_reordering_copy(r));
Packit fb9d21
	if (!ls->div)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	ls = isl_local_space_reset_space(ls, isl_space_copy(r->dim));
Packit fb9d21
Packit fb9d21
	isl_reordering_free(r);
Packit fb9d21
	return ls;
Packit fb9d21
error:
Packit fb9d21
	isl_local_space_free(ls);
Packit fb9d21
	isl_reordering_free(r);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
__isl_give isl_local_space *isl_local_space_add_div(
Packit fb9d21
	__isl_take isl_local_space *ls, __isl_take isl_vec *div)
Packit fb9d21
{
Packit fb9d21
	ls = isl_local_space_cow(ls);
Packit fb9d21
	if (!ls || !div)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	if (ls->div->n_col != div->size)
Packit fb9d21
		isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
Packit fb9d21
			"incompatible dimensions", goto error);
Packit fb9d21
Packit fb9d21
	ls->div = isl_mat_add_zero_cols(ls->div, 1);
Packit fb9d21
	ls->div = isl_mat_add_rows(ls->div, 1);
Packit fb9d21
	if (!ls->div)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	isl_seq_cpy(ls->div->row[ls->div->n_row - 1], div->el, div->size);
Packit fb9d21
	isl_int_set_si(ls->div->row[ls->div->n_row - 1][div->size], 0);
Packit fb9d21
Packit fb9d21
	isl_vec_free(div);
Packit fb9d21
	return ls;
Packit fb9d21
error:
Packit fb9d21
	isl_local_space_free(ls);
Packit fb9d21
	isl_vec_free(div);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
__isl_give isl_local_space *isl_local_space_replace_divs(
Packit fb9d21
	__isl_take isl_local_space *ls, __isl_take isl_mat *div)
Packit fb9d21
{
Packit fb9d21
	ls = isl_local_space_cow(ls);
Packit fb9d21
Packit fb9d21
	if (!ls || !div)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	isl_mat_free(ls->div);
Packit fb9d21
	ls->div = div;
Packit fb9d21
	return ls;
Packit fb9d21
error:
Packit fb9d21
	isl_mat_free(div);
Packit fb9d21
	isl_local_space_free(ls);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Copy row "s" of "src" to row "d" of "dst", applying the expansion
Packit fb9d21
 * defined by "exp".
Packit fb9d21
 */
Packit fb9d21
static void expand_row(__isl_keep isl_mat *dst, int d,
Packit fb9d21
	__isl_keep isl_mat *src, int s, int *exp)
Packit fb9d21
{
Packit fb9d21
	int i;
Packit fb9d21
	unsigned c = src->n_col - src->n_row;
Packit fb9d21
Packit fb9d21
	isl_seq_cpy(dst->row[d], src->row[s], c);
Packit fb9d21
	isl_seq_clr(dst->row[d] + c, dst->n_col - c);
Packit fb9d21
Packit fb9d21
	for (i = 0; i < s; ++i)
Packit fb9d21
		isl_int_set(dst->row[d][c + exp[i]], src->row[s][c + i]);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Compare (known) divs.
Packit fb9d21
 * Return non-zero if at least one of the two divs is unknown.
Packit fb9d21
 * In particular, if both divs are unknown, we respect their
Packit fb9d21
 * current order.  Otherwise, we sort the known div after the unknown
Packit fb9d21
 * div only if the known div depends on the unknown div.
Packit fb9d21
 */
Packit fb9d21
static int cmp_row(isl_int *row_i, isl_int *row_j, int i, int j,
Packit fb9d21
	unsigned n_row, unsigned n_col)
Packit fb9d21
{
Packit fb9d21
	int li, lj;
Packit fb9d21
	int unknown_i, unknown_j;
Packit fb9d21
Packit fb9d21
	unknown_i = isl_int_is_zero(row_i[0]);
Packit fb9d21
	unknown_j = isl_int_is_zero(row_j[0]);
Packit fb9d21
Packit fb9d21
	if (unknown_i && unknown_j)
Packit fb9d21
		return i - j;
Packit fb9d21
Packit fb9d21
	if (unknown_i)
Packit fb9d21
		li = n_col - n_row + i;
Packit fb9d21
	else
Packit fb9d21
		li = isl_seq_last_non_zero(row_i, n_col);
Packit fb9d21
	if (unknown_j)
Packit fb9d21
		lj = n_col - n_row + j;
Packit fb9d21
	else
Packit fb9d21
		lj = isl_seq_last_non_zero(row_j, n_col);
Packit fb9d21
Packit fb9d21
	if (li != lj)
Packit fb9d21
		return li - lj;
Packit fb9d21
Packit fb9d21
	return isl_seq_cmp(row_i, row_j, n_col);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Call cmp_row for divs in a matrix.
Packit fb9d21
 */
Packit fb9d21
int isl_mat_cmp_div(__isl_keep isl_mat *div, int i, int j)
Packit fb9d21
{
Packit fb9d21
	return cmp_row(div->row[i], div->row[j], i, j, div->n_row, div->n_col);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Call cmp_row for divs in a basic map.
Packit fb9d21
 */
Packit fb9d21
static int bmap_cmp_row(__isl_keep isl_basic_map *bmap, int i, int j,
Packit fb9d21
	unsigned total)
Packit fb9d21
{
Packit fb9d21
	return cmp_row(bmap->div[i], bmap->div[j], i, j, bmap->n_div, total);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Sort the divs in "bmap".
Packit fb9d21
 *
Packit fb9d21
 * We first make sure divs are placed after divs on which they depend.
Packit fb9d21
 * Then we perform a simple insertion sort based on the same ordering
Packit fb9d21
 * that is used in isl_merge_divs.
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_basic_map *isl_basic_map_sort_divs(
Packit fb9d21
	__isl_take isl_basic_map *bmap)
Packit fb9d21
{
Packit fb9d21
	int i, j;
Packit fb9d21
	unsigned total;
Packit fb9d21
Packit fb9d21
	bmap = isl_basic_map_order_divs(bmap);
Packit fb9d21
	if (!bmap)
Packit fb9d21
		return NULL;
Packit fb9d21
	if (bmap->n_div <= 1)
Packit fb9d21
		return bmap;
Packit fb9d21
Packit fb9d21
	total = 2 + isl_basic_map_total_dim(bmap);
Packit fb9d21
	for (i = 1; i < bmap->n_div; ++i) {
Packit fb9d21
		for (j = i - 1; j >= 0; --j) {
Packit fb9d21
			if (bmap_cmp_row(bmap, j, j + 1, total) <= 0)
Packit fb9d21
				break;
Packit fb9d21
			isl_basic_map_swap_div(bmap, j, j + 1);
Packit fb9d21
		}
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	return bmap;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Sort the divs in the basic maps of "map".
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_map *isl_map_sort_divs(__isl_take isl_map *map)
Packit fb9d21
{
Packit fb9d21
	return isl_map_inline_foreach_basic_map(map, &isl_basic_map_sort_divs);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Combine the two lists of divs into a single list.
Packit fb9d21
 * For each row i in div1, exp1[i] is set to the position of the corresponding
Packit fb9d21
 * row in the result.  Similarly for div2 and exp2.
Packit fb9d21
 * This function guarantees
Packit fb9d21
 *	exp1[i] >= i
Packit fb9d21
 *	exp1[i+1] > exp1[i]
Packit fb9d21
 * For optimal merging, the two input list should have been sorted.
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_mat *isl_merge_divs(__isl_keep isl_mat *div1,
Packit fb9d21
	__isl_keep isl_mat *div2, int *exp1, int *exp2)
Packit fb9d21
{
Packit fb9d21
	int i, j, k;
Packit fb9d21
	isl_mat *div = NULL;
Packit fb9d21
	unsigned d;
Packit fb9d21
Packit fb9d21
	if (!div1 || !div2)
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	d = div1->n_col - div1->n_row;
Packit fb9d21
	div = isl_mat_alloc(div1->ctx, 1 + div1->n_row + div2->n_row,
Packit fb9d21
				d + div1->n_row + div2->n_row);
Packit fb9d21
	if (!div)
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	for (i = 0, j = 0, k = 0; i < div1->n_row && j < div2->n_row; ++k) {
Packit fb9d21
		int cmp;
Packit fb9d21
Packit fb9d21
		expand_row(div, k, div1, i, exp1);
Packit fb9d21
		expand_row(div, k + 1, div2, j, exp2);
Packit fb9d21
Packit fb9d21
		cmp = isl_mat_cmp_div(div, k, k + 1);
Packit fb9d21
		if (cmp == 0) {
Packit fb9d21
			exp1[i++] = k;
Packit fb9d21
			exp2[j++] = k;
Packit fb9d21
		} else if (cmp < 0) {
Packit fb9d21
			exp1[i++] = k;
Packit fb9d21
		} else {
Packit fb9d21
			exp2[j++] = k;
Packit fb9d21
			isl_seq_cpy(div->row[k], div->row[k + 1], div->n_col);
Packit fb9d21
		}
Packit fb9d21
	}
Packit fb9d21
	for (; i < div1->n_row; ++i, ++k) {
Packit fb9d21
		expand_row(div, k, div1, i, exp1);
Packit fb9d21
		exp1[i] = k;
Packit fb9d21
	}
Packit fb9d21
	for (; j < div2->n_row; ++j, ++k) {
Packit fb9d21
		expand_row(div, k, div2, j, exp2);
Packit fb9d21
		exp2[j] = k;
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	div->n_row = k;
Packit fb9d21
	div->n_col = d + k;
Packit fb9d21
Packit fb9d21
	return div;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Swap divs "a" and "b" in "ls".
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_local_space *isl_local_space_swap_div(
Packit fb9d21
	__isl_take isl_local_space *ls, int a, int b)
Packit fb9d21
{
Packit fb9d21
	int offset;
Packit fb9d21
Packit fb9d21
	ls = isl_local_space_cow(ls);
Packit fb9d21
	if (!ls)
Packit fb9d21
		return NULL;
Packit fb9d21
	if (a < 0 || a >= ls->div->n_row || b < 0 || b >= ls->div->n_row)
Packit fb9d21
		isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
Packit fb9d21
			"index out of bounds", return isl_local_space_free(ls));
Packit fb9d21
	offset = ls->div->n_col - ls->div->n_row;
Packit fb9d21
	ls->div = isl_mat_swap_cols(ls->div, offset + a, offset + b);
Packit fb9d21
	ls->div = isl_mat_swap_rows(ls->div, a, b);
Packit fb9d21
	if (!ls->div)
Packit fb9d21
		return isl_local_space_free(ls);
Packit fb9d21
	return ls;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Construct a local space that contains all the divs in either
Packit fb9d21
 * "ls1" or "ls2".
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_local_space *isl_local_space_intersect(
Packit fb9d21
	__isl_take isl_local_space *ls1, __isl_take isl_local_space *ls2)
Packit fb9d21
{
Packit fb9d21
	isl_ctx *ctx;
Packit fb9d21
	int *exp1 = NULL;
Packit fb9d21
	int *exp2 = NULL;
Packit fb9d21
	isl_mat *div;
Packit fb9d21
	int equal;
Packit fb9d21
Packit fb9d21
	if (!ls1 || !ls2)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	ctx = isl_local_space_get_ctx(ls1);
Packit fb9d21
	if (!isl_space_is_equal(ls1->dim, ls2->dim))
Packit fb9d21
		isl_die(ctx, isl_error_invalid,
Packit fb9d21
			"spaces should be identical", goto error);
Packit fb9d21
Packit fb9d21
	if (ls2->div->n_row == 0) {
Packit fb9d21
		isl_local_space_free(ls2);
Packit fb9d21
		return ls1;
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	if (ls1->div->n_row == 0) {
Packit fb9d21
		isl_local_space_free(ls1);
Packit fb9d21
		return ls2;
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	exp1 = isl_alloc_array(ctx, int, ls1->div->n_row);
Packit fb9d21
	exp2 = isl_alloc_array(ctx, int, ls2->div->n_row);
Packit fb9d21
	if (!exp1 || !exp2)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	div = isl_merge_divs(ls1->div, ls2->div, exp1, exp2);
Packit fb9d21
	if (!div)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	equal = isl_mat_is_equal(ls1->div, div);
Packit fb9d21
	if (equal < 0)
Packit fb9d21
		goto error;
Packit fb9d21
	if (!equal)
Packit fb9d21
		ls1 = isl_local_space_cow(ls1);
Packit fb9d21
	if (!ls1)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	free(exp1);
Packit fb9d21
	free(exp2);
Packit fb9d21
	isl_local_space_free(ls2);
Packit fb9d21
	isl_mat_free(ls1->div);
Packit fb9d21
	ls1->div = div;
Packit fb9d21
Packit fb9d21
	return ls1;
Packit fb9d21
error:
Packit fb9d21
	free(exp1);
Packit fb9d21
	free(exp2);
Packit fb9d21
	isl_local_space_free(ls1);
Packit fb9d21
	isl_local_space_free(ls2);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Does "ls" have an explicit representation for div "div"?
Packit fb9d21
 */
Packit fb9d21
int isl_local_space_div_is_known(__isl_keep isl_local_space *ls, int div)
Packit fb9d21
{
Packit fb9d21
	if (!ls)
Packit fb9d21
		return -1;
Packit fb9d21
	if (div < 0 || div >= ls->div->n_row)
Packit fb9d21
		isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
Packit fb9d21
			"position out of bounds", return -1);
Packit fb9d21
	return !isl_int_is_zero(ls->div->row[div][0]);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
int isl_local_space_divs_known(__isl_keep isl_local_space *ls)
Packit fb9d21
{
Packit fb9d21
	int i;
Packit fb9d21
Packit fb9d21
	if (!ls)
Packit fb9d21
		return -1;
Packit fb9d21
Packit fb9d21
	for (i = 0; i < ls->div->n_row; ++i)
Packit fb9d21
		if (isl_int_is_zero(ls->div->row[i][0]))
Packit fb9d21
			return 0;
Packit fb9d21
Packit fb9d21
	return 1;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
__isl_give isl_local_space *isl_local_space_domain(
Packit fb9d21
	__isl_take isl_local_space *ls)
Packit fb9d21
{
Packit fb9d21
	ls = isl_local_space_drop_dims(ls, isl_dim_out,
Packit fb9d21
					0, isl_local_space_dim(ls, isl_dim_out));
Packit fb9d21
	ls = isl_local_space_cow(ls);
Packit fb9d21
	if (!ls)
Packit fb9d21
		return NULL;
Packit fb9d21
	ls->dim = isl_space_domain(ls->dim);
Packit fb9d21
	if (!ls->dim)
Packit fb9d21
		return isl_local_space_free(ls);
Packit fb9d21
	return ls;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
__isl_give isl_local_space *isl_local_space_range(
Packit fb9d21
	__isl_take isl_local_space *ls)
Packit fb9d21
{
Packit fb9d21
	ls = isl_local_space_drop_dims(ls, isl_dim_in,
Packit fb9d21
					0, isl_local_space_dim(ls, isl_dim_in));
Packit fb9d21
	ls = isl_local_space_cow(ls);
Packit fb9d21
	if (!ls)
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	ls->dim = isl_space_range(ls->dim);
Packit fb9d21
	if (!ls->dim)
Packit fb9d21
		return isl_local_space_free(ls);
Packit fb9d21
	return ls;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Construct a local space for a map that has the given local
Packit fb9d21
 * space as domain and that has a zero-dimensional range.
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_local_space *isl_local_space_from_domain(
Packit fb9d21
	__isl_take isl_local_space *ls)
Packit fb9d21
{
Packit fb9d21
	ls = isl_local_space_cow(ls);
Packit fb9d21
	if (!ls)
Packit fb9d21
		return NULL;
Packit fb9d21
	ls->dim = isl_space_from_domain(ls->dim);
Packit fb9d21
	if (!ls->dim)
Packit fb9d21
		return isl_local_space_free(ls);
Packit fb9d21
	return ls;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
__isl_give isl_local_space *isl_local_space_add_dims(
Packit fb9d21
	__isl_take isl_local_space *ls, enum isl_dim_type type, unsigned n)
Packit fb9d21
{
Packit fb9d21
	int pos;
Packit fb9d21
Packit fb9d21
	if (!ls)
Packit fb9d21
		return NULL;
Packit fb9d21
	pos = isl_local_space_dim(ls, type);
Packit fb9d21
	return isl_local_space_insert_dims(ls, type, pos, n);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Remove common factor of non-constant terms and denominator.
Packit fb9d21
 */
Packit fb9d21
static void normalize_div(__isl_keep isl_local_space *ls, int div)
Packit fb9d21
{
Packit fb9d21
	isl_ctx *ctx = ls->div->ctx;
Packit fb9d21
	unsigned total = ls->div->n_col - 2;
Packit fb9d21
Packit fb9d21
	isl_seq_gcd(ls->div->row[div] + 2, total, &ctx->normalize_gcd);
Packit fb9d21
	isl_int_gcd(ctx->normalize_gcd,
Packit fb9d21
		    ctx->normalize_gcd, ls->div->row[div][0]);
Packit fb9d21
	if (isl_int_is_one(ctx->normalize_gcd))
Packit fb9d21
		return;
Packit fb9d21
Packit fb9d21
	isl_seq_scale_down(ls->div->row[div] + 2, ls->div->row[div] + 2,
Packit fb9d21
			    ctx->normalize_gcd, total);
Packit fb9d21
	isl_int_divexact(ls->div->row[div][0], ls->div->row[div][0],
Packit fb9d21
			    ctx->normalize_gcd);
Packit fb9d21
	isl_int_fdiv_q(ls->div->row[div][1], ls->div->row[div][1],
Packit fb9d21
			    ctx->normalize_gcd);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Exploit the equalities in "eq" to simplify the expressions of
Packit fb9d21
 * the integer divisions in "ls".
Packit fb9d21
 * The integer divisions in "ls" are assumed to appear as regular
Packit fb9d21
 * dimensions in "eq".
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_local_space *isl_local_space_substitute_equalities(
Packit fb9d21
	__isl_take isl_local_space *ls, __isl_take isl_basic_set *eq)
Packit fb9d21
{
Packit fb9d21
	int i, j, k;
Packit fb9d21
	unsigned total;
Packit fb9d21
	unsigned n_div;
Packit fb9d21
Packit fb9d21
	if (!ls || !eq)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	total = isl_space_dim(eq->dim, isl_dim_all);
Packit fb9d21
	if (isl_local_space_dim(ls, isl_dim_all) != total)
Packit fb9d21
		isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
Packit fb9d21
			"spaces don't match", goto error);
Packit fb9d21
	total++;
Packit fb9d21
	n_div = eq->n_div;
Packit fb9d21
	for (i = 0; i < eq->n_eq; ++i) {
Packit fb9d21
		j = isl_seq_last_non_zero(eq->eq[i], total + n_div);
Packit fb9d21
		if (j < 0 || j == 0 || j >= total)
Packit fb9d21
			continue;
Packit fb9d21
Packit fb9d21
		for (k = 0; k < ls->div->n_row; ++k) {
Packit fb9d21
			if (isl_int_is_zero(ls->div->row[k][1 + j]))
Packit fb9d21
				continue;
Packit fb9d21
			ls = isl_local_space_cow(ls);
Packit fb9d21
			if (!ls)
Packit fb9d21
				goto error;
Packit fb9d21
			ls->div = isl_mat_cow(ls->div);
Packit fb9d21
			if (!ls->div)
Packit fb9d21
				goto error;
Packit fb9d21
			isl_seq_elim(ls->div->row[k] + 1, eq->eq[i], j, total,
Packit fb9d21
					&ls->div->row[k][0]);
Packit fb9d21
			normalize_div(ls, k);
Packit fb9d21
		}
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	isl_basic_set_free(eq);
Packit fb9d21
	return ls;
Packit fb9d21
error:
Packit fb9d21
	isl_basic_set_free(eq);
Packit fb9d21
	isl_local_space_free(ls);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Plug in the affine expressions "subs" of length "subs_len" (including
Packit fb9d21
 * the denominator and the constant term) into the variable at position "pos"
Packit fb9d21
 * of the "n" div expressions starting at "first".
Packit fb9d21
 *
Packit fb9d21
 * Let i be the dimension to replace and let "subs" be of the form
Packit fb9d21
 *
Packit fb9d21
 *	f/d
Packit fb9d21
 *
Packit fb9d21
 * Any integer division starting at "first" with a non-zero coefficient for i,
Packit fb9d21
 *
Packit fb9d21
 *	floor((a i + g)/m)
Packit fb9d21
 *
Packit fb9d21
 * is replaced by
Packit fb9d21
 *
Packit fb9d21
 *	floor((a f + d g)/(m d))
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_local_space *isl_local_space_substitute_seq(
Packit fb9d21
	__isl_take isl_local_space *ls,
Packit fb9d21
	enum isl_dim_type type, unsigned pos, isl_int *subs, int subs_len,
Packit fb9d21
	int first, int n)
Packit fb9d21
{
Packit fb9d21
	int i;
Packit fb9d21
	isl_int v;
Packit fb9d21
Packit fb9d21
	if (n == 0)
Packit fb9d21
		return ls;
Packit fb9d21
	ls = isl_local_space_cow(ls);
Packit fb9d21
	if (!ls)
Packit fb9d21
		return NULL;
Packit fb9d21
	ls->div = isl_mat_cow(ls->div);
Packit fb9d21
	if (!ls->div)
Packit fb9d21
		return isl_local_space_free(ls);
Packit fb9d21
Packit fb9d21
	if (first + n > ls->div->n_row)
Packit fb9d21
		isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
Packit fb9d21
			"index out of bounds", return isl_local_space_free(ls));
Packit fb9d21
Packit fb9d21
	pos += isl_local_space_offset(ls, type);
Packit fb9d21
Packit fb9d21
	isl_int_init(v);
Packit fb9d21
	for (i = first; i < ls->div->n_row; ++i) {
Packit fb9d21
		if (isl_int_is_zero(ls->div->row[i][1 + pos]))
Packit fb9d21
			continue;
Packit fb9d21
		isl_seq_substitute(ls->div->row[i], pos, subs,
Packit fb9d21
			ls->div->n_col, subs_len, v);
Packit fb9d21
		normalize_div(ls, i);
Packit fb9d21
	}
Packit fb9d21
	isl_int_clear(v);
Packit fb9d21
Packit fb9d21
	return ls;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Plug in "subs" for dimension "type", "pos" in the integer divisions
Packit fb9d21
 * of "ls".
Packit fb9d21
 *
Packit fb9d21
 * Let i be the dimension to replace and let "subs" be of the form
Packit fb9d21
 *
Packit fb9d21
 *	f/d
Packit fb9d21
 *
Packit fb9d21
 * Any integer division with a non-zero coefficient for i,
Packit fb9d21
 *
Packit fb9d21
 *	floor((a i + g)/m)
Packit fb9d21
 *
Packit fb9d21
 * is replaced by
Packit fb9d21
 *
Packit fb9d21
 *	floor((a f + d g)/(m d))
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_local_space *isl_local_space_substitute(
Packit fb9d21
	__isl_take isl_local_space *ls,
Packit fb9d21
	enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
Packit fb9d21
{
Packit fb9d21
	ls = isl_local_space_cow(ls);
Packit fb9d21
	if (!ls || !subs)
Packit fb9d21
		return isl_local_space_free(ls);
Packit fb9d21
Packit fb9d21
	if (!isl_space_is_equal(ls->dim, subs->ls->dim))
Packit fb9d21
		isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
Packit fb9d21
			"spaces don't match", return isl_local_space_free(ls));
Packit fb9d21
	if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
Packit fb9d21
		isl_die(isl_local_space_get_ctx(ls), isl_error_unsupported,
Packit fb9d21
			"cannot handle divs yet",
Packit fb9d21
			return isl_local_space_free(ls));
Packit fb9d21
Packit fb9d21
	return isl_local_space_substitute_seq(ls, type, pos, subs->v->el,
Packit fb9d21
					    subs->v->size, 0, ls->div->n_row);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
int isl_local_space_is_named_or_nested(__isl_keep isl_local_space *ls,
Packit fb9d21
	enum isl_dim_type type)
Packit fb9d21
{
Packit fb9d21
	if (!ls)
Packit fb9d21
		return -1;
Packit fb9d21
	return isl_space_is_named_or_nested(ls->dim, type);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
__isl_give isl_local_space *isl_local_space_drop_dims(
Packit fb9d21
	__isl_take isl_local_space *ls,
Packit fb9d21
	enum isl_dim_type type, unsigned first, unsigned n)
Packit fb9d21
{
Packit fb9d21
	isl_ctx *ctx;
Packit fb9d21
Packit fb9d21
	if (!ls)
Packit fb9d21
		return NULL;
Packit fb9d21
	if (n == 0 && !isl_local_space_is_named_or_nested(ls, type))
Packit fb9d21
		return ls;
Packit fb9d21
Packit fb9d21
	ctx = isl_local_space_get_ctx(ls);
Packit fb9d21
	if (first + n > isl_local_space_dim(ls, type))
Packit fb9d21
		isl_die(ctx, isl_error_invalid, "range out of bounds",
Packit fb9d21
			return isl_local_space_free(ls));
Packit fb9d21
Packit fb9d21
	ls = isl_local_space_cow(ls);
Packit fb9d21
	if (!ls)
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	if (type == isl_dim_div) {
Packit fb9d21
		ls->div = isl_mat_drop_rows(ls->div, first, n);
Packit fb9d21
	} else {
Packit fb9d21
		ls->dim = isl_space_drop_dims(ls->dim, type, first, n);
Packit fb9d21
		if (!ls->dim)
Packit fb9d21
			return isl_local_space_free(ls);
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	first += 1 + isl_local_space_offset(ls, type);
Packit fb9d21
	ls->div = isl_mat_drop_cols(ls->div, first, n);
Packit fb9d21
	if (!ls->div)
Packit fb9d21
		return isl_local_space_free(ls);
Packit fb9d21
Packit fb9d21
	return ls;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
__isl_give isl_local_space *isl_local_space_insert_dims(
Packit fb9d21
	__isl_take isl_local_space *ls,
Packit fb9d21
	enum isl_dim_type type, unsigned first, unsigned n)
Packit fb9d21
{
Packit fb9d21
	isl_ctx *ctx;
Packit fb9d21
Packit fb9d21
	if (!ls)
Packit fb9d21
		return NULL;
Packit fb9d21
	if (n == 0 && !isl_local_space_is_named_or_nested(ls, type))
Packit fb9d21
		return ls;
Packit fb9d21
Packit fb9d21
	ctx = isl_local_space_get_ctx(ls);
Packit fb9d21
	if (first > isl_local_space_dim(ls, type))
Packit fb9d21
		isl_die(ctx, isl_error_invalid, "position out of bounds",
Packit fb9d21
			return isl_local_space_free(ls));
Packit fb9d21
Packit fb9d21
	ls = isl_local_space_cow(ls);
Packit fb9d21
	if (!ls)
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	if (type == isl_dim_div) {
Packit fb9d21
		ls->div = isl_mat_insert_zero_rows(ls->div, first, n);
Packit fb9d21
	} else {
Packit fb9d21
		ls->dim = isl_space_insert_dims(ls->dim, type, first, n);
Packit fb9d21
		if (!ls->dim)
Packit fb9d21
			return isl_local_space_free(ls);
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	first += 1 + isl_local_space_offset(ls, type);
Packit fb9d21
	ls->div = isl_mat_insert_zero_cols(ls->div, first, n);
Packit fb9d21
	if (!ls->div)
Packit fb9d21
		return isl_local_space_free(ls);
Packit fb9d21
Packit fb9d21
	return ls;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Check if the constraints pointed to by "constraint" is a div
Packit fb9d21
 * constraint corresponding to div "div" in "ls".
Packit fb9d21
 *
Packit fb9d21
 * That is, if div = floor(f/m), then check if the constraint is
Packit fb9d21
 *
Packit fb9d21
 *		f - m d >= 0
Packit fb9d21
 * or
Packit fb9d21
 *		-(f-(m-1)) + m d >= 0
Packit fb9d21
 */
Packit fb9d21
int isl_local_space_is_div_constraint(__isl_keep isl_local_space *ls,
Packit fb9d21
	isl_int *constraint, unsigned div)
Packit fb9d21
{
Packit fb9d21
	unsigned pos;
Packit fb9d21
Packit fb9d21
	if (!ls)
Packit fb9d21
		return -1;
Packit fb9d21
Packit fb9d21
	if (isl_int_is_zero(ls->div->row[div][0]))
Packit fb9d21
		return 0;
Packit fb9d21
Packit fb9d21
	pos = isl_local_space_offset(ls, isl_dim_div) + div;
Packit fb9d21
Packit fb9d21
	if (isl_int_eq(constraint[pos], ls->div->row[div][0])) {
Packit fb9d21
		int neg;
Packit fb9d21
		isl_int_sub(ls->div->row[div][1],
Packit fb9d21
				ls->div->row[div][1], ls->div->row[div][0]);
Packit fb9d21
		isl_int_add_ui(ls->div->row[div][1], ls->div->row[div][1], 1);
Packit fb9d21
		neg = isl_seq_is_neg(constraint, ls->div->row[div]+1, pos);
Packit fb9d21
		isl_int_sub_ui(ls->div->row[div][1], ls->div->row[div][1], 1);
Packit fb9d21
		isl_int_add(ls->div->row[div][1],
Packit fb9d21
				ls->div->row[div][1], ls->div->row[div][0]);
Packit fb9d21
		if (!neg)
Packit fb9d21
			return 0;
Packit fb9d21
		if (isl_seq_first_non_zero(constraint+pos+1,
Packit fb9d21
					    ls->div->n_row-div-1) != -1)
Packit fb9d21
			return 0;
Packit fb9d21
	} else if (isl_int_abs_eq(constraint[pos], ls->div->row[div][0])) {
Packit fb9d21
		if (!isl_seq_eq(constraint, ls->div->row[div]+1, pos))
Packit fb9d21
			return 0;
Packit fb9d21
		if (isl_seq_first_non_zero(constraint+pos+1,
Packit fb9d21
					    ls->div->n_row-div-1) != -1)
Packit fb9d21
			return 0;
Packit fb9d21
	} else
Packit fb9d21
		return 0;
Packit fb9d21
Packit fb9d21
	return 1;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/*
Packit fb9d21
 * Set active[i] to 1 if the dimension at position i is involved
Packit fb9d21
 * in the linear expression l.
Packit fb9d21
 */
Packit fb9d21
int *isl_local_space_get_active(__isl_keep isl_local_space *ls, isl_int *l)
Packit fb9d21
{
Packit fb9d21
	int i, j;
Packit fb9d21
	isl_ctx *ctx;
Packit fb9d21
	int *active = NULL;
Packit fb9d21
	unsigned total;
Packit fb9d21
	unsigned offset;
Packit fb9d21
Packit fb9d21
	ctx = isl_local_space_get_ctx(ls);
Packit fb9d21
	total = isl_local_space_dim(ls, isl_dim_all);
Packit fb9d21
	active = isl_calloc_array(ctx, int, total);
Packit fb9d21
	if (total && !active)
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	for (i = 0; i < total; ++i)
Packit fb9d21
		active[i] = !isl_int_is_zero(l[i]);
Packit fb9d21
Packit fb9d21
	offset = isl_local_space_offset(ls, isl_dim_div) - 1;
Packit fb9d21
	for (i = ls->div->n_row - 1; i >= 0; --i) {
Packit fb9d21
		if (!active[offset + i])
Packit fb9d21
			continue;
Packit fb9d21
		for (j = 0; j < total; ++j)
Packit fb9d21
			active[j] |= !isl_int_is_zero(ls->div->row[i][2 + j]);
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	return active;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Given a local space "ls" of a set, create a local space
Packit fb9d21
 * for the lift of the set.  In particular, the result
Packit fb9d21
 * is of the form [dim -> local[..]], with ls->div->n_row variables in the
Packit fb9d21
 * range of the wrapped map.
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_local_space *isl_local_space_lift(
Packit fb9d21
	__isl_take isl_local_space *ls)
Packit fb9d21
{
Packit fb9d21
	ls = isl_local_space_cow(ls);
Packit fb9d21
	if (!ls)
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	ls->dim = isl_space_lift(ls->dim, ls->div->n_row);
Packit fb9d21
	ls->div = isl_mat_drop_rows(ls->div, 0, ls->div->n_row);
Packit fb9d21
	if (!ls->dim || !ls->div)
Packit fb9d21
		return isl_local_space_free(ls);
Packit fb9d21
Packit fb9d21
	return ls;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Construct a basic map that maps a set living in local space "ls"
Packit fb9d21
 * to the corresponding lifted local space.
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_basic_map *isl_local_space_lifting(
Packit fb9d21
	__isl_take isl_local_space *ls)
Packit fb9d21
{
Packit fb9d21
	isl_basic_map *lifting;
Packit fb9d21
	isl_basic_set *bset;
Packit fb9d21
Packit fb9d21
	if (!ls)
Packit fb9d21
		return NULL;
Packit fb9d21
	if (!isl_local_space_is_set(ls))
Packit fb9d21
		isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
Packit fb9d21
			"lifting only defined on set spaces", goto error);
Packit fb9d21
Packit fb9d21
	bset = isl_basic_set_from_local_space(ls);
Packit fb9d21
	lifting = isl_basic_set_unwrap(isl_basic_set_lift(bset));
Packit fb9d21
	lifting = isl_basic_map_domain_map(lifting);
Packit fb9d21
	lifting = isl_basic_map_reverse(lifting);
Packit fb9d21
Packit fb9d21
	return lifting;
Packit fb9d21
error:
Packit fb9d21
	isl_local_space_free(ls);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Compute the preimage of "ls" under the function represented by "ma".
Packit fb9d21
 * In other words, plug in "ma" in "ls".  The result is a local space
Packit fb9d21
 * that is part of the domain space of "ma".
Packit fb9d21
 *
Packit fb9d21
 * If the divs in "ls" are represented as
Packit fb9d21
 *
Packit fb9d21
 *	floor((a_i(p) + b_i x + c_i(divs))/n_i)
Packit fb9d21
 *
Packit fb9d21
 * and ma is represented by
Packit fb9d21
 *
Packit fb9d21
 *	x = D(p) + F(y) + G(divs')
Packit fb9d21
 *
Packit fb9d21
 * then the resulting divs are
Packit fb9d21
 *
Packit fb9d21
 *	floor((a_i(p) + b_i D(p) + b_i F(y) + B_i G(divs') + c_i(divs))/n_i)
Packit fb9d21
 *
Packit fb9d21
 * We first copy over the divs from "ma" and then
Packit fb9d21
 * we add the modified divs from "ls".
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_local_space *isl_local_space_preimage_multi_aff(
Packit fb9d21
	__isl_take isl_local_space *ls, __isl_take isl_multi_aff *ma)
Packit fb9d21
{
Packit fb9d21
	int i;
Packit fb9d21
	isl_space *space;
Packit fb9d21
	isl_local_space *res = NULL;
Packit fb9d21
	int n_div_ls, n_div_ma;
Packit fb9d21
	isl_int f, c1, c2, g;
Packit fb9d21
Packit fb9d21
	ma = isl_multi_aff_align_divs(ma);
Packit fb9d21
	if (!ls || !ma)
Packit fb9d21
		goto error;
Packit fb9d21
	if (!isl_space_is_range_internal(ls->dim, ma->space))
Packit fb9d21
		isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
Packit fb9d21
			"spaces don't match", goto error);
Packit fb9d21
Packit fb9d21
	n_div_ls = isl_local_space_dim(ls, isl_dim_div);
Packit fb9d21
	n_div_ma = ma->n ? isl_aff_dim(ma->p[0], isl_dim_div) : 0;
Packit fb9d21
Packit fb9d21
	space = isl_space_domain(isl_multi_aff_get_space(ma));
Packit fb9d21
	res = isl_local_space_alloc(space, n_div_ma + n_div_ls);
Packit fb9d21
	if (!res)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	if (n_div_ma) {
Packit fb9d21
		isl_mat_free(res->div);
Packit fb9d21
		res->div = isl_mat_copy(ma->p[0]->ls->div);
Packit fb9d21
		res->div = isl_mat_add_zero_cols(res->div, n_div_ls);
Packit fb9d21
		res->div = isl_mat_add_rows(res->div, n_div_ls);
Packit fb9d21
		if (!res->div)
Packit fb9d21
			goto error;
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	isl_int_init(f);
Packit fb9d21
	isl_int_init(c1);
Packit fb9d21
	isl_int_init(c2);
Packit fb9d21
	isl_int_init(g);
Packit fb9d21
Packit fb9d21
	for (i = 0; i < ls->div->n_row; ++i) {
Packit fb9d21
		if (isl_int_is_zero(ls->div->row[i][0])) {
Packit fb9d21
			isl_int_set_si(res->div->row[n_div_ma + i][0], 0);
Packit fb9d21
			continue;
Packit fb9d21
		}
Packit fb9d21
		isl_seq_preimage(res->div->row[n_div_ma + i], ls->div->row[i],
Packit fb9d21
				ma, 0, 0, n_div_ma, n_div_ls, f, c1, c2, g, 1);
Packit fb9d21
		normalize_div(res, n_div_ma + i);
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	isl_int_clear(f);
Packit fb9d21
	isl_int_clear(c1);
Packit fb9d21
	isl_int_clear(c2);
Packit fb9d21
	isl_int_clear(g);
Packit fb9d21
Packit fb9d21
	isl_local_space_free(ls);
Packit fb9d21
	isl_multi_aff_free(ma);
Packit fb9d21
	return res;
Packit fb9d21
error:
Packit fb9d21
	isl_local_space_free(ls);
Packit fb9d21
	isl_multi_aff_free(ma);
Packit fb9d21
	isl_local_space_free(res);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Move the "n" dimensions of "src_type" starting at "src_pos" of "ls"
Packit fb9d21
 * to dimensions of "dst_type" at "dst_pos".
Packit fb9d21
 *
Packit fb9d21
 * Moving to/from local dimensions is not allowed.
Packit fb9d21
 * We currently assume that the dimension type changes.
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_local_space *isl_local_space_move_dims(
Packit fb9d21
	__isl_take isl_local_space *ls,
Packit fb9d21
	enum isl_dim_type dst_type, unsigned dst_pos,
Packit fb9d21
	enum isl_dim_type src_type, unsigned src_pos, unsigned n)
Packit fb9d21
{
Packit fb9d21
	unsigned g_dst_pos;
Packit fb9d21
	unsigned g_src_pos;
Packit fb9d21
Packit fb9d21
	if (!ls)
Packit fb9d21
		return NULL;
Packit fb9d21
	if (n == 0 &&
Packit fb9d21
	    !isl_local_space_is_named_or_nested(ls, src_type) &&
Packit fb9d21
	    !isl_local_space_is_named_or_nested(ls, dst_type))
Packit fb9d21
		return ls;
Packit fb9d21
Packit fb9d21
	if (src_pos + n > isl_local_space_dim(ls, src_type))
Packit fb9d21
		isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
Packit fb9d21
			"range out of bounds", return isl_local_space_free(ls));
Packit fb9d21
	if (dst_pos > isl_local_space_dim(ls, dst_type))
Packit fb9d21
		isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
Packit fb9d21
			"position out of bounds",
Packit fb9d21
			return isl_local_space_free(ls));
Packit fb9d21
	if (src_type == isl_dim_div)
Packit fb9d21
		isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
Packit fb9d21
			"cannot move divs", return isl_local_space_free(ls));
Packit fb9d21
	if (dst_type == isl_dim_div)
Packit fb9d21
		isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
Packit fb9d21
			"cannot move to divs", return isl_local_space_free(ls));
Packit fb9d21
	if (dst_type == src_type && dst_pos == src_pos)
Packit fb9d21
		return ls;
Packit fb9d21
	if (dst_type == src_type)
Packit fb9d21
		isl_die(isl_local_space_get_ctx(ls), isl_error_unsupported,
Packit fb9d21
			"moving dims within the same type not supported",
Packit fb9d21
			return isl_local_space_free(ls));
Packit fb9d21
Packit fb9d21
	ls = isl_local_space_cow(ls);
Packit fb9d21
	if (!ls)
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	g_src_pos = 1 + isl_local_space_offset(ls, src_type) + src_pos;
Packit fb9d21
	g_dst_pos = 1 + isl_local_space_offset(ls, dst_type) + dst_pos;
Packit fb9d21
	if (dst_type > src_type)
Packit fb9d21
		g_dst_pos -= n;
Packit fb9d21
	ls->div = isl_mat_move_cols(ls->div, g_dst_pos, g_src_pos, n);
Packit fb9d21
	if (!ls->div)
Packit fb9d21
		return isl_local_space_free(ls);
Packit fb9d21
	ls->dim = isl_space_move_dims(ls->dim, dst_type, dst_pos,
Packit fb9d21
					src_type, src_pos, n);
Packit fb9d21
	if (!ls->dim)
Packit fb9d21
		return isl_local_space_free(ls);
Packit fb9d21
Packit fb9d21
	return ls;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Remove any internal structure of the domain of "ls".
Packit fb9d21
 * If there is any such internal structure in the input,
Packit fb9d21
 * then the name of the corresponding space is also removed.
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_local_space *isl_local_space_flatten_domain(
Packit fb9d21
	__isl_take isl_local_space *ls)
Packit fb9d21
{
Packit fb9d21
	if (!ls)
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	if (!ls->dim->nested[0])
Packit fb9d21
		return ls;
Packit fb9d21
Packit fb9d21
	ls = isl_local_space_cow(ls);
Packit fb9d21
	if (!ls)
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	ls->dim = isl_space_flatten_domain(ls->dim);
Packit fb9d21
	if (!ls->dim)
Packit fb9d21
		return isl_local_space_free(ls);
Packit fb9d21
Packit fb9d21
	return ls;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Remove any internal structure of the range of "ls".
Packit fb9d21
 * If there is any such internal structure in the input,
Packit fb9d21
 * then the name of the corresponding space is also removed.
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_local_space *isl_local_space_flatten_range(
Packit fb9d21
	__isl_take isl_local_space *ls)
Packit fb9d21
{
Packit fb9d21
	if (!ls)
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	if (!ls->dim->nested[1])
Packit fb9d21
		return ls;
Packit fb9d21
Packit fb9d21
	ls = isl_local_space_cow(ls);
Packit fb9d21
	if (!ls)
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	ls->dim = isl_space_flatten_range(ls->dim);
Packit fb9d21
	if (!ls->dim)
Packit fb9d21
		return isl_local_space_free(ls);
Packit fb9d21
Packit fb9d21
	return ls;
Packit fb9d21
}