X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=isl_map.c;h=7496da78d82cb161c5239c2739a0416536256989;hb=d17afdd1f5e49c0c5dcf6a6e0687330dcdc8e6bf;hp=70af1c8dadf6f76f59d994be65f850838a554927;hpb=bf6b6b752381a58b0b9017f0e573b8d838a54339;p=platform%2Fupstream%2Fisl.git diff --git a/isl_map.c b/isl_map.c index 70af1c8..7496da7 100644 --- a/isl_map.c +++ b/isl_map.c @@ -11,13 +11,12 @@ */ #include -#include #include #include #include #include "isl_dim_private.h" #include "isl_equalities.h" -#include +#include #include #include #include @@ -29,6 +28,8 @@ #include #include #include +#include +#include static unsigned n(struct isl_dim *dim, enum isl_dim_type type) { @@ -160,17 +161,17 @@ unsigned isl_basic_map_total_dim(const struct isl_basic_map *bmap) unsigned isl_map_n_in(const struct isl_map *map) { - return map->dim->n_in; + return map ? map->dim->n_in : 0; } unsigned isl_map_n_out(const struct isl_map *map) { - return map->dim->n_out; + return map ? map->dim->n_out : 0; } unsigned isl_map_n_param(const struct isl_map *map) { - return map->dim->nparam; + return map ? map->dim->nparam : 0; } int isl_map_compatible_domain(struct isl_map *map, struct isl_set *set) @@ -253,6 +254,71 @@ struct isl_dim *isl_basic_set_get_dim(struct isl_basic_set *bset) return isl_dim_copy(bset->dim); } +__isl_give isl_local_space *isl_basic_map_get_local_space( + __isl_keep isl_basic_map *bmap) +{ + int i; + isl_local_space *ls; + unsigned total; + + if (!bmap) + return NULL; + + total = isl_basic_map_total_dim(bmap); + ls = isl_local_space_alloc(isl_dim_copy(bmap->dim), bmap->n_div); + if (!ls) + return NULL; + + for (i = 0; i < bmap->n_div; ++i) + isl_seq_cpy(ls->div->row[i], bmap->div[i], 2 + total); + + return ls; +} + +__isl_give isl_local_space *isl_basic_set_get_local_space( + __isl_keep isl_basic_set *bset) +{ + return isl_basic_map_get_local_space(bset); +} + +__isl_give isl_basic_map *isl_basic_map_from_local_space( + __isl_take isl_local_space *ls) +{ + int i; + int n_div; + isl_basic_map *bmap; + + if (!ls) + return NULL; + + n_div = isl_local_space_dim(ls, isl_dim_div); + bmap = isl_basic_map_alloc_dim(isl_local_space_get_dim(ls), + n_div, 0, 2 * n_div); + + for (i = 0; i < n_div; ++i) + if (isl_basic_map_alloc_div(bmap) < 0) + goto error; + + for (i = 0; i < n_div; ++i) { + isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col); + if (isl_basic_map_add_div_constraints(bmap, i) < 0) + goto error; + } + + isl_local_space_free(ls); + return bmap; +error: + isl_local_space_free(ls); + isl_basic_map_free(bmap); + return NULL; +} + +__isl_give isl_basic_set *isl_basic_set_from_local_space( + __isl_take isl_local_space *ls) +{ + return isl_basic_map_from_local_space(ls); +} + struct isl_dim *isl_map_get_dim(struct isl_map *map) { if (!map) @@ -283,6 +349,12 @@ error: return NULL; } +__isl_give isl_basic_set *isl_basic_set_set_tuple_name( + __isl_take isl_basic_set *bset, const char *s) +{ + return isl_basic_map_set_tuple_name(bset, isl_dim_set, s); +} + const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map *bmap, enum isl_dim_type type) { @@ -1212,25 +1284,6 @@ error: return NULL; } -static __isl_give isl_set *isl_set_swap_vars(__isl_take isl_set *set, - unsigned n) -{ - int i; - set = isl_set_cow(set); - if (!set) - return NULL; - - for (i = 0; i < set->n; ++i) { - set->p[i] = isl_basic_set_swap_vars(set->p[i], n); - if (!set->p[i]) { - isl_set_free(set); - return NULL; - } - } - ISL_F_CLR(set, ISL_SET_NORMALIZED); - return set; -} - struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap) { int i = 0; @@ -1337,6 +1390,8 @@ __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set, __isl_give isl_basic_map *isl_basic_map_remove_divs( __isl_take isl_basic_map *bmap) { + if (!bmap) + return NULL; bmap = isl_basic_map_eliminate_vars(bmap, isl_dim_total(bmap->dim), bmap->n_div); if (!bmap) @@ -1388,7 +1443,7 @@ struct isl_basic_map *isl_basic_map_remove_dims(struct isl_basic_map *bmap, return NULL; isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type), goto error); - if (n == 0) + if (n == 0 && !isl_dim_is_named_or_nested(bmap->dim, type)) return bmap; bmap = isl_basic_map_eliminate_vars(bmap, isl_basic_map_offset(bmap, type) - 1 + first, n); @@ -1446,6 +1501,9 @@ __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims( if (!div_involves_vars(bmap, i, first, n)) continue; bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1); + if (!bmap) + return NULL; + i = bmap->n_div; } return bmap; @@ -2159,9 +2217,6 @@ struct isl_basic_set *isl_basic_set_intersect( static __isl_give isl_map *map_intersect_add_constraint( __isl_take isl_map *map1, __isl_take isl_map *map2) { - struct isl_basic_map *bmap1; - struct isl_basic_map *bmap2; - isl_assert(map1->ctx, map1->n == 1, goto error); isl_assert(map2->ctx, map1->n == 1, goto error); isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error); @@ -2215,11 +2270,13 @@ struct isl_map *isl_map_intersect(struct isl_map *map1, struct isl_map *map2) if (!map1 || !map2) goto error; - if (isl_map_plain_is_empty(map1)) { + if (isl_map_plain_is_empty(map1) && + isl_dim_equal(map1->dim, map2->dim)) { isl_map_free(map2); return map1; } - if (isl_map_plain_is_empty(map2)) { + if (isl_map_plain_is_empty(map2) && + isl_dim_equal(map1->dim, map2->dim)) { isl_map_free(map1); return map2; } @@ -2278,6 +2335,21 @@ struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2) (struct isl_map *)set2); } +/* The current implementation of isl_map_intersect accepts intersections + * with parameter domains, so we can just call that for now. + */ +__isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map, + __isl_take isl_set *params) +{ + return isl_map_intersect(map, params); +} + +__isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set, + __isl_take isl_set *params) +{ + return isl_map_intersect_params(set, params); +} + struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap) { struct isl_dim *dim; @@ -2418,7 +2490,6 @@ __isl_give isl_basic_map *isl_basic_map_move_dims( enum isl_dim_type dst_type, unsigned dst_pos, enum isl_dim_type src_type, unsigned src_pos, unsigned n) { - int i; struct isl_dim_map *dim_map; struct isl_basic_map *res; enum isl_dim_type t; @@ -3784,8 +3855,19 @@ struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap) return domain; } +int isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap) +{ + if (!bmap) + return -1; + return isl_dim_may_be_set(bmap->dim); +} + struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap) { + if (!bmap) + return NULL; + if (isl_basic_map_may_be_set(bmap)) + return bmap; return isl_basic_map_domain(isl_basic_map_reverse(bmap)); } @@ -3795,7 +3877,6 @@ __isl_give isl_basic_map *isl_basic_map_domain_map( int i, k; isl_dim *dim; isl_basic_map *domain; - isl_basic_set *bset; int nparam, n_in, n_out; unsigned total; @@ -3834,7 +3915,6 @@ __isl_give isl_basic_map *isl_basic_map_range_map( int i, k; isl_dim *dim; isl_basic_map *range; - isl_basic_set *bset; int nparam, n_in, n_out; unsigned total; @@ -3867,6 +3947,13 @@ error: return NULL; } +int isl_map_may_be_set(__isl_keep isl_map *map) +{ + if (!map) + return -1; + return isl_dim_may_be_set(map->dim); +} + struct isl_set *isl_map_range(struct isl_map *map) { int i; @@ -3874,8 +3961,7 @@ struct isl_set *isl_map_range(struct isl_map *map) if (!map) goto error; - if (isl_map_dim(map, isl_dim_in) == 0 && - !isl_dim_is_named_or_nested(map->dim, isl_dim_in)) + if (isl_map_may_be_set(map)) return (isl_set *)map; map = isl_map_cow(map); @@ -4957,6 +5043,126 @@ __isl_give isl_set *isl_set_lexmax(__isl_take isl_set *set) return (isl_set *)isl_map_lexmax((isl_map *)set); } +/* Construct a map that equates the two given dimensions in the given space. + */ +static __isl_give isl_map *equate(__isl_take isl_dim *dim, + enum isl_dim_type src_type, int src_pos, + enum isl_dim_type dst_type, int dst_pos) +{ + isl_basic_map *bmap; + int k; + + bmap = isl_basic_map_alloc_dim(dim, 0, 1, 0); + k = isl_basic_map_alloc_equality(bmap); + if (k < 0) + goto error; + isl_seq_clr(bmap->eq[k], 1 + isl_basic_map_total_dim(bmap)); + src_pos += isl_basic_map_offset(bmap, src_type); + dst_pos += isl_basic_map_offset(bmap, dst_type); + isl_int_set_si(bmap->eq[k][src_pos], 1); + isl_int_set_si(bmap->eq[k][dst_pos], -1); + + return isl_map_from_basic_map(bmap); +error: + isl_basic_map_free(bmap); + return NULL; +} + +/* Extract the first and only affine expression from list + * and then add it to *pwaff with the given dom. + * This domain is known to be disjoint from other domains + * because of the way isl_basic_set_foreach_lexmax works. + */ +static int update_dim_max(__isl_take isl_basic_set *dom, + __isl_take isl_aff_list *list, void *user) +{ + isl_ctx *ctx = isl_basic_set_get_ctx(dom); + isl_aff *aff; + isl_pw_aff **pwaff = user; + isl_pw_aff *pwaff_i; + + if (isl_aff_list_n_aff(list) != 1) + isl_die(ctx, isl_error_internal, + "expecting single element list", goto error); + + aff = isl_aff_list_get_aff(list, 0); + pwaff_i = isl_pw_aff_alloc(isl_set_from_basic_set(dom), aff); + + *pwaff = isl_pw_aff_add_disjoint(*pwaff, pwaff_i); + + isl_aff_list_free(list); + + return 0; +error: + isl_basic_set_free(dom); + isl_aff_list_free(list); + return -1; +} + +/* Given a one-dimensional basic set, compute the maximum of that + * dimension as an isl_pw_aff. + * + * The isl_pw_aff is constructed by having isl_basic_set_foreach_lexmax + * call update_dim_max on each leaf of the result. + */ +static __isl_give isl_pw_aff *basic_set_dim_max(__isl_keep isl_basic_set *bset) +{ + isl_dim *dim = isl_basic_set_get_dim(bset); + isl_pw_aff *pwaff; + int r; + + dim = isl_dim_domain(isl_dim_from_range(dim)); + pwaff = isl_pw_aff_empty(dim); + + r = isl_basic_set_foreach_lexmax(bset, &update_dim_max, &pwaff); + if (r < 0) + return isl_pw_aff_free(pwaff); + + return pwaff; +} + +/* Compute the maximum of the given set dimension as a function of the + * parameters, but independently of the other set dimensions. + * + * We first project the set onto the given dimension and then compute + * the "lexicographic" maximum in each basic set, combining the results + * using isl_pw_aff_max. + */ +__isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos) +{ + int i; + isl_map *map; + isl_pw_aff *pwaff; + + map = isl_map_from_domain(set); + map = isl_map_add_dims(map, isl_dim_out, 1); + map = isl_map_intersect(map, + equate(isl_map_get_dim(map), isl_dim_in, pos, + isl_dim_out, 0)); + set = isl_map_range(map); + if (!set) + return NULL; + + if (set->n == 0) { + isl_dim *dim = isl_set_get_dim(set); + dim = isl_dim_domain(isl_dim_from_range(dim)); + isl_set_free(set); + return isl_pw_aff_empty(dim); + } + + pwaff = basic_set_dim_max(set->p[0]); + for (i = 1; i < set->n; ++i) { + isl_pw_aff *pwaff_i; + + pwaff_i = basic_set_dim_max(set->p[i]); + pwaff = isl_pw_aff_max(pwaff, pwaff_i); + } + + isl_set_free(set); + + return pwaff; +} + /* Apply a preimage specified by "mat" on the parameters of "bset". * bset is assumed to have only parameters and divs. */ @@ -5195,7 +5401,7 @@ error: return NULL; } -static int basic_map_divs_known(__isl_keep isl_basic_map *bmap) +int isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap) { int i; unsigned off; @@ -5221,7 +5427,7 @@ static int map_divs_known(__isl_keep isl_map *map) return -1; for (i = 0; i < map->n; ++i) { - int known = basic_map_divs_known(map->p[i]); + int known = isl_basic_map_divs_known(map->p[i]); if (known <= 0) return known; } @@ -5236,11 +5442,10 @@ static int map_divs_known(__isl_keep isl_map *map) */ struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap) { - int i; int known; struct isl_map *map; - known = basic_map_divs_known(bmap); + known = isl_basic_map_divs_known(bmap); if (known < 0) goto error; if (known) @@ -5248,7 +5453,7 @@ struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap) bmap = isl_basic_map_drop_redundant_divs(bmap); - known = basic_map_divs_known(bmap); + known = isl_basic_map_divs_known(bmap); if (known < 0) goto error; if (known) @@ -5599,7 +5804,6 @@ __isl_give isl_basic_map *isl_basic_map_deltas_map( int i, k; isl_dim *dim; isl_basic_map *domain; - isl_basic_set *bset; int nparam, n; unsigned total; @@ -6263,6 +6467,54 @@ error: return NULL; } +/* Apply the expansion computed by isl_merge_divs. + * The expansion itself is given by "exp" while the resulting + * list of divs is given by "div". + */ +__isl_give isl_basic_set *isl_basic_set_expand_divs( + __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp) +{ + int i, j; + int n_div; + + bset = isl_basic_set_cow(bset); + if (!bset || !div) + goto error; + + if (div->n_row < bset->n_div) + isl_die(isl_mat_get_ctx(div), isl_error_invalid, + "not an expansion", goto error); + + bset = isl_basic_map_extend_dim(bset, isl_dim_copy(bset->dim), + div->n_row - bset->n_div, 0, + 2 * (div->n_row - bset->n_div)); + + n_div = bset->n_div; + for (i = n_div; i < div->n_row; ++i) + if (isl_basic_set_alloc_div(bset) < 0) + goto error; + + j = n_div - 1; + for (i = div->n_row - 1; i >= 0; --i) { + if (j >= 0 && exp[j] == i) { + if (i != j) + isl_basic_map_swap_div(bset, i, j); + j--; + } else { + isl_seq_cpy(bset->div[i], div->row[i], div->n_col); + if (isl_basic_map_add_div_constraints(bset, i) < 0) + goto error; + } + } + + isl_mat_free(div); + return bset; +error: + isl_basic_set_free(bset); + isl_mat_free(div); + return NULL; +} + /* Look for a div in dst that corresponds to the div "div" in src. * The divs before "div" in src and dst are assumed to be the same. * @@ -7252,6 +7504,16 @@ error: return NULL; } +__isl_give isl_basic_map *isl_basic_map_flat_range_product( + __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2) +{ + isl_basic_map *prod; + + prod = isl_basic_map_range_product(bmap1, bmap2); + prod = isl_basic_map_flatten_range(prod); + return prod; +} + 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, @@ -7322,8 +7584,7 @@ __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1, */ struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2) { - return (struct isl_set *)isl_map_product((struct isl_map *)set1, - (struct isl_map *)set2); + return isl_map_range_product(set1, set2); } __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1, @@ -7341,6 +7602,18 @@ __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1, &isl_basic_map_range_product); } +/* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D) + */ +__isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1, + __isl_take isl_map *map2) +{ + isl_map *prod; + + prod = isl_map_range_product(map1, map2); + prod = isl_map_flatten_range(prod); + return prod; +} + uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap) { int i; @@ -7810,8 +8083,6 @@ int isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap) */ int isl_map_plain_is_single_valued(__isl_keep isl_map *map) { - int sv; - if (!map) return -1; if (map->n == 0) @@ -8159,6 +8430,31 @@ __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_basic_map *isl_basic_map_flatten_range( + __isl_take isl_basic_map *bmap) +{ + if (!bmap) + return NULL; + + if (!bmap->dim->nested[1]) + return bmap; + + bmap = isl_basic_map_cow(bmap); + if (!bmap) + return NULL; + + bmap->dim = isl_dim_flatten_range(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; @@ -8206,6 +8502,35 @@ __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set) return map; } +__isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map) +{ + int i; + + if (!map) + return NULL; + + if (!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_range(map->p[i]); + if (!map->p[i]) + goto error; + } + map->dim = isl_dim_flatten_range(map->dim); + if (!map->dim) + goto error; + + return map; +error: + isl_map_free(map); + return NULL; +} + /* Reorder the dimensions of "bmap" according to the given dim_map * and set the dimension specification to "dim". */ @@ -8556,3 +8881,74 @@ error: isl_map_free(map); return NULL; } + +/* Construct a basic map mapping the domain of the affine expression + * to a one-dimensional range prescribed by the affine expression. + */ +__isl_give isl_basic_map *isl_basic_map_from_aff(__isl_take isl_aff *aff) +{ + int k; + int pos; + isl_local_space *ls; + isl_basic_map *bmap; + + if (!aff) + return NULL; + + ls = isl_aff_get_local_space(aff); + ls = isl_local_space_from_domain(ls); + ls = isl_local_space_add_dims(ls, isl_dim_out, 1); + bmap = isl_basic_map_from_local_space(ls); + bmap = isl_basic_map_extend_constraints(bmap, 1, 0); + k = isl_basic_map_alloc_equality(bmap); + if (k < 0) + goto error; + + pos = isl_basic_map_offset(bmap, isl_dim_out); + isl_seq_cpy(bmap->eq[k], aff->v->el + 1, pos); + isl_int_neg(bmap->eq[k][pos], aff->v->el[0]); + isl_seq_cpy(bmap->eq[k] + pos + 1, aff->v->el + 1 + pos, + aff->v->size - (pos + 1)); + + isl_aff_free(aff); + bmap = isl_basic_map_finalize(bmap); + return bmap; +error: + isl_aff_free(aff); + isl_basic_map_free(bmap); + return NULL; +} + +/* Construct a basic map mapping a domain in the given space to + * to an n-dimensional range, with n the number of elements in the list, + * where each coordinate in the range is prescribed by the + * corresponding affine expression. + * The domains of all affine expressions in the list are assumed to match + * domain_dim. + */ +__isl_give isl_basic_map *isl_basic_map_from_aff_list( + __isl_take isl_dim *domain_dim, __isl_take isl_aff_list *list) +{ + int i; + isl_dim *dim; + isl_basic_map *bmap; + + if (!list) + return NULL; + + dim = isl_dim_from_domain(domain_dim); + bmap = isl_basic_map_universe(dim); + + for (i = 0; i < list->n; ++i) { + isl_aff *aff; + isl_basic_map *bmap_i; + + aff = isl_aff_copy(list->p[i]); + bmap_i = isl_basic_map_from_aff(aff); + + bmap = isl_basic_map_flat_range_product(bmap, bmap_i); + } + + isl_aff_list_free(list); + return bmap; +}