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
= 0; i
< bmap
->n_ineq
; ++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
);
53 isl_int_clear(opt_denom
);
56 ISL_F_SET(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
60 isl_basic_map_free(bmap
);
64 /* Make eq[row][col] of both bmaps equal so we can add the row
65 * add the column to the common matrix.
66 * Note that because of the echelon form, the columns of row row
67 * after column col are zero.
69 static void set_common_multiple(
70 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
,
71 unsigned row
, unsigned col
)
75 if (isl_int_eq(bset1
->eq
[row
][col
], bset2
->eq
[row
][col
]))
80 isl_int_lcm(m
, bset1
->eq
[row
][col
], bset2
->eq
[row
][col
]);
81 isl_int_divexact(c
, m
, bset1
->eq
[row
][col
]);
82 isl_seq_scale(bset1
->eq
[row
], bset1
->eq
[row
], c
, col
+1);
83 isl_int_divexact(c
, m
, bset2
->eq
[row
][col
]);
84 isl_seq_scale(bset2
->eq
[row
], bset2
->eq
[row
], c
, col
+1);
89 /* Delete a given equality, moving all the following equalities one up.
91 static void delete_row(struct isl_basic_set
*bset
, unsigned row
)
98 for (r
= row
; r
< bset
->n_eq
; ++r
)
99 bset
->eq
[r
] = bset
->eq
[r
+1];
100 bset
->eq
[bset
->n_eq
] = t
;
103 /* Make first row entries in column col of bset1 identical to
104 * those of bset2, using the fact that entry bset1->eq[row][col]=a
105 * is non-zero. Initially, these elements of bset1 are all zero.
106 * For each row i < row, we set
107 * A[i] = a * A[i] + B[i][col] * A[row]
110 * A[i][col] = B[i][col] = a * old(B[i][col])
112 static void construct_column(
113 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
,
114 unsigned row
, unsigned col
)
123 total
= 1 + isl_basic_set_n_dim(bset1
);
124 for (r
= 0; r
< row
; ++r
) {
125 if (isl_int_is_zero(bset2
->eq
[r
][col
]))
127 isl_int_gcd(b
, bset2
->eq
[r
][col
], bset1
->eq
[row
][col
]);
128 isl_int_divexact(a
, bset1
->eq
[row
][col
], b
);
129 isl_int_divexact(b
, bset2
->eq
[r
][col
], b
);
130 isl_seq_combine(bset1
->eq
[r
], a
, bset1
->eq
[r
],
131 b
, bset1
->eq
[row
], total
);
132 isl_seq_scale(bset2
->eq
[r
], bset2
->eq
[r
], a
, total
);
136 delete_row(bset1
, row
);
139 /* Make first row entries in column col of bset1 identical to
140 * those of bset2, using only these entries of the two matrices.
141 * Let t be the last row with different entries.
142 * For each row i < t, we set
143 * A[i] = (A[t][col]-B[t][col]) * A[i] + (B[i][col]-A[i][col) * A[t]
144 * B[i] = (A[t][col]-B[t][col]) * B[i] + (B[i][col]-A[i][col) * B[t]
146 * A[i][col] = B[i][col] = old(A[t][col]*B[i][col]-A[i][col]*B[t][col])
148 static int transform_column(
149 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
,
150 unsigned row
, unsigned col
)
156 for (t
= row
-1; t
>= 0; --t
)
157 if (isl_int_ne(bset1
->eq
[t
][col
], bset2
->eq
[t
][col
]))
162 total
= 1 + isl_basic_set_n_dim(bset1
);
166 isl_int_sub(b
, bset1
->eq
[t
][col
], bset2
->eq
[t
][col
]);
167 for (i
= 0; i
< t
; ++i
) {
168 isl_int_sub(a
, bset2
->eq
[i
][col
], bset1
->eq
[i
][col
]);
169 isl_int_gcd(g
, a
, b
);
170 isl_int_divexact(a
, a
, g
);
171 isl_int_divexact(g
, b
, g
);
172 isl_seq_combine(bset1
->eq
[i
], g
, bset1
->eq
[i
], a
, bset1
->eq
[t
],
174 isl_seq_combine(bset2
->eq
[i
], g
, bset2
->eq
[i
], a
, bset2
->eq
[t
],
180 delete_row(bset1
, t
);
181 delete_row(bset2
, t
);
185 /* The implementation is based on Section 5.2 of Michael Karr,
186 * "Affine Relationships Among Variables of a Program",
187 * except that the echelon form we use starts from the last column
188 * and that we are dealing with integer coefficients.
190 static struct isl_basic_set
*affine_hull(
191 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
197 total
= 1 + isl_basic_set_n_dim(bset1
);
200 for (col
= total
-1; col
>= 0; --col
) {
201 int is_zero1
= row
>= bset1
->n_eq
||
202 isl_int_is_zero(bset1
->eq
[row
][col
]);
203 int is_zero2
= row
>= bset2
->n_eq
||
204 isl_int_is_zero(bset2
->eq
[row
][col
]);
205 if (!is_zero1
&& !is_zero2
) {
206 set_common_multiple(bset1
, bset2
, row
, col
);
208 } else if (!is_zero1
&& is_zero2
) {
209 construct_column(bset1
, bset2
, row
, col
);
210 } else if (is_zero1
&& !is_zero2
) {
211 construct_column(bset2
, bset1
, row
, col
);
213 if (transform_column(bset1
, bset2
, row
, col
))
217 isl_basic_set_free(bset2
);
218 isl_assert(ctx
, row
== bset1
->n_eq
, goto error
);
221 isl_basic_set_free(bset1
);
225 static struct isl_basic_set
*isl_basic_set_from_vec(struct isl_ctx
*ctx
,
230 struct isl_basic_set
*bset
= NULL
;
235 isl_assert(ctx
, vec
->size
!= 0, goto error
);
237 bset
= isl_basic_set_alloc(ctx
, 0, vec
->size
- 1, 0, vec
->size
- 1, 0);
240 dim
= isl_basic_set_n_dim(bset
);
241 for (i
= dim
- 1; i
>= 0; --i
) {
242 k
= isl_basic_set_alloc_equality(bset
);
245 isl_seq_clr(bset
->eq
[k
], 1 + dim
);
246 isl_int_neg(bset
->eq
[k
][0], vec
->block
.data
[1 + i
]);
247 isl_int_set(bset
->eq
[k
][1 + i
], vec
->block
.data
[0]);
249 isl_vec_free(ctx
, vec
);
253 isl_basic_set_free(bset
);
254 isl_vec_free(ctx
, vec
);
258 /* Find an integer point in "bset" that lies outside of the equality
260 * If "up" is true, look for a point satisfying e(x) - 1 >= 0.
261 * Otherwise, look for a point satisfying -e(x) - 1 >= 0 (i.e., e(x) <= -1).
262 * The point, if found, is returned as a singleton set.
263 * If no point can be found, the empty set is returned.
265 static struct isl_basic_set
*outside_point(struct isl_ctx
*ctx
,
266 struct isl_basic_set
*bset
, isl_int
*eq
, int up
)
268 struct isl_basic_set
*slice
= NULL
;
269 struct isl_vec
*sample
;
270 struct isl_basic_set
*point
;
274 slice
= isl_basic_set_copy(bset
);
277 dim
= isl_basic_set_n_dim(slice
);
278 slice
= isl_basic_set_extend(slice
, 0, dim
, 0, 0, 1);
279 k
= isl_basic_set_alloc_inequality(slice
);
283 isl_seq_cpy(slice
->ineq
[k
], eq
, 1 + dim
);
285 isl_seq_neg(slice
->ineq
[k
], eq
, 1 + dim
);
286 isl_int_sub_ui(slice
->ineq
[k
][0], slice
->ineq
[k
][0], 1);
288 sample
= isl_basic_set_sample(slice
);
291 if (sample
->size
== 0) {
292 isl_vec_free(ctx
, sample
);
293 point
= isl_basic_set_empty_like(bset
);
295 point
= isl_basic_set_from_vec(ctx
, sample
);
299 isl_basic_set_free(slice
);
303 /* Look for all equalities satisfied by the integer points in bmap
304 * that are independent of the equalities already explicitly available
307 * We first remove all equalities already explicitly available,
308 * then look for additional equalities in the reduced space
309 * and then transform the result to the original space.
310 * The original equalities are _not_ added to this set. This is
311 * the responsibility of the calling function.
312 * The resulting basic set has all meaning about the dimensions removed.
313 * In particular, dimensions that correspond to existential variables
314 * in bmap and that are found to be fixed are not removed.
316 * The additional equalities are obtained by successively looking for
317 * a point that is affinely independent of the points found so far.
318 * In particular, for each equality satisfied by the points so far,
319 * we check if there is any point on a hyperplane parallel to the
320 * corresponding hyperplane shifted by at least one (in either direction).
322 static struct isl_basic_set
*equalities_in_underlying_set(
323 struct isl_basic_map
*bmap
)
326 struct isl_mat
*T2
= NULL
;
327 struct isl_basic_set
*bset
= NULL
;
328 struct isl_basic_set
*hull
= NULL
;
329 struct isl_vec
*sample
;
333 bset
= isl_basic_map_underlying_set(bmap
);
334 bset
= isl_basic_set_remove_equalities(bset
, NULL
, &T2
);
339 sample
= isl_basic_set_sample(isl_basic_set_copy(bset
));
342 if (sample
->size
== 0) {
343 isl_vec_free(ctx
, sample
);
344 hull
= isl_basic_set_empty_like(bset
);
346 hull
= isl_basic_set_from_vec(ctx
, sample
);
348 dim
= isl_basic_set_n_dim(bset
);
349 for (i
= 0; i
< dim
; ++i
) {
350 struct isl_basic_set
*point
;
351 if (ISL_F_ISSET(hull
, ISL_BASIC_SET_EMPTY
))
353 for (j
= 0; j
< hull
->n_eq
; ++j
) {
354 point
= outside_point(ctx
, bset
, hull
->eq
[j
], 1);
357 if (!ISL_F_ISSET(point
, ISL_BASIC_SET_EMPTY
))
359 isl_basic_set_free(point
);
360 point
= outside_point(ctx
, bset
, hull
->eq
[j
], 0);
363 if (!ISL_F_ISSET(point
, ISL_BASIC_SET_EMPTY
))
365 isl_basic_set_free(point
);
369 hull
= affine_hull(hull
, point
);
371 isl_basic_set_free(bset
);
373 hull
= isl_basic_set_preimage(ctx
, hull
, T2
);
377 isl_mat_free(ctx
, T2
);
378 isl_basic_set_free(bset
);
379 isl_basic_set_free(hull
);
383 /* Detect and make explicit all equalities satisfied by the (integer)
386 struct isl_basic_map
*isl_basic_map_detect_equalities(
387 struct isl_basic_map
*bmap
)
390 struct isl_basic_set
*hull
= NULL
;
394 if (bmap
->n_ineq
== 0)
396 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
398 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
))
400 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
401 return isl_basic_map_implicit_equalities(bmap
);
403 hull
= equalities_in_underlying_set(isl_basic_map_copy(bmap
));
406 bmap
= isl_basic_map_extend_dim(bmap
, isl_dim_copy(bmap
->dim
), 0,
408 for (i
= 0; i
< hull
->n_eq
; ++i
) {
409 j
= isl_basic_map_alloc_equality(bmap
);
412 isl_seq_cpy(bmap
->eq
[j
], hull
->eq
[i
],
413 1 + isl_basic_set_total_dim(hull
));
415 isl_basic_set_free(hull
);
416 ISL_F_SET(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
| ISL_BASIC_MAP_ALL_EQUALITIES
);
417 bmap
= isl_basic_map_simplify(bmap
);
418 return isl_basic_map_finalize(bmap
);
420 isl_basic_set_free(hull
);
421 isl_basic_map_free(bmap
);
425 /* After computing the rational affine hull (by detecting the implicit
426 * equalities), we compute the additional equalities satisfied by
427 * the integer points (if any) and add the original equalities back in.
429 struct isl_basic_map
*isl_basic_map_affine_hull(struct isl_basic_map
*bmap
)
431 struct isl_basic_set
*hull
= NULL
;
433 bmap
= isl_basic_map_implicit_equalities(bmap
);
436 if (bmap
->n_ineq
== 0)
439 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
440 bmap
= isl_basic_map_cow(bmap
);
441 isl_basic_map_free_inequality(bmap
, bmap
->n_ineq
);
445 hull
= equalities_in_underlying_set(isl_basic_map_copy(bmap
));
449 bmap
= isl_basic_map_cow(bmap
);
452 isl_basic_map_free_inequality(bmap
, bmap
->n_ineq
);
453 bmap
= isl_basic_map_intersect(bmap
,
454 isl_basic_map_overlying_set(hull
,
455 isl_basic_map_copy(bmap
)));
457 return isl_basic_map_finalize(bmap
);
459 isl_basic_set_free(hull
);
460 isl_basic_map_free(bmap
);
464 struct isl_basic_set
*isl_basic_set_affine_hull(struct isl_basic_set
*bset
)
466 return (struct isl_basic_set
*)
467 isl_basic_map_affine_hull((struct isl_basic_map
*)bset
);
470 struct isl_basic_map
*isl_map_affine_hull(struct isl_map
*map
)
473 struct isl_basic_map
*model
= NULL
;
474 struct isl_basic_map
*hull
= NULL
;
481 hull
= isl_basic_map_empty_like_map(map
);
486 map
= isl_map_align_divs(map
);
487 model
= isl_basic_map_copy(map
->p
[0]);
488 set
= isl_map_underlying_set(map
);
489 set
= isl_set_cow(set
);
493 for (i
= 0; i
< set
->n
; ++i
) {
494 set
->p
[i
] = isl_basic_set_cow(set
->p
[i
]);
495 set
->p
[i
] = isl_basic_set_affine_hull(set
->p
[i
]);
496 set
->p
[i
] = isl_basic_set_gauss(set
->p
[i
], NULL
);
500 set
= isl_set_remove_empty_parts(set
);
502 hull
= isl_basic_map_empty_like(model
);
503 isl_basic_map_free(model
);
505 struct isl_basic_set
*bset
;
507 set
->p
[0] = affine_hull(set
->p
[0], set
->p
[--set
->n
]);
511 bset
= isl_basic_set_copy(set
->p
[0]);
512 hull
= isl_basic_map_overlying_set(bset
, model
);
515 hull
= isl_basic_map_simplify(hull
);
516 return isl_basic_map_finalize(hull
);
518 isl_basic_map_free(model
);
523 struct isl_basic_set
*isl_set_affine_hull(struct isl_set
*set
)
525 return (struct isl_basic_set
*)
526 isl_map_affine_hull((struct isl_map
*)set
);