Blame isl-0.14/isl_convex_hull.c

Packit fb9d21
/*
Packit fb9d21
 * Copyright 2008-2009 Katholieke Universiteit Leuven
Packit fb9d21
 * Copyright 2014      INRIA Rocquencourt
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 Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
Packit fb9d21
 * B.P. 105 - 78153 Le Chesnay, France
Packit fb9d21
 */
Packit fb9d21
Packit fb9d21
#include <isl_ctx_private.h>
Packit fb9d21
#include <isl_map_private.h>
Packit fb9d21
#include <isl_lp_private.h>
Packit fb9d21
#include <isl/map.h>
Packit fb9d21
#include <isl_mat_private.h>
Packit fb9d21
#include <isl_vec_private.h>
Packit fb9d21
#include <isl/set.h>
Packit fb9d21
#include <isl_seq.h>
Packit fb9d21
#include <isl_options_private.h>
Packit fb9d21
#include "isl_equalities.h"
Packit fb9d21
#include "isl_tab.h"
Packit fb9d21
#include <isl_sort.h>
Packit fb9d21
Packit fb9d21
static struct isl_basic_set *uset_convex_hull_wrap_bounded(struct isl_set *set);
Packit fb9d21
Packit fb9d21
/* Return 1 if constraint c is redundant with respect to the constraints
Packit fb9d21
 * in bmap.  If c is a lower [upper] bound in some variable and bmap
Packit fb9d21
 * does not have a lower [upper] bound in that variable, then c cannot
Packit fb9d21
 * be redundant and we do not need solve any lp.
Packit fb9d21
 */
Packit fb9d21
int isl_basic_map_constraint_is_redundant(struct isl_basic_map **bmap,
Packit fb9d21
	isl_int *c, isl_int *opt_n, isl_int *opt_d)
