2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
5 * Use of this software is governed by the GNU LGPLv2.1 license
7 * Written by Sven Verdoolaege, K.U.Leuven, Departement
8 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
9 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
10 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
13 #include "isl_map_private.h"
15 #include <isl/options.h>
17 #include <isl_mat_private.h>
19 #define STATUS_ERROR -1
20 #define STATUS_REDUNDANT 1
21 #define STATUS_VALID 2
22 #define STATUS_SEPARATE 3
24 #define STATUS_ADJ_EQ 5
25 #define STATUS_ADJ_INEQ 6
27 static int status_in(isl_int
*ineq
, struct isl_tab
*tab
)
29 enum isl_ineq_type type
= isl_tab_ineq_type(tab
, ineq
);
32 case isl_ineq_error
: return STATUS_ERROR
;
33 case isl_ineq_redundant
: return STATUS_VALID
;
34 case isl_ineq_separate
: return STATUS_SEPARATE
;
35 case isl_ineq_cut
: return STATUS_CUT
;
36 case isl_ineq_adj_eq
: return STATUS_ADJ_EQ
;
37 case isl_ineq_adj_ineq
: return STATUS_ADJ_INEQ
;
41 /* Compute the position of the equalities of basic map "i"
42 * with respect to basic map "j".
43 * The resulting array has twice as many entries as the number
44 * of equalities corresponding to the two inequalties to which
45 * each equality corresponds.
47 static int *eq_status_in(struct isl_map
*map
, int i
, int j
,
48 struct isl_tab
**tabs
)
51 int *eq
= isl_calloc_array(map
->ctx
, int, 2 * map
->p
[i
]->n_eq
);
54 dim
= isl_basic_map_total_dim(map
->p
[i
]);
55 for (k
= 0; k
< map
->p
[i
]->n_eq
; ++k
) {
56 for (l
= 0; l
< 2; ++l
) {
57 isl_seq_neg(map
->p
[i
]->eq
[k
], map
->p
[i
]->eq
[k
], 1+dim
);
58 eq
[2 * k
+ l
] = status_in(map
->p
[i
]->eq
[k
], tabs
[j
]);
59 if (eq
[2 * k
+ l
] == STATUS_ERROR
)
62 if (eq
[2 * k
] == STATUS_SEPARATE
||
63 eq
[2 * k
+ 1] == STATUS_SEPARATE
)
73 /* Compute the position of the inequalities of basic map "i"
74 * with respect to basic map "j".
76 static int *ineq_status_in(struct isl_map
*map
, int i
, int j
,
77 struct isl_tab
**tabs
)
80 unsigned n_eq
= map
->p
[i
]->n_eq
;
81 int *ineq
= isl_calloc_array(map
->ctx
, int, map
->p
[i
]->n_ineq
);
83 for (k
= 0; k
< map
->p
[i
]->n_ineq
; ++k
) {
84 if (isl_tab_is_redundant(tabs
[i
], n_eq
+ k
)) {
85 ineq
[k
] = STATUS_REDUNDANT
;
88 ineq
[k
] = status_in(map
->p
[i
]->ineq
[k
], tabs
[j
]);
89 if (ineq
[k
] == STATUS_ERROR
)
91 if (ineq
[k
] == STATUS_SEPARATE
)
101 static int any(int *con
, unsigned len
, int status
)
105 for (i
= 0; i
< len
; ++i
)
106 if (con
[i
] == status
)
111 static int count(int *con
, unsigned len
, int status
)
116 for (i
= 0; i
< len
; ++i
)
117 if (con
[i
] == status
)
122 static int all(int *con
, unsigned len
, int status
)
126 for (i
= 0; i
< len
; ++i
) {
127 if (con
[i
] == STATUS_REDUNDANT
)
129 if (con
[i
] != status
)
135 static void drop(struct isl_map
*map
, int i
, struct isl_tab
**tabs
)
137 isl_basic_map_free(map
->p
[i
]);
138 isl_tab_free(tabs
[i
]);
140 if (i
!= map
->n
- 1) {
141 map
->p
[i
] = map
->p
[map
->n
- 1];
142 tabs
[i
] = tabs
[map
->n
- 1];
144 tabs
[map
->n
- 1] = NULL
;
148 /* Replace the pair of basic maps i and j by the basic map bounded
149 * by the valid constraints in both basic maps and the constraint
150 * in extra (if not NULL).
152 static int fuse(struct isl_map
*map
, int i
, int j
,
153 struct isl_tab
**tabs
, int *eq_i
, int *ineq_i
, int *eq_j
, int *ineq_j
,
154 __isl_keep isl_mat
*extra
)
157 struct isl_basic_map
*fused
= NULL
;
158 struct isl_tab
*fused_tab
= NULL
;
159 unsigned total
= isl_basic_map_total_dim(map
->p
[i
]);
160 unsigned extra_rows
= extra
? extra
->n_row
: 0;
162 fused
= isl_basic_map_alloc_space(isl_space_copy(map
->p
[i
]->dim
),
164 map
->p
[i
]->n_eq
+ map
->p
[j
]->n_eq
,
165 map
->p
[i
]->n_ineq
+ map
->p
[j
]->n_ineq
+ extra_rows
);
169 for (k
= 0; k
< map
->p
[i
]->n_eq
; ++k
) {
170 if (eq_i
&& (eq_i
[2 * k
] != STATUS_VALID
||
171 eq_i
[2 * k
+ 1] != STATUS_VALID
))
173 l
= isl_basic_map_alloc_equality(fused
);
176 isl_seq_cpy(fused
->eq
[l
], map
->p
[i
]->eq
[k
], 1 + total
);
179 for (k
= 0; k
< map
->p
[j
]->n_eq
; ++k
) {
180 if (eq_j
&& (eq_j
[2 * k
] != STATUS_VALID
||
181 eq_j
[2 * k
+ 1] != STATUS_VALID
))
183 l
= isl_basic_map_alloc_equality(fused
);
186 isl_seq_cpy(fused
->eq
[l
], map
->p
[j
]->eq
[k
], 1 + total
);
189 for (k
= 0; k
< map
->p
[i
]->n_ineq
; ++k
) {
190 if (ineq_i
[k
] != STATUS_VALID
)
192 l
= isl_basic_map_alloc_inequality(fused
);
195 isl_seq_cpy(fused
->ineq
[l
], map
->p
[i
]->ineq
[k
], 1 + total
);
198 for (k
= 0; k
< map
->p
[j
]->n_ineq
; ++k
) {
199 if (ineq_j
[k
] != STATUS_VALID
)
201 l
= isl_basic_map_alloc_inequality(fused
);
204 isl_seq_cpy(fused
->ineq
[l
], map
->p
[j
]->ineq
[k
], 1 + total
);
207 for (k
= 0; k
< map
->p
[i
]->n_div
; ++k
) {
208 int l
= isl_basic_map_alloc_div(fused
);
211 isl_seq_cpy(fused
->div
[l
], map
->p
[i
]->div
[k
], 1 + 1 + total
);
214 for (k
= 0; k
< extra_rows
; ++k
) {
215 l
= isl_basic_map_alloc_inequality(fused
);
218 isl_seq_cpy(fused
->ineq
[l
], extra
->row
[k
], 1 + total
);
221 fused
= isl_basic_map_gauss(fused
, NULL
);
222 ISL_F_SET(fused
, ISL_BASIC_MAP_FINAL
);
223 if (ISL_F_ISSET(map
->p
[i
], ISL_BASIC_MAP_RATIONAL
) &&
224 ISL_F_ISSET(map
->p
[j
], ISL_BASIC_MAP_RATIONAL
))
225 ISL_F_SET(fused
, ISL_BASIC_MAP_RATIONAL
);
227 fused_tab
= isl_tab_from_basic_map(fused
, 0);
228 if (isl_tab_detect_redundant(fused_tab
) < 0)
231 isl_basic_map_free(map
->p
[i
]);
233 isl_tab_free(tabs
[i
]);
239 isl_tab_free(fused_tab
);
240 isl_basic_map_free(fused
);
244 /* Given a pair of basic maps i and j such that all constraints are either
245 * "valid" or "cut", check if the facets corresponding to the "cut"
246 * constraints of i lie entirely within basic map j.
247 * If so, replace the pair by the basic map consisting of the valid
248 * constraints in both basic maps.
250 * To see that we are not introducing any extra points, call the
251 * two basic maps A and B and the resulting map U and let x
252 * be an element of U \setminus ( A \cup B ).
253 * Then there is a pair of cut constraints c_1 and c_2 in A and B such that x
254 * violates them. Let X be the intersection of U with the opposites
255 * of these constraints. Then x \in X.
256 * The facet corresponding to c_1 contains the corresponding facet of A.
257 * This facet is entirely contained in B, so c_2 is valid on the facet.
258 * However, since it is also (part of) a facet of X, -c_2 is also valid
259 * on the facet. This means c_2 is saturated on the facet, so c_1 and
260 * c_2 must be opposites of each other, but then x could not violate
263 static int check_facets(struct isl_map
*map
, int i
, int j
,
264 struct isl_tab
**tabs
, int *ineq_i
, int *ineq_j
)
267 struct isl_tab_undo
*snap
;
268 unsigned n_eq
= map
->p
[i
]->n_eq
;
270 snap
= isl_tab_snap(tabs
[i
]);
272 for (k
= 0; k
< map
->p
[i
]->n_ineq
; ++k
) {
273 if (ineq_i
[k
] != STATUS_CUT
)
275 if (isl_tab_select_facet(tabs
[i
], n_eq
+ k
) < 0)
277 for (l
= 0; l
< map
->p
[j
]->n_ineq
; ++l
) {
279 if (ineq_j
[l
] != STATUS_CUT
)
281 stat
= status_in(map
->p
[j
]->ineq
[l
], tabs
[i
]);
282 if (stat
!= STATUS_VALID
)
285 if (isl_tab_rollback(tabs
[i
], snap
) < 0)
287 if (l
< map
->p
[j
]->n_ineq
)
291 if (k
< map
->p
[i
]->n_ineq
)
294 return fuse(map
, i
, j
, tabs
, NULL
, ineq_i
, NULL
, ineq_j
, NULL
);
297 /* Both basic maps have at least one inequality with and adjacent
298 * (but opposite) inequality in the other basic map.
299 * Check that there are no cut constraints and that there is only
300 * a single pair of adjacent inequalities.
301 * If so, we can replace the pair by a single basic map described
302 * by all but the pair of adjacent inequalities.
303 * Any additional points introduced lie strictly between the two
304 * adjacent hyperplanes and can therefore be integral.
313 * The test for a single pair of adjancent inequalities is important
314 * for avoiding the combination of two basic maps like the following
324 static int check_adj_ineq(struct isl_map
*map
, int i
, int j
,
325 struct isl_tab
**tabs
, int *ineq_i
, int *ineq_j
)
329 if (any(ineq_i
, map
->p
[i
]->n_ineq
, STATUS_CUT
) ||
330 any(ineq_j
, map
->p
[j
]->n_ineq
, STATUS_CUT
))
333 else if (count(ineq_i
, map
->p
[i
]->n_ineq
, STATUS_ADJ_INEQ
) == 1 &&
334 count(ineq_j
, map
->p
[j
]->n_ineq
, STATUS_ADJ_INEQ
) == 1)
335 changed
= fuse(map
, i
, j
, tabs
, NULL
, ineq_i
, NULL
, ineq_j
, NULL
);
336 /* else ADJ INEQ TOO MANY */
341 /* Check if basic map "i" contains the basic map represented
342 * by the tableau "tab".
344 static int contains(struct isl_map
*map
, int i
, int *ineq_i
,
350 dim
= isl_basic_map_total_dim(map
->p
[i
]);
351 for (k
= 0; k
< map
->p
[i
]->n_eq
; ++k
) {
352 for (l
= 0; l
< 2; ++l
) {
354 isl_seq_neg(map
->p
[i
]->eq
[k
], map
->p
[i
]->eq
[k
], 1+dim
);
355 stat
= status_in(map
->p
[i
]->eq
[k
], tab
);
356 if (stat
!= STATUS_VALID
)
361 for (k
= 0; k
< map
->p
[i
]->n_ineq
; ++k
) {
363 if (ineq_i
[k
] == STATUS_REDUNDANT
)
365 stat
= status_in(map
->p
[i
]->ineq
[k
], tab
);
366 if (stat
!= STATUS_VALID
)
372 /* Basic map "i" has an inequality "k" that is adjacent to some equality
373 * of basic map "j". All the other inequalities are valid for "j".
374 * Check if basic map "j" forms an extension of basic map "i".
376 * In particular, we relax constraint "k", compute the corresponding
377 * facet and check whether it is included in the other basic map.
378 * If so, we know that relaxing the constraint extends the basic
379 * map with exactly the other basic map (we already know that this
380 * other basic map is included in the extension, because there
381 * were no "cut" inequalities in "i") and we can replace the
382 * two basic maps by thie extension.
390 static int is_extension(struct isl_map
*map
, int i
, int j
, int k
,
391 struct isl_tab
**tabs
, int *eq_i
, int *ineq_i
, int *eq_j
, int *ineq_j
)
395 struct isl_tab_undo
*snap
, *snap2
;
396 unsigned n_eq
= map
->p
[i
]->n_eq
;
398 if (isl_tab_is_equality(tabs
[i
], n_eq
+ k
))
401 snap
= isl_tab_snap(tabs
[i
]);
402 tabs
[i
] = isl_tab_relax(tabs
[i
], n_eq
+ k
);
403 snap2
= isl_tab_snap(tabs
[i
]);
404 if (isl_tab_select_facet(tabs
[i
], n_eq
+ k
) < 0)
406 super
= contains(map
, j
, ineq_j
, tabs
[i
]);
408 if (isl_tab_rollback(tabs
[i
], snap2
) < 0)
410 map
->p
[i
] = isl_basic_map_cow(map
->p
[i
]);
413 isl_int_add_ui(map
->p
[i
]->ineq
[k
][0], map
->p
[i
]->ineq
[k
][0], 1);
414 ISL_F_SET(map
->p
[i
], ISL_BASIC_MAP_FINAL
);
418 if (isl_tab_rollback(tabs
[i
], snap
) < 0)
424 /* Data structure that keeps track of the wrapping constraints
425 * and of information to bound the coefficients of those constraints.
427 * bound is set if we want to apply a bound on the coefficients
428 * mat contains the wrapping constraints
429 * max is the bound on the coefficients (if bound is set)
437 /* Update wraps->max to be greater than or equal to the coefficients
438 * in the equalities and inequalities of bmap that can be removed if we end up
441 static void wraps_update_max(struct isl_wraps
*wraps
,
442 __isl_keep isl_basic_map
*bmap
, int *eq
, int *ineq
)
446 unsigned total
= isl_basic_map_total_dim(bmap
);
450 for (k
= 0; k
< bmap
->n_eq
; ++k
) {
451 if (eq
[2 * k
] == STATUS_VALID
&&
452 eq
[2 * k
+ 1] == STATUS_VALID
)
454 isl_seq_abs_max(bmap
->eq
[k
] + 1, total
, &max_k
);
455 if (isl_int_abs_gt(max_k
, wraps
->max
))
456 isl_int_set(wraps
->max
, max_k
);
459 for (k
= 0; k
< bmap
->n_ineq
; ++k
) {
460 if (ineq
[k
] == STATUS_VALID
|| ineq
[k
] == STATUS_REDUNDANT
)
462 isl_seq_abs_max(bmap
->ineq
[k
] + 1, total
, &max_k
);
463 if (isl_int_abs_gt(max_k
, wraps
->max
))
464 isl_int_set(wraps
->max
, max_k
);
467 isl_int_clear(max_k
);
470 /* Initialize the isl_wraps data structure.
471 * If we want to bound the coefficients of the wrapping constraints,
472 * we set wraps->max to the largest coefficient
473 * in the equalities and inequalities that can be removed if we end up
476 static void wraps_init(struct isl_wraps
*wraps
, __isl_take isl_mat
*mat
,
477 __isl_keep isl_map
*map
, int i
, int j
,
478 int *eq_i
, int *ineq_i
, int *eq_j
, int *ineq_j
)
486 ctx
= isl_mat_get_ctx(mat
);
487 wraps
->bound
= isl_options_get_coalesce_bounded_wrapping(ctx
);
490 isl_int_init(wraps
->max
);
491 isl_int_set_si(wraps
->max
, 0);
492 wraps_update_max(wraps
, map
->p
[i
], eq_i
, ineq_i
);
493 wraps_update_max(wraps
, map
->p
[j
], eq_j
, ineq_j
);
496 /* Free the contents of the isl_wraps data structure.
498 static void wraps_free(struct isl_wraps
*wraps
)
500 isl_mat_free(wraps
->mat
);
502 isl_int_clear(wraps
->max
);
505 /* Is the wrapping constraint in row "row" allowed?
507 * If wraps->bound is set, we check that none of the coefficients
508 * is greater than wraps->max.
510 static int allow_wrap(struct isl_wraps
*wraps
, int row
)
517 for (i
= 1; i
< wraps
->mat
->n_col
; ++i
)
518 if (isl_int_abs_gt(wraps
->mat
->row
[row
][i
], wraps
->max
))
524 /* For each non-redundant constraint in "bmap" (as determined by "tab"),
525 * wrap the constraint around "bound" such that it includes the whole
526 * set "set" and append the resulting constraint to "wraps".
527 * "wraps" is assumed to have been pre-allocated to the appropriate size.
528 * wraps->n_row is the number of actual wrapped constraints that have
530 * If any of the wrapping problems results in a constraint that is
531 * identical to "bound", then this means that "set" is unbounded in such
532 * way that no wrapping is possible. If this happens then wraps->n_row
534 * Similarly, if we want to bound the coefficients of the wrapping
535 * constraints and a newly added wrapping constraint does not
536 * satisfy the bound, then wraps->n_row is also reset to zero.
538 static int add_wraps(struct isl_wraps
*wraps
, __isl_keep isl_basic_map
*bmap
,
539 struct isl_tab
*tab
, isl_int
*bound
, __isl_keep isl_set
*set
)
543 unsigned total
= isl_basic_map_total_dim(bmap
);
545 w
= wraps
->mat
->n_row
;
547 for (l
= 0; l
< bmap
->n_ineq
; ++l
) {
548 if (isl_seq_is_neg(bound
, bmap
->ineq
[l
], 1 + total
))
550 if (isl_seq_eq(bound
, bmap
->ineq
[l
], 1 + total
))
552 if (isl_tab_is_redundant(tab
, bmap
->n_eq
+ l
))
555 isl_seq_cpy(wraps
->mat
->row
[w
], bound
, 1 + total
);
556 if (!isl_set_wrap_facet(set
, wraps
->mat
->row
[w
], bmap
->ineq
[l
]))
558 if (isl_seq_eq(wraps
->mat
->row
[w
], bound
, 1 + total
))
560 if (!allow_wrap(wraps
, w
))
564 for (l
= 0; l
< bmap
->n_eq
; ++l
) {
565 if (isl_seq_is_neg(bound
, bmap
->eq
[l
], 1 + total
))
567 if (isl_seq_eq(bound
, bmap
->eq
[l
], 1 + total
))
570 isl_seq_cpy(wraps
->mat
->row
[w
], bound
, 1 + total
);
571 isl_seq_neg(wraps
->mat
->row
[w
+ 1], bmap
->eq
[l
], 1 + total
);
572 if (!isl_set_wrap_facet(set
, wraps
->mat
->row
[w
],
573 wraps
->mat
->row
[w
+ 1]))
575 if (isl_seq_eq(wraps
->mat
->row
[w
], bound
, 1 + total
))
577 if (!allow_wrap(wraps
, w
))
581 isl_seq_cpy(wraps
->mat
->row
[w
], bound
, 1 + total
);
582 if (!isl_set_wrap_facet(set
, wraps
->mat
->row
[w
], bmap
->eq
[l
]))
584 if (isl_seq_eq(wraps
->mat
->row
[w
], bound
, 1 + total
))
586 if (!allow_wrap(wraps
, w
))
591 wraps
->mat
->n_row
= w
;
594 wraps
->mat
->n_row
= 0;
598 /* Check if the constraints in "wraps" from "first" until the last
599 * are all valid for the basic set represented by "tab".
600 * If not, wraps->n_row is set to zero.
602 static int check_wraps(__isl_keep isl_mat
*wraps
, int first
,
607 for (i
= first
; i
< wraps
->n_row
; ++i
) {
608 enum isl_ineq_type type
;
609 type
= isl_tab_ineq_type(tab
, wraps
->row
[i
]);
610 if (type
== isl_ineq_error
)
612 if (type
== isl_ineq_redundant
)
621 /* Return a set that corresponds to the non-redudant constraints
622 * (as recorded in tab) of bmap.
624 * It's important to remove the redundant constraints as some
625 * of the other constraints may have been modified after the
626 * constraints were marked redundant.
627 * In particular, a constraint may have been relaxed.
628 * Redundant constraints are ignored when a constraint is relaxed
629 * and should therefore continue to be ignored ever after.
630 * Otherwise, the relaxation might be thwarted by some of
633 static __isl_give isl_set
*set_from_updated_bmap(__isl_keep isl_basic_map
*bmap
,
636 bmap
= isl_basic_map_copy(bmap
);
637 bmap
= isl_basic_map_cow(bmap
);
638 bmap
= isl_basic_map_update_from_tab(bmap
, tab
);
639 return isl_set_from_basic_set(isl_basic_map_underlying_set(bmap
));
642 /* Given a basic set i with a constraint k that is adjacent to either the
643 * whole of basic set j or a facet of basic set j, check if we can wrap
644 * both the facet corresponding to k and the facet of j (or the whole of j)
645 * around their ridges to include the other set.
646 * If so, replace the pair of basic sets by their union.
648 * All constraints of i (except k) are assumed to be valid for j.
650 * However, the constraints of j may not be valid for i and so
651 * we have to check that the wrapping constraints for j are valid for i.
653 * In the case where j has a facet adjacent to i, tab[j] is assumed
654 * to have been restricted to this facet, so that the non-redundant
655 * constraints in tab[j] are the ridges of the facet.
656 * Note that for the purpose of wrapping, it does not matter whether
657 * we wrap the ridges of i around the whole of j or just around
658 * the facet since all the other constraints are assumed to be valid for j.
659 * In practice, we wrap to include the whole of j.
668 static int can_wrap_in_facet(struct isl_map
*map
, int i
, int j
, int k
,
669 struct isl_tab
**tabs
, int *eq_i
, int *ineq_i
, int *eq_j
, int *ineq_j
)
672 struct isl_wraps wraps
;
674 struct isl_set
*set_i
= NULL
;
675 struct isl_set
*set_j
= NULL
;
676 struct isl_vec
*bound
= NULL
;
677 unsigned total
= isl_basic_map_total_dim(map
->p
[i
]);
678 struct isl_tab_undo
*snap
;
681 set_i
= set_from_updated_bmap(map
->p
[i
], tabs
[i
]);
682 set_j
= set_from_updated_bmap(map
->p
[j
], tabs
[j
]);
683 mat
= isl_mat_alloc(map
->ctx
, 2 * (map
->p
[i
]->n_eq
+ map
->p
[j
]->n_eq
) +
684 map
->p
[i
]->n_ineq
+ map
->p
[j
]->n_ineq
,
686 wraps_init(&wraps
, mat
, map
, i
, j
, eq_i
, ineq_i
, eq_j
, ineq_j
);
687 bound
= isl_vec_alloc(map
->ctx
, 1 + total
);
688 if (!set_i
|| !set_j
|| !wraps
.mat
|| !bound
)
691 isl_seq_cpy(bound
->el
, map
->p
[i
]->ineq
[k
], 1 + total
);
692 isl_int_add_ui(bound
->el
[0], bound
->el
[0], 1);
694 isl_seq_cpy(wraps
.mat
->row
[0], bound
->el
, 1 + total
);
695 wraps
.mat
->n_row
= 1;
697 if (add_wraps(&wraps
, map
->p
[j
], tabs
[j
], bound
->el
, set_i
) < 0)
699 if (!wraps
.mat
->n_row
)
702 snap
= isl_tab_snap(tabs
[i
]);
704 if (isl_tab_select_facet(tabs
[i
], map
->p
[i
]->n_eq
+ k
) < 0)
706 if (isl_tab_detect_redundant(tabs
[i
]) < 0)
709 isl_seq_neg(bound
->el
, map
->p
[i
]->ineq
[k
], 1 + total
);
711 n
= wraps
.mat
->n_row
;
712 if (add_wraps(&wraps
, map
->p
[i
], tabs
[i
], bound
->el
, set_j
) < 0)
715 if (isl_tab_rollback(tabs
[i
], snap
) < 0)
717 if (check_wraps(wraps
.mat
, n
, tabs
[i
]) < 0)
719 if (!wraps
.mat
->n_row
)
722 changed
= fuse(map
, i
, j
, tabs
, eq_i
, ineq_i
, eq_j
, ineq_j
, wraps
.mat
);
741 /* Set the is_redundant property of the "n" constraints in "cuts",
743 * This is a fairly tricky operation as it bypasses isl_tab.c.
744 * The reason we want to temporarily mark some constraints redundant
745 * is that we want to ignore them in add_wraps.
747 * Initially all cut constraints are non-redundant, but the
748 * selection of a facet right before the call to this function
749 * may have made some of them redundant.
750 * Likewise, the same constraints are marked non-redundant
751 * in the second call to this function, before they are officially
752 * made non-redundant again in the subsequent rollback.
754 static void set_is_redundant(struct isl_tab
*tab
, unsigned n_eq
,
755 int *cuts
, int n
, int k
, int v
)
759 for (l
= 0; l
< n
; ++l
) {
762 tab
->con
[n_eq
+ cuts
[l
]].is_redundant
= v
;
766 /* Given a pair of basic maps i and j such that j sticks out
767 * of i at n cut constraints, each time by at most one,
768 * try to compute wrapping constraints and replace the two
769 * basic maps by a single basic map.
770 * The other constraints of i are assumed to be valid for j.
772 * The facets of i corresponding to the cut constraints are
773 * wrapped around their ridges, except those ridges determined
774 * by any of the other cut constraints.
775 * The intersections of cut constraints need to be ignored
776 * as the result of wrapping one cut constraint around another
777 * would result in a constraint cutting the union.
778 * In each case, the facets are wrapped to include the union
779 * of the two basic maps.
781 * The pieces of j that lie at an offset of exactly one from
782 * one of the cut constraints of i are wrapped around their edges.
783 * Here, there is no need to ignore intersections because we
784 * are wrapping around the union of the two basic maps.
786 * If any wrapping fails, i.e., if we cannot wrap to touch
787 * the union, then we give up.
788 * Otherwise, the pair of basic maps is replaced by their union.
790 static int wrap_in_facets(struct isl_map
*map
, int i
, int j
,
791 int *cuts
, int n
, struct isl_tab
**tabs
,
792 int *eq_i
, int *ineq_i
, int *eq_j
, int *ineq_j
)
795 struct isl_wraps wraps
;
798 isl_vec
*bound
= NULL
;
799 unsigned total
= isl_basic_map_total_dim(map
->p
[i
]);
802 struct isl_tab_undo
*snap_i
, *snap_j
;
804 if (isl_tab_extend_cons(tabs
[j
], 1) < 0)
807 max_wrap
= 2 * (map
->p
[i
]->n_eq
+ map
->p
[j
]->n_eq
) +
808 map
->p
[i
]->n_ineq
+ map
->p
[j
]->n_ineq
;
811 set
= isl_set_union(set_from_updated_bmap(map
->p
[i
], tabs
[i
]),
812 set_from_updated_bmap(map
->p
[j
], tabs
[j
]));
813 mat
= isl_mat_alloc(map
->ctx
, max_wrap
, 1 + total
);
814 wraps_init(&wraps
, mat
, map
, i
, j
, eq_i
, ineq_i
, eq_j
, ineq_j
);
815 bound
= isl_vec_alloc(map
->ctx
, 1 + total
);
816 if (!set
|| !wraps
.mat
|| !bound
)
819 snap_i
= isl_tab_snap(tabs
[i
]);
820 snap_j
= isl_tab_snap(tabs
[j
]);
822 wraps
.mat
->n_row
= 0;
824 for (k
= 0; k
< n
; ++k
) {
825 if (isl_tab_select_facet(tabs
[i
], map
->p
[i
]->n_eq
+ cuts
[k
]) < 0)
827 if (isl_tab_detect_redundant(tabs
[i
]) < 0)
829 set_is_redundant(tabs
[i
], map
->p
[i
]->n_eq
, cuts
, n
, k
, 1);
831 isl_seq_neg(bound
->el
, map
->p
[i
]->ineq
[cuts
[k
]], 1 + total
);
832 if (!tabs
[i
]->empty
&&
833 add_wraps(&wraps
, map
->p
[i
], tabs
[i
], bound
->el
, set
) < 0)
836 set_is_redundant(tabs
[i
], map
->p
[i
]->n_eq
, cuts
, n
, k
, 0);
837 if (isl_tab_rollback(tabs
[i
], snap_i
) < 0)
842 if (!wraps
.mat
->n_row
)
845 isl_seq_cpy(bound
->el
, map
->p
[i
]->ineq
[cuts
[k
]], 1 + total
);
846 isl_int_add_ui(bound
->el
[0], bound
->el
[0], 1);
847 if (isl_tab_add_eq(tabs
[j
], bound
->el
) < 0)
849 if (isl_tab_detect_redundant(tabs
[j
]) < 0)
852 if (!tabs
[j
]->empty
&&
853 add_wraps(&wraps
, map
->p
[j
], tabs
[j
], bound
->el
, set
) < 0)
856 if (isl_tab_rollback(tabs
[j
], snap_j
) < 0)
859 if (!wraps
.mat
->n_row
)
864 changed
= fuse(map
, i
, j
, tabs
,
865 eq_i
, ineq_i
, eq_j
, ineq_j
, wraps
.mat
);
879 /* Given two basic sets i and j such that i has no cut equalities,
880 * check if relaxing all the cut inequalities of i by one turns
881 * them into valid constraint for j and check if we can wrap in
882 * the bits that are sticking out.
883 * If so, replace the pair by their union.
885 * We first check if all relaxed cut inequalities of i are valid for j
886 * and then try to wrap in the intersections of the relaxed cut inequalities
889 * During this wrapping, we consider the points of j that lie at a distance
890 * of exactly 1 from i. In particular, we ignore the points that lie in
891 * between this lower-dimensional space and the basic map i.
892 * We can therefore only apply this to integer maps.
918 * Wrapping can fail if the result of wrapping one of the facets
919 * around its edges does not produce any new facet constraint.
920 * In particular, this happens when we try to wrap in unbounded sets.
922 * _______________________________________________________________________
926 * |_| |_________________________________________________________________
929 * The following is not an acceptable result of coalescing the above two
930 * sets as it includes extra integer points.
931 * _______________________________________________________________________
936 * \______________________________________________________________________
938 static int can_wrap_in_set(struct isl_map
*map
, int i
, int j
,
939 struct isl_tab
**tabs
, int *eq_i
, int *ineq_i
, int *eq_j
, int *ineq_j
)
946 if (ISL_F_ISSET(map
->p
[i
], ISL_BASIC_MAP_RATIONAL
) ||
947 ISL_F_ISSET(map
->p
[j
], ISL_BASIC_MAP_RATIONAL
))
950 n
= count(ineq_i
, map
->p
[i
]->n_ineq
, STATUS_CUT
);
954 cuts
= isl_alloc_array(map
->ctx
, int, n
);
958 for (k
= 0, m
= 0; m
< n
; ++k
) {
959 enum isl_ineq_type type
;
961 if (ineq_i
[k
] != STATUS_CUT
)
964 isl_int_add_ui(map
->p
[i
]->ineq
[k
][0], map
->p
[i
]->ineq
[k
][0], 1);
965 type
= isl_tab_ineq_type(tabs
[j
], map
->p
[i
]->ineq
[k
]);
966 isl_int_sub_ui(map
->p
[i
]->ineq
[k
][0], map
->p
[i
]->ineq
[k
][0], 1);
967 if (type
== isl_ineq_error
)
969 if (type
!= isl_ineq_redundant
)
976 changed
= wrap_in_facets(map
, i
, j
, cuts
, n
, tabs
,
977 eq_i
, ineq_i
, eq_j
, ineq_j
);
987 /* Check if either i or j has a single cut constraint that can
988 * be used to wrap in (a facet of) the other basic set.
989 * if so, replace the pair by their union.
991 static int check_wrap(struct isl_map
*map
, int i
, int j
,
992 struct isl_tab
**tabs
, int *eq_i
, int *ineq_i
, int *eq_j
, int *ineq_j
)
996 if (!any(eq_i
, 2 * map
->p
[i
]->n_eq
, STATUS_CUT
))
997 changed
= can_wrap_in_set(map
, i
, j
, tabs
,
998 eq_i
, ineq_i
, eq_j
, ineq_j
);
1002 if (!any(eq_j
, 2 * map
->p
[j
]->n_eq
, STATUS_CUT
))
1003 changed
= can_wrap_in_set(map
, j
, i
, tabs
,
1004 eq_j
, ineq_j
, eq_i
, ineq_i
);
1008 /* At least one of the basic maps has an equality that is adjacent
1009 * to inequality. Make sure that only one of the basic maps has
1010 * such an equality and that the other basic map has exactly one
1011 * inequality adjacent to an equality.
1012 * We call the basic map that has the inequality "i" and the basic
1013 * map that has the equality "j".
1014 * If "i" has any "cut" (in)equality, then relaxing the inequality
1015 * by one would not result in a basic map that contains the other
1018 static int check_adj_eq(struct isl_map
*map
, int i
, int j
,
1019 struct isl_tab
**tabs
, int *eq_i
, int *ineq_i
, int *eq_j
, int *ineq_j
)
1024 if (any(eq_i
, 2 * map
->p
[i
]->n_eq
, STATUS_ADJ_INEQ
) &&
1025 any(eq_j
, 2 * map
->p
[j
]->n_eq
, STATUS_ADJ_INEQ
))
1026 /* ADJ EQ TOO MANY */
1029 if (any(eq_i
, 2 * map
->p
[i
]->n_eq
, STATUS_ADJ_INEQ
))
1030 return check_adj_eq(map
, j
, i
, tabs
,
1031 eq_j
, ineq_j
, eq_i
, ineq_i
);
1033 /* j has an equality adjacent to an inequality in i */
1035 if (any(eq_i
, 2 * map
->p
[i
]->n_eq
, STATUS_CUT
))
1037 if (any(ineq_i
, map
->p
[i
]->n_ineq
, STATUS_CUT
))
1040 if (count(ineq_i
, map
->p
[i
]->n_ineq
, STATUS_ADJ_EQ
) != 1 ||
1041 any(ineq_j
, map
->p
[j
]->n_ineq
, STATUS_ADJ_EQ
) ||
1042 any(ineq_i
, map
->p
[i
]->n_ineq
, STATUS_ADJ_INEQ
) ||
1043 any(ineq_j
, map
->p
[j
]->n_ineq
, STATUS_ADJ_INEQ
))
1044 /* ADJ EQ TOO MANY */
1047 for (k
= 0; k
< map
->p
[i
]->n_ineq
; ++k
)
1048 if (ineq_i
[k
] == STATUS_ADJ_EQ
)
1051 changed
= is_extension(map
, i
, j
, k
, tabs
, eq_i
, ineq_i
, eq_j
, ineq_j
);
1055 if (count(eq_j
, 2 * map
->p
[j
]->n_eq
, STATUS_ADJ_INEQ
) != 1)
1058 changed
= can_wrap_in_facet(map
, i
, j
, k
, tabs
, eq_i
, ineq_i
, eq_j
, ineq_j
);
1063 /* The two basic maps lie on adjacent hyperplanes. In particular,
1064 * basic map "i" has an equality that lies parallel to basic map "j".
1065 * Check if we can wrap the facets around the parallel hyperplanes
1066 * to include the other set.
1068 * We perform basically the same operations as can_wrap_in_facet,
1069 * except that we don't need to select a facet of one of the sets.
1075 * We only allow one equality of "i" to be adjacent to an equality of "j"
1076 * to avoid coalescing
1078 * [m, n] -> { [x, y] -> [x, 1 + y] : x >= 1 and y >= 1 and
1079 * x <= 10 and y <= 10;
1080 * [x, y] -> [1 + x, y] : x >= 1 and x <= 20 and
1081 * y >= 5 and y <= 15 }
1085 * [m, n] -> { [x, y] -> [x2, y2] : x >= 1 and 10y2 <= 20 - x + 10y and
1086 * 4y2 >= 5 + 3y and 5y2 <= 15 + 4y and
1087 * y2 <= 1 + x + y - x2 and y2 >= y and
1088 * y2 >= 1 + x + y - x2 }
1090 static int check_eq_adj_eq(struct isl_map
*map
, int i
, int j
,
1091 struct isl_tab
**tabs
, int *eq_i
, int *ineq_i
, int *eq_j
, int *ineq_j
)
1095 struct isl_wraps wraps
;
1097 struct isl_set
*set_i
= NULL
;
1098 struct isl_set
*set_j
= NULL
;
1099 struct isl_vec
*bound
= NULL
;
1100 unsigned total
= isl_basic_map_total_dim(map
->p
[i
]);
1102 if (count(eq_i
, 2 * map
->p
[i
]->n_eq
, STATUS_ADJ_EQ
) != 1)
1105 for (k
= 0; k
< 2 * map
->p
[i
]->n_eq
; ++k
)
1106 if (eq_i
[k
] == STATUS_ADJ_EQ
)
1109 set_i
= set_from_updated_bmap(map
->p
[i
], tabs
[i
]);
1110 set_j
= set_from_updated_bmap(map
->p
[j
], tabs
[j
]);
1111 mat
= isl_mat_alloc(map
->ctx
, 2 * (map
->p
[i
]->n_eq
+ map
->p
[j
]->n_eq
) +
1112 map
->p
[i
]->n_ineq
+ map
->p
[j
]->n_ineq
,
1114 wraps_init(&wraps
, mat
, map
, i
, j
, eq_i
, ineq_i
, eq_j
, ineq_j
);
1115 bound
= isl_vec_alloc(map
->ctx
, 1 + total
);
1116 if (!set_i
|| !set_j
|| !wraps
.mat
|| !bound
)
1120 isl_seq_neg(bound
->el
, map
->p
[i
]->eq
[k
/ 2], 1 + total
);
1122 isl_seq_cpy(bound
->el
, map
->p
[i
]->eq
[k
/ 2], 1 + total
);
1123 isl_int_add_ui(bound
->el
[0], bound
->el
[0], 1);
1125 isl_seq_cpy(wraps
.mat
->row
[0], bound
->el
, 1 + total
);
1126 wraps
.mat
->n_row
= 1;
1128 if (add_wraps(&wraps
, map
->p
[j
], tabs
[j
], bound
->el
, set_i
) < 0)
1130 if (!wraps
.mat
->n_row
)
1133 isl_int_sub_ui(bound
->el
[0], bound
->el
[0], 1);
1134 isl_seq_neg(bound
->el
, bound
->el
, 1 + total
);
1136 isl_seq_cpy(wraps
.mat
->row
[wraps
.mat
->n_row
], bound
->el
, 1 + total
);
1139 if (add_wraps(&wraps
, map
->p
[i
], tabs
[i
], bound
->el
, set_j
) < 0)
1141 if (!wraps
.mat
->n_row
)
1144 changed
= fuse(map
, i
, j
, tabs
, eq_i
, ineq_i
, eq_j
, ineq_j
, wraps
.mat
);
1147 error
: changed
= -1;
1152 isl_set_free(set_i
);
1153 isl_set_free(set_j
);
1154 isl_vec_free(bound
);
1159 /* Check if the union of the given pair of basic maps
1160 * can be represented by a single basic map.
1161 * If so, replace the pair by the single basic map and return 1.
1162 * Otherwise, return 0;
1164 * We first check the effect of each constraint of one basic map
1165 * on the other basic map.
1166 * The constraint may be
1167 * redundant the constraint is redundant in its own
1168 * basic map and should be ignore and removed
1170 * valid all (integer) points of the other basic map
1171 * satisfy the constraint
1172 * separate no (integer) point of the other basic map
1173 * satisfies the constraint
1174 * cut some but not all points of the other basic map
1175 * satisfy the constraint
1176 * adj_eq the given constraint is adjacent (on the outside)
1177 * to an equality of the other basic map
1178 * adj_ineq the given constraint is adjacent (on the outside)
1179 * to an inequality of the other basic map
1181 * We consider seven cases in which we can replace the pair by a single
1182 * basic map. We ignore all "redundant" constraints.
1184 * 1. all constraints of one basic map are valid
1185 * => the other basic map is a subset and can be removed
1187 * 2. all constraints of both basic maps are either "valid" or "cut"
1188 * and the facets corresponding to the "cut" constraints
1189 * of one of the basic maps lies entirely inside the other basic map
1190 * => the pair can be replaced by a basic map consisting
1191 * of the valid constraints in both basic maps
1193 * 3. there is a single pair of adjacent inequalities
1194 * (all other constraints are "valid")
1195 * => the pair can be replaced by a basic map consisting
1196 * of the valid constraints in both basic maps
1198 * 4. there is a single adjacent pair of an inequality and an equality,
1199 * the other constraints of the basic map containing the inequality are
1200 * "valid". Moreover, if the inequality the basic map is relaxed
1201 * and then turned into an equality, then resulting facet lies
1202 * entirely inside the other basic map
1203 * => the pair can be replaced by the basic map containing
1204 * the inequality, with the inequality relaxed.
1206 * 5. there is a single adjacent pair of an inequality and an equality,
1207 * the other constraints of the basic map containing the inequality are
1208 * "valid". Moreover, the facets corresponding to both
1209 * the inequality and the equality can be wrapped around their
1210 * ridges to include the other basic map
1211 * => the pair can be replaced by a basic map consisting
1212 * of the valid constraints in both basic maps together
1213 * with all wrapping constraints
1215 * 6. one of the basic maps extends beyond the other by at most one.
1216 * Moreover, the facets corresponding to the cut constraints and
1217 * the pieces of the other basic map at offset one from these cut
1218 * constraints can be wrapped around their ridges to include
1219 * the union of the two basic maps
1220 * => the pair can be replaced by a basic map consisting
1221 * of the valid constraints in both basic maps together
1222 * with all wrapping constraints
1224 * 7. the two basic maps live in adjacent hyperplanes. In principle
1225 * such sets can always be combined through wrapping, but we impose
1226 * that there is only one such pair, to avoid overeager coalescing.
1228 * Throughout the computation, we maintain a collection of tableaus
1229 * corresponding to the basic maps. When the basic maps are dropped
1230 * or combined, the tableaus are modified accordingly.
1232 static int coalesce_pair(struct isl_map
*map
, int i
, int j
,
1233 struct isl_tab
**tabs
)
1241 eq_i
= eq_status_in(map
, i
, j
, tabs
);
1244 if (any(eq_i
, 2 * map
->p
[i
]->n_eq
, STATUS_ERROR
))
1246 if (any(eq_i
, 2 * map
->p
[i
]->n_eq
, STATUS_SEPARATE
))
1249 eq_j
= eq_status_in(map
, j
, i
, tabs
);
1252 if (any(eq_j
, 2 * map
->p
[j
]->n_eq
, STATUS_ERROR
))
1254 if (any(eq_j
, 2 * map
->p
[j
]->n_eq
, STATUS_SEPARATE
))
1257 ineq_i
= ineq_status_in(map
, i
, j
, tabs
);
1260 if (any(ineq_i
, map
->p
[i
]->n_ineq
, STATUS_ERROR
))
1262 if (any(ineq_i
, map
->p
[i
]->n_ineq
, STATUS_SEPARATE
))
1265 ineq_j
= ineq_status_in(map
, j
, i
, tabs
);
1268 if (any(ineq_j
, map
->p
[j
]->n_ineq
, STATUS_ERROR
))
1270 if (any(ineq_j
, map
->p
[j
]->n_ineq
, STATUS_SEPARATE
))
1273 if (all(eq_i
, 2 * map
->p
[i
]->n_eq
, STATUS_VALID
) &&
1274 all(ineq_i
, map
->p
[i
]->n_ineq
, STATUS_VALID
)) {
1277 } else if (all(eq_j
, 2 * map
->p
[j
]->n_eq
, STATUS_VALID
) &&
1278 all(ineq_j
, map
->p
[j
]->n_ineq
, STATUS_VALID
)) {
1281 } else if (any(eq_i
, 2 * map
->p
[i
]->n_eq
, STATUS_ADJ_EQ
)) {
1282 changed
= check_eq_adj_eq(map
, i
, j
, tabs
,
1283 eq_i
, ineq_i
, eq_j
, ineq_j
);
1284 } else if (any(eq_j
, 2 * map
->p
[j
]->n_eq
, STATUS_ADJ_EQ
)) {
1285 changed
= check_eq_adj_eq(map
, j
, i
, tabs
,
1286 eq_j
, ineq_j
, eq_i
, ineq_i
);
1287 } else if (any(eq_i
, 2 * map
->p
[i
]->n_eq
, STATUS_ADJ_INEQ
) ||
1288 any(eq_j
, 2 * map
->p
[j
]->n_eq
, STATUS_ADJ_INEQ
)) {
1289 changed
= check_adj_eq(map
, i
, j
, tabs
,
1290 eq_i
, ineq_i
, eq_j
, ineq_j
);
1291 } else if (any(ineq_i
, map
->p
[i
]->n_ineq
, STATUS_ADJ_EQ
) ||
1292 any(ineq_j
, map
->p
[j
]->n_ineq
, STATUS_ADJ_EQ
)) {
1295 } else if (any(ineq_i
, map
->p
[i
]->n_ineq
, STATUS_ADJ_INEQ
) ||
1296 any(ineq_j
, map
->p
[j
]->n_ineq
, STATUS_ADJ_INEQ
)) {
1297 if (!any(eq_i
, 2 * map
->p
[i
]->n_eq
, STATUS_CUT
) &&
1298 !any(eq_j
, 2 * map
->p
[j
]->n_eq
, STATUS_CUT
))
1299 changed
= check_adj_ineq(map
, i
, j
, tabs
,
1302 if (!any(eq_i
, 2 * map
->p
[i
]->n_eq
, STATUS_CUT
) &&
1303 !any(eq_j
, 2 * map
->p
[j
]->n_eq
, STATUS_CUT
))
1304 changed
= check_facets(map
, i
, j
, tabs
, ineq_i
, ineq_j
);
1306 changed
= check_wrap(map
, i
, j
, tabs
,
1307 eq_i
, ineq_i
, eq_j
, ineq_j
);
1324 static struct isl_map
*coalesce(struct isl_map
*map
, struct isl_tab
**tabs
)
1328 for (i
= map
->n
- 2; i
>= 0; --i
)
1330 for (j
= i
+ 1; j
< map
->n
; ++j
) {
1332 changed
= coalesce_pair(map
, i
, j
, tabs
);
1344 /* For each pair of basic maps in the map, check if the union of the two
1345 * can be represented by a single basic map.
1346 * If so, replace the pair by the single basic map and start over.
1348 struct isl_map
*isl_map_coalesce(struct isl_map
*map
)
1352 struct isl_tab
**tabs
= NULL
;
1354 map
= isl_map_remove_empty_parts(map
);
1361 map
= isl_map_align_divs(map
);
1363 tabs
= isl_calloc_array(map
->ctx
, struct isl_tab
*, map
->n
);
1368 for (i
= 0; i
< map
->n
; ++i
) {
1369 tabs
[i
] = isl_tab_from_basic_map(map
->p
[i
], 0);
1372 if (!ISL_F_ISSET(map
->p
[i
], ISL_BASIC_MAP_NO_IMPLICIT
))
1373 if (isl_tab_detect_implicit_equalities(tabs
[i
]) < 0)
1375 if (!ISL_F_ISSET(map
->p
[i
], ISL_BASIC_MAP_NO_REDUNDANT
))
1376 if (isl_tab_detect_redundant(tabs
[i
]) < 0)
1379 for (i
= map
->n
- 1; i
>= 0; --i
)
1383 map
= coalesce(map
, tabs
);
1386 for (i
= 0; i
< map
->n
; ++i
) {
1387 map
->p
[i
] = isl_basic_map_update_from_tab(map
->p
[i
],
1389 map
->p
[i
] = isl_basic_map_finalize(map
->p
[i
]);
1392 ISL_F_SET(map
->p
[i
], ISL_BASIC_MAP_NO_IMPLICIT
);
1393 ISL_F_SET(map
->p
[i
], ISL_BASIC_MAP_NO_REDUNDANT
);
1396 for (i
= 0; i
< n
; ++i
)
1397 isl_tab_free(tabs
[i
]);
1404 for (i
= 0; i
< n
; ++i
)
1405 isl_tab_free(tabs
[i
]);
1411 /* For each pair of basic sets in the set, check if the union of the two
1412 * can be represented by a single basic set.
1413 * If so, replace the pair by the single basic set and start over.
1415 struct isl_set
*isl_set_coalesce(struct isl_set
*set
)
1417 return (struct isl_set
*)isl_map_coalesce((struct isl_map
*)set
);