2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2012-2013 Ecole Normale Superieure
5 * Copyright 2014 INRIA Rocquencourt
7 * Use of this software is governed by the MIT license
9 * Written by Sven Verdoolaege, K.U.Leuven, Departement
10 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
11 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
12 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
13 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
14 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
15 * B.P. 105 - 78153 Le Chesnay, France
18 #include "isl_map_private.h"
20 #include <isl/options.h>
22 #include <isl_mat_private.h>
23 #include <isl_local_space_private.h>
24 #include <isl_vec_private.h>
26 #define STATUS_ERROR -1
27 #define STATUS_REDUNDANT 1
28 #define STATUS_VALID 2
29 #define STATUS_SEPARATE 3
31 #define STATUS_ADJ_EQ 5
32 #define STATUS_ADJ_INEQ 6
34 static int status_in(isl_int
*ineq
, struct isl_tab
*tab
)
36 enum isl_ineq_type type
= isl_tab_ineq_type(tab
, ineq
);
39 case isl_ineq_error
: return STATUS_ERROR
;
40 case isl_ineq_redundant
: return STATUS_VALID
;
41 case isl_ineq_separate
: return STATUS_SEPARATE
;
42 case isl_ineq_cut
: return STATUS_CUT
;
43 case isl_ineq_adj_eq
: return STATUS_ADJ_EQ
;
44 case isl_ineq_adj_ineq
: return STATUS_ADJ_INEQ
;
48 /* Compute the position of the equalities of basic map "bmap_i"
49 * with respect to the basic map represented by "tab_j".
50 * The resulting array has twice as many entries as the number
51 * of equalities corresponding to the two inequalties to which
52 * each equality corresponds.
54 static int *eq_status_in(__isl_keep isl_basic_map
*bmap_i
,
55 struct isl_tab
*tab_j
)
58 int *eq
= isl_calloc_array(bmap_i
->ctx
, int, 2 * bmap_i
->n_eq
);
64 dim
= isl_basic_map_total_dim(bmap_i
);
65 for (k
= 0; k
< bmap_i
->n_eq
; ++k
) {
66 for (l
= 0; l
< 2; ++l
) {
67 isl_seq_neg(bmap_i
->eq
[k
], bmap_i
->eq
[k
], 1+dim
);
68 eq
[2 * k
+ l
] = status_in(bmap_i
->eq
[k
], tab_j
);
69 if (eq
[2 * k
+ l
] == STATUS_ERROR
)
72 if (eq
[2 * k
] == STATUS_SEPARATE
||
73 eq
[2 * k
+ 1] == STATUS_SEPARATE
)
83 /* Compute the position of the inequalities of basic map "bmap_i"
84 * (also represented by "tab_i", if not NULL) with respect to the basic map
85 * represented by "tab_j".
87 static int *ineq_status_in(__isl_keep isl_basic_map
*bmap_i
,
88 struct isl_tab
*tab_i
, struct isl_tab
*tab_j
)
91 unsigned n_eq
= bmap_i
->n_eq
;
92 int *ineq
= isl_calloc_array(bmap_i
->ctx
, int, bmap_i
->n_ineq
);
97 for (k
= 0; k
< bmap_i
->n_ineq
; ++k
) {
98 if (tab_i
&& isl_tab_is_redundant(tab_i
, n_eq
+ k
)) {
99 ineq
[k
] = STATUS_REDUNDANT
;
102 ineq
[k
] = status_in(bmap_i
->ineq
[k
], tab_j
);
103 if (ineq
[k
] == STATUS_ERROR
)
105 if (ineq
[k
] == STATUS_SEPARATE
)
115 static int any(int *con
, unsigned len
, int status
)
119 for (i
= 0; i
< len
; ++i
)
120 if (con
[i
] == status
)
125 static int count(int *con
, unsigned len
, int status
)
130 for (i
= 0; i
< len
; ++i
)
131 if (con
[i
] == status
)
136 static int all(int *con
, unsigned len
, int status
)
140 for (i
= 0; i
< len
; ++i
) {
141 if (con
[i
] == STATUS_REDUNDANT
)
143 if (con
[i
] != status
)
149 /* Internal information associated to a basic map in a map
150 * that is to be coalesced by isl_map_coalesce.
152 * "bmap" is the basic map itself (or NULL if "removed" is set)
153 * "tab" is the corresponding tableau (or NULL if "removed" is set)
154 * "removed" is set if this basic map has been removed from the map
156 struct isl_coalesce_info
{
162 /* Free all the allocated memory in an array
163 * of "n" isl_coalesce_info elements.
165 static void clear_coalesce_info(int n
, struct isl_coalesce_info
*info
)
172 for (i
= 0; i
< n
; ++i
) {
173 isl_basic_map_free(info
[i
].bmap
);
174 isl_tab_free(info
[i
].tab
);
180 /* Drop the basic map represented by "info".
181 * That is, clear the memory associated to the entry and
182 * mark it as having been removed.
184 static void drop(struct isl_coalesce_info
*info
)
186 info
->bmap
= isl_basic_map_free(info
->bmap
);
187 isl_tab_free(info
->tab
);
192 /* Exchange the information in "info1" with that in "info2".
194 static void exchange(struct isl_coalesce_info
*info1
,
195 struct isl_coalesce_info
*info2
)
197 struct isl_coalesce_info info
;
204 /* This type represents the kind of change that has been performed
205 * while trying to coalesce two basic maps.
207 * isl_change_none: nothing was changed
208 * isl_change_drop_first: the first basic map was removed
209 * isl_change_drop_second: the second basic map was removed
210 * isl_change_fuse: the two basic maps were replaced by a new basic map.
213 isl_change_error
= -1,
215 isl_change_drop_first
,
216 isl_change_drop_second
,
220 /* Replace the pair of basic maps i and j by the basic map bounded
221 * by the valid constraints in both basic maps and the constraints
222 * in extra (if not NULL).
223 * Place the fused basic map in the position that is the smallest of i and j.
225 * If "detect_equalities" is set, then look for equalities encoded
226 * as pairs of inequalities.
228 static enum isl_change
fuse(int i
, int j
, struct isl_coalesce_info
*info
,
229 int *eq_i
, int *ineq_i
, int *eq_j
, int *ineq_j
,
230 __isl_keep isl_mat
*extra
, int detect_equalities
)
233 struct isl_basic_map
*fused
= NULL
;
234 struct isl_tab
*fused_tab
= NULL
;
235 unsigned total
= isl_basic_map_total_dim(info
[i
].bmap
);
236 unsigned extra_rows
= extra
? extra
->n_row
: 0;
239 return fuse(j
, i
, info
, eq_j
, ineq_j
, eq_i
, ineq_i
, extra
,
242 fused
= isl_basic_map_alloc_space(isl_space_copy(info
[i
].bmap
->dim
),
244 info
[i
].bmap
->n_eq
+ info
[j
].bmap
->n_eq
,
245 info
[i
].bmap
->n_ineq
+ info
[j
].bmap
->n_ineq
+ extra_rows
);
249 for (k
= 0; k
< info
[i
].bmap
->n_eq
; ++k
) {
250 if (eq_i
&& (eq_i
[2 * k
] != STATUS_VALID
||
251 eq_i
[2 * k
+ 1] != STATUS_VALID
))
253 l
= isl_basic_map_alloc_equality(fused
);
256 isl_seq_cpy(fused
->eq
[l
], info
[i
].bmap
->eq
[k
], 1 + total
);
259 for (k
= 0; k
< info
[j
].bmap
->n_eq
; ++k
) {
260 if (eq_j
&& (eq_j
[2 * k
] != STATUS_VALID
||
261 eq_j
[2 * k
+ 1] != STATUS_VALID
))
263 l
= isl_basic_map_alloc_equality(fused
);
266 isl_seq_cpy(fused
->eq
[l
], info
[j
].bmap
->eq
[k
], 1 + total
);
269 for (k
= 0; k
< info
[i
].bmap
->n_ineq
; ++k
) {
270 if (ineq_i
[k
] != STATUS_VALID
)
272 l
= isl_basic_map_alloc_inequality(fused
);
275 isl_seq_cpy(fused
->ineq
[l
], info
[i
].bmap
->ineq
[k
], 1 + total
);
278 for (k
= 0; k
< info
[j
].bmap
->n_ineq
; ++k
) {
279 if (ineq_j
[k
] != STATUS_VALID
)
281 l
= isl_basic_map_alloc_inequality(fused
);
284 isl_seq_cpy(fused
->ineq
[l
], info
[j
].bmap
->ineq
[k
], 1 + total
);
287 for (k
= 0; k
< info
[i
].bmap
->n_div
; ++k
) {
288 int l
= isl_basic_map_alloc_div(fused
);
291 isl_seq_cpy(fused
->div
[l
], info
[i
].bmap
->div
[k
], 1 + 1 + total
);
294 for (k
= 0; k
< extra_rows
; ++k
) {
295 l
= isl_basic_map_alloc_inequality(fused
);
298 isl_seq_cpy(fused
->ineq
[l
], extra
->row
[k
], 1 + total
);
301 if (detect_equalities
)
302 fused
= isl_basic_map_detect_inequality_pairs(fused
, NULL
);
303 fused
= isl_basic_map_gauss(fused
, NULL
);
304 ISL_F_SET(fused
, ISL_BASIC_MAP_FINAL
);
305 if (ISL_F_ISSET(info
[i
].bmap
, ISL_BASIC_MAP_RATIONAL
) &&
306 ISL_F_ISSET(info
[j
].bmap
, ISL_BASIC_MAP_RATIONAL
))
307 ISL_F_SET(fused
, ISL_BASIC_MAP_RATIONAL
);
309 fused_tab
= isl_tab_from_basic_map(fused
, 0);
310 if (isl_tab_detect_redundant(fused_tab
) < 0)
313 isl_basic_map_free(info
[i
].bmap
);
314 info
[i
].bmap
= fused
;
315 isl_tab_free(info
[i
].tab
);
316 info
[i
].tab
= fused_tab
;
319 return isl_change_fuse
;
321 isl_tab_free(fused_tab
);
322 isl_basic_map_free(fused
);
323 return isl_change_error
;
326 /* Given a pair of basic maps i and j such that all constraints are either
327 * "valid" or "cut", check if the facets corresponding to the "cut"
328 * constraints of i lie entirely within basic map j.
329 * If so, replace the pair by the basic map consisting of the valid
330 * constraints in both basic maps.
331 * Checking whether the facet lies entirely within basic map j
332 * is performed by checking whether the constraints of basic map j
333 * are valid for the facet. These tests are performed on a rational
334 * tableau to avoid the theoretical possibility that a constraint
335 * that was considered to be a cut constraint for the entire basic map i
336 * happens to be considered to be a valid constraint for the facet,
337 * even though it cuts off the same rational points.
339 * To see that we are not introducing any extra points, call the
340 * two basic maps A and B and the resulting map U and let x
341 * be an element of U \setminus ( A \cup B ).
342 * A line connecting x with an element of A \cup B meets a facet F
343 * of either A or B. Assume it is a facet of B and let c_1 be
344 * the corresponding facet constraint. We have c_1(x) < 0 and
345 * so c_1 is a cut constraint. This implies that there is some
346 * (possibly rational) point x' satisfying the constraints of A
347 * and the opposite of c_1 as otherwise c_1 would have been marked
348 * valid for A. The line connecting x and x' meets a facet of A
349 * in a (possibly rational) point that also violates c_1, but this
350 * is impossible since all cut constraints of B are valid for all
352 * In case F is a facet of A rather than B, then we can apply the
353 * above reasoning to find a facet of B separating x from A \cup B first.
355 static enum isl_change
check_facets(int i
, int j
,
356 struct isl_coalesce_info
*info
, int *ineq_i
, int *ineq_j
)
359 struct isl_tab_undo
*snap
, *snap2
;
360 unsigned n_eq
= info
[i
].bmap
->n_eq
;
362 snap
= isl_tab_snap(info
[i
].tab
);
363 if (isl_tab_mark_rational(info
[i
].tab
) < 0)
364 return isl_change_error
;
365 snap2
= isl_tab_snap(info
[i
].tab
);
367 for (k
= 0; k
< info
[i
].bmap
->n_ineq
; ++k
) {
368 if (ineq_i
[k
] != STATUS_CUT
)
370 if (isl_tab_select_facet(info
[i
].tab
, n_eq
+ k
) < 0)
371 return isl_change_error
;
372 for (l
= 0; l
< info
[j
].bmap
->n_ineq
; ++l
) {
374 if (ineq_j
[l
] != STATUS_CUT
)
376 stat
= status_in(info
[j
].bmap
->ineq
[l
], info
[i
].tab
);
377 if (stat
!= STATUS_VALID
)
380 if (isl_tab_rollback(info
[i
].tab
, snap2
) < 0)
381 return isl_change_error
;
382 if (l
< info
[j
].bmap
->n_ineq
)
386 if (k
< info
[i
].bmap
->n_ineq
) {
387 if (isl_tab_rollback(info
[i
].tab
, snap
) < 0)
388 return isl_change_error
;
389 return isl_change_none
;
391 return fuse(i
, j
, info
, NULL
, ineq_i
, NULL
, ineq_j
, NULL
, 0);
394 /* Check if info->bmap contains the basic map represented
395 * by the tableau "tab".
397 static int contains(struct isl_coalesce_info
*info
, int *ineq_i
,
402 isl_basic_map
*bmap
= info
->bmap
;
404 dim
= isl_basic_map_total_dim(bmap
);
405 for (k
= 0; k
< bmap
->n_eq
; ++k
) {
406 for (l
= 0; l
< 2; ++l
) {
408 isl_seq_neg(bmap
->eq
[k
], bmap
->eq
[k
], 1+dim
);
409 stat
= status_in(bmap
->eq
[k
], tab
);
410 if (stat
!= STATUS_VALID
)
415 for (k
= 0; k
< bmap
->n_ineq
; ++k
) {
417 if (ineq_i
[k
] == STATUS_REDUNDANT
)
419 stat
= status_in(bmap
->ineq
[k
], tab
);
420 if (stat
!= STATUS_VALID
)
426 /* Basic map "i" has an inequality (say "k") that is adjacent
427 * to some inequality of basic map "j". All the other inequalities
429 * Check if basic map "j" forms an extension of basic map "i".
431 * Note that this function is only called if some of the equalities or
432 * inequalities of basic map "j" do cut basic map "i". The function is
433 * correct even if there are no such cut constraints, but in that case
434 * the additional checks performed by this function are overkill.
436 * In particular, we replace constraint k, say f >= 0, by constraint
437 * f <= -1, add the inequalities of "j" that are valid for "i"
438 * and check if the result is a subset of basic map "j".
439 * If so, then we know that this result is exactly equal to basic map "j"
440 * since all its constraints are valid for basic map "j".
441 * By combining the valid constraints of "i" (all equalities and all
442 * inequalities except "k") and the valid constraints of "j" we therefore
443 * obtain a basic map that is equal to their union.
444 * In this case, there is no need to perform a rollback of the tableau
445 * since it is going to be destroyed in fuse().
451 * |_______| _ |_________\
463 static enum isl_change
is_adj_ineq_extension(int i
, int j
,
464 struct isl_coalesce_info
*info
, int *eq_i
, int *ineq_i
,
465 int *eq_j
, int *ineq_j
)
468 struct isl_tab_undo
*snap
;
469 unsigned n_eq
= info
[i
].bmap
->n_eq
;
470 unsigned total
= isl_basic_map_total_dim(info
[i
].bmap
);
473 if (isl_tab_extend_cons(info
[i
].tab
, 1 + info
[j
].bmap
->n_ineq
) < 0)
474 return isl_change_error
;
476 for (k
= 0; k
< info
[i
].bmap
->n_ineq
; ++k
)
477 if (ineq_i
[k
] == STATUS_ADJ_INEQ
)
479 if (k
>= info
[i
].bmap
->n_ineq
)
480 isl_die(isl_basic_map_get_ctx(info
[i
].bmap
), isl_error_internal
,
481 "ineq_i should have exactly one STATUS_ADJ_INEQ",
482 return isl_change_error
);
484 snap
= isl_tab_snap(info
[i
].tab
);
486 if (isl_tab_unrestrict(info
[i
].tab
, n_eq
+ k
) < 0)
487 return isl_change_error
;
489 isl_seq_neg(info
[i
].bmap
->ineq
[k
], info
[i
].bmap
->ineq
[k
], 1 + total
);
490 isl_int_sub_ui(info
[i
].bmap
->ineq
[k
][0], info
[i
].bmap
->ineq
[k
][0], 1);
491 r
= isl_tab_add_ineq(info
[i
].tab
, info
[i
].bmap
->ineq
[k
]);
492 isl_seq_neg(info
[i
].bmap
->ineq
[k
], info
[i
].bmap
->ineq
[k
], 1 + total
);
493 isl_int_sub_ui(info
[i
].bmap
->ineq
[k
][0], info
[i
].bmap
->ineq
[k
][0], 1);
495 return isl_change_error
;
497 for (k
= 0; k
< info
[j
].bmap
->n_ineq
; ++k
) {
498 if (ineq_j
[k
] != STATUS_VALID
)
500 if (isl_tab_add_ineq(info
[i
].tab
, info
[j
].bmap
->ineq
[k
]) < 0)
501 return isl_change_error
;
504 if (contains(&info
[j
], ineq_j
, info
[i
].tab
))
505 return fuse(i
, j
, info
, eq_i
, ineq_i
, eq_j
, ineq_j
, NULL
, 0);
507 if (isl_tab_rollback(info
[i
].tab
, snap
) < 0)
508 return isl_change_error
;
510 return isl_change_none
;
514 /* Both basic maps have at least one inequality with and adjacent
515 * (but opposite) inequality in the other basic map.
516 * Check that there are no cut constraints and that there is only
517 * a single pair of adjacent inequalities.
518 * If so, we can replace the pair by a single basic map described
519 * by all but the pair of adjacent inequalities.
520 * Any additional points introduced lie strictly between the two
521 * adjacent hyperplanes and can therefore be integral.
530 * The test for a single pair of adjancent inequalities is important
531 * for avoiding the combination of two basic maps like the following
541 * If there are some cut constraints on one side, then we may
542 * still be able to fuse the two basic maps, but we need to perform
543 * some additional checks in is_adj_ineq_extension.
545 static enum isl_change
check_adj_ineq(int i
, int j
,
546 struct isl_coalesce_info
*info
, int *eq_i
, int *ineq_i
,
547 int *eq_j
, int *ineq_j
)
549 int count_i
, count_j
;
552 count_i
= count(ineq_i
, info
[i
].bmap
->n_ineq
, STATUS_ADJ_INEQ
);
553 count_j
= count(ineq_j
, info
[j
].bmap
->n_ineq
, STATUS_ADJ_INEQ
);
555 if (count_i
!= 1 && count_j
!= 1)
556 return isl_change_none
;
558 cut_i
= any(eq_i
, 2 * info
[i
].bmap
->n_eq
, STATUS_CUT
) ||
559 any(ineq_i
, info
[i
].bmap
->n_ineq
, STATUS_CUT
);
560 cut_j
= any(eq_j
, 2 * info
[j
].bmap
->n_eq
, STATUS_CUT
) ||
561 any(ineq_j
, info
[j
].bmap
->n_ineq
, STATUS_CUT
);
563 if (!cut_i
&& !cut_j
&& count_i
== 1 && count_j
== 1)
564 return fuse(i
, j
, info
, NULL
, ineq_i
, NULL
, ineq_j
, NULL
, 0);
566 if (count_i
== 1 && !cut_i
)
567 return is_adj_ineq_extension(i
, j
, info
,
568 eq_i
, ineq_i
, eq_j
, ineq_j
);
570 if (count_j
== 1 && !cut_j
)
571 return is_adj_ineq_extension(j
, i
, info
,
572 eq_j
, ineq_j
, eq_i
, ineq_i
);
574 return isl_change_none
;
577 /* Basic map "i" has an inequality "k" that is adjacent to some equality
578 * of basic map "j". All the other inequalities are valid for "j".
579 * Check if basic map "j" forms an extension of basic map "i".
581 * In particular, we relax constraint "k", compute the corresponding
582 * facet and check whether it is included in the other basic map.
583 * If so, we know that relaxing the constraint extends the basic
584 * map with exactly the other basic map (we already know that this
585 * other basic map is included in the extension, because there
586 * were no "cut" inequalities in "i") and we can replace the
587 * two basic maps by this extension.
588 * Place this extension in the position that is the smallest of i and j.
596 static enum isl_change
is_adj_eq_extension(int i
, int j
, int k
,
597 struct isl_coalesce_info
*info
, int *eq_i
, int *ineq_i
,
598 int *eq_j
, int *ineq_j
)
600 int change
= isl_change_none
;
602 struct isl_tab_undo
*snap
, *snap2
;
603 unsigned n_eq
= info
[i
].bmap
->n_eq
;
605 if (isl_tab_is_equality(info
[i
].tab
, n_eq
+ k
))
606 return isl_change_none
;
608 snap
= isl_tab_snap(info
[i
].tab
);
609 if (isl_tab_relax(info
[i
].tab
, n_eq
+ k
) < 0)
610 return isl_change_error
;
611 snap2
= isl_tab_snap(info
[i
].tab
);
612 if (isl_tab_select_facet(info
[i
].tab
, n_eq
+ k
) < 0)
613 return isl_change_error
;
614 super
= contains(&info
[j
], ineq_j
, info
[i
].tab
);
616 if (isl_tab_rollback(info
[i
].tab
, snap2
) < 0)
617 return isl_change_error
;
618 info
[i
].bmap
= isl_basic_map_cow(info
[i
].bmap
);
620 return isl_change_error
;
621 isl_int_add_ui(info
[i
].bmap
->ineq
[k
][0],
622 info
[i
].bmap
->ineq
[k
][0], 1);
623 ISL_F_SET(info
[i
].bmap
, ISL_BASIC_MAP_FINAL
);
626 exchange(&info
[i
], &info
[j
]);
627 change
= isl_change_fuse
;
629 if (isl_tab_rollback(info
[i
].tab
, snap
) < 0)
630 return isl_change_error
;
635 /* Data structure that keeps track of the wrapping constraints
636 * and of information to bound the coefficients of those constraints.
638 * bound is set if we want to apply a bound on the coefficients
639 * mat contains the wrapping constraints
640 * max is the bound on the coefficients (if bound is set)
648 /* Update wraps->max to be greater than or equal to the coefficients
649 * in the equalities and inequalities of info->bmap that can be removed
650 * if we end up applying wrapping.
652 static void wraps_update_max(struct isl_wraps
*wraps
,
653 struct isl_coalesce_info
*info
, int *eq
, int *ineq
)
657 unsigned total
= isl_basic_map_total_dim(info
->bmap
);
661 for (k
= 0; k
< info
->bmap
->n_eq
; ++k
) {
662 if (eq
[2 * k
] == STATUS_VALID
&&
663 eq
[2 * k
+ 1] == STATUS_VALID
)
665 isl_seq_abs_max(info
->bmap
->eq
[k
] + 1, total
, &max_k
);
666 if (isl_int_abs_gt(max_k
, wraps
->max
))
667 isl_int_set(wraps
->max
, max_k
);
670 for (k
= 0; k
< info
->bmap
->n_ineq
; ++k
) {
671 if (ineq
[k
] == STATUS_VALID
|| ineq
[k
] == STATUS_REDUNDANT
)
673 isl_seq_abs_max(info
->bmap
->ineq
[k
] + 1, total
, &max_k
);
674 if (isl_int_abs_gt(max_k
, wraps
->max
))
675 isl_int_set(wraps
->max
, max_k
);
678 isl_int_clear(max_k
);
681 /* Initialize the isl_wraps data structure.
682 * If we want to bound the coefficients of the wrapping constraints,
683 * we set wraps->max to the largest coefficient
684 * in the equalities and inequalities that can be removed if we end up
687 static void wraps_init(struct isl_wraps
*wraps
, __isl_take isl_mat
*mat
,
688 struct isl_coalesce_info
*info
, int i
, int j
,
689 int *eq_i
, int *ineq_i
, int *eq_j
, int *ineq_j
)
697 ctx
= isl_mat_get_ctx(mat
);
698 wraps
->bound
= isl_options_get_coalesce_bounded_wrapping(ctx
);
701 isl_int_init(wraps
->max
);
702 isl_int_set_si(wraps
->max
, 0);
703 wraps_update_max(wraps
, &info
[i
], eq_i
, ineq_i
);
704 wraps_update_max(wraps
, &info
[j
], eq_j
, ineq_j
);
707 /* Free the contents of the isl_wraps data structure.
709 static void wraps_free(struct isl_wraps
*wraps
)
711 isl_mat_free(wraps
->mat
);
713 isl_int_clear(wraps
->max
);
716 /* Is the wrapping constraint in row "row" allowed?
718 * If wraps->bound is set, we check that none of the coefficients
719 * is greater than wraps->max.
721 static int allow_wrap(struct isl_wraps
*wraps
, int row
)
728 for (i
= 1; i
< wraps
->mat
->n_col
; ++i
)
729 if (isl_int_abs_gt(wraps
->mat
->row
[row
][i
], wraps
->max
))
735 /* For each non-redundant constraint in info->bmap (as determined by info->tab),
736 * wrap the constraint around "bound" such that it includes the whole
737 * set "set" and append the resulting constraint to "wraps".
738 * "wraps" is assumed to have been pre-allocated to the appropriate size.
739 * wraps->n_row is the number of actual wrapped constraints that have
741 * If any of the wrapping problems results in a constraint that is
742 * identical to "bound", then this means that "set" is unbounded in such
743 * way that no wrapping is possible. If this happens then wraps->n_row
745 * Similarly, if we want to bound the coefficients of the wrapping
746 * constraints and a newly added wrapping constraint does not
747 * satisfy the bound, then wraps->n_row is also reset to zero.
749 static int add_wraps(struct isl_wraps
*wraps
, struct isl_coalesce_info
*info
,
750 isl_int
*bound
, __isl_keep isl_set
*set
)
754 isl_basic_map
*bmap
= info
->bmap
;
755 unsigned total
= isl_basic_map_total_dim(bmap
);
757 w
= wraps
->mat
->n_row
;
759 for (l
= 0; l
< bmap
->n_ineq
; ++l
) {
760 if (isl_seq_is_neg(bound
, bmap
->ineq
[l
], 1 + total
))
762 if (isl_seq_eq(bound
, bmap
->ineq
[l
], 1 + total
))
764 if (isl_tab_is_redundant(info
->tab
, bmap
->n_eq
+ l
))
767 isl_seq_cpy(wraps
->mat
->row
[w
], bound
, 1 + total
);
768 if (!isl_set_wrap_facet(set
, wraps
->mat
->row
[w
], bmap
->ineq
[l
]))
770 if (isl_seq_eq(wraps
->mat
->row
[w
], bound
, 1 + total
))
772 if (!allow_wrap(wraps
, w
))
776 for (l
= 0; l
< bmap
->n_eq
; ++l
) {
777 if (isl_seq_is_neg(bound
, bmap
->eq
[l
], 1 + total
))
779 if (isl_seq_eq(bound
, bmap
->eq
[l
], 1 + total
))
782 isl_seq_cpy(wraps
->mat
->row
[w
], bound
, 1 + total
);
783 isl_seq_neg(wraps
->mat
->row
[w
+ 1], bmap
->eq
[l
], 1 + total
);
784 if (!isl_set_wrap_facet(set
, wraps
->mat
->row
[w
],
785 wraps
->mat
->row
[w
+ 1]))
787 if (isl_seq_eq(wraps
->mat
->row
[w
], bound
, 1 + total
))
789 if (!allow_wrap(wraps
, w
))
793 isl_seq_cpy(wraps
->mat
->row
[w
], bound
, 1 + total
);
794 if (!isl_set_wrap_facet(set
, wraps
->mat
->row
[w
], bmap
->eq
[l
]))
796 if (isl_seq_eq(wraps
->mat
->row
[w
], bound
, 1 + total
))
798 if (!allow_wrap(wraps
, w
))
803 wraps
->mat
->n_row
= w
;
806 wraps
->mat
->n_row
= 0;
810 /* Check if the constraints in "wraps" from "first" until the last
811 * are all valid for the basic set represented by "tab".
812 * If not, wraps->n_row is set to zero.
814 static int check_wraps(__isl_keep isl_mat
*wraps
, int first
,
819 for (i
= first
; i
< wraps
->n_row
; ++i
) {
820 enum isl_ineq_type type
;
821 type
= isl_tab_ineq_type(tab
, wraps
->row
[i
]);
822 if (type
== isl_ineq_error
)
824 if (type
== isl_ineq_redundant
)
833 /* Return a set that corresponds to the non-redundant constraints
834 * (as recorded in tab) of bmap.
836 * It's important to remove the redundant constraints as some
837 * of the other constraints may have been modified after the
838 * constraints were marked redundant.
839 * In particular, a constraint may have been relaxed.
840 * Redundant constraints are ignored when a constraint is relaxed
841 * and should therefore continue to be ignored ever after.
842 * Otherwise, the relaxation might be thwarted by some of
845 * Update the underlying set to ensure that the dimension doesn't change.
846 * Otherwise the integer divisions could get dropped if the tab
847 * turns out to be empty.
849 static __isl_give isl_set
*set_from_updated_bmap(__isl_keep isl_basic_map
*bmap
,
854 bmap
= isl_basic_map_copy(bmap
);
855 bset
= isl_basic_map_underlying_set(bmap
);
856 bset
= isl_basic_set_cow(bset
);
857 bset
= isl_basic_set_update_from_tab(bset
, tab
);
858 return isl_set_from_basic_set(bset
);
861 /* Given a basic set i with a constraint k that is adjacent to
862 * basic set j, check if we can wrap
863 * both the facet corresponding to k and basic map j
864 * around their ridges to include the other set.
865 * If so, replace the pair of basic sets by their union.
867 * All constraints of i (except k) are assumed to be valid for j.
868 * This means that there is no real need to wrap the ridges of
869 * the faces of basic map i around basic map j but since we do,
870 * we have to check that the resulting wrapping constraints are valid for i.
879 static enum isl_change
can_wrap_in_facet(int i
, int j
, int k
,
880 struct isl_coalesce_info
*info
, int *eq_i
, int *ineq_i
,
881 int *eq_j
, int *ineq_j
)
883 enum isl_change change
= isl_change_none
;
884 struct isl_wraps wraps
;
887 struct isl_set
*set_i
= NULL
;
888 struct isl_set
*set_j
= NULL
;
889 struct isl_vec
*bound
= NULL
;
890 unsigned total
= isl_basic_map_total_dim(info
[i
].bmap
);
891 struct isl_tab_undo
*snap
;
894 set_i
= set_from_updated_bmap(info
[i
].bmap
, info
[i
].tab
);
895 set_j
= set_from_updated_bmap(info
[j
].bmap
, info
[j
].tab
);
896 ctx
= isl_basic_map_get_ctx(info
[i
].bmap
);
897 mat
= isl_mat_alloc(ctx
, 2 * (info
[i
].bmap
->n_eq
+ info
[j
].bmap
->n_eq
) +
898 info
[i
].bmap
->n_ineq
+ info
[j
].bmap
->n_ineq
,
900 wraps_init(&wraps
, mat
, info
, i
, j
, eq_i
, ineq_i
, eq_j
, ineq_j
);
901 bound
= isl_vec_alloc(ctx
, 1 + total
);
902 if (!set_i
|| !set_j
|| !wraps
.mat
|| !bound
)
905 isl_seq_cpy(bound
->el
, info
[i
].bmap
->ineq
[k
], 1 + total
);
906 isl_int_add_ui(bound
->el
[0], bound
->el
[0], 1);
908 isl_seq_cpy(wraps
.mat
->row
[0], bound
->el
, 1 + total
);
909 wraps
.mat
->n_row
= 1;
911 if (add_wraps(&wraps
, &info
[j
], bound
->el
, set_i
) < 0)
913 if (!wraps
.mat
->n_row
)
916 snap
= isl_tab_snap(info
[i
].tab
);
918 if (isl_tab_select_facet(info
[i
].tab
, info
[i
].bmap
->n_eq
+ k
) < 0)
920 if (isl_tab_detect_redundant(info
[i
].tab
) < 0)
923 isl_seq_neg(bound
->el
, info
[i
].bmap
->ineq
[k
], 1 + total
);
925 n
= wraps
.mat
->n_row
;
926 if (add_wraps(&wraps
, &info
[i
], bound
->el
, set_j
) < 0)
929 if (isl_tab_rollback(info
[i
].tab
, snap
) < 0)
931 if (check_wraps(wraps
.mat
, n
, info
[i
].tab
) < 0)
933 if (!wraps
.mat
->n_row
)
936 change
= fuse(i
, j
, info
, eq_i
, ineq_i
, eq_j
, ineq_j
, wraps
.mat
, 0);
952 return isl_change_error
;
955 /* Given a pair of basic maps i and j such that j sticks out
956 * of i at n cut constraints, each time by at most one,
957 * try to compute wrapping constraints and replace the two
958 * basic maps by a single basic map.
959 * The other constraints of i are assumed to be valid for j.
961 * For each cut constraint t(x) >= 0 of i, we add the relaxed version
962 * t(x) + 1 >= 0, along with wrapping constraints for all constraints
963 * of basic map j that bound the part of basic map j that sticks out
964 * of the cut constraint.
965 * In particular, we first intersect basic map j with t(x) + 1 = 0.
966 * If the result is empty, then t(x) >= 0 was actually a valid constraint
967 * (with respect to the integer points), so we add t(x) >= 0 instead.
968 * Otherwise, we wrap the constraints of basic map j that are not
969 * redundant in this intersection over the union of the two basic maps.
971 * If any wrapping fails, i.e., if we cannot wrap to touch
972 * the union, then we give up.
973 * Otherwise, the pair of basic maps is replaced by their union.
975 static enum isl_change
wrap_in_facets(int i
, int j
, int *cuts
, int n
,
976 struct isl_coalesce_info
*info
,
977 int *eq_i
, int *ineq_i
, int *eq_j
, int *ineq_j
)
979 enum isl_change change
= isl_change_none
;
980 struct isl_wraps wraps
;
984 unsigned total
= isl_basic_map_total_dim(info
[i
].bmap
);
987 struct isl_tab_undo
*snap
;
989 if (isl_tab_extend_cons(info
[j
].tab
, 1) < 0)
992 max_wrap
= 1 + 2 * info
[j
].bmap
->n_eq
+ info
[j
].bmap
->n_ineq
;
995 set
= isl_set_union(set_from_updated_bmap(info
[i
].bmap
, info
[i
].tab
),
996 set_from_updated_bmap(info
[j
].bmap
, info
[j
].tab
));
997 ctx
= isl_basic_map_get_ctx(info
[i
].bmap
);
998 mat
= isl_mat_alloc(ctx
, max_wrap
, 1 + total
);
999 wraps_init(&wraps
, mat
, info
, i
, j
, eq_i
, ineq_i
, eq_j
, ineq_j
);
1000 if (!set
|| !wraps
.mat
)
1003 snap
= isl_tab_snap(info
[j
].tab
);
1005 wraps
.mat
->n_row
= 0;
1007 for (k
= 0; k
< n
; ++k
) {
1008 w
= wraps
.mat
->n_row
++;
1009 isl_seq_cpy(wraps
.mat
->row
[w
],
1010 info
[i
].bmap
->ineq
[cuts
[k
]], 1 + total
);
1011 isl_int_add_ui(wraps
.mat
->row
[w
][0], wraps
.mat
->row
[w
][0], 1);
1012 if (isl_tab_add_eq(info
[j
].tab
, wraps
.mat
->row
[w
]) < 0)
1014 if (isl_tab_detect_redundant(info
[j
].tab
) < 0)
1017 if (info
[j
].tab
->empty
)
1018 isl_int_sub_ui(wraps
.mat
->row
[w
][0],
1019 wraps
.mat
->row
[w
][0], 1);
1020 else if (add_wraps(&wraps
, &info
[j
],
1021 wraps
.mat
->row
[w
], set
) < 0)
1024 if (isl_tab_rollback(info
[j
].tab
, snap
) < 0)
1027 if (!wraps
.mat
->n_row
)
1032 change
= fuse(i
, j
, info
,
1033 eq_i
, ineq_i
, eq_j
, ineq_j
, wraps
.mat
, 0);
1042 return isl_change_error
;
1045 /* Given two basic sets i and j such that i has no cut equalities,
1046 * check if relaxing all the cut inequalities of i by one turns
1047 * them into valid constraint for j and check if we can wrap in
1048 * the bits that are sticking out.
1049 * If so, replace the pair by their union.
1051 * We first check if all relaxed cut inequalities of i are valid for j
1052 * and then try to wrap in the intersections of the relaxed cut inequalities
1055 * During this wrapping, we consider the points of j that lie at a distance
1056 * of exactly 1 from i. In particular, we ignore the points that lie in
1057 * between this lower-dimensional space and the basic map i.
1058 * We can therefore only apply this to integer maps.
1084 * Wrapping can fail if the result of wrapping one of the facets
1085 * around its edges does not produce any new facet constraint.
1086 * In particular, this happens when we try to wrap in unbounded sets.
1088 * _______________________________________________________________________
1092 * |_| |_________________________________________________________________
1095 * The following is not an acceptable result of coalescing the above two
1096 * sets as it includes extra integer points.
1097 * _______________________________________________________________________
1102 * \______________________________________________________________________
1104 static enum isl_change
can_wrap_in_set(int i
, int j
,
1105 struct isl_coalesce_info
*info
, int *eq_i
, int *ineq_i
,
1106 int *eq_j
, int *ineq_j
)
1108 enum isl_change change
= isl_change_none
;
1114 if (ISL_F_ISSET(info
[i
].bmap
, ISL_BASIC_MAP_RATIONAL
) ||
1115 ISL_F_ISSET(info
[j
].bmap
, ISL_BASIC_MAP_RATIONAL
))
1116 return isl_change_none
;
1118 n
= count(ineq_i
, info
[i
].bmap
->n_ineq
, STATUS_CUT
);
1120 return isl_change_none
;
1122 ctx
= isl_basic_map_get_ctx(info
[i
].bmap
);
1123 cuts
= isl_alloc_array(ctx
, int, n
);
1125 return isl_change_error
;
1127 for (k
= 0, m
= 0; m
< n
; ++k
) {
1128 enum isl_ineq_type type
;
1130 if (ineq_i
[k
] != STATUS_CUT
)
1133 isl_int_add_ui(info
[i
].bmap
->ineq
[k
][0],
1134 info
[i
].bmap
->ineq
[k
][0], 1);
1135 type
= isl_tab_ineq_type(info
[j
].tab
, info
[i
].bmap
->ineq
[k
]);
1136 isl_int_sub_ui(info
[i
].bmap
->ineq
[k
][0],
1137 info
[i
].bmap
->ineq
[k
][0], 1);
1138 if (type
== isl_ineq_error
)
1140 if (type
!= isl_ineq_redundant
)
1147 change
= wrap_in_facets(i
, j
, cuts
, n
, info
,
1148 eq_i
, ineq_i
, eq_j
, ineq_j
);
1155 return isl_change_error
;
1158 /* Check if either i or j has only cut inequalities that can
1159 * be used to wrap in (a facet of) the other basic set.
1160 * if so, replace the pair by their union.
1162 static enum isl_change
check_wrap(int i
, int j
, struct isl_coalesce_info
*info
,
1163 int *eq_i
, int *ineq_i
, int *eq_j
, int *ineq_j
)
1165 enum isl_change change
= isl_change_none
;
1167 if (!any(eq_i
, 2 * info
[i
].bmap
->n_eq
, STATUS_CUT
))
1168 change
= can_wrap_in_set(i
, j
, info
,
1169 eq_i
, ineq_i
, eq_j
, ineq_j
);
1170 if (change
!= isl_change_none
)
1173 if (!any(eq_j
, 2 * info
[j
].bmap
->n_eq
, STATUS_CUT
))
1174 change
= can_wrap_in_set(j
, i
, info
,
1175 eq_j
, ineq_j
, eq_i
, ineq_i
);
1179 /* At least one of the basic maps has an equality that is adjacent
1180 * to inequality. Make sure that only one of the basic maps has
1181 * such an equality and that the other basic map has exactly one
1182 * inequality adjacent to an equality.
1183 * We call the basic map that has the inequality "i" and the basic
1184 * map that has the equality "j".
1185 * If "i" has any "cut" (in)equality, then relaxing the inequality
1186 * by one would not result in a basic map that contains the other
1189 static enum isl_change
check_adj_eq(int i
, int j
,
1190 struct isl_coalesce_info
*info
, int *eq_i
, int *ineq_i
,
1191 int *eq_j
, int *ineq_j
)
1193 enum isl_change change
= isl_change_none
;
1196 if (any(eq_i
, 2 * info
[i
].bmap
->n_eq
, STATUS_ADJ_INEQ
) &&
1197 any(eq_j
, 2 * info
[j
].bmap
->n_eq
, STATUS_ADJ_INEQ
))
1198 /* ADJ EQ TOO MANY */
1199 return isl_change_none
;
1201 if (any(eq_i
, 2 * info
[i
].bmap
->n_eq
, STATUS_ADJ_INEQ
))
1202 return check_adj_eq(j
, i
, info
, eq_j
, ineq_j
, eq_i
, ineq_i
);
1204 /* j has an equality adjacent to an inequality in i */
1206 if (any(eq_i
, 2 * info
[i
].bmap
->n_eq
, STATUS_CUT
))
1207 return isl_change_none
;
1208 if (any(ineq_i
, info
[i
].bmap
->n_ineq
, STATUS_CUT
))
1210 return isl_change_none
;
1211 if (count(ineq_i
, info
[i
].bmap
->n_ineq
, STATUS_ADJ_EQ
) != 1 ||
1212 any(ineq_j
, info
[j
].bmap
->n_ineq
, STATUS_ADJ_EQ
) ||
1213 any(ineq_i
, info
[i
].bmap
->n_ineq
, STATUS_ADJ_INEQ
) ||
1214 any(ineq_j
, info
[j
].bmap
->n_ineq
, STATUS_ADJ_INEQ
))
1215 /* ADJ EQ TOO MANY */
1216 return isl_change_none
;
1218 for (k
= 0; k
< info
[i
].bmap
->n_ineq
; ++k
)
1219 if (ineq_i
[k
] == STATUS_ADJ_EQ
)
1222 change
= is_adj_eq_extension(i
, j
, k
, info
,
1223 eq_i
, ineq_i
, eq_j
, ineq_j
);
1224 if (change
!= isl_change_none
)
1227 if (count(eq_j
, 2 * info
[j
].bmap
->n_eq
, STATUS_ADJ_INEQ
) != 1)
1228 return isl_change_none
;
1230 change
= can_wrap_in_facet(i
, j
, k
, info
, eq_i
, ineq_i
, eq_j
, ineq_j
);
1235 /* The two basic maps lie on adjacent hyperplanes. In particular,
1236 * basic map "i" has an equality that lies parallel to basic map "j".
1237 * Check if we can wrap the facets around the parallel hyperplanes
1238 * to include the other set.
1240 * We perform basically the same operations as can_wrap_in_facet,
1241 * except that we don't need to select a facet of one of the sets.
1247 * If there is more than one equality of "i" adjacent to an equality of "j",
1248 * then the result will satisfy one or more equalities that are a linear
1249 * combination of these equalities. These will be encoded as pairs
1250 * of inequalities in the wrapping constraints and need to be made
1253 static enum isl_change
check_eq_adj_eq(int i
, int j
,
1254 struct isl_coalesce_info
*info
, int *eq_i
, int *ineq_i
,
1255 int *eq_j
, int *ineq_j
)
1258 enum isl_change change
= isl_change_none
;
1259 int detect_equalities
= 0;
1260 struct isl_wraps wraps
;
1263 struct isl_set
*set_i
= NULL
;
1264 struct isl_set
*set_j
= NULL
;
1265 struct isl_vec
*bound
= NULL
;
1266 unsigned total
= isl_basic_map_total_dim(info
[i
].bmap
);
1268 if (count(eq_i
, 2 * info
[i
].bmap
->n_eq
, STATUS_ADJ_EQ
) != 1)
1269 detect_equalities
= 1;
1271 for (k
= 0; k
< 2 * info
[i
].bmap
->n_eq
; ++k
)
1272 if (eq_i
[k
] == STATUS_ADJ_EQ
)
1275 set_i
= set_from_updated_bmap(info
[i
].bmap
, info
[i
].tab
);
1276 set_j
= set_from_updated_bmap(info
[j
].bmap
, info
[j
].tab
);
1277 ctx
= isl_basic_map_get_ctx(info
[i
].bmap
);
1278 mat
= isl_mat_alloc(ctx
, 2 * (info
[i
].bmap
->n_eq
+ info
[j
].bmap
->n_eq
) +
1279 info
[i
].bmap
->n_ineq
+ info
[j
].bmap
->n_ineq
,
1281 wraps_init(&wraps
, mat
, info
, i
, j
, eq_i
, ineq_i
, eq_j
, ineq_j
);
1282 bound
= isl_vec_alloc(ctx
, 1 + total
);
1283 if (!set_i
|| !set_j
|| !wraps
.mat
|| !bound
)
1287 isl_seq_neg(bound
->el
, info
[i
].bmap
->eq
[k
/ 2], 1 + total
);
1289 isl_seq_cpy(bound
->el
, info
[i
].bmap
->eq
[k
/ 2], 1 + total
);
1290 isl_int_add_ui(bound
->el
[0], bound
->el
[0], 1);
1292 isl_seq_cpy(wraps
.mat
->row
[0], bound
->el
, 1 + total
);
1293 wraps
.mat
->n_row
= 1;
1295 if (add_wraps(&wraps
, &info
[j
], bound
->el
, set_i
) < 0)
1297 if (!wraps
.mat
->n_row
)
1300 isl_int_sub_ui(bound
->el
[0], bound
->el
[0], 1);
1301 isl_seq_neg(bound
->el
, bound
->el
, 1 + total
);
1303 isl_seq_cpy(wraps
.mat
->row
[wraps
.mat
->n_row
], bound
->el
, 1 + total
);
1306 if (add_wraps(&wraps
, &info
[i
], bound
->el
, set_j
) < 0)
1308 if (!wraps
.mat
->n_row
)
1311 change
= fuse(i
, j
, info
, eq_i
, ineq_i
, eq_j
, ineq_j
, wraps
.mat
,
1315 error
: change
= isl_change_error
;
1320 isl_set_free(set_i
);
1321 isl_set_free(set_j
);
1322 isl_vec_free(bound
);
1327 /* Check if the union of the given pair of basic maps
1328 * can be represented by a single basic map.
1329 * If so, replace the pair by the single basic map and return
1330 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
1331 * Otherwise, return isl_change_none.
1332 * The two basic maps are assumed to live in the same local space.
1334 * We first check the effect of each constraint of one basic map
1335 * on the other basic map.
1336 * The constraint may be
1337 * redundant the constraint is redundant in its own
1338 * basic map and should be ignore and removed
1340 * valid all (integer) points of the other basic map
1341 * satisfy the constraint
1342 * separate no (integer) point of the other basic map
1343 * satisfies the constraint
1344 * cut some but not all points of the other basic map
1345 * satisfy the constraint
1346 * adj_eq the given constraint is adjacent (on the outside)
1347 * to an equality of the other basic map
1348 * adj_ineq the given constraint is adjacent (on the outside)
1349 * to an inequality of the other basic map
1351 * We consider seven cases in which we can replace the pair by a single
1352 * basic map. We ignore all "redundant" constraints.
1354 * 1. all constraints of one basic map are valid
1355 * => the other basic map is a subset and can be removed
1357 * 2. all constraints of both basic maps are either "valid" or "cut"
1358 * and the facets corresponding to the "cut" constraints
1359 * of one of the basic maps lies entirely inside the other basic map
1360 * => the pair can be replaced by a basic map consisting
1361 * of the valid constraints in both basic maps
1363 * 3. there is a single pair of adjacent inequalities
1364 * (all other constraints are "valid")
1365 * => the pair can be replaced by a basic map consisting
1366 * of the valid constraints in both basic maps
1368 * 4. one basic map has a single adjacent inequality, while the other
1369 * constraints are "valid". The other basic map has some
1370 * "cut" constraints, but replacing the adjacent inequality by
1371 * its opposite and adding the valid constraints of the other
1372 * basic map results in a subset of the other basic map
1373 * => the pair can be replaced by a basic map consisting
1374 * of the valid constraints in both basic maps
1376 * 5. there is a single adjacent pair of an inequality and an equality,
1377 * the other constraints of the basic map containing the inequality are
1378 * "valid". Moreover, if the inequality the basic map is relaxed
1379 * and then turned into an equality, then resulting facet lies
1380 * entirely inside the other basic map
1381 * => the pair can be replaced by the basic map containing
1382 * the inequality, with the inequality relaxed.
1384 * 6. there is a single adjacent pair of an inequality and an equality,
1385 * the other constraints of the basic map containing the inequality are
1386 * "valid". Moreover, the facets corresponding to both
1387 * the inequality and the equality can be wrapped around their
1388 * ridges to include the other basic map
1389 * => the pair can be replaced by a basic map consisting
1390 * of the valid constraints in both basic maps together
1391 * with all wrapping constraints
1393 * 7. one of the basic maps extends beyond the other by at most one.
1394 * Moreover, the facets corresponding to the cut constraints and
1395 * the pieces of the other basic map at offset one from these cut
1396 * constraints can be wrapped around their ridges to include
1397 * the union of the two basic maps
1398 * => the pair can be replaced by a basic map consisting
1399 * of the valid constraints in both basic maps together
1400 * with all wrapping constraints
1402 * 8. the two basic maps live in adjacent hyperplanes. In principle
1403 * such sets can always be combined through wrapping, but we impose
1404 * that there is only one such pair, to avoid overeager coalescing.
1406 * Throughout the computation, we maintain a collection of tableaus
1407 * corresponding to the basic maps. When the basic maps are dropped
1408 * or combined, the tableaus are modified accordingly.
1410 static enum isl_change
coalesce_local_pair(int i
, int j
,
1411 struct isl_coalesce_info
*info
)
1413 enum isl_change change
= isl_change_none
;
1419 eq_i
= eq_status_in(info
[i
].bmap
, info
[j
].tab
);
1420 if (info
[i
].bmap
->n_eq
&& !eq_i
)
1422 if (any(eq_i
, 2 * info
[i
].bmap
->n_eq
, STATUS_ERROR
))
1424 if (any(eq_i
, 2 * info
[i
].bmap
->n_eq
, STATUS_SEPARATE
))
1427 eq_j
= eq_status_in(info
[j
].bmap
, info
[i
].tab
);
1428 if (info
[j
].bmap
->n_eq
&& !eq_j
)
1430 if (any(eq_j
, 2 * info
[j
].bmap
->n_eq
, STATUS_ERROR
))
1432 if (any(eq_j
, 2 * info
[j
].bmap
->n_eq
, STATUS_SEPARATE
))
1435 ineq_i
= ineq_status_in(info
[i
].bmap
, info
[i
].tab
, info
[j
].tab
);
1436 if (info
[i
].bmap
->n_ineq
&& !ineq_i
)
1438 if (any(ineq_i
, info
[i
].bmap
->n_ineq
, STATUS_ERROR
))
1440 if (any(ineq_i
, info
[i
].bmap
->n_ineq
, STATUS_SEPARATE
))
1443 ineq_j
= ineq_status_in(info
[j
].bmap
, info
[j
].tab
, info
[i
].tab
);
1444 if (info
[j
].bmap
->n_ineq
&& !ineq_j
)
1446 if (any(ineq_j
, info
[j
].bmap
->n_ineq
, STATUS_ERROR
))
1448 if (any(ineq_j
, info
[j
].bmap
->n_ineq
, STATUS_SEPARATE
))
1451 if (all(eq_i
, 2 * info
[i
].bmap
->n_eq
, STATUS_VALID
) &&
1452 all(ineq_i
, info
[i
].bmap
->n_ineq
, STATUS_VALID
)) {
1454 change
= isl_change_drop_second
;
1455 } else if (all(eq_j
, 2 * info
[j
].bmap
->n_eq
, STATUS_VALID
) &&
1456 all(ineq_j
, info
[j
].bmap
->n_ineq
, STATUS_VALID
)) {
1458 change
= isl_change_drop_first
;
1459 } else if (any(eq_i
, 2 * info
[i
].bmap
->n_eq
, STATUS_ADJ_EQ
)) {
1460 change
= check_eq_adj_eq(i
, j
, info
,
1461 eq_i
, ineq_i
, eq_j
, ineq_j
);
1462 } else if (any(eq_j
, 2 * info
[j
].bmap
->n_eq
, STATUS_ADJ_EQ
)) {
1463 change
= check_eq_adj_eq(j
, i
, info
,
1464 eq_j
, ineq_j
, eq_i
, ineq_i
);
1465 } else if (any(eq_i
, 2 * info
[i
].bmap
->n_eq
, STATUS_ADJ_INEQ
) ||
1466 any(eq_j
, 2 * info
[j
].bmap
->n_eq
, STATUS_ADJ_INEQ
)) {
1467 change
= check_adj_eq(i
, j
, info
,
1468 eq_i
, ineq_i
, eq_j
, ineq_j
);
1469 } else if (any(ineq_i
, info
[i
].bmap
->n_ineq
, STATUS_ADJ_EQ
) ||
1470 any(ineq_j
, info
[j
].bmap
->n_ineq
, STATUS_ADJ_EQ
)) {
1473 } else if (any(ineq_i
, info
[i
].bmap
->n_ineq
, STATUS_ADJ_INEQ
) ||
1474 any(ineq_j
, info
[j
].bmap
->n_ineq
, STATUS_ADJ_INEQ
)) {
1475 change
= check_adj_ineq(i
, j
, info
,
1476 eq_i
, ineq_i
, eq_j
, ineq_j
);
1478 if (!any(eq_i
, 2 * info
[i
].bmap
->n_eq
, STATUS_CUT
) &&
1479 !any(eq_j
, 2 * info
[j
].bmap
->n_eq
, STATUS_CUT
))
1480 change
= check_facets(i
, j
, info
, ineq_i
, ineq_j
);
1481 if (change
== isl_change_none
)
1482 change
= check_wrap(i
, j
, info
,
1483 eq_i
, ineq_i
, eq_j
, ineq_j
);
1497 return isl_change_error
;
1500 /* Do the two basic maps live in the same local space, i.e.,
1501 * do they have the same (known) divs?
1502 * If either basic map has any unknown divs, then we can only assume
1503 * that they do not live in the same local space.
1505 static int same_divs(__isl_keep isl_basic_map
*bmap1
,
1506 __isl_keep isl_basic_map
*bmap2
)
1512 if (!bmap1
|| !bmap2
)
1514 if (bmap1
->n_div
!= bmap2
->n_div
)
1517 if (bmap1
->n_div
== 0)
1520 known
= isl_basic_map_divs_known(bmap1
);
1521 if (known
< 0 || !known
)
1523 known
= isl_basic_map_divs_known(bmap2
);
1524 if (known
< 0 || !known
)
1527 total
= isl_basic_map_total_dim(bmap1
);
1528 for (i
= 0; i
< bmap1
->n_div
; ++i
)
1529 if (!isl_seq_eq(bmap1
->div
[i
], bmap2
->div
[i
], 2 + total
))
1535 /* Does "bmap" contain the basic map represented by the tableau "tab"
1536 * after expanding the divs of "bmap" to match those of "tab"?
1537 * The expansion is performed using the divs "div" and expansion "exp"
1538 * computed by the caller.
1539 * Then we check if all constraints of the expanded "bmap" are valid for "tab".
1541 static int contains_with_expanded_divs(__isl_keep isl_basic_map
*bmap
,
1542 struct isl_tab
*tab
, __isl_keep isl_mat
*div
, int *exp
)
1548 bmap
= isl_basic_map_copy(bmap
);
1549 bmap
= isl_basic_set_expand_divs(bmap
, isl_mat_copy(div
), exp
);
1554 eq_i
= eq_status_in(bmap
, tab
);
1555 if (bmap
->n_eq
&& !eq_i
)
1557 if (any(eq_i
, 2 * bmap
->n_eq
, STATUS_ERROR
))
1559 if (any(eq_i
, 2 * bmap
->n_eq
, STATUS_SEPARATE
))
1562 ineq_i
= ineq_status_in(bmap
, NULL
, tab
);
1563 if (bmap
->n_ineq
&& !ineq_i
)
1565 if (any(ineq_i
, bmap
->n_ineq
, STATUS_ERROR
))
1567 if (any(ineq_i
, bmap
->n_ineq
, STATUS_SEPARATE
))
1570 if (all(eq_i
, 2 * bmap
->n_eq
, STATUS_VALID
) &&
1571 all(ineq_i
, bmap
->n_ineq
, STATUS_VALID
))
1575 isl_basic_map_free(bmap
);
1580 isl_basic_map_free(bmap
);
1586 /* Does "bmap_i" contain the basic map represented by "info_j"
1587 * after aligning the divs of "bmap_i" to those of "info_j".
1588 * Note that this can only succeed if the number of divs of "bmap_i"
1589 * is smaller than (or equal to) the number of divs of "info_j".
1591 * We first check if the divs of "bmap_i" are all known and form a subset
1592 * of those of "bmap_j". If so, we pass control over to
1593 * contains_with_expanded_divs.
1595 static int contains_after_aligning_divs(__isl_keep isl_basic_map
*bmap_i
,
1596 struct isl_coalesce_info
*info_j
)
1599 isl_mat
*div_i
, *div_j
, *div
;
1605 known
= isl_basic_map_divs_known(bmap_i
);
1606 if (known
< 0 || !known
)
1609 ctx
= isl_basic_map_get_ctx(bmap_i
);
1611 div_i
= isl_basic_map_get_divs(bmap_i
);
1612 div_j
= isl_basic_map_get_divs(info_j
->bmap
);
1614 if (!div_i
|| !div_j
)
1617 exp1
= isl_alloc_array(ctx
, int, div_i
->n_row
);
1618 exp2
= isl_alloc_array(ctx
, int, div_j
->n_row
);
1619 if ((div_i
->n_row
&& !exp1
) || (div_j
->n_row
&& !exp2
))
1622 div
= isl_merge_divs(div_i
, div_j
, exp1
, exp2
);
1626 if (div
->n_row
== div_j
->n_row
)
1627 subset
= contains_with_expanded_divs(bmap_i
,
1628 info_j
->tab
, div
, exp1
);
1634 isl_mat_free(div_i
);
1635 isl_mat_free(div_j
);
1642 isl_mat_free(div_i
);
1643 isl_mat_free(div_j
);
1649 /* Check if the basic map "j" is a subset of basic map "i",
1650 * if "i" has fewer divs that "j".
1651 * If so, remove basic map "j".
1653 * If the two basic maps have the same number of divs, then
1654 * they must necessarily be different. Otherwise, we would have
1655 * called coalesce_local_pair. We therefore don't try anything
1658 static int coalesced_subset(int i
, int j
, struct isl_coalesce_info
*info
)
1662 if (info
[i
].bmap
->n_div
>= info
[j
].bmap
->n_div
)
1665 superset
= contains_after_aligning_divs(info
[i
].bmap
, &info
[j
]);
1674 /* Check if one of the basic maps is a subset of the other and, if so,
1676 * Note that we only perform any test if the number of divs is different
1677 * in the two basic maps. In case the number of divs is the same,
1678 * we have already established that the divs are different
1679 * in the two basic maps.
1680 * In particular, if the number of divs of basic map i is smaller than
1681 * the number of divs of basic map j, then we check if j is a subset of i
1684 static enum isl_change
check_coalesce_subset(int i
, int j
,
1685 struct isl_coalesce_info
*info
)
1689 changed
= coalesced_subset(i
, j
, info
);
1690 if (changed
< 0 || changed
)
1691 return changed
< 0 ? isl_change_error
: isl_change_drop_second
;
1693 changed
= coalesced_subset(j
, i
, info
);
1694 if (changed
< 0 || changed
)
1695 return changed
< 0 ? isl_change_error
: isl_change_drop_first
;
1697 return isl_change_none
;
1700 /* Check if the union of the given pair of basic maps
1701 * can be represented by a single basic map.
1702 * If so, replace the pair by the single basic map and return
1703 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
1704 * Otherwise, return isl_change_none.
1706 * We first check if the two basic maps live in the same local space.
1707 * If so, we do the complete check. Otherwise, we check if one is
1708 * an obvious subset of the other.
1710 static enum isl_change
coalesce_pair(int i
, int j
,
1711 struct isl_coalesce_info
*info
)
1715 same
= same_divs(info
[i
].bmap
, info
[j
].bmap
);
1717 return isl_change_error
;
1719 return coalesce_local_pair(i
, j
, info
);
1721 return check_coalesce_subset(i
, j
, info
);
1724 /* Pairwise coalesce the basic maps described by the "n" elements of "info",
1725 * skipping basic maps that have been removed (either before or within
1728 * For each basic map i, we check if it can be coalesced with respect
1729 * to any previously considered basic map j.
1730 * If i gets dropped (because it was a subset of some j), then
1731 * we can move on to the next basic map.
1732 * If j gets dropped, we need to continue checking against the other
1733 * previously considered basic maps.
1734 * If the two basic maps got fused, then we recheck the fused basic map
1735 * against the previously considered basic maps.
1737 static int coalesce(isl_ctx
*ctx
, int n
, struct isl_coalesce_info
*info
)
1741 for (i
= n
- 2; i
>= 0; --i
) {
1742 if (info
[i
].removed
)
1744 for (j
= i
+ 1; j
< n
; ++j
) {
1745 enum isl_change changed
;
1747 if (info
[j
].removed
)
1749 if (info
[i
].removed
)
1750 isl_die(ctx
, isl_error_internal
,
1751 "basic map unexpectedly removed",
1753 changed
= coalesce_pair(i
, j
, info
);
1755 case isl_change_error
:
1757 case isl_change_none
:
1758 case isl_change_drop_second
:
1760 case isl_change_drop_first
:
1763 case isl_change_fuse
:
1773 /* Update the basic maps in "map" based on the information in "info".
1774 * In particular, remove the basic maps that have been marked removed and
1775 * update the others based on the information in the corresponding tableau.
1776 * Since we detected implicit equalities without calling
1777 * isl_basic_map_gauss, we need to do it now.
1779 static __isl_give isl_map
*update_basic_maps(__isl_take isl_map
*map
,
1780 int n
, struct isl_coalesce_info
*info
)
1787 for (i
= n
- 1; i
>= 0; --i
) {
1788 if (info
[i
].removed
) {
1789 isl_basic_map_free(map
->p
[i
]);
1790 if (i
!= map
->n
- 1)
1791 map
->p
[i
] = map
->p
[map
->n
- 1];
1796 info
[i
].bmap
= isl_basic_map_update_from_tab(info
[i
].bmap
,
1798 info
[i
].bmap
= isl_basic_map_gauss(info
[i
].bmap
, NULL
);
1799 info
[i
].bmap
= isl_basic_map_finalize(info
[i
].bmap
);
1801 return isl_map_free(map
);
1802 ISL_F_SET(info
[i
].bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
1803 ISL_F_SET(info
[i
].bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1804 isl_basic_map_free(map
->p
[i
]);
1805 map
->p
[i
] = info
[i
].bmap
;
1806 info
[i
].bmap
= NULL
;
1812 /* For each pair of basic maps in the map, check if the union of the two
1813 * can be represented by a single basic map.
1814 * If so, replace the pair by the single basic map and start over.
1816 * Since we are constructing the tableaus of the basic maps anyway,
1817 * we exploit them to detect implicit equalities and redundant constraints.
1818 * This also helps the coalescing as it can ignore the redundant constraints.
1819 * In order to avoid confusion, we make all implicit equalities explicit
1820 * in the basic maps. We don't call isl_basic_map_gauss, though,
1821 * as that may affect the number of constraints.
1822 * This means that we have to call isl_basic_map_gauss at the end
1823 * of the computation (in update_basic_maps) to ensure that
1824 * the basic maps are not left in an unexpected state.
1826 struct isl_map
*isl_map_coalesce(struct isl_map
*map
)
1831 struct isl_coalesce_info
*info
= NULL
;
1833 map
= isl_map_remove_empty_parts(map
);
1840 ctx
= isl_map_get_ctx(map
);
1841 map
= isl_map_sort_divs(map
);
1842 map
= isl_map_cow(map
);
1849 info
= isl_calloc_array(map
->ctx
, struct isl_coalesce_info
, n
);
1853 for (i
= 0; i
< map
->n
; ++i
) {
1854 info
[i
].bmap
= isl_basic_map_copy(map
->p
[i
]);
1855 info
[i
].tab
= isl_tab_from_basic_map(info
[i
].bmap
, 0);
1858 if (!ISL_F_ISSET(info
[i
].bmap
, ISL_BASIC_MAP_NO_IMPLICIT
))
1859 if (isl_tab_detect_implicit_equalities(info
[i
].tab
) < 0)
1861 info
[i
].bmap
= isl_tab_make_equalities_explicit(info
[i
].tab
,
1865 if (!ISL_F_ISSET(info
[i
].bmap
, ISL_BASIC_MAP_NO_REDUNDANT
))
1866 if (isl_tab_detect_redundant(info
[i
].tab
) < 0)
1869 for (i
= map
->n
- 1; i
>= 0; --i
)
1870 if (info
[i
].tab
->empty
)
1873 if (coalesce(ctx
, n
, info
) < 0)
1876 map
= update_basic_maps(map
, n
, info
);
1878 clear_coalesce_info(n
, info
);
1882 clear_coalesce_info(n
, info
);
1887 /* For each pair of basic sets in the set, check if the union of the two
1888 * can be represented by a single basic set.
1889 * If so, replace the pair by the single basic set and start over.
1891 struct isl_set
*isl_set_coalesce(struct isl_set
*set
)
1893 return (struct isl_set
*)isl_map_coalesce((struct isl_map
*)set
);