#include <string.h>
#include <strings.h>
+#include <isl_map_private.h>
#include <isl/ctx.h>
#include <isl/blk.h>
#include "isl_dim_private.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"
return (isl_set *)isl_map_set_tuple_name((isl_map *)set, isl_dim_set, s);
}
+const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set *bset)
+{
+ return bset ? isl_dim_get_tuple_name(bset->dim, isl_dim_set) : NULL;
+}
+
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;
return ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
}
+int isl_basic_set_is_rational(__isl_keep isl_basic_set *bset)
+{
+ return isl_basic_map_is_rational(bset);
+}
+
static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
struct isl_basic_map *bmap, unsigned extra,
unsigned n_eq, unsigned n_ineq)
if (!bmap)
return NULL;
bmap->n_div = 0;
- return bmap;
+ return isl_basic_map_finalize(bmap);
}
__isl_give isl_basic_set *isl_basic_set_remove_divs(
type, first, n);
}
+/* Return true if the definition of the given div is unknown or depends
+ * on unknown divs.
+ */
+static int div_is_unknown(__isl_keep isl_basic_map *bmap, int div)
+{
+ int i;
+ unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
+
+ if (isl_int_is_zero(bmap->div[div][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_is_unknown(bmap, i))
+ return 1;
+ }
+
+ return 0;
+}
+
+/* Remove all divs that are unknown or defined in terms of unknown divs.
+ */
+__isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
+ __isl_take isl_basic_map *bmap)
+{
+ int i;
+
+ if (!bmap)
+ return NULL;
+
+ for (i = bmap->n_div - 1; i >= 0; --i) {
+ if (!div_is_unknown(bmap, i))
+ 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_unknown_divs(__isl_take isl_map *map)
+{
+ 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_unknown_divs(map->p[i]);
+ if (!map->p[i])
+ goto error;
+ }
+ return map;
+error:
+ isl_map_free(map);
+ return NULL;
+}
+
+__isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
+{
+ return (isl_set *)isl_map_remove_unknown_divs((isl_map *)set);
+}
+
__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)
}
}
-void isl_basic_set_dump(struct isl_basic_set *bset, FILE *out, int indent)
+void isl_basic_set_print_internal(struct isl_basic_set *bset,
+ FILE *out, int indent)
{
if (!bset) {
fprintf(out, "null basic set\n");
dump((struct isl_basic_map *)bset, out, indent);
}
-void isl_basic_map_dump(struct isl_basic_map *bmap, FILE *out, int indent)
+void isl_basic_map_print_internal(struct isl_basic_map *bmap,
+ FILE *out, int indent)
{
if (!bmap) {
fprintf(out, "null basic map\n");
struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
{
- struct isl_set *set;
-
- if (!bset)
- return NULL;
-
- set = isl_set_alloc_dim(isl_dim_copy(bset->dim), 1, ISL_MAP_DISJOINT);
- if (!set) {
- isl_basic_set_free(bset);
- return NULL;
- }
- return isl_set_add_basic_set(set, bset);
+ return isl_map_from_basic_map(bset);
}
struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
return NULL;
map = isl_map_alloc_dim(isl_dim_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
- if (!map) {
- isl_basic_map_free(bmap);
- return NULL;
- }
return isl_map_add_basic_map(map, bmap);
}
free(set);
}
-void isl_set_dump(struct isl_set *set, FILE *out, int indent)
+void isl_set_print_internal(struct isl_set *set, FILE *out, int indent)
{
int i;
for (i = 0; i < set->n; ++i) {
fprintf(out, "%*s", indent, "");
fprintf(out, "basic set %d:\n", i);
- isl_basic_set_dump(set->p[i], out, indent+4);
+ isl_basic_set_print_internal(set->p[i], out, indent+4);
}
}
-void isl_map_dump(struct isl_map *map, FILE *out, int indent)
+void isl_map_print_internal(struct isl_map *map, FILE *out, int indent)
{
int i;
for (i = 0; i < map->n; ++i) {
fprintf(out, "%*s", indent, "");
fprintf(out, "basic map %d:\n", i);
- isl_basic_map_dump(map->p[i], out, indent+4);
+ isl_basic_map_print_internal(map->p[i], out, indent+4);
}
}
struct isl_basic_map *bmap, struct isl_basic_set *bset)
{
struct isl_basic_map *bmap_domain;
- struct isl_dim *dim;
if (!bmap || !bset)
goto error;
goto error;
bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
bset->n_div, bset->n_eq, bset->n_ineq);
- dim = isl_dim_reverse(isl_dim_copy(bset->dim));
- bmap_domain = isl_basic_map_from_basic_set(bset, dim);
+ bmap_domain = isl_basic_map_from_domain(bset);
bmap = add_constraints(bmap, bmap_domain, 0, 0);
bmap = isl_basic_map_simplify(bmap);
res = isl_basic_map_alloc_dim(res_dim,
bmap->n_div, bmap->n_eq, bmap->n_ineq);
+ if (isl_basic_map_is_rational(bmap))
+ res = isl_basic_map_set_rational(res);
res = add_constraints_dim_map(res, bmap, dim_map);
- res = isl_basic_map_simplify(res);
return isl_basic_map_finalize(res);
}
return isl_basic_map_finalize(bmap);
}
+__isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
+{
+ return isl_basic_map_neg(bset);
+}
+
/* Given a map A -> f(A), construct A -> -f(A).
*/
struct isl_map *isl_map_neg(struct isl_map *map)
return isl_map_reverse(isl_map_from_range(set));
}
+__isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
+ __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
+{
+ return isl_basic_map_apply_range(isl_basic_map_from_domain(domain),
+ isl_basic_map_from_range(range));
+}
+
__isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
__isl_take isl_set *range)
{
return bset;
}
+__isl_give isl_basic_map *isl_basic_map_nat_universe(__isl_take isl_dim *dim)
+{
+ int i;
+ unsigned total = isl_dim_total(dim);
+ isl_basic_map *bmap;
+
+ bmap= isl_basic_map_alloc_dim(dim, 0, 0, total);
+ for (i = 0; i < total; ++i) {
+ int k = isl_basic_map_alloc_inequality(bmap);
+ if (k < 0)
+ goto error;
+ isl_seq_clr(bmap->ineq[k], 1 + total);
+ isl_int_set_si(bmap->ineq[k][1 + i], 1);
+ }
+ return bmap;
+error:
+ isl_basic_map_free(bmap);
+ return NULL;
+}
+
+__isl_give isl_basic_set *isl_basic_set_nat_universe(__isl_take isl_dim *dim)
+{
+ return isl_basic_map_nat_universe(dim);
+}
+
+__isl_give isl_map *isl_map_nat_universe(__isl_take isl_dim *dim)
+{
+ return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
+}
+
+__isl_give isl_set *isl_set_nat_universe(__isl_take isl_dim *dim)
+{
+ return isl_map_nat_universe(dim);
+}
+
__isl_give isl_basic_map *isl_basic_map_universe_like(
__isl_keep isl_basic_map *model)
{
dom, empty);
}
-/* Given a basic map "bmap", compute the lexicograhically minimal
+/* Given a basic map "bmap", compute the lexicographically minimal
* (or maximal) image element for each domain element in dom.
* Set *empty to those elements in dom that do not have an image element.
*
return NULL;
}
-/* Given a map "map", compute the lexicograhically minimal
+/* Given a map "map", compute the lexicographically minimal
* (or maximal) image element for each domain element in dom.
* Set *empty to those elements in dom that do not have an image element.
*
*
* Let res^k and todo^k be the results after k steps and let i = k + 1.
* Assume we are computing the lexicographical maximum.
- * We first intersect basic map i with a relation that maps elements
- * to elements that are lexicographically larger than the image elements
- * in res^k and the compute the maximum image element of this intersection.
- * The result ("better") corresponds to those image elements in basic map i
- * that are better than what we had before. The remainder ("keep") are the
- * domain elements for which the image element in res_k was better.
- * We also compute the lexicographical maximum of basic map i in todo^k.
- * res^i is the result of the operation + better + those elements in
- * res^k that we should keep
- * todo^i is the remainder of the maximum operation on todo^k.
+ * We first compute the lexicographically maximal element in basic map i.
+ * This results in a partial solution res_i and a subset todo_i.
+ * Then we combine these results with those obtain for the first k basic maps
+ * to obtain a result that is valid for the first k+1 basic maps.
+ * In particular, the set where there is no solution is the set where
+ * there is no solution for the first k basic maps and also no solution
+ * for the ith basic map, i.e.,
+ *
+ * todo^i = todo^k * todo_i
+ *
+ * On dom(res^k) * dom(res_i), we need to pick the larger of the two
+ * solutions, arbitrarily breaking ties in favor of res^k.
+ * That is, when res^k(a) >= res_i(a), we pick res^k and
+ * when res^k(a) < res_i(a), we pick res_i. (Here, ">=" and "<" denote
+ * the lexicographic order.)
+ * In practice, we compute
+ *
+ * res^k * (res_i . "<=")
+ *
+ * and
+ *
+ * res_i * (res^k . "<")
+ *
+ * Finally, we consider the symmetric difference of dom(res^k) and dom(res_i),
+ * where only one of res^k and res_i provides a solution and we simply pick
+ * that one, i.e.,
+ *
+ * res^k * todo_i
+ * and
+ * res_i * todo^k
+ *
+ * Note that we only compute these intersections when dom(res^k) intersects
+ * dom(res_i). Otherwise, the only effect of these intersections is to
+ * potentially break up res^k and res_i into smaller pieces.
+ * We want to avoid such splintering as much as possible.
+ * In fact, an earlier implementation of this function would look for
+ * better results in the domain of res^k and for extra results in todo^k,
+ * but this would always result in a splintering according to todo^k,
+ * even when the domain of basic map i is disjoint from the domains of
+ * the previous basic maps.
*/
static __isl_give isl_map *isl_map_partial_lexopt(
__isl_take isl_map *map, __isl_take isl_set *dom,
isl_set_copy(dom), &todo, max);
for (i = 1; i < map->n; ++i) {
- struct isl_map *lt;
- struct isl_map *better;
- struct isl_set *keep;
- struct isl_map *res_i;
- struct isl_set *todo_i;
- struct isl_dim *dim = isl_map_get_dim(res);
-
- dim = isl_dim_range(dim);
- if (max)
- lt = isl_map_lex_lt(dim);
- else
- lt = isl_map_lex_gt(dim);
- lt = isl_map_apply_range(isl_map_copy(res), lt);
- lt = isl_map_intersect(lt,
- isl_map_from_basic_map(isl_basic_map_copy(map->p[i])));
- better = isl_map_partial_lexopt(lt,
- isl_map_domain(isl_map_copy(res)),
- &keep, max);
+ isl_map *lt, *le;
+ isl_map *res_i;
+ isl_set *todo_i;
+ isl_dim *dim = isl_dim_range(isl_map_get_dim(res));
res_i = basic_map_partial_lexopt(isl_basic_map_copy(map->p[i]),
- todo, &todo_i, max);
+ isl_set_copy(dom), &todo_i, max);
+
+ if (max) {
+ lt = isl_map_lex_lt(isl_dim_copy(dim));
+ le = isl_map_lex_le(dim);
+ } else {
+ lt = isl_map_lex_gt(isl_dim_copy(dim));
+ le = isl_map_lex_ge(dim);
+ }
+ lt = isl_map_apply_range(isl_map_copy(res), lt);
+ lt = isl_map_intersect(lt, isl_map_copy(res_i));
+ le = isl_map_apply_range(isl_map_copy(res_i), le);
+ le = isl_map_intersect(le, isl_map_copy(res));
+
+ if (!isl_map_is_empty(lt) || !isl_map_is_empty(le)) {
+ res = isl_map_intersect_domain(res,
+ isl_set_copy(todo_i));
+ res_i = isl_map_intersect_domain(res_i,
+ isl_set_copy(todo));
+ }
- res = isl_map_intersect_domain(res, keep);
res = isl_map_union_disjoint(res, res_i);
- res = isl_map_union_disjoint(res, better);
- todo = todo_i;
+ res = isl_map_union_disjoint(res, lt);
+ res = isl_map_union_disjoint(res, le);
+
+ todo = isl_set_intersect(todo, todo_i);
}
isl_set_free(dom);
isl_int *c;
};
+/* uset_gist depends on constraints without existentially quantified
+ * variables sorting first.
+ */
static int qsort_constraint_cmp(const void *p1, const void *p2)
{
const struct constraint *c1 = (const struct constraint *)p1;
const struct constraint *c2 = (const struct constraint *)p2;
+ int l1, l2;
unsigned size = isl_min(c1->size, c2->size);
+
+ l1 = isl_seq_last_non_zero(c1->c, size);
+ l2 = isl_seq_last_non_zero(c2->c, size);
+
+ if (l1 != l2)
+ return l1 - l2;
+
return isl_seq_cmp(c1->c, c2->c, size);
}
if (bmap1 == bmap2)
return 0;
+ if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
+ ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
+ return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
/* Return the Cartesian product of the basic sets in list (in the given order).
*/
-struct isl_basic_set *isl_basic_set_product(struct isl_basic_set_list *list)
+__isl_give isl_basic_set *isl_basic_set_list_product(
+ __isl_take struct isl_basic_set_list *list)
{
int i;
unsigned dim;
return NULL;
}
-/* 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)
+__isl_give isl_basic_map *isl_basic_map_flat_product(
+ __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
+{
+ isl_basic_map *prod;
+
+ prod = isl_basic_map_product(bmap1, bmap2);
+ prod = isl_basic_map_flatten(prod);
+ return prod;
+}
+
+__isl_give isl_basic_set *isl_basic_set_flat_product(
+ __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
+{
+ return isl_basic_map_flat_product(bset1, bset2);
+}
+
+__isl_give isl_basic_map *isl_basic_map_range_product(
+ __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
+{
+ isl_dim *dim_result = NULL;
+ isl_basic_map *bmap;
+ unsigned in, out1, out2, nparam, total, pos;
+ struct isl_dim_map *dim_map1, *dim_map2;
+
+ if (!bmap1 || !bmap2)
+ goto error;
+
+ dim_result = isl_dim_range_product(isl_dim_copy(bmap1->dim),
+ isl_dim_copy(bmap2->dim));
+
+ in = isl_basic_map_dim(bmap1, isl_dim_in);
+ out1 = isl_basic_map_n_out(bmap1);
+ out2 = isl_basic_map_n_out(bmap2);
+ nparam = isl_basic_map_n_param(bmap1);
+
+ total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
+ dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
+ dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
+ isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
+ isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
+ isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
+ isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
+ isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
+ isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
+ isl_dim_map_div(dim_map1, bmap1, pos += out2);
+ isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
+
+ bmap = isl_basic_map_alloc_dim(dim_result,
+ bmap1->n_div + bmap2->n_div,
+ bmap1->n_eq + bmap2->n_eq,
+ bmap1->n_ineq + bmap2->n_ineq);
+ bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
+ bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
+ bmap = isl_basic_map_simplify(bmap);
+ return isl_basic_map_finalize(bmap);
+error:
+ isl_basic_map_free(bmap1);
+ isl_basic_map_free(bmap2);
+ return NULL;
+}
+
+static __isl_give isl_map *map_product(__isl_take isl_map *map1,
+ __isl_take isl_map *map2,
+ __isl_give isl_dim *(*dim_product)(__isl_take isl_dim *left,
+ __isl_take isl_dim *right),
+ __isl_give isl_basic_map *(*basic_map_product)(
+ __isl_take isl_basic_map *left, __isl_take isl_basic_map *right))
{
unsigned flags = 0;
struct isl_map *result;
ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
ISL_FL_SET(flags, ISL_MAP_DISJOINT);
- result = isl_map_alloc_dim(isl_dim_product(isl_dim_copy(map1->dim),
- isl_dim_copy(map2->dim)),
+ result = isl_map_alloc_dim(dim_product(isl_dim_copy(map1->dim),
+ isl_dim_copy(map2->dim)),
map1->n * map2->n, flags);
if (!result)
goto error;
for (i = 0; i < map1->n; ++i)
for (j = 0; j < map2->n; ++j) {
struct isl_basic_map *part;
- part = isl_basic_map_product(
- isl_basic_map_copy(map1->p[i]),
- isl_basic_map_copy(map2->p[j]));
+ part = basic_map_product(isl_basic_map_copy(map1->p[i]),
+ isl_basic_map_copy(map2->p[j]));
if (isl_basic_map_is_empty(part))
isl_basic_map_free(part);
else
return NULL;
}
+/* 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 map_product(map1, map2, &isl_dim_product, &isl_basic_map_product);
+}
+
/* 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,
return (isl_set *)isl_map_flat_product((isl_map *)set1, (isl_map *)set2);
}
+/* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
+ */
+__isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
+ __isl_take isl_map *map2)
+{
+ return map_product(map1, map2, &isl_dim_range_product,
+ &isl_basic_map_range_product);
+}
+
uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
{
int i;
return isl_basic_set_vars_get_sign(bset, first, n, signs);
}
+/* Check if the given basic map is obviously single-valued.
+ * In particular, for each output dimension, check that there is
+ * an equality that defines the output dimension in terms of
+ * earlier dimensions.
+ */
+int isl_basic_map_fast_is_single_valued(__isl_keep isl_basic_map *bmap)
+{
+ int i, j;
+ unsigned total;
+ unsigned n_out;
+ unsigned o_out;
+
+ if (!bmap)
+ return -1;
+
+ total = 1 + isl_basic_map_total_dim(bmap);
+ n_out = isl_basic_map_dim(bmap, isl_dim_out);
+ o_out = isl_basic_map_offset(bmap, isl_dim_out);
+
+ for (i = 0; i < n_out; ++i) {
+ for (j = 0; j < bmap->n_eq; ++j) {
+ if (isl_int_is_zero(bmap->eq[j][o_out + i]))
+ continue;
+ if (isl_seq_first_non_zero(bmap->eq[j] + o_out + i + 1,
+ total - (o_out + i + 1)) == -1)
+ break;
+ }
+ if (j >= bmap->n_eq)
+ return 0;
+ }
+
+ return 1;
+}
+
+/* Check if the given map is obviously single-valued.
+ */
+int isl_map_fast_is_single_valued(__isl_keep isl_map *map)
+{
+ int sv;
+
+ if (!map)
+ return -1;
+ if (map->n == 0)
+ return 1;
+ if (map->n >= 2)
+ return 0;
+
+ return isl_basic_map_fast_is_single_valued(map->p[0]);
+}
+
/* Check if the given map is single-valued.
* We simply compute
*
isl_map *id;
int sv;
+ sv = isl_map_fast_is_single_valued(map);
+ if (sv < 0 || sv)
+ return sv;
+
test = isl_map_reverse(isl_map_copy(map));
test = isl_map_apply_range(test, isl_map_copy(map));
return NULL;
}
+__isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
+{
+ return (isl_basic_set *)isl_basic_map_flatten((isl_basic_map *)bset);
+}
+
__isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
{
int i;