X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=isl_map_simplify.c;h=7a24c5bb11f23f0b1f0f733c2636745bdc2a83f0;hb=0c59aae9f850bc3cea66e3fb6e70ad15d4b120ef;hp=57b13a3d26e94dbd48ed6167bb214ba5c8418ad8;hpb=1bc0f67b8b2c3a78752bbf2089710816ced3ae30;p=platform%2Fupstream%2Fisl.git diff --git a/isl_map_simplify.c b/isl_map_simplify.c index 57b13a3..7a24c5b 100644 --- a/isl_map_simplify.c +++ b/isl_map_simplify.c @@ -1,8 +1,21 @@ +/* + * Copyright 2008-2009 Katholieke Universiteit Leuven + * + * 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 + */ + +#include +#include +#include #include "isl_equalities.h" -#include "isl_map.h" -#include "isl_map_private.h" -#include "isl_seq.h" +#include +#include #include "isl_tab.h" +#include +#include static void swap_equality(struct isl_basic_map *bmap, int a, int b) { @@ -20,11 +33,6 @@ static void swap_inequality(struct isl_basic_map *bmap, int a, int b) } } -static void set_swap_inequality(struct isl_basic_set *bset, int a, int b) -{ - swap_inequality((struct isl_basic_map *)bset, a, b); -} - static void constraint_drop_vars(isl_int *c, unsigned n, unsigned rem) { isl_seq_cpy(c, c + n, rem); @@ -48,7 +56,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) + if (n == 0 && !isl_dim_get_tuple_name(bset->dim, isl_dim_set)) return bset; bset = isl_basic_set_cow(bset); @@ -89,7 +97,7 @@ struct isl_set *isl_set_drop_dims( isl_assert(set->ctx, first + n <= set->dim->n_out, goto error); - if (n == 0) + if (n == 0 && !isl_dim_get_tuple_name(set->dim, isl_dim_set)) return set; set = isl_set_cow(set); if (!set) @@ -159,7 +167,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) + if (n == 0 && !isl_dim_get_tuple_name(bmap->dim, type)) return bmap; bmap = isl_basic_map_cow(bmap); @@ -195,6 +203,13 @@ error: return NULL; } +__isl_give isl_basic_set *isl_basic_set_drop(__isl_take isl_basic_set *bset, + enum isl_dim_type type, unsigned first, unsigned n) +{ + return (isl_basic_set *)isl_basic_map_drop((isl_basic_map *)bset, + type, first, n); +} + struct isl_basic_map *isl_basic_map_drop_inputs( struct isl_basic_map *bmap, unsigned first, unsigned n) { @@ -211,7 +226,7 @@ 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) + if (n == 0 && !isl_dim_get_tuple_name(map->dim, type)) return map; map = isl_map_cow(map); if (!map) @@ -233,6 +248,12 @@ error: return NULL; } +struct isl_set *isl_set_drop(struct isl_set *set, + enum isl_dim_type type, unsigned first, unsigned n) +{ + return (isl_set *)isl_map_drop((isl_map *)set, type, first, n); +} + struct isl_map *isl_map_drop_inputs( struct isl_map *map, unsigned first, unsigned n) { @@ -295,6 +316,9 @@ struct isl_basic_map *isl_basic_map_normalize_constraints( isl_int gcd; unsigned total = isl_basic_map_total_dim(bmap); + if (!bmap) + return NULL; + isl_int_init(gcd); for (i = bmap->n_eq - 1; i >= 0; --i) { isl_seq_gcd(bmap->eq[i]+1, total, &gcd); @@ -342,36 +366,103 @@ struct isl_basic_map *isl_basic_map_normalize_constraints( struct isl_basic_set *isl_basic_set_normalize_constraints( struct isl_basic_set *bset) { - (struct isl_basic_set *)isl_basic_map_normalize_constraints( + return (struct isl_basic_set *)isl_basic_map_normalize_constraints( (struct isl_basic_map *)bset); } -static void eliminate_div(struct isl_basic_map *bmap, isl_int *eq, unsigned div) +/* 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) { - int i; - unsigned pos = 1 + isl_dim_total(bmap->dim) + div; - unsigned len; - len = 1 + isl_basic_map_total_dim(bmap); + unsigned total; + int k; + int last_div; - for (i = 0; i < bmap->n_eq; ++i) - if (bmap->eq[i] != eq) - isl_seq_elim(bmap->eq[i], eq, pos, len, NULL); + total = isl_basic_map_total_dim(bmap); + last_div = isl_seq_last_non_zero(eq + 1 + isl_dim_total(bmap->dim), + bmap->n_div); + for (k = 0; k < bmap->n_eq; ++k) { + if (bmap->eq[k] == eq) + continue; + if (isl_int_is_zero(bmap->eq[k][1+pos])) + continue; + if (progress) + *progress = 1; + isl_seq_elim(bmap->eq[k], eq, 1+pos, 1+total, NULL); + isl_seq_normalize(bmap->ctx, bmap->eq[k], 1 + total); + } - for (i = 0; i < bmap->n_ineq; ++i) - isl_seq_elim(bmap->ineq[i], eq, pos, len, NULL); + for (k = 0; k < bmap->n_ineq; ++k) { + if (isl_int_is_zero(bmap->ineq[k][1+pos])) + continue; + if (progress) + *progress = 1; + isl_seq_elim(bmap->ineq[k], eq, 1+pos, 1+total, NULL); + isl_seq_normalize(bmap->ctx, bmap->ineq[k], 1 + total); + ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED); + } - /* We need to be careful about circular definitions, - * so for now we just remove the definitions of other divs that - * depend on this div and (possibly) recompute them later. - */ - for (i = 0; i < bmap->n_div; ++i) - if (!isl_int_is_zero(bmap->div[i][0]) && - !isl_int_is_zero(bmap->div[i][1 + pos])) - isl_seq_clr(bmap->div[i], 1 + len); + for (k = 0; k < bmap->n_div; ++k) { + if (isl_int_is_zero(bmap->div[k][0])) + continue; + if (isl_int_is_zero(bmap->div[k][1+1+pos])) + continue; + if (progress) + *progress = 1; + /* We need to be careful about circular definitions, + * so for now we just remove the definition of div k + * if the equality contains any divs. + * If keep_divs is set, then the divs have been ordered + * and we can keep the definition as long as the result + * is still ordered. + */ + if (last_div == -1 || (keep_divs && last_div < k)) + isl_seq_elim(bmap->div[k]+1, eq, + 1+pos, 1+total, &bmap->div[k][0]); + else + isl_seq_clr(bmap->div[k], 1 + total); + ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED); + } +} + +/* Assumes divs have been ordered if keep_divs is set. + */ +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; + + eliminate_var_using_equality(bmap, pos, eq, keep_divs, NULL); isl_basic_map_drop_div(bmap, div); } +/* Check if elimination of div "div" using equality "eq" would not + * result in a div depending on a later div. + */ +static int ok_to_eliminate_div(struct isl_basic_map *bmap, isl_int *eq, + unsigned div) +{ + int k; + int last_div; + unsigned pos = isl_dim_total(bmap->dim) + div; + + last_div = isl_seq_last_non_zero(eq + 1 + isl_dim_total(bmap->dim), + bmap->n_div); + if (last_div < 0 || last_div <= div) + return 1; + + for (k = 0; k <= last_div; ++k) { + if (isl_int_is_zero(bmap->div[k][0])) + return 1; + if (!isl_int_is_zero(bmap->div[k][1 + 1 + pos])) + return 0; + } + + return 1; +} + /* Elimininate divs based on equalities */ static struct isl_basic_map *eliminate_divs_eq( @@ -382,6 +473,8 @@ static struct isl_basic_map *eliminate_divs_eq( int modified = 0; unsigned off; + bmap = isl_basic_map_order_divs(bmap); + if (!bmap) return NULL; @@ -392,9 +485,11 @@ static struct isl_basic_map *eliminate_divs_eq( if (!isl_int_is_one(bmap->eq[i][off + d]) && !isl_int_is_negone(bmap->eq[i][off + d])) continue; + if (!ok_to_eliminate_div(bmap, bmap->eq[i], d)) + continue; modified = 1; *progress = 1; - eliminate_div(bmap, bmap->eq[i], d); + eliminate_div(bmap, bmap->eq[i], d, 1); isl_basic_map_drop_equality(bmap, i); break; } @@ -433,7 +528,7 @@ static struct isl_basic_map *eliminate_divs_ineq( continue; *progress = 1; bmap = isl_basic_map_eliminate_vars(bmap, (off-1)+d, 1); - if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY)) + if (!bmap || ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY)) break; bmap = isl_basic_map_drop_div(bmap, d); if (!bmap) @@ -442,60 +537,6 @@ static struct isl_basic_map *eliminate_divs_ineq( 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; - 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); - for (k = 0; k < bmap->n_eq; ++k) { - if (bmap->eq[k] == eq) - continue; - if (isl_int_is_zero(bmap->eq[k][1+pos])) - continue; - if (progress) - *progress = 1; - isl_seq_elim(bmap->eq[k], eq, 1+pos, 1+total, NULL); - } - - for (k = 0; k < bmap->n_ineq; ++k) { - if (isl_int_is_zero(bmap->ineq[k][1+pos])) - continue; - if (progress) - *progress = 1; - isl_seq_elim(bmap->ineq[k], eq, 1+pos, 1+total, NULL); - ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED); - } - - for (k = 0; k < bmap->n_div; ++k) { - if (isl_int_is_zero(bmap->div[k][0])) - continue; - if (isl_int_is_zero(bmap->div[k][1+1+pos])) - continue; - if (progress) - *progress = 1; - /* We need to be careful about circular definitions, - * so for now we just remove the definition of div k - * if the equality contains any divs. - * If keep_divs is set, then the divs have been ordered - * and we can keep the definition as long as the result - * is still ordered. - */ - if (last_div == -1 || (keep_divs && last_div < k)) - isl_seq_elim(bmap->div[k]+1, eq, - 1+pos, 1+total, &bmap->div[k][0]); - else - isl_seq_clr(bmap->div[k], 1 + total); - ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED); - } -} - struct isl_basic_map *isl_basic_map_gauss( struct isl_basic_map *bmap, int *progress) { @@ -605,13 +646,16 @@ static struct isl_basic_map *remove_duplicate_divs( int k, l, h; int bits; struct isl_blk eq; - unsigned total_var = isl_dim_total(bmap->dim); - unsigned total = total_var + bmap->n_div; + unsigned total_var; + unsigned total; struct isl_ctx *ctx; - if (bmap->n_div <= 1) + if (!bmap || bmap->n_div <= 1) return bmap; + total_var = isl_dim_total(bmap->dim); + total = total_var + bmap->n_div; + ctx = bmap->ctx; for (k = bmap->n_div - 1; k >= 0; --k) if (!isl_int_is_zero(bmap->div[k][0])) @@ -655,7 +699,7 @@ static struct isl_basic_map *remove_duplicate_divs( k = elim_for[l] - 1; isl_int_set_si(eq.data[1+total_var+k], -1); isl_int_set_si(eq.data[1+total_var+l], 1); - eliminate_div(bmap, eq.data, l); + eliminate_div(bmap, eq.data, l, 0); isl_int_set_si(eq.data[1+total_var+k], 0); isl_int_set_si(eq.data[1+total_var+l], 0); } @@ -765,7 +809,7 @@ static struct isl_basic_map *normalize_divs( return bmap; if (div_eq < bmap->n_eq) { - B = isl_mat_sub_alloc(bmap->ctx, bmap->eq, div_eq, + B = isl_mat_sub_alloc6(bmap->ctx, bmap->eq, div_eq, bmap->n_eq - div_eq, 0, 1 + total); C = isl_mat_variable_compression(B, &C2); if (!C || !C2) @@ -786,7 +830,7 @@ static struct isl_basic_map *normalize_divs( --j; isl_int_set(d->block.data[i], bmap->eq[i][1 + total + j]); } - B = isl_mat_sub_alloc(bmap->ctx, bmap->eq, 0, div_eq, 0, 1 + total); + B = isl_mat_sub_alloc6(bmap->ctx, bmap->eq, 0, div_eq, 0, 1 + total); if (C) { B = isl_mat_product(B, C); @@ -811,6 +855,8 @@ static struct isl_basic_map *normalize_divs( } isl_int_clear(v); pos = isl_alloc_array(bmap->ctx, int, T->n_row); + if (!pos) + goto error; /* We have to be careful because dropping equalities may reorder them */ dropped = 0; for (j = bmap->n_div - 1; j >= 0; --j) { @@ -942,7 +988,7 @@ static int ok_to_set_div_from_bound(struct isl_basic_map *bmap, 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, j; + int i; unsigned total = 1 + isl_dim_total(bmap->dim); for (i = 0; i < bmap->n_div; ++i) { @@ -966,7 +1012,7 @@ static struct isl_basic_map *check_for_div_constraints( } static struct isl_basic_map *remove_duplicate_constraints( - struct isl_basic_map *bmap, int *progress) + struct isl_basic_map *bmap, int *progress, int detect_divs) { unsigned int size; isl_int ***index; @@ -974,12 +1020,14 @@ static struct isl_basic_map *remove_duplicate_constraints( int bits; unsigned total = isl_basic_map_total_dim(bmap); isl_int sum; + isl_ctx *ctx; - if (bmap->n_ineq <= 1) + if (!bmap || bmap->n_ineq <= 1) return bmap; size = round_up(4 * (bmap->n_ineq+1) / 3 - 1); bits = ffs(size) - 1; + ctx = isl_basic_map_get_ctx(bmap); index = isl_calloc_array(ctx, isl_int **, size); if (!index) return bmap; @@ -1009,8 +1057,9 @@ static struct isl_basic_map *remove_duplicate_constraints( l = index[h] - &bmap->ineq[0]; isl_int_add(sum, bmap->ineq[k][0], bmap->ineq[l][0]); if (isl_int_is_pos(sum)) { - bmap = check_for_div_constraints(bmap, k, l, sum, - progress); + if (detect_divs) + bmap = check_for_div_constraints(bmap, k, l, + sum, progress); continue; } if (isl_int_is_zero(sum)) { @@ -1019,6 +1068,8 @@ static struct isl_basic_map *remove_duplicate_constraints( * will no longer be valid. * Plus, we probably we want to regauss first. */ + if (progress) + *progress = 1; isl_basic_map_drop_inequality(bmap, l); isl_basic_map_inequality_to_equality(bmap, k); } else @@ -1046,7 +1097,7 @@ struct isl_basic_map *isl_basic_map_simplify(struct isl_basic_map *bmap) bmap = isl_basic_map_gauss(bmap, &progress); /* requires equalities in normal form */ bmap = normalize_divs(bmap, &progress); - bmap = remove_duplicate_constraints(bmap, &progress); + bmap = remove_duplicate_constraints(bmap, &progress, 1); } return bmap; } @@ -1058,6 +1109,43 @@ struct isl_basic_set *isl_basic_set_simplify(struct isl_basic_set *bset) } +int isl_basic_map_is_div_constraint(__isl_keep isl_basic_map *bmap, + isl_int *constraint, unsigned div) +{ + unsigned pos; + + if (!bmap) + return -1; + + pos = 1 + isl_dim_total(bmap->dim) + div; + + if (isl_int_eq(constraint[pos], bmap->div[div][0])) { + int neg; + isl_int_sub(bmap->div[div][1], + bmap->div[div][1], bmap->div[div][0]); + isl_int_add_ui(bmap->div[div][1], bmap->div[div][1], 1); + neg = isl_seq_is_neg(constraint, bmap->div[div]+1, pos); + isl_int_sub_ui(bmap->div[div][1], bmap->div[div][1], 1); + isl_int_add(bmap->div[div][1], + bmap->div[div][1], bmap->div[div][0]); + if (!neg) + return 0; + if (isl_seq_first_non_zero(constraint+pos+1, + bmap->n_div-div-1) != -1) + return 0; + } else if (isl_int_abs_eq(constraint[pos], bmap->div[div][0])) { + if (!isl_seq_eq(constraint, bmap->div[div]+1, pos)) + return 0; + if (isl_seq_first_non_zero(constraint+pos+1, + bmap->n_div-div-1) != -1) + return 0; + } else + return 0; + + return 1; +} + + /* If the only constraints a div d=floor(f/m) * appears in are its two defining constraints * @@ -1078,27 +1166,7 @@ static int div_is_redundant(struct isl_basic_map *bmap, int div) for (i = 0; i < bmap->n_ineq; ++i) { if (isl_int_is_zero(bmap->ineq[i][pos])) continue; - if (isl_int_eq(bmap->ineq[i][pos], bmap->div[div][0])) { - int neg; - isl_int_sub(bmap->div[div][1], - bmap->div[div][1], bmap->div[div][0]); - isl_int_add_ui(bmap->div[div][1], bmap->div[div][1], 1); - neg = isl_seq_is_neg(bmap->ineq[i], bmap->div[div]+1, pos); - isl_int_sub_ui(bmap->div[div][1], bmap->div[div][1], 1); - isl_int_add(bmap->div[div][1], - bmap->div[div][1], bmap->div[div][0]); - if (!neg) - return 0; - if (isl_seq_first_non_zero(bmap->ineq[i]+pos+1, - bmap->n_div-div-1) != -1) - return 0; - } else if (isl_int_abs_eq(bmap->ineq[i][pos], bmap->div[div][0])) { - if (!isl_seq_eq(bmap->ineq[i], bmap->div[div]+1, pos)) - return 0; - if (isl_seq_first_non_zero(bmap->ineq[i]+pos+1, - bmap->n_div-div-1) != -1) - return 0; - } else + if (!isl_basic_map_is_div_constraint(bmap, bmap->ineq[i], div)) return 0; } @@ -1188,7 +1256,6 @@ static struct isl_basic_map *remove_dependent_vars(struct isl_basic_map *bmap, int pos) { int i; - unsigned dim = isl_dim_total(bmap->dim); for (i = 0; i < bmap->n_div; ++i) { if (isl_int_is_zero(bmap->div[i][0])) @@ -1246,6 +1313,8 @@ struct isl_basic_map *isl_basic_map_eliminate_vars( } bmap = isl_basic_map_extend_constraints(bmap, 0, n_lower * n_upper); + if (!bmap) + goto error; for (i = bmap->n_ineq - 1; i >= 0; --i) { int last; if (isl_int_is_zero(bmap->ineq[i][1+d])) @@ -1271,9 +1340,9 @@ struct isl_basic_map *isl_basic_map_eliminate_vars( } if (n_lower > 0 && n_upper > 0) { bmap = isl_basic_map_normalize_constraints(bmap); - bmap = remove_duplicate_constraints(bmap, NULL); + bmap = remove_duplicate_constraints(bmap, NULL, 0); bmap = isl_basic_map_gauss(bmap, NULL); - bmap = isl_basic_map_convex_hull(bmap); + bmap = isl_basic_map_remove_redundancies(bmap); if (!bmap) goto error; if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY)) @@ -1323,7 +1392,7 @@ static void set_compute_elimination_index(struct isl_basic_set *bset, int *elim) static int reduced_using_equalities(isl_int *dst, isl_int *src, struct isl_basic_map *bmap, int *elim) { - int d, i; + int d; int copied = 0; unsigned total; @@ -1358,11 +1427,16 @@ static struct isl_basic_set *isl_basic_set_reduce_using_equalities( if (!bset || !context) goto error; + if (context->n_eq == 0) { + isl_basic_set_free(context); + return bset; + } + bset = isl_basic_set_cow(bset); if (!bset) goto error; - elim = isl_alloc_array(ctx, int, isl_basic_set_n_dim(bset)); + elim = isl_alloc_array(bset->ctx, int, isl_basic_set_n_dim(bset)); if (!elim) goto error; set_compute_elimination_index(context, elim); @@ -1390,12 +1464,14 @@ static struct isl_basic_set *remove_shifted_constraints( isl_int ***index; int bits; int k, h, l; + isl_ctx *ctx; if (!bset) return NULL; size = round_up(4 * (context->n_ineq+1) / 3 - 1); bits = ffs(size) - 1; + ctx = isl_basic_set_get_ctx(bset); index = isl_calloc_array(ctx, isl_int **, size); if (!index) return bset; @@ -1424,92 +1500,110 @@ error: return bset; } -/* Tighten (decrease) the constant terms of the inequalities based - * on the equalities, without removing any integer points. - * For example, if there is an equality - * - * i = 3 * j - * - * and an inequality - * - * i >= 1 - * - * then we want to replace the inequality by - * - * i >= 3 - * - * We do this by computing a variable compression and translating - * the constraints to the compressed space. - * If any constraint has coefficients (except the contant term) - * with a common factor "f", then we can replace the constant term "c" - * by - * - * f * floor(c/f) - * - * That is, we add - * - * f * floor(c/f) - c = -fract(c/f) - * - * and we can add the same value to the original constraint. - * - * In the example, the compressed space only contains "j", - * and the inequality translates to - * - * 3 * j - 1 >= 0 +/* Remove all information from bset that is redundant in the context + * of context. Both bset and context are assumed to be full-dimensional. * - * We add -fract(-1/3) = -2 to the original constraint to obtain + * We first * remove the inequalities from "bset" + * that are obviously redundant with respect to some inequality in "context". * - * i - 3 >= 0 + * If there are any inequalities left, we construct a tableau for + * the context and then add the inequalities of "bset". + * Before adding these inequalities, we freeze all constraints such that + * they won't be considered redundant in terms of the constraints of "bset". + * Then we detect all redundant constraints (among the + * constraints that weren't frozen), first by checking for redundancy in the + * the tableau and then by checking if replacing a constraint by its negation + * would lead to an empty set. This last step is fairly expensive + * and could be optimized by more reuse of the tableau. + * Finally, we update bset according to the results. */ -static struct isl_basic_set *normalize_constraints_in_compressed_space( - struct isl_basic_set *bset) +static __isl_give isl_basic_set *uset_gist_full(__isl_take isl_basic_set *bset, + __isl_take isl_basic_set *context) { - int i; + int i, k; + isl_basic_set *combined = NULL; + struct isl_tab *tab = NULL; + unsigned context_ineq; unsigned total; - struct isl_mat *B, *C; - isl_int gcd; - if (!bset) - return NULL; + if (!bset || !context) + goto error; - if (ISL_F_ISSET(bset, ISL_BASIC_SET_RATIONAL)) + if (isl_basic_set_is_universe(bset)) { + isl_basic_set_free(context); return bset; + } - if (!bset->n_ineq) + if (isl_basic_set_is_universe(context)) { + isl_basic_set_free(context); return bset; + } - bset = isl_basic_set_cow(bset); + bset = remove_shifted_constraints(bset, context); if (!bset) - return NULL; + goto error; + if (bset->n_ineq == 0) + goto done; + 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); + for (i = 0; i < context_ineq; ++i) + if (isl_tab_freeze_constraint(tab, i) < 0) + goto error; + tab = isl_tab_extend(tab, bset->n_ineq); + for (i = 0; i < bset->n_ineq; ++i) + if (isl_tab_add_ineq(tab, bset->ineq[i]) < 0) + goto error; + bset = isl_basic_set_add_constraints(combined, bset, 0); + combined = NULL; + if (!bset) + goto error; + if (isl_tab_detect_redundant(tab) < 0) + goto error; total = isl_basic_set_total_dim(bset); - B = isl_mat_sub_alloc(bset->ctx, bset->eq, 0, bset->n_eq, 0, 1 + total); - C = isl_mat_variable_compression(B, NULL); - if (!C) - return bset; - if (C->n_col == 0) { - isl_mat_free(C); - return isl_basic_set_set_to_empty(bset); - } - B = isl_mat_sub_alloc(bset->ctx, bset->ineq, - 0, bset->n_ineq, 0, 1 + total); - C = isl_mat_product(B, C); - if (!C) - return bset; - - isl_int_init(gcd); - for (i = 0; i < bset->n_ineq; ++i) { - isl_seq_gcd(C->row[i] + 1, C->n_col - 1, &gcd); - if (isl_int_is_one(gcd)) + for (i = context_ineq; i < bset->n_ineq; ++i) { + int is_empty; + if (tab->con[i].is_redundant) continue; - isl_int_fdiv_r(C->row[i][0], C->row[i][0], gcd); - isl_int_sub(bset->ineq[i][0], bset->ineq[i][0], C->row[i][0]); + tab->con[i].is_redundant = 1; + combined = isl_basic_set_dup(bset); + combined = isl_basic_set_update_from_tab(combined, tab); + combined = isl_basic_set_extend_constraints(combined, 0, 1); + k = isl_basic_set_alloc_inequality(combined); + if (k < 0) + goto error; + isl_seq_neg(combined->ineq[k], bset->ineq[i], 1 + total); + isl_int_sub_ui(combined->ineq[k][0], combined->ineq[k][0], 1); + is_empty = isl_basic_set_is_empty(combined); + if (is_empty < 0) + goto error; + isl_basic_set_free(combined); + combined = NULL; + if (!is_empty) + tab->con[i].is_redundant = 0; + } + for (i = 0; i < context_ineq; ++i) + tab->con[i].is_redundant = 1; + bset = isl_basic_set_update_from_tab(bset, tab); + if (bset) { + ISL_F_SET(bset, ISL_BASIC_SET_NO_IMPLICIT); + ISL_F_SET(bset, ISL_BASIC_SET_NO_REDUNDANT); } - isl_int_clear(gcd); - - isl_mat_free(C); + isl_tab_free(tab); +done: + bset = isl_basic_set_simplify(bset); + bset = isl_basic_set_finalize(bset); + isl_basic_set_free(context); return bset; +error: + isl_tab_free(tab); + isl_basic_set_free(combined); + isl_basic_set_free(context); + isl_basic_set_free(bset); + return NULL; } /* Remove all information from bset that is redundant in the context @@ -1518,102 +1612,76 @@ static struct isl_basic_set *normalize_constraints_in_compressed_space( * redundant in the context of the equalities and inequalities of * context are removed. * - * We first simplify the constraints of "bset" in the context of the - * equalities of "context". - * Then we simplify the inequalities of the context in the context - * of the equalities of bset and remove the inequalities from "bset" - * that are obviously redundant with respect to some inequality in "context". + * We first compute the integer affine hull of the intersection, + * compute the gist inside this affine hull and then add back + * those equalities that are not implied by the context. * - * If there are any inequalities left, we construct a tableau for - * the context and then add the inequalities of "bset". - * Before adding these equalities, we freeze all constraints such that - * they won't be considered redundant in terms of the constraints of "bset". - * Then we detect all equalities and redundant constraints (among the - * constraints that weren't frozen) and update bset according to the results. - * We have to be careful here because we don't want any of the context - * constraints to remain and because we haven't added the equalities of "bset" - * to the tableau so we temporarily have to pretend that there were no - * equalities. + * If two constraints are mutually redundant, then uset_gist_full + * will remove the second of those constraints. We therefore first + * sort the constraints so that constraints not involving existentially + * quantified variables are given precedence over those that do. + * We have to perform this sorting before the variable compression, + * because that may effect the order of the variables. */ -static struct isl_basic_set *uset_gist(struct isl_basic_set *bset, - struct isl_basic_set *context) +static __isl_give isl_basic_set *uset_gist(__isl_take isl_basic_set *bset, + __isl_take isl_basic_set *context) { - int i; - struct isl_tab *tab; - unsigned context_ineq; - struct isl_basic_set *combined = NULL; - - if (!context || !bset) - goto error; + isl_mat *eq; + isl_mat *T, *T2; + isl_basic_set *aff; + isl_basic_set *aff_context; + unsigned total; - if (context->n_eq > 0) - bset = isl_basic_set_reduce_using_equalities(bset, - isl_basic_set_copy(context)); - if (!bset) + if (!bset || !context) goto error; - if (isl_basic_set_fast_is_empty(bset)) - goto done; - if (!bset->n_ineq) - goto done; - if (bset->n_eq > 0) { - struct isl_basic_set *affine_hull; - affine_hull = isl_basic_set_copy(bset); - affine_hull = isl_basic_set_cow(affine_hull); - if (!affine_hull) - goto error; - isl_basic_set_free_inequality(affine_hull, affine_hull->n_ineq); - context = isl_basic_set_intersect(context, affine_hull); - context = isl_basic_set_gauss(context, NULL); - context = normalize_constraints_in_compressed_space(context); + bset = isl_basic_set_intersect(bset, isl_basic_set_copy(context)); + if (isl_basic_set_plain_is_empty(bset)) { + isl_basic_set_free(context); + return bset; } - if (!context) + bset = isl_basic_set_sort_constraints(bset); + aff = isl_basic_set_affine_hull(isl_basic_set_copy(bset)); + if (!aff) goto error; - if (ISL_F_ISSET(context, ISL_BASIC_SET_EMPTY)) { - isl_basic_set_free(bset); - return context; + if (isl_basic_set_plain_is_empty(aff)) { + isl_basic_set_free(aff); + isl_basic_set_free(context); + return bset; } - if (!context->n_ineq) - goto done; - bset = remove_shifted_constraints(bset, context); - if (!bset->n_ineq) - goto done; - isl_basic_set_free_equality(context, context->n_eq); - context_ineq = context->n_ineq; - combined = isl_basic_set_cow(isl_basic_set_copy(context)); - combined = isl_basic_set_extend_constraints(combined, - bset->n_eq, bset->n_ineq); - tab = isl_tab_from_basic_set(combined); - if (!tab) - goto error; - for (i = 0; i < context_ineq; ++i) - tab->con[i].frozen = 1; - tab = isl_tab_extend(tab, bset->n_ineq); - if (!tab) - goto error; - for (i = 0; i < bset->n_ineq; ++i) - tab = isl_tab_add_ineq(tab, bset->ineq[i]); - bset = isl_basic_set_add_constraints(combined, bset, 0); - tab = isl_tab_detect_equalities(tab); - tab = isl_tab_detect_redundant(tab); - if (!tab) - goto error2; - for (i = 0; i < context_ineq; ++i) { - tab->con[i].is_zero = 0; - tab->con[i].is_redundant = 1; + if (aff->n_eq == 0) { + isl_basic_set_free(aff); + return uset_gist_full(bset, context); } - bset = isl_basic_set_update_from_tab(bset, tab); - isl_tab_free(tab); - ISL_F_SET(bset, ISL_BASIC_SET_NO_IMPLICIT); - ISL_F_SET(bset, ISL_BASIC_SET_NO_REDUNDANT); -done: - bset = isl_basic_set_simplify(bset); - bset = isl_basic_set_finalize(bset); - isl_basic_set_free(context); + total = isl_basic_set_total_dim(bset); + eq = isl_mat_sub_alloc6(bset->ctx, aff->eq, 0, aff->n_eq, 0, 1 + total); + eq = isl_mat_cow(eq); + T = isl_mat_variable_compression(eq, &T2); + if (T && T->n_col == 0) { + isl_mat_free(T); + isl_mat_free(T2); + isl_basic_set_free(context); + isl_basic_set_free(aff); + return isl_basic_set_set_to_empty(bset); + } + + aff_context = isl_basic_set_affine_hull(isl_basic_set_copy(context)); + + bset = isl_basic_set_preimage(bset, isl_mat_copy(T)); + context = isl_basic_set_preimage(context, T); + + bset = uset_gist_full(bset, context); + bset = isl_basic_set_preimage(bset, T2); + bset = isl_basic_set_intersect(bset, aff); + bset = isl_basic_set_reduce_using_equalities(bset, aff_context); + + if (bset) { + ISL_F_SET(bset, ISL_BASIC_SET_NO_IMPLICIT); + ISL_F_SET(bset, ISL_BASIC_SET_NO_REDUNDANT); + } + return bset; error: - isl_basic_set_free(combined); -error2: isl_basic_set_free(bset); isl_basic_set_free(context); return NULL; @@ -1664,27 +1732,23 @@ struct isl_basic_map *isl_basic_map_gist(struct isl_basic_map *bmap, if (!bmap || !context) goto error; - if (isl_basic_map_is_universe(context)) { - isl_basic_map_free(context); - return bmap; - } if (isl_basic_map_is_universe(bmap)) { isl_basic_map_free(context); return bmap; } - if (isl_basic_map_fast_is_empty(context)) { + 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); } - if (isl_basic_map_fast_is_empty(bmap)) { + if (isl_basic_map_plain_is_empty(bmap)) { isl_basic_map_free(context); return bmap; } - bmap = isl_basic_map_convex_hull(bmap); - context = isl_basic_map_convex_hull(context); + bmap = isl_basic_map_remove_redundancies(bmap); + context = isl_basic_map_remove_redundancies(context); if (context->n_eq) bmap = normalize_divs_in_context(bmap, context); @@ -1705,25 +1769,22 @@ error: /* * Assumes context has no implicit divs. */ -struct isl_map *isl_map_gist(struct isl_map *map, struct isl_basic_map *context) +__isl_give isl_map *isl_map_gist_basic_map(__isl_take isl_map *map, + __isl_take isl_basic_map *context) { int i; if (!map || !context) goto error;; - if (isl_basic_map_is_universe(context)) { - isl_basic_map_free(context); - return map; - } - if (isl_basic_map_fast_is_empty(context)) { + 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); } - context = isl_basic_map_convex_hull(context); + context = isl_basic_map_remove_redundancies(context); map = isl_map_cow(map); if (!map || !context) goto error;; @@ -1731,11 +1792,17 @@ struct isl_map *isl_map_gist(struct isl_map *map, struct isl_basic_map *context) map = isl_map_compute_divs(map); for (i = 0; i < map->n; ++i) context = isl_basic_map_align_divs(context, map->p[i]); - for (i = 0; i < map->n; ++i) { + for (i = map->n - 1; i >= 0; --i) { map->p[i] = isl_basic_map_gist(map->p[i], isl_basic_map_copy(context)); if (!map->p[i]) goto error; + if (isl_basic_map_plain_is_empty(map->p[i])) { + isl_basic_map_free(map->p[i]); + if (i != map->n - 1) + map->p[i] = map->p[map->n - 1]; + map->n--; + } } isl_basic_map_free(context); ISL_F_CLR(map, ISL_MAP_NORMALIZED); @@ -1746,6 +1813,13 @@ error: return NULL; } +__isl_give isl_map *isl_map_gist(__isl_take isl_map *map, + __isl_take isl_map *context) +{ + context = isl_map_compute_divs(context); + return isl_map_gist_basic_map(map, isl_map_simple_hull(context)); +} + struct isl_basic_set *isl_basic_set_gist(struct isl_basic_set *bset, struct isl_basic_set *context) { @@ -1753,24 +1827,32 @@ struct isl_basic_set *isl_basic_set_gist(struct isl_basic_set *bset, (struct isl_basic_map *)bset, (struct isl_basic_map *)context); } -struct isl_set *isl_set_gist(struct isl_set *set, struct isl_basic_set *context) +__isl_give isl_set *isl_set_gist_basic_set(__isl_take isl_set *set, + __isl_take isl_basic_set *context) { - return (struct isl_set *)isl_map_gist((struct isl_map *)set, + return (struct isl_set *)isl_map_gist_basic_map((struct isl_map *)set, (struct isl_basic_map *)context); } +__isl_give isl_set *isl_set_gist(__isl_take isl_set *set, + __isl_take isl_set *context) +{ + return (struct isl_set *)isl_map_gist((struct isl_map *)set, + (struct isl_map *)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 * basic map and check if we get a contradiction. */ -int isl_basic_map_fast_is_disjoint(struct isl_basic_map *bmap1, - struct isl_basic_map *bmap2) +int isl_basic_map_plain_is_disjoint(__isl_keep isl_basic_map *bmap1, + __isl_keep isl_basic_map *bmap2) { struct isl_vec *v = NULL; int *elim = NULL; unsigned total; - int d, i; + int i; if (!bmap1 || !bmap2) return -1; @@ -1829,26 +1911,27 @@ error: return -1; } -int isl_basic_set_fast_is_disjoint(struct isl_basic_set *bset1, - struct isl_basic_set *bset2) +int isl_basic_set_plain_is_disjoint(__isl_keep isl_basic_set *bset1, + __isl_keep isl_basic_set *bset2) { - return isl_basic_map_fast_is_disjoint((struct isl_basic_map *)bset1, + return isl_basic_map_plain_is_disjoint((struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2); } -int isl_map_fast_is_disjoint(struct isl_map *map1, struct isl_map *map2) +int isl_map_plain_is_disjoint(__isl_keep isl_map *map1, + __isl_keep isl_map *map2) { int i, j; if (!map1 || !map2) return -1; - if (isl_map_fast_is_equal(map1, map2)) + if (isl_map_plain_is_equal(map1, map2)) return 0; for (i = 0; i < map1->n; ++i) { for (j = 0; j < map2->n; ++j) { - int d = isl_basic_map_fast_is_disjoint(map1->p[i], + int d = isl_basic_map_plain_is_disjoint(map1->p[i], map2->p[j]); if (d != 1) return d; @@ -1857,12 +1940,18 @@ int isl_map_fast_is_disjoint(struct isl_map *map1, struct isl_map *map2) return 1; } -int isl_set_fast_is_disjoint(struct isl_set *set1, struct isl_set *set2) +int isl_set_plain_is_disjoint(__isl_keep isl_set *set1, + __isl_keep isl_set *set2) { - return isl_map_fast_is_disjoint((struct isl_map *)set1, + return isl_map_plain_is_disjoint((struct isl_map *)set1, (struct isl_map *)set2); } +int isl_set_fast_is_disjoint(__isl_keep isl_set *set1, __isl_keep isl_set *set2) +{ + return isl_set_plain_is_disjoint(set1, set2); +} + /* Check if we can combine a given div with lower bound l and upper * bound u with some other div and if so return that other div. * Otherwise return -1. @@ -1918,6 +2007,14 @@ static int div_find_coalesce(struct isl_basic_map *bmap, int *pairs, } isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0], bmap->ineq[u][0]); + if (isl_int_is_neg(bmap->ineq[l][0])) { + isl_int_sub(bmap->ineq[l][0], + bmap->ineq[l][0], bmap->ineq[u][0]); + bmap = isl_basic_map_copy(bmap); + bmap = isl_basic_map_set_to_empty(bmap); + isl_basic_map_free(bmap); + return -1; + } isl_int_add_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1); for (i = 0; i < bmap->n_div; ++i) { if (i == div) @@ -2096,7 +2193,7 @@ static struct isl_basic_map *drop_more_redundant_divs( if (remove < 0) return bmap; - bmap = isl_basic_map_remove(bmap, isl_dim_div, remove, 1); + bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, remove, 1); return isl_basic_map_drop_redundant_divs(bmap); error: free(pairs); @@ -2247,6 +2344,9 @@ static struct isl_basic_map *coalesce_or_drop_more_redundant_divs( } } + if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY)) + return bmap; + return drop_more_redundant_divs(bmap, pairs, n); }