Blame isl-0.16.1/isl_union_multi.c

Packit fb9d21
/*
Packit fb9d21
 * Copyright 2010      INRIA Saclay
Packit fb9d21
 * Copyright 2013      Ecole Normale Superieure
Packit fb9d21
 * Copyright 2015      INRIA Paris-Rocquencourt
Packit fb9d21
 *
Packit fb9d21
 * Use of this software is governed by the MIT license
Packit fb9d21
 *
Packit fb9d21
 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
Packit fb9d21
 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
Packit fb9d21
 * 91893 Orsay, France
Packit fb9d21
 * and Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
Packit fb9d21
 * and INRIA Paris-Rocquencourt, Domaine de Voluceau, Rocquenqourt, B.P. 105,
Packit fb9d21
 * 78153 Le Chesnay Cedex France
Packit fb9d21
 */
Packit fb9d21
Packit fb9d21
#include <isl_hash_private.h>
Packit fb9d21
#include <isl_union_macro.h>
Packit fb9d21
Packit fb9d21
/* A group of expressions defined over the same domain space "domain_space".
Packit fb9d21
 * The entries of "part_table" are the individual expressions,
Packit fb9d21
 * keyed on the entire space of the expression.
Packit fb9d21
 *
Packit fb9d21
 * Each UNION has its own groups, so there can only ever be a single
Packit fb9d21
 * reference to each group.
Packit fb9d21
 */
Packit fb9d21
S(UNION,group) {
Packit fb9d21
	isl_space *domain_space;
Packit fb9d21
	struct isl_hash_table	part_table;
Packit fb9d21
};
Packit fb9d21
Packit fb9d21
/* A union of expressions defined over different disjoint domains.
Packit fb9d21
 * "space" describes the parameters.
Packit fb9d21
 * The entries of "table" are keyed on the domain space of the entry and
Packit fb9d21
 * contain groups of expressions that are defined over the same domain space.
Packit fb9d21
 */
Packit fb9d21
struct UNION {
Packit fb9d21
	int ref;
Packit fb9d21
	isl_space *space;
Packit fb9d21
Packit fb9d21
	struct isl_hash_table	table;
Packit fb9d21
};
Packit fb9d21
Packit fb9d21
/* Internal data structure for isl_union_*_foreach_group.
Packit fb9d21
 * "fn" is the function that needs to be called on each group.
Packit fb9d21
 */
Packit fb9d21
S(UNION,foreach_group_data)
Packit fb9d21
{
Packit fb9d21
	isl_stat (*fn)(__isl_keep S(UNION,group) *group, void *user);
Packit fb9d21
	void *user;
Packit fb9d21
};
Packit fb9d21
Packit fb9d21
/* Call data->fn on the group stored at *entry.
Packit fb9d21
 */
