#include <string.h>
#include <strings.h>
-#include "isl_ctx.h"
-#include "isl_blk.h"
+#include <isl/ctx.h>
+#include <isl/blk.h>
#include "isl_dim_private.h"
#include "isl_equalities.h"
-#include "isl_list.h"
-#include "isl_lp.h"
-#include "isl_seq.h"
-#include "isl_set.h"
-#include "isl_map.h"
+#include <isl/list.h>
+#include <isl/lp.h>
+#include <isl/seq.h>
+#include <isl/set.h>
+#include <isl/map.h>
#include "isl_map_private.h"
#include "isl_map_piplib.h"
+#include <isl_reordering.h>
#include "isl_sample.h"
#include "isl_tab.h"
-#include "isl_vec.h"
+#include <isl/vec.h>
+#include <isl_mat_private.h>
/* Maps dst positions to src positions */
struct isl_dim_map {
if (!bmap)
return 0;
switch (type) {
+ case isl_dim_cst: return 1;
case isl_dim_param:
case isl_dim_in:
case isl_dim_out: return isl_dim_size(bmap->dim, type);
{
struct isl_dim *dim = bmap->dim;
switch (type) {
+ case isl_dim_cst: return 0;
case isl_dim_param: return 1;
case isl_dim_in: return 1 + dim->nparam;
case isl_dim_out: return 1 + dim->nparam + dim->n_in;
unsigned isl_basic_map_n_in(const struct isl_basic_map *bmap)
{
- return bmap->dim->n_in;
+ return bmap ? bmap->dim->n_in : 0;
}
unsigned isl_basic_map_n_out(const struct isl_basic_map *bmap)
{
- return bmap->dim->n_out;
+ return bmap ? bmap->dim->n_out : 0;
}
unsigned isl_basic_map_n_param(const struct isl_basic_map *bmap)
{
- return bmap->dim->nparam;
+ return bmap ? bmap->dim->nparam : 0;
}
unsigned isl_basic_map_n_div(const struct isl_basic_map *bmap)
{
- return bmap->n_div;
+ return bmap ? bmap->n_div : 0;
}
unsigned isl_basic_map_total_dim(const struct isl_basic_map *bmap)
int isl_map_compatible_domain(struct isl_map *map, struct isl_set *set)
{
- return map->dim->n_in == set->dim->n_out &&
- map->dim->nparam == set->dim->nparam;
+ int m;
+ if (!map || !set)
+ return -1;
+ m = isl_dim_match(map->dim, isl_dim_param, set->dim, isl_dim_param);
+ if (m < 0 || !m)
+ return m;
+ return isl_dim_tuple_match(map->dim, isl_dim_in, set->dim, isl_dim_set);
}
int isl_basic_map_compatible_domain(struct isl_basic_map *bmap,
struct isl_basic_set *bset)
{
- return bmap->dim->n_in == bset->dim->n_out &&
- bmap->dim->nparam == bset->dim->nparam;
+ int m;
+ if (!bmap || !bset)
+ return -1;
+ m = isl_dim_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
+ if (m < 0 || !m)
+ return m;
+ return isl_dim_tuple_match(bmap->dim, isl_dim_in, bset->dim, isl_dim_set);
}
int isl_basic_map_compatible_range(struct isl_basic_map *bmap,
struct isl_basic_set *bset)
{
- return bmap->dim->n_out == bset->dim->n_out &&
- bmap->dim->nparam == bset->dim->nparam;
+ int m;
+ if (!bmap || !bset)
+ return -1;
+ m = isl_dim_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
+ if (m < 0 || !m)
+ return m;
+ return isl_dim_tuple_match(bmap->dim, isl_dim_out, bset->dim, isl_dim_set);
+}
+
+isl_ctx *isl_basic_map_get_ctx(__isl_keep isl_basic_map *bmap)
+{
+ return bmap ? bmap->ctx : NULL;
+}
+
+isl_ctx *isl_basic_set_get_ctx(__isl_keep isl_basic_set *bset)
+{
+ return bset ? bset->ctx : NULL;
+}
+
+isl_ctx *isl_map_get_ctx(__isl_keep isl_map *map)
+{
+ return map ? map->ctx : NULL;
+}
+
+isl_ctx *isl_set_get_ctx(__isl_keep isl_set *set)
+{
+ return set ? set->ctx : NULL;
}
struct isl_dim *isl_basic_map_get_dim(struct isl_basic_map *bmap)
return isl_dim_copy(set->dim);
}
+__isl_give isl_basic_map *isl_basic_map_set_tuple_name(
+ __isl_take isl_basic_map *bmap, enum isl_dim_type type, const char *s)
+{
+ bmap = isl_basic_map_cow(bmap);
+ if (!bmap)
+ return NULL;
+ bmap->dim = isl_dim_set_tuple_name(bmap->dim, type, s);
+ if (!bmap->dim)
+ goto error;
+ return bmap;
+error:
+ isl_basic_map_free(bmap);
+ return NULL;
+}
+
+const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map *bmap,
+ enum isl_dim_type type)
+{
+ return bmap ? isl_dim_get_tuple_name(bmap->dim, type) : NULL;
+}
+
+__isl_give isl_map *isl_map_set_tuple_name(__isl_take isl_map *map,
+ enum isl_dim_type type, const char *s)
+{
+ int i;
+
+ map = isl_map_cow(map);
+ if (!map)
+ return NULL;
+
+ map->dim = isl_dim_set_tuple_name(map->dim, type, s);
+ if (!map->dim)
+ goto error;
+
+ for (i = 0; i < map->n; ++i) {
+ map->p[i] = isl_basic_map_set_tuple_name(map->p[i], type, s);
+ if (!map->p[i])
+ goto error;
+ }
+
+ return map;
+error:
+ isl_map_free(map);
+ return NULL;
+}
+
+const char *isl_map_get_tuple_name(__isl_keep isl_map *map,
+ enum isl_dim_type type)
+{
+ return map ? isl_dim_get_tuple_name(map->dim, type) : NULL;
+}
+
+__isl_give isl_set *isl_set_set_tuple_name(__isl_take isl_set *set,
+ const char *s)
+{
+ return (isl_set *)isl_map_set_tuple_name((isl_map *)set, isl_dim_set, s);
+}
+
+const char *isl_set_get_tuple_name(__isl_keep isl_set *set)
+{
+ return set ? isl_dim_get_tuple_name(set->dim, isl_dim_set) : NULL;
+}
+
+const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map *bmap,
+ enum isl_dim_type type, unsigned pos)
+{
+ return bmap ? isl_dim_get_name(bmap->dim, type, pos) : NULL;
+}
+
+const char *isl_map_get_dim_name(__isl_keep isl_map *map,
+ enum isl_dim_type type, unsigned pos)
+{
+ return map ? isl_dim_get_name(map->dim, type, pos) : NULL;
+}
+
+const char *isl_set_get_dim_name(__isl_keep isl_set *set,
+ enum isl_dim_type type, unsigned pos)
+{
+ return set ? isl_dim_get_name(set->dim, type, pos) : NULL;
+}
+
__isl_give isl_basic_map *isl_basic_map_set_dim_name(
__isl_take isl_basic_map *bmap,
enum isl_dim_type type, unsigned pos, const char *s)
struct isl_basic_map *bmap;
if (!dim)
return NULL;
- isl_assert(dim->ctx, dim->n_in == 0, return NULL);
+ isl_assert(dim->ctx, dim->n_in == 0, goto error);
bmap = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
return (struct isl_basic_set *)bmap;
+error:
+ isl_dim_free(dim);
+ return NULL;
}
struct isl_basic_map *isl_basic_map_alloc_dim(struct isl_dim *dim,
isl_blk_free(bset->ctx, blk);
ISL_F_CLR(bset, ISL_BASIC_SET_NORMALIZED);
- return isl_basic_set_gauss(bset, NULL);
+ bset = isl_basic_set_gauss(bset, NULL);
+ return isl_basic_set_finalize(bset);
error:
isl_basic_set_free(bset);
return NULL;
return isl_set_eliminate(set, isl_dim_set, first, n);
}
-/* Project out n dimensions starting at first using Fourier-Motzkin */
-struct isl_set *isl_set_remove_dims(struct isl_set *set,
- unsigned first, unsigned n)
+__isl_give isl_basic_map *isl_basic_map_remove_divs(
+ __isl_take isl_basic_map *bmap)
{
- set = isl_set_eliminate_dims(set, first, n);
- set = isl_set_drop_dims(set, first, n);
- return set;
+ bmap = isl_basic_map_eliminate_vars(bmap, isl_dim_total(bmap->dim),
+ bmap->n_div);
+ if (!bmap)
+ return NULL;
+ bmap->n_div = 0;
+ return bmap;
}
-struct isl_basic_set *isl_basic_set_remove_divs(struct isl_basic_set *bset)
+__isl_give isl_basic_set *isl_basic_set_remove_divs(
+ __isl_take isl_basic_set *bset)
{
- bset = isl_basic_set_eliminate_vars(bset, isl_dim_total(bset->dim),
- bset->n_div);
- if (!bset)
- return NULL;
- bset->n_div = 0;
- return bset;
+ return (struct isl_basic_set *)isl_basic_map_remove_divs(
+ (struct isl_basic_map *)bset);
}
struct isl_set *isl_set_remove_divs(struct isl_set *set)
return NULL;
}
-struct isl_basic_map *isl_basic_map_remove(struct isl_basic_map *bmap,
+struct isl_basic_map *isl_basic_map_remove_dims(struct isl_basic_map *bmap,
enum isl_dim_type type, unsigned first, unsigned n)
{
if (!bmap)
return NULL;
}
-__isl_give isl_basic_set *isl_basic_set_remove(__isl_take isl_basic_set *bset,
+/* Return true if the definition of the given div (recursively) involves
+ * any of the given variables.
+ */
+static int div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
+ unsigned first, unsigned n)
+{
+ int i;
+ unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
+
+ if (isl_int_is_zero(bmap->div[div][0]))
+ return 0;
+ if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
+ return 1;
+
+ for (i = bmap->n_div - 1; i >= 0; --i) {
+ if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
+ continue;
+ if (div_involves_vars(bmap, i, first, n))
+ return 1;
+ }
+
+ return 0;
+}
+
+/* Remove all divs (recursively) involving any of the given dimensions
+ * in their definitions.
+ */
+__isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
+ __isl_take isl_basic_map *bmap,
+ enum isl_dim_type type, unsigned first, unsigned n)
+{
+ int i;
+
+ if (!bmap)
+ return NULL;
+ isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
+ goto error);
+ first += isl_basic_map_offset(bmap, type);
+
+ for (i = bmap->n_div - 1; i >= 0; --i) {
+ if (!div_involves_vars(bmap, i, first, n))
+ continue;
+ bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
+ }
+
+ return bmap;
+error:
+ isl_basic_map_free(bmap);
+ return NULL;
+}
+
+__isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
+ enum isl_dim_type type, unsigned first, unsigned n)
+{
+ int i;
+
+ if (!map)
+ return NULL;
+ if (map->n == 0)
+ return map;
+
+ map = isl_map_cow(map);
+ if (!map)
+ return NULL;
+
+ for (i = 0; i < map->n; ++i) {
+ map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
+ type, first, n);
+ if (!map->p[i])
+ goto error;
+ }
+ return map;
+error:
+ isl_map_free(map);
+ return NULL;
+}
+
+__isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
+ enum isl_dim_type type, unsigned first, unsigned n)
+{
+ return (isl_set *)isl_map_remove_divs_involving_dims((isl_map *)set,
+ type, first, n);
+}
+
+__isl_give isl_basic_set *isl_basic_set_remove_dims(
+ __isl_take isl_basic_set *bset,
enum isl_dim_type type, unsigned first, unsigned n)
{
return (isl_basic_set *)
- isl_basic_map_remove((isl_basic_map *)bset, type, first, n);
+ isl_basic_map_remove_dims((isl_basic_map *)bset, type, first, n);
}
-struct isl_map *isl_map_remove(struct isl_map *map,
+struct isl_map *isl_map_remove_dims(struct isl_map *map,
enum isl_dim_type type, unsigned first, unsigned n)
{
int i;
return NULL;
}
-__isl_give isl_set *isl_set_remove(__isl_take isl_set *bset,
+__isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
enum isl_dim_type type, unsigned first, unsigned n)
{
- return (isl_set *)isl_map_remove((isl_map *)bset, type, first, n);
+ return (isl_set *)isl_map_remove_dims((isl_map *)bset, type, first, n);
}
/* Project out n inputs starting at first using Fourier-Motzkin */
struct isl_map *isl_map_remove_inputs(struct isl_map *map,
unsigned first, unsigned n)
{
- return isl_map_remove(map, isl_dim_in, first, n);
-}
-
-/* Project out n dimensions starting at first using Fourier-Motzkin */
-struct isl_basic_set *isl_basic_set_remove_dims(struct isl_basic_set *bset,
- unsigned first, unsigned n)
-{
- unsigned nparam = isl_basic_set_n_param(bset);
- bset = isl_basic_set_eliminate_vars(bset, nparam + first, n);
- bset = isl_basic_set_drop_dims(bset, first, n);
- return bset;
+ return isl_map_remove_dims(map, isl_dim_in, first, n);
}
static void dump_term(struct isl_basic_map *bmap,
if (!dim)
return NULL;
- isl_assert(dim->ctx, dim->n_in == 0, return NULL);
- isl_assert(dim->ctx, n >= 0, return NULL);
+ isl_assert(dim->ctx, dim->n_in == 0, goto error);
+ isl_assert(dim->ctx, n >= 0, goto error);
set = isl_alloc(dim->ctx, struct isl_set,
sizeof(struct isl_set) +
(n - 1) * sizeof(struct isl_basic_set *));
return NULL;
}
-__isl_give isl_map *isl_map_add(__isl_take isl_map *map,
+__isl_give isl_set *isl_set_insert(__isl_take isl_set *set,
+ enum isl_dim_type type, unsigned pos, unsigned n)
+{
+ return (isl_set *)isl_map_insert((isl_map *)set, type, pos, n);
+}
+
+__isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
enum isl_dim_type type, unsigned n)
{
if (!map)
return isl_map_insert(map, type, isl_map_dim(map, type), n);
}
-__isl_give isl_set *isl_set_add(__isl_take isl_set *set,
+__isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
enum isl_dim_type type, unsigned n)
{
if (!set)
return NULL;
isl_assert(set->ctx, type != isl_dim_in, goto error);
- return (isl_set *)isl_map_add((isl_map *)set, type, n);
+ return (isl_set *)isl_map_add_dims((isl_map *)set, type, n);
error:
isl_set_free(set);
return NULL;
off += size;
}
}
- isl_dim_map_div(dim_map, bmap, off + n);
+ isl_dim_map_div(dim_map, bmap, off);
res = isl_basic_map_alloc_dim(isl_basic_map_get_dim(bmap),
bmap->n_div, bmap->n_eq, bmap->n_ineq);
return NULL;
if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
- return isl_basic_map_remove(bmap, type, first, n);
+ return isl_basic_map_remove_dims(bmap, type, first, n);
isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
goto error);
struct isl_map *map;
int i;
+ if (n == 0 && equal)
+ return isl_map_universe(dims);
+
map = isl_map_alloc_dim(isl_dim_copy(dims), n, ISL_MAP_DISJOINT);
for (i = 0; i + 1 < n; ++i)
struct isl_map *map;
int i;
+ if (n == 0 && equal)
+ return isl_map_universe(dims);
+
map = isl_map_alloc_dim(isl_dim_copy(dims), n, ISL_MAP_DISJOINT);
for (i = 0; i + 1 < n; ++i)
return map_lex_gte(isl_dim_map(set_dim), 1);
}
+__isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
+ __isl_take isl_set *set2)
+{
+ isl_map *map;
+ map = isl_map_lex_le(isl_set_get_dim(set1));
+ map = isl_map_intersect_domain(map, set1);
+ map = isl_map_intersect_range(map, set2);
+ return map;
+}
+
+__isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
+ __isl_take isl_set *set2)
+{
+ isl_map *map;
+ map = isl_map_lex_lt(isl_set_get_dim(set1));
+ map = isl_map_intersect_domain(map, set1);
+ map = isl_map_intersect_range(map, set2);
+ return map;
+}
+
+__isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
+ __isl_take isl_set *set2)
+{
+ isl_map *map;
+ map = isl_map_lex_ge(isl_set_get_dim(set1));
+ map = isl_map_intersect_domain(map, set1);
+ map = isl_map_intersect_range(map, set2);
+ return map;
+}
+
+__isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
+ __isl_take isl_set *set2)
+{
+ isl_map *map;
+ map = isl_map_lex_gt(isl_set_get_dim(set1));
+ map = isl_map_intersect_domain(map, set1);
+ map = isl_map_intersect_range(map, set2);
+ return map;
+}
+
+__isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
+ __isl_take isl_map *map2)
+{
+ isl_map *map;
+ map = isl_map_lex_le(isl_dim_range(isl_map_get_dim(map1)));
+ map = isl_map_apply_domain(map, isl_map_reverse(map1));
+ map = isl_map_apply_range(map, isl_map_reverse(map2));
+ return map;
+}
+
+__isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
+ __isl_take isl_map *map2)
+{
+ isl_map *map;
+ map = isl_map_lex_lt(isl_dim_range(isl_map_get_dim(map1)));
+ map = isl_map_apply_domain(map, isl_map_reverse(map1));
+ map = isl_map_apply_range(map, isl_map_reverse(map2));
+ return map;
+}
+
+__isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
+ __isl_take isl_map *map2)
+{
+ isl_map *map;
+ map = isl_map_lex_ge(isl_dim_range(isl_map_get_dim(map1)));
+ map = isl_map_apply_domain(map, isl_map_reverse(map1));
+ map = isl_map_apply_range(map, isl_map_reverse(map2));
+ return map;
+}
+
+__isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
+ __isl_take isl_map *map2)
+{
+ isl_map *map;
+ map = isl_map_lex_gt(isl_dim_range(isl_map_get_dim(map1)));
+ map = isl_map_apply_domain(map, isl_map_reverse(map1));
+ map = isl_map_apply_range(map, isl_map_reverse(map2));
+ return map;
+}
+
struct isl_basic_map *isl_basic_map_from_basic_set(
struct isl_basic_set *bset, struct isl_dim *dim)
{
bmap = isl_basic_map_cow(bmap);
if (!bmap)
goto error;
- bmap->dim = isl_dim_cow(bmap->dim);
+ bmap->dim = isl_dim_as_set_dim(bmap->dim);
if (!bmap->dim)
goto error;
- bmap->dim->n_out += bmap->dim->n_in;
- bmap->dim->n_in = 0;
bmap = isl_basic_map_finalize(bmap);
return (struct isl_basic_set *)bmap;
error:
{
if (!bmap)
goto error;
- if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 && bmap->n_div == 0)
+ if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
+ bmap->n_div == 0 &&
+ !isl_dim_is_named_or_nested(bmap->dim, isl_dim_in) &&
+ !isl_dim_is_named_or_nested(bmap->dim, isl_dim_out))
return (struct isl_basic_set *)bmap;
bmap = isl_basic_map_cow(bmap);
if (!bmap)
return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
}
-struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
+__isl_give isl_basic_map *isl_basic_map_reset_dim(
+ __isl_take isl_basic_map *bmap, __isl_take isl_dim *dim)
{
- struct isl_basic_set *domain;
- unsigned n_in;
- unsigned n_out;
- if (!bmap)
- return NULL;
- n_in = isl_basic_map_n_in(bmap);
- n_out = isl_basic_map_n_out(bmap);
- domain = isl_basic_set_from_basic_map(bmap);
- return isl_basic_set_project_out(domain, isl_dim_set, n_in, n_out);
+ bmap = isl_basic_map_cow(bmap);
+ if (!bmap || !dim)
+ goto error;
+
+ isl_dim_free(bmap->dim);
+ bmap->dim = dim;
+
+ bmap = isl_basic_map_finalize(bmap);
+
+ return bmap;
+error:
+ isl_basic_map_free(bmap);
+ isl_dim_free(dim);
+ return NULL;
}
-struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
+static __isl_give isl_basic_set *isl_basic_set_reset_dim(
+ __isl_take isl_basic_set *bset, __isl_take isl_dim *dim)
{
- return isl_basic_map_domain(isl_basic_map_reverse(bmap));
+ return (isl_basic_set *)isl_basic_map_reset_dim((isl_basic_map *)bset,
+ dim);
}
-struct isl_set *isl_map_range(struct isl_map *map)
+__isl_give isl_map *isl_map_reset_dim(__isl_take isl_map *map,
+ __isl_take isl_dim *dim)
{
int i;
- struct isl_set *set;
-
- if (!map)
- goto error;
- if (isl_map_dim(map, isl_dim_in) == 0)
- return (isl_set *)map;
map = isl_map_cow(map);
- if (!map)
+ if (!map || !dim)
goto error;
- set = (struct isl_set *) map;
- set->dim = isl_dim_drop_inputs(set->dim, 0, set->dim->n_in);
- if (!set->dim)
- goto error;
for (i = 0; i < map->n; ++i) {
- set->p[i] = isl_basic_map_range(map->p[i]);
- if (!set->p[i])
+ map->p[i] = isl_basic_map_reset_dim(map->p[i],
+ isl_dim_copy(dim));
+ if (!map->p[i])
goto error;
}
- ISL_F_CLR(set, ISL_MAP_DISJOINT);
- ISL_F_CLR(set, ISL_SET_NORMALIZED);
- return set;
+ isl_dim_free(map->dim);
+ map->dim = dim;
+
+ return map;
error:
isl_map_free(map);
+ isl_dim_free(dim);
return NULL;
}
-struct isl_map *isl_map_from_set(struct isl_set *set, struct isl_dim *dim)
+__isl_give isl_set *isl_set_reset_dim(__isl_take isl_set *set,
+ __isl_take isl_dim *dim)
{
- int i;
- struct isl_map *map = NULL;
-
+ return (struct isl_set *) isl_map_reset_dim((struct isl_map *)set, dim);
+}
+
+struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
+{
+ isl_dim *dim;
+ struct isl_basic_set *domain;
+ unsigned n_in;
+ unsigned n_out;
+
+ if (!bmap)
+ return NULL;
+ dim = isl_dim_domain(isl_basic_map_get_dim(bmap));
+
+ n_in = isl_basic_map_n_in(bmap);
+ n_out = isl_basic_map_n_out(bmap);
+ domain = isl_basic_set_from_basic_map(bmap);
+ domain = isl_basic_set_project_out(domain, isl_dim_set, n_in, n_out);
+
+ domain = isl_basic_set_reset_dim(domain, dim);
+
+ return domain;
+}
+
+struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
+{
+ return isl_basic_map_domain(isl_basic_map_reverse(bmap));
+}
+
+__isl_give isl_basic_map *isl_basic_map_domain_map(
+ __isl_take isl_basic_map *bmap)
+{
+ int i, k;
+ isl_dim *dim;
+ isl_basic_map *domain;
+ isl_basic_set *bset;
+ int nparam, n_in, n_out;
+ unsigned total;
+
+ nparam = isl_basic_map_dim(bmap, isl_dim_param);
+ n_in = isl_basic_map_dim(bmap, isl_dim_in);
+ n_out = isl_basic_map_dim(bmap, isl_dim_out);
+
+ dim = isl_dim_from_range(isl_dim_domain(isl_basic_map_get_dim(bmap)));
+ domain = isl_basic_map_universe(dim);
+
+ bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
+ bmap = isl_basic_map_apply_range(bmap, domain);
+ bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
+
+ total = isl_basic_map_total_dim(bmap);
+
+ for (i = 0; i < n_in; ++i) {
+ k = isl_basic_map_alloc_equality(bmap);
+ if (k < 0)
+ goto error;
+ isl_seq_clr(bmap->eq[k], 1 + total);
+ isl_int_set_si(bmap->eq[k][1 + nparam + i], -1);
+ isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
+ }
+
+ bmap = isl_basic_map_gauss(bmap, NULL);
+ return isl_basic_map_finalize(bmap);
+error:
+ isl_basic_map_free(bmap);
+ return NULL;
+}
+
+__isl_give isl_basic_map *isl_basic_map_range_map(
+ __isl_take isl_basic_map *bmap)
+{
+ int i, k;
+ isl_dim *dim;
+ isl_basic_map *range;
+ isl_basic_set *bset;
+ int nparam, n_in, n_out;
+ unsigned total;
+
+ nparam = isl_basic_map_dim(bmap, isl_dim_param);
+ n_in = isl_basic_map_dim(bmap, isl_dim_in);
+ n_out = isl_basic_map_dim(bmap, isl_dim_out);
+
+ dim = isl_dim_from_range(isl_dim_range(isl_basic_map_get_dim(bmap)));
+ range = isl_basic_map_universe(dim);
+
+ bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
+ bmap = isl_basic_map_apply_range(bmap, range);
+ bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
+
+ total = isl_basic_map_total_dim(bmap);
+
+ for (i = 0; i < n_out; ++i) {
+ k = isl_basic_map_alloc_equality(bmap);
+ if (k < 0)
+ goto error;
+ isl_seq_clr(bmap->eq[k], 1 + total);
+ isl_int_set_si(bmap->eq[k][1 + nparam + n_in + i], -1);
+ isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
+ }
+
+ bmap = isl_basic_map_gauss(bmap, NULL);
+ return isl_basic_map_finalize(bmap);
+error:
+ isl_basic_map_free(bmap);
+ return NULL;
+}
+
+struct isl_set *isl_map_range(struct isl_map *map)
+{
+ int i;
+ struct isl_set *set;
+
+ if (!map)
+ goto error;
+ if (isl_map_dim(map, isl_dim_in) == 0)
+ return (isl_set *)map;
+
+ map = isl_map_cow(map);
+ if (!map)
+ goto error;
+
+ set = (struct isl_set *) map;
+ set->dim = isl_dim_drop_inputs(set->dim, 0, set->dim->n_in);
+ if (!set->dim)
+ goto error;
+ for (i = 0; i < map->n; ++i) {
+ set->p[i] = isl_basic_map_range(map->p[i]);
+ if (!set->p[i])
+ goto error;
+ }
+ ISL_F_CLR(set, ISL_MAP_DISJOINT);
+ ISL_F_CLR(set, ISL_SET_NORMALIZED);
+ return set;
+error:
+ isl_map_free(map);
+ return NULL;
+}
+
+__isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
+{
+ int i;
+ isl_dim *domain_dim;
+
+ map = isl_map_cow(map);
+ if (!map)
+ return NULL;
+
+ domain_dim = isl_dim_from_range(isl_dim_domain(isl_map_get_dim(map)));
+ map->dim = isl_dim_from_domain(isl_dim_wrap(map->dim));
+ map->dim = isl_dim_join(map->dim, domain_dim);
+ if (!map->dim)
+ goto error;
+ for (i = 0; i < map->n; ++i) {
+ map->p[i] = isl_basic_map_domain_map(map->p[i]);
+ if (!map->p[i])
+ goto error;
+ }
+ ISL_F_CLR(map, ISL_MAP_DISJOINT);
+ ISL_F_CLR(map, ISL_MAP_NORMALIZED);
+ return map;
+error:
+ isl_map_free(map);
+ return NULL;
+}
+
+__isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
+{
+ int i;
+ isl_dim *range_dim;
+
+ map = isl_map_cow(map);
+ if (!map)
+ return NULL;
+
+ range_dim = isl_dim_range(isl_map_get_dim(map));
+ map->dim = isl_dim_from_domain(isl_dim_wrap(map->dim));
+ map->dim = isl_dim_join(map->dim, range_dim);
+ if (!map->dim)
+ goto error;
+ for (i = 0; i < map->n; ++i) {
+ map->p[i] = isl_basic_map_range_map(map->p[i]);
+ if (!map->p[i])
+ goto error;
+ }
+ ISL_F_CLR(map, ISL_MAP_DISJOINT);
+ ISL_F_CLR(map, ISL_MAP_NORMALIZED);
+ return map;
+error:
+ isl_map_free(map);
+ return NULL;
+}
+
+struct isl_map *isl_map_from_set(struct isl_set *set, struct isl_dim *dim)
+{
+ int i;
+ struct isl_map *map = NULL;
+
set = isl_set_cow(set);
if (!set || !dim)
goto error;
return NULL;
}
+__isl_give isl_basic_map *isl_basic_map_from_domain(
+ __isl_take isl_basic_set *bset)
+{
+ return isl_basic_map_reverse(isl_basic_map_from_range(bset));
+}
+
+__isl_give isl_basic_map *isl_basic_map_from_range(
+ __isl_take isl_basic_set *bset)
+{
+ return (isl_basic_map *)bset;
+}
+
struct isl_map *isl_map_from_range(struct isl_set *set)
{
return (struct isl_map *)set;
__isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
{
- return isl_map_reverse(isl_map_from_range(set));;
+ return isl_map_reverse(isl_map_from_range(set));
}
__isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
__isl_take isl_set *range)
{
- return isl_map_product(isl_map_from_domain(domain),
- isl_map_from_range(range));
+ return isl_map_apply_range(isl_map_from_domain(domain),
+ isl_map_from_range(range));
}
struct isl_set *isl_set_from_map(struct isl_map *map)
map = isl_map_cow(map);
if (!map)
return NULL;
- map->dim = isl_dim_cow(map->dim);
+ map->dim = isl_dim_as_set_dim(map->dim);
if (!map->dim)
goto error;
- map->dim->n_out += map->dim->n_in;
- map->dim->n_in = 0;
set = (struct isl_set *)map;
for (i = 0; i < map->n; ++i) {
set->p[i] = isl_basic_set_from_basic_map(map->p[i]);
return (isl_set *)isl_map_lexmax((isl_map *)set);
}
-static struct isl_map *isl_map_reset_dim(struct isl_map *map,
- struct isl_dim *dim)
-{
- int i;
-
- if (!map || !dim)
- goto error;
-
- for (i = 0; i < map->n; ++i) {
- isl_dim_free(map->p[i]->dim);
- map->p[i]->dim = isl_dim_copy(dim);
- }
- isl_dim_free(map->dim);
- map->dim = dim;
-
- return map;
-error:
- isl_map_free(map);
- isl_dim_free(dim);
- return NULL;
-}
-
-static struct isl_set *isl_set_reset_dim(struct isl_set *set,
- struct isl_dim *dim)
-{
- return (struct isl_set *) isl_map_reset_dim((struct isl_map *)set, dim);
-}
-
/* Apply a preimage specified by "mat" on the parameters of "bset".
* bset is assumed to have only parameters and divs.
*/
*/
struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
{
- isl_dim *dims;
+ isl_dim *dims, *target_dim;
struct isl_basic_set *bset;
unsigned dim;
unsigned nparam;
if (!bmap)
goto error;
+ isl_assert(bmap->ctx, isl_dim_tuple_match(bmap->dim, isl_dim_in,
+ bmap->dim, isl_dim_out),
+ goto error);
+ target_dim = isl_dim_domain(isl_basic_map_get_dim(bmap));
dim = isl_basic_map_n_in(bmap);
nparam = isl_basic_map_n_param(bmap);
- isl_assert(bmap->ctx, dim == isl_basic_map_n_out(bmap), goto error);
bset = isl_basic_set_from_basic_map(bmap);
bset = isl_basic_set_cow(bset);
dims = isl_basic_set_get_dim(bset);
isl_int_set_si(bset->eq[j][1+nparam+dim+i], 1);
isl_int_set_si(bset->eq[j][1+nparam+2*dim+i], -1);
}
- return isl_basic_set_project_out(bset, isl_dim_set, dim, 2*dim);
+ bset = isl_basic_set_project_out(bset, isl_dim_set, dim, 2*dim);
+ bset = isl_basic_set_reset_dim(bset, target_dim);
+ return bset;
error:
isl_basic_map_free(bmap);
return NULL;
if (!map)
return NULL;
- isl_assert(map->ctx, isl_map_n_in(map) == isl_map_n_out(map), goto error);
+ isl_assert(map->ctx, isl_dim_tuple_match(map->dim, isl_dim_in,
+ map->dim, isl_dim_out),
+ goto error);
dim = isl_map_get_dim(map);
dim = isl_dim_domain(dim);
result = isl_set_alloc_dim(dim, map->n, map->flags);
return map_identity(isl_dim_copy(model->dim));
}
+__isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
+{
+ isl_dim *dim = isl_set_get_dim(set);
+ isl_map *id;
+ id = isl_map_identity(dim);
+ return isl_map_intersect_range(id, set);
+}
+
/* Construct a basic set with all set dimensions having only non-negative
* values.
*/
return NULL;
}
+/* Construct the half-space x_pos >= 0.
+ */
+static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_dim *dim,
+ int pos)
+{
+ int k;
+ isl_basic_set *nonneg;
+
+ nonneg = isl_basic_set_alloc_dim(dim, 0, 0, 1);
+ k = isl_basic_set_alloc_inequality(nonneg);
+ if (k < 0)
+ goto error;
+ isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
+ isl_int_set_si(nonneg->ineq[k][pos], 1);
+
+ return isl_basic_set_finalize(nonneg);
+error:
+ isl_basic_set_free(nonneg);
+ return NULL;
+}
+
+/* Construct the half-space x_pos <= -1.
+ */
+static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_dim *dim, int pos)
+{
+ int k;
+ isl_basic_set *neg;
+
+ neg = isl_basic_set_alloc_dim(dim, 0, 0, 1);
+ k = isl_basic_set_alloc_inequality(neg);
+ if (k < 0)
+ goto error;
+ isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
+ isl_int_set_si(neg->ineq[k][0], -1);
+ isl_int_set_si(neg->ineq[k][pos], -1);
+
+ return isl_basic_set_finalize(neg);
+error:
+ isl_basic_set_free(neg);
+ return NULL;
+}
+
__isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
enum isl_dim_type type, unsigned first, unsigned n)
{
int i;
- isl_basic_set *nonneg = NULL;
- isl_basic_set *neg = NULL;
+ isl_basic_set *nonneg;
+ isl_basic_set *neg;
if (!set)
return NULL;
isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
for (i = 0; i < n; ++i) {
- int k;
-
- neg = NULL;
- nonneg = isl_basic_set_alloc_dim(isl_set_get_dim(set), 0, 0, 1);
- k = isl_basic_set_alloc_inequality(nonneg);
- if (k < 0)
- goto error;
- isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
- isl_int_set_si(nonneg->ineq[k][pos(set->dim, type) + first + i], 1);
-
- neg = isl_basic_set_alloc_dim(isl_set_get_dim(set), 0, 0, 1);
- k = isl_basic_set_alloc_inequality(neg);
- if (k < 0)
- goto error;
- isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
- isl_int_set_si(neg->ineq[k][0], -1);
- isl_int_set_si(neg->ineq[k][pos(set->dim, type) + first + i], -1);
+ nonneg = nonneg_halfspace(isl_set_get_dim(set),
+ pos(set->dim, type) + first + i);
+ neg = neg_halfspace(isl_set_get_dim(set),
+ pos(set->dim, type) + first + i);
set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
}
return set;
error:
- isl_basic_set_free(nonneg);
- isl_basic_set_free(neg);
isl_set_free(set);
return NULL;
}
+static int foreach_orthant(__isl_take isl_set *set, int *signs, int first,
+ int len, int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
+ void *user)
+{
+ isl_set *half;
+
+ if (!set)
+ return -1;
+ if (isl_set_fast_is_empty(set)) {
+ isl_set_free(set);
+ return 0;
+ }
+ if (first == len)
+ return fn(set, signs, user);
+
+ signs[first] = 1;
+ half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_dim(set),
+ 1 + first));
+ half = isl_set_intersect(half, isl_set_copy(set));
+ if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
+ goto error;
+
+ signs[first] = -1;
+ half = isl_set_from_basic_set(neg_halfspace(isl_set_get_dim(set),
+ 1 + first));
+ half = isl_set_intersect(half, set);
+ return foreach_orthant(half, signs, first + 1, len, fn, user);
+error:
+ isl_set_free(set);
+ return -1;
+}
+
+/* Call "fn" on the intersections of "set" with each of the orthants
+ * (except for obviously empty intersections). The orthant is identified
+ * by the signs array, with each entry having value 1 or -1 according
+ * to the sign of the corresponding variable.
+ */
+int isl_set_foreach_orthant(__isl_keep isl_set *set,
+ int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
+ void *user)
+{
+ unsigned nparam;
+ unsigned nvar;
+ int *signs;
+ int r;
+
+ if (!set)
+ return -1;
+ if (isl_set_fast_is_empty(set))
+ return 0;
+
+ nparam = isl_set_dim(set, isl_dim_param);
+ nvar = isl_set_dim(set, isl_dim_set);
+
+ signs = isl_alloc_array(set->ctx, int, nparam + nvar);
+
+ r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
+ fn, user);
+
+ free(signs);
+
+ return r;
+}
+
int isl_set_is_equal(struct isl_set *set1, struct isl_set *set2)
{
return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
return isl_map_is_empty((struct isl_map *)set);
}
-int isl_map_is_equal(struct isl_map *map1, struct isl_map *map2)
+int isl_map_has_equal_dim(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
{
- int is_subset;
-
if (!map1 || !map2)
return -1;
- is_subset = isl_map_is_subset(map1, map2);
- if (is_subset != 1)
+
+ return isl_dim_equal(map1->dim, map2->dim);
+}
+
+int isl_set_has_equal_dim(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
+{
+ if (!set1 || !set2)
+ return -1;
+
+ return isl_dim_equal(set1->dim, set2->dim);
+}
+
+int isl_map_is_equal(struct isl_map *map1, struct isl_map *map2)
+{
+ int is_subset;
+
+ if (!map1 || !map2)
+ return -1;
+ is_subset = isl_map_is_subset(map1, map2);
+ if (is_subset != 1)
return is_subset;
is_subset = isl_map_is_subset(map2, map1);
return is_subset;
int isl_map_fast_is_universe(__isl_keep isl_map *map)
{
+ int i;
+
if (!map)
return -1;
- return map->n == 1 && isl_basic_map_is_universe(map->p[0]);
+ for (i = 0; i < map->n; ++i) {
+ int r = isl_basic_map_is_universe(map->p[i]);
+ if (r < 0 || r)
+ return r;
+ }
+
+ return 0;
}
int isl_set_fast_is_universe(__isl_keep isl_set *set)
if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
struct isl_basic_map *copy = isl_basic_map_copy(bmap);
- copy = isl_basic_map_convex_hull(copy);
+ copy = isl_basic_map_remove_redundancies(copy);
empty = ISL_F_ISSET(copy, ISL_BASIC_MAP_EMPTY);
isl_basic_map_free(copy);
return empty;
return NULL;
if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
return bmap;
- bmap = isl_basic_map_convex_hull(bmap);
+ bmap = isl_basic_map_remove_redundancies(bmap);
bmap = isl_basic_map_sort_constraints(bmap);
ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
return bmap;
(struct isl_basic_map *)bset);
}
-static int isl_basic_map_fast_cmp(const struct isl_basic_map *bmap1,
- const struct isl_basic_map *bmap2)
+int isl_basic_map_fast_cmp(const __isl_keep isl_basic_map *bmap1,
+ const __isl_keep isl_basic_map *bmap2)
{
int i, cmp;
unsigned total;
return 0;
}
-static int isl_basic_map_fast_is_equal(struct isl_basic_map *bmap1,
- struct isl_basic_map *bmap2)
+int isl_basic_map_fast_is_equal(__isl_keep isl_basic_map *bmap1,
+ __isl_keep isl_basic_map *bmap2)
{
return isl_basic_map_fast_cmp(bmap1, bmap2) == 0;
}
return NULL;
}
-/* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
+/* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
*/
struct isl_map *isl_map_product(struct isl_map *map1, struct isl_map *map2)
{
return NULL;
}
+/* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
+ */
+__isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
+ __isl_take isl_map *map2)
+{
+ isl_map *prod;
+
+ prod = isl_map_product(map1, map2);
+ prod = isl_map_flatten(prod);
+ return prod;
+}
+
/* Given two set A and B, construct its Cartesian product A x B.
*/
struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
(struct isl_map *)set2);
}
+__isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
+ __isl_take isl_set *set2)
+{
+ return (isl_set *)isl_map_flat_product((isl_map *)set1, (isl_map *)set2);
+}
+
uint32_t isl_basic_set_get_hash(struct isl_basic_set *bset)
{
int i;
return 1;
}
+int isl_set_n_basic_set(__isl_keep isl_set *set)
+{
+ return set ? set->n : 0;
+}
+
int isl_map_foreach_basic_map(__isl_keep isl_map *map,
int (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
{
return sv;
}
+int isl_map_is_bijective(__isl_keep isl_map *map)
+{
+ int sv;
+
+ sv = isl_map_is_single_valued(map);
+ if (sv < 0 || !sv)
+ return sv;
+
+ map = isl_map_copy(map);
+ map = isl_map_reverse(map);
+ sv = isl_map_is_single_valued(map);
+ isl_map_free(map);
+
+ return sv;
+}
+
int isl_set_is_singleton(__isl_keep isl_set *set)
{
return isl_map_is_single_valued((isl_map *)set);
return isl_basic_set_is_box(set->p[0]);
}
+
+int isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
+{
+ if (!bset)
+ return -1;
+
+ return isl_dim_is_wrapping(bset->dim);
+}
+
+int isl_set_is_wrapping(__isl_keep isl_set *set)
+{
+ if (!set)
+ return -1;
+
+ return isl_dim_is_wrapping(set->dim);
+}
+
+__isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
+{
+ bmap = isl_basic_map_cow(bmap);
+ if (!bmap)
+ return NULL;
+
+ bmap->dim = isl_dim_wrap(bmap->dim);
+ if (!bmap->dim)
+ goto error;
+
+ bmap = isl_basic_map_finalize(bmap);
+
+ return (isl_basic_set *)bmap;
+error:
+ isl_basic_map_free(bmap);
+ return NULL;
+}
+
+__isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
+{
+ int i;
+
+ map = isl_map_cow(map);
+ if (!map)
+ return NULL;
+
+ for (i = 0; i < map->n; ++i) {
+ map->p[i] = (isl_basic_map *)isl_basic_map_wrap(map->p[i]);
+ if (!map->p[i])
+ goto error;
+ }
+ map->dim = isl_dim_wrap(map->dim);
+ if (!map->dim)
+ goto error;
+
+ return (isl_set *)map;
+error:
+ isl_map_free(map);
+ return NULL;
+}
+
+__isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
+{
+ bset = isl_basic_set_cow(bset);
+ if (!bset)
+ return NULL;
+
+ bset->dim = isl_dim_unwrap(bset->dim);
+ if (!bset->dim)
+ goto error;
+
+ bset = isl_basic_set_finalize(bset);
+
+ return (isl_basic_map *)bset;
+error:
+ isl_basic_set_free(bset);
+ return NULL;
+}
+
+__isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
+{
+ int i;
+
+ if (!set)
+ return NULL;
+
+ if (!isl_set_is_wrapping(set))
+ isl_die(set->ctx, isl_error_invalid, "not a wrapping set",
+ goto error);
+
+ set = isl_set_cow(set);
+ if (!set)
+ return NULL;
+
+ for (i = 0; i < set->n; ++i) {
+ set->p[i] = (isl_basic_set *)isl_basic_set_unwrap(set->p[i]);
+ if (!set->p[i])
+ goto error;
+ }
+
+ set->dim = isl_dim_unwrap(set->dim);
+ if (!set->dim)
+ goto error;
+
+ return (isl_map *)set;
+error:
+ isl_set_free(set);
+ return NULL;
+}
+
+__isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
+ enum isl_dim_type type)
+{
+ if (!bmap)
+ return NULL;
+
+ if (!isl_dim_is_named_or_nested(bmap->dim, type))
+ return bmap;
+
+ bmap = isl_basic_map_cow(bmap);
+ if (!bmap)
+ return NULL;
+
+ bmap->dim = isl_dim_reset(bmap->dim, type);
+ if (!bmap->dim)
+ goto error;
+
+ bmap = isl_basic_map_finalize(bmap);
+
+ return bmap;
+error:
+ isl_basic_map_free(bmap);
+ return NULL;
+}
+
+__isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
+ enum isl_dim_type type)
+{
+ int i;
+
+ if (!map)
+ return NULL;
+
+ if (!isl_dim_is_named_or_nested(map->dim, type))
+ return map;
+
+ map = isl_map_cow(map);
+ if (!map)
+ return NULL;
+
+ for (i = 0; i < map->n; ++i) {
+ map->p[i] = isl_basic_map_reset(map->p[i], type);
+ if (!map->p[i])
+ goto error;
+ }
+ map->dim = isl_dim_reset(map->dim, type);
+ if (!map->dim)
+ goto error;
+
+ return map;
+error:
+ isl_map_free(map);
+ return NULL;
+}
+
+__isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
+{
+ if (!bmap)
+ return NULL;
+
+ if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
+ return bmap;
+
+ bmap = isl_basic_map_cow(bmap);
+ if (!bmap)
+ return NULL;
+
+ bmap->dim = isl_dim_flatten(bmap->dim);
+ if (!bmap->dim)
+ goto error;
+
+ bmap = isl_basic_map_finalize(bmap);
+
+ return bmap;
+error:
+ isl_basic_map_free(bmap);
+ return NULL;
+}
+
+__isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
+{
+ int i;
+
+ if (!map)
+ return NULL;
+
+ if (!map->dim->nested[0] && !map->dim->nested[1])
+ return map;
+
+ map = isl_map_cow(map);
+ if (!map)
+ return NULL;
+
+ for (i = 0; i < map->n; ++i) {
+ map->p[i] = isl_basic_map_flatten(map->p[i]);
+ if (!map->p[i])
+ goto error;
+ }
+ map->dim = isl_dim_flatten(map->dim);
+ if (!map->dim)
+ goto error;
+
+ return map;
+error:
+ isl_map_free(map);
+ return NULL;
+}
+
+__isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
+{
+ return (isl_set *)isl_map_flatten((isl_map *)set);
+}
+
+__isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
+{
+ isl_dim *dim, *flat_dim;
+ isl_map *map;
+
+ dim = isl_set_get_dim(set);
+ flat_dim = isl_dim_flatten(isl_dim_copy(dim));
+ map = map_identity(isl_dim_join(isl_dim_reverse(dim), flat_dim));
+ map = isl_map_intersect_domain(map, set);
+
+ return map;
+}
+
+/* Extend the given dim_map with mappings for the divs in bmap.
+ */
+static __isl_give struct isl_dim_map *extend_dim_map(
+ __isl_keep struct isl_dim_map *dim_map,
+ __isl_keep isl_basic_map *bmap)
+{
+ int i;
+ struct isl_dim_map *res;
+ int offset;
+
+ offset = isl_basic_map_offset(bmap, isl_dim_div);
+
+ res = isl_dim_map_alloc(bmap->ctx, dim_map->len - 1 + bmap->n_div);
+ if (!res)
+ return NULL;
+
+ for (i = 0; i < dim_map->len; ++i)
+ res->pos[i] = dim_map->pos[i];
+ for (i = 0; i < bmap->n_div; ++i)
+ res->pos[dim_map->len + i] = offset + i;
+
+ return res;
+}
+
+/* Extract a dim_map from a reordering.
+ * We essentially need to reverse the mapping, and add an offset
+ * of 1 for the constant term.
+ */
+__isl_give struct isl_dim_map *isl_dim_map_from_reordering(
+ __isl_keep isl_reordering *exp)
+{
+ int i;
+ struct isl_dim_map *dim_map;
+
+ if (!exp)
+ return NULL;
+
+ dim_map = isl_dim_map_alloc(exp->dim->ctx, isl_dim_total(exp->dim));
+ if (!dim_map)
+ return NULL;
+
+ for (i = 0; i < exp->len; ++i)
+ dim_map->pos[1 + exp->pos[i]] = 1 + i;
+
+ return dim_map;
+}
+
+/* Reorder the dimensions of "bmap" according to the given dim_map
+ * and set the dimension specification to "dim".
+ */
+__isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
+ __isl_take isl_dim *dim, __isl_take struct isl_dim_map *dim_map)
+{
+ isl_basic_map *res;
+
+ bmap = isl_basic_map_cow(bmap);
+ if (!bmap || !dim || !dim_map)
+ goto error;
+
+ res = isl_basic_map_alloc_dim(dim,
+ bmap->n_div, bmap->n_eq, bmap->n_ineq);
+ res = add_constraints_dim_map(res, bmap, dim_map);
+ res = isl_basic_map_finalize(res);
+ return res;
+error:
+ free(dim_map);
+ isl_basic_map_free(bmap);
+ isl_dim_free(dim);
+ return NULL;
+}
+
+/* Reorder the dimensions of "map" according to given reordering.
+ */
+__isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
+ __isl_take isl_reordering *r)
+{
+ int i;
+ struct isl_dim_map *dim_map;
+
+ map = isl_map_cow(map);
+ dim_map = isl_dim_map_from_reordering(r);
+ if (!map || !r || !dim_map)
+ goto error;
+
+ for (i = 0; i < map->n; ++i) {
+ struct isl_dim_map *dim_map_i;
+
+ dim_map_i = extend_dim_map(dim_map, map->p[i]);
+
+ map->p[i] = isl_basic_map_realign(map->p[i],
+ isl_dim_copy(r->dim), dim_map_i);
+
+ if (!map->p[i])
+ goto error;
+ }
+
+ map = isl_map_reset_dim(map, isl_dim_copy(r->dim));
+
+ isl_reordering_free(r);
+ free(dim_map);
+ return map;
+error:
+ free(dim_map);
+ isl_map_free(map);
+ isl_reordering_free(r);
+ return NULL;
+}
+
+__isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
+ __isl_take isl_reordering *r)
+{
+ return (isl_set *)isl_map_realign((isl_map *)set, r);
+}
+
+__isl_give isl_mat *isl_basic_map_equalities_matrix(
+ __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
+ enum isl_dim_type c2, enum isl_dim_type c3,
+ enum isl_dim_type c4, enum isl_dim_type c5)
+{
+ enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
+ struct isl_mat *mat;
+ int i, j, k;
+ int pos;
+
+ if (!bmap)
+ return NULL;
+ mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
+ isl_basic_map_total_dim(bmap) + 1);
+ if (!mat)
+ return NULL;
+ for (i = 0; i < bmap->n_eq; ++i)
+ for (j = 0, pos = 0; j < 5; ++j) {
+ int off = isl_basic_map_offset(bmap, c[j]);
+ for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
+ isl_int_set(mat->row[i][pos],
+ bmap->eq[i][off + k]);
+ ++pos;
+ }
+ }
+
+ return mat;
+}
+
+__isl_give isl_mat *isl_basic_map_inequalities_matrix(
+ __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
+ enum isl_dim_type c2, enum isl_dim_type c3,
+ enum isl_dim_type c4, enum isl_dim_type c5)
+{
+ enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
+ struct isl_mat *mat;
+ int i, j, k;
+ int pos;
+
+ if (!bmap)
+ return NULL;
+ mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
+ isl_basic_map_total_dim(bmap) + 1);
+ if (!mat)
+ return NULL;
+ for (i = 0; i < bmap->n_ineq; ++i)
+ for (j = 0, pos = 0; j < 5; ++j) {
+ int off = isl_basic_map_offset(bmap, c[j]);
+ for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
+ isl_int_set(mat->row[i][pos],
+ bmap->ineq[i][off + k]);
+ ++pos;
+ }
+ }
+
+ return mat;
+}
+
+__isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
+ __isl_take isl_dim *dim,
+ __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
+ enum isl_dim_type c2, enum isl_dim_type c3,
+ enum isl_dim_type c4, enum isl_dim_type c5)
+{
+ enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
+ isl_basic_map *bmap;
+ unsigned total;
+ unsigned extra;
+ int i, j, k, l;
+ int pos;
+
+ if (!dim || !eq || !ineq)
+ goto error;
+
+ if (eq->n_col != ineq->n_col)
+ isl_die(dim->ctx, isl_error_invalid,
+ "equalities and inequalities matrices should have "
+ "same number of columns", goto error);
+
+ total = 1 + isl_dim_total(dim);
+
+ if (eq->n_col < total)
+ isl_die(dim->ctx, isl_error_invalid,
+ "number of columns too small", goto error);
+
+ extra = eq->n_col - total;
+
+ bmap = isl_basic_map_alloc_dim(isl_dim_copy(dim), extra,
+ eq->n_row, ineq->n_row);
+ if (!bmap)
+ goto error;
+ for (i = 0; i < extra; ++i)
+ if (isl_basic_map_alloc_div(bmap) < 0)
+ goto error;
+ for (i = 0; i < eq->n_row; ++i) {
+ l = isl_basic_map_alloc_equality(bmap);
+ if (l < 0)
+ goto error;
+ for (j = 0, pos = 0; j < 5; ++j) {
+ int off = isl_basic_map_offset(bmap, c[j]);
+ for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
+ isl_int_set(bmap->eq[l][off + k],
+ eq->row[i][pos]);
+ ++pos;
+ }
+ }
+ }
+ for (i = 0; i < ineq->n_row; ++i) {
+ l = isl_basic_map_alloc_inequality(bmap);
+ if (l < 0)
+ goto error;
+ for (j = 0, pos = 0; j < 5; ++j) {
+ int off = isl_basic_map_offset(bmap, c[j]);
+ for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
+ isl_int_set(bmap->ineq[l][off + k],
+ ineq->row[i][pos]);
+ ++pos;
+ }
+ }
+ }
+
+ isl_dim_free(dim);
+ isl_mat_free(eq);
+ isl_mat_free(ineq);
+
+ return bmap;
+error:
+ isl_dim_free(dim);
+ isl_mat_free(eq);
+ isl_mat_free(ineq);
+ return NULL;
+}
+
+__isl_give isl_mat *isl_basic_set_equalities_matrix(
+ __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
+ enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
+{
+ return isl_basic_map_equalities_matrix((isl_basic_map *)bset,
+ c1, c2, c3, c4, isl_dim_in);
+}
+
+__isl_give isl_mat *isl_basic_set_inequalities_matrix(
+ __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
+ enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
+{
+ return isl_basic_map_inequalities_matrix((isl_basic_map *)bset,
+ c1, c2, c3, c4, isl_dim_in);
+}
+
+__isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
+ __isl_take isl_dim *dim,
+ __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
+ enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
+{
+ return (isl_basic_set*)
+ isl_basic_map_from_constraint_matrices(dim, eq, ineq,
+ c1, c2, c3, c4, isl_dim_in);
+}