/** * cloog_constraint_equal_type function : * This function returns the type of the equality in the constraint (line) of * (constraints) for the element (level). An equality is 'constant' iff all * other factors are null except the constant one. It is a 'pure item' iff * it is equal or opposite to a single variable or parameter. * Otherwise it is an 'affine expression'. * For instance: * i = -13 is constant, i = j, j = -M are pure items, * j = 2*M, i = j+1, 2*j = M are affine expressions. * * - constraints is the matrix of constraints, * - level is the column number in equal of the element which is 'equal to', */ static int cloog_constraint_equal_type(CloogConstraint *cc, int level) { int i; isl_int c; int type = EQTYPE_NONE; struct isl_constraint *constraint = cloog_constraint_to_isl(cc); isl_int_init(c); isl_constraint_get_constant(constraint, &c); if (!isl_int_is_zero(c)) type = EQTYPE_CONSTANT; isl_constraint_get_coefficient(constraint, isl_dim_set, level - 1, &c); if (!isl_int_is_one(c) && !isl_int_is_negone(c)) type = EQTYPE_EXAFFINE; for (i = 0; i < isl_constraint_dim(constraint, isl_dim_param); ++i) { isl_constraint_get_coefficient(constraint, isl_dim_param, i, &c); if (isl_int_is_zero(c)) continue; if ((!isl_int_is_one(c) && !isl_int_is_negone(c)) || type != EQTYPE_NONE) { type = EQTYPE_EXAFFINE; break; } type = EQTYPE_PUREITEM; } for (i = 0; i < isl_constraint_dim(constraint, isl_dim_set); ++i) { if (i == level - 1) continue; isl_constraint_get_coefficient(constraint, isl_dim_set, i, &c); if (isl_int_is_zero(c)) continue; if ((!isl_int_is_one(c) && !isl_int_is_negone(c)) || type != EQTYPE_NONE) { type = EQTYPE_EXAFFINE; break; } type = EQTYPE_PUREITEM; } for (i = 0; i < isl_constraint_dim(constraint, isl_dim_div); ++i) { isl_constraint_get_coefficient(constraint, isl_dim_div, i, &c); if (isl_int_is_zero(c)) continue; if ((!isl_int_is_one(c) && !isl_int_is_negone(c)) || type != EQTYPE_NONE) { type = EQTYPE_EXAFFINE; break; } type = EQTYPE_PUREITEM; } isl_int_clear(c); if (type == EQTYPE_NONE) type = EQTYPE_CONSTANT; return type; }
/* Make first row entries in column col of bset1 identical to * those of bset2, using the fact that entry bset1->eq[row][col]=a * is non-zero. Initially, these elements of bset1 are all zero. * For each row i < row, we set * A[i] = a * A[i] + B[i][col] * A[row] * B[i] = a * B[i] * so that * A[i][col] = B[i][col] = a * old(B[i][col]) */ static void construct_column( struct isl_basic_set *bset1, struct isl_basic_set *bset2, unsigned row, unsigned col) { int r; isl_int a; isl_int b; unsigned total; isl_int_init(a); isl_int_init(b); total = 1 + isl_basic_set_n_dim(bset1); for (r = 0; r < row; ++r) { if (isl_int_is_zero(bset2->eq[r][col])) continue; isl_int_gcd(b, bset2->eq[r][col], bset1->eq[row][col]); isl_int_divexact(a, bset1->eq[row][col], b); isl_int_divexact(b, bset2->eq[r][col], b); isl_seq_combine(bset1->eq[r], a, bset1->eq[r], b, bset1->eq[row], total); isl_seq_scale(bset2->eq[r], bset2->eq[r], a, total); } isl_int_clear(a); isl_int_clear(b); delete_row(bset1, row); }
/// @brief Create an isl constraint from a row of OpenScop integers. /// /// @param row An array of isl/OpenScop integers. /// @param Space An isl space object, describing how to spilt the dimensions. /// /// @return An isl constraint representing this integer array. isl_constraint *constraintFromMatrixRowFull(isl_int *row, __isl_take isl_space *Space) { isl_constraint *c; unsigned NbOut = isl_space_dim(Space, isl_dim_out); unsigned NbIn = isl_space_dim(Space, isl_dim_in); unsigned NbParam = isl_space_dim(Space, isl_dim_param); isl_local_space *LSpace = isl_local_space_from_space(Space); if (isl_int_is_zero(row[0])) c = isl_equality_alloc(LSpace); else c = isl_inequality_alloc(LSpace); unsigned current_column = 1; for (unsigned j = 0; j < NbOut; ++j) isl_constraint_set_coefficient(c, isl_dim_out, j, row[current_column++]); for (unsigned j = 0; j < NbIn; ++j) isl_constraint_set_coefficient(c, isl_dim_in, j, row[current_column++]); for (unsigned j = 0; j < NbParam; ++j) isl_constraint_set_coefficient(c, isl_dim_param, j, row[current_column++]); isl_constraint_set_constant(c, row[current_column]); return c; }
/** * Return true if constraint c involves variable v (zero-based). */ int cloog_constraint_involves(CloogConstraint *constraint, int v) { isl_int c; int res; isl_int_init(c); cloog_constraint_coefficient_get(constraint, v, &c); res = !isl_int_is_zero(c); isl_int_clear(c); return res; }
/* The implementation is based on Section 5.2 of Michael Karr, * "Affine Relationships Among Variables of a Program", * except that the echelon form we use starts from the last column * and that we are dealing with integer coefficients. */ static struct isl_basic_set *affine_hull( struct isl_basic_set *bset1, struct isl_basic_set *bset2) { unsigned total; int col; int row; if (!bset1 || !bset2) goto error; total = 1 + isl_basic_set_n_dim(bset1); row = 0; for (col = total-1; col >= 0; --col) { int is_zero1 = row >= bset1->n_eq || isl_int_is_zero(bset1->eq[row][col]); int is_zero2 = row >= bset2->n_eq || isl_int_is_zero(bset2->eq[row][col]); if (!is_zero1 && !is_zero2) { set_common_multiple(bset1, bset2, row, col); ++row; } else if (!is_zero1 && is_zero2) { construct_column(bset1, bset2, row, col); } else if (is_zero1 && !is_zero2) { construct_column(bset2, bset1, row, col); } else { if (transform_column(bset1, bset2, row, col)) --row; } } isl_assert(bset1->ctx, row == bset1->n_eq, goto error); isl_basic_set_free(bset2); bset1 = isl_basic_set_normalize_constraints(bset1); return bset1; error: isl_basic_set_free(bset1); isl_basic_set_free(bset2); return NULL; }
static int increment_range(struct isl_scan_callback *cb, isl_int min, isl_int max) { struct isl_counter *cnt = (struct isl_counter *)cb; isl_int_add(cnt->count, cnt->count, max); isl_int_sub(cnt->count, cnt->count, min); isl_int_add_ui(cnt->count, cnt->count, 1); if (isl_int_is_zero(cnt->max) || isl_int_lt(cnt->count, cnt->max)) return 0; isl_int_set(cnt->count, cnt->max); return -1; }
static int increment_counter(struct isl_scan_callback *cb, __isl_take isl_vec *sample) { struct isl_counter *cnt = (struct isl_counter *)cb; isl_int_add_ui(cnt->count, cnt->count, 1); isl_vec_free(sample); if (isl_int_is_zero(cnt->max) || isl_int_lt(cnt->count, cnt->max)) return 0; return -1; }
/* Check if dimension dim belongs to a residue class * i_dim \equiv r mod m * with m != 1 and if so return m in *modulo and r in *residue. * As a special case, when i_dim has a fixed value v, then * *modulo is set to 0 and *residue to v. * * If i_dim does not belong to such a residue class, then *modulo * is set to 1 and *residue is set to 0. */ int isl_set_dim_residue_class(struct isl_set *set, int pos, isl_int *modulo, isl_int *residue) { isl_int m; isl_int r; int i; if (!set || !modulo || !residue) return -1; if (set->n == 0) { isl_int_set_si(*modulo, 0); isl_int_set_si(*residue, 0); return 0; } if (isl_basic_set_dim_residue_class(set->p[0], pos, modulo, residue)<0) return -1; if (set->n == 1) return 0; if (isl_int_is_one(*modulo)) return 0; isl_int_init(m); isl_int_init(r); for (i = 1; i < set->n; ++i) { if (isl_basic_set_dim_residue_class(set->p[i], pos, &m, &r) < 0) goto error; isl_int_gcd(*modulo, *modulo, m); isl_int_sub(m, *residue, r); isl_int_gcd(*modulo, *modulo, m); if (!isl_int_is_zero(*modulo)) isl_int_fdiv_r(*residue, *residue, *modulo); if (isl_int_is_one(*modulo)) break; } isl_int_clear(m); isl_int_clear(r); return 0; error: isl_int_clear(m); isl_int_clear(r); return -1; }
/* Check if dimension dim belongs to a residue class * i_dim \equiv r mod m * with m != 1 and if so return m in *modulo and r in *residue. * As a special case, when i_dim has a fixed value v, then * *modulo is set to 0 and *residue to v. * * If i_dim does not belong to such a residue class, then *modulo * is set to 1 and *residue is set to 0. */ int isl_basic_set_dim_residue_class(struct isl_basic_set *bset, int pos, isl_int *modulo, isl_int *residue) { struct isl_ctx *ctx; struct isl_mat *H = NULL, *U = NULL, *C, *H1, *U1; unsigned total; unsigned nparam; if (!bset || !modulo || !residue) return -1; if (isl_basic_set_plain_dim_is_fixed(bset, pos, residue)) { isl_int_set_si(*modulo, 0); return 0; } ctx = isl_basic_set_get_ctx(bset); total = isl_basic_set_total_dim(bset); nparam = isl_basic_set_n_param(bset); H = isl_mat_sub_alloc6(ctx, bset->eq, 0, bset->n_eq, 1, total); H = isl_mat_left_hermite(H, 0, &U, NULL); if (!H) return -1; isl_seq_gcd(U->row[nparam + pos]+bset->n_eq, total-bset->n_eq, modulo); if (isl_int_is_zero(*modulo)) isl_int_set_si(*modulo, 1); if (isl_int_is_one(*modulo)) { isl_int_set_si(*residue, 0); isl_mat_free(H); isl_mat_free(U); return 0; } C = isl_mat_alloc(ctx, 1 + bset->n_eq, 1); if (!C) goto error; isl_int_set_si(C->row[0][0], 1); isl_mat_sub_neg(ctx, C->row + 1, bset->eq, bset->n_eq, 0, 0, 1); H1 = isl_mat_sub_alloc(H, 0, H->n_row, 0, H->n_row); H1 = isl_mat_lin_to_aff(H1); C = isl_mat_inverse_product(H1, C); isl_mat_free(H); U1 = isl_mat_sub_alloc(U, nparam+pos, 1, 0, bset->n_eq); U1 = isl_mat_lin_to_aff(U1); isl_mat_free(U); C = isl_mat_product(U1, C); if (!C) return -1; if (!isl_int_is_divisible_by(C->row[1][0], C->row[0][0])) { bset = isl_basic_set_copy(bset); bset = isl_basic_set_set_to_empty(bset); isl_basic_set_free(bset); isl_int_set_si(*modulo, 1); isl_int_set_si(*residue, 0); return 0; } isl_int_divexact(*residue, C->row[1][0], C->row[0][0]); isl_int_fdiv_r(*residue, *residue, *modulo); isl_mat_free(C); return 0; error: isl_mat_free(H); isl_mat_free(U); return -1; }
/* 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; }