Blame isl-0.14/isl_ast_graft.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_ast_private.h>
Packit fb9d21
#include <isl_ast_build_expr.h>
Packit fb9d21
#include <isl_ast_build_private.h>
Packit fb9d21
#include <isl_ast_graft_private.h>
Packit fb9d21
Packit fb9d21
static __isl_give isl_ast_graft *isl_ast_graft_copy(
Packit fb9d21
	__isl_keep isl_ast_graft *graft);
Packit fb9d21
Packit fb9d21
#undef BASE
Packit fb9d21
#define BASE ast_graft
Packit fb9d21
Packit fb9d21
#include <isl_list_templ.c>
Packit fb9d21
Packit fb9d21
#undef BASE
Packit fb9d21
#define BASE ast_graft
Packit fb9d21
#include <print_templ.c>
Packit fb9d21
Packit fb9d21
isl_ctx *isl_ast_graft_get_ctx(__isl_keep isl_ast_graft *graft)
Packit fb9d21
{
Packit fb9d21
	if (!graft)
Packit fb9d21
		return NULL;
Packit fb9d21
	return isl_basic_set_get_ctx(graft->enforced);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
__isl_give isl_ast_node *isl_ast_graft_get_node(
Packit fb9d21
	__isl_keep isl_ast_graft *graft)
Packit fb9d21
{
Packit fb9d21
	return graft ? isl_ast_node_copy(graft->node) : NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Create a graft for "node" with no guards and no enforced conditions.
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_ast_graft *isl_ast_graft_alloc(
Packit fb9d21
	__isl_take isl_ast_node *node, __isl_keep isl_ast_build *build)
Packit fb9d21
{
Packit fb9d21
	isl_ctx *ctx;
Packit fb9d21
	isl_space *space;
Packit fb9d21
	isl_ast_graft *graft;
Packit fb9d21
Packit fb9d21
	if (!node)
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	ctx = isl_ast_node_get_ctx(node);
Packit fb9d21
	graft = isl_calloc_type(ctx, isl_ast_graft);
Packit fb9d21
	if (!graft)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	space = isl_ast_build_get_space(build, 1);
Packit fb9d21
Packit fb9d21
	graft->ref = 1;
Packit fb9d21
	graft->node = node;
Packit fb9d21
	graft->guard = isl_set_universe(isl_space_copy(space));
Packit fb9d21
	graft->enforced = isl_basic_set_universe(space);
Packit fb9d21
Packit fb9d21
	if (!graft->guard || !graft->enforced)
Packit fb9d21
		return isl_ast_graft_free(graft);
Packit fb9d21
Packit fb9d21
	return graft;
Packit fb9d21
error:
Packit fb9d21
	isl_ast_node_free(node);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Create a graft with no guards and no enforced conditions
Packit fb9d21
 * encapsulating a call to the domain element specified by "executed".
Packit fb9d21
 * "executed" is assumed to be single-valued.
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_ast_graft *isl_ast_graft_alloc_domain(
Packit fb9d21
	__isl_take isl_map *executed, __isl_keep isl_ast_build *build)
Packit fb9d21
{
Packit fb9d21
	isl_ast_node *node;
Packit fb9d21
Packit fb9d21
	node = isl_ast_build_call_from_executed(build, executed);
Packit fb9d21
Packit fb9d21
	return isl_ast_graft_alloc(node, build);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
static __isl_give isl_ast_graft *isl_ast_graft_copy(
Packit fb9d21
	__isl_keep isl_ast_graft *graft)
Packit fb9d21
{
Packit fb9d21
	if (!graft)
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	graft->ref++;
Packit fb9d21
	return graft;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Do all the grafts in "list" have the same guard and is this guard
Packit fb9d21
 * independent of the current depth?
Packit fb9d21
 */
Packit fb9d21
static int equal_independent_guards(__isl_keep isl_ast_graft_list *list,
Packit fb9d21
	__isl_keep isl_ast_build *build)
Packit fb9d21
{
Packit fb9d21
	int i, n;
Packit fb9d21
	int depth;
Packit fb9d21
	isl_ast_graft *graft_0;
Packit fb9d21
	int equal = 1;
Packit fb9d21
	int skip;
Packit fb9d21
Packit fb9d21
	graft_0 = isl_ast_graft_list_get_ast_graft(list, 0);
Packit fb9d21
	if (!graft_0)
Packit fb9d21
		return -1;
Packit fb9d21
Packit fb9d21
	depth = isl_ast_build_get_depth(build);
Packit fb9d21
	skip = isl_set_involves_dims(graft_0->guard, isl_dim_set, depth, 1);
Packit fb9d21
	if (skip < 0 || skip) {
Packit fb9d21
		isl_ast_graft_free(graft_0);
Packit fb9d21
		return skip < 0 ? -1 : 0;
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	n = isl_ast_graft_list_n_ast_graft(list);
Packit fb9d21
	for (i = 1; i < n; ++i) {
Packit fb9d21
		isl_ast_graft *graft;
Packit fb9d21
		graft = isl_ast_graft_list_get_ast_graft(list, i);
Packit fb9d21
		if (!graft)
Packit fb9d21
			equal = -1;
Packit fb9d21
		else
Packit fb9d21
			equal = isl_set_is_equal(graft_0->guard, graft->guard);
Packit fb9d21
		isl_ast_graft_free(graft);
Packit fb9d21
		if (equal < 0 || !equal)
Packit fb9d21
			break;
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	isl_ast_graft_free(graft_0);
Packit fb9d21
Packit fb9d21
	return equal;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Hoist "guard" out of the current level (given by "build").
Packit fb9d21
 *
Packit fb9d21
 * In particular, eliminate the dimension corresponding to the current depth.
Packit fb9d21
 */
Packit fb9d21
static __isl_give isl_set *hoist_guard(__isl_take isl_set *guard,
Packit fb9d21
	__isl_keep isl_ast_build *build)
Packit fb9d21
{
Packit fb9d21
	int depth;
Packit fb9d21
Packit fb9d21
	depth = isl_ast_build_get_depth(build);
Packit fb9d21
	if (depth < isl_set_dim(guard, isl_dim_set)) {
Packit fb9d21
		guard = isl_set_remove_divs_involving_dims(guard,
Packit fb9d21
						isl_dim_set, depth, 1);
Packit fb9d21
		guard = isl_set_eliminate(guard, isl_dim_set, depth, 1);
Packit fb9d21
		guard = isl_set_compute_divs(guard);
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	return guard;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Extract a common guard from the grafts in "list" that can be hoisted
Packit fb9d21
 * out of the current level.  If no such guard can be found, then return
Packit fb9d21
 * a universal set.
Packit fb9d21
 *
Packit fb9d21
 * If all the grafts in the list have the same guard and if this guard
Packit fb9d21
 * is independent of the current level, then it can be hoisted out.
Packit fb9d21
 * If there is only one graft in the list and if its guard
Packit fb9d21
 * depends on the current level, then we eliminate this level and
Packit fb9d21
 * return the result.
Packit fb9d21
 *
Packit fb9d21
 * Otherwise, we return the unshifted simple hull of the guards.
Packit fb9d21
 * In order to be able to hoist as many constraints as possible,
Packit fb9d21
 * but at the same time avoid hoisting constraints that did not
Packit fb9d21
 * appear in the guards in the first place, we intersect the guards
Packit fb9d21
 * with all the information that is available (i.e., the domain
Packit fb9d21
 * from the build and the enforced constraints of the graft) and
Packit fb9d21
 * compute the unshifted hull of the result using only constraints
Packit fb9d21
 * from the original guards.
Packit fb9d21
 * In particular, intersecting the guards with other known information
Packit fb9d21
 * allows us to hoist guards that are only explicit is some of
Packit fb9d21
 * the grafts and implicit in the others.
Packit fb9d21
 *
Packit fb9d21
 * The special case for equal guards is needed in case those guards
Packit fb9d21
 * are non-convex.  Taking the simple hull would remove information
Packit fb9d21
 * and would not allow for these guards to be hoisted completely.
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_set *isl_ast_graft_list_extract_hoistable_guard(
Packit fb9d21
	__isl_keep isl_ast_graft_list *list, __isl_keep isl_ast_build *build)
Packit fb9d21
{
Packit fb9d21
	int i, n;
Packit fb9d21
	int equal;
Packit fb9d21
	isl_ctx *ctx;
Packit fb9d21
	isl_set *guard;
Packit fb9d21
	isl_set_list *set_list;
Packit fb9d21
	isl_basic_set *hull;
Packit fb9d21
Packit fb9d21
	if (!list || !build)
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	n = isl_ast_graft_list_n_ast_graft(list);
Packit fb9d21
	if (n == 0)
Packit fb9d21
		return isl_set_universe(isl_ast_build_get_space(build, 1));
Packit fb9d21
Packit fb9d21
	equal = equal_independent_guards(list, build);
Packit fb9d21
	if (equal < 0)
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	if (equal || n == 1) {
Packit fb9d21
		isl_ast_graft *graft_0;
Packit fb9d21
Packit fb9d21
		graft_0 = isl_ast_graft_list_get_ast_graft(list, 0);
Packit fb9d21
		if (!graft_0)
Packit fb9d21
			return NULL;
Packit fb9d21
		guard = isl_set_copy(graft_0->guard);
Packit fb9d21
		if (!equal)
Packit fb9d21
			guard = hoist_guard(guard, build);
Packit fb9d21
		isl_ast_graft_free(graft_0);
Packit fb9d21
		return guard;
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	ctx = isl_ast_build_get_ctx(build);
Packit fb9d21
	set_list = isl_set_list_alloc(ctx, n);
Packit fb9d21
	guard = isl_set_empty(isl_ast_build_get_space(build, 1));
Packit fb9d21
	for (i = 0; i < n; ++i) {
Packit fb9d21
		isl_ast_graft *graft;
Packit fb9d21
		isl_basic_set *enforced;
Packit fb9d21
		isl_set *guard_i;
Packit fb9d21
Packit fb9d21
		graft = isl_ast_graft_list_get_ast_graft(list, i);
Packit fb9d21
		enforced = isl_ast_graft_get_enforced(graft);
Packit fb9d21
		guard_i = isl_set_copy(graft->guard);
Packit fb9d21
		isl_ast_graft_free(graft);
Packit fb9d21
		set_list = isl_set_list_add(set_list, isl_set_copy(guard_i));
Packit fb9d21
		guard_i = isl_set_intersect(guard_i,
Packit fb9d21
					    isl_set_from_basic_set(enforced));
Packit fb9d21
		guard_i = isl_set_intersect(guard_i,
Packit fb9d21
					    isl_ast_build_get_domain(build));
Packit fb9d21
		guard = isl_set_union(guard, guard_i);
Packit fb9d21
	}
Packit fb9d21
	hull = isl_set_unshifted_simple_hull_from_set_list(guard, set_list);
Packit fb9d21
	guard = isl_set_from_basic_set(hull);
Packit fb9d21
	return hoist_guard(guard, build);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Internal data structure used inside insert_if.
Packit fb9d21
 *
Packit fb9d21
 * list is the list of guarded nodes created by each call to insert_if.
Packit fb9d21
 * node is the original node that is guarded by insert_if.
Packit fb9d21
 * build is the build in which the AST is constructed.
Packit fb9d21
 */
Packit fb9d21
struct isl_insert_if_data {
Packit fb9d21
	isl_ast_node_list *list;
Packit fb9d21
	isl_ast_node *node;
Packit fb9d21
	isl_ast_build *build;
Packit fb9d21
};
Packit fb9d21
Packit fb9d21
static int insert_if(__isl_take isl_basic_set *bset, void *user);
Packit fb9d21
Packit fb9d21
/* Insert an if node around "node" testing the condition encoded
Packit fb9d21
 * in guard "guard".
Packit fb9d21
 *
Packit fb9d21
 * If the user does not want any disjunctions in the if conditions
Packit fb9d21
 * and if "guard" does involve a disjunction, then we make the different
Packit fb9d21
 * disjuncts disjoint and insert an if node corresponding to each disjunct
Packit fb9d21
 * around a copy of "node".  The result is then a block node containing
Packit fb9d21
 * this sequence of guarded copies of "node".
Packit fb9d21
 */
Packit fb9d21
static __isl_give isl_ast_node *ast_node_insert_if(
Packit fb9d21
	__isl_take isl_ast_node *node, __isl_take isl_set *guard,
Packit fb9d21
	__isl_keep isl_ast_build *build)
Packit fb9d21
{
Packit fb9d21
	struct isl_insert_if_data data;
Packit fb9d21
	isl_ctx *ctx;
Packit fb9d21
Packit fb9d21
	ctx = isl_ast_build_get_ctx(build);
Packit fb9d21
	if (isl_options_get_ast_build_allow_or(ctx) ||
Packit fb9d21
	    isl_set_n_basic_set(guard) <= 1) {
Packit fb9d21
		isl_ast_node *if_node;
Packit fb9d21
		isl_ast_expr *expr;
Packit fb9d21
Packit fb9d21
		expr = isl_ast_build_expr_from_set(build, guard);
Packit fb9d21
Packit fb9d21
		if_node = isl_ast_node_alloc_if(expr);
Packit fb9d21
		return isl_ast_node_if_set_then(if_node, node);
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	guard = isl_set_make_disjoint(guard);
Packit fb9d21
Packit fb9d21
	data.list = isl_ast_node_list_alloc(ctx, 0);
Packit fb9d21
	data.node = node;
Packit fb9d21
	data.build = build;
Packit fb9d21
	if (isl_set_foreach_basic_set(guard, &insert_if, &data) < 0)
Packit fb9d21
		data.list = isl_ast_node_list_free(data.list);
Packit fb9d21
Packit fb9d21
	isl_set_free(guard);
Packit fb9d21
	isl_ast_node_free(data.node);
Packit fb9d21
	return isl_ast_node_alloc_block(data.list);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Insert an if node around a copy of "data->node" testing the condition
Packit fb9d21
 * encoded in guard "bset" and add the result to data->list.
Packit fb9d21
 */
Packit fb9d21
static int insert_if(__isl_take isl_basic_set *bset, void *user)
Packit fb9d21
{
Packit fb9d21
	struct isl_insert_if_data *data = user;
Packit fb9d21
	isl_ast_node *node;
Packit fb9d21
	isl_set *set;
Packit fb9d21
Packit fb9d21
	set = isl_set_from_basic_set(bset);
Packit fb9d21
	node = isl_ast_node_copy(data->node);
Packit fb9d21
	node = ast_node_insert_if(node, set, data->build);
Packit fb9d21
	data->list = isl_ast_node_list_add(data->list, node);
Packit fb9d21
Packit fb9d21
	return 0;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Insert an if node around graft->node testing the condition encoded
Packit fb9d21
 * in guard "guard", assuming guard involves any conditions.
Packit fb9d21
 */
Packit fb9d21
static __isl_give isl_ast_graft *insert_if_node(
Packit fb9d21
	__isl_take isl_ast_graft *graft, __isl_take isl_set *guard,
Packit fb9d21
	__isl_keep isl_ast_build *build)
Packit fb9d21
{
Packit fb9d21
	int univ;
Packit fb9d21
Packit fb9d21
	if (!graft)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	univ = isl_set_plain_is_universe(guard);
Packit fb9d21
	if (univ < 0)
Packit fb9d21
		goto error;
Packit fb9d21
	if (univ) {
Packit fb9d21
		isl_set_free(guard);
Packit fb9d21
		return graft;
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	build = isl_ast_build_copy(build);
Packit fb9d21
	graft->node = ast_node_insert_if(graft->node, guard, build);
Packit fb9d21
	isl_ast_build_free(build);
Packit fb9d21
Packit fb9d21
	if (!graft->node)
Packit fb9d21
		return isl_ast_graft_free(graft);
Packit fb9d21
Packit fb9d21
	return graft;
Packit fb9d21
error:
Packit fb9d21
	isl_set_free(guard);
Packit fb9d21
	return isl_ast_graft_free(graft);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Insert an if node around graft->node testing the condition encoded
Packit fb9d21
 * in graft->guard, assuming graft->guard involves any conditions.
Packit fb9d21
 */
Packit fb9d21
static __isl_give isl_ast_graft *insert_pending_guard_node(
Packit fb9d21
	__isl_take isl_ast_graft *graft, __isl_keep isl_ast_build *build)
Packit fb9d21
{
Packit fb9d21
	if (!graft)
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	return insert_if_node(graft, isl_set_copy(graft->guard), build);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Replace graft->enforced by "enforced".
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_ast_graft *isl_ast_graft_set_enforced(
Packit fb9d21
	__isl_take isl_ast_graft *graft, __isl_take isl_basic_set *enforced)
Packit fb9d21
{
Packit fb9d21
	if (!graft || !enforced)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	isl_basic_set_free(graft->enforced);
Packit fb9d21
	graft->enforced = enforced;
Packit fb9d21
Packit fb9d21
	return graft;
Packit fb9d21
error:
Packit fb9d21
	isl_basic_set_free(enforced);
Packit fb9d21
	return isl_ast_graft_free(graft);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Update "enforced" such that it only involves constraints that are
Packit fb9d21
 * also enforced by "graft".
Packit fb9d21
 */
Packit fb9d21
static __isl_give isl_basic_set *update_enforced(
Packit fb9d21
	__isl_take isl_basic_set *enforced, __isl_keep isl_ast_graft *graft,
Packit fb9d21
	int depth)
Packit fb9d21
{
Packit fb9d21
	isl_basic_set *enforced_g;
Packit fb9d21
Packit fb9d21
	enforced_g = isl_ast_graft_get_enforced(graft);
Packit fb9d21
	if (depth < isl_basic_set_dim(enforced_g, isl_dim_set))
Packit fb9d21
		enforced_g = isl_basic_set_eliminate(enforced_g,
Packit fb9d21
							isl_dim_set, depth, 1);
Packit fb9d21
	enforced_g = isl_basic_set_remove_unknown_divs(enforced_g);
Packit fb9d21
	enforced_g = isl_basic_set_align_params(enforced_g,
Packit fb9d21
				isl_basic_set_get_space(enforced));
Packit fb9d21
	enforced = isl_basic_set_align_params(enforced,
Packit fb9d21
				isl_basic_set_get_space(enforced_g));
Packit fb9d21
	enforced = isl_set_simple_hull(isl_basic_set_union(enforced,
Packit fb9d21
						enforced_g));
Packit fb9d21
Packit fb9d21
	return enforced;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Extend the node at *body with node.
Packit fb9d21
 *
Packit fb9d21
 * If body points to the else branch, then *body may still be NULL.
Packit fb9d21
 * If so, we simply attach node to this else branch.
Packit fb9d21
 * Otherwise, we attach a list containing the statements already
Packit fb9d21
 * attached at *body followed by node.
Packit fb9d21
 */
Packit fb9d21
static void extend_body(__isl_keep isl_ast_node **body,
Packit fb9d21
	__isl_take isl_ast_node *node)
Packit fb9d21
{
Packit fb9d21
	isl_ast_node_list *list;
Packit fb9d21
Packit fb9d21
	if  (!*body) {
Packit fb9d21
		*body = node;
Packit fb9d21
		return;
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	if ((*body)->type == isl_ast_node_block) {
Packit fb9d21
		list = isl_ast_node_block_get_children(*body);
Packit fb9d21
		isl_ast_node_free(*body);
Packit fb9d21
	} else
Packit fb9d21
		list = isl_ast_node_list_from_ast_node(*body);
Packit fb9d21
	list = isl_ast_node_list_add(list, node);
Packit fb9d21
	*body = isl_ast_node_alloc_block(list);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Merge "graft" into the last graft of "list".
Packit fb9d21
 * body points to the then or else branch of an if node in that last graft.
Packit fb9d21
 *
Packit fb9d21
 * We attach graft->node to this branch and update the enforced
Packit fb9d21
 * set of the last graft of "list" to take into account the enforced
Packit fb9d21
 * set of "graft".
Packit fb9d21
 */
Packit fb9d21
static __isl_give isl_ast_graft_list *graft_extend_body(
Packit fb9d21
	__isl_take isl_ast_graft_list *list,
Packit fb9d21
	__isl_keep isl_ast_node **body, __isl_take isl_ast_graft *graft,
Packit fb9d21
	__isl_keep isl_ast_build *build)
Packit fb9d21
{
Packit fb9d21
	int n;
Packit fb9d21
	int depth;
Packit fb9d21
	isl_ast_graft *last;
Packit fb9d21
	isl_space *space;
Packit fb9d21
	isl_basic_set *enforced;
Packit fb9d21
Packit fb9d21
	if (!list || !graft)
Packit fb9d21
		goto error;
Packit fb9d21
	extend_body(body, isl_ast_node_copy(graft->node));
Packit fb9d21
	if (!*body)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	n = isl_ast_graft_list_n_ast_graft(list);
Packit fb9d21
	last = isl_ast_graft_list_get_ast_graft(list, n - 1);
Packit fb9d21
Packit fb9d21
	depth = isl_ast_build_get_depth(build);
Packit fb9d21
	space = isl_ast_build_get_space(build, 1);
Packit fb9d21
	enforced = isl_basic_set_empty(space);
Packit fb9d21
	enforced = update_enforced(enforced, last, depth);
Packit fb9d21
	enforced = update_enforced(enforced, graft, depth);
Packit fb9d21
	last = isl_ast_graft_set_enforced(last, enforced);
Packit fb9d21
Packit fb9d21
	list = isl_ast_graft_list_set_ast_graft(list, n - 1, last);
Packit fb9d21
	isl_ast_graft_free(graft);
Packit fb9d21
	return list;
Packit fb9d21
error:
Packit fb9d21
	isl_ast_graft_free(graft);
Packit fb9d21
	return isl_ast_graft_list_free(list);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Merge "graft" into the last graft of "list", attaching graft->node
Packit fb9d21
 * to the then branch of "last_if".
Packit fb9d21
 */
Packit fb9d21
static __isl_give isl_ast_graft_list *extend_then(
Packit fb9d21
	__isl_take isl_ast_graft_list *list,
Packit fb9d21
	__isl_keep isl_ast_node *last_if, __isl_take isl_ast_graft *graft,
Packit fb9d21
	__isl_keep isl_ast_build *build)
Packit fb9d21
{
Packit fb9d21
	return graft_extend_body(list, &last_if->u.i.then, graft, build);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Merge "graft" into the last graft of "list", attaching graft->node
Packit fb9d21
 * to the else branch of "last_if".
Packit fb9d21
 */
Packit fb9d21
static __isl_give isl_ast_graft_list *extend_else(
Packit fb9d21
	__isl_take isl_ast_graft_list *list,
Packit fb9d21
	__isl_keep isl_ast_node *last_if, __isl_take isl_ast_graft *graft,
Packit fb9d21
	__isl_keep isl_ast_build *build)
Packit fb9d21
{
Packit fb9d21
	return graft_extend_body(list, &last_if->u.i.else_node, graft, build);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* This data structure keeps track of an if node.
Packit fb9d21
 *
Packit fb9d21
 * "node" is the actual if-node
Packit fb9d21
 * "guard" is the original, non-simplified guard of the node
Packit fb9d21
 * "complement" is the complement of "guard" in the context of outer if nodes
Packit fb9d21
 */
Packit fb9d21
struct isl_if_node {
Packit fb9d21
	isl_ast_node *node;
Packit fb9d21
	isl_set *guard;
Packit fb9d21
	isl_set *complement;
Packit fb9d21
};
Packit fb9d21
Packit fb9d21
/* Given a list of "n" if nodes, clear those starting at "first"
Packit fb9d21
 * and return "first" (i.e., the updated size of the array).
Packit fb9d21
 */
Packit fb9d21
static int clear_if_nodes(struct isl_if_node *if_node, int first, int n)
Packit fb9d21
{
Packit fb9d21
	int i;
Packit fb9d21
Packit fb9d21
	for (i = first; i < n; ++i) {
Packit fb9d21
		isl_set_free(if_node[i].guard);
Packit fb9d21
		isl_set_free(if_node[i].complement);
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	return first;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* For each graft in "list",
Packit fb9d21
 * insert an if node around graft->node testing the condition encoded
Packit fb9d21
 * in graft->guard, assuming graft->guard involves any conditions.
Packit fb9d21
 *
Packit fb9d21
 * We keep track of a list of generated if nodes that can be extended
Packit fb9d21
 * without changing the order of the elements in "list".
Packit fb9d21
 * If the guard of a graft is a subset of either the guard or its complement
Packit fb9d21
 * of one of those if nodes, then the node
Packit fb9d21
 * of the new graft is inserted into the then or else branch of the last graft
Packit fb9d21
 * and the current graft is discarded.
Packit fb9d21
 * The guard of the node is then simplified based on the conditions
Packit fb9d21
 * enforced at that then or else branch.
Packit fb9d21
 * Otherwise, the current graft is appended to the list.
Packit fb9d21
 *
Packit fb9d21
 * We only construct else branches if allowed by the user.
Packit fb9d21
 */
Packit fb9d21
static __isl_give isl_ast_graft_list *insert_pending_guard_nodes(
Packit fb9d21
	__isl_take isl_ast_graft_list *list,
Packit fb9d21
	__isl_keep isl_ast_build *build)
Packit fb9d21
{
Packit fb9d21
	int i, j, n, n_if;
Packit fb9d21
	int allow_else;
Packit fb9d21
	isl_ctx *ctx;
Packit fb9d21
	isl_ast_graft_list *res;
Packit fb9d21
	struct isl_if_node *if_node = NULL;
Packit fb9d21
Packit fb9d21
	if (!build || !list)
Packit fb9d21
		return isl_ast_graft_list_free(list);
Packit fb9d21
Packit fb9d21
	ctx = isl_ast_build_get_ctx(build);
Packit fb9d21
	n = isl_ast_graft_list_n_ast_graft(list);
Packit fb9d21
Packit fb9d21
	allow_else = isl_options_get_ast_build_allow_else(ctx);
Packit fb9d21
Packit fb9d21
	n_if = 0;
Packit fb9d21
	if (n > 1) {
Packit fb9d21
		if_node = isl_alloc_array(ctx, struct isl_if_node, n - 1);
Packit fb9d21
		if (!if_node)
Packit fb9d21
			return isl_ast_graft_list_free(list);
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	res = isl_ast_graft_list_alloc(ctx, n);
Packit fb9d21
Packit fb9d21
	for (i = 0; i < n; ++i) {
Packit fb9d21
		isl_set *guard;
Packit fb9d21
		isl_ast_graft *graft;
Packit fb9d21
		int subset, found_then, found_else;
Packit fb9d21
		isl_ast_node *node;
Packit fb9d21
Packit fb9d21
		graft = isl_ast_graft_list_get_ast_graft(list, i);
Packit fb9d21
		if (!graft)
Packit fb9d21
			break;
Packit fb9d21
		subset = 0;
Packit fb9d21
		found_then = found_else = -1;
Packit fb9d21
		if (n_if > 0) {
Packit fb9d21
			isl_set *test;
Packit fb9d21
			test = isl_set_copy(graft->guard);
Packit fb9d21
			test = isl_set_intersect(test,
Packit fb9d21
						isl_set_copy(build->domain));
Packit fb9d21
			for (j = n_if - 1; j >= 0; --j) {
Packit fb9d21
				subset = isl_set_is_subset(test,
Packit fb9d21
							if_node[j].guard);
Packit fb9d21
				if (subset < 0 || subset) {
Packit fb9d21
					found_then = j;
Packit fb9d21
					break;
Packit fb9d21
				}
Packit fb9d21
				if (!allow_else)
Packit fb9d21
					continue;
Packit fb9d21
				subset = isl_set_is_subset(test,
Packit fb9d21
							if_node[j].complement);
Packit fb9d21
				if (subset < 0 || subset) {
Packit fb9d21
					found_else = j;
Packit fb9d21
					break;
Packit fb9d21
				}
Packit fb9d21
			}
Packit fb9d21
			n_if = clear_if_nodes(if_node, j + 1, n_if);
Packit fb9d21
			isl_set_free(test);
Packit fb9d21
		}
Packit fb9d21
		if (subset < 0) {
Packit fb9d21
			graft = isl_ast_graft_free(graft);
Packit fb9d21
			break;
Packit fb9d21
		}
Packit fb9d21
Packit fb9d21
		guard = isl_set_copy(graft->guard);
Packit fb9d21
		if (found_then >= 0)
Packit fb9d21
			graft->guard = isl_set_gist(graft->guard,
Packit fb9d21
				isl_set_copy(if_node[found_then].guard));
Packit fb9d21
		else if (found_else >= 0)
Packit fb9d21
			graft->guard = isl_set_gist(graft->guard,
Packit fb9d21
				isl_set_copy(if_node[found_else].complement));
Packit fb9d21
Packit fb9d21
		node = graft->node;
Packit fb9d21
		if (!graft->guard)
Packit fb9d21
			graft = isl_ast_graft_free(graft);
Packit fb9d21
		graft = insert_pending_guard_node(graft, build);
Packit fb9d21
		if (graft && graft->node != node && i != n - 1) {
Packit fb9d21
			isl_set *set;
Packit fb9d21
			if_node[n_if].node = graft->node;
Packit fb9d21
			if_node[n_if].guard = guard;
Packit fb9d21
			if (found_then >= 0)
Packit fb9d21
				set = if_node[found_then].guard;
Packit fb9d21
			else if (found_else >= 0)
Packit fb9d21
				set = if_node[found_else].complement;
Packit fb9d21
			else
Packit fb9d21
				set = build->domain;
Packit fb9d21
			set = isl_set_copy(set);
Packit fb9d21
			set = isl_set_subtract(set, isl_set_copy(guard));
Packit fb9d21
			if_node[n_if].complement = set;
Packit fb9d21
			n_if++;
Packit fb9d21
		} else
Packit fb9d21
			isl_set_free(guard);
Packit fb9d21
		if (!graft)
Packit fb9d21
			break;
Packit fb9d21
Packit fb9d21
		if (found_then >= 0)
Packit fb9d21
			res = extend_then(res, if_node[found_then].node,
Packit fb9d21
						graft, build);
Packit fb9d21
		else if (found_else >= 0)
Packit fb9d21
			res = extend_else(res, if_node[found_else].node,
Packit fb9d21
						graft, build);
Packit fb9d21
		else
Packit fb9d21
			res = isl_ast_graft_list_add(res, graft);
Packit fb9d21
	}
Packit fb9d21
	if (i < n)
Packit fb9d21
		res = isl_ast_graft_list_free(res);
Packit fb9d21
Packit fb9d21
	isl_ast_graft_list_free(list);
Packit fb9d21
	clear_if_nodes(if_node, 0, n_if);
Packit fb9d21
	free(if_node);
Packit fb9d21
	return res;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Collect the nodes contained in the grafts in "list" in a node list.
Packit fb9d21
 */
Packit fb9d21
static __isl_give isl_ast_node_list *extract_node_list(
Packit fb9d21
	__isl_keep isl_ast_graft_list *list)
Packit fb9d21
{
Packit fb9d21
	int i, n;
Packit fb9d21
	isl_ctx *ctx;
Packit fb9d21
	isl_ast_node_list *node_list;
Packit fb9d21
Packit fb9d21
	if (!list)
Packit fb9d21
		return NULL;
Packit fb9d21
	ctx = isl_ast_graft_list_get_ctx(list);
Packit fb9d21
	n = isl_ast_graft_list_n_ast_graft(list);
Packit fb9d21
	node_list = isl_ast_node_list_alloc(ctx, n);
Packit fb9d21
	for (i = 0; i < n; ++i) {
Packit fb9d21
		isl_ast_node *node;
Packit fb9d21
		isl_ast_graft *graft;
Packit fb9d21
Packit fb9d21
		graft = isl_ast_graft_list_get_ast_graft(list, i);
Packit fb9d21
		node = isl_ast_graft_get_node(graft);
Packit fb9d21
		node_list = isl_ast_node_list_add(node_list, node);
Packit fb9d21
		isl_ast_graft_free(graft);
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	return node_list;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Look for shared enforced constraints by all the elements in "list"
Packit fb9d21
 * on outer loops (with respect to the current depth) and return the result.
Packit fb9d21
 *
Packit fb9d21
 * We assume that the number of children is at least one.
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_basic_set *isl_ast_graft_list_extract_shared_enforced(
Packit fb9d21
	__isl_keep isl_ast_graft_list *list,
Packit fb9d21
	__isl_keep isl_ast_build *build)
Packit fb9d21
{
Packit fb9d21
	int i, n;
Packit fb9d21
	int depth;
Packit fb9d21
	isl_space *space;
Packit fb9d21
	isl_basic_set *enforced;
Packit fb9d21
Packit fb9d21
	if (!list)
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	n = isl_ast_graft_list_n_ast_graft(list);
Packit fb9d21
	if (n == 0)
Packit fb9d21
		isl_die(isl_ast_graft_list_get_ctx(list), isl_error_invalid,
Packit fb9d21
			"for node should have at least one child",
Packit fb9d21
			return NULL);
Packit fb9d21
Packit fb9d21
	space = isl_ast_build_get_space(build, 1);
Packit fb9d21
	enforced = isl_basic_set_empty(space);
Packit fb9d21
Packit fb9d21
	depth = isl_ast_build_get_depth(build);
Packit fb9d21
	for (i = 0; i < n; ++i) {
Packit fb9d21
		isl_ast_graft *graft;
Packit fb9d21
Packit fb9d21
		graft = isl_ast_graft_list_get_ast_graft(list, i);
Packit fb9d21
		enforced = update_enforced(enforced, graft, depth);
Packit fb9d21
		isl_ast_graft_free(graft);
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	return enforced;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Record "guard" in "graft" so that it will be enforced somewhere
Packit fb9d21
 * up the tree.  If the graft already has a guard, then it may be partially
Packit fb9d21
 * redundant in combination with the new guard and in the context
Packit fb9d21
 * the generated constraints of "build".  In fact, the new guard
Packit fb9d21
 * may in itself have some redundant constraints.
Packit fb9d21
 * We therefore (re)compute the gist of the intersection
Packit fb9d21
 * and coalesce the result.
Packit fb9d21
 */
Packit fb9d21
static __isl_give isl_ast_graft *store_guard(__isl_take isl_ast_graft *graft,
Packit fb9d21
	__isl_take isl_set *guard, __isl_keep isl_ast_build *build)
Packit fb9d21
{
Packit fb9d21
	int is_universe;
Packit fb9d21
Packit fb9d21
	if (!graft)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	is_universe = isl_set_plain_is_universe(guard);
Packit fb9d21
	if (is_universe < 0)
Packit fb9d21
		goto error;
Packit fb9d21
	if (is_universe) {
Packit fb9d21
		isl_set_free(guard);
Packit fb9d21
		return graft;
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	graft->guard = isl_set_intersect(graft->guard, guard);
Packit fb9d21
	graft->guard = isl_set_gist(graft->guard,
Packit fb9d21
				    isl_ast_build_get_generated(build));
Packit fb9d21
	graft->guard = isl_set_coalesce(graft->guard);
Packit fb9d21
	if (!graft->guard)
Packit fb9d21
		return isl_ast_graft_free(graft);
Packit fb9d21
Packit fb9d21
	return graft;
Packit fb9d21
error:
Packit fb9d21
	isl_set_free(guard);
Packit fb9d21
	return isl_ast_graft_free(graft);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* For each graft in "list", replace its guard with the gist with
Packit fb9d21
 * respect to "context".
Packit fb9d21
 */
Packit fb9d21
static __isl_give isl_ast_graft_list *gist_guards(
Packit fb9d21
	__isl_take isl_ast_graft_list *list, __isl_keep isl_set *context)
Packit fb9d21
{
Packit fb9d21
	int i, n;
Packit fb9d21
Packit fb9d21
	if (!list)
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	n = isl_ast_graft_list_n_ast_graft(list);
Packit fb9d21
	for (i = 0; i < n; ++i) {
Packit fb9d21
		isl_ast_graft *graft;
Packit fb9d21
Packit fb9d21
		graft = isl_ast_graft_list_get_ast_graft(list, i);
Packit fb9d21
		if (!graft)
Packit fb9d21
			break;
Packit fb9d21
		graft->guard = isl_set_gist(graft->guard,
Packit fb9d21
						isl_set_copy(context));
Packit fb9d21
		if (!graft->guard)
Packit fb9d21
			graft = isl_ast_graft_free(graft);
Packit fb9d21
		list = isl_ast_graft_list_set_ast_graft(list, i, graft);
Packit fb9d21
	}
Packit fb9d21
	if (i < n)
Packit fb9d21
		return isl_ast_graft_list_free(list);
Packit fb9d21
Packit fb9d21
	return list;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* For each graft in "list", replace its guard with the gist with
Packit fb9d21
 * respect to "context".
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_ast_graft_list *isl_ast_graft_list_gist_guards(
Packit fb9d21
	__isl_take isl_ast_graft_list *list, __isl_take isl_set *context)
Packit fb9d21
{
Packit fb9d21
	list = gist_guards(list, context);
Packit fb9d21
	isl_set_free(context);
Packit fb9d21
Packit fb9d21
	return list;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Allocate a graft in "build" based on the list of grafts in "sub_build".
Packit fb9d21
 * "guard" and "enforced" are the guard and enforced constraints
Packit fb9d21
 * of the allocated graft.  The guard is used to simplify the guards
Packit fb9d21
 * of the elements in "list".
Packit fb9d21
 *
Packit fb9d21
 * The node is initialized to either a block containing the nodes of "children"
Packit fb9d21
 * or, if there is only a single child, the node of that child.
Packit fb9d21
 * If the current level requires a for node, it should be inserted by
Packit fb9d21
 * a subsequent call to isl_ast_graft_insert_for.
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_ast_graft *isl_ast_graft_alloc_from_children(
Packit fb9d21
	__isl_take isl_ast_graft_list *list, __isl_take isl_set *guard,
Packit fb9d21
	__isl_take isl_basic_set *enforced, __isl_keep isl_ast_build *build,
Packit fb9d21
	__isl_keep isl_ast_build *sub_build)
Packit fb9d21
{
Packit fb9d21
	isl_ast_build *guard_build;
Packit fb9d21
	isl_ast_node *node;
Packit fb9d21
	isl_ast_node_list *node_list;
Packit fb9d21
	isl_ast_graft *graft;
Packit fb9d21
Packit fb9d21
	guard_build = isl_ast_build_copy(sub_build);
Packit fb9d21
	guard_build = isl_ast_build_replace_pending_by_guard(guard_build,
Packit fb9d21
						isl_set_copy(guard));
Packit fb9d21
	list = gist_guards(list, guard);
Packit fb9d21
	list = insert_pending_guard_nodes(list, guard_build);
Packit fb9d21
	isl_ast_build_free(guard_build);
Packit fb9d21
Packit fb9d21
	node_list = extract_node_list(list);
Packit fb9d21
	node = isl_ast_node_from_ast_node_list(node_list);
Packit fb9d21
	isl_ast_graft_list_free(list);
Packit fb9d21
Packit fb9d21
	graft = isl_ast_graft_alloc(node, build);
Packit fb9d21
	graft = store_guard(graft, guard, build);
Packit fb9d21
	graft = isl_ast_graft_enforce(graft, enforced);
Packit fb9d21
Packit fb9d21
	return graft;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Combine the grafts in the list into a single graft.
Packit fb9d21
 *
Packit fb9d21
 * The guard is initialized to the shared guard of the list elements (if any),
Packit fb9d21
 * provided it does not depend on the current dimension.
Packit fb9d21
 * The guards in the elements are then simplified with respect to the
Packit fb9d21
 * hoisted guard and materialized as if nodes around the contained AST nodes
Packit fb9d21
 * in the context of "sub_build".
Packit fb9d21
 *
Packit fb9d21
 * The enforced set is initialized to the simple hull of the enforced sets
Packit fb9d21
 * of the elements, provided the ast_build_exploit_nested_bounds option is set
Packit fb9d21
 * or the new graft will be used at the same level.
Packit fb9d21
 *
Packit fb9d21
 * The node is initialized to either a block containing the nodes of "list"
Packit fb9d21
 * or, if there is only a single element, the node of that element.
Packit fb9d21
 */
Packit fb9d21
static __isl_give isl_ast_graft *ast_graft_list_fuse(
Packit fb9d21
	__isl_take isl_ast_graft_list *list, __isl_keep isl_ast_build *build)
Packit fb9d21
{
Packit fb9d21
	isl_ast_graft *graft;
Packit fb9d21
	isl_basic_set *enforced;
Packit fb9d21
	isl_set *guard;
Packit fb9d21
Packit fb9d21
	if (!list)
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	enforced = isl_ast_graft_list_extract_shared_enforced(list, build);
Packit fb9d21
	guard = isl_ast_graft_list_extract_hoistable_guard(list, build);
Packit fb9d21
	graft = isl_ast_graft_alloc_from_children(list, guard, enforced,
Packit fb9d21
						    build, build);
Packit fb9d21
Packit fb9d21
	return graft;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Combine the grafts in the list into a single graft.
Packit fb9d21
 * Return a list containing this single graft.
Packit fb9d21
 * If the original list is empty, then return an empty list.
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_ast_graft_list *isl_ast_graft_list_fuse(
Packit fb9d21
	__isl_take isl_ast_graft_list *list,
Packit fb9d21
	__isl_keep isl_ast_build *build)
Packit fb9d21
{
Packit fb9d21
	isl_ast_graft *graft;
Packit fb9d21
Packit fb9d21
	if (!list)
Packit fb9d21
		return NULL;
Packit fb9d21
	if (isl_ast_graft_list_n_ast_graft(list) <= 1)
Packit fb9d21
		return list;
Packit fb9d21
	graft = ast_graft_list_fuse(list, build);
Packit fb9d21
	return isl_ast_graft_list_from_ast_graft(graft);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Combine the two grafts into a single graft.
Packit fb9d21
 * Return a list containing this single graft.
Packit fb9d21
 */
Packit fb9d21
static __isl_give isl_ast_graft *isl_ast_graft_fuse(
Packit fb9d21
	__isl_take isl_ast_graft *graft1, __isl_take isl_ast_graft *graft2,
Packit fb9d21
	__isl_keep isl_ast_build *build)
Packit fb9d21
{
Packit fb9d21
	isl_ctx *ctx;
Packit fb9d21
	isl_ast_graft_list *list;
Packit fb9d21
Packit fb9d21
	ctx = isl_ast_build_get_ctx(build);
Packit fb9d21
Packit fb9d21
	list = isl_ast_graft_list_alloc(ctx, 2);
Packit fb9d21
	list = isl_ast_graft_list_add(list, graft1);
Packit fb9d21
	list = isl_ast_graft_list_add(list, graft2);
Packit fb9d21
Packit fb9d21
	return ast_graft_list_fuse(list, build);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Insert a for node enclosing the current graft->node.
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_ast_graft *isl_ast_graft_insert_for(
Packit fb9d21
	__isl_take isl_ast_graft *graft, __isl_take isl_ast_node *node)
Packit fb9d21
{
Packit fb9d21
	if (!graft)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	graft->node = isl_ast_node_for_set_body(node, graft->node);
Packit fb9d21
	if (!graft->node)
Packit fb9d21
		return isl_ast_graft_free(graft);
Packit fb9d21
Packit fb9d21
	return graft;
Packit fb9d21
error:
Packit fb9d21
	isl_ast_node_free(node);
Packit fb9d21
	isl_ast_graft_free(graft);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Represent the graft list as an AST node.
Packit fb9d21
 * This operation drops the information about guards in the grafts, so
Packit fb9d21
 * if there are any pending guards, then they are materialized as if nodes.
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_ast_node *isl_ast_node_from_graft_list(
Packit fb9d21
	__isl_take isl_ast_graft_list *list,
Packit fb9d21
	__isl_keep isl_ast_build *build)
Packit fb9d21
{
Packit fb9d21
	isl_ast_node_list *node_list;
Packit fb9d21
Packit fb9d21
	list = insert_pending_guard_nodes(list, build);
Packit fb9d21
	node_list = extract_node_list(list);
Packit fb9d21
	isl_ast_graft_list_free(list);
Packit fb9d21
Packit fb9d21
	return isl_ast_node_from_ast_node_list(node_list);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
void *isl_ast_graft_free(__isl_take isl_ast_graft *graft)
Packit fb9d21
{
Packit fb9d21
	if (!graft)
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	if (--graft->ref > 0)
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	isl_ast_node_free(graft->node);
Packit fb9d21
	isl_set_free(graft->guard);
Packit fb9d21
	isl_basic_set_free(graft->enforced);
Packit fb9d21
	free(graft);
Packit fb9d21
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Record that the grafted tree enforces
Packit fb9d21
 * "enforced" by intersecting graft->enforced with "enforced".
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_ast_graft *isl_ast_graft_enforce(
Packit fb9d21
	__isl_take isl_ast_graft *graft, __isl_take isl_basic_set *enforced)
Packit fb9d21
{
Packit fb9d21
	if (!graft || !enforced)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	enforced = isl_basic_set_align_params(enforced,
Packit fb9d21
				isl_basic_set_get_space(graft->enforced));
Packit fb9d21
	graft->enforced = isl_basic_set_align_params(graft->enforced,
Packit fb9d21
				isl_basic_set_get_space(enforced));
Packit fb9d21
	graft->enforced = isl_basic_set_intersect(graft->enforced, enforced);
Packit fb9d21
	if (!graft->enforced)
Packit fb9d21
		return isl_ast_graft_free(graft);
Packit fb9d21
Packit fb9d21
	return graft;
Packit fb9d21
error:
Packit fb9d21
	isl_basic_set_free(enforced);
Packit fb9d21
	return isl_ast_graft_free(graft);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
__isl_give isl_basic_set *isl_ast_graft_get_enforced(
Packit fb9d21
	__isl_keep isl_ast_graft *graft)
Packit fb9d21
{
Packit fb9d21
	return graft ? isl_basic_set_copy(graft->enforced) : NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
__isl_give isl_set *isl_ast_graft_get_guard(__isl_keep isl_ast_graft *graft)
Packit fb9d21
{
Packit fb9d21
	return graft ? isl_set_copy(graft->guard) : NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Record that "guard" needs to be inserted in "graft".
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_ast_graft *isl_ast_graft_add_guard(
Packit fb9d21
	__isl_take isl_ast_graft *graft,
Packit fb9d21
	__isl_take isl_set *guard, __isl_keep isl_ast_build *build)
Packit fb9d21
{
Packit fb9d21
	return store_guard(graft, guard, build);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Reformulate the "graft", which was generated in the context
Packit fb9d21
 * of an inner code generation, in terms of the outer code generation
Packit fb9d21
 * AST build.
Packit fb9d21
 *
Packit fb9d21
 * If "product" is set, then the domain of the inner code generation build is
Packit fb9d21
 *
Packit fb9d21
 *	[O -> S]
Packit fb9d21
 *
Packit fb9d21
 * with O the domain of the outer code generation build.
Packit fb9d21
 * We essentially need to project out S.
Packit fb9d21
 *
Packit fb9d21
 * If "product" is not set, then we need to project the domains onto
Packit fb9d21
 * their parameter spaces.
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_ast_graft *isl_ast_graft_unembed(__isl_take isl_ast_graft *graft,
Packit fb9d21
	int product)
Packit fb9d21
{
Packit fb9d21
	isl_basic_set *enforced;
Packit fb9d21
Packit fb9d21
	if (!graft)
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	if (product) {
Packit fb9d21
		enforced = graft->enforced;
Packit fb9d21
		enforced = isl_basic_map_domain(isl_basic_set_unwrap(enforced));
Packit fb9d21
		graft->enforced = enforced;
Packit fb9d21
		graft->guard = isl_map_domain(isl_set_unwrap(graft->guard));
Packit fb9d21
	} else {
Packit fb9d21
		graft->enforced = isl_basic_set_params(graft->enforced);
Packit fb9d21
		graft->guard = isl_set_params(graft->guard);
Packit fb9d21
	}
Packit fb9d21
	graft->guard = isl_set_compute_divs(graft->guard);
Packit fb9d21
Packit fb9d21
	if (!graft->enforced || !graft->guard)
Packit fb9d21
		return isl_ast_graft_free(graft);
Packit fb9d21
Packit fb9d21
	return graft;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Reformulate the grafts in "list", which were generated in the context
Packit fb9d21
 * of an inner code generation, in terms of the outer code generation
Packit fb9d21
 * AST build.
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_ast_graft_list *isl_ast_graft_list_unembed(
Packit fb9d21
	__isl_take isl_ast_graft_list *list, int product)
Packit fb9d21
{
Packit fb9d21
	int i, n;
Packit fb9d21
Packit fb9d21
	n = isl_ast_graft_list_n_ast_graft(list);
Packit fb9d21
	for (i = 0; i < n; ++i) {
Packit fb9d21
		isl_ast_graft *graft;
Packit fb9d21
Packit fb9d21
		graft = isl_ast_graft_list_get_ast_graft(list, i);
Packit fb9d21
		graft = isl_ast_graft_unembed(graft, product);
Packit fb9d21
		list = isl_ast_graft_list_set_ast_graft(list, i, graft);
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	return list;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Compute the preimage of "graft" under the function represented by "ma".
Packit fb9d21
 * In other words, plug in "ma" in "enforced" and "guard" fields of "graft".
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_ast_graft *isl_ast_graft_preimage_multi_aff(
Packit fb9d21
	__isl_take isl_ast_graft *graft, __isl_take isl_multi_aff *ma)
Packit fb9d21
{
Packit fb9d21
	isl_basic_set *enforced;
Packit fb9d21
Packit fb9d21
	if (!graft)
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	enforced = graft->enforced;
Packit fb9d21
	graft->enforced = isl_basic_set_preimage_multi_aff(enforced,
Packit fb9d21
						isl_multi_aff_copy(ma));
Packit fb9d21
	graft->guard = isl_set_preimage_multi_aff(graft->guard, ma);
Packit fb9d21
Packit fb9d21
	if (!graft->enforced || !graft->guard)
Packit fb9d21
		return isl_ast_graft_free(graft);
Packit fb9d21
Packit fb9d21
	return graft;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Compute the preimage of all the grafts in "list" under
Packit fb9d21
 * the function represented by "ma".
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_ast_graft_list *isl_ast_graft_list_preimage_multi_aff(
Packit fb9d21
	__isl_take isl_ast_graft_list *list, __isl_take isl_multi_aff *ma)
Packit fb9d21
{
Packit fb9d21
	int i, n;
Packit fb9d21
Packit fb9d21
	n = isl_ast_graft_list_n_ast_graft(list);
Packit fb9d21
	for (i = 0; i < n; ++i) {
Packit fb9d21
		isl_ast_graft *graft;
Packit fb9d21
Packit fb9d21
		graft = isl_ast_graft_list_get_ast_graft(list, i);
Packit fb9d21
		graft = isl_ast_graft_preimage_multi_aff(graft,
Packit fb9d21
						    isl_multi_aff_copy(ma));
Packit fb9d21
		list = isl_ast_graft_list_set_ast_graft(list, i, graft);
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	isl_multi_aff_free(ma);
Packit fb9d21
	return list;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Compare two grafts based on their guards.
Packit fb9d21
 */
Packit fb9d21
static int cmp_graft(__isl_keep isl_ast_graft *a, __isl_keep isl_ast_graft *b,
Packit fb9d21
	void *user)
Packit fb9d21
{
Packit fb9d21
	return isl_set_plain_cmp(a->guard, b->guard);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Order the elements in "list" based on their guards.
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_ast_graft_list *isl_ast_graft_list_sort_guard(
Packit fb9d21
	__isl_take isl_ast_graft_list *list)
Packit fb9d21
{
Packit fb9d21
	return isl_ast_graft_list_sort(list, &cmp_graft, NULL);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Merge the given two lists into a single list of grafts,
Packit fb9d21
 * merging grafts with the same guard into a single graft.
Packit fb9d21
 *
Packit fb9d21
 * "list2" has been sorted using isl_ast_graft_list_sort.
Packit fb9d21
 * "list1" may be the result of a previous call to isl_ast_graft_list_merge
Packit fb9d21
 * and may therefore not be completely sorted.
Packit fb9d21
 *
Packit fb9d21
 * The elements in "list2" need to be executed after those in "list1",
Packit fb9d21
 * but if the guard of a graft in "list2" is disjoint from the guards
Packit fb9d21
 * of some final elements in "list1", then it can be moved up to before
Packit fb9d21
 * those final elements.
Packit fb9d21
 *
Packit fb9d21
 * In particular, we look at each element g of "list2" in turn
Packit fb9d21
 * and move it up beyond elements of "list1" that would be sorted
Packit fb9d21
 * after g as long as each of these elements has a guard that is disjoint
Packit fb9d21
 * from that of g.
Packit fb9d21
 *
Packit fb9d21
 * We do not allow the second or any later element of "list2" to be moved
Packit fb9d21
 * before a previous elements of "list2" even if the reason that
Packit fb9d21
 * that element didn't move up further was that its guard was not disjoint
Packit fb9d21
 * from that of the previous element in "list1".
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_ast_graft_list *isl_ast_graft_list_merge(
Packit fb9d21
	__isl_take isl_ast_graft_list *list1,
Packit fb9d21
	__isl_take isl_ast_graft_list *list2,
Packit fb9d21
	__isl_keep isl_ast_build *build)
Packit fb9d21
{
Packit fb9d21
	int i, j, first;
Packit fb9d21
Packit fb9d21
	if (!list1 || !list2 || !build)
Packit fb9d21
		goto error;
Packit fb9d21
	if (list2->n == 0) {
Packit fb9d21
		isl_ast_graft_list_free(list2);
Packit fb9d21
		return list1;
Packit fb9d21
	}
Packit fb9d21
	if (list1->n == 0) {
Packit fb9d21
		isl_ast_graft_list_free(list1);
Packit fb9d21
		return list2;
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	first = 0;
Packit fb9d21
	for (i = 0; i < list2->n; ++i) {
Packit fb9d21
		isl_ast_graft *graft;
Packit fb9d21
		graft = isl_ast_graft_list_get_ast_graft(list2, i);
Packit fb9d21
		if (!graft)
Packit fb9d21
			break;
Packit fb9d21
Packit fb9d21
		for (j = list1->n; j >= 0; --j) {
Packit fb9d21
			int cmp, disjoint;
Packit fb9d21
			isl_ast_graft *graft_j;
Packit fb9d21
Packit fb9d21
			if (j == first)
Packit fb9d21
				cmp = -1;
Packit fb9d21
			else
Packit fb9d21
				cmp = isl_set_plain_cmp(list1->p[j - 1]->guard,
Packit fb9d21
							graft->guard);
Packit fb9d21
			if (cmp > 0) {
Packit fb9d21
				disjoint = isl_set_is_disjoint(graft->guard,
Packit fb9d21
							list1->p[j - 1]->guard);
Packit fb9d21
				if (disjoint < 0) {
Packit fb9d21
					list1 = isl_ast_graft_list_free(list1);
Packit fb9d21
					break;
Packit fb9d21
				}
Packit fb9d21
				if (!disjoint)
Packit fb9d21
					cmp = -1;
Packit fb9d21
			}
Packit fb9d21
			if (cmp > 0)
Packit fb9d21
				continue;
Packit fb9d21
			if (cmp < 0) {
Packit fb9d21
				list1 = isl_ast_graft_list_insert(list1, j,
Packit fb9d21
								graft);
Packit fb9d21
				break;
Packit fb9d21
			}
Packit fb9d21
Packit fb9d21
			--j;
Packit fb9d21
Packit fb9d21
			graft_j = isl_ast_graft_list_get_ast_graft(list1, j);
Packit fb9d21
			graft_j = isl_ast_graft_fuse(graft_j, graft, build);
Packit fb9d21
			list1 = isl_ast_graft_list_set_ast_graft(list1, j,
Packit fb9d21
								graft_j);
Packit fb9d21
			break;
Packit fb9d21
		}
Packit fb9d21
Packit fb9d21
		if (j < 0)
Packit fb9d21
			isl_die(isl_ast_build_get_ctx(build),
Packit fb9d21
				isl_error_internal,
Packit fb9d21
				"element failed to get inserted", break);
Packit fb9d21
Packit fb9d21
		first = j + 1;
Packit fb9d21
		if (!list1)
Packit fb9d21
			break;
Packit fb9d21
	}
Packit fb9d21
	if (i < list2->n)
Packit fb9d21
		list1 = isl_ast_graft_list_free(list1);
Packit fb9d21
	isl_ast_graft_list_free(list2);
Packit fb9d21
Packit fb9d21
	return list1;
Packit fb9d21
error:
Packit fb9d21
	isl_ast_graft_list_free(list1);
Packit fb9d21
	isl_ast_graft_list_free(list2);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
__isl_give isl_printer *isl_printer_print_ast_graft(__isl_take isl_printer *p,
Packit fb9d21
	__isl_keep isl_ast_graft *graft)
Packit fb9d21
{
Packit fb9d21
	if (!p)
Packit fb9d21
		return NULL;
Packit fb9d21
	if (!graft)
Packit fb9d21
		return isl_printer_free(p);
Packit fb9d21
Packit fb9d21
	p = isl_printer_print_str(p, "(");
Packit fb9d21
	p = isl_printer_print_str(p, "guard: ");
Packit fb9d21
	p = isl_printer_print_set(p, graft->guard);
Packit fb9d21
	p = isl_printer_print_str(p, ", ");
Packit fb9d21
	p = isl_printer_print_str(p, "enforced: ");
Packit fb9d21
	p = isl_printer_print_basic_set(p, graft->enforced);
Packit fb9d21
	p = isl_printer_print_str(p, ", ");
Packit fb9d21
	p = isl_printer_print_str(p, "node: ");
Packit fb9d21
	p = isl_printer_print_ast_node(p, graft->node);
Packit fb9d21
	p = isl_printer_print_str(p, ")");
Packit fb9d21
Packit fb9d21
	return p;
Packit fb9d21
}