/* Construct a basic set described by the "n" equalities of "bset" starting * at "first". */ static __isl_give isl_basic_set *copy_equalities(__isl_keep isl_basic_set *bset, unsigned first, unsigned n) { int i, k; isl_basic_set *eq; unsigned total; isl_assert(bset->ctx, bset->n_div == 0, return NULL); total = isl_basic_set_total_dim(bset); eq = isl_basic_set_alloc_space(isl_space_copy(bset->dim), 0, n, 0); if (!eq) return NULL; for (i = 0; i < n; ++i) { k = isl_basic_set_alloc_equality(eq); if (k < 0) goto error; isl_seq_cpy(eq->eq[k], bset->eq[first + k], 1 + total); } return eq; error: isl_basic_set_free(eq); return NULL; }
/* Return 1 if "bmap" contains the point "point". * "bmap" is assumed to have known divs. * The point is first extended with the divs and then passed * to basic_map_contains. */ int isl_basic_map_contains_point(__isl_keep isl_basic_map *bmap, __isl_keep isl_point *point) { int i; struct isl_vec *vec; unsigned dim; int contains; if (!bmap || !point) return -1; isl_assert(bmap->ctx, isl_dim_equal(bmap->dim, point->dim), return -1); if (bmap->n_div == 0) return isl_basic_map_contains(bmap, point->vec); dim = isl_basic_map_total_dim(bmap) - bmap->n_div; vec = isl_vec_alloc(bmap->ctx, 1 + dim + bmap->n_div); if (!vec) return -1; isl_seq_cpy(vec->el, point->vec->el, point->vec->size); for (i = 0; i < bmap->n_div; ++i) { isl_seq_inner_product(bmap->div[i] + 1, vec->el, 1 + dim + i, &vec->el[1+dim+i]); isl_int_fdiv_q(vec->el[1+dim+i], vec->el[1+dim+i], bmap->div[i][0]); } contains = isl_basic_map_contains(bmap, vec); isl_vec_free(vec); return contains; }
/* Compute a common lattice of solutions to the linear modulo * constraints specified by B and d. * See also the documentation of isl_mat_parameter_compression. * We put the matrix * * A = [ L_1^{-T} L_2^{-T} ... L_k^{-T} ] * * on a common denominator. This denominator D is the lcm of modulos d. * Since L_i = U_i^{-1} diag(d_i, 1, ... 1), we have * L_i^{-T} = U_i^T diag(d_i, 1, ... 1)^{-T} = U_i^T diag(1/d_i, 1, ..., 1). * Putting this on the common denominator, we have * D * L_i^{-T} = U_i^T diag(D/d_i, D, ..., D). */ static struct isl_mat *parameter_compression_multi( struct isl_mat *B, struct isl_vec *d) { int i, j, k; isl_int D; struct isl_mat *A = NULL, *U = NULL; struct isl_mat *T; unsigned size; isl_int_init(D); isl_vec_lcm(d, &D); size = B->n_col - 1; A = isl_mat_alloc(B->ctx, size, B->n_row * size); U = isl_mat_alloc(B->ctx, size, size); if (!U || !A) goto error; for (i = 0; i < B->n_row; ++i) { isl_seq_cpy(U->row[0], B->row[i] + 1, size); U = isl_mat_unimodular_complete(U, 1); if (!U) goto error; isl_int_divexact(D, D, d->block.data[i]); for (k = 0; k < U->n_col; ++k) isl_int_mul(A->row[k][i*size+0], D, U->row[0][k]); isl_int_mul(D, D, d->block.data[i]); for (j = 1; j < U->n_row; ++j) for (k = 0; k < U->n_col; ++k) isl_int_mul(A->row[k][i*size+j], D, U->row[j][k]); } A = isl_mat_left_hermite(A, 0, NULL, NULL); T = isl_mat_sub_alloc(A, 0, A->n_row, 0, A->n_row); T = isl_mat_lin_to_aff(T); if (!T) goto error; isl_int_set(T->row[0][0], D); T = isl_mat_right_inverse(T); if (!T) goto error; isl_assert(T->ctx, isl_int_is_one(T->row[0][0]), goto error); T = isl_mat_transpose(T); isl_mat_free(A); isl_mat_free(U); isl_int_clear(D); return T; error: isl_mat_free(A); isl_mat_free(U); isl_int_clear(D); return NULL; }
static void expand_constraint(isl_vec *v, unsigned dim, isl_int *c, int *div_map, unsigned n_div) { int i; isl_seq_cpy(v->el, c, 1 + dim); isl_seq_clr(v->el + 1 + dim, v->size - (1 + dim)); for (i = 0; i < n_div; ++i) isl_int_set(v->el[1 + dim + div_map[i]], c[1 + dim + i]); }
static int tab_add_divs(struct isl_tab *tab, __isl_keep isl_basic_map *bmap, int **div_map) { int i, j; struct isl_vec *vec; unsigned total; unsigned dim; if (!bmap) return -1; if (!bmap->n_div) return 0; if (!*div_map) *div_map = isl_alloc_array(bmap->ctx, int, bmap->n_div); if (!*div_map) return -1; total = isl_basic_map_total_dim(tab->bmap); dim = total - tab->bmap->n_div; vec = isl_vec_alloc(bmap->ctx, 2 + total + bmap->n_div); if (!vec) return -1; for (i = 0; i < bmap->n_div; ++i) { isl_seq_cpy(vec->el, bmap->div[i], 2 + dim); isl_seq_clr(vec->el + 2 + dim, tab->bmap->n_div); for (j = 0; j < i; ++j) isl_int_set(vec->el[2 + dim + (*div_map)[j]], bmap->div[i][2 + dim + j]); for (j = 0; j < tab->bmap->n_div; ++j) if (isl_seq_eq(tab->bmap->div[j], vec->el, 2 + dim + tab->bmap->n_div)) break; (*div_map)[i] = j; if (j == tab->bmap->n_div) { vec->size = 2 + dim + tab->bmap->n_div; if (isl_tab_add_div(tab, vec) < 0) goto error; } } isl_vec_free(vec); return 0; error: isl_vec_free(vec); return -1; }
/* Compute and return the matrix * * U_1^{-1} diag(d_1, 1, ..., 1) * * with U_1 the unimodular completion of the first (and only) row of B. * The columns of this matrix generate the lattice that satisfies * the single (linear) modulo constraint. */ static struct isl_mat *parameter_compression_1( struct isl_mat *B, struct isl_vec *d) { struct isl_mat *U; U = isl_mat_alloc(B->ctx, B->n_col - 1, B->n_col - 1); if (!U) return NULL; isl_seq_cpy(U->row[0], B->row[0] + 1, B->n_col - 1); U = isl_mat_unimodular_complete(U, 1); U = isl_mat_right_inverse(U); if (!U) return NULL; isl_mat_col_mul(U, 0, d->block.data[0], 0); U = isl_mat_lin_to_aff(U); return U; }
static struct isl_vec *isl_vec_lin_to_aff(struct isl_vec *vec) { struct isl_vec *aff; if (!vec) return NULL; aff = isl_vec_alloc(vec->ctx, 1 + vec->size); if (!aff) goto error; isl_int_set_si(aff->el[0], 0); isl_seq_cpy(aff->el + 1, vec->el, vec->size); isl_vec_free(vec); return aff; error: isl_vec_free(vec); return NULL; }
/* Detect and make explicit all equalities satisfied by the (integer) * points in bmap. */ struct isl_basic_map *isl_basic_map_detect_equalities( struct isl_basic_map *bmap) { int i, j; struct isl_basic_set *hull = NULL; if (!bmap) return NULL; if (bmap->n_ineq == 0) return bmap; if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY)) return bmap; if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_ALL_EQUALITIES)) return bmap; if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) return isl_basic_map_implicit_equalities(bmap); hull = equalities_in_underlying_set(isl_basic_map_copy(bmap)); if (!hull) goto error; if (ISL_F_ISSET(hull, ISL_BASIC_SET_EMPTY)) { isl_basic_set_free(hull); return isl_basic_map_set_to_empty(bmap); } bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim), 0, hull->n_eq, 0); for (i = 0; i < hull->n_eq; ++i) { j = isl_basic_map_alloc_equality(bmap); if (j < 0) goto error; isl_seq_cpy(bmap->eq[j], hull->eq[i], 1 + isl_basic_set_total_dim(hull)); } isl_vec_free(bmap->sample); bmap->sample = isl_vec_copy(hull->sample); isl_basic_set_free(hull); ISL_F_SET(bmap, ISL_BASIC_MAP_NO_IMPLICIT | ISL_BASIC_MAP_ALL_EQUALITIES); bmap = isl_basic_map_simplify(bmap); return isl_basic_map_finalize(bmap); error: isl_basic_set_free(hull); isl_basic_map_free(bmap); return NULL; }
static int scan_samples_add_sample(struct isl_scan_callback *cb, __isl_take isl_vec *sample) { struct scan_samples *ss = (struct scan_samples *)cb; ss->samples = isl_mat_extend(ss->samples, ss->samples->n_row + 1, ss->samples->n_col); if (!ss->samples) goto error; isl_seq_cpy(ss->samples->row[ss->samples->n_row - 1], sample->el, sample->size); isl_vec_free(sample); return 0; error: isl_vec_free(sample); return -1; }
/* Given an unbounded tableau and an integer point satisfying the tableau, * construct an initial affine hull containing the recession cone * shifted to the given point. * * The unbounded directions are taken from the last rows of the basis, * which is assumed to have been initialized appropriately. */ static __isl_give isl_basic_set *initial_hull(struct isl_tab *tab, __isl_take isl_vec *vec) { int i; int k; struct isl_basic_set *bset = NULL; struct isl_ctx *ctx; unsigned dim; if (!vec || !tab) return NULL; ctx = vec->ctx; isl_assert(ctx, vec->size != 0, goto error); bset = isl_basic_set_alloc(ctx, 0, vec->size - 1, 0, vec->size - 1, 0); if (!bset) goto error; dim = isl_basic_set_n_dim(bset) - tab->n_unbounded; for (i = 0; i < dim; ++i) { k = isl_basic_set_alloc_equality(bset); if (k < 0) goto error; isl_seq_cpy(bset->eq[k] + 1, tab->basis->row[1 + i] + 1, vec->size - 1); isl_seq_inner_product(bset->eq[k] + 1, vec->el +1, vec->size - 1, &bset->eq[k][0]); isl_int_neg(bset->eq[k][0], bset->eq[k][0]); } bset->sample = vec; bset = isl_basic_set_gauss(bset, NULL); return bset; error: isl_basic_set_free(bset); isl_vec_free(vec); return NULL; }
/* Add stride constraints to "bset" based on the inverse mapping * that was plugged in. In particular, if morph maps x' to x, * the the constraints of the original input * * A x' + b >= 0 * * have been rewritten to * * A inv x + b >= 0 * * However, this substitution may loose information on the integrality of x', * so we need to impose that * * inv x * * is integral. If inv = B/d, this means that we need to impose that * * B x = 0 mod d * * or * * exists alpha in Z^m: B x = d alpha * */ static __isl_give isl_basic_set *add_strides(__isl_take isl_basic_set *bset, __isl_keep isl_morph *morph) { int i, div, k; isl_int gcd; if (isl_int_is_one(morph->inv->row[0][0])) return bset; isl_int_init(gcd); for (i = 0; 1 + i < morph->inv->n_row; ++i) { isl_seq_gcd(morph->inv->row[1 + i], morph->inv->n_col, &gcd); if (isl_int_is_divisible_by(gcd, morph->inv->row[0][0])) continue; div = isl_basic_set_alloc_div(bset); if (div < 0) goto error; k = isl_basic_set_alloc_equality(bset); if (k < 0) goto error; isl_seq_cpy(bset->eq[k], morph->inv->row[1 + i], morph->inv->n_col); isl_seq_clr(bset->eq[k] + morph->inv->n_col, bset->n_div); isl_int_set(bset->eq[k][morph->inv->n_col + div], morph->inv->row[0][0]); } isl_int_clear(gcd); return bset; error: isl_int_clear(gcd); isl_basic_set_free(bset); return NULL; }
/* Apply the morphism to the basic set. * We basically just compute the preimage of "bset" under the inverse mapping * in morph, add in stride constraints and intersect with the range * of the morphism. */ __isl_give isl_basic_set *isl_morph_basic_set(__isl_take isl_morph *morph, __isl_take isl_basic_set *bset) { isl_basic_set *res = NULL; isl_mat *mat = NULL; int i, k; int max_stride; if (!morph || !bset) goto error; isl_assert(bset->ctx, isl_space_is_equal(bset->dim, morph->dom->dim), goto error); max_stride = morph->inv->n_row - 1; if (isl_int_is_one(morph->inv->row[0][0])) max_stride = 0; res = isl_basic_set_alloc_space(isl_space_copy(morph->ran->dim), bset->n_div + max_stride, bset->n_eq + max_stride, bset->n_ineq); for (i = 0; i < bset->n_div; ++i) if (isl_basic_set_alloc_div(res) < 0) goto error; mat = isl_mat_sub_alloc6(bset->ctx, bset->eq, 0, bset->n_eq, 0, morph->inv->n_row); mat = isl_mat_product(mat, isl_mat_copy(morph->inv)); if (!mat) goto error; for (i = 0; i < bset->n_eq; ++i) { k = isl_basic_set_alloc_equality(res); if (k < 0) goto error; isl_seq_cpy(res->eq[k], mat->row[i], mat->n_col); isl_seq_scale(res->eq[k] + mat->n_col, bset->eq[i] + mat->n_col, morph->inv->row[0][0], bset->n_div); } isl_mat_free(mat); mat = isl_mat_sub_alloc6(bset->ctx, bset->ineq, 0, bset->n_ineq, 0, morph->inv->n_row); mat = isl_mat_product(mat, isl_mat_copy(morph->inv)); if (!mat) goto error; for (i = 0; i < bset->n_ineq; ++i) { k = isl_basic_set_alloc_inequality(res); if (k < 0) goto error; isl_seq_cpy(res->ineq[k], mat->row[i], mat->n_col); isl_seq_scale(res->ineq[k] + mat->n_col, bset->ineq[i] + mat->n_col, morph->inv->row[0][0], bset->n_div); } isl_mat_free(mat); mat = isl_mat_sub_alloc6(bset->ctx, bset->div, 0, bset->n_div, 1, morph->inv->n_row); mat = isl_mat_product(mat, isl_mat_copy(morph->inv)); if (!mat) goto error; for (i = 0; i < bset->n_div; ++i) { isl_int_mul(res->div[i][0], morph->inv->row[0][0], bset->div[i][0]); isl_seq_cpy(res->div[i] + 1, mat->row[i], mat->n_col); isl_seq_scale(res->div[i] + 1 + mat->n_col, bset->div[i] + 1 + mat->n_col, morph->inv->row[0][0], bset->n_div); } isl_mat_free(mat); res = add_strides(res, morph); if (isl_basic_set_is_rational(bset)) res = isl_basic_set_set_rational(res); res = isl_basic_set_simplify(res); res = isl_basic_set_finalize(res); res = isl_basic_set_intersect(res, isl_basic_set_copy(morph->ran)); isl_morph_free(morph); isl_basic_set_free(bset); return res; error: isl_mat_free(mat); isl_morph_free(morph); isl_basic_set_free(bset); isl_basic_set_free(res); return NULL; }
/* Given a set of modulo constraints * * c + A y = 0 mod d * * this function returns an affine transformation T, * * y = T y' * * that bijectively maps the integer vectors y' to integer * vectors y that satisfy the modulo constraints. * * This function is inspired by Section 2.5.3 * of B. Meister, "Stating and Manipulating Periodicity in the Polytope * Model. Applications to Program Analysis and Optimization". * However, the implementation only follows the algorithm of that * section for computing a particular solution and not for computing * a general homogeneous solution. The latter is incomplete and * may remove some valid solutions. * Instead, we use an adaptation of the algorithm in Section 7 of * B. Meister, S. Verdoolaege, "Polynomial Approximations in the Polytope * Model: Bringing the Power of Quasi-Polynomials to the Masses". * * The input is given as a matrix B = [ c A ] and a vector d. * Each element of the vector d corresponds to a row in B. * The output is a lower triangular matrix. * If no integer vector y satisfies the given constraints then * a matrix with zero columns is returned. * * We first compute a particular solution y_0 to the given set of * modulo constraints in particular_solution. If no such solution * exists, then we return a zero-columned transformation matrix. * Otherwise, we compute the generic solution to * * A y = 0 mod d * * That is we want to compute G such that * * y = G y'' * * with y'' integer, describes the set of solutions. * * We first remove the common factors of each row. * In particular if gcd(A_i,d_i) != 1, then we divide the whole * row i (including d_i) by this common factor. If afterwards gcd(A_i) != 1, * then we divide this row of A by the common factor, unless gcd(A_i) = 0. * In the later case, we simply drop the row (in both A and d). * * If there are no rows left in A, then G is the identity matrix. Otherwise, * for each row i, we now determine the lattice of integer vectors * that satisfies this row. Let U_i be the unimodular extension of the * row A_i. This unimodular extension exists because gcd(A_i) = 1. * The first component of * * y' = U_i y * * needs to be a multiple of d_i. Let y' = diag(d_i, 1, ..., 1) y''. * Then, * * y = U_i^{-1} diag(d_i, 1, ..., 1) y'' * * for arbitrary integer vectors y''. That is, y belongs to the lattice * generated by the columns of L_i = U_i^{-1} diag(d_i, 1, ..., 1). * If there is only one row, then G = L_1. * * If there is more than one row left, we need to compute the intersection * of the lattices. That is, we need to compute an L such that * * L = L_i L_i' for all i * * with L_i' some integer matrices. Let A be constructed as follows * * A = [ L_1^{-T} L_2^{-T} ... L_k^{-T} ] * * and computed the Hermite Normal Form of A = [ H 0 ] U * Then, * * L_i^{-T} = H U_{1,i} * * or * * H^{-T} = L_i U_{1,i}^T * * In other words G = L = H^{-T}. * To ensure that G is lower triangular, we compute and use its Hermite * normal form. * * The affine transformation matrix returned is then * * [ 1 0 ] * [ y_0 G ] * * as any y = y_0 + G y' with y' integer is a solution to the original * modulo constraints. */ struct isl_mat *isl_mat_parameter_compression( struct isl_mat *B, struct isl_vec *d) { int i; struct isl_mat *cst = NULL; struct isl_mat *T = NULL; isl_int D; if (!B || !d) goto error; isl_assert(B->ctx, B->n_row == d->size, goto error); cst = particular_solution(B, d); if (!cst) goto error; if (cst->n_col == 0) { T = isl_mat_alloc(B->ctx, B->n_col, 0); isl_mat_free(cst); isl_mat_free(B); isl_vec_free(d); return T; } isl_int_init(D); /* Replace a*g*row = 0 mod g*m by row = 0 mod m */ for (i = 0; i < B->n_row; ++i) { isl_seq_gcd(B->row[i] + 1, B->n_col - 1, &D); if (isl_int_is_one(D)) continue; if (isl_int_is_zero(D)) { B = isl_mat_drop_rows(B, i, 1); d = isl_vec_cow(d); if (!B || !d) goto error2; isl_seq_cpy(d->block.data+i, d->block.data+i+1, d->size - (i+1)); d->size--; i--; continue; } B = isl_mat_cow(B); if (!B) goto error2; isl_seq_scale_down(B->row[i] + 1, B->row[i] + 1, D, B->n_col-1); isl_int_gcd(D, D, d->block.data[i]); d = isl_vec_cow(d); if (!d) goto error2; isl_int_divexact(d->block.data[i], d->block.data[i], D); } isl_int_clear(D); if (B->n_row == 0) T = isl_mat_identity(B->ctx, B->n_col); else if (B->n_row == 1) T = parameter_compression_1(B, d); else T = parameter_compression_multi(B, d); T = isl_mat_left_hermite(T, 0, NULL, NULL); if (!T) goto error; isl_mat_sub_copy(T->ctx, T->row + 1, cst->row, cst->n_row, 0, 0, 1); isl_mat_free(cst); isl_mat_free(B); isl_vec_free(d); return T; error2: isl_int_clear(D); error: isl_mat_free(cst); isl_mat_free(B); isl_vec_free(d); return NULL; }
/* Given a tableau of a set and a tableau of the corresponding * recession cone, detect and add all equalities to the tableau. * If the tableau is bounded, then we can simply keep the * tableau in its state after the return from extend_affine_hull. * However, if the tableau is unbounded, then * isl_tab_set_initial_basis_with_cone will add some additional * constraints to the tableau that have to be removed again. * In this case, we therefore rollback to the state before * any constraints were added and then add the equalities back in. */ struct isl_tab *isl_tab_detect_equalities(struct isl_tab *tab, struct isl_tab *tab_cone) { int j; struct isl_vec *sample; struct isl_basic_set *hull; struct isl_tab_undo *snap; if (!tab || !tab_cone) goto error; snap = isl_tab_snap(tab); isl_mat_free(tab->basis); tab->basis = NULL; isl_assert(tab->mat->ctx, tab->bmap, goto error); isl_assert(tab->mat->ctx, tab->samples, goto error); isl_assert(tab->mat->ctx, tab->samples->n_col == 1 + tab->n_var, goto error); isl_assert(tab->mat->ctx, tab->n_sample > tab->n_outside, goto error); if (isl_tab_set_initial_basis_with_cone(tab, tab_cone) < 0) goto error; sample = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var); if (!sample) goto error; isl_seq_cpy(sample->el, tab->samples->row[tab->n_outside], sample->size); isl_vec_free(tab->bmap->sample); tab->bmap->sample = isl_vec_copy(sample); if (tab->n_unbounded == 0) hull = isl_basic_set_from_vec(isl_vec_copy(sample)); else hull = initial_hull(tab, isl_vec_copy(sample)); for (j = tab->n_outside + 1; j < tab->n_sample; ++j) { isl_seq_cpy(sample->el, tab->samples->row[j], sample->size); hull = affine_hull(hull, isl_basic_set_from_vec(isl_vec_copy(sample))); } isl_vec_free(sample); hull = extend_affine_hull(tab, hull); if (!hull) goto error; if (tab->n_unbounded == 0) { isl_basic_set_free(hull); return tab; } if (isl_tab_rollback(tab, snap) < 0) goto error; if (hull->n_eq > tab->n_zero) { for (j = 0; j < hull->n_eq; ++j) { isl_seq_normalize(tab->mat->ctx, hull->eq[j], 1 + tab->n_var); if (isl_tab_add_eq(tab, hull->eq[j]) < 0) goto error; } } isl_basic_set_free(hull); return tab; error: isl_tab_free(tab); return NULL; }