2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2012 Ecole Normale Superieure
6 * Use of this software is governed by the MIT license
8 * Written by Sven Verdoolaege, K.U.Leuven, Departement
9 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
10 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
11 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
12 * and Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
15 #include <isl_ctx_private.h>
16 #include <isl_map_private.h>
21 #include "isl_equalities.h"
22 #include "isl_sample.h"
24 #include <isl_mat_private.h>
25 #include <isl_vec_private.h>
27 struct isl_basic_map
*isl_basic_map_implicit_equalities(
28 struct isl_basic_map
*bmap
)
35 bmap
= isl_basic_map_gauss(bmap
, NULL
);
36 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
38 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
))
40 if (bmap
->n_ineq
<= 1)
43 tab
= isl_tab_from_basic_map(bmap
, 0);
44 if (isl_tab_detect_implicit_equalities(tab
) < 0)
46 bmap
= isl_basic_map_update_from_tab(bmap
, tab
);
48 bmap
= isl_basic_map_gauss(bmap
, NULL
);
49 ISL_F_SET(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
53 isl_basic_map_free(bmap
);
57 struct isl_basic_set
*isl_basic_set_implicit_equalities(
58 struct isl_basic_set
*bset
)
60 return (struct isl_basic_set
*)
61 isl_basic_map_implicit_equalities((struct isl_basic_map
*)bset
);
64 struct isl_map
*isl_map_implicit_equalities(struct isl_map
*map
)
71 for (i
= 0; i
< map
->n
; ++i
) {
72 map
->p
[i
] = isl_basic_map_implicit_equalities(map
->p
[i
]);
83 /* Make eq[row][col] of both bmaps equal so we can add the row
84 * add the column to the common matrix.
85 * Note that because of the echelon form, the columns of row row
86 * after column col are zero.
88 static void set_common_multiple(
89 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
,
90 unsigned row
, unsigned col
)
94 if (isl_int_eq(bset1
->eq
[row
][col
], bset2
->eq
[row
][col
]))
99 isl_int_lcm(m
, bset1
->eq
[row
][col
], bset2
->eq
[row
][col
]);
100 isl_int_divexact(c
, m
, bset1
->eq
[row
][col
]);
101 isl_seq_scale(bset1
->eq
[row
], bset1
->eq
[row
], c
, col
+1);
102 isl_int_divexact(c
, m
, bset2
->eq
[row
][col
]);
103 isl_seq_scale(bset2
->eq
[row
], bset2
->eq
[row
], c
, col
+1);
108 /* Delete a given equality, moving all the following equalities one up.
110 static void delete_row(struct isl_basic_set
*bset
, unsigned row
)
117 for (r
= row
; r
< bset
->n_eq
; ++r
)
118 bset
->eq
[r
] = bset
->eq
[r
+1];
119 bset
->eq
[bset
->n_eq
] = t
;
122 /* Make first row entries in column col of bset1 identical to
123 * those of bset2, using the fact that entry bset1->eq[row][col]=a
124 * is non-zero. Initially, these elements of bset1 are all zero.
125 * For each row i < row, we set
126 * A[i] = a * A[i] + B[i][col] * A[row]
129 * A[i][col] = B[i][col] = a * old(B[i][col])
131 static void construct_column(
132 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
,
133 unsigned row
, unsigned col
)
142 total
= 1 + isl_basic_set_n_dim(bset1
);
143 for (r
= 0; r
< row
; ++r
) {
144 if (isl_int_is_zero(bset2
->eq
[r
][col
]))
146 isl_int_gcd(b
, bset2
->eq
[r
][col
], bset1
->eq
[row
][col
]);
147 isl_int_divexact(a
, bset1
->eq
[row
][col
], b
);
148 isl_int_divexact(b
, bset2
->eq
[r
][col
], b
);
149 isl_seq_combine(bset1
->eq
[r
], a
, bset1
->eq
[r
],
150 b
, bset1
->eq
[row
], total
);
151 isl_seq_scale(bset2
->eq
[r
], bset2
->eq
[r
], a
, total
);
155 delete_row(bset1
, row
);
158 /* Make first row entries in column col of bset1 identical to
159 * those of bset2, using only these entries of the two matrices.
160 * Let t be the last row with different entries.
161 * For each row i < t, we set
162 * A[i] = (A[t][col]-B[t][col]) * A[i] + (B[i][col]-A[i][col) * A[t]
163 * B[i] = (A[t][col]-B[t][col]) * B[i] + (B[i][col]-A[i][col) * B[t]
165 * A[i][col] = B[i][col] = old(A[t][col]*B[i][col]-A[i][col]*B[t][col])
167 static int transform_column(
168 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
,
169 unsigned row
, unsigned col
)
175 for (t
= row
-1; t
>= 0; --t
)
176 if (isl_int_ne(bset1
->eq
[t
][col
], bset2
->eq
[t
][col
]))
181 total
= 1 + isl_basic_set_n_dim(bset1
);
185 isl_int_sub(b
, bset1
->eq
[t
][col
], bset2
->eq
[t
][col
]);
186 for (i
= 0; i
< t
; ++i
) {
187 isl_int_sub(a
, bset2
->eq
[i
][col
], bset1
->eq
[i
][col
]);
188 isl_int_gcd(g
, a
, b
);
189 isl_int_divexact(a
, a
, g
);
190 isl_int_divexact(g
, b
, g
);
191 isl_seq_combine(bset1
->eq
[i
], g
, bset1
->eq
[i
], a
, bset1
->eq
[t
],
193 isl_seq_combine(bset2
->eq
[i
], g
, bset2
->eq
[i
], a
, bset2
->eq
[t
],
199 delete_row(bset1
, t
);
200 delete_row(bset2
, t
);
204 /* The implementation is based on Section 5.2 of Michael Karr,
205 * "Affine Relationships Among Variables of a Program",
206 * except that the echelon form we use starts from the last column
207 * and that we are dealing with integer coefficients.
209 static struct isl_basic_set
*affine_hull(
210 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
216 if (!bset1
|| !bset2
)
219 total
= 1 + isl_basic_set_n_dim(bset1
);
222 for (col
= total
-1; col
>= 0; --col
) {
223 int is_zero1
= row
>= bset1
->n_eq
||
224 isl_int_is_zero(bset1
->eq
[row
][col
]);
225 int is_zero2
= row
>= bset2
->n_eq
||
226 isl_int_is_zero(bset2
->eq
[row
][col
]);
227 if (!is_zero1
&& !is_zero2
) {
228 set_common_multiple(bset1
, bset2
, row
, col
);
230 } else if (!is_zero1
&& is_zero2
) {
231 construct_column(bset1
, bset2
, row
, col
);
232 } else if (is_zero1
&& !is_zero2
) {
233 construct_column(bset2
, bset1
, row
, col
);
235 if (transform_column(bset1
, bset2
, row
, col
))
239 isl_assert(bset1
->ctx
, row
== bset1
->n_eq
, goto error
);
240 isl_basic_set_free(bset2
);
241 bset1
= isl_basic_set_normalize_constraints(bset1
);
244 isl_basic_set_free(bset1
);
245 isl_basic_set_free(bset2
);
249 /* Find an integer point in the set represented by "tab"
250 * that lies outside of the equality "eq" e(x) = 0.
251 * If "up" is true, look for a point satisfying e(x) - 1 >= 0.
252 * Otherwise, look for a point satisfying -e(x) - 1 >= 0 (i.e., e(x) <= -1).
253 * The point, if found, is returned.
254 * If no point can be found, a zero-length vector is returned.
256 * Before solving an ILP problem, we first check if simply
257 * adding the normal of the constraint to one of the known
258 * integer points in the basic set represented by "tab"
259 * yields another point inside the basic set.
261 * The caller of this function ensures that the tableau is bounded or
262 * that tab->basis and tab->n_unbounded have been set appropriately.
264 static struct isl_vec
*outside_point(struct isl_tab
*tab
, isl_int
*eq
, int up
)
267 struct isl_vec
*sample
= NULL
;
268 struct isl_tab_undo
*snap
;
276 sample
= isl_vec_alloc(ctx
, 1 + dim
);
279 isl_int_set_si(sample
->el
[0], 1);
280 isl_seq_combine(sample
->el
+ 1,
281 ctx
->one
, tab
->bmap
->sample
->el
+ 1,
282 up
? ctx
->one
: ctx
->negone
, eq
+ 1, dim
);
283 if (isl_basic_map_contains(tab
->bmap
, sample
))
285 isl_vec_free(sample
);
288 snap
= isl_tab_snap(tab
);
291 isl_seq_neg(eq
, eq
, 1 + dim
);
292 isl_int_sub_ui(eq
[0], eq
[0], 1);
294 if (isl_tab_extend_cons(tab
, 1) < 0)
296 if (isl_tab_add_ineq(tab
, eq
) < 0)
299 sample
= isl_tab_sample(tab
);
301 isl_int_add_ui(eq
[0], eq
[0], 1);
303 isl_seq_neg(eq
, eq
, 1 + dim
);
305 if (sample
&& isl_tab_rollback(tab
, snap
) < 0)
310 isl_vec_free(sample
);
314 struct isl_basic_set
*isl_basic_set_recession_cone(struct isl_basic_set
*bset
)
318 bset
= isl_basic_set_cow(bset
);
321 isl_assert(bset
->ctx
, bset
->n_div
== 0, goto error
);
323 for (i
= 0; i
< bset
->n_eq
; ++i
)
324 isl_int_set_si(bset
->eq
[i
][0], 0);
326 for (i
= 0; i
< bset
->n_ineq
; ++i
)
327 isl_int_set_si(bset
->ineq
[i
][0], 0);
329 ISL_F_CLR(bset
, ISL_BASIC_SET_NO_IMPLICIT
);
330 return isl_basic_set_implicit_equalities(bset
);
332 isl_basic_set_free(bset
);
336 __isl_give isl_set
*isl_set_recession_cone(__isl_take isl_set
*set
)
345 set
= isl_set_remove_divs(set
);
346 set
= isl_set_cow(set
);
350 for (i
= 0; i
< set
->n
; ++i
) {
351 set
->p
[i
] = isl_basic_set_recession_cone(set
->p
[i
]);
362 /* Move "sample" to a point that is one up (or down) from the original
363 * point in dimension "pos".
365 static void adjacent_point(__isl_keep isl_vec
*sample
, int pos
, int up
)
368 isl_int_add_ui(sample
->el
[1 + pos
], sample
->el
[1 + pos
], 1);
370 isl_int_sub_ui(sample
->el
[1 + pos
], sample
->el
[1 + pos
], 1);
373 /* Check if any points that are adjacent to "sample" also belong to "bset".
374 * If so, add them to "hull" and return the updated hull.
376 * Before checking whether and adjacent point belongs to "bset", we first
377 * check whether it already belongs to "hull" as this test is typically
380 static __isl_give isl_basic_set
*add_adjacent_points(
381 __isl_take isl_basic_set
*hull
, __isl_take isl_vec
*sample
,
382 __isl_keep isl_basic_set
*bset
)
390 dim
= isl_basic_set_dim(hull
, isl_dim_set
);
392 for (i
= 0; i
< dim
; ++i
) {
393 for (up
= 0; up
<= 1; ++up
) {
395 isl_basic_set
*point
;
397 adjacent_point(sample
, i
, up
);
398 contains
= isl_basic_set_contains(hull
, sample
);
402 adjacent_point(sample
, i
, !up
);
405 contains
= isl_basic_set_contains(bset
, sample
);
409 point
= isl_basic_set_from_vec(
410 isl_vec_copy(sample
));
411 hull
= affine_hull(hull
, point
);
413 adjacent_point(sample
, i
, !up
);
419 isl_vec_free(sample
);
423 isl_vec_free(sample
);
424 isl_basic_set_free(hull
);
428 /* Extend an initial (under-)approximation of the affine hull of basic
429 * set represented by the tableau "tab"
430 * by looking for points that do not satisfy one of the equalities
431 * in the current approximation and adding them to that approximation
432 * until no such points can be found any more.
434 * The caller of this function ensures that "tab" is bounded or
435 * that tab->basis and tab->n_unbounded have been set appropriately.
437 * "bset" may be either NULL or the basic set represented by "tab".
438 * If "bset" is not NULL, we check for any point we find if any
439 * of its adjacent points also belong to "bset".
441 static __isl_give isl_basic_set
*extend_affine_hull(struct isl_tab
*tab
,
442 __isl_take isl_basic_set
*hull
, __isl_keep isl_basic_set
*bset
)
452 if (isl_tab_extend_cons(tab
, 2 * dim
+ 1) < 0)
455 for (i
= 0; i
< dim
; ++i
) {
456 struct isl_vec
*sample
;
457 struct isl_basic_set
*point
;
458 for (j
= 0; j
< hull
->n_eq
; ++j
) {
459 sample
= outside_point(tab
, hull
->eq
[j
], 1);
462 if (sample
->size
> 0)
464 isl_vec_free(sample
);
465 sample
= outside_point(tab
, hull
->eq
[j
], 0);
468 if (sample
->size
> 0)
470 isl_vec_free(sample
);
472 if (isl_tab_add_eq(tab
, hull
->eq
[j
]) < 0)
478 isl_tab_add_sample(tab
, isl_vec_copy(sample
)) < 0)
479 hull
= isl_basic_set_free(hull
);
481 hull
= add_adjacent_points(hull
, isl_vec_copy(sample
),
483 point
= isl_basic_set_from_vec(sample
);
484 hull
= affine_hull(hull
, point
);
491 isl_basic_set_free(hull
);
495 /* Drop all constraints in bmap that involve any of the dimensions
496 * first to first+n-1.
498 static __isl_give isl_basic_map
*isl_basic_map_drop_constraints_involving(
499 __isl_take isl_basic_map
*bmap
, unsigned first
, unsigned n
)
506 bmap
= isl_basic_map_cow(bmap
);
511 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
) {
512 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + first
, n
) == -1)
514 isl_basic_map_drop_equality(bmap
, i
);
517 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
518 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + 1 + first
, n
) == -1)
520 isl_basic_map_drop_inequality(bmap
, i
);
523 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
527 /* Drop all constraints in bset that involve any of the dimensions
528 * first to first+n-1.
530 __isl_give isl_basic_set
*isl_basic_set_drop_constraints_involving(
531 __isl_take isl_basic_set
*bset
, unsigned first
, unsigned n
)
533 return isl_basic_map_drop_constraints_involving(bset
, first
, n
);
536 /* Drop all constraints in bmap that do not involve any of the dimensions
537 * first to first + n - 1 of the given type.
539 __isl_give isl_basic_map
*isl_basic_map_drop_constraints_not_involving_dims(
540 __isl_take isl_basic_map
*bmap
,
541 enum isl_dim_type type
, unsigned first
, unsigned n
)
547 isl_space
*space
= isl_basic_map_get_space(bmap
);
548 isl_basic_map_free(bmap
);
549 return isl_basic_map_universe(space
);
551 bmap
= isl_basic_map_cow(bmap
);
555 dim
= isl_basic_map_dim(bmap
, type
);
556 if (first
+ n
> dim
|| first
+ n
< first
)
557 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
558 "index out of bounds", return isl_basic_map_free(bmap
));
560 first
+= isl_basic_map_offset(bmap
, type
) - 1;
562 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
) {
563 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + first
, n
) != -1)
565 isl_basic_map_drop_equality(bmap
, i
);
568 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
569 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + 1 + first
, n
) != -1)
571 isl_basic_map_drop_inequality(bmap
, i
);
574 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
578 /* Drop all constraints in bset that do not involve any of the dimensions
579 * first to first + n - 1 of the given type.
581 __isl_give isl_basic_set
*isl_basic_set_drop_constraints_not_involving_dims(
582 __isl_take isl_basic_set
*bset
,
583 enum isl_dim_type type
, unsigned first
, unsigned n
)
585 return isl_basic_map_drop_constraints_not_involving_dims(bset
,
589 /* Drop all constraints in bmap that involve any of the dimensions
590 * first to first + n - 1 of the given type.
592 __isl_give isl_basic_map
*isl_basic_map_drop_constraints_involving_dims(
593 __isl_take isl_basic_map
*bmap
,
594 enum isl_dim_type type
, unsigned first
, unsigned n
)
603 dim
= isl_basic_map_dim(bmap
, type
);
604 if (first
+ n
> dim
|| first
+ n
< first
)
605 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
606 "index out of bounds", return isl_basic_map_free(bmap
));
608 bmap
= isl_basic_map_remove_divs_involving_dims(bmap
, type
, first
, n
);
609 first
+= isl_basic_map_offset(bmap
, type
) - 1;
610 return isl_basic_map_drop_constraints_involving(bmap
, first
, n
);
613 /* Drop all constraints in bset that involve any of the dimensions
614 * first to first + n - 1 of the given type.
616 __isl_give isl_basic_set
*isl_basic_set_drop_constraints_involving_dims(
617 __isl_take isl_basic_set
*bset
,
618 enum isl_dim_type type
, unsigned first
, unsigned n
)
620 return isl_basic_map_drop_constraints_involving_dims(bset
,
624 /* Drop all constraints in map that involve any of the dimensions
625 * first to first + n - 1 of the given type.
627 __isl_give isl_map
*isl_map_drop_constraints_involving_dims(
628 __isl_take isl_map
*map
,
629 enum isl_dim_type type
, unsigned first
, unsigned n
)
639 dim
= isl_map_dim(map
, type
);
640 if (first
+ n
> dim
|| first
+ n
< first
)
641 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
642 "index out of bounds", return isl_map_free(map
));
644 map
= isl_map_cow(map
);
648 for (i
= 0; i
< map
->n
; ++i
) {
649 map
->p
[i
] = isl_basic_map_drop_constraints_involving_dims(
650 map
->p
[i
], type
, first
, n
);
652 return isl_map_free(map
);
658 /* Drop all constraints in set that involve any of the dimensions
659 * first to first + n - 1 of the given type.
661 __isl_give isl_set
*isl_set_drop_constraints_involving_dims(
662 __isl_take isl_set
*set
,
663 enum isl_dim_type type
, unsigned first
, unsigned n
)
665 return isl_map_drop_constraints_involving_dims(set
, type
, first
, n
);
668 /* Construct an initial underapproximatino of the hull of "bset"
669 * from "sample" and any of its adjacent points that also belong to "bset".
671 static __isl_give isl_basic_set
*initialize_hull(__isl_keep isl_basic_set
*bset
,
672 __isl_take isl_vec
*sample
)
676 hull
= isl_basic_set_from_vec(isl_vec_copy(sample
));
677 hull
= add_adjacent_points(hull
, sample
, bset
);
682 /* Look for all equalities satisfied by the integer points in bset,
683 * which is assumed to be bounded.
685 * The equalities are obtained by successively looking for
686 * a point that is affinely independent of the points found so far.
687 * In particular, for each equality satisfied by the points so far,
688 * we check if there is any point on a hyperplane parallel to the
689 * corresponding hyperplane shifted by at least one (in either direction).
691 static struct isl_basic_set
*uset_affine_hull_bounded(struct isl_basic_set
*bset
)
693 struct isl_vec
*sample
= NULL
;
694 struct isl_basic_set
*hull
;
695 struct isl_tab
*tab
= NULL
;
698 if (isl_basic_set_plain_is_empty(bset
))
701 dim
= isl_basic_set_n_dim(bset
);
703 if (bset
->sample
&& bset
->sample
->size
== 1 + dim
) {
704 int contains
= isl_basic_set_contains(bset
, bset
->sample
);
710 sample
= isl_vec_copy(bset
->sample
);
712 isl_vec_free(bset
->sample
);
717 tab
= isl_tab_from_basic_set(bset
, 1);
722 isl_vec_free(sample
);
723 return isl_basic_set_set_to_empty(bset
);
727 struct isl_tab_undo
*snap
;
728 snap
= isl_tab_snap(tab
);
729 sample
= isl_tab_sample(tab
);
730 if (isl_tab_rollback(tab
, snap
) < 0)
732 isl_vec_free(tab
->bmap
->sample
);
733 tab
->bmap
->sample
= isl_vec_copy(sample
);
738 if (sample
->size
== 0) {
740 isl_vec_free(sample
);
741 return isl_basic_set_set_to_empty(bset
);
744 hull
= initialize_hull(bset
, sample
);
746 hull
= extend_affine_hull(tab
, hull
, bset
);
747 isl_basic_set_free(bset
);
752 isl_vec_free(sample
);
754 isl_basic_set_free(bset
);
758 /* Given an unbounded tableau and an integer point satisfying the tableau,
759 * construct an initial affine hull containing the recession cone
760 * shifted to the given point.
762 * The unbounded directions are taken from the last rows of the basis,
763 * which is assumed to have been initialized appropriately.
765 static __isl_give isl_basic_set
*initial_hull(struct isl_tab
*tab
,
766 __isl_take isl_vec
*vec
)
770 struct isl_basic_set
*bset
= NULL
;
777 isl_assert(ctx
, vec
->size
!= 0, goto error
);
779 bset
= isl_basic_set_alloc(ctx
, 0, vec
->size
- 1, 0, vec
->size
- 1, 0);
782 dim
= isl_basic_set_n_dim(bset
) - tab
->n_unbounded
;
783 for (i
= 0; i
< dim
; ++i
) {
784 k
= isl_basic_set_alloc_equality(bset
);
787 isl_seq_cpy(bset
->eq
[k
] + 1, tab
->basis
->row
[1 + i
] + 1,
789 isl_seq_inner_product(bset
->eq
[k
] + 1, vec
->el
+1,
790 vec
->size
- 1, &bset
->eq
[k
][0]);
791 isl_int_neg(bset
->eq
[k
][0], bset
->eq
[k
][0]);
794 bset
= isl_basic_set_gauss(bset
, NULL
);
798 isl_basic_set_free(bset
);
803 /* Given a tableau of a set and a tableau of the corresponding
804 * recession cone, detect and add all equalities to the tableau.
805 * If the tableau is bounded, then we can simply keep the
806 * tableau in its state after the return from extend_affine_hull.
807 * However, if the tableau is unbounded, then
808 * isl_tab_set_initial_basis_with_cone will add some additional
809 * constraints to the tableau that have to be removed again.
810 * In this case, we therefore rollback to the state before
811 * any constraints were added and then add the equalities back in.
813 struct isl_tab
*isl_tab_detect_equalities(struct isl_tab
*tab
,
814 struct isl_tab
*tab_cone
)
817 struct isl_vec
*sample
;
818 struct isl_basic_set
*hull
= NULL
;
819 struct isl_tab_undo
*snap
;
821 if (!tab
|| !tab_cone
)
824 snap
= isl_tab_snap(tab
);
826 isl_mat_free(tab
->basis
);
829 isl_assert(tab
->mat
->ctx
, tab
->bmap
, goto error
);
830 isl_assert(tab
->mat
->ctx
, tab
->samples
, goto error
);
831 isl_assert(tab
->mat
->ctx
, tab
->samples
->n_col
== 1 + tab
->n_var
, goto error
);
832 isl_assert(tab
->mat
->ctx
, tab
->n_sample
> tab
->n_outside
, goto error
);
834 if (isl_tab_set_initial_basis_with_cone(tab
, tab_cone
) < 0)
837 sample
= isl_vec_alloc(tab
->mat
->ctx
, 1 + tab
->n_var
);
841 isl_seq_cpy(sample
->el
, tab
->samples
->row
[tab
->n_outside
], sample
->size
);
843 isl_vec_free(tab
->bmap
->sample
);
844 tab
->bmap
->sample
= isl_vec_copy(sample
);
846 if (tab
->n_unbounded
== 0)
847 hull
= isl_basic_set_from_vec(isl_vec_copy(sample
));
849 hull
= initial_hull(tab
, isl_vec_copy(sample
));
851 for (j
= tab
->n_outside
+ 1; j
< tab
->n_sample
; ++j
) {
852 isl_seq_cpy(sample
->el
, tab
->samples
->row
[j
], sample
->size
);
853 hull
= affine_hull(hull
,
854 isl_basic_set_from_vec(isl_vec_copy(sample
)));
857 isl_vec_free(sample
);
859 hull
= extend_affine_hull(tab
, hull
, NULL
);
863 if (tab
->n_unbounded
== 0) {
864 isl_basic_set_free(hull
);
868 if (isl_tab_rollback(tab
, snap
) < 0)
871 if (hull
->n_eq
> tab
->n_zero
) {
872 for (j
= 0; j
< hull
->n_eq
; ++j
) {
873 isl_seq_normalize(tab
->mat
->ctx
, hull
->eq
[j
], 1 + tab
->n_var
);
874 if (isl_tab_add_eq(tab
, hull
->eq
[j
]) < 0)
879 isl_basic_set_free(hull
);
883 isl_basic_set_free(hull
);
888 /* Compute the affine hull of "bset", where "cone" is the recession cone
891 * We first compute a unimodular transformation that puts the unbounded
892 * directions in the last dimensions. In particular, we take a transformation
893 * that maps all equalities to equalities (in HNF) on the first dimensions.
894 * Let x be the original dimensions and y the transformed, with y_1 bounded
897 * [ y_1 ] [ y_1 ] [ Q_1 ]
898 * x = U [ y_2 ] [ y_2 ] = [ Q_2 ] x
900 * Let's call the input basic set S. We compute S' = preimage(S, U)
901 * and drop the final dimensions including any constraints involving them.
902 * This results in set S''.
903 * Then we compute the affine hull A'' of S''.
904 * Let F y_1 >= g be the constraint system of A''. In the transformed
905 * space the y_2 are unbounded, so we can add them back without any constraints,
909 * [ F 0 ] [ y_2 ] >= g
912 * [ F 0 ] [ Q_2 ] x >= g
916 * The affine hull in the original space is then obtained as
917 * A = preimage(A'', Q_1).
919 static struct isl_basic_set
*affine_hull_with_cone(struct isl_basic_set
*bset
,
920 struct isl_basic_set
*cone
)
924 struct isl_basic_set
*hull
;
925 struct isl_mat
*M
, *U
, *Q
;
930 total
= isl_basic_set_total_dim(cone
);
931 cone_dim
= total
- cone
->n_eq
;
933 M
= isl_mat_sub_alloc6(bset
->ctx
, cone
->eq
, 0, cone
->n_eq
, 1, total
);
934 M
= isl_mat_left_hermite(M
, 0, &U
, &Q
);
939 U
= isl_mat_lin_to_aff(U
);
940 bset
= isl_basic_set_preimage(bset
, isl_mat_copy(U
));
942 bset
= isl_basic_set_drop_constraints_involving(bset
, total
- cone_dim
,
944 bset
= isl_basic_set_drop_dims(bset
, total
- cone_dim
, cone_dim
);
946 Q
= isl_mat_lin_to_aff(Q
);
947 Q
= isl_mat_drop_rows(Q
, 1 + total
- cone_dim
, cone_dim
);
949 if (bset
&& bset
->sample
&& bset
->sample
->size
== 1 + total
)
950 bset
->sample
= isl_mat_vec_product(isl_mat_copy(Q
), bset
->sample
);
952 hull
= uset_affine_hull_bounded(bset
);
958 struct isl_vec
*sample
= isl_vec_copy(hull
->sample
);
959 U
= isl_mat_drop_cols(U
, 1 + total
- cone_dim
, cone_dim
);
960 if (sample
&& sample
->size
> 0)
961 sample
= isl_mat_vec_product(U
, sample
);
964 hull
= isl_basic_set_preimage(hull
, Q
);
966 isl_vec_free(hull
->sample
);
967 hull
->sample
= sample
;
969 isl_vec_free(sample
);
972 isl_basic_set_free(cone
);
976 isl_basic_set_free(bset
);
977 isl_basic_set_free(cone
);
981 /* Look for all equalities satisfied by the integer points in bset,
982 * which is assumed not to have any explicit equalities.
984 * The equalities are obtained by successively looking for
985 * a point that is affinely independent of the points found so far.
986 * In particular, for each equality satisfied by the points so far,
987 * we check if there is any point on a hyperplane parallel to the
988 * corresponding hyperplane shifted by at least one (in either direction).
990 * Before looking for any outside points, we first compute the recession
991 * cone. The directions of this recession cone will always be part
992 * of the affine hull, so there is no need for looking for any points
993 * in these directions.
994 * In particular, if the recession cone is full-dimensional, then
995 * the affine hull is simply the whole universe.
997 static struct isl_basic_set
*uset_affine_hull(struct isl_basic_set
*bset
)
999 struct isl_basic_set
*cone
;
1001 if (isl_basic_set_plain_is_empty(bset
))
1004 cone
= isl_basic_set_recession_cone(isl_basic_set_copy(bset
));
1007 if (cone
->n_eq
== 0) {
1008 struct isl_basic_set
*hull
;
1009 isl_basic_set_free(cone
);
1010 hull
= isl_basic_set_universe_like(bset
);
1011 isl_basic_set_free(bset
);
1015 if (cone
->n_eq
< isl_basic_set_total_dim(cone
))
1016 return affine_hull_with_cone(bset
, cone
);
1018 isl_basic_set_free(cone
);
1019 return uset_affine_hull_bounded(bset
);
1021 isl_basic_set_free(bset
);
1025 /* Look for all equalities satisfied by the integer points in bmap
1026 * that are independent of the equalities already explicitly available
1029 * We first remove all equalities already explicitly available,
1030 * then look for additional equalities in the reduced space
1031 * and then transform the result to the original space.
1032 * The original equalities are _not_ added to this set. This is
1033 * the responsibility of the calling function.
1034 * The resulting basic set has all meaning about the dimensions removed.
1035 * In particular, dimensions that correspond to existential variables
1036 * in bmap and that are found to be fixed are not removed.
1038 static struct isl_basic_set
*equalities_in_underlying_set(
1039 struct isl_basic_map
*bmap
)
1041 struct isl_mat
*T1
= NULL
;
1042 struct isl_mat
*T2
= NULL
;
1043 struct isl_basic_set
*bset
= NULL
;
1044 struct isl_basic_set
*hull
= NULL
;
1046 bset
= isl_basic_map_underlying_set(bmap
);
1050 bset
= isl_basic_set_remove_equalities(bset
, &T1
, &T2
);
1054 hull
= uset_affine_hull(bset
);
1062 struct isl_vec
*sample
= isl_vec_copy(hull
->sample
);
1063 if (sample
&& sample
->size
> 0)
1064 sample
= isl_mat_vec_product(T1
, sample
);
1067 hull
= isl_basic_set_preimage(hull
, T2
);
1069 isl_vec_free(hull
->sample
);
1070 hull
->sample
= sample
;
1072 isl_vec_free(sample
);
1079 isl_basic_set_free(bset
);
1080 isl_basic_set_free(hull
);
1084 /* Detect and make explicit all equalities satisfied by the (integer)
1087 struct isl_basic_map
*isl_basic_map_detect_equalities(
1088 struct isl_basic_map
*bmap
)
1091 struct isl_basic_set
*hull
= NULL
;
1095 if (bmap
->n_ineq
== 0)
1097 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
1099 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
))
1101 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
1102 return isl_basic_map_implicit_equalities(bmap
);
1104 hull
= equalities_in_underlying_set(isl_basic_map_copy(bmap
));
1107 if (ISL_F_ISSET(hull
, ISL_BASIC_SET_EMPTY
)) {
1108 isl_basic_set_free(hull
);
1109 return isl_basic_map_set_to_empty(bmap
);
1111 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
), 0,
1113 for (i
= 0; i
< hull
->n_eq
; ++i
) {
1114 j
= isl_basic_map_alloc_equality(bmap
);
1117 isl_seq_cpy(bmap
->eq
[j
], hull
->eq
[i
],
1118 1 + isl_basic_set_total_dim(hull
));
1120 isl_vec_free(bmap
->sample
);
1121 bmap
->sample
= isl_vec_copy(hull
->sample
);
1122 isl_basic_set_free(hull
);
1123 ISL_F_SET(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
| ISL_BASIC_MAP_ALL_EQUALITIES
);
1124 bmap
= isl_basic_map_simplify(bmap
);
1125 return isl_basic_map_finalize(bmap
);
1127 isl_basic_set_free(hull
);
1128 isl_basic_map_free(bmap
);
1132 __isl_give isl_basic_set
*isl_basic_set_detect_equalities(
1133 __isl_take isl_basic_set
*bset
)
1135 return (isl_basic_set
*)
1136 isl_basic_map_detect_equalities((isl_basic_map
*)bset
);
1139 __isl_give isl_map
*isl_map_detect_equalities(__isl_take isl_map
*map
)
1141 return isl_map_inline_foreach_basic_map(map
,
1142 &isl_basic_map_detect_equalities
);
1145 __isl_give isl_set
*isl_set_detect_equalities(__isl_take isl_set
*set
)
1147 return (isl_set
*)isl_map_detect_equalities((isl_map
*)set
);
1150 /* After computing the rational affine hull (by detecting the implicit
1151 * equalities), we compute the additional equalities satisfied by
1152 * the integer points (if any) and add the original equalities back in.
1154 struct isl_basic_map
*isl_basic_map_affine_hull(struct isl_basic_map
*bmap
)
1156 bmap
= isl_basic_map_detect_equalities(bmap
);
1157 bmap
= isl_basic_map_cow(bmap
);
1159 isl_basic_map_free_inequality(bmap
, bmap
->n_ineq
);
1160 bmap
= isl_basic_map_finalize(bmap
);
1164 struct isl_basic_set
*isl_basic_set_affine_hull(struct isl_basic_set
*bset
)
1166 return (struct isl_basic_set
*)
1167 isl_basic_map_affine_hull((struct isl_basic_map
*)bset
);
1170 /* Given a rational affine matrix "M", add stride constraints to "bmap"
1175 * is an integer vector. The variables x include all the variables
1176 * of "bmap" except the unknown divs.
1178 * If d is the common denominator of M, then we need to impose that
1184 * exists alpha : d M(x) = d alpha
1186 * This function is similar to add_strides in isl_morph.c
1188 static __isl_give isl_basic_map
*add_strides(__isl_take isl_basic_map
*bmap
,
1189 __isl_keep isl_mat
*M
, int n_known
)
1194 if (isl_int_is_one(M
->row
[0][0]))
1197 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
1198 M
->n_row
- 1, M
->n_row
- 1, 0);
1201 for (i
= 1; i
< M
->n_row
; ++i
) {
1202 isl_seq_gcd(M
->row
[i
], M
->n_col
, &gcd
);
1203 if (isl_int_is_divisible_by(gcd
, M
->row
[0][0]))
1205 div
= isl_basic_map_alloc_div(bmap
);
1208 isl_int_set_si(bmap
->div
[div
][0], 0);
1209 k
= isl_basic_map_alloc_equality(bmap
);
1212 isl_seq_cpy(bmap
->eq
[k
], M
->row
[i
], M
->n_col
);
1213 isl_seq_clr(bmap
->eq
[k
] + M
->n_col
, bmap
->n_div
- n_known
);
1214 isl_int_set(bmap
->eq
[k
][M
->n_col
- n_known
+ div
],
1222 isl_basic_map_free(bmap
);
1226 /* If there are any equalities that involve (multiple) unknown divs,
1227 * then extract the stride information encoded by those equalities
1228 * and make it explicitly available in "bmap".
1230 * We first sort the divs so that the unknown divs appear last and
1231 * then we count how many equalities involve these divs.
1233 * Let these equalities be of the form
1237 * where y represents the unknown divs and x the remaining variables.
1238 * Let [H 0] be the Hermite Normal Form of B, i.e.,
1242 * Then x is a solution of the equalities iff
1244 * H^-1 A(x) (= - [I 0] Q y)
1246 * is an integer vector. Let d be the common denominator of H^-1.
1249 * d H^-1 A(x) = d alpha
1251 * in add_strides, with alpha fresh existentially quantified variables.
1253 static __isl_give isl_basic_map
*isl_basic_map_make_strides_explicit(
1254 __isl_take isl_basic_map
*bmap
)
1263 known
= isl_basic_map_divs_known(bmap
);
1265 return isl_basic_map_free(bmap
);
1268 bmap
= isl_basic_map_sort_divs(bmap
);
1269 bmap
= isl_basic_map_gauss(bmap
, NULL
);
1273 for (n_known
= 0; n_known
< bmap
->n_div
; ++n_known
)
1274 if (isl_int_is_zero(bmap
->div
[n_known
][0]))
1276 ctx
= isl_basic_map_get_ctx(bmap
);
1277 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
1278 for (n
= 0; n
< bmap
->n_eq
; ++n
)
1279 if (isl_seq_first_non_zero(bmap
->eq
[n
] + 1 + total
+ n_known
,
1280 bmap
->n_div
- n_known
) == -1)
1284 B
= isl_mat_sub_alloc6(ctx
, bmap
->eq
, 0, n
, 0, 1 + total
+ n_known
);
1285 n_col
= bmap
->n_div
- n_known
;
1286 A
= isl_mat_sub_alloc6(ctx
, bmap
->eq
, 0, n
, 1 + total
+ n_known
, n_col
);
1287 A
= isl_mat_left_hermite(A
, 0, NULL
, NULL
);
1288 A
= isl_mat_drop_cols(A
, n
, n_col
- n
);
1289 A
= isl_mat_lin_to_aff(A
);
1290 A
= isl_mat_right_inverse(A
);
1291 B
= isl_mat_insert_zero_rows(B
, 0, 1);
1292 B
= isl_mat_set_element_si(B
, 0, 0, 1);
1293 M
= isl_mat_product(A
, B
);
1295 return isl_basic_map_free(bmap
);
1296 bmap
= add_strides(bmap
, M
, n_known
);
1297 bmap
= isl_basic_map_gauss(bmap
, NULL
);
1303 /* Compute the affine hull of each basic map in "map" separately
1304 * and make all stride information explicit so that we can remove
1305 * all unknown divs without losing this information.
1306 * The result is also guaranteed to be gaussed.
1308 * In simple cases where a div is determined by an equality,
1309 * calling isl_basic_map_gauss is enough to make the stride information
1310 * explicit, as it will derive an explicit representation for the div
1311 * from the equality. If, however, the stride information
1312 * is encoded through multiple unknown divs then we need to make
1313 * some extra effort in isl_basic_map_make_strides_explicit.
1315 static __isl_give isl_map
*isl_map_local_affine_hull(__isl_take isl_map
*map
)
1319 map
= isl_map_cow(map
);
1323 for (i
= 0; i
< map
->n
; ++i
) {
1324 map
->p
[i
] = isl_basic_map_affine_hull(map
->p
[i
]);
1325 map
->p
[i
] = isl_basic_map_gauss(map
->p
[i
], NULL
);
1326 map
->p
[i
] = isl_basic_map_make_strides_explicit(map
->p
[i
]);
1328 return isl_map_free(map
);
1334 static __isl_give isl_set
*isl_set_local_affine_hull(__isl_take isl_set
*set
)
1336 return isl_map_local_affine_hull(set
);
1339 /* Compute the affine hull of "map".
1341 * We first compute the affine hull of each basic map separately.
1342 * Then we align the divs and recompute the affine hulls of the basic
1343 * maps since some of them may now have extra divs.
1344 * In order to avoid performing parametric integer programming to
1345 * compute explicit expressions for the divs, possible leading to
1346 * an explosion in the number of basic maps, we first drop all unknown
1347 * divs before aligning the divs. Note that isl_map_local_affine_hull tries
1348 * to make sure that all stride information is explicitly available
1349 * in terms of known divs. This involves calling isl_basic_set_gauss,
1350 * which is also needed because affine_hull assumes its input has been gaussed,
1351 * while isl_map_affine_hull may be called on input that has not been gaussed,
1352 * in particular from initial_facet_constraint.
1353 * Similarly, align_divs may reorder some divs so that we need to
1354 * gauss the result again.
1355 * Finally, we combine the individual affine hulls into a single
1358 __isl_give isl_basic_map
*isl_map_affine_hull(__isl_take isl_map
*map
)
1360 struct isl_basic_map
*model
= NULL
;
1361 struct isl_basic_map
*hull
= NULL
;
1362 struct isl_set
*set
;
1363 isl_basic_set
*bset
;
1365 map
= isl_map_detect_equalities(map
);
1366 map
= isl_map_local_affine_hull(map
);
1367 map
= isl_map_remove_empty_parts(map
);
1368 map
= isl_map_remove_unknown_divs(map
);
1369 map
= isl_map_align_divs(map
);
1375 hull
= isl_basic_map_empty_like_map(map
);
1380 model
= isl_basic_map_copy(map
->p
[0]);
1381 set
= isl_map_underlying_set(map
);
1382 set
= isl_set_cow(set
);
1383 set
= isl_set_local_affine_hull(set
);
1388 set
->p
[0] = affine_hull(set
->p
[0], set
->p
[--set
->n
]);
1390 bset
= isl_basic_set_copy(set
->p
[0]);
1391 hull
= isl_basic_map_overlying_set(bset
, model
);
1393 hull
= isl_basic_map_simplify(hull
);
1394 return isl_basic_map_finalize(hull
);
1396 isl_basic_map_free(model
);
1401 struct isl_basic_set
*isl_set_affine_hull(struct isl_set
*set
)
1403 return (struct isl_basic_set
*)
1404 isl_map_affine_hull((struct isl_map
*)set
);