Packit fb9d21
static isl_stat FN(UNION,call_on_group)(void **entry, void *user)
Packit fb9d21
{
Packit fb9d21
	S(UNION,group) *group = *entry;
Packit fb9d21
	S(UNION,foreach_group_data) *data;
Packit fb9d21
Packit fb9d21
	data = (S(UNION,foreach_group_data) *) user;
Packit fb9d21
	return data->fn(group, data->user);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Call "fn" on each group of expressions in "u".
Packit fb9d21
 */
Packit fb9d21
static isl_stat FN(UNION,foreach_group)(__isl_keep UNION *u,
Packit fb9d21
	isl_stat (*fn)(__isl_keep S(UNION,group) *group, void *user),
Packit fb9d21
	void *user)
Packit fb9d21
{
Packit fb9d21
	S(UNION,foreach_group_data) data = { fn, user };
Packit fb9d21
Packit fb9d21
	if (!u)
Packit fb9d21
		return isl_stat_error;
Packit fb9d21
Packit fb9d21
	return isl_hash_table_foreach(u->space->ctx, &u->table,
Packit fb9d21
				      &FN(UNION,call_on_group), &data);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* A isl_union_*_foreach_group callback for counting the total number
Packit fb9d21
 * of expressions in a UNION.  Add the number of expressions in "group"
Packit fb9d21
 * to *n.
Packit fb9d21
 */
Packit fb9d21
static isl_stat FN(UNION,count_part)(__isl_keep S(UNION,group) *group,
Packit fb9d21
	void *user)
Packit fb9d21
{
Packit fb9d21
	int *n = user;
Packit fb9d21
Packit fb9d21
	if (!group)
Packit fb9d21
		return isl_stat_error;
Packit fb9d21
Packit fb9d21
	*n += group->part_table.n;
Packit fb9d21
	return isl_stat_ok;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Return the number of base expressions in "u".
Packit fb9d21
 */
Packit fb9d21
int FN(FN(UNION,n),PARTS)(__isl_keep UNION *u)
Packit fb9d21
{
Packit fb9d21
	int n;
Packit fb9d21
Packit fb9d21
	n = 0;
Packit fb9d21
	if (FN(UNION,foreach_group)(u, &FN(UNION,count_part), &n) < 0)
Packit fb9d21
		n = -1;
Packit fb9d21
	return n;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Free an entry in a group of expressions.
Packit fb9d21
 * Each entry in such a group is a single expression.
Packit fb9d21
 */
Packit fb9d21
static isl_stat FN(UNION,free_group_entry)(void **entry, void *user)
Packit fb9d21
{
Packit fb9d21
	PART *part = *entry;
Packit fb9d21
Packit fb9d21
	FN(PART,free)(part);
Packit fb9d21
	return isl_stat_ok;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Free all memory allocated for "group" and return NULL.
Packit fb9d21
 */
Packit fb9d21
static __isl_null S(UNION,group) *FN(UNION,group_free)(
Packit fb9d21
	__isl_take S(UNION,group) *group)
Packit fb9d21
{
Packit fb9d21
	isl_ctx *ctx;
Packit fb9d21
Packit fb9d21
	if (!group)
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	ctx = isl_space_get_ctx(group->domain_space);
Packit fb9d21
	isl_hash_table_foreach(ctx, &group->part_table,
Packit fb9d21
				&FN(UNION,free_group_entry), NULL);
Packit fb9d21
	isl_hash_table_clear(&group->part_table);
Packit fb9d21
	isl_space_free(group->domain_space);
Packit fb9d21
	free(group);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Allocate a group of expressions defined over the same domain space
Packit fb9d21
 * with domain space "domain_space" and initial size "size".
Packit fb9d21
 */
Packit fb9d21
static __isl_give S(UNION,group) *FN(UNION,group_alloc)(
Packit fb9d21
	__isl_take isl_space *domain_space, int size)
Packit fb9d21
{
Packit fb9d21
	isl_ctx *ctx;
Packit fb9d21
	S(UNION,group) *group;
Packit fb9d21
Packit fb9d21
	if (!domain_space)
Packit fb9d21
		return NULL;
Packit fb9d21
	ctx = isl_space_get_ctx(domain_space);
Packit fb9d21
	group = isl_calloc_type(ctx, S(UNION,group));
Packit fb9d21
	if (!group)
Packit fb9d21
		goto error;
Packit fb9d21
	group->domain_space = domain_space;
Packit fb9d21
	if (isl_hash_table_init(ctx, &group->part_table, size) < 0)
Packit fb9d21
		return FN(UNION,group_free)(group);
Packit fb9d21
Packit fb9d21
	return group;
Packit fb9d21
error:
Packit fb9d21
	isl_space_free(domain_space);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Is the space of "entry" equal to "space"?
Packit fb9d21
 */
Packit fb9d21
static int FN(UNION,has_space)(const void *entry, const void *val)
Packit fb9d21
{
Packit fb9d21
	PART *part = (PART *) entry;
Packit fb9d21
	isl_space *space = (isl_space *) val;
Packit fb9d21
Packit fb9d21
	return isl_space_is_equal(part->dim, space);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Return a group equal to "group", but with a single reference.
Packit fb9d21
 * Since all groups have only a single reference, simply return "group".
Packit fb9d21
 */
Packit fb9d21
static __isl_give S(UNION,group) *FN(UNION,group_cow)(
Packit fb9d21
	__isl_take S(UNION,group) *group)
Packit fb9d21
{
Packit fb9d21
	return group;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
S(UNION,foreach_data)
Packit fb9d21
{
Packit fb9d21
	isl_stat (*fn)(__isl_take PART *part, void *user);
Packit fb9d21
	void *user;
Packit fb9d21
};
Packit fb9d21
Packit fb9d21
static isl_stat FN(UNION,call_on_copy)(void **entry, void *user)
Packit fb9d21
{
Packit fb9d21
	PART *part = *entry;
Packit fb9d21
	S(UNION,foreach_data) *data = (S(UNION,foreach_data) *) user;
Packit fb9d21
Packit fb9d21
	part = FN(PART,copy)(part);
Packit fb9d21
	if (!part)
Packit fb9d21
		return isl_stat_error;
Packit fb9d21
	return data->fn(part, data->user);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Call data->fn on a copy of each expression in "group".
Packit fb9d21
 */
Packit fb9d21
static isl_stat FN(UNION,group_call_on_copy)(__isl_keep S(UNION,group) *group,
Packit fb9d21
	void *user)
Packit fb9d21
{
Packit fb9d21
	isl_ctx *ctx;
Packit fb9d21
Packit fb9d21
	if (!group)
Packit fb9d21
		return isl_stat_error;
Packit fb9d21
Packit fb9d21
	ctx = isl_space_get_ctx(group->domain_space);
Packit fb9d21
	return isl_hash_table_foreach(ctx, &group->part_table,
Packit fb9d21
				      &FN(UNION,call_on_copy), user);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
isl_stat FN(FN(UNION,foreach),PARTS)(__isl_keep UNION *u,
Packit fb9d21
	isl_stat (*fn)(__isl_take PART *part, void *user), void *user)
Packit fb9d21
{
Packit fb9d21
	S(UNION,foreach_data) data = { fn, user };
Packit fb9d21
Packit fb9d21
	if (!u)
Packit fb9d21
		return isl_stat_error;
Packit fb9d21
Packit fb9d21
	return FN(UNION,foreach_group)(u, &FN(UNION,group_call_on_copy), &data);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Is the domain space of the group of expressions at "entry"
Packit fb9d21
 * equal to "space"?
Packit fb9d21
 */
Packit fb9d21
static int FN(UNION,group_has_domain_space)(const void *entry, const void *val)
Packit fb9d21
{
Packit fb9d21
	S(UNION,group) *group = (S(UNION,group) *) entry;
Packit fb9d21
	isl_space *space = (isl_space *) val;
Packit fb9d21
Packit fb9d21
	return isl_space_is_domain_internal(group->domain_space, space);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Return the entry, if any, in "u" that lives in "space".
Packit fb9d21
 * If "reserve" is set, then an entry is created if it does not exist yet.
Packit fb9d21
 * Return NULL on error and isl_hash_table_entry_none if no entry was found.
Packit fb9d21
 * Note that when "reserve" is set, the function will never return
Packit fb9d21
 * isl_hash_table_entry_none.
Packit fb9d21
 *
Packit fb9d21
 * First look for the group of expressions with the same domain space,
Packit fb9d21
 * creating one if needed.
Packit fb9d21
 * Then look for the expression living in the specified space in that group.
Packit fb9d21
 */
Packit fb9d21
static struct isl_hash_table_entry *FN(UNION,find_part_entry)(
Packit fb9d21
	__isl_keep UNION *u, __isl_keep isl_space *space, int reserve)
Packit fb9d21
{
Packit fb9d21
	isl_ctx *ctx;
Packit fb9d21
	uint32_t hash;
Packit fb9d21
	struct isl_hash_table_entry *group_entry, *part_entry;
Packit fb9d21
	S(UNION,group) *group;
Packit fb9d21
Packit fb9d21
	if (!u || !space)
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	ctx = FN(UNION,get_ctx)(u);
Packit fb9d21
	hash = isl_space_get_domain_hash(space);
Packit fb9d21
	group_entry = isl_hash_table_find(ctx, &u->table, hash,
Packit fb9d21
			    &FN(UNION,group_has_domain_space), space, reserve);
Packit fb9d21
	if (!group_entry)
Packit fb9d21
		return reserve ? NULL : isl_hash_table_entry_none;
Packit fb9d21
	if (reserve && !group_entry->data) {
Packit fb9d21
		isl_space *domain = isl_space_domain(isl_space_copy(space));
Packit fb9d21
		group = FN(UNION,group_alloc)(domain, 1);
Packit fb9d21
		group_entry->data = group;
Packit fb9d21
	} else {
Packit fb9d21
		group = group_entry->data;
Packit fb9d21
		if (reserve)
Packit fb9d21
			group = FN(UNION,group_cow)(group);
Packit fb9d21
	}
Packit fb9d21
	if (!group)
Packit fb9d21
		return NULL;
Packit fb9d21
	hash = isl_space_get_hash(space);
Packit fb9d21
	part_entry = isl_hash_table_find(ctx, &group->part_table, hash,
Packit fb9d21
				&FN(UNION,has_space), space, reserve);
Packit fb9d21
	if (!reserve && !part_entry)
Packit fb9d21
		return isl_hash_table_entry_none;
Packit fb9d21
	return part_entry;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Remove "part_entry" from the hash table of "u".
Packit fb9d21
 *
Packit fb9d21
 * First look the group_entry in "u" holding the group that
Packit fb9d21
 * contains "part_entry".  Remove "part_entry" from that group.
Packit fb9d21
 * If the group becomes empty, then also remove the group_entry from "u".
Packit fb9d21
 */
Packit fb9d21
static __isl_give UNION *FN(UNION,remove_part_entry)(__isl_take UNION *u,
Packit fb9d21
	struct isl_hash_table_entry *part_entry)
Packit fb9d21
{
Packit fb9d21
	isl_ctx *ctx;
Packit fb9d21
	uint32_t hash;
Packit fb9d21
	PART *part;
Packit fb9d21
	struct isl_hash_table_entry *group_entry;
Packit fb9d21
	S(UNION,group) *group;
Packit fb9d21
Packit fb9d21
	if (!u || !part_entry)
Packit fb9d21
		return FN(UNION,free)(u);
Packit fb9d21
Packit fb9d21
	part = part_entry->data;
Packit fb9d21
	ctx = FN(UNION,get_ctx)(u);
Packit fb9d21
	hash = isl_space_get_domain_hash(part->dim);
Packit fb9d21
	group_entry = isl_hash_table_find(ctx, &u->table, hash,
Packit fb9d21
			    &FN(UNION,group_has_domain_space), part->dim, 0);
Packit fb9d21
	if (!group_entry)
Packit fb9d21
		isl_die(ctx, isl_error_internal, "missing group",
Packit fb9d21
			return FN(UNION,free)(u));
Packit fb9d21
	group = group_entry->data;
Packit fb9d21
	isl_hash_table_remove(ctx, &group->part_table, part_entry);
Packit fb9d21
	FN(PART,free)(part);
Packit fb9d21
Packit fb9d21
	if (group->part_table.n != 0)
Packit fb9d21
		return u;
Packit fb9d21
Packit fb9d21
	isl_hash_table_remove(ctx, &u->table, group_entry);
Packit fb9d21
	FN(UNION,group_free)(group);
Packit fb9d21
Packit fb9d21
	return u;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Are the domains of "part1" and "part2" disjoint?
Packit fb9d21
 */
Packit fb9d21
static isl_bool FN(UNION,disjoint_domain)(__isl_keep PART *part1,
Packit fb9d21
	__isl_keep PART *part2)
Packit fb9d21
{
Packit fb9d21
	isl_set *dom1, *dom2;
Packit fb9d21
	isl_bool disjoint;
Packit fb9d21
Packit fb9d21
	if (!part1 || !part2)
Packit fb9d21
		return isl_bool_error;
Packit fb9d21
	dom1 = FN(PART,domain)(FN(PART,copy)(part1));
Packit fb9d21
	dom2 = FN(PART,domain)(FN(PART,copy)(part2));
Packit fb9d21
	disjoint = isl_set_is_disjoint(dom1, dom2);
Packit fb9d21
	isl_set_free(dom1);
Packit fb9d21
	isl_set_free(dom2);
Packit fb9d21
Packit fb9d21
	return disjoint;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Check that the expression at *entry has a domain that is disjoint
Packit fb9d21
 * from that of "part", unless they also have the same target space.
Packit fb9d21
 */
Packit fb9d21
static isl_stat FN(UNION,check_disjoint_domain_entry)(void **entry, void *user)
Packit fb9d21
{
Packit fb9d21
	PART *part = user;
Packit fb9d21
	PART *other = *entry;
Packit fb9d21
	isl_bool equal;
Packit fb9d21
	isl_bool disjoint;
Packit fb9d21
Packit fb9d21
	equal = isl_space_is_equal(part->dim, other->dim);
Packit fb9d21
	if (equal < 0)
Packit fb9d21
		return isl_stat_error;
Packit fb9d21
	if (equal)
Packit fb9d21
		return isl_stat_ok;
Packit fb9d21
Packit fb9d21
	disjoint = FN(UNION,disjoint_domain)(part, other);
Packit fb9d21
	if (disjoint < 0)
Packit fb9d21
		return isl_stat_error;
Packit fb9d21
	if (!disjoint)
Packit fb9d21
		isl_die(FN(PART,get_ctx)(part), isl_error_invalid,
Packit fb9d21
			"overlapping domain with other part",
Packit fb9d21
			return isl_stat_error);
Packit fb9d21
	return isl_stat_ok;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Check that the domain of "part" is disjoint from the domain of the entries
Packit fb9d21
 * in "u" that are defined on the same domain space, but have a different
Packit fb9d21
 * target space.
Packit fb9d21
 * If there is no group of expressions in "u" with the same domain space,
Packit fb9d21
 * then everything is fine.  Otherwise, check the individual expressions
Packit fb9d21
 * in that group.
Packit fb9d21
 */
Packit fb9d21
static isl_stat FN(UNION,check_disjoint_domain_other)(__isl_keep UNION *u,
Packit fb9d21
	__isl_keep PART *part)
Packit fb9d21
{
Packit fb9d21
	isl_ctx *ctx;
Packit fb9d21
	uint32_t hash;
Packit fb9d21
	struct isl_hash_table_entry *group_entry;
Packit fb9d21
	S(UNION,group) *group;
Packit fb9d21
Packit fb9d21
	if (!u || !part)
Packit fb9d21
		return isl_stat_error;
Packit fb9d21
	ctx = FN(UNION,get_ctx)(u);
Packit fb9d21
	hash = isl_space_get_domain_hash(part->dim);
Packit fb9d21
	group_entry = isl_hash_table_find(ctx, &u->table, hash,
Packit fb9d21
			    &FN(UNION,group_has_domain_space), part->dim, 0);
Packit fb9d21
	if (!group_entry)
Packit fb9d21
		return isl_stat_ok;
Packit fb9d21
	group = group_entry->data;
Packit fb9d21
	return isl_hash_table_foreach(ctx, &group->part_table,
Packit fb9d21
			      &FN(UNION,check_disjoint_domain_entry), part);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Check that the domain of "part1" is disjoint from the domain of "part2".
Packit fb9d21
 * This check is performed before "part2" is added to a UNION to ensure
Packit fb9d21
 * that the UNION expression remains a function.
Packit fb9d21
 */
Packit fb9d21
static isl_stat FN(UNION,check_disjoint_domain)(__isl_keep PART *part1,
Packit fb9d21
	__isl_keep PART *part2)
Packit fb9d21
{
Packit fb9d21
	isl_bool disjoint;
Packit fb9d21
Packit fb9d21
	disjoint = FN(UNION,disjoint_domain)(part1, part2);
Packit fb9d21
	if (disjoint < 0)
Packit fb9d21
		return isl_stat_error;
Packit fb9d21
	if (!disjoint)
Packit fb9d21
		isl_die(FN(PART,get_ctx)(part1), isl_error_invalid,
Packit fb9d21
			"domain of additional part should be disjoint",
Packit fb9d21
			return isl_stat_error);
Packit fb9d21
	return isl_stat_ok;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Internal data structure for isl_union_*_foreach_inplace.
Packit fb9d21
 * "fn" is the function that needs to be called on each entry.
Packit fb9d21
 */
Packit fb9d21
S(UNION,foreach_inplace_data)
Packit fb9d21
{
Packit fb9d21
	isl_stat (*fn)(void **entry, void *user);
Packit fb9d21
	void *user;
Packit fb9d21
};
Packit fb9d21
Packit fb9d21
/* isl_union_*_foreach_group callback for calling data->fn on
Packit fb9d21
 * each part entry in the group.
Packit fb9d21
 */
Packit fb9d21
static isl_stat FN(UNION,group_call_inplace)(__isl_keep S(UNION,group) *group,
Packit fb9d21
	void *user)
Packit fb9d21
{
Packit fb9d21
	isl_ctx *ctx;
Packit fb9d21
	S(UNION,foreach_inplace_data) *data;
Packit fb9d21
Packit fb9d21
	if (!group)
Packit fb9d21
		return isl_stat_error;
Packit fb9d21
Packit fb9d21
	data = (S(UNION,foreach_inplace_data) *) user;
Packit fb9d21
	ctx = isl_space_get_ctx(group->domain_space);
Packit fb9d21
	return isl_hash_table_foreach(ctx, &group->part_table,
Packit fb9d21
				      data->fn, data->user);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Call "fn" on each part entry of "u".
Packit fb9d21
 */
Packit fb9d21
static isl_stat FN(UNION,foreach_inplace)(__isl_keep UNION *u,
Packit fb9d21
	isl_stat (*fn)(void **part, void *user), void *user)
Packit fb9d21
{
Packit fb9d21
	S(UNION,foreach_inplace_data) data = { fn, user };
Packit fb9d21
Packit fb9d21
	return FN(UNION,foreach_group)(u, &FN(UNION,group_call_inplace), &data);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Does "u" have a single reference?
Packit fb9d21
 * That is, can we change "u" inplace?
Packit fb9d21
 */
Packit fb9d21
static isl_bool FN(UNION,has_single_reference)(__isl_keep UNION *u)
Packit fb9d21
{
Packit fb9d21
	if (!u)
Packit fb9d21
		return isl_bool_error;
Packit fb9d21
	return u->ref == 1;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
static isl_stat FN(UNION,free_u_entry)(void **entry, void *user)
Packit fb9d21
{
Packit fb9d21
	S(UNION,group) *group = *entry;
Packit fb9d21
	FN(UNION,group_free)(group);
Packit fb9d21
	return isl_stat_ok;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
#include <isl_union_templ.c>