Blame isl-0.14/isl_ast_build.c

Packit fb9d21
/*
Packit fb9d21
 * Copyright 2012      Ecole Normale Superieure
Packit fb9d21
 * Copyright 2014      INRIA Rocquencourt
Packit fb9d21
 *
Packit fb9d21
 * Use of this software is governed by the MIT license
Packit fb9d21
 *
Packit fb9d21
 * Written by Sven Verdoolaege,
Packit fb9d21
 * Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
Packit fb9d21
 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
Packit fb9d21
 * B.P. 105 - 78153 Le Chesnay, France
Packit fb9d21
 */
Packit fb9d21
Packit fb9d21
#include <isl/map.h>
Packit fb9d21
#include <isl/aff.h>
Packit fb9d21
#include <isl/map.h>
Packit fb9d21
#include <isl_ast_build_private.h>
Packit fb9d21
#include <isl_ast_private.h>
Packit fb9d21
Packit fb9d21
/* Construct a map that isolates the current dimension.
Packit fb9d21
 *
Packit fb9d21
 * Essentially, the current dimension of "set" is moved to the single output
Packit fb9d21
 * dimension in the result, with the current dimension in the domain replaced
Packit fb9d21
 * by an unconstrained variable.
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_map *isl_ast_build_map_to_iterator(
Packit fb9d21
	__isl_keep isl_ast_build *build, __isl_take isl_set *set)
Packit fb9d21
{
Packit fb9d21
	isl_map *map;
Packit fb9d21
Packit fb9d21
	map = isl_map_from_domain(set);
Packit fb9d21
	map = isl_map_add_dims(map, isl_dim_out, 1);
Packit fb9d21
Packit fb9d21
	if (!build)
Packit fb9d21
		return isl_map_free(map);
Packit fb9d21
Packit fb9d21
	map = isl_map_equate(map, isl_dim_in, build->depth, isl_dim_out, 0);
Packit fb9d21
	map = isl_map_eliminate(map, isl_dim_in, build->depth, 1);
Packit fb9d21
Packit fb9d21
	return map;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Initialize the information derived during the AST generation to default
Packit fb9d21
 * values for a schedule domain in "space".
Packit fb9d21
 *
Packit fb9d21
 * We also check that the remaining fields are not NULL so that
Packit fb9d21
 * the calling functions don't have to perform this test.
Packit fb9d21
 */
Packit fb9d21
static __isl_give isl_ast_build *isl_ast_build_init_derived(
Packit fb9d21
	__isl_take isl_ast_build *build, __isl_take isl_space *space)
