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 * "eq" and "ineq" are only set if we are currently trying to coalesce
157 * this basic map with another basic map, in which case they represent
158 * the position of the inequalities of this basic map with respect to
159 * the other basic map. The number of elements in the "eq" array
160 * is twice the number of equalities in the "bmap", corresponding
161 * to the two inequalities that make up each equality.
163 struct isl_coalesce_info
{
171 /* Free all the allocated memory in an array
172 * of "n" isl_coalesce_info elements.
174 static void clear_coalesce_info(int n
, struct isl_coalesce_info
*info
)
181 for (i
= 0; i
< n
; ++i
) {
182 isl_basic_map_free(info
[i
].bmap
);
183 isl_tab_free(info
[i
].tab
);
189 /* Drop the basic map represented by "info".
190 * That is, clear the memory associated to the entry and
191 * mark it as having been removed.
193 static void drop(struct isl_coalesce_info
*info
)
195 info
->bmap
= isl_basic_map_free(info
->bmap
);
196 isl_tab_free(info
->tab
);
201 /* Exchange the information in "info1" with that in "info2".
203 static void exchange(struct isl_coalesce_info
*info1
,
204 struct isl_coalesce_info
*info2
)
206 struct isl_coalesce_info info
;
213 /* This type represents the kind of change that has been performed
214 * while trying to coalesce two basic maps.
216 * isl_change_none: nothing was changed
217 * isl_change_drop_first: the first basic map was removed
218 * isl_change_drop_second: the second basic map was removed
219 * isl_change_fuse: the two basic maps were replaced by a new basic map.
222 isl_change_error
= -1,
224 isl_change_drop_first
,
225 isl_change_drop_second
,
229 /* Add the valid constraints of the basic map represented by "info"
230 * to "bmap". "len" is the size of the constraints.
231 * If only one of the pair of inequalities that make up an equality
232 * is valid, then add that inequality.
234 static __isl_give isl_basic_map
*add_valid_constraints(
235 __isl_take isl_basic_map
*bmap
, struct isl_coalesce_info
*info
,
243 for (k
= 0; k
< info
->bmap
->n_eq
; ++k
) {
244 if (info
->eq
[2 * k
] == STATUS_VALID
&&
245 info
->eq
[2 * k
+ 1] == STATUS_VALID
) {
246 l
= isl_basic_map_alloc_equality(bmap
);
248 return isl_basic_map_free(bmap
);
249 isl_seq_cpy(bmap
->eq
[l
], info
->bmap
->eq
[k
], len
);
250 } else if (info
->eq
[2 * k
] == STATUS_VALID
) {
251 l
= isl_basic_map_alloc_inequality(bmap
);
253 return isl_basic_map_free(bmap
);
254 isl_seq_neg(bmap
->ineq
[l
], info
->bmap
->eq
[k
], len
);
255 } else if (info
->eq
[2 * k
+ 1] == STATUS_VALID
) {
256 l
= isl_basic_map_alloc_inequality(bmap
);
258 return isl_basic_map_free(bmap
);
259 isl_seq_cpy(bmap
->ineq
[l
], info
->bmap
->eq
[k
], len
);
263 for (k
= 0; k
< info
->bmap
->n_ineq
; ++k
) {
264 if (info
->ineq
[k
] != STATUS_VALID
)
266 l
= isl_basic_map_alloc_inequality(bmap
);
268 return isl_basic_map_free(bmap
);
269 isl_seq_cpy(bmap
->ineq
[l
], info
->bmap
->ineq
[k
], len
);
275 /* Replace the pair of basic maps i and j by the basic map bounded
276 * by the valid constraints in both basic maps and the constraints
277 * in extra (if not NULL).
278 * Place the fused basic map in the position that is the smallest of i and j.
280 * If "detect_equalities" is set, then look for equalities encoded
281 * as pairs of inequalities.
283 static enum isl_change
fuse(int i
, int j
, struct isl_coalesce_info
*info
,
284 __isl_keep isl_mat
*extra
, int detect_equalities
)
287 struct isl_basic_map
*fused
= NULL
;
288 struct isl_tab
*fused_tab
= NULL
;
289 unsigned total
= isl_basic_map_total_dim(info
[i
].bmap
);
290 unsigned extra_rows
= extra
? extra
->n_row
: 0;
291 unsigned n_eq
, n_ineq
;
294 return fuse(j
, i
, info
, extra
, detect_equalities
);
296 n_eq
= info
[i
].bmap
->n_eq
+ info
[j
].bmap
->n_eq
;
297 n_ineq
= info
[i
].bmap
->n_ineq
+ info
[j
].bmap
->n_ineq
;
298 fused
= isl_basic_map_alloc_space(isl_space_copy(info
[i
].bmap
->dim
),
299 info
[i
].bmap
->n_div
, n_eq
, n_eq
+ n_ineq
+ extra_rows
);
300 fused
= add_valid_constraints(fused
, &info
[i
], 1 + total
);
301 fused
= add_valid_constraints(fused
, &info
[j
], 1 + total
);
305 for (k
= 0; k
< info
[i
].bmap
->n_div
; ++k
) {
306 int l
= isl_basic_map_alloc_div(fused
);
309 isl_seq_cpy(fused
->div
[l
], info
[i
].bmap
->div
[k
], 1 + 1 + total
);
312 for (k
= 0; k
< extra_rows
; ++k
) {
313 l
= isl_basic_map_alloc_inequality(fused
);
316 isl_seq_cpy(fused
->ineq
[l
], extra
->row
[k
], 1 + total
);
319 if (detect_equalities
)
320 fused
= isl_basic_map_detect_inequality_pairs(fused
, NULL
);
321 fused
= isl_basic_map_gauss(fused
, NULL
);
322 ISL_F_SET(fused
, ISL_BASIC_MAP_FINAL
);
323 if (ISL_F_ISSET(info
[i
].bmap
, ISL_BASIC_MAP_RATIONAL
) &&
324 ISL_F_ISSET(info
[j
].bmap
, ISL_BASIC_MAP_RATIONAL
))
325 ISL_F_SET(fused
, ISL_BASIC_MAP_RATIONAL
);
327 fused_tab
= isl_tab_from_basic_map(fused
, 0);
328 if (isl_tab_detect_redundant(fused_tab
) < 0)
331 isl_basic_map_free(info
[i
].bmap
);
332 info
[i
].bmap
= fused
;
333 isl_tab_free(info
[i
].tab
);
334 info
[i
].tab
= fused_tab
;
337 return isl_change_fuse
;
339 isl_tab_free(fused_tab
);
340 isl_basic_map_free(fused
);
341 return isl_change_error
;
344 /* Given a pair of basic maps i and j such that all constraints are either
345 * "valid" or "cut", check if the facets corresponding to the "cut"
346 * constraints of i lie entirely within basic map j.
347 * If so, replace the pair by the basic map consisting of the valid
348 * constraints in both basic maps.
349 * Checking whether the facet lies entirely within basic map j
350 * is performed by checking whether the constraints of basic map j
351 * are valid for the facet. These tests are performed on a rational
352 * tableau to avoid the theoretical possibility that a constraint
353 * that was considered to be a cut constraint for the entire basic map i
354 * happens to be considered to be a valid constraint for the facet,
355 * even though it cuts off the same rational points.
357 * To see that we are not introducing any extra points, call the
358 * two basic maps A and B and the resulting map U and let x
359 * be an element of U \setminus ( A \cup B ).
360 * A line connecting x with an element of A \cup B meets a facet F
361 * of either A or B. Assume it is a facet of B and let c_1 be
362 * the corresponding facet constraint. We have c_1(x) < 0 and
363 * so c_1 is a cut constraint. This implies that there is some
364 * (possibly rational) point x' satisfying the constraints of A
365 * and the opposite of c_1 as otherwise c_1 would have been marked
366 * valid for A. The line connecting x and x' meets a facet of A
367 * in a (possibly rational) point that also violates c_1, but this
368 * is impossible since all cut constraints of B are valid for all
370 * In case F is a facet of A rather than B, then we can apply the
371 * above reasoning to find a facet of B separating x from A \cup B first.
373 static enum isl_change
check_facets(int i
, int j
,
374 struct isl_coalesce_info
*info
)
377 struct isl_tab_undo
*snap
, *snap2
;
378 unsigned n_eq
= info
[i
].bmap
->n_eq
;
380 snap
= isl_tab_snap(info
[i
].tab
);
381 if (isl_tab_mark_rational(info
[i
].tab
) < 0)
382 return isl_change_error
;
383 snap2
= isl_tab_snap(info
[i
].tab
);
385 for (k
= 0; k
< info
[i
].bmap
->n_ineq
; ++k
) {
386 if (info
[i
].ineq
[k
] != STATUS_CUT
)
388 if (isl_tab_select_facet(info
[i
].tab
, n_eq
+ k
) < 0)
389 return isl_change_error
;
390 for (l
= 0; l
< info
[j
].bmap
->n_ineq
; ++l
) {
392 if (info
[j
].ineq
[l
] != STATUS_CUT
)
394 stat
= status_in(info
[j
].bmap
->ineq
[l
], info
[i
].tab
);
395 if (stat
!= STATUS_VALID
)
398 if (isl_tab_rollback(info
[i
].tab
, snap2
) < 0)
399 return isl_change_error
;
400 if (l
< info
[j
].bmap
->n_ineq
)
404 if (k
< info
[i
].bmap
->n_ineq
) {
405 if (isl_tab_rollback(info
[i
].tab
, snap
) < 0)
406 return isl_change_error
;
407 return isl_change_none
;
409 return fuse(i
, j
, info
, NULL
, 0);
412 /* Check if info->bmap contains the basic map represented
413 * by the tableau "tab".
414 * For each equality, we check both the constraint itself
415 * (as an inequality) and its negation. Make sure the
416 * equality is returned to its original state before returning.
418 static int contains(struct isl_coalesce_info
*info
, struct isl_tab
*tab
)
422 isl_basic_map
*bmap
= info
->bmap
;
424 dim
= isl_basic_map_total_dim(bmap
);
425 for (k
= 0; k
< bmap
->n_eq
; ++k
) {
427 isl_seq_neg(bmap
->eq
[k
], bmap
->eq
[k
], 1 + dim
);
428 stat
= status_in(bmap
->eq
[k
], tab
);
429 isl_seq_neg(bmap
->eq
[k
], bmap
->eq
[k
], 1 + dim
);
430 if (stat
!= STATUS_VALID
)
432 stat
= status_in(bmap
->eq
[k
], tab
);
433 if (stat
!= STATUS_VALID
)
437 for (k
= 0; k
< bmap
->n_ineq
; ++k
) {
439 if (info
->ineq
[k
] == STATUS_REDUNDANT
)
441 stat
= status_in(bmap
->ineq
[k
], tab
);
442 if (stat
!= STATUS_VALID
)
448 /* Basic map "i" has an inequality (say "k") that is adjacent
449 * to some inequality of basic map "j". All the other inequalities
451 * Check if basic map "j" forms an extension of basic map "i".
453 * Note that this function is only called if some of the equalities or
454 * inequalities of basic map "j" do cut basic map "i". The function is
455 * correct even if there are no such cut constraints, but in that case
456 * the additional checks performed by this function are overkill.
458 * In particular, we replace constraint k, say f >= 0, by constraint
459 * f <= -1, add the inequalities of "j" that are valid for "i"
460 * and check if the result is a subset of basic map "j".
461 * If so, then we know that this result is exactly equal to basic map "j"
462 * since all its constraints are valid for basic map "j".
463 * By combining the valid constraints of "i" (all equalities and all
464 * inequalities except "k") and the valid constraints of "j" we therefore
465 * obtain a basic map that is equal to their union.
466 * In this case, there is no need to perform a rollback of the tableau
467 * since it is going to be destroyed in fuse().
473 * |_______| _ |_________\
485 static enum isl_change
is_adj_ineq_extension(int i
, int j
,
486 struct isl_coalesce_info
*info
)
489 struct isl_tab_undo
*snap
;
490 unsigned n_eq
= info
[i
].bmap
->n_eq
;
491 unsigned total
= isl_basic_map_total_dim(info
[i
].bmap
);
494 if (isl_tab_extend_cons(info
[i
].tab
, 1 + info
[j
].bmap
->n_ineq
) < 0)
495 return isl_change_error
;
497 for (k
= 0; k
< info
[i
].bmap
->n_ineq
; ++k
)
498 if (info
[i
].ineq
[k
] == STATUS_ADJ_INEQ
)
500 if (k
>= info
[i
].bmap
->n_ineq
)
501 isl_die(isl_basic_map_get_ctx(info
[i
].bmap
), isl_error_internal
,
502 "info[i].ineq should have exactly one STATUS_ADJ_INEQ",
503 return isl_change_error
);
505 snap
= isl_tab_snap(info
[i
].tab
);
507 if (isl_tab_unrestrict(info
[i
].tab
, n_eq
+ k
) < 0)
508 return isl_change_error
;
510 isl_seq_neg(info
[i
].bmap
->ineq
[k
], info
[i
].bmap
->ineq
[k
], 1 + total
);
511 isl_int_sub_ui(info
[i
].bmap
->ineq
[k
][0], info
[i
].bmap
->ineq
[k
][0], 1);
512 r
= isl_tab_add_ineq(info
[i
].tab
, info
[i
].bmap
->ineq
[k
]);
513 isl_seq_neg(info
[i
].bmap
->ineq
[k
], info
[i
].bmap
->ineq
[k
], 1 + total
);
514 isl_int_sub_ui(info
[i
].bmap
->ineq
[k
][0], info
[i
].bmap
->ineq
[k
][0], 1);
516 return isl_change_error
;
518 for (k
= 0; k
< info
[j
].bmap
->n_ineq
; ++k
) {
519 if (info
[j
].ineq
[k
] != STATUS_VALID
)
521 if (isl_tab_add_ineq(info
[i
].tab
, info
[j
].bmap
->ineq
[k
]) < 0)
522 return isl_change_error
;
525 if (contains(&info
[j
], info
[i
].tab
))
526 return fuse(i
, j
, info
, NULL
, 0);
528 if (isl_tab_rollback(info
[i
].tab
, snap
) < 0)
529 return isl_change_error
;
531 return isl_change_none
;
535 /* Both basic maps have at least one inequality with and adjacent
536 * (but opposite) inequality in the other basic map.
537 * Check that there are no cut constraints and that there is only
538 * a single pair of adjacent inequalities.
539 * If so, we can replace the pair by a single basic map described
540 * by all but the pair of adjacent inequalities.
541 * Any additional points introduced lie strictly between the two
542 * adjacent hyperplanes and can therefore be integral.
551 * The test for a single pair of adjancent inequalities is important
552 * for avoiding the combination of two basic maps like the following
562 * If there are some cut constraints on one side, then we may
563 * still be able to fuse the two basic maps, but we need to perform
564 * some additional checks in is_adj_ineq_extension.
566 static enum isl_change
check_adj_ineq(int i
, int j
,
567 struct isl_coalesce_info
*info
)
569 int count_i
, count_j
;
572 count_i
= count(info
[i
].ineq
, info
[i
].bmap
->n_ineq
, STATUS_ADJ_INEQ
);
573 count_j
= count(info
[j
].ineq
, info
[j
].bmap
->n_ineq
, STATUS_ADJ_INEQ
);
575 if (count_i
!= 1 && count_j
!= 1)
576 return isl_change_none
;
578 cut_i
= any(info
[i
].eq
, 2 * info
[i
].bmap
->n_eq
, STATUS_CUT
) ||
579 any(info
[i
].ineq
, info
[i
].bmap
->n_ineq
, STATUS_CUT
);
580 cut_j
= any(info
[j
].eq
, 2 * info
[j
].bmap
->n_eq
, STATUS_CUT
) ||
581 any(info
[j
].ineq
, info
[j
].bmap
->n_ineq
, STATUS_CUT
);
583 if (!cut_i
&& !cut_j
&& count_i
== 1 && count_j
== 1)
584 return fuse(i
, j
, info
, NULL
, 0);
586 if (count_i
== 1 && !cut_i
)
587 return is_adj_ineq_extension(i
, j
, info
);
589 if (count_j
== 1 && !cut_j
)
590 return is_adj_ineq_extension(j
, i
, info
);
592 return isl_change_none
;
595 /* Basic map "i" has an inequality "k" that is adjacent to some equality
596 * of basic map "j". All the other inequalities are valid for "j".
597 * Check if basic map "j" forms an extension of basic map "i".
599 * In particular, we relax constraint "k", compute the corresponding
600 * facet and check whether it is included in the other basic map.
601 * If so, we know that relaxing the constraint extends the basic
602 * map with exactly the other basic map (we already know that this
603 * other basic map is included in the extension, because there
604 * were no "cut" inequalities in "i") and we can replace the
605 * two basic maps by this extension.
606 * Place this extension in the position that is the smallest of i and j.
614 static enum isl_change
is_adj_eq_extension(int i
, int j
, int k
,
615 struct isl_coalesce_info
*info
)
617 int change
= isl_change_none
;
619 struct isl_tab_undo
*snap
, *snap2
;
620 unsigned n_eq
= info
[i
].bmap
->n_eq
;
622 if (isl_tab_is_equality(info
[i
].tab
, n_eq
+ k
))
623 return isl_change_none
;
625 snap
= isl_tab_snap(info
[i
].tab
);
626 if (isl_tab_relax(info
[i
].tab
, n_eq
+ k
) < 0)
627 return isl_change_error
;
628 snap2
= isl_tab_snap(info
[i
].tab
);
629 if (isl_tab_select_facet(info
[i
].tab
, n_eq
+ k
) < 0)
630 return isl_change_error
;
631 super
= contains(&info
[j
], info
[i
].tab
);
633 if (isl_tab_rollback(info
[i
].tab
, snap2
) < 0)
634 return isl_change_error
;
635 info
[i
].bmap
= isl_basic_map_cow(info
[i
].bmap
);
637 return isl_change_error
;
638 isl_int_add_ui(info
[i
].bmap
->ineq
[k
][0],
639 info
[i
].bmap
->ineq
[k
][0], 1);
640 ISL_F_SET(info
[i
].bmap
, ISL_BASIC_MAP_FINAL
);
643 exchange(&info
[i
], &info
[j
]);
644 change
= isl_change_fuse
;
646 if (isl_tab_rollback(info
[i
].tab
, snap
) < 0)
647 return isl_change_error
;
652 /* Data structure that keeps track of the wrapping constraints
653 * and of information to bound the coefficients of those constraints.
655 * bound is set if we want to apply a bound on the coefficients
656 * mat contains the wrapping constraints
657 * max is the bound on the coefficients (if bound is set)
665 /* Update wraps->max to be greater than or equal to the coefficients
666 * in the equalities and inequalities of info->bmap that can be removed
667 * if we end up applying wrapping.
669 static void wraps_update_max(struct isl_wraps
*wraps
,
670 struct isl_coalesce_info
*info
)
674 unsigned total
= isl_basic_map_total_dim(info
->bmap
);
678 for (k
= 0; k
< info
->bmap
->n_eq
; ++k
) {
679 if (info
->eq
[2 * k
] == STATUS_VALID
&&
680 info
->eq
[2 * k
+ 1] == STATUS_VALID
)
682 isl_seq_abs_max(info
->bmap
->eq
[k
] + 1, total
, &max_k
);
683 if (isl_int_abs_gt(max_k
, wraps
->max
))
684 isl_int_set(wraps
->max
, max_k
);
687 for (k
= 0; k
< info
->bmap
->n_ineq
; ++k
) {
688 if (info
->ineq
[k
] == STATUS_VALID
||
689 info
->ineq
[k
] == STATUS_REDUNDANT
)
691 isl_seq_abs_max(info
->bmap
->ineq
[k
] + 1, total
, &max_k
);
692 if (isl_int_abs_gt(max_k
, wraps
->max
))
693 isl_int_set(wraps
->max
, max_k
);
696 isl_int_clear(max_k
);
699 /* Initialize the isl_wraps data structure.
700 * If we want to bound the coefficients of the wrapping constraints,
701 * we set wraps->max to the largest coefficient
702 * in the equalities and inequalities that can be removed if we end up
705 static void wraps_init(struct isl_wraps
*wraps
, __isl_take isl_mat
*mat
,
706 struct isl_coalesce_info
*info
, int i
, int j
)
714 ctx
= isl_mat_get_ctx(mat
);
715 wraps
->bound
= isl_options_get_coalesce_bounded_wrapping(ctx
);
718 isl_int_init(wraps
->max
);
719 isl_int_set_si(wraps
->max
, 0);
720 wraps_update_max(wraps
, &info
[i
]);
721 wraps_update_max(wraps
, &info
[j
]);
724 /* Free the contents of the isl_wraps data structure.
726 static void wraps_free(struct isl_wraps
*wraps
)
728 isl_mat_free(wraps
->mat
);
730 isl_int_clear(wraps
->max
);
733 /* Is the wrapping constraint in row "row" allowed?
735 * If wraps->bound is set, we check that none of the coefficients
736 * is greater than wraps->max.
738 static int allow_wrap(struct isl_wraps
*wraps
, int row
)
745 for (i
= 1; i
< wraps
->mat
->n_col
; ++i
)
746 if (isl_int_abs_gt(wraps
->mat
->row
[row
][i
], wraps
->max
))
752 /* Wrap "ineq" (or its opposite if "negate" is set) around "bound"
753 * to include "set" and add the result in position "w" of "wraps".
754 * "len" is the total number of coefficients in "bound" and "ineq".
755 * Return 1 on success, 0 on failure and -1 on error.
756 * Wrapping can fail if the result of wrapping is equal to "bound"
757 * or if we want to bound the sizes of the coefficients and
758 * the wrapped constraint does not satisfy this bound.
760 static int add_wrap(struct isl_wraps
*wraps
, int w
, isl_int
*bound
,
761 isl_int
*ineq
, unsigned len
, __isl_keep isl_set
*set
, int negate
)
763 isl_seq_cpy(wraps
->mat
->row
[w
], bound
, len
);
765 isl_seq_neg(wraps
->mat
->row
[w
+ 1], ineq
, len
);
766 ineq
= wraps
->mat
->row
[w
+ 1];
768 if (!isl_set_wrap_facet(set
, wraps
->mat
->row
[w
], ineq
))
770 if (isl_seq_eq(wraps
->mat
->row
[w
], bound
, len
))
772 if (!allow_wrap(wraps
, w
))
777 /* For each constraint in info->bmap that is not redundant (as determined
778 * by info->tab) and that is not a valid constraint for the other basic map,
779 * wrap the constraint around "bound" such that it includes the whole
780 * set "set" and append the resulting constraint to "wraps".
781 * Note that the constraints that are valid for the other basic map
782 * will be added to the combined basic map by default, so there is
783 * no need to wrap them.
784 * The caller wrap_in_facets even relies on this function not wrapping
785 * any constraints that are already valid.
786 * "wraps" is assumed to have been pre-allocated to the appropriate size.
787 * wraps->n_row is the number of actual wrapped constraints that have
789 * If any of the wrapping problems results in a constraint that is
790 * identical to "bound", then this means that "set" is unbounded in such
791 * way that no wrapping is possible. If this happens then wraps->n_row
793 * Similarly, if we want to bound the coefficients of the wrapping
794 * constraints and a newly added wrapping constraint does not
795 * satisfy the bound, then wraps->n_row is also reset to zero.
797 static int add_wraps(struct isl_wraps
*wraps
, struct isl_coalesce_info
*info
,
798 isl_int
*bound
, __isl_keep isl_set
*set
)
803 isl_basic_map
*bmap
= info
->bmap
;
804 unsigned len
= 1 + isl_basic_map_total_dim(bmap
);
806 w
= wraps
->mat
->n_row
;
808 for (l
= 0; l
< bmap
->n_ineq
; ++l
) {
809 if (info
->ineq
[l
] == STATUS_VALID
||
810 info
->ineq
[l
] == STATUS_REDUNDANT
)
812 if (isl_seq_is_neg(bound
, bmap
->ineq
[l
], len
))
814 if (isl_seq_eq(bound
, bmap
->ineq
[l
], len
))
816 if (isl_tab_is_redundant(info
->tab
, bmap
->n_eq
+ l
))
819 added
= add_wrap(wraps
, w
, bound
, bmap
->ineq
[l
], len
, set
, 0);
826 for (l
= 0; l
< bmap
->n_eq
; ++l
) {
827 if (isl_seq_is_neg(bound
, bmap
->eq
[l
], len
))
829 if (isl_seq_eq(bound
, bmap
->eq
[l
], len
))
832 for (m
= 0; m
< 2; ++m
) {
833 if (info
->eq
[2 * l
+ m
] == STATUS_VALID
)
835 added
= add_wrap(wraps
, w
, bound
, bmap
->eq
[l
], len
,
845 wraps
->mat
->n_row
= w
;
848 wraps
->mat
->n_row
= 0;
852 /* Check if the constraints in "wraps" from "first" until the last
853 * are all valid for the basic set represented by "tab".
854 * If not, wraps->n_row is set to zero.
856 static int check_wraps(__isl_keep isl_mat
*wraps
, int first
,
861 for (i
= first
; i
< wraps
->n_row
; ++i
) {
862 enum isl_ineq_type type
;
863 type
= isl_tab_ineq_type(tab
, wraps
->row
[i
]);
864 if (type
== isl_ineq_error
)
866 if (type
== isl_ineq_redundant
)
875 /* Return a set that corresponds to the non-redundant constraints
876 * (as recorded in tab) of bmap.
878 * It's important to remove the redundant constraints as some
879 * of the other constraints may have been modified after the
880 * constraints were marked redundant.
881 * In particular, a constraint may have been relaxed.
882 * Redundant constraints are ignored when a constraint is relaxed
883 * and should therefore continue to be ignored ever after.
884 * Otherwise, the relaxation might be thwarted by some of
887 * Update the underlying set to ensure that the dimension doesn't change.
888 * Otherwise the integer divisions could get dropped if the tab
889 * turns out to be empty.
891 static __isl_give isl_set
*set_from_updated_bmap(__isl_keep isl_basic_map
*bmap
,
896 bmap
= isl_basic_map_copy(bmap
);
897 bset
= isl_basic_map_underlying_set(bmap
);
898 bset
= isl_basic_set_cow(bset
);
899 bset
= isl_basic_set_update_from_tab(bset
, tab
);
900 return isl_set_from_basic_set(bset
);
903 /* Given a basic set i with a constraint k that is adjacent to
904 * basic set j, check if we can wrap
905 * both the facet corresponding to k and basic map j
906 * around their ridges to include the other set.
907 * If so, replace the pair of basic sets by their union.
909 * All constraints of i (except k) are assumed to be valid for j.
910 * This means that there is no real need to wrap the ridges of
911 * the faces of basic map i around basic map j but since we do,
912 * we have to check that the resulting wrapping constraints are valid for i.
921 static enum isl_change
can_wrap_in_facet(int i
, int j
, int k
,
922 struct isl_coalesce_info
*info
)
924 enum isl_change change
= isl_change_none
;
925 struct isl_wraps wraps
;
928 struct isl_set
*set_i
= NULL
;
929 struct isl_set
*set_j
= NULL
;
930 struct isl_vec
*bound
= NULL
;
931 unsigned total
= isl_basic_map_total_dim(info
[i
].bmap
);
932 struct isl_tab_undo
*snap
;
935 set_i
= set_from_updated_bmap(info
[i
].bmap
, info
[i
].tab
);
936 set_j
= set_from_updated_bmap(info
[j
].bmap
, info
[j
].tab
);
937 ctx
= isl_basic_map_get_ctx(info
[i
].bmap
);
938 mat
= isl_mat_alloc(ctx
, 2 * (info
[i
].bmap
->n_eq
+ info
[j
].bmap
->n_eq
) +
939 info
[i
].bmap
->n_ineq
+ info
[j
].bmap
->n_ineq
,
941 wraps_init(&wraps
, mat
, info
, i
, j
);
942 bound
= isl_vec_alloc(ctx
, 1 + total
);
943 if (!set_i
|| !set_j
|| !wraps
.mat
|| !bound
)
946 isl_seq_cpy(bound
->el
, info
[i
].bmap
->ineq
[k
], 1 + total
);
947 isl_int_add_ui(bound
->el
[0], bound
->el
[0], 1);
949 isl_seq_cpy(wraps
.mat
->row
[0], bound
->el
, 1 + total
);
950 wraps
.mat
->n_row
= 1;
952 if (add_wraps(&wraps
, &info
[j
], bound
->el
, set_i
) < 0)
954 if (!wraps
.mat
->n_row
)
957 snap
= isl_tab_snap(info
[i
].tab
);
959 if (isl_tab_select_facet(info
[i
].tab
, info
[i
].bmap
->n_eq
+ k
) < 0)
961 if (isl_tab_detect_redundant(info
[i
].tab
) < 0)
964 isl_seq_neg(bound
->el
, info
[i
].bmap
->ineq
[k
], 1 + total
);
966 n
= wraps
.mat
->n_row
;
967 if (add_wraps(&wraps
, &info
[i
], bound
->el
, set_j
) < 0)
970 if (isl_tab_rollback(info
[i
].tab
, snap
) < 0)
972 if (check_wraps(wraps
.mat
, n
, info
[i
].tab
) < 0)
974 if (!wraps
.mat
->n_row
)
977 change
= fuse(i
, j
, info
, wraps
.mat
, 0);
993 return isl_change_error
;
996 /* Given a pair of basic maps i and j such that j sticks out
997 * of i at n cut constraints, each time by at most one,
998 * try to compute wrapping constraints and replace the two
999 * basic maps by a single basic map.
1000 * The other constraints of i are assumed to be valid for j.
1002 * For each cut constraint t(x) >= 0 of i, we add the relaxed version
1003 * t(x) + 1 >= 0, along with wrapping constraints for all constraints
1004 * of basic map j that bound the part of basic map j that sticks out
1005 * of the cut constraint.
1006 * In particular, we first intersect basic map j with t(x) + 1 = 0.
1007 * If the result is empty, then t(x) >= 0 was actually a valid constraint
1008 * (with respect to the integer points), so we add t(x) >= 0 instead.
1009 * Otherwise, we wrap the constraints of basic map j that are not
1010 * redundant in this intersection and that are not already valid
1011 * for basic map i over basic map i.
1012 * Note that it is sufficient to wrap the constraints to include
1013 * basic map i, because we will only wrap the constraints that do
1014 * not include basic map i already. The wrapped constraint will
1015 * therefore be more relaxed compared to the original constraint.
1016 * Since the original constraint is valid for basic map j, so is
1017 * the wrapped constraint.
1019 * If any wrapping fails, i.e., if we cannot wrap to touch
1020 * the union, then we give up.
1021 * Otherwise, the pair of basic maps is replaced by their union.
1023 static enum isl_change
wrap_in_facets(int i
, int j
, int *cuts
, int n
,
1024 struct isl_coalesce_info
*info
)
1026 enum isl_change change
= isl_change_none
;
1027 struct isl_wraps wraps
;
1030 isl_set
*set_i
= NULL
;
1031 unsigned total
= isl_basic_map_total_dim(info
[i
].bmap
);
1034 struct isl_tab_undo
*snap
;
1036 if (isl_tab_extend_cons(info
[j
].tab
, 1) < 0)
1039 max_wrap
= 1 + 2 * info
[j
].bmap
->n_eq
+ info
[j
].bmap
->n_ineq
;
1042 set_i
= set_from_updated_bmap(info
[i
].bmap
, info
[i
].tab
);
1043 ctx
= isl_basic_map_get_ctx(info
[i
].bmap
);
1044 mat
= isl_mat_alloc(ctx
, max_wrap
, 1 + total
);
1045 wraps_init(&wraps
, mat
, info
, i
, j
);
1046 if (!set_i
|| !wraps
.mat
)
1049 snap
= isl_tab_snap(info
[j
].tab
);
1051 wraps
.mat
->n_row
= 0;
1053 for (k
= 0; k
< n
; ++k
) {
1054 w
= wraps
.mat
->n_row
++;
1055 isl_seq_cpy(wraps
.mat
->row
[w
],
1056 info
[i
].bmap
->ineq
[cuts
[k
]], 1 + total
);
1057 isl_int_add_ui(wraps
.mat
->row
[w
][0], wraps
.mat
->row
[w
][0], 1);
1058 if (isl_tab_add_eq(info
[j
].tab
, wraps
.mat
->row
[w
]) < 0)
1060 if (isl_tab_detect_redundant(info
[j
].tab
) < 0)
1063 if (info
[j
].tab
->empty
)
1064 isl_int_sub_ui(wraps
.mat
->row
[w
][0],
1065 wraps
.mat
->row
[w
][0], 1);
1066 else if (add_wraps(&wraps
, &info
[j
],
1067 wraps
.mat
->row
[w
], set_i
) < 0)
1070 if (isl_tab_rollback(info
[j
].tab
, snap
) < 0)
1073 if (!wraps
.mat
->n_row
)
1078 change
= fuse(i
, j
, info
, wraps
.mat
, 0);
1081 isl_set_free(set_i
);
1086 isl_set_free(set_i
);
1087 return isl_change_error
;
1090 /* Given two basic sets i and j such that i has no cut equalities,
1091 * check if relaxing all the cut inequalities of i by one turns
1092 * them into valid constraint for j and check if we can wrap in
1093 * the bits that are sticking out.
1094 * If so, replace the pair by their union.
1096 * We first check if all relaxed cut inequalities of i are valid for j
1097 * and then try to wrap in the intersections of the relaxed cut inequalities
1100 * During this wrapping, we consider the points of j that lie at a distance
1101 * of exactly 1 from i. In particular, we ignore the points that lie in
1102 * between this lower-dimensional space and the basic map i.
1103 * We can therefore only apply this to integer maps.
1129 * Wrapping can fail if the result of wrapping one of the facets
1130 * around its edges does not produce any new facet constraint.
1131 * In particular, this happens when we try to wrap in unbounded sets.
1133 * _______________________________________________________________________
1137 * |_| |_________________________________________________________________
1140 * The following is not an acceptable result of coalescing the above two
1141 * sets as it includes extra integer points.
1142 * _______________________________________________________________________
1147 * \______________________________________________________________________
1149 static enum isl_change
can_wrap_in_set(int i
, int j
,
1150 struct isl_coalesce_info
*info
)
1152 enum isl_change change
= isl_change_none
;
1158 if (ISL_F_ISSET(info
[i
].bmap
, ISL_BASIC_MAP_RATIONAL
) ||
1159 ISL_F_ISSET(info
[j
].bmap
, ISL_BASIC_MAP_RATIONAL
))
1160 return isl_change_none
;
1162 n
= count(info
[i
].ineq
, info
[i
].bmap
->n_ineq
, STATUS_CUT
);
1164 return isl_change_none
;
1166 ctx
= isl_basic_map_get_ctx(info
[i
].bmap
);
1167 cuts
= isl_alloc_array(ctx
, int, n
);
1169 return isl_change_error
;
1171 for (k
= 0, m
= 0; m
< n
; ++k
) {
1172 enum isl_ineq_type type
;
1174 if (info
[i
].ineq
[k
] != STATUS_CUT
)
1177 isl_int_add_ui(info
[i
].bmap
->ineq
[k
][0],
1178 info
[i
].bmap
->ineq
[k
][0], 1);
1179 type
= isl_tab_ineq_type(info
[j
].tab
, info
[i
].bmap
->ineq
[k
]);
1180 isl_int_sub_ui(info
[i
].bmap
->ineq
[k
][0],
1181 info
[i
].bmap
->ineq
[k
][0], 1);
1182 if (type
== isl_ineq_error
)
1184 if (type
!= isl_ineq_redundant
)
1191 change
= wrap_in_facets(i
, j
, cuts
, n
, info
);
1198 return isl_change_error
;
1201 /* Check if either i or j has only cut inequalities that can
1202 * be used to wrap in (a facet of) the other basic set.
1203 * if so, replace the pair by their union.
1205 static enum isl_change
check_wrap(int i
, int j
, struct isl_coalesce_info
*info
)
1207 enum isl_change change
= isl_change_none
;
1209 if (!any(info
[i
].eq
, 2 * info
[i
].bmap
->n_eq
, STATUS_CUT
))
1210 change
= can_wrap_in_set(i
, j
, info
);
1211 if (change
!= isl_change_none
)
1214 if (!any(info
[j
].eq
, 2 * info
[j
].bmap
->n_eq
, STATUS_CUT
))
1215 change
= can_wrap_in_set(j
, i
, info
);
1219 /* At least one of the basic maps has an equality that is adjacent
1220 * to inequality. Make sure that only one of the basic maps has
1221 * such an equality and that the other basic map has exactly one
1222 * inequality adjacent to an equality.
1223 * We call the basic map that has the inequality "i" and the basic
1224 * map that has the equality "j".
1225 * If "i" has any "cut" (in)equality, then relaxing the inequality
1226 * by one would not result in a basic map that contains the other
1229 static enum isl_change
check_adj_eq(int i
, int j
,
1230 struct isl_coalesce_info
*info
)
1232 enum isl_change change
= isl_change_none
;
1235 if (any(info
[i
].eq
, 2 * info
[i
].bmap
->n_eq
, STATUS_ADJ_INEQ
) &&
1236 any(info
[j
].eq
, 2 * info
[j
].bmap
->n_eq
, STATUS_ADJ_INEQ
))
1237 /* ADJ EQ TOO MANY */
1238 return isl_change_none
;
1240 if (any(info
[i
].eq
, 2 * info
[i
].bmap
->n_eq
, STATUS_ADJ_INEQ
))
1241 return check_adj_eq(j
, i
, info
);
1243 /* j has an equality adjacent to an inequality in i */
1245 if (any(info
[i
].eq
, 2 * info
[i
].bmap
->n_eq
, STATUS_CUT
))
1246 return isl_change_none
;
1247 if (any(info
[i
].ineq
, info
[i
].bmap
->n_ineq
, STATUS_CUT
))
1249 return isl_change_none
;
1250 if (count(info
[i
].ineq
, info
[i
].bmap
->n_ineq
, STATUS_ADJ_EQ
) != 1 ||
1251 any(info
[j
].ineq
, info
[j
].bmap
->n_ineq
, STATUS_ADJ_EQ
) ||
1252 any(info
[i
].ineq
, info
[i
].bmap
->n_ineq
, STATUS_ADJ_INEQ
) ||
1253 any(info
[j
].ineq
, info
[j
].bmap
->n_ineq
, STATUS_ADJ_INEQ
))
1254 /* ADJ EQ TOO MANY */
1255 return isl_change_none
;
1257 for (k
= 0; k
< info
[i
].bmap
->n_ineq
; ++k
)
1258 if (info
[i
].ineq
[k
] == STATUS_ADJ_EQ
)
1261 change
= is_adj_eq_extension(i
, j
, k
, info
);
1262 if (change
!= isl_change_none
)
1265 if (count(info
[j
].eq
, 2 * info
[j
].bmap
->n_eq
, STATUS_ADJ_INEQ
) != 1)
1266 return isl_change_none
;
1268 change
= can_wrap_in_facet(i
, j
, k
, info
);
1273 /* The two basic maps lie on adjacent hyperplanes. In particular,
1274 * basic map "i" has an equality that lies parallel to basic map "j".
1275 * Check if we can wrap the facets around the parallel hyperplanes
1276 * to include the other set.
1278 * We perform basically the same operations as can_wrap_in_facet,
1279 * except that we don't need to select a facet of one of the sets.
1285 * If there is more than one equality of "i" adjacent to an equality of "j",
1286 * then the result will satisfy one or more equalities that are a linear
1287 * combination of these equalities. These will be encoded as pairs
1288 * of inequalities in the wrapping constraints and need to be made
1291 static enum isl_change
check_eq_adj_eq(int i
, int j
,
1292 struct isl_coalesce_info
*info
)
1295 enum isl_change change
= isl_change_none
;
1296 int detect_equalities
= 0;
1297 struct isl_wraps wraps
;
1300 struct isl_set
*set_i
= NULL
;
1301 struct isl_set
*set_j
= NULL
;
1302 struct isl_vec
*bound
= NULL
;
1303 unsigned total
= isl_basic_map_total_dim(info
[i
].bmap
);
1305 if (count(info
[i
].eq
, 2 * info
[i
].bmap
->n_eq
, STATUS_ADJ_EQ
) != 1)
1306 detect_equalities
= 1;
1308 for (k
= 0; k
< 2 * info
[i
].bmap
->n_eq
; ++k
)
1309 if (info
[i
].eq
[k
] == STATUS_ADJ_EQ
)
1312 set_i
= set_from_updated_bmap(info
[i
].bmap
, info
[i
].tab
);
1313 set_j
= set_from_updated_bmap(info
[j
].bmap
, info
[j
].tab
);
1314 ctx
= isl_basic_map_get_ctx(info
[i
].bmap
);
1315 mat
= isl_mat_alloc(ctx
, 2 * (info
[i
].bmap
->n_eq
+ info
[j
].bmap
->n_eq
) +
1316 info
[i
].bmap
->n_ineq
+ info
[j
].bmap
->n_ineq
,
1318 wraps_init(&wraps
, mat
, info
, i
, j
);
1319 bound
= isl_vec_alloc(ctx
, 1 + total
);
1320 if (!set_i
|| !set_j
|| !wraps
.mat
|| !bound
)
1324 isl_seq_neg(bound
->el
, info
[i
].bmap
->eq
[k
/ 2], 1 + total
);
1326 isl_seq_cpy(bound
->el
, info
[i
].bmap
->eq
[k
/ 2], 1 + total
);
1327 isl_int_add_ui(bound
->el
[0], bound
->el
[0], 1);
1329 isl_seq_cpy(wraps
.mat
->row
[0], bound
->el
, 1 + total
);
1330 wraps
.mat
->n_row
= 1;
1332 if (add_wraps(&wraps
, &info
[j
], bound
->el
, set_i
) < 0)
1334 if (!wraps
.mat
->n_row
)
1337 isl_int_sub_ui(bound
->el
[0], bound
->el
[0], 1);
1338 isl_seq_neg(bound
->el
, bound
->el
, 1 + total
);
1340 isl_seq_cpy(wraps
.mat
->row
[wraps
.mat
->n_row
], bound
->el
, 1 + total
);
1343 if (add_wraps(&wraps
, &info
[i
], bound
->el
, set_j
) < 0)
1345 if (!wraps
.mat
->n_row
)
1348 change
= fuse(i
, j
, info
, wraps
.mat
, detect_equalities
);
1351 error
: change
= isl_change_error
;
1356 isl_set_free(set_i
);
1357 isl_set_free(set_j
);
1358 isl_vec_free(bound
);
1363 /* Check if the union of the given pair of basic maps
1364 * can be represented by a single basic map.
1365 * If so, replace the pair by the single basic map and return
1366 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
1367 * Otherwise, return isl_change_none.
1368 * The two basic maps are assumed to live in the same local space.
1370 * We first check the effect of each constraint of one basic map
1371 * on the other basic map.
1372 * The constraint may be
1373 * redundant the constraint is redundant in its own
1374 * basic map and should be ignore and removed
1376 * valid all (integer) points of the other basic map
1377 * satisfy the constraint
1378 * separate no (integer) point of the other basic map
1379 * satisfies the constraint
1380 * cut some but not all points of the other basic map
1381 * satisfy the constraint
1382 * adj_eq the given constraint is adjacent (on the outside)
1383 * to an equality of the other basic map
1384 * adj_ineq the given constraint is adjacent (on the outside)
1385 * to an inequality of the other basic map
1387 * We consider seven cases in which we can replace the pair by a single
1388 * basic map. We ignore all "redundant" constraints.
1390 * 1. all constraints of one basic map are valid
1391 * => the other basic map is a subset and can be removed
1393 * 2. all constraints of both basic maps are either "valid" or "cut"
1394 * and the facets corresponding to the "cut" constraints
1395 * of one of the basic maps lies entirely inside the other basic map
1396 * => the pair can be replaced by a basic map consisting
1397 * of the valid constraints in both basic maps
1399 * 3. there is a single pair of adjacent inequalities
1400 * (all other constraints are "valid")
1401 * => the pair can be replaced by a basic map consisting
1402 * of the valid constraints in both basic maps
1404 * 4. one basic map has a single adjacent inequality, while the other
1405 * constraints are "valid". The other basic map has some
1406 * "cut" constraints, but replacing the adjacent inequality by
1407 * its opposite and adding the valid constraints of the other
1408 * basic map results in a subset of the other basic map
1409 * => the pair can be replaced by a basic map consisting
1410 * of the valid constraints in both basic maps
1412 * 5. there is a single adjacent pair of an inequality and an equality,
1413 * the other constraints of the basic map containing the inequality are
1414 * "valid". Moreover, if the inequality the basic map is relaxed
1415 * and then turned into an equality, then resulting facet lies
1416 * entirely inside the other basic map
1417 * => the pair can be replaced by the basic map containing
1418 * the inequality, with the inequality relaxed.
1420 * 6. there is a single adjacent pair of an inequality and an equality,
1421 * the other constraints of the basic map containing the inequality are
1422 * "valid". Moreover, the facets corresponding to both
1423 * the inequality and the equality can be wrapped around their
1424 * ridges to include the other basic map
1425 * => the pair can be replaced by a basic map consisting
1426 * of the valid constraints in both basic maps together
1427 * with all wrapping constraints
1429 * 7. one of the basic maps extends beyond the other by at most one.
1430 * Moreover, the facets corresponding to the cut constraints and
1431 * the pieces of the other basic map at offset one from these cut
1432 * constraints can be wrapped around their ridges to include
1433 * the union of the two basic maps
1434 * => the pair can be replaced by a basic map consisting
1435 * of the valid constraints in both basic maps together
1436 * with all wrapping constraints
1438 * 8. the two basic maps live in adjacent hyperplanes. In principle
1439 * such sets can always be combined through wrapping, but we impose
1440 * that there is only one such pair, to avoid overeager coalescing.
1442 * Throughout the computation, we maintain a collection of tableaus
1443 * corresponding to the basic maps. When the basic maps are dropped
1444 * or combined, the tableaus are modified accordingly.
1446 static enum isl_change
coalesce_local_pair(int i
, int j
,
1447 struct isl_coalesce_info
*info
)
1449 enum isl_change change
= isl_change_none
;
1451 info
[i
].eq
= info
[i
].ineq
= NULL
;
1452 info
[j
].eq
= info
[j
].ineq
= NULL
;
1454 info
[i
].eq
= eq_status_in(info
[i
].bmap
, info
[j
].tab
);
1455 if (info
[i
].bmap
->n_eq
&& !info
[i
].eq
)
1457 if (any(info
[i
].eq
, 2 * info
[i
].bmap
->n_eq
, STATUS_ERROR
))
1459 if (any(info
[i
].eq
, 2 * info
[i
].bmap
->n_eq
, STATUS_SEPARATE
))
1462 info
[j
].eq
= eq_status_in(info
[j
].bmap
, info
[i
].tab
);
1463 if (info
[j
].bmap
->n_eq
&& !info
[j
].eq
)
1465 if (any(info
[j
].eq
, 2 * info
[j
].bmap
->n_eq
, STATUS_ERROR
))
1467 if (any(info
[j
].eq
, 2 * info
[j
].bmap
->n_eq
, STATUS_SEPARATE
))
1470 info
[i
].ineq
= ineq_status_in(info
[i
].bmap
, info
[i
].tab
, info
[j
].tab
);
1471 if (info
[i
].bmap
->n_ineq
&& !info
[i
].ineq
)
1473 if (any(info
[i
].ineq
, info
[i
].bmap
->n_ineq
, STATUS_ERROR
))
1475 if (any(info
[i
].ineq
, info
[i
].bmap
->n_ineq
, STATUS_SEPARATE
))
1478 info
[j
].ineq
= ineq_status_in(info
[j
].bmap
, info
[j
].tab
, info
[i
].tab
);
1479 if (info
[j
].bmap
->n_ineq
&& !info
[j
].ineq
)
1481 if (any(info
[j
].ineq
, info
[j
].bmap
->n_ineq
, STATUS_ERROR
))
1483 if (any(info
[j
].ineq
, info
[j
].bmap
->n_ineq
, STATUS_SEPARATE
))
1486 if (all(info
[i
].eq
, 2 * info
[i
].bmap
->n_eq
, STATUS_VALID
) &&
1487 all(info
[i
].ineq
, info
[i
].bmap
->n_ineq
, STATUS_VALID
)) {
1489 change
= isl_change_drop_second
;
1490 } else if (all(info
[j
].eq
, 2 * info
[j
].bmap
->n_eq
, STATUS_VALID
) &&
1491 all(info
[j
].ineq
, info
[j
].bmap
->n_ineq
, STATUS_VALID
)) {
1493 change
= isl_change_drop_first
;
1494 } else if (any(info
[i
].eq
, 2 * info
[i
].bmap
->n_eq
, STATUS_ADJ_EQ
)) {
1495 change
= check_eq_adj_eq(i
, j
, info
);
1496 } else if (any(info
[j
].eq
, 2 * info
[j
].bmap
->n_eq
, STATUS_ADJ_EQ
)) {
1497 change
= check_eq_adj_eq(j
, i
, info
);
1498 } else if (any(info
[i
].eq
, 2 * info
[i
].bmap
->n_eq
, STATUS_ADJ_INEQ
) ||
1499 any(info
[j
].eq
, 2 * info
[j
].bmap
->n_eq
, STATUS_ADJ_INEQ
)) {
1500 change
= check_adj_eq(i
, j
, info
);
1501 } else if (any(info
[i
].ineq
, info
[i
].bmap
->n_ineq
, STATUS_ADJ_EQ
) ||
1502 any(info
[j
].ineq
, info
[j
].bmap
->n_ineq
, STATUS_ADJ_EQ
)) {
1505 } else if (any(info
[i
].ineq
, info
[i
].bmap
->n_ineq
, STATUS_ADJ_INEQ
) ||
1506 any(info
[j
].ineq
, info
[j
].bmap
->n_ineq
, STATUS_ADJ_INEQ
)) {
1507 change
= check_adj_ineq(i
, j
, info
);
1509 if (!any(info
[i
].eq
, 2 * info
[i
].bmap
->n_eq
, STATUS_CUT
) &&
1510 !any(info
[j
].eq
, 2 * info
[j
].bmap
->n_eq
, STATUS_CUT
))
1511 change
= check_facets(i
, j
, info
);
1512 if (change
== isl_change_none
)
1513 change
= check_wrap(i
, j
, info
);
1527 return isl_change_error
;
1530 /* Do the two basic maps live in the same local space, i.e.,
1531 * do they have the same (known) divs?
1532 * If either basic map has any unknown divs, then we can only assume
1533 * that they do not live in the same local space.
1535 static int same_divs(__isl_keep isl_basic_map
*bmap1
,
1536 __isl_keep isl_basic_map
*bmap2
)
1542 if (!bmap1
|| !bmap2
)
1544 if (bmap1
->n_div
!= bmap2
->n_div
)
1547 if (bmap1
->n_div
== 0)
1550 known
= isl_basic_map_divs_known(bmap1
);
1551 if (known
< 0 || !known
)
1553 known
= isl_basic_map_divs_known(bmap2
);
1554 if (known
< 0 || !known
)
1557 total
= isl_basic_map_total_dim(bmap1
);
1558 for (i
= 0; i
< bmap1
->n_div
; ++i
)
1559 if (!isl_seq_eq(bmap1
->div
[i
], bmap2
->div
[i
], 2 + total
))
1565 /* Does "bmap" contain the basic map represented by the tableau "tab"
1566 * after expanding the divs of "bmap" to match those of "tab"?
1567 * The expansion is performed using the divs "div" and expansion "exp"
1568 * computed by the caller.
1569 * Then we check if all constraints of the expanded "bmap" are valid for "tab".
1571 static int contains_with_expanded_divs(__isl_keep isl_basic_map
*bmap
,
1572 struct isl_tab
*tab
, __isl_keep isl_mat
*div
, int *exp
)
1578 bmap
= isl_basic_map_copy(bmap
);
1579 bmap
= isl_basic_set_expand_divs(bmap
, isl_mat_copy(div
), exp
);
1584 eq_i
= eq_status_in(bmap
, tab
);
1585 if (bmap
->n_eq
&& !eq_i
)
1587 if (any(eq_i
, 2 * bmap
->n_eq
, STATUS_ERROR
))
1589 if (any(eq_i
, 2 * bmap
->n_eq
, STATUS_SEPARATE
))
1592 ineq_i
= ineq_status_in(bmap
, NULL
, tab
);
1593 if (bmap
->n_ineq
&& !ineq_i
)
1595 if (any(ineq_i
, bmap
->n_ineq
, STATUS_ERROR
))
1597 if (any(ineq_i
, bmap
->n_ineq
, STATUS_SEPARATE
))
1600 if (all(eq_i
, 2 * bmap
->n_eq
, STATUS_VALID
) &&
1601 all(ineq_i
, bmap
->n_ineq
, STATUS_VALID
))
1605 isl_basic_map_free(bmap
);
1610 isl_basic_map_free(bmap
);
1616 /* Does "bmap_i" contain the basic map represented by "info_j"
1617 * after aligning the divs of "bmap_i" to those of "info_j".
1618 * Note that this can only succeed if the number of divs of "bmap_i"
1619 * is smaller than (or equal to) the number of divs of "info_j".
1621 * We first check if the divs of "bmap_i" are all known and form a subset
1622 * of those of "bmap_j". If so, we pass control over to
1623 * contains_with_expanded_divs.
1625 static int contains_after_aligning_divs(__isl_keep isl_basic_map
*bmap_i
,
1626 struct isl_coalesce_info
*info_j
)
1629 isl_mat
*div_i
, *div_j
, *div
;
1635 known
= isl_basic_map_divs_known(bmap_i
);
1636 if (known
< 0 || !known
)
1639 ctx
= isl_basic_map_get_ctx(bmap_i
);
1641 div_i
= isl_basic_map_get_divs(bmap_i
);
1642 div_j
= isl_basic_map_get_divs(info_j
->bmap
);
1644 if (!div_i
|| !div_j
)
1647 exp1
= isl_alloc_array(ctx
, int, div_i
->n_row
);
1648 exp2
= isl_alloc_array(ctx
, int, div_j
->n_row
);
1649 if ((div_i
->n_row
&& !exp1
) || (div_j
->n_row
&& !exp2
))
1652 div
= isl_merge_divs(div_i
, div_j
, exp1
, exp2
);
1656 if (div
->n_row
== div_j
->n_row
)
1657 subset
= contains_with_expanded_divs(bmap_i
,
1658 info_j
->tab
, div
, exp1
);
1664 isl_mat_free(div_i
);
1665 isl_mat_free(div_j
);
1672 isl_mat_free(div_i
);
1673 isl_mat_free(div_j
);
1679 /* Check if the basic map "j" is a subset of basic map "i",
1680 * if "i" has fewer divs that "j".
1681 * If so, remove basic map "j".
1683 * If the two basic maps have the same number of divs, then
1684 * they must necessarily be different. Otherwise, we would have
1685 * called coalesce_local_pair. We therefore don't try anything
1688 static int coalesced_subset(int i
, int j
, struct isl_coalesce_info
*info
)
1692 if (info
[i
].bmap
->n_div
>= info
[j
].bmap
->n_div
)
1695 superset
= contains_after_aligning_divs(info
[i
].bmap
, &info
[j
]);
1704 /* Check if one of the basic maps is a subset of the other and, if so,
1706 * Note that we only perform any test if the number of divs is different
1707 * in the two basic maps. In case the number of divs is the same,
1708 * we have already established that the divs are different
1709 * in the two basic maps.
1710 * In particular, if the number of divs of basic map i is smaller than
1711 * the number of divs of basic map j, then we check if j is a subset of i
1714 static enum isl_change
check_coalesce_subset(int i
, int j
,
1715 struct isl_coalesce_info
*info
)
1719 changed
= coalesced_subset(i
, j
, info
);
1720 if (changed
< 0 || changed
)
1721 return changed
< 0 ? isl_change_error
: isl_change_drop_second
;
1723 changed
= coalesced_subset(j
, i
, info
);
1724 if (changed
< 0 || changed
)
1725 return changed
< 0 ? isl_change_error
: isl_change_drop_first
;
1727 return isl_change_none
;
1730 /* Check if the union of the given pair of basic maps
1731 * can be represented by a single basic map.
1732 * If so, replace the pair by the single basic map and return
1733 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
1734 * Otherwise, return isl_change_none.
1736 * We first check if the two basic maps live in the same local space.
1737 * If so, we do the complete check. Otherwise, we check if one is
1738 * an obvious subset of the other.
1740 static enum isl_change
coalesce_pair(int i
, int j
,
1741 struct isl_coalesce_info
*info
)
1745 same
= same_divs(info
[i
].bmap
, info
[j
].bmap
);
1747 return isl_change_error
;
1749 return coalesce_local_pair(i
, j
, info
);
1751 return check_coalesce_subset(i
, j
, info
);
1754 /* Pairwise coalesce the basic maps described by the "n" elements of "info",
1755 * skipping basic maps that have been removed (either before or within
1758 * For each basic map i, we check if it can be coalesced with respect
1759 * to any previously considered basic map j.
1760 * If i gets dropped (because it was a subset of some j), then
1761 * we can move on to the next basic map.
1762 * If j gets dropped, we need to continue checking against the other
1763 * previously considered basic maps.
1764 * If the two basic maps got fused, then we recheck the fused basic map
1765 * against the previously considered basic maps.
1767 static int coalesce(isl_ctx
*ctx
, int n
, struct isl_coalesce_info
*info
)
1771 for (i
= n
- 2; i
>= 0; --i
) {
1772 if (info
[i
].removed
)
1774 for (j
= i
+ 1; j
< n
; ++j
) {
1775 enum isl_change changed
;
1777 if (info
[j
].removed
)
1779 if (info
[i
].removed
)
1780 isl_die(ctx
, isl_error_internal
,
1781 "basic map unexpectedly removed",
1783 changed
= coalesce_pair(i
, j
, info
);
1785 case isl_change_error
:
1787 case isl_change_none
:
1788 case isl_change_drop_second
:
1790 case isl_change_drop_first
:
1793 case isl_change_fuse
:
1803 /* Update the basic maps in "map" based on the information in "info".
1804 * In particular, remove the basic maps that have been marked removed and
1805 * update the others based on the information in the corresponding tableau.
1806 * Since we detected implicit equalities without calling
1807 * isl_basic_map_gauss, we need to do it now.
1809 static __isl_give isl_map
*update_basic_maps(__isl_take isl_map
*map
,
1810 int n
, struct isl_coalesce_info
*info
)
1817 for (i
= n
- 1; i
>= 0; --i
) {
1818 if (info
[i
].removed
) {
1819 isl_basic_map_free(map
->p
[i
]);
1820 if (i
!= map
->n
- 1)
1821 map
->p
[i
] = map
->p
[map
->n
- 1];
1826 info
[i
].bmap
= isl_basic_map_update_from_tab(info
[i
].bmap
,
1828 info
[i
].bmap
= isl_basic_map_gauss(info
[i
].bmap
, NULL
);
1829 info
[i
].bmap
= isl_basic_map_finalize(info
[i
].bmap
);
1831 return isl_map_free(map
);
1832 ISL_F_SET(info
[i
].bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
1833 ISL_F_SET(info
[i
].bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1834 isl_basic_map_free(map
->p
[i
]);
1835 map
->p
[i
] = info
[i
].bmap
;
1836 info
[i
].bmap
= NULL
;
1842 /* For each pair of basic maps in the map, check if the union of the two
1843 * can be represented by a single basic map.
1844 * If so, replace the pair by the single basic map and start over.
1846 * Since we are constructing the tableaus of the basic maps anyway,
1847 * we exploit them to detect implicit equalities and redundant constraints.
1848 * This also helps the coalescing as it can ignore the redundant constraints.
1849 * In order to avoid confusion, we make all implicit equalities explicit
1850 * in the basic maps. We don't call isl_basic_map_gauss, though,
1851 * as that may affect the number of constraints.
1852 * This means that we have to call isl_basic_map_gauss at the end
1853 * of the computation (in update_basic_maps) to ensure that
1854 * the basic maps are not left in an unexpected state.
1856 struct isl_map
*isl_map_coalesce(struct isl_map
*map
)
1861 struct isl_coalesce_info
*info
= NULL
;
1863 map
= isl_map_remove_empty_parts(map
);
1870 ctx
= isl_map_get_ctx(map
);
1871 map
= isl_map_sort_divs(map
);
1872 map
= isl_map_cow(map
);
1879 info
= isl_calloc_array(map
->ctx
, struct isl_coalesce_info
, n
);
1883 for (i
= 0; i
< map
->n
; ++i
) {
1884 info
[i
].bmap
= isl_basic_map_copy(map
->p
[i
]);
1885 info
[i
].tab
= isl_tab_from_basic_map(info
[i
].bmap
, 0);
1888 if (!ISL_F_ISSET(info
[i
].bmap
, ISL_BASIC_MAP_NO_IMPLICIT
))
1889 if (isl_tab_detect_implicit_equalities(info
[i
].tab
) < 0)
1891 info
[i
].bmap
= isl_tab_make_equalities_explicit(info
[i
].tab
,
1895 if (!ISL_F_ISSET(info
[i
].bmap
, ISL_BASIC_MAP_NO_REDUNDANT
))
1896 if (isl_tab_detect_redundant(info
[i
].tab
) < 0)
1899 for (i
= map
->n
- 1; i
>= 0; --i
)
1900 if (info
[i
].tab
->empty
)
1903 if (coalesce(ctx
, n
, info
) < 0)
1906 map
= update_basic_maps(map
, n
, info
);
1908 clear_coalesce_info(n
, info
);
1912 clear_coalesce_info(n
, info
);
1917 /* For each pair of basic sets in the set, check if the union of the two
1918 * can be represented by a single basic set.
1919 * If so, replace the pair by the single basic set and start over.
1921 struct isl_set
*isl_set_coalesce(struct isl_set
*set
)
1923 return (struct isl_set
*)isl_map_coalesce((struct isl_map
*)set
);