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 /* Wrap the constraints of info->bmap that bound the facet defined
934 * by inequality "k" around (the opposite of) this inequality to
935 * include "set". "bound" may be used to store the negated inequality.
936 * Since the wrapped constraints are not guaranteed to contain the whole
937 * of info->bmap, we check them in check_wraps.
938 * If any of the wrapped constraints turn out to be invalid, then
939 * check_wraps will reset wrap->n_row to zero.
941 static int add_wraps_around_facet(struct isl_wraps
*wraps
,
942 struct isl_coalesce_info
*info
, int k
, isl_int
*bound
,
943 __isl_keep isl_set
*set
)
945 struct isl_tab_undo
*snap
;
947 unsigned total
= isl_basic_map_total_dim(info
->bmap
);
949 snap
= isl_tab_snap(info
->tab
);
951 if (isl_tab_select_facet(info
->tab
, info
->bmap
->n_eq
+ k
) < 0)
953 if (isl_tab_detect_redundant(info
->tab
) < 0)
956 isl_seq_neg(bound
, info
->bmap
->ineq
[k
], 1 + total
);
958 n
= wraps
->mat
->n_row
;
959 if (add_wraps(wraps
, info
, bound
, set
) < 0)
962 if (isl_tab_rollback(info
->tab
, snap
) < 0)
964 if (check_wraps(wraps
->mat
, n
, info
->tab
) < 0)
970 /* Given a basic set i with a constraint k that is adjacent to
971 * basic set j, check if we can wrap
972 * both the facet corresponding to k (if "wrap_facet" is set) and basic map j
973 * (always) around their ridges to include the other set.
974 * If so, replace the pair of basic sets by their union.
976 * All constraints of i (except k) are assumed to be valid or
977 * cut constraints for j.
978 * Wrapping the cut constraints to include basic map j may result
979 * in constraints that are no longer valid of basic map i
980 * we have to check that the resulting wrapping constraints are valid for i.
981 * If "wrap_facet" is not set, then all constraints of i (except k)
982 * are assumed to be valid for j.
991 static enum isl_change
can_wrap_in_facet(int i
, int j
, int k
,
992 struct isl_coalesce_info
*info
, int wrap_facet
)
994 enum isl_change change
= isl_change_none
;
995 struct isl_wraps wraps
;
998 struct isl_set
*set_i
= NULL
;
999 struct isl_set
*set_j
= NULL
;
1000 struct isl_vec
*bound
= NULL
;
1001 unsigned total
= isl_basic_map_total_dim(info
[i
].bmap
);
1003 set_i
= set_from_updated_bmap(info
[i
].bmap
, info
[i
].tab
);
1004 set_j
= set_from_updated_bmap(info
[j
].bmap
, info
[j
].tab
);
1005 ctx
= isl_basic_map_get_ctx(info
[i
].bmap
);
1006 mat
= isl_mat_alloc(ctx
, 2 * (info
[i
].bmap
->n_eq
+ info
[j
].bmap
->n_eq
) +
1007 info
[i
].bmap
->n_ineq
+ info
[j
].bmap
->n_ineq
,
1009 wraps_init(&wraps
, mat
, info
, i
, j
);
1010 bound
= isl_vec_alloc(ctx
, 1 + total
);
1011 if (!set_i
|| !set_j
|| !wraps
.mat
|| !bound
)
1014 isl_seq_cpy(bound
->el
, info
[i
].bmap
->ineq
[k
], 1 + total
);
1015 isl_int_add_ui(bound
->el
[0], bound
->el
[0], 1);
1017 isl_seq_cpy(wraps
.mat
->row
[0], bound
->el
, 1 + total
);
1018 wraps
.mat
->n_row
= 1;
1020 if (add_wraps(&wraps
, &info
[j
], bound
->el
, set_i
) < 0)
1022 if (!wraps
.mat
->n_row
)
1026 if (add_wraps_around_facet(&wraps
, &info
[i
], k
,
1027 bound
->el
, set_j
) < 0)
1029 if (!wraps
.mat
->n_row
)
1033 change
= fuse(i
, j
, info
, wraps
.mat
, 0, 0);
1038 isl_set_free(set_i
);
1039 isl_set_free(set_j
);
1041 isl_vec_free(bound
);
1046 isl_vec_free(bound
);
1047 isl_set_free(set_i
);
1048 isl_set_free(set_j
);
1049 return isl_change_error
;
1052 /* Given a pair of basic maps i and j such that j sticks out
1053 * of i at n cut constraints, each time by at most one,
1054 * try to compute wrapping constraints and replace the two
1055 * basic maps by a single basic map.
1056 * The other constraints of i are assumed to be valid for j.
1058 * For each cut constraint t(x) >= 0 of i, we add the relaxed version
1059 * t(x) + 1 >= 0, along with wrapping constraints for all constraints
1060 * of basic map j that bound the part of basic map j that sticks out
1061 * of the cut constraint.
1062 * In particular, we first intersect basic map j with t(x) + 1 = 0.
1063 * If the result is empty, then t(x) >= 0 was actually a valid constraint
1064 * (with respect to the integer points), so we add t(x) >= 0 instead.
1065 * Otherwise, we wrap the constraints of basic map j that are not
1066 * redundant in this intersection and that are not already valid
1067 * for basic map i over basic map i.
1068 * Note that it is sufficient to wrap the constraints to include
1069 * basic map i, because we will only wrap the constraints that do
1070 * not include basic map i already. The wrapped constraint will
1071 * therefore be more relaxed compared to the original constraint.
1072 * Since the original constraint is valid for basic map j, so is
1073 * the wrapped constraint.
1075 * If any wrapping fails, i.e., if we cannot wrap to touch
1076 * the union, then we give up.
1077 * Otherwise, the pair of basic maps is replaced by their union.
1079 static enum isl_change
wrap_in_facets(int i
, int j
, int *cuts
, int n
,
1080 struct isl_coalesce_info
*info
)
1082 enum isl_change change
= isl_change_none
;
1083 struct isl_wraps wraps
;
1086 isl_set
*set_i
= NULL
;
1087 unsigned total
= isl_basic_map_total_dim(info
[i
].bmap
);
1090 struct isl_tab_undo
*snap
;
1092 if (isl_tab_extend_cons(info
[j
].tab
, 1) < 0)
1095 max_wrap
= 1 + 2 * info
[j
].bmap
->n_eq
+ info
[j
].bmap
->n_ineq
;
1098 set_i
= set_from_updated_bmap(info
[i
].bmap
, info
[i
].tab
);
1099 ctx
= isl_basic_map_get_ctx(info
[i
].bmap
);
1100 mat
= isl_mat_alloc(ctx
, max_wrap
, 1 + total
);
1101 wraps_init(&wraps
, mat
, info
, i
, j
);
1102 if (!set_i
|| !wraps
.mat
)
1105 snap
= isl_tab_snap(info
[j
].tab
);
1107 wraps
.mat
->n_row
= 0;
1109 for (k
= 0; k
< n
; ++k
) {
1110 w
= wraps
.mat
->n_row
++;
1111 isl_seq_cpy(wraps
.mat
->row
[w
],
1112 info
[i
].bmap
->ineq
[cuts
[k
]], 1 + total
);
1113 isl_int_add_ui(wraps
.mat
->row
[w
][0], wraps
.mat
->row
[w
][0], 1);
1114 if (isl_tab_add_eq(info
[j
].tab
, wraps
.mat
->row
[w
]) < 0)
1116 if (isl_tab_detect_redundant(info
[j
].tab
) < 0)
1119 if (info
[j
].tab
->empty
)
1120 isl_int_sub_ui(wraps
.mat
->row
[w
][0],
1121 wraps
.mat
->row
[w
][0], 1);
1122 else if (add_wraps(&wraps
, &info
[j
],
1123 wraps
.mat
->row
[w
], set_i
) < 0)
1126 if (isl_tab_rollback(info
[j
].tab
, snap
) < 0)
1129 if (!wraps
.mat
->n_row
)
1134 change
= fuse(i
, j
, info
, wraps
.mat
, 0, 1);
1137 isl_set_free(set_i
);
1142 isl_set_free(set_i
);
1143 return isl_change_error
;
1146 /* Given two basic sets i and j such that i has no cut equalities,
1147 * check if relaxing all the cut inequalities of i by one turns
1148 * them into valid constraint for j and check if we can wrap in
1149 * the bits that are sticking out.
1150 * If so, replace the pair by their union.
1152 * We first check if all relaxed cut inequalities of i are valid for j
1153 * and then try to wrap in the intersections of the relaxed cut inequalities
1156 * During this wrapping, we consider the points of j that lie at a distance
1157 * of exactly 1 from i. In particular, we ignore the points that lie in
1158 * between this lower-dimensional space and the basic map i.
1159 * We can therefore only apply this to integer maps.
1185 * Wrapping can fail if the result of wrapping one of the facets
1186 * around its edges does not produce any new facet constraint.
1187 * In particular, this happens when we try to wrap in unbounded sets.
1189 * _______________________________________________________________________
1193 * |_| |_________________________________________________________________
1196 * The following is not an acceptable result of coalescing the above two
1197 * sets as it includes extra integer points.
1198 * _______________________________________________________________________
1203 * \______________________________________________________________________
1205 static enum isl_change
can_wrap_in_set(int i
, int j
,
1206 struct isl_coalesce_info
*info
)
1208 enum isl_change change
= isl_change_none
;
1214 if (ISL_F_ISSET(info
[i
].bmap
, ISL_BASIC_MAP_RATIONAL
) ||
1215 ISL_F_ISSET(info
[j
].bmap
, ISL_BASIC_MAP_RATIONAL
))
1216 return isl_change_none
;
1218 n
= count(info
[i
].ineq
, info
[i
].bmap
->n_ineq
, STATUS_CUT
);
1220 return isl_change_none
;
1222 ctx
= isl_basic_map_get_ctx(info
[i
].bmap
);
1223 cuts
= isl_alloc_array(ctx
, int, n
);
1225 return isl_change_error
;
1227 for (k
= 0, m
= 0; m
< n
; ++k
) {
1228 enum isl_ineq_type type
;
1230 if (info
[i
].ineq
[k
] != STATUS_CUT
)
1233 isl_int_add_ui(info
[i
].bmap
->ineq
[k
][0],
1234 info
[i
].bmap
->ineq
[k
][0], 1);
1235 type
= isl_tab_ineq_type(info
[j
].tab
, info
[i
].bmap
->ineq
[k
]);
1236 isl_int_sub_ui(info
[i
].bmap
->ineq
[k
][0],
1237 info
[i
].bmap
->ineq
[k
][0], 1);
1238 if (type
== isl_ineq_error
)
1240 if (type
!= isl_ineq_redundant
)
1247 change
= wrap_in_facets(i
, j
, cuts
, n
, info
);
1254 return isl_change_error
;
1257 /* Check if either i or j has only cut inequalities that can
1258 * be used to wrap in (a facet of) the other basic set.
1259 * if so, replace the pair by their union.
1261 static enum isl_change
check_wrap(int i
, int j
, struct isl_coalesce_info
*info
)
1263 enum isl_change change
= isl_change_none
;
1265 if (!any(info
[i
].eq
, 2 * info
[i
].bmap
->n_eq
, STATUS_CUT
))
1266 change
= can_wrap_in_set(i
, j
, info
);
1267 if (change
!= isl_change_none
)
1270 if (!any(info
[j
].eq
, 2 * info
[j
].bmap
->n_eq
, STATUS_CUT
))
1271 change
= can_wrap_in_set(j
, i
, info
);
1275 /* At least one of the basic maps has an equality that is adjacent
1276 * to inequality. Make sure that only one of the basic maps has
1277 * such an equality and that the other basic map has exactly one
1278 * inequality adjacent to an equality.
1279 * We call the basic map that has the inequality "i" and the basic
1280 * map that has the equality "j".
1281 * If "i" has any "cut" (in)equality, then relaxing the inequality
1282 * by one would not result in a basic map that contains the other
1283 * basic map. However, it may still be possible to wrap in the other
1286 static enum isl_change
check_adj_eq(int i
, int j
,
1287 struct isl_coalesce_info
*info
)
1289 enum isl_change change
= isl_change_none
;
1293 if (any(info
[i
].eq
, 2 * info
[i
].bmap
->n_eq
, STATUS_ADJ_INEQ
) &&
1294 any(info
[j
].eq
, 2 * info
[j
].bmap
->n_eq
, STATUS_ADJ_INEQ
))
1295 /* ADJ EQ TOO MANY */
1296 return isl_change_none
;
1298 if (any(info
[i
].eq
, 2 * info
[i
].bmap
->n_eq
, STATUS_ADJ_INEQ
))
1299 return check_adj_eq(j
, i
, info
);
1301 /* j has an equality adjacent to an inequality in i */
1303 if (any(info
[i
].eq
, 2 * info
[i
].bmap
->n_eq
, STATUS_CUT
))
1304 return isl_change_none
;
1305 any_cut
= any(info
[i
].ineq
, info
[i
].bmap
->n_ineq
, STATUS_CUT
);
1306 if (count(info
[i
].ineq
, info
[i
].bmap
->n_ineq
, STATUS_ADJ_EQ
) != 1 ||
1307 any(info
[j
].ineq
, info
[j
].bmap
->n_ineq
, STATUS_ADJ_EQ
) ||
1308 any(info
[i
].ineq
, info
[i
].bmap
->n_ineq
, STATUS_ADJ_INEQ
) ||
1309 any(info
[j
].ineq
, info
[j
].bmap
->n_ineq
, STATUS_ADJ_INEQ
))
1310 /* ADJ EQ TOO MANY */
1311 return isl_change_none
;
1313 for (k
= 0; k
< info
[i
].bmap
->n_ineq
; ++k
)
1314 if (info
[i
].ineq
[k
] == STATUS_ADJ_EQ
)
1318 change
= is_adj_eq_extension(i
, j
, k
, info
);
1319 if (change
!= isl_change_none
)
1323 change
= can_wrap_in_facet(i
, j
, k
, info
, any_cut
);
1328 /* The two basic maps lie on adjacent hyperplanes. In particular,
1329 * basic map "i" has an equality that lies parallel to basic map "j".
1330 * Check if we can wrap the facets around the parallel hyperplanes
1331 * to include the other set.
1333 * We perform basically the same operations as can_wrap_in_facet,
1334 * except that we don't need to select a facet of one of the sets.
1340 * If there is more than one equality of "i" adjacent to an equality of "j",
1341 * then the result will satisfy one or more equalities that are a linear
1342 * combination of these equalities. These will be encoded as pairs
1343 * of inequalities in the wrapping constraints and need to be made
1346 static enum isl_change
check_eq_adj_eq(int i
, int j
,
1347 struct isl_coalesce_info
*info
)
1350 enum isl_change change
= isl_change_none
;
1351 int detect_equalities
= 0;
1352 struct isl_wraps wraps
;
1355 struct isl_set
*set_i
= NULL
;
1356 struct isl_set
*set_j
= NULL
;
1357 struct isl_vec
*bound
= NULL
;
1358 unsigned total
= isl_basic_map_total_dim(info
[i
].bmap
);
1360 if (count(info
[i
].eq
, 2 * info
[i
].bmap
->n_eq
, STATUS_ADJ_EQ
) != 1)
1361 detect_equalities
= 1;
1363 for (k
= 0; k
< 2 * info
[i
].bmap
->n_eq
; ++k
)
1364 if (info
[i
].eq
[k
] == STATUS_ADJ_EQ
)
1367 set_i
= set_from_updated_bmap(info
[i
].bmap
, info
[i
].tab
);
1368 set_j
= set_from_updated_bmap(info
[j
].bmap
, info
[j
].tab
);
1369 ctx
= isl_basic_map_get_ctx(info
[i
].bmap
);
1370 mat
= isl_mat_alloc(ctx
, 2 * (info
[i
].bmap
->n_eq
+ info
[j
].bmap
->n_eq
) +
1371 info
[i
].bmap
->n_ineq
+ info
[j
].bmap
->n_ineq
,
1373 wraps_init(&wraps
, mat
, info
, i
, j
);
1374 bound
= isl_vec_alloc(ctx
, 1 + total
);
1375 if (!set_i
|| !set_j
|| !wraps
.mat
|| !bound
)
1379 isl_seq_neg(bound
->el
, info
[i
].bmap
->eq
[k
/ 2], 1 + total
);
1381 isl_seq_cpy(bound
->el
, info
[i
].bmap
->eq
[k
/ 2], 1 + total
);
1382 isl_int_add_ui(bound
->el
[0], bound
->el
[0], 1);
1384 isl_seq_cpy(wraps
.mat
->row
[0], bound
->el
, 1 + total
);
1385 wraps
.mat
->n_row
= 1;
1387 if (add_wraps(&wraps
, &info
[j
], bound
->el
, set_i
) < 0)
1389 if (!wraps
.mat
->n_row
)
1392 isl_int_sub_ui(bound
->el
[0], bound
->el
[0], 1);
1393 isl_seq_neg(bound
->el
, bound
->el
, 1 + total
);
1395 isl_seq_cpy(wraps
.mat
->row
[wraps
.mat
->n_row
], bound
->el
, 1 + total
);
1398 if (add_wraps(&wraps
, &info
[i
], bound
->el
, set_j
) < 0)
1400 if (!wraps
.mat
->n_row
)
1403 change
= fuse(i
, j
, info
, wraps
.mat
, detect_equalities
, 0);
1406 error
: change
= isl_change_error
;
1411 isl_set_free(set_i
);
1412 isl_set_free(set_j
);
1413 isl_vec_free(bound
);
1418 /* Check if the union of the given pair of basic maps
1419 * can be represented by a single basic map.
1420 * If so, replace the pair by the single basic map and return
1421 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
1422 * Otherwise, return isl_change_none.
1423 * The two basic maps are assumed to live in the same local space.
1425 * We first check the effect of each constraint of one basic map
1426 * on the other basic map.
1427 * The constraint may be
1428 * redundant the constraint is redundant in its own
1429 * basic map and should be ignore and removed
1431 * valid all (integer) points of the other basic map
1432 * satisfy the constraint
1433 * separate no (integer) point of the other basic map
1434 * satisfies the constraint
1435 * cut some but not all points of the other basic map
1436 * satisfy the constraint
1437 * adj_eq the given constraint is adjacent (on the outside)
1438 * to an equality of the other basic map
1439 * adj_ineq the given constraint is adjacent (on the outside)
1440 * to an inequality of the other basic map
1442 * We consider seven cases in which we can replace the pair by a single
1443 * basic map. We ignore all "redundant" constraints.
1445 * 1. all constraints of one basic map are valid
1446 * => the other basic map is a subset and can be removed
1448 * 2. all constraints of both basic maps are either "valid" or "cut"
1449 * and the facets corresponding to the "cut" constraints
1450 * of one of the basic maps lies entirely inside the other basic map
1451 * => the pair can be replaced by a basic map consisting
1452 * of the valid constraints in both basic maps
1454 * 3. there is a single pair of adjacent inequalities
1455 * (all other constraints are "valid")
1456 * => the pair can be replaced by a basic map consisting
1457 * of the valid constraints in both basic maps
1459 * 4. one basic map has a single adjacent inequality, while the other
1460 * constraints are "valid". The other basic map has some
1461 * "cut" constraints, but replacing the adjacent inequality by
1462 * its opposite and adding the valid constraints of the other
1463 * basic map results in a subset of the other basic map
1464 * => the pair can be replaced by a basic map consisting
1465 * of the valid constraints in both basic maps
1467 * 5. there is a single adjacent pair of an inequality and an equality,
1468 * the other constraints of the basic map containing the inequality are
1469 * "valid". Moreover, if the inequality the basic map is relaxed
1470 * and then turned into an equality, then resulting facet lies
1471 * entirely inside the other basic map
1472 * => the pair can be replaced by the basic map containing
1473 * the inequality, with the inequality relaxed.
1475 * 6. there is a single adjacent pair of an inequality and an equality,
1476 * the other constraints of the basic map containing the inequality are
1477 * "valid". Moreover, the facets corresponding to both
1478 * the inequality and the equality can be wrapped around their
1479 * ridges to include the other basic map
1480 * => the pair can be replaced by a basic map consisting
1481 * of the valid constraints in both basic maps together
1482 * with all wrapping constraints
1484 * 7. one of the basic maps extends beyond the other by at most one.
1485 * Moreover, the facets corresponding to the cut constraints and
1486 * the pieces of the other basic map at offset one from these cut
1487 * constraints can be wrapped around their ridges to include
1488 * the union of the two basic maps
1489 * => the pair can be replaced by a basic map consisting
1490 * of the valid constraints in both basic maps together
1491 * with all wrapping constraints
1493 * 8. the two basic maps live in adjacent hyperplanes. In principle
1494 * such sets can always be combined through wrapping, but we impose
1495 * that there is only one such pair, to avoid overeager coalescing.
1497 * Throughout the computation, we maintain a collection of tableaus
1498 * corresponding to the basic maps. When the basic maps are dropped
1499 * or combined, the tableaus are modified accordingly.
1501 static enum isl_change
coalesce_local_pair(int i
, int j
,
1502 struct isl_coalesce_info
*info
)
1504 enum isl_change change
= isl_change_none
;
1506 info
[i
].eq
= info
[i
].ineq
= NULL
;
1507 info
[j
].eq
= info
[j
].ineq
= NULL
;
1509 info
[i
].eq
= eq_status_in(info
[i
].bmap
, info
[j
].tab
);
1510 if (info
[i
].bmap
->n_eq
&& !info
[i
].eq
)
1512 if (any(info
[i
].eq
, 2 * info
[i
].bmap
->n_eq
, STATUS_ERROR
))
1514 if (any(info
[i
].eq
, 2 * info
[i
].bmap
->n_eq
, STATUS_SEPARATE
))
1517 info
[j
].eq
= eq_status_in(info
[j
].bmap
, info
[i
].tab
);
1518 if (info
[j
].bmap
->n_eq
&& !info
[j
].eq
)
1520 if (any(info
[j
].eq
, 2 * info
[j
].bmap
->n_eq
, STATUS_ERROR
))
1522 if (any(info
[j
].eq
, 2 * info
[j
].bmap
->n_eq
, STATUS_SEPARATE
))
1525 info
[i
].ineq
= ineq_status_in(info
[i
].bmap
, info
[i
].tab
, info
[j
].tab
);
1526 if (info
[i
].bmap
->n_ineq
&& !info
[i
].ineq
)
1528 if (any(info
[i
].ineq
, info
[i
].bmap
->n_ineq
, STATUS_ERROR
))
1530 if (any(info
[i
].ineq
, info
[i
].bmap
->n_ineq
, STATUS_SEPARATE
))
1533 info
[j
].ineq
= ineq_status_in(info
[j
].bmap
, info
[j
].tab
, info
[i
].tab
);
1534 if (info
[j
].bmap
->n_ineq
&& !info
[j
].ineq
)
1536 if (any(info
[j
].ineq
, info
[j
].bmap
->n_ineq
, STATUS_ERROR
))
1538 if (any(info
[j
].ineq
, info
[j
].bmap
->n_ineq
, STATUS_SEPARATE
))
1541 if (all(info
[i
].eq
, 2 * info
[i
].bmap
->n_eq
, STATUS_VALID
) &&
1542 all(info
[i
].ineq
, info
[i
].bmap
->n_ineq
, STATUS_VALID
)) {
1544 change
= isl_change_drop_second
;
1545 } else if (all(info
[j
].eq
, 2 * info
[j
].bmap
->n_eq
, STATUS_VALID
) &&
1546 all(info
[j
].ineq
, info
[j
].bmap
->n_ineq
, STATUS_VALID
)) {
1548 change
= isl_change_drop_first
;
1549 } else if (any(info
[i
].eq
, 2 * info
[i
].bmap
->n_eq
, STATUS_ADJ_EQ
)) {
1550 change
= check_eq_adj_eq(i
, j
, info
);
1551 } else if (any(info
[j
].eq
, 2 * info
[j
].bmap
->n_eq
, STATUS_ADJ_EQ
)) {
1552 change
= check_eq_adj_eq(j
, i
, info
);
1553 } else if (any(info
[i
].eq
, 2 * info
[i
].bmap
->n_eq
, STATUS_ADJ_INEQ
) ||
1554 any(info
[j
].eq
, 2 * info
[j
].bmap
->n_eq
, STATUS_ADJ_INEQ
)) {
1555 change
= check_adj_eq(i
, j
, info
);
1556 } else if (any(info
[i
].ineq
, info
[i
].bmap
->n_ineq
, STATUS_ADJ_EQ
) ||
1557 any(info
[j
].ineq
, info
[j
].bmap
->n_ineq
, STATUS_ADJ_EQ
)) {
1560 } else if (any(info
[i
].ineq
, info
[i
].bmap
->n_ineq
, STATUS_ADJ_INEQ
) ||
1561 any(info
[j
].ineq
, info
[j
].bmap
->n_ineq
, STATUS_ADJ_INEQ
)) {
1562 change
= check_adj_ineq(i
, j
, info
);
1564 if (!any(info
[i
].eq
, 2 * info
[i
].bmap
->n_eq
, STATUS_CUT
) &&
1565 !any(info
[j
].eq
, 2 * info
[j
].bmap
->n_eq
, STATUS_CUT
))
1566 change
= check_facets(i
, j
, info
);
1567 if (change
== isl_change_none
)
1568 change
= check_wrap(i
, j
, info
);
1582 return isl_change_error
;
1585 /* Do the two basic maps live in the same local space, i.e.,
1586 * do they have the same (known) divs?
1587 * If either basic map has any unknown divs, then we can only assume
1588 * that they do not live in the same local space.
1590 static int same_divs(__isl_keep isl_basic_map
*bmap1
,
1591 __isl_keep isl_basic_map
*bmap2
)
1597 if (!bmap1
|| !bmap2
)
1599 if (bmap1
->n_div
!= bmap2
->n_div
)
1602 if (bmap1
->n_div
== 0)
1605 known
= isl_basic_map_divs_known(bmap1
);
1606 if (known
< 0 || !known
)
1608 known
= isl_basic_map_divs_known(bmap2
);
1609 if (known
< 0 || !known
)
1612 total
= isl_basic_map_total_dim(bmap1
);
1613 for (i
= 0; i
< bmap1
->n_div
; ++i
)
1614 if (!isl_seq_eq(bmap1
->div
[i
], bmap2
->div
[i
], 2 + total
))
1620 /* Does "bmap" contain the basic map represented by the tableau "tab"
1621 * after expanding the divs of "bmap" to match those of "tab"?
1622 * The expansion is performed using the divs "div" and expansion "exp"
1623 * computed by the caller.
1624 * Then we check if all constraints of the expanded "bmap" are valid for "tab".
1626 static int contains_with_expanded_divs(__isl_keep isl_basic_map
*bmap
,
1627 struct isl_tab
*tab
, __isl_keep isl_mat
*div
, int *exp
)
1633 bmap
= isl_basic_map_copy(bmap
);
1634 bmap
= isl_basic_set_expand_divs(bmap
, isl_mat_copy(div
), exp
);
1639 eq_i
= eq_status_in(bmap
, tab
);
1640 if (bmap
->n_eq
&& !eq_i
)
1642 if (any(eq_i
, 2 * bmap
->n_eq
, STATUS_ERROR
))
1644 if (any(eq_i
, 2 * bmap
->n_eq
, STATUS_SEPARATE
))
1647 ineq_i
= ineq_status_in(bmap
, NULL
, tab
);
1648 if (bmap
->n_ineq
&& !ineq_i
)
1650 if (any(ineq_i
, bmap
->n_ineq
, STATUS_ERROR
))
1652 if (any(ineq_i
, bmap
->n_ineq
, STATUS_SEPARATE
))
1655 if (all(eq_i
, 2 * bmap
->n_eq
, STATUS_VALID
) &&
1656 all(ineq_i
, bmap
->n_ineq
, STATUS_VALID
))
1660 isl_basic_map_free(bmap
);
1665 isl_basic_map_free(bmap
);
1671 /* Does "bmap_i" contain the basic map represented by "info_j"
1672 * after aligning the divs of "bmap_i" to those of "info_j".
1673 * Note that this can only succeed if the number of divs of "bmap_i"
1674 * is smaller than (or equal to) the number of divs of "info_j".
1676 * We first check if the divs of "bmap_i" are all known and form a subset
1677 * of those of "bmap_j". If so, we pass control over to
1678 * contains_with_expanded_divs.
1680 static int contains_after_aligning_divs(__isl_keep isl_basic_map
*bmap_i
,
1681 struct isl_coalesce_info
*info_j
)
1684 isl_mat
*div_i
, *div_j
, *div
;
1690 known
= isl_basic_map_divs_known(bmap_i
);
1691 if (known
< 0 || !known
)
1694 ctx
= isl_basic_map_get_ctx(bmap_i
);
1696 div_i
= isl_basic_map_get_divs(bmap_i
);
1697 div_j
= isl_basic_map_get_divs(info_j
->bmap
);
1699 if (!div_i
|| !div_j
)
1702 exp1
= isl_alloc_array(ctx
, int, div_i
->n_row
);
1703 exp2
= isl_alloc_array(ctx
, int, div_j
->n_row
);
1704 if ((div_i
->n_row
&& !exp1
) || (div_j
->n_row
&& !exp2
))
1707 div
= isl_merge_divs(div_i
, div_j
, exp1
, exp2
);
1711 if (div
->n_row
== div_j
->n_row
)
1712 subset
= contains_with_expanded_divs(bmap_i
,
1713 info_j
->tab
, div
, exp1
);
1719 isl_mat_free(div_i
);
1720 isl_mat_free(div_j
);
1727 isl_mat_free(div_i
);
1728 isl_mat_free(div_j
);
1734 /* Check if the basic map "j" is a subset of basic map "i",
1735 * if "i" has fewer divs that "j".
1736 * If so, remove basic map "j".
1738 * If the two basic maps have the same number of divs, then
1739 * they must necessarily be different. Otherwise, we would have
1740 * called coalesce_local_pair. We therefore don't try anything
1743 static int coalesced_subset(int i
, int j
, struct isl_coalesce_info
*info
)
1747 if (info
[i
].bmap
->n_div
>= info
[j
].bmap
->n_div
)
1750 superset
= contains_after_aligning_divs(info
[i
].bmap
, &info
[j
]);
1759 /* Check if basic map "j" is a subset of basic map "i" after
1760 * exploiting the extra equalities of "j" to simplify the divs of "i".
1761 * If so, remove basic map "j".
1763 * If "j" does not have any equalities or if they are the same
1764 * as those of "i", then we cannot exploit them to simplify the divs.
1765 * Similarly, if there are no divs in "i", then they cannot be simplified.
1766 * If, on the other hand, the affine hulls of "i" and "j" do not intersect,
1767 * then "j" cannot be a subset of "i".
1769 * Otherwise, we intersect "i" with the affine hull of "j" and then
1770 * check if "j" is a subset of the result after aligning the divs.
1771 * If so, then "j" is definitely a subset of "i" and can be removed.
1772 * Note that if after intersection with the affine hull of "j".
1773 * "i" still has more divs than "j", then there is no way we can
1774 * align the divs of "i" to those of "j".
1776 static int coalesced_subset_with_equalities(int i
, int j
,
1777 struct isl_coalesce_info
*info
)
1779 isl_basic_map
*hull_i
, *hull_j
, *bmap_i
;
1780 int equal
, empty
, subset
;
1782 if (info
[j
].bmap
->n_eq
== 0)
1784 if (info
[i
].bmap
->n_div
== 0)
1787 hull_i
= isl_basic_map_copy(info
[i
].bmap
);
1788 hull_i
= isl_basic_map_plain_affine_hull(hull_i
);
1789 hull_j
= isl_basic_map_copy(info
[j
].bmap
);
1790 hull_j
= isl_basic_map_plain_affine_hull(hull_j
);
1792 hull_j
= isl_basic_map_intersect(hull_j
, isl_basic_map_copy(hull_i
));
1793 equal
= isl_basic_map_plain_is_equal(hull_i
, hull_j
);
1794 empty
= isl_basic_map_plain_is_empty(hull_j
);
1795 isl_basic_map_free(hull_i
);
1797 if (equal
< 0 || equal
|| empty
< 0 || empty
) {
1798 isl_basic_map_free(hull_j
);
1799 return equal
< 0 || empty
< 0 ? -1 : 0;
1802 bmap_i
= isl_basic_map_copy(info
[i
].bmap
);
1803 bmap_i
= isl_basic_map_intersect(bmap_i
, hull_j
);
1807 if (bmap_i
->n_div
> info
[j
].bmap
->n_div
) {
1808 isl_basic_map_free(bmap_i
);
1812 subset
= contains_after_aligning_divs(bmap_i
, &info
[j
]);
1814 isl_basic_map_free(bmap_i
);
1824 /* Check if one of the basic maps is a subset of the other and, if so,
1826 * Note that we only perform any test if the number of divs is different
1827 * in the two basic maps. In case the number of divs is the same,
1828 * we have already established that the divs are different
1829 * in the two basic maps.
1830 * In particular, if the number of divs of basic map i is smaller than
1831 * the number of divs of basic map j, then we check if j is a subset of i
1834 static enum isl_change
check_coalesce_subset(int i
, int j
,
1835 struct isl_coalesce_info
*info
)
1839 changed
= coalesced_subset(i
, j
, info
);
1840 if (changed
< 0 || changed
)
1841 return changed
< 0 ? isl_change_error
: isl_change_drop_second
;
1843 changed
= coalesced_subset(j
, i
, info
);
1844 if (changed
< 0 || changed
)
1845 return changed
< 0 ? isl_change_error
: isl_change_drop_first
;
1847 changed
= coalesced_subset_with_equalities(i
, j
, info
);
1848 if (changed
< 0 || changed
)
1849 return changed
< 0 ? isl_change_error
: isl_change_drop_second
;
1851 changed
= coalesced_subset_with_equalities(j
, i
, info
);
1852 if (changed
< 0 || changed
)
1853 return changed
< 0 ? isl_change_error
: isl_change_drop_first
;
1855 return isl_change_none
;
1858 /* Check if the union of the given pair of basic maps
1859 * can be represented by a single basic map.
1860 * If so, replace the pair by the single basic map and return
1861 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
1862 * Otherwise, return isl_change_none.
1864 * We first check if the two basic maps live in the same local space.
1865 * If so, we do the complete check. Otherwise, we check if one is
1866 * an obvious subset of the other.
1868 static enum isl_change
coalesce_pair(int i
, int j
,
1869 struct isl_coalesce_info
*info
)
1873 same
= same_divs(info
[i
].bmap
, info
[j
].bmap
);
1875 return isl_change_error
;
1877 return coalesce_local_pair(i
, j
, info
);
1879 return check_coalesce_subset(i
, j
, info
);
1882 /* Pairwise coalesce the basic maps described by the "n" elements of "info",
1883 * skipping basic maps that have been removed (either before or within
1886 * For each basic map i, we check if it can be coalesced with respect
1887 * to any previously considered basic map j.
1888 * If i gets dropped (because it was a subset of some j), then
1889 * we can move on to the next basic map.
1890 * If j gets dropped, we need to continue checking against the other
1891 * previously considered basic maps.
1892 * If the two basic maps got fused, then we recheck the fused basic map
1893 * against the previously considered basic maps.
1895 static int coalesce(isl_ctx
*ctx
, int n
, struct isl_coalesce_info
*info
)
1899 for (i
= n
- 2; i
>= 0; --i
) {
1900 if (info
[i
].removed
)
1902 for (j
= i
+ 1; j
< n
; ++j
) {
1903 enum isl_change changed
;
1905 if (info
[j
].removed
)
1907 if (info
[i
].removed
)
1908 isl_die(ctx
, isl_error_internal
,
1909 "basic map unexpectedly removed",
1911 changed
= coalesce_pair(i
, j
, info
);
1913 case isl_change_error
:
1915 case isl_change_none
:
1916 case isl_change_drop_second
:
1918 case isl_change_drop_first
:
1921 case isl_change_fuse
:
1931 /* Update the basic maps in "map" based on the information in "info".
1932 * In particular, remove the basic maps that have been marked removed and
1933 * update the others based on the information in the corresponding tableau.
1934 * Since we detected implicit equalities without calling
1935 * isl_basic_map_gauss, we need to do it now.
1937 static __isl_give isl_map
*update_basic_maps(__isl_take isl_map
*map
,
1938 int n
, struct isl_coalesce_info
*info
)
1945 for (i
= n
- 1; i
>= 0; --i
) {
1946 if (info
[i
].removed
) {
1947 isl_basic_map_free(map
->p
[i
]);
1948 if (i
!= map
->n
- 1)
1949 map
->p
[i
] = map
->p
[map
->n
- 1];
1954 info
[i
].bmap
= isl_basic_map_update_from_tab(info
[i
].bmap
,
1956 info
[i
].bmap
= isl_basic_map_gauss(info
[i
].bmap
, NULL
);
1957 info
[i
].bmap
= isl_basic_map_finalize(info
[i
].bmap
);
1959 return isl_map_free(map
);
1960 ISL_F_SET(info
[i
].bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
1961 ISL_F_SET(info
[i
].bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1962 isl_basic_map_free(map
->p
[i
]);
1963 map
->p
[i
] = info
[i
].bmap
;
1964 info
[i
].bmap
= NULL
;
1970 /* For each pair of basic maps in the map, check if the union of the two
1971 * can be represented by a single basic map.
1972 * If so, replace the pair by the single basic map and start over.
1974 * Since we are constructing the tableaus of the basic maps anyway,
1975 * we exploit them to detect implicit equalities and redundant constraints.
1976 * This also helps the coalescing as it can ignore the redundant constraints.
1977 * In order to avoid confusion, we make all implicit equalities explicit
1978 * in the basic maps. We don't call isl_basic_map_gauss, though,
1979 * as that may affect the number of constraints.
1980 * This means that we have to call isl_basic_map_gauss at the end
1981 * of the computation (in update_basic_maps) to ensure that
1982 * the basic maps are not left in an unexpected state.
1984 struct isl_map
*isl_map_coalesce(struct isl_map
*map
)
1989 struct isl_coalesce_info
*info
= NULL
;
1991 map
= isl_map_remove_empty_parts(map
);
1998 ctx
= isl_map_get_ctx(map
);
1999 map
= isl_map_sort_divs(map
);
2000 map
= isl_map_cow(map
);
2007 info
= isl_calloc_array(map
->ctx
, struct isl_coalesce_info
, n
);
2011 for (i
= 0; i
< map
->n
; ++i
) {
2012 info
[i
].bmap
= isl_basic_map_copy(map
->p
[i
]);
2013 info
[i
].tab
= isl_tab_from_basic_map(info
[i
].bmap
, 0);
2016 if (!ISL_F_ISSET(info
[i
].bmap
, ISL_BASIC_MAP_NO_IMPLICIT
))
2017 if (isl_tab_detect_implicit_equalities(info
[i
].tab
) < 0)
2019 info
[i
].bmap
= isl_tab_make_equalities_explicit(info
[i
].tab
,
2023 if (!ISL_F_ISSET(info
[i
].bmap
, ISL_BASIC_MAP_NO_REDUNDANT
))
2024 if (isl_tab_detect_redundant(info
[i
].tab
) < 0)
2027 for (i
= map
->n
- 1; i
>= 0; --i
)
2028 if (info
[i
].tab
->empty
)
2031 if (coalesce(ctx
, n
, info
) < 0)
2034 map
= update_basic_maps(map
, n
, info
);
2036 clear_coalesce_info(n
, info
);
2040 clear_coalesce_info(n
, info
);
2045 /* For each pair of basic sets in the set, check if the union of the two
2046 * can be represented by a single basic set.
2047 * If so, replace the pair by the single basic set and start over.
2049 struct isl_set
*isl_set_coalesce(struct isl_set
*set
)
2051 return (struct isl_set
*)isl_map_coalesce((struct isl_map
*)set
);