free(dim);
}
+/* Check if "s" is a valid dimension or tuple name.
+ * We currently only forbid names that look like a number.
+ *
+ * s is assumed to be non-NULL.
+ */
static int name_ok(isl_ctx *ctx, const char *s)
{
char *p;
return 1;
}
-int isl_space_has_tuple_id(__isl_keep isl_space *dim, enum isl_dim_type type)
+/* Is it possible for the given dimension type to have a tuple id?
+ */
+static int space_can_have_id(__isl_keep isl_space *space,
+ enum isl_dim_type type)
{
- if (!dim)
- return -1;
- if (isl_space_is_params(dim))
- isl_die(dim->ctx, isl_error_invalid,
- "parameter spaces don't have tuple ids", return -1);
- if (isl_space_is_set(dim) && type != isl_dim_set)
- isl_die(dim->ctx, isl_error_invalid,
- "set spaces can only have a set id", return -1);
+ if (!space)
+ return 0;
+ if (isl_space_is_params(space))
+ isl_die(space->ctx, isl_error_invalid,
+ "parameter spaces don't have tuple ids", return 0);
+ if (isl_space_is_set(space) && type != isl_dim_set)
+ isl_die(space->ctx, isl_error_invalid,
+ "set spaces can only have a set id", return 0);
if (type != isl_dim_in && type != isl_dim_out)
- isl_die(dim->ctx, isl_error_invalid,
+ isl_die(space->ctx, isl_error_invalid,
"only input, output and set tuples can have ids",
- return -1);
+ return 0);
+
+ return 1;
+}
+
+/* Does the tuple have an id?
+ */
+int isl_space_has_tuple_id(__isl_keep isl_space *dim, enum isl_dim_type type)
+{
+ if (!space_can_have_id(dim, type))
+ return -1;
return dim->tuple_id[type - isl_dim_in] != NULL;
}
return NULL;
}
-__isl_give isl_space *isl_space_set_dim_id(__isl_take isl_space *dim,
+/* Set the id of the given dimension of "space" to "id".
+ * If the dimension already has an id, then it is replaced.
+ * If the dimension is a parameter, then we need to change it
+ * in the nested spaces (if any) as well.
+ */
+__isl_give isl_space *isl_space_set_dim_id(__isl_take isl_space *space,
enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
{
- dim = isl_space_cow(dim);
- if (!dim || !id)
+ space = isl_space_cow(space);
+ if (!space || !id)
goto error;
- isl_id_free(get_id(dim, type, pos));
- return set_id(dim, type, pos, id);
+
+ if (type == isl_dim_param) {
+ int i;
+
+ for (i = 0; i < 2; ++i) {
+ if (!space->nested[i])
+ continue;
+ space->nested[i] =
+ isl_space_set_dim_id(space->nested[i],
+ type, pos, isl_id_copy(id));
+ if (!space->nested[i])
+ goto error;
+ }
+ }
+
+ isl_id_free(get_id(space, type, pos));
+ return set_id(space, type, pos, id);
error:
isl_id_free(id);
- isl_space_free(dim);
+ isl_space_free(space);
+ return NULL;
+}
+
+/* Reset the id of the given dimension of "space".
+ * If the dimension already has an id, then it is removed.
+ * If the dimension is a parameter, then we need to reset it
+ * in the nested spaces (if any) as well.
+ */
+__isl_give isl_space *isl_space_reset_dim_id(__isl_take isl_space *space,
+ enum isl_dim_type type, unsigned pos)
+{
+ space = isl_space_cow(space);
+ if (!space)
+ goto error;
+
+ if (type == isl_dim_param) {
+ int i;
+
+ for (i = 0; i < 2; ++i) {
+ if (!space->nested[i])
+ continue;
+ space->nested[i] =
+ isl_space_reset_dim_id(space->nested[i],
+ type, pos);
+ if (!space->nested[i])
+ goto error;
+ }
+ }
+
+ isl_id_free(get_id(space, type, pos));
+ return set_id(space, type, pos, NULL);
+error:
+ isl_space_free(space);
return NULL;
}
return NULL;
}
+/* Does the tuple have a name?
+ */
+int isl_space_has_tuple_name(__isl_keep isl_space *space,
+ enum isl_dim_type type)
+{
+ isl_id *id;
+
+ if (!space_can_have_id(space, type))
+ return -1;
+ id = space->tuple_id[type - isl_dim_in];
+ return id && id->name;
+}
+
const char *isl_space_get_tuple_name(__isl_keep isl_space *dim,
enum isl_dim_type type)
{
if (!dim)
return NULL;
+ if (!s)
+ return isl_space_reset_dim_id(dim, type, pos);
if (!name_ok(dim->ctx, s))
goto error;
id = isl_id_alloc(dim->ctx, s, NULL);
return NULL;
}
+/* Does the given dimension have a name?
+ */
+int isl_space_has_dim_name(__isl_keep isl_space *space,
+ enum isl_dim_type type, unsigned pos)
+{
+ isl_id *id;
+
+ if (!space)
+ return -1;
+ id = get_id(space, type, pos);
+ return id && id->name;
+}
+
__isl_keep const char *isl_space_get_dim_name(__isl_keep isl_space *dim,
enum isl_dim_type type, unsigned pos)
{
return -1;
}
+int isl_space_find_dim_by_name(__isl_keep isl_space *space,
+ enum isl_dim_type type, const char *name)
+{
+ int i;
+ int offset;
+ int n;
+
+ if (!space || !name)
+ return -1;
+
+ offset = isl_space_offset(space, type);
+ n = isl_space_dim(space, type);
+ for (i = 0; i < n && offset + i < space->n_id; ++i)
+ if (space->ids[offset + i]->name &&
+ !strcmp(space->ids[offset + i]->name, name))
+ return i;
+
+ return -1;
+}
+
static __isl_keep isl_id *tuple_id(__isl_keep isl_space *dim,
enum isl_dim_type type)
{
}
}
+/* Insert "n" dimensions of type "type" at position "pos".
+ * If we are inserting parameters, then they are also inserted in
+ * any nested spaces.
+ */
__isl_give isl_space *isl_space_insert_dims(__isl_take isl_space *dim,
enum isl_dim_type type, unsigned pos, unsigned n)
{
return NULL;
if (dim->ids) {
- enum isl_dim_type t;
+ enum isl_dim_type t, o = isl_dim_param;
int off;
int s[3];
- int *size = s - isl_dim_param;
ids = isl_calloc_array(dim->ctx, isl_id *,
dim->nparam + dim->n_in + dim->n_out + n);
if (!ids)
goto error;
off = 0;
- size[isl_dim_param] = dim->nparam;
- size[isl_dim_in] = dim->n_in;
- size[isl_dim_out] = dim->n_out;
+ s[isl_dim_param - o] = dim->nparam;
+ s[isl_dim_in - o] = dim->n_in;
+ s[isl_dim_out - o] = dim->n_out;
for (t = isl_dim_param; t <= isl_dim_out; ++t) {
if (t != type) {
- get_ids(dim, t, 0, size[t], ids + off);
- off += size[t];
+ get_ids(dim, t, 0, s[t - o], ids + off);
+ off += s[t - o];
} else {
get_ids(dim, t, 0, pos, ids + off);
off += pos + n;
- get_ids(dim, t, pos, size[t] - pos, ids + off);
- off += size[t] - pos;
+ get_ids(dim, t, pos, s[t - o] - pos, ids + off);
+ off += s[t - o] - pos;
}
}
free(dim->ids);
}
dim = isl_space_reset(dim, type);
+ if (type == isl_dim_param) {
+ if (dim && dim->nested[0] &&
+ !(dim->nested[0] = isl_space_insert_dims(dim->nested[0],
+ isl_dim_param, pos, n)))
+ goto error;
+ if (dim && dim->nested[1] &&
+ !(dim->nested[1] = isl_space_insert_dims(dim->nested[1],
+ isl_dim_param, pos, n)))
+ goto error;
+ }
+
return dim;
error:
isl_space_free(dim);
if (dim->ids) {
isl_id **ids;
- enum isl_dim_type t;
+ enum isl_dim_type t, o = isl_dim_param;
int off;
int s[3];
- int *size = s - isl_dim_param;
ids = isl_calloc_array(dim->ctx, isl_id *,
dim->nparam + dim->n_in + dim->n_out);
if (!ids)
goto error;
off = 0;
- size[isl_dim_param] = dim->nparam;
- size[isl_dim_in] = dim->n_in;
- size[isl_dim_out] = dim->n_out;
+ s[isl_dim_param - o] = dim->nparam;
+ s[isl_dim_in - o] = dim->n_in;
+ s[isl_dim_out - o] = dim->n_out;
for (t = isl_dim_param; t <= isl_dim_out; ++t) {
if (t == dst_type) {
get_ids(dim, t, 0, dst_pos, ids + off);
off += dst_pos;
get_ids(dim, src_type, src_pos, n, ids + off);
off += n;
- get_ids(dim, t, dst_pos, size[t] - dst_pos,
+ get_ids(dim, t, dst_pos, s[t - o] - dst_pos,
ids + off);
- off += size[t] - dst_pos;
+ off += s[t - o] - dst_pos;
} else if (t == src_type) {
get_ids(dim, t, 0, src_pos, ids + off);
off += src_pos;
get_ids(dim, t, src_pos + n,
- size[t] - src_pos - n, ids + off);
- off += size[t] - src_pos - n;
+ s[t - o] - src_pos - n, ids + off);
+ off += s[t - o] - src_pos - n;
} else {
- get_ids(dim, t, 0, size[t], ids + off);
- off += size[t];
+ get_ids(dim, t, 0, s[t - o], ids + off);
+ off += s[t - o];
}
}
free(dim->ids);
return NULL;
}
+__isl_give isl_space *isl_space_map_from_domain_and_range(
+ __isl_take isl_space *domain, __isl_take isl_space *range)
+{
+ if (!domain || !range)
+ goto error;
+ if (!isl_space_is_set(domain))
+ isl_die(isl_space_get_ctx(domain), isl_error_invalid,
+ "domain is not a set space", goto error);
+ if (!isl_space_is_set(range))
+ isl_die(isl_space_get_ctx(range), isl_error_invalid,
+ "range is not a set space", goto error);
+ return isl_space_join(isl_space_reverse(domain), range);
+error:
+ isl_space_free(domain);
+ isl_space_free(range);
+ return NULL;
+}
+
static __isl_give isl_space *set_ids(__isl_take isl_space *dim,
enum isl_dim_type type,
unsigned first, unsigned n, __isl_take isl_id **ids)
return space;
}
+__isl_give isl_space *isl_space_set_from_params(__isl_take isl_space *space)
+{
+ if (!space)
+ return NULL;
+ if (!isl_space_is_params(space))
+ isl_die(isl_space_get_ctx(space), isl_error_invalid,
+ "not a parameter space", goto error);
+ return isl_space_reset(space, isl_dim_set);
+error:
+ isl_space_free(space);
+ return NULL;
+}
+
__isl_give isl_space *isl_space_as_set_space(__isl_take isl_space *dim)
{
dim = isl_space_cow(dim);
return NULL;
if (!isl_space_is_wrapping(dim))
- isl_die(dim->ctx, isl_error_invalid, "not a wrapping dim",
+ isl_die(dim->ctx, isl_error_invalid, "not a wrapping space",
goto error);
unwrap = isl_space_copy(dim->nested[1]);
goto error);
if (!dim)
- return 0;
+ return NULL;
dom = isl_space_unwrap(isl_space_domain(isl_space_copy(dim)));
ran = isl_space_unwrap(isl_space_range(dim));
dom_dom = isl_space_domain(isl_space_copy(dom));
return NULL;
}
+/* Can we apply isl_space_curry to "space"?
+ * That is, does it have a nested relation in its domain?
+ */
+int isl_space_can_curry(__isl_keep isl_space *space)
+{
+ if (!space)
+ return -1;
+
+ return !!space->nested[0];
+}
+
+/* Given a space (A -> B) -> C, return the corresponding space
+ * A -> (B -> C).
+ */
+__isl_give isl_space *isl_space_curry(__isl_take isl_space *space)
+{
+ isl_space *dom, *ran;
+ isl_space *dom_dom, *dom_ran;
+
+ if (!space)
+ return NULL;
+
+ if (!isl_space_can_curry(space))
+ isl_die(space->ctx, isl_error_invalid,
+ "space cannot be curried", goto error);
+
+ dom = isl_space_unwrap(isl_space_domain(isl_space_copy(space)));
+ ran = isl_space_range(space);
+ dom_dom = isl_space_domain(isl_space_copy(dom));
+ dom_ran = isl_space_range(dom);
+ ran = isl_space_join(isl_space_from_domain(dom_ran),
+ isl_space_from_range(ran));
+ return isl_space_join(isl_space_from_domain(dom_dom),
+ isl_space_from_range(isl_space_wrap(ran)));
+error:
+ isl_space_free(space);
+ return NULL;
+}
+
int isl_space_has_named_params(__isl_keep isl_space *dim)
{
int i;
isl_space_free(dim2);
return NULL;
}
+
+/* Given the space of set (domain), construct a space for a map
+ * with as domain the given space and as range the range of "model".
+ */
+__isl_give isl_space *isl_space_extend_domain_with_range(
+ __isl_take isl_space *space, __isl_take isl_space *model)
+{
+ if (!model)
+ goto error;
+
+ space = isl_space_from_domain(space);
+ space = isl_space_add_dims(space, isl_dim_out,
+ isl_space_dim(model, isl_dim_out));
+ if (isl_space_has_tuple_id(model, isl_dim_out))
+ space = isl_space_set_tuple_id(space, isl_dim_out,
+ isl_space_get_tuple_id(model, isl_dim_out));
+ if (!space)
+ goto error;
+ if (model->nested[1]) {
+ isl_space *nested = isl_space_copy(model->nested[1]);
+ int n_nested, n_space;
+ nested = isl_space_align_params(nested, isl_space_copy(space));
+ n_nested = isl_space_dim(nested, isl_dim_param);
+ n_space = isl_space_dim(space, isl_dim_param);
+ if (n_nested > n_space)
+ nested = isl_space_drop_dims(nested, isl_dim_param,
+ n_space, n_nested - n_space);
+ if (!nested)
+ goto error;
+ space->nested[1] = nested;
+ }
+ isl_space_free(model);
+ return space;
+error:
+ isl_space_free(model);
+ isl_space_free(space);
+ return NULL;
+}