Packit fb9d21
{
Packit fb9d21
	isl_ctx *ctx;
Packit fb9d21
	isl_vec *strides;
Packit fb9d21
Packit fb9d21
	build = isl_ast_build_cow(build);
Packit fb9d21
	if (!build || !build->domain)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	ctx = isl_ast_build_get_ctx(build);
Packit fb9d21
	strides = isl_vec_alloc(ctx, isl_space_dim(space, isl_dim_set));
Packit fb9d21
	strides = isl_vec_set_si(strides, 1);
Packit fb9d21
Packit fb9d21
	isl_vec_free(build->strides);
Packit fb9d21
	build->strides = strides;
Packit fb9d21
Packit fb9d21
	space = isl_space_map_from_set(space);
Packit fb9d21
	isl_multi_aff_free(build->offsets);
Packit fb9d21
	build->offsets = isl_multi_aff_zero(isl_space_copy(space));
Packit fb9d21
	isl_multi_aff_free(build->values);
Packit fb9d21
	build->values = isl_multi_aff_identity(space);
Packit fb9d21
Packit fb9d21
	if (!build->iterators || !build->domain || !build->generated ||
Packit fb9d21
	    !build->pending || !build->values ||
Packit fb9d21
	    !build->strides || !build->offsets || !build->options)
Packit fb9d21
		return isl_ast_build_free(build);
Packit fb9d21
Packit fb9d21
	return build;
Packit fb9d21
error:
Packit fb9d21
	isl_space_free(space);
Packit fb9d21
	return isl_ast_build_free(build);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Return an isl_id called "c%d", with "%d" set to "i".
Packit fb9d21
 * If an isl_id with such a name already appears among the parameters
Packit fb9d21
 * in build->domain, then adjust the name to "c%d_%d".
Packit fb9d21
 */
Packit fb9d21
static __isl_give isl_id *generate_name(isl_ctx *ctx, int i,
Packit fb9d21
	__isl_keep isl_ast_build *build)
Packit fb9d21
{
Packit fb9d21
	int j;
Packit fb9d21
	char name[16];
Packit fb9d21
	isl_set *dom = build->domain;
Packit fb9d21
Packit fb9d21
	snprintf(name, sizeof(name), "c%d", i);
Packit fb9d21
	j = 0;
Packit fb9d21
	while (isl_set_find_dim_by_name(dom, isl_dim_param, name) >= 0)
Packit fb9d21
		snprintf(name, sizeof(name), "c%d_%d", i, j++);
Packit fb9d21
	return isl_id_alloc(ctx, name, NULL);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Create an isl_ast_build with "set" as domain.
Packit fb9d21
 *
Packit fb9d21
 * The input set is usually a parameter domain, but we currently allow it to
Packit fb9d21
 * be any kind of set.  We set the domain of the returned isl_ast_build
Packit fb9d21
 * to "set" and initialize all the other fields to default values.
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_ast_build *isl_ast_build_from_context(__isl_take isl_set *set)
Packit fb9d21
{
Packit fb9d21
	int i, n;
Packit fb9d21
	isl_ctx *ctx;
Packit fb9d21
	isl_space *space;
Packit fb9d21
	isl_ast_build *build;
Packit fb9d21
Packit fb9d21
	set = isl_set_compute_divs(set);
Packit fb9d21
	if (!set)
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	ctx = isl_set_get_ctx(set);
Packit fb9d21
Packit fb9d21
	build = isl_calloc_type(ctx, isl_ast_build);
Packit fb9d21
	if (!build)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	build->ref = 1;
Packit fb9d21
	build->domain = set;
Packit fb9d21
	build->generated = isl_set_copy(build->domain);
Packit fb9d21
	build->pending = isl_set_universe(isl_set_get_space(build->domain));
Packit fb9d21
	build->options = isl_union_map_empty(isl_space_params_alloc(ctx, 0));
Packit fb9d21
	n = isl_set_dim(set, isl_dim_set);
Packit fb9d21
	build->depth = n;
Packit fb9d21
	build->iterators = isl_id_list_alloc(ctx, n);
Packit fb9d21
	for (i = 0; i < n; ++i) {
Packit fb9d21
		isl_id *id;
Packit fb9d21
		if (isl_set_has_dim_id(set, isl_dim_set, i))
Packit fb9d21
			id = isl_set_get_dim_id(set, isl_dim_set, i);
Packit fb9d21
		else
Packit fb9d21
			id = generate_name(ctx, i, build);
Packit fb9d21
		build->iterators = isl_id_list_add(build->iterators, id);
Packit fb9d21
	}
Packit fb9d21
	space = isl_set_get_space(set);
Packit fb9d21
	if (isl_space_is_params(space))
Packit fb9d21
		space = isl_space_set_from_params(space);
Packit fb9d21
Packit fb9d21
	return isl_ast_build_init_derived(build, space);
Packit fb9d21
error:
Packit fb9d21
	isl_set_free(set);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
__isl_give isl_ast_build *isl_ast_build_copy(__isl_keep isl_ast_build *build)
Packit fb9d21
{
Packit fb9d21
	if (!build)
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	build->ref++;
Packit fb9d21
	return build;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
__isl_give isl_ast_build *isl_ast_build_dup(__isl_keep isl_ast_build *build)
Packit fb9d21
{
Packit fb9d21
	isl_ctx *ctx;
Packit fb9d21
	isl_ast_build *dup;
Packit fb9d21
Packit fb9d21
	if (!build)
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	ctx = isl_ast_build_get_ctx(build);
Packit fb9d21
	dup = isl_calloc_type(ctx, isl_ast_build);
Packit fb9d21
	if (!dup)
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	dup->ref = 1;
Packit fb9d21
	dup->outer_pos = build->outer_pos;
Packit fb9d21
	dup->depth = build->depth;
Packit fb9d21
	dup->iterators = isl_id_list_copy(build->iterators);
Packit fb9d21
	dup->domain = isl_set_copy(build->domain);
Packit fb9d21
	dup->generated = isl_set_copy(build->generated);
Packit fb9d21
	dup->pending = isl_set_copy(build->pending);
Packit fb9d21
	dup->values = isl_multi_aff_copy(build->values);
Packit fb9d21
	dup->value = isl_pw_aff_copy(build->value);
Packit fb9d21
	dup->strides = isl_vec_copy(build->strides);
Packit fb9d21
	dup->offsets = isl_multi_aff_copy(build->offsets);
Packit fb9d21
	dup->executed = isl_union_map_copy(build->executed);
Packit fb9d21
	dup->single_valued = build->single_valued;
Packit fb9d21
	dup->options = isl_union_map_copy(build->options);
Packit fb9d21
	dup->at_each_domain = build->at_each_domain;
Packit fb9d21
	dup->at_each_domain_user = build->at_each_domain_user;
Packit fb9d21
	dup->before_each_for = build->before_each_for;
Packit fb9d21
	dup->before_each_for_user = build->before_each_for_user;
Packit fb9d21
	dup->after_each_for = build->after_each_for;
Packit fb9d21
	dup->after_each_for_user = build->after_each_for_user;
Packit fb9d21
	dup->create_leaf = build->create_leaf;
Packit fb9d21
	dup->create_leaf_user = build->create_leaf_user;
Packit fb9d21
Packit fb9d21
	if (!dup->iterators || !dup->domain || !dup->generated ||
Packit fb9d21
	    !dup->pending || !dup->values ||
Packit fb9d21
	    !dup->strides || !dup->offsets || !dup->options ||
Packit fb9d21
	    (build->executed && !dup->executed) ||
Packit fb9d21
	    (build->value && !dup->value))
Packit fb9d21
		return isl_ast_build_free(dup);
Packit fb9d21
Packit fb9d21
	return dup;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Align the parameters of "build" to those of "model", introducing
Packit fb9d21
 * additional parameters if needed.
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_ast_build *isl_ast_build_align_params(
Packit fb9d21
	__isl_take isl_ast_build *build, __isl_take isl_space *model)
Packit fb9d21
{
Packit fb9d21
	build = isl_ast_build_cow(build);
Packit fb9d21
	if (!build)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	build->domain = isl_set_align_params(build->domain,
Packit fb9d21
						isl_space_copy(model));
Packit fb9d21
	build->generated = isl_set_align_params(build->generated,
Packit fb9d21
						isl_space_copy(model));
Packit fb9d21
	build->pending = isl_set_align_params(build->pending,
Packit fb9d21
						isl_space_copy(model));
Packit fb9d21
	build->values = isl_multi_aff_align_params(build->values,
Packit fb9d21
						isl_space_copy(model));
Packit fb9d21
	build->offsets = isl_multi_aff_align_params(build->offsets,
Packit fb9d21
						isl_space_copy(model));
Packit fb9d21
	build->options = isl_union_map_align_params(build->options,
Packit fb9d21
						isl_space_copy(model));
Packit fb9d21
	isl_space_free(model);
Packit fb9d21
Packit fb9d21
	if (!build->domain || !build->values || !build->offsets ||
Packit fb9d21
	    !build->options)
Packit fb9d21
		return isl_ast_build_free(build);
Packit fb9d21
Packit fb9d21
	return build;
Packit fb9d21
error:
Packit fb9d21
	isl_space_free(model);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
__isl_give isl_ast_build *isl_ast_build_cow(__isl_take isl_ast_build *build)
Packit fb9d21
{
Packit fb9d21
	if (!build)
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	if (build->ref == 1)
Packit fb9d21
		return build;
Packit fb9d21
	build->ref--;
Packit fb9d21
	return isl_ast_build_dup(build);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
__isl_null isl_ast_build *isl_ast_build_free(
Packit fb9d21
	__isl_take isl_ast_build *build)
Packit fb9d21
{
Packit fb9d21
	if (!build)
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	if (--build->ref > 0)
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	isl_id_list_free(build->iterators);
Packit fb9d21
	isl_set_free(build->domain);
Packit fb9d21
	isl_set_free(build->generated);
Packit fb9d21
	isl_set_free(build->pending);
Packit fb9d21
	isl_multi_aff_free(build->values);
Packit fb9d21
	isl_pw_aff_free(build->value);
Packit fb9d21
	isl_vec_free(build->strides);
Packit fb9d21
	isl_multi_aff_free(build->offsets);
Packit fb9d21
	isl_multi_aff_free(build->schedule_map);
Packit fb9d21
	isl_union_map_free(build->executed);
Packit fb9d21
	isl_union_map_free(build->options);
Packit fb9d21
Packit fb9d21
	free(build);
Packit fb9d21
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
isl_ctx *isl_ast_build_get_ctx(__isl_keep isl_ast_build *build)
Packit fb9d21
{
Packit fb9d21
	return build ? isl_set_get_ctx(build->domain) : NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Replace build->options by "options".
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_ast_build *isl_ast_build_set_options(
Packit fb9d21
	__isl_take isl_ast_build *build, __isl_take isl_union_map *options)
Packit fb9d21
{
Packit fb9d21
	build = isl_ast_build_cow(build);
Packit fb9d21
Packit fb9d21
	if (!build || !options)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	isl_union_map_free(build->options);
Packit fb9d21
	build->options = options;
Packit fb9d21
Packit fb9d21
	return build;
Packit fb9d21
error:
Packit fb9d21
	isl_union_map_free(options);
Packit fb9d21
	return isl_ast_build_free(build);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Set the iterators for the next code generation.
Packit fb9d21
 *
Packit fb9d21
 * If we still have some iterators left from the previous code generation
Packit fb9d21
 * (if any) or if iterators have already been set by a previous
Packit fb9d21
 * call to this function, then we remove them first.
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_ast_build *isl_ast_build_set_iterators(
Packit fb9d21
	__isl_take isl_ast_build *build, __isl_take isl_id_list *iterators)
Packit fb9d21
{
Packit fb9d21
	int dim, n_it;
Packit fb9d21
Packit fb9d21
	build = isl_ast_build_cow(build);
Packit fb9d21
	if (!build)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	dim = isl_set_dim(build->domain, isl_dim_set);
Packit fb9d21
	n_it = isl_id_list_n_id(build->iterators);
Packit fb9d21
	if (n_it < dim)
Packit fb9d21
		isl_die(isl_ast_build_get_ctx(build), isl_error_internal,
Packit fb9d21
			"isl_ast_build in inconsistent state", goto error);
Packit fb9d21
	if (n_it > dim)
Packit fb9d21
		build->iterators = isl_id_list_drop(build->iterators,
Packit fb9d21
							dim, n_it - dim);
Packit fb9d21
	build->iterators = isl_id_list_concat(build->iterators, iterators);
Packit fb9d21
	if (!build->iterators)
Packit fb9d21
		return isl_ast_build_free(build);
Packit fb9d21
Packit fb9d21
	return build;
Packit fb9d21
error:
Packit fb9d21
	isl_id_list_free(iterators);
Packit fb9d21
	return isl_ast_build_free(build);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Set the "at_each_domain" callback of "build" to "fn".
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_ast_build *isl_ast_build_set_at_each_domain(
Packit fb9d21
	__isl_take isl_ast_build *build,
Packit fb9d21
	__isl_give isl_ast_node *(*fn)(__isl_take isl_ast_node *node,
Packit fb9d21
		__isl_keep isl_ast_build *build, void *user), void *user)
Packit fb9d21
{
Packit fb9d21
	build = isl_ast_build_cow(build);
Packit fb9d21
Packit fb9d21
	if (!build)
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	build->at_each_domain = fn;
Packit fb9d21
	build->at_each_domain_user = user;
Packit fb9d21
Packit fb9d21
	return build;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Set the "before_each_for" callback of "build" to "fn".
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_ast_build *isl_ast_build_set_before_each_for(
Packit fb9d21
	__isl_take isl_ast_build *build,
Packit fb9d21
	__isl_give isl_id *(*fn)(__isl_keep isl_ast_build *build,
Packit fb9d21
		void *user), void *user)
Packit fb9d21
{
Packit fb9d21
	build = isl_ast_build_cow(build);
Packit fb9d21
Packit fb9d21
	if (!build)
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	build->before_each_for = fn;
Packit fb9d21
	build->before_each_for_user = user;
Packit fb9d21
Packit fb9d21
	return build;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Set the "after_each_for" callback of "build" to "fn".
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_ast_build *isl_ast_build_set_after_each_for(
Packit fb9d21
	__isl_take isl_ast_build *build,
Packit fb9d21
	__isl_give isl_ast_node *(*fn)(__isl_take isl_ast_node *node,
Packit fb9d21
		__isl_keep isl_ast_build *build, void *user), void *user)
Packit fb9d21
{
Packit fb9d21
	build = isl_ast_build_cow(build);
Packit fb9d21
Packit fb9d21
	if (!build)
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	build->after_each_for = fn;
Packit fb9d21
	build->after_each_for_user = user;
Packit fb9d21
Packit fb9d21
	return build;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Set the "create_leaf" callback of "build" to "fn".
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_ast_build *isl_ast_build_set_create_leaf(
Packit fb9d21
	__isl_take isl_ast_build *build,
Packit fb9d21
	__isl_give isl_ast_node *(*fn)(__isl_take isl_ast_build *build,
Packit fb9d21
		void *user), void *user)
Packit fb9d21
{
Packit fb9d21
	build = isl_ast_build_cow(build);
Packit fb9d21
Packit fb9d21
	if (!build)
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	build->create_leaf = fn;
Packit fb9d21
	build->create_leaf_user = user;
Packit fb9d21
Packit fb9d21
	return build;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Clear all information that is specific to this code generation
Packit fb9d21
 * and that is (probably) not meaningful to any nested code generation.
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_ast_build *isl_ast_build_clear_local_info(
Packit fb9d21
	__isl_take isl_ast_build *build)
Packit fb9d21
{
Packit fb9d21
	isl_space *space;
Packit fb9d21
Packit fb9d21
	build = isl_ast_build_cow(build);
Packit fb9d21
	if (!build)
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	space = isl_union_map_get_space(build->options);
Packit fb9d21
	isl_union_map_free(build->options);
Packit fb9d21
	build->options = isl_union_map_empty(space);
Packit fb9d21
Packit fb9d21
	build->at_each_domain = NULL;
Packit fb9d21
	build->at_each_domain_user = NULL;
Packit fb9d21
	build->before_each_for = NULL;
Packit fb9d21
	build->before_each_for_user = NULL;
Packit fb9d21
	build->after_each_for = NULL;
Packit fb9d21
	build->after_each_for_user = NULL;
Packit fb9d21
	build->create_leaf = NULL;
Packit fb9d21
	build->create_leaf_user = NULL;
Packit fb9d21
Packit fb9d21
	if (!build->options)
Packit fb9d21
		return isl_ast_build_free(build);
Packit fb9d21
Packit fb9d21
	return build;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Have any loops been eliminated?
Packit fb9d21
 * That is, do any of the original schedule dimensions have a fixed
Packit fb9d21
 * value that has been substituted?
Packit fb9d21
 */
Packit fb9d21
static int any_eliminated(isl_ast_build *build)
Packit fb9d21
{
Packit fb9d21
	int i;
Packit fb9d21
Packit fb9d21
	for (i = 0; i < build->depth; ++i)
Packit fb9d21
		if (isl_ast_build_has_affine_value(build, i))
Packit fb9d21
			return 1;
Packit fb9d21
Packit fb9d21
	return 0;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Clear build->schedule_map.
Packit fb9d21
 * This function should be called whenever anything that might affect
Packit fb9d21
 * the result of isl_ast_build_get_schedule_map_multi_aff changes.
Packit fb9d21
 * In particular, it should be called when the depth is changed or
Packit fb9d21
 * when an iterator is determined to have a fixed value.
Packit fb9d21
 */
Packit fb9d21
static void isl_ast_build_reset_schedule_map(__isl_keep isl_ast_build *build)
Packit fb9d21
{
Packit fb9d21
	if (!build)
Packit fb9d21
		return;
Packit fb9d21
	isl_multi_aff_free(build->schedule_map);
Packit fb9d21
	build->schedule_map = NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Do we need a (non-trivial) schedule map?
Packit fb9d21
 * That is, is the internal schedule space different from
Packit fb9d21
 * the external schedule space?
Packit fb9d21
 *
Packit fb9d21
 * The internal and external schedule spaces are only the same
Packit fb9d21
 * if code has been generated for the entire schedule and if none
Packit fb9d21
 * of the loops have been eliminated.
Packit fb9d21
 */
Packit fb9d21
__isl_give int isl_ast_build_need_schedule_map(__isl_keep isl_ast_build *build)
Packit fb9d21
{
Packit fb9d21
	int dim;
Packit fb9d21
Packit fb9d21
	if (!build)
Packit fb9d21
		return -1;
Packit fb9d21
Packit fb9d21
	dim = isl_set_dim(build->domain, isl_dim_set);
Packit fb9d21
	return build->depth != dim || any_eliminated(build);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Return a mapping from the internal schedule space to the external
Packit fb9d21
 * schedule space in the form of an isl_multi_aff.
Packit fb9d21
 * The internal schedule space originally corresponds to that of the
Packit fb9d21
 * input schedule.  This may change during the code generation if
Packit fb9d21
 * if isl_ast_build_insert_dim is ever called.
Packit fb9d21
 * The external schedule space corresponds to the
Packit fb9d21
 * loops that have been generated.
Packit fb9d21
 *
Packit fb9d21
 * Currently, the only difference between the internal schedule domain
Packit fb9d21
 * and the external schedule domain is that some dimensions are projected
Packit fb9d21
 * out in the external schedule domain.  In particular, the dimensions
Packit fb9d21
 * for which no code has been generated yet and the dimensions that correspond
Packit fb9d21
 * to eliminated loops.
Packit fb9d21
 *
Packit fb9d21
 * We cache a copy of the schedule_map in build->schedule_map.
Packit fb9d21
 * The cache is cleared through isl_ast_build_reset_schedule_map
Packit fb9d21
 * whenever anything changes that might affect the result of this function.
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_multi_aff *isl_ast_build_get_schedule_map_multi_aff(
Packit fb9d21
	__isl_keep isl_ast_build *build)
Packit fb9d21
{
Packit fb9d21
	isl_space *space;
Packit fb9d21
	isl_multi_aff *ma;
Packit fb9d21
Packit fb9d21
	if (!build)
Packit fb9d21
		return NULL;
Packit fb9d21
	if (build->schedule_map)
Packit fb9d21
		return isl_multi_aff_copy(build->schedule_map);
Packit fb9d21
Packit fb9d21
	space = isl_ast_build_get_space(build, 1);
Packit fb9d21
	space = isl_space_map_from_set(space);
Packit fb9d21
	ma = isl_multi_aff_identity(space);
Packit fb9d21
	if (isl_ast_build_need_schedule_map(build)) {
Packit fb9d21
		int i;
Packit fb9d21
		int dim = isl_set_dim(build->domain, isl_dim_set);
Packit fb9d21
		ma = isl_multi_aff_drop_dims(ma, isl_dim_out,
Packit fb9d21
					build->depth, dim - build->depth);
Packit fb9d21
		for (i = build->depth - 1; i >= 0; --i)
Packit fb9d21
			if (isl_ast_build_has_affine_value(build, i))
Packit fb9d21
				ma = isl_multi_aff_drop_dims(ma,
Packit fb9d21
							isl_dim_out, i, 1);
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	build->schedule_map = ma;
Packit fb9d21
	return isl_multi_aff_copy(build->schedule_map);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Return a mapping from the internal schedule space to the external
Packit fb9d21
 * schedule space in the form of an isl_map.
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_map *isl_ast_build_get_schedule_map(
Packit fb9d21
	__isl_keep isl_ast_build *build)
Packit fb9d21
{
Packit fb9d21
	isl_multi_aff *ma;
Packit fb9d21
Packit fb9d21
	ma = isl_ast_build_get_schedule_map_multi_aff(build);
Packit fb9d21
	return isl_map_from_multi_aff(ma);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Return the position of the dimension in build->domain for which
Packit fb9d21
 * an AST node is currently being generated.
Packit fb9d21
 */
Packit fb9d21
int isl_ast_build_get_depth(__isl_keep isl_ast_build *build)
Packit fb9d21
{
Packit fb9d21
	return build ? build->depth : -1;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Prepare for generating code for the next level.
Packit fb9d21
 * In particular, increase the depth and reset any information
Packit fb9d21
 * that is local to the current depth.
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_ast_build *isl_ast_build_increase_depth(
Packit fb9d21
	__isl_take isl_ast_build *build)
Packit fb9d21
{
Packit fb9d21
	build = isl_ast_build_cow(build);
Packit fb9d21
	if (!build)
Packit fb9d21
		return NULL;
Packit fb9d21
	build->depth++;
Packit fb9d21
	isl_ast_build_reset_schedule_map(build);
Packit fb9d21
	build->value = isl_pw_aff_free(build->value);
Packit fb9d21
	return build;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
void isl_ast_build_dump(__isl_keep isl_ast_build *build)
Packit fb9d21
{
Packit fb9d21
	if (!build)
Packit fb9d21
		return;
Packit fb9d21
Packit fb9d21
	fprintf(stderr, "domain: ");
Packit fb9d21
	isl_set_dump(build->domain);
Packit fb9d21
	fprintf(stderr, "generated: ");
Packit fb9d21
	isl_set_dump(build->generated);
Packit fb9d21
	fprintf(stderr, "pending: ");
Packit fb9d21
	isl_set_dump(build->pending);
Packit fb9d21
	fprintf(stderr, "iterators: ");
Packit fb9d21
	isl_id_list_dump(build->iterators);
Packit fb9d21
	fprintf(stderr, "values: ");
Packit fb9d21
	isl_multi_aff_dump(build->values);
Packit fb9d21
	if (build->value) {
Packit fb9d21
		fprintf(stderr, "value: ");
Packit fb9d21
		isl_pw_aff_dump(build->value);
Packit fb9d21
	}
Packit fb9d21
	fprintf(stderr, "strides: ");
Packit fb9d21
	isl_vec_dump(build->strides);
Packit fb9d21
	fprintf(stderr, "offsets: ");
Packit fb9d21
	isl_multi_aff_dump(build->offsets);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Initialize "build" for AST construction in schedule space "space"
Packit fb9d21
 * in the case that build->domain is a parameter set.
Packit fb9d21
 *
Packit fb9d21
 * build->iterators is assumed to have been updated already.
Packit fb9d21
 */
Packit fb9d21
static __isl_give isl_ast_build *isl_ast_build_init(
Packit fb9d21
	__isl_take isl_ast_build *build, __isl_take isl_space *space)
Packit fb9d21
{
Packit fb9d21
	isl_set *set;
Packit fb9d21
Packit fb9d21
	build = isl_ast_build_cow(build);
Packit fb9d21
	if (!build)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	set = isl_set_universe(isl_space_copy(space));
Packit fb9d21
	build->domain = isl_set_intersect_params(isl_set_copy(set),
Packit fb9d21
						    build->domain);
Packit fb9d21
	build->pending = isl_set_intersect_params(isl_set_copy(set),
Packit fb9d21
						    build->pending);
Packit fb9d21
	build->generated = isl_set_intersect_params(set, build->generated);
Packit fb9d21
Packit fb9d21
	return isl_ast_build_init_derived(build, space);
Packit fb9d21
error:
Packit fb9d21
	isl_ast_build_free(build);
Packit fb9d21
	isl_space_free(space);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Assign "aff" to *user and return -1, effectively extracting
Packit fb9d21
 * the first (and presumably only) affine expression in the isl_pw_aff
Packit fb9d21
 * on which this function is used.
Packit fb9d21
 */
Packit fb9d21
static int extract_single_piece(__isl_take isl_set *set,
Packit fb9d21
	__isl_take isl_aff *aff, void *user)
Packit fb9d21
{
Packit fb9d21
	isl_aff **p = user;
Packit fb9d21
Packit fb9d21
	*p = aff;
Packit fb9d21
	isl_set_free(set);
Packit fb9d21
Packit fb9d21
	return -1;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Intersect "set" with the stride constraint of "build", if any.
Packit fb9d21
 */
Packit fb9d21
static __isl_give isl_set *intersect_stride_constraint(__isl_take isl_set *set,
Packit fb9d21
	__isl_keep isl_ast_build *build)
Packit fb9d21
{
Packit fb9d21
	isl_set *stride;
Packit fb9d21
Packit fb9d21
	if (!build)
Packit fb9d21
		return isl_set_free(set);
Packit fb9d21
	if (!isl_ast_build_has_stride(build, build->depth))
Packit fb9d21
		return set;
Packit fb9d21
Packit fb9d21
	stride = isl_ast_build_get_stride_constraint(build);
Packit fb9d21
	return isl_set_intersect(set, stride);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Check if the given bounds on the current dimension (together with
Packit fb9d21
 * the stride constraint, if any) imply that
Packit fb9d21
 * this current dimension attains only a single value (in terms of
Packit fb9d21
 * parameters and outer dimensions).
Packit fb9d21
 * If so, we record it in build->value.
Packit fb9d21
 * If, moreover, this value can be represented as a single affine expression,
Packit fb9d21
 * then we also update build->values, effectively marking the current
Packit fb9d21
 * dimension as "eliminated".
Packit fb9d21
 *
Packit fb9d21
 * When computing the gist of the fixed value that can be represented
Packit fb9d21
 * as a single affine expression, it is important to only take into
Packit fb9d21
 * account the domain constraints in the original AST build and
Packit fb9d21
 * not the domain of the affine expression itself.
Packit fb9d21
 * Otherwise, a [i/3] is changed into a i/3 because we know that i
Packit fb9d21
 * is a multiple of 3, but then we end up not expressing anywhere
Packit fb9d21
 * in the context that i is a multiple of 3.
Packit fb9d21
 */
Packit fb9d21
static __isl_give isl_ast_build *update_values(
Packit fb9d21
	__isl_take isl_ast_build *build, __isl_take isl_basic_set *bounds)
Packit fb9d21
{
Packit fb9d21
	int sv;
Packit fb9d21
	isl_pw_multi_aff *pma;
Packit fb9d21
	isl_aff *aff = NULL;
Packit fb9d21
	isl_map *it_map;
Packit fb9d21
	isl_set *set;
Packit fb9d21
Packit fb9d21
	set = isl_set_from_basic_set(bounds);
Packit fb9d21
	set = isl_set_intersect(set, isl_set_copy(build->domain));
Packit fb9d21
	set = intersect_stride_constraint(set, build);
Packit fb9d21
	it_map = isl_ast_build_map_to_iterator(build, set);
Packit fb9d21
Packit fb9d21
	sv = isl_map_is_single_valued(it_map);
Packit fb9d21
	if (sv < 0)
Packit fb9d21
		build = isl_ast_build_free(build);
Packit fb9d21
	if (!build || !sv) {
Packit fb9d21
		isl_map_free(it_map);
Packit fb9d21
		return build;
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	pma = isl_pw_multi_aff_from_map(it_map);
Packit fb9d21
	build->value = isl_pw_multi_aff_get_pw_aff(pma, 0);
Packit fb9d21
	build->value = isl_ast_build_compute_gist_pw_aff(build, build->value);
Packit fb9d21
	build->value = isl_pw_aff_coalesce(build->value);
Packit fb9d21
	isl_pw_multi_aff_free(pma);
Packit fb9d21
Packit fb9d21
	if (!build->value)
Packit fb9d21
		return isl_ast_build_free(build);
Packit fb9d21
Packit fb9d21
	if (isl_pw_aff_n_piece(build->value) != 1)
Packit fb9d21
		return build;
Packit fb9d21
Packit fb9d21
	isl_pw_aff_foreach_piece(build->value, &extract_single_piece, &aff;;
Packit fb9d21
Packit fb9d21
	build->values = isl_multi_aff_set_aff(build->values, build->depth, aff);
Packit fb9d21
	if (!build->values)
Packit fb9d21
		return isl_ast_build_free(build);
Packit fb9d21
	isl_ast_build_reset_schedule_map(build);
Packit fb9d21
	return build;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Update the AST build based on the given loop bounds for
Packit fb9d21
 * the current dimension and the stride information available in the build.
Packit fb9d21
 *
Packit fb9d21
 * We first make sure that the bounds do not refer to any iterators
Packit fb9d21
 * that have already been eliminated.
Packit fb9d21
 * Then, we check if the bounds imply that the current iterator
Packit fb9d21
 * has a fixed value.
Packit fb9d21
 * If they do and if this fixed value can be expressed as a single
Packit fb9d21
 * affine expression, we eliminate the iterators from the bounds.
Packit fb9d21
 * Note that we cannot simply plug in this single value using
Packit fb9d21
 * isl_basic_set_preimage_multi_aff as the single value may only
Packit fb9d21
 * be defined on a subset of the domain.  Plugging in the value
Packit fb9d21
 * would restrict the build domain to this subset, while this
Packit fb9d21
 * restriction may not be reflected in the generated code.
Packit fb9d21
 * Finally, we intersect build->domain with the updated bounds.
Packit fb9d21
 * We also add the stride constraint unless we have been able
Packit fb9d21
 * to find a fixed value expressed as a single affine expression.
Packit fb9d21
 *
Packit fb9d21
 * Note that the check for a fixed value in update_values requires
Packit fb9d21
 * us to intersect the bounds with the current build domain.
Packit fb9d21
 * When we intersect build->domain with the updated bounds in
Packit fb9d21
 * the final step, we make sure that these updated bounds have
Packit fb9d21
 * not been intersected with the old build->domain.
Packit fb9d21
 * Otherwise, we would indirectly intersect the build domain with itself,
Packit fb9d21
 * which can lead to inefficiencies, in particular if the build domain
Packit fb9d21
 * contains any unknown divs.
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_ast_build *isl_ast_build_set_loop_bounds(
Packit fb9d21
	__isl_take isl_ast_build *build, __isl_take isl_basic_set *bounds)
Packit fb9d21
{
Packit fb9d21
	isl_set *set;
Packit fb9d21
Packit fb9d21
	build = isl_ast_build_cow(build);
Packit fb9d21
	if (!build)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	bounds = isl_basic_set_preimage_multi_aff(bounds,
Packit fb9d21
					isl_multi_aff_copy(build->values));
Packit fb9d21
	build = update_values(build, isl_basic_set_copy(bounds));
Packit fb9d21
	if (!build)
Packit fb9d21
		goto error;
Packit fb9d21
	set = isl_set_from_basic_set(isl_basic_set_copy(bounds));
Packit fb9d21
	if (isl_ast_build_has_affine_value(build, build->depth)) {
Packit fb9d21
		set = isl_set_eliminate(set, isl_dim_set, build->depth, 1);
Packit fb9d21
		set = isl_set_compute_divs(set);
Packit fb9d21
		build->pending = isl_set_intersect(build->pending,
Packit fb9d21
							isl_set_copy(set));
Packit fb9d21
		build->domain = isl_set_intersect(build->domain, set);
Packit fb9d21
	} else {
Packit fb9d21
		isl_basic_set *generated, *pending;
Packit fb9d21
Packit fb9d21
		pending = isl_basic_set_copy(bounds);
Packit fb9d21
		pending = isl_basic_set_drop_constraints_involving_dims(pending,
Packit fb9d21
					isl_dim_set, build->depth, 1);
Packit fb9d21
		build->pending = isl_set_intersect(build->pending,
Packit fb9d21
					isl_set_from_basic_set(pending));
Packit fb9d21
		generated = isl_basic_set_copy(bounds);
Packit fb9d21
		generated = isl_basic_set_drop_constraints_not_involving_dims(
Packit fb9d21
				    generated, isl_dim_set, build->depth, 1);
Packit fb9d21
		build->generated = isl_set_intersect(build->generated,
Packit fb9d21
					isl_set_from_basic_set(generated));
Packit fb9d21
		build->domain = isl_set_intersect(build->domain, set);
Packit fb9d21
		build = isl_ast_build_include_stride(build);
Packit fb9d21
		if (!build)
Packit fb9d21
			goto error;
Packit fb9d21
	}
Packit fb9d21
	isl_basic_set_free(bounds);
Packit fb9d21
Packit fb9d21
	if (!build->domain || !build->pending || !build->generated)
Packit fb9d21
		return isl_ast_build_free(build);
Packit fb9d21
Packit fb9d21
	return build;
Packit fb9d21
error:
Packit fb9d21
	isl_ast_build_free(build);
Packit fb9d21
	isl_basic_set_free(bounds);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Intersect build->domain with "set", where "set" is specified
Packit fb9d21
 * in terms of the internal schedule domain.
Packit fb9d21
 */
Packit fb9d21
static __isl_give isl_ast_build *isl_ast_build_restrict_internal(
Packit fb9d21
	__isl_take isl_ast_build *build, __isl_take isl_set *set)
Packit fb9d21
{
Packit fb9d21
	build = isl_ast_build_cow(build);
Packit fb9d21
	if (!build)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	set = isl_set_compute_divs(set);
Packit fb9d21
	build->domain = isl_set_intersect(build->domain, set);
Packit fb9d21
	build->domain = isl_set_coalesce(build->domain);
Packit fb9d21
Packit fb9d21
	if (!build->domain)
Packit fb9d21
		return isl_ast_build_free(build);
Packit fb9d21
Packit fb9d21
	return build;
Packit fb9d21
error:
Packit fb9d21
	isl_ast_build_free(build);
Packit fb9d21
	isl_set_free(set);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Intersect build->generated and build->domain with "set",
Packit fb9d21
 * where "set" is specified in terms of the internal schedule domain.
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_ast_build *isl_ast_build_restrict_generated(
Packit fb9d21
	__isl_take isl_ast_build *build, __isl_take isl_set *set)
Packit fb9d21
{
Packit fb9d21
	set = isl_set_compute_divs(set);
Packit fb9d21
	build = isl_ast_build_restrict_internal(build, isl_set_copy(set));
Packit fb9d21
	build = isl_ast_build_cow(build);
Packit fb9d21
	if (!build)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	build->generated = isl_set_intersect(build->generated, set);
Packit fb9d21
	build->generated = isl_set_coalesce(build->generated);
Packit fb9d21
Packit fb9d21
	if (!build->generated)
Packit fb9d21
		return isl_ast_build_free(build);
Packit fb9d21
Packit fb9d21
	return build;
Packit fb9d21
error:
Packit fb9d21
	isl_ast_build_free(build);
Packit fb9d21
	isl_set_free(set);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Replace the set of pending constraints by "guard", which is then
Packit fb9d21
 * no longer considered as pending.
Packit fb9d21
 * That is, add "guard" to the generated constraints and clear all pending
Packit fb9d21
 * constraints, making the domain equal to the generated constraints.
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_ast_build *isl_ast_build_replace_pending_by_guard(
Packit fb9d21
	__isl_take isl_ast_build *build, __isl_take isl_set *guard)
Packit fb9d21
{
Packit fb9d21
	build = isl_ast_build_restrict_generated(build, guard);
Packit fb9d21
	build = isl_ast_build_cow(build);
Packit fb9d21
	if (!build)
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	isl_set_free(build->domain);
Packit fb9d21
	build->domain = isl_set_copy(build->generated);
Packit fb9d21
	isl_set_free(build->pending);
Packit fb9d21
	build->pending = isl_set_universe(isl_set_get_space(build->domain));
Packit fb9d21
Packit fb9d21
	if (!build->pending)
Packit fb9d21
		return isl_ast_build_free(build);
Packit fb9d21
Packit fb9d21
	return build;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Intersect build->pending and build->domain with "set",
Packit fb9d21
 * where "set" is specified in terms of the internal schedule domain.
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_ast_build *isl_ast_build_restrict_pending(
Packit fb9d21
	__isl_take isl_ast_build *build, __isl_take isl_set *set)
Packit fb9d21
{
Packit fb9d21
	set = isl_set_compute_divs(set);
Packit fb9d21
	build = isl_ast_build_restrict_internal(build, isl_set_copy(set));
Packit fb9d21
	build = isl_ast_build_cow(build);
Packit fb9d21
	if (!build)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	build->pending = isl_set_intersect(build->pending, set);
Packit fb9d21
	build->pending = isl_set_coalesce(build->pending);
Packit fb9d21
Packit fb9d21
	if (!build->pending)
Packit fb9d21
		return isl_ast_build_free(build);
Packit fb9d21
Packit fb9d21
	return build;
Packit fb9d21
error:
Packit fb9d21
	isl_ast_build_free(build);
Packit fb9d21
	isl_set_free(set);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Intersect build->domain with "set", where "set" is specified
Packit fb9d21
 * in terms of the external schedule domain.
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_ast_build *isl_ast_build_restrict(
Packit fb9d21
	__isl_take isl_ast_build *build, __isl_take isl_set *set)
Packit fb9d21
{
Packit fb9d21
	if (isl_set_is_params(set))
Packit fb9d21
		return isl_ast_build_restrict_generated(build, set);
Packit fb9d21
Packit fb9d21
	if (isl_ast_build_need_schedule_map(build)) {
Packit fb9d21
		isl_multi_aff *ma;
Packit fb9d21
		ma = isl_ast_build_get_schedule_map_multi_aff(build);
Packit fb9d21
		set = isl_set_preimage_multi_aff(set, ma);
Packit fb9d21
	}
Packit fb9d21
	return isl_ast_build_restrict_generated(build, set);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Replace build->executed by "executed".
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_ast_build *isl_ast_build_set_executed(
Packit fb9d21
	__isl_take isl_ast_build *build, __isl_take isl_union_map *executed)
Packit fb9d21
{
Packit fb9d21
	build = isl_ast_build_cow(build);
Packit fb9d21
	if (!build)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	isl_union_map_free(build->executed);
Packit fb9d21
	build->executed = executed;
Packit fb9d21
Packit fb9d21
	return build;
Packit fb9d21
error:
Packit fb9d21
	isl_ast_build_free(build);
Packit fb9d21
	isl_union_map_free(executed);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Return a copy of the current schedule domain.
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_set *isl_ast_build_get_domain(__isl_keep isl_ast_build *build)
Packit fb9d21
{
Packit fb9d21
	return build ? isl_set_copy(build->domain) : NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Return a copy of the set of pending constraints.
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_set *isl_ast_build_get_pending(
Packit fb9d21
	__isl_keep isl_ast_build *build)
Packit fb9d21
{
Packit fb9d21
	return build ? isl_set_copy(build->pending) : NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Return a copy of the set of generated constraints.
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_set *isl_ast_build_get_generated(
Packit fb9d21
	__isl_keep isl_ast_build *build)
Packit fb9d21
{
Packit fb9d21
	return build ? isl_set_copy(build->generated) : NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Return the number of variables of the given type
Packit fb9d21
 * in the (internal) schedule space.
Packit fb9d21
 */
Packit fb9d21
unsigned isl_ast_build_dim(__isl_keep isl_ast_build *build,
Packit fb9d21
	enum isl_dim_type type)
Packit fb9d21
{
Packit fb9d21
	if (!build)
Packit fb9d21
		return 0;
Packit fb9d21
	return isl_set_dim(build->domain, type);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Return the (schedule) space of "build".
Packit fb9d21
 *
Packit fb9d21
 * If "internal" is set, then this space is the space of the internal
Packit fb9d21
 * representation of the entire schedule, including those parts for
Packit fb9d21
 * which no code has been generated yet.
Packit fb9d21
 *
Packit fb9d21
 * If "internal" is not set, then this space is the external representation
Packit fb9d21
 * of the loops generated so far.
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_space *isl_ast_build_get_space(__isl_keep isl_ast_build *build,
Packit fb9d21
	int internal)
Packit fb9d21
{
Packit fb9d21
	int i;
Packit fb9d21
	int dim;
Packit fb9d21
	isl_space *space;
Packit fb9d21
Packit fb9d21
	if (!build)
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	space = isl_set_get_space(build->domain);
Packit fb9d21
	if (internal)
Packit fb9d21
		return space;
Packit fb9d21
Packit fb9d21
	if (!isl_ast_build_need_schedule_map(build))
Packit fb9d21
		return space;
Packit fb9d21
Packit fb9d21
	dim = isl_set_dim(build->domain, isl_dim_set);
Packit fb9d21
	space = isl_space_drop_dims(space, isl_dim_set,
Packit fb9d21
				    build->depth, dim - build->depth);
Packit fb9d21
	for (i = build->depth - 1; i >= 0; --i)
Packit fb9d21
		if (isl_ast_build_has_affine_value(build, i))
Packit fb9d21
			space = isl_space_drop_dims(space, isl_dim_set, i, 1);
Packit fb9d21
Packit fb9d21
	return space;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Return the external representation of the schedule space of "build",
Packit fb9d21
 * i.e., a space with a dimension for each loop generated so far,
Packit fb9d21
 * with the names of the dimensions set to the loop iterators.
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_space *isl_ast_build_get_schedule_space(
Packit fb9d21
	__isl_keep isl_ast_build *build)
Packit fb9d21
{
Packit fb9d21
	isl_space *space;
Packit fb9d21
	int i, skip;
Packit fb9d21
Packit fb9d21
	if (!build)
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	space = isl_ast_build_get_space(build, 0);
Packit fb9d21
Packit fb9d21
	skip = 0;
Packit fb9d21
	for (i = 0; i < build->depth; ++i) {
Packit fb9d21
		isl_id *id;
Packit fb9d21
Packit fb9d21
		if (isl_ast_build_has_affine_value(build, i)) {
Packit fb9d21
			skip++;
Packit fb9d21
			continue;
Packit fb9d21
		}
Packit fb9d21
Packit fb9d21
		id = isl_ast_build_get_iterator_id(build, i);
Packit fb9d21
		space = isl_space_set_dim_id(space, isl_dim_set, i - skip, id);
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	return space;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Return the current schedule, as stored in build->executed, in terms
Packit fb9d21
 * of the external schedule domain.
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_union_map *isl_ast_build_get_schedule(
Packit fb9d21
	__isl_keep isl_ast_build *build)
Packit fb9d21
{
Packit fb9d21
	isl_union_map *executed;
Packit fb9d21
	isl_union_map *schedule;
Packit fb9d21
Packit fb9d21
	if (!build)
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	executed = isl_union_map_copy(build->executed);
Packit fb9d21
	if (isl_ast_build_need_schedule_map(build)) {
Packit fb9d21
		isl_map *proj = isl_ast_build_get_schedule_map(build);
Packit fb9d21
		executed = isl_union_map_apply_domain(executed,
Packit fb9d21
					isl_union_map_from_map(proj));
Packit fb9d21
	}
Packit fb9d21
	schedule = isl_union_map_reverse(executed);
Packit fb9d21
Packit fb9d21
	return schedule;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Return the iterator attached to the internal schedule dimension "pos".
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_id *isl_ast_build_get_iterator_id(
Packit fb9d21
	__isl_keep isl_ast_build *build, int pos)
Packit fb9d21
{
Packit fb9d21
	if (!build)
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	return isl_id_list_get_id(build->iterators, pos);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Set the stride and offset of the current dimension to the given
Packit fb9d21
 * value and expression.
Packit fb9d21
 *
Packit fb9d21
 * If we had already found a stride before, then the two strides
Packit fb9d21
 * are combined into a single stride.
Packit fb9d21
 *
Packit fb9d21
 * In particular, if the new stride information is of the form
Packit fb9d21
 *
Packit fb9d21
 *	i = f + s (...)
Packit fb9d21
 *
Packit fb9d21
 * and the old stride information is of the form
Packit fb9d21
 *
Packit fb9d21
 *	i = f2 + s2 (...)
Packit fb9d21
 *
Packit fb9d21
 * then we compute the extended gcd of s and s2
Packit fb9d21
 *
Packit fb9d21
 *	a s + b s2 = g,
Packit fb9d21
 *
Packit fb9d21
 * with g = gcd(s,s2), multiply the first equation with t1 = b s2/g
Packit fb9d21
 * and the second with t2 = a s1/g.
Packit fb9d21
 * This results in
Packit fb9d21
 *
Packit fb9d21
 *	i = (b s2 + a s1)/g i = t1 f + t2 f2 + (s s2)/g (...)
Packit fb9d21
 *
Packit fb9d21
 * so that t1 f + t2 f2 is the combined offset and (s s2)/g = lcm(s,s2)
Packit fb9d21
 * is the combined stride.
Packit fb9d21
 */
Packit fb9d21
static __isl_give isl_ast_build *set_stride(__isl_take isl_ast_build *build,
Packit fb9d21
	__isl_take isl_val *stride, __isl_take isl_aff *offset)
Packit fb9d21
{
Packit fb9d21
	int pos;
Packit fb9d21
Packit fb9d21
	build = isl_ast_build_cow(build);
Packit fb9d21
	if (!build || !stride || !offset)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	pos = build->depth;
Packit fb9d21
Packit fb9d21
	if (isl_ast_build_has_stride(build, pos)) {
Packit fb9d21
		isl_val *stride2, *a, *b, *g;
Packit fb9d21
		isl_aff *offset2;
Packit fb9d21
Packit fb9d21
		stride2 = isl_vec_get_element_val(build->strides, pos);
Packit fb9d21
		g = isl_val_gcdext(isl_val_copy(stride), isl_val_copy(stride2),
Packit fb9d21
					&a, &b);
Packit fb9d21
		a = isl_val_mul(a, isl_val_copy(stride));
Packit fb9d21
		a = isl_val_div(a, isl_val_copy(g));
Packit fb9d21
		stride2 = isl_val_div(stride2, g);
Packit fb9d21
		b = isl_val_mul(b, isl_val_copy(stride2));
Packit fb9d21
		stride = isl_val_mul(stride, stride2);
Packit fb9d21
Packit fb9d21
		offset2 = isl_multi_aff_get_aff(build->offsets, pos);
Packit fb9d21
		offset2 = isl_aff_scale_val(offset2, a);
Packit fb9d21
		offset = isl_aff_scale_val(offset, b);
Packit fb9d21
		offset = isl_aff_add(offset, offset2);
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	build->strides = isl_vec_set_element_val(build->strides, pos, stride);
Packit fb9d21
	build->offsets = isl_multi_aff_set_aff(build->offsets, pos, offset);
Packit fb9d21
	if (!build->strides || !build->offsets)
Packit fb9d21
		return isl_ast_build_free(build);
Packit fb9d21
Packit fb9d21
	return build;
Packit fb9d21
error:
Packit fb9d21
	isl_val_free(stride);
Packit fb9d21
	isl_aff_free(offset);
Packit fb9d21
	return isl_ast_build_free(build);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Return a set expressing the stride constraint at the current depth.
Packit fb9d21
 *
Packit fb9d21
 * In particular, if the current iterator (i) is known to attain values
Packit fb9d21
 *
Packit fb9d21
 *	f + s a
Packit fb9d21
 *
Packit fb9d21
 * where f is the offset and s is the stride, then the returned set
Packit fb9d21
 * expresses the constraint
Packit fb9d21
 *
Packit fb9d21
 *	(f - i) mod s = 0
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_set *isl_ast_build_get_stride_constraint(
Packit fb9d21
	__isl_keep isl_ast_build *build)
Packit fb9d21
{
Packit fb9d21
	isl_aff *aff;
Packit fb9d21
	isl_set *set;
Packit fb9d21
	isl_val *stride;
Packit fb9d21
	int pos;
Packit fb9d21
Packit fb9d21
	if (!build)
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	pos = build->depth;
Packit fb9d21
Packit fb9d21
	if (!isl_ast_build_has_stride(build, pos))
Packit fb9d21
		return isl_set_universe(isl_ast_build_get_space(build, 1));
Packit fb9d21
Packit fb9d21
	stride = isl_ast_build_get_stride(build, pos);
Packit fb9d21
	aff = isl_ast_build_get_offset(build, pos);
Packit fb9d21
	aff = isl_aff_add_coefficient_si(aff, isl_dim_in, pos, -1);
Packit fb9d21
	aff = isl_aff_mod_val(aff, stride);
Packit fb9d21
	set = isl_set_from_basic_set(isl_aff_zero_basic_set(aff));
Packit fb9d21
Packit fb9d21
	return set;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Return the expansion implied by the stride and offset at the current
Packit fb9d21
 * depth.
Packit fb9d21
 *
Packit fb9d21
 * That is, return the mapping
Packit fb9d21
 *
Packit fb9d21
 *	[i_0, ..., i_{d-1}, i_d, i_{d+1}, ...]
Packit fb9d21
 *		-> [i_0, ..., i_{d-1}, s * i_d + offset(i),  i_{d+1}, ...]
Packit fb9d21
 *
Packit fb9d21
 * where s is the stride at the current depth d and offset(i) is
Packit fb9d21
 * the corresponding offset.
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_multi_aff *isl_ast_build_get_stride_expansion(
Packit fb9d21
	__isl_keep isl_ast_build *build)
Packit fb9d21
{
Packit fb9d21
	isl_space *space;
Packit fb9d21
	isl_multi_aff *ma;
Packit fb9d21
	int pos;
Packit fb9d21
	isl_aff *aff, *offset;
Packit fb9d21
	isl_val *stride;
Packit fb9d21
Packit fb9d21
	if (!build)
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	pos = isl_ast_build_get_depth(build);
Packit fb9d21
	space = isl_ast_build_get_space(build, 1);
Packit fb9d21
	space = isl_space_map_from_set(space);
Packit fb9d21
	ma = isl_multi_aff_identity(space);
Packit fb9d21
Packit fb9d21
	if (!isl_ast_build_has_stride(build, pos))
Packit fb9d21
		return ma;
Packit fb9d21
Packit fb9d21
	offset = isl_ast_build_get_offset(build, pos);
Packit fb9d21
	stride = isl_ast_build_get_stride(build, pos);
Packit fb9d21
	aff = isl_multi_aff_get_aff(ma, pos);
Packit fb9d21
	aff = isl_aff_scale_val(aff, stride);
Packit fb9d21
	aff = isl_aff_add(aff, offset);
Packit fb9d21
	ma = isl_multi_aff_set_aff(ma, pos, aff);
Packit fb9d21
Packit fb9d21
	return ma;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Add constraints corresponding to any previously detected
Packit fb9d21
 * stride on the current dimension to build->domain.
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_ast_build *isl_ast_build_include_stride(
Packit fb9d21
	__isl_take isl_ast_build *build)
Packit fb9d21
{
Packit fb9d21
	isl_set *set;
Packit fb9d21
Packit fb9d21
	if (!build)
Packit fb9d21
		return NULL;
Packit fb9d21
	if (!isl_ast_build_has_stride(build, build->depth))
Packit fb9d21
		return build;
Packit fb9d21
	build = isl_ast_build_cow(build);
Packit fb9d21
	if (!build)
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	set = isl_ast_build_get_stride_constraint(build);
Packit fb9d21
Packit fb9d21
	build->domain = isl_set_intersect(build->domain, isl_set_copy(set));
Packit fb9d21
	build->generated = isl_set_intersect(build->generated, set);
Packit fb9d21
	if (!build->domain || !build->generated)
Packit fb9d21
		return isl_ast_build_free(build);
Packit fb9d21
Packit fb9d21
	return build;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Information used inside detect_stride.
Packit fb9d21
 *
Packit fb9d21
 * "build" may be updated by detect_stride to include stride information.
Packit fb9d21
 * "pos" is equal to build->depth.
Packit fb9d21
 */
Packit fb9d21
struct isl_detect_stride_data {
Packit fb9d21
	isl_ast_build *build;
Packit fb9d21
	int pos;
Packit fb9d21
};
Packit fb9d21
Packit fb9d21
/* Check if constraint "c" imposes any stride on dimension data->pos
Packit fb9d21
 * and, if so, update the stride information in data->build.
Packit fb9d21
 *
Packit fb9d21
 * In order to impose a stride on the dimension, "c" needs to be an equality
Packit fb9d21
 * and it needs to involve the dimension.  Note that "c" may also be
Packit fb9d21
 * a div constraint and thus an inequality that we cannot use.
Packit fb9d21
 *
Packit fb9d21
 * Let c be of the form
Packit fb9d21
 *
Packit fb9d21
 *	h(p) + g * v * i + g * stride * f(alpha) = 0
Packit fb9d21
 *
Packit fb9d21
 * with h(p) an expression in terms of the parameters and outer dimensions
Packit fb9d21
 * and f(alpha) an expression in terms of the existentially quantified
Packit fb9d21
 * variables.  Note that the inner dimensions have been eliminated so
Packit fb9d21
 * they do not appear in "c".
Packit fb9d21
 *
Packit fb9d21
 * If "stride" is not zero and not one, then it represents a non-trivial stride
Packit fb9d21
 * on "i".  We compute a and b such that
Packit fb9d21
 *
Packit fb9d21
 *	a v + b stride = 1
Packit fb9d21
 *
Packit fb9d21
 * We have
Packit fb9d21
 *
Packit fb9d21
 *	g v i = -h(p) + g stride f(alpha)
Packit fb9d21
 *
Packit fb9d21
 *	a g v i = -a h(p) + g stride f(alpha)
Packit fb9d21
 *
Packit fb9d21
 *	a g v i + b g stride i = -a h(p) + g stride * (...)
Packit fb9d21
 *
Packit fb9d21
 *	g i = -a h(p) + g stride * (...)
Packit fb9d21
 *
Packit fb9d21
 *	i = -a h(p)/g + stride * (...)
Packit fb9d21
 *
Packit fb9d21
 * The expression "-a h(p)/g" can therefore be used as offset.
Packit fb9d21
 */
Packit fb9d21
static int detect_stride(__isl_take isl_constraint *c, void *user)
Packit fb9d21
{
Packit fb9d21
	struct isl_detect_stride_data *data = user;
Packit fb9d21
	int i, n_div;
Packit fb9d21
	isl_ctx *ctx;
Packit fb9d21
	isl_val *v, *stride, *m;
Packit fb9d21
Packit fb9d21
	if (!isl_constraint_is_equality(c) ||
Packit fb9d21
	    !isl_constraint_involves_dims(c, isl_dim_set, data->pos, 1)) {
Packit fb9d21
		isl_constraint_free(c);
Packit fb9d21
		return 0;
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	ctx = isl_constraint_get_ctx(c);
Packit fb9d21
	stride = isl_val_zero(ctx);
Packit fb9d21
	n_div = isl_constraint_dim(c, isl_dim_div);
Packit fb9d21
	for (i = 0; i < n_div; ++i) {
Packit fb9d21
		v = isl_constraint_get_coefficient_val(c, isl_dim_div, i);
Packit fb9d21
		stride = isl_val_gcd(stride, v);
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	v = isl_constraint_get_coefficient_val(c, isl_dim_set, data->pos);
Packit fb9d21
	m = isl_val_gcd(isl_val_copy(stride), isl_val_copy(v));
Packit fb9d21
	stride = isl_val_div(stride, isl_val_copy(m));
Packit fb9d21
	v = isl_val_div(v, isl_val_copy(m));
Packit fb9d21
Packit fb9d21
	if (!isl_val_is_zero(stride) && !isl_val_is_one(stride)) {
Packit fb9d21
		isl_aff *aff;
Packit fb9d21
		isl_val *gcd, *a, *b;
Packit fb9d21
Packit fb9d21
		gcd = isl_val_gcdext(v, isl_val_copy(stride), &a, &b);
Packit fb9d21
		isl_val_free(gcd);
Packit fb9d21
		isl_val_free(b);
Packit fb9d21
Packit fb9d21
		aff = isl_constraint_get_aff(c);
Packit fb9d21
		for (i = 0; i < n_div; ++i)
Packit fb9d21
			aff = isl_aff_set_coefficient_si(aff,
Packit fb9d21
							 isl_dim_div, i, 0);
Packit fb9d21
		aff = isl_aff_set_coefficient_si(aff, isl_dim_in, data->pos, 0);
Packit fb9d21
		a = isl_val_neg(a);
Packit fb9d21
		aff = isl_aff_scale_val(aff, a);
Packit fb9d21
		aff = isl_aff_scale_down_val(aff, m);
Packit fb9d21
		data->build = set_stride(data->build, stride, aff);
Packit fb9d21
	} else {
Packit fb9d21
		isl_val_free(stride);
Packit fb9d21
		isl_val_free(m);
Packit fb9d21
		isl_val_free(v);
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	isl_constraint_free(c);
Packit fb9d21
	return 0;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Check if the constraints in "set" imply any stride on the current
Packit fb9d21
 * dimension and, if so, record the stride information in "build"
Packit fb9d21
 * and return the updated "build".
Packit fb9d21
 *
Packit fb9d21
 * We compute the affine hull and then check if any of the constraints
Packit fb9d21
 * in the hull imposes any stride on the current dimension.
Packit fb9d21
 *
Packit fb9d21
 * We assume that inner dimensions have been eliminated from "set"
Packit fb9d21
 * by the caller.  This is needed because the common stride
Packit fb9d21
 * may be imposed by different inner dimensions on different parts of
Packit fb9d21
 * the domain.
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_ast_build *isl_ast_build_detect_strides(
Packit fb9d21
	__isl_take isl_ast_build *build, __isl_take isl_set *set)
Packit fb9d21
{
Packit fb9d21
	isl_basic_set *hull;
Packit fb9d21
	struct isl_detect_stride_data data;
Packit fb9d21
Packit fb9d21
	if (!build)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	data.build = build;
Packit fb9d21
	data.pos = isl_ast_build_get_depth(build);
Packit fb9d21
	hull = isl_set_affine_hull(set);
Packit fb9d21
Packit fb9d21
	if (isl_basic_set_foreach_constraint(hull, &detect_stride, &data) < 0)
Packit fb9d21
		data.build = isl_ast_build_free(data.build);
Packit fb9d21
Packit fb9d21
	isl_basic_set_free(hull);
Packit fb9d21
	return data.build;
Packit fb9d21
error:
Packit fb9d21
	isl_set_free(set);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
struct isl_ast_build_involves_data {
Packit fb9d21
	int depth;
Packit fb9d21
	int involves;
Packit fb9d21
};
Packit fb9d21
Packit fb9d21
/* Check if "map" involves the input dimension data->depth.
Packit fb9d21
 */
Packit fb9d21
static int involves_depth(__isl_take isl_map *map, void *user)
Packit fb9d21
{
Packit fb9d21
	struct isl_ast_build_involves_data *data = user;
Packit fb9d21
Packit fb9d21
	data->involves = isl_map_involves_dims(map, isl_dim_in, data->depth, 1);
Packit fb9d21
	isl_map_free(map);
Packit fb9d21
Packit fb9d21
	if (data->involves < 0 || data->involves)
Packit fb9d21
		return -1;
Packit fb9d21
	return 0;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Do any options depend on the value of the dimension at the current depth?
Packit fb9d21
 */
Packit fb9d21
int isl_ast_build_options_involve_depth(__isl_keep isl_ast_build *build)
Packit fb9d21
{
Packit fb9d21
	struct isl_ast_build_involves_data data;
Packit fb9d21
Packit fb9d21
	if (!build)
Packit fb9d21
		return -1;
Packit fb9d21
Packit fb9d21
	data.depth = build->depth;
Packit fb9d21
	data.involves = 0;
Packit fb9d21
Packit fb9d21
	if (isl_union_map_foreach_map(build->options,
Packit fb9d21
					&involves_depth, &data) < 0) {
Packit fb9d21
		if (data.involves < 0 || !data.involves)
Packit fb9d21
			return -1;
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	return data.involves;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Construct the map
Packit fb9d21
 *
Packit fb9d21
 *	{ [i] -> [i] : i < pos; [i] -> [i + 1] : i >= pos }
Packit fb9d21
 *
Packit fb9d21
 * with "space" the parameter space of the constructed map.
Packit fb9d21
 */
Packit fb9d21
static __isl_give isl_map *construct_insertion_map(__isl_take isl_space *space,
Packit fb9d21
	int pos)
Packit fb9d21
{
Packit fb9d21
	isl_constraint *c;
Packit fb9d21
	isl_basic_map *bmap1, *bmap2;
Packit fb9d21
Packit fb9d21
	space = isl_space_set_from_params(space);
Packit fb9d21
	space = isl_space_add_dims(space, isl_dim_set, 1);
Packit fb9d21
	space = isl_space_map_from_set(space);
Packit fb9d21
	c = isl_equality_alloc(isl_local_space_from_space(space));
Packit fb9d21
	c = isl_constraint_set_coefficient_si(c, isl_dim_in, 0, 1);
Packit fb9d21
	c = isl_constraint_set_coefficient_si(c, isl_dim_out, 0, -1);
Packit fb9d21
	bmap1 = isl_basic_map_from_constraint(isl_constraint_copy(c));
Packit fb9d21
	c = isl_constraint_set_constant_si(c, 1);
Packit fb9d21
	bmap2 = isl_basic_map_from_constraint(c);
Packit fb9d21
Packit fb9d21
	bmap1 = isl_basic_map_upper_bound_si(bmap1, isl_dim_in, 0, pos - 1);
Packit fb9d21
	bmap2 = isl_basic_map_lower_bound_si(bmap2, isl_dim_in, 0, pos);
Packit fb9d21
Packit fb9d21
	return isl_basic_map_union(bmap1, bmap2);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
static const char *option_str[] = {
Packit fb9d21
	[atomic] = "atomic",
Packit fb9d21
	[unroll] = "unroll",
Packit fb9d21
	[separate] = "separate"
Packit fb9d21
};
Packit fb9d21
Packit fb9d21
/* Update the "options" to reflect the insertion of a dimension
Packit fb9d21
 * at position "pos" in the schedule domain space.
Packit fb9d21
 * "space" is the original domain space before the insertion and
Packit fb9d21
 * may be named and/or structured.
Packit fb9d21
 *
Packit fb9d21
 * The (relevant) input options all have "space" as domain, which
Packit fb9d21
 * has to be mapped to the extended space.
Packit fb9d21
 * The values of the ranges also refer to the schedule domain positions
Packit fb9d21
 * and they therefore also need to be adjusted.  In particular, values
Packit fb9d21
 * smaller than pos do not need to change, while values greater than or
Packit fb9d21
 * equal to pos need to be incremented.
Packit fb9d21
 * That is, we need to apply the following map.
Packit fb9d21
 *
Packit fb9d21
 *	{ atomic[i] -> atomic[i] : i < pos; [i] -> [i + 1] : i >= pos;
Packit fb9d21
 *	  unroll[i] -> unroll[i] : i < pos; [i] -> [i + 1] : i >= pos;
Packit fb9d21
 *	  separate[i] -> separate[i] : i < pos; [i] -> [i + 1] : i >= pos;
Packit fb9d21
 *	  separation_class[[i] -> [c]]
Packit fb9d21
 *		-> separation_class[[i] -> [c]] : i < pos;
Packit fb9d21
 *	  separation_class[[i] -> [c]]
Packit fb9d21
 *		-> separation_class[[i + 1] -> [c]] : i >= pos }
Packit fb9d21
 */
Packit fb9d21
static __isl_give isl_union_map *options_insert_dim(
Packit fb9d21
	__isl_take isl_union_map *options, __isl_take isl_space *space, int pos)
Packit fb9d21
{
Packit fb9d21
	isl_map *map;
Packit fb9d21
	isl_union_map *insertion;
Packit fb9d21
	enum isl_ast_build_domain_type type;
Packit fb9d21
	const char *name = "separation_class";
Packit fb9d21
Packit fb9d21
	space = isl_space_map_from_set(space);
Packit fb9d21
	map = isl_map_identity(space);
Packit fb9d21
	map = isl_map_insert_dims(map, isl_dim_out, pos, 1);
Packit fb9d21
	options = isl_union_map_apply_domain(options,
Packit fb9d21
						isl_union_map_from_map(map));
Packit fb9d21
Packit fb9d21
	if (!options)
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	map = construct_insertion_map(isl_union_map_get_space(options), pos);
Packit fb9d21
Packit fb9d21
	insertion = isl_union_map_empty(isl_union_map_get_space(options));
Packit fb9d21
Packit fb9d21
	for (type = atomic; type <= separate; ++type) {
Packit fb9d21
		isl_map *map_type = isl_map_copy(map);
Packit fb9d21
		const char *name = option_str[type];
Packit fb9d21
		map_type = isl_map_set_tuple_name(map_type, isl_dim_in, name);
Packit fb9d21
		map_type = isl_map_set_tuple_name(map_type, isl_dim_out, name);
Packit fb9d21
		insertion = isl_union_map_add_map(insertion, map_type);
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	map = isl_map_product(map, isl_map_identity(isl_map_get_space(map)));
Packit fb9d21
	map = isl_map_set_tuple_name(map, isl_dim_in, name);
Packit fb9d21
	map = isl_map_set_tuple_name(map, isl_dim_out, name);
Packit fb9d21
	insertion = isl_union_map_add_map(insertion, map);
Packit fb9d21
Packit fb9d21
	options = isl_union_map_apply_range(options, insertion);
Packit fb9d21
Packit fb9d21
	return options;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Insert a single dimension in the schedule domain at position "pos".
Packit fb9d21
 * The new dimension is given an isl_id with the empty string as name.
Packit fb9d21
 *
Packit fb9d21
 * The main difficulty is updating build->options to reflect the
Packit fb9d21
 * extra dimension.  This is handled in options_insert_dim.
Packit fb9d21
 *
Packit fb9d21
 * Note that because of the dimension manipulations, the resulting
Packit fb9d21
 * schedule domain space will always be unnamed and unstructured.
Packit fb9d21
 * However, the original schedule domain space may be named and/or
Packit fb9d21
 * structured, so we have to take this possibility into account
Packit fb9d21
 * while performing the transformations.
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_ast_build *isl_ast_build_insert_dim(
Packit fb9d21
	__isl_take isl_ast_build *build, int pos)
Packit fb9d21
{
Packit fb9d21
	isl_ctx *ctx;
Packit fb9d21
	isl_space *space, *ma_space;
Packit fb9d21
	isl_id *id;
Packit fb9d21
	isl_multi_aff *ma;
Packit fb9d21
Packit fb9d21
	build = isl_ast_build_cow(build);
Packit fb9d21
	if (!build)
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	ctx = isl_ast_build_get_ctx(build);
Packit fb9d21
	id = isl_id_alloc(ctx, "", NULL);
Packit fb9d21
	space = isl_ast_build_get_space(build, 1);
Packit fb9d21
	build->iterators = isl_id_list_insert(build->iterators, pos, id);
Packit fb9d21
	build->domain = isl_set_insert_dims(build->domain,
Packit fb9d21
						isl_dim_set, pos, 1);
Packit fb9d21
	build->generated = isl_set_insert_dims(build->generated,
Packit fb9d21
						isl_dim_set, pos, 1);
Packit fb9d21
	build->pending = isl_set_insert_dims(build->pending,
Packit fb9d21
						isl_dim_set, pos, 1);
Packit fb9d21
	build->strides = isl_vec_insert_els(build->strides, pos, 1);
Packit fb9d21
	build->strides = isl_vec_set_element_si(build->strides, pos, 1);
Packit fb9d21
	ma_space = isl_space_params(isl_multi_aff_get_space(build->offsets));
Packit fb9d21
	ma_space = isl_space_set_from_params(ma_space);
Packit fb9d21
	ma_space = isl_space_add_dims(ma_space, isl_dim_set, 1);
Packit fb9d21
	ma_space = isl_space_map_from_set(ma_space);
Packit fb9d21
	ma = isl_multi_aff_zero(isl_space_copy(ma_space));
Packit fb9d21
	build->offsets = isl_multi_aff_splice(build->offsets, pos, pos, ma);
Packit fb9d21
	ma = isl_multi_aff_identity(ma_space);
Packit fb9d21
	build->values = isl_multi_aff_splice(build->values, pos, pos, ma);
Packit fb9d21
	build->options = options_insert_dim(build->options, space, pos);
Packit fb9d21
Packit fb9d21
	if (!build->iterators || !build->domain || !build->generated ||
Packit fb9d21
	    !build->pending || !build->values ||
Packit fb9d21
	    !build->strides || !build->offsets || !build->options)
Packit fb9d21
		return isl_ast_build_free(build);
Packit fb9d21
Packit fb9d21
	return build;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Scale down the current dimension by a factor of "m".
Packit fb9d21
 * "umap" is an isl_union_map that implements the scaling down.
Packit fb9d21
 * That is, it is of the form
Packit fb9d21
 *
Packit fb9d21
 *	{ [.... i ....] -> [.... i' ....] : i = m i' }
Packit fb9d21
 *
Packit fb9d21
 * This function is called right after the strides have been
Packit fb9d21
 * detected, but before any constraints on the current dimension
Packit fb9d21
 * have been included in build->domain.
Packit fb9d21
 * We therefore only need to update stride, offset and the options.
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_ast_build *isl_ast_build_scale_down(
Packit fb9d21
	__isl_take isl_ast_build *build, __isl_take isl_val *m,
Packit fb9d21
	__isl_take isl_union_map *umap)
Packit fb9d21
{
Packit fb9d21
	isl_aff *aff;
Packit fb9d21
	isl_val *v;
Packit fb9d21
	int depth;
Packit fb9d21
Packit fb9d21
	build = isl_ast_build_cow(build);
Packit fb9d21
	if (!build || !umap || !m)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	depth = build->depth;
Packit fb9d21
Packit fb9d21
	v = isl_vec_get_element_val(build->strides, depth);
Packit fb9d21
	v = isl_val_div(v, isl_val_copy(m));
Packit fb9d21
	build->strides = isl_vec_set_element_val(build->strides, depth, v);
Packit fb9d21
Packit fb9d21
	aff = isl_multi_aff_get_aff(build->offsets, depth);
Packit fb9d21
	aff = isl_aff_scale_down_val(aff, m);
Packit fb9d21
	build->offsets = isl_multi_aff_set_aff(build->offsets, depth, aff);
Packit fb9d21
	build->options = isl_union_map_apply_domain(build->options, umap);
Packit fb9d21
	if (!build->strides || !build->offsets || !build->options)
Packit fb9d21
		return isl_ast_build_free(build);
Packit fb9d21
Packit fb9d21
	return build;
Packit fb9d21
error:
Packit fb9d21
	isl_val_free(m);
Packit fb9d21
	isl_union_map_free(umap);
Packit fb9d21
	return isl_ast_build_free(build);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Return a list of "n" isl_ids called "c%d", with "%d" starting at "first".
Packit fb9d21
 * If an isl_id with such a name already appears among the parameters
Packit fb9d21
 * in build->domain, then adjust the name to "c%d_%d".
Packit fb9d21
 */
Packit fb9d21
static __isl_give isl_id_list *generate_names(isl_ctx *ctx, int n, int first,
Packit fb9d21
	__isl_keep isl_ast_build *build)
Packit fb9d21
{
Packit fb9d21
	int i;
Packit fb9d21
	isl_id_list *names;
Packit fb9d21
Packit fb9d21
	names = isl_id_list_alloc(ctx, n);
Packit fb9d21
	for (i = 0; i < n; ++i) {
Packit fb9d21
		isl_id *id;
Packit fb9d21
Packit fb9d21
		id = generate_name(ctx, first + i, build);
Packit fb9d21
		names = isl_id_list_add(names, id);
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	return names;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Embed "options" into the given isl_ast_build space.
Packit fb9d21
 *
Packit fb9d21
 * This function is called from within a nested call to
Packit fb9d21
 * isl_ast_build_ast_from_schedule.
Packit fb9d21
 * "options" refers to the additional schedule,
Packit fb9d21
 * while space refers to both the space of the outer isl_ast_build and
Packit fb9d21
 * that of the additional schedule.
Packit fb9d21
 * Specifically, space is of the form
Packit fb9d21
 *
Packit fb9d21
 *	[I -> S]
Packit fb9d21
 *
Packit fb9d21
 * while options lives in the space(s)
Packit fb9d21
 *
Packit fb9d21
 *	S -> *
Packit fb9d21
 *
Packit fb9d21
 * We compute
Packit fb9d21
 *
Packit fb9d21
 *	[I -> S] -> S
Packit fb9d21
 *
Packit fb9d21
 * and compose this with options, to obtain the new options
Packit fb9d21
 * living in the space(s)
Packit fb9d21
 *
Packit fb9d21
 *	[I -> S] -> *
Packit fb9d21
 */
Packit fb9d21
static __isl_give isl_union_map *embed_options(
Packit fb9d21
	__isl_take isl_union_map *options, __isl_take isl_space *space)
Packit fb9d21
{
Packit fb9d21
	isl_map *map;
Packit fb9d21
Packit fb9d21
	map = isl_map_universe(isl_space_unwrap(space));
Packit fb9d21
	map = isl_map_range_map(map);
Packit fb9d21
Packit fb9d21
	options = isl_union_map_apply_range(
Packit fb9d21
				isl_union_map_from_map(map), options);
Packit fb9d21
Packit fb9d21
	return options;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Update "build" for use in a (possibly nested) code generation.  That is,
Packit fb9d21
 * extend "build" from an AST build on some domain O to an AST build
Packit fb9d21
 * on domain [O -> S], with S corresponding to "space".
Packit fb9d21
 * If the original domain is a parameter domain, then the new domain is
Packit fb9d21
 * simply S.
Packit fb9d21
 * "iterators" is a list of iterators for S, but the number of elements
Packit fb9d21
 * may be smaller or greater than the number of set dimensions of S.
Packit fb9d21
 * If "keep_iterators" is set, then any extra ids in build->iterators
Packit fb9d21
 * are reused for S.  Otherwise, these extra ids are dropped.
Packit fb9d21
 *
Packit fb9d21
 * We first update build->outer_pos to the current depth.
Packit fb9d21
 * This depth is zero in case this is the outermost code generation.
Packit fb9d21
 *
Packit fb9d21
 * We then add additional ids such that the number of iterators is at least
Packit fb9d21
 * equal to the dimension of the new build domain.
Packit fb9d21
 *
Packit fb9d21
 * If the original domain is parametric, then we are constructing
Packit fb9d21
 * an isl_ast_build for the outer code generation and we pass control
Packit fb9d21
 * to isl_ast_build_init.
Packit fb9d21
 *
Packit fb9d21
 * Otherwise, we adjust the fields of "build" to include "space".
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_ast_build *isl_ast_build_product(
Packit fb9d21
	__isl_take isl_ast_build *build, __isl_take isl_space *space)
Packit fb9d21
{
Packit fb9d21
	isl_ctx *ctx;
Packit fb9d21
	isl_vec *strides;
Packit fb9d21
	isl_set *set;
Packit fb9d21
	isl_multi_aff *embedding;
Packit fb9d21
	int dim, n_it;
Packit fb9d21
Packit fb9d21
	build = isl_ast_build_cow(build);
Packit fb9d21
	if (!build)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	build->outer_pos = build->depth;
Packit fb9d21
Packit fb9d21
	ctx = isl_ast_build_get_ctx(build);
Packit fb9d21
	dim = isl_set_dim(build->domain, isl_dim_set);
Packit fb9d21
	dim += isl_space_dim(space, isl_dim_set);
Packit fb9d21
	n_it = isl_id_list_n_id(build->iterators);
Packit fb9d21
	if (n_it < dim) {
Packit fb9d21
		isl_id_list *l;
Packit fb9d21
		l = generate_names(ctx, dim - n_it, n_it, build);
Packit fb9d21
		build->iterators = isl_id_list_concat(build->iterators, l);
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	if (isl_set_is_params(build->domain))
Packit fb9d21
		return isl_ast_build_init(build, space);
Packit fb9d21
Packit fb9d21
	set = isl_set_universe(isl_space_copy(space));
Packit fb9d21
	build->domain = isl_set_product(build->domain, isl_set_copy(set));
Packit fb9d21
	build->pending = isl_set_product(build->pending, isl_set_copy(set));
Packit fb9d21
	build->generated = isl_set_product(build->generated, set);
Packit fb9d21
Packit fb9d21
	strides = isl_vec_alloc(ctx, isl_space_dim(space, isl_dim_set));
Packit fb9d21
	strides = isl_vec_set_si(strides, 1);
Packit fb9d21
	build->strides = isl_vec_concat(build->strides, strides);
Packit fb9d21
Packit fb9d21
	space = isl_space_map_from_set(space);
Packit fb9d21
	build->offsets = isl_multi_aff_align_params(build->offsets,
Packit fb9d21
						    isl_space_copy(space));
Packit fb9d21
	build->offsets = isl_multi_aff_product(build->offsets,
Packit fb9d21
				isl_multi_aff_zero(isl_space_copy(space)));
Packit fb9d21
	build->values = isl_multi_aff_align_params(build->values,
Packit fb9d21
						    isl_space_copy(space));
Packit fb9d21
	embedding = isl_multi_aff_identity(space);
Packit fb9d21
	build->values = isl_multi_aff_product(build->values, embedding);
Packit fb9d21
Packit fb9d21
	space = isl_ast_build_get_space(build, 1);
Packit fb9d21
	build->options = embed_options(build->options, space);
Packit fb9d21
Packit fb9d21
	if (!build->iterators || !build->domain || !build->generated ||
Packit fb9d21
	    !build->pending || !build->values ||
Packit fb9d21
	    !build->strides || !build->offsets || !build->options)
Packit fb9d21
		return isl_ast_build_free(build);
Packit fb9d21
Packit fb9d21
	return build;
Packit fb9d21
error:
Packit fb9d21
	isl_ast_build_free(build);
Packit fb9d21
	isl_space_free(space);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Does "aff" only attain non-negative values over build->domain?
Packit fb9d21
 * That is, does it not attain any negative values?
Packit fb9d21
 */
Packit fb9d21
int isl_ast_build_aff_is_nonneg(__isl_keep isl_ast_build *build,
Packit fb9d21
	__isl_keep isl_aff *aff)
Packit fb9d21
{
Packit fb9d21
	isl_set *test;
Packit fb9d21
	int empty;
Packit fb9d21
Packit fb9d21
	if (!build)
Packit fb9d21
		return -1;
Packit fb9d21
Packit fb9d21
	aff = isl_aff_copy(aff);
Packit fb9d21
	test = isl_set_from_basic_set(isl_aff_neg_basic_set(aff));
Packit fb9d21
	test = isl_set_intersect(test, isl_set_copy(build->domain));
Packit fb9d21
	empty = isl_set_is_empty(test);
Packit fb9d21
	isl_set_free(test);
Packit fb9d21
Packit fb9d21
	return empty;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Does the dimension at (internal) position "pos" have a non-trivial stride?
Packit fb9d21
 */
Packit fb9d21
int isl_ast_build_has_stride(__isl_keep isl_ast_build *build, int pos)
Packit fb9d21
{
Packit fb9d21
	isl_val *v;
Packit fb9d21
	int has_stride;
Packit fb9d21
Packit fb9d21
	if (!build)
Packit fb9d21
		return -1;
Packit fb9d21
Packit fb9d21
	v = isl_vec_get_element_val(build->strides, pos);
Packit fb9d21
	if (!v)
Packit fb9d21
		return -1;
Packit fb9d21
	has_stride = !isl_val_is_one(v);
Packit fb9d21
	isl_val_free(v);
Packit fb9d21
Packit fb9d21
	return has_stride;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Given that the dimension at position "pos" takes on values
Packit fb9d21
 *
Packit fb9d21
 *	f + s a
Packit fb9d21
 *
Packit fb9d21
 * with a an integer, return s through *stride.
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_val *isl_ast_build_get_stride(__isl_keep isl_ast_build *build,
Packit fb9d21
	int pos)
Packit fb9d21
{
Packit fb9d21
	if (!build)
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	return isl_vec_get_element_val(build->strides, pos);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Given that the dimension at position "pos" takes on values
Packit fb9d21
 *
Packit fb9d21
 *	f + s a
Packit fb9d21
 *
Packit fb9d21
 * with a an integer, return f.
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_aff *isl_ast_build_get_offset(
Packit fb9d21
	__isl_keep isl_ast_build *build, int pos)
Packit fb9d21
{
Packit fb9d21
	if (!build)
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	return isl_multi_aff_get_aff(build->offsets, pos);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Is the dimension at position "pos" known to attain only a single
Packit fb9d21
 * value that, moreover, can be described by a single affine expression
Packit fb9d21
 * in terms of the outer dimensions and parameters?
Packit fb9d21
 *
Packit fb9d21
 * If not, then the correponding affine expression in build->values
Packit fb9d21
 * is set to be equal to the same input dimension.
Packit fb9d21
 * Otherwise, it is set to the requested expression in terms of
Packit fb9d21
 * outer dimensions and parameters.
Packit fb9d21
 */
Packit fb9d21
int isl_ast_build_has_affine_value(__isl_keep isl_ast_build *build,
Packit fb9d21
	int pos)
Packit fb9d21
{
Packit fb9d21
	isl_aff *aff;
Packit fb9d21
	int involves;
Packit fb9d21
Packit fb9d21
	if (!build)
Packit fb9d21
		return -1;
Packit fb9d21
Packit fb9d21
	aff = isl_multi_aff_get_aff(build->values, pos);
Packit fb9d21
	involves = isl_aff_involves_dims(aff, isl_dim_in, pos, 1);
Packit fb9d21
	isl_aff_free(aff);
Packit fb9d21
Packit fb9d21
	if (involves < 0)
Packit fb9d21
		return -1;
Packit fb9d21
Packit fb9d21
	return !involves;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Plug in the known values (fixed affine expressions in terms of
Packit fb9d21
 * parameters and outer loop iterators) of all loop iterators
Packit fb9d21
 * in the domain of "umap".
Packit fb9d21
 *
Packit fb9d21
 * We simply precompose "umap" with build->values.
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_union_map *isl_ast_build_substitute_values_union_map_domain(
Packit fb9d21
	__isl_keep isl_ast_build *build, __isl_take isl_union_map *umap)
Packit fb9d21
{
Packit fb9d21
	isl_multi_aff *values;
Packit fb9d21
Packit fb9d21
	if (!build)
Packit fb9d21
		return isl_union_map_free(umap);
Packit fb9d21
Packit fb9d21
	values = isl_multi_aff_copy(build->values);
Packit fb9d21
	umap = isl_union_map_preimage_domain_multi_aff(umap, values);
Packit fb9d21
Packit fb9d21
	return umap;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Is the current dimension known to attain only a single value?
Packit fb9d21
 */
Packit fb9d21
int isl_ast_build_has_value(__isl_keep isl_ast_build *build)
Packit fb9d21
{
Packit fb9d21
	if (!build)
Packit fb9d21
		return -1;
Packit fb9d21
Packit fb9d21
	return build->value != NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Simplify the basic set "bset" based on what we know about
Packit fb9d21
 * the iterators of already generated loops.
Packit fb9d21
 *
Packit fb9d21
 * "bset" is assumed to live in the (internal) schedule domain.
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_basic_set *isl_ast_build_compute_gist_basic_set(
Packit fb9d21
	__isl_keep isl_ast_build *build, __isl_take isl_basic_set *bset)
Packit fb9d21
{
Packit fb9d21
	if (!build)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	bset = isl_basic_set_preimage_multi_aff(bset,
Packit fb9d21
					isl_multi_aff_copy(build->values));
Packit fb9d21
	bset = isl_basic_set_gist(bset,
Packit fb9d21
			isl_set_simple_hull(isl_set_copy(build->domain)));
Packit fb9d21
Packit fb9d21
	return bset;
Packit fb9d21
error:
Packit fb9d21
	isl_basic_set_free(bset);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Simplify the set "set" based on what we know about
Packit fb9d21
 * the iterators of already generated loops.
Packit fb9d21
 *
Packit fb9d21
 * "set" is assumed to live in the (internal) schedule domain.
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_set *isl_ast_build_compute_gist(
Packit fb9d21
	__isl_keep isl_ast_build *build, __isl_take isl_set *set)
Packit fb9d21
{
Packit fb9d21
	if (!build)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	set = isl_set_preimage_multi_aff(set,
Packit fb9d21
					isl_multi_aff_copy(build->values));
Packit fb9d21
	set = isl_set_gist(set, isl_set_copy(build->domain));
Packit fb9d21
Packit fb9d21
	return set;
Packit fb9d21
error:
Packit fb9d21
	isl_set_free(set);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Include information about what we know about the iterators of
Packit fb9d21
 * already generated loops to "set".
Packit fb9d21
 *
Packit fb9d21
 * We currently only plug in the known affine values of outer loop
Packit fb9d21
 * iterators.
Packit fb9d21
 * In principle we could also introduce equalities or even other
Packit fb9d21
 * constraints implied by the intersection of "set" and build->domain.
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_set *isl_ast_build_specialize(__isl_keep isl_ast_build *build,
Packit fb9d21
	__isl_take isl_set *set)
Packit fb9d21
{
Packit fb9d21
	if (!build)
Packit fb9d21
		return isl_set_free(set);
Packit fb9d21
Packit fb9d21
	return isl_set_preimage_multi_aff(set,
Packit fb9d21
					isl_multi_aff_copy(build->values));
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Simplify the map "map" based on what we know about
Packit fb9d21
 * the iterators of already generated loops.
Packit fb9d21
 *
Packit fb9d21
 * The domain of "map" is assumed to live in the (internal) schedule domain.
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_map *isl_ast_build_compute_gist_map_domain(
Packit fb9d21
	__isl_keep isl_ast_build *build, __isl_take isl_map *map)
Packit fb9d21
{
Packit fb9d21
	if (!build)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	map = isl_map_gist_domain(map, isl_set_copy(build->domain));
Packit fb9d21
Packit fb9d21
	return map;
Packit fb9d21
error:
Packit fb9d21
	isl_map_free(map);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Simplify the affine expression "aff" based on what we know about
Packit fb9d21
 * the iterators of already generated loops.
Packit fb9d21
 *
Packit fb9d21
 * The domain of "aff" is assumed to live in the (internal) schedule domain.
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_aff *isl_ast_build_compute_gist_aff(
Packit fb9d21
	__isl_keep isl_ast_build *build, __isl_take isl_aff *aff)
Packit fb9d21
{
Packit fb9d21
	if (!build)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	aff = isl_aff_gist(aff, isl_set_copy(build->domain));
Packit fb9d21
Packit fb9d21
	return aff;
Packit fb9d21
error:
Packit fb9d21
	isl_aff_free(aff);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Simplify the piecewise affine expression "aff" based on what we know about
Packit fb9d21
 * the iterators of already generated loops.
Packit fb9d21
 *
Packit fb9d21
 * The domain of "pa" is assumed to live in the (internal) schedule domain.
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_pw_aff *isl_ast_build_compute_gist_pw_aff(
Packit fb9d21
	__isl_keep isl_ast_build *build, __isl_take isl_pw_aff *pa)
Packit fb9d21
{
Packit fb9d21
	if (!build)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	if (!isl_set_is_params(build->domain))
Packit fb9d21
		pa = isl_pw_aff_pullback_multi_aff(pa,
Packit fb9d21
					isl_multi_aff_copy(build->values));
Packit fb9d21
	pa = isl_pw_aff_gist(pa, isl_set_copy(build->domain));
Packit fb9d21
Packit fb9d21
	return pa;
Packit fb9d21
error:
Packit fb9d21
	isl_pw_aff_free(pa);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Simplify the piecewise multi-affine expression "aff" based on what
Packit fb9d21
 * we know about the iterators of already generated loops.
Packit fb9d21
 *
Packit fb9d21
 * The domain of "pma" is assumed to live in the (internal) schedule domain.
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_pw_multi_aff *isl_ast_build_compute_gist_pw_multi_aff(
Packit fb9d21
	__isl_keep isl_ast_build *build, __isl_take isl_pw_multi_aff *pma)
Packit fb9d21
{
Packit fb9d21
	if (!build)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	pma = isl_pw_multi_aff_pullback_multi_aff(pma,
Packit fb9d21
					isl_multi_aff_copy(build->values));
Packit fb9d21
	pma = isl_pw_multi_aff_gist(pma, isl_set_copy(build->domain));
Packit fb9d21
Packit fb9d21
	return pma;
Packit fb9d21
error:
Packit fb9d21
	isl_pw_multi_aff_free(pma);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Extract the schedule domain of the given type from build->options
Packit fb9d21
 * at the current depth.
Packit fb9d21
 *
Packit fb9d21
 * In particular, find the subset of build->options that is of
Packit fb9d21
 * the following form
Packit fb9d21
 *
Packit fb9d21
 *	schedule_domain -> type[depth]
Packit fb9d21
 *
Packit fb9d21
 * and return the corresponding domain, after eliminating inner dimensions
Packit fb9d21
 * and divs that depend on the current dimension.
Packit fb9d21
 *
Packit fb9d21
 * Note that the domain of build->options has been reformulated
Packit fb9d21
 * in terms of the internal build space in embed_options,
Packit fb9d21
 * but the position is still that within the current code generation.
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_set *isl_ast_build_get_option_domain(
Packit fb9d21
	__isl_keep isl_ast_build *build,
Packit fb9d21
	enum isl_ast_build_domain_type type)
Packit fb9d21
{
Packit fb9d21
	const char *name;
Packit fb9d21
	isl_space *space;
Packit fb9d21
	isl_map *option;
Packit fb9d21
	isl_set *domain;
Packit fb9d21
	int local_pos;
Packit fb9d21
Packit fb9d21
	if (!build)
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	name = option_str[type];
Packit fb9d21
	local_pos = build->depth - build->outer_pos;
Packit fb9d21
Packit fb9d21
	space = isl_ast_build_get_space(build, 1);
Packit fb9d21
	space = isl_space_from_domain(space);
Packit fb9d21
	space = isl_space_add_dims(space, isl_dim_out, 1);
Packit fb9d21
	space = isl_space_set_tuple_name(space, isl_dim_out, name);
Packit fb9d21
Packit fb9d21
	option = isl_union_map_extract_map(build->options, space);
Packit fb9d21
	option = isl_map_fix_si(option, isl_dim_out, 0, local_pos);
Packit fb9d21
Packit fb9d21
	domain = isl_map_domain(option);
Packit fb9d21
	domain = isl_ast_build_eliminate(build, domain);
Packit fb9d21
Packit fb9d21
	return domain;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Extract the separation class mapping at the current depth.
Packit fb9d21
 *
Packit fb9d21
 * In particular, find and return the subset of build->options that is of
Packit fb9d21
 * the following form
Packit fb9d21
 *
Packit fb9d21
 *	schedule_domain -> separation_class[[depth] -> [class]]
Packit fb9d21
 *
Packit fb9d21
 * The caller is expected to eliminate inner dimensions from the domain.
Packit fb9d21
 *
Packit fb9d21
 * Note that the domain of build->options has been reformulated
Packit fb9d21
 * in terms of the internal build space in embed_options,
Packit fb9d21
 * but the position is still that within the current code generation.
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_map *isl_ast_build_get_separation_class(
Packit fb9d21
	__isl_keep isl_ast_build *build)
Packit fb9d21
{
Packit fb9d21
	isl_ctx *ctx;
Packit fb9d21
	isl_space *space_sep, *space;
Packit fb9d21
	isl_map *res;
Packit fb9d21
	int local_pos;
Packit fb9d21
Packit fb9d21
	if (!build)
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	local_pos = build->depth - build->outer_pos;
Packit fb9d21
	ctx = isl_ast_build_get_ctx(build);
Packit fb9d21
	space_sep = isl_space_alloc(ctx, 0, 1, 1);
Packit fb9d21
	space_sep = isl_space_wrap(space_sep);
Packit fb9d21
	space_sep = isl_space_set_tuple_name(space_sep, isl_dim_set,
Packit fb9d21
						"separation_class");
Packit fb9d21
	space = isl_ast_build_get_space(build, 1);
Packit fb9d21
	space_sep = isl_space_align_params(space_sep, isl_space_copy(space));
Packit fb9d21
	space = isl_space_map_from_domain_and_range(space, space_sep);
Packit fb9d21
Packit fb9d21
	res = isl_union_map_extract_map(build->options, space);
Packit fb9d21
	res = isl_map_fix_si(res, isl_dim_out, 0, local_pos);
Packit fb9d21
	res = isl_map_coalesce(res);
Packit fb9d21
Packit fb9d21
	return res;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Eliminate dimensions inner to the current dimension.
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_set *isl_ast_build_eliminate_inner(
Packit fb9d21
	__isl_keep isl_ast_build *build, __isl_take isl_set *set)
Packit fb9d21
{
Packit fb9d21
	int dim;
Packit fb9d21
	int depth;
Packit fb9d21
Packit fb9d21
	if (!build)
Packit fb9d21
		return isl_set_free(set);
Packit fb9d21
Packit fb9d21
	dim = isl_set_dim(set, isl_dim_set);
Packit fb9d21
	depth = build->depth;
Packit fb9d21
	set = isl_set_detect_equalities(set);
Packit fb9d21
	set = isl_set_eliminate(set, isl_dim_set, depth + 1, dim - (depth + 1));
Packit fb9d21
Packit fb9d21
	return set;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Eliminate unknown divs and divs that depend on the current dimension.
Packit fb9d21
 *
Packit fb9d21
 * Note that during the elimination of unknown divs, we may discover
Packit fb9d21
 * an explicit representation of some other unknown divs, which may
Packit fb9d21
 * depend on the current dimension.  We therefore need to eliminate
Packit fb9d21
 * unknown divs first.
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_set *isl_ast_build_eliminate_divs(
Packit fb9d21
	__isl_keep isl_ast_build *build, __isl_take isl_set *set)
Packit fb9d21
{
Packit fb9d21
	int depth;
Packit fb9d21
Packit fb9d21
	if (!build)
Packit fb9d21
		return isl_set_free(set);
Packit fb9d21
Packit fb9d21
	set = isl_set_remove_unknown_divs(set);
Packit fb9d21
	depth = build->depth;
Packit fb9d21
	set = isl_set_remove_divs_involving_dims(set, isl_dim_set, depth, 1);
Packit fb9d21
Packit fb9d21
	return set;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Eliminate dimensions inner to the current dimension as well as
Packit fb9d21
 * unknown divs and divs that depend on the current dimension.
Packit fb9d21
 * The result then consists only of constraints that are independent
Packit fb9d21
 * of the current dimension and upper and lower bounds on the current
Packit fb9d21
 * dimension.
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_set *isl_ast_build_eliminate(
Packit fb9d21
	__isl_keep isl_ast_build *build, __isl_take isl_set *domain)
Packit fb9d21
{
Packit fb9d21
	domain = isl_ast_build_eliminate_inner(build, domain);
Packit fb9d21
	domain = isl_ast_build_eliminate_divs(build, domain);
Packit fb9d21
	return domain;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Replace build->single_valued by "sv".
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_ast_build *isl_ast_build_set_single_valued(
Packit fb9d21
	__isl_take isl_ast_build *build, int sv)
Packit fb9d21
{
Packit fb9d21
	if (!build)
Packit fb9d21
		return build;
Packit fb9d21
	if (build->single_valued == sv)
Packit fb9d21
		return build;
Packit fb9d21
	build = isl_ast_build_cow(build);
Packit fb9d21
	if (!build)
Packit fb9d21
		return build;
Packit fb9d21
	build->single_valued = sv;
Packit fb9d21
Packit fb9d21
	return build;
Packit fb9d21
}