Blame isl-0.14/isl_coalesce.c

Packit fb9d21
/*
Packit fb9d21
 * Copyright 2008-2009 Katholieke Universiteit Leuven
Packit fb9d21
 * Copyright 2010      INRIA Saclay
Packit fb9d21
 * Copyright 2012-2013 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, K.U.Leuven, Departement
Packit fb9d21
 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
Packit fb9d21
 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
Packit fb9d21
 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France 
Packit fb9d21
 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
Packit fb9d21
 */
Packit fb9d21
Packit fb9d21
#include "isl_map_private.h"
Packit fb9d21
#include <isl_seq.h>
Packit fb9d21
#include <isl/options.h>
Packit fb9d21
#include "isl_tab.h"
Packit fb9d21
#include <isl_mat_private.h>
Packit fb9d21
#include <isl_local_space_private.h>
Packit fb9d21
#include <isl_vec_private.h>
Packit fb9d21
Packit fb9d21
#define STATUS_ERROR		-1
Packit fb9d21
#define STATUS_REDUNDANT	 1
Packit fb9d21
#define STATUS_VALID	 	 2
Packit fb9d21
#define STATUS_SEPARATE	 	 3
Packit fb9d21
#define STATUS_CUT	 	 4
Packit fb9d21
#define STATUS_ADJ_EQ	 	 5
Packit fb9d21
#define STATUS_ADJ_INEQ	 	 6
Packit fb9d21
Packit fb9d21
static int status_in(isl_int *ineq, struct isl_tab *tab)
Packit fb9d21
{
Packit fb9d21
	enum isl_ineq_type type = isl_tab_ineq_type(tab, ineq);
Packit fb9d21
	switch (type) {
Packit fb9d21
	default:
Packit fb9d21
	case isl_ineq_error:		return STATUS_ERROR;
Packit fb9d21
	case isl_ineq_redundant:	return STATUS_VALID;
Packit fb9d21
	case isl_ineq_separate:		return STATUS_SEPARATE;
Packit fb9d21
	case isl_ineq_cut:		return STATUS_CUT;
Packit fb9d21
	case isl_ineq_adj_eq:		return STATUS_ADJ_EQ;
Packit fb9d21
	case isl_ineq_adj_ineq:		return STATUS_ADJ_INEQ;
Packit fb9d21
	}
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Compute the position of the equalities of basic map "bmap_i"
Packit fb9d21
 * with respect to the basic map represented by "tab_j".
Packit fb9d21
 * The resulting array has twice as many entries as the number
Packit fb9d21
 * of equalities corresponding to the two inequalties to which
Packit fb9d21
 * each equality corresponds.
Packit fb9d21
 */
Packit fb9d21
static int *eq_status_in(__isl_keep isl_basic_map *bmap_i,
Packit fb9d21
	struct isl_tab *tab_j)
Packit fb9d21
{
Packit fb9d21
	int k, l;
Packit fb9d21
	int *eq = isl_calloc_array(bmap_i->ctx, int, 2 * bmap_i->n_eq);
Packit fb9d21
	unsigned dim;
Packit fb9d21
Packit fb9d21
	if (!eq)
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	dim = isl_basic_map_total_dim(bmap_i);
Packit fb9d21
	for (k = 0; k < bmap_i->n_eq; ++k) {
Packit fb9d21
		for (l = 0; l < 2; ++l) {
Packit fb9d21
			isl_seq_neg(bmap_i->eq[k], bmap_i->eq[k], 1+dim);
Packit fb9d21
			eq[2 * k + l] = status_in(bmap_i->eq[k], tab_j);
Packit fb9d21
			if (eq[2 * k + l] == STATUS_ERROR)
Packit fb9d21
				goto error;
Packit fb9d21
		}
Packit fb9d21
		if (eq[2 * k] == STATUS_SEPARATE ||
Packit fb9d21
		    eq[2 * k + 1] == STATUS_SEPARATE)
Packit fb9d21
			break;
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	return eq;
Packit fb9d21
error:
Packit fb9d21
	free(eq);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Compute the position of the inequalities of basic map "bmap_i"
Packit fb9d21
 * (also represented by "tab_i", if not NULL) with respect to the basic map
Packit fb9d21
 * represented by "tab_j".
Packit fb9d21
 */
Packit fb9d21
static int *ineq_status_in(__isl_keep isl_basic_map *bmap_i,
Packit fb9d21
	struct isl_tab *tab_i, struct isl_tab *tab_j)
Packit fb9d21
{
Packit fb9d21
	int k;
Packit fb9d21
	unsigned n_eq = bmap_i->n_eq;
Packit fb9d21
	int *ineq = isl_calloc_array(bmap_i->ctx, int, bmap_i->n_ineq);
Packit fb9d21
Packit fb9d21
	if (!ineq)
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	for (k = 0; k < bmap_i->n_ineq; ++k) {
Packit fb9d21
		if (tab_i && isl_tab_is_redundant(tab_i, n_eq + k)) {
Packit fb9d21
			ineq[k] = STATUS_REDUNDANT;
Packit fb9d21
			continue;
Packit fb9d21
		}
Packit fb9d21
		ineq[k] = status_in(bmap_i->ineq[k], tab_j);
Packit fb9d21
		if (ineq[k] == STATUS_ERROR)
Packit fb9d21
			goto error;
Packit fb9d21
		if (ineq[k] == STATUS_SEPARATE)
Packit fb9d21
			break;
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	return ineq;
Packit fb9d21
error:
Packit fb9d21
	free(ineq);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
static int any(int *con, unsigned len, int status)
Packit fb9d21
{
Packit fb9d21
	int i;
Packit fb9d21
Packit fb9d21
	for (i = 0; i < len ; ++i)
Packit fb9d21
		if (con[i] == status)
Packit fb9d21
			return 1;
Packit fb9d21
	return 0;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
static int count(int *con, unsigned len, int status)
Packit fb9d21
{
Packit fb9d21
	int i;
Packit fb9d21
	int c = 0;
Packit fb9d21
Packit fb9d21
	for (i = 0; i < len ; ++i)
Packit fb9d21
		if (con[i] == status)
Packit fb9d21
			c++;
Packit fb9d21
	return c;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
static int all(int *con, unsigned len, int status)
Packit fb9d21
{
Packit fb9d21
	int i;
Packit fb9d21
Packit fb9d21
	for (i = 0; i < len ; ++i) {
Packit fb9d21
		if (con[i] == STATUS_REDUNDANT)
Packit fb9d21
			continue;
Packit fb9d21
		if (con[i] != status)
Packit fb9d21
			return 0;
Packit fb9d21
	}
Packit fb9d21
	return 1;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
static void drop(struct isl_map *map, int i, struct isl_tab **tabs)
Packit fb9d21
{
Packit fb9d21
	isl_basic_map_free(map->p[i]);
Packit fb9d21
	isl_tab_free(tabs[i]);
Packit fb9d21
Packit fb9d21
	if (i != map->n - 1) {
Packit fb9d21
		map->p[i] = map->p[map->n - 1];
Packit fb9d21
		tabs[i] = tabs[map->n - 1];
Packit fb9d21
	}
Packit fb9d21
	tabs[map->n - 1] = NULL;
Packit fb9d21
	map->n--;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Replace the pair of basic maps i and j by the basic map bounded
Packit fb9d21
 * by the valid constraints in both basic maps and the constraint
Packit fb9d21
 * in extra (if not NULL).
Packit fb9d21
 */
Packit fb9d21
static int fuse(struct isl_map *map, int i, int j,
Packit fb9d21
	struct isl_tab **tabs, int *eq_i, int *ineq_i, int *eq_j, int *ineq_j,
Packit fb9d21
	__isl_keep isl_mat *extra)
Packit fb9d21
{
Packit fb9d21
	int k, l;
Packit fb9d21
	struct isl_basic_map *fused = NULL;
Packit fb9d21
	struct isl_tab *fused_tab = NULL;
Packit fb9d21
	unsigned total = isl_basic_map_total_dim(map->p[i]);
Packit fb9d21
	unsigned extra_rows = extra ? extra->n_row : 0;
Packit fb9d21
Packit fb9d21
	fused = isl_basic_map_alloc_space(isl_space_copy(map->p[i]->dim),
Packit fb9d21
			map->p[i]->n_div,
Packit fb9d21
			map->p[i]->n_eq + map->p[j]->n_eq,
Packit fb9d21
			map->p[i]->n_ineq + map->p[j]->n_ineq + extra_rows);
Packit fb9d21
	if (!fused)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	for (k = 0; k < map->p[i]->n_eq; ++k) {
Packit fb9d21
		if (eq_i && (eq_i[2 * k] != STATUS_VALID ||
Packit fb9d21
			     eq_i[2 * k + 1] != STATUS_VALID))
Packit fb9d21
			continue;
Packit fb9d21
		l = isl_basic_map_alloc_equality(fused);
Packit fb9d21
		if (l < 0)
Packit fb9d21
			goto error;
Packit fb9d21
		isl_seq_cpy(fused->eq[l], map->p[i]->eq[k], 1 + total);
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	for (k = 0; k < map->p[j]->n_eq; ++k) {
Packit fb9d21
		if (eq_j && (eq_j[2 * k] != STATUS_VALID ||
Packit fb9d21
			     eq_j[2 * k + 1] != STATUS_VALID))
Packit fb9d21
			continue;
Packit fb9d21
		l = isl_basic_map_alloc_equality(fused);
Packit fb9d21
		if (l < 0)
Packit fb9d21
			goto error;
Packit fb9d21
		isl_seq_cpy(fused->eq[l], map->p[j]->eq[k], 1 + total);
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	for (k = 0; k < map->p[i]->n_ineq; ++k) {
Packit fb9d21
		if (ineq_i[k] != STATUS_VALID)
Packit fb9d21
			continue;
Packit fb9d21
		l = isl_basic_map_alloc_inequality(fused);
Packit fb9d21
		if (l < 0)
Packit fb9d21
			goto error;
Packit fb9d21
		isl_seq_cpy(fused->ineq[l], map->p[i]->ineq[k], 1 + total);
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	for (k = 0; k < map->p[j]->n_ineq; ++k) {
Packit fb9d21
		if (ineq_j[k] != STATUS_VALID)
Packit fb9d21
			continue;
Packit fb9d21
		l = isl_basic_map_alloc_inequality(fused);
Packit fb9d21
		if (l < 0)
Packit fb9d21
			goto error;
Packit fb9d21
		isl_seq_cpy(fused->ineq[l], map->p[j]->ineq[k], 1 + total);
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	for (k = 0; k < map->p[i]->n_div; ++k) {
Packit fb9d21
		int l = isl_basic_map_alloc_div(fused);
Packit fb9d21
		if (l < 0)
Packit fb9d21
			goto error;
Packit fb9d21
		isl_seq_cpy(fused->div[l], map->p[i]->div[k], 1 + 1 + total);
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	for (k = 0; k < extra_rows; ++k) {
Packit fb9d21
		l = isl_basic_map_alloc_inequality(fused);
Packit fb9d21
		if (l < 0)
Packit fb9d21
			goto error;
Packit fb9d21
		isl_seq_cpy(fused->ineq[l], extra->row[k], 1 + total);
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	fused = isl_basic_map_gauss(fused, NULL);
Packit fb9d21
	ISL_F_SET(fused, ISL_BASIC_MAP_FINAL);
Packit fb9d21
	if (ISL_F_ISSET(map->p[i], ISL_BASIC_MAP_RATIONAL) &&
Packit fb9d21
	    ISL_F_ISSET(map->p[j], ISL_BASIC_MAP_RATIONAL))
Packit fb9d21
		ISL_F_SET(fused, ISL_BASIC_MAP_RATIONAL);
Packit fb9d21
Packit fb9d21
	fused_tab = isl_tab_from_basic_map(fused, 0);
Packit fb9d21
	if (isl_tab_detect_redundant(fused_tab) < 0)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	isl_basic_map_free(map->p[i]);
Packit fb9d21
	map->p[i] = fused;
Packit fb9d21
	isl_tab_free(tabs[i]);
Packit fb9d21
	tabs[i] = fused_tab;
Packit fb9d21
	drop(map, j, tabs);
Packit fb9d21
Packit fb9d21
	return 1;
Packit fb9d21
error:
Packit fb9d21
	isl_tab_free(fused_tab);
Packit fb9d21
	isl_basic_map_free(fused);
Packit fb9d21
	return -1;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Given a pair of basic maps i and j such that all constraints are either
Packit fb9d21
 * "valid" or "cut", check if the facets corresponding to the "cut"
Packit fb9d21
 * constraints of i lie entirely within basic map j.
Packit fb9d21
 * If so, replace the pair by the basic map consisting of the valid
Packit fb9d21
 * constraints in both basic maps.
Packit fb9d21
 *
Packit fb9d21
 * To see that we are not introducing any extra points, call the
Packit fb9d21
 * two basic maps A and B and the resulting map U and let x
Packit fb9d21
 * be an element of U \setminus ( A \cup B ).
Packit fb9d21
 * Then there is a pair of cut constraints c_1 and c_2 in A and B such that x
Packit fb9d21
 * violates them.  Let X be the intersection of U with the opposites
Packit fb9d21
 * of these constraints.  Then x \in X.
Packit fb9d21
 * The facet corresponding to c_1 contains the corresponding facet of A.
Packit fb9d21
 * This facet is entirely contained in B, so c_2 is valid on the facet.
Packit fb9d21
 * However, since it is also (part of) a facet of X, -c_2 is also valid
Packit fb9d21
 * on the facet.  This means c_2 is saturated on the facet, so c_1 and
Packit fb9d21
 * c_2 must be opposites of each other, but then x could not violate
Packit fb9d21
 * both of them.
Packit fb9d21
 */
Packit fb9d21
static int check_facets(struct isl_map *map, int i, int j,
Packit fb9d21
	struct isl_tab **tabs, int *ineq_i, int *ineq_j)
Packit fb9d21
{
Packit fb9d21
	int k, l;
Packit fb9d21
	struct isl_tab_undo *snap;
Packit fb9d21
	unsigned n_eq = map->p[i]->n_eq;
Packit fb9d21
Packit fb9d21
	snap = isl_tab_snap(tabs[i]);
Packit fb9d21
Packit fb9d21
	for (k = 0; k < map->p[i]->n_ineq; ++k) {
Packit fb9d21
		if (ineq_i[k] != STATUS_CUT)
Packit fb9d21
			continue;
Packit fb9d21
		if (isl_tab_select_facet(tabs[i], n_eq + k) < 0)
Packit fb9d21
			return -1;
Packit fb9d21
		for (l = 0; l < map->p[j]->n_ineq; ++l) {
Packit fb9d21
			int stat;
Packit fb9d21
			if (ineq_j[l] != STATUS_CUT)
Packit fb9d21
				continue;
Packit fb9d21
			stat = status_in(map->p[j]->ineq[l], tabs[i]);
Packit fb9d21
			if (stat != STATUS_VALID)
Packit fb9d21
				break;
Packit fb9d21
		}
Packit fb9d21
		if (isl_tab_rollback(tabs[i], snap) < 0)
Packit fb9d21
			return -1;
Packit fb9d21
		if (l < map->p[j]->n_ineq)
Packit fb9d21
			break;
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	if (k < map->p[i]->n_ineq)
Packit fb9d21
		/* BAD CUT PAIR */
Packit fb9d21
		return 0;
Packit fb9d21
	return fuse(map, i, j, tabs, NULL, ineq_i, NULL, ineq_j, NULL);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Check if basic map "i" contains the basic map represented
Packit fb9d21
 * by the tableau "tab".
Packit fb9d21
 */
Packit fb9d21
static int contains(struct isl_map *map, int i, int *ineq_i,
Packit fb9d21
	struct isl_tab *tab)
Packit fb9d21
{
Packit fb9d21
	int k, l;
Packit fb9d21
	unsigned dim;
Packit fb9d21
Packit fb9d21
	dim = isl_basic_map_total_dim(map->p[i]);
Packit fb9d21
	for (k = 0; k < map->p[i]->n_eq; ++k) {
Packit fb9d21
		for (l = 0; l < 2; ++l) {
Packit fb9d21
			int stat;
Packit fb9d21
			isl_seq_neg(map->p[i]->eq[k], map->p[i]->eq[k], 1+dim);
Packit fb9d21
			stat = status_in(map->p[i]->eq[k], tab);
Packit fb9d21
			if (stat != STATUS_VALID)
Packit fb9d21
				return 0;
Packit fb9d21
		}
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	for (k = 0; k < map->p[i]->n_ineq; ++k) {
Packit fb9d21
		int stat;
Packit fb9d21
		if (ineq_i[k] == STATUS_REDUNDANT)
Packit fb9d21
			continue;
Packit fb9d21
		stat = status_in(map->p[i]->ineq[k], tab);
Packit fb9d21
		if (stat != STATUS_VALID)
Packit fb9d21
			return 0;
Packit fb9d21
	}
Packit fb9d21
	return 1;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Basic map "i" has an inequality (say "k") that is adjacent
Packit fb9d21
 * to some inequality of basic map "j".  All the other inequalities
Packit fb9d21
 * are valid for "j".
Packit fb9d21
 * Check if basic map "j" forms an extension of basic map "i".
Packit fb9d21
 *
Packit fb9d21
 * Note that this function is only called if some of the equalities or
Packit fb9d21
 * inequalities of basic map "j" do cut basic map "i".  The function is
Packit fb9d21
 * correct even if there are no such cut constraints, but in that case
Packit fb9d21
 * the additional checks performed by this function are overkill.
Packit fb9d21
 *
Packit fb9d21
 * In particular, we replace constraint k, say f >= 0, by constraint
Packit fb9d21
 * f <= -1, add the inequalities of "j" that are valid for "i"
Packit fb9d21
 * and check if the result is a subset of basic map "j".
Packit fb9d21
 * If so, then we know that this result is exactly equal to basic map "j"
Packit fb9d21
 * since all its constraints are valid for basic map "j".
Packit fb9d21
 * By combining the valid constraints of "i" (all equalities and all
Packit fb9d21
 * inequalities except "k") and the valid constraints of "j" we therefore
Packit fb9d21
 * obtain a basic map that is equal to their union.
Packit fb9d21
 * In this case, there is no need to perform a rollback of the tableau
Packit fb9d21
 * since it is going to be destroyed in fuse().
Packit fb9d21
 *
Packit fb9d21
 *
Packit fb9d21
 *	|\__			|\__
Packit fb9d21
 *	|   \__			|   \__
Packit fb9d21
 *	|      \_	=>	|      \__
Packit fb9d21
 *	|_______| _		|_________\
Packit fb9d21
 *
Packit fb9d21
 *
Packit fb9d21
 *	|\			|\
Packit fb9d21
 *	| \			| \
Packit fb9d21
 *	|  \			|  \
Packit fb9d21
 *	|  |			|   \
Packit fb9d21
 *	|  ||\		=>      |    \
Packit fb9d21
 *	|  || \			|     \
Packit fb9d21
 *	|  ||  |		|      |
Packit fb9d21
 *	|__||_/			|_____/
Packit fb9d21
 */
Packit fb9d21
static int is_adj_ineq_extension(__isl_keep isl_map *map, int i, int j,
Packit fb9d21
	struct isl_tab **tabs, int *eq_i, int *ineq_i, int *eq_j, int *ineq_j)
Packit fb9d21
{
Packit fb9d21
	int k;
Packit fb9d21
	struct isl_tab_undo *snap;
Packit fb9d21
	unsigned n_eq = map->p[i]->n_eq;
Packit fb9d21
	unsigned total = isl_basic_map_total_dim(map->p[i]);
Packit fb9d21
	int r;
Packit fb9d21
Packit fb9d21
	if (isl_tab_extend_cons(tabs[i], 1 + map->p[j]->n_ineq) < 0)
Packit fb9d21
		return -1;
Packit fb9d21
Packit fb9d21
	for (k = 0; k < map->p[i]->n_ineq; ++k)
Packit fb9d21
		if (ineq_i[k] == STATUS_ADJ_INEQ)
Packit fb9d21
			break;
Packit fb9d21
	if (k >= map->p[i]->n_ineq)
Packit fb9d21
		isl_die(isl_map_get_ctx(map), isl_error_internal,
Packit fb9d21
			"ineq_i should have exactly one STATUS_ADJ_INEQ",
Packit fb9d21
			return -1);
Packit fb9d21
Packit fb9d21
	snap = isl_tab_snap(tabs[i]);
Packit fb9d21
Packit fb9d21
	if (isl_tab_unrestrict(tabs[i], n_eq + k) < 0)
Packit fb9d21
		return -1;
Packit fb9d21
Packit fb9d21
	isl_seq_neg(map->p[i]->ineq[k], map->p[i]->ineq[k], 1 + total);
Packit fb9d21
	isl_int_sub_ui(map->p[i]->ineq[k][0], map->p[i]->ineq[k][0], 1);
Packit fb9d21
	r = isl_tab_add_ineq(tabs[i], map->p[i]->ineq[k]);
Packit fb9d21
	isl_seq_neg(map->p[i]->ineq[k], map->p[i]->ineq[k], 1 + total);
Packit fb9d21
	isl_int_sub_ui(map->p[i]->ineq[k][0], map->p[i]->ineq[k][0], 1);
Packit fb9d21
	if (r < 0)
Packit fb9d21
		return -1;
Packit fb9d21
Packit fb9d21
	for (k = 0; k < map->p[j]->n_ineq; ++k) {
Packit fb9d21
		if (ineq_j[k] != STATUS_VALID)
Packit fb9d21
			continue;
Packit fb9d21
		if (isl_tab_add_ineq(tabs[i], map->p[j]->ineq[k]) < 0)
Packit fb9d21
			return -1;
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	if (contains(map, j, ineq_j, tabs[i]))
Packit fb9d21
		return fuse(map, i, j, tabs, eq_i, ineq_i, eq_j, ineq_j, NULL);
Packit fb9d21
Packit fb9d21
	if (isl_tab_rollback(tabs[i], snap) < 0)
Packit fb9d21
		return -1;
Packit fb9d21
Packit fb9d21
	return 0;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
Packit fb9d21
/* Both basic maps have at least one inequality with and adjacent
Packit fb9d21
 * (but opposite) inequality in the other basic map.
Packit fb9d21
 * Check that there are no cut constraints and that there is only
Packit fb9d21
 * a single pair of adjacent inequalities.
Packit fb9d21
 * If so, we can replace the pair by a single basic map described
Packit fb9d21
 * by all but the pair of adjacent inequalities.
Packit fb9d21
 * Any additional points introduced lie strictly between the two
Packit fb9d21
 * adjacent hyperplanes and can therefore be integral.
Packit fb9d21
 *
Packit fb9d21
 *        ____			  _____
Packit fb9d21
 *       /    ||\		 /     \
Packit fb9d21
 *      /     || \		/       \
Packit fb9d21
 *      \     ||  \	=>	\        \
Packit fb9d21
 *       \    ||  /		 \       /
Packit fb9d21
 *        \___||_/		  \_____/
Packit fb9d21
 *
Packit fb9d21
 * The test for a single pair of adjancent inequalities is important
Packit fb9d21
 * for avoiding the combination of two basic maps like the following
Packit fb9d21
 *
Packit fb9d21
 *       /|
Packit fb9d21
 *      / |
Packit fb9d21
 *     /__|
Packit fb9d21
 *         _____
Packit fb9d21
 *         |   |
Packit fb9d21
 *         |   |
Packit fb9d21
 *         |___|
Packit fb9d21
 *
Packit fb9d21
 * If there are some cut constraints on one side, then we may
Packit fb9d21
 * still be able to fuse the two basic maps, but we need to perform
Packit fb9d21
 * some additional checks in is_adj_ineq_extension.
Packit fb9d21
 */
Packit fb9d21
static int check_adj_ineq(struct isl_map *map, int i, int j,
Packit fb9d21
	struct isl_tab **tabs, int *eq_i, int *ineq_i, int *eq_j, int *ineq_j)
Packit fb9d21
{
Packit fb9d21
	int count_i, count_j;
Packit fb9d21
	int cut_i, cut_j;
Packit fb9d21
Packit fb9d21
	count_i = count(ineq_i, map->p[i]->n_ineq, STATUS_ADJ_INEQ);
Packit fb9d21
	count_j = count(ineq_j, map->p[j]->n_ineq, STATUS_ADJ_INEQ);
Packit fb9d21
Packit fb9d21
	if (count_i != 1 && count_j != 1)
Packit fb9d21
		return 0;
Packit fb9d21
Packit fb9d21
	cut_i = any(eq_i, 2 * map->p[i]->n_eq, STATUS_CUT) ||
Packit fb9d21
		any(ineq_i, map->p[i]->n_ineq, STATUS_CUT);
Packit fb9d21
	cut_j = any(eq_j, 2 * map->p[j]->n_eq, STATUS_CUT) ||
Packit fb9d21
		any(ineq_j, map->p[j]->n_ineq, STATUS_CUT);
Packit fb9d21
Packit fb9d21
	if (!cut_i && !cut_j && count_i == 1 && count_j == 1)
Packit fb9d21
		return fuse(map, i, j, tabs, NULL, ineq_i, NULL, ineq_j, NULL);
Packit fb9d21
Packit fb9d21
	if (count_i == 1 && !cut_i)
Packit fb9d21
		return is_adj_ineq_extension(map, i, j, tabs,
Packit fb9d21
						eq_i, ineq_i, eq_j, ineq_j);
Packit fb9d21
Packit fb9d21
	if (count_j == 1 && !cut_j)
Packit fb9d21
		return is_adj_ineq_extension(map, j, i, tabs,
Packit fb9d21
						eq_j, ineq_j, eq_i, ineq_i);
Packit fb9d21
Packit fb9d21
	return 0;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Basic map "i" has an inequality "k" that is adjacent to some equality
Packit fb9d21
 * of basic map "j".  All the other inequalities are valid for "j".
Packit fb9d21
 * Check if basic map "j" forms an extension of basic map "i".
Packit fb9d21
 *
Packit fb9d21
 * In particular, we relax constraint "k", compute the corresponding
Packit fb9d21
 * facet and check whether it is included in the other basic map.
Packit fb9d21
 * If so, we know that relaxing the constraint extends the basic
Packit fb9d21
 * map with exactly the other basic map (we already know that this
Packit fb9d21
 * other basic map is included in the extension, because there
Packit fb9d21
 * were no "cut" inequalities in "i") and we can replace the
Packit fb9d21
 * two basic maps by this extension.
Packit fb9d21
 *        ____			  _____
Packit fb9d21
 *       /    || 		 /     |
Packit fb9d21
 *      /     ||  		/      |
Packit fb9d21
 *      \     ||   	=>	\      |
Packit fb9d21
 *       \    ||		 \     |
Packit fb9d21
 *        \___||		  \____|
Packit fb9d21
 */
Packit fb9d21
static int is_adj_eq_extension(struct isl_map *map, int i, int j, int k,
Packit fb9d21
	struct isl_tab **tabs, int *eq_i, int *ineq_i, int *eq_j, int *ineq_j)
Packit fb9d21
{
Packit fb9d21
	int changed = 0;
Packit fb9d21
	int super;
Packit fb9d21
	struct isl_tab_undo *snap, *snap2;
Packit fb9d21
	unsigned n_eq = map->p[i]->n_eq;
Packit fb9d21
Packit fb9d21
	if (isl_tab_is_equality(tabs[i], n_eq + k))
Packit fb9d21
		return 0;
Packit fb9d21
Packit fb9d21
	snap = isl_tab_snap(tabs[i]);
Packit fb9d21
	tabs[i] = isl_tab_relax(tabs[i], n_eq + k);
Packit fb9d21
	snap2 = isl_tab_snap(tabs[i]);
Packit fb9d21
	if (isl_tab_select_facet(tabs[i], n_eq + k) < 0)
Packit fb9d21
		return -1;
Packit fb9d21
	super = contains(map, j, ineq_j, tabs[i]);
Packit fb9d21
	if (super) {
Packit fb9d21
		if (isl_tab_rollback(tabs[i], snap2) < 0)
Packit fb9d21
			return -1;
Packit fb9d21
		map->p[i] = isl_basic_map_cow(map->p[i]);
Packit fb9d21
		if (!map->p[i])
Packit fb9d21
			return -1;
Packit fb9d21
		isl_int_add_ui(map->p[i]->ineq[k][0], map->p[i]->ineq[k][0], 1);
Packit fb9d21
		ISL_F_SET(map->p[i], ISL_BASIC_MAP_FINAL);
Packit fb9d21
		drop(map, j, tabs);
Packit fb9d21
		changed = 1;
Packit fb9d21
	} else
Packit fb9d21
		if (isl_tab_rollback(tabs[i], snap) < 0)
Packit fb9d21
			return -1;
Packit fb9d21
Packit fb9d21
	return changed;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Data structure that keeps track of the wrapping constraints
Packit fb9d21
 * and of information to bound the coefficients of those constraints.
Packit fb9d21
 *
Packit fb9d21
 * bound is set if we want to apply a bound on the coefficients
Packit fb9d21
 * mat contains the wrapping constraints
Packit fb9d21
 * max is the bound on the coefficients (if bound is set)
Packit fb9d21
 */
Packit fb9d21
struct isl_wraps {
Packit fb9d21
	int bound;
Packit fb9d21
	isl_mat *mat;
Packit fb9d21
	isl_int max;
Packit fb9d21
};
Packit fb9d21
Packit fb9d21
/* Update wraps->max to be greater than or equal to the coefficients
Packit fb9d21
 * in the equalities and inequalities of bmap that can be removed if we end up
Packit fb9d21
 * applying wrapping.
Packit fb9d21
 */
Packit fb9d21
static void wraps_update_max(struct isl_wraps *wraps,
Packit fb9d21
	__isl_keep isl_basic_map *bmap, int *eq, int *ineq)
Packit fb9d21
{
Packit fb9d21
	int k;
Packit fb9d21
	isl_int max_k;
Packit fb9d21
	unsigned total = isl_basic_map_total_dim(bmap);
Packit fb9d21
Packit fb9d21
	isl_int_init(max_k);
Packit fb9d21
Packit fb9d21
	for (k = 0; k < bmap->n_eq; ++k) {
Packit fb9d21
		if (eq[2 * k] == STATUS_VALID &&
Packit fb9d21
		    eq[2 * k + 1] == STATUS_VALID)
Packit fb9d21
			continue;
Packit fb9d21
		isl_seq_abs_max(bmap->eq[k] + 1, total, &max_k);
Packit fb9d21
		if (isl_int_abs_gt(max_k, wraps->max))
Packit fb9d21
			isl_int_set(wraps->max, max_k);
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	for (k = 0; k < bmap->n_ineq; ++k) {
Packit fb9d21
		if (ineq[k] == STATUS_VALID || ineq[k] == STATUS_REDUNDANT)
Packit fb9d21
			continue;
Packit fb9d21
		isl_seq_abs_max(bmap->ineq[k] + 1, total, &max_k);
Packit fb9d21
		if (isl_int_abs_gt(max_k, wraps->max))
Packit fb9d21
			isl_int_set(wraps->max, max_k);
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	isl_int_clear(max_k);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Initialize the isl_wraps data structure.
Packit fb9d21
 * If we want to bound the coefficients of the wrapping constraints,
Packit fb9d21
 * we set wraps->max to the largest coefficient
Packit fb9d21
 * in the equalities and inequalities that can be removed if we end up
Packit fb9d21
 * applying wrapping.
Packit fb9d21
 */
Packit fb9d21
static void wraps_init(struct isl_wraps *wraps, __isl_take isl_mat *mat,
Packit fb9d21
	__isl_keep isl_map *map, int i, int j,
Packit fb9d21
	int *eq_i, int *ineq_i, int *eq_j, int *ineq_j)
Packit fb9d21
{
Packit fb9d21
	isl_ctx *ctx;
Packit fb9d21
Packit fb9d21
	wraps->bound = 0;
Packit fb9d21
	wraps->mat = mat;
Packit fb9d21
	if (!mat)
Packit fb9d21
		return;
Packit fb9d21
	ctx = isl_mat_get_ctx(mat);
Packit fb9d21
	wraps->bound = isl_options_get_coalesce_bounded_wrapping(ctx);
Packit fb9d21
	if (!wraps->bound)
Packit fb9d21
		return;
Packit fb9d21
	isl_int_init(wraps->max);
Packit fb9d21
	isl_int_set_si(wraps->max, 0);
Packit fb9d21
	wraps_update_max(wraps, map->p[i], eq_i, ineq_i);
Packit fb9d21
	wraps_update_max(wraps, map->p[j], eq_j, ineq_j);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Free the contents of the isl_wraps data structure.
Packit fb9d21
 */
Packit fb9d21
static void wraps_free(struct isl_wraps *wraps)
Packit fb9d21
{
Packit fb9d21
	isl_mat_free(wraps->mat);
Packit fb9d21
	if (wraps->bound)
Packit fb9d21
		isl_int_clear(wraps->max);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Is the wrapping constraint in row "row" allowed?
Packit fb9d21
 *
Packit fb9d21
 * If wraps->bound is set, we check that none of the coefficients
Packit fb9d21
 * is greater than wraps->max.
Packit fb9d21
 */
Packit fb9d21
static int allow_wrap(struct isl_wraps *wraps, int row)
Packit fb9d21
{
Packit fb9d21
	int i;
Packit fb9d21
Packit fb9d21
	if (!wraps->bound)
Packit fb9d21
		return 1;
Packit fb9d21
Packit fb9d21
	for (i = 1; i < wraps->mat->n_col; ++i)
Packit fb9d21
		if (isl_int_abs_gt(wraps->mat->row[row][i], wraps->max))
Packit fb9d21
			return 0;
Packit fb9d21
Packit fb9d21
	return 1;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* For each non-redundant constraint in "bmap" (as determined by "tab"),
Packit fb9d21
 * wrap the constraint around "bound" such that it includes the whole
Packit fb9d21
 * set "set" and append the resulting constraint to "wraps".
Packit fb9d21
 * "wraps" is assumed to have been pre-allocated to the appropriate size.
Packit fb9d21
 * wraps->n_row is the number of actual wrapped constraints that have
Packit fb9d21
 * been added.
Packit fb9d21
 * If any of the wrapping problems results in a constraint that is
Packit fb9d21
 * identical to "bound", then this means that "set" is unbounded in such
Packit fb9d21
 * way that no wrapping is possible.  If this happens then wraps->n_row
Packit fb9d21
 * is reset to zero.
Packit fb9d21
 * Similarly, if we want to bound the coefficients of the wrapping
Packit fb9d21
 * constraints and a newly added wrapping constraint does not
Packit fb9d21
 * satisfy the bound, then wraps->n_row is also reset to zero.
Packit fb9d21
 */
Packit fb9d21
static int add_wraps(struct isl_wraps *wraps, __isl_keep isl_basic_map *bmap,
Packit fb9d21
	struct isl_tab *tab, isl_int *bound, __isl_keep isl_set *set)
Packit fb9d21
{
Packit fb9d21
	int l;
Packit fb9d21
	int w;
Packit fb9d21
	unsigned total = isl_basic_map_total_dim(bmap);
Packit fb9d21
Packit fb9d21
	w = wraps->mat->n_row;
Packit fb9d21
Packit fb9d21
	for (l = 0; l < bmap->n_ineq; ++l) {
Packit fb9d21
		if (isl_seq_is_neg(bound, bmap->ineq[l], 1 + total))
Packit fb9d21
			continue;
Packit fb9d21
		if (isl_seq_eq(bound, bmap->ineq[l], 1 + total))
Packit fb9d21
			continue;
Packit fb9d21
		if (isl_tab_is_redundant(tab, bmap->n_eq + l))
Packit fb9d21
			continue;
Packit fb9d21
Packit fb9d21
		isl_seq_cpy(wraps->mat->row[w], bound, 1 + total);
Packit fb9d21
		if (!isl_set_wrap_facet(set, wraps->mat->row[w], bmap->ineq[l]))
Packit fb9d21
			return -1;
Packit fb9d21
		if (isl_seq_eq(wraps->mat->row[w], bound, 1 + total))
Packit fb9d21
			goto unbounded;
Packit fb9d21
		if (!allow_wrap(wraps, w))
Packit fb9d21
			goto unbounded;
Packit fb9d21
		++w;
Packit fb9d21
	}
Packit fb9d21
	for (l = 0; l < bmap->n_eq; ++l) {
Packit fb9d21
		if (isl_seq_is_neg(bound, bmap->eq[l], 1 + total))
Packit fb9d21
			continue;
Packit fb9d21
		if (isl_seq_eq(bound, bmap->eq[l], 1 + total))
Packit fb9d21
			continue;
Packit fb9d21
Packit fb9d21
		isl_seq_cpy(wraps->mat->row[w], bound, 1 + total);
Packit fb9d21
		isl_seq_neg(wraps->mat->row[w + 1], bmap->eq[l], 1 + total);
Packit fb9d21
		if (!isl_set_wrap_facet(set, wraps->mat->row[w],
Packit fb9d21
					wraps->mat->row[w + 1]))
Packit fb9d21
			return -1;
Packit fb9d21
		if (isl_seq_eq(wraps->mat->row[w], bound, 1 + total))
Packit fb9d21
			goto unbounded;
Packit fb9d21
		if (!allow_wrap(wraps, w))
Packit fb9d21
			goto unbounded;
Packit fb9d21
		++w;
Packit fb9d21
Packit fb9d21
		isl_seq_cpy(wraps->mat->row[w], bound, 1 + total);
Packit fb9d21
		if (!isl_set_wrap_facet(set, wraps->mat->row[w], bmap->eq[l]))
Packit fb9d21
			return -1;
Packit fb9d21
		if (isl_seq_eq(wraps->mat->row[w], bound, 1 + total))
Packit fb9d21
			goto unbounded;
Packit fb9d21
		if (!allow_wrap(wraps, w))
Packit fb9d21
			goto unbounded;
Packit fb9d21
		++w;
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	wraps->mat->n_row = w;
Packit fb9d21
	return 0;
Packit fb9d21
unbounded:
Packit fb9d21
	wraps->mat->n_row = 0;
Packit fb9d21
	return 0;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Check if the constraints in "wraps" from "first" until the last
Packit fb9d21
 * are all valid for the basic set represented by "tab".
Packit fb9d21
 * If not, wraps->n_row is set to zero.
Packit fb9d21
 */
Packit fb9d21
static int check_wraps(__isl_keep isl_mat *wraps, int first,
Packit fb9d21
	struct isl_tab *tab)
Packit fb9d21
{
Packit fb9d21
	int i;
Packit fb9d21
Packit fb9d21
	for (i = first; i < wraps->n_row; ++i) {
Packit fb9d21
		enum isl_ineq_type type;
Packit fb9d21
		type = isl_tab_ineq_type(tab, wraps->row[i]);
Packit fb9d21
		if (type == isl_ineq_error)
Packit fb9d21
			return -1;
Packit fb9d21
		if (type == isl_ineq_redundant)
Packit fb9d21
			continue;
Packit fb9d21
		wraps->n_row = 0;
Packit fb9d21
		return 0;
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	return 0;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Return a set that corresponds to the non-redudant constraints
Packit fb9d21
 * (as recorded in tab) of bmap.
Packit fb9d21
 *
Packit fb9d21
 * It's important to remove the redundant constraints as some
Packit fb9d21
 * of the other constraints may have been modified after the
Packit fb9d21
 * constraints were marked redundant.
Packit fb9d21
 * In particular, a constraint may have been relaxed.
Packit fb9d21
 * Redundant constraints are ignored when a constraint is relaxed
Packit fb9d21
 * and should therefore continue to be ignored ever after.
Packit fb9d21
 * Otherwise, the relaxation might be thwarted by some of
Packit fb9d21
 * these constraints.
Packit fb9d21
 */
Packit fb9d21
static __isl_give isl_set *set_from_updated_bmap(__isl_keep isl_basic_map *bmap,
Packit fb9d21
	struct isl_tab *tab)
Packit fb9d21
{
Packit fb9d21
	bmap = isl_basic_map_copy(bmap);
Packit fb9d21
	bmap = isl_basic_map_cow(bmap);
Packit fb9d21
	bmap = isl_basic_map_update_from_tab(bmap, tab);
Packit fb9d21
	return isl_set_from_basic_set(isl_basic_map_underlying_set(bmap));
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Given a basic set i with a constraint k that is adjacent to either the
Packit fb9d21
 * whole of basic set j or a facet of basic set j, check if we can wrap
Packit fb9d21
 * both the facet corresponding to k and the facet of j (or the whole of j)
Packit fb9d21
 * around their ridges to include the other set.
Packit fb9d21
 * If so, replace the pair of basic sets by their union.
Packit fb9d21
 *
Packit fb9d21
 * All constraints of i (except k) are assumed to be valid for j.
Packit fb9d21
 *
Packit fb9d21
 * However, the constraints of j may not be valid for i and so
Packit fb9d21
 * we have to check that the wrapping constraints for j are valid for i.
Packit fb9d21
 *
Packit fb9d21
 * In the case where j has a facet adjacent to i, tab[j] is assumed
Packit fb9d21
 * to have been restricted to this facet, so that the non-redundant
Packit fb9d21
 * constraints in tab[j] are the ridges of the facet.
Packit fb9d21
 * Note that for the purpose of wrapping, it does not matter whether
Packit fb9d21
 * we wrap the ridges of i around the whole of j or just around
Packit fb9d21
 * the facet since all the other constraints are assumed to be valid for j.
Packit fb9d21
 * In practice, we wrap to include the whole of j.
Packit fb9d21
 *        ____			  _____
Packit fb9d21
 *       /    | 		 /     \
Packit fb9d21
 *      /     ||  		/      |
Packit fb9d21
 *      \     ||   	=>	\      |
Packit fb9d21
 *       \    ||		 \     |
Packit fb9d21
 *        \___||		  \____|
Packit fb9d21
 *
Packit fb9d21
 */
Packit fb9d21
static int can_wrap_in_facet(struct isl_map *map, int i, int j, int k,
Packit fb9d21
	struct isl_tab **tabs, int *eq_i, int *ineq_i, int *eq_j, int *ineq_j)
Packit fb9d21
{
Packit fb9d21
	int changed = 0;
Packit fb9d21
	struct isl_wraps wraps;
Packit fb9d21
	isl_mat *mat;
Packit fb9d21
	struct isl_set *set_i = NULL;
Packit fb9d21
	struct isl_set *set_j = NULL;
Packit fb9d21
	struct isl_vec *bound = NULL;
Packit fb9d21
	unsigned total = isl_basic_map_total_dim(map->p[i]);
Packit fb9d21
	struct isl_tab_undo *snap;
Packit fb9d21
	int n;
Packit fb9d21
Packit fb9d21
	set_i = set_from_updated_bmap(map->p[i], tabs[i]);
Packit fb9d21
	set_j = set_from_updated_bmap(map->p[j], tabs[j]);
Packit fb9d21
	mat = isl_mat_alloc(map->ctx, 2 * (map->p[i]->n_eq + map->p[j]->n_eq) +
Packit fb9d21
					map->p[i]->n_ineq + map->p[j]->n_ineq,
Packit fb9d21
					1 + total);
Packit fb9d21
	wraps_init(&wraps, mat, map, i, j, eq_i, ineq_i, eq_j, ineq_j);
Packit fb9d21
	bound = isl_vec_alloc(map->ctx, 1 + total);
Packit fb9d21
	if (!set_i || !set_j || !wraps.mat || !bound)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	isl_seq_cpy(bound->el, map->p[i]->ineq[k], 1 + total);
Packit fb9d21
	isl_int_add_ui(bound->el[0], bound->el[0], 1);
Packit fb9d21
Packit fb9d21
	isl_seq_cpy(wraps.mat->row[0], bound->el, 1 + total);
Packit fb9d21
	wraps.mat->n_row = 1;
Packit fb9d21
Packit fb9d21
	if (add_wraps(&wraps, map->p[j], tabs[j], bound->el, set_i) < 0)
Packit fb9d21
		goto error;
Packit fb9d21
	if (!wraps.mat->n_row)
Packit fb9d21
		goto unbounded;
Packit fb9d21
Packit fb9d21
	snap = isl_tab_snap(tabs[i]);
Packit fb9d21
Packit fb9d21
	if (isl_tab_select_facet(tabs[i], map->p[i]->n_eq + k) < 0)
Packit fb9d21
		goto error;
Packit fb9d21
	if (isl_tab_detect_redundant(tabs[i]) < 0)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	isl_seq_neg(bound->el, map->p[i]->ineq[k], 1 + total);
Packit fb9d21
Packit fb9d21
	n = wraps.mat->n_row;
Packit fb9d21
	if (add_wraps(&wraps, map->p[i], tabs[i], bound->el, set_j) < 0)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	if (isl_tab_rollback(tabs[i], snap) < 0)
Packit fb9d21
		goto error;
Packit fb9d21
	if (check_wraps(wraps.mat, n, tabs[i]) < 0)
Packit fb9d21
		goto error;
Packit fb9d21
	if (!wraps.mat->n_row)
Packit fb9d21
		goto unbounded;
Packit fb9d21
Packit fb9d21
	changed = fuse(map, i, j, tabs, eq_i, ineq_i, eq_j, ineq_j, wraps.mat);
Packit fb9d21
Packit fb9d21
unbounded:
Packit fb9d21
	wraps_free(&wraps);
Packit fb9d21
Packit fb9d21
	isl_set_free(set_i);
Packit fb9d21
	isl_set_free(set_j);
Packit fb9d21
Packit fb9d21
	isl_vec_free(bound);
Packit fb9d21
Packit fb9d21
	return changed;
Packit fb9d21
error:
Packit fb9d21
	wraps_free(&wraps);
Packit fb9d21
	isl_vec_free(bound);
Packit fb9d21
	isl_set_free(set_i);
Packit fb9d21
	isl_set_free(set_j);
Packit fb9d21
	return -1;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Set the is_redundant property of the "n" constraints in "cuts",
Packit fb9d21
 * except "k" to "v".
Packit fb9d21
 * This is a fairly tricky operation as it bypasses isl_tab.c.
Packit fb9d21
 * The reason we want to temporarily mark some constraints redundant
Packit fb9d21
 * is that we want to ignore them in add_wraps.
Packit fb9d21
 *
Packit fb9d21
 * Initially all cut constraints are non-redundant, but the
Packit fb9d21
 * selection of a facet right before the call to this function
Packit fb9d21
 * may have made some of them redundant.
Packit fb9d21
 * Likewise, the same constraints are marked non-redundant
Packit fb9d21
 * in the second call to this function, before they are officially
Packit fb9d21
 * made non-redundant again in the subsequent rollback.
Packit fb9d21
 */
Packit fb9d21
static void set_is_redundant(struct isl_tab *tab, unsigned n_eq,
Packit fb9d21
	int *cuts, int n, int k, int v)
Packit fb9d21
{
Packit fb9d21
	int l;
Packit fb9d21
Packit fb9d21
	for (l = 0; l < n; ++l) {
Packit fb9d21
		if (l == k)
Packit fb9d21
			continue;
Packit fb9d21
		tab->con[n_eq + cuts[l]].is_redundant = v;
Packit fb9d21
	}
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Given a pair of basic maps i and j such that j sticks out
Packit fb9d21
 * of i at n cut constraints, each time by at most one,
Packit fb9d21
 * try to compute wrapping constraints and replace the two
Packit fb9d21
 * basic maps by a single basic map.
Packit fb9d21
 * The other constraints of i are assumed to be valid for j.
Packit fb9d21
 *
Packit fb9d21
 * The facets of i corresponding to the cut constraints are
Packit fb9d21
 * wrapped around their ridges, except those ridges determined
Packit fb9d21
 * by any of the other cut constraints.
Packit fb9d21
 * The intersections of cut constraints need to be ignored
Packit fb9d21
 * as the result of wrapping one cut constraint around another
Packit fb9d21
 * would result in a constraint cutting the union.
Packit fb9d21
 * In each case, the facets are wrapped to include the union
Packit fb9d21
 * of the two basic maps.
Packit fb9d21
 *
Packit fb9d21
 * The pieces of j that lie at an offset of exactly one from
Packit fb9d21
 * one of the cut constraints of i are wrapped around their edges.
Packit fb9d21
 * Here, there is no need to ignore intersections because we
Packit fb9d21
 * are wrapping around the union of the two basic maps.
Packit fb9d21
 *
Packit fb9d21
 * If any wrapping fails, i.e., if we cannot wrap to touch
Packit fb9d21
 * the union, then we give up.
Packit fb9d21
 * Otherwise, the pair of basic maps is replaced by their union.
Packit fb9d21
 */
Packit fb9d21
static int wrap_in_facets(struct isl_map *map, int i, int j,
Packit fb9d21
	int *cuts, int n, struct isl_tab **tabs,
Packit fb9d21
	int *eq_i, int *ineq_i, int *eq_j, int *ineq_j)
Packit fb9d21
{
Packit fb9d21
	int changed = 0;
Packit fb9d21
	struct isl_wraps wraps;
Packit fb9d21
	isl_mat *mat;
Packit fb9d21
	isl_set *set = NULL;
Packit fb9d21
	isl_vec *bound = NULL;
Packit fb9d21
	unsigned total = isl_basic_map_total_dim(map->p[i]);
Packit fb9d21
	int max_wrap;
Packit fb9d21
	int k;
Packit fb9d21
	struct isl_tab_undo *snap_i, *snap_j;
Packit fb9d21
Packit fb9d21
	if (isl_tab_extend_cons(tabs[j], 1) < 0)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	max_wrap = 2 * (map->p[i]->n_eq + map->p[j]->n_eq) +
Packit fb9d21
		    map->p[i]->n_ineq + map->p[j]->n_ineq;
Packit fb9d21
	max_wrap *= n;
Packit fb9d21
Packit fb9d21
	set = isl_set_union(set_from_updated_bmap(map->p[i], tabs[i]),
Packit fb9d21
			    set_from_updated_bmap(map->p[j], tabs[j]));
Packit fb9d21
	mat = isl_mat_alloc(map->ctx, max_wrap, 1 + total);
Packit fb9d21
	wraps_init(&wraps, mat, map, i, j, eq_i, ineq_i, eq_j, ineq_j);
Packit fb9d21
	bound = isl_vec_alloc(map->ctx, 1 + total);
Packit fb9d21
	if (!set || !wraps.mat || !bound)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	snap_i = isl_tab_snap(tabs[i]);
Packit fb9d21
	snap_j = isl_tab_snap(tabs[j]);
Packit fb9d21
Packit fb9d21
	wraps.mat->n_row = 0;
Packit fb9d21
Packit fb9d21
	for (k = 0; k < n; ++k) {
Packit fb9d21
		if (isl_tab_select_facet(tabs[i], map->p[i]->n_eq + cuts[k]) < 0)
Packit fb9d21
			goto error;
Packit fb9d21
		if (isl_tab_detect_redundant(tabs[i]) < 0)
Packit fb9d21
			goto error;
Packit fb9d21
		set_is_redundant(tabs[i], map->p[i]->n_eq, cuts, n, k, 1);
Packit fb9d21
Packit fb9d21
		isl_seq_neg(bound->el, map->p[i]->ineq[cuts[k]], 1 + total);
Packit fb9d21
		if (!tabs[i]->empty &&
Packit fb9d21
		    add_wraps(&wraps, map->p[i], tabs[i], bound->el, set) < 0)
Packit fb9d21
			goto error;
Packit fb9d21
Packit fb9d21
		set_is_redundant(tabs[i], map->p[i]->n_eq, cuts, n, k, 0);
Packit fb9d21
		if (isl_tab_rollback(tabs[i], snap_i) < 0)
Packit fb9d21
			goto error;
Packit fb9d21
Packit fb9d21
		if (tabs[i]->empty)
Packit fb9d21
			break;
Packit fb9d21
		if (!wraps.mat->n_row)
Packit fb9d21
			break;
Packit fb9d21
Packit fb9d21
		isl_seq_cpy(bound->el, map->p[i]->ineq[cuts[k]], 1 + total);
Packit fb9d21
		isl_int_add_ui(bound->el[0], bound->el[0], 1);
Packit fb9d21
		if (isl_tab_add_eq(tabs[j], bound->el) < 0)
Packit fb9d21
			goto error;
Packit fb9d21
		if (isl_tab_detect_redundant(tabs[j]) < 0)
Packit fb9d21
			goto error;
Packit fb9d21
Packit fb9d21
		if (!tabs[j]->empty &&
Packit fb9d21
		    add_wraps(&wraps, map->p[j], tabs[j], bound->el, set) < 0)
Packit fb9d21
			goto error;
Packit fb9d21
Packit fb9d21
		if (isl_tab_rollback(tabs[j], snap_j) < 0)
Packit fb9d21
			goto error;
Packit fb9d21
Packit fb9d21
		if (!wraps.mat->n_row)
Packit fb9d21
			break;
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	if (k == n)
Packit fb9d21
		changed = fuse(map, i, j, tabs,
Packit fb9d21
				eq_i, ineq_i, eq_j, ineq_j, wraps.mat);
Packit fb9d21
Packit fb9d21
	isl_vec_free(bound);
Packit fb9d21
	wraps_free(&wraps);
Packit fb9d21
	isl_set_free(set);
Packit fb9d21
Packit fb9d21
	return changed;
Packit fb9d21
error:
Packit fb9d21
	isl_vec_free(bound);
Packit fb9d21
	wraps_free(&wraps);
Packit fb9d21
	isl_set_free(set);
Packit fb9d21
	return -1;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Given two basic sets i and j such that i has no cut equalities,
Packit fb9d21
 * check if relaxing all the cut inequalities of i by one turns
Packit fb9d21
 * them into valid constraint for j and check if we can wrap in
Packit fb9d21
 * the bits that are sticking out.
Packit fb9d21
 * If so, replace the pair by their union.
Packit fb9d21
 *
Packit fb9d21
 * We first check if all relaxed cut inequalities of i are valid for j
Packit fb9d21
 * and then try to wrap in the intersections of the relaxed cut inequalities
Packit fb9d21
 * with j.
Packit fb9d21
 *
Packit fb9d21
 * During this wrapping, we consider the points of j that lie at a distance
Packit fb9d21
 * of exactly 1 from i.  In particular, we ignore the points that lie in
Packit fb9d21
 * between this lower-dimensional space and the basic map i.
Packit fb9d21
 * We can therefore only apply this to integer maps.
Packit fb9d21
 *        ____			  _____
Packit fb9d21
 *       / ___|_		 /     \
Packit fb9d21
 *      / |    |  		/      |
Packit fb9d21
 *      \ |    |   	=>	\      |
Packit fb9d21
 *       \|____|		 \     |
Packit fb9d21
 *        \___| 		  \____/
Packit fb9d21
 *
Packit fb9d21
 *	 _____			 ______
Packit fb9d21
 *	| ____|_		|      \
Packit fb9d21
 *	| |     |		|       |
Packit fb9d21
 *	| |	|	=>	|       |
Packit fb9d21
 *	|_|     |		|       |
Packit fb9d21
 *	  |_____|		 \______|
Packit fb9d21
 *
Packit fb9d21
 *	 _______
Packit fb9d21
 *	|       |
Packit fb9d21
 *	|  |\   |
Packit fb9d21
 *	|  | \  |
Packit fb9d21
 *	|  |  \ |
Packit fb9d21
 *	|  |   \|
Packit fb9d21
 *	|  |    \
Packit fb9d21
 *	|  |_____\
Packit fb9d21
 *	|       |
Packit fb9d21
 *	|_______|
Packit fb9d21
 *
Packit fb9d21
 * Wrapping can fail if the result of wrapping one of the facets
Packit fb9d21
 * around its edges does not produce any new facet constraint.
Packit fb9d21
 * In particular, this happens when we try to wrap in unbounded sets.
Packit fb9d21
 *
Packit fb9d21
 *	 _______________________________________________________________________
Packit fb9d21
 *	|
Packit fb9d21
 *	|  ___
Packit fb9d21
 *	| |   |
Packit fb9d21
 *	|_|   |_________________________________________________________________
Packit fb9d21
 *	  |___|
Packit fb9d21
 *
Packit fb9d21
 * The following is not an acceptable result of coalescing the above two
Packit fb9d21
 * sets as it includes extra integer points.
Packit fb9d21
 *	 _______________________________________________________________________
Packit fb9d21
 *	|
Packit fb9d21
 *	|     
Packit fb9d21
 *	|      
Packit fb9d21
 *	|
Packit fb9d21
 *	 \______________________________________________________________________
Packit fb9d21
 */
Packit fb9d21
static int can_wrap_in_set(struct isl_map *map, int i, int j,
Packit fb9d21
	struct isl_tab **tabs, int *eq_i, int *ineq_i, int *eq_j, int *ineq_j)
Packit fb9d21
{
Packit fb9d21
	int changed = 0;
Packit fb9d21
	int k, m;
Packit fb9d21
	int n;
Packit fb9d21
	int *cuts = NULL;
Packit fb9d21
Packit fb9d21
	if (ISL_F_ISSET(map->p[i], ISL_BASIC_MAP_RATIONAL) ||
Packit fb9d21
	    ISL_F_ISSET(map->p[j], ISL_BASIC_MAP_RATIONAL))
Packit fb9d21
		return 0;
Packit fb9d21
Packit fb9d21
	n = count(ineq_i, map->p[i]->n_ineq, STATUS_CUT);
Packit fb9d21
	if (n == 0)
Packit fb9d21
		return 0;
Packit fb9d21
Packit fb9d21
	cuts = isl_alloc_array(map->ctx, int, n);
Packit fb9d21
	if (!cuts)
Packit fb9d21
		return -1;
Packit fb9d21
Packit fb9d21
	for (k = 0, m = 0; m < n; ++k) {
Packit fb9d21
		enum isl_ineq_type type;
Packit fb9d21
Packit fb9d21
		if (ineq_i[k] != STATUS_CUT)
Packit fb9d21
			continue;
Packit fb9d21
Packit fb9d21
		isl_int_add_ui(map->p[i]->ineq[k][0], map->p[i]->ineq[k][0], 1);
Packit fb9d21
		type = isl_tab_ineq_type(tabs[j], map->p[i]->ineq[k]);
Packit fb9d21
		isl_int_sub_ui(map->p[i]->ineq[k][0], map->p[i]->ineq[k][0], 1);
Packit fb9d21
		if (type == isl_ineq_error)
Packit fb9d21
			goto error;
Packit fb9d21
		if (type != isl_ineq_redundant)
Packit fb9d21
			break;
Packit fb9d21
		cuts[m] = k;
Packit fb9d21
		++m;
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	if (m == n)
Packit fb9d21
		changed = wrap_in_facets(map, i, j, cuts, n, tabs,
Packit fb9d21
					 eq_i, ineq_i, eq_j, ineq_j);
Packit fb9d21
Packit fb9d21
	free(cuts);
Packit fb9d21
Packit fb9d21
	return changed;
Packit fb9d21
error:
Packit fb9d21
	free(cuts);
Packit fb9d21
	return -1;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Check if either i or j has a single cut constraint that can
Packit fb9d21
 * be used to wrap in (a facet of) the other basic set.
Packit fb9d21
 * if so, replace the pair by their union.
Packit fb9d21
 */
Packit fb9d21
static int check_wrap(struct isl_map *map, int i, int j,
Packit fb9d21
	struct isl_tab **tabs, int *eq_i, int *ineq_i, int *eq_j, int *ineq_j)
Packit fb9d21
{
Packit fb9d21
	int changed = 0;
Packit fb9d21
Packit fb9d21
	if (!any(eq_i, 2 * map->p[i]->n_eq, STATUS_CUT))
Packit fb9d21
		changed = can_wrap_in_set(map, i, j, tabs,
Packit fb9d21
					    eq_i, ineq_i, eq_j, ineq_j);
Packit fb9d21
	if (changed)
Packit fb9d21
		return changed;
Packit fb9d21
Packit fb9d21
	if (!any(eq_j, 2 * map->p[j]->n_eq, STATUS_CUT))
Packit fb9d21
		changed = can_wrap_in_set(map, j, i, tabs,
Packit fb9d21
					    eq_j, ineq_j, eq_i, ineq_i);
Packit fb9d21
	return changed;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* At least one of the basic maps has an equality that is adjacent
Packit fb9d21
 * to inequality.  Make sure that only one of the basic maps has
Packit fb9d21
 * such an equality and that the other basic map has exactly one
Packit fb9d21
 * inequality adjacent to an equality.
Packit fb9d21
 * We call the basic map that has the inequality "i" and the basic
Packit fb9d21
 * map that has the equality "j".
Packit fb9d21
 * If "i" has any "cut" (in)equality, then relaxing the inequality
Packit fb9d21
 * by one would not result in a basic map that contains the other
Packit fb9d21
 * basic map.
Packit fb9d21
 */
Packit fb9d21
static int check_adj_eq(struct isl_map *map, int i, int j,
Packit fb9d21
	struct isl_tab **tabs, int *eq_i, int *ineq_i, int *eq_j, int *ineq_j)
Packit fb9d21
{
Packit fb9d21
	int changed = 0;
Packit fb9d21
	int k;
Packit fb9d21
Packit fb9d21
	if (any(eq_i, 2 * map->p[i]->n_eq, STATUS_ADJ_INEQ) &&
Packit fb9d21
	    any(eq_j, 2 * map->p[j]->n_eq, STATUS_ADJ_INEQ))
Packit fb9d21
		/* ADJ EQ TOO MANY */
Packit fb9d21
		return 0;
Packit fb9d21
Packit fb9d21
	if (any(eq_i, 2 * map->p[i]->n_eq, STATUS_ADJ_INEQ))
Packit fb9d21
		return check_adj_eq(map, j, i, tabs,
Packit fb9d21
					eq_j, ineq_j, eq_i, ineq_i);
Packit fb9d21
Packit fb9d21
	/* j has an equality adjacent to an inequality in i */
Packit fb9d21
Packit fb9d21
	if (any(eq_i, 2 * map->p[i]->n_eq, STATUS_CUT))
Packit fb9d21
		return 0;
Packit fb9d21
	if (any(ineq_i, map->p[i]->n_ineq, STATUS_CUT))
Packit fb9d21
		/* ADJ EQ CUT */
Packit fb9d21
		return 0;
Packit fb9d21
	if (count(ineq_i, map->p[i]->n_ineq, STATUS_ADJ_EQ) != 1 ||
Packit fb9d21
	    any(ineq_j, map->p[j]->n_ineq, STATUS_ADJ_EQ) ||
Packit fb9d21
	    any(ineq_i, map->p[i]->n_ineq, STATUS_ADJ_INEQ) ||
Packit fb9d21
	    any(ineq_j, map->p[j]->n_ineq, STATUS_ADJ_INEQ))
Packit fb9d21
		/* ADJ EQ TOO MANY */
Packit fb9d21
		return 0;
Packit fb9d21
Packit fb9d21
	for (k = 0; k < map->p[i]->n_ineq; ++k)
Packit fb9d21
		if (ineq_i[k] == STATUS_ADJ_EQ)
Packit fb9d21
			break;
Packit fb9d21
Packit fb9d21
	changed = is_adj_eq_extension(map, i, j, k, tabs,
Packit fb9d21
					eq_i, ineq_i, eq_j, ineq_j);
Packit fb9d21
	if (changed)
Packit fb9d21
		return changed;
Packit fb9d21
Packit fb9d21
	if (count(eq_j, 2 * map->p[j]->n_eq, STATUS_ADJ_INEQ) != 1)
Packit fb9d21
		return 0;
Packit fb9d21
Packit fb9d21
	changed = can_wrap_in_facet(map, i, j, k, tabs, eq_i, ineq_i, eq_j, ineq_j);
Packit fb9d21
Packit fb9d21
	return changed;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* The two basic maps lie on adjacent hyperplanes.  In particular,
Packit fb9d21
 * basic map "i" has an equality that lies parallel to basic map "j".
Packit fb9d21
 * Check if we can wrap the facets around the parallel hyperplanes
Packit fb9d21
 * to include the other set.
Packit fb9d21
 *
Packit fb9d21
 * We perform basically the same operations as can_wrap_in_facet,
Packit fb9d21
 * except that we don't need to select a facet of one of the sets.
Packit fb9d21
 *				_
Packit fb9d21
 *	\\			\\
Packit fb9d21
 *	 \\		=>	 \\
Packit fb9d21
 *	  \			  \|
Packit fb9d21
 *
Packit fb9d21
 * We only allow one equality of "i" to be adjacent to an equality of "j"
Packit fb9d21
 * to avoid coalescing
Packit fb9d21
 *
Packit fb9d21
 *	[m, n] -> { [x, y] -> [x, 1 + y] : x >= 1 and y >= 1 and
Packit fb9d21
 *					    x <= 10 and y <= 10;
Packit fb9d21
 *		    [x, y] -> [1 + x, y] : x >= 1 and x <= 20 and
Packit fb9d21
 *					    y >= 5 and y <= 15 }
Packit fb9d21
 *
Packit fb9d21
 * to
Packit fb9d21
 *
Packit fb9d21
 *	[m, n] -> { [x, y] -> [x2, y2] : x >= 1 and 10y2 <= 20 - x + 10y and
Packit fb9d21
 *					4y2 >= 5 + 3y and 5y2 <= 15 + 4y and
Packit fb9d21
 *					y2 <= 1 + x + y - x2 and y2 >= y and
Packit fb9d21
 *					y2 >= 1 + x + y - x2 }
Packit fb9d21
 */
Packit fb9d21
static int check_eq_adj_eq(struct isl_map *map, int i, int j,
Packit fb9d21
	struct isl_tab **tabs, int *eq_i, int *ineq_i, int *eq_j, int *ineq_j)
Packit fb9d21
{
Packit fb9d21
	int k;
Packit fb9d21
	int changed = 0;
Packit fb9d21
	struct isl_wraps wraps;
Packit fb9d21
	isl_mat *mat;
Packit fb9d21
	struct isl_set *set_i = NULL;
Packit fb9d21
	struct isl_set *set_j = NULL;
Packit fb9d21
	struct isl_vec *bound = NULL;
Packit fb9d21
	unsigned total = isl_basic_map_total_dim(map->p[i]);
Packit fb9d21
Packit fb9d21
	if (count(eq_i, 2 * map->p[i]->n_eq, STATUS_ADJ_EQ) != 1)
Packit fb9d21
		return 0;
Packit fb9d21
Packit fb9d21
	for (k = 0; k < 2 * map->p[i]->n_eq ; ++k)
Packit fb9d21
		if (eq_i[k] == STATUS_ADJ_EQ)
Packit fb9d21
			break;
Packit fb9d21
Packit fb9d21
	set_i = set_from_updated_bmap(map->p[i], tabs[i]);
Packit fb9d21
	set_j = set_from_updated_bmap(map->p[j], tabs[j]);
Packit fb9d21
	mat = isl_mat_alloc(map->ctx, 2 * (map->p[i]->n_eq + map->p[j]->n_eq) +
Packit fb9d21
					map->p[i]->n_ineq + map->p[j]->n_ineq,
Packit fb9d21
					1 + total);
Packit fb9d21
	wraps_init(&wraps, mat, map, i, j, eq_i, ineq_i, eq_j, ineq_j);
Packit fb9d21
	bound = isl_vec_alloc(map->ctx, 1 + total);
Packit fb9d21
	if (!set_i || !set_j || !wraps.mat || !bound)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	if (k % 2 == 0)
Packit fb9d21
		isl_seq_neg(bound->el, map->p[i]->eq[k / 2], 1 + total);
Packit fb9d21
	else
Packit fb9d21
		isl_seq_cpy(bound->el, map->p[i]->eq[k / 2], 1 + total);
Packit fb9d21
	isl_int_add_ui(bound->el[0], bound->el[0], 1);
Packit fb9d21
Packit fb9d21
	isl_seq_cpy(wraps.mat->row[0], bound->el, 1 + total);
Packit fb9d21
	wraps.mat->n_row = 1;
Packit fb9d21
Packit fb9d21
	if (add_wraps(&wraps, map->p[j], tabs[j], bound->el, set_i) < 0)
Packit fb9d21
		goto error;
Packit fb9d21
	if (!wraps.mat->n_row)
Packit fb9d21
		goto unbounded;
Packit fb9d21
Packit fb9d21
	isl_int_sub_ui(bound->el[0], bound->el[0], 1);
Packit fb9d21
	isl_seq_neg(bound->el, bound->el, 1 + total);
Packit fb9d21
Packit fb9d21
	isl_seq_cpy(wraps.mat->row[wraps.mat->n_row], bound->el, 1 + total);
Packit fb9d21
	wraps.mat->n_row++;
Packit fb9d21
Packit fb9d21
	if (add_wraps(&wraps, map->p[i], tabs[i], bound->el, set_j) < 0)
Packit fb9d21
		goto error;
Packit fb9d21
	if (!wraps.mat->n_row)
Packit fb9d21
		goto unbounded;
Packit fb9d21
Packit fb9d21
	changed = fuse(map, i, j, tabs, eq_i, ineq_i, eq_j, ineq_j, wraps.mat);
Packit fb9d21
Packit fb9d21
	if (0) {
Packit fb9d21
error:		changed = -1;
Packit fb9d21
	}
Packit fb9d21
unbounded:
Packit fb9d21
Packit fb9d21
	wraps_free(&wraps);
Packit fb9d21
	isl_set_free(set_i);
Packit fb9d21
	isl_set_free(set_j);
Packit fb9d21
	isl_vec_free(bound);
Packit fb9d21
Packit fb9d21
	return changed;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Check if the union of the given pair of basic maps
Packit fb9d21
 * can be represented by a single basic map.
Packit fb9d21
 * If so, replace the pair by the single basic map and return 1.
Packit fb9d21
 * Otherwise, return 0;
Packit fb9d21
 * The two basic maps are assumed to live in the same local space.
Packit fb9d21
 *
Packit fb9d21
 * We first check the effect of each constraint of one basic map
Packit fb9d21
 * on the other basic map.
Packit fb9d21
 * The constraint may be
Packit fb9d21
 *	redundant	the constraint is redundant in its own
Packit fb9d21
 *			basic map and should be ignore and removed
Packit fb9d21
 *			in the end
Packit fb9d21
 *	valid		all (integer) points of the other basic map
Packit fb9d21
 *			satisfy the constraint
Packit fb9d21
 *	separate	no (integer) point of the other basic map
Packit fb9d21
 *			satisfies the constraint
Packit fb9d21
 *	cut		some but not all points of the other basic map
Packit fb9d21
 *			satisfy the constraint
Packit fb9d21
 *	adj_eq		the given constraint is adjacent (on the outside)
Packit fb9d21
 *			to an equality of the other basic map
Packit fb9d21
 *	adj_ineq	the given constraint is adjacent (on the outside)
Packit fb9d21
 *			to an inequality of the other basic map
Packit fb9d21
 *
Packit fb9d21
 * We consider seven cases in which we can replace the pair by a single
Packit fb9d21
 * basic map.  We ignore all "redundant" constraints.
Packit fb9d21
 *
Packit fb9d21
 *	1. all constraints of one basic map are valid
Packit fb9d21
 *		=> the other basic map is a subset and can be removed
Packit fb9d21
 *
Packit fb9d21
 *	2. all constraints of both basic maps are either "valid" or "cut"
Packit fb9d21
 *	   and the facets corresponding to the "cut" constraints
Packit fb9d21
 *	   of one of the basic maps lies entirely inside the other basic map
Packit fb9d21
 *		=> the pair can be replaced by a basic map consisting
Packit fb9d21
 *		   of the valid constraints in both basic maps
Packit fb9d21
 *
Packit fb9d21
 *	3. there is a single pair of adjacent inequalities
Packit fb9d21
 *	   (all other constraints are "valid")
Packit fb9d21
 *		=> the pair can be replaced by a basic map consisting
Packit fb9d21
 *		   of the valid constraints in both basic maps
Packit fb9d21
 *
Packit fb9d21
 *	4. one basic map has a single adjacent inequality, while the other
Packit fb9d21
 *	   constraints are "valid".  The other basic map has some
Packit fb9d21
 *	   "cut" constraints, but replacing the adjacent inequality by
Packit fb9d21
 *	   its opposite and adding the valid constraints of the other
Packit fb9d21
 *	   basic map results in a subset of the other basic map
Packit fb9d21
 *		=> the pair can be replaced by a basic map consisting
Packit fb9d21
 *		   of the valid constraints in both basic maps
Packit fb9d21
 *
Packit fb9d21
 *	5. there is a single adjacent pair of an inequality and an equality,
Packit fb9d21
 *	   the other constraints of the basic map containing the inequality are
Packit fb9d21
 *	   "valid".  Moreover, if the inequality the basic map is relaxed
Packit fb9d21
 *	   and then turned into an equality, then resulting facet lies
Packit fb9d21
 *	   entirely inside the other basic map
Packit fb9d21
 *		=> the pair can be replaced by the basic map containing
Packit fb9d21
 *		   the inequality, with the inequality relaxed.
Packit fb9d21
 *
Packit fb9d21
 *	6. there is a single adjacent pair of an inequality and an equality,
Packit fb9d21
 *	   the other constraints of the basic map containing the inequality are
Packit fb9d21
 *	   "valid".  Moreover, the facets corresponding to both
Packit fb9d21
 *	   the inequality and the equality can be wrapped around their
Packit fb9d21
 *	   ridges to include the other basic map
Packit fb9d21
 *		=> the pair can be replaced by a basic map consisting
Packit fb9d21
 *		   of the valid constraints in both basic maps together
Packit fb9d21
 *		   with all wrapping constraints
Packit fb9d21
 *
Packit fb9d21
 *	7. one of the basic maps extends beyond the other by at most one.
Packit fb9d21
 *	   Moreover, the facets corresponding to the cut constraints and
Packit fb9d21
 *	   the pieces of the other basic map at offset one from these cut
Packit fb9d21
 *	   constraints can be wrapped around their ridges to include
Packit fb9d21
 *	   the union of the two basic maps
Packit fb9d21
 *		=> the pair can be replaced by a basic map consisting
Packit fb9d21
 *		   of the valid constraints in both basic maps together
Packit fb9d21
 *		   with all wrapping constraints
Packit fb9d21
 *
Packit fb9d21
 *	8. the two basic maps live in adjacent hyperplanes.  In principle
Packit fb9d21
 *	   such sets can always be combined through wrapping, but we impose
Packit fb9d21
 *	   that there is only one such pair, to avoid overeager coalescing.
Packit fb9d21
 *
Packit fb9d21
 * Throughout the computation, we maintain a collection of tableaus
Packit fb9d21
 * corresponding to the basic maps.  When the basic maps are dropped
Packit fb9d21
 * or combined, the tableaus are modified accordingly.
Packit fb9d21
 */
Packit fb9d21
static int coalesce_local_pair(__isl_keep isl_map *map, int i, int j,
Packit fb9d21
	struct isl_tab **tabs)
Packit fb9d21
{
Packit fb9d21
	int changed = 0;
Packit fb9d21
	int *eq_i = NULL;
Packit fb9d21
	int *eq_j = NULL;
Packit fb9d21
	int *ineq_i = NULL;
Packit fb9d21
	int *ineq_j = NULL;
Packit fb9d21
Packit fb9d21
	eq_i = eq_status_in(map->p[i], tabs[j]);
Packit fb9d21
	if (map->p[i]->n_eq && !eq_i)
Packit fb9d21
		goto error;
Packit fb9d21
	if (any(eq_i, 2 * map->p[i]->n_eq, STATUS_ERROR))
Packit fb9d21
		goto error;
Packit fb9d21
	if (any(eq_i, 2 * map->p[i]->n_eq, STATUS_SEPARATE))
Packit fb9d21
		goto done;
Packit fb9d21
Packit fb9d21
	eq_j = eq_status_in(map->p[j], tabs[i]);
Packit fb9d21
	if (map->p[j]->n_eq && !eq_j)
Packit fb9d21
		goto error;
Packit fb9d21
	if (any(eq_j, 2 * map->p[j]->n_eq, STATUS_ERROR))
Packit fb9d21
		goto error;
Packit fb9d21
	if (any(eq_j, 2 * map->p[j]->n_eq, STATUS_SEPARATE))
Packit fb9d21
		goto done;
Packit fb9d21
Packit fb9d21
	ineq_i = ineq_status_in(map->p[i], tabs[i], tabs[j]);
Packit fb9d21
	if (map->p[i]->n_ineq && !ineq_i)
Packit fb9d21
		goto error;
Packit fb9d21
	if (any(ineq_i, map->p[i]->n_ineq, STATUS_ERROR))
Packit fb9d21
		goto error;
Packit fb9d21
	if (any(ineq_i, map->p[i]->n_ineq, STATUS_SEPARATE))
Packit fb9d21
		goto done;
Packit fb9d21
Packit fb9d21
	ineq_j = ineq_status_in(map->p[j], tabs[j], tabs[i]);
Packit fb9d21
	if (map->p[j]->n_ineq && !ineq_j)
Packit fb9d21
		goto error;
Packit fb9d21
	if (any(ineq_j, map->p[j]->n_ineq, STATUS_ERROR))
Packit fb9d21
		goto error;
Packit fb9d21
	if (any(ineq_j, map->p[j]->n_ineq, STATUS_SEPARATE))
Packit fb9d21
		goto done;
Packit fb9d21
Packit fb9d21
	if (all(eq_i, 2 * map->p[i]->n_eq, STATUS_VALID) &&
Packit fb9d21
	    all(ineq_i, map->p[i]->n_ineq, STATUS_VALID)) {
Packit fb9d21
		drop(map, j, tabs);
Packit fb9d21
		changed = 1;
Packit fb9d21
	} else if (all(eq_j, 2 * map->p[j]->n_eq, STATUS_VALID) &&
Packit fb9d21
		   all(ineq_j, map->p[j]->n_ineq, STATUS_VALID)) {
Packit fb9d21
		drop(map, i, tabs);
Packit fb9d21
		changed = 1;
Packit fb9d21
	} else if (any(eq_i, 2 * map->p[i]->n_eq, STATUS_ADJ_EQ)) {
Packit fb9d21
		changed = check_eq_adj_eq(map, i, j, tabs,
Packit fb9d21
					eq_i, ineq_i, eq_j, ineq_j);
Packit fb9d21
	} else if (any(eq_j, 2 * map->p[j]->n_eq, STATUS_ADJ_EQ)) {
Packit fb9d21
		changed = check_eq_adj_eq(map, j, i, tabs,
Packit fb9d21
					eq_j, ineq_j, eq_i, ineq_i);
Packit fb9d21
	} else if (any(eq_i, 2 * map->p[i]->n_eq, STATUS_ADJ_INEQ) ||
Packit fb9d21
		   any(eq_j, 2 * map->p[j]->n_eq, STATUS_ADJ_INEQ)) {
Packit fb9d21
		changed = check_adj_eq(map, i, j, tabs,
Packit fb9d21
					eq_i, ineq_i, eq_j, ineq_j);
Packit fb9d21
	} else if (any(ineq_i, map->p[i]->n_ineq, STATUS_ADJ_EQ) ||
Packit fb9d21
		   any(ineq_j, map->p[j]->n_ineq, STATUS_ADJ_EQ)) {
Packit fb9d21
		/* Can't happen */
Packit fb9d21
		/* BAD ADJ INEQ */
Packit fb9d21
	} else if (any(ineq_i, map->p[i]->n_ineq, STATUS_ADJ_INEQ) ||
Packit fb9d21
		   any(ineq_j, map->p[j]->n_ineq, STATUS_ADJ_INEQ)) {
Packit fb9d21
		changed = check_adj_ineq(map, i, j, tabs,
Packit fb9d21
					eq_i, ineq_i, eq_j, ineq_j);
Packit fb9d21
	} else {
Packit fb9d21
		if (!any(eq_i, 2 * map->p[i]->n_eq, STATUS_CUT) &&
Packit fb9d21
		    !any(eq_j, 2 * map->p[j]->n_eq, STATUS_CUT))
Packit fb9d21
			changed = check_facets(map, i, j, tabs, ineq_i, ineq_j);
Packit fb9d21
		if (!changed)
Packit fb9d21
			changed = check_wrap(map, i, j, tabs,
Packit fb9d21
						eq_i, ineq_i, eq_j, ineq_j);
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
done:
Packit fb9d21
	free(eq_i);
Packit fb9d21
	free(eq_j);
Packit fb9d21
	free(ineq_i);
Packit fb9d21
	free(ineq_j);
Packit fb9d21
	return changed;
Packit fb9d21
error:
Packit fb9d21
	free(eq_i);
Packit fb9d21
	free(eq_j);
Packit fb9d21
	free(ineq_i);
Packit fb9d21
	free(ineq_j);
Packit fb9d21
	return -1;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Do the two basic maps live in the same local space, i.e.,
Packit fb9d21
 * do they have the same (known) divs?
Packit fb9d21
 * If either basic map has any unknown divs, then we can only assume
Packit fb9d21
 * that they do not live in the same local space.
Packit fb9d21
 */
Packit fb9d21
static int same_divs(__isl_keep isl_basic_map *bmap1,
Packit fb9d21
	__isl_keep isl_basic_map *bmap2)
Packit fb9d21
{
Packit fb9d21
	int i;
Packit fb9d21
	int known;
Packit fb9d21
	int total;
Packit fb9d21
Packit fb9d21
	if (!bmap1 || !bmap2)
Packit fb9d21
		return -1;
Packit fb9d21
	if (bmap1->n_div != bmap2->n_div)
Packit fb9d21
		return 0;
Packit fb9d21
Packit fb9d21
	if (bmap1->n_div == 0)
Packit fb9d21
		return 1;
Packit fb9d21
Packit fb9d21
	known = isl_basic_map_divs_known(bmap1);
Packit fb9d21
	if (known < 0 || !known)
Packit fb9d21
		return known;
Packit fb9d21
	known = isl_basic_map_divs_known(bmap2);
Packit fb9d21
	if (known < 0 || !known)
Packit fb9d21
		return known;
Packit fb9d21
Packit fb9d21
	total = isl_basic_map_total_dim(bmap1);
Packit fb9d21
	for (i = 0; i < bmap1->n_div; ++i)
Packit fb9d21
		if (!isl_seq_eq(bmap1->div[i], bmap2->div[i], 2 + total))
Packit fb9d21
			return 0;
Packit fb9d21
Packit fb9d21
	return 1;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Given two basic maps "i" and "j", where the divs of "i" form a subset
Packit fb9d21
 * of those of "j", check if basic map "j" is a subset of basic map "i"
Packit fb9d21
 * and, if so, drop basic map "j".
Packit fb9d21
 *
Packit fb9d21
 * We first expand the divs of basic map "i" to match those of basic map "j",
Packit fb9d21
 * using the divs and expansion computed by the caller.
Packit fb9d21
 * Then we check if all constraints of the expanded "i" are valid for "j".
Packit fb9d21
 */
Packit fb9d21
static int coalesce_subset(__isl_keep isl_map *map, int i, int j,
Packit fb9d21
	struct isl_tab **tabs, __isl_keep isl_mat *div, int *exp)
Packit fb9d21
{
Packit fb9d21
	isl_basic_map *bmap;
Packit fb9d21
	int changed = 0;
Packit fb9d21
	int *eq_i = NULL;
Packit fb9d21
	int *ineq_i = NULL;
Packit fb9d21
Packit fb9d21
	bmap = isl_basic_map_copy(map->p[i]);
Packit fb9d21
	bmap = isl_basic_set_expand_divs(bmap, isl_mat_copy(div), exp);
Packit fb9d21
Packit fb9d21
	if (!bmap)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	eq_i = eq_status_in(bmap, tabs[j]);
Packit fb9d21
	if (bmap->n_eq && !eq_i)
Packit fb9d21
		goto error;
Packit fb9d21
	if (any(eq_i, 2 * bmap->n_eq, STATUS_ERROR))
Packit fb9d21
		goto error;
Packit fb9d21
	if (any(eq_i, 2 * bmap->n_eq, STATUS_SEPARATE))
Packit fb9d21
		goto done;
Packit fb9d21
Packit fb9d21
	ineq_i = ineq_status_in(bmap, NULL, tabs[j]);
Packit fb9d21
	if (bmap->n_ineq && !ineq_i)
Packit fb9d21
		goto error;
Packit fb9d21
	if (any(ineq_i, bmap->n_ineq, STATUS_ERROR))
Packit fb9d21
		goto error;
Packit fb9d21
	if (any(ineq_i, bmap->n_ineq, STATUS_SEPARATE))
Packit fb9d21
		goto done;
Packit fb9d21
Packit fb9d21
	if (all(eq_i, 2 * map->p[i]->n_eq, STATUS_VALID) &&
Packit fb9d21
	    all(ineq_i, map->p[i]->n_ineq, STATUS_VALID)) {
Packit fb9d21
		drop(map, j, tabs);
Packit fb9d21
		changed = 1;
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
done:
Packit fb9d21
	isl_basic_map_free(bmap);
Packit fb9d21
	free(eq_i);
Packit fb9d21
	free(ineq_i);
Packit fb9d21
	return 0;
Packit fb9d21
error:
Packit fb9d21
	isl_basic_map_free(bmap);
Packit fb9d21
	free(eq_i);
Packit fb9d21
	free(ineq_i);
Packit fb9d21
	return -1;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Check if the basic map "j" is a subset of basic map "i",
Packit fb9d21
 * assuming that "i" has fewer divs that "j".
Packit fb9d21
 * If not, then we change the order.
Packit fb9d21
 *
Packit fb9d21
 * If the two basic maps have the same number of divs, then
Packit fb9d21
 * they must necessarily be different.  Otherwise, we would have
Packit fb9d21
 * called coalesce_local_pair.  We therefore don't try anything
Packit fb9d21
 * in this case.
Packit fb9d21
 *
Packit fb9d21
 * We first check if the divs of "i" are all known and form a subset
Packit fb9d21
 * of those of "j".  If so, we pass control over to coalesce_subset.
Packit fb9d21
 */
Packit fb9d21
static int check_coalesce_subset(__isl_keep isl_map *map, int i, int j,
Packit fb9d21
	struct isl_tab **tabs)
Packit fb9d21
{
Packit fb9d21
	int known;
Packit fb9d21
	isl_mat *div_i, *div_j, *div;
Packit fb9d21
	int *exp1 = NULL;
Packit fb9d21
	int *exp2 = NULL;
Packit fb9d21
	isl_ctx *ctx;
Packit fb9d21
	int subset;
Packit fb9d21
Packit fb9d21
	if (map->p[i]->n_div == map->p[j]->n_div)
Packit fb9d21
		return 0;
Packit fb9d21
	if (map->p[j]->n_div < map->p[i]->n_div)
Packit fb9d21
		return check_coalesce_subset(map, j, i, tabs);
Packit fb9d21
Packit fb9d21
	known = isl_basic_map_divs_known(map->p[i]);
Packit fb9d21
	if (known < 0 || !known)
Packit fb9d21
		return known;
Packit fb9d21
Packit fb9d21
	ctx = isl_map_get_ctx(map);
Packit fb9d21
Packit fb9d21
	div_i = isl_basic_map_get_divs(map->p[i]);
Packit fb9d21
	div_j = isl_basic_map_get_divs(map->p[j]);
Packit fb9d21
Packit fb9d21
	if (!div_i || !div_j)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	exp1 = isl_alloc_array(ctx, int, div_i->n_row);
Packit fb9d21
	exp2 = isl_alloc_array(ctx, int, div_j->n_row);
Packit fb9d21
	if ((div_i->n_row && !exp1) || (div_j->n_row && !exp2))
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	div = isl_merge_divs(div_i, div_j, exp1, exp2);
Packit fb9d21
	if (!div)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	if (div->n_row == div_j->n_row)
Packit fb9d21
		subset = coalesce_subset(map, i, j, tabs, div, exp1);
Packit fb9d21
	else
Packit fb9d21
		subset = 0;
Packit fb9d21
Packit fb9d21
	isl_mat_free(div);
Packit fb9d21
Packit fb9d21
	isl_mat_free(div_i);
Packit fb9d21
	isl_mat_free(div_j);
Packit fb9d21
Packit fb9d21
	free(exp2);
Packit fb9d21
	free(exp1);
Packit fb9d21
Packit fb9d21
	return subset;
Packit fb9d21
error:
Packit fb9d21
	isl_mat_free(div_i);
Packit fb9d21
	isl_mat_free(div_j);
Packit fb9d21
	free(exp1);
Packit fb9d21
	free(exp2);
Packit fb9d21
	return -1;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Check if the union of the given pair of basic maps
Packit fb9d21
 * can be represented by a single basic map.
Packit fb9d21
 * If so, replace the pair by the single basic map and return 1.
Packit fb9d21
 * Otherwise, return 0;
Packit fb9d21
 *
Packit fb9d21
 * We first check if the two basic maps live in the same local space.
Packit fb9d21
 * If so, we do the complete check.  Otherwise, we check if one is
Packit fb9d21
 * an obvious subset of the other.
Packit fb9d21
 */
Packit fb9d21
static int coalesce_pair(__isl_keep isl_map *map, int i, int j,
Packit fb9d21
	struct isl_tab **tabs)
Packit fb9d21
{
Packit fb9d21
	int same;
Packit fb9d21
Packit fb9d21
	same = same_divs(map->p[i], map->p[j]);
Packit fb9d21
	if (same < 0)
Packit fb9d21
		return -1;
Packit fb9d21
	if (same)
Packit fb9d21
		return coalesce_local_pair(map, i, j, tabs);
Packit fb9d21
Packit fb9d21
	return check_coalesce_subset(map, i, j, tabs);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
static struct isl_map *coalesce(struct isl_map *map, struct isl_tab **tabs)
Packit fb9d21
{
Packit fb9d21
	int i, j;
Packit fb9d21
Packit fb9d21
	for (i = map->n - 2; i >= 0; --i)
Packit fb9d21
restart:
Packit fb9d21
		for (j = i + 1; j < map->n; ++j) {
Packit fb9d21
			int changed;
Packit fb9d21
			changed = coalesce_pair(map, i, j, tabs);
Packit fb9d21
			if (changed < 0)
Packit fb9d21
				goto error;
Packit fb9d21
			if (changed)
Packit fb9d21
				goto restart;
Packit fb9d21
		}
Packit fb9d21
	return map;
Packit fb9d21
error:
Packit fb9d21
	isl_map_free(map);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* For each pair of basic maps in the map, check if the union of the two
Packit fb9d21
 * can be represented by a single basic map.
Packit fb9d21
 * If so, replace the pair by the single basic map and start over.
Packit fb9d21
 *
Packit fb9d21
 * Since we are constructing the tableaus of the basic maps anyway,
Packit fb9d21
 * we exploit them to detect implicit equalities and redundant constraints.
Packit fb9d21
 * This also helps the coalescing as it can ignore the redundant constraints.
Packit fb9d21
 * In order to avoid confusion, we make all implicit equalities explicit
Packit fb9d21
 * in the basic maps.  We don't call isl_basic_map_gauss, though,
Packit fb9d21
 * as that may affect the number of constraints.
Packit fb9d21
 * This means that we have to call isl_basic_map_gauss at the end
Packit fb9d21
 * of the computation to ensure that the basic maps are not left
Packit fb9d21
 * in an unexpected state.
Packit fb9d21
 */
Packit fb9d21
struct isl_map *isl_map_coalesce(struct isl_map *map)
Packit fb9d21
{
Packit fb9d21
	int i;
Packit fb9d21
	unsigned n;
Packit fb9d21
	struct isl_tab **tabs = NULL;
Packit fb9d21
Packit fb9d21
	map = isl_map_remove_empty_parts(map);
Packit fb9d21
	if (!map)
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	if (map->n <= 1)
Packit fb9d21
		return map;
Packit fb9d21
Packit fb9d21
	map = isl_map_sort_divs(map);
Packit fb9d21
	map = isl_map_cow(map);
Packit fb9d21
Packit fb9d21
	if (!map)
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	tabs = isl_calloc_array(map->ctx, struct isl_tab *, map->n);
Packit fb9d21
	if (!tabs)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	n = map->n;
Packit fb9d21
	for (i = 0; i < map->n; ++i) {
Packit fb9d21
		tabs[i] = isl_tab_from_basic_map(map->p[i], 0);
Packit fb9d21
		if (!tabs[i])
Packit fb9d21
			goto error;
Packit fb9d21
		if (!ISL_F_ISSET(map->p[i], ISL_BASIC_MAP_NO_IMPLICIT))
Packit fb9d21
			if (isl_tab_detect_implicit_equalities(tabs[i]) < 0)
Packit fb9d21
				goto error;
Packit fb9d21
		map->p[i] = isl_tab_make_equalities_explicit(tabs[i],
Packit fb9d21
								map->p[i]);
Packit fb9d21
		if (!map->p[i])
Packit fb9d21
			goto error;
Packit fb9d21
		if (!ISL_F_ISSET(map->p[i], ISL_BASIC_MAP_NO_REDUNDANT))
Packit fb9d21
			if (isl_tab_detect_redundant(tabs[i]) < 0)
Packit fb9d21
				goto error;
Packit fb9d21
	}
Packit fb9d21
	for (i = map->n - 1; i >= 0; --i)
Packit fb9d21
		if (tabs[i]->empty)
Packit fb9d21
			drop(map, i, tabs);
Packit fb9d21
Packit fb9d21
	map = coalesce(map, tabs);
Packit fb9d21
Packit fb9d21
	if (map)
Packit fb9d21
		for (i = 0; i < map->n; ++i) {
Packit fb9d21
			map->p[i] = isl_basic_map_update_from_tab(map->p[i],
Packit fb9d21
								    tabs[i]);
Packit fb9d21
			map->p[i] = isl_basic_map_gauss(map->p[i], NULL);
Packit fb9d21
			map->p[i] = isl_basic_map_finalize(map->p[i]);
Packit fb9d21
			if (!map->p[i])
Packit fb9d21
				goto error;
Packit fb9d21
			ISL_F_SET(map->p[i], ISL_BASIC_MAP_NO_IMPLICIT);
Packit fb9d21
			ISL_F_SET(map->p[i], ISL_BASIC_MAP_NO_REDUNDANT);
Packit fb9d21
		}
Packit fb9d21
Packit fb9d21
	for (i = 0; i < n; ++i)
Packit fb9d21
		isl_tab_free(tabs[i]);
Packit fb9d21
Packit fb9d21
	free(tabs);
Packit fb9d21
Packit fb9d21
	return map;
Packit fb9d21
error:
Packit fb9d21
	if (tabs)
Packit fb9d21
		for (i = 0; i < n; ++i)
Packit fb9d21
			isl_tab_free(tabs[i]);
Packit fb9d21
	free(tabs);
Packit fb9d21
	isl_map_free(map);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* For each pair of basic sets in the set, check if the union of the two
Packit fb9d21
 * can be represented by a single basic set.
Packit fb9d21
 * If so, replace the pair by the single basic set and start over.
Packit fb9d21
 */
Packit fb9d21
struct isl_set *isl_set_coalesce(struct isl_set *set)
Packit fb9d21
{
Packit fb9d21
	return (struct isl_set *)isl_map_coalesce((struct isl_map *)set);
Packit fb9d21
}