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 /* Is "bmap" defined by a number of (non-redundant) constraints that
276 * is greater than the number of constraints of basic maps i and j combined?
277 * Equalities are counted as two inequalities.
279 static int number_of_constraints_increases(int i
, int j
,
280 struct isl_coalesce_info
*info
,
281 __isl_keep isl_basic_map
*bmap
, struct isl_tab
*tab
)
285 n_old
= 2 * info
[i
].bmap
->n_eq
+ info
[i
].bmap
->n_ineq
;
286 n_old
+= 2 * info
[j
].bmap
->n_eq
+ info
[j
].bmap
->n_ineq
;
288 n_new
= 2 * bmap
->n_eq
;
289 for (k
= 0; k
< bmap
->n_ineq
; ++k
)
290 if (!isl_tab_is_redundant(tab
, bmap
->n_eq
+ k
))
293 return n_new
> n_old
;
296 /* Replace the pair of basic maps i and j by the basic map bounded
297 * by the valid constraints in both basic maps and the constraints
298 * in extra (if not NULL).
299 * Place the fused basic map in the position that is the smallest of i and j.
301 * If "detect_equalities" is set, then look for equalities encoded
302 * as pairs of inequalities.
303 * If "check_number" is set, then the original basic maps are only
304 * replaced if the total number of constraints does not increase.
306 static enum isl_change
fuse(int i
, int j
, struct isl_coalesce_info
*info
,
307 __isl_keep isl_mat
*extra
, int detect_equalities
, int check_number
)
310 struct isl_basic_map
*fused
= NULL
;
311 struct isl_tab
*fused_tab
= NULL
;
312 unsigned total
= isl_basic_map_total_dim(info
[i
].bmap
);
313 unsigned extra_rows
= extra
? extra
->n_row
: 0;
314 unsigned n_eq
, n_ineq
;
317 return fuse(j
, i
, info
, extra
, detect_equalities
, check_number
);
319 n_eq
= info
[i
].bmap
->n_eq
+ info
[j
].bmap
->n_eq
;
320 n_ineq
= info
[i
].bmap
->n_ineq
+ info
[j
].bmap
->n_ineq
;
321 fused
= isl_basic_map_alloc_space(isl_space_copy(info
[i
].bmap
->dim
),
322 info
[i
].bmap
->n_div
, n_eq
, n_eq
+ n_ineq
+ extra_rows
);
323 fused
= add_valid_constraints(fused
, &info
[i
], 1 + total
);
324 fused
= add_valid_constraints(fused
, &info
[j
], 1 + total
);
328 for (k
= 0; k
< info
[i
].bmap
->n_div
; ++k
) {
329 int l
= isl_basic_map_alloc_div(fused
);
332 isl_seq_cpy(fused
->div
[l
], info
[i
].bmap
->div
[k
], 1 + 1 + total
);
335 for (k
= 0; k
< extra_rows
; ++k
) {
336 l
= isl_basic_map_alloc_inequality(fused
);
339 isl_seq_cpy(fused
->ineq
[l
], extra
->row
[k
], 1 + total
);
342 if (detect_equalities
)
343 fused
= isl_basic_map_detect_inequality_pairs(fused
, NULL
);
344 fused
= isl_basic_map_gauss(fused
, NULL
);
345 ISL_F_SET(fused
, ISL_BASIC_MAP_FINAL
);
346 if (ISL_F_ISSET(info
[i
].bmap
, ISL_BASIC_MAP_RATIONAL
) &&
347 ISL_F_ISSET(info
[j
].bmap
, ISL_BASIC_MAP_RATIONAL
))
348 ISL_F_SET(fused
, ISL_BASIC_MAP_RATIONAL
);
350 fused_tab
= isl_tab_from_basic_map(fused
, 0);
351 if (isl_tab_detect_redundant(fused_tab
) < 0)
355 number_of_constraints_increases(i
, j
, info
, fused
, fused_tab
)) {
356 isl_tab_free(fused_tab
);
357 isl_basic_map_free(fused
);
358 return isl_change_none
;
361 isl_basic_map_free(info
[i
].bmap
);
362 info
[i
].bmap
= fused
;
363 isl_tab_free(info
[i
].tab
);
364 info
[i
].tab
= fused_tab
;
367 return isl_change_fuse
;
369 isl_tab_free(fused_tab
);
370 isl_basic_map_free(fused
);
371 return isl_change_error
;
374 /* Given a pair of basic maps i and j such that all constraints are either
375 * "valid" or "cut", check if the facets corresponding to the "cut"
376 * constraints of i lie entirely within basic map j.
377 * If so, replace the pair by the basic map consisting of the valid
378 * constraints in both basic maps.
379 * Checking whether the facet lies entirely within basic map j
380 * is performed by checking whether the constraints of basic map j
381 * are valid for the facet. These tests are performed on a rational
382 * tableau to avoid the theoretical possibility that a constraint
383 * that was considered to be a cut constraint for the entire basic map i
384 * happens to be considered to be a valid constraint for the facet,
385 * even though it cuts off the same rational points.
387 * To see that we are not introducing any extra points, call the
388 * two basic maps A and B and the resulting map U and let x
389 * be an element of U \setminus ( A \cup B ).
390 * A line connecting x with an element of A \cup B meets a facet F
391 * of either A or B. Assume it is a facet of B and let c_1 be
392 * the corresponding facet constraint. We have c_1(x) < 0 and
393 * so c_1 is a cut constraint. This implies that there is some
394 * (possibly rational) point x' satisfying the constraints of A
395 * and the opposite of c_1 as otherwise c_1 would have been marked
396 * valid for A. The line connecting x and x' meets a facet of A
397 * in a (possibly rational) point that also violates c_1, but this
398 * is impossible since all cut constraints of B are valid for all
400 * In case F is a facet of A rather than B, then we can apply the
401 * above reasoning to find a facet of B separating x from A \cup B first.
403 static enum isl_change
check_facets(int i
, int j
,
404 struct isl_coalesce_info
*info
)
407 struct isl_tab_undo
*snap
, *snap2
;
408 unsigned n_eq
= info
[i
].bmap
->n_eq
;
410 snap
= isl_tab_snap(info
[i
].tab
);
411 if (isl_tab_mark_rational(info
[i
].tab
) < 0)
412 return isl_change_error
;
413 snap2
= isl_tab_snap(info
[i
].tab
);
415 for (k
= 0; k
< info
[i
].bmap
->n_ineq
; ++k
) {
416 if (info
[i
].ineq
[k
] != STATUS_CUT
)
418 if (isl_tab_select_facet(info
[i
].tab
, n_eq
+ k
) < 0)
419 return isl_change_error
;
420 for (l
= 0; l
< info
[j
].bmap
->n_ineq
; ++l
) {
422 if (info
[j
].ineq
[l
] != STATUS_CUT
)
424 stat
= status_in(info
[j
].bmap
->ineq
[l
], info
[i
].tab
);
425 if (stat
!= STATUS_VALID
)
428 if (isl_tab_rollback(info
[i
].tab
, snap2
) < 0)
429 return isl_change_error
;
430 if (l
< info
[j
].bmap
->n_ineq
)
434 if (k
< info
[i
].bmap
->n_ineq
) {
435 if (isl_tab_rollback(info
[i
].tab
, snap
) < 0)
436 return isl_change_error
;
437 return isl_change_none
;
439 return fuse(i
, j
, info
, NULL
, 0, 0);
442 /* Check if info->bmap contains the basic map represented
443 * by the tableau "tab".
444 * For each equality, we check both the constraint itself
445 * (as an inequality) and its negation. Make sure the
446 * equality is returned to its original state before returning.
448 static int contains(struct isl_coalesce_info
*info
, struct isl_tab
*tab
)
452 isl_basic_map
*bmap
= info
->bmap
;
454 dim
= isl_basic_map_total_dim(bmap
);
455 for (k
= 0; k
< bmap
->n_eq
; ++k
) {
457 isl_seq_neg(bmap
->eq
[k
], bmap
->eq
[k
], 1 + dim
);
458 stat
= status_in(bmap
->eq
[k
], tab
);
459 isl_seq_neg(bmap
->eq
[k
], bmap
->eq
[k
], 1 + dim
);
460 if (stat
!= STATUS_VALID
)
462 stat
= status_in(bmap
->eq
[k
], tab
);
463 if (stat
!= STATUS_VALID
)
467 for (k
= 0; k
< bmap
->n_ineq
; ++k
) {
469 if (info
->ineq
[k
] == STATUS_REDUNDANT
)
471 stat
= status_in(bmap
->ineq
[k
], tab
);
472 if (stat
!= STATUS_VALID
)
478 /* Basic map "i" has an inequality (say "k") that is adjacent
479 * to some inequality of basic map "j". All the other inequalities
481 * Check if basic map "j" forms an extension of basic map "i".
483 * Note that this function is only called if some of the equalities or
484 * inequalities of basic map "j" do cut basic map "i". The function is
485 * correct even if there are no such cut constraints, but in that case
486 * the additional checks performed by this function are overkill.
488 * In particular, we replace constraint k, say f >= 0, by constraint
489 * f <= -1, add the inequalities of "j" that are valid for "i"
490 * and check if the result is a subset of basic map "j".
491 * If so, then we know that this result is exactly equal to basic map "j"
492 * since all its constraints are valid for basic map "j".
493 * By combining the valid constraints of "i" (all equalities and all
494 * inequalities except "k") and the valid constraints of "j" we therefore
495 * obtain a basic map that is equal to their union.
496 * In this case, there is no need to perform a rollback of the tableau
497 * since it is going to be destroyed in fuse().
503 * |_______| _ |_________\
515 static enum isl_change
is_adj_ineq_extension(int i
, int j
,
516 struct isl_coalesce_info
*info
)
519 struct isl_tab_undo
*snap
;
520 unsigned n_eq
= info
[i
].bmap
->n_eq
;
521 unsigned total
= isl_basic_map_total_dim(info
[i
].bmap
);
524 if (isl_tab_extend_cons(info
[i
].tab
, 1 + info
[j
].bmap
->n_ineq
) < 0)
525 return isl_change_error
;
527 for (k
= 0; k
< info
[i
].bmap
->n_ineq
; ++k
)
528 if (info
[i
].ineq
[k
] == STATUS_ADJ_INEQ
)
530 if (k
>= info
[i
].bmap
->n_ineq
)
531 isl_die(isl_basic_map_get_ctx(info
[i
].bmap
), isl_error_internal
,
532 "info[i].ineq should have exactly one STATUS_ADJ_INEQ",
533 return isl_change_error
);
535 snap
= isl_tab_snap(info
[i
].tab
);
537 if (isl_tab_unrestrict(info
[i
].tab
, n_eq
+ k
) < 0)
538 return isl_change_error
;
540 isl_seq_neg(info
[i
].bmap
->ineq
[k
], info
[i
].bmap
->ineq
[k
], 1 + total
);
541 isl_int_sub_ui(info
[i
].bmap
->ineq
[k
][0], info
[i
].bmap
->ineq
[k
][0], 1);
542 r
= isl_tab_add_ineq(info
[i
].tab
, info
[i
].bmap
->ineq
[k
]);
543 isl_seq_neg(info
[i
].bmap
->ineq
[k
], info
[i
].bmap
->ineq
[k
], 1 + total
);
544 isl_int_sub_ui(info
[i
].bmap
->ineq
[k
][0], info
[i
].bmap
->ineq
[k
][0], 1);
546 return isl_change_error
;
548 for (k
= 0; k
< info
[j
].bmap
->n_ineq
; ++k
) {
549 if (info
[j
].ineq
[k
] != STATUS_VALID
)
551 if (isl_tab_add_ineq(info
[i
].tab
, info
[j
].bmap
->ineq
[k
]) < 0)
552 return isl_change_error
;
555 if (contains(&info
[j
], info
[i
].tab
))
556 return fuse(i
, j
, info
, NULL
, 0, 0);
558 if (isl_tab_rollback(info
[i
].tab
, snap
) < 0)
559 return isl_change_error
;
561 return isl_change_none
;
565 /* Both basic maps have at least one inequality with and adjacent
566 * (but opposite) inequality in the other basic map.
567 * Check that there are no cut constraints and that there is only
568 * a single pair of adjacent inequalities.
569 * If so, we can replace the pair by a single basic map described
570 * by all but the pair of adjacent inequalities.
571 * Any additional points introduced lie strictly between the two
572 * adjacent hyperplanes and can therefore be integral.
581 * The test for a single pair of adjancent inequalities is important
582 * for avoiding the combination of two basic maps like the following
592 * If there are some cut constraints on one side, then we may
593 * still be able to fuse the two basic maps, but we need to perform
594 * some additional checks in is_adj_ineq_extension.
596 static enum isl_change
check_adj_ineq(int i
, int j
,
597 struct isl_coalesce_info
*info
)
599 int count_i
, count_j
;
602 count_i
= count(info
[i
].ineq
, info
[i
].bmap
->n_ineq
, STATUS_ADJ_INEQ
);
603 count_j
= count(info
[j
].ineq
, info
[j
].bmap
->n_ineq
, STATUS_ADJ_INEQ
);
605 if (count_i
!= 1 && count_j
!= 1)
606 return isl_change_none
;
608 cut_i
= any(info
[i
].eq
, 2 * info
[i
].bmap
->n_eq
, STATUS_CUT
) ||
609 any(info
[i
].ineq
, info
[i
].bmap
->n_ineq
, STATUS_CUT
);
610 cut_j
= any(info
[j
].eq
, 2 * info
[j
].bmap
->n_eq
, STATUS_CUT
) ||
611 any(info
[j
].ineq
, info
[j
].bmap
->n_ineq
, STATUS_CUT
);
613 if (!cut_i
&& !cut_j
&& count_i
== 1 && count_j
== 1)
614 return fuse(i
, j
, info
, NULL
, 0, 0);
616 if (count_i
== 1 && !cut_i
)
617 return is_adj_ineq_extension(i
, j
, info
);
619 if (count_j
== 1 && !cut_j
)
620 return is_adj_ineq_extension(j
, i
, info
);
622 return isl_change_none
;
625 /* Basic map "i" has an inequality "k" that is adjacent to some equality
626 * of basic map "j". All the other inequalities are valid for "j".
627 * Check if basic map "j" forms an extension of basic map "i".
629 * In particular, we relax constraint "k", compute the corresponding
630 * facet and check whether it is included in the other basic map.
631 * If so, we know that relaxing the constraint extends the basic
632 * map with exactly the other basic map (we already know that this
633 * other basic map is included in the extension, because there
634 * were no "cut" inequalities in "i") and we can replace the
635 * two basic maps by this extension.
636 * Place this extension in the position that is the smallest of i and j.
644 static enum isl_change
is_adj_eq_extension(int i
, int j
, int k
,
645 struct isl_coalesce_info
*info
)
647 int change
= isl_change_none
;
649 struct isl_tab_undo
*snap
, *snap2
;
650 unsigned n_eq
= info
[i
].bmap
->n_eq
;
652 if (isl_tab_is_equality(info
[i
].tab
, n_eq
+ k
))
653 return isl_change_none
;
655 snap
= isl_tab_snap(info
[i
].tab
);
656 if (isl_tab_relax(info
[i
].tab
, n_eq
+ k
) < 0)
657 return isl_change_error
;
658 snap2
= isl_tab_snap(info
[i
].tab
);
659 if (isl_tab_select_facet(info
[i
].tab
, n_eq
+ k
) < 0)
660 return isl_change_error
;
661 super
= contains(&info
[j
], info
[i
].tab
);
663 if (isl_tab_rollback(info
[i
].tab
, snap2
) < 0)
664 return isl_change_error
;
665 info
[i
].bmap
= isl_basic_map_cow(info
[i
].bmap
);
667 return isl_change_error
;
668 isl_int_add_ui(info
[i
].bmap
->ineq
[k
][0],
669 info
[i
].bmap
->ineq
[k
][0], 1);
670 ISL_F_SET(info
[i
].bmap
, ISL_BASIC_MAP_FINAL
);
673 exchange(&info
[i
], &info
[j
]);
674 change
= isl_change_fuse
;
676 if (isl_tab_rollback(info
[i
].tab
, snap
) < 0)
677 return isl_change_error
;
682 /* Data structure that keeps track of the wrapping constraints
683 * and of information to bound the coefficients of those constraints.
685 * bound is set if we want to apply a bound on the coefficients
686 * mat contains the wrapping constraints
687 * max is the bound on the coefficients (if bound is set)
695 /* Update wraps->max to be greater than or equal to the coefficients
696 * in the equalities and inequalities of info->bmap that can be removed
697 * if we end up applying wrapping.
699 static void wraps_update_max(struct isl_wraps
*wraps
,
700 struct isl_coalesce_info
*info
)
704 unsigned total
= isl_basic_map_total_dim(info
->bmap
);
708 for (k
= 0; k
< info
->bmap
->n_eq
; ++k
) {
709 if (info
->eq
[2 * k
] == STATUS_VALID
&&
710 info
->eq
[2 * k
+ 1] == STATUS_VALID
)
712 isl_seq_abs_max(info
->bmap
->eq
[k
] + 1, total
, &max_k
);
713 if (isl_int_abs_gt(max_k
, wraps
->max
))
714 isl_int_set(wraps
->max
, max_k
);
717 for (k
= 0; k
< info
->bmap
->n_ineq
; ++k
) {
718 if (info
->ineq
[k
] == STATUS_VALID
||
719 info
->ineq
[k
] == STATUS_REDUNDANT
)
721 isl_seq_abs_max(info
->bmap
->ineq
[k
] + 1, total
, &max_k
);
722 if (isl_int_abs_gt(max_k
, wraps
->max
))
723 isl_int_set(wraps
->max
, max_k
);
726 isl_int_clear(max_k
);
729 /* Initialize the isl_wraps data structure.
730 * If we want to bound the coefficients of the wrapping constraints,
731 * we set wraps->max to the largest coefficient
732 * in the equalities and inequalities that can be removed if we end up
735 static void wraps_init(struct isl_wraps
*wraps
, __isl_take isl_mat
*mat
,
736 struct isl_coalesce_info
*info
, int i
, int j
)
744 ctx
= isl_mat_get_ctx(mat
);
745 wraps
->bound
= isl_options_get_coalesce_bounded_wrapping(ctx
);
748 isl_int_init(wraps
->max
);
749 isl_int_set_si(wraps
->max
, 0);
750 wraps_update_max(wraps
, &info
[i
]);
751 wraps_update_max(wraps
, &info
[j
]);
754 /* Free the contents of the isl_wraps data structure.
756 static void wraps_free(struct isl_wraps
*wraps
)
758 isl_mat_free(wraps
->mat
);
760 isl_int_clear(wraps
->max
);
763 /* Is the wrapping constraint in row "row" allowed?
765 * If wraps->bound is set, we check that none of the coefficients
766 * is greater than wraps->max.
768 static int allow_wrap(struct isl_wraps
*wraps
, int row
)
775 for (i
= 1; i
< wraps
->mat
->n_col
; ++i
)
776 if (isl_int_abs_gt(wraps
->mat
->row
[row
][i
], wraps
->max
))
782 /* Wrap "ineq" (or its opposite if "negate" is set) around "bound"
783 * to include "set" and add the result in position "w" of "wraps".
784 * "len" is the total number of coefficients in "bound" and "ineq".
785 * Return 1 on success, 0 on failure and -1 on error.
786 * Wrapping can fail if the result of wrapping is equal to "bound"
787 * or if we want to bound the sizes of the coefficients and
788 * the wrapped constraint does not satisfy this bound.
790 static int add_wrap(struct isl_wraps
*wraps
, int w
, isl_int
*bound
,
791 isl_int
*ineq
, unsigned len
, __isl_keep isl_set
*set
, int negate
)
793 isl_seq_cpy(wraps
->mat
->row
[w
], bound
, len
);
795 isl_seq_neg(wraps
->mat
->row
[w
+ 1], ineq
, len
);
796 ineq
= wraps
->mat
->row
[w
+ 1];
798 if (!isl_set_wrap_facet(set
, wraps
->mat
->row
[w
], ineq
))
800 if (isl_seq_eq(wraps
->mat
->row
[w
], bound
, len
))
802 if (!allow_wrap(wraps
, w
))
807 /* For each constraint in info->bmap that is not redundant (as determined
808 * by info->tab) and that is not a valid constraint for the other basic map,
809 * wrap the constraint around "bound" such that it includes the whole
810 * set "set" and append the resulting constraint to "wraps".
811 * Note that the constraints that are valid for the other basic map
812 * will be added to the combined basic map by default, so there is
813 * no need to wrap them.
814 * The caller wrap_in_facets even relies on this function not wrapping
815 * any constraints that are already valid.
816 * "wraps" is assumed to have been pre-allocated to the appropriate size.
817 * wraps->n_row is the number of actual wrapped constraints that have
819 * If any of the wrapping problems results in a constraint that is
820 * identical to "bound", then this means that "set" is unbounded in such
821 * way that no wrapping is possible. If this happens then wraps->n_row
823 * Similarly, if we want to bound the coefficients of the wrapping
824 * constraints and a newly added wrapping constraint does not
825 * satisfy the bound, then wraps->n_row is also reset to zero.
827 static int add_wraps(struct isl_wraps
*wraps
, struct isl_coalesce_info
*info
,
828 isl_int
*bound
, __isl_keep isl_set
*set
)
833 isl_basic_map
*bmap
= info
->bmap
;
834 unsigned len
= 1 + isl_basic_map_total_dim(bmap
);
836 w
= wraps
->mat
->n_row
;
838 for (l
= 0; l
< bmap
->n_ineq
; ++l
) {
839 if (info
->ineq
[l
] == STATUS_VALID
||
840 info
->ineq
[l
] == STATUS_REDUNDANT
)
842 if (isl_seq_is_neg(bound
, bmap
->ineq
[l
], len
))
844 if (isl_seq_eq(bound
, bmap
->ineq
[l
], len
))
846 if (isl_tab_is_redundant(info
->tab
, bmap
->n_eq
+ l
))
849 added
= add_wrap(wraps
, w
, bound
, bmap
->ineq
[l
], len
, set
, 0);
856 for (l
= 0; l
< bmap
->n_eq
; ++l
) {
857 if (isl_seq_is_neg(bound
, bmap
->eq
[l
], len
))
859 if (isl_seq_eq(bound
, bmap
->eq
[l
], len
))
862 for (m
= 0; m
< 2; ++m
) {
863 if (info
->eq
[2 * l
+ m
] == STATUS_VALID
)
865 added
= add_wrap(wraps
, w
, bound
, bmap
->eq
[l
], len
,
875 wraps
->mat
->n_row
= w
;
878 wraps
->mat
->n_row
= 0;
882 /* Check if the constraints in "wraps" from "first" until the last
883 * are all valid for the basic set represented by "tab".
884 * If not, wraps->n_row is set to zero.
886 static int check_wraps(__isl_keep isl_mat
*wraps
, int first
,
891 for (i
= first
; i
< wraps
->n_row
; ++i
) {
892 enum isl_ineq_type type
;
893 type
= isl_tab_ineq_type(tab
, wraps
->row
[i
]);
894 if (type
== isl_ineq_error
)
896 if (type
== isl_ineq_redundant
)
905 /* Return a set that corresponds to the non-redundant constraints
906 * (as recorded in tab) of bmap.
908 * It's important to remove the redundant constraints as some
909 * of the other constraints may have been modified after the
910 * constraints were marked redundant.
911 * In particular, a constraint may have been relaxed.
912 * Redundant constraints are ignored when a constraint is relaxed
913 * and should therefore continue to be ignored ever after.
914 * Otherwise, the relaxation might be thwarted by some of
917 * Update the underlying set to ensure that the dimension doesn't change.
918 * Otherwise the integer divisions could get dropped if the tab
919 * turns out to be empty.
921 static __isl_give isl_set
*set_from_updated_bmap(__isl_keep isl_basic_map
*bmap
,
926 bmap
= isl_basic_map_copy(bmap
);
927 bset
= isl_basic_map_underlying_set(bmap
);
928 bset
= isl_basic_set_cow(bset
);
929 bset
= isl_basic_set_update_from_tab(bset
, tab
);
930 return isl_set_from_basic_set(bset
);
933 /* Given a basic set i with a constraint k that is adjacent to
934 * basic set j, check if we can wrap
935 * both the facet corresponding to k and basic map j
936 * around their ridges to include the other set.
937 * If so, replace the pair of basic sets by their union.
939 * All constraints of i (except k) are assumed to be valid for j.
940 * This means that there is no real need to wrap the ridges of
941 * the faces of basic map i around basic map j but since we do,
942 * we have to check that the resulting wrapping constraints are valid for i.
951 static enum isl_change
can_wrap_in_facet(int i
, int j
, int k
,
952 struct isl_coalesce_info
*info
)
954 enum isl_change change
= isl_change_none
;
955 struct isl_wraps wraps
;
958 struct isl_set
*set_i
= NULL
;
959 struct isl_set
*set_j
= NULL
;
960 struct isl_vec
*bound
= NULL
;
961 unsigned total
= isl_basic_map_total_dim(info
[i
].bmap
);
962 struct isl_tab_undo
*snap
;
965 set_i
= set_from_updated_bmap(info
[i
].bmap
, info
[i
].tab
);
966 set_j
= set_from_updated_bmap(info
[j
].bmap
, info
[j
].tab
);
967 ctx
= isl_basic_map_get_ctx(info
[i
].bmap
);
968 mat
= isl_mat_alloc(ctx
, 2 * (info
[i
].bmap
->n_eq
+ info
[j
].bmap
->n_eq
) +
969 info
[i
].bmap
->n_ineq
+ info
[j
].bmap
->n_ineq
,
971 wraps_init(&wraps
, mat
, info
, i
, j
);
972 bound
= isl_vec_alloc(ctx
, 1 + total
);
973 if (!set_i
|| !set_j
|| !wraps
.mat
|| !bound
)
976 isl_seq_cpy(bound
->el
, info
[i
].bmap
->ineq
[k
], 1 + total
);
977 isl_int_add_ui(bound
->el
[0], bound
->el
[0], 1);
979 isl_seq_cpy(wraps
.mat
->row
[0], bound
->el
, 1 + total
);
980 wraps
.mat
->n_row
= 1;
982 if (add_wraps(&wraps
, &info
[j
], bound
->el
, set_i
) < 0)
984 if (!wraps
.mat
->n_row
)
987 snap
= isl_tab_snap(info
[i
].tab
);
989 if (isl_tab_select_facet(info
[i
].tab
, info
[i
].bmap
->n_eq
+ k
) < 0)
991 if (isl_tab_detect_redundant(info
[i
].tab
) < 0)
994 isl_seq_neg(bound
->el
, info
[i
].bmap
->ineq
[k
], 1 + total
);
996 n
= wraps
.mat
->n_row
;
997 if (add_wraps(&wraps
, &info
[i
], bound
->el
, set_j
) < 0)
1000 if (isl_tab_rollback(info
[i
].tab
, snap
) < 0)
1002 if (check_wraps(wraps
.mat
, n
, info
[i
].tab
) < 0)
1004 if (!wraps
.mat
->n_row
)
1007 change
= fuse(i
, j
, info
, wraps
.mat
, 0, 0);
1012 isl_set_free(set_i
);
1013 isl_set_free(set_j
);
1015 isl_vec_free(bound
);
1020 isl_vec_free(bound
);
1021 isl_set_free(set_i
);
1022 isl_set_free(set_j
);
1023 return isl_change_error
;
1026 /* Given a pair of basic maps i and j such that j sticks out
1027 * of i at n cut constraints, each time by at most one,
1028 * try to compute wrapping constraints and replace the two
1029 * basic maps by a single basic map.
1030 * The other constraints of i are assumed to be valid for j.
1032 * For each cut constraint t(x) >= 0 of i, we add the relaxed version
1033 * t(x) + 1 >= 0, along with wrapping constraints for all constraints
1034 * of basic map j that bound the part of basic map j that sticks out
1035 * of the cut constraint.
1036 * In particular, we first intersect basic map j with t(x) + 1 = 0.
1037 * If the result is empty, then t(x) >= 0 was actually a valid constraint
1038 * (with respect to the integer points), so we add t(x) >= 0 instead.
1039 * Otherwise, we wrap the constraints of basic map j that are not
1040 * redundant in this intersection and that are not already valid
1041 * for basic map i over basic map i.
1042 * Note that it is sufficient to wrap the constraints to include
1043 * basic map i, because we will only wrap the constraints that do
1044 * not include basic map i already. The wrapped constraint will
1045 * therefore be more relaxed compared to the original constraint.
1046 * Since the original constraint is valid for basic map j, so is
1047 * the wrapped constraint.
1049 * If any wrapping fails, i.e., if we cannot wrap to touch
1050 * the union, then we give up.
1051 * Otherwise, the pair of basic maps is replaced by their union.
1053 static enum isl_change
wrap_in_facets(int i
, int j
, int *cuts
, int n
,
1054 struct isl_coalesce_info
*info
)
1056 enum isl_change change
= isl_change_none
;
1057 struct isl_wraps wraps
;
1060 isl_set
*set_i
= NULL
;
1061 unsigned total
= isl_basic_map_total_dim(info
[i
].bmap
);
1064 struct isl_tab_undo
*snap
;
1066 if (isl_tab_extend_cons(info
[j
].tab
, 1) < 0)
1069 max_wrap
= 1 + 2 * info
[j
].bmap
->n_eq
+ info
[j
].bmap
->n_ineq
;
1072 set_i
= set_from_updated_bmap(info
[i
].bmap
, info
[i
].tab
);
1073 ctx
= isl_basic_map_get_ctx(info
[i
].bmap
);
1074 mat
= isl_mat_alloc(ctx
, max_wrap
, 1 + total
);
1075 wraps_init(&wraps
, mat
, info
, i
, j
);
1076 if (!set_i
|| !wraps
.mat
)
1079 snap
= isl_tab_snap(info
[j
].tab
);
1081 wraps
.mat
->n_row
= 0;
1083 for (k
= 0; k
< n
; ++k
) {
1084 w
= wraps
.mat
->n_row
++;
1085 isl_seq_cpy(wraps
.mat
->row
[w
],
1086 info
[i
].bmap
->ineq
[cuts
[k
]], 1 + total
);
1087 isl_int_add_ui(wraps
.mat
->row
[w
][0], wraps
.mat
->row
[w
][0], 1);
1088 if (isl_tab_add_eq(info
[j
].tab
, wraps
.mat
->row
[w
]) < 0)
1090 if (isl_tab_detect_redundant(info
[j
].tab
) < 0)
1093 if (info
[j
].tab
->empty
)
1094 isl_int_sub_ui(wraps
.mat
->row
[w
][0],
1095 wraps
.mat
->row
[w
][0], 1);
1096 else if (add_wraps(&wraps
, &info
[j
],
1097 wraps
.mat
->row
[w
], set_i
) < 0)
1100 if (isl_tab_rollback(info
[j
].tab
, snap
) < 0)
1103 if (!wraps
.mat
->n_row
)
1108 change
= fuse(i
, j
, info
, wraps
.mat
, 0, 1);
1111 isl_set_free(set_i
);
1116 isl_set_free(set_i
);
1117 return isl_change_error
;
1120 /* Given two basic sets i and j such that i has no cut equalities,
1121 * check if relaxing all the cut inequalities of i by one turns
1122 * them into valid constraint for j and check if we can wrap in
1123 * the bits that are sticking out.
1124 * If so, replace the pair by their union.
1126 * We first check if all relaxed cut inequalities of i are valid for j
1127 * and then try to wrap in the intersections of the relaxed cut inequalities
1130 * During this wrapping, we consider the points of j that lie at a distance
1131 * of exactly 1 from i. In particular, we ignore the points that lie in
1132 * between this lower-dimensional space and the basic map i.
1133 * We can therefore only apply this to integer maps.
1159 * Wrapping can fail if the result of wrapping one of the facets
1160 * around its edges does not produce any new facet constraint.
1161 * In particular, this happens when we try to wrap in unbounded sets.
1163 * _______________________________________________________________________
1167 * |_| |_________________________________________________________________
1170 * The following is not an acceptable result of coalescing the above two
1171 * sets as it includes extra integer points.
1172 * _______________________________________________________________________
1177 * \______________________________________________________________________
1179 static enum isl_change
can_wrap_in_set(int i
, int j
,
1180 struct isl_coalesce_info
*info
)
1182 enum isl_change change
= isl_change_none
;
1188 if (ISL_F_ISSET(info
[i
].bmap
, ISL_BASIC_MAP_RATIONAL
) ||
1189 ISL_F_ISSET(info
[j
].bmap
, ISL_BASIC_MAP_RATIONAL
))
1190 return isl_change_none
;
1192 n
= count(info
[i
].ineq
, info
[i
].bmap
->n_ineq
, STATUS_CUT
);
1194 return isl_change_none
;
1196 ctx
= isl_basic_map_get_ctx(info
[i
].bmap
);
1197 cuts
= isl_alloc_array(ctx
, int, n
);
1199 return isl_change_error
;
1201 for (k
= 0, m
= 0; m
< n
; ++k
) {
1202 enum isl_ineq_type type
;
1204 if (info
[i
].ineq
[k
] != STATUS_CUT
)
1207 isl_int_add_ui(info
[i
].bmap
->ineq
[k
][0],
1208 info
[i
].bmap
->ineq
[k
][0], 1);
1209 type
= isl_tab_ineq_type(info
[j
].tab
, info
[i
].bmap
->ineq
[k
]);
1210 isl_int_sub_ui(info
[i
].bmap
->ineq
[k
][0],
1211 info
[i
].bmap
->ineq
[k
][0], 1);
1212 if (type
== isl_ineq_error
)
1214 if (type
!= isl_ineq_redundant
)
1221 change
= wrap_in_facets(i
, j
, cuts
, n
, info
);
1228 return isl_change_error
;
1231 /* Check if either i or j has only cut inequalities that can
1232 * be used to wrap in (a facet of) the other basic set.
1233 * if so, replace the pair by their union.
1235 static enum isl_change
check_wrap(int i
, int j
, struct isl_coalesce_info
*info
)
1237 enum isl_change change
= isl_change_none
;
1239 if (!any(info
[i
].eq
, 2 * info
[i
].bmap
->n_eq
, STATUS_CUT
))
1240 change
= can_wrap_in_set(i
, j
, info
);
1241 if (change
!= isl_change_none
)
1244 if (!any(info
[j
].eq
, 2 * info
[j
].bmap
->n_eq
, STATUS_CUT
))
1245 change
= can_wrap_in_set(j
, i
, info
);
1249 /* At least one of the basic maps has an equality that is adjacent
1250 * to inequality. Make sure that only one of the basic maps has
1251 * such an equality and that the other basic map has exactly one
1252 * inequality adjacent to an equality.
1253 * We call the basic map that has the inequality "i" and the basic
1254 * map that has the equality "j".
1255 * If "i" has any "cut" (in)equality, then relaxing the inequality
1256 * by one would not result in a basic map that contains the other
1259 static enum isl_change
check_adj_eq(int i
, int j
,
1260 struct isl_coalesce_info
*info
)
1262 enum isl_change change
= isl_change_none
;
1265 if (any(info
[i
].eq
, 2 * info
[i
].bmap
->n_eq
, STATUS_ADJ_INEQ
) &&
1266 any(info
[j
].eq
, 2 * info
[j
].bmap
->n_eq
, STATUS_ADJ_INEQ
))
1267 /* ADJ EQ TOO MANY */
1268 return isl_change_none
;
1270 if (any(info
[i
].eq
, 2 * info
[i
].bmap
->n_eq
, STATUS_ADJ_INEQ
))
1271 return check_adj_eq(j
, i
, info
);
1273 /* j has an equality adjacent to an inequality in i */
1275 if (any(info
[i
].eq
, 2 * info
[i
].bmap
->n_eq
, STATUS_CUT
))
1276 return isl_change_none
;
1277 if (any(info
[i
].ineq
, info
[i
].bmap
->n_ineq
, STATUS_CUT
))
1279 return isl_change_none
;
1280 if (count(info
[i
].ineq
, info
[i
].bmap
->n_ineq
, STATUS_ADJ_EQ
) != 1 ||
1281 any(info
[j
].ineq
, info
[j
].bmap
->n_ineq
, STATUS_ADJ_EQ
) ||
1282 any(info
[i
].ineq
, info
[i
].bmap
->n_ineq
, STATUS_ADJ_INEQ
) ||
1283 any(info
[j
].ineq
, info
[j
].bmap
->n_ineq
, STATUS_ADJ_INEQ
))
1284 /* ADJ EQ TOO MANY */
1285 return isl_change_none
;
1287 for (k
= 0; k
< info
[i
].bmap
->n_ineq
; ++k
)
1288 if (info
[i
].ineq
[k
] == STATUS_ADJ_EQ
)
1291 change
= is_adj_eq_extension(i
, j
, k
, info
);
1292 if (change
!= isl_change_none
)
1295 change
= can_wrap_in_facet(i
, j
, k
, info
);
1300 /* The two basic maps lie on adjacent hyperplanes. In particular,
1301 * basic map "i" has an equality that lies parallel to basic map "j".
1302 * Check if we can wrap the facets around the parallel hyperplanes
1303 * to include the other set.
1305 * We perform basically the same operations as can_wrap_in_facet,
1306 * except that we don't need to select a facet of one of the sets.
1312 * If there is more than one equality of "i" adjacent to an equality of "j",
1313 * then the result will satisfy one or more equalities that are a linear
1314 * combination of these equalities. These will be encoded as pairs
1315 * of inequalities in the wrapping constraints and need to be made
1318 static enum isl_change
check_eq_adj_eq(int i
, int j
,
1319 struct isl_coalesce_info
*info
)
1322 enum isl_change change
= isl_change_none
;
1323 int detect_equalities
= 0;
1324 struct isl_wraps wraps
;
1327 struct isl_set
*set_i
= NULL
;
1328 struct isl_set
*set_j
= NULL
;
1329 struct isl_vec
*bound
= NULL
;
1330 unsigned total
= isl_basic_map_total_dim(info
[i
].bmap
);
1332 if (count(info
[i
].eq
, 2 * info
[i
].bmap
->n_eq
, STATUS_ADJ_EQ
) != 1)
1333 detect_equalities
= 1;
1335 for (k
= 0; k
< 2 * info
[i
].bmap
->n_eq
; ++k
)
1336 if (info
[i
].eq
[k
] == STATUS_ADJ_EQ
)
1339 set_i
= set_from_updated_bmap(info
[i
].bmap
, info
[i
].tab
);
1340 set_j
= set_from_updated_bmap(info
[j
].bmap
, info
[j
].tab
);
1341 ctx
= isl_basic_map_get_ctx(info
[i
].bmap
);
1342 mat
= isl_mat_alloc(ctx
, 2 * (info
[i
].bmap
->n_eq
+ info
[j
].bmap
->n_eq
) +
1343 info
[i
].bmap
->n_ineq
+ info
[j
].bmap
->n_ineq
,
1345 wraps_init(&wraps
, mat
, info
, i
, j
);
1346 bound
= isl_vec_alloc(ctx
, 1 + total
);
1347 if (!set_i
|| !set_j
|| !wraps
.mat
|| !bound
)
1351 isl_seq_neg(bound
->el
, info
[i
].bmap
->eq
[k
/ 2], 1 + total
);
1353 isl_seq_cpy(bound
->el
, info
[i
].bmap
->eq
[k
/ 2], 1 + total
);
1354 isl_int_add_ui(bound
->el
[0], bound
->el
[0], 1);
1356 isl_seq_cpy(wraps
.mat
->row
[0], bound
->el
, 1 + total
);
1357 wraps
.mat
->n_row
= 1;
1359 if (add_wraps(&wraps
, &info
[j
], bound
->el
, set_i
) < 0)
1361 if (!wraps
.mat
->n_row
)
1364 isl_int_sub_ui(bound
->el
[0], bound
->el
[0], 1);
1365 isl_seq_neg(bound
->el
, bound
->el
, 1 + total
);
1367 isl_seq_cpy(wraps
.mat
->row
[wraps
.mat
->n_row
], bound
->el
, 1 + total
);
1370 if (add_wraps(&wraps
, &info
[i
], bound
->el
, set_j
) < 0)
1372 if (!wraps
.mat
->n_row
)
1375 change
= fuse(i
, j
, info
, wraps
.mat
, detect_equalities
, 0);
1378 error
: change
= isl_change_error
;
1383 isl_set_free(set_i
);
1384 isl_set_free(set_j
);
1385 isl_vec_free(bound
);
1390 /* Check if the union of the given pair of basic maps
1391 * can be represented by a single basic map.
1392 * If so, replace the pair by the single basic map and return
1393 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
1394 * Otherwise, return isl_change_none.
1395 * The two basic maps are assumed to live in the same local space.
1397 * We first check the effect of each constraint of one basic map
1398 * on the other basic map.
1399 * The constraint may be
1400 * redundant the constraint is redundant in its own
1401 * basic map and should be ignore and removed
1403 * valid all (integer) points of the other basic map
1404 * satisfy the constraint
1405 * separate no (integer) point of the other basic map
1406 * satisfies the constraint
1407 * cut some but not all points of the other basic map
1408 * satisfy the constraint
1409 * adj_eq the given constraint is adjacent (on the outside)
1410 * to an equality of the other basic map
1411 * adj_ineq the given constraint is adjacent (on the outside)
1412 * to an inequality of the other basic map
1414 * We consider seven cases in which we can replace the pair by a single
1415 * basic map. We ignore all "redundant" constraints.
1417 * 1. all constraints of one basic map are valid
1418 * => the other basic map is a subset and can be removed
1420 * 2. all constraints of both basic maps are either "valid" or "cut"
1421 * and the facets corresponding to the "cut" constraints
1422 * of one of the basic maps lies entirely inside the other basic map
1423 * => the pair can be replaced by a basic map consisting
1424 * of the valid constraints in both basic maps
1426 * 3. there is a single pair of adjacent inequalities
1427 * (all other constraints are "valid")
1428 * => the pair can be replaced by a basic map consisting
1429 * of the valid constraints in both basic maps
1431 * 4. one basic map has a single adjacent inequality, while the other
1432 * constraints are "valid". The other basic map has some
1433 * "cut" constraints, but replacing the adjacent inequality by
1434 * its opposite and adding the valid constraints of the other
1435 * basic map results in a subset of the other basic map
1436 * => the pair can be replaced by a basic map consisting
1437 * of the valid constraints in both basic maps
1439 * 5. there is a single adjacent pair of an inequality and an equality,
1440 * the other constraints of the basic map containing the inequality are
1441 * "valid". Moreover, if the inequality the basic map is relaxed
1442 * and then turned into an equality, then resulting facet lies
1443 * entirely inside the other basic map
1444 * => the pair can be replaced by the basic map containing
1445 * the inequality, with the inequality relaxed.
1447 * 6. there is a single adjacent pair of an inequality and an equality,
1448 * the other constraints of the basic map containing the inequality are
1449 * "valid". Moreover, the facets corresponding to both
1450 * the inequality and the equality can be wrapped around their
1451 * ridges to include the other basic map
1452 * => the pair can be replaced by a basic map consisting
1453 * of the valid constraints in both basic maps together
1454 * with all wrapping constraints
1456 * 7. one of the basic maps extends beyond the other by at most one.
1457 * Moreover, the facets corresponding to the cut constraints and
1458 * the pieces of the other basic map at offset one from these cut
1459 * constraints can be wrapped around their ridges to include
1460 * the union of the two basic maps
1461 * => the pair can be replaced by a basic map consisting
1462 * of the valid constraints in both basic maps together
1463 * with all wrapping constraints
1465 * 8. the two basic maps live in adjacent hyperplanes. In principle
1466 * such sets can always be combined through wrapping, but we impose
1467 * that there is only one such pair, to avoid overeager coalescing.
1469 * Throughout the computation, we maintain a collection of tableaus
1470 * corresponding to the basic maps. When the basic maps are dropped
1471 * or combined, the tableaus are modified accordingly.
1473 static enum isl_change
coalesce_local_pair(int i
, int j
,
1474 struct isl_coalesce_info
*info
)
1476 enum isl_change change
= isl_change_none
;
1478 info
[i
].eq
= info
[i
].ineq
= NULL
;
1479 info
[j
].eq
= info
[j
].ineq
= NULL
;
1481 info
[i
].eq
= eq_status_in(info
[i
].bmap
, info
[j
].tab
);
1482 if (info
[i
].bmap
->n_eq
&& !info
[i
].eq
)
1484 if (any(info
[i
].eq
, 2 * info
[i
].bmap
->n_eq
, STATUS_ERROR
))
1486 if (any(info
[i
].eq
, 2 * info
[i
].bmap
->n_eq
, STATUS_SEPARATE
))
1489 info
[j
].eq
= eq_status_in(info
[j
].bmap
, info
[i
].tab
);
1490 if (info
[j
].bmap
->n_eq
&& !info
[j
].eq
)
1492 if (any(info
[j
].eq
, 2 * info
[j
].bmap
->n_eq
, STATUS_ERROR
))
1494 if (any(info
[j
].eq
, 2 * info
[j
].bmap
->n_eq
, STATUS_SEPARATE
))
1497 info
[i
].ineq
= ineq_status_in(info
[i
].bmap
, info
[i
].tab
, info
[j
].tab
);
1498 if (info
[i
].bmap
->n_ineq
&& !info
[i
].ineq
)
1500 if (any(info
[i
].ineq
, info
[i
].bmap
->n_ineq
, STATUS_ERROR
))
1502 if (any(info
[i
].ineq
, info
[i
].bmap
->n_ineq
, STATUS_SEPARATE
))
1505 info
[j
].ineq
= ineq_status_in(info
[j
].bmap
, info
[j
].tab
, info
[i
].tab
);
1506 if (info
[j
].bmap
->n_ineq
&& !info
[j
].ineq
)
1508 if (any(info
[j
].ineq
, info
[j
].bmap
->n_ineq
, STATUS_ERROR
))
1510 if (any(info
[j
].ineq
, info
[j
].bmap
->n_ineq
, STATUS_SEPARATE
))
1513 if (all(info
[i
].eq
, 2 * info
[i
].bmap
->n_eq
, STATUS_VALID
) &&
1514 all(info
[i
].ineq
, info
[i
].bmap
->n_ineq
, STATUS_VALID
)) {
1516 change
= isl_change_drop_second
;
1517 } else if (all(info
[j
].eq
, 2 * info
[j
].bmap
->n_eq
, STATUS_VALID
) &&
1518 all(info
[j
].ineq
, info
[j
].bmap
->n_ineq
, STATUS_VALID
)) {
1520 change
= isl_change_drop_first
;
1521 } else if (any(info
[i
].eq
, 2 * info
[i
].bmap
->n_eq
, STATUS_ADJ_EQ
)) {
1522 change
= check_eq_adj_eq(i
, j
, info
);
1523 } else if (any(info
[j
].eq
, 2 * info
[j
].bmap
->n_eq
, STATUS_ADJ_EQ
)) {
1524 change
= check_eq_adj_eq(j
, i
, info
);
1525 } else if (any(info
[i
].eq
, 2 * info
[i
].bmap
->n_eq
, STATUS_ADJ_INEQ
) ||
1526 any(info
[j
].eq
, 2 * info
[j
].bmap
->n_eq
, STATUS_ADJ_INEQ
)) {
1527 change
= check_adj_eq(i
, j
, info
);
1528 } else if (any(info
[i
].ineq
, info
[i
].bmap
->n_ineq
, STATUS_ADJ_EQ
) ||
1529 any(info
[j
].ineq
, info
[j
].bmap
->n_ineq
, STATUS_ADJ_EQ
)) {
1532 } else if (any(info
[i
].ineq
, info
[i
].bmap
->n_ineq
, STATUS_ADJ_INEQ
) ||
1533 any(info
[j
].ineq
, info
[j
].bmap
->n_ineq
, STATUS_ADJ_INEQ
)) {
1534 change
= check_adj_ineq(i
, j
, info
);
1536 if (!any(info
[i
].eq
, 2 * info
[i
].bmap
->n_eq
, STATUS_CUT
) &&
1537 !any(info
[j
].eq
, 2 * info
[j
].bmap
->n_eq
, STATUS_CUT
))
1538 change
= check_facets(i
, j
, info
);
1539 if (change
== isl_change_none
)
1540 change
= check_wrap(i
, j
, info
);
1554 return isl_change_error
;
1557 /* Do the two basic maps live in the same local space, i.e.,
1558 * do they have the same (known) divs?
1559 * If either basic map has any unknown divs, then we can only assume
1560 * that they do not live in the same local space.
1562 static int same_divs(__isl_keep isl_basic_map
*bmap1
,
1563 __isl_keep isl_basic_map
*bmap2
)
1569 if (!bmap1
|| !bmap2
)
1571 if (bmap1
->n_div
!= bmap2
->n_div
)
1574 if (bmap1
->n_div
== 0)
1577 known
= isl_basic_map_divs_known(bmap1
);
1578 if (known
< 0 || !known
)
1580 known
= isl_basic_map_divs_known(bmap2
);
1581 if (known
< 0 || !known
)
1584 total
= isl_basic_map_total_dim(bmap1
);
1585 for (i
= 0; i
< bmap1
->n_div
; ++i
)
1586 if (!isl_seq_eq(bmap1
->div
[i
], bmap2
->div
[i
], 2 + total
))
1592 /* Does "bmap" contain the basic map represented by the tableau "tab"
1593 * after expanding the divs of "bmap" to match those of "tab"?
1594 * The expansion is performed using the divs "div" and expansion "exp"
1595 * computed by the caller.
1596 * Then we check if all constraints of the expanded "bmap" are valid for "tab".
1598 static int contains_with_expanded_divs(__isl_keep isl_basic_map
*bmap
,
1599 struct isl_tab
*tab
, __isl_keep isl_mat
*div
, int *exp
)
1605 bmap
= isl_basic_map_copy(bmap
);
1606 bmap
= isl_basic_set_expand_divs(bmap
, isl_mat_copy(div
), exp
);
1611 eq_i
= eq_status_in(bmap
, tab
);
1612 if (bmap
->n_eq
&& !eq_i
)
1614 if (any(eq_i
, 2 * bmap
->n_eq
, STATUS_ERROR
))
1616 if (any(eq_i
, 2 * bmap
->n_eq
, STATUS_SEPARATE
))
1619 ineq_i
= ineq_status_in(bmap
, NULL
, tab
);
1620 if (bmap
->n_ineq
&& !ineq_i
)
1622 if (any(ineq_i
, bmap
->n_ineq
, STATUS_ERROR
))
1624 if (any(ineq_i
, bmap
->n_ineq
, STATUS_SEPARATE
))
1627 if (all(eq_i
, 2 * bmap
->n_eq
, STATUS_VALID
) &&
1628 all(ineq_i
, bmap
->n_ineq
, STATUS_VALID
))
1632 isl_basic_map_free(bmap
);
1637 isl_basic_map_free(bmap
);
1643 /* Does "bmap_i" contain the basic map represented by "info_j"
1644 * after aligning the divs of "bmap_i" to those of "info_j".
1645 * Note that this can only succeed if the number of divs of "bmap_i"
1646 * is smaller than (or equal to) the number of divs of "info_j".
1648 * We first check if the divs of "bmap_i" are all known and form a subset
1649 * of those of "bmap_j". If so, we pass control over to
1650 * contains_with_expanded_divs.
1652 static int contains_after_aligning_divs(__isl_keep isl_basic_map
*bmap_i
,
1653 struct isl_coalesce_info
*info_j
)
1656 isl_mat
*div_i
, *div_j
, *div
;
1662 known
= isl_basic_map_divs_known(bmap_i
);
1663 if (known
< 0 || !known
)
1666 ctx
= isl_basic_map_get_ctx(bmap_i
);
1668 div_i
= isl_basic_map_get_divs(bmap_i
);
1669 div_j
= isl_basic_map_get_divs(info_j
->bmap
);
1671 if (!div_i
|| !div_j
)
1674 exp1
= isl_alloc_array(ctx
, int, div_i
->n_row
);
1675 exp2
= isl_alloc_array(ctx
, int, div_j
->n_row
);
1676 if ((div_i
->n_row
&& !exp1
) || (div_j
->n_row
&& !exp2
))
1679 div
= isl_merge_divs(div_i
, div_j
, exp1
, exp2
);
1683 if (div
->n_row
== div_j
->n_row
)
1684 subset
= contains_with_expanded_divs(bmap_i
,
1685 info_j
->tab
, div
, exp1
);
1691 isl_mat_free(div_i
);
1692 isl_mat_free(div_j
);
1699 isl_mat_free(div_i
);
1700 isl_mat_free(div_j
);
1706 /* Check if the basic map "j" is a subset of basic map "i",
1707 * if "i" has fewer divs that "j".
1708 * If so, remove basic map "j".
1710 * If the two basic maps have the same number of divs, then
1711 * they must necessarily be different. Otherwise, we would have
1712 * called coalesce_local_pair. We therefore don't try anything
1715 static int coalesced_subset(int i
, int j
, struct isl_coalesce_info
*info
)
1719 if (info
[i
].bmap
->n_div
>= info
[j
].bmap
->n_div
)
1722 superset
= contains_after_aligning_divs(info
[i
].bmap
, &info
[j
]);
1731 /* Check if one of the basic maps is a subset of the other and, if so,
1733 * Note that we only perform any test if the number of divs is different
1734 * in the two basic maps. In case the number of divs is the same,
1735 * we have already established that the divs are different
1736 * in the two basic maps.
1737 * In particular, if the number of divs of basic map i is smaller than
1738 * the number of divs of basic map j, then we check if j is a subset of i
1741 static enum isl_change
check_coalesce_subset(int i
, int j
,
1742 struct isl_coalesce_info
*info
)
1746 changed
= coalesced_subset(i
, j
, info
);
1747 if (changed
< 0 || changed
)
1748 return changed
< 0 ? isl_change_error
: isl_change_drop_second
;
1750 changed
= coalesced_subset(j
, i
, info
);
1751 if (changed
< 0 || changed
)
1752 return changed
< 0 ? isl_change_error
: isl_change_drop_first
;
1754 return isl_change_none
;
1757 /* Check if the union of the given pair of basic maps
1758 * can be represented by a single basic map.
1759 * If so, replace the pair by the single basic map and return
1760 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
1761 * Otherwise, return isl_change_none.
1763 * We first check if the two basic maps live in the same local space.
1764 * If so, we do the complete check. Otherwise, we check if one is
1765 * an obvious subset of the other.
1767 static enum isl_change
coalesce_pair(int i
, int j
,
1768 struct isl_coalesce_info
*info
)
1772 same
= same_divs(info
[i
].bmap
, info
[j
].bmap
);
1774 return isl_change_error
;
1776 return coalesce_local_pair(i
, j
, info
);
1778 return check_coalesce_subset(i
, j
, info
);
1781 /* Pairwise coalesce the basic maps described by the "n" elements of "info",
1782 * skipping basic maps that have been removed (either before or within
1785 * For each basic map i, we check if it can be coalesced with respect
1786 * to any previously considered basic map j.
1787 * If i gets dropped (because it was a subset of some j), then
1788 * we can move on to the next basic map.
1789 * If j gets dropped, we need to continue checking against the other
1790 * previously considered basic maps.
1791 * If the two basic maps got fused, then we recheck the fused basic map
1792 * against the previously considered basic maps.
1794 static int coalesce(isl_ctx
*ctx
, int n
, struct isl_coalesce_info
*info
)
1798 for (i
= n
- 2; i
>= 0; --i
) {
1799 if (info
[i
].removed
)
1801 for (j
= i
+ 1; j
< n
; ++j
) {
1802 enum isl_change changed
;
1804 if (info
[j
].removed
)
1806 if (info
[i
].removed
)
1807 isl_die(ctx
, isl_error_internal
,
1808 "basic map unexpectedly removed",
1810 changed
= coalesce_pair(i
, j
, info
);
1812 case isl_change_error
:
1814 case isl_change_none
:
1815 case isl_change_drop_second
:
1817 case isl_change_drop_first
:
1820 case isl_change_fuse
:
1830 /* Update the basic maps in "map" based on the information in "info".
1831 * In particular, remove the basic maps that have been marked removed and
1832 * update the others based on the information in the corresponding tableau.
1833 * Since we detected implicit equalities without calling
1834 * isl_basic_map_gauss, we need to do it now.
1836 static __isl_give isl_map
*update_basic_maps(__isl_take isl_map
*map
,
1837 int n
, struct isl_coalesce_info
*info
)
1844 for (i
= n
- 1; i
>= 0; --i
) {
1845 if (info
[i
].removed
) {
1846 isl_basic_map_free(map
->p
[i
]);
1847 if (i
!= map
->n
- 1)
1848 map
->p
[i
] = map
->p
[map
->n
- 1];
1853 info
[i
].bmap
= isl_basic_map_update_from_tab(info
[i
].bmap
,
1855 info
[i
].bmap
= isl_basic_map_gauss(info
[i
].bmap
, NULL
);
1856 info
[i
].bmap
= isl_basic_map_finalize(info
[i
].bmap
);
1858 return isl_map_free(map
);
1859 ISL_F_SET(info
[i
].bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
1860 ISL_F_SET(info
[i
].bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1861 isl_basic_map_free(map
->p
[i
]);
1862 map
->p
[i
] = info
[i
].bmap
;
1863 info
[i
].bmap
= NULL
;
1869 /* For each pair of basic maps in the map, check if the union of the two
1870 * can be represented by a single basic map.
1871 * If so, replace the pair by the single basic map and start over.
1873 * Since we are constructing the tableaus of the basic maps anyway,
1874 * we exploit them to detect implicit equalities and redundant constraints.
1875 * This also helps the coalescing as it can ignore the redundant constraints.
1876 * In order to avoid confusion, we make all implicit equalities explicit
1877 * in the basic maps. We don't call isl_basic_map_gauss, though,
1878 * as that may affect the number of constraints.
1879 * This means that we have to call isl_basic_map_gauss at the end
1880 * of the computation (in update_basic_maps) to ensure that
1881 * the basic maps are not left in an unexpected state.
1883 struct isl_map
*isl_map_coalesce(struct isl_map
*map
)
1888 struct isl_coalesce_info
*info
= NULL
;
1890 map
= isl_map_remove_empty_parts(map
);
1897 ctx
= isl_map_get_ctx(map
);
1898 map
= isl_map_sort_divs(map
);
1899 map
= isl_map_cow(map
);
1906 info
= isl_calloc_array(map
->ctx
, struct isl_coalesce_info
, n
);
1910 for (i
= 0; i
< map
->n
; ++i
) {
1911 info
[i
].bmap
= isl_basic_map_copy(map
->p
[i
]);
1912 info
[i
].tab
= isl_tab_from_basic_map(info
[i
].bmap
, 0);
1915 if (!ISL_F_ISSET(info
[i
].bmap
, ISL_BASIC_MAP_NO_IMPLICIT
))
1916 if (isl_tab_detect_implicit_equalities(info
[i
].tab
) < 0)
1918 info
[i
].bmap
= isl_tab_make_equalities_explicit(info
[i
].tab
,
1922 if (!ISL_F_ISSET(info
[i
].bmap
, ISL_BASIC_MAP_NO_REDUNDANT
))
1923 if (isl_tab_detect_redundant(info
[i
].tab
) < 0)
1926 for (i
= map
->n
- 1; i
>= 0; --i
)
1927 if (info
[i
].tab
->empty
)
1930 if (coalesce(ctx
, n
, info
) < 0)
1933 map
= update_basic_maps(map
, n
, info
);
1935 clear_coalesce_info(n
, info
);
1939 clear_coalesce_info(n
, info
);
1944 /* For each pair of basic sets in the set, check if the union of the two
1945 * can be represented by a single basic set.
1946 * If so, replace the pair by the single basic set and start over.
1948 struct isl_set
*isl_set_coalesce(struct isl_set
*set
)
1950 return (struct isl_set
*)isl_map_coalesce((struct isl_map
*)set
);