Blame isl-0.16.1/isl_flow.c

Packit fb9d21
/*
Packit fb9d21
 * Copyright 2005-2007 Universiteit Leiden
Packit fb9d21
 * Copyright 2008-2009 Katholieke Universiteit Leuven
Packit fb9d21
 * Copyright 2010      INRIA Saclay
Packit fb9d21
 * Copyright 2012      Universiteit Leiden
Packit fb9d21
 * Copyright 2014      Ecole Normale Superieure
Packit fb9d21
 *
Packit fb9d21
 * Use of this software is governed by the MIT license
Packit fb9d21
 *
Packit fb9d21
 * Written by Sven Verdoolaege, Leiden Institute of Advanced Computer Science,
Packit fb9d21
 * Universiteit Leiden, Niels Bohrweg 1, 2333 CA Leiden, The Netherlands
Packit fb9d21
 * and K.U.Leuven, Departement Computerwetenschappen, Celestijnenlaan 200A,
Packit fb9d21
 * B-3001 Leuven, Belgium
Packit fb9d21
 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
Packit fb9d21
 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France 
Packit fb9d21
 * and Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
Packit fb9d21
 */
Packit fb9d21
Packit fb9d21
#include <isl/set.h>
Packit fb9d21
#include <isl/map.h>
Packit fb9d21
#include <isl/union_set.h>
Packit fb9d21
#include <isl/union_map.h>
Packit fb9d21
#include <isl/flow.h>
Packit fb9d21
#include <isl/schedule_node.h>
Packit fb9d21
#include <isl_sort.h>
Packit fb9d21
Packit fb9d21
enum isl_restriction_type {
Packit fb9d21
	isl_restriction_type_empty,
Packit fb9d21
	isl_restriction_type_none,
Packit fb9d21
	isl_restriction_type_input,
Packit fb9d21
	isl_restriction_type_output
Packit fb9d21
};
Packit fb9d21
Packit fb9d21
struct isl_restriction {
Packit fb9d21
	enum isl_restriction_type type;
Packit fb9d21
Packit fb9d21
	isl_set *source;
Packit fb9d21
	isl_set *sink;
Packit fb9d21
};
Packit fb9d21
Packit fb9d21
/* Create a restriction of the given type.
Packit fb9d21
 */
Packit fb9d21
static __isl_give isl_restriction *isl_restriction_alloc(
Packit fb9d21
	__isl_take isl_map *source_map, enum isl_restriction_type type)
