6 #include "isl_map_private.h"
7 #include "isl_equalities.h"
8 #include "isl_sample.h"
11 struct isl_basic_map
*isl_basic_map_implicit_equalities(
12 struct isl_basic_map
*bmap
)
19 bmap
= isl_basic_map_gauss(bmap
, NULL
);
20 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
22 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
))
24 if (bmap
->n_ineq
<= 1)
27 tab
= isl_tab_from_basic_map(bmap
);
28 tab
= isl_tab_detect_equalities(bmap
->ctx
, tab
);
29 bmap
= isl_basic_map_update_from_tab(bmap
, tab
);
30 isl_tab_free(bmap
->ctx
, tab
);
31 ISL_F_SET(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
35 /* Make eq[row][col] of both bmaps equal so we can add the row
36 * add the column to the common matrix.
37 * Note that because of the echelon form, the columns of row row
38 * after column col are zero.
40 static void set_common_multiple(
41 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
,
42 unsigned row
, unsigned col
)
46 if (isl_int_eq(bset1
->eq
[row
][col
], bset2
->eq
[row
][col
]))
51 isl_int_lcm(m
, bset1
->eq
[row
][col
], bset2
->eq
[row
][col
]);
52 isl_int_divexact(c
, m
, bset1
->eq
[row
][col
]);
53 isl_seq_scale(bset1
->eq
[row
], bset1
->eq
[row
], c
, col
+1);
54 isl_int_divexact(c
, m
, bset2
->eq
[row
][col
]);
55 isl_seq_scale(bset2
->eq
[row
], bset2
->eq
[row
], c
, col
+1);
60 /* Delete a given equality, moving all the following equalities one up.
62 static void delete_row(struct isl_basic_set
*bset
, unsigned row
)
69 for (r
= row
; r
< bset
->n_eq
; ++r
)
70 bset
->eq
[r
] = bset
->eq
[r
+1];
71 bset
->eq
[bset
->n_eq
] = t
;
74 /* Make first row entries in column col of bset1 identical to
75 * those of bset2, using the fact that entry bset1->eq[row][col]=a
76 * is non-zero. Initially, these elements of bset1 are all zero.
77 * For each row i < row, we set
78 * A[i] = a * A[i] + B[i][col] * A[row]
81 * A[i][col] = B[i][col] = a * old(B[i][col])
83 static void construct_column(
84 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
,
85 unsigned row
, unsigned col
)
94 total
= 1 + isl_basic_set_n_dim(bset1
);
95 for (r
= 0; r
< row
; ++r
) {
96 if (isl_int_is_zero(bset2
->eq
[r
][col
]))
98 isl_int_gcd(b
, bset2
->eq
[r
][col
], bset1
->eq
[row
][col
]);
99 isl_int_divexact(a
, bset1
->eq
[row
][col
], b
);
100 isl_int_divexact(b
, bset2
->eq
[r
][col
], b
);
101 isl_seq_combine(bset1
->eq
[r
], a
, bset1
->eq
[r
],
102 b
, bset1
->eq
[row
], total
);
103 isl_seq_scale(bset2
->eq
[r
], bset2
->eq
[r
], a
, total
);
107 delete_row(bset1
, row
);
110 /* Make first row entries in column col of bset1 identical to
111 * those of bset2, using only these entries of the two matrices.
112 * Let t be the last row with different entries.
113 * For each row i < t, we set
114 * A[i] = (A[t][col]-B[t][col]) * A[i] + (B[i][col]-A[i][col) * A[t]
115 * B[i] = (A[t][col]-B[t][col]) * B[i] + (B[i][col]-A[i][col) * B[t]
117 * A[i][col] = B[i][col] = old(A[t][col]*B[i][col]-A[i][col]*B[t][col])
119 static int transform_column(
120 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
,
121 unsigned row
, unsigned col
)
127 for (t
= row
-1; t
>= 0; --t
)
128 if (isl_int_ne(bset1
->eq
[t
][col
], bset2
->eq
[t
][col
]))
133 total
= 1 + isl_basic_set_n_dim(bset1
);
137 isl_int_sub(b
, bset1
->eq
[t
][col
], bset2
->eq
[t
][col
]);
138 for (i
= 0; i
< t
; ++i
) {
139 isl_int_sub(a
, bset2
->eq
[i
][col
], bset1
->eq
[i
][col
]);
140 isl_int_gcd(g
, a
, b
);
141 isl_int_divexact(a
, a
, g
);
142 isl_int_divexact(g
, b
, g
);
143 isl_seq_combine(bset1
->eq
[i
], g
, bset1
->eq
[i
], a
, bset1
->eq
[t
],
145 isl_seq_combine(bset2
->eq
[i
], g
, bset2
->eq
[i
], a
, bset2
->eq
[t
],
151 delete_row(bset1
, t
);
152 delete_row(bset2
, t
);
156 /* The implementation is based on Section 5.2 of Michael Karr,
157 * "Affine Relationships Among Variables of a Program",
158 * except that the echelon form we use starts from the last column
159 * and that we are dealing with integer coefficients.
161 static struct isl_basic_set
*affine_hull(
162 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
168 total
= 1 + isl_basic_set_n_dim(bset1
);
171 for (col
= total
-1; col
>= 0; --col
) {
172 int is_zero1
= row
>= bset1
->n_eq
||
173 isl_int_is_zero(bset1
->eq
[row
][col
]);
174 int is_zero2
= row
>= bset2
->n_eq
||
175 isl_int_is_zero(bset2
->eq
[row
][col
]);
176 if (!is_zero1
&& !is_zero2
) {
177 set_common_multiple(bset1
, bset2
, row
, col
);
179 } else if (!is_zero1
&& is_zero2
) {
180 construct_column(bset1
, bset2
, row
, col
);
181 } else if (is_zero1
&& !is_zero2
) {
182 construct_column(bset2
, bset1
, row
, col
);
184 if (transform_column(bset1
, bset2
, row
, col
))
188 isl_basic_set_free(bset2
);
189 isl_assert(ctx
, row
== bset1
->n_eq
, goto error
);
192 isl_basic_set_free(bset1
);
196 static struct isl_basic_set
*isl_basic_set_from_vec(struct isl_ctx
*ctx
,
201 struct isl_basic_set
*bset
= NULL
;
206 isl_assert(ctx
, vec
->size
!= 0, goto error
);
208 bset
= isl_basic_set_alloc(ctx
, 0, vec
->size
- 1, 0, vec
->size
- 1, 0);
211 dim
= isl_basic_set_n_dim(bset
);
212 for (i
= dim
- 1; i
>= 0; --i
) {
213 k
= isl_basic_set_alloc_equality(bset
);
216 isl_seq_clr(bset
->eq
[k
], 1 + dim
);
217 isl_int_neg(bset
->eq
[k
][0], vec
->block
.data
[1 + i
]);
218 isl_int_set(bset
->eq
[k
][1 + i
], vec
->block
.data
[0]);
220 isl_vec_free(ctx
, vec
);
224 isl_basic_set_free(bset
);
225 isl_vec_free(ctx
, vec
);
229 /* Find an integer point in "bset" that lies outside of the equality
231 * If "up" is true, look for a point satisfying e(x) - 1 >= 0.
232 * Otherwise, look for a point satisfying -e(x) - 1 >= 0 (i.e., e(x) <= -1).
233 * The point, if found, is returned as a singleton set.
234 * If no point can be found, the empty set is returned.
236 static struct isl_basic_set
*outside_point(struct isl_ctx
*ctx
,
237 struct isl_basic_set
*bset
, isl_int
*eq
, int up
)
239 struct isl_basic_set
*slice
= NULL
;
240 struct isl_vec
*sample
;
241 struct isl_basic_set
*point
;
245 slice
= isl_basic_set_copy(bset
);
248 dim
= isl_basic_set_n_dim(slice
);
249 slice
= isl_basic_set_extend(slice
, 0, dim
, 0, 0, 1);
250 k
= isl_basic_set_alloc_inequality(slice
);
254 isl_seq_cpy(slice
->ineq
[k
], eq
, 1 + dim
);
256 isl_seq_neg(slice
->ineq
[k
], eq
, 1 + dim
);
257 isl_int_sub_ui(slice
->ineq
[k
][0], slice
->ineq
[k
][0], 1);
259 sample
= isl_basic_set_sample(slice
);
262 if (sample
->size
== 0) {
263 isl_vec_free(ctx
, sample
);
264 point
= isl_basic_set_empty_like(bset
);
266 point
= isl_basic_set_from_vec(ctx
, sample
);
270 isl_basic_set_free(slice
);
274 /* Look for all equalities satisfied by the integer points in bmap
275 * that are independent of the equalities already explicitly available
278 * We first remove all equalities already explicitly available,
279 * then look for additional equalities in the reduced space
280 * and then transform the result to the original space.
281 * The original equalities are _not_ added to this set. This is
282 * the responsibility of the calling function.
283 * The resulting basic set has all meaning about the dimensions removed.
284 * In particular, dimensions that correspond to existential variables
285 * in bmap and that are found to be fixed are not removed.
287 * The additional equalities are obtained by successively looking for
288 * a point that is affinely independent of the points found so far.
289 * In particular, for each equality satisfied by the points so far,
290 * we check if there is any point on a hyperplane parallel to the
291 * corresponding hyperplane shifted by at least one (in either direction).
293 static struct isl_basic_set
*equalities_in_underlying_set(
294 struct isl_basic_map
*bmap
)
297 struct isl_mat
*T2
= NULL
;
298 struct isl_basic_set
*bset
= NULL
;
299 struct isl_basic_set
*hull
= NULL
;
300 struct isl_vec
*sample
;
304 bset
= isl_basic_map_underlying_set(bmap
);
305 bset
= isl_basic_set_remove_equalities(bset
, NULL
, &T2
);
310 sample
= isl_basic_set_sample(isl_basic_set_copy(bset
));
313 if (sample
->size
== 0) {
314 isl_vec_free(ctx
, sample
);
315 hull
= isl_basic_set_empty_like(bset
);
317 hull
= isl_basic_set_from_vec(ctx
, sample
);
319 dim
= isl_basic_set_n_dim(bset
);
320 for (i
= 0; i
< dim
; ++i
) {
321 struct isl_basic_set
*point
;
322 if (ISL_F_ISSET(hull
, ISL_BASIC_SET_EMPTY
))
324 for (j
= 0; j
< hull
->n_eq
; ++j
) {
325 point
= outside_point(ctx
, bset
, hull
->eq
[j
], 1);
328 if (!ISL_F_ISSET(point
, ISL_BASIC_SET_EMPTY
))
330 isl_basic_set_free(point
);
331 point
= outside_point(ctx
, bset
, hull
->eq
[j
], 0);
334 if (!ISL_F_ISSET(point
, ISL_BASIC_SET_EMPTY
))
336 isl_basic_set_free(point
);
340 hull
= affine_hull(hull
, point
);
342 isl_basic_set_free(bset
);
344 hull
= isl_basic_set_preimage(hull
, T2
);
348 isl_mat_free(ctx
, T2
);
349 isl_basic_set_free(bset
);
350 isl_basic_set_free(hull
);
354 /* Detect and make explicit all equalities satisfied by the (integer)
357 struct isl_basic_map
*isl_basic_map_detect_equalities(
358 struct isl_basic_map
*bmap
)
361 struct isl_basic_set
*hull
= NULL
;
365 if (bmap
->n_ineq
== 0)
367 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
369 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
))
371 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
372 return isl_basic_map_implicit_equalities(bmap
);
374 hull
= equalities_in_underlying_set(isl_basic_map_copy(bmap
));
377 bmap
= isl_basic_map_extend_dim(bmap
, isl_dim_copy(bmap
->dim
), 0,
379 for (i
= 0; i
< hull
->n_eq
; ++i
) {
380 j
= isl_basic_map_alloc_equality(bmap
);
383 isl_seq_cpy(bmap
->eq
[j
], hull
->eq
[i
],
384 1 + isl_basic_set_total_dim(hull
));
386 isl_basic_set_free(hull
);
387 ISL_F_SET(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
| ISL_BASIC_MAP_ALL_EQUALITIES
);
388 bmap
= isl_basic_map_simplify(bmap
);
389 return isl_basic_map_finalize(bmap
);
391 isl_basic_set_free(hull
);
392 isl_basic_map_free(bmap
);
396 /* After computing the rational affine hull (by detecting the implicit
397 * equalities), we compute the additional equalities satisfied by
398 * the integer points (if any) and add the original equalities back in.
400 struct isl_basic_map
*isl_basic_map_affine_hull(struct isl_basic_map
*bmap
)
402 struct isl_basic_set
*hull
= NULL
;
404 bmap
= isl_basic_map_implicit_equalities(bmap
);
407 if (bmap
->n_ineq
== 0)
410 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
411 bmap
= isl_basic_map_cow(bmap
);
412 isl_basic_map_free_inequality(bmap
, bmap
->n_ineq
);
416 hull
= equalities_in_underlying_set(isl_basic_map_copy(bmap
));
420 bmap
= isl_basic_map_cow(bmap
);
423 isl_basic_map_free_inequality(bmap
, bmap
->n_ineq
);
424 bmap
= isl_basic_map_intersect(bmap
,
425 isl_basic_map_overlying_set(hull
,
426 isl_basic_map_copy(bmap
)));
428 return isl_basic_map_finalize(bmap
);
430 isl_basic_set_free(hull
);
431 isl_basic_map_free(bmap
);
435 struct isl_basic_set
*isl_basic_set_affine_hull(struct isl_basic_set
*bset
)
437 return (struct isl_basic_set
*)
438 isl_basic_map_affine_hull((struct isl_basic_map
*)bset
);
441 struct isl_basic_map
*isl_map_affine_hull(struct isl_map
*map
)
444 struct isl_basic_map
*model
= NULL
;
445 struct isl_basic_map
*hull
= NULL
;
452 hull
= isl_basic_map_empty_like_map(map
);
457 map
= isl_map_align_divs(map
);
458 model
= isl_basic_map_copy(map
->p
[0]);
459 set
= isl_map_underlying_set(map
);
460 set
= isl_set_cow(set
);
464 for (i
= 0; i
< set
->n
; ++i
) {
465 set
->p
[i
] = isl_basic_set_cow(set
->p
[i
]);
466 set
->p
[i
] = isl_basic_set_affine_hull(set
->p
[i
]);
467 set
->p
[i
] = isl_basic_set_gauss(set
->p
[i
], NULL
);
471 set
= isl_set_remove_empty_parts(set
);
473 hull
= isl_basic_map_empty_like(model
);
474 isl_basic_map_free(model
);
476 struct isl_basic_set
*bset
;
478 set
->p
[0] = affine_hull(set
->p
[0], set
->p
[--set
->n
]);
482 bset
= isl_basic_set_copy(set
->p
[0]);
483 hull
= isl_basic_map_overlying_set(bset
, model
);
486 hull
= isl_basic_map_simplify(hull
);
487 return isl_basic_map_finalize(hull
);
489 isl_basic_map_free(model
);
494 struct isl_basic_set
*isl_set_affine_hull(struct isl_set
*set
)
496 return (struct isl_basic_set
*)
497 isl_map_affine_hull((struct isl_map
*)set
);