Blame isl-0.14/isl_band.c

Packit fb9d21
/*
Packit fb9d21
 * Copyright 2011      INRIA Saclay
Packit fb9d21
 * Copyright 2012-2013 Ecole Normale Superieure
Packit fb9d21
 *
Packit fb9d21
 * Use of this software is governed by the MIT license
Packit fb9d21
 *
Packit fb9d21
 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
Packit fb9d21
 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
Packit fb9d21
 * 91893 Orsay, France
Packit fb9d21
 * and Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
Packit fb9d21
 */
Packit fb9d21
Packit fb9d21
#include <isl_band_private.h>
Packit fb9d21
#include <isl_schedule_private.h>
Packit fb9d21
Packit fb9d21
#undef BASE
Packit fb9d21
#define BASE band
Packit fb9d21
Packit fb9d21
#include <isl_list_templ.c>
Packit fb9d21
Packit fb9d21
isl_ctx *isl_band_get_ctx(__isl_keep isl_band *band)
Packit fb9d21
{
Packit fb9d21
	return band ? isl_union_pw_multi_aff_get_ctx(band->pma) : NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
__isl_give isl_band *isl_band_alloc(isl_ctx *ctx)
Packit fb9d21
{
Packit fb9d21
	isl_band *band;
Packit fb9d21
Packit fb9d21
	band = isl_calloc_type(ctx, isl_band);
Packit fb9d21
	if (!band)
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	band->ref = 1;
Packit fb9d21
Packit fb9d21
	return band;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Create a duplicate of the given band.  The duplicate refers
Packit fb9d21
 * to the same schedule and parent as the input, but does not
Packit fb9d21
 * increment their reference counts.
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_band *isl_band_dup(__isl_keep isl_band *band)
Packit fb9d21
{
Packit fb9d21
	int i;
Packit fb9d21
	isl_ctx *ctx;
Packit fb9d21
	isl_band *dup;
Packit fb9d21
Packit fb9d21
	if (!band)
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	ctx = isl_band_get_ctx(band);
Packit fb9d21
	dup = isl_band_alloc(ctx);
Packit fb9d21
	if (!dup)
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	dup->n = band->n;
Packit fb9d21
	dup->coincident = isl_alloc_array(ctx, int, band->n);
Packit fb9d21
	if (band->n && !dup->coincident)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	for (i = 0; i < band->n; ++i)
Packit fb9d21
		dup->coincident[i] = band->coincident[i];
Packit fb9d21
Packit fb9d21
	dup->pma = isl_union_pw_multi_aff_copy(band->pma);
Packit fb9d21
	dup->schedule = band->schedule;
Packit fb9d21
	dup->parent = band->parent;
Packit fb9d21
Packit fb9d21
	if (!dup->pma)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	return dup;
Packit fb9d21
error:
Packit fb9d21
	isl_band_free(dup);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* We not only increment the reference count of the band,
Packit fb9d21
 * but also that of the schedule that contains this band.
Packit fb9d21
 * This ensures that the schedule won't disappear while there
Packit fb9d21
 * is still a reference to the band outside of the schedule.
Packit fb9d21
 * There is no need to increment the reference count of the parent
Packit fb9d21
 * band as the parent band is part of the same schedule.
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_band *isl_band_copy(__isl_keep isl_band *band)
Packit fb9d21
{
Packit fb9d21
	if (!band)
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	band->ref++;
Packit fb9d21
	band->schedule->ref++;
Packit fb9d21
	return band;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* If this is not the last reference to the band (the one from within the
Packit fb9d21
 * schedule), then we also need to decrement the reference count of the
Packit fb9d21
 * containing schedule as it was incremented in isl_band_copy.
Packit fb9d21
 */
Packit fb9d21
__isl_null isl_band *isl_band_free(__isl_take isl_band *band)
Packit fb9d21
{
Packit fb9d21
	if (!band)
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	if (--band->ref > 0) {
Packit fb9d21
		isl_schedule_free(band->schedule);
Packit fb9d21
		return NULL;
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	isl_union_pw_multi_aff_free(band->pma);
Packit fb9d21
	isl_band_list_free(band->children);
Packit fb9d21
	free(band->coincident);
Packit fb9d21
	free(band);
Packit fb9d21
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
int isl_band_has_children(__isl_keep isl_band *band)
Packit fb9d21
{
Packit fb9d21
	if (!band)
Packit fb9d21
		return -1;
Packit fb9d21
Packit fb9d21
	return band->children != NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
__isl_give isl_band_list *isl_band_get_children(
Packit fb9d21
	__isl_keep isl_band *band)
Packit fb9d21
{
Packit fb9d21
	if (!band)
Packit fb9d21
		return NULL;
Packit fb9d21
	if (!band->children)
Packit fb9d21
		isl_die(isl_band_get_ctx(band), isl_error_invalid,
Packit fb9d21
			"band has no children", return NULL);
Packit fb9d21
	return isl_band_list_dup(band->children);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
int isl_band_n_member(__isl_keep isl_band *band)
Packit fb9d21
{
Packit fb9d21
	return band ? band->n : 0;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Is the given scheduling dimension coincident within the band and
Packit fb9d21
 * with respect to the coincidence constraints.
Packit fb9d21
 */
Packit fb9d21
int isl_band_member_is_coincident(__isl_keep isl_band *band, int pos)
Packit fb9d21
{
Packit fb9d21
	if (!band)
Packit fb9d21
		return -1;
Packit fb9d21
Packit fb9d21
	if (pos < 0 || pos >= band->n)
Packit fb9d21
		isl_die(isl_band_get_ctx(band), isl_error_invalid,
Packit fb9d21
			"invalid member position", return -1);
Packit fb9d21
Packit fb9d21
	return band->coincident[pos];
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Return the schedule that leads up to this band.
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_union_map *isl_band_get_prefix_schedule(
Packit fb9d21
	__isl_keep isl_band *band)
Packit fb9d21
{
Packit fb9d21
	isl_union_set *domain;
Packit fb9d21
	isl_union_pw_multi_aff *prefix;
Packit fb9d21
	isl_band *a;
Packit fb9d21
Packit fb9d21
	if (!band)
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	prefix = isl_union_pw_multi_aff_copy(band->pma);
Packit fb9d21
	domain = isl_union_pw_multi_aff_domain(prefix);
Packit fb9d21
	prefix = isl_union_pw_multi_aff_from_domain(domain);
Packit fb9d21
Packit fb9d21
	for (a = band->parent; a; a = a->parent) {
Packit fb9d21
		isl_union_pw_multi_aff *partial;
Packit fb9d21
Packit fb9d21
		partial = isl_union_pw_multi_aff_copy(a->pma);
Packit fb9d21
		prefix = isl_union_pw_multi_aff_flat_range_product(partial,
Packit fb9d21
								   prefix);
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	return isl_union_map_from_union_pw_multi_aff(prefix);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Return the schedule of the band in isolation.
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_union_pw_multi_aff *
Packit fb9d21
isl_band_get_partial_schedule_union_pw_multi_aff(__isl_keep isl_band *band)
Packit fb9d21
{
Packit fb9d21
	return band ? isl_union_pw_multi_aff_copy(band->pma) : NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Return the schedule of the band in isolation.
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_union_map *isl_band_get_partial_schedule(
Packit fb9d21
	__isl_keep isl_band *band)
Packit fb9d21
{
Packit fb9d21
	isl_union_pw_multi_aff *sched;
Packit fb9d21
Packit fb9d21
	sched = isl_band_get_partial_schedule_union_pw_multi_aff(band);
Packit fb9d21
	return isl_union_map_from_union_pw_multi_aff(sched);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
__isl_give isl_union_pw_multi_aff *
Packit fb9d21
isl_band_get_suffix_schedule_union_pw_multi_aff(__isl_keep isl_band *band);
Packit fb9d21
Packit fb9d21
/* Return the schedule for the given band list.
Packit fb9d21
 * For each band in the list, the schedule is composed of the partial
Packit fb9d21
 * and suffix schedules of that band.
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_union_pw_multi_aff *
Packit fb9d21
isl_band_list_get_suffix_schedule_union_pw_multi_aff(
Packit fb9d21
	__isl_keep isl_band_list *list)
Packit fb9d21
{
Packit fb9d21
	isl_ctx *ctx;
Packit fb9d21
	int i, n;
Packit fb9d21
	isl_space *space;
Packit fb9d21
	isl_union_pw_multi_aff *suffix;
Packit fb9d21
Packit fb9d21
	if (!list)
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	ctx = isl_band_list_get_ctx(list);
Packit fb9d21
	space = isl_space_alloc(ctx, 0, 0, 0);
Packit fb9d21
	suffix = isl_union_pw_multi_aff_empty(space);
Packit fb9d21
	n = isl_band_list_n_band(list);
Packit fb9d21
	for (i = 0; i < n; ++i) {
Packit fb9d21
		isl_band *el;
Packit fb9d21
		isl_union_pw_multi_aff *partial;
Packit fb9d21
		isl_union_pw_multi_aff *suffix_i;
Packit fb9d21
Packit fb9d21
		el = isl_band_list_get_band(list, i);
Packit fb9d21
		partial = isl_band_get_partial_schedule_union_pw_multi_aff(el);
Packit fb9d21
		suffix_i = isl_band_get_suffix_schedule_union_pw_multi_aff(el);
Packit fb9d21
		suffix_i = isl_union_pw_multi_aff_flat_range_product(
Packit fb9d21
				partial, suffix_i);
Packit fb9d21
		suffix = isl_union_pw_multi_aff_add(suffix, suffix_i);
Packit fb9d21
Packit fb9d21
		isl_band_free(el);
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	return suffix;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Return the schedule for the given band list.
Packit fb9d21
 * For each band in the list, the schedule is composed of the partial
Packit fb9d21
 * and suffix schedules of that band.
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_union_map *isl_band_list_get_suffix_schedule(
Packit fb9d21
	__isl_keep isl_band_list *list)
Packit fb9d21
{
Packit fb9d21
	isl_union_pw_multi_aff *suffix;
Packit fb9d21
Packit fb9d21
	suffix = isl_band_list_get_suffix_schedule_union_pw_multi_aff(list);
Packit fb9d21
	return isl_union_map_from_union_pw_multi_aff(suffix);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Return the schedule for the forest underneath the given band.
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_union_pw_multi_aff *
Packit fb9d21
isl_band_get_suffix_schedule_union_pw_multi_aff(__isl_keep isl_band *band)
Packit fb9d21
{
Packit fb9d21
	isl_union_pw_multi_aff *suffix;
Packit fb9d21
Packit fb9d21
	if (!band)
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	if (!isl_band_has_children(band)) {
Packit fb9d21
		isl_union_set *domain;
Packit fb9d21
Packit fb9d21
		suffix = isl_union_pw_multi_aff_copy(band->pma);
Packit fb9d21
		domain = isl_union_pw_multi_aff_domain(suffix);
Packit fb9d21
		suffix = isl_union_pw_multi_aff_from_domain(domain);
Packit fb9d21
	} else {
Packit fb9d21
		isl_band_list *list;
Packit fb9d21
Packit fb9d21
		list = isl_band_get_children(band);
Packit fb9d21
		suffix =
Packit fb9d21
		    isl_band_list_get_suffix_schedule_union_pw_multi_aff(list);
Packit fb9d21
		isl_band_list_free(list);
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	return suffix;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Return the schedule for the forest underneath the given band.
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_union_map *isl_band_get_suffix_schedule(
Packit fb9d21
	__isl_keep isl_band *band)
Packit fb9d21
{
Packit fb9d21
	isl_union_pw_multi_aff *suffix;
Packit fb9d21
Packit fb9d21
	suffix = isl_band_get_suffix_schedule_union_pw_multi_aff(band);
Packit fb9d21
	return isl_union_map_from_union_pw_multi_aff(suffix);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Call "fn" on each band (recursively) in the list
Packit fb9d21
 * in depth-first post-order.
Packit fb9d21
 */
Packit fb9d21
int isl_band_list_foreach_band(__isl_keep isl_band_list *list,
Packit fb9d21
	int (*fn)(__isl_keep isl_band *band, void *user), void *user)
Packit fb9d21
{
Packit fb9d21
	int i, n;
Packit fb9d21
Packit fb9d21
	if (!list)
Packit fb9d21
		return -1;
Packit fb9d21
Packit fb9d21
	n = isl_band_list_n_band(list);
Packit fb9d21
	for (i = 0; i < n; ++i) {
Packit fb9d21
		isl_band *band;
Packit fb9d21
		int r = 0;
Packit fb9d21
Packit fb9d21
		band = isl_band_list_get_band(list, i);
Packit fb9d21
		if (isl_band_has_children(band)) {
Packit fb9d21
			isl_band_list *children;
Packit fb9d21
Packit fb9d21
			children = isl_band_get_children(band);
Packit fb9d21
			r = isl_band_list_foreach_band(children, fn, user);
Packit fb9d21
			isl_band_list_free(children);
Packit fb9d21
		}
Packit fb9d21
Packit fb9d21
		if (!band)
Packit fb9d21
			r = -1;
Packit fb9d21
		if (r == 0)
Packit fb9d21
			r = fn(band, user);
Packit fb9d21
Packit fb9d21
		isl_band_free(band);
Packit fb9d21
		if (r)
Packit fb9d21
			return r;
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	return 0;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Internal data used during the construction of the schedule
Packit fb9d21
 * for the tile loops.
Packit fb9d21
 *
Packit fb9d21
 * sizes contains the tile sizes
Packit fb9d21
 * scale is set if the tile loops should be scaled
Packit fb9d21
 * tiled collects the result for a single statement
Packit fb9d21
 * res collects the result for all statements
Packit fb9d21
 */
Packit fb9d21
struct isl_band_tile_data {
Packit fb9d21
	isl_multi_val *sizes;
Packit fb9d21
	isl_union_pw_multi_aff *res;
Packit fb9d21
	isl_pw_multi_aff *tiled;
Packit fb9d21
	int scale;
Packit fb9d21
};
Packit fb9d21
Packit fb9d21
/* Given part of the schedule of a band, construct the corresponding
Packit fb9d21
 * schedule for the tile loops based on the tile sizes in data->sizes
Packit fb9d21
 * and add the result to data->tiled.
Packit fb9d21
 *
Packit fb9d21
 * If data->scale is set, then dimension i of the schedule will be
Packit fb9d21
 * of the form
Packit fb9d21
 *
Packit fb9d21
 *	m_i * floor(s_i(x) / m_i)
Packit fb9d21
 *
Packit fb9d21
 * where s_i(x) refers to the original schedule and m_i is the tile size.
Packit fb9d21
 * If data->scale is not set, then dimension i of the schedule will be
Packit fb9d21
 * of the form
Packit fb9d21
 *
Packit fb9d21
 *	floor(s_i(x) / m_i)
Packit fb9d21
 *
Packit fb9d21
 */
Packit fb9d21
static int multi_aff_tile(__isl_take isl_set *set,
Packit fb9d21
	__isl_take isl_multi_aff *ma, void *user)
Packit fb9d21
{
Packit fb9d21
	struct isl_band_tile_data *data = user;
Packit fb9d21
	isl_pw_multi_aff *pma;
Packit fb9d21
	int i, n;
Packit fb9d21
	isl_val *v;
Packit fb9d21
Packit fb9d21
	n = isl_multi_aff_dim(ma, isl_dim_out);
Packit fb9d21
Packit fb9d21
	for (i = 0; i < n; ++i) {
Packit fb9d21
		isl_aff *aff;
Packit fb9d21
Packit fb9d21
		aff = isl_multi_aff_get_aff(ma, i);
Packit fb9d21
		v = isl_multi_val_get_val(data->sizes, i);
Packit fb9d21
Packit fb9d21
		aff = isl_aff_scale_down_val(aff, isl_val_copy(v));
Packit fb9d21
		aff = isl_aff_floor(aff);
Packit fb9d21
		if (data->scale)
Packit fb9d21
			aff = isl_aff_scale_val(aff, isl_val_copy(v));
Packit fb9d21
		isl_val_free(v);
Packit fb9d21
Packit fb9d21
		ma = isl_multi_aff_set_aff(ma, i, aff);
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	pma = isl_pw_multi_aff_alloc(set, ma);
Packit fb9d21
	data->tiled = isl_pw_multi_aff_union_add(data->tiled, pma);
Packit fb9d21
Packit fb9d21
	return 0;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Given part of the schedule of a band, construct the corresponding
Packit fb9d21
 * schedule for the tile loops based on the tile sizes in data->sizes
Packit fb9d21
 * and add the result to data->res.
Packit fb9d21
 */
Packit fb9d21
static int pw_multi_aff_tile(__isl_take isl_pw_multi_aff *pma, void *user)
Packit fb9d21
{
Packit fb9d21
	struct isl_band_tile_data *data = user;
Packit fb9d21
Packit fb9d21
	data->tiled = isl_pw_multi_aff_empty(isl_pw_multi_aff_get_space(pma));
Packit fb9d21
Packit fb9d21
	if (isl_pw_multi_aff_foreach_piece(pma, &multi_aff_tile, data) < 0)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	isl_pw_multi_aff_free(pma);
Packit fb9d21
	data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res,
Packit fb9d21
								data->tiled);
Packit fb9d21
Packit fb9d21
	return 0;
Packit fb9d21
error:
Packit fb9d21
	isl_pw_multi_aff_free(pma);
Packit fb9d21
	isl_pw_multi_aff_free(data->tiled);
Packit fb9d21
	return -1;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Given the schedule of a band, construct the corresponding
Packit fb9d21
 * schedule for the tile loops based on the given tile sizes
Packit fb9d21
 * and return the result.
Packit fb9d21
 */
Packit fb9d21
static isl_union_pw_multi_aff *isl_union_pw_multi_aff_tile(
Packit fb9d21
	__isl_take isl_union_pw_multi_aff *sched,
Packit fb9d21
	__isl_keep isl_multi_val *sizes)
Packit fb9d21
{
Packit fb9d21
	isl_ctx *ctx;
Packit fb9d21
	isl_space *space;
Packit fb9d21
	struct isl_band_tile_data data = { sizes };
Packit fb9d21
Packit fb9d21
	ctx = isl_multi_val_get_ctx(sizes);
Packit fb9d21
Packit fb9d21
	space = isl_union_pw_multi_aff_get_space(sched);
Packit fb9d21
	data.res = isl_union_pw_multi_aff_empty(space);
Packit fb9d21
	data.scale = isl_options_get_tile_scale_tile_loops(ctx);
Packit fb9d21
Packit fb9d21
	if (isl_union_pw_multi_aff_foreach_pw_multi_aff(sched,
Packit fb9d21
						&pw_multi_aff_tile, &data) < 0)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	isl_union_pw_multi_aff_free(sched);
Packit fb9d21
	return data.res;
Packit fb9d21
error:
Packit fb9d21
	isl_union_pw_multi_aff_free(sched);
Packit fb9d21
	isl_union_pw_multi_aff_free(data.res);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Extract the range space from "pma" and store it in *user.
Packit fb9d21
 * All entries are expected to have the same range space, so we can
Packit fb9d21
 * stop after extracting the range space from the first entry.
Packit fb9d21
 */
Packit fb9d21
static int extract_range_space(__isl_take isl_pw_multi_aff *pma, void *user)
Packit fb9d21
{
Packit fb9d21
	isl_space **space = user;
Packit fb9d21
Packit fb9d21
	*space = isl_space_range(isl_pw_multi_aff_get_space(pma));
Packit fb9d21
	isl_pw_multi_aff_free(pma);
Packit fb9d21
Packit fb9d21
	return -1;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Extract the range space of "band".  All entries in band->pma should
Packit fb9d21
 * have the same range space.  Furthermore, band->pma should have at least
Packit fb9d21
 * one entry.
Packit fb9d21
 */
Packit fb9d21
static __isl_give isl_space *band_get_range_space(__isl_keep isl_band *band)
Packit fb9d21
{
Packit fb9d21
	isl_space *space;
Packit fb9d21
Packit fb9d21
	if (!band)
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	space = NULL;
Packit fb9d21
	isl_union_pw_multi_aff_foreach_pw_multi_aff(band->pma,
Packit fb9d21
						&extract_range_space, &space);
Packit fb9d21
Packit fb9d21
	return space;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Construct and return an isl_multi_val in the given space, with as entries
Packit fb9d21
 * the first elements of "v", padded with ones if the size of "v" is smaller
Packit fb9d21
 * than the dimension of "space".
Packit fb9d21
 */
Packit fb9d21
static __isl_give isl_multi_val *multi_val_from_vec(__isl_take isl_space *space,
Packit fb9d21
	__isl_take isl_vec *v)
Packit fb9d21
{
Packit fb9d21
	isl_ctx *ctx;
Packit fb9d21
	isl_multi_val *mv;
Packit fb9d21
	int i, n, size;
Packit fb9d21
Packit fb9d21
	if (!space || !v)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	ctx = isl_space_get_ctx(space);
Packit fb9d21
	mv = isl_multi_val_zero(space);
Packit fb9d21
	n = isl_multi_val_dim(mv, isl_dim_set);
Packit fb9d21
	size = isl_vec_size(v);
Packit fb9d21
	if (n < size)
Packit fb9d21
		size = n;
Packit fb9d21
Packit fb9d21
	for (i = 0; i < size; ++i) {
Packit fb9d21
		isl_val *val = isl_vec_get_element_val(v, i);
Packit fb9d21
		mv = isl_multi_val_set_val(mv, i, val);
Packit fb9d21
	}
Packit fb9d21
	for (i = size; i < n; ++i)
Packit fb9d21
		mv = isl_multi_val_set_val(mv, i, isl_val_one(ctx));
Packit fb9d21
Packit fb9d21
	isl_vec_free(v);
Packit fb9d21
	return mv;
Packit fb9d21
error:
Packit fb9d21
	isl_space_free(space);
Packit fb9d21
	isl_vec_free(v);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Tile the given band using the specified tile sizes.
Packit fb9d21
 * The given band is modified to refer to the tile loops and
Packit fb9d21
 * a child band is created to refer to the point loops.
Packit fb9d21
 * The children of this point loop band are the children
Packit fb9d21
 * of the original band.
Packit fb9d21
 *
Packit fb9d21
 * If the scale tile loops option is set, then the tile loops
Packit fb9d21
 * are scaled by the tile sizes.  If the shift point loops option is set,
Packit fb9d21
 * then the point loops are shifted to start at zero.
Packit fb9d21
 * In particular, these options affect the tile and point loop schedules
Packit fb9d21
 * as follows
Packit fb9d21
 *
Packit fb9d21
 *	scale	shift	original	tile		point
Packit fb9d21
 *
Packit fb9d21
 *	0	0	i		floor(i/s)	i
Packit fb9d21
 *	1	0	i		s * floor(i/s)	i
Packit fb9d21
 *	0	1	i		floor(i/s)	i - s * floor(i/s)
Packit fb9d21
 *	1	1	i		s * floor(i/s)	i - s * floor(i/s)
Packit fb9d21
 */
Packit fb9d21
int isl_band_tile(__isl_keep isl_band *band, __isl_take isl_vec *sizes)
Packit fb9d21
{
Packit fb9d21
	isl_ctx *ctx;
Packit fb9d21
	isl_band *child;
Packit fb9d21
	isl_band_list *list = NULL;
Packit fb9d21
	isl_union_pw_multi_aff *sched = NULL, *child_sched = NULL;
Packit fb9d21
	isl_space *space;
Packit fb9d21
	isl_multi_val *mv_sizes;
Packit fb9d21
Packit fb9d21
	if (!band || !sizes)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	ctx = isl_vec_get_ctx(sizes);
Packit fb9d21
	child = isl_band_dup(band);
Packit fb9d21
	list = isl_band_list_alloc(ctx, 1);
Packit fb9d21
	list = isl_band_list_add(list, child);
Packit fb9d21
	if (!list)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	space = band_get_range_space(band);
Packit fb9d21
	mv_sizes = multi_val_from_vec(space, isl_vec_copy(sizes));
Packit fb9d21
	sched = isl_union_pw_multi_aff_copy(band->pma);
Packit fb9d21
	sched = isl_union_pw_multi_aff_tile(sched, mv_sizes);
Packit fb9d21
Packit fb9d21
	child_sched = isl_union_pw_multi_aff_copy(child->pma);
Packit fb9d21
	if (isl_options_get_tile_shift_point_loops(ctx)) {
Packit fb9d21
		isl_union_pw_multi_aff *scaled;
Packit fb9d21
		scaled = isl_union_pw_multi_aff_copy(sched);
Packit fb9d21
		if (!isl_options_get_tile_scale_tile_loops(ctx))
Packit fb9d21
			scaled = isl_union_pw_multi_aff_scale_multi_val(scaled,
Packit fb9d21
						isl_multi_val_copy(mv_sizes));
Packit fb9d21
		child_sched = isl_union_pw_multi_aff_sub(child_sched, scaled);
Packit fb9d21
	}
Packit fb9d21
	isl_multi_val_free(mv_sizes);
Packit fb9d21
	if (!sched || !child_sched)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	child->children = band->children;
Packit fb9d21
	band->children = list;
Packit fb9d21
	child->parent = band;
Packit fb9d21
	isl_union_pw_multi_aff_free(band->pma);
Packit fb9d21
	band->pma = sched;
Packit fb9d21
	isl_union_pw_multi_aff_free(child->pma);
Packit fb9d21
	child->pma = child_sched;
Packit fb9d21
Packit fb9d21
	isl_vec_free(sizes);
Packit fb9d21
	return 0;
Packit fb9d21
error:
Packit fb9d21
	isl_union_pw_multi_aff_free(sched);
Packit fb9d21
	isl_union_pw_multi_aff_free(child_sched);
Packit fb9d21
	isl_band_list_free(list);
Packit fb9d21
	isl_vec_free(sizes);
Packit fb9d21
	return -1;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Internal data structure used inside isl_union_pw_multi_aff_drop.
Packit fb9d21
 *
Packit fb9d21
 * "pos" is the position of the first dimension to drop.
Packit fb9d21
 * "n" is the number of dimensions to drop.
Packit fb9d21
 * "res" accumulates the result.
Packit fb9d21
 */
Packit fb9d21
struct isl_union_pw_multi_aff_drop_data {
Packit fb9d21
	int pos;
Packit fb9d21
	int n;
Packit fb9d21
	isl_union_pw_multi_aff *res;
Packit fb9d21
};
Packit fb9d21
Packit fb9d21
/* Drop the data->n output dimensions starting at data->pos from "pma"
Packit fb9d21
 * and add the result to data->res.
Packit fb9d21
 */
Packit fb9d21
static int pw_multi_aff_drop(__isl_take isl_pw_multi_aff *pma, void *user)
Packit fb9d21
{
Packit fb9d21
	struct isl_union_pw_multi_aff_drop_data *data = user;
Packit fb9d21
Packit fb9d21
	pma = isl_pw_multi_aff_drop_dims(pma, isl_dim_out, data->pos, data->n);
Packit fb9d21
Packit fb9d21
	data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
Packit fb9d21
	if (!data->res)
Packit fb9d21
		return -1;
Packit fb9d21
Packit fb9d21
	return 0;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Drop the "n" output dimensions starting at "pos" from "sched".
Packit fb9d21
 */
Packit fb9d21
static isl_union_pw_multi_aff *isl_union_pw_multi_aff_drop(
Packit fb9d21
	__isl_take isl_union_pw_multi_aff *sched, int pos, int n)
Packit fb9d21
{
Packit fb9d21
	isl_space *space;
Packit fb9d21
	struct isl_union_pw_multi_aff_drop_data data = { pos, n };
Packit fb9d21
Packit fb9d21
	space = isl_union_pw_multi_aff_get_space(sched);
Packit fb9d21
	data.res = isl_union_pw_multi_aff_empty(space);
Packit fb9d21
Packit fb9d21
	if (isl_union_pw_multi_aff_foreach_pw_multi_aff(sched,
Packit fb9d21
						&pw_multi_aff_drop, &data) < 0)
Packit fb9d21
		data.res = isl_union_pw_multi_aff_free(data.res);
Packit fb9d21
Packit fb9d21
	isl_union_pw_multi_aff_free(sched);
Packit fb9d21
	return data.res;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Drop the "n" dimensions starting at "pos" from "band".
Packit fb9d21
 */
Packit fb9d21
static int isl_band_drop(__isl_keep isl_band *band, int pos, int n)
Packit fb9d21
{
Packit fb9d21
	int i;
Packit fb9d21
	isl_union_pw_multi_aff *sched;
Packit fb9d21
Packit fb9d21
	if (!band)
Packit fb9d21
		return -1;
Packit fb9d21
	if (n == 0)
Packit fb9d21
		return 0;
Packit fb9d21
Packit fb9d21
	sched = isl_union_pw_multi_aff_copy(band->pma);
Packit fb9d21
	sched = isl_union_pw_multi_aff_drop(sched, pos, n);
Packit fb9d21
	if (!sched)
Packit fb9d21
		return -1;
Packit fb9d21
Packit fb9d21
	isl_union_pw_multi_aff_free(band->pma);
Packit fb9d21
	band->pma = sched;
Packit fb9d21
Packit fb9d21
	for (i = pos + n; i < band->n; ++i)
Packit fb9d21
		band->coincident[i - n] = band->coincident[i];
Packit fb9d21
Packit fb9d21
	band->n -= n;
Packit fb9d21
Packit fb9d21
	return 0;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Split the given band into two nested bands, one with the first "pos"
Packit fb9d21
 * dimensions of "band" and one with the remaining band->n - pos dimensions.
Packit fb9d21
 */
Packit fb9d21
int isl_band_split(__isl_keep isl_band *band, int pos)
Packit fb9d21
{
Packit fb9d21
	isl_ctx *ctx;
Packit fb9d21
	isl_band *child;
Packit fb9d21
	isl_band_list *list;
Packit fb9d21
Packit fb9d21
	if (!band)
Packit fb9d21
		return -1;
Packit fb9d21
Packit fb9d21
	ctx = isl_band_get_ctx(band);
Packit fb9d21
Packit fb9d21
	if (pos < 0 || pos > band->n)
Packit fb9d21
		isl_die(ctx, isl_error_invalid, "position out of bounds",
Packit fb9d21
			return -1);
Packit fb9d21
Packit fb9d21
	child = isl_band_dup(band);
Packit fb9d21
	if (isl_band_drop(child, 0, pos) < 0)
Packit fb9d21
		child = isl_band_free(child);
Packit fb9d21
	list = isl_band_list_alloc(ctx, 1);
Packit fb9d21
	list = isl_band_list_add(list, child);
Packit fb9d21
	if (!list)
Packit fb9d21
		return -1;
Packit fb9d21
Packit fb9d21
	if (isl_band_drop(band, pos, band->n - pos) < 0) {
Packit fb9d21
		isl_band_list_free(list);
Packit fb9d21
		return -1;
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	child->children = band->children;
Packit fb9d21
	band->children = list;
Packit fb9d21
	child->parent = band;
Packit fb9d21
Packit fb9d21
	return 0;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
__isl_give isl_printer *isl_printer_print_band(__isl_take isl_printer *p,
Packit fb9d21
	__isl_keep isl_band *band)
Packit fb9d21
{
Packit fb9d21
	isl_union_map *prefix, *partial, *suffix;
Packit fb9d21
Packit fb9d21
	prefix = isl_band_get_prefix_schedule(band);
Packit fb9d21
	partial = isl_band_get_partial_schedule(band);
Packit fb9d21
	suffix = isl_band_get_suffix_schedule(band);
Packit fb9d21
Packit fb9d21
	p = isl_printer_print_str(p, "(");
Packit fb9d21
	p = isl_printer_print_union_map(p, prefix);
Packit fb9d21
	p = isl_printer_print_str(p, ",");
Packit fb9d21
	p = isl_printer_print_union_map(p, partial);
Packit fb9d21
	p = isl_printer_print_str(p, ",");
Packit fb9d21
	p = isl_printer_print_union_map(p, suffix);
Packit fb9d21
	p = isl_printer_print_str(p, ")");
Packit fb9d21
Packit fb9d21
	isl_union_map_free(prefix);
Packit fb9d21
	isl_union_map_free(partial);
Packit fb9d21
	isl_union_map_free(suffix);
Packit fb9d21
Packit fb9d21
	return p;
Packit fb9d21
}