2 * Copyright 2008-2009 Katholieke Universiteit Leuven
4 * Use of this software is governed by the MIT license
6 * Written by Sven Verdoolaege, K.U.Leuven, Departement
7 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
10 #include <isl_ctx_private.h>
11 #include <isl_map_private.h>
16 #include "isl_equalities.h"
17 #include "isl_sample.h"
19 #include <isl_mat_private.h>
21 struct isl_basic_map
*isl_basic_map_implicit_equalities(
22 struct isl_basic_map
*bmap
)
29 bmap
= isl_basic_map_gauss(bmap
, NULL
);
30 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
32 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
))
34 if (bmap
->n_ineq
<= 1)
37 tab
= isl_tab_from_basic_map(bmap
, 0);
38 if (isl_tab_detect_implicit_equalities(tab
) < 0)
40 bmap
= isl_basic_map_update_from_tab(bmap
, tab
);
42 bmap
= isl_basic_map_gauss(bmap
, NULL
);
43 ISL_F_SET(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
47 isl_basic_map_free(bmap
);
51 struct isl_basic_set
*isl_basic_set_implicit_equalities(
52 struct isl_basic_set
*bset
)
54 return (struct isl_basic_set
*)
55 isl_basic_map_implicit_equalities((struct isl_basic_map
*)bset
);
58 struct isl_map
*isl_map_implicit_equalities(struct isl_map
*map
)
65 for (i
= 0; i
< map
->n
; ++i
) {
66 map
->p
[i
] = isl_basic_map_implicit_equalities(map
->p
[i
]);
77 /* Make eq[row][col] of both bmaps equal so we can add the row
78 * add the column to the common matrix.
79 * Note that because of the echelon form, the columns of row row
80 * after column col are zero.
82 static void set_common_multiple(
83 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
,
84 unsigned row
, unsigned col
)
88 if (isl_int_eq(bset1
->eq
[row
][col
], bset2
->eq
[row
][col
]))
93 isl_int_lcm(m
, bset1
->eq
[row
][col
], bset2
->eq
[row
][col
]);
94 isl_int_divexact(c
, m
, bset1
->eq
[row
][col
]);
95 isl_seq_scale(bset1
->eq
[row
], bset1
->eq
[row
], c
, col
+1);
96 isl_int_divexact(c
, m
, bset2
->eq
[row
][col
]);
97 isl_seq_scale(bset2
->eq
[row
], bset2
->eq
[row
], c
, col
+1);
102 /* Delete a given equality, moving all the following equalities one up.
104 static void delete_row(struct isl_basic_set
*bset
, unsigned row
)
111 for (r
= row
; r
< bset
->n_eq
; ++r
)
112 bset
->eq
[r
] = bset
->eq
[r
+1];
113 bset
->eq
[bset
->n_eq
] = t
;
116 /* Make first row entries in column col of bset1 identical to
117 * those of bset2, using the fact that entry bset1->eq[row][col]=a
118 * is non-zero. Initially, these elements of bset1 are all zero.
119 * For each row i < row, we set
120 * A[i] = a * A[i] + B[i][col] * A[row]
123 * A[i][col] = B[i][col] = a * old(B[i][col])
125 static void construct_column(
126 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
,
127 unsigned row
, unsigned col
)
136 total
= 1 + isl_basic_set_n_dim(bset1
);
137 for (r
= 0; r
< row
; ++r
) {
138 if (isl_int_is_zero(bset2
->eq
[r
][col
]))
140 isl_int_gcd(b
, bset2
->eq
[r
][col
], bset1
->eq
[row
][col
]);
141 isl_int_divexact(a
, bset1
->eq
[row
][col
], b
);
142 isl_int_divexact(b
, bset2
->eq
[r
][col
], b
);
143 isl_seq_combine(bset1
->eq
[r
], a
, bset1
->eq
[r
],
144 b
, bset1
->eq
[row
], total
);
145 isl_seq_scale(bset2
->eq
[r
], bset2
->eq
[r
], a
, total
);
149 delete_row(bset1
, row
);
152 /* Make first row entries in column col of bset1 identical to
153 * those of bset2, using only these entries of the two matrices.
154 * Let t be the last row with different entries.
155 * For each row i < t, we set
156 * A[i] = (A[t][col]-B[t][col]) * A[i] + (B[i][col]-A[i][col) * A[t]
157 * B[i] = (A[t][col]-B[t][col]) * B[i] + (B[i][col]-A[i][col) * B[t]
159 * A[i][col] = B[i][col] = old(A[t][col]*B[i][col]-A[i][col]*B[t][col])
161 static int transform_column(
162 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
,
163 unsigned row
, unsigned col
)
169 for (t
= row
-1; t
>= 0; --t
)
170 if (isl_int_ne(bset1
->eq
[t
][col
], bset2
->eq
[t
][col
]))
175 total
= 1 + isl_basic_set_n_dim(bset1
);
179 isl_int_sub(b
, bset1
->eq
[t
][col
], bset2
->eq
[t
][col
]);
180 for (i
= 0; i
< t
; ++i
) {
181 isl_int_sub(a
, bset2
->eq
[i
][col
], bset1
->eq
[i
][col
]);
182 isl_int_gcd(g
, a
, b
);
183 isl_int_divexact(a
, a
, g
);
184 isl_int_divexact(g
, b
, g
);
185 isl_seq_combine(bset1
->eq
[i
], g
, bset1
->eq
[i
], a
, bset1
->eq
[t
],
187 isl_seq_combine(bset2
->eq
[i
], g
, bset2
->eq
[i
], a
, bset2
->eq
[t
],
193 delete_row(bset1
, t
);
194 delete_row(bset2
, t
);
198 /* The implementation is based on Section 5.2 of Michael Karr,
199 * "Affine Relationships Among Variables of a Program",
200 * except that the echelon form we use starts from the last column
201 * and that we are dealing with integer coefficients.
203 static struct isl_basic_set
*affine_hull(
204 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
210 if (!bset1
|| !bset2
)
213 total
= 1 + isl_basic_set_n_dim(bset1
);
216 for (col
= total
-1; col
>= 0; --col
) {
217 int is_zero1
= row
>= bset1
->n_eq
||
218 isl_int_is_zero(bset1
->eq
[row
][col
]);
219 int is_zero2
= row
>= bset2
->n_eq
||
220 isl_int_is_zero(bset2
->eq
[row
][col
]);
221 if (!is_zero1
&& !is_zero2
) {
222 set_common_multiple(bset1
, bset2
, row
, col
);
224 } else if (!is_zero1
&& is_zero2
) {
225 construct_column(bset1
, bset2
, row
, col
);
226 } else if (is_zero1
&& !is_zero2
) {
227 construct_column(bset2
, bset1
, row
, col
);
229 if (transform_column(bset1
, bset2
, row
, col
))
233 isl_assert(bset1
->ctx
, row
== bset1
->n_eq
, goto error
);
234 isl_basic_set_free(bset2
);
235 bset1
= isl_basic_set_normalize_constraints(bset1
);
238 isl_basic_set_free(bset1
);
239 isl_basic_set_free(bset2
);
243 /* Find an integer point in the set represented by "tab"
244 * that lies outside of the equality "eq" e(x) = 0.
245 * If "up" is true, look for a point satisfying e(x) - 1 >= 0.
246 * Otherwise, look for a point satisfying -e(x) - 1 >= 0 (i.e., e(x) <= -1).
247 * The point, if found, is returned.
248 * If no point can be found, a zero-length vector is returned.
250 * Before solving an ILP problem, we first check if simply
251 * adding the normal of the constraint to one of the known
252 * integer points in the basic set represented by "tab"
253 * yields another point inside the basic set.
255 * The caller of this function ensures that the tableau is bounded or
256 * that tab->basis and tab->n_unbounded have been set appropriately.
258 static struct isl_vec
*outside_point(struct isl_tab
*tab
, isl_int
*eq
, int up
)
261 struct isl_vec
*sample
= NULL
;
262 struct isl_tab_undo
*snap
;
270 sample
= isl_vec_alloc(ctx
, 1 + dim
);
273 isl_int_set_si(sample
->el
[0], 1);
274 isl_seq_combine(sample
->el
+ 1,
275 ctx
->one
, tab
->bmap
->sample
->el
+ 1,
276 up
? ctx
->one
: ctx
->negone
, eq
+ 1, dim
);
277 if (isl_basic_map_contains(tab
->bmap
, sample
))
279 isl_vec_free(sample
);
282 snap
= isl_tab_snap(tab
);
285 isl_seq_neg(eq
, eq
, 1 + dim
);
286 isl_int_sub_ui(eq
[0], eq
[0], 1);
288 if (isl_tab_extend_cons(tab
, 1) < 0)
290 if (isl_tab_add_ineq(tab
, eq
) < 0)
293 sample
= isl_tab_sample(tab
);
295 isl_int_add_ui(eq
[0], eq
[0], 1);
297 isl_seq_neg(eq
, eq
, 1 + dim
);
299 if (sample
&& isl_tab_rollback(tab
, snap
) < 0)
304 isl_vec_free(sample
);
308 struct isl_basic_set
*isl_basic_set_recession_cone(struct isl_basic_set
*bset
)
312 bset
= isl_basic_set_cow(bset
);
315 isl_assert(bset
->ctx
, bset
->n_div
== 0, goto error
);
317 for (i
= 0; i
< bset
->n_eq
; ++i
)
318 isl_int_set_si(bset
->eq
[i
][0], 0);
320 for (i
= 0; i
< bset
->n_ineq
; ++i
)
321 isl_int_set_si(bset
->ineq
[i
][0], 0);
323 ISL_F_CLR(bset
, ISL_BASIC_SET_NO_IMPLICIT
);
324 return isl_basic_set_implicit_equalities(bset
);
326 isl_basic_set_free(bset
);
330 __isl_give isl_set
*isl_set_recession_cone(__isl_take isl_set
*set
)
339 set
= isl_set_remove_divs(set
);
340 set
= isl_set_cow(set
);
344 for (i
= 0; i
< set
->n
; ++i
) {
345 set
->p
[i
] = isl_basic_set_recession_cone(set
->p
[i
]);
356 /* Move "sample" to a point that is one up (or down) from the original
357 * point in dimension "pos".
359 static void adjacent_point(__isl_keep isl_vec
*sample
, int pos
, int up
)
362 isl_int_add_ui(sample
->el
[1 + pos
], sample
->el
[1 + pos
], 1);
364 isl_int_sub_ui(sample
->el
[1 + pos
], sample
->el
[1 + pos
], 1);
367 /* Check if any points that are adjacent to "sample" also belong to "bset".
368 * If so, add them to "hull" and return the updated hull.
370 * Before checking whether and adjacent point belongs to "bset", we first
371 * check whether it already belongs to "hull" as this test is typically
374 static __isl_give isl_basic_set
*add_adjacent_points(
375 __isl_take isl_basic_set
*hull
, __isl_take isl_vec
*sample
,
376 __isl_keep isl_basic_set
*bset
)
384 dim
= isl_basic_set_dim(hull
, isl_dim_set
);
386 for (i
= 0; i
< dim
; ++i
) {
387 for (up
= 0; up
<= 1; ++up
) {
389 isl_basic_set
*point
;
391 adjacent_point(sample
, i
, up
);
392 contains
= isl_basic_set_contains(hull
, sample
);
396 adjacent_point(sample
, i
, !up
);
399 contains
= isl_basic_set_contains(bset
, sample
);
403 point
= isl_basic_set_from_vec(
404 isl_vec_copy(sample
));
405 hull
= affine_hull(hull
, point
);
407 adjacent_point(sample
, i
, !up
);
413 isl_vec_free(sample
);
417 isl_vec_free(sample
);
418 isl_basic_set_free(hull
);
422 /* Extend an initial (under-)approximation of the affine hull of basic
423 * set represented by the tableau "tab"
424 * by looking for points that do not satisfy one of the equalities
425 * in the current approximation and adding them to that approximation
426 * until no such points can be found any more.
428 * The caller of this function ensures that "tab" is bounded or
429 * that tab->basis and tab->n_unbounded have been set appropriately.
431 * "bset" may be either NULL or the basic set represented by "tab".
432 * If "bset" is not NULL, we check for any point we find if any
433 * of its adjacent points also belong to "bset".
435 static __isl_give isl_basic_set
*extend_affine_hull(struct isl_tab
*tab
,
436 __isl_take isl_basic_set
*hull
, __isl_keep isl_basic_set
*bset
)
446 if (isl_tab_extend_cons(tab
, 2 * dim
+ 1) < 0)
449 for (i
= 0; i
< dim
; ++i
) {
450 struct isl_vec
*sample
;
451 struct isl_basic_set
*point
;
452 for (j
= 0; j
< hull
->n_eq
; ++j
) {
453 sample
= outside_point(tab
, hull
->eq
[j
], 1);
456 if (sample
->size
> 0)
458 isl_vec_free(sample
);
459 sample
= outside_point(tab
, hull
->eq
[j
], 0);
462 if (sample
->size
> 0)
464 isl_vec_free(sample
);
466 if (isl_tab_add_eq(tab
, hull
->eq
[j
]) < 0)
472 tab
= isl_tab_add_sample(tab
, isl_vec_copy(sample
));
476 hull
= add_adjacent_points(hull
, isl_vec_copy(sample
),
478 point
= isl_basic_set_from_vec(sample
);
479 hull
= affine_hull(hull
, point
);
486 isl_basic_set_free(hull
);
490 /* Drop all constraints in bmap that involve any of the dimensions
491 * first to first+n-1.
493 static __isl_give isl_basic_map
*isl_basic_map_drop_constraints_involving(
494 __isl_take isl_basic_map
*bmap
, unsigned first
, unsigned n
)
501 bmap
= isl_basic_map_cow(bmap
);
506 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
) {
507 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + first
, n
) == -1)
509 isl_basic_map_drop_equality(bmap
, i
);
512 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
513 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + 1 + first
, n
) == -1)
515 isl_basic_map_drop_inequality(bmap
, i
);
521 /* Drop all constraints in bset that involve any of the dimensions
522 * first to first+n-1.
524 __isl_give isl_basic_set
*isl_basic_set_drop_constraints_involving(
525 __isl_take isl_basic_set
*bset
, unsigned first
, unsigned n
)
527 return isl_basic_map_drop_constraints_involving(bset
, first
, n
);
530 /* Drop all constraints in bmap that do not involve any of the dimensions
531 * first to first + n - 1 of the given type.
533 __isl_give isl_basic_map
*isl_basic_map_drop_constraints_not_involving_dims(
534 __isl_take isl_basic_map
*bmap
,
535 enum isl_dim_type type
, unsigned first
, unsigned n
)
541 return isl_basic_map_set_to_empty(bmap
);
542 bmap
= isl_basic_map_cow(bmap
);
546 dim
= isl_basic_map_dim(bmap
, type
);
547 if (first
+ n
> dim
|| first
+ n
< first
)
548 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
549 "index out of bounds", return isl_basic_map_free(bmap
));
551 first
+= isl_basic_map_offset(bmap
, type
) - 1;
553 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
) {
554 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + first
, n
) != -1)
556 isl_basic_map_drop_equality(bmap
, i
);
559 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
560 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + 1 + first
, n
) != -1)
562 isl_basic_map_drop_inequality(bmap
, i
);
568 /* Drop all constraints in bset that do not involve any of the dimensions
569 * first to first + n - 1 of the given type.
571 __isl_give isl_basic_set
*isl_basic_set_drop_constraints_not_involving_dims(
572 __isl_take isl_basic_set
*bset
,
573 enum isl_dim_type type
, unsigned first
, unsigned n
)
575 return isl_basic_map_drop_constraints_not_involving_dims(bset
,
579 /* Drop all constraints in bmap that involve any of the dimensions
580 * first to first + n - 1 of the given type.
582 __isl_give isl_basic_map
*isl_basic_map_drop_constraints_involving_dims(
583 __isl_take isl_basic_map
*bmap
,
584 enum isl_dim_type type
, unsigned first
, unsigned n
)
593 dim
= isl_basic_map_dim(bmap
, type
);
594 if (first
+ n
> dim
|| first
+ n
< first
)
595 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
596 "index out of bounds", return isl_basic_map_free(bmap
));
598 bmap
= isl_basic_map_remove_divs_involving_dims(bmap
, type
, first
, n
);
599 first
+= isl_basic_map_offset(bmap
, type
) - 1;
600 return isl_basic_map_drop_constraints_involving(bmap
, first
, n
);
603 /* Drop all constraints in bset that involve any of the dimensions
604 * first to first + n - 1 of the given type.
606 __isl_give isl_basic_set
*isl_basic_set_drop_constraints_involving_dims(
607 __isl_take isl_basic_set
*bset
,
608 enum isl_dim_type type
, unsigned first
, unsigned n
)
610 return isl_basic_map_drop_constraints_involving_dims(bset
,
614 /* Drop all constraints in map that involve any of the dimensions
615 * first to first + n - 1 of the given type.
617 __isl_give isl_map
*isl_map_drop_constraints_involving_dims(
618 __isl_take isl_map
*map
,
619 enum isl_dim_type type
, unsigned first
, unsigned n
)
629 dim
= isl_map_dim(map
, type
);
630 if (first
+ n
> dim
|| first
+ n
< first
)
631 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
632 "index out of bounds", return isl_map_free(map
));
634 map
= isl_map_cow(map
);
638 for (i
= 0; i
< map
->n
; ++i
) {
639 map
->p
[i
] = isl_basic_map_drop_constraints_involving_dims(
640 map
->p
[i
], type
, first
, n
);
642 return isl_map_free(map
);
648 /* Drop all constraints in set that involve any of the dimensions
649 * first to first + n - 1 of the given type.
651 __isl_give isl_set
*isl_set_drop_constraints_involving_dims(
652 __isl_take isl_set
*set
,
653 enum isl_dim_type type
, unsigned first
, unsigned n
)
655 return isl_map_drop_constraints_involving_dims(set
, type
, first
, n
);
658 /* Construct an initial underapproximatino of the hull of "bset"
659 * from "sample" and any of its adjacent points that also belong to "bset".
661 static __isl_give isl_basic_set
*initialize_hull(__isl_keep isl_basic_set
*bset
,
662 __isl_take isl_vec
*sample
)
666 hull
= isl_basic_set_from_vec(isl_vec_copy(sample
));
667 hull
= add_adjacent_points(hull
, sample
, bset
);
672 /* Look for all equalities satisfied by the integer points in bset,
673 * which is assumed to be bounded.
675 * The equalities are obtained by successively looking for
676 * a point that is affinely independent of the points found so far.
677 * In particular, for each equality satisfied by the points so far,
678 * we check if there is any point on a hyperplane parallel to the
679 * corresponding hyperplane shifted by at least one (in either direction).
681 static struct isl_basic_set
*uset_affine_hull_bounded(struct isl_basic_set
*bset
)
683 struct isl_vec
*sample
= NULL
;
684 struct isl_basic_set
*hull
;
685 struct isl_tab
*tab
= NULL
;
688 if (isl_basic_set_plain_is_empty(bset
))
691 dim
= isl_basic_set_n_dim(bset
);
693 if (bset
->sample
&& bset
->sample
->size
== 1 + dim
) {
694 int contains
= isl_basic_set_contains(bset
, bset
->sample
);
700 sample
= isl_vec_copy(bset
->sample
);
702 isl_vec_free(bset
->sample
);
707 tab
= isl_tab_from_basic_set(bset
, 1);
712 isl_vec_free(sample
);
713 return isl_basic_set_set_to_empty(bset
);
717 struct isl_tab_undo
*snap
;
718 snap
= isl_tab_snap(tab
);
719 sample
= isl_tab_sample(tab
);
720 if (isl_tab_rollback(tab
, snap
) < 0)
722 isl_vec_free(tab
->bmap
->sample
);
723 tab
->bmap
->sample
= isl_vec_copy(sample
);
728 if (sample
->size
== 0) {
730 isl_vec_free(sample
);
731 return isl_basic_set_set_to_empty(bset
);
734 hull
= initialize_hull(bset
, sample
);
736 hull
= extend_affine_hull(tab
, hull
, bset
);
737 isl_basic_set_free(bset
);
742 isl_vec_free(sample
);
744 isl_basic_set_free(bset
);
748 /* Given an unbounded tableau and an integer point satisfying the tableau,
749 * construct an initial affine hull containing the recession cone
750 * shifted to the given point.
752 * The unbounded directions are taken from the last rows of the basis,
753 * which is assumed to have been initialized appropriately.
755 static __isl_give isl_basic_set
*initial_hull(struct isl_tab
*tab
,
756 __isl_take isl_vec
*vec
)
760 struct isl_basic_set
*bset
= NULL
;
767 isl_assert(ctx
, vec
->size
!= 0, goto error
);
769 bset
= isl_basic_set_alloc(ctx
, 0, vec
->size
- 1, 0, vec
->size
- 1, 0);
772 dim
= isl_basic_set_n_dim(bset
) - tab
->n_unbounded
;
773 for (i
= 0; i
< dim
; ++i
) {
774 k
= isl_basic_set_alloc_equality(bset
);
777 isl_seq_cpy(bset
->eq
[k
] + 1, tab
->basis
->row
[1 + i
] + 1,
779 isl_seq_inner_product(bset
->eq
[k
] + 1, vec
->el
+1,
780 vec
->size
- 1, &bset
->eq
[k
][0]);
781 isl_int_neg(bset
->eq
[k
][0], bset
->eq
[k
][0]);
784 bset
= isl_basic_set_gauss(bset
, NULL
);
788 isl_basic_set_free(bset
);
793 /* Given a tableau of a set and a tableau of the corresponding
794 * recession cone, detect and add all equalities to the tableau.
795 * If the tableau is bounded, then we can simply keep the
796 * tableau in its state after the return from extend_affine_hull.
797 * However, if the tableau is unbounded, then
798 * isl_tab_set_initial_basis_with_cone will add some additional
799 * constraints to the tableau that have to be removed again.
800 * In this case, we therefore rollback to the state before
801 * any constraints were added and then add the equalities back in.
803 struct isl_tab
*isl_tab_detect_equalities(struct isl_tab
*tab
,
804 struct isl_tab
*tab_cone
)
807 struct isl_vec
*sample
;
808 struct isl_basic_set
*hull
= NULL
;
809 struct isl_tab_undo
*snap
;
811 if (!tab
|| !tab_cone
)
814 snap
= isl_tab_snap(tab
);
816 isl_mat_free(tab
->basis
);
819 isl_assert(tab
->mat
->ctx
, tab
->bmap
, goto error
);
820 isl_assert(tab
->mat
->ctx
, tab
->samples
, goto error
);
821 isl_assert(tab
->mat
->ctx
, tab
->samples
->n_col
== 1 + tab
->n_var
, goto error
);
822 isl_assert(tab
->mat
->ctx
, tab
->n_sample
> tab
->n_outside
, goto error
);
824 if (isl_tab_set_initial_basis_with_cone(tab
, tab_cone
) < 0)
827 sample
= isl_vec_alloc(tab
->mat
->ctx
, 1 + tab
->n_var
);
831 isl_seq_cpy(sample
->el
, tab
->samples
->row
[tab
->n_outside
], sample
->size
);
833 isl_vec_free(tab
->bmap
->sample
);
834 tab
->bmap
->sample
= isl_vec_copy(sample
);
836 if (tab
->n_unbounded
== 0)
837 hull
= isl_basic_set_from_vec(isl_vec_copy(sample
));
839 hull
= initial_hull(tab
, isl_vec_copy(sample
));
841 for (j
= tab
->n_outside
+ 1; j
< tab
->n_sample
; ++j
) {
842 isl_seq_cpy(sample
->el
, tab
->samples
->row
[j
], sample
->size
);
843 hull
= affine_hull(hull
,
844 isl_basic_set_from_vec(isl_vec_copy(sample
)));
847 isl_vec_free(sample
);
849 hull
= extend_affine_hull(tab
, hull
, NULL
);
853 if (tab
->n_unbounded
== 0) {
854 isl_basic_set_free(hull
);
858 if (isl_tab_rollback(tab
, snap
) < 0)
861 if (hull
->n_eq
> tab
->n_zero
) {
862 for (j
= 0; j
< hull
->n_eq
; ++j
) {
863 isl_seq_normalize(tab
->mat
->ctx
, hull
->eq
[j
], 1 + tab
->n_var
);
864 if (isl_tab_add_eq(tab
, hull
->eq
[j
]) < 0)
869 isl_basic_set_free(hull
);
873 isl_basic_set_free(hull
);
878 /* Compute the affine hull of "bset", where "cone" is the recession cone
881 * We first compute a unimodular transformation that puts the unbounded
882 * directions in the last dimensions. In particular, we take a transformation
883 * that maps all equalities to equalities (in HNF) on the first dimensions.
884 * Let x be the original dimensions and y the transformed, with y_1 bounded
887 * [ y_1 ] [ y_1 ] [ Q_1 ]
888 * x = U [ y_2 ] [ y_2 ] = [ Q_2 ] x
890 * Let's call the input basic set S. We compute S' = preimage(S, U)
891 * and drop the final dimensions including any constraints involving them.
892 * This results in set S''.
893 * Then we compute the affine hull A'' of S''.
894 * Let F y_1 >= g be the constraint system of A''. In the transformed
895 * space the y_2 are unbounded, so we can add them back without any constraints,
899 * [ F 0 ] [ y_2 ] >= g
902 * [ F 0 ] [ Q_2 ] x >= g
906 * The affine hull in the original space is then obtained as
907 * A = preimage(A'', Q_1).
909 static struct isl_basic_set
*affine_hull_with_cone(struct isl_basic_set
*bset
,
910 struct isl_basic_set
*cone
)
914 struct isl_basic_set
*hull
;
915 struct isl_mat
*M
, *U
, *Q
;
920 total
= isl_basic_set_total_dim(cone
);
921 cone_dim
= total
- cone
->n_eq
;
923 M
= isl_mat_sub_alloc6(bset
->ctx
, cone
->eq
, 0, cone
->n_eq
, 1, total
);
924 M
= isl_mat_left_hermite(M
, 0, &U
, &Q
);
929 U
= isl_mat_lin_to_aff(U
);
930 bset
= isl_basic_set_preimage(bset
, isl_mat_copy(U
));
932 bset
= isl_basic_set_drop_constraints_involving(bset
, total
- cone_dim
,
934 bset
= isl_basic_set_drop_dims(bset
, total
- cone_dim
, cone_dim
);
936 Q
= isl_mat_lin_to_aff(Q
);
937 Q
= isl_mat_drop_rows(Q
, 1 + total
- cone_dim
, cone_dim
);
939 if (bset
&& bset
->sample
&& bset
->sample
->size
== 1 + total
)
940 bset
->sample
= isl_mat_vec_product(isl_mat_copy(Q
), bset
->sample
);
942 hull
= uset_affine_hull_bounded(bset
);
948 struct isl_vec
*sample
= isl_vec_copy(hull
->sample
);
949 U
= isl_mat_drop_cols(U
, 1 + total
- cone_dim
, cone_dim
);
950 if (sample
&& sample
->size
> 0)
951 sample
= isl_mat_vec_product(U
, sample
);
954 hull
= isl_basic_set_preimage(hull
, Q
);
956 isl_vec_free(hull
->sample
);
957 hull
->sample
= sample
;
959 isl_vec_free(sample
);
962 isl_basic_set_free(cone
);
966 isl_basic_set_free(bset
);
967 isl_basic_set_free(cone
);
971 /* Look for all equalities satisfied by the integer points in bset,
972 * which is assumed not to have any explicit equalities.
974 * The equalities are obtained by successively looking for
975 * a point that is affinely independent of the points found so far.
976 * In particular, for each equality satisfied by the points so far,
977 * we check if there is any point on a hyperplane parallel to the
978 * corresponding hyperplane shifted by at least one (in either direction).
980 * Before looking for any outside points, we first compute the recession
981 * cone. The directions of this recession cone will always be part
982 * of the affine hull, so there is no need for looking for any points
983 * in these directions.
984 * In particular, if the recession cone is full-dimensional, then
985 * the affine hull is simply the whole universe.
987 static struct isl_basic_set
*uset_affine_hull(struct isl_basic_set
*bset
)
989 struct isl_basic_set
*cone
;
991 if (isl_basic_set_plain_is_empty(bset
))
994 cone
= isl_basic_set_recession_cone(isl_basic_set_copy(bset
));
997 if (cone
->n_eq
== 0) {
998 struct isl_basic_set
*hull
;
999 isl_basic_set_free(cone
);
1000 hull
= isl_basic_set_universe_like(bset
);
1001 isl_basic_set_free(bset
);
1005 if (cone
->n_eq
< isl_basic_set_total_dim(cone
))
1006 return affine_hull_with_cone(bset
, cone
);
1008 isl_basic_set_free(cone
);
1009 return uset_affine_hull_bounded(bset
);
1011 isl_basic_set_free(bset
);
1015 /* Look for all equalities satisfied by the integer points in bmap
1016 * that are independent of the equalities already explicitly available
1019 * We first remove all equalities already explicitly available,
1020 * then look for additional equalities in the reduced space
1021 * and then transform the result to the original space.
1022 * The original equalities are _not_ added to this set. This is
1023 * the responsibility of the calling function.
1024 * The resulting basic set has all meaning about the dimensions removed.
1025 * In particular, dimensions that correspond to existential variables
1026 * in bmap and that are found to be fixed are not removed.
1028 static struct isl_basic_set
*equalities_in_underlying_set(
1029 struct isl_basic_map
*bmap
)
1031 struct isl_mat
*T1
= NULL
;
1032 struct isl_mat
*T2
= NULL
;
1033 struct isl_basic_set
*bset
= NULL
;
1034 struct isl_basic_set
*hull
= NULL
;
1036 bset
= isl_basic_map_underlying_set(bmap
);
1040 bset
= isl_basic_set_remove_equalities(bset
, &T1
, &T2
);
1044 hull
= uset_affine_hull(bset
);
1052 struct isl_vec
*sample
= isl_vec_copy(hull
->sample
);
1053 if (sample
&& sample
->size
> 0)
1054 sample
= isl_mat_vec_product(T1
, sample
);
1057 hull
= isl_basic_set_preimage(hull
, T2
);
1059 isl_vec_free(hull
->sample
);
1060 hull
->sample
= sample
;
1062 isl_vec_free(sample
);
1069 isl_basic_set_free(bset
);
1070 isl_basic_set_free(hull
);
1074 /* Detect and make explicit all equalities satisfied by the (integer)
1077 struct isl_basic_map
*isl_basic_map_detect_equalities(
1078 struct isl_basic_map
*bmap
)
1081 struct isl_basic_set
*hull
= NULL
;
1085 if (bmap
->n_ineq
== 0)
1087 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
1089 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
))
1091 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
1092 return isl_basic_map_implicit_equalities(bmap
);
1094 hull
= equalities_in_underlying_set(isl_basic_map_copy(bmap
));
1097 if (ISL_F_ISSET(hull
, ISL_BASIC_SET_EMPTY
)) {
1098 isl_basic_set_free(hull
);
1099 return isl_basic_map_set_to_empty(bmap
);
1101 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
), 0,
1103 for (i
= 0; i
< hull
->n_eq
; ++i
) {
1104 j
= isl_basic_map_alloc_equality(bmap
);
1107 isl_seq_cpy(bmap
->eq
[j
], hull
->eq
[i
],
1108 1 + isl_basic_set_total_dim(hull
));
1110 isl_vec_free(bmap
->sample
);
1111 bmap
->sample
= isl_vec_copy(hull
->sample
);
1112 isl_basic_set_free(hull
);
1113 ISL_F_SET(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
| ISL_BASIC_MAP_ALL_EQUALITIES
);
1114 bmap
= isl_basic_map_simplify(bmap
);
1115 return isl_basic_map_finalize(bmap
);
1117 isl_basic_set_free(hull
);
1118 isl_basic_map_free(bmap
);
1122 __isl_give isl_basic_set
*isl_basic_set_detect_equalities(
1123 __isl_take isl_basic_set
*bset
)
1125 return (isl_basic_set
*)
1126 isl_basic_map_detect_equalities((isl_basic_map
*)bset
);
1129 __isl_give isl_map
*isl_map_detect_equalities(__isl_take isl_map
*map
)
1131 return isl_map_inline_foreach_basic_map(map
,
1132 &isl_basic_map_detect_equalities
);
1135 __isl_give isl_set
*isl_set_detect_equalities(__isl_take isl_set
*set
)
1137 return (isl_set
*)isl_map_detect_equalities((isl_map
*)set
);
1140 /* After computing the rational affine hull (by detecting the implicit
1141 * equalities), we compute the additional equalities satisfied by
1142 * the integer points (if any) and add the original equalities back in.
1144 struct isl_basic_map
*isl_basic_map_affine_hull(struct isl_basic_map
*bmap
)
1146 bmap
= isl_basic_map_detect_equalities(bmap
);
1147 bmap
= isl_basic_map_cow(bmap
);
1149 isl_basic_map_free_inequality(bmap
, bmap
->n_ineq
);
1150 bmap
= isl_basic_map_finalize(bmap
);
1154 struct isl_basic_set
*isl_basic_set_affine_hull(struct isl_basic_set
*bset
)
1156 return (struct isl_basic_set
*)
1157 isl_basic_map_affine_hull((struct isl_basic_map
*)bset
);
1160 /* Compute the affine hull of each basic map in "map" separately
1161 * and call isl_basic_map_gauss on the result.
1163 static __isl_give isl_map
*isl_map_local_affine_hull(__isl_take isl_map
*map
)
1167 map
= isl_map_cow(map
);
1171 for (i
= 0; i
< map
->n
; ++i
) {
1172 map
->p
[i
] = isl_basic_map_affine_hull(map
->p
[i
]);
1173 map
->p
[i
] = isl_basic_map_gauss(map
->p
[i
], NULL
);
1175 return isl_map_free(map
);
1181 static __isl_give isl_set
*isl_set_local_affine_hull(__isl_take isl_set
*set
)
1183 return isl_map_local_affine_hull(set
);
1186 /* Compute the affine hull of "map".
1188 * We first compute the affine hull of each basic map separately.
1189 * Then we align the divs and recompute the affine hulls of the basic
1190 * maps since some of them may now have extra divs.
1191 * In order to avoid performing parametric integer programming to
1192 * compute explicit expressions for the divs, possible leading to
1193 * an explosion in the number of basic maps, we first drop all unknown
1194 * divs before aligning the divs. Note that the divs determined
1195 * by an equality will be known since we call isl_basic_set_gauss
1196 * from isl_map_local_affine_hull. Calling gauss is also needed
1197 * because affine_hull assumes its input has been gaussed, while
1198 * isl_map_affine_hull may be called on input that has not been gaussed,
1199 * in particular from initial_facet_constraint.
1200 * Similarly, align_divs may reorder some divs so that we need to
1201 * gauss the result again.
1202 * Finally, we combine the individual affine hulls into a single
1205 __isl_give isl_basic_map
*isl_map_affine_hull(__isl_take isl_map
*map
)
1207 struct isl_basic_map
*model
= NULL
;
1208 struct isl_basic_map
*hull
= NULL
;
1209 struct isl_set
*set
;
1210 isl_basic_set
*bset
;
1212 map
= isl_map_detect_equalities(map
);
1213 map
= isl_map_local_affine_hull(map
);
1214 map
= isl_map_remove_empty_parts(map
);
1215 map
= isl_map_remove_unknown_divs(map
);
1216 map
= isl_map_align_divs(map
);
1222 hull
= isl_basic_map_empty_like_map(map
);
1227 model
= isl_basic_map_copy(map
->p
[0]);
1228 set
= isl_map_underlying_set(map
);
1229 set
= isl_set_cow(set
);
1230 set
= isl_set_local_affine_hull(set
);
1235 set
->p
[0] = affine_hull(set
->p
[0], set
->p
[--set
->n
]);
1237 bset
= isl_basic_set_copy(set
->p
[0]);
1238 hull
= isl_basic_map_overlying_set(bset
, model
);
1240 hull
= isl_basic_map_simplify(hull
);
1241 return isl_basic_map_finalize(hull
);
1243 isl_basic_map_free(model
);
1248 struct isl_basic_set
*isl_set_affine_hull(struct isl_set
*set
)
1250 return (struct isl_basic_set
*)
1251 isl_map_affine_hull((struct isl_map
*)set
);