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(tab
);
29 bmap
= isl_basic_map_update_from_tab(bmap
, tab
);
31 bmap
= isl_basic_map_gauss(bmap
, NULL
);
32 ISL_F_SET(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
36 struct isl_basic_set
*isl_basic_set_implicit_equalities(
37 struct isl_basic_set
*bset
)
39 return (struct isl_basic_set
*)
40 isl_basic_map_implicit_equalities((struct isl_basic_map
*)bset
);
43 struct isl_map
*isl_map_implicit_equalities(struct isl_map
*map
)
50 for (i
= 0; i
< map
->n
; ++i
) {
51 map
->p
[i
] = isl_basic_map_implicit_equalities(map
->p
[i
]);
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
);
217 bset1
= isl_basic_set_normalize_constraints(bset1
);
220 isl_basic_set_free(bset1
);
224 static struct isl_basic_set
*isl_basic_set_from_vec(struct isl_vec
*vec
)
228 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
->el
[1 + i
]);
247 isl_int_set(bset
->eq
[k
][1 + i
], vec
->el
[0]);
253 isl_basic_set_free(bset
);
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 * Before solving an ILP problem, we first check if simply
266 * adding the normal of the constraint to one of the known
267 * integer points in the basic set yields another point
268 * inside the basic set.
270 static struct isl_basic_set
*outside_point(struct isl_ctx
*ctx
,
271 struct isl_basic_set
*bset
, isl_int
*eq
, int up
)
273 struct isl_basic_set
*slice
= NULL
;
274 struct isl_vec
*sample
;
275 struct isl_basic_set
*point
;
279 dim
= isl_basic_set_n_dim(bset
);
280 sample
= isl_vec_alloc(ctx
, 1 + dim
);
283 isl_int_set_si(sample
->block
.data
[0], 1);
284 isl_seq_combine(sample
->block
.data
+ 1,
285 ctx
->one
, bset
->sample
->block
.data
+ 1,
286 up
? ctx
->one
: ctx
->negone
, eq
+ 1, dim
);
287 if (isl_basic_set_contains(bset
, sample
))
288 return isl_basic_set_from_vec(sample
);
289 isl_vec_free(sample
);
292 slice
= isl_basic_set_copy(bset
);
295 slice
= isl_basic_set_cow(slice
);
296 slice
= isl_basic_set_extend(slice
, 0, dim
, 0, 0, 1);
297 k
= isl_basic_set_alloc_inequality(slice
);
301 isl_seq_cpy(slice
->ineq
[k
], eq
, 1 + dim
);
303 isl_seq_neg(slice
->ineq
[k
], eq
, 1 + dim
);
304 isl_int_sub_ui(slice
->ineq
[k
][0], slice
->ineq
[k
][0], 1);
306 sample
= isl_basic_set_sample(slice
);
309 if (sample
->size
== 0) {
310 isl_vec_free(sample
);
311 point
= isl_basic_set_empty_like(bset
);
313 point
= isl_basic_set_from_vec(sample
);
317 isl_basic_set_free(slice
);
321 struct isl_basic_set
*isl_basic_set_recession_cone(struct isl_basic_set
*bset
)
325 bset
= isl_basic_set_cow(bset
);
328 isl_assert(bset
->ctx
, bset
->n_div
== 0, goto error
);
330 for (i
= 0; i
< bset
->n_eq
; ++i
)
331 isl_int_set_si(bset
->eq
[i
][0], 0);
333 for (i
= 0; i
< bset
->n_ineq
; ++i
)
334 isl_int_set_si(bset
->ineq
[i
][0], 0);
336 ISL_F_CLR(bset
, ISL_BASIC_SET_NO_IMPLICIT
);
337 return isl_basic_set_implicit_equalities(bset
);
339 isl_basic_set_free(bset
);
343 static struct isl_basic_set
*shift(struct isl_basic_set
*bset
, isl_int
*point
)
348 bset
= isl_basic_set_cow(bset
);
352 dim
= isl_basic_set_n_dim(bset
);
353 for (i
= 0; i
< bset
->n_eq
; ++i
) {
354 isl_seq_inner_product(bset
->eq
[i
]+1, point
+1, dim
,
356 isl_int_neg(bset
->eq
[i
][0], bset
->eq
[i
][0]);
359 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
360 isl_seq_inner_product(bset
->ineq
[i
]+1, point
+1, dim
,
362 isl_int_neg(bset
->ineq
[i
][0], bset
->ineq
[i
][0]);
368 /* Look for all equalities satisfied by the integer points in bset,
369 * which is assume not to have any explicit equalities.
371 * The equalities are obtained by successively looking for
372 * a point that is affinely independent of the points found so far.
373 * In particular, for each equality satisfied by the points so far,
374 * we check if there is any point on a hyperplane parallel to the
375 * corresponding hyperplane shifted by at least one (in either direction).
377 * Before looking for any outside points, we first remove the equalities
378 * that correspond to the affine hull of the recession cone.
379 * These equalities will never be equalities over the whols basic set.
381 static struct isl_basic_set
*uset_affine_hull(struct isl_basic_set
*bset
)
384 struct isl_basic_set
*hull
= NULL
;
385 struct isl_vec
*sample
;
389 if (isl_basic_set_is_empty(bset
))
393 sample
= isl_basic_set_sample(isl_basic_set_copy(bset
));
396 if (sample
->size
== 0) {
397 isl_vec_free(sample
);
398 hull
= isl_basic_set_empty_like(bset
);
399 isl_basic_set_free(bset
);
402 hull
= isl_basic_set_from_vec(sample
);
404 if (hull
->n_eq
> 0) {
405 struct isl_basic_set
*cone
;
406 cone
= isl_basic_set_recession_cone(isl_basic_set_copy(bset
));
407 isl_basic_set_free_inequality(cone
, cone
->n_ineq
);
408 cone
= isl_basic_set_normalize_constraints(cone
);
409 cone
= shift(cone
, bset
->sample
->block
.data
);
410 hull
= affine_hull(hull
, cone
);
413 dim
= isl_basic_set_n_dim(bset
);
414 for (i
= 0; i
< dim
; ++i
) {
415 struct isl_basic_set
*point
;
416 for (j
= 0; j
< hull
->n_eq
; ++j
) {
417 point
= outside_point(ctx
, bset
, hull
->eq
[j
], 1);
420 if (!ISL_F_ISSET(point
, ISL_BASIC_SET_EMPTY
))
422 isl_basic_set_free(point
);
423 point
= outside_point(ctx
, bset
, hull
->eq
[j
], 0);
426 if (!ISL_F_ISSET(point
, ISL_BASIC_SET_EMPTY
))
428 isl_basic_set_free(point
);
432 hull
= affine_hull(hull
, point
);
434 isl_basic_set_free(bset
);
438 isl_basic_set_free(bset
);
439 isl_basic_set_free(hull
);
443 /* Look for all equalities satisfied by the integer points in bmap
444 * that are independent of the equalities already explicitly available
447 * We first remove all equalities already explicitly available,
448 * then look for additional equalities in the reduced space
449 * and then transform the result to the original space.
450 * The original equalities are _not_ added to this set. This is
451 * the responsibility of the calling function.
452 * The resulting basic set has all meaning about the dimensions removed.
453 * In particular, dimensions that correspond to existential variables
454 * in bmap and that are found to be fixed are not removed.
456 static struct isl_basic_set
*equalities_in_underlying_set(
457 struct isl_basic_map
*bmap
)
459 struct isl_mat
*T2
= NULL
;
460 struct isl_basic_set
*bset
= NULL
;
461 struct isl_basic_set
*hull
= NULL
;
463 bset
= isl_basic_map_underlying_set(bmap
);
464 bset
= isl_basic_set_remove_equalities(bset
, NULL
, &T2
);
468 hull
= uset_affine_hull(bset
);
470 hull
= isl_basic_set_preimage(hull
, T2
);
475 isl_basic_set_free(bset
);
476 isl_basic_set_free(hull
);
480 /* Detect and make explicit all equalities satisfied by the (integer)
483 struct isl_basic_map
*isl_basic_map_detect_equalities(
484 struct isl_basic_map
*bmap
)
487 struct isl_basic_set
*hull
= NULL
;
491 if (bmap
->n_ineq
== 0)
493 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
495 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
))
497 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
498 return isl_basic_map_implicit_equalities(bmap
);
500 hull
= equalities_in_underlying_set(isl_basic_map_copy(bmap
));
503 if (ISL_F_ISSET(hull
, ISL_BASIC_SET_EMPTY
)) {
504 isl_basic_set_free(hull
);
505 return isl_basic_map_set_to_empty(bmap
);
507 bmap
= isl_basic_map_extend_dim(bmap
, isl_dim_copy(bmap
->dim
), 0,
509 for (i
= 0; i
< hull
->n_eq
; ++i
) {
510 j
= isl_basic_map_alloc_equality(bmap
);
513 isl_seq_cpy(bmap
->eq
[j
], hull
->eq
[i
],
514 1 + isl_basic_set_total_dim(hull
));
516 isl_basic_set_free(hull
);
517 ISL_F_SET(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
| ISL_BASIC_MAP_ALL_EQUALITIES
);
518 bmap
= isl_basic_map_simplify(bmap
);
519 return isl_basic_map_finalize(bmap
);
521 isl_basic_set_free(hull
);
522 isl_basic_map_free(bmap
);
526 struct isl_map
*isl_map_detect_equalities(struct isl_map
*map
)
528 struct isl_basic_map
*bmap
;
534 for (i
= 0; i
< map
->n
; ++i
) {
535 bmap
= isl_basic_map_copy(map
->p
[i
]);
536 bmap
= isl_basic_map_detect_equalities(bmap
);
539 isl_basic_map_free(map
->p
[i
]);
549 /* After computing the rational affine hull (by detecting the implicit
550 * equalities), we compute the additional equalities satisfied by
551 * the integer points (if any) and add the original equalities back in.
553 struct isl_basic_map
*isl_basic_map_affine_hull(struct isl_basic_map
*bmap
)
555 struct isl_basic_set
*hull
= NULL
;
557 bmap
= isl_basic_map_detect_equalities(bmap
);
558 bmap
= isl_basic_map_cow(bmap
);
559 isl_basic_map_free_inequality(bmap
, bmap
->n_ineq
);
563 struct isl_basic_set
*isl_basic_set_affine_hull(struct isl_basic_set
*bset
)
565 return (struct isl_basic_set
*)
566 isl_basic_map_affine_hull((struct isl_basic_map
*)bset
);
569 struct isl_basic_map
*isl_map_affine_hull(struct isl_map
*map
)
572 struct isl_basic_map
*model
= NULL
;
573 struct isl_basic_map
*hull
= NULL
;
580 hull
= isl_basic_map_empty_like_map(map
);
585 map
= isl_map_detect_equalities(map
);
586 map
= isl_map_align_divs(map
);
589 model
= isl_basic_map_copy(map
->p
[0]);
590 set
= isl_map_underlying_set(map
);
591 set
= isl_set_cow(set
);
595 for (i
= 0; i
< set
->n
; ++i
) {
596 set
->p
[i
] = isl_basic_set_cow(set
->p
[i
]);
597 set
->p
[i
] = isl_basic_set_affine_hull(set
->p
[i
]);
598 set
->p
[i
] = isl_basic_set_gauss(set
->p
[i
], NULL
);
602 set
= isl_set_remove_empty_parts(set
);
604 hull
= isl_basic_map_empty_like(model
);
605 isl_basic_map_free(model
);
607 struct isl_basic_set
*bset
;
609 set
->p
[0] = affine_hull(set
->p
[0], set
->p
[--set
->n
]);
613 bset
= isl_basic_set_copy(set
->p
[0]);
614 hull
= isl_basic_map_overlying_set(bset
, model
);
617 hull
= isl_basic_map_simplify(hull
);
618 return isl_basic_map_finalize(hull
);
620 isl_basic_map_free(model
);
625 struct isl_basic_set
*isl_set_affine_hull(struct isl_set
*set
)
627 return (struct isl_basic_set
*)
628 isl_map_affine_hull((struct isl_map
*)set
);