Packit fb9d21
{
Packit fb9d21
	isl_ctx *ctx;
Packit fb9d21
	isl_restriction *restr;
Packit fb9d21
Packit fb9d21
	if (!source_map)
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	ctx = isl_map_get_ctx(source_map);
Packit fb9d21
	restr = isl_calloc_type(ctx, struct isl_restriction);
Packit fb9d21
	if (!restr)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	restr->type = type;
Packit fb9d21
Packit fb9d21
	isl_map_free(source_map);
Packit fb9d21
	return restr;
Packit fb9d21
error:
Packit fb9d21
	isl_map_free(source_map);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Create a restriction that doesn't restrict anything.
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_restriction *isl_restriction_none(__isl_take isl_map *source_map)
Packit fb9d21
{
Packit fb9d21
	return isl_restriction_alloc(source_map, isl_restriction_type_none);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Create a restriction that removes everything.
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_restriction *isl_restriction_empty(
Packit fb9d21
	__isl_take isl_map *source_map)
Packit fb9d21
{
Packit fb9d21
	return isl_restriction_alloc(source_map, isl_restriction_type_empty);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Create a restriction on the input of the maximization problem
Packit fb9d21
 * based on the given source and sink restrictions.
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_restriction *isl_restriction_input(
Packit fb9d21
	__isl_take isl_set *source_restr, __isl_take isl_set *sink_restr)
Packit fb9d21
{
Packit fb9d21
	isl_ctx *ctx;
Packit fb9d21
	isl_restriction *restr;
Packit fb9d21
Packit fb9d21
	if (!source_restr || !sink_restr)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	ctx = isl_set_get_ctx(source_restr);
Packit fb9d21
	restr = isl_calloc_type(ctx, struct isl_restriction);
Packit fb9d21
	if (!restr)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	restr->type = isl_restriction_type_input;
Packit fb9d21
	restr->source = source_restr;
Packit fb9d21
	restr->sink = sink_restr;
Packit fb9d21
Packit fb9d21
	return restr;
Packit fb9d21
error:
Packit fb9d21
	isl_set_free(source_restr);
Packit fb9d21
	isl_set_free(sink_restr);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Create a restriction on the output of the maximization problem
Packit fb9d21
 * based on the given source restriction.
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_restriction *isl_restriction_output(
Packit fb9d21
	__isl_take isl_set *source_restr)
Packit fb9d21
{
Packit fb9d21
	isl_ctx *ctx;
Packit fb9d21
	isl_restriction *restr;
Packit fb9d21
Packit fb9d21
	if (!source_restr)
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	ctx = isl_set_get_ctx(source_restr);
Packit fb9d21
	restr = isl_calloc_type(ctx, struct isl_restriction);
Packit fb9d21
	if (!restr)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	restr->type = isl_restriction_type_output;
Packit fb9d21
	restr->source = source_restr;
Packit fb9d21
Packit fb9d21
	return restr;
Packit fb9d21
error:
Packit fb9d21
	isl_set_free(source_restr);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
__isl_null isl_restriction *isl_restriction_free(
Packit fb9d21
	__isl_take isl_restriction *restr)
Packit fb9d21
{
Packit fb9d21
	if (!restr)
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	isl_set_free(restr->source);
Packit fb9d21
	isl_set_free(restr->sink);
Packit fb9d21
	free(restr);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
isl_ctx *isl_restriction_get_ctx(__isl_keep isl_restriction *restr)
Packit fb9d21
{
Packit fb9d21
	return restr ? isl_set_get_ctx(restr->source) : NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* A private structure to keep track of a mapping together with
Packit fb9d21
 * a user-specified identifier and a boolean indicating whether
Packit fb9d21
 * the map represents a must or may access/dependence.
Packit fb9d21
 */
Packit fb9d21
struct isl_labeled_map {
Packit fb9d21
	struct isl_map	*map;
Packit fb9d21
	void		*data;
Packit fb9d21
	int		must;
Packit fb9d21
};
Packit fb9d21
Packit fb9d21
/* A structure containing the input for dependence analysis:
Packit fb9d21
 * - a sink
Packit fb9d21
 * - n_must + n_may (<= max_source) sources
Packit fb9d21
 * - a function for determining the relative order of sources and sink
Packit fb9d21
 * The must sources are placed before the may sources.
Packit fb9d21
 *
Packit fb9d21
 * domain_map is an auxiliary map that maps the sink access relation
Packit fb9d21
 * to the domain of this access relation.
Packit fb9d21
 * This field is only needed when restrict_fn is set and
Packit fb9d21
 * the field itself is set by isl_access_info_compute_flow.
Packit fb9d21
 *
Packit fb9d21
 * restrict_fn is a callback that (if not NULL) will be called
Packit fb9d21
 * right before any lexicographical maximization.
Packit fb9d21
 */
Packit fb9d21
struct isl_access_info {
Packit fb9d21
	isl_map				*domain_map;
Packit fb9d21
	struct isl_labeled_map		sink;
Packit fb9d21
	isl_access_level_before		level_before;
Packit fb9d21
Packit fb9d21
	isl_access_restrict		restrict_fn;
Packit fb9d21
	void				*restrict_user;
Packit fb9d21
Packit fb9d21
	int		    		max_source;
Packit fb9d21
	int		    		n_must;
Packit fb9d21
	int		    		n_may;
Packit fb9d21
	struct isl_labeled_map		source[1];
Packit fb9d21
};
Packit fb9d21
Packit fb9d21
/* A structure containing the output of dependence analysis:
Packit fb9d21
 * - n_source dependences
Packit fb9d21
 * - a wrapped subset of the sink for which definitely no source could be found
Packit fb9d21
 * - a wrapped subset of the sink for which possibly no source could be found
Packit fb9d21
 */
Packit fb9d21
struct isl_flow {
Packit fb9d21
	isl_set			*must_no_source;
Packit fb9d21
	isl_set			*may_no_source;
Packit fb9d21
	int			n_source;
Packit fb9d21
	struct isl_labeled_map	*dep;
Packit fb9d21
};
Packit fb9d21
Packit fb9d21
/* Construct an isl_access_info structure and fill it up with
Packit fb9d21
 * the given data.  The number of sources is set to 0.
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_access_info *isl_access_info_alloc(__isl_take isl_map *sink,
Packit fb9d21
	void *sink_user, isl_access_level_before fn, int max_source)
Packit fb9d21
{
Packit fb9d21
	isl_ctx *ctx;
Packit fb9d21
	struct isl_access_info *acc;
Packit fb9d21
Packit fb9d21
	if (!sink)
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	ctx = isl_map_get_ctx(sink);
Packit fb9d21
	isl_assert(ctx, max_source >= 0, goto error);
Packit fb9d21
Packit fb9d21
	acc = isl_calloc(ctx, struct isl_access_info,
Packit fb9d21
			sizeof(struct isl_access_info) +
Packit fb9d21
			(max_source - 1) * sizeof(struct isl_labeled_map));
Packit fb9d21
	if (!acc)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	acc->sink.map = sink;
Packit fb9d21
	acc->sink.data = sink_user;
Packit fb9d21
	acc->level_before = fn;
Packit fb9d21
	acc->max_source = max_source;
Packit fb9d21
	acc->n_must = 0;
Packit fb9d21
	acc->n_may = 0;
Packit fb9d21
Packit fb9d21
	return acc;
Packit fb9d21
error:
Packit fb9d21
	isl_map_free(sink);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Free the given isl_access_info structure.
Packit fb9d21
 */
Packit fb9d21
__isl_null isl_access_info *isl_access_info_free(
Packit fb9d21
	__isl_take isl_access_info *acc)
Packit fb9d21
{
Packit fb9d21
	int i;
Packit fb9d21
Packit fb9d21
	if (!acc)
Packit fb9d21
		return NULL;
Packit fb9d21
	isl_map_free(acc->domain_map);
Packit fb9d21
	isl_map_free(acc->sink.map);
Packit fb9d21
	for (i = 0; i < acc->n_must + acc->n_may; ++i)
Packit fb9d21
		isl_map_free(acc->source[i].map);
Packit fb9d21
	free(acc);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
isl_ctx *isl_access_info_get_ctx(__isl_keep isl_access_info *acc)
Packit fb9d21
{
Packit fb9d21
	return acc ? isl_map_get_ctx(acc->sink.map) : NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
__isl_give isl_access_info *isl_access_info_set_restrict(
Packit fb9d21
	__isl_take isl_access_info *acc, isl_access_restrict fn, void *user)
Packit fb9d21
{
Packit fb9d21
	if (!acc)
Packit fb9d21
		return NULL;
Packit fb9d21
	acc->restrict_fn = fn;
Packit fb9d21
	acc->restrict_user = user;
Packit fb9d21
	return acc;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Add another source to an isl_access_info structure, making
Packit fb9d21
 * sure the "must" sources are placed before the "may" sources.
Packit fb9d21
 * This function may be called at most max_source times on a
Packit fb9d21
 * given isl_access_info structure, with max_source as specified
Packit fb9d21
 * in the call to isl_access_info_alloc that constructed the structure.
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_access_info *isl_access_info_add_source(
Packit fb9d21
	__isl_take isl_access_info *acc, __isl_take isl_map *source,
Packit fb9d21
	int must, void *source_user)
Packit fb9d21
{
Packit fb9d21
	isl_ctx *ctx;
Packit fb9d21
Packit fb9d21
	if (!acc)
Packit fb9d21
		goto error;
Packit fb9d21
	ctx = isl_map_get_ctx(acc->sink.map);
Packit fb9d21
	isl_assert(ctx, acc->n_must + acc->n_may < acc->max_source, goto error);
Packit fb9d21
	
Packit fb9d21
	if (must) {
Packit fb9d21
		if (acc->n_may)
Packit fb9d21
			acc->source[acc->n_must + acc->n_may] =
Packit fb9d21
				acc->source[acc->n_must];
Packit fb9d21
		acc->source[acc->n_must].map = source;
Packit fb9d21
		acc->source[acc->n_must].data = source_user;
Packit fb9d21
		acc->source[acc->n_must].must = 1;
Packit fb9d21
		acc->n_must++;
Packit fb9d21
	} else {
Packit fb9d21
		acc->source[acc->n_must + acc->n_may].map = source;
Packit fb9d21
		acc->source[acc->n_must + acc->n_may].data = source_user;
Packit fb9d21
		acc->source[acc->n_must + acc->n_may].must = 0;
Packit fb9d21
		acc->n_may++;
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	return acc;
Packit fb9d21
error:
Packit fb9d21
	isl_map_free(source);
Packit fb9d21
	isl_access_info_free(acc);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Return -n, 0 or n (with n a positive value), depending on whether
Packit fb9d21
 * the source access identified by p1 should be sorted before, together
Packit fb9d21
 * or after that identified by p2.
Packit fb9d21
 *
Packit fb9d21
 * If p1 appears before p2, then it should be sorted first.
Packit fb9d21
 * For more generic initial schedules, it is possible that neither
Packit fb9d21
 * p1 nor p2 appears before the other, or at least not in any obvious way.
Packit fb9d21
 * We therefore also check if p2 appears before p1, in which case p2
Packit fb9d21
 * should be sorted first.
Packit fb9d21
 * If not, we try to order the two statements based on the description
Packit fb9d21
 * of the iteration domains.  This results in an arbitrary, but fairly
Packit fb9d21
 * stable ordering.
Packit fb9d21
 */
Packit fb9d21
static int access_sort_cmp(const void *p1, const void *p2, void *user)
Packit fb9d21
{
Packit fb9d21
	isl_access_info *acc = user;
Packit fb9d21
	const struct isl_labeled_map *i1, *i2;
Packit fb9d21
	int level1, level2;
Packit fb9d21
	uint32_t h1, h2;
Packit fb9d21
	i1 = (const struct isl_labeled_map *) p1;
Packit fb9d21
	i2 = (const struct isl_labeled_map *) p2;
Packit fb9d21
Packit fb9d21
	level1 = acc->level_before(i1->data, i2->data);
Packit fb9d21
	if (level1 % 2)
Packit fb9d21
		return -1;
Packit fb9d21
Packit fb9d21
	level2 = acc->level_before(i2->data, i1->data);
Packit fb9d21
	if (level2 % 2)
Packit fb9d21
		return 1;
Packit fb9d21
Packit fb9d21
	h1 = isl_map_get_hash(i1->map);
Packit fb9d21
	h2 = isl_map_get_hash(i2->map);
Packit fb9d21
	return h1 > h2 ? 1 : h1 < h2 ? -1 : 0;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Sort the must source accesses in their textual order.
Packit fb9d21
 */
Packit fb9d21
static __isl_give isl_access_info *isl_access_info_sort_sources(
Packit fb9d21
	__isl_take isl_access_info *acc)
Packit fb9d21
{
Packit fb9d21
	if (!acc)
Packit fb9d21
		return NULL;
Packit fb9d21
	if (acc->n_must <= 1)
Packit fb9d21
		return acc;
Packit fb9d21
Packit fb9d21
	if (isl_sort(acc->source, acc->n_must, sizeof(struct isl_labeled_map),
Packit fb9d21
		    access_sort_cmp, acc) < 0)
Packit fb9d21
		return isl_access_info_free(acc);
Packit fb9d21
Packit fb9d21
	return acc;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Align the parameters of the two spaces if needed and then call
Packit fb9d21
 * isl_space_join.
Packit fb9d21
 */
Packit fb9d21
static __isl_give isl_space *space_align_and_join(__isl_take isl_space *left,
Packit fb9d21
	__isl_take isl_space *right)
Packit fb9d21
{
Packit fb9d21
	if (isl_space_match(left, isl_dim_param, right, isl_dim_param))
Packit fb9d21
		return isl_space_join(left, right);
Packit fb9d21
Packit fb9d21
	left = isl_space_align_params(left, isl_space_copy(right));
Packit fb9d21
	right = isl_space_align_params(right, isl_space_copy(left));
Packit fb9d21
	return isl_space_join(left, right);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Initialize an empty isl_flow structure corresponding to a given
Packit fb9d21
 * isl_access_info structure.
Packit fb9d21
 * For each must access, two dependences are created (initialized
Packit fb9d21
 * to the empty relation), one for the resulting must dependences
Packit fb9d21
 * and one for the resulting may dependences.  May accesses can
Packit fb9d21
 * only lead to may dependences, so only one dependence is created
Packit fb9d21
 * for each of them.
Packit fb9d21
 * This function is private as isl_flow structures are only supposed
Packit fb9d21
 * to be created by isl_access_info_compute_flow.
Packit fb9d21
 */
Packit fb9d21
static __isl_give isl_flow *isl_flow_alloc(__isl_keep isl_access_info *acc)
Packit fb9d21
{
Packit fb9d21
	int i, n;
Packit fb9d21
	struct isl_ctx *ctx;
Packit fb9d21
	struct isl_flow *dep;
Packit fb9d21
Packit fb9d21
	if (!acc)
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	ctx = isl_map_get_ctx(acc->sink.map);
Packit fb9d21
	dep = isl_calloc_type(ctx, struct isl_flow);
Packit fb9d21
	if (!dep)
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	n = 2 * acc->n_must + acc->n_may;
Packit fb9d21
	dep->dep = isl_calloc_array(ctx, struct isl_labeled_map, n);
Packit fb9d21
	if (n && !dep->dep)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	dep->n_source = n;
Packit fb9d21
	for (i = 0; i < acc->n_must; ++i) {
Packit fb9d21
		isl_space *dim;
Packit fb9d21
		dim = space_align_and_join(
Packit fb9d21
			isl_map_get_space(acc->source[i].map),
Packit fb9d21
			isl_space_reverse(isl_map_get_space(acc->sink.map)));
Packit fb9d21
		dep->dep[2 * i].map = isl_map_empty(dim);
Packit fb9d21
		dep->dep[2 * i + 1].map = isl_map_copy(dep->dep[2 * i].map);
Packit fb9d21
		dep->dep[2 * i].data = acc->source[i].data;
Packit fb9d21
		dep->dep[2 * i + 1].data = acc->source[i].data;
Packit fb9d21
		dep->dep[2 * i].must = 1;
Packit fb9d21
		dep->dep[2 * i + 1].must = 0;
Packit fb9d21
		if (!dep->dep[2 * i].map || !dep->dep[2 * i + 1].map)
Packit fb9d21
			goto error;
Packit fb9d21
	}
Packit fb9d21
	for (i = acc->n_must; i < acc->n_must + acc->n_may; ++i) {
Packit fb9d21
		isl_space *dim;
Packit fb9d21
		dim = space_align_and_join(
Packit fb9d21
			isl_map_get_space(acc->source[i].map),
Packit fb9d21
			isl_space_reverse(isl_map_get_space(acc->sink.map)));
Packit fb9d21
		dep->dep[acc->n_must + i].map = isl_map_empty(dim);
Packit fb9d21
		dep->dep[acc->n_must + i].data = acc->source[i].data;
Packit fb9d21
		dep->dep[acc->n_must + i].must = 0;
Packit fb9d21
		if (!dep->dep[acc->n_must + i].map)
Packit fb9d21
			goto error;
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	return dep;
Packit fb9d21
error:
Packit fb9d21
	isl_flow_free(dep);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Iterate over all sources and for each resulting flow dependence
Packit fb9d21
 * that is not empty, call the user specfied function.
Packit fb9d21
 * The second argument in this function call identifies the source,
Packit fb9d21
 * while the third argument correspond to the final argument of
Packit fb9d21
 * the isl_flow_foreach call.
Packit fb9d21
 */
Packit fb9d21
isl_stat isl_flow_foreach(__isl_keep isl_flow *deps,
Packit fb9d21
	isl_stat (*fn)(__isl_take isl_map *dep, int must, void *dep_user,
Packit fb9d21
		void *user),
Packit fb9d21
	void *user)
Packit fb9d21
{
Packit fb9d21
	int i;
Packit fb9d21
Packit fb9d21
	if (!deps)
Packit fb9d21
		return isl_stat_error;
Packit fb9d21
Packit fb9d21
	for (i = 0; i < deps->n_source; ++i) {
Packit fb9d21
		if (isl_map_plain_is_empty(deps->dep[i].map))
Packit fb9d21
			continue;
Packit fb9d21
		if (fn(isl_map_copy(deps->dep[i].map), deps->dep[i].must,
Packit fb9d21
				deps->dep[i].data, user) < 0)
Packit fb9d21
			return isl_stat_error;
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	return isl_stat_ok;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Return a copy of the subset of the sink for which no source could be found.
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_map *isl_flow_get_no_source(__isl_keep isl_flow *deps, int must)
Packit fb9d21
{
Packit fb9d21
	if (!deps)
Packit fb9d21
		return NULL;
Packit fb9d21
	
Packit fb9d21
	if (must)
Packit fb9d21
		return isl_set_unwrap(isl_set_copy(deps->must_no_source));
Packit fb9d21
	else
Packit fb9d21
		return isl_set_unwrap(isl_set_copy(deps->may_no_source));
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
void isl_flow_free(__isl_take isl_flow *deps)
Packit fb9d21
{
Packit fb9d21
	int i;
Packit fb9d21
Packit fb9d21
	if (!deps)
Packit fb9d21
		return;
Packit fb9d21
	isl_set_free(deps->must_no_source);
Packit fb9d21
	isl_set_free(deps->may_no_source);
Packit fb9d21
	if (deps->dep) {
Packit fb9d21
		for (i = 0; i < deps->n_source; ++i)
Packit fb9d21
			isl_map_free(deps->dep[i].map);
Packit fb9d21
		free(deps->dep);
Packit fb9d21
	}
Packit fb9d21
	free(deps);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
isl_ctx *isl_flow_get_ctx(__isl_keep isl_flow *deps)
Packit fb9d21
{
Packit fb9d21
	return deps ? isl_set_get_ctx(deps->must_no_source) : NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Return a map that enforces that the domain iteration occurs after
Packit fb9d21
 * the range iteration at the given level.
Packit fb9d21
 * If level is odd, then the domain iteration should occur after
Packit fb9d21
 * the target iteration in their shared level/2 outermost loops.
Packit fb9d21
 * In this case we simply need to enforce that these outermost
Packit fb9d21
 * loop iterations are the same.
Packit fb9d21
 * If level is even, then the loop iterator of the domain should
Packit fb9d21
 * be greater than the loop iterator of the range at the last
Packit fb9d21
 * of the level/2 shared loops, i.e., loop level/2 - 1.
Packit fb9d21
 */
Packit fb9d21
static __isl_give isl_map *after_at_level(__isl_take isl_space *dim, int level)
Packit fb9d21
{
Packit fb9d21
	struct isl_basic_map *bmap;
Packit fb9d21
Packit fb9d21
	if (level % 2)
Packit fb9d21
		bmap = isl_basic_map_equal(dim, level/2);
Packit fb9d21
	else
Packit fb9d21
		bmap = isl_basic_map_more_at(dim, level/2 - 1);
Packit fb9d21
Packit fb9d21
	return isl_map_from_basic_map(bmap);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Compute the partial lexicographic maximum of "dep" on domain "sink",
Packit fb9d21
 * but first check if the user has set acc->restrict_fn and if so
Packit fb9d21
 * update either the input or the output of the maximization problem
Packit fb9d21
 * with respect to the resulting restriction.
Packit fb9d21
 *
Packit fb9d21
 * Since the user expects a mapping from sink iterations to source iterations,
Packit fb9d21
 * whereas the domain of "dep" is a wrapped map, mapping sink iterations
Packit fb9d21
 * to accessed array elements, we first need to project out the accessed
Packit fb9d21
 * sink array elements by applying acc->domain_map.
Packit fb9d21
 * Similarly, the sink restriction specified by the user needs to be
Packit fb9d21
 * converted back to the wrapped map.
Packit fb9d21
 */
Packit fb9d21
static __isl_give isl_map *restricted_partial_lexmax(
Packit fb9d21
	__isl_keep isl_access_info *acc, __isl_take isl_map *dep,
Packit fb9d21
	int source, __isl_take isl_set *sink, __isl_give isl_set **empty)
Packit fb9d21
{
Packit fb9d21
	isl_map *source_map;
Packit fb9d21
	isl_restriction *restr;
Packit fb9d21
	isl_set *sink_domain;
Packit fb9d21
	isl_set *sink_restr;
Packit fb9d21
	isl_map *res;
Packit fb9d21
Packit fb9d21
	if (!acc->restrict_fn)
Packit fb9d21
		return isl_map_partial_lexmax(dep, sink, empty);
Packit fb9d21
Packit fb9d21
	source_map = isl_map_copy(dep);
Packit fb9d21
	source_map = isl_map_apply_domain(source_map,
Packit fb9d21
					    isl_map_copy(acc->domain_map));
Packit fb9d21
	sink_domain = isl_set_copy(sink);
Packit fb9d21
	sink_domain = isl_set_apply(sink_domain, isl_map_copy(acc->domain_map));
Packit fb9d21
	restr = acc->restrict_fn(source_map, sink_domain,
Packit fb9d21
				acc->source[source].data, acc->restrict_user);
Packit fb9d21
	isl_set_free(sink_domain);
Packit fb9d21
	isl_map_free(source_map);
Packit fb9d21
Packit fb9d21
	if (!restr)
Packit fb9d21
		goto error;
Packit fb9d21
	if (restr->type == isl_restriction_type_input) {
Packit fb9d21
		dep = isl_map_intersect_range(dep, isl_set_copy(restr->source));
Packit fb9d21
		sink_restr = isl_set_copy(restr->sink);
Packit fb9d21
		sink_restr = isl_set_apply(sink_restr,
Packit fb9d21
				isl_map_reverse(isl_map_copy(acc->domain_map)));
Packit fb9d21
		sink = isl_set_intersect(sink, sink_restr);
Packit fb9d21
	} else if (restr->type == isl_restriction_type_empty) {
Packit fb9d21
		isl_space *space = isl_map_get_space(dep);
Packit fb9d21
		isl_map_free(dep);
Packit fb9d21
		dep = isl_map_empty(space);
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	res = isl_map_partial_lexmax(dep, sink, empty);
Packit fb9d21
Packit fb9d21
	if (restr->type == isl_restriction_type_output)
Packit fb9d21
		res = isl_map_intersect_range(res, isl_set_copy(restr->source));
Packit fb9d21
Packit fb9d21
	isl_restriction_free(restr);
Packit fb9d21
	return res;
Packit fb9d21
error:
Packit fb9d21
	isl_map_free(dep);
Packit fb9d21
	isl_set_free(sink);
Packit fb9d21
	*empty = NULL;
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Compute the last iteration of must source j that precedes the sink
Packit fb9d21
 * at the given level for sink iterations in set_C.
Packit fb9d21
 * The subset of set_C for which no such iteration can be found is returned
Packit fb9d21
 * in *empty.
Packit fb9d21
 */
Packit fb9d21
static struct isl_map *last_source(struct isl_access_info *acc, 
Packit fb9d21
				    struct isl_set *set_C,
Packit fb9d21
				    int j, int level, struct isl_set **empty)
Packit fb9d21
{
Packit fb9d21
	struct isl_map *read_map;
Packit fb9d21
	struct isl_map *write_map;
Packit fb9d21
	struct isl_map *dep_map;
Packit fb9d21
	struct isl_map *after;
Packit fb9d21
	struct isl_map *result;
Packit fb9d21
Packit fb9d21
	read_map = isl_map_copy(acc->sink.map);
Packit fb9d21
	write_map = isl_map_copy(acc->source[j].map);
Packit fb9d21
	write_map = isl_map_reverse(write_map);
Packit fb9d21
	dep_map = isl_map_apply_range(read_map, write_map);
Packit fb9d21
	after = after_at_level(isl_map_get_space(dep_map), level);
Packit fb9d21
	dep_map = isl_map_intersect(dep_map, after);
Packit fb9d21
	result = restricted_partial_lexmax(acc, dep_map, j, set_C, empty);
Packit fb9d21
	result = isl_map_reverse(result);
Packit fb9d21
Packit fb9d21
	return result;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* For a given mapping between iterations of must source j and iterations
Packit fb9d21
 * of the sink, compute the last iteration of must source k preceding
Packit fb9d21
 * the sink at level before_level for any of the sink iterations,
Packit fb9d21
 * but following the corresponding iteration of must source j at level
Packit fb9d21
 * after_level.
Packit fb9d21
 */
Packit fb9d21
static struct isl_map *last_later_source(struct isl_access_info *acc,
Packit fb9d21
					 struct isl_map *old_map,
Packit fb9d21
					 int j, int before_level,
Packit fb9d21
					 int k, int after_level,
Packit fb9d21
					 struct isl_set **empty)
Packit fb9d21
{
Packit fb9d21
	isl_space *dim;
Packit fb9d21
	struct isl_set *set_C;
Packit fb9d21
	struct isl_map *read_map;
Packit fb9d21
	struct isl_map *write_map;
Packit fb9d21
	struct isl_map *dep_map;
Packit fb9d21
	struct isl_map *after_write;
Packit fb9d21
	struct isl_map *before_read;
Packit fb9d21
	struct isl_map *result;
Packit fb9d21
Packit fb9d21
	set_C = isl_map_range(isl_map_copy(old_map));
Packit fb9d21
	read_map = isl_map_copy(acc->sink.map);
Packit fb9d21
	write_map = isl_map_copy(acc->source[k].map);
Packit fb9d21
Packit fb9d21
	write_map = isl_map_reverse(write_map);
Packit fb9d21
	dep_map = isl_map_apply_range(read_map, write_map);
Packit fb9d21
	dim = space_align_and_join(isl_map_get_space(acc->source[k].map),
Packit fb9d21
		    isl_space_reverse(isl_map_get_space(acc->source[j].map)));
Packit fb9d21
	after_write = after_at_level(dim, after_level);
Packit fb9d21
	after_write = isl_map_apply_range(after_write, old_map);
Packit fb9d21
	after_write = isl_map_reverse(after_write);
Packit fb9d21
	dep_map = isl_map_intersect(dep_map, after_write);
Packit fb9d21
	before_read = after_at_level(isl_map_get_space(dep_map), before_level);
Packit fb9d21
	dep_map = isl_map_intersect(dep_map, before_read);
Packit fb9d21
	result = restricted_partial_lexmax(acc, dep_map, k, set_C, empty);
Packit fb9d21
	result = isl_map_reverse(result);
Packit fb9d21
Packit fb9d21
	return result;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Given a shared_level between two accesses, return 1 if the
Packit fb9d21
 * the first can precede the second at the requested target_level.
Packit fb9d21
 * If the target level is odd, i.e., refers to a statement level
Packit fb9d21
 * dimension, then first needs to precede second at the requested
Packit fb9d21
 * level, i.e., shared_level must be equal to target_level.
Packit fb9d21
 * If the target level is odd, then the two loops should share
Packit fb9d21
 * at least the requested number of outer loops.
Packit fb9d21
 */
Packit fb9d21
static int can_precede_at_level(int shared_level, int target_level)
Packit fb9d21
{
Packit fb9d21
	if (shared_level < target_level)
Packit fb9d21
		return 0;
Packit fb9d21
	if ((target_level % 2) && shared_level > target_level)
Packit fb9d21
		return 0;
Packit fb9d21
	return 1;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Given a possible flow dependence temp_rel[j] between source j and the sink
Packit fb9d21
 * at level sink_level, remove those elements for which
Packit fb9d21
 * there is an iteration of another source k < j that is closer to the sink.
Packit fb9d21
 * The flow dependences temp_rel[k] are updated with the improved sources.
Packit fb9d21
 * Any improved source needs to precede the sink at the same level
Packit fb9d21
 * and needs to follow source j at the same or a deeper level.
Packit fb9d21
 * The lower this level, the later the execution date of source k.
Packit fb9d21
 * We therefore consider lower levels first.
Packit fb9d21
 *
Packit fb9d21
 * If temp_rel[j] is empty, then there can be no improvement and
Packit fb9d21
 * we return immediately.
Packit fb9d21
 */
Packit fb9d21
static int intermediate_sources(__isl_keep isl_access_info *acc,
Packit fb9d21
	struct isl_map **temp_rel, int j, int sink_level)
Packit fb9d21
{
Packit fb9d21
	int k, level;
Packit fb9d21
	int depth = 2 * isl_map_dim(acc->source[j].map, isl_dim_in) + 1;
Packit fb9d21
Packit fb9d21
	if (isl_map_plain_is_empty(temp_rel[j]))
Packit fb9d21
		return 0;
Packit fb9d21
Packit fb9d21
	for (k = j - 1; k >= 0; --k) {
Packit fb9d21
		int plevel, plevel2;
Packit fb9d21
		plevel = acc->level_before(acc->source[k].data, acc->sink.data);
Packit fb9d21
		if (!can_precede_at_level(plevel, sink_level))
Packit fb9d21
			continue;
Packit fb9d21
Packit fb9d21
		plevel2 = acc->level_before(acc->source[j].data,
Packit fb9d21
						acc->source[k].data);
Packit fb9d21
Packit fb9d21
		for (level = sink_level; level <= depth; ++level) {
Packit fb9d21
			struct isl_map *T;
Packit fb9d21
			struct isl_set *trest;
Packit fb9d21
			struct isl_map *copy;
Packit fb9d21
Packit fb9d21
			if (!can_precede_at_level(plevel2, level))
Packit fb9d21
				continue;
Packit fb9d21
Packit fb9d21
			copy = isl_map_copy(temp_rel[j]);
Packit fb9d21
			T = last_later_source(acc, copy, j, sink_level, k,
Packit fb9d21
					      level, &trest);
Packit fb9d21
			if (isl_map_plain_is_empty(T)) {
Packit fb9d21
				isl_set_free(trest);
Packit fb9d21
				isl_map_free(T);
Packit fb9d21
				continue;
Packit fb9d21
			}
Packit fb9d21
			temp_rel[j] = isl_map_intersect_range(temp_rel[j], trest);
Packit fb9d21
			temp_rel[k] = isl_map_union_disjoint(temp_rel[k], T);
Packit fb9d21
		}
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	return 0;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Compute all iterations of may source j that precedes the sink at the given
Packit fb9d21
 * level for sink iterations in set_C.
Packit fb9d21
 */
Packit fb9d21
static __isl_give isl_map *all_sources(__isl_keep isl_access_info *acc,
Packit fb9d21
				    __isl_take isl_set *set_C, int j, int level)
Packit fb9d21
{
Packit fb9d21
	isl_map *read_map;
Packit fb9d21
	isl_map *write_map;
Packit fb9d21
	isl_map *dep_map;
Packit fb9d21
	isl_map *after;
Packit fb9d21
Packit fb9d21
	read_map = isl_map_copy(acc->sink.map);
Packit fb9d21
	read_map = isl_map_intersect_domain(read_map, set_C);
Packit fb9d21
	write_map = isl_map_copy(acc->source[acc->n_must + j].map);
Packit fb9d21
	write_map = isl_map_reverse(write_map);
Packit fb9d21
	dep_map = isl_map_apply_range(read_map, write_map);
Packit fb9d21
	after = after_at_level(isl_map_get_space(dep_map), level);
Packit fb9d21
	dep_map = isl_map_intersect(dep_map, after);
Packit fb9d21
Packit fb9d21
	return isl_map_reverse(dep_map);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* For a given mapping between iterations of must source k and iterations
Packit fb9d21
 * of the sink, compute the all iteration of may source j preceding
Packit fb9d21
 * the sink at level before_level for any of the sink iterations,
Packit fb9d21
 * but following the corresponding iteration of must source k at level
Packit fb9d21
 * after_level.
Packit fb9d21
 */
Packit fb9d21
static __isl_give isl_map *all_later_sources(__isl_keep isl_access_info *acc,
Packit fb9d21
	__isl_take isl_map *old_map,
Packit fb9d21
	int j, int before_level, int k, int after_level)
Packit fb9d21
{
Packit fb9d21
	isl_space *dim;
Packit fb9d21
	isl_set *set_C;
Packit fb9d21
	isl_map *read_map;
Packit fb9d21
	isl_map *write_map;
Packit fb9d21
	isl_map *dep_map;
Packit fb9d21
	isl_map *after_write;
Packit fb9d21
	isl_map *before_read;
Packit fb9d21
Packit fb9d21
	set_C = isl_map_range(isl_map_copy(old_map));
Packit fb9d21
	read_map = isl_map_copy(acc->sink.map);
Packit fb9d21
	read_map = isl_map_intersect_domain(read_map, set_C);
Packit fb9d21
	write_map = isl_map_copy(acc->source[acc->n_must + j].map);
Packit fb9d21
Packit fb9d21
	write_map = isl_map_reverse(write_map);
Packit fb9d21
	dep_map = isl_map_apply_range(read_map, write_map);
Packit fb9d21
	dim = isl_space_join(isl_map_get_space(acc->source[acc->n_must + j].map),
Packit fb9d21
		    isl_space_reverse(isl_map_get_space(acc->source[k].map)));
Packit fb9d21
	after_write = after_at_level(dim, after_level);
Packit fb9d21
	after_write = isl_map_apply_range(after_write, old_map);
Packit fb9d21
	after_write = isl_map_reverse(after_write);
Packit fb9d21
	dep_map = isl_map_intersect(dep_map, after_write);
Packit fb9d21
	before_read = after_at_level(isl_map_get_space(dep_map), before_level);
Packit fb9d21
	dep_map = isl_map_intersect(dep_map, before_read);
Packit fb9d21
	return isl_map_reverse(dep_map);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Given the must and may dependence relations for the must accesses
Packit fb9d21
 * for level sink_level, check if there are any accesses of may access j
Packit fb9d21
 * that occur in between and return their union.
Packit fb9d21
 * If some of these accesses are intermediate with respect to
Packit fb9d21
 * (previously thought to be) must dependences, then these
Packit fb9d21
 * must dependences are turned into may dependences.
Packit fb9d21
 */
Packit fb9d21
static __isl_give isl_map *all_intermediate_sources(
Packit fb9d21
	__isl_keep isl_access_info *acc, __isl_take isl_map *map,
Packit fb9d21
	struct isl_map **must_rel, struct isl_map **may_rel,
Packit fb9d21
	int j, int sink_level)
Packit fb9d21
{
Packit fb9d21
	int k, level;
Packit fb9d21
	int depth = 2 * isl_map_dim(acc->source[acc->n_must + j].map,
Packit fb9d21
					isl_dim_in) + 1;
Packit fb9d21
Packit fb9d21
	for (k = 0; k < acc->n_must; ++k) {
Packit fb9d21
		int plevel;
Packit fb9d21
Packit fb9d21
		if (isl_map_plain_is_empty(may_rel[k]) &&
Packit fb9d21
		    isl_map_plain_is_empty(must_rel[k]))
Packit fb9d21
			continue;
Packit fb9d21
Packit fb9d21
		plevel = acc->level_before(acc->source[k].data,
Packit fb9d21
					acc->source[acc->n_must + j].data);
Packit fb9d21
Packit fb9d21
		for (level = sink_level; level <= depth; ++level) {
Packit fb9d21
			isl_map *T;
Packit fb9d21
			isl_map *copy;
Packit fb9d21
			isl_set *ran;
Packit fb9d21
Packit fb9d21
			if (!can_precede_at_level(plevel, level))
Packit fb9d21
				continue;
Packit fb9d21
Packit fb9d21
			copy = isl_map_copy(may_rel[k]);
Packit fb9d21
			T = all_later_sources(acc, copy, j, sink_level, k, level);
Packit fb9d21
			map = isl_map_union(map, T);
Packit fb9d21
Packit fb9d21
			copy = isl_map_copy(must_rel[k]);
Packit fb9d21
			T = all_later_sources(acc, copy, j, sink_level, k, level);
Packit fb9d21
			ran = isl_map_range(isl_map_copy(T));
Packit fb9d21
			map = isl_map_union(map, T);
Packit fb9d21
			may_rel[k] = isl_map_union_disjoint(may_rel[k],
Packit fb9d21
			    isl_map_intersect_range(isl_map_copy(must_rel[k]),
Packit fb9d21
						    isl_set_copy(ran)));
Packit fb9d21
			T = isl_map_from_domain_and_range(
Packit fb9d21
			    isl_set_universe(
Packit fb9d21
				isl_space_domain(isl_map_get_space(must_rel[k]))),
Packit fb9d21
			    ran);
Packit fb9d21
			must_rel[k] = isl_map_subtract(must_rel[k], T);
Packit fb9d21
		}
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	return map;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Compute dependences for the case where all accesses are "may"
Packit fb9d21
 * accesses, which boils down to computing memory based dependences.
Packit fb9d21
 * The generic algorithm would also work in this case, but it would
Packit fb9d21
 * be overkill to use it.
Packit fb9d21
 */
Packit fb9d21
static __isl_give isl_flow *compute_mem_based_dependences(
Packit fb9d21
	__isl_keep isl_access_info *acc)
Packit fb9d21
{
Packit fb9d21
	int i;
Packit fb9d21
	isl_set *mustdo;
Packit fb9d21
	isl_set *maydo;
Packit fb9d21
	isl_flow *res;
Packit fb9d21
Packit fb9d21
	res = isl_flow_alloc(acc);
Packit fb9d21
	if (!res)
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	mustdo = isl_map_domain(isl_map_copy(acc->sink.map));
Packit fb9d21
	maydo = isl_set_copy(mustdo);
Packit fb9d21
Packit fb9d21
	for (i = 0; i < acc->n_may; ++i) {
Packit fb9d21
		int plevel;
Packit fb9d21
		int is_before;
Packit fb9d21
		isl_space *dim;
Packit fb9d21
		isl_map *before;
Packit fb9d21
		isl_map *dep;
Packit fb9d21
Packit fb9d21
		plevel = acc->level_before(acc->source[i].data, acc->sink.data);
Packit fb9d21
		is_before = plevel & 1;
Packit fb9d21
		plevel >>= 1;
Packit fb9d21
Packit fb9d21
		dim = isl_map_get_space(res->dep[i].map);
Packit fb9d21
		if (is_before)
Packit fb9d21
			before = isl_map_lex_le_first(dim, plevel);
Packit fb9d21
		else
Packit fb9d21
			before = isl_map_lex_lt_first(dim, plevel);
Packit fb9d21
		dep = isl_map_apply_range(isl_map_copy(acc->source[i].map),
Packit fb9d21
			isl_map_reverse(isl_map_copy(acc->sink.map)));
Packit fb9d21
		dep = isl_map_intersect(dep, before);
Packit fb9d21
		mustdo = isl_set_subtract(mustdo,
Packit fb9d21
					    isl_map_range(isl_map_copy(dep)));
Packit fb9d21
		res->dep[i].map = isl_map_union(res->dep[i].map, dep);
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	res->may_no_source = isl_set_subtract(maydo, isl_set_copy(mustdo));
Packit fb9d21
	res->must_no_source = mustdo;
Packit fb9d21
Packit fb9d21
	return res;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Compute dependences for the case where there is at least one
Packit fb9d21
 * "must" access.
Packit fb9d21
 *
Packit fb9d21
 * The core algorithm considers all levels in which a source may precede
Packit fb9d21
 * the sink, where a level may either be a statement level or a loop level.
Packit fb9d21
 * The outermost statement level is 1, the first loop level is 2, etc...
Packit fb9d21
 * The algorithm basically does the following:
Packit fb9d21
 * for all levels l of the read access from innermost to outermost
Packit fb9d21
 *	for all sources w that may precede the sink access at that level
Packit fb9d21
 *	    compute the last iteration of the source that precedes the sink access
Packit fb9d21
 *					    at that level
Packit fb9d21
 *	    add result to possible last accesses at level l of source w
Packit fb9d21
 *	    for all sources w2 that we haven't considered yet at this level that may
Packit fb9d21
 *					    also precede the sink access
Packit fb9d21
 *		for all levels l2 of w from l to innermost
Packit fb9d21
 *		    for all possible last accesses dep of w at l
Packit fb9d21
 *			compute last iteration of w2 between the source and sink
Packit fb9d21
 *								of dep
Packit fb9d21
 *			add result to possible last accesses at level l of write w2
Packit fb9d21
 *			and replace possible last accesses dep by the remainder
Packit fb9d21
 *
Packit fb9d21
 *
Packit fb9d21
 * The above algorithm is applied to the must access.  During the course
Packit fb9d21
 * of the algorithm, we keep track of sink iterations that still
Packit fb9d21
 * need to be considered.  These iterations are split into those that
Packit fb9d21
 * haven't been matched to any source access (mustdo) and those that have only
Packit fb9d21
 * been matched to may accesses (maydo).
Packit fb9d21
 * At the end of each level, we also consider the may accesses.
Packit fb9d21
 * In particular, we consider may accesses that precede the remaining
Packit fb9d21
 * sink iterations, moving elements from mustdo to maydo when appropriate,
Packit fb9d21
 * and may accesses that occur between a must source and a sink of any 
Packit fb9d21
 * dependences found at the current level, turning must dependences into
Packit fb9d21
 * may dependences when appropriate.
Packit fb9d21
 * 
Packit fb9d21
 */
Packit fb9d21
static __isl_give isl_flow *compute_val_based_dependences(
Packit fb9d21
	__isl_keep isl_access_info *acc)
Packit fb9d21
{
Packit fb9d21
	isl_ctx *ctx;
Packit fb9d21
	isl_flow *res;
Packit fb9d21
	isl_set *mustdo = NULL;
Packit fb9d21
	isl_set *maydo = NULL;
Packit fb9d21
	int level, j;
Packit fb9d21
	int depth;
Packit fb9d21
	isl_map **must_rel = NULL;
Packit fb9d21
	isl_map **may_rel = NULL;
Packit fb9d21
Packit fb9d21
	if (!acc)
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	res = isl_flow_alloc(acc);
Packit fb9d21
	if (!res)
Packit fb9d21
		goto error;
Packit fb9d21
	ctx = isl_map_get_ctx(acc->sink.map);
Packit fb9d21
Packit fb9d21
	depth = 2 * isl_map_dim(acc->sink.map, isl_dim_in) + 1;
Packit fb9d21
	mustdo = isl_map_domain(isl_map_copy(acc->sink.map));
Packit fb9d21
	maydo = isl_set_empty(isl_set_get_space(mustdo));
Packit fb9d21
	if (!mustdo || !maydo)
Packit fb9d21
		goto error;
Packit fb9d21
	if (isl_set_plain_is_empty(mustdo))
Packit fb9d21
		goto done;
Packit fb9d21
Packit fb9d21
	must_rel = isl_alloc_array(ctx, struct isl_map *, acc->n_must);
Packit fb9d21
	may_rel = isl_alloc_array(ctx, struct isl_map *, acc->n_must);
Packit fb9d21
	if (!must_rel || !may_rel)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	for (level = depth; level >= 1; --level) {
Packit fb9d21
		for (j = acc->n_must-1; j >=0; --j) {
Packit fb9d21
			isl_space *space;
Packit fb9d21
			space = isl_map_get_space(res->dep[2 * j].map);
Packit fb9d21
			must_rel[j] = isl_map_empty(space);
Packit fb9d21
			may_rel[j] = isl_map_copy(must_rel[j]);
Packit fb9d21
		}
Packit fb9d21
Packit fb9d21
		for (j = acc->n_must - 1; j >= 0; --j) {
Packit fb9d21
			struct isl_map *T;
Packit fb9d21
			struct isl_set *rest;
Packit fb9d21
			int plevel;
Packit fb9d21
Packit fb9d21
			plevel = acc->level_before(acc->source[j].data,
Packit fb9d21
						     acc->sink.data);
Packit fb9d21
			if (!can_precede_at_level(plevel, level))
Packit fb9d21
				continue;
Packit fb9d21
Packit fb9d21
			T = last_source(acc, mustdo, j, level, &rest);
Packit fb9d21
			must_rel[j] = isl_map_union_disjoint(must_rel[j], T);
Packit fb9d21
			mustdo = rest;
Packit fb9d21
Packit fb9d21
			intermediate_sources(acc, must_rel, j, level);
Packit fb9d21
Packit fb9d21
			T = last_source(acc, maydo, j, level, &rest);
Packit fb9d21
			may_rel[j] = isl_map_union_disjoint(may_rel[j], T);
Packit fb9d21
			maydo = rest;
Packit fb9d21
Packit fb9d21
			intermediate_sources(acc, may_rel, j, level);
Packit fb9d21
Packit fb9d21
			if (isl_set_plain_is_empty(mustdo) &&
Packit fb9d21
			    isl_set_plain_is_empty(maydo))
Packit fb9d21
				break;
Packit fb9d21
		}
Packit fb9d21
		for (j = j - 1; j >= 0; --j) {
Packit fb9d21
			int plevel;
Packit fb9d21
Packit fb9d21
			plevel = acc->level_before(acc->source[j].data,
Packit fb9d21
						     acc->sink.data);
Packit fb9d21
			if (!can_precede_at_level(plevel, level))
Packit fb9d21
				continue;
Packit fb9d21
Packit fb9d21
			intermediate_sources(acc, must_rel, j, level);
Packit fb9d21
			intermediate_sources(acc, may_rel, j, level);
Packit fb9d21
		}
Packit fb9d21
Packit fb9d21
		for (j = 0; j < acc->n_may; ++j) {
Packit fb9d21
			int plevel;
Packit fb9d21
			isl_map *T;
Packit fb9d21
			isl_set *ran;
Packit fb9d21
Packit fb9d21
			plevel = acc->level_before(acc->source[acc->n_must + j].data,
Packit fb9d21
						     acc->sink.data);
Packit fb9d21
			if (!can_precede_at_level(plevel, level))
Packit fb9d21
				continue;
Packit fb9d21
Packit fb9d21
			T = all_sources(acc, isl_set_copy(maydo), j, level);
Packit fb9d21
			res->dep[2 * acc->n_must + j].map =
Packit fb9d21
			    isl_map_union(res->dep[2 * acc->n_must + j].map, T);
Packit fb9d21
			T = all_sources(acc, isl_set_copy(mustdo), j, level);
Packit fb9d21
			ran = isl_map_range(isl_map_copy(T));
Packit fb9d21
			res->dep[2 * acc->n_must + j].map =
Packit fb9d21
			    isl_map_union(res->dep[2 * acc->n_must + j].map, T);
Packit fb9d21
			mustdo = isl_set_subtract(mustdo, isl_set_copy(ran));
Packit fb9d21
			maydo = isl_set_union_disjoint(maydo, ran);
Packit fb9d21
Packit fb9d21
			T = res->dep[2 * acc->n_must + j].map;
Packit fb9d21
			T = all_intermediate_sources(acc, T, must_rel, may_rel,
Packit fb9d21
							j, level);
Packit fb9d21
			res->dep[2 * acc->n_must + j].map = T;
Packit fb9d21
		}
Packit fb9d21
Packit fb9d21
		for (j = acc->n_must - 1; j >= 0; --j) {
Packit fb9d21
			res->dep[2 * j].map =
Packit fb9d21
				isl_map_union_disjoint(res->dep[2 * j].map,
Packit fb9d21
							     must_rel[j]);
Packit fb9d21
			res->dep[2 * j + 1].map =
Packit fb9d21
				isl_map_union_disjoint(res->dep[2 * j + 1].map,
Packit fb9d21
							     may_rel[j]);
Packit fb9d21
		}
Packit fb9d21
Packit fb9d21
		if (isl_set_plain_is_empty(mustdo) &&
Packit fb9d21
		    isl_set_plain_is_empty(maydo))
Packit fb9d21
			break;
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	free(must_rel);
Packit fb9d21
	free(may_rel);
Packit fb9d21
done:
Packit fb9d21
	res->must_no_source = mustdo;
Packit fb9d21
	res->may_no_source = maydo;
Packit fb9d21
	return res;
Packit fb9d21
error:
Packit fb9d21
	isl_flow_free(res);
Packit fb9d21
	isl_set_free(mustdo);
Packit fb9d21
	isl_set_free(maydo);
Packit fb9d21
	free(must_rel);
Packit fb9d21
	free(may_rel);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Given a "sink" access, a list of n "source" accesses,
Packit fb9d21
 * compute for each iteration of the sink access
Packit fb9d21
 * and for each element accessed by that iteration,
Packit fb9d21
 * the source access in the list that last accessed the
Packit fb9d21
 * element accessed by the sink access before this sink access.
Packit fb9d21
 * Each access is given as a map from the loop iterators
Packit fb9d21
 * to the array indices.
Packit fb9d21
 * The result is a list of n relations between source and sink
Packit fb9d21
 * iterations and a subset of the domain of the sink access,
Packit fb9d21
 * corresponding to those iterations that access an element
Packit fb9d21
 * not previously accessed.
Packit fb9d21
 *
Packit fb9d21
 * To deal with multi-valued sink access relations, the sink iteration
Packit fb9d21
 * domain is first extended with dimensions that correspond to the data
Packit fb9d21
 * space.  However, these extra dimensions are not projected out again.
Packit fb9d21
 * It is up to the caller to decide whether these dimensions should be kept.
Packit fb9d21
 */
Packit fb9d21
static __isl_give isl_flow *access_info_compute_flow_core(
Packit fb9d21
	__isl_take isl_access_info *acc)
Packit fb9d21
{
Packit fb9d21
	struct isl_flow *res = NULL;
Packit fb9d21
Packit fb9d21
	if (!acc)
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	acc->sink.map = isl_map_range_map(acc->sink.map);
Packit fb9d21
	if (!acc->sink.map)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	if (acc->n_must == 0)
Packit fb9d21
		res = compute_mem_based_dependences(acc);
Packit fb9d21
	else {
Packit fb9d21
		acc = isl_access_info_sort_sources(acc);
Packit fb9d21
		res = compute_val_based_dependences(acc);
Packit fb9d21
	}
Packit fb9d21
	acc = isl_access_info_free(acc);
Packit fb9d21
	if (!res)
Packit fb9d21
		return NULL;
Packit fb9d21
	if (!res->must_no_source || !res->may_no_source)
Packit fb9d21
		goto error;
Packit fb9d21
	return res;
Packit fb9d21
error:
Packit fb9d21
	isl_access_info_free(acc);
Packit fb9d21
	isl_flow_free(res);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Given a "sink" access, a list of n "source" accesses,
Packit fb9d21
 * compute for each iteration of the sink access
Packit fb9d21
 * and for each element accessed by that iteration,
Packit fb9d21
 * the source access in the list that last accessed the
Packit fb9d21
 * element accessed by the sink access before this sink access.
Packit fb9d21
 * Each access is given as a map from the loop iterators
Packit fb9d21
 * to the array indices.
Packit fb9d21
 * The result is a list of n relations between source and sink
Packit fb9d21
 * iterations and a subset of the domain of the sink access,
Packit fb9d21
 * corresponding to those iterations that access an element
Packit fb9d21
 * not previously accessed.
Packit fb9d21
 *
Packit fb9d21
 * To deal with multi-valued sink access relations,
Packit fb9d21
 * access_info_compute_flow_core extends the sink iteration domain
Packit fb9d21
 * with dimensions that correspond to the data space.  These extra dimensions
Packit fb9d21
 * are projected out from the result of access_info_compute_flow_core.
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_flow *isl_access_info_compute_flow(__isl_take isl_access_info *acc)
Packit fb9d21
{
Packit fb9d21
	int j;
Packit fb9d21
	struct isl_flow *res;
Packit fb9d21
Packit fb9d21
	if (!acc)
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	acc->domain_map = isl_map_domain_map(isl_map_copy(acc->sink.map));
Packit fb9d21
	res = access_info_compute_flow_core(acc);
Packit fb9d21
	if (!res)
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	for (j = 0; j < res->n_source; ++j) {
Packit fb9d21
		res->dep[j].map = isl_map_range_factor_domain(res->dep[j].map);
Packit fb9d21
		if (!res->dep[j].map)
Packit fb9d21
			goto error;
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	return res;
Packit fb9d21
error:
Packit fb9d21
	isl_flow_free(res);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
Packit fb9d21
/* Keep track of some information about a schedule for a given
Packit fb9d21
 * access.  In particular, keep track of which dimensions
Packit fb9d21
 * have a constant value and of the actual constant values.
Packit fb9d21
 */
Packit fb9d21
struct isl_sched_info {
Packit fb9d21
	int *is_cst;
Packit fb9d21
	isl_vec *cst;
Packit fb9d21
};
Packit fb9d21
Packit fb9d21
static void sched_info_free(__isl_take struct isl_sched_info *info)
Packit fb9d21
{
Packit fb9d21
	if (!info)
Packit fb9d21
		return;
Packit fb9d21
	isl_vec_free(info->cst);
Packit fb9d21
	free(info->is_cst);
Packit fb9d21
	free(info);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Extract information on the constant dimensions of the schedule
Packit fb9d21
 * for a given access.  The "map" is of the form
Packit fb9d21
 *
Packit fb9d21
 *	[S -> D] -> A
Packit fb9d21
 *
Packit fb9d21
 * with S the schedule domain, D the iteration domain and A the data domain.
Packit fb9d21
 */
Packit fb9d21
static __isl_give struct isl_sched_info *sched_info_alloc(
Packit fb9d21
	__isl_keep isl_map *map)
Packit fb9d21
{
Packit fb9d21
	isl_ctx *ctx;
Packit fb9d21
	isl_space *dim;
Packit fb9d21
	struct isl_sched_info *info;
Packit fb9d21
	int i, n;
Packit fb9d21
Packit fb9d21
	if (!map)
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	dim = isl_space_unwrap(isl_space_domain(isl_map_get_space(map)));
Packit fb9d21
	if (!dim)
Packit fb9d21
		return NULL;
Packit fb9d21
	n = isl_space_dim(dim, isl_dim_in);
Packit fb9d21
	isl_space_free(dim);
Packit fb9d21
Packit fb9d21
	ctx = isl_map_get_ctx(map);
Packit fb9d21
	info = isl_alloc_type(ctx, struct isl_sched_info);
Packit fb9d21
	if (!info)
Packit fb9d21
		return NULL;
Packit fb9d21
	info->is_cst = isl_alloc_array(ctx, int, n);
Packit fb9d21
	info->cst = isl_vec_alloc(ctx, n);
Packit fb9d21
	if (n && (!info->is_cst || !info->cst))
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	for (i = 0; i < n; ++i) {
Packit fb9d21
		isl_val *v;
Packit fb9d21
Packit fb9d21
		v = isl_map_plain_get_val_if_fixed(map, isl_dim_in, i);
Packit fb9d21
		if (!v)
Packit fb9d21
			goto error;
Packit fb9d21
		info->is_cst[i] = !isl_val_is_nan(v);
Packit fb9d21
		if (info->is_cst[i])
Packit fb9d21
			info->cst = isl_vec_set_element_val(info->cst, i, v);
Packit fb9d21
		else
Packit fb9d21
			isl_val_free(v);
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	return info;
Packit fb9d21
error:
Packit fb9d21
	sched_info_free(info);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* This structure represents the input for a dependence analysis computation.
Packit fb9d21
 *
Packit fb9d21
 * "sink" represents the sink accesses.
Packit fb9d21
 * "must_source" represents the definite source accesses.
Packit fb9d21
 * "may_source" represents the possible source accesses.
Packit fb9d21
 *
Packit fb9d21
 * "schedule" or "schedule_map" represents the execution order.
Packit fb9d21
 * Exactly one of these fields should be NULL.  The other field
Packit fb9d21
 * determines the execution order.
Packit fb9d21
 *
Packit fb9d21
 * The domains of these four maps refer to the same iteration spaces(s).
Packit fb9d21
 * The ranges of the first three maps also refer to the same data space(s).
Packit fb9d21
 *
Packit fb9d21
 * After a call to isl_union_access_info_introduce_schedule,
Packit fb9d21
 * the "schedule_map" field no longer contains useful information.
Packit fb9d21
 */
Packit fb9d21
struct isl_union_access_info {
Packit fb9d21
	isl_union_map *sink;
Packit fb9d21
	isl_union_map *must_source;
Packit fb9d21
	isl_union_map *may_source;
Packit fb9d21
Packit fb9d21
	isl_schedule *schedule;
Packit fb9d21
	isl_union_map *schedule_map;
Packit fb9d21
};
Packit fb9d21
Packit fb9d21
/* Free "access" and return NULL.
Packit fb9d21
 */
Packit fb9d21
__isl_null isl_union_access_info *isl_union_access_info_free(
Packit fb9d21
	__isl_take isl_union_access_info *access)
Packit fb9d21
{
Packit fb9d21
	if (!access)
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	isl_union_map_free(access->sink);
Packit fb9d21
	isl_union_map_free(access->must_source);
Packit fb9d21
	isl_union_map_free(access->may_source);
Packit fb9d21
	isl_schedule_free(access->schedule);
Packit fb9d21
	isl_union_map_free(access->schedule_map);
Packit fb9d21
	free(access);
Packit fb9d21
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Return the isl_ctx to which "access" belongs.
Packit fb9d21
 */
Packit fb9d21
isl_ctx *isl_union_access_info_get_ctx(__isl_keep isl_union_access_info *access)
Packit fb9d21
{
Packit fb9d21
	return access ? isl_union_map_get_ctx(access->sink) : NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Create a new isl_union_access_info with the given sink accesses and
Packit fb9d21
 * and no source accesses or schedule information.
Packit fb9d21
 *
Packit fb9d21
 * By default, we use the schedule field of the isl_union_access_info,
Packit fb9d21
 * but this may be overridden by a call
Packit fb9d21
 * to isl_union_access_info_set_schedule_map.
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_union_access_info *isl_union_access_info_from_sink(
Packit fb9d21
	__isl_take isl_union_map *sink)
Packit fb9d21
{
Packit fb9d21
	isl_ctx *ctx;
Packit fb9d21
	isl_space *space;
Packit fb9d21
	isl_union_map *empty;
Packit fb9d21
	isl_union_access_info *access;
Packit fb9d21
Packit fb9d21
	if (!sink)
Packit fb9d21
		return NULL;
Packit fb9d21
	ctx = isl_union_map_get_ctx(sink);
Packit fb9d21
	access = isl_alloc_type(ctx, isl_union_access_info);
Packit fb9d21
	if (!access)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	space = isl_union_map_get_space(sink);
Packit fb9d21
	empty = isl_union_map_empty(isl_space_copy(space));
Packit fb9d21
	access->sink = sink;
Packit fb9d21
	access->must_source = isl_union_map_copy(empty);
Packit fb9d21
	access->may_source = empty;
Packit fb9d21
	access->schedule = isl_schedule_empty(space);
Packit fb9d21
	access->schedule_map = NULL;
Packit fb9d21
Packit fb9d21
	if (!access->sink || !access->must_source ||
Packit fb9d21
	    !access->may_source || !access->schedule)
Packit fb9d21
		return isl_union_access_info_free(access);
Packit fb9d21
Packit fb9d21
	return access;
Packit fb9d21
error:
Packit fb9d21
	isl_union_map_free(sink);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Replace the definite source accesses of "access" by "must_source".
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_union_access_info *isl_union_access_info_set_must_source(
Packit fb9d21
	__isl_take isl_union_access_info *access,
Packit fb9d21
	__isl_take isl_union_map *must_source)
Packit fb9d21
{
Packit fb9d21
	if (!access || !must_source)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	isl_union_map_free(access->must_source);
Packit fb9d21
	access->must_source = must_source;
Packit fb9d21
Packit fb9d21
	return access;
Packit fb9d21
error:
Packit fb9d21
	isl_union_access_info_free(access);
Packit fb9d21
	isl_union_map_free(must_source);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Replace the possible source accesses of "access" by "may_source".
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_union_access_info *isl_union_access_info_set_may_source(
Packit fb9d21
	__isl_take isl_union_access_info *access,
Packit fb9d21
	__isl_take isl_union_map *may_source)
Packit fb9d21
{
Packit fb9d21
	if (!access || !may_source)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	isl_union_map_free(access->may_source);
Packit fb9d21
	access->may_source = may_source;
Packit fb9d21
Packit fb9d21
	return access;
Packit fb9d21
error:
Packit fb9d21
	isl_union_access_info_free(access);
Packit fb9d21
	isl_union_map_free(may_source);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Replace the schedule of "access" by "schedule".
Packit fb9d21
 * Also free the schedule_map in case it was set last.
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_union_access_info *isl_union_access_info_set_schedule(
Packit fb9d21
	__isl_take isl_union_access_info *access,
Packit fb9d21
	__isl_take isl_schedule *schedule)
Packit fb9d21
{
Packit fb9d21
	if (!access || !schedule)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	access->schedule_map = isl_union_map_free(access->schedule_map);
Packit fb9d21
	isl_schedule_free(access->schedule);
Packit fb9d21
	access->schedule = schedule;
Packit fb9d21
Packit fb9d21
	return access;
Packit fb9d21
error:
Packit fb9d21
	isl_union_access_info_free(access);
Packit fb9d21
	isl_schedule_free(schedule);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Replace the schedule map of "access" by "schedule_map".
Packit fb9d21
 * Also free the schedule in case it was set last.
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_union_access_info *isl_union_access_info_set_schedule_map(
Packit fb9d21
	__isl_take isl_union_access_info *access,
Packit fb9d21
	__isl_take isl_union_map *schedule_map)
Packit fb9d21
{
Packit fb9d21
	if (!access || !schedule_map)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	isl_union_map_free(access->schedule_map);
Packit fb9d21
	access->schedule = isl_schedule_free(access->schedule);
Packit fb9d21
	access->schedule_map = schedule_map;
Packit fb9d21
Packit fb9d21
	return access;
Packit fb9d21
error:
Packit fb9d21
	isl_union_access_info_free(access);
Packit fb9d21
	isl_union_map_free(schedule_map);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
__isl_give isl_union_access_info *isl_union_access_info_copy(
Packit fb9d21
	__isl_keep isl_union_access_info *access)
Packit fb9d21
{
Packit fb9d21
	isl_union_access_info *copy;
Packit fb9d21
Packit fb9d21
	if (!access)
Packit fb9d21
		return NULL;
Packit fb9d21
	copy = isl_union_access_info_from_sink(
Packit fb9d21
				isl_union_map_copy(access->sink));
Packit fb9d21
	copy = isl_union_access_info_set_must_source(copy,
Packit fb9d21
				isl_union_map_copy(access->must_source));
Packit fb9d21
	copy = isl_union_access_info_set_may_source(copy,
Packit fb9d21
				isl_union_map_copy(access->may_source));
Packit fb9d21
	if (access->schedule)
Packit fb9d21
		copy = isl_union_access_info_set_schedule(copy,
Packit fb9d21
				isl_schedule_copy(access->schedule));
Packit fb9d21
	else
Packit fb9d21
		copy = isl_union_access_info_set_schedule_map(copy,
Packit fb9d21
				isl_union_map_copy(access->schedule_map));
Packit fb9d21
Packit fb9d21
	return copy;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Print a key-value pair of a YAML mapping to "p",
Packit fb9d21
 * with key "name" and value "umap".
Packit fb9d21
 */
Packit fb9d21
static __isl_give isl_printer *print_union_map_field(__isl_take isl_printer *p,
Packit fb9d21
	const char *name, __isl_keep isl_union_map *umap)
Packit fb9d21
{
Packit fb9d21
	p = isl_printer_print_str(p, name);
Packit fb9d21
	p = isl_printer_yaml_next(p);
Packit fb9d21
	p = isl_printer_print_str(p, "\"");
Packit fb9d21
	p = isl_printer_print_union_map(p, umap);
Packit fb9d21
	p = isl_printer_print_str(p, "\"");
Packit fb9d21
	p = isl_printer_yaml_next(p);
Packit fb9d21
Packit fb9d21
	return p;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Print the information contained in "access" to "p".
Packit fb9d21
 * The information is printed as a YAML document.
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_printer *isl_printer_print_union_access_info(
Packit fb9d21
	__isl_take isl_printer *p, __isl_keep isl_union_access_info *access)
Packit fb9d21
{
Packit fb9d21
	if (!access)
Packit fb9d21
		return isl_printer_free(p);
Packit fb9d21
Packit fb9d21
	p = isl_printer_yaml_start_mapping(p);
Packit fb9d21
	p = print_union_map_field(p, "sink", access->sink);
Packit fb9d21
	p = print_union_map_field(p, "must_source", access->must_source);
Packit fb9d21
	p = print_union_map_field(p, "may_source", access->may_source);
Packit fb9d21
	if (access->schedule) {
Packit fb9d21
		p = isl_printer_print_str(p, "schedule");
Packit fb9d21
		p = isl_printer_yaml_next(p);
Packit fb9d21
		p = isl_printer_print_schedule(p, access->schedule);
Packit fb9d21
		p = isl_printer_yaml_next(p);
Packit fb9d21
	} else {
Packit fb9d21
		p = print_union_map_field(p, "schedule_map",
Packit fb9d21
						access->schedule_map);
Packit fb9d21
	}
Packit fb9d21
	p = isl_printer_yaml_end_mapping(p);
Packit fb9d21
Packit fb9d21
	return p;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Return a string representation of the information in "access".
Packit fb9d21
 * The information is printed in flow format.
Packit fb9d21
 */
Packit fb9d21
__isl_give char *isl_union_access_info_to_str(
Packit fb9d21
	__isl_keep isl_union_access_info *access)
Packit fb9d21
{
Packit fb9d21
	isl_printer *p;
Packit fb9d21
	char *s;
Packit fb9d21
Packit fb9d21
	if (!access)
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	p = isl_printer_to_str(isl_union_access_info_get_ctx(access));
Packit fb9d21
	p = isl_printer_set_yaml_style(p, ISL_YAML_STYLE_FLOW);
Packit fb9d21
	p = isl_printer_print_union_access_info(p, access);
Packit fb9d21
	s = isl_printer_get_str(p);
Packit fb9d21
	isl_printer_free(p);
Packit fb9d21
Packit fb9d21
	return s;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Update the fields of "access" such that they all have the same parameters,
Packit fb9d21
 * keeping in mind that the schedule_map field may be NULL and ignoring
Packit fb9d21
 * the schedule field.
Packit fb9d21
 */
Packit fb9d21
static __isl_give isl_union_access_info *isl_union_access_info_align_params(
Packit fb9d21
	__isl_take isl_union_access_info *access)
Packit fb9d21
{
Packit fb9d21
	isl_space *space;
Packit fb9d21
Packit fb9d21
	if (!access)
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	space = isl_union_map_get_space(access->sink);
Packit fb9d21
	space = isl_space_align_params(space,
Packit fb9d21
				isl_union_map_get_space(access->must_source));
Packit fb9d21
	space = isl_space_align_params(space,
Packit fb9d21
				isl_union_map_get_space(access->may_source));
Packit fb9d21
	if (access->schedule_map)
Packit fb9d21
		space = isl_space_align_params(space,
Packit fb9d21
				isl_union_map_get_space(access->schedule_map));
Packit fb9d21
	access->sink = isl_union_map_align_params(access->sink,
Packit fb9d21
							isl_space_copy(space));
Packit fb9d21
	access->must_source = isl_union_map_align_params(access->must_source,
Packit fb9d21
							isl_space_copy(space));
Packit fb9d21
	access->may_source = isl_union_map_align_params(access->may_source,
Packit fb9d21
							isl_space_copy(space));
Packit fb9d21
	if (!access->schedule_map) {
Packit fb9d21
		isl_space_free(space);
Packit fb9d21
	} else {
Packit fb9d21
		access->schedule_map =
Packit fb9d21
		    isl_union_map_align_params(access->schedule_map, space);
Packit fb9d21
		if (!access->schedule_map)
Packit fb9d21
			return isl_union_access_info_free(access);
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	if (!access->sink || !access->must_source || !access->may_source)
Packit fb9d21
		return isl_union_access_info_free(access);
Packit fb9d21
Packit fb9d21
	return access;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Prepend the schedule dimensions to the iteration domains.
Packit fb9d21
 *
Packit fb9d21
 * That is, if the schedule is of the form
Packit fb9d21
 *
Packit fb9d21
 *	D -> S
Packit fb9d21
 *
Packit fb9d21
 * while the access relations are of the form
Packit fb9d21
 *
Packit fb9d21
 *	D -> A
Packit fb9d21
 *
Packit fb9d21
 * then the updated access relations are of the form
Packit fb9d21
 *
Packit fb9d21
 *	[S -> D] -> A
Packit fb9d21
 *
Packit fb9d21
 * The schedule map is also replaced by the map
Packit fb9d21
 *
Packit fb9d21
 *	[S -> D] -> D
Packit fb9d21
 *
Packit fb9d21
 * that is used during the internal computation.
Packit fb9d21
 * Neither the original schedule map nor this updated schedule map
Packit fb9d21
 * are used after the call to this function.
Packit fb9d21
 */
Packit fb9d21
static __isl_give isl_union_access_info *
Packit fb9d21
isl_union_access_info_introduce_schedule(
Packit fb9d21
	__isl_take isl_union_access_info *access)
Packit fb9d21
{
Packit fb9d21
	isl_union_map *sm;
Packit fb9d21
Packit fb9d21
	if (!access)
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	sm = isl_union_map_reverse(access->schedule_map);
Packit fb9d21
	sm = isl_union_map_range_map(sm);
Packit fb9d21
	access->sink = isl_union_map_apply_range(isl_union_map_copy(sm),
Packit fb9d21
						access->sink);
Packit fb9d21
	access->may_source = isl_union_map_apply_range(isl_union_map_copy(sm),
Packit fb9d21
						access->may_source);
Packit fb9d21
	access->must_source = isl_union_map_apply_range(isl_union_map_copy(sm),
Packit fb9d21
						access->must_source);
Packit fb9d21
	access->schedule_map = sm;
Packit fb9d21
Packit fb9d21
	if (!access->sink || !access->must_source ||
Packit fb9d21
	    !access->may_source || !access->schedule_map)
Packit fb9d21
		return isl_union_access_info_free(access);
Packit fb9d21
Packit fb9d21
	return access;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* This structure represents the result of a dependence analysis computation.
Packit fb9d21
 *
Packit fb9d21
 * "must_dep" represents the full definite dependences
Packit fb9d21
 * "may_dep" represents the full non-definite dependences.
Packit fb9d21
 * Both are of the form
Packit fb9d21
 *
Packit fb9d21
 *	[Source] -> [[Sink -> Data]]
Packit fb9d21
 *
Packit fb9d21
 * (after the schedule dimensions have been projected out).
Packit fb9d21
 * "must_no_source" represents the subset of the sink accesses for which
Packit fb9d21
 * definitely no source was found.
Packit fb9d21
 * "may_no_source" represents the subset of the sink accesses for which
Packit fb9d21
 * possibly, but not definitely, no source was found.
Packit fb9d21
 */
Packit fb9d21
struct isl_union_flow {
Packit fb9d21
	isl_union_map *must_dep;
Packit fb9d21
	isl_union_map *may_dep;
Packit fb9d21
	isl_union_map *must_no_source;
Packit fb9d21
	isl_union_map *may_no_source;
Packit fb9d21
};
Packit fb9d21
Packit fb9d21
/* Return the isl_ctx to which "flow" belongs.
Packit fb9d21
 */
Packit fb9d21
isl_ctx *isl_union_flow_get_ctx(__isl_keep isl_union_flow *flow)
Packit fb9d21
{
Packit fb9d21
	return flow ? isl_union_map_get_ctx(flow->must_dep) : NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Free "flow" and return NULL.
Packit fb9d21
 */
Packit fb9d21
__isl_null isl_union_flow *isl_union_flow_free(__isl_take isl_union_flow *flow)
Packit fb9d21
{
Packit fb9d21
	if (!flow)
Packit fb9d21
		return NULL;
Packit fb9d21
	isl_union_map_free(flow->must_dep);
Packit fb9d21
	isl_union_map_free(flow->may_dep);
Packit fb9d21
	isl_union_map_free(flow->must_no_source);
Packit fb9d21
	isl_union_map_free(flow->may_no_source);
Packit fb9d21
	free(flow);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
void isl_union_flow_dump(__isl_keep isl_union_flow *flow)
Packit fb9d21
{
Packit fb9d21
	if (!flow)
Packit fb9d21
		return;
Packit fb9d21
Packit fb9d21
	fprintf(stderr, "must dependences: ");
Packit fb9d21
	isl_union_map_dump(flow->must_dep);
Packit fb9d21
	fprintf(stderr, "may dependences: ");
Packit fb9d21
	isl_union_map_dump(flow->may_dep);
Packit fb9d21
	fprintf(stderr, "must no source: ");
Packit fb9d21
	isl_union_map_dump(flow->must_no_source);
Packit fb9d21
	fprintf(stderr, "may no source: ");
Packit fb9d21
	isl_union_map_dump(flow->may_no_source);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Return the full definite dependences in "flow", with accessed elements.
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_union_map *isl_union_flow_get_full_must_dependence(
Packit fb9d21
	__isl_keep isl_union_flow *flow)
Packit fb9d21
{
Packit fb9d21
	if (!flow)
Packit fb9d21
		return NULL;
Packit fb9d21
	return isl_union_map_copy(flow->must_dep);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Return the full possible dependences in "flow", including the definite
Packit fb9d21
 * dependences, with accessed elements.
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_union_map *isl_union_flow_get_full_may_dependence(
Packit fb9d21
	__isl_keep isl_union_flow *flow)
Packit fb9d21
{
Packit fb9d21
	if (!flow)
Packit fb9d21
		return NULL;
Packit fb9d21
	return isl_union_map_union(isl_union_map_copy(flow->must_dep),
Packit fb9d21
				    isl_union_map_copy(flow->may_dep));
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Return the definite dependences in "flow", without the accessed elements.
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_union_map *isl_union_flow_get_must_dependence(
Packit fb9d21
	__isl_keep isl_union_flow *flow)
Packit fb9d21
{
Packit fb9d21
	isl_union_map *dep;
Packit fb9d21
Packit fb9d21
	if (!flow)
Packit fb9d21
		return NULL;
Packit fb9d21
	dep = isl_union_map_copy(flow->must_dep);
Packit fb9d21
	return isl_union_map_range_factor_domain(dep);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Return the possible dependences in "flow", including the definite
Packit fb9d21
 * dependences, without the accessed elements.
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_union_map *isl_union_flow_get_may_dependence(
Packit fb9d21
	__isl_keep isl_union_flow *flow)
Packit fb9d21
{
Packit fb9d21
	isl_union_map *dep;
Packit fb9d21
Packit fb9d21
	if (!flow)
Packit fb9d21
		return NULL;
Packit fb9d21
	dep = isl_union_map_union(isl_union_map_copy(flow->must_dep),
Packit fb9d21
				    isl_union_map_copy(flow->may_dep));
Packit fb9d21
	return isl_union_map_range_factor_domain(dep);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Return the non-definite dependences in "flow".
Packit fb9d21
 */
Packit fb9d21
static __isl_give isl_union_map *isl_union_flow_get_non_must_dependence(
Packit fb9d21
	__isl_keep isl_union_flow *flow)
Packit fb9d21
{
Packit fb9d21
	if (!flow)
Packit fb9d21
		return NULL;
Packit fb9d21
	return isl_union_map_copy(flow->may_dep);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Return the subset of the sink accesses for which definitely
Packit fb9d21
 * no source was found.
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_union_map *isl_union_flow_get_must_no_source(
Packit fb9d21
	__isl_keep isl_union_flow *flow)
Packit fb9d21
{
Packit fb9d21
	if (!flow)
Packit fb9d21
		return NULL;
Packit fb9d21
	return isl_union_map_copy(flow->must_no_source);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Return the subset of the sink accesses for which possibly
Packit fb9d21
 * no source was found, including those for which definitely
Packit fb9d21
 * no source was found.
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_union_map *isl_union_flow_get_may_no_source(
Packit fb9d21
	__isl_keep isl_union_flow *flow)
Packit fb9d21
{
Packit fb9d21
	if (!flow)
Packit fb9d21
		return NULL;
Packit fb9d21
	return isl_union_map_union(isl_union_map_copy(flow->must_no_source),
Packit fb9d21
				    isl_union_map_copy(flow->may_no_source));
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Return the subset of the sink accesses for which possibly, but not
Packit fb9d21
 * definitely, no source was found.
Packit fb9d21
 */
Packit fb9d21
static __isl_give isl_union_map *isl_union_flow_get_non_must_no_source(
Packit fb9d21
	__isl_keep isl_union_flow *flow)
Packit fb9d21
{
Packit fb9d21
	if (!flow)
Packit fb9d21
		return NULL;
Packit fb9d21
	return isl_union_map_copy(flow->may_no_source);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Create a new isl_union_flow object, initialized with empty
Packit fb9d21
 * dependence relations and sink subsets.
Packit fb9d21
 */
Packit fb9d21
static __isl_give isl_union_flow *isl_union_flow_alloc(
Packit fb9d21
	__isl_take isl_space *space)
Packit fb9d21
{
Packit fb9d21
	isl_ctx *ctx;
Packit fb9d21
	isl_union_map *empty;
Packit fb9d21
	isl_union_flow *flow;
Packit fb9d21
Packit fb9d21
	if (!space)
Packit fb9d21
		return NULL;
Packit fb9d21
	ctx = isl_space_get_ctx(space);
Packit fb9d21
	flow = isl_alloc_type(ctx, isl_union_flow);
Packit fb9d21
	if (!flow)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	empty = isl_union_map_empty(space);
Packit fb9d21
	flow->must_dep = isl_union_map_copy(empty);
Packit fb9d21
	flow->may_dep = isl_union_map_copy(empty);
Packit fb9d21
	flow->must_no_source = isl_union_map_copy(empty);
Packit fb9d21
	flow->may_no_source = empty;
Packit fb9d21
Packit fb9d21
	if (!flow->must_dep || !flow->may_dep ||
Packit fb9d21
	    !flow->must_no_source || !flow->may_no_source)
Packit fb9d21
		return isl_union_flow_free(flow);
Packit fb9d21
Packit fb9d21
	return flow;
Packit fb9d21
error:
Packit fb9d21
	isl_space_free(space);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Drop the schedule dimensions from the iteration domains in "flow".
Packit fb9d21
 * In particular, the schedule dimensions have been prepended
Packit fb9d21
 * to the iteration domains prior to the dependence analysis by
Packit fb9d21
 * replacing the iteration domain D, by the wrapped map [S -> D].
Packit fb9d21
 * Replace these wrapped maps by the original D.
Packit fb9d21
 *
Packit fb9d21
 * In particular, the dependences computed by access_info_compute_flow_core
Packit fb9d21
 * are of the form
Packit fb9d21
 *
Packit fb9d21
 *	[S -> D] -> [[S' -> D'] -> A]
Packit fb9d21
 *
Packit fb9d21
 * The schedule dimensions are projected out by first currying the range,
Packit fb9d21
 * resulting in
Packit fb9d21
 *
Packit fb9d21
 *	[S -> D] -> [S' -> [D' -> A]]
Packit fb9d21
 *
Packit fb9d21
 * and then computing the factor range
Packit fb9d21
 *
Packit fb9d21
 *	D -> [D' -> A]
Packit fb9d21
 */
Packit fb9d21
static __isl_give isl_union_flow *isl_union_flow_drop_schedule(
Packit fb9d21
	__isl_take isl_union_flow *flow)
Packit fb9d21
{
Packit fb9d21
	if (!flow)
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	flow->must_dep = isl_union_map_range_curry(flow->must_dep);
Packit fb9d21
	flow->must_dep = isl_union_map_factor_range(flow->must_dep);
Packit fb9d21
	flow->may_dep = isl_union_map_range_curry(flow->may_dep);
Packit fb9d21
	flow->may_dep = isl_union_map_factor_range(flow->may_dep);
Packit fb9d21
	flow->must_no_source =
Packit fb9d21
		isl_union_map_domain_factor_range(flow->must_no_source);
Packit fb9d21
	flow->may_no_source =
Packit fb9d21
		isl_union_map_domain_factor_range(flow->may_no_source);
Packit fb9d21
Packit fb9d21
	if (!flow->must_dep || !flow->may_dep ||
Packit fb9d21
	    !flow->must_no_source || !flow->may_no_source)
Packit fb9d21
		return isl_union_flow_free(flow);
Packit fb9d21
Packit fb9d21
	return flow;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
struct isl_compute_flow_data {
Packit fb9d21
	isl_union_map *must_source;
Packit fb9d21
	isl_union_map *may_source;
Packit fb9d21
	isl_union_flow *flow;
Packit fb9d21
Packit fb9d21
	int count;
Packit fb9d21
	int must;
Packit fb9d21
	isl_space *dim;
Packit fb9d21
	struct isl_sched_info *sink_info;
Packit fb9d21
	struct isl_sched_info **source_info;
Packit fb9d21
	isl_access_info *accesses;
Packit fb9d21
};
Packit fb9d21
Packit fb9d21
static isl_stat count_matching_array(__isl_take isl_map *map, void *user)
Packit fb9d21
{
Packit fb9d21
	int eq;
Packit fb9d21
	isl_space *dim;
Packit fb9d21
	struct isl_compute_flow_data *data;
Packit fb9d21
Packit fb9d21
	data = (struct isl_compute_flow_data *)user;
Packit fb9d21
Packit fb9d21
	dim = isl_space_range(isl_map_get_space(map));
Packit fb9d21
Packit fb9d21
	eq = isl_space_is_equal(dim, data->dim);
Packit fb9d21
Packit fb9d21
	isl_space_free(dim);
Packit fb9d21
	isl_map_free(map);
Packit fb9d21
Packit fb9d21
	if (eq < 0)
Packit fb9d21
		return isl_stat_error;
Packit fb9d21
	if (eq)
Packit fb9d21
		data->count++;
Packit fb9d21
Packit fb9d21
	return isl_stat_ok;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
static isl_stat collect_matching_array(__isl_take isl_map *map, void *user)
Packit fb9d21
{
Packit fb9d21
	int eq;
Packit fb9d21
	isl_space *dim;
Packit fb9d21
	struct isl_sched_info *info;
Packit fb9d21
	struct isl_compute_flow_data *data;
Packit fb9d21
Packit fb9d21
	data = (struct isl_compute_flow_data *)user;
Packit fb9d21
Packit fb9d21
	dim = isl_space_range(isl_map_get_space(map));
Packit fb9d21
Packit fb9d21
	eq = isl_space_is_equal(dim, data->dim);
Packit fb9d21
Packit fb9d21
	isl_space_free(dim);
Packit fb9d21
Packit fb9d21
	if (eq < 0)
Packit fb9d21
		goto error;
Packit fb9d21
	if (!eq) {
Packit fb9d21
		isl_map_free(map);
Packit fb9d21
		return isl_stat_ok;
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	info = sched_info_alloc(map);
Packit fb9d21
	data->source_info[data->count] = info;
Packit fb9d21
Packit fb9d21
	data->accesses = isl_access_info_add_source(data->accesses,
Packit fb9d21
						    map, data->must, info);
Packit fb9d21
Packit fb9d21
	data->count++;
Packit fb9d21
Packit fb9d21
	return isl_stat_ok;
Packit fb9d21
error:
Packit fb9d21
	isl_map_free(map);
Packit fb9d21
	return isl_stat_error;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Determine the shared nesting level and the "textual order" of
Packit fb9d21
 * the given accesses.
Packit fb9d21
 *
Packit fb9d21
 * We first determine the minimal schedule dimension for both accesses.
Packit fb9d21
 *
Packit fb9d21
 * If among those dimensions, we can find one where both have a fixed
Packit fb9d21
 * value and if moreover those values are different, then the previous
Packit fb9d21
 * dimension is the last shared nesting level and the textual order
Packit fb9d21
 * is determined based on the order of the fixed values.
Packit fb9d21
 * If no such fixed values can be found, then we set the shared
Packit fb9d21
 * nesting level to the minimal schedule dimension, with no textual ordering.
Packit fb9d21
 */
Packit fb9d21
static int before(void *first, void *second)
Packit fb9d21
{
Packit fb9d21
	struct isl_sched_info *info1 = first;
Packit fb9d21
	struct isl_sched_info *info2 = second;
Packit fb9d21
	int n1, n2;
Packit fb9d21
	int i;
Packit fb9d21
Packit fb9d21
	n1 = isl_vec_size(info1->cst);
Packit fb9d21
	n2 = isl_vec_size(info2->cst);
Packit fb9d21
Packit fb9d21
	if (n2 < n1)
Packit fb9d21
		n1 = n2;
Packit fb9d21
Packit fb9d21
	for (i = 0; i < n1; ++i) {
Packit fb9d21
		int r;
Packit fb9d21
		int cmp;
Packit fb9d21
Packit fb9d21
		if (!info1->is_cst[i])
Packit fb9d21
			continue;
Packit fb9d21
		if (!info2->is_cst[i])
Packit fb9d21
			continue;
Packit fb9d21
		cmp = isl_vec_cmp_element(info1->cst, info2->cst, i);
Packit fb9d21
		if (cmp == 0)
Packit fb9d21
			continue;
Packit fb9d21
Packit fb9d21
		r = 2 * i + (cmp < 0);
Packit fb9d21
Packit fb9d21
		return r;
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	return 2 * n1;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Given a sink access, look for all the source accesses that access
Packit fb9d21
 * the same array and perform dataflow analysis on them using
Packit fb9d21
 * isl_access_info_compute_flow_core.
Packit fb9d21
 */
Packit fb9d21
static isl_stat compute_flow(__isl_take isl_map *map, void *user)
Packit fb9d21
{
Packit fb9d21
	int i;
Packit fb9d21
	isl_ctx *ctx;
Packit fb9d21
	struct isl_compute_flow_data *data;
Packit fb9d21
	isl_flow *flow;
Packit fb9d21
	isl_union_flow *df;
Packit fb9d21
Packit fb9d21
	data = (struct isl_compute_flow_data *)user;
Packit fb9d21
	df = data->flow;
Packit fb9d21
Packit fb9d21
	ctx = isl_map_get_ctx(map);
Packit fb9d21
Packit fb9d21
	data->accesses = NULL;
Packit fb9d21
	data->sink_info = NULL;
Packit fb9d21
	data->source_info = NULL;
Packit fb9d21
	data->count = 0;
Packit fb9d21
	data->dim = isl_space_range(isl_map_get_space(map));
Packit fb9d21
Packit fb9d21
	if (isl_union_map_foreach_map(data->must_source,
Packit fb9d21
					&count_matching_array, data) < 0)
Packit fb9d21
		goto error;
Packit fb9d21
	if (isl_union_map_foreach_map(data->may_source,
Packit fb9d21
					&count_matching_array, data) < 0)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	data->sink_info = sched_info_alloc(map);
Packit fb9d21
	data->source_info = isl_calloc_array(ctx, struct isl_sched_info *,
Packit fb9d21
					     data->count);
Packit fb9d21
Packit fb9d21
	data->accesses = isl_access_info_alloc(isl_map_copy(map),
Packit fb9d21
				data->sink_info, &before, data->count);
Packit fb9d21
	if (!data->sink_info || (data->count && !data->source_info) ||
Packit fb9d21
	    !data->accesses)
Packit fb9d21
		goto error;
Packit fb9d21
	data->count = 0;
Packit fb9d21
	data->must = 1;
Packit fb9d21
	if (isl_union_map_foreach_map(data->must_source,
Packit fb9d21
					&collect_matching_array, data) < 0)
Packit fb9d21
		goto error;
Packit fb9d21
	data->must = 0;
Packit fb9d21
	if (isl_union_map_foreach_map(data->may_source,
Packit fb9d21
					&collect_matching_array, data) < 0)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	flow = access_info_compute_flow_core(data->accesses);
Packit fb9d21
	data->accesses = NULL;
Packit fb9d21
Packit fb9d21
	if (!flow)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	df->must_no_source = isl_union_map_union(df->must_no_source,
Packit fb9d21
		    isl_union_map_from_map(isl_flow_get_no_source(flow, 1)));
Packit fb9d21
	df->may_no_source = isl_union_map_union(df->may_no_source,
Packit fb9d21
		    isl_union_map_from_map(isl_flow_get_no_source(flow, 0)));
Packit fb9d21
Packit fb9d21
	for (i = 0; i < flow->n_source; ++i) {
Packit fb9d21
		isl_union_map *dep;
Packit fb9d21
		dep = isl_union_map_from_map(isl_map_copy(flow->dep[i].map));
Packit fb9d21
		if (flow->dep[i].must)
Packit fb9d21
			df->must_dep = isl_union_map_union(df->must_dep, dep);
Packit fb9d21
		else
Packit fb9d21
			df->may_dep = isl_union_map_union(df->may_dep, dep);
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	isl_flow_free(flow);
Packit fb9d21
Packit fb9d21
	sched_info_free(data->sink_info);
Packit fb9d21
	if (data->source_info) {
Packit fb9d21
		for (i = 0; i < data->count; ++i)
Packit fb9d21
			sched_info_free(data->source_info[i]);
Packit fb9d21
		free(data->source_info);
Packit fb9d21
	}
Packit fb9d21
	isl_space_free(data->dim);
Packit fb9d21
	isl_map_free(map);
Packit fb9d21
Packit fb9d21
	return isl_stat_ok;
Packit fb9d21
error:
Packit fb9d21
	isl_access_info_free(data->accesses);
Packit fb9d21
	sched_info_free(data->sink_info);
Packit fb9d21
	if (data->source_info) {
Packit fb9d21
		for (i = 0; i < data->count; ++i)
Packit fb9d21
			sched_info_free(data->source_info[i]);
Packit fb9d21
		free(data->source_info);
Packit fb9d21
	}
Packit fb9d21
	isl_space_free(data->dim);
Packit fb9d21
	isl_map_free(map);
Packit fb9d21
Packit fb9d21
	return isl_stat_error;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Remove the must accesses from the may accesses.
Packit fb9d21
 *
Packit fb9d21
 * A must access always trumps a may access, so there is no need
Packit fb9d21
 * for a must access to also be considered as a may access.  Doing so
Packit fb9d21
 * would only cost extra computations only to find out that
Packit fb9d21
 * the duplicated may access does not make any difference.
Packit fb9d21
 */
Packit fb9d21
static __isl_give isl_union_access_info *isl_union_access_info_normalize(
Packit fb9d21
	__isl_take isl_union_access_info *access)
Packit fb9d21
{
Packit fb9d21
	if (!access)
Packit fb9d21
		return NULL;
Packit fb9d21
	access->may_source = isl_union_map_subtract(access->may_source,
Packit fb9d21
				    isl_union_map_copy(access->must_source));
Packit fb9d21
	if (!access->may_source)
Packit fb9d21
		return isl_union_access_info_free(access);
Packit fb9d21
Packit fb9d21
	return access;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Given a description of the "sink" accesses, the "source" accesses and
Packit fb9d21
 * a schedule, compute for each instance of a sink access
Packit fb9d21
 * and for each element accessed by that instance,
Packit fb9d21
 * the possible or definite source accesses that last accessed the
Packit fb9d21
 * element accessed by the sink access before this sink access
Packit fb9d21
 * in the sense that there is no intermediate definite source access.
Packit fb9d21
 *
Packit fb9d21
 * The must_no_source and may_no_source elements of the result
Packit fb9d21
 * are subsets of access->sink.  The elements must_dep and may_dep
Packit fb9d21
 * map domain elements of access->{may,must)_source to
Packit fb9d21
 * domain elements of access->sink.
Packit fb9d21
 *
Packit fb9d21
 * This function is used when only the schedule map representation
Packit fb9d21
 * is available.
Packit fb9d21
 *
Packit fb9d21
 * We first prepend the schedule dimensions to the domain
Packit fb9d21
 * of the accesses so that we can easily compare their relative order.
Packit fb9d21
 * Then we consider each sink access individually in compute_flow.
Packit fb9d21
 */
Packit fb9d21
static __isl_give isl_union_flow *compute_flow_union_map(
Packit fb9d21
	__isl_take isl_union_access_info *access)
Packit fb9d21
{
Packit fb9d21
	struct isl_compute_flow_data data;
Packit fb9d21
Packit fb9d21
	access = isl_union_access_info_align_params(access);
Packit fb9d21
	access = isl_union_access_info_introduce_schedule(access);
Packit fb9d21
	if (!access)
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	data.must_source = access->must_source;
Packit fb9d21
	data.may_source = access->may_source;
Packit fb9d21
Packit fb9d21
	data.flow = isl_union_flow_alloc(isl_union_map_get_space(access->sink));
Packit fb9d21
Packit fb9d21
	if (isl_union_map_foreach_map(access->sink, &compute_flow, &data) < 0)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	data.flow = isl_union_flow_drop_schedule(data.flow);
Packit fb9d21
Packit fb9d21
	isl_union_access_info_free(access);
Packit fb9d21
	return data.flow;
Packit fb9d21
error:
Packit fb9d21
	isl_union_access_info_free(access);
Packit fb9d21
	isl_union_flow_free(data.flow);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* A schedule access relation.
Packit fb9d21
 *
Packit fb9d21
 * The access relation "access" is of the form [S -> D] -> A,
Packit fb9d21
 * where S corresponds to the prefix schedule at "node".
Packit fb9d21
 * "must" is only relevant for source accesses and indicates
Packit fb9d21
 * whether the access is a must source or a may source.
Packit fb9d21
 */
Packit fb9d21
struct isl_scheduled_access {
Packit fb9d21
	isl_map *access;
Packit fb9d21
	int must;
Packit fb9d21
	isl_schedule_node *node;
Packit fb9d21
};
Packit fb9d21
Packit fb9d21
/* Data structure for keeping track of individual scheduled sink and source
Packit fb9d21
 * accesses when computing dependence analysis based on a schedule tree.
Packit fb9d21
 *
Packit fb9d21
 * "n_sink" is the number of used entries in "sink"
Packit fb9d21
 * "n_source" is the number of used entries in "source"
Packit fb9d21
 *
Packit fb9d21
 * "set_sink", "must" and "node" are only used inside collect_sink_source,
Packit fb9d21
 * to keep track of the current node and
Packit fb9d21
 * of what extract_sink_source needs to do.
Packit fb9d21
 */
Packit fb9d21
struct isl_compute_flow_schedule_data {
Packit fb9d21
	isl_union_access_info *access;
Packit fb9d21
Packit fb9d21
	int n_sink;
Packit fb9d21
	int n_source;
Packit fb9d21
Packit fb9d21
	struct isl_scheduled_access *sink;
Packit fb9d21
	struct isl_scheduled_access *source;
Packit fb9d21
Packit fb9d21
	int set_sink;
Packit fb9d21
	int must;
Packit fb9d21
	isl_schedule_node *node;
Packit fb9d21
};
Packit fb9d21
Packit fb9d21
/* Align the parameters of all sinks with all sources.
Packit fb9d21
 *
Packit fb9d21
 * If there are no sinks or no sources, then no alignment is needed.
Packit fb9d21
 */
Packit fb9d21
static void isl_compute_flow_schedule_data_align_params(
Packit fb9d21
	struct isl_compute_flow_schedule_data *data)
Packit fb9d21
{
Packit fb9d21
	int i;
Packit fb9d21
	isl_space *space;
Packit fb9d21
Packit fb9d21
	if (data->n_sink == 0 || data->n_source == 0)
Packit fb9d21
		return;
Packit fb9d21
Packit fb9d21
	space = isl_map_get_space(data->sink[0].access);
Packit fb9d21
Packit fb9d21
	for (i = 1; i < data->n_sink; ++i)
Packit fb9d21
		space = isl_space_align_params(space,
Packit fb9d21
				isl_map_get_space(data->sink[i].access));
Packit fb9d21
	for (i = 0; i < data->n_source; ++i)
Packit fb9d21
		space = isl_space_align_params(space,
Packit fb9d21
				isl_map_get_space(data->source[i].access));
Packit fb9d21
Packit fb9d21
	for (i = 0; i < data->n_sink; ++i)
Packit fb9d21
		data->sink[i].access =
Packit fb9d21
			isl_map_align_params(data->sink[i].access,
Packit fb9d21
							isl_space_copy(space));
Packit fb9d21
	for (i = 0; i < data->n_source; ++i)
Packit fb9d21
		data->source[i].access =
Packit fb9d21
			isl_map_align_params(data->source[i].access,
Packit fb9d21
							isl_space_copy(space));
Packit fb9d21
Packit fb9d21
	isl_space_free(space);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Free all the memory referenced from "data".
Packit fb9d21
 * Do not free "data" itself as it may be allocated on the stack.
Packit fb9d21
 */
Packit fb9d21
static void isl_compute_flow_schedule_data_clear(
Packit fb9d21
	struct isl_compute_flow_schedule_data *data)
Packit fb9d21
{
Packit fb9d21
	int i;
Packit fb9d21
Packit fb9d21
	if (!data->sink)
Packit fb9d21
		return;
Packit fb9d21
Packit fb9d21
	for (i = 0; i < data->n_sink; ++i) {
Packit fb9d21
		isl_map_free(data->sink[i].access);
Packit fb9d21
		isl_schedule_node_free(data->sink[i].node);
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	for (i = 0; i < data->n_source; ++i) {
Packit fb9d21
		isl_map_free(data->source[i].access);
Packit fb9d21
		isl_schedule_node_free(data->source[i].node);
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	free(data->sink);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* isl_schedule_foreach_schedule_node_top_down callback for counting
Packit fb9d21
 * (an upper bound on) the number of sinks and sources.
Packit fb9d21
 *
Packit fb9d21
 * Sinks and sources are only extracted at leaves of the tree,
Packit fb9d21
 * so we skip the node if it is not a leaf.
Packit fb9d21
 * Otherwise we increment data->n_sink and data->n_source with
Packit fb9d21
 * the number of spaces in the sink and source access domains
Packit fb9d21
 * that reach this node.
Packit fb9d21
 */
Packit fb9d21
static isl_bool count_sink_source(__isl_keep isl_schedule_node *node,
Packit fb9d21
	void *user)
Packit fb9d21
{
Packit fb9d21
	struct isl_compute_flow_schedule_data *data = user;
Packit fb9d21
	isl_union_set *domain;
Packit fb9d21
	isl_union_map *umap;
Packit fb9d21
	isl_bool r = isl_bool_false;
Packit fb9d21
Packit fb9d21
	if (isl_schedule_node_get_type(node) != isl_schedule_node_leaf)
Packit fb9d21
		return isl_bool_true;
Packit fb9d21
Packit fb9d21
	domain = isl_schedule_node_get_universe_domain(node);
Packit fb9d21
Packit fb9d21
	umap = isl_union_map_copy(data->access->sink);
Packit fb9d21
	umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(domain));
Packit fb9d21
	data->n_sink += isl_union_map_n_map(umap);
Packit fb9d21
	isl_union_map_free(umap);
Packit fb9d21
	if (!umap)
Packit fb9d21
		r = isl_bool_error;
Packit fb9d21
Packit fb9d21
	umap = isl_union_map_copy(data->access->must_source);
Packit fb9d21
	umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(domain));
Packit fb9d21
	data->n_source += isl_union_map_n_map(umap);
Packit fb9d21
	isl_union_map_free(umap);
Packit fb9d21
	if (!umap)
Packit fb9d21
		r = isl_bool_error;
Packit fb9d21
Packit fb9d21
	umap = isl_union_map_copy(data->access->may_source);
Packit fb9d21
	umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(domain));
Packit fb9d21
	data->n_source += isl_union_map_n_map(umap);
Packit fb9d21
	isl_union_map_free(umap);
Packit fb9d21
	if (!umap)
Packit fb9d21
		r = isl_bool_error;
Packit fb9d21
Packit fb9d21
	isl_union_set_free(domain);
Packit fb9d21
Packit fb9d21
	return r;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Add a single scheduled sink or source (depending on data->set_sink)
Packit fb9d21
 * with scheduled access relation "map", must property data->must and
Packit fb9d21
 * schedule node data->node to the list of sinks or sources.
Packit fb9d21
 */
Packit fb9d21
static isl_stat extract_sink_source(__isl_take isl_map *map, void *user)
Packit fb9d21
{
Packit fb9d21
	struct isl_compute_flow_schedule_data *data = user;
Packit fb9d21
	struct isl_scheduled_access *access;
Packit fb9d21
Packit fb9d21
	if (data->set_sink)
Packit fb9d21
		access = data->sink + data->n_sink++;
Packit fb9d21
	else
Packit fb9d21
		access = data->source + data->n_source++;
Packit fb9d21
Packit fb9d21
	access->access = map;
Packit fb9d21
	access->must = data->must;
Packit fb9d21
	access->node = isl_schedule_node_copy(data->node);
Packit fb9d21
Packit fb9d21
	return isl_stat_ok;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* isl_schedule_foreach_schedule_node_top_down callback for collecting
Packit fb9d21
 * individual scheduled source and sink accesses (taking into account
Packit fb9d21
 * the domain of the schedule).
Packit fb9d21
 *
Packit fb9d21
 * We only collect accesses at the leaves of the schedule tree.
Packit fb9d21
 * We prepend the schedule dimensions at the leaf to the iteration
Packit fb9d21
 * domains of the source and sink accesses and then extract
Packit fb9d21
 * the individual accesses (per space).
Packit fb9d21
 *
Packit fb9d21
 * In particular, if the prefix schedule at the node is of the form
Packit fb9d21
 *
Packit fb9d21
 *	D -> S
Packit fb9d21
 *
Packit fb9d21
 * while the access relations are of the form
Packit fb9d21
 *
Packit fb9d21
 *	D -> A
Packit fb9d21
 *
Packit fb9d21
 * then the updated access relations are of the form
Packit fb9d21
 *
Packit fb9d21
 *	[S -> D] -> A
Packit fb9d21
 *
Packit fb9d21
 * Note that S consists of a single space such that introducing S
Packit fb9d21
 * in the access relations does not increase the number of spaces.
Packit fb9d21
 */
Packit fb9d21
static isl_bool collect_sink_source(__isl_keep isl_schedule_node *node,
Packit fb9d21
	void *user)
Packit fb9d21
{
Packit fb9d21
	struct isl_compute_flow_schedule_data *data = user;
Packit fb9d21
	isl_union_map *prefix;
Packit fb9d21
	isl_union_map *umap;
Packit fb9d21
	isl_bool r = isl_bool_false;
Packit fb9d21
Packit fb9d21
	if (isl_schedule_node_get_type(node) != isl_schedule_node_leaf)
Packit fb9d21
		return isl_bool_true;
Packit fb9d21
Packit fb9d21
	data->node = node;
Packit fb9d21
Packit fb9d21
	prefix = isl_schedule_node_get_prefix_schedule_relation(node);
Packit fb9d21
	prefix = isl_union_map_reverse(prefix);
Packit fb9d21
	prefix = isl_union_map_range_map(prefix);
Packit fb9d21
Packit fb9d21
	data->set_sink = 1;
Packit fb9d21
	umap = isl_union_map_copy(data->access->sink);
Packit fb9d21
	umap = isl_union_map_apply_range(isl_union_map_copy(prefix), umap);
Packit fb9d21
	if (isl_union_map_foreach_map(umap, &extract_sink_source, data) < 0)
Packit fb9d21
		r = isl_bool_error;
Packit fb9d21
	isl_union_map_free(umap);
Packit fb9d21
Packit fb9d21
	data->set_sink = 0;
Packit fb9d21
	data->must = 1;
Packit fb9d21
	umap = isl_union_map_copy(data->access->must_source);
Packit fb9d21
	umap = isl_union_map_apply_range(isl_union_map_copy(prefix), umap);
Packit fb9d21
	if (isl_union_map_foreach_map(umap, &extract_sink_source, data) < 0)
Packit fb9d21
		r = isl_bool_error;
Packit fb9d21
	isl_union_map_free(umap);
Packit fb9d21
Packit fb9d21
	data->set_sink = 0;
Packit fb9d21
	data->must = 0;
Packit fb9d21
	umap = isl_union_map_copy(data->access->may_source);
Packit fb9d21
	umap = isl_union_map_apply_range(isl_union_map_copy(prefix), umap);
Packit fb9d21
	if (isl_union_map_foreach_map(umap, &extract_sink_source, data) < 0)
Packit fb9d21
		r = isl_bool_error;
Packit fb9d21
	isl_union_map_free(umap);
Packit fb9d21
Packit fb9d21
	isl_union_map_free(prefix);
Packit fb9d21
Packit fb9d21
	return r;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* isl_access_info_compute_flow callback for determining whether
Packit fb9d21
 * the shared nesting level and the ordering within that level
Packit fb9d21
 * for two scheduled accesses for use in compute_single_flow.
Packit fb9d21
 *
Packit fb9d21
 * The tokens passed to this function refer to the leaves
Packit fb9d21
 * in the schedule tree where the accesses take place.
Packit fb9d21
 *
Packit fb9d21
 * If n is the shared number of loops, then we need to return
Packit fb9d21
 * "2 * n + 1" if "first" precedes "second" inside the innermost
Packit fb9d21
 * shared loop and "2 * n" otherwise.
Packit fb9d21
 *
Packit fb9d21
 * The innermost shared ancestor may be the leaves themselves
Packit fb9d21
 * if the accesses take place in the same leaf.  Otherwise,
Packit fb9d21
 * it is either a set node or a sequence node.  Only in the case
Packit fb9d21
 * of a sequence node do we consider one access to precede the other.
Packit fb9d21
 */
Packit fb9d21
static int before_node(void *first, void *second)
Packit fb9d21
{
Packit fb9d21
	isl_schedule_node *node1 = first;
Packit fb9d21
	isl_schedule_node *node2 = second;
Packit fb9d21
	isl_schedule_node *shared;
Packit fb9d21
	int depth;
Packit fb9d21
	int before = 0;
Packit fb9d21
Packit fb9d21
	shared = isl_schedule_node_get_shared_ancestor(node1, node2);
Packit fb9d21
	if (!shared)
Packit fb9d21
		return -1;
Packit fb9d21
Packit fb9d21
	depth = isl_schedule_node_get_schedule_depth(shared);
Packit fb9d21
	if (isl_schedule_node_get_type(shared) == isl_schedule_node_sequence) {
Packit fb9d21
		int pos1, pos2;
Packit fb9d21
Packit fb9d21
		pos1 = isl_schedule_node_get_ancestor_child_position(node1,
Packit fb9d21
								    shared);
Packit fb9d21
		pos2 = isl_schedule_node_get_ancestor_child_position(node2,
Packit fb9d21
								    shared);
Packit fb9d21
		before = pos1 < pos2;
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	isl_schedule_node_free(shared);
Packit fb9d21
Packit fb9d21
	return 2 * depth + before;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Add the scheduled sources from "data" that access
Packit fb9d21
 * the same data space as "sink" to "access".
Packit fb9d21
 */
Packit fb9d21
static __isl_give isl_access_info *add_matching_sources(
Packit fb9d21
	__isl_take isl_access_info *access, struct isl_scheduled_access *sink,
Packit fb9d21
	struct isl_compute_flow_schedule_data *data)
Packit fb9d21
{
Packit fb9d21
	int i;
Packit fb9d21
	isl_space *space;
Packit fb9d21
Packit fb9d21
	space = isl_space_range(isl_map_get_space(sink->access));
Packit fb9d21
	for (i = 0; i < data->n_source; ++i) {
Packit fb9d21
		struct isl_scheduled_access *source;
Packit fb9d21
		isl_space *source_space;
Packit fb9d21
		int eq;
Packit fb9d21
Packit fb9d21
		source = &data->source[i];
Packit fb9d21
		source_space = isl_map_get_space(source->access);
Packit fb9d21
		source_space = isl_space_range(source_space);
Packit fb9d21
		eq = isl_space_is_equal(space, source_space);
Packit fb9d21
		isl_space_free(source_space);
Packit fb9d21
Packit fb9d21
		if (!eq)
Packit fb9d21
			continue;
Packit fb9d21
		if (eq < 0)
Packit fb9d21
			goto error;
Packit fb9d21
Packit fb9d21
		access = isl_access_info_add_source(access,
Packit fb9d21
		    isl_map_copy(source->access), source->must, source->node);
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	isl_space_free(space);
Packit fb9d21
	return access;
Packit fb9d21
error:
Packit fb9d21
	isl_space_free(space);
Packit fb9d21
	isl_access_info_free(access);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Given a scheduled sink access relation "sink", compute the corresponding
Packit fb9d21
 * dependences on the sources in "data" and add the computed dependences
Packit fb9d21
 * to "uf".
Packit fb9d21
 *
Packit fb9d21
 * The dependences computed by access_info_compute_flow_core are of the form
Packit fb9d21
 *
Packit fb9d21
 *	[S -> I] -> [[S' -> I'] -> A]
Packit fb9d21
 *
Packit fb9d21
 * The schedule dimensions are projected out by first currying the range,
Packit fb9d21
 * resulting in
Packit fb9d21
 *
Packit fb9d21
 *	[S -> I] -> [S' -> [I' -> A]]
Packit fb9d21
 *
Packit fb9d21
 * and then computing the factor range
Packit fb9d21
 *
Packit fb9d21
 *	I -> [I' -> A]
Packit fb9d21
 */
Packit fb9d21
static __isl_give isl_union_flow *compute_single_flow(
Packit fb9d21
	__isl_take isl_union_flow *uf, struct isl_scheduled_access *sink,
Packit fb9d21
	struct isl_compute_flow_schedule_data *data)
Packit fb9d21
{
Packit fb9d21
	int i;
Packit fb9d21
	isl_access_info *access;
Packit fb9d21
	isl_flow *flow;
Packit fb9d21
	isl_map *map;
Packit fb9d21
Packit fb9d21
	if (!uf)
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	access = isl_access_info_alloc(isl_map_copy(sink->access), sink->node,
Packit fb9d21
					&before_node, data->n_source);
Packit fb9d21
	access = add_matching_sources(access, sink, data);
Packit fb9d21
Packit fb9d21
	flow = access_info_compute_flow_core(access);
Packit fb9d21
	if (!flow)
Packit fb9d21
		return isl_union_flow_free(uf);
Packit fb9d21
Packit fb9d21
	map = isl_map_domain_factor_range(isl_flow_get_no_source(flow, 1));
Packit fb9d21
	uf->must_no_source = isl_union_map_union(uf->must_no_source,
Packit fb9d21
						isl_union_map_from_map(map));
Packit fb9d21
	map = isl_map_domain_factor_range(isl_flow_get_no_source(flow, 0));
Packit fb9d21
	uf->may_no_source = isl_union_map_union(uf->may_no_source,
Packit fb9d21
						isl_union_map_from_map(map));
Packit fb9d21
Packit fb9d21
	for (i = 0; i < flow->n_source; ++i) {
Packit fb9d21
		isl_union_map *dep;
Packit fb9d21
Packit fb9d21
		map = isl_map_range_curry(isl_map_copy(flow->dep[i].map));
Packit fb9d21
		map = isl_map_factor_range(map);
Packit fb9d21
		dep = isl_union_map_from_map(map);
Packit fb9d21
		if (flow->dep[i].must)
Packit fb9d21
			uf->must_dep = isl_union_map_union(uf->must_dep, dep);
Packit fb9d21
		else
Packit fb9d21
			uf->may_dep = isl_union_map_union(uf->may_dep, dep);
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	isl_flow_free(flow);
Packit fb9d21
Packit fb9d21
	return uf;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Given a description of the "sink" accesses, the "source" accesses and
Packit fb9d21
 * a schedule, compute for each instance of a sink access
Packit fb9d21
 * and for each element accessed by that instance,
Packit fb9d21
 * the possible or definite source accesses that last accessed the
Packit fb9d21
 * element accessed by the sink access before this sink access
Packit fb9d21
 * in the sense that there is no intermediate definite source access.
Packit fb9d21
 * Only consider dependences between statement instances that belong
Packit fb9d21
 * to the domain of the schedule.
Packit fb9d21
 *
Packit fb9d21
 * The must_no_source and may_no_source elements of the result
Packit fb9d21
 * are subsets of access->sink.  The elements must_dep and may_dep
Packit fb9d21
 * map domain elements of access->{may,must)_source to
Packit fb9d21
 * domain elements of access->sink.
Packit fb9d21
 *
Packit fb9d21
 * This function is used when a schedule tree representation
Packit fb9d21
 * is available.
Packit fb9d21
 *
Packit fb9d21
 * We extract the individual scheduled source and sink access relations
Packit fb9d21
 * (taking into account the domain of the schedule) and
Packit fb9d21
 * then compute dependences for each scheduled sink individually.
Packit fb9d21
 */
Packit fb9d21
static __isl_give isl_union_flow *compute_flow_schedule(
Packit fb9d21
	__isl_take isl_union_access_info *access)
Packit fb9d21
{
Packit fb9d21
	struct isl_compute_flow_schedule_data data = { access };
Packit fb9d21
	int i, n;
Packit fb9d21
	isl_ctx *ctx;
Packit fb9d21
	isl_union_flow *flow;
Packit fb9d21
Packit fb9d21
	ctx = isl_union_access_info_get_ctx(access);
Packit fb9d21
Packit fb9d21
	data.n_sink = 0;
Packit fb9d21
	data.n_source = 0;
Packit fb9d21
	if (isl_schedule_foreach_schedule_node_top_down(access->schedule,
Packit fb9d21
						&count_sink_source, &data) < 0)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	n = data.n_sink + data.n_source;
Packit fb9d21
	data.sink = isl_calloc_array(ctx, struct isl_scheduled_access, n);
Packit fb9d21
	if (n && !data.sink)
Packit fb9d21
		goto error;
Packit fb9d21
	data.source = data.sink + data.n_sink;
Packit fb9d21
Packit fb9d21
	data.n_sink = 0;
Packit fb9d21
	data.n_source = 0;
Packit fb9d21
	if (isl_schedule_foreach_schedule_node_top_down(access->schedule,
Packit fb9d21
					    &collect_sink_source, &data) < 0)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	flow = isl_union_flow_alloc(isl_union_map_get_space(access->sink));
Packit fb9d21
Packit fb9d21
	isl_compute_flow_schedule_data_align_params(&data);
Packit fb9d21
Packit fb9d21
	for (i = 0; i < data.n_sink; ++i)
Packit fb9d21
		flow = compute_single_flow(flow, &data.sink[i], &data);
Packit fb9d21
Packit fb9d21
	isl_compute_flow_schedule_data_clear(&data);
Packit fb9d21
Packit fb9d21
	isl_union_access_info_free(access);
Packit fb9d21
	return flow;
Packit fb9d21
error:
Packit fb9d21
	isl_union_access_info_free(access);
Packit fb9d21
	isl_compute_flow_schedule_data_clear(&data);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Given a description of the "sink" accesses, the "source" accesses and
Packit fb9d21
 * a schedule, compute for each instance of a sink access
Packit fb9d21
 * and for each element accessed by that instance,
Packit fb9d21
 * the possible or definite source accesses that last accessed the
Packit fb9d21
 * element accessed by the sink access before this sink access
Packit fb9d21
 * in the sense that there is no intermediate definite source access.
Packit fb9d21
 *
Packit fb9d21
 * The must_no_source and may_no_source elements of the result
Packit fb9d21
 * are subsets of access->sink.  The elements must_dep and may_dep
Packit fb9d21
 * map domain elements of access->{may,must)_source to
Packit fb9d21
 * domain elements of access->sink.
Packit fb9d21
 *
Packit fb9d21
 * We check whether the schedule is available as a schedule tree
Packit fb9d21
 * or a schedule map and call the correpsonding function to perform
Packit fb9d21
 * the analysis.
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_union_flow *isl_union_access_info_compute_flow(
Packit fb9d21
	__isl_take isl_union_access_info *access)
Packit fb9d21
{
Packit fb9d21
	access = isl_union_access_info_normalize(access);
Packit fb9d21
	if (!access)
Packit fb9d21
		return NULL;
Packit fb9d21
	if (access->schedule)
Packit fb9d21
		return compute_flow_schedule(access);
Packit fb9d21
	else
Packit fb9d21
		return compute_flow_union_map(access);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Print the information contained in "flow" to "p".
Packit fb9d21
 * The information is printed as a YAML document.
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_printer *isl_printer_print_union_flow(
Packit fb9d21
	__isl_take isl_printer *p, __isl_keep isl_union_flow *flow)
Packit fb9d21
{
Packit fb9d21
	isl_union_map *umap;
Packit fb9d21
Packit fb9d21
	if (!flow)
Packit fb9d21
		return isl_printer_free(p);
Packit fb9d21
Packit fb9d21
	p = isl_printer_yaml_start_mapping(p);
Packit fb9d21
	p = print_union_map_field(p, "must_dependence", flow->must_dep);
Packit fb9d21
	umap = isl_union_flow_get_may_dependence(flow);
Packit fb9d21
	p = print_union_map_field(p, "may_dependence", umap);
Packit fb9d21
	isl_union_map_free(umap);
Packit fb9d21
	p = print_union_map_field(p, "must_no_source", flow->must_no_source);
Packit fb9d21
	umap = isl_union_flow_get_may_no_source(flow);
Packit fb9d21
	p = print_union_map_field(p, "may_no_source", umap);
Packit fb9d21
	isl_union_map_free(umap);
Packit fb9d21
	p = isl_printer_yaml_end_mapping(p);
Packit fb9d21
Packit fb9d21
	return p;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Return a string representation of the information in "flow".
Packit fb9d21
 * The information is printed in flow format.
Packit fb9d21
 */
Packit fb9d21
__isl_give char *isl_union_flow_to_str(__isl_keep isl_union_flow *flow)
Packit fb9d21
{
Packit fb9d21
	isl_printer *p;
Packit fb9d21
	char *s;
Packit fb9d21
Packit fb9d21
	if (!flow)
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	p = isl_printer_to_str(isl_union_flow_get_ctx(flow));
Packit fb9d21
	p = isl_printer_set_yaml_style(p, ISL_YAML_STYLE_FLOW);
Packit fb9d21
	p = isl_printer_print_union_flow(p, flow);
Packit fb9d21
	s = isl_printer_get_str(p);
Packit fb9d21
	isl_printer_free(p);
Packit fb9d21
Packit fb9d21
	return s;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Given a collection of "sink" and "source" accesses,
Packit fb9d21
 * compute for each iteration of a sink access
Packit fb9d21
 * and for each element accessed by that iteration,
Packit fb9d21
 * the source access in the list that last accessed the
Packit fb9d21
 * element accessed by the sink access before this sink access.
Packit fb9d21
 * Each access is given as a map from the loop iterators
Packit fb9d21
 * to the array indices.
Packit fb9d21
 * The result is a relations between source and sink
Packit fb9d21
 * iterations and a subset of the domain of the sink accesses,
Packit fb9d21
 * corresponding to those iterations that access an element
Packit fb9d21
 * not previously accessed.
Packit fb9d21
 *
Packit fb9d21
 * We collect the inputs in an isl_union_access_info object,
Packit fb9d21
 * call isl_union_access_info_compute_flow and extract
Packit fb9d21
 * the outputs from the result.
Packit fb9d21
 */
Packit fb9d21
int isl_union_map_compute_flow(__isl_take isl_union_map *sink,
Packit fb9d21
	__isl_take isl_union_map *must_source,
Packit fb9d21
	__isl_take isl_union_map *may_source,
Packit fb9d21
	__isl_take isl_union_map *schedule,
Packit fb9d21
	__isl_give isl_union_map **must_dep, __isl_give isl_union_map **may_dep,
Packit fb9d21
	__isl_give isl_union_map **must_no_source,
Packit fb9d21
	__isl_give isl_union_map **may_no_source)
Packit fb9d21
{
Packit fb9d21
	isl_union_access_info *access;
Packit fb9d21
	isl_union_flow *flow;
Packit fb9d21
Packit fb9d21
	access = isl_union_access_info_from_sink(sink);
Packit fb9d21
	access = isl_union_access_info_set_must_source(access, must_source);
Packit fb9d21
	access = isl_union_access_info_set_may_source(access, may_source);
Packit fb9d21
	access = isl_union_access_info_set_schedule_map(access, schedule);
Packit fb9d21
	flow = isl_union_access_info_compute_flow(access);
Packit fb9d21
Packit fb9d21
	if (must_dep)
Packit fb9d21
		*must_dep = isl_union_flow_get_must_dependence(flow);
Packit fb9d21
	if (may_dep)
Packit fb9d21
		*may_dep = isl_union_flow_get_non_must_dependence(flow);
Packit fb9d21
	if (must_no_source)
Packit fb9d21
		*must_no_source = isl_union_flow_get_must_no_source(flow);
Packit fb9d21
	if (may_no_source)
Packit fb9d21
		*may_no_source = isl_union_flow_get_non_must_no_source(flow);
Packit fb9d21
Packit fb9d21
	isl_union_flow_free(flow);
Packit fb9d21
Packit fb9d21
	if ((must_dep && !*must_dep) || (may_dep && !*may_dep) ||
Packit fb9d21
	    (must_no_source && !*must_no_source) ||
Packit fb9d21
	    (may_no_source && !*may_no_source))
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	return 0;
Packit fb9d21
error:
Packit fb9d21
	if (must_dep)
Packit fb9d21
		*must_dep = isl_union_map_free(*must_dep);
Packit fb9d21
	if (may_dep)
Packit fb9d21
		*may_dep = isl_union_map_free(*may_dep);
Packit fb9d21
	if (must_no_source)
Packit fb9d21
		*must_no_source = isl_union_map_free(*must_no_source);
Packit fb9d21
	if (may_no_source)
Packit fb9d21
		*may_no_source = isl_union_map_free(*may_no_source);
Packit fb9d21
	return -1;
Packit fb9d21
}