X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=isl_map_simplify.c;h=fa80971a6905d7ea5a08137bce9dc87fda2e580b;hb=7c513a3bc2033ec87934c7287262ba0ec65c9adb;hp=9558a09e081ace1d7ac878672df3d615a17238e5;hpb=890e13b4486f876822bfad8919864db8571b75b7;p=platform%2Fupstream%2Fisl.git diff --git a/isl_map_simplify.c b/isl_map_simplify.c index 9558a09..fa80971 100644 --- a/isl_map_simplify.c +++ b/isl_map_simplify.c @@ -1,10 +1,12 @@ /* * Copyright 2008-2009 Katholieke Universiteit Leuven + * Copyright 2012 Ecole Normale Superieure * * Use of this software is governed by the GNU LGPLv2.1 license * * Written by Sven Verdoolaege, K.U.Leuven, Departement * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium + * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France */ #include @@ -14,7 +16,7 @@ #include #include #include "isl_tab.h" -#include +#include #include static void swap_equality(struct isl_basic_map *bmap, int a, int b) @@ -56,7 +58,7 @@ struct isl_basic_set *isl_basic_set_drop_dims( isl_assert(bset->ctx, first + n <= bset->dim->n_out, goto error); - if (n == 0 && !isl_dim_get_tuple_name(bset->dim, isl_dim_set)) + if (n == 0 && !isl_space_get_tuple_name(bset->dim, isl_dim_set)) return bset; bset = isl_basic_set_cow(bset); @@ -75,7 +77,7 @@ struct isl_basic_set *isl_basic_set_drop_dims( constraint_drop_vars(bset->div[i]+1+1+bset->dim->nparam+first, n, (bset->dim->n_out-first-n)+bset->extra); - bset->dim = isl_dim_drop_outputs(bset->dim, first, n); + bset->dim = isl_space_drop_outputs(bset->dim, first, n); if (!bset->dim) goto error; @@ -97,12 +99,12 @@ struct isl_set *isl_set_drop_dims( isl_assert(set->ctx, first + n <= set->dim->n_out, goto error); - if (n == 0 && !isl_dim_get_tuple_name(set->dim, isl_dim_set)) + if (n == 0 && !isl_space_get_tuple_name(set->dim, isl_dim_set)) return set; set = isl_set_cow(set); if (!set) goto error; - set->dim = isl_dim_drop_outputs(set->dim, first, n); + set->dim = isl_space_drop_outputs(set->dim, first, n); if (!set->dim) goto error; @@ -167,7 +169,7 @@ struct isl_basic_map *isl_basic_map_drop(struct isl_basic_map *bmap, dim = isl_basic_map_dim(bmap, type); isl_assert(bmap->ctx, first + n <= dim, goto error); - if (n == 0 && !isl_dim_get_tuple_name(bmap->dim, type)) + if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type)) return bmap; bmap = isl_basic_map_cow(bmap); @@ -191,7 +193,7 @@ struct isl_basic_map *isl_basic_map_drop(struct isl_basic_map *bmap, goto error; isl_basic_map_free_div(bmap, n); } else - bmap->dim = isl_dim_drop(bmap->dim, type, first, n); + bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n); if (!bmap->dim) goto error; @@ -226,12 +228,12 @@ struct isl_map *isl_map_drop(struct isl_map *map, isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error); - if (n == 0 && !isl_dim_get_tuple_name(map->dim, type)) + if (n == 0 && !isl_space_get_tuple_name(map->dim, type)) return map; map = isl_map_cow(map); if (!map) goto error; - map->dim = isl_dim_drop(map->dim, type, first, n); + map->dim = isl_space_drop_dims(map->dim, type, first, n); if (!map->dim) goto error; @@ -272,7 +274,7 @@ static struct isl_basic_map *isl_basic_map_drop_div( if (!bmap) goto error; - pos = 1 + isl_dim_total(bmap->dim) + div; + pos = 1 + isl_space_dim(bmap->dim, isl_dim_all) + div; isl_assert(bmap->ctx, div < bmap->n_div, goto error); @@ -370,18 +372,62 @@ struct isl_basic_set *isl_basic_set_normalize_constraints( (struct isl_basic_map *)bset); } +/* Remove any common factor in numerator and denominator of a div expression, + * not taking into account the constant term. + * That is, look for any div of the form + * + * floor((a + m f(x))/(m d)) + * + * and replace it by + * + * floor((floor(a/m) + f(x))/d) + * + * The difference {a/m}/d in the argument satisfies 0 <= {a/m}/d < 1/d + * and can therefore not influence the result of the floor. + */ +static __isl_give isl_basic_map *normalize_div_expressions( + __isl_take isl_basic_map *bmap) +{ + int i; + isl_int gcd; + unsigned total = isl_basic_map_total_dim(bmap); + + if (!bmap) + return NULL; + if (bmap->n_div == 0) + return bmap; + + isl_int_init(gcd); + for (i = 0; i < bmap->n_div; ++i) { + if (isl_int_is_zero(bmap->div[i][0])) + continue; + isl_seq_gcd(bmap->div[i] + 2, total, &gcd); + isl_int_gcd(gcd, gcd, bmap->div[i][0]); + if (isl_int_is_one(gcd)) + continue; + isl_int_fdiv_q(bmap->div[i][1], bmap->div[i][1], gcd); + isl_int_divexact(bmap->div[i][0], bmap->div[i][0], gcd); + isl_seq_scale_down(bmap->div[i] + 2, bmap->div[i] + 2, gcd, + total); + } + isl_int_clear(gcd); + + return bmap; +} + /* Assumes divs have been ordered if keep_divs is set. */ static void eliminate_var_using_equality(struct isl_basic_map *bmap, unsigned pos, isl_int *eq, int keep_divs, int *progress) { unsigned total; + unsigned space_total; int k; int last_div; total = isl_basic_map_total_dim(bmap); - last_div = isl_seq_last_non_zero(eq + 1 + isl_dim_total(bmap->dim), - bmap->n_div); + space_total = isl_space_dim(bmap->dim, isl_dim_all); + last_div = isl_seq_last_non_zero(eq + 1 + space_total, bmap->n_div); for (k = 0; k < bmap->n_eq; ++k) { if (bmap->eq[k] == eq) continue; @@ -431,7 +477,7 @@ static void eliminate_var_using_equality(struct isl_basic_map *bmap, static void eliminate_div(struct isl_basic_map *bmap, isl_int *eq, unsigned div, int keep_divs) { - unsigned pos = isl_dim_total(bmap->dim) + div; + unsigned pos = isl_space_dim(bmap->dim, isl_dim_all) + div; eliminate_var_using_equality(bmap, pos, eq, keep_divs, NULL); @@ -446,10 +492,10 @@ static int ok_to_eliminate_div(struct isl_basic_map *bmap, isl_int *eq, { int k; int last_div; - unsigned pos = isl_dim_total(bmap->dim) + div; + unsigned space_total = isl_space_dim(bmap->dim, isl_dim_all); + unsigned pos = space_total + div; - last_div = isl_seq_last_non_zero(eq + 1 + isl_dim_total(bmap->dim), - bmap->n_div); + last_div = isl_seq_last_non_zero(eq + 1 + space_total, bmap->n_div); if (last_div < 0 || last_div <= div) return 1; @@ -478,7 +524,7 @@ static struct isl_basic_map *eliminate_divs_eq( if (!bmap) return NULL; - off = 1 + isl_dim_total(bmap->dim); + off = 1 + isl_space_dim(bmap->dim, isl_dim_all); for (d = bmap->n_div - 1; d >= 0 ; --d) { for (i = 0; i < bmap->n_eq; ++i) { @@ -513,7 +559,7 @@ static struct isl_basic_map *eliminate_divs_ineq( return NULL; ctx = bmap->ctx; - off = 1 + isl_dim_total(bmap->dim); + off = 1 + isl_space_dim(bmap->dim, isl_dim_all); for (d = bmap->n_div - 1; d >= 0 ; --d) { for (i = 0; i < bmap->n_eq; ++i) @@ -653,7 +699,7 @@ static struct isl_basic_map *remove_duplicate_divs( if (!bmap || bmap->n_div <= 1) return bmap; - total_var = isl_dim_total(bmap->dim); + total_var = isl_space_dim(bmap->dim, isl_dim_all); total = total_var + bmap->n_div; ctx = bmap->ctx; @@ -716,7 +762,7 @@ static int n_pure_div_eq(struct isl_basic_map *bmap) int i, j; unsigned total; - total = isl_dim_total(bmap->dim); + total = isl_space_dim(bmap->dim, isl_dim_all); for (i = 0, j = bmap->n_div-1; i < bmap->n_eq; ++i) { while (j >= 0 && isl_int_is_zero(bmap->eq[i][1 + total + j])) --j; @@ -803,7 +849,7 @@ static struct isl_basic_map *normalize_divs( if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS)) return bmap; - total = isl_dim_total(bmap->dim); + total = isl_space_dim(bmap->dim, isl_dim_all); div_eq = n_pure_div_eq(bmap); if (div_eq == 0) return bmap; @@ -878,7 +924,7 @@ static struct isl_basic_map *normalize_divs( needed++; } if (needed > dropped) { - bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim), + bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim), needed, needed, 0); if (!bmap) goto error; @@ -928,7 +974,7 @@ error: static struct isl_basic_map *set_div_from_lower_bound( struct isl_basic_map *bmap, int div, int ineq) { - unsigned total = 1 + isl_dim_total(bmap->dim); + unsigned total = 1 + isl_space_dim(bmap->dim, isl_dim_all); isl_seq_neg(bmap->div[div] + 1, bmap->ineq[ineq], total + bmap->n_div); isl_int_set(bmap->div[div][0], bmap->ineq[ineq][total + div]); @@ -949,7 +995,7 @@ static int ok_to_set_div_from_bound(struct isl_basic_map *bmap, int div, int ineq) { int j; - unsigned total = 1 + isl_dim_total(bmap->dim); + unsigned total = 1 + isl_space_dim(bmap->dim, isl_dim_all); /* Not defined in terms of unknown divs */ for (j = 0; j < bmap->n_div; ++j) { @@ -989,7 +1035,7 @@ static struct isl_basic_map *check_for_div_constraints( struct isl_basic_map *bmap, int k, int l, isl_int sum, int *progress) { int i; - unsigned total = 1 + isl_dim_total(bmap->dim); + unsigned total = 1 + isl_space_dim(bmap->dim, isl_dim_all); for (i = 0; i < bmap->n_div; ++i) { if (!isl_int_is_zero(bmap->div[i][0])) @@ -1091,6 +1137,7 @@ struct isl_basic_map *isl_basic_map_simplify(struct isl_basic_map *bmap) while (progress) { progress = 0; bmap = isl_basic_map_normalize_constraints(bmap); + bmap = normalize_div_expressions(bmap); bmap = remove_duplicate_divs(bmap, &progress); bmap = eliminate_divs_eq(bmap, &progress); bmap = eliminate_divs_ineq(bmap, &progress); @@ -1117,7 +1164,7 @@ int isl_basic_map_is_div_constraint(__isl_keep isl_basic_map *bmap, if (!bmap) return -1; - pos = 1 + isl_dim_total(bmap->dim) + div; + pos = 1 + isl_space_dim(bmap->dim, isl_dim_all) + div; if (isl_int_eq(constraint[pos], bmap->div[div][0])) { int neg; @@ -1145,6 +1192,12 @@ int isl_basic_map_is_div_constraint(__isl_keep isl_basic_map *bmap, return 1; } +int isl_basic_set_is_div_constraint(__isl_keep isl_basic_set *bset, + isl_int *constraint, unsigned div) +{ + return isl_basic_map_is_div_constraint(bset, constraint, div); +} + /* If the only constraints a div d=floor(f/m) * appears in are its two defining constraints @@ -1157,7 +1210,7 @@ int isl_basic_map_is_div_constraint(__isl_keep isl_basic_map *bmap, static int div_is_redundant(struct isl_basic_map *bmap, int div) { int i; - unsigned pos = 1 + isl_dim_total(bmap->dim) + div; + unsigned pos = 1 + isl_space_dim(bmap->dim, isl_dim_all) + div; for (i = 0; i < bmap->n_eq; ++i) if (!isl_int_is_zero(bmap->eq[i][pos])) @@ -1276,6 +1329,7 @@ struct isl_basic_map *isl_basic_map_eliminate_vars( int d; int i, j, k; unsigned total; + int need_gauss = 0; if (n == 0) return bmap; @@ -1299,6 +1353,7 @@ struct isl_basic_map *isl_basic_map_eliminate_vars( continue; eliminate_var_using_equality(bmap, d, bmap->eq[i], 0, NULL); isl_basic_map_drop_equality(bmap, i); + need_gauss = 1; break; } if (i < bmap->n_eq) @@ -1343,6 +1398,7 @@ struct isl_basic_map *isl_basic_map_eliminate_vars( bmap = remove_duplicate_constraints(bmap, NULL, 0); bmap = isl_basic_map_gauss(bmap, NULL); bmap = isl_basic_map_remove_redundancies(bmap); + need_gauss = 0; if (!bmap) goto error; if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY)) @@ -1350,6 +1406,8 @@ struct isl_basic_map *isl_basic_map_eliminate_vars( } } ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED); + if (need_gauss) + bmap = isl_basic_map_gauss(bmap, NULL); return bmap; error: isl_basic_map_free(bmap); @@ -1363,6 +1421,42 @@ struct isl_basic_set *isl_basic_set_eliminate_vars( (struct isl_basic_map *)bset, pos, n); } +/* Eliminate the specified n dimensions starting at first from the + * constraints, without removing the dimensions from the space. + * If the set is rational, the dimensions are eliminated using Fourier-Motzkin. + * Otherwise, they are projected out and the original space is restored. + */ +__isl_give isl_basic_map *isl_basic_map_eliminate( + __isl_take isl_basic_map *bmap, + enum isl_dim_type type, unsigned first, unsigned n) +{ + isl_space *space; + + if (!bmap) + return NULL; + if (n == 0) + return bmap; + + if (first + n > isl_basic_map_dim(bmap, type) || first + n < first) + isl_die(bmap->ctx, isl_error_invalid, + "index out of bounds", goto error); + + if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) { + first += isl_basic_map_offset(bmap, type) - 1; + bmap = isl_basic_map_eliminate_vars(bmap, first, n); + return isl_basic_map_finalize(bmap); + } + + space = isl_basic_map_get_space(bmap); + bmap = isl_basic_map_project_out(bmap, type, first, n); + bmap = isl_basic_map_insert(bmap, type, first, n); + bmap = isl_basic_map_reset_space(bmap, space); + return bmap; +error: + isl_basic_map_free(bmap); + return NULL; +} + /* Don't assume equalities are in order, because align_divs * may have changed the order of the divs. */ @@ -1371,7 +1465,7 @@ static void compute_elimination_index(struct isl_basic_map *bmap, int *elim) int d, i; unsigned total; - total = isl_dim_total(bmap->dim); + total = isl_space_dim(bmap->dim, isl_dim_all); for (d = 0; d < total; ++d) elim[d] = -1; for (i = 0; i < bmap->n_eq; ++i) { @@ -1396,7 +1490,7 @@ static int reduced_using_equalities(isl_int *dst, isl_int *src, int copied = 0; unsigned total; - total = isl_dim_total(bmap->dim); + total = isl_space_dim(bmap->dim, isl_dim_all); for (d = total - 1; d >= 0; --d) { if (isl_int_is_zero(src[1+d])) continue; @@ -1548,7 +1642,7 @@ static __isl_give isl_basic_set *uset_gist_full(__isl_take isl_basic_set *bset, context_ineq = context->n_ineq; combined = isl_basic_set_cow(isl_basic_set_copy(context)); combined = isl_basic_set_extend_constraints(combined, 0, bset->n_ineq); - tab = isl_tab_from_basic_set(combined); + tab = isl_tab_from_basic_set(combined, 0); for (i = 0; i < context_ineq; ++i) if (isl_tab_freeze_constraint(tab, i) < 0) goto error; @@ -1737,10 +1831,8 @@ struct isl_basic_map *isl_basic_map_gist(struct isl_basic_map *bmap, return bmap; } if (isl_basic_map_plain_is_empty(context)) { - struct isl_dim *dim = isl_dim_copy(bmap->dim); - isl_basic_map_free(context); isl_basic_map_free(bmap); - return isl_basic_map_universe(dim); + return context; } if (isl_basic_map_plain_is_empty(bmap)) { isl_basic_map_free(context); @@ -1778,17 +1870,15 @@ __isl_give isl_map *isl_map_gist_basic_map(__isl_take isl_map *map, goto error;; if (isl_basic_map_plain_is_empty(context)) { - struct isl_dim *dim = isl_dim_copy(map->dim); - isl_basic_map_free(context); isl_map_free(map); - return isl_map_universe(dim); + return isl_map_from_basic_map(context); } context = isl_basic_map_remove_redundancies(context); map = isl_map_cow(map); if (!map || !context) goto error;; - isl_assert(map->ctx, isl_dim_equal(map->dim, context->dim), goto error); + isl_assert(map->ctx, isl_space_is_equal(map->dim, context->dim), goto error); map = isl_map_compute_divs(map); for (i = 0; i < map->n; ++i) context = isl_basic_map_align_divs(context, map->p[i]); @@ -1840,6 +1930,15 @@ __isl_give isl_set *isl_set_gist_basic_set(__isl_take isl_set *set, (struct isl_basic_map *)context); } +__isl_give isl_set *isl_set_gist_params_basic_set(__isl_take isl_set *set, + __isl_take isl_basic_set *context) +{ + isl_space *space = isl_set_get_space(set); + isl_basic_set *dom_context = isl_basic_set_universe(space); + dom_context = isl_basic_set_intersect_params(dom_context, context); + return isl_set_gist_basic_set(set, dom_context); +} + __isl_give isl_set *isl_set_gist(__isl_take isl_set *set, __isl_take isl_set *context) { @@ -1847,6 +1946,36 @@ __isl_give isl_set *isl_set_gist(__isl_take isl_set *set, (struct isl_map *)context); } +__isl_give isl_map *isl_map_gist_domain(__isl_take isl_map *map, + __isl_take isl_set *context) +{ + isl_map *map_context = isl_map_universe(isl_map_get_space(map)); + map_context = isl_map_intersect_domain(map_context, context); + return isl_map_gist(map, map_context); +} + +__isl_give isl_map *isl_map_gist_range(__isl_take isl_map *map, + __isl_take isl_set *context) +{ + isl_map *map_context = isl_map_universe(isl_map_get_space(map)); + map_context = isl_map_intersect_range(map_context, context); + return isl_map_gist(map, map_context); +} + +__isl_give isl_map *isl_map_gist_params(__isl_take isl_map *map, + __isl_take isl_set *context) +{ + isl_map *map_context = isl_map_universe(isl_map_get_space(map)); + map_context = isl_map_intersect_params(map_context, context); + return isl_map_gist(map, map_context); +} + +__isl_give isl_set *isl_set_gist_params(__isl_take isl_set *set, + __isl_take isl_set *context) +{ + return isl_map_gist_params(set, context); +} + /* Quick check to see if two basic maps are disjoint. * In particular, we reduce the equalities and inequalities of * one basic map in the context of the equalities of the other @@ -1862,14 +1991,14 @@ int isl_basic_map_plain_is_disjoint(__isl_keep isl_basic_map *bmap1, if (!bmap1 || !bmap2) return -1; - isl_assert(bmap1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim), + isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim), return -1); if (bmap1->n_div || bmap2->n_div) return 0; if (!bmap1->n_eq && !bmap2->n_eq) return 0; - total = isl_dim_total(bmap1->dim); + total = isl_space_dim(bmap1->dim, isl_dim_all); if (total == 0) return 0; v = isl_vec_alloc(bmap1->ctx, 1 + total); @@ -1995,7 +2124,7 @@ static int div_find_coalesce(struct isl_basic_map *bmap, int *pairs, if (bmap->n_div <= 1) return -1; - dim = isl_dim_total(bmap->dim); + dim = isl_space_dim(bmap->dim, isl_dim_all); if (isl_seq_first_non_zero(bmap->ineq[l] + 1 + dim, div) != -1) return -1; if (isl_seq_first_non_zero(bmap->ineq[l] + 1 + dim + div + 1, @@ -2098,7 +2227,7 @@ static void construct_test_ineq(struct isl_basic_map *bmap, int i, int l, int u, isl_int *ineq, isl_int g, isl_int fl, isl_int fu) { unsigned dim; - dim = isl_dim_total(bmap->dim); + dim = isl_space_dim(bmap->dim, isl_dim_all); isl_int_gcd(g, bmap->ineq[l][1 + dim + i], bmap->ineq[u][1 + dim + i]); isl_int_divexact(fl, bmap->ineq[l][1 + dim + i], g); @@ -2136,12 +2265,12 @@ static struct isl_basic_map *drop_more_redundant_divs( if (!bmap) goto error; - dim = isl_dim_total(bmap->dim); + dim = isl_space_dim(bmap->dim, isl_dim_all); vec = isl_vec_alloc(bmap->ctx, 1 + dim + bmap->n_div); if (!vec) goto error; - tab = isl_tab_from_basic_map(bmap); + tab = isl_tab_from_basic_map(bmap, 0); while (n > 0) { int i, l, u; @@ -2267,7 +2396,7 @@ static struct isl_basic_map *coalesce_divs(struct isl_basic_map *bmap, unsigned dim, total; int i; - dim = isl_dim_total(bmap->dim); + dim = isl_space_dim(bmap->dim, isl_dim_all); total = 1 + dim + bmap->n_div; isl_int_init(a); @@ -2327,7 +2456,7 @@ static struct isl_basic_map *coalesce_or_drop_more_redundant_divs( int i, l, u; unsigned dim; - dim = isl_dim_total(bmap->dim); + dim = isl_space_dim(bmap->dim, isl_dim_all); for (i = 0; i < bmap->n_div; ++i) { if (!pairs[i]) @@ -2381,7 +2510,7 @@ struct isl_basic_map *isl_basic_map_drop_redundant_divs( if (!bmap) goto error; - off = isl_dim_total(bmap->dim); + off = isl_space_dim(bmap->dim, isl_dim_all); pairs = isl_calloc_array(bmap->ctx, int, bmap->n_div); if (!pairs) goto error;