Packit fb9d21
{
Packit fb9d21
	enum isl_lp_result res;
Packit fb9d21
	unsigned total;
Packit fb9d21
	int i, j;
Packit fb9d21
Packit fb9d21
	if (!bmap)
Packit fb9d21
		return -1;
Packit fb9d21
Packit fb9d21
	total = isl_basic_map_total_dim(*bmap);
Packit fb9d21
	for (i = 0; i < total; ++i) {
Packit fb9d21
		int sign;
Packit fb9d21
		if (isl_int_is_zero(c[1+i]))
Packit fb9d21
			continue;
Packit fb9d21
		sign = isl_int_sgn(c[1+i]);
Packit fb9d21
		for (j = 0; j < (*bmap)->n_ineq; ++j)
Packit fb9d21
			if (sign == isl_int_sgn((*bmap)->ineq[j][1+i]))
Packit fb9d21
				break;
Packit fb9d21
		if (j == (*bmap)->n_ineq)
Packit fb9d21
			break;
Packit fb9d21
	}
Packit fb9d21
	if (i < total)
Packit fb9d21
		return 0;
Packit fb9d21
Packit fb9d21
	res = isl_basic_map_solve_lp(*bmap, 0, c, (*bmap)->ctx->one,
Packit fb9d21
					opt_n, opt_d, NULL);
Packit fb9d21
	if (res == isl_lp_unbounded)
Packit fb9d21
		return 0;
Packit fb9d21
	if (res == isl_lp_error)
Packit fb9d21
		return -1;
Packit fb9d21
	if (res == isl_lp_empty) {
Packit fb9d21
		*bmap = isl_basic_map_set_to_empty(*bmap);
Packit fb9d21
		return 0;
Packit fb9d21
	}
Packit fb9d21
	return !isl_int_is_neg(*opt_n);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
int isl_basic_set_constraint_is_redundant(struct isl_basic_set **bset,
Packit fb9d21
	isl_int *c, isl_int *opt_n, isl_int *opt_d)
Packit fb9d21
{
Packit fb9d21
	return isl_basic_map_constraint_is_redundant(
Packit fb9d21
			(struct isl_basic_map **)bset, c, opt_n, opt_d);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Remove redundant
Packit fb9d21
 * constraints.  If the minimal value along the normal of a constraint
Packit fb9d21
 * is the same if the constraint is removed, then the constraint is redundant.
Packit fb9d21
 *
Packit fb9d21
 * Alternatively, we could have intersected the basic map with the
Packit fb9d21
 * corresponding equality and the checked if the dimension was that
Packit fb9d21
 * of a facet.
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_basic_map *isl_basic_map_remove_redundancies(
Packit fb9d21
	__isl_take isl_basic_map *bmap)
Packit fb9d21
{
Packit fb9d21
	struct isl_tab *tab;
Packit fb9d21
Packit fb9d21
	if (!bmap)
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	bmap = isl_basic_map_gauss(bmap, NULL);
Packit fb9d21
	if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
Packit fb9d21
		return bmap;
Packit fb9d21
	if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NO_REDUNDANT))
Packit fb9d21
		return bmap;
Packit fb9d21
	if (bmap->n_ineq <= 1)
Packit fb9d21
		return bmap;
Packit fb9d21
Packit fb9d21
	tab = isl_tab_from_basic_map(bmap, 0);
Packit fb9d21
	if (isl_tab_detect_implicit_equalities(tab) < 0)
Packit fb9d21
		goto error;
Packit fb9d21
	if (isl_tab_detect_redundant(tab) < 0)
Packit fb9d21
		goto error;
Packit fb9d21
	bmap = isl_basic_map_update_from_tab(bmap, tab);
Packit fb9d21
	isl_tab_free(tab);
Packit fb9d21
	ISL_F_SET(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
Packit fb9d21
	ISL_F_SET(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
Packit fb9d21
	return bmap;
Packit fb9d21
error:
Packit fb9d21
	isl_tab_free(tab);
Packit fb9d21
	isl_basic_map_free(bmap);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
__isl_give isl_basic_set *isl_basic_set_remove_redundancies(
Packit fb9d21
	__isl_take isl_basic_set *bset)
Packit fb9d21
{
Packit fb9d21
	return (struct isl_basic_set *)
Packit fb9d21
		isl_basic_map_remove_redundancies((struct isl_basic_map *)bset);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Remove redundant constraints in each of the basic maps.
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_map *isl_map_remove_redundancies(__isl_take isl_map *map)
Packit fb9d21
{
Packit fb9d21
	return isl_map_inline_foreach_basic_map(map,
Packit fb9d21
					    &isl_basic_map_remove_redundancies);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
__isl_give isl_set *isl_set_remove_redundancies(__isl_take isl_set *set)
Packit fb9d21
{
Packit fb9d21
	return isl_map_remove_redundancies(set);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Check if the set set is bound in the direction of the affine
Packit fb9d21
 * constraint c and if so, set the constant term such that the
Packit fb9d21
 * resulting constraint is a bounding constraint for the set.
Packit fb9d21
 */
Packit fb9d21
static int uset_is_bound(struct isl_set *set, isl_int *c, unsigned len)
Packit fb9d21
{
Packit fb9d21
	int first;
Packit fb9d21
	int j;
Packit fb9d21
	isl_int opt;
Packit fb9d21
	isl_int opt_denom;
Packit fb9d21
Packit fb9d21
	isl_int_init(opt);
Packit fb9d21
	isl_int_init(opt_denom);
Packit fb9d21
	first = 1;
Packit fb9d21
	for (j = 0; j < set->n; ++j) {
Packit fb9d21
		enum isl_lp_result res;
Packit fb9d21
Packit fb9d21
		if (ISL_F_ISSET(set->p[j], ISL_BASIC_SET_EMPTY))
Packit fb9d21
			continue;
Packit fb9d21
Packit fb9d21
		res = isl_basic_set_solve_lp(set->p[j],
Packit fb9d21
				0, c, set->ctx->one, &opt, &opt_denom, NULL);
Packit fb9d21
		if (res == isl_lp_unbounded)
Packit fb9d21
			break;
Packit fb9d21
		if (res == isl_lp_error)
Packit fb9d21
			goto error;
Packit fb9d21
		if (res == isl_lp_empty) {
Packit fb9d21
			set->p[j] = isl_basic_set_set_to_empty(set->p[j]);
Packit fb9d21
			if (!set->p[j])
Packit fb9d21
				goto error;
Packit fb9d21
			continue;
Packit fb9d21
		}
Packit fb9d21
		if (first || isl_int_is_neg(opt)) {
Packit fb9d21
			if (!isl_int_is_one(opt_denom))
Packit fb9d21
				isl_seq_scale(c, c, opt_denom, len);
Packit fb9d21
			isl_int_sub(c[0], c[0], opt);
Packit fb9d21
		}
Packit fb9d21
		first = 0;
Packit fb9d21
	}
Packit fb9d21
	isl_int_clear(opt);
Packit fb9d21
	isl_int_clear(opt_denom);
Packit fb9d21
	return j >= set->n;
Packit fb9d21
error:
Packit fb9d21
	isl_int_clear(opt);
Packit fb9d21
	isl_int_clear(opt_denom);
Packit fb9d21
	return -1;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
__isl_give isl_basic_map *isl_basic_map_set_rational(
Packit fb9d21
	__isl_take isl_basic_set *bmap)
Packit fb9d21
{
Packit fb9d21
	if (!bmap)
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
Packit fb9d21
		return bmap;
Packit fb9d21
Packit fb9d21
	bmap = isl_basic_map_cow(bmap);
Packit fb9d21
	if (!bmap)
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	ISL_F_SET(bmap, ISL_BASIC_MAP_RATIONAL);
Packit fb9d21
Packit fb9d21
	return isl_basic_map_finalize(bmap);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
__isl_give isl_basic_set *isl_basic_set_set_rational(
Packit fb9d21
	__isl_take isl_basic_set *bset)
Packit fb9d21
{
Packit fb9d21
	return isl_basic_map_set_rational(bset);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
__isl_give isl_map *isl_map_set_rational(__isl_take isl_map *map)
Packit fb9d21
{
Packit fb9d21
	int i;
Packit fb9d21
Packit fb9d21
	map = isl_map_cow(map);
Packit fb9d21
	if (!map)
Packit fb9d21
		return NULL;
Packit fb9d21
	for (i = 0; i < map->n; ++i) {
Packit fb9d21
		map->p[i] = isl_basic_map_set_rational(map->p[i]);
Packit fb9d21
		if (!map->p[i])
Packit fb9d21
			goto error;
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
__isl_give isl_set *isl_set_set_rational(__isl_take isl_set *set)
Packit fb9d21
{
Packit fb9d21
	return isl_map_set_rational(set);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
static struct isl_basic_set *isl_basic_set_add_equality(
Packit fb9d21
	struct isl_basic_set *bset, isl_int *c)
Packit fb9d21
{
Packit fb9d21
	int i;
Packit fb9d21
	unsigned dim;
Packit fb9d21
Packit fb9d21
	if (!bset)
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	if (ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY))
Packit fb9d21
		return bset;
Packit fb9d21
Packit fb9d21
	isl_assert(bset->ctx, isl_basic_set_n_param(bset) == 0, goto error);
Packit fb9d21
	isl_assert(bset->ctx, bset->n_div == 0, goto error);
Packit fb9d21
	dim = isl_basic_set_n_dim(bset);
Packit fb9d21
	bset = isl_basic_set_cow(bset);
Packit fb9d21
	bset = isl_basic_set_extend(bset, 0, dim, 0, 1, 0);
Packit fb9d21
	i = isl_basic_set_alloc_equality(bset);
Packit fb9d21
	if (i < 0)
Packit fb9d21
		goto error;
Packit fb9d21
	isl_seq_cpy(bset->eq[i], c, 1 + dim);
Packit fb9d21
	return bset;
Packit fb9d21
error:
Packit fb9d21
	isl_basic_set_free(bset);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
static struct isl_set *isl_set_add_basic_set_equality(struct isl_set *set, isl_int *c)
Packit fb9d21
{
Packit fb9d21
	int i;
Packit fb9d21
Packit fb9d21
	set = isl_set_cow(set);
Packit fb9d21
	if (!set)
Packit fb9d21
		return NULL;
Packit fb9d21
	for (i = 0; i < set->n; ++i) {
Packit fb9d21
		set->p[i] = isl_basic_set_add_equality(set->p[i], c);
Packit fb9d21
		if (!set->p[i])
Packit fb9d21
			goto error;
Packit fb9d21
	}
Packit fb9d21
	return set;
Packit fb9d21
error:
Packit fb9d21
	isl_set_free(set);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Given a union of basic sets, construct the constraints for wrapping
Packit fb9d21
 * a facet around one of its ridges.
Packit fb9d21
 * In particular, if each of n the d-dimensional basic sets i in "set"
Packit fb9d21
 * contains the origin, satisfies the constraints x_1 >= 0 and x_2 >= 0
Packit fb9d21
 * and is defined by the constraints
Packit fb9d21
 *				    [ 1 ]
Packit fb9d21
 *				A_i [ x ]  >= 0
Packit fb9d21
 *
Packit fb9d21
 * then the resulting set is of dimension n*(1+d) and has as constraints
Packit fb9d21
 *
Packit fb9d21
 *				    [ a_i ]
Packit fb9d21
 *				A_i [ x_i ] >= 0
Packit fb9d21
 *
Packit fb9d21
 *				      a_i   >= 0
Packit fb9d21
 *
Packit fb9d21
 *			\sum_i x_{i,1} = 1
Packit fb9d21
 */
Packit fb9d21
static struct isl_basic_set *wrap_constraints(struct isl_set *set)
Packit fb9d21
{
Packit fb9d21
	struct isl_basic_set *lp;
Packit fb9d21
	unsigned n_eq;
Packit fb9d21
	unsigned n_ineq;
Packit fb9d21
	int i, j, k;
Packit fb9d21
	unsigned dim, lp_dim;
Packit fb9d21
Packit fb9d21
	if (!set)
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	dim = 1 + isl_set_n_dim(set);
Packit fb9d21
	n_eq = 1;
Packit fb9d21
	n_ineq = set->n;
Packit fb9d21
	for (i = 0; i < set->n; ++i) {
Packit fb9d21
		n_eq += set->p[i]->n_eq;
Packit fb9d21
		n_ineq += set->p[i]->n_ineq;
Packit fb9d21
	}
Packit fb9d21
	lp = isl_basic_set_alloc(set->ctx, 0, dim * set->n, 0, n_eq, n_ineq);
Packit fb9d21
	lp = isl_basic_set_set_rational(lp);
Packit fb9d21
	if (!lp)
Packit fb9d21
		return NULL;
Packit fb9d21
	lp_dim = isl_basic_set_n_dim(lp);
Packit fb9d21
	k = isl_basic_set_alloc_equality(lp);
Packit fb9d21
	isl_int_set_si(lp->eq[k][0], -1);
Packit fb9d21
	for (i = 0; i < set->n; ++i) {
Packit fb9d21
		isl_int_set_si(lp->eq[k][1+dim*i], 0);
Packit fb9d21
		isl_int_set_si(lp->eq[k][1+dim*i+1], 1);
Packit fb9d21
		isl_seq_clr(lp->eq[k]+1+dim*i+2, dim-2);
Packit fb9d21
	}
Packit fb9d21
	for (i = 0; i < set->n; ++i) {
Packit fb9d21
		k = isl_basic_set_alloc_inequality(lp);
Packit fb9d21
		isl_seq_clr(lp->ineq[k], 1+lp_dim);
Packit fb9d21
		isl_int_set_si(lp->ineq[k][1+dim*i], 1);
Packit fb9d21
Packit fb9d21
		for (j = 0; j < set->p[i]->n_eq; ++j) {
Packit fb9d21
			k = isl_basic_set_alloc_equality(lp);
Packit fb9d21
			isl_seq_clr(lp->eq[k], 1+dim*i);
Packit fb9d21
			isl_seq_cpy(lp->eq[k]+1+dim*i, set->p[i]->eq[j], dim);
Packit fb9d21
			isl_seq_clr(lp->eq[k]+1+dim*(i+1), dim*(set->n-i-1));
Packit fb9d21
		}
Packit fb9d21
Packit fb9d21
		for (j = 0; j < set->p[i]->n_ineq; ++j) {
Packit fb9d21
			k = isl_basic_set_alloc_inequality(lp);
Packit fb9d21
			isl_seq_clr(lp->ineq[k], 1+dim*i);
Packit fb9d21
			isl_seq_cpy(lp->ineq[k]+1+dim*i, set->p[i]->ineq[j], dim);
Packit fb9d21
			isl_seq_clr(lp->ineq[k]+1+dim*(i+1), dim*(set->n-i-1));
Packit fb9d21
		}
Packit fb9d21
	}
Packit fb9d21
	return lp;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Given a facet "facet" of the convex hull of "set" and a facet "ridge"
Packit fb9d21
 * of that facet, compute the other facet of the convex hull that contains
Packit fb9d21
 * the ridge.
Packit fb9d21
 *
Packit fb9d21
 * We first transform the set such that the facet constraint becomes
Packit fb9d21
 *
Packit fb9d21
 *			x_1 >= 0
Packit fb9d21
 *
Packit fb9d21
 * I.e., the facet lies in
Packit fb9d21
 *
Packit fb9d21
 *			x_1 = 0
Packit fb9d21
 *
Packit fb9d21
 * and on that facet, the constraint that defines the ridge is
Packit fb9d21
 *
Packit fb9d21
 *			x_2 >= 0
Packit fb9d21
 *
Packit fb9d21
 * (This transformation is not strictly needed, all that is needed is
Packit fb9d21
 * that the ridge contains the origin.)
Packit fb9d21
 *
Packit fb9d21
 * Since the ridge contains the origin, the cone of the convex hull
Packit fb9d21
 * will be of the form
Packit fb9d21
 *
Packit fb9d21
 *			x_1 >= 0
Packit fb9d21
 *			x_2 >= a x_1
Packit fb9d21
 *
Packit fb9d21
 * with this second constraint defining the new facet.
Packit fb9d21
 * The constant a is obtained by settting x_1 in the cone of the
Packit fb9d21
 * convex hull to 1 and minimizing x_2.
Packit fb9d21
 * Now, each element in the cone of the convex hull is the sum
Packit fb9d21
 * of elements in the cones of the basic sets.
Packit fb9d21
 * If a_i is the dilation factor of basic set i, then the problem
Packit fb9d21
 * we need to solve is
Packit fb9d21
 *
Packit fb9d21
 *			min \sum_i x_{i,2}
Packit fb9d21
 *			st
Packit fb9d21
 *				\sum_i x_{i,1} = 1
Packit fb9d21
 *				    a_i   >= 0
Packit fb9d21
 *				  [ a_i ]
Packit fb9d21
 *				A [ x_i ] >= 0
Packit fb9d21
 *
Packit fb9d21
 * with
Packit fb9d21
 *				    [  1  ]
Packit fb9d21
 *				A_i [ x_i ] >= 0
Packit fb9d21
 *
Packit fb9d21
 * the constraints of each (transformed) basic set.
Packit fb9d21
 * If a = n/d, then the constraint defining the new facet (in the transformed
Packit fb9d21
 * space) is
Packit fb9d21
 *
Packit fb9d21
 *			-n x_1 + d x_2 >= 0
Packit fb9d21
 *
Packit fb9d21
 * In the original space, we need to take the same combination of the
Packit fb9d21
 * corresponding constraints "facet" and "ridge".
Packit fb9d21
 *
Packit fb9d21
 * If a = -infty = "-1/0", then we just return the original facet constraint.
Packit fb9d21
 * This means that the facet is unbounded, but has a bounded intersection
Packit fb9d21
 * with the union of sets.
Packit fb9d21
 */
Packit fb9d21
isl_int *isl_set_wrap_facet(__isl_keep isl_set *set,
Packit fb9d21
	isl_int *facet, isl_int *ridge)
Packit fb9d21
{
Packit fb9d21
	int i;
Packit fb9d21
	isl_ctx *ctx;
Packit fb9d21
	struct isl_mat *T = NULL;
Packit fb9d21
	struct isl_basic_set *lp = NULL;
Packit fb9d21
	struct isl_vec *obj;
Packit fb9d21
	enum isl_lp_result res;
Packit fb9d21
	isl_int num, den;
Packit fb9d21
	unsigned dim;
Packit fb9d21
Packit fb9d21
	if (!set)
Packit fb9d21
		return NULL;
Packit fb9d21
	ctx = set->ctx;
Packit fb9d21
	set = isl_set_copy(set);
Packit fb9d21
	set = isl_set_set_rational(set);
Packit fb9d21
Packit fb9d21
	dim = 1 + isl_set_n_dim(set);
Packit fb9d21
	T = isl_mat_alloc(ctx, 3, dim);
Packit fb9d21
	if (!T)
Packit fb9d21
		goto error;
Packit fb9d21
	isl_int_set_si(T->row[0][0], 1);
Packit fb9d21
	isl_seq_clr(T->row[0]+1, dim - 1);
Packit fb9d21
	isl_seq_cpy(T->row[1], facet, dim);
Packit fb9d21
	isl_seq_cpy(T->row[2], ridge, dim);
Packit fb9d21
	T = isl_mat_right_inverse(T);
Packit fb9d21
	set = isl_set_preimage(set, T);
Packit fb9d21
	T = NULL;
Packit fb9d21
	if (!set)
Packit fb9d21
		goto error;
Packit fb9d21
	lp = wrap_constraints(set);
Packit fb9d21
	obj = isl_vec_alloc(ctx, 1 + dim*set->n);
Packit fb9d21
	if (!obj)
Packit fb9d21
		goto error;
Packit fb9d21
	isl_int_set_si(obj->block.data[0], 0);
Packit fb9d21
	for (i = 0; i < set->n; ++i) {
Packit fb9d21
		isl_seq_clr(obj->block.data + 1 + dim*i, 2);
Packit fb9d21
		isl_int_set_si(obj->block.data[1 + dim*i+2], 1);
Packit fb9d21
		isl_seq_clr(obj->block.data + 1 + dim*i+3, dim-3);
Packit fb9d21
	}
Packit fb9d21
	isl_int_init(num);
Packit fb9d21
	isl_int_init(den);
Packit fb9d21
	res = isl_basic_set_solve_lp(lp, 0,
Packit fb9d21
			    obj->block.data, ctx->one, &num, &den, NULL);
Packit fb9d21
	if (res == isl_lp_ok) {
Packit fb9d21
		isl_int_neg(num, num);
Packit fb9d21
		isl_seq_combine(facet, num, facet, den, ridge, dim);
Packit fb9d21
		isl_seq_normalize(ctx, facet, dim);
Packit fb9d21
	}
Packit fb9d21
	isl_int_clear(num);
Packit fb9d21
	isl_int_clear(den);
Packit fb9d21
	isl_vec_free(obj);
Packit fb9d21
	isl_basic_set_free(lp);
Packit fb9d21
	isl_set_free(set);
Packit fb9d21
	if (res == isl_lp_error)
Packit fb9d21
		return NULL;
Packit fb9d21
	isl_assert(ctx, res == isl_lp_ok || res == isl_lp_unbounded, 
Packit fb9d21
		   return NULL);
Packit fb9d21
	return facet;
Packit fb9d21
error:
Packit fb9d21
	isl_basic_set_free(lp);
Packit fb9d21
	isl_mat_free(T);
Packit fb9d21
	isl_set_free(set);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Compute the constraint of a facet of "set".
Packit fb9d21
 *
Packit fb9d21
 * We first compute the intersection with a bounding constraint
Packit fb9d21
 * that is orthogonal to one of the coordinate axes.
Packit fb9d21
 * If the affine hull of this intersection has only one equality,
Packit fb9d21
 * we have found a facet.
Packit fb9d21
 * Otherwise, we wrap the current bounding constraint around
Packit fb9d21
 * one of the equalities of the face (one that is not equal to
Packit fb9d21
 * the current bounding constraint).
Packit fb9d21
 * This process continues until we have found a facet.
Packit fb9d21
 * The dimension of the intersection increases by at least
Packit fb9d21
 * one on each iteration, so termination is guaranteed.
Packit fb9d21
 */
Packit fb9d21
static __isl_give isl_mat *initial_facet_constraint(__isl_keep isl_set *set)
Packit fb9d21
{
Packit fb9d21
	struct isl_set *slice = NULL;
Packit fb9d21
	struct isl_basic_set *face = NULL;
Packit fb9d21
	int i;
Packit fb9d21
	unsigned dim = isl_set_n_dim(set);
Packit fb9d21
	int is_bound;
Packit fb9d21
	isl_mat *bounds = NULL;
Packit fb9d21
Packit fb9d21
	isl_assert(set->ctx, set->n > 0, goto error);
Packit fb9d21
	bounds = isl_mat_alloc(set->ctx, 1, 1 + dim);
Packit fb9d21
	if (!bounds)
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	isl_seq_clr(bounds->row[0], dim);
Packit fb9d21
	isl_int_set_si(bounds->row[0][1 + dim - 1], 1);
Packit fb9d21
	is_bound = uset_is_bound(set, bounds->row[0], 1 + dim);
Packit fb9d21
	if (is_bound < 0)
Packit fb9d21
		goto error;
Packit fb9d21
	isl_assert(set->ctx, is_bound, goto error);
Packit fb9d21
	isl_seq_normalize(set->ctx, bounds->row[0], 1 + dim);
Packit fb9d21
	bounds->n_row = 1;
Packit fb9d21
Packit fb9d21
	for (;;) {
Packit fb9d21
		slice = isl_set_copy(set);
Packit fb9d21
		slice = isl_set_add_basic_set_equality(slice, bounds->row[0]);
Packit fb9d21
		face = isl_set_affine_hull(slice);
Packit fb9d21
		if (!face)
Packit fb9d21
			goto error;
Packit fb9d21
		if (face->n_eq == 1) {
Packit fb9d21
			isl_basic_set_free(face);
Packit fb9d21
			break;
Packit fb9d21
		}
Packit fb9d21
		for (i = 0; i < face->n_eq; ++i)
Packit fb9d21
			if (!isl_seq_eq(bounds->row[0], face->eq[i], 1 + dim) &&
Packit fb9d21
			    !isl_seq_is_neg(bounds->row[0],
Packit fb9d21
						face->eq[i], 1 + dim))
Packit fb9d21
				break;
Packit fb9d21
		isl_assert(set->ctx, i < face->n_eq, goto error);
Packit fb9d21
		if (!isl_set_wrap_facet(set, bounds->row[0], face->eq[i]))
Packit fb9d21
			goto error;
Packit fb9d21
		isl_seq_normalize(set->ctx, bounds->row[0], bounds->n_col);
Packit fb9d21
		isl_basic_set_free(face);
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	return bounds;
Packit fb9d21
error:
Packit fb9d21
	isl_basic_set_free(face);
Packit fb9d21
	isl_mat_free(bounds);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Given the bounding constraint "c" of a facet of the convex hull of "set",
Packit fb9d21
 * compute a hyperplane description of the facet, i.e., compute the facets
Packit fb9d21
 * of the facet.
Packit fb9d21
 *
Packit fb9d21
 * We compute an affine transformation that transforms the constraint
Packit fb9d21
 *
Packit fb9d21
 *			  [ 1 ]
Packit fb9d21
 *			c [ x ] = 0
Packit fb9d21
 *
Packit fb9d21
 * to the constraint
Packit fb9d21
 *
Packit fb9d21
 *			   z_1  = 0
Packit fb9d21
 *
Packit fb9d21
 * by computing the right inverse U of a matrix that starts with the rows
Packit fb9d21
 *
Packit fb9d21
 *			[ 1 0 ]
Packit fb9d21
 *			[  c  ]
Packit fb9d21
 *
Packit fb9d21
 * Then
Packit fb9d21
 *			[ 1 ]     [ 1 ]
Packit fb9d21
 *			[ x ] = U [ z ]
Packit fb9d21
 * and
Packit fb9d21
 *			[ 1 ]     [ 1 ]
Packit fb9d21
 *			[ z ] = Q [ x ]
Packit fb9d21
 *
Packit fb9d21
 * with Q = U^{-1}
Packit fb9d21
 * Since z_1 is zero, we can drop this variable as well as the corresponding
Packit fb9d21
 * column of U to obtain
Packit fb9d21
 *
Packit fb9d21
 *			[ 1 ]      [ 1  ]
Packit fb9d21
 *			[ x ] = U' [ z' ]
Packit fb9d21
 * and
Packit fb9d21
 *			[ 1  ]      [ 1 ]
Packit fb9d21
 *			[ z' ] = Q' [ x ]
Packit fb9d21
 *
Packit fb9d21
 * with Q' equal to Q, but without the corresponding row.
Packit fb9d21
 * After computing the facets of the facet in the z' space,
Packit fb9d21
 * we convert them back to the x space through Q.
Packit fb9d21
 */
Packit fb9d21
static struct isl_basic_set *compute_facet(struct isl_set *set, isl_int *c)
Packit fb9d21
{
Packit fb9d21
	struct isl_mat *m, *U, *Q;
Packit fb9d21
	struct isl_basic_set *facet = NULL;
Packit fb9d21
	struct isl_ctx *ctx;
Packit fb9d21
	unsigned dim;
Packit fb9d21
Packit fb9d21
	ctx = set->ctx;
Packit fb9d21
	set = isl_set_copy(set);
Packit fb9d21
	dim = isl_set_n_dim(set);
Packit fb9d21
	m = isl_mat_alloc(set->ctx, 2, 1 + dim);
Packit fb9d21
	if (!m)
Packit fb9d21
		goto error;
Packit fb9d21
	isl_int_set_si(m->row[0][0], 1);
Packit fb9d21
	isl_seq_clr(m->row[0]+1, dim);
Packit fb9d21
	isl_seq_cpy(m->row[1], c, 1+dim);
Packit fb9d21
	U = isl_mat_right_inverse(m);
Packit fb9d21
	Q = isl_mat_right_inverse(isl_mat_copy(U));
Packit fb9d21
	U = isl_mat_drop_cols(U, 1, 1);
Packit fb9d21
	Q = isl_mat_drop_rows(Q, 1, 1);
Packit fb9d21
	set = isl_set_preimage(set, U);
Packit fb9d21
	facet = uset_convex_hull_wrap_bounded(set);
Packit fb9d21
	facet = isl_basic_set_preimage(facet, Q);
Packit fb9d21
	if (facet)
Packit fb9d21
		isl_assert(ctx, facet->n_eq == 0, goto error);
Packit fb9d21
	return facet;
Packit fb9d21
error:
Packit fb9d21
	isl_basic_set_free(facet);
Packit fb9d21
	isl_set_free(set);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Given an initial facet constraint, compute the remaining facets.
Packit fb9d21
 * We do this by running through all facets found so far and computing
Packit fb9d21
 * the adjacent facets through wrapping, adding those facets that we
Packit fb9d21
 * hadn't already found before.
Packit fb9d21
 *
Packit fb9d21
 * For each facet we have found so far, we first compute its facets
Packit fb9d21
 * in the resulting convex hull.  That is, we compute the ridges
Packit fb9d21
 * of the resulting convex hull contained in the facet.
Packit fb9d21
 * We also compute the corresponding facet in the current approximation
Packit fb9d21
 * of the convex hull.  There is no need to wrap around the ridges
Packit fb9d21
 * in this facet since that would result in a facet that is already
Packit fb9d21
 * present in the current approximation.
Packit fb9d21
 *
Packit fb9d21
 * This function can still be significantly optimized by checking which of
Packit fb9d21
 * the facets of the basic sets are also facets of the convex hull and
Packit fb9d21
 * using all the facets so far to help in constructing the facets of the
Packit fb9d21
 * facets
Packit fb9d21
 * and/or
Packit fb9d21
 * using the technique in section "3.1 Ridge Generation" of
Packit fb9d21
 * "Extended Convex Hull" by Fukuda et al.
Packit fb9d21
 */
Packit fb9d21
static struct isl_basic_set *extend(struct isl_basic_set *hull,
Packit fb9d21
	struct isl_set *set)
Packit fb9d21
{
Packit fb9d21
	int i, j, f;
Packit fb9d21
	int k;
Packit fb9d21
	struct isl_basic_set *facet = NULL;
Packit fb9d21
	struct isl_basic_set *hull_facet = NULL;
Packit fb9d21
	unsigned dim;
Packit fb9d21
Packit fb9d21
	if (!hull)
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	isl_assert(set->ctx, set->n > 0, goto error);
Packit fb9d21
Packit fb9d21
	dim = isl_set_n_dim(set);
Packit fb9d21
Packit fb9d21
	for (i = 0; i < hull->n_ineq; ++i) {
Packit fb9d21
		facet = compute_facet(set, hull->ineq[i]);
Packit fb9d21
		facet = isl_basic_set_add_equality(facet, hull->ineq[i]);
Packit fb9d21
		facet = isl_basic_set_gauss(facet, NULL);
Packit fb9d21
		facet = isl_basic_set_normalize_constraints(facet);
Packit fb9d21
		hull_facet = isl_basic_set_copy(hull);
Packit fb9d21
		hull_facet = isl_basic_set_add_equality(hull_facet, hull->ineq[i]);
Packit fb9d21
		hull_facet = isl_basic_set_gauss(hull_facet, NULL);
Packit fb9d21
		hull_facet = isl_basic_set_normalize_constraints(hull_facet);
Packit fb9d21
		if (!facet || !hull_facet)
Packit fb9d21
			goto error;
Packit fb9d21
		hull = isl_basic_set_cow(hull);
Packit fb9d21
		hull = isl_basic_set_extend_space(hull,
Packit fb9d21
			isl_space_copy(hull->dim), 0, 0, facet->n_ineq);
Packit fb9d21
		if (!hull)
Packit fb9d21
			goto error;
Packit fb9d21
		for (j = 0; j < facet->n_ineq; ++j) {
Packit fb9d21
			for (f = 0; f < hull_facet->n_ineq; ++f)
Packit fb9d21
				if (isl_seq_eq(facet->ineq[j],
Packit fb9d21
						hull_facet->ineq[f], 1 + dim))
Packit fb9d21
					break;
Packit fb9d21
			if (f < hull_facet->n_ineq)
Packit fb9d21
				continue;
Packit fb9d21
			k = isl_basic_set_alloc_inequality(hull);
Packit fb9d21
			if (k < 0)
Packit fb9d21
				goto error;
Packit fb9d21
			isl_seq_cpy(hull->ineq[k], hull->ineq[i], 1+dim);
Packit fb9d21
			if (!isl_set_wrap_facet(set, hull->ineq[k], facet->ineq[j]))
Packit fb9d21
				goto error;
Packit fb9d21
		}
Packit fb9d21
		isl_basic_set_free(hull_facet);
Packit fb9d21
		isl_basic_set_free(facet);
Packit fb9d21
	}
Packit fb9d21
	hull = isl_basic_set_simplify(hull);
Packit fb9d21
	hull = isl_basic_set_finalize(hull);
Packit fb9d21
	return hull;
Packit fb9d21
error:
Packit fb9d21
	isl_basic_set_free(hull_facet);
Packit fb9d21
	isl_basic_set_free(facet);
Packit fb9d21
	isl_basic_set_free(hull);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Special case for computing the convex hull of a one dimensional set.
Packit fb9d21
 * We simply collect the lower and upper bounds of each basic set
Packit fb9d21
 * and the biggest of those.
Packit fb9d21
 */
Packit fb9d21
static struct isl_basic_set *convex_hull_1d(struct isl_set *set)
Packit fb9d21
{
Packit fb9d21
	struct isl_mat *c = NULL;
Packit fb9d21
	isl_int *lower = NULL;
Packit fb9d21
	isl_int *upper = NULL;
Packit fb9d21
	int i, j, k;
Packit fb9d21
	isl_int a, b;
Packit fb9d21
	struct isl_basic_set *hull;
Packit fb9d21
Packit fb9d21
	for (i = 0; i < set->n; ++i) {
Packit fb9d21
		set->p[i] = isl_basic_set_simplify(set->p[i]);
Packit fb9d21
		if (!set->p[i])
Packit fb9d21
			goto error;
Packit fb9d21
	}
Packit fb9d21
	set = isl_set_remove_empty_parts(set);
Packit fb9d21
	if (!set)
Packit fb9d21
		goto error;
Packit fb9d21
	isl_assert(set->ctx, set->n > 0, goto error);
Packit fb9d21
	c = isl_mat_alloc(set->ctx, 2, 2);
Packit fb9d21
	if (!c)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	if (set->p[0]->n_eq > 0) {
Packit fb9d21
		isl_assert(set->ctx, set->p[0]->n_eq == 1, goto error);
Packit fb9d21
		lower = c->row[0];
Packit fb9d21
		upper = c->row[1];
Packit fb9d21
		if (isl_int_is_pos(set->p[0]->eq[0][1])) {
Packit fb9d21
			isl_seq_cpy(lower, set->p[0]->eq[0], 2);
Packit fb9d21
			isl_seq_neg(upper, set->p[0]->eq[0], 2);
Packit fb9d21
		} else {
Packit fb9d21
			isl_seq_neg(lower, set->p[0]->eq[0], 2);
Packit fb9d21
			isl_seq_cpy(upper, set->p[0]->eq[0], 2);
Packit fb9d21
		}
Packit fb9d21
	} else {
Packit fb9d21
		for (j = 0; j < set->p[0]->n_ineq; ++j) {
Packit fb9d21
			if (isl_int_is_pos(set->p[0]->ineq[j][1])) {
Packit fb9d21
				lower = c->row[0];
Packit fb9d21
				isl_seq_cpy(lower, set->p[0]->ineq[j], 2);
Packit fb9d21
			} else {
Packit fb9d21
				upper = c->row[1];
Packit fb9d21
				isl_seq_cpy(upper, set->p[0]->ineq[j], 2);
Packit fb9d21
			}
Packit fb9d21
		}
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	isl_int_init(a);
Packit fb9d21
	isl_int_init(b);
Packit fb9d21
	for (i = 0; i < set->n; ++i) {
Packit fb9d21
		struct isl_basic_set *bset = set->p[i];
Packit fb9d21
		int has_lower = 0;
Packit fb9d21
		int has_upper = 0;
Packit fb9d21
Packit fb9d21
		for (j = 0; j < bset->n_eq; ++j) {
Packit fb9d21
			has_lower = 1;
Packit fb9d21
			has_upper = 1;
Packit fb9d21
			if (lower) {
Packit fb9d21
				isl_int_mul(a, lower[0], bset->eq[j][1]);
Packit fb9d21
				isl_int_mul(b, lower[1], bset->eq[j][0]);
Packit fb9d21
				if (isl_int_lt(a, b) && isl_int_is_pos(bset->eq[j][1]))
Packit fb9d21
					isl_seq_cpy(lower, bset->eq[j], 2);
Packit fb9d21
				if (isl_int_gt(a, b) && isl_int_is_neg(bset->eq[j][1]))
Packit fb9d21
					isl_seq_neg(lower, bset->eq[j], 2);
Packit fb9d21
			}
Packit fb9d21
			if (upper) {
Packit fb9d21
				isl_int_mul(a, upper[0], bset->eq[j][1]);
Packit fb9d21
				isl_int_mul(b, upper[1], bset->eq[j][0]);
Packit fb9d21
				if (isl_int_lt(a, b) && isl_int_is_pos(bset->eq[j][1]))
Packit fb9d21
					isl_seq_neg(upper, bset->eq[j], 2);
Packit fb9d21
				if (isl_int_gt(a, b) && isl_int_is_neg(bset->eq[j][1]))
Packit fb9d21
					isl_seq_cpy(upper, bset->eq[j], 2);
Packit fb9d21
			}
Packit fb9d21
		}
Packit fb9d21
		for (j = 0; j < bset->n_ineq; ++j) {
Packit fb9d21
			if (isl_int_is_pos(bset->ineq[j][1]))
Packit fb9d21
				has_lower = 1;
Packit fb9d21
			if (isl_int_is_neg(bset->ineq[j][1]))
Packit fb9d21
				has_upper = 1;
Packit fb9d21
			if (lower && isl_int_is_pos(bset->ineq[j][1])) {
Packit fb9d21
				isl_int_mul(a, lower[0], bset->ineq[j][1]);
Packit fb9d21
				isl_int_mul(b, lower[1], bset->ineq[j][0]);
Packit fb9d21
				if (isl_int_lt(a, b))
Packit fb9d21
					isl_seq_cpy(lower, bset->ineq[j], 2);
Packit fb9d21
			}
Packit fb9d21
			if (upper && isl_int_is_neg(bset->ineq[j][1])) {
Packit fb9d21
				isl_int_mul(a, upper[0], bset->ineq[j][1]);
Packit fb9d21
				isl_int_mul(b, upper[1], bset->ineq[j][0]);
Packit fb9d21
				if (isl_int_gt(a, b))
Packit fb9d21
					isl_seq_cpy(upper, bset->ineq[j], 2);
Packit fb9d21
			}
Packit fb9d21
		}
Packit fb9d21
		if (!has_lower)
Packit fb9d21
			lower = NULL;
Packit fb9d21
		if (!has_upper)
Packit fb9d21
			upper = NULL;
Packit fb9d21
	}
Packit fb9d21
	isl_int_clear(a);
Packit fb9d21
	isl_int_clear(b);
Packit fb9d21
Packit fb9d21
	hull = isl_basic_set_alloc(set->ctx, 0, 1, 0, 0, 2);
Packit fb9d21
	hull = isl_basic_set_set_rational(hull);
Packit fb9d21
	if (!hull)
Packit fb9d21
		goto error;
Packit fb9d21
	if (lower) {
Packit fb9d21
		k = isl_basic_set_alloc_inequality(hull);
Packit fb9d21
		isl_seq_cpy(hull->ineq[k], lower, 2);
Packit fb9d21
	}
Packit fb9d21
	if (upper) {
Packit fb9d21
		k = isl_basic_set_alloc_inequality(hull);
Packit fb9d21
		isl_seq_cpy(hull->ineq[k], upper, 2);
Packit fb9d21
	}
Packit fb9d21
	hull = isl_basic_set_finalize(hull);
Packit fb9d21
	isl_set_free(set);
Packit fb9d21
	isl_mat_free(c);
Packit fb9d21
	return hull;
Packit fb9d21
error:
Packit fb9d21
	isl_set_free(set);
Packit fb9d21
	isl_mat_free(c);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
static struct isl_basic_set *convex_hull_0d(struct isl_set *set)
Packit fb9d21
{
Packit fb9d21
	struct isl_basic_set *convex_hull;
Packit fb9d21
Packit fb9d21
	if (!set)
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	if (isl_set_is_empty(set))
Packit fb9d21
		convex_hull = isl_basic_set_empty(isl_space_copy(set->dim));
Packit fb9d21
	else
Packit fb9d21
		convex_hull = isl_basic_set_universe(isl_space_copy(set->dim));
Packit fb9d21
	isl_set_free(set);
Packit fb9d21
	return convex_hull;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Compute the convex hull of a pair of basic sets without any parameters or
Packit fb9d21
 * integer divisions using Fourier-Motzkin elimination.
Packit fb9d21
 * The convex hull is the set of all points that can be written as
Packit fb9d21
 * the sum of points from both basic sets (in homogeneous coordinates).
Packit fb9d21
 * We set up the constraints in a space with dimensions for each of
Packit fb9d21
 * the three sets and then project out the dimensions corresponding
Packit fb9d21
 * to the two original basic sets, retaining only those corresponding
Packit fb9d21
 * to the convex hull.
Packit fb9d21
 */
Packit fb9d21
static struct isl_basic_set *convex_hull_pair_elim(struct isl_basic_set *bset1,
Packit fb9d21
	struct isl_basic_set *bset2)
Packit fb9d21
{
Packit fb9d21
	int i, j, k;
Packit fb9d21
	struct isl_basic_set *bset[2];
Packit fb9d21
	struct isl_basic_set *hull = NULL;
Packit fb9d21
	unsigned dim;
Packit fb9d21
Packit fb9d21
	if (!bset1 || !bset2)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	dim = isl_basic_set_n_dim(bset1);
Packit fb9d21
	hull = isl_basic_set_alloc(bset1->ctx, 0, 2 + 3 * dim, 0,
Packit fb9d21
				1 + dim + bset1->n_eq + bset2->n_eq,
Packit fb9d21
				2 + bset1->n_ineq + bset2->n_ineq);
Packit fb9d21
	bset[0] = bset1;
Packit fb9d21
	bset[1] = bset2;
Packit fb9d21
	for (i = 0; i < 2; ++i) {
Packit fb9d21
		for (j = 0; j < bset[i]->n_eq; ++j) {
Packit fb9d21
			k = isl_basic_set_alloc_equality(hull);
Packit fb9d21
			if (k < 0)
Packit fb9d21
				goto error;
Packit fb9d21
			isl_seq_clr(hull->eq[k], (i+1) * (1+dim));
Packit fb9d21
			isl_seq_clr(hull->eq[k]+(i+2)*(1+dim), (1-i)*(1+dim));
Packit fb9d21
			isl_seq_cpy(hull->eq[k]+(i+1)*(1+dim), bset[i]->eq[j],
Packit fb9d21
					1+dim);
Packit fb9d21
		}
Packit fb9d21
		for (j = 0; j < bset[i]->n_ineq; ++j) {
Packit fb9d21
			k = isl_basic_set_alloc_inequality(hull);
Packit fb9d21
			if (k < 0)
Packit fb9d21
				goto error;
Packit fb9d21
			isl_seq_clr(hull->ineq[k], (i+1) * (1+dim));
Packit fb9d21
			isl_seq_clr(hull->ineq[k]+(i+2)*(1+dim), (1-i)*(1+dim));
Packit fb9d21
			isl_seq_cpy(hull->ineq[k]+(i+1)*(1+dim),
Packit fb9d21
					bset[i]->ineq[j], 1+dim);
Packit fb9d21
		}
Packit fb9d21
		k = isl_basic_set_alloc_inequality(hull);
Packit fb9d21
		if (k < 0)
Packit fb9d21
			goto error;
Packit fb9d21
		isl_seq_clr(hull->ineq[k], 1+2+3*dim);
Packit fb9d21
		isl_int_set_si(hull->ineq[k][(i+1)*(1+dim)], 1);
Packit fb9d21
	}
Packit fb9d21
	for (j = 0; j < 1+dim; ++j) {
Packit fb9d21
		k = isl_basic_set_alloc_equality(hull);
Packit fb9d21
		if (k < 0)
Packit fb9d21
			goto error;
Packit fb9d21
		isl_seq_clr(hull->eq[k], 1+2+3*dim);
Packit fb9d21
		isl_int_set_si(hull->eq[k][j], -1);
Packit fb9d21
		isl_int_set_si(hull->eq[k][1+dim+j], 1);
Packit fb9d21
		isl_int_set_si(hull->eq[k][2*(1+dim)+j], 1);
Packit fb9d21
	}
Packit fb9d21
	hull = isl_basic_set_set_rational(hull);
Packit fb9d21
	hull = isl_basic_set_remove_dims(hull, isl_dim_set, dim, 2*(1+dim));
Packit fb9d21
	hull = isl_basic_set_remove_redundancies(hull);
Packit fb9d21
	isl_basic_set_free(bset1);
Packit fb9d21
	isl_basic_set_free(bset2);
Packit fb9d21
	return hull;
Packit fb9d21
error:
Packit fb9d21
	isl_basic_set_free(bset1);
Packit fb9d21
	isl_basic_set_free(bset2);
Packit fb9d21
	isl_basic_set_free(hull);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Is the set bounded for each value of the parameters?
Packit fb9d21
 */
Packit fb9d21
int isl_basic_set_is_bounded(__isl_keep isl_basic_set *bset)
Packit fb9d21
{
Packit fb9d21
	struct isl_tab *tab;
Packit fb9d21
	int bounded;
Packit fb9d21
Packit fb9d21
	if (!bset)
Packit fb9d21
		return -1;
Packit fb9d21
	if (isl_basic_set_plain_is_empty(bset))
Packit fb9d21
		return 1;
Packit fb9d21
Packit fb9d21
	tab = isl_tab_from_recession_cone(bset, 1);
Packit fb9d21
	bounded = isl_tab_cone_is_bounded(tab);
Packit fb9d21
	isl_tab_free(tab);
Packit fb9d21
	return bounded;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Is the image bounded for each value of the parameters and
Packit fb9d21
 * the domain variables?
Packit fb9d21
 */
Packit fb9d21
int isl_basic_map_image_is_bounded(__isl_keep isl_basic_map *bmap)
Packit fb9d21
{
Packit fb9d21
	unsigned nparam = isl_basic_map_dim(bmap, isl_dim_param);
Packit fb9d21
	unsigned n_in = isl_basic_map_dim(bmap, isl_dim_in);
Packit fb9d21
	int bounded;
Packit fb9d21
Packit fb9d21
	bmap = isl_basic_map_copy(bmap);
Packit fb9d21
	bmap = isl_basic_map_cow(bmap);
Packit fb9d21
	bmap = isl_basic_map_move_dims(bmap, isl_dim_param, nparam,
Packit fb9d21
					isl_dim_in, 0, n_in);
Packit fb9d21
	bounded = isl_basic_set_is_bounded((isl_basic_set *)bmap);
Packit fb9d21
	isl_basic_map_free(bmap);
Packit fb9d21
Packit fb9d21
	return bounded;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Is the set bounded for each value of the parameters?
Packit fb9d21
 */
Packit fb9d21
int isl_set_is_bounded(__isl_keep isl_set *set)
Packit fb9d21
{
Packit fb9d21
	int i;
Packit fb9d21
Packit fb9d21
	if (!set)
Packit fb9d21
		return -1;
Packit fb9d21
Packit fb9d21
	for (i = 0; i < set->n; ++i) {
Packit fb9d21
		int bounded = isl_basic_set_is_bounded(set->p[i]);
Packit fb9d21
		if (!bounded || bounded < 0)
Packit fb9d21
			return bounded;
Packit fb9d21
	}
Packit fb9d21
	return 1;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Compute the lineality space of the convex hull of bset1 and bset2.
Packit fb9d21
 *
Packit fb9d21
 * We first compute the intersection of the recession cone of bset1
Packit fb9d21
 * with the negative of the recession cone of bset2 and then compute
Packit fb9d21
 * the linear hull of the resulting cone.
Packit fb9d21
 */
Packit fb9d21
static struct isl_basic_set *induced_lineality_space(
Packit fb9d21
	struct isl_basic_set *bset1, struct isl_basic_set *bset2)
Packit fb9d21
{
Packit fb9d21
	int i, k;
Packit fb9d21
	struct isl_basic_set *lin = NULL;
Packit fb9d21
	unsigned dim;
Packit fb9d21
Packit fb9d21
	if (!bset1 || !bset2)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	dim = isl_basic_set_total_dim(bset1);
Packit fb9d21
	lin = isl_basic_set_alloc_space(isl_basic_set_get_space(bset1), 0,
Packit fb9d21
					bset1->n_eq + bset2->n_eq,
Packit fb9d21
					bset1->n_ineq + bset2->n_ineq);
Packit fb9d21
	lin = isl_basic_set_set_rational(lin);
Packit fb9d21
	if (!lin)
Packit fb9d21
		goto error;
Packit fb9d21
	for (i = 0; i < bset1->n_eq; ++i) {
Packit fb9d21
		k = isl_basic_set_alloc_equality(lin);
Packit fb9d21
		if (k < 0)
Packit fb9d21
			goto error;
Packit fb9d21
		isl_int_set_si(lin->eq[k][0], 0);
Packit fb9d21
		isl_seq_cpy(lin->eq[k] + 1, bset1->eq[i] + 1, dim);
Packit fb9d21
	}
Packit fb9d21
	for (i = 0; i < bset1->n_ineq; ++i) {
Packit fb9d21
		k = isl_basic_set_alloc_inequality(lin);
Packit fb9d21
		if (k < 0)
Packit fb9d21
			goto error;
Packit fb9d21
		isl_int_set_si(lin->ineq[k][0], 0);
Packit fb9d21
		isl_seq_cpy(lin->ineq[k] + 1, bset1->ineq[i] + 1, dim);
Packit fb9d21
	}
Packit fb9d21
	for (i = 0; i < bset2->n_eq; ++i) {
Packit fb9d21
		k = isl_basic_set_alloc_equality(lin);
Packit fb9d21
		if (k < 0)
Packit fb9d21
			goto error;
Packit fb9d21
		isl_int_set_si(lin->eq[k][0], 0);
Packit fb9d21
		isl_seq_neg(lin->eq[k] + 1, bset2->eq[i] + 1, dim);
Packit fb9d21
	}
Packit fb9d21
	for (i = 0; i < bset2->n_ineq; ++i) {
Packit fb9d21
		k = isl_basic_set_alloc_inequality(lin);
Packit fb9d21
		if (k < 0)
Packit fb9d21
			goto error;
Packit fb9d21
		isl_int_set_si(lin->ineq[k][0], 0);
Packit fb9d21
		isl_seq_neg(lin->ineq[k] + 1, bset2->ineq[i] + 1, dim);
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	isl_basic_set_free(bset1);
Packit fb9d21
	isl_basic_set_free(bset2);
Packit fb9d21
	return isl_basic_set_affine_hull(lin);
Packit fb9d21
error:
Packit fb9d21
	isl_basic_set_free(lin);
Packit fb9d21
	isl_basic_set_free(bset1);
Packit fb9d21
	isl_basic_set_free(bset2);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
static struct isl_basic_set *uset_convex_hull(struct isl_set *set);
Packit fb9d21
Packit fb9d21
/* Given a set and a linear space "lin" of dimension n > 0,
Packit fb9d21
 * project the linear space from the set, compute the convex hull
Packit fb9d21
 * and then map the set back to the original space.
Packit fb9d21
 *
Packit fb9d21
 * Let
Packit fb9d21
 *
Packit fb9d21
 *	M x = 0
Packit fb9d21
 *
Packit fb9d21
 * describe the linear space.  We first compute the Hermite normal
Packit fb9d21
 * form H = M U of M = H Q, to obtain
Packit fb9d21
 *
Packit fb9d21
 *	H Q x = 0
Packit fb9d21
 *
Packit fb9d21
 * The last n rows of H will be zero, so the last n variables of x' = Q x
Packit fb9d21
 * are the one we want to project out.  We do this by transforming each
Packit fb9d21
 * basic set A x >= b to A U x' >= b and then removing the last n dimensions.
Packit fb9d21
 * After computing the convex hull in x'_1, i.e., A' x'_1 >= b',
Packit fb9d21
 * we transform the hull back to the original space as A' Q_1 x >= b',
Packit fb9d21
 * with Q_1 all but the last n rows of Q.
Packit fb9d21
 */
Packit fb9d21
static struct isl_basic_set *modulo_lineality(struct isl_set *set,
Packit fb9d21
	struct isl_basic_set *lin)
Packit fb9d21
{
Packit fb9d21
	unsigned total = isl_basic_set_total_dim(lin);
Packit fb9d21
	unsigned lin_dim;
Packit fb9d21
	struct isl_basic_set *hull;
Packit fb9d21
	struct isl_mat *M, *U, *Q;
Packit fb9d21
Packit fb9d21
	if (!set || !lin)
Packit fb9d21
		goto error;
Packit fb9d21
	lin_dim = total - lin->n_eq;
Packit fb9d21
	M = isl_mat_sub_alloc6(set->ctx, lin->eq, 0, lin->n_eq, 1, total);
Packit fb9d21
	M = isl_mat_left_hermite(M, 0, &U, &Q);
Packit fb9d21
	if (!M)
Packit fb9d21
		goto error;
Packit fb9d21
	isl_mat_free(M);
Packit fb9d21
	isl_basic_set_free(lin);
Packit fb9d21
Packit fb9d21
	Q = isl_mat_drop_rows(Q, Q->n_row - lin_dim, lin_dim);
Packit fb9d21
Packit fb9d21
	U = isl_mat_lin_to_aff(U);
Packit fb9d21
	Q = isl_mat_lin_to_aff(Q);
Packit fb9d21
Packit fb9d21
	set = isl_set_preimage(set, U);
Packit fb9d21
	set = isl_set_remove_dims(set, isl_dim_set, total - lin_dim, lin_dim);
Packit fb9d21
	hull = uset_convex_hull(set);
Packit fb9d21
	hull = isl_basic_set_preimage(hull, Q);
Packit fb9d21
Packit fb9d21
	return hull;
Packit fb9d21
error:
Packit fb9d21
	isl_basic_set_free(lin);
Packit fb9d21
	isl_set_free(set);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Given two polyhedra with as constraints h_{ij} x >= 0 in homegeneous space,
Packit fb9d21
 * set up an LP for solving
Packit fb9d21
 *
Packit fb9d21
 *	\sum_j \alpha_{1j} h_{1j} = \sum_j \alpha_{2j} h_{2j}
Packit fb9d21
 *
Packit fb9d21
 * \alpha{i0} corresponds to the (implicit) positivity constraint 1 >= 0
Packit fb9d21
 * The next \alpha{ij} correspond to the equalities and come in pairs.
Packit fb9d21
 * The final \alpha{ij} correspond to the inequalities.
Packit fb9d21
 */
Packit fb9d21
static struct isl_basic_set *valid_direction_lp(
Packit fb9d21
	struct isl_basic_set *bset1, struct isl_basic_set *bset2)
Packit fb9d21
{
Packit fb9d21
	isl_space *dim;
Packit fb9d21
	struct isl_basic_set *lp;
Packit fb9d21
	unsigned d;
Packit fb9d21
	int n;
Packit fb9d21
	int i, j, k;
Packit fb9d21
Packit fb9d21
	if (!bset1 || !bset2)
Packit fb9d21
		goto error;
Packit fb9d21
	d = 1 + isl_basic_set_total_dim(bset1);
Packit fb9d21
	n = 2 +
Packit fb9d21
	    2 * bset1->n_eq + bset1->n_ineq + 2 * bset2->n_eq + bset2->n_ineq;
Packit fb9d21
	dim = isl_space_set_alloc(bset1->ctx, 0, n);
Packit fb9d21
	lp = isl_basic_set_alloc_space(dim, 0, d, n);
Packit fb9d21
	if (!lp)
Packit fb9d21
		goto error;
Packit fb9d21
	for (i = 0; i < n; ++i) {
Packit fb9d21
		k = isl_basic_set_alloc_inequality(lp);
Packit fb9d21
		if (k < 0)
Packit fb9d21
			goto error;
Packit fb9d21
		isl_seq_clr(lp->ineq[k] + 1, n);
Packit fb9d21
		isl_int_set_si(lp->ineq[k][0], -1);
Packit fb9d21
		isl_int_set_si(lp->ineq[k][1 + i], 1);
Packit fb9d21
	}
Packit fb9d21
	for (i = 0; i < d; ++i) {
Packit fb9d21
		k = isl_basic_set_alloc_equality(lp);
Packit fb9d21
		if (k < 0)
Packit fb9d21
			goto error;
Packit fb9d21
		n = 0;
Packit fb9d21
		isl_int_set_si(lp->eq[k][n], 0); n++;
Packit fb9d21
		/* positivity constraint 1 >= 0 */
Packit fb9d21
		isl_int_set_si(lp->eq[k][n], i == 0); n++;
Packit fb9d21
		for (j = 0; j < bset1->n_eq; ++j) {
Packit fb9d21
			isl_int_set(lp->eq[k][n], bset1->eq[j][i]); n++;
Packit fb9d21
			isl_int_neg(lp->eq[k][n], bset1->eq[j][i]); n++;
Packit fb9d21
		}
Packit fb9d21
		for (j = 0; j < bset1->n_ineq; ++j) {
Packit fb9d21
			isl_int_set(lp->eq[k][n], bset1->ineq[j][i]); n++;
Packit fb9d21
		}
Packit fb9d21
		/* positivity constraint 1 >= 0 */
Packit fb9d21
		isl_int_set_si(lp->eq[k][n], -(i == 0)); n++;
Packit fb9d21
		for (j = 0; j < bset2->n_eq; ++j) {
Packit fb9d21
			isl_int_neg(lp->eq[k][n], bset2->eq[j][i]); n++;
Packit fb9d21
			isl_int_set(lp->eq[k][n], bset2->eq[j][i]); n++;
Packit fb9d21
		}
Packit fb9d21
		for (j = 0; j < bset2->n_ineq; ++j) {
Packit fb9d21
			isl_int_neg(lp->eq[k][n], bset2->ineq[j][i]); n++;
Packit fb9d21
		}
Packit fb9d21
	}
Packit fb9d21
	lp = isl_basic_set_gauss(lp, NULL);
Packit fb9d21
	isl_basic_set_free(bset1);
Packit fb9d21
	isl_basic_set_free(bset2);
Packit fb9d21
	return lp;
Packit fb9d21
error:
Packit fb9d21
	isl_basic_set_free(bset1);
Packit fb9d21
	isl_basic_set_free(bset2);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Compute a vector s in the homogeneous space such that <s, r> > 0
Packit fb9d21
 * for all rays in the homogeneous space of the two cones that correspond
Packit fb9d21
 * to the input polyhedra bset1 and bset2.
Packit fb9d21
 *
Packit fb9d21
 * We compute s as a vector that satisfies
Packit fb9d21
 *
Packit fb9d21
 *	s = \sum_j \alpha_{ij} h_{ij}	for i = 1,2			(*)
Packit fb9d21
 *
Packit fb9d21
 * with h_{ij} the normals of the facets of polyhedron i
Packit fb9d21
 * (including the "positivity constraint" 1 >= 0) and \alpha_{ij}
Packit fb9d21
 * strictly positive numbers.  For simplicity we impose \alpha_{ij} >= 1.
Packit fb9d21
 * We first set up an LP with as variables the \alpha{ij}.
Packit fb9d21
 * In this formulation, for each polyhedron i,
Packit fb9d21
 * the first constraint is the positivity constraint, followed by pairs
Packit fb9d21
 * of variables for the equalities, followed by variables for the inequalities.
Packit fb9d21
 * We then simply pick a feasible solution and compute s using (*).
Packit fb9d21
 *
Packit fb9d21
 * Note that we simply pick any valid direction and make no attempt
Packit fb9d21
 * to pick a "good" or even the "best" valid direction.
Packit fb9d21
 */
Packit fb9d21
static struct isl_vec *valid_direction(
Packit fb9d21
	struct isl_basic_set *bset1, struct isl_basic_set *bset2)
Packit fb9d21
{
Packit fb9d21
	struct isl_basic_set *lp;
Packit fb9d21
	struct isl_tab *tab;
Packit fb9d21
	struct isl_vec *sample = NULL;
Packit fb9d21
	struct isl_vec *dir;
Packit fb9d21
	unsigned d;
Packit fb9d21
	int i;
Packit fb9d21
	int n;
Packit fb9d21
Packit fb9d21
	if (!bset1 || !bset2)
Packit fb9d21
		goto error;
Packit fb9d21
	lp = valid_direction_lp(isl_basic_set_copy(bset1),
Packit fb9d21
				isl_basic_set_copy(bset2));
Packit fb9d21
	tab = isl_tab_from_basic_set(lp, 0);
Packit fb9d21
	sample = isl_tab_get_sample_value(tab);
Packit fb9d21
	isl_tab_free(tab);
Packit fb9d21
	isl_basic_set_free(lp);
Packit fb9d21
	if (!sample)
Packit fb9d21
		goto error;
Packit fb9d21
	d = isl_basic_set_total_dim(bset1);
Packit fb9d21
	dir = isl_vec_alloc(bset1->ctx, 1 + d);
Packit fb9d21
	if (!dir)
Packit fb9d21
		goto error;
Packit fb9d21
	isl_seq_clr(dir->block.data + 1, dir->size - 1);
Packit fb9d21
	n = 1;
Packit fb9d21
	/* positivity constraint 1 >= 0 */
Packit fb9d21
	isl_int_set(dir->block.data[0], sample->block.data[n]); n++;
Packit fb9d21
	for (i = 0; i < bset1->n_eq; ++i) {
Packit fb9d21
		isl_int_sub(sample->block.data[n],
Packit fb9d21
			    sample->block.data[n], sample->block.data[n+1]);
Packit fb9d21
		isl_seq_combine(dir->block.data,
Packit fb9d21
				bset1->ctx->one, dir->block.data,
Packit fb9d21
				sample->block.data[n], bset1->eq[i], 1 + d);
Packit fb9d21
Packit fb9d21
		n += 2;
Packit fb9d21
	}
Packit fb9d21
	for (i = 0; i < bset1->n_ineq; ++i)
Packit fb9d21
		isl_seq_combine(dir->block.data,
Packit fb9d21
				bset1->ctx->one, dir->block.data,
Packit fb9d21
				sample->block.data[n++], bset1->ineq[i], 1 + d);
Packit fb9d21
	isl_vec_free(sample);
Packit fb9d21
	isl_seq_normalize(bset1->ctx, dir->el, dir->size);
Packit fb9d21
	isl_basic_set_free(bset1);
Packit fb9d21
	isl_basic_set_free(bset2);
Packit fb9d21
	return dir;
Packit fb9d21
error:
Packit fb9d21
	isl_vec_free(sample);
Packit fb9d21
	isl_basic_set_free(bset1);
Packit fb9d21
	isl_basic_set_free(bset2);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Given a polyhedron b_i + A_i x >= 0 and a map T = S^{-1},
Packit fb9d21
 * compute b_i' + A_i' x' >= 0, with
Packit fb9d21
 *
Packit fb9d21
 *	[ b_i A_i ]        [ y' ]		              [ y' ]
Packit fb9d21
 *	[  1   0  ] S^{-1} [ x' ] >= 0	or	[ b_i' A_i' ] [ x' ] >= 0
Packit fb9d21
 *
Packit fb9d21
 * In particular, add the "positivity constraint" and then perform
Packit fb9d21
 * the mapping.
Packit fb9d21
 */
Packit fb9d21
static struct isl_basic_set *homogeneous_map(struct isl_basic_set *bset,
Packit fb9d21
	struct isl_mat *T)
Packit fb9d21
{
Packit fb9d21
	int k;
Packit fb9d21
Packit fb9d21
	if (!bset)
Packit fb9d21
		goto error;
Packit fb9d21
	bset = isl_basic_set_extend_constraints(bset, 0, 1);
Packit fb9d21
	k = isl_basic_set_alloc_inequality(bset);
Packit fb9d21
	if (k < 0)
Packit fb9d21
		goto error;
Packit fb9d21
	isl_seq_clr(bset->ineq[k] + 1, isl_basic_set_total_dim(bset));
Packit fb9d21
	isl_int_set_si(bset->ineq[k][0], 1);
Packit fb9d21
	bset = isl_basic_set_preimage(bset, T);
Packit fb9d21
	return bset;
Packit fb9d21
error:
Packit fb9d21
	isl_mat_free(T);
Packit fb9d21
	isl_basic_set_free(bset);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Compute the convex hull of a pair of basic sets without any parameters or
Packit fb9d21
 * integer divisions, where the convex hull is known to be pointed,
Packit fb9d21
 * but the basic sets may be unbounded.
Packit fb9d21
 *
Packit fb9d21
 * We turn this problem into the computation of a convex hull of a pair
Packit fb9d21
 * _bounded_ polyhedra by "changing the direction of the homogeneous
Packit fb9d21
 * dimension".  This idea is due to Matthias Koeppe.
Packit fb9d21
 *
Packit fb9d21
 * Consider the cones in homogeneous space that correspond to the
Packit fb9d21
 * input polyhedra.  The rays of these cones are also rays of the
Packit fb9d21
 * polyhedra if the coordinate that corresponds to the homogeneous
Packit fb9d21
 * dimension is zero.  That is, if the inner product of the rays
Packit fb9d21
 * with the homogeneous direction is zero.
Packit fb9d21
 * The cones in the homogeneous space can also be considered to
Packit fb9d21
 * correspond to other pairs of polyhedra by chosing a different
Packit fb9d21
 * homogeneous direction.  To ensure that both of these polyhedra
Packit fb9d21
 * are bounded, we need to make sure that all rays of the cones
Packit fb9d21
 * correspond to vertices and not to rays.
Packit fb9d21
 * Let s be a direction such that <s, r> > 0 for all rays r of both cones.
Packit fb9d21
 * Then using s as a homogeneous direction, we obtain a pair of polytopes.
Packit fb9d21
 * The vector s is computed in valid_direction.
Packit fb9d21
 *
Packit fb9d21
 * Note that we need to consider _all_ rays of the cones and not just
Packit fb9d21
 * the rays that correspond to rays in the polyhedra.  If we were to
Packit fb9d21
 * only consider those rays and turn them into vertices, then we
Packit fb9d21
 * may inadvertently turn some vertices into rays.
Packit fb9d21
 *
Packit fb9d21
 * The standard homogeneous direction is the unit vector in the 0th coordinate.
Packit fb9d21
 * We therefore transform the two polyhedra such that the selected
Packit fb9d21
 * direction is mapped onto this standard direction and then proceed
Packit fb9d21
 * with the normal computation.
Packit fb9d21
 * Let S be a non-singular square matrix with s as its first row,
Packit fb9d21
 * then we want to map the polyhedra to the space
Packit fb9d21
 *
Packit fb9d21
 *	[ y' ]     [ y ]		[ y ]          [ y' ]
Packit fb9d21
 *	[ x' ] = S [ x ]	i.e.,	[ x ] = S^{-1} [ x' ]
Packit fb9d21
 *
Packit fb9d21
 * We take S to be the unimodular completion of s to limit the growth
Packit fb9d21
 * of the coefficients in the following computations.
Packit fb9d21
 *
Packit fb9d21
 * Let b_i + A_i x >= 0 be the constraints of polyhedron i.
Packit fb9d21
 * We first move to the homogeneous dimension
Packit fb9d21
 *
Packit fb9d21
 *	b_i y + A_i x >= 0		[ b_i A_i ] [ y ]    [ 0 ]
Packit fb9d21
 *	    y         >= 0	or	[  1   0  ] [ x ] >= [ 0 ]
Packit fb9d21
 *
Packit fb9d21
 * Then we change directoin
Packit fb9d21
 *
Packit fb9d21
 *	[ b_i A_i ]        [ y' ]		              [ y' ]
Packit fb9d21
 *	[  1   0  ] S^{-1} [ x' ] >= 0	or	[ b_i' A_i' ] [ x' ] >= 0
Packit fb9d21
 *
Packit fb9d21
 * Then we compute the convex hull of the polytopes b_i' + A_i' x' >= 0
Packit fb9d21
 * resulting in b' + A' x' >= 0, which we then convert back
Packit fb9d21
 *
Packit fb9d21
 *	            [ y ]		        [ y ]
Packit fb9d21
 *	[ b' A' ] S [ x ] >= 0	or	[ b A ] [ x ] >= 0
Packit fb9d21
 *
Packit fb9d21
 * The polyhedron b + A x >= 0 is then the convex hull of the input polyhedra.
Packit fb9d21
 */
Packit fb9d21
static struct isl_basic_set *convex_hull_pair_pointed(
Packit fb9d21
	struct isl_basic_set *bset1, struct isl_basic_set *bset2)
Packit fb9d21
{
Packit fb9d21
	struct isl_ctx *ctx = NULL;
Packit fb9d21
	struct isl_vec *dir = NULL;
Packit fb9d21
	struct isl_mat *T = NULL;
Packit fb9d21
	struct isl_mat *T2 = NULL;
Packit fb9d21
	struct isl_basic_set *hull;
Packit fb9d21
	struct isl_set *set;
Packit fb9d21
Packit fb9d21
	if (!bset1 || !bset2)
Packit fb9d21
		goto error;
Packit fb9d21
	ctx = bset1->ctx;
Packit fb9d21
	dir = valid_direction(isl_basic_set_copy(bset1),
Packit fb9d21
				isl_basic_set_copy(bset2));
Packit fb9d21
	if (!dir)
Packit fb9d21
		goto error;
Packit fb9d21
	T = isl_mat_alloc(bset1->ctx, dir->size, dir->size);
Packit fb9d21
	if (!T)
Packit fb9d21
		goto error;
Packit fb9d21
	isl_seq_cpy(T->row[0], dir->block.data, dir->size);
Packit fb9d21
	T = isl_mat_unimodular_complete(T, 1);
Packit fb9d21
	T2 = isl_mat_right_inverse(isl_mat_copy(T));
Packit fb9d21
Packit fb9d21
	bset1 = homogeneous_map(bset1, isl_mat_copy(T2));
Packit fb9d21
	bset2 = homogeneous_map(bset2, T2);
Packit fb9d21
	set = isl_set_alloc_space(isl_basic_set_get_space(bset1), 2, 0);
Packit fb9d21
	set = isl_set_add_basic_set(set, bset1);
Packit fb9d21
	set = isl_set_add_basic_set(set, bset2);
Packit fb9d21
	hull = uset_convex_hull(set);
Packit fb9d21
	hull = isl_basic_set_preimage(hull, T);
Packit fb9d21
	 
Packit fb9d21
	isl_vec_free(dir);
Packit fb9d21
Packit fb9d21
	return hull;
Packit fb9d21
error:
Packit fb9d21
	isl_vec_free(dir);
Packit fb9d21
	isl_basic_set_free(bset1);
Packit fb9d21
	isl_basic_set_free(bset2);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
static struct isl_basic_set *uset_convex_hull_wrap(struct isl_set *set);
Packit fb9d21
static struct isl_basic_set *modulo_affine_hull(
Packit fb9d21
	struct isl_set *set, struct isl_basic_set *affine_hull);
Packit fb9d21
Packit fb9d21
/* Compute the convex hull of a pair of basic sets without any parameters or
Packit fb9d21
 * integer divisions.
Packit fb9d21
 *
Packit fb9d21
 * This function is called from uset_convex_hull_unbounded, which
Packit fb9d21
 * means that the complete convex hull is unbounded.  Some pairs
Packit fb9d21
 * of basic sets may still be bounded, though.
Packit fb9d21
 * They may even lie inside a lower dimensional space, in which
Packit fb9d21
 * case they need to be handled inside their affine hull since
Packit fb9d21
 * the main algorithm assumes that the result is full-dimensional.
Packit fb9d21
 *
Packit fb9d21
 * If the convex hull of the two basic sets would have a non-trivial
Packit fb9d21
 * lineality space, we first project out this lineality space.
Packit fb9d21
 */
Packit fb9d21
static struct isl_basic_set *convex_hull_pair(struct isl_basic_set *bset1,
Packit fb9d21
	struct isl_basic_set *bset2)
Packit fb9d21
{
Packit fb9d21
	isl_basic_set *lin, *aff;
Packit fb9d21
	int bounded1, bounded2;
Packit fb9d21
Packit fb9d21
	if (bset1->ctx->opt->convex == ISL_CONVEX_HULL_FM)
Packit fb9d21
		return convex_hull_pair_elim(bset1, bset2);
Packit fb9d21
Packit fb9d21
	aff = isl_set_affine_hull(isl_basic_set_union(isl_basic_set_copy(bset1),
Packit fb9d21
						    isl_basic_set_copy(bset2)));
Packit fb9d21
	if (!aff)
Packit fb9d21
		goto error;
Packit fb9d21
	if (aff->n_eq != 0) 
Packit fb9d21
		return modulo_affine_hull(isl_basic_set_union(bset1, bset2), aff);
Packit fb9d21
	isl_basic_set_free(aff);
Packit fb9d21
Packit fb9d21
	bounded1 = isl_basic_set_is_bounded(bset1);
Packit fb9d21
	bounded2 = isl_basic_set_is_bounded(bset2);
Packit fb9d21
Packit fb9d21
	if (bounded1 < 0 || bounded2 < 0)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	if (bounded1 && bounded2)
Packit fb9d21
		return uset_convex_hull_wrap(isl_basic_set_union(bset1, bset2));
Packit fb9d21
Packit fb9d21
	if (bounded1 || bounded2)
Packit fb9d21
		return convex_hull_pair_pointed(bset1, bset2);
Packit fb9d21
Packit fb9d21
	lin = induced_lineality_space(isl_basic_set_copy(bset1),
Packit fb9d21
				      isl_basic_set_copy(bset2));
Packit fb9d21
	if (!lin)
Packit fb9d21
		goto error;
Packit fb9d21
	if (isl_basic_set_is_universe(lin)) {
Packit fb9d21
		isl_basic_set_free(bset1);
Packit fb9d21
		isl_basic_set_free(bset2);
Packit fb9d21
		return lin;
Packit fb9d21
	}
Packit fb9d21
	if (lin->n_eq < isl_basic_set_total_dim(lin)) {
Packit fb9d21
		struct isl_set *set;
Packit fb9d21
		set = isl_set_alloc_space(isl_basic_set_get_space(bset1), 2, 0);
Packit fb9d21
		set = isl_set_add_basic_set(set, bset1);
Packit fb9d21
		set = isl_set_add_basic_set(set, bset2);
Packit fb9d21
		return modulo_lineality(set, lin);
Packit fb9d21
	}
Packit fb9d21
	isl_basic_set_free(lin);
Packit fb9d21
Packit fb9d21
	return convex_hull_pair_pointed(bset1, bset2);
Packit fb9d21
error:
Packit fb9d21
	isl_basic_set_free(bset1);
Packit fb9d21
	isl_basic_set_free(bset2);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Compute the lineality space of a basic set.
Packit fb9d21
 * We currently do not allow the basic set to have any divs.
Packit fb9d21
 * We basically just drop the constants and turn every inequality
Packit fb9d21
 * into an equality.
Packit fb9d21
 */
Packit fb9d21
struct isl_basic_set *isl_basic_set_lineality_space(struct isl_basic_set *bset)
Packit fb9d21
{
Packit fb9d21
	int i, k;
Packit fb9d21
	struct isl_basic_set *lin = NULL;
Packit fb9d21
	unsigned dim;
Packit fb9d21
Packit fb9d21
	if (!bset)
Packit fb9d21
		goto error;
Packit fb9d21
	isl_assert(bset->ctx, bset->n_div == 0, goto error);
Packit fb9d21
	dim = isl_basic_set_total_dim(bset);
Packit fb9d21
Packit fb9d21
	lin = isl_basic_set_alloc_space(isl_basic_set_get_space(bset), 0, dim, 0);
Packit fb9d21
	if (!lin)
Packit fb9d21
		goto error;
Packit fb9d21
	for (i = 0; i < bset->n_eq; ++i) {
Packit fb9d21
		k = isl_basic_set_alloc_equality(lin);
Packit fb9d21
		if (k < 0)
Packit fb9d21
			goto error;
Packit fb9d21
		isl_int_set_si(lin->eq[k][0], 0);
Packit fb9d21
		isl_seq_cpy(lin->eq[k] + 1, bset->eq[i] + 1, dim);
Packit fb9d21
	}
Packit fb9d21
	lin = isl_basic_set_gauss(lin, NULL);
Packit fb9d21
	if (!lin)
Packit fb9d21
		goto error;
Packit fb9d21
	for (i = 0; i < bset->n_ineq && lin->n_eq < dim; ++i) {
Packit fb9d21
		k = isl_basic_set_alloc_equality(lin);
Packit fb9d21
		if (k < 0)
Packit fb9d21
			goto error;
Packit fb9d21
		isl_int_set_si(lin->eq[k][0], 0);
Packit fb9d21
		isl_seq_cpy(lin->eq[k] + 1, bset->ineq[i] + 1, dim);
Packit fb9d21
		lin = isl_basic_set_gauss(lin, NULL);
Packit fb9d21
		if (!lin)
Packit fb9d21
			goto error;
Packit fb9d21
	}
Packit fb9d21
	isl_basic_set_free(bset);
Packit fb9d21
	return lin;
Packit fb9d21
error:
Packit fb9d21
	isl_basic_set_free(lin);
Packit fb9d21
	isl_basic_set_free(bset);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Compute the (linear) hull of the lineality spaces of the basic sets in the
Packit fb9d21
 * "underlying" set "set".
Packit fb9d21
 */
Packit fb9d21
static struct isl_basic_set *uset_combined_lineality_space(struct isl_set *set)
Packit fb9d21
{
Packit fb9d21
	int i;
Packit fb9d21
	struct isl_set *lin = NULL;
Packit fb9d21
Packit fb9d21
	if (!set)
Packit fb9d21
		return NULL;
Packit fb9d21
	if (set->n == 0) {
Packit fb9d21
		isl_space *dim = isl_set_get_space(set);
Packit fb9d21
		isl_set_free(set);
Packit fb9d21
		return isl_basic_set_empty(dim);
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	lin = isl_set_alloc_space(isl_set_get_space(set), set->n, 0);
Packit fb9d21
	for (i = 0; i < set->n; ++i)
Packit fb9d21
		lin = isl_set_add_basic_set(lin,
Packit fb9d21
		    isl_basic_set_lineality_space(isl_basic_set_copy(set->p[i])));
Packit fb9d21
	isl_set_free(set);
Packit fb9d21
	return isl_set_affine_hull(lin);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Compute the convex hull of a set without any parameters or
Packit fb9d21
 * integer divisions.
Packit fb9d21
 * In each step, we combined two basic sets until only one
Packit fb9d21
 * basic set is left.
Packit fb9d21
 * The input basic sets are assumed not to have a non-trivial
Packit fb9d21
 * lineality space.  If any of the intermediate results has
Packit fb9d21
 * a non-trivial lineality space, it is projected out.
Packit fb9d21
 */
Packit fb9d21
static struct isl_basic_set *uset_convex_hull_unbounded(struct isl_set *set)
Packit fb9d21
{
Packit fb9d21
	struct isl_basic_set *convex_hull = NULL;
Packit fb9d21
Packit fb9d21
	convex_hull = isl_set_copy_basic_set(set);
Packit fb9d21
	set = isl_set_drop_basic_set(set, convex_hull);
Packit fb9d21
	if (!set)
Packit fb9d21
		goto error;
Packit fb9d21
	while (set->n > 0) {
Packit fb9d21
		struct isl_basic_set *t;
Packit fb9d21
		t = isl_set_copy_basic_set(set);
Packit fb9d21
		if (!t)
Packit fb9d21
			goto error;
Packit fb9d21
		set = isl_set_drop_basic_set(set, t);
Packit fb9d21
		if (!set)
Packit fb9d21
			goto error;
Packit fb9d21
		convex_hull = convex_hull_pair(convex_hull, t);
Packit fb9d21
		if (set->n == 0)
Packit fb9d21
			break;
Packit fb9d21
		t = isl_basic_set_lineality_space(isl_basic_set_copy(convex_hull));
Packit fb9d21
		if (!t)
Packit fb9d21
			goto error;
Packit fb9d21
		if (isl_basic_set_is_universe(t)) {
Packit fb9d21
			isl_basic_set_free(convex_hull);
Packit fb9d21
			convex_hull = t;
Packit fb9d21
			break;
Packit fb9d21
		}
Packit fb9d21
		if (t->n_eq < isl_basic_set_total_dim(t)) {
Packit fb9d21
			set = isl_set_add_basic_set(set, convex_hull);
Packit fb9d21
			return modulo_lineality(set, t);
Packit fb9d21
		}
Packit fb9d21
		isl_basic_set_free(t);
Packit fb9d21
	}
Packit fb9d21
	isl_set_free(set);
Packit fb9d21
	return convex_hull;
Packit fb9d21
error:
Packit fb9d21
	isl_set_free(set);
Packit fb9d21
	isl_basic_set_free(convex_hull);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Compute an initial hull for wrapping containing a single initial
Packit fb9d21
 * facet.
Packit fb9d21
 * This function assumes that the given set is bounded.
Packit fb9d21
 */
Packit fb9d21
static struct isl_basic_set *initial_hull(struct isl_basic_set *hull,
Packit fb9d21
	struct isl_set *set)
Packit fb9d21
{
Packit fb9d21
	struct isl_mat *bounds = NULL;
Packit fb9d21
	unsigned dim;
Packit fb9d21
	int k;
Packit fb9d21
Packit fb9d21
	if (!hull)
Packit fb9d21
		goto error;
Packit fb9d21
	bounds = initial_facet_constraint(set);
Packit fb9d21
	if (!bounds)
Packit fb9d21
		goto error;
Packit fb9d21
	k = isl_basic_set_alloc_inequality(hull);
Packit fb9d21
	if (k < 0)
Packit fb9d21
		goto error;
Packit fb9d21
	dim = isl_set_n_dim(set);
Packit fb9d21
	isl_assert(set->ctx, 1 + dim == bounds->n_col, goto error);
Packit fb9d21
	isl_seq_cpy(hull->ineq[k], bounds->row[0], bounds->n_col);
Packit fb9d21
	isl_mat_free(bounds);
Packit fb9d21
Packit fb9d21
	return hull;
Packit fb9d21
error:
Packit fb9d21
	isl_basic_set_free(hull);
Packit fb9d21
	isl_mat_free(bounds);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
struct max_constraint {
Packit fb9d21
	struct isl_mat *c;
Packit fb9d21
	int	 	count;
Packit fb9d21
	int		ineq;
Packit fb9d21
};
Packit fb9d21
Packit fb9d21
static int max_constraint_equal(const void *entry, const void *val)
Packit fb9d21
{
Packit fb9d21
	struct max_constraint *a = (struct max_constraint *)entry;
Packit fb9d21
	isl_int *b = (isl_int *)val;
Packit fb9d21
Packit fb9d21
	return isl_seq_eq(a->c->row[0] + 1, b, a->c->n_col - 1);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
static void update_constraint(struct isl_ctx *ctx, struct isl_hash_table *table,
Packit fb9d21
	isl_int *con, unsigned len, int n, int ineq)
Packit fb9d21
{
Packit fb9d21
	struct isl_hash_table_entry *entry;
Packit fb9d21
	struct max_constraint *c;
Packit fb9d21
	uint32_t c_hash;
Packit fb9d21
Packit fb9d21
	c_hash = isl_seq_get_hash(con + 1, len);
Packit fb9d21
	entry = isl_hash_table_find(ctx, table, c_hash, max_constraint_equal,
Packit fb9d21
			con + 1, 0);
Packit fb9d21
	if (!entry)
Packit fb9d21
		return;
Packit fb9d21
	c = entry->data;
Packit fb9d21
	if (c->count < n) {
Packit fb9d21
		isl_hash_table_remove(ctx, table, entry);
Packit fb9d21
		return;
Packit fb9d21
	}
Packit fb9d21
	c->count++;
Packit fb9d21
	if (isl_int_gt(c->c->row[0][0], con[0]))
Packit fb9d21
		return;
Packit fb9d21
	if (isl_int_eq(c->c->row[0][0], con[0])) {
Packit fb9d21
		if (ineq)
Packit fb9d21
			c->ineq = ineq;
Packit fb9d21
		return;
Packit fb9d21
	}
Packit fb9d21
	c->c = isl_mat_cow(c->c);
Packit fb9d21
	isl_int_set(c->c->row[0][0], con[0]);
Packit fb9d21
	c->ineq = ineq;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Check whether the constraint hash table "table" constains the constraint
Packit fb9d21
 * "con".
Packit fb9d21
 */
Packit fb9d21
static int has_constraint(struct isl_ctx *ctx, struct isl_hash_table *table,
Packit fb9d21
	isl_int *con, unsigned len, int n)
Packit fb9d21
{
Packit fb9d21
	struct isl_hash_table_entry *entry;
Packit fb9d21
	struct max_constraint *c;
Packit fb9d21
	uint32_t c_hash;
Packit fb9d21
Packit fb9d21
	c_hash = isl_seq_get_hash(con + 1, len);
Packit fb9d21
	entry = isl_hash_table_find(ctx, table, c_hash, max_constraint_equal,
Packit fb9d21
			con + 1, 0);
Packit fb9d21
	if (!entry)
Packit fb9d21
		return 0;
Packit fb9d21
	c = entry->data;
Packit fb9d21
	if (c->count < n)
Packit fb9d21
		return 0;
Packit fb9d21
	return isl_int_eq(c->c->row[0][0], con[0]);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Check for inequality constraints of a basic set without equalities
Packit fb9d21
 * such that the same or more stringent copies of the constraint appear
Packit fb9d21
 * in all of the basic sets.  Such constraints are necessarily facet
Packit fb9d21
 * constraints of the convex hull.
Packit fb9d21
 *
Packit fb9d21
 * If the resulting basic set is by chance identical to one of
Packit fb9d21
 * the basic sets in "set", then we know that this basic set contains
Packit fb9d21
 * all other basic sets and is therefore the convex hull of set.
Packit fb9d21
 * In this case we set *is_hull to 1.
Packit fb9d21
 */
Packit fb9d21
static struct isl_basic_set *common_constraints(struct isl_basic_set *hull,
Packit fb9d21
	struct isl_set *set, int *is_hull)
Packit fb9d21
{
Packit fb9d21
	int i, j, s, n;
Packit fb9d21
	int min_constraints;
Packit fb9d21
	int best;
Packit fb9d21
	struct max_constraint *constraints = NULL;
Packit fb9d21
	struct isl_hash_table *table = NULL;
Packit fb9d21
	unsigned total;
Packit fb9d21
Packit fb9d21
	*is_hull = 0;
Packit fb9d21
Packit fb9d21
	for (i = 0; i < set->n; ++i)
Packit fb9d21
		if (set->p[i]->n_eq == 0)
Packit fb9d21
			break;
Packit fb9d21
	if (i >= set->n)
Packit fb9d21
		return hull;
Packit fb9d21
	min_constraints = set->p[i]->n_ineq;
Packit fb9d21
	best = i;
Packit fb9d21
	for (i = best + 1; i < set->n; ++i) {
Packit fb9d21
		if (set->p[i]->n_eq != 0)
Packit fb9d21
			continue;
Packit fb9d21
		if (set->p[i]->n_ineq >= min_constraints)
Packit fb9d21
			continue;
Packit fb9d21
		min_constraints = set->p[i]->n_ineq;
Packit fb9d21
		best = i;
Packit fb9d21
	}
Packit fb9d21
	constraints = isl_calloc_array(hull->ctx, struct max_constraint,
Packit fb9d21
					min_constraints);
Packit fb9d21
	if (!constraints)
Packit fb9d21
		return hull;
Packit fb9d21
	table = isl_alloc_type(hull->ctx, struct isl_hash_table);
Packit fb9d21
	if (isl_hash_table_init(hull->ctx, table, min_constraints))
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	total = isl_space_dim(set->dim, isl_dim_all);
Packit fb9d21
	for (i = 0; i < set->p[best]->n_ineq; ++i) {
Packit fb9d21
		constraints[i].c = isl_mat_sub_alloc6(hull->ctx,
Packit fb9d21
			set->p[best]->ineq + i, 0, 1, 0, 1 + total);
Packit fb9d21
		if (!constraints[i].c)
Packit fb9d21
			goto error;
Packit fb9d21
		constraints[i].ineq = 1;
Packit fb9d21
	}
Packit fb9d21
	for (i = 0; i < min_constraints; ++i) {
Packit fb9d21
		struct isl_hash_table_entry *entry;
Packit fb9d21
		uint32_t c_hash;
Packit fb9d21
		c_hash = isl_seq_get_hash(constraints[i].c->row[0] + 1, total);
Packit fb9d21
		entry = isl_hash_table_find(hull->ctx, table, c_hash,
Packit fb9d21
			max_constraint_equal, constraints[i].c->row[0] + 1, 1);
Packit fb9d21
		if (!entry)
Packit fb9d21
			goto error;
Packit fb9d21
		isl_assert(hull->ctx, !entry->data, goto error);
Packit fb9d21
		entry->data = &constraints[i];
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	n = 0;
Packit fb9d21
	for (s = 0; s < set->n; ++s) {
Packit fb9d21
		if (s == best)
Packit fb9d21
			continue;
Packit fb9d21
Packit fb9d21
		for (i = 0; i < set->p[s]->n_eq; ++i) {
Packit fb9d21
			isl_int *eq = set->p[s]->eq[i];
Packit fb9d21
			for (j = 0; j < 2; ++j) {
Packit fb9d21
				isl_seq_neg(eq, eq, 1 + total);
Packit fb9d21
				update_constraint(hull->ctx, table,
Packit fb9d21
							    eq, total, n, 0);
Packit fb9d21
			}
Packit fb9d21
		}
Packit fb9d21
		for (i = 0; i < set->p[s]->n_ineq; ++i) {
Packit fb9d21
			isl_int *ineq = set->p[s]->ineq[i];
Packit fb9d21
			update_constraint(hull->ctx, table, ineq, total, n,
Packit fb9d21
				set->p[s]->n_eq == 0);
Packit fb9d21
		}
Packit fb9d21
		++n;
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	for (i = 0; i < min_constraints; ++i) {
Packit fb9d21
		if (constraints[i].count < n)
Packit fb9d21
			continue;
Packit fb9d21
		if (!constraints[i].ineq)
Packit fb9d21
			continue;
Packit fb9d21
		j = isl_basic_set_alloc_inequality(hull);
Packit fb9d21
		if (j < 0)
Packit fb9d21
			goto error;
Packit fb9d21
		isl_seq_cpy(hull->ineq[j], constraints[i].c->row[0], 1 + total);
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	for (s = 0; s < set->n; ++s) {
Packit fb9d21
		if (set->p[s]->n_eq)
Packit fb9d21
			continue;
Packit fb9d21
		if (set->p[s]->n_ineq != hull->n_ineq)
Packit fb9d21
			continue;
Packit fb9d21
		for (i = 0; i < set->p[s]->n_ineq; ++i) {
Packit fb9d21
			isl_int *ineq = set->p[s]->ineq[i];
Packit fb9d21
			if (!has_constraint(hull->ctx, table, ineq, total, n))
Packit fb9d21
				break;
Packit fb9d21
		}
Packit fb9d21
		if (i == set->p[s]->n_ineq)
Packit fb9d21
			*is_hull = 1;
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	isl_hash_table_clear(table);
Packit fb9d21
	for (i = 0; i < min_constraints; ++i)
Packit fb9d21
		isl_mat_free(constraints[i].c);
Packit fb9d21
	free(constraints);
Packit fb9d21
	free(table);
Packit fb9d21
	return hull;
Packit fb9d21
error:
Packit fb9d21
	isl_hash_table_clear(table);
Packit fb9d21
	free(table);
Packit fb9d21
	if (constraints)
Packit fb9d21
		for (i = 0; i < min_constraints; ++i)
Packit fb9d21
			isl_mat_free(constraints[i].c);
Packit fb9d21
	free(constraints);
Packit fb9d21
	return hull;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Create a template for the convex hull of "set" and fill it up
Packit fb9d21
 * obvious facet constraints, if any.  If the result happens to
Packit fb9d21
 * be the convex hull of "set" then *is_hull is set to 1.
Packit fb9d21
 */
Packit fb9d21
static struct isl_basic_set *proto_hull(struct isl_set *set, int *is_hull)
Packit fb9d21
{
Packit fb9d21
	struct isl_basic_set *hull;
Packit fb9d21
	unsigned n_ineq;
Packit fb9d21
	int i;
Packit fb9d21
Packit fb9d21
	n_ineq = 1;
Packit fb9d21
	for (i = 0; i < set->n; ++i) {
Packit fb9d21
		n_ineq += set->p[i]->n_eq;
Packit fb9d21
		n_ineq += set->p[i]->n_ineq;
Packit fb9d21
	}
Packit fb9d21
	hull = isl_basic_set_alloc_space(isl_space_copy(set->dim), 0, 0, n_ineq);
Packit fb9d21
	hull = isl_basic_set_set_rational(hull);
Packit fb9d21
	if (!hull)
Packit fb9d21
		return NULL;
Packit fb9d21
	return common_constraints(hull, set, is_hull);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
static struct isl_basic_set *uset_convex_hull_wrap(struct isl_set *set)
Packit fb9d21
{
Packit fb9d21
	struct isl_basic_set *hull;
Packit fb9d21
	int is_hull;
Packit fb9d21
Packit fb9d21
	hull = proto_hull(set, &is_hull);
Packit fb9d21
	if (hull && !is_hull) {
Packit fb9d21
		if (hull->n_ineq == 0)
Packit fb9d21
			hull = initial_hull(hull, set);
Packit fb9d21
		hull = extend(hull, set);
Packit fb9d21
	}
Packit fb9d21
	isl_set_free(set);
Packit fb9d21
Packit fb9d21
	return hull;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Compute the convex hull of a set without any parameters or
Packit fb9d21
 * integer divisions.  Depending on whether the set is bounded,
Packit fb9d21
 * we pass control to the wrapping based convex hull or
Packit fb9d21
 * the Fourier-Motzkin elimination based convex hull.
Packit fb9d21
 * We also handle a few special cases before checking the boundedness.
Packit fb9d21
 */
Packit fb9d21
static struct isl_basic_set *uset_convex_hull(struct isl_set *set)
Packit fb9d21
{
Packit fb9d21
	struct isl_basic_set *convex_hull = NULL;
Packit fb9d21
	struct isl_basic_set *lin;
Packit fb9d21
Packit fb9d21
	if (isl_set_n_dim(set) == 0)
Packit fb9d21
		return convex_hull_0d(set);
Packit fb9d21
Packit fb9d21
	set = isl_set_coalesce(set);
Packit fb9d21
	set = isl_set_set_rational(set);
Packit fb9d21
Packit fb9d21
	if (!set)
Packit fb9d21
		goto error;
Packit fb9d21
	if (!set)
Packit fb9d21
		return NULL;
Packit fb9d21
	if (set->n == 1) {
Packit fb9d21
		convex_hull = isl_basic_set_copy(set->p[0]);
Packit fb9d21
		isl_set_free(set);
Packit fb9d21
		return convex_hull;
Packit fb9d21
	}
Packit fb9d21
	if (isl_set_n_dim(set) == 1)
Packit fb9d21
		return convex_hull_1d(set);
Packit fb9d21
Packit fb9d21
	if (isl_set_is_bounded(set) &&
Packit fb9d21
	    set->ctx->opt->convex == ISL_CONVEX_HULL_WRAP)
Packit fb9d21
		return uset_convex_hull_wrap(set);
Packit fb9d21
Packit fb9d21
	lin = uset_combined_lineality_space(isl_set_copy(set));
Packit fb9d21
	if (!lin)
Packit fb9d21
		goto error;
Packit fb9d21
	if (isl_basic_set_is_universe(lin)) {
Packit fb9d21
		isl_set_free(set);
Packit fb9d21
		return lin;
Packit fb9d21
	}
Packit fb9d21
	if (lin->n_eq < isl_basic_set_total_dim(lin))
Packit fb9d21
		return modulo_lineality(set, lin);
Packit fb9d21
	isl_basic_set_free(lin);
Packit fb9d21
Packit fb9d21
	return uset_convex_hull_unbounded(set);
Packit fb9d21
error:
Packit fb9d21
	isl_set_free(set);
Packit fb9d21
	isl_basic_set_free(convex_hull);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* This is the core procedure, where "set" is a "pure" set, i.e.,
Packit fb9d21
 * without parameters or divs and where the convex hull of set is
Packit fb9d21
 * known to be full-dimensional.
Packit fb9d21
 */
Packit fb9d21
static struct isl_basic_set *uset_convex_hull_wrap_bounded(struct isl_set *set)
Packit fb9d21
{
Packit fb9d21
	struct isl_basic_set *convex_hull = NULL;
Packit fb9d21
Packit fb9d21
	if (!set)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	if (isl_set_n_dim(set) == 0) {
Packit fb9d21
		convex_hull = isl_basic_set_universe(isl_space_copy(set->dim));
Packit fb9d21
		isl_set_free(set);
Packit fb9d21
		convex_hull = isl_basic_set_set_rational(convex_hull);
Packit fb9d21
		return convex_hull;
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	set = isl_set_set_rational(set);
Packit fb9d21
	set = isl_set_coalesce(set);
Packit fb9d21
	if (!set)
Packit fb9d21
		goto error;
Packit fb9d21
	if (set->n == 1) {
Packit fb9d21
		convex_hull = isl_basic_set_copy(set->p[0]);
Packit fb9d21
		isl_set_free(set);
Packit fb9d21
		convex_hull = isl_basic_map_remove_redundancies(convex_hull);
Packit fb9d21
		return convex_hull;
Packit fb9d21
	}
Packit fb9d21
	if (isl_set_n_dim(set) == 1)
Packit fb9d21
		return convex_hull_1d(set);
Packit fb9d21
Packit fb9d21
	return uset_convex_hull_wrap(set);
Packit fb9d21
error:
Packit fb9d21
	isl_set_free(set);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Compute the convex hull of set "set" with affine hull "affine_hull",
Packit fb9d21
 * We first remove the equalities (transforming the set), compute the
Packit fb9d21
 * convex hull of the transformed set and then add the equalities back
Packit fb9d21
 * (after performing the inverse transformation.
Packit fb9d21
 */
Packit fb9d21
static struct isl_basic_set *modulo_affine_hull(
Packit fb9d21
	struct isl_set *set, struct isl_basic_set *affine_hull)
Packit fb9d21
{
Packit fb9d21
	struct isl_mat *T;
Packit fb9d21
	struct isl_mat *T2;
Packit fb9d21
	struct isl_basic_set *dummy;
Packit fb9d21
	struct isl_basic_set *convex_hull;
Packit fb9d21
Packit fb9d21
	dummy = isl_basic_set_remove_equalities(
Packit fb9d21
			isl_basic_set_copy(affine_hull), &T, &T2;;
Packit fb9d21
	if (!dummy)
Packit fb9d21
		goto error;
Packit fb9d21
	isl_basic_set_free(dummy);
Packit fb9d21
	set = isl_set_preimage(set, T);
Packit fb9d21
	convex_hull = uset_convex_hull(set);
Packit fb9d21
	convex_hull = isl_basic_set_preimage(convex_hull, T2);
Packit fb9d21
	convex_hull = isl_basic_set_intersect(convex_hull, affine_hull);
Packit fb9d21
	return convex_hull;
Packit fb9d21
error:
Packit fb9d21
	isl_basic_set_free(affine_hull);
Packit fb9d21
	isl_set_free(set);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Compute the convex hull of a map.
Packit fb9d21
 *
Packit fb9d21
 * The implementation was inspired by "Extended Convex Hull" by Fukuda et al.,
Packit fb9d21
 * specifically, the wrapping of facets to obtain new facets.
Packit fb9d21
 */
Packit fb9d21
struct isl_basic_map *isl_map_convex_hull(struct isl_map *map)
Packit fb9d21
{
Packit fb9d21
	struct isl_basic_set *bset;
Packit fb9d21
	struct isl_basic_map *model = NULL;
Packit fb9d21
	struct isl_basic_set *affine_hull = NULL;
Packit fb9d21
	struct isl_basic_map *convex_hull = NULL;
Packit fb9d21
	struct isl_set *set = NULL;
Packit fb9d21
	struct isl_ctx *ctx;
Packit fb9d21
Packit fb9d21
	map = isl_map_detect_equalities(map);
Packit fb9d21
	map = isl_map_align_divs(map);
Packit fb9d21
	if (!map)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	ctx = map->ctx;
Packit fb9d21
	if (map->n == 0) {
Packit fb9d21
		convex_hull = isl_basic_map_empty_like_map(map);
Packit fb9d21
		isl_map_free(map);
Packit fb9d21
		return convex_hull;
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	model = isl_basic_map_copy(map->p[0]);
Packit fb9d21
	set = isl_map_underlying_set(map);
Packit fb9d21
	if (!set)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	affine_hull = isl_set_affine_hull(isl_set_copy(set));
Packit fb9d21
	if (!affine_hull)
Packit fb9d21
		goto error;
Packit fb9d21
	if (affine_hull->n_eq != 0)
Packit fb9d21
		bset = modulo_affine_hull(set, affine_hull);
Packit fb9d21
	else {
Packit fb9d21
		isl_basic_set_free(affine_hull);
Packit fb9d21
		bset = uset_convex_hull(set);
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	convex_hull = isl_basic_map_overlying_set(bset, model);
Packit fb9d21
	if (!convex_hull)
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	ISL_F_SET(convex_hull, ISL_BASIC_MAP_NO_IMPLICIT);
Packit fb9d21
	ISL_F_SET(convex_hull, ISL_BASIC_MAP_ALL_EQUALITIES);
Packit fb9d21
	ISL_F_CLR(convex_hull, ISL_BASIC_MAP_RATIONAL);
Packit fb9d21
	return convex_hull;
Packit fb9d21
error:
Packit fb9d21
	isl_set_free(set);
Packit fb9d21
	isl_basic_map_free(model);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
struct isl_basic_set *isl_set_convex_hull(struct isl_set *set)
Packit fb9d21
{
Packit fb9d21
	return (struct isl_basic_set *)
Packit fb9d21
		isl_map_convex_hull((struct isl_map *)set);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
__isl_give isl_basic_map *isl_map_polyhedral_hull(__isl_take isl_map *map)
Packit fb9d21
{
Packit fb9d21
	isl_basic_map *hull;
Packit fb9d21
Packit fb9d21
	hull = isl_map_convex_hull(map);
Packit fb9d21
	return isl_basic_map_remove_divs(hull);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
__isl_give isl_basic_set *isl_set_polyhedral_hull(__isl_take isl_set *set)
Packit fb9d21
{
Packit fb9d21
	return (isl_basic_set *)isl_map_polyhedral_hull((isl_map *)set);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
struct sh_data_entry {
Packit fb9d21
	struct isl_hash_table	*table;
Packit fb9d21
	struct isl_tab		*tab;
Packit fb9d21
};
Packit fb9d21
Packit fb9d21
/* Holds the data needed during the simple hull computation.
Packit fb9d21
 * In particular,
Packit fb9d21
 *	n		the number of basic sets in the original set
Packit fb9d21
 *	hull_table	a hash table of already computed constraints
Packit fb9d21
 *			in the simple hull
Packit fb9d21
 *	p		for each basic set,
Packit fb9d21
 *		table		a hash table of the constraints
Packit fb9d21
 *		tab		the tableau corresponding to the basic set
Packit fb9d21
 */
Packit fb9d21
struct sh_data {
Packit fb9d21
	struct isl_ctx		*ctx;
Packit fb9d21
	unsigned		n;
Packit fb9d21
	struct isl_hash_table	*hull_table;
Packit fb9d21
	struct sh_data_entry	p[1];
Packit fb9d21
};
Packit fb9d21
Packit fb9d21
static void sh_data_free(struct sh_data *data)
Packit fb9d21
{
Packit fb9d21
	int i;
Packit fb9d21
Packit fb9d21
	if (!data)
Packit fb9d21
		return;
Packit fb9d21
	isl_hash_table_free(data->ctx, data->hull_table);
Packit fb9d21
	for (i = 0; i < data->n; ++i) {
Packit fb9d21
		isl_hash_table_free(data->ctx, data->p[i].table);
Packit fb9d21
		isl_tab_free(data->p[i].tab);
Packit fb9d21
	}
Packit fb9d21
	free(data);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
struct ineq_cmp_data {
Packit fb9d21
	unsigned	len;
Packit fb9d21
	isl_int		*p;
Packit fb9d21
};
Packit fb9d21
Packit fb9d21
static int has_ineq(const void *entry, const void *val)
Packit fb9d21
{
Packit fb9d21
	isl_int *row = (isl_int *)entry;
Packit fb9d21
	struct ineq_cmp_data *v = (struct ineq_cmp_data *)val;
Packit fb9d21
Packit fb9d21
	return isl_seq_eq(row + 1, v->p + 1, v->len) ||
Packit fb9d21
	       isl_seq_is_neg(row + 1, v->p + 1, v->len);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
static int hash_ineq(struct isl_ctx *ctx, struct isl_hash_table *table,
Packit fb9d21
			isl_int *ineq, unsigned len)
Packit fb9d21
{
Packit fb9d21
	uint32_t c_hash;
Packit fb9d21
	struct ineq_cmp_data v;
Packit fb9d21
	struct isl_hash_table_entry *entry;
Packit fb9d21
Packit fb9d21
	v.len = len;
Packit fb9d21
	v.p = ineq;
Packit fb9d21
	c_hash = isl_seq_get_hash(ineq + 1, len);
Packit fb9d21
	entry = isl_hash_table_find(ctx, table, c_hash, has_ineq, &v, 1);
Packit fb9d21
	if (!entry)
Packit fb9d21
		return - 1;
Packit fb9d21
	entry->data = ineq;
Packit fb9d21
	return 0;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Fill hash table "table" with the constraints of "bset".
Packit fb9d21
 * Equalities are added as two inequalities.
Packit fb9d21
 * The value in the hash table is a pointer to the (in)equality of "bset".
Packit fb9d21
 */
Packit fb9d21
static int hash_basic_set(struct isl_hash_table *table,
Packit fb9d21
				struct isl_basic_set *bset)
Packit fb9d21
{
Packit fb9d21
	int i, j;
Packit fb9d21
	unsigned dim = isl_basic_set_total_dim(bset);
Packit fb9d21
Packit fb9d21
	for (i = 0; i < bset->n_eq; ++i) {
Packit fb9d21
		for (j = 0; j < 2; ++j) {
Packit fb9d21
			isl_seq_neg(bset->eq[i], bset->eq[i], 1 + dim);
Packit fb9d21
			if (hash_ineq(bset->ctx, table, bset->eq[i], dim) < 0)
Packit fb9d21
				return -1;
Packit fb9d21
		}
Packit fb9d21
	}
Packit fb9d21
	for (i = 0; i < bset->n_ineq; ++i) {
Packit fb9d21
		if (hash_ineq(bset->ctx, table, bset->ineq[i], dim) < 0)
Packit fb9d21
			return -1;
Packit fb9d21
	}
Packit fb9d21
	return 0;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
static struct sh_data *sh_data_alloc(struct isl_set *set, unsigned n_ineq)
Packit fb9d21
{
Packit fb9d21
	struct sh_data *data;
Packit fb9d21
	int i;
Packit fb9d21
Packit fb9d21
	data = isl_calloc(set->ctx, struct sh_data,
Packit fb9d21
		sizeof(struct sh_data) +
Packit fb9d21
		(set->n - 1) * sizeof(struct sh_data_entry));
Packit fb9d21
	if (!data)
Packit fb9d21
		return NULL;
Packit fb9d21
	data->ctx = set->ctx;
Packit fb9d21
	data->n = set->n;
Packit fb9d21
	data->hull_table = isl_hash_table_alloc(set->ctx, n_ineq);
Packit fb9d21
	if (!data->hull_table)
Packit fb9d21
		goto error;
Packit fb9d21
	for (i = 0; i < set->n; ++i) {
Packit fb9d21
		data->p[i].table = isl_hash_table_alloc(set->ctx,
Packit fb9d21
				    2 * set->p[i]->n_eq + set->p[i]->n_ineq);
Packit fb9d21
		if (!data->p[i].table)
Packit fb9d21
			goto error;
Packit fb9d21
		if (hash_basic_set(data->p[i].table, set->p[i]) < 0)
Packit fb9d21
			goto error;
Packit fb9d21
	}
Packit fb9d21
	return data;
Packit fb9d21
error:
Packit fb9d21
	sh_data_free(data);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Check if inequality "ineq" is a bound for basic set "j" or if
Packit fb9d21
 * it can be relaxed (by increasing the constant term) to become
Packit fb9d21
 * a bound for that basic set.  In the latter case, the constant
Packit fb9d21
 * term is updated.
Packit fb9d21
 * Relaxation of the constant term is only allowed if "shift" is set.
Packit fb9d21
 *
Packit fb9d21
 * Return 1 if "ineq" is a bound
Packit fb9d21
 *	  0 if "ineq" may attain arbitrarily small values on basic set "j"
Packit fb9d21
 *	 -1 if some error occurred
Packit fb9d21
 */
Packit fb9d21
static int is_bound(struct sh_data *data, struct isl_set *set, int j,
Packit fb9d21
	isl_int *ineq, int shift)
Packit fb9d21
{
Packit fb9d21
	enum isl_lp_result res;
Packit fb9d21
	isl_int opt;
Packit fb9d21
Packit fb9d21
	if (!data->p[j].tab) {
Packit fb9d21
		data->p[j].tab = isl_tab_from_basic_set(set->p[j], 0);
Packit fb9d21
		if (!data->p[j].tab)
Packit fb9d21
			return -1;
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	isl_int_init(opt);
Packit fb9d21
Packit fb9d21
	res = isl_tab_min(data->p[j].tab, ineq, data->ctx->one,
Packit fb9d21
				&opt, NULL, 0);
Packit fb9d21
	if (res == isl_lp_ok && isl_int_is_neg(opt)) {
Packit fb9d21
		if (shift)
Packit fb9d21
			isl_int_sub(ineq[0], ineq[0], opt);
Packit fb9d21
		else
Packit fb9d21
			res = isl_lp_unbounded;
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	isl_int_clear(opt);
Packit fb9d21
Packit fb9d21
	return (res == isl_lp_ok || res == isl_lp_empty) ? 1 :
Packit fb9d21
	       res == isl_lp_unbounded ? 0 : -1;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Check if inequality "ineq" from basic set "i" is or can be relaxed to
Packit fb9d21
 * become a bound on the whole set.  If so, add the (relaxed) inequality
Packit fb9d21
 * to "hull".  Relaxation is only allowed if "shift" is set.
Packit fb9d21
 *
Packit fb9d21
 * We first check if "hull" already contains a translate of the inequality.
Packit fb9d21
 * If so, we are done.
Packit fb9d21
 * Then, we check if any of the previous basic sets contains a translate
Packit fb9d21
 * of the inequality.  If so, then we have already considered this
Packit fb9d21
 * inequality and we are done.
Packit fb9d21
 * Otherwise, for each basic set other than "i", we check if the inequality
Packit fb9d21
 * is a bound on the basic set.
Packit fb9d21
 * For previous basic sets, we know that they do not contain a translate
Packit fb9d21
 * of the inequality, so we directly call is_bound.
Packit fb9d21
 * For following basic sets, we first check if a translate of the
Packit fb9d21
 * inequality appears in its description and if so directly update
Packit fb9d21
 * the inequality accordingly.
Packit fb9d21
 */
Packit fb9d21
static struct isl_basic_set *add_bound(struct isl_basic_set *hull,
Packit fb9d21
	struct sh_data *data, struct isl_set *set, int i, isl_int *ineq,
Packit fb9d21
	int shift)
Packit fb9d21
{
Packit fb9d21
	uint32_t c_hash;
Packit fb9d21
	struct ineq_cmp_data v;
Packit fb9d21
	struct isl_hash_table_entry *entry;
Packit fb9d21
	int j, k;
Packit fb9d21
Packit fb9d21
	if (!hull)
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	v.len = isl_basic_set_total_dim(hull);
Packit fb9d21
	v.p = ineq;
Packit fb9d21
	c_hash = isl_seq_get_hash(ineq + 1, v.len);
Packit fb9d21
Packit fb9d21
	entry = isl_hash_table_find(hull->ctx, data->hull_table, c_hash,
Packit fb9d21
					has_ineq, &v, 0);
Packit fb9d21
	if (entry)
Packit fb9d21
		return hull;
Packit fb9d21
Packit fb9d21
	for (j = 0; j < i; ++j) {
Packit fb9d21
		entry = isl_hash_table_find(hull->ctx, data->p[j].table,
Packit fb9d21
						c_hash, has_ineq, &v, 0);
Packit fb9d21
		if (entry)
Packit fb9d21
			break;
Packit fb9d21
	}
Packit fb9d21
	if (j < i)
Packit fb9d21
		return hull;
Packit fb9d21
Packit fb9d21
	k = isl_basic_set_alloc_inequality(hull);
Packit fb9d21
	if (k < 0)
Packit fb9d21
		goto error;
Packit fb9d21
	isl_seq_cpy(hull->ineq[k], ineq, 1 + v.len);
Packit fb9d21
Packit fb9d21
	for (j = 0; j < i; ++j) {
Packit fb9d21
		int bound;
Packit fb9d21
		bound = is_bound(data, set, j, hull->ineq[k], shift);
Packit fb9d21
		if (bound < 0)
Packit fb9d21
			goto error;
Packit fb9d21
		if (!bound)
Packit fb9d21
			break;
Packit fb9d21
	}
Packit fb9d21
	if (j < i) {
Packit fb9d21
		isl_basic_set_free_inequality(hull, 1);
Packit fb9d21
		return hull;
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	for (j = i + 1; j < set->n; ++j) {
Packit fb9d21
		int bound, neg;
Packit fb9d21
		isl_int *ineq_j;
Packit fb9d21
		entry = isl_hash_table_find(hull->ctx, data->p[j].table,
Packit fb9d21
						c_hash, has_ineq, &v, 0);
Packit fb9d21
		if (entry) {
Packit fb9d21
			ineq_j = entry->data;
Packit fb9d21
			neg = isl_seq_is_neg(ineq_j + 1,
Packit fb9d21
					     hull->ineq[k] + 1, v.len);
Packit fb9d21
			if (neg)
Packit fb9d21
				isl_int_neg(ineq_j[0], ineq_j[0]);
Packit fb9d21
			if (isl_int_gt(ineq_j[0], hull->ineq[k][0]))
Packit fb9d21
				isl_int_set(hull->ineq[k][0], ineq_j[0]);
Packit fb9d21
			if (neg)
Packit fb9d21
				isl_int_neg(ineq_j[0], ineq_j[0]);
Packit fb9d21
			continue;
Packit fb9d21
		}
Packit fb9d21
		bound = is_bound(data, set, j, hull->ineq[k], shift);
Packit fb9d21
		if (bound < 0)
Packit fb9d21
			goto error;
Packit fb9d21
		if (!bound)
Packit fb9d21
			break;
Packit fb9d21
	}
Packit fb9d21
	if (j < set->n) {
Packit fb9d21
		isl_basic_set_free_inequality(hull, 1);
Packit fb9d21
		return hull;
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	entry = isl_hash_table_find(hull->ctx, data->hull_table, c_hash,
Packit fb9d21
					has_ineq, &v, 1);
Packit fb9d21
	if (!entry)
Packit fb9d21
		goto error;
Packit fb9d21
	entry->data = hull->ineq[k];
Packit fb9d21
Packit fb9d21
	return hull;
Packit fb9d21
error:
Packit fb9d21
	isl_basic_set_free(hull);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Check if any inequality from basic set "i" is or can be relaxed to
Packit fb9d21
 * become a bound on the whole set.  If so, add the (relaxed) inequality
Packit fb9d21
 * to "hull".  Relaxation is only allowed if "shift" is set.
Packit fb9d21
 */
Packit fb9d21
static struct isl_basic_set *add_bounds(struct isl_basic_set *bset,
Packit fb9d21
	struct sh_data *data, struct isl_set *set, int i, int shift)
Packit fb9d21
{
Packit fb9d21
	int j, k;
Packit fb9d21
	unsigned dim = isl_basic_set_total_dim(bset);
Packit fb9d21
Packit fb9d21
	for (j = 0; j < set->p[i]->n_eq; ++j) {
Packit fb9d21
		for (k = 0; k < 2; ++k) {
Packit fb9d21
			isl_seq_neg(set->p[i]->eq[j], set->p[i]->eq[j], 1+dim);
Packit fb9d21
			bset = add_bound(bset, data, set, i, set->p[i]->eq[j],
Packit fb9d21
					    shift);
Packit fb9d21
		}
Packit fb9d21
	}
Packit fb9d21
	for (j = 0; j < set->p[i]->n_ineq; ++j)
Packit fb9d21
		bset = add_bound(bset, data, set, i, set->p[i]->ineq[j], shift);
Packit fb9d21
	return bset;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Compute a superset of the convex hull of set that is described
Packit fb9d21
 * by only (translates of) the constraints in the constituents of set.
Packit fb9d21
 * Translation is only allowed if "shift" is set.
Packit fb9d21
 */
Packit fb9d21
static __isl_give isl_basic_set *uset_simple_hull(__isl_take isl_set *set,
Packit fb9d21
	int shift)
Packit fb9d21
{
Packit fb9d21
	struct sh_data *data = NULL;
Packit fb9d21
	struct isl_basic_set *hull = NULL;
Packit fb9d21
	unsigned n_ineq;
Packit fb9d21
	int i;
Packit fb9d21
Packit fb9d21
	if (!set)
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	n_ineq = 0;
Packit fb9d21
	for (i = 0; i < set->n; ++i) {
Packit fb9d21
		if (!set->p[i])
Packit fb9d21
			goto error;
Packit fb9d21
		n_ineq += 2 * set->p[i]->n_eq + set->p[i]->n_ineq;
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	hull = isl_basic_set_alloc_space(isl_space_copy(set->dim), 0, 0, n_ineq);
Packit fb9d21
	if (!hull)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	data = sh_data_alloc(set, n_ineq);
Packit fb9d21
	if (!data)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	for (i = 0; i < set->n; ++i)
Packit fb9d21
		hull = add_bounds(hull, data, set, i, shift);
Packit fb9d21
Packit fb9d21
	sh_data_free(data);
Packit fb9d21
	isl_set_free(set);
Packit fb9d21
Packit fb9d21
	return hull;
Packit fb9d21
error:
Packit fb9d21
	sh_data_free(data);
Packit fb9d21
	isl_basic_set_free(hull);
Packit fb9d21
	isl_set_free(set);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Compute a superset of the convex hull of map that is described
Packit fb9d21
 * by only (translates of) the constraints in the constituents of map.
Packit fb9d21
 * Translation is only allowed if "shift" is set.
Packit fb9d21
 */
Packit fb9d21
static __isl_give isl_basic_map *map_simple_hull(__isl_take isl_map *map,
Packit fb9d21
	int shift)
Packit fb9d21
{
Packit fb9d21
	struct isl_set *set = NULL;
Packit fb9d21
	struct isl_basic_map *model = NULL;
Packit fb9d21
	struct isl_basic_map *hull;
Packit fb9d21
	struct isl_basic_map *affine_hull;
Packit fb9d21
	struct isl_basic_set *bset = NULL;
Packit fb9d21
Packit fb9d21
	if (!map)
Packit fb9d21
		return NULL;
Packit fb9d21
	if (map->n == 0) {
Packit fb9d21
		hull = isl_basic_map_empty_like_map(map);
Packit fb9d21
		isl_map_free(map);
Packit fb9d21
		return hull;
Packit fb9d21
	}
Packit fb9d21
	if (map->n == 1) {
Packit fb9d21
		hull = isl_basic_map_copy(map->p[0]);
Packit fb9d21
		isl_map_free(map);
Packit fb9d21
		return hull;
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	map = isl_map_detect_equalities(map);
Packit fb9d21
	affine_hull = isl_map_affine_hull(isl_map_copy(map));
Packit fb9d21
	map = isl_map_align_divs(map);
Packit fb9d21
	model = map ? isl_basic_map_copy(map->p[0]) : NULL;
Packit fb9d21
Packit fb9d21
	set = isl_map_underlying_set(map);
Packit fb9d21
Packit fb9d21
	bset = uset_simple_hull(set, shift);
Packit fb9d21
Packit fb9d21
	hull = isl_basic_map_overlying_set(bset, model);
Packit fb9d21
Packit fb9d21
	hull = isl_basic_map_intersect(hull, affine_hull);
Packit fb9d21
	hull = isl_basic_map_remove_redundancies(hull);
Packit fb9d21
Packit fb9d21
	if (!hull)
Packit fb9d21
		return NULL;
Packit fb9d21
	ISL_F_SET(hull, ISL_BASIC_MAP_NO_IMPLICIT);
Packit fb9d21
	ISL_F_SET(hull, ISL_BASIC_MAP_ALL_EQUALITIES);
Packit fb9d21
Packit fb9d21
	hull = isl_basic_map_finalize(hull);
Packit fb9d21
Packit fb9d21
	return hull;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Compute a superset of the convex hull of map that is described
Packit fb9d21
 * by only translates of the constraints in the constituents of map.
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_basic_map *isl_map_simple_hull(__isl_take isl_map *map)
Packit fb9d21
{
Packit fb9d21
	return map_simple_hull(map, 1);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
struct isl_basic_set *isl_set_simple_hull(struct isl_set *set)
Packit fb9d21
{
Packit fb9d21
	return (struct isl_basic_set *)
Packit fb9d21
		isl_map_simple_hull((struct isl_map *)set);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Compute a superset of the convex hull of map that is described
Packit fb9d21
 * by only the constraints in the constituents of map.
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_basic_map *isl_map_unshifted_simple_hull(
Packit fb9d21
	__isl_take isl_map *map)
Packit fb9d21
{
Packit fb9d21
	return map_simple_hull(map, 0);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
__isl_give isl_basic_set *isl_set_unshifted_simple_hull(
Packit fb9d21
	__isl_take isl_set *set)
Packit fb9d21
{
Packit fb9d21
	return isl_map_unshifted_simple_hull(set);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Check if "ineq" is a bound on "set" and, if so, add it to "hull".
Packit fb9d21
 *
Packit fb9d21
 * For each basic set in "set", we first check if the basic set
Packit fb9d21
 * contains a translate of "ineq".  If this translate is more relaxed,
Packit fb9d21
 * then we assume that "ineq" is not a bound on this basic set.
Packit fb9d21
 * Otherwise, we know that it is a bound.
Packit fb9d21
 * If the basic set does not contain a translate of "ineq", then
Packit fb9d21
 * we call is_bound to perform the test.
Packit fb9d21
 */
Packit fb9d21
static __isl_give isl_basic_set *add_bound_from_constraint(
Packit fb9d21
	__isl_take isl_basic_set *hull, struct sh_data *data,
Packit fb9d21
	__isl_keep isl_set *set, isl_int *ineq)
Packit fb9d21
{
Packit fb9d21
	int i, k;
Packit fb9d21
	isl_ctx *ctx;
Packit fb9d21
	uint32_t c_hash;
Packit fb9d21
	struct ineq_cmp_data v;
Packit fb9d21
Packit fb9d21
	if (!hull || !set)
Packit fb9d21
		return isl_basic_set_free(hull);
Packit fb9d21
Packit fb9d21
	v.len = isl_basic_set_total_dim(hull);
Packit fb9d21
	v.p = ineq;
Packit fb9d21
	c_hash = isl_seq_get_hash(ineq + 1, v.len);
Packit fb9d21
Packit fb9d21
	ctx = isl_basic_set_get_ctx(hull);
Packit fb9d21
	for (i = 0; i < set->n; ++i) {
Packit fb9d21
		int bound;
Packit fb9d21
		struct isl_hash_table_entry *entry;
Packit fb9d21
Packit fb9d21
		entry = isl_hash_table_find(ctx, data->p[i].table,
Packit fb9d21
						c_hash, &has_ineq, &v, 0);
Packit fb9d21
		if (entry) {
Packit fb9d21
			isl_int *ineq_i = entry->data;
Packit fb9d21
			int neg, more_relaxed;
Packit fb9d21
Packit fb9d21
			neg = isl_seq_is_neg(ineq_i + 1, ineq + 1, v.len);
Packit fb9d21
			if (neg)
Packit fb9d21
				isl_int_neg(ineq_i[0], ineq_i[0]);
Packit fb9d21
			more_relaxed = isl_int_gt(ineq_i[0], ineq[0]);
Packit fb9d21
			if (neg)
Packit fb9d21
				isl_int_neg(ineq_i[0], ineq_i[0]);
Packit fb9d21
			if (more_relaxed)
Packit fb9d21
				break;
Packit fb9d21
			else
Packit fb9d21
				continue;
Packit fb9d21
		}
Packit fb9d21
		bound = is_bound(data, set, i, ineq, 0);
Packit fb9d21
		if (bound < 0)
Packit fb9d21
			return isl_basic_set_free(hull);
Packit fb9d21
		if (!bound)
Packit fb9d21
			break;
Packit fb9d21
	}
Packit fb9d21
	if (i < set->n)
Packit fb9d21
		return hull;
Packit fb9d21
Packit fb9d21
	k = isl_basic_set_alloc_inequality(hull);
Packit fb9d21
	if (k < 0)
Packit fb9d21
		return isl_basic_set_free(hull);
Packit fb9d21
	isl_seq_cpy(hull->ineq[k], ineq, 1 + v.len);
Packit fb9d21
Packit fb9d21
	return hull;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Compute a superset of the convex hull of "set" that is described
Packit fb9d21
 * by only some of the "n_ineq" constraints in the list "ineq", where "set"
Packit fb9d21
 * has no parameters or integer divisions.
Packit fb9d21
 *
Packit fb9d21
 * The inequalities in "ineq" are assumed to have been sorted such
Packit fb9d21
 * that constraints with the same linear part appear together and
Packit fb9d21
 * that among constraints with the same linear part, those with
Packit fb9d21
 * smaller constant term appear first.
Packit fb9d21
 *
Packit fb9d21
 * We reuse the same data structure that is used by uset_simple_hull,
Packit fb9d21
 * but we do not need the hull table since we will not consider the
Packit fb9d21
 * same constraint more than once.  We therefore allocate it with zero size.
Packit fb9d21
 *
Packit fb9d21
 * We run through the constraints and try to add them one by one,
Packit fb9d21
 * skipping identical constraints.  If we have added a constraint and
Packit fb9d21
 * the next constraint is a more relaxed translate, then we skip this
Packit fb9d21
 * next constraint as well.
Packit fb9d21
 */
Packit fb9d21
static __isl_give isl_basic_set *uset_unshifted_simple_hull_from_constraints(
Packit fb9d21
	__isl_take isl_set *set, int n_ineq, isl_int **ineq)
Packit fb9d21
{
Packit fb9d21
	int i;
Packit fb9d21
	int last_added = 0;
Packit fb9d21
	struct sh_data *data = NULL;
Packit fb9d21
	isl_basic_set *hull = NULL;
Packit fb9d21
	unsigned dim;
Packit fb9d21
Packit fb9d21
	hull = isl_basic_set_alloc_space(isl_set_get_space(set), 0, 0, n_ineq);
Packit fb9d21
	if (!hull)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	data = sh_data_alloc(set, 0);
Packit fb9d21
	if (!data)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	dim = isl_set_dim(set, isl_dim_set);
Packit fb9d21
	for (i = 0; i < n_ineq; ++i) {
Packit fb9d21
		int hull_n_ineq = hull->n_ineq;
Packit fb9d21
		int parallel;
Packit fb9d21
Packit fb9d21
		parallel = i > 0 && isl_seq_eq(ineq[i - 1] + 1, ineq[i] + 1,
Packit fb9d21
						dim);
Packit fb9d21
		if (parallel &&
Packit fb9d21
		    (last_added || isl_int_eq(ineq[i - 1][0], ineq[i][0])))
Packit fb9d21
			continue;
Packit fb9d21
		hull = add_bound_from_constraint(hull, data, set, ineq[i]);
Packit fb9d21
		if (!hull)
Packit fb9d21
			goto error;
Packit fb9d21
		last_added = hull->n_ineq > hull_n_ineq;
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	sh_data_free(data);
Packit fb9d21
	isl_set_free(set);
Packit fb9d21
	return hull;
Packit fb9d21
error:
Packit fb9d21
	sh_data_free(data);
Packit fb9d21
	isl_set_free(set);
Packit fb9d21
	isl_basic_set_free(hull);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Collect pointers to all the inequalities in the elements of "list"
Packit fb9d21
 * in "ineq".  For equalities, store both a pointer to the equality and
Packit fb9d21
 * a pointer to its opposite, which is first copied to "mat".
Packit fb9d21
 * "ineq" and "mat" are assumed to have been preallocated to the right size
Packit fb9d21
 * (the number of inequalities + 2 times the number of equalites and
Packit fb9d21
 * the number of equalities, respectively).
Packit fb9d21
 */
Packit fb9d21
static __isl_give isl_mat *collect_inequalities(__isl_take isl_mat *mat,
Packit fb9d21
	__isl_keep isl_basic_set_list *list, isl_int **ineq)
Packit fb9d21
{
Packit fb9d21
	int i, j, n, n_eq, n_ineq;
Packit fb9d21
Packit fb9d21
	if (!mat)
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	n_eq = 0;
Packit fb9d21
	n_ineq = 0;
Packit fb9d21
	n = isl_basic_set_list_n_basic_set(list);
Packit fb9d21
	for (i = 0; i < n; ++i) {
Packit fb9d21
		isl_basic_set *bset;
Packit fb9d21
		bset = isl_basic_set_list_get_basic_set(list, i);
Packit fb9d21
		if (!bset)
Packit fb9d21
			return isl_mat_free(mat);
Packit fb9d21
		for (j = 0; j < bset->n_eq; ++j) {
Packit fb9d21
			ineq[n_ineq++] = mat->row[n_eq];
Packit fb9d21
			ineq[n_ineq++] = bset->eq[j];
Packit fb9d21
			isl_seq_neg(mat->row[n_eq++], bset->eq[j], mat->n_col);
Packit fb9d21
		}
Packit fb9d21
		for (j = 0; j < bset->n_ineq; ++j)
Packit fb9d21
			ineq[n_ineq++] = bset->ineq[j];
Packit fb9d21
		isl_basic_set_free(bset);
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	return mat;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Comparison routine for use as an isl_sort callback.
Packit fb9d21
 *
Packit fb9d21
 * Constraints with the same linear part are sorted together and
Packit fb9d21
 * among constraints with the same linear part, those with smaller
Packit fb9d21
 * constant term are sorted first.
Packit fb9d21
 */
Packit fb9d21
static int cmp_ineq(const void *a, const void *b, void *arg)
Packit fb9d21
{
Packit fb9d21
	unsigned dim = *(unsigned *) arg;
Packit fb9d21
	isl_int * const *ineq1 = a;
Packit fb9d21
	isl_int * const *ineq2 = b;
Packit fb9d21
	int cmp;
Packit fb9d21
Packit fb9d21
	cmp = isl_seq_cmp((*ineq1) + 1, (*ineq2) + 1, dim);
Packit fb9d21
	if (cmp != 0)
Packit fb9d21
		return cmp;
Packit fb9d21
	return isl_int_cmp((*ineq1)[0], (*ineq2)[0]);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Compute a superset of the convex hull of "set" that is described
Packit fb9d21
 * by only constraints in the elements of "list", where "set" has
Packit fb9d21
 * no parameters or integer divisions.
Packit fb9d21
 *
Packit fb9d21
 * We collect all the constraints in those elements and then
Packit fb9d21
 * sort the constraints such that constraints with the same linear part
Packit fb9d21
 * are sorted together and that those with smaller constant term are
Packit fb9d21
 * sorted first.
Packit fb9d21
 */
Packit fb9d21
static __isl_give isl_basic_set *uset_unshifted_simple_hull_from_basic_set_list(
Packit fb9d21
	__isl_take isl_set *set, __isl_take isl_basic_set_list *list)
Packit fb9d21
{
Packit fb9d21
	int i, n, n_eq, n_ineq;
Packit fb9d21
	unsigned dim;
Packit fb9d21
	isl_ctx *ctx;
Packit fb9d21
	isl_mat *mat = NULL;
Packit fb9d21
	isl_int **ineq = NULL;
Packit fb9d21
	isl_basic_set *hull;
Packit fb9d21
Packit fb9d21
	if (!set)
Packit fb9d21
		goto error;
Packit fb9d21
	ctx = isl_set_get_ctx(set);
Packit fb9d21
Packit fb9d21
	n_eq = 0;
Packit fb9d21
	n_ineq = 0;
Packit fb9d21
	n = isl_basic_set_list_n_basic_set(list);
Packit fb9d21
	for (i = 0; i < n; ++i) {
Packit fb9d21
		isl_basic_set *bset;
Packit fb9d21
		bset = isl_basic_set_list_get_basic_set(list, i);
Packit fb9d21
		if (!bset)
Packit fb9d21
			goto error;
Packit fb9d21
		n_eq += bset->n_eq;
Packit fb9d21
		n_ineq += 2 * bset->n_eq + bset->n_ineq;
Packit fb9d21
		isl_basic_set_free(bset);
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	ineq = isl_alloc_array(ctx, isl_int *, n_ineq);
Packit fb9d21
	if (n_ineq > 0 && !ineq)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	dim = isl_set_dim(set, isl_dim_set);
Packit fb9d21
	mat = isl_mat_alloc(ctx, n_eq, 1 + dim);
Packit fb9d21
	mat = collect_inequalities(mat, list, ineq);
Packit fb9d21
	if (!mat)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	if (isl_sort(ineq, n_ineq, sizeof(ineq[0]), &cmp_ineq, &dim) < 0)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	hull = uset_unshifted_simple_hull_from_constraints(set, n_ineq, ineq);
Packit fb9d21
Packit fb9d21
	isl_mat_free(mat);
Packit fb9d21
	free(ineq);
Packit fb9d21
	isl_basic_set_list_free(list);
Packit fb9d21
	return hull;
Packit fb9d21
error:
Packit fb9d21
	isl_mat_free(mat);
Packit fb9d21
	free(ineq);
Packit fb9d21
	isl_set_free(set);
Packit fb9d21
	isl_basic_set_list_free(list);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Compute a superset of the convex hull of "set" that is described
Packit fb9d21
 * by only constraints in the elements of "list".
Packit fb9d21
 *
Packit fb9d21
 * If the list is empty, then we can only describe the universe set.
Packit fb9d21
 * If the input set is empty, then all constraints are valid, so
Packit fb9d21
 * we return the intersection of the elements in "list".
Packit fb9d21
 *
Packit fb9d21
 * Otherwise, we align all divs and temporarily treat them
Packit fb9d21
 * as regular variables, computing the unshifted simple hull in
Packit fb9d21
 * uset_unshifted_simple_hull_from_basic_set_list.
Packit fb9d21
 */
Packit fb9d21
static __isl_give isl_basic_set *set_unshifted_simple_hull_from_basic_set_list(
Packit fb9d21
	__isl_take isl_set *set, __isl_take isl_basic_set_list *list)
Packit fb9d21
{
Packit fb9d21
	isl_basic_set *model;
Packit fb9d21
	isl_basic_set *hull;
Packit fb9d21
Packit fb9d21
	if (!set || !list)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	if (isl_basic_set_list_n_basic_set(list) == 0) {
Packit fb9d21
		isl_space *space;
Packit fb9d21
Packit fb9d21
		space = isl_set_get_space(set);
Packit fb9d21
		isl_set_free(set);
Packit fb9d21
		isl_basic_set_list_free(list);
Packit fb9d21
		return isl_basic_set_universe(space);
Packit fb9d21
	}
Packit fb9d21
	if (isl_set_plain_is_empty(set)) {
Packit fb9d21
		isl_set_free(set);
Packit fb9d21
		return isl_basic_set_list_intersect(list);
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	set = isl_set_align_divs_to_basic_set_list(set, list);
Packit fb9d21
	if (!set)
Packit fb9d21
		goto error;
Packit fb9d21
	list = isl_basic_set_list_align_divs_to_basic_set(list, set->p[0]);
Packit fb9d21
Packit fb9d21
	model = isl_basic_set_list_get_basic_set(list, 0);
Packit fb9d21
Packit fb9d21
	set = isl_set_to_underlying_set(set);
Packit fb9d21
	list = isl_basic_set_list_underlying_set(list);
Packit fb9d21
Packit fb9d21
	hull = uset_unshifted_simple_hull_from_basic_set_list(set, list);
Packit fb9d21
	hull = isl_basic_map_overlying_set(hull, model);
Packit fb9d21
Packit fb9d21
	return hull;
Packit fb9d21
error:
Packit fb9d21
	isl_set_free(set);
Packit fb9d21
	isl_basic_set_list_free(list);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Return a sequence of the basic sets that make up the sets in "list".
Packit fb9d21
 */
Packit fb9d21
static __isl_give isl_basic_set_list *collect_basic_sets(
Packit fb9d21
	__isl_take isl_set_list *list)
Packit fb9d21
{
Packit fb9d21
	int i, n;
Packit fb9d21
	isl_ctx *ctx;
Packit fb9d21
	isl_basic_set_list *bset_list;
Packit fb9d21
Packit fb9d21
	if (!list)
Packit fb9d21
		return NULL;
Packit fb9d21
	n = isl_set_list_n_set(list);
Packit fb9d21
	ctx = isl_set_list_get_ctx(list);
Packit fb9d21
	bset_list = isl_basic_set_list_alloc(ctx, 0);
Packit fb9d21
Packit fb9d21
	for (i = 0; i < n; ++i) {
Packit fb9d21
		isl_set *set;
Packit fb9d21
		isl_basic_set_list *list_i;
Packit fb9d21
Packit fb9d21
		set = isl_set_list_get_set(list, i);
Packit fb9d21
		set = isl_set_compute_divs(set);
Packit fb9d21
		list_i = isl_set_get_basic_set_list(set);
Packit fb9d21
		isl_set_free(set);
Packit fb9d21
		bset_list = isl_basic_set_list_concat(bset_list, list_i);
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	isl_set_list_free(list);
Packit fb9d21
	return bset_list;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Compute a superset of the convex hull of "set" that is described
Packit fb9d21
 * by only constraints in the elements of "list".
Packit fb9d21
 *
Packit fb9d21
 * If "set" is the universe, then the convex hull (and therefore
Packit fb9d21
 * any superset of the convexhull) is the universe as well.
Packit fb9d21
 *
Packit fb9d21
 * Otherwise, we collect all the basic sets in the set list and
Packit fb9d21
 * continue with set_unshifted_simple_hull_from_basic_set_list.
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_basic_set *isl_set_unshifted_simple_hull_from_set_list(
Packit fb9d21
	__isl_take isl_set *set, __isl_take isl_set_list *list)
Packit fb9d21
{
Packit fb9d21
	isl_basic_set_list *bset_list;
Packit fb9d21
	int is_universe;
Packit fb9d21
Packit fb9d21
	is_universe = isl_set_plain_is_universe(set);
Packit fb9d21
	if (is_universe < 0)
Packit fb9d21
		set = isl_set_free(set);
Packit fb9d21
	if (is_universe < 0 || is_universe) {
Packit fb9d21
		isl_set_list_free(list);
Packit fb9d21
		return isl_set_unshifted_simple_hull(set);
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	bset_list = collect_basic_sets(list);
Packit fb9d21
	return set_unshifted_simple_hull_from_basic_set_list(set, bset_list);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Given a set "set", return parametric bounds on the dimension "dim".
Packit fb9d21
 */
Packit fb9d21
static struct isl_basic_set *set_bounds(struct isl_set *set, int dim)
Packit fb9d21
{
Packit fb9d21
	unsigned set_dim = isl_set_dim(set, isl_dim_set);
Packit fb9d21
	set = isl_set_copy(set);
Packit fb9d21
	set = isl_set_eliminate_dims(set, dim + 1, set_dim - (dim + 1));
Packit fb9d21
	set = isl_set_eliminate_dims(set, 0, dim);
Packit fb9d21
	return isl_set_convex_hull(set);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Computes a "simple hull" and then check if each dimension in the
Packit fb9d21
 * resulting hull is bounded by a symbolic constant.  If not, the
Packit fb9d21
 * hull is intersected with the corresponding bounds on the whole set.
Packit fb9d21
 */
Packit fb9d21
struct isl_basic_set *isl_set_bounded_simple_hull(struct isl_set *set)
Packit fb9d21
{
Packit fb9d21
	int i, j;
Packit fb9d21
	struct isl_basic_set *hull;
Packit fb9d21
	unsigned nparam, left;
Packit fb9d21
	int removed_divs = 0;
Packit fb9d21
Packit fb9d21
	hull = isl_set_simple_hull(isl_set_copy(set));
Packit fb9d21
	if (!hull)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	nparam = isl_basic_set_dim(hull, isl_dim_param);
Packit fb9d21
	for (i = 0; i < isl_basic_set_dim(hull, isl_dim_set); ++i) {
Packit fb9d21
		int lower = 0, upper = 0;
Packit fb9d21
		struct isl_basic_set *bounds;
Packit fb9d21
Packit fb9d21
		left = isl_basic_set_total_dim(hull) - nparam - i - 1;
Packit fb9d21
		for (j = 0; j < hull->n_eq; ++j) {
Packit fb9d21
			if (isl_int_is_zero(hull->eq[j][1 + nparam + i]))
Packit fb9d21
				continue;
Packit fb9d21
			if (isl_seq_first_non_zero(hull->eq[j]+1+nparam+i+1,
Packit fb9d21
						    left) == -1)
Packit fb9d21
				break;
Packit fb9d21
		}
Packit fb9d21
		if (j < hull->n_eq)
Packit fb9d21
			continue;
Packit fb9d21
Packit fb9d21
		for (j = 0; j < hull->n_ineq; ++j) {
Packit fb9d21
			if (isl_int_is_zero(hull->ineq[j][1 + nparam + i]))
Packit fb9d21
				continue;
Packit fb9d21
			if (isl_seq_first_non_zero(hull->ineq[j]+1+nparam+i+1,
Packit fb9d21
						    left) != -1 ||
Packit fb9d21
			    isl_seq_first_non_zero(hull->ineq[j]+1+nparam,
Packit fb9d21
						    i) != -1)
Packit fb9d21
				continue;
Packit fb9d21
			if (isl_int_is_pos(hull->ineq[j][1 + nparam + i]))
Packit fb9d21
				lower = 1;
Packit fb9d21
			else
Packit fb9d21
				upper = 1;
Packit fb9d21
			if (lower && upper)
Packit fb9d21
				break;
Packit fb9d21
		}
Packit fb9d21
Packit fb9d21
		if (lower && upper)
Packit fb9d21
			continue;
Packit fb9d21
Packit fb9d21
		if (!removed_divs) {
Packit fb9d21
			set = isl_set_remove_divs(set);
Packit fb9d21
			if (!set)
Packit fb9d21
				goto error;
Packit fb9d21
			removed_divs = 1;
Packit fb9d21
		}
Packit fb9d21
		bounds = set_bounds(set, i);
Packit fb9d21
		hull = isl_basic_set_intersect(hull, bounds);
Packit fb9d21
		if (!hull)
Packit fb9d21
			goto error;
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	isl_set_free(set);
Packit fb9d21
	return hull;
Packit fb9d21
error:
Packit fb9d21
	isl_set_free(set);
Packit fb9d21
	return NULL;
Packit fb9d21
}