6 #include "isl_map_private.h"
7 #include "isl_equalities.h"
8 #include "isl_sample.h"
10 struct isl_basic_map *isl_basic_map_implicit_equalities(
11 struct isl_basic_map *bmap)
22 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
24 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NO_IMPLICIT))
28 rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
30 isl_int_init(opt_denom);
32 isl_int_set_si(opt_denom, 1);
33 for (i = bmap->n_ineq - 1; i >= 0; --i) {
34 enum isl_lp_result res;
35 res = isl_solve_lp(bmap, 1, bmap->ineq[i]+1, ctx->one,
36 &opt, rational ? &opt_denom : NULL);
37 if (res == isl_lp_unbounded)
39 if (res == isl_lp_error)
41 if (res == isl_lp_empty) {
42 bmap = isl_basic_map_set_to_empty(bmap);
45 if (!isl_int_is_one(opt_denom))
47 isl_int_add(opt, opt, bmap->ineq[i][0]);
48 if (isl_int_is_zero(opt))
49 isl_basic_map_inequality_to_equality(bmap, i);
51 isl_int_clear(opt_denom);
54 ISL_F_SET(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
58 isl_basic_map_free(bmap);
62 /* Make eq[row][col] of both bmaps equal so we can add the row
63 * add the column to the common matrix.
64 * Note that because of the echelon form, the columns of row row
65 * after column col are zero.
67 static void set_common_multiple(
68 struct isl_basic_set *bset1, struct isl_basic_set *bset2,
69 unsigned row, unsigned col)
73 if (isl_int_eq(bset1->eq[row][col], bset2->eq[row][col]))
78 isl_int_lcm(m, bset1->eq[row][col], bset2->eq[row][col]);
79 isl_int_divexact(c, m, bset1->eq[row][col]);
80 isl_seq_scale(bset1->eq[row], bset1->eq[row], c, col+1);
81 isl_int_divexact(c, m, bset2->eq[row][col]);
82 isl_seq_scale(bset2->eq[row], bset2->eq[row], c, col+1);
87 /* Delete a given equality, moving all the following equalities one up.
89 static void delete_row(struct isl_basic_set *bset, unsigned row)
96 for (r = row; r < bset->n_eq; ++r)
97 bset->eq[r] = bset->eq[r+1];
98 bset->eq[bset->n_eq] = t;
101 /* Make first row entries in column col of bset1 identical to
102 * those of bset2, using the fact that entry bset1->eq[row][col]=a
103 * is non-zero. Initially, these elements of bset1 are all zero.
104 * For each row i < row, we set
105 * A[i] = a * A[i] + B[i][col] * A[row]
108 * A[i][col] = B[i][col] = a * old(B[i][col])
110 static void construct_column(
111 struct isl_basic_set *bset1, struct isl_basic_set *bset2,
112 unsigned row, unsigned col)
121 total = 1 + isl_basic_set_n_dim(bset1);
122 for (r = 0; r < row; ++r) {
123 if (isl_int_is_zero(bset2->eq[r][col]))
125 isl_int_gcd(b, bset2->eq[r][col], bset1->eq[row][col]);
126 isl_int_divexact(a, bset1->eq[row][col], b);
127 isl_int_divexact(b, bset2->eq[r][col], b);
128 isl_seq_combine(bset1->eq[r], a, bset1->eq[r],
129 b, bset1->eq[row], total);
130 isl_seq_scale(bset2->eq[r], bset2->eq[r], a, total);
134 delete_row(bset1, row);
137 /* Make first row entries in column col of bset1 identical to
138 * those of bset2, using only these entries of the two matrices.
139 * Let t be the last row with different entries.
140 * For each row i < t, we set
141 * A[i] = (A[t][col]-B[t][col]) * A[i] + (B[i][col]-A[i][col) * A[t]
142 * B[i] = (A[t][col]-B[t][col]) * B[i] + (B[i][col]-A[i][col) * B[t]
144 * A[i][col] = B[i][col] = old(A[t][col]*B[i][col]-A[i][col]*B[t][col])
146 static int transform_column(
147 struct isl_basic_set *bset1, struct isl_basic_set *bset2,
148 unsigned row, unsigned col)
154 for (t = row-1; t >= 0; --t)
155 if (isl_int_ne(bset1->eq[t][col], bset2->eq[t][col]))
160 total = 1 + isl_basic_set_n_dim(bset1);
164 isl_int_sub(b, bset1->eq[t][col], bset2->eq[t][col]);
165 for (i = 0; i < t; ++i) {
166 isl_int_sub(a, bset2->eq[i][col], bset1->eq[i][col]);
167 isl_int_gcd(g, a, b);
168 isl_int_divexact(a, a, g);
169 isl_int_divexact(g, b, g);
170 isl_seq_combine(bset1->eq[i], g, bset1->eq[i], a, bset1->eq[t],
172 isl_seq_combine(bset2->eq[i], g, bset2->eq[i], a, bset2->eq[t],
178 delete_row(bset1, t);
179 delete_row(bset2, t);
183 /* The implementation is based on Section 5.2 of Michael Karr,
184 * "Affine Relationships Among Variables of a Program",
185 * except that the echelon form we use starts from the last column
186 * and that we are dealing with integer coefficients.
188 static struct isl_basic_set *affine_hull(
189 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
195 total = 1 + isl_basic_set_n_dim(bset1);
198 for (col = total-1; col >= 0; --col) {
199 int is_zero1 = row >= bset1->n_eq ||
200 isl_int_is_zero(bset1->eq[row][col]);
201 int is_zero2 = row >= bset2->n_eq ||
202 isl_int_is_zero(bset2->eq[row][col]);
203 if (!is_zero1 && !is_zero2) {
204 set_common_multiple(bset1, bset2, row, col);
206 } else if (!is_zero1 && is_zero2) {
207 construct_column(bset1, bset2, row, col);
208 } else if (is_zero1 && !is_zero2) {
209 construct_column(bset2, bset1, row, col);
211 if (transform_column(bset1, bset2, row, col))
215 isl_basic_set_free(bset2);
216 isl_assert(ctx, row == bset1->n_eq, goto error);
219 isl_basic_set_free(bset1);
223 static struct isl_basic_set *isl_basic_set_from_vec(struct isl_ctx *ctx,
228 struct isl_basic_set *bset = NULL;
233 isl_assert(ctx, vec->size != 0, goto error);
235 bset = isl_basic_set_alloc(ctx, 0, vec->size - 1, 0, vec->size - 1, 0);
238 dim = isl_basic_set_n_dim(bset);
239 for (i = dim - 1; i >= 0; --i) {
240 k = isl_basic_set_alloc_equality(bset);
243 isl_seq_clr(bset->eq[k], 1 + dim);
244 isl_int_neg(bset->eq[k][0], vec->block.data[1 + i]);
245 isl_int_set(bset->eq[k][1 + i], vec->block.data[0]);
247 isl_vec_free(ctx, vec);
251 isl_basic_set_free(bset);
252 isl_vec_free(ctx, vec);
256 /* Find an integer point in "bset" that lies outside of the equality
258 * If "up" is true, look for a point satisfying e(x) - 1 >= 0.
259 * Otherwise, look for a point satisfying -e(x) - 1 >= 0 (i.e., e(x) <= -1).
260 * The point, if found, is returned as a singleton set.
261 * If no point can be found, the empty set is returned.
263 static struct isl_basic_set *outside_point(struct isl_ctx *ctx,
264 struct isl_basic_set *bset, isl_int *eq, int up)
266 struct isl_basic_set *slice = NULL;
267 struct isl_vec *sample;
268 struct isl_basic_set *point;
272 slice = isl_basic_set_copy(bset);
275 dim = isl_basic_set_n_dim(slice);
276 slice = isl_basic_set_extend(slice, 0, dim, 0, 0, 1);
277 k = isl_basic_set_alloc_inequality(slice);
281 isl_seq_cpy(slice->ineq[k], eq, 1 + dim);
283 isl_seq_neg(slice->ineq[k], eq, 1 + dim);
284 isl_int_sub_ui(slice->ineq[k][0], slice->ineq[k][0], 1);
286 sample = isl_basic_set_sample(slice);
289 if (sample->size == 0) {
290 isl_vec_free(ctx, sample);
291 point = isl_basic_set_empty_like(bset);
293 point = isl_basic_set_from_vec(ctx, sample);
297 isl_basic_set_free(slice);
301 /* Look for all equalities satisfied by the integer points in bmap
302 * that are independent of the equalities already explicitly available
305 * We first remove all equalities already explicitly available,
306 * then look for additional equalities in the reduced space
307 * and then transform the result to the original space.
308 * The original equalities are _not_ added to this set. This is
309 * the responsibility of the calling function.
310 * The resulting basic set has all meaning about the dimensions removed.
311 * In particular, dimensions that correspond to existential variables
312 * in bmap and that are found to be fixed are not removed.
314 * The additional equalities are obtained by successively looking for
315 * a point that is affinely independent of the points found so far.
316 * In particular, for each equality satisfied by the points so far,
317 * we check if there is any point on a hyperplane parallel to the
318 * corresponding hyperplane shifted by at least one (in either direction).
320 static struct isl_basic_set *equalities_in_underlying_set(
321 struct isl_basic_map *bmap)
324 struct isl_mat *T2 = NULL;
325 struct isl_basic_set *bset = NULL;
326 struct isl_basic_set *hull = NULL;
327 struct isl_vec *sample;
331 bset = isl_basic_map_underlying_set(bmap);
332 bset = isl_basic_set_remove_equalities(bset, NULL, &T2);
337 sample = isl_basic_set_sample(isl_basic_set_copy(bset));
340 if (sample->size == 0) {
341 isl_vec_free(ctx, sample);
342 hull = isl_basic_set_empty_like(bset);
344 hull = isl_basic_set_from_vec(ctx, sample);
346 dim = isl_basic_set_n_dim(bset);
347 for (i = 0; i < dim; ++i) {
348 struct isl_basic_set *point;
349 if (ISL_F_ISSET(hull, ISL_BASIC_SET_EMPTY))
351 for (j = 0; j < hull->n_eq; ++j) {
352 point = outside_point(ctx, bset, hull->eq[j], 1);
355 if (!ISL_F_ISSET(point, ISL_BASIC_SET_EMPTY))
357 isl_basic_set_free(point);
358 point = outside_point(ctx, bset, hull->eq[j], 0);
361 if (!ISL_F_ISSET(point, ISL_BASIC_SET_EMPTY))
363 isl_basic_set_free(point);
367 hull = affine_hull(hull, point);
369 isl_basic_set_free(bset);
371 hull = isl_basic_set_preimage(hull, T2);
375 isl_mat_free(ctx, T2);
376 isl_basic_set_free(bset);
377 isl_basic_set_free(hull);
381 /* Detect and make explicit all equalities satisfied by the (integer)
384 struct isl_basic_map *isl_basic_map_detect_equalities(
385 struct isl_basic_map *bmap)
388 struct isl_basic_set *hull = NULL;
392 if (bmap->n_ineq == 0)
394 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
396 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_ALL_EQUALITIES))
398 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
399 return isl_basic_map_implicit_equalities(bmap);
401 hull = equalities_in_underlying_set(isl_basic_map_copy(bmap));
404 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim), 0,
406 for (i = 0; i < hull->n_eq; ++i) {
407 j = isl_basic_map_alloc_equality(bmap);
410 isl_seq_cpy(bmap->eq[j], hull->eq[i],
411 1 + isl_basic_set_total_dim(hull));
413 isl_basic_set_free(hull);
414 ISL_F_SET(bmap, ISL_BASIC_MAP_NO_IMPLICIT | ISL_BASIC_MAP_ALL_EQUALITIES);
415 bmap = isl_basic_map_simplify(bmap);
416 return isl_basic_map_finalize(bmap);
418 isl_basic_set_free(hull);
419 isl_basic_map_free(bmap);
423 /* After computing the rational affine hull (by detecting the implicit
424 * equalities), we compute the additional equalities satisfied by
425 * the integer points (if any) and add the original equalities back in.
427 struct isl_basic_map *isl_basic_map_affine_hull(struct isl_basic_map *bmap)
429 struct isl_basic_set *hull = NULL;
431 bmap = isl_basic_map_implicit_equalities(bmap);
434 if (bmap->n_ineq == 0)
437 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
438 bmap = isl_basic_map_cow(bmap);
439 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
443 hull = equalities_in_underlying_set(isl_basic_map_copy(bmap));
447 bmap = isl_basic_map_cow(bmap);
450 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
451 bmap = isl_basic_map_intersect(bmap,
452 isl_basic_map_overlying_set(hull,
453 isl_basic_map_copy(bmap)));
455 return isl_basic_map_finalize(bmap);
457 isl_basic_set_free(hull);
458 isl_basic_map_free(bmap);
462 struct isl_basic_set *isl_basic_set_affine_hull(struct isl_basic_set *bset)
464 return (struct isl_basic_set *)
465 isl_basic_map_affine_hull((struct isl_basic_map *)bset);
468 struct isl_basic_map *isl_map_affine_hull(struct isl_map *map)
471 struct isl_basic_map *model = NULL;
472 struct isl_basic_map *hull = NULL;
479 hull = isl_basic_map_empty_like_map(map);
484 map = isl_map_align_divs(map);
485 model = isl_basic_map_copy(map->p[0]);
486 set = isl_map_underlying_set(map);
487 set = isl_set_cow(set);
491 for (i = 0; i < set->n; ++i) {
492 set->p[i] = isl_basic_set_cow(set->p[i]);
493 set->p[i] = isl_basic_set_affine_hull(set->p[i]);
494 set->p[i] = isl_basic_set_gauss(set->p[i], NULL);
498 set = isl_set_remove_empty_parts(set);
500 hull = isl_basic_map_empty_like(model);
501 isl_basic_map_free(model);
503 struct isl_basic_set *bset;
505 set->p[0] = affine_hull(set->p[0], set->p[--set->n]);
509 bset = isl_basic_set_copy(set->p[0]);
510 hull = isl_basic_map_overlying_set(bset, model);
513 hull = isl_basic_map_simplify(hull);
514 return isl_basic_map_finalize(hull);
516 isl_basic_map_free(model);
521 struct isl_basic_set *isl_set_affine_hull(struct isl_set *set)
523 return (struct isl_basic_set *)
524 isl_map_affine_hull((struct isl_map *)set);