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>
25 #include <isl_aff_private.h>
27 #define STATUS_ERROR -1
28 #define STATUS_REDUNDANT 1
29 #define STATUS_VALID 2
30 #define STATUS_SEPARATE 3
32 #define STATUS_ADJ_EQ 5
33 #define STATUS_ADJ_INEQ 6
35 static int status_in(isl_int
*ineq
, struct isl_tab
*tab
)
37 enum isl_ineq_type type
= isl_tab_ineq_type(tab
, ineq
);
40 case isl_ineq_error
: return STATUS_ERROR
;
41 case isl_ineq_redundant
: return STATUS_VALID
;
42 case isl_ineq_separate
: return STATUS_SEPARATE
;
43 case isl_ineq_cut
: return STATUS_CUT
;
44 case isl_ineq_adj_eq
: return STATUS_ADJ_EQ
;
45 case isl_ineq_adj_ineq
: return STATUS_ADJ_INEQ
;
49 /* Compute the position of the equalities of basic map "bmap_i"
50 * with respect to the basic map represented by "tab_j".
51 * The resulting array has twice as many entries as the number
52 * of equalities corresponding to the two inequalties to which
53 * each equality corresponds.
55 static int *eq_status_in(__isl_keep isl_basic_map
*bmap_i
,
56 struct isl_tab
*tab_j
)
59 int *eq
= isl_calloc_array(bmap_i
->ctx
, int, 2 * bmap_i
->n_eq
);
65 dim
= isl_basic_map_total_dim(bmap_i
);
66 for (k
= 0; k
< bmap_i
->n_eq
; ++k
) {
67 for (l
= 0; l
< 2; ++l
) {
68 isl_seq_neg(bmap_i
->eq
[k
], bmap_i
->eq
[k
], 1+dim
);
69 eq
[2 * k
+ l
] = status_in(bmap_i
->eq
[k
], tab_j
);
70 if (eq
[2 * k
+ l
] == STATUS_ERROR
)
73 if (eq
[2 * k
] == STATUS_SEPARATE
||
74 eq
[2 * k
+ 1] == STATUS_SEPARATE
)
84 /* Compute the position of the inequalities of basic map "bmap_i"
85 * (also represented by "tab_i", if not NULL) with respect to the basic map
86 * represented by "tab_j".
88 static int *ineq_status_in(__isl_keep isl_basic_map
*bmap_i
,
89 struct isl_tab
*tab_i
, struct isl_tab
*tab_j
)
92 unsigned n_eq
= bmap_i
->n_eq
;
93 int *ineq
= isl_calloc_array(bmap_i
->ctx
, int, bmap_i
->n_ineq
);
98 for (k
= 0; k
< bmap_i
->n_ineq
; ++k
) {
99 if (tab_i
&& isl_tab_is_redundant(tab_i
, n_eq
+ k
)) {
100 ineq
[k
] = STATUS_REDUNDANT
;
103 ineq
[k
] = status_in(bmap_i
->ineq
[k
], tab_j
);
104 if (ineq
[k
] == STATUS_ERROR
)
106 if (ineq
[k
] == STATUS_SEPARATE
)
116 static int any(int *con
, unsigned len
, int status
)
120 for (i
= 0; i
< len
; ++i
)
121 if (con
[i
] == status
)
126 static int count(int *con
, unsigned len
, int status
)
131 for (i
= 0; i
< len
; ++i
)
132 if (con
[i
] == status
)
137 static int all(int *con
, unsigned len
, int status
)
141 for (i
= 0; i
< len
; ++i
) {
142 if (con
[i
] == STATUS_REDUNDANT
)
144 if (con
[i
] != status
)
150 /* Internal information associated to a basic map in a map
151 * that is to be coalesced by isl_map_coalesce.
153 * "bmap" is the basic map itself (or NULL if "removed" is set)
154 * "tab" is the corresponding tableau (or NULL if "removed" is set)
155 * "removed" is set if this basic map has been removed from the map
157 * "eq" and "ineq" are only set if we are currently trying to coalesce
158 * this basic map with another basic map, in which case they represent
159 * the position of the inequalities of this basic map with respect to
160 * the other basic map. The number of elements in the "eq" array
161 * is twice the number of equalities in the "bmap", corresponding
162 * to the two inequalities that make up each equality.
164 struct isl_coalesce_info
{
172 /* Free all the allocated memory in an array
173 * of "n" isl_coalesce_info elements.
175 static void clear_coalesce_info(int n
, struct isl_coalesce_info
*info
)
182 for (i
= 0; i
< n
; ++i
) {
183 isl_basic_map_free(info
[i
].bmap
);
184 isl_tab_free(info
[i
].tab
);
190 /* Drop the basic map represented by "info".
191 * That is, clear the memory associated to the entry and
192 * mark it as having been removed.
194 static void drop(struct isl_coalesce_info
*info
)
196 info
->bmap
= isl_basic_map_free(info
->bmap
);
197 isl_tab_free(info
->tab
);
202 /* Exchange the information in "info1" with that in "info2".
204 static void exchange(struct isl_coalesce_info
*info1
,
205 struct isl_coalesce_info
*info2
)
207 struct isl_coalesce_info info
;
214 /* This type represents the kind of change that has been performed
215 * while trying to coalesce two basic maps.
217 * isl_change_none: nothing was changed
218 * isl_change_drop_first: the first basic map was removed
219 * isl_change_drop_second: the second basic map was removed
220 * isl_change_fuse: the two basic maps were replaced by a new basic map.
223 isl_change_error
= -1,
225 isl_change_drop_first
,
226 isl_change_drop_second
,
230 /* Update "change" based on an interchange of the first and the second
231 * basic map. That is, interchange isl_change_drop_first and
232 * isl_change_drop_second.
234 static enum isl_change
invert_change(enum isl_change change
)
237 case isl_change_error
:
238 return isl_change_error
;
239 case isl_change_none
:
240 return isl_change_none
;
241 case isl_change_drop_first
:
242 return isl_change_drop_second
;
243 case isl_change_drop_second
:
244 return isl_change_drop_first
;
245 case isl_change_fuse
:
246 return isl_change_fuse
;
250 /* Add the valid constraints of the basic map represented by "info"
251 * to "bmap". "len" is the size of the constraints.
252 * If only one of the pair of inequalities that make up an equality
253 * is valid, then add that inequality.
255 static __isl_give isl_basic_map
*add_valid_constraints(
256 __isl_take isl_basic_map
*bmap
, struct isl_coalesce_info
*info
,
264 for (k
= 0; k
< info
->bmap
->n_eq
; ++k
) {
265 if (info
->eq
[2 * k
] == STATUS_VALID
&&
266 info
->eq
[2 * k
+ 1] == STATUS_VALID
) {
267 l
= isl_basic_map_alloc_equality(bmap
);
269 return isl_basic_map_free(bmap
);
270 isl_seq_cpy(bmap
->eq
[l
], info
->bmap
->eq
[k
], len
);
271 } else if (info
->eq
[2 * k
] == STATUS_VALID
) {
272 l
= isl_basic_map_alloc_inequality(bmap
);
274 return isl_basic_map_free(bmap
);
275 isl_seq_neg(bmap
->ineq
[l
], info
->bmap
->eq
[k
], len
);
276 } else if (info
->eq
[2 * k
+ 1] == STATUS_VALID
) {
277 l
= isl_basic_map_alloc_inequality(bmap
);
279 return isl_basic_map_free(bmap
);
280 isl_seq_cpy(bmap
->ineq
[l
], info
->bmap
->eq
[k
], len
);
284 for (k
= 0; k
< info
->bmap
->n_ineq
; ++k
) {
285 if (info
->ineq
[k
] != STATUS_VALID
)
287 l
= isl_basic_map_alloc_inequality(bmap
);
289 return isl_basic_map_free(bmap
);
290 isl_seq_cpy(bmap
->ineq
[l
], info
->bmap
->ineq
[k
], len
);
296 /* Is "bmap" defined by a number of (non-redundant) constraints that
297 * is greater than the number of constraints of basic maps i and j combined?
298 * Equalities are counted as two inequalities.
300 static int number_of_constraints_increases(int i
, int j
,
301 struct isl_coalesce_info
*info
,
302 __isl_keep isl_basic_map
*bmap
, struct isl_tab
*tab
)
306 n_old
= 2 * info
[i
].bmap
->n_eq
+ info
[i
].bmap
->n_ineq
;
307 n_old
+= 2 * info
[j
].bmap
->n_eq
+ info
[j
].bmap
->n_ineq
;
309 n_new
= 2 * bmap
->n_eq
;
310 for (k
= 0; k
< bmap
->n_ineq
; ++k
)
311 if (!isl_tab_is_redundant(tab
, bmap
->n_eq
+ k
))
314 return n_new
> n_old
;
317 /* Replace the pair of basic maps i and j by the basic map bounded
318 * by the valid constraints in both basic maps and the constraints
319 * in extra (if not NULL).
320 * Place the fused basic map in the position that is the smallest of i and j.
322 * If "detect_equalities" is set, then look for equalities encoded
323 * as pairs of inequalities.
324 * If "check_number" is set, then the original basic maps are only
325 * replaced if the total number of constraints does not increase.
327 static enum isl_change
fuse(int i
, int j
, struct isl_coalesce_info
*info
,
328 __isl_keep isl_mat
*extra
, int detect_equalities
, int check_number
)
331 struct isl_basic_map
*fused
= NULL
;
332 struct isl_tab
*fused_tab
= NULL
;
333 unsigned total
= isl_basic_map_total_dim(info
[i
].bmap
);
334 unsigned extra_rows
= extra
? extra
->n_row
: 0;
335 unsigned n_eq
, n_ineq
;
338 return fuse(j
, i
, info
, extra
, detect_equalities
, check_number
);
340 n_eq
= info
[i
].bmap
->n_eq
+ info
[j
].bmap
->n_eq
;
341 n_ineq
= info
[i
].bmap
->n_ineq
+ info
[j
].bmap
->n_ineq
;
342 fused
= isl_basic_map_alloc_space(isl_space_copy(info
[i
].bmap
->dim
),
343 info
[i
].bmap
->n_div
, n_eq
, n_eq
+ n_ineq
+ extra_rows
);
344 fused
= add_valid_constraints(fused
, &info
[i
], 1 + total
);
345 fused
= add_valid_constraints(fused
, &info
[j
], 1 + total
);
349 for (k
= 0; k
< info
[i
].bmap
->n_div
; ++k
) {
350 int l
= isl_basic_map_alloc_div(fused
);
353 isl_seq_cpy(fused
->div
[l
], info
[i
].bmap
->div
[k
], 1 + 1 + total
);
356 for (k
= 0; k
< extra_rows
; ++k
) {
357 l
= isl_basic_map_alloc_inequality(fused
);
360 isl_seq_cpy(fused
->ineq
[l
], extra
->row
[k
], 1 + total
);
363 if (detect_equalities
)
364 fused
= isl_basic_map_detect_inequality_pairs(fused
, NULL
);
365 fused
= isl_basic_map_gauss(fused
, NULL
);
366 ISL_F_SET(fused
, ISL_BASIC_MAP_FINAL
);
367 if (ISL_F_ISSET(info
[i
].bmap
, ISL_BASIC_MAP_RATIONAL
) &&
368 ISL_F_ISSET(info
[j
].bmap
, ISL_BASIC_MAP_RATIONAL
))
369 ISL_F_SET(fused
, ISL_BASIC_MAP_RATIONAL
);
371 fused_tab
= isl_tab_from_basic_map(fused
, 0);
372 if (isl_tab_detect_redundant(fused_tab
) < 0)
376 number_of_constraints_increases(i
, j
, info
, fused
, fused_tab
)) {
377 isl_tab_free(fused_tab
);
378 isl_basic_map_free(fused
);
379 return isl_change_none
;
382 isl_basic_map_free(info
[i
].bmap
);
383 info
[i
].bmap
= fused
;
384 isl_tab_free(info
[i
].tab
);
385 info
[i
].tab
= fused_tab
;
388 return isl_change_fuse
;
390 isl_tab_free(fused_tab
);
391 isl_basic_map_free(fused
);
392 return isl_change_error
;
395 /* Given a pair of basic maps i and j such that all constraints are either
396 * "valid" or "cut", check if the facets corresponding to the "cut"
397 * constraints of i lie entirely within basic map j.
398 * If so, replace the pair by the basic map consisting of the valid
399 * constraints in both basic maps.
400 * Checking whether the facet lies entirely within basic map j
401 * is performed by checking whether the constraints of basic map j
402 * are valid for the facet. These tests are performed on a rational
403 * tableau to avoid the theoretical possibility that a constraint
404 * that was considered to be a cut constraint for the entire basic map i
405 * happens to be considered to be a valid constraint for the facet,
406 * even though it cuts off the same rational points.
408 * To see that we are not introducing any extra points, call the
409 * two basic maps A and B and the resulting map U and let x
410 * be an element of U \setminus ( A \cup B ).
411 * A line connecting x with an element of A \cup B meets a facet F
412 * of either A or B. Assume it is a facet of B and let c_1 be
413 * the corresponding facet constraint. We have c_1(x) < 0 and
414 * so c_1 is a cut constraint. This implies that there is some
415 * (possibly rational) point x' satisfying the constraints of A
416 * and the opposite of c_1 as otherwise c_1 would have been marked
417 * valid for A. The line connecting x and x' meets a facet of A
418 * in a (possibly rational) point that also violates c_1, but this
419 * is impossible since all cut constraints of B are valid for all
421 * In case F is a facet of A rather than B, then we can apply the
422 * above reasoning to find a facet of B separating x from A \cup B first.
424 static enum isl_change
check_facets(int i
, int j
,
425 struct isl_coalesce_info
*info
)
428 struct isl_tab_undo
*snap
, *snap2
;
429 unsigned n_eq
= info
[i
].bmap
->n_eq
;
431 snap
= isl_tab_snap(info
[i
].tab
);
432 if (isl_tab_mark_rational(info
[i
].tab
) < 0)
433 return isl_change_error
;
434 snap2
= isl_tab_snap(info
[i
].tab
);
436 for (k
= 0; k
< info
[i
].bmap
->n_ineq
; ++k
) {
437 if (info
[i
].ineq
[k
] != STATUS_CUT
)
439 if (isl_tab_select_facet(info
[i
].tab
, n_eq
+ k
) < 0)
440 return isl_change_error
;
441 for (l
= 0; l
< info
[j
].bmap
->n_ineq
; ++l
) {
443 if (info
[j
].ineq
[l
] != STATUS_CUT
)
445 stat
= status_in(info
[j
].bmap
->ineq
[l
], info
[i
].tab
);
446 if (stat
!= STATUS_VALID
)
449 if (isl_tab_rollback(info
[i
].tab
, snap2
) < 0)
450 return isl_change_error
;
451 if (l
< info
[j
].bmap
->n_ineq
)
455 if (k
< info
[i
].bmap
->n_ineq
) {
456 if (isl_tab_rollback(info
[i
].tab
, snap
) < 0)
457 return isl_change_error
;
458 return isl_change_none
;
460 return fuse(i
, j
, info
, NULL
, 0, 0);
463 /* Check if info->bmap contains the basic map represented
464 * by the tableau "tab".
465 * For each equality, we check both the constraint itself
466 * (as an inequality) and its negation. Make sure the
467 * equality is returned to its original state before returning.
469 static int contains(struct isl_coalesce_info
*info
, struct isl_tab
*tab
)
473 isl_basic_map
*bmap
= info
->bmap
;
475 dim
= isl_basic_map_total_dim(bmap
);
476 for (k
= 0; k
< bmap
->n_eq
; ++k
) {
478 isl_seq_neg(bmap
->eq
[k
], bmap
->eq
[k
], 1 + dim
);
479 stat
= status_in(bmap
->eq
[k
], tab
);
480 isl_seq_neg(bmap
->eq
[k
], bmap
->eq
[k
], 1 + dim
);
481 if (stat
!= STATUS_VALID
)
483 stat
= status_in(bmap
->eq
[k
], tab
);
484 if (stat
!= STATUS_VALID
)
488 for (k
= 0; k
< bmap
->n_ineq
; ++k
) {
490 if (info
->ineq
[k
] == STATUS_REDUNDANT
)
492 stat
= status_in(bmap
->ineq
[k
], tab
);
493 if (stat
!= STATUS_VALID
)
499 /* Basic map "i" has an inequality (say "k") that is adjacent
500 * to some inequality of basic map "j". All the other inequalities
502 * Check if basic map "j" forms an extension of basic map "i".
504 * Note that this function is only called if some of the equalities or
505 * inequalities of basic map "j" do cut basic map "i". The function is
506 * correct even if there are no such cut constraints, but in that case
507 * the additional checks performed by this function are overkill.
509 * In particular, we replace constraint k, say f >= 0, by constraint
510 * f <= -1, add the inequalities of "j" that are valid for "i"
511 * and check if the result is a subset of basic map "j".
512 * If so, then we know that this result is exactly equal to basic map "j"
513 * since all its constraints are valid for basic map "j".
514 * By combining the valid constraints of "i" (all equalities and all
515 * inequalities except "k") and the valid constraints of "j" we therefore
516 * obtain a basic map that is equal to their union.
517 * In this case, there is no need to perform a rollback of the tableau
518 * since it is going to be destroyed in fuse().
524 * |_______| _ |_________\
536 static enum isl_change
is_adj_ineq_extension(int i
, int j
,
537 struct isl_coalesce_info
*info
)
540 struct isl_tab_undo
*snap
;
541 unsigned n_eq
= info
[i
].bmap
->n_eq
;
542 unsigned total
= isl_basic_map_total_dim(info
[i
].bmap
);
545 if (isl_tab_extend_cons(info
[i
].tab
, 1 + info
[j
].bmap
->n_ineq
) < 0)
546 return isl_change_error
;
548 for (k
= 0; k
< info
[i
].bmap
->n_ineq
; ++k
)
549 if (info
[i
].ineq
[k
] == STATUS_ADJ_INEQ
)
551 if (k
>= info
[i
].bmap
->n_ineq
)
552 isl_die(isl_basic_map_get_ctx(info
[i
].bmap
), isl_error_internal
,
553 "info[i].ineq should have exactly one STATUS_ADJ_INEQ",
554 return isl_change_error
);
556 snap
= isl_tab_snap(info
[i
].tab
);
558 if (isl_tab_unrestrict(info
[i
].tab
, n_eq
+ k
) < 0)
559 return isl_change_error
;
561 isl_seq_neg(info
[i
].bmap
->ineq
[k
], info
[i
].bmap
->ineq
[k
], 1 + total
);
562 isl_int_sub_ui(info
[i
].bmap
->ineq
[k
][0], info
[i
].bmap
->ineq
[k
][0], 1);
563 r
= isl_tab_add_ineq(info
[i
].tab
, info
[i
].bmap
->ineq
[k
]);
564 isl_seq_neg(info
[i
].bmap
->ineq
[k
], info
[i
].bmap
->ineq
[k
], 1 + total
);
565 isl_int_sub_ui(info
[i
].bmap
->ineq
[k
][0], info
[i
].bmap
->ineq
[k
][0], 1);
567 return isl_change_error
;
569 for (k
= 0; k
< info
[j
].bmap
->n_ineq
; ++k
) {
570 if (info
[j
].ineq
[k
] != STATUS_VALID
)
572 if (isl_tab_add_ineq(info
[i
].tab
, info
[j
].bmap
->ineq
[k
]) < 0)
573 return isl_change_error
;
576 if (contains(&info
[j
], info
[i
].tab
))
577 return fuse(i
, j
, info
, NULL
, 0, 0);
579 if (isl_tab_rollback(info
[i
].tab
, snap
) < 0)
580 return isl_change_error
;
582 return isl_change_none
;
586 /* Both basic maps have at least one inequality with and adjacent
587 * (but opposite) inequality in the other basic map.
588 * Check that there are no cut constraints and that there is only
589 * a single pair of adjacent inequalities.
590 * If so, we can replace the pair by a single basic map described
591 * by all but the pair of adjacent inequalities.
592 * Any additional points introduced lie strictly between the two
593 * adjacent hyperplanes and can therefore be integral.
602 * The test for a single pair of adjancent inequalities is important
603 * for avoiding the combination of two basic maps like the following
613 * If there are some cut constraints on one side, then we may
614 * still be able to fuse the two basic maps, but we need to perform
615 * some additional checks in is_adj_ineq_extension.
617 static enum isl_change
check_adj_ineq(int i
, int j
,
618 struct isl_coalesce_info
*info
)
620 int count_i
, count_j
;
623 count_i
= count(info
[i
].ineq
, info
[i
].bmap
->n_ineq
, STATUS_ADJ_INEQ
);
624 count_j
= count(info
[j
].ineq
, info
[j
].bmap
->n_ineq
, STATUS_ADJ_INEQ
);
626 if (count_i
!= 1 && count_j
!= 1)
627 return isl_change_none
;
629 cut_i
= any(info
[i
].eq
, 2 * info
[i
].bmap
->n_eq
, STATUS_CUT
) ||
630 any(info
[i
].ineq
, info
[i
].bmap
->n_ineq
, STATUS_CUT
);
631 cut_j
= any(info
[j
].eq
, 2 * info
[j
].bmap
->n_eq
, STATUS_CUT
) ||
632 any(info
[j
].ineq
, info
[j
].bmap
->n_ineq
, STATUS_CUT
);
634 if (!cut_i
&& !cut_j
&& count_i
== 1 && count_j
== 1)
635 return fuse(i
, j
, info
, NULL
, 0, 0);
637 if (count_i
== 1 && !cut_i
)
638 return is_adj_ineq_extension(i
, j
, info
);
640 if (count_j
== 1 && !cut_j
)
641 return is_adj_ineq_extension(j
, i
, info
);
643 return isl_change_none
;
646 /* Basic map "i" has an inequality "k" that is adjacent to some equality
647 * of basic map "j". All the other inequalities are valid for "j".
648 * Check if basic map "j" forms an extension of basic map "i".
650 * In particular, we relax constraint "k", compute the corresponding
651 * facet and check whether it is included in the other basic map.
652 * If so, we know that relaxing the constraint extends the basic
653 * map with exactly the other basic map (we already know that this
654 * other basic map is included in the extension, because there
655 * were no "cut" inequalities in "i") and we can replace the
656 * two basic maps by this extension.
657 * Place this extension in the position that is the smallest of i and j.
665 static enum isl_change
is_adj_eq_extension(int i
, int j
, int k
,
666 struct isl_coalesce_info
*info
)
668 int change
= isl_change_none
;
670 struct isl_tab_undo
*snap
, *snap2
;
671 unsigned n_eq
= info
[i
].bmap
->n_eq
;
673 if (isl_tab_is_equality(info
[i
].tab
, n_eq
+ k
))
674 return isl_change_none
;
676 snap
= isl_tab_snap(info
[i
].tab
);
677 if (isl_tab_relax(info
[i
].tab
, n_eq
+ k
) < 0)
678 return isl_change_error
;
679 snap2
= isl_tab_snap(info
[i
].tab
);
680 if (isl_tab_select_facet(info
[i
].tab
, n_eq
+ k
) < 0)
681 return isl_change_error
;
682 super
= contains(&info
[j
], info
[i
].tab
);
684 if (isl_tab_rollback(info
[i
].tab
, snap2
) < 0)
685 return isl_change_error
;
686 info
[i
].bmap
= isl_basic_map_cow(info
[i
].bmap
);
688 return isl_change_error
;
689 isl_int_add_ui(info
[i
].bmap
->ineq
[k
][0],
690 info
[i
].bmap
->ineq
[k
][0], 1);
691 ISL_F_SET(info
[i
].bmap
, ISL_BASIC_MAP_FINAL
);
694 exchange(&info
[i
], &info
[j
]);
695 change
= isl_change_fuse
;
697 if (isl_tab_rollback(info
[i
].tab
, snap
) < 0)
698 return isl_change_error
;
703 /* Data structure that keeps track of the wrapping constraints
704 * and of information to bound the coefficients of those constraints.
706 * bound is set if we want to apply a bound on the coefficients
707 * mat contains the wrapping constraints
708 * max is the bound on the coefficients (if bound is set)
716 /* Update wraps->max to be greater than or equal to the coefficients
717 * in the equalities and inequalities of info->bmap that can be removed
718 * if we end up applying wrapping.
720 static void wraps_update_max(struct isl_wraps
*wraps
,
721 struct isl_coalesce_info
*info
)
725 unsigned total
= isl_basic_map_total_dim(info
->bmap
);
729 for (k
= 0; k
< info
->bmap
->n_eq
; ++k
) {
730 if (info
->eq
[2 * k
] == STATUS_VALID
&&
731 info
->eq
[2 * k
+ 1] == STATUS_VALID
)
733 isl_seq_abs_max(info
->bmap
->eq
[k
] + 1, total
, &max_k
);
734 if (isl_int_abs_gt(max_k
, wraps
->max
))
735 isl_int_set(wraps
->max
, max_k
);
738 for (k
= 0; k
< info
->bmap
->n_ineq
; ++k
) {
739 if (info
->ineq
[k
] == STATUS_VALID
||
740 info
->ineq
[k
] == STATUS_REDUNDANT
)
742 isl_seq_abs_max(info
->bmap
->ineq
[k
] + 1, total
, &max_k
);
743 if (isl_int_abs_gt(max_k
, wraps
->max
))
744 isl_int_set(wraps
->max
, max_k
);
747 isl_int_clear(max_k
);
750 /* Initialize the isl_wraps data structure.
751 * If we want to bound the coefficients of the wrapping constraints,
752 * we set wraps->max to the largest coefficient
753 * in the equalities and inequalities that can be removed if we end up
756 static void wraps_init(struct isl_wraps
*wraps
, __isl_take isl_mat
*mat
,
757 struct isl_coalesce_info
*info
, int i
, int j
)
765 ctx
= isl_mat_get_ctx(mat
);
766 wraps
->bound
= isl_options_get_coalesce_bounded_wrapping(ctx
);
769 isl_int_init(wraps
->max
);
770 isl_int_set_si(wraps
->max
, 0);
771 wraps_update_max(wraps
, &info
[i
]);
772 wraps_update_max(wraps
, &info
[j
]);
775 /* Free the contents of the isl_wraps data structure.
777 static void wraps_free(struct isl_wraps
*wraps
)
779 isl_mat_free(wraps
->mat
);
781 isl_int_clear(wraps
->max
);
784 /* Is the wrapping constraint in row "row" allowed?
786 * If wraps->bound is set, we check that none of the coefficients
787 * is greater than wraps->max.
789 static int allow_wrap(struct isl_wraps
*wraps
, int row
)
796 for (i
= 1; i
< wraps
->mat
->n_col
; ++i
)
797 if (isl_int_abs_gt(wraps
->mat
->row
[row
][i
], wraps
->max
))
803 /* Wrap "ineq" (or its opposite if "negate" is set) around "bound"
804 * to include "set" and add the result in position "w" of "wraps".
805 * "len" is the total number of coefficients in "bound" and "ineq".
806 * Return 1 on success, 0 on failure and -1 on error.
807 * Wrapping can fail if the result of wrapping is equal to "bound"
808 * or if we want to bound the sizes of the coefficients and
809 * the wrapped constraint does not satisfy this bound.
811 static int add_wrap(struct isl_wraps
*wraps
, int w
, isl_int
*bound
,
812 isl_int
*ineq
, unsigned len
, __isl_keep isl_set
*set
, int negate
)
814 isl_seq_cpy(wraps
->mat
->row
[w
], bound
, len
);
816 isl_seq_neg(wraps
->mat
->row
[w
+ 1], ineq
, len
);
817 ineq
= wraps
->mat
->row
[w
+ 1];
819 if (!isl_set_wrap_facet(set
, wraps
->mat
->row
[w
], ineq
))
821 if (isl_seq_eq(wraps
->mat
->row
[w
], bound
, len
))
823 if (!allow_wrap(wraps
, w
))
828 /* For each constraint in info->bmap that is not redundant (as determined
829 * by info->tab) and that is not a valid constraint for the other basic map,
830 * wrap the constraint around "bound" such that it includes the whole
831 * set "set" and append the resulting constraint to "wraps".
832 * Note that the constraints that are valid for the other basic map
833 * will be added to the combined basic map by default, so there is
834 * no need to wrap them.
835 * The caller wrap_in_facets even relies on this function not wrapping
836 * any constraints that are already valid.
837 * "wraps" is assumed to have been pre-allocated to the appropriate size.
838 * wraps->n_row is the number of actual wrapped constraints that have
840 * If any of the wrapping problems results in a constraint that is
841 * identical to "bound", then this means that "set" is unbounded in such
842 * way that no wrapping is possible. If this happens then wraps->n_row
844 * Similarly, if we want to bound the coefficients of the wrapping
845 * constraints and a newly added wrapping constraint does not
846 * satisfy the bound, then wraps->n_row is also reset to zero.
848 static int add_wraps(struct isl_wraps
*wraps
, struct isl_coalesce_info
*info
,
849 isl_int
*bound
, __isl_keep isl_set
*set
)
854 isl_basic_map
*bmap
= info
->bmap
;
855 unsigned len
= 1 + isl_basic_map_total_dim(bmap
);
857 w
= wraps
->mat
->n_row
;
859 for (l
= 0; l
< bmap
->n_ineq
; ++l
) {
860 if (info
->ineq
[l
] == STATUS_VALID
||
861 info
->ineq
[l
] == STATUS_REDUNDANT
)
863 if (isl_seq_is_neg(bound
, bmap
->ineq
[l
], len
))
865 if (isl_seq_eq(bound
, bmap
->ineq
[l
], len
))
867 if (isl_tab_is_redundant(info
->tab
, bmap
->n_eq
+ l
))
870 added
= add_wrap(wraps
, w
, bound
, bmap
->ineq
[l
], len
, set
, 0);
877 for (l
= 0; l
< bmap
->n_eq
; ++l
) {
878 if (isl_seq_is_neg(bound
, bmap
->eq
[l
], len
))
880 if (isl_seq_eq(bound
, bmap
->eq
[l
], len
))
883 for (m
= 0; m
< 2; ++m
) {
884 if (info
->eq
[2 * l
+ m
] == STATUS_VALID
)
886 added
= add_wrap(wraps
, w
, bound
, bmap
->eq
[l
], len
,
896 wraps
->mat
->n_row
= w
;
899 wraps
->mat
->n_row
= 0;
903 /* Check if the constraints in "wraps" from "first" until the last
904 * are all valid for the basic set represented by "tab".
905 * If not, wraps->n_row is set to zero.
907 static int check_wraps(__isl_keep isl_mat
*wraps
, int first
,
912 for (i
= first
; i
< wraps
->n_row
; ++i
) {
913 enum isl_ineq_type type
;
914 type
= isl_tab_ineq_type(tab
, wraps
->row
[i
]);
915 if (type
== isl_ineq_error
)
917 if (type
== isl_ineq_redundant
)
926 /* Return a set that corresponds to the non-redundant constraints
927 * (as recorded in tab) of bmap.
929 * It's important to remove the redundant constraints as some
930 * of the other constraints may have been modified after the
931 * constraints were marked redundant.
932 * In particular, a constraint may have been relaxed.
933 * Redundant constraints are ignored when a constraint is relaxed
934 * and should therefore continue to be ignored ever after.
935 * Otherwise, the relaxation might be thwarted by some of
938 * Update the underlying set to ensure that the dimension doesn't change.
939 * Otherwise the integer divisions could get dropped if the tab
940 * turns out to be empty.
942 static __isl_give isl_set
*set_from_updated_bmap(__isl_keep isl_basic_map
*bmap
,
947 bmap
= isl_basic_map_copy(bmap
);
948 bset
= isl_basic_map_underlying_set(bmap
);
949 bset
= isl_basic_set_cow(bset
);
950 bset
= isl_basic_set_update_from_tab(bset
, tab
);
951 return isl_set_from_basic_set(bset
);
954 /* Wrap the constraints of info->bmap that bound the facet defined
955 * by inequality "k" around (the opposite of) this inequality to
956 * include "set". "bound" may be used to store the negated inequality.
957 * Since the wrapped constraints are not guaranteed to contain the whole
958 * of info->bmap, we check them in check_wraps.
959 * If any of the wrapped constraints turn out to be invalid, then
960 * check_wraps will reset wrap->n_row to zero.
962 static int add_wraps_around_facet(struct isl_wraps
*wraps
,
963 struct isl_coalesce_info
*info
, int k
, isl_int
*bound
,
964 __isl_keep isl_set
*set
)
966 struct isl_tab_undo
*snap
;
968 unsigned total
= isl_basic_map_total_dim(info
->bmap
);
970 snap
= isl_tab_snap(info
->tab
);
972 if (isl_tab_select_facet(info
->tab
, info
->bmap
->n_eq
+ k
) < 0)
974 if (isl_tab_detect_redundant(info
->tab
) < 0)
977 isl_seq_neg(bound
, info
->bmap
->ineq
[k
], 1 + total
);
979 n
= wraps
->mat
->n_row
;
980 if (add_wraps(wraps
, info
, bound
, set
) < 0)
983 if (isl_tab_rollback(info
->tab
, snap
) < 0)
985 if (check_wraps(wraps
->mat
, n
, info
->tab
) < 0)
991 /* Given a basic set i with a constraint k that is adjacent to
992 * basic set j, check if we can wrap
993 * both the facet corresponding to k (if "wrap_facet" is set) and basic map j
994 * (always) around their ridges to include the other set.
995 * If so, replace the pair of basic sets by their union.
997 * All constraints of i (except k) are assumed to be valid or
998 * cut constraints for j.
999 * Wrapping the cut constraints to include basic map j may result
1000 * in constraints that are no longer valid of basic map i
1001 * we have to check that the resulting wrapping constraints are valid for i.
1002 * If "wrap_facet" is not set, then all constraints of i (except k)
1003 * are assumed to be valid for j.
1012 static enum isl_change
can_wrap_in_facet(int i
, int j
, int k
,
1013 struct isl_coalesce_info
*info
, int wrap_facet
)
1015 enum isl_change change
= isl_change_none
;
1016 struct isl_wraps wraps
;
1019 struct isl_set
*set_i
= NULL
;
1020 struct isl_set
*set_j
= NULL
;
1021 struct isl_vec
*bound
= NULL
;
1022 unsigned total
= isl_basic_map_total_dim(info
[i
].bmap
);
1024 set_i
= set_from_updated_bmap(info
[i
].bmap
, info
[i
].tab
);
1025 set_j
= set_from_updated_bmap(info
[j
].bmap
, info
[j
].tab
);
1026 ctx
= isl_basic_map_get_ctx(info
[i
].bmap
);
1027 mat
= isl_mat_alloc(ctx
, 2 * (info
[i
].bmap
->n_eq
+ info
[j
].bmap
->n_eq
) +
1028 info
[i
].bmap
->n_ineq
+ info
[j
].bmap
->n_ineq
,
1030 wraps_init(&wraps
, mat
, info
, i
, j
);
1031 bound
= isl_vec_alloc(ctx
, 1 + total
);
1032 if (!set_i
|| !set_j
|| !wraps
.mat
|| !bound
)
1035 isl_seq_cpy(bound
->el
, info
[i
].bmap
->ineq
[k
], 1 + total
);
1036 isl_int_add_ui(bound
->el
[0], bound
->el
[0], 1);
1038 isl_seq_cpy(wraps
.mat
->row
[0], bound
->el
, 1 + total
);
1039 wraps
.mat
->n_row
= 1;
1041 if (add_wraps(&wraps
, &info
[j
], bound
->el
, set_i
) < 0)
1043 if (!wraps
.mat
->n_row
)
1047 if (add_wraps_around_facet(&wraps
, &info
[i
], k
,
1048 bound
->el
, set_j
) < 0)
1050 if (!wraps
.mat
->n_row
)
1054 change
= fuse(i
, j
, info
, wraps
.mat
, 0, 0);
1059 isl_set_free(set_i
);
1060 isl_set_free(set_j
);
1062 isl_vec_free(bound
);
1067 isl_vec_free(bound
);
1068 isl_set_free(set_i
);
1069 isl_set_free(set_j
);
1070 return isl_change_error
;
1073 /* Given a pair of basic maps i and j such that j sticks out
1074 * of i at n cut constraints, each time by at most one,
1075 * try to compute wrapping constraints and replace the two
1076 * basic maps by a single basic map.
1077 * The other constraints of i are assumed to be valid for j.
1079 * For each cut constraint t(x) >= 0 of i, we add the relaxed version
1080 * t(x) + 1 >= 0, along with wrapping constraints for all constraints
1081 * of basic map j that bound the part of basic map j that sticks out
1082 * of the cut constraint.
1083 * In particular, we first intersect basic map j with t(x) + 1 = 0.
1084 * If the result is empty, then t(x) >= 0 was actually a valid constraint
1085 * (with respect to the integer points), so we add t(x) >= 0 instead.
1086 * Otherwise, we wrap the constraints of basic map j that are not
1087 * redundant in this intersection and that are not already valid
1088 * for basic map i over basic map i.
1089 * Note that it is sufficient to wrap the constraints to include
1090 * basic map i, because we will only wrap the constraints that do
1091 * not include basic map i already. The wrapped constraint will
1092 * therefore be more relaxed compared to the original constraint.
1093 * Since the original constraint is valid for basic map j, so is
1094 * the wrapped constraint.
1096 * If any wrapping fails, i.e., if we cannot wrap to touch
1097 * the union, then we give up.
1098 * Otherwise, the pair of basic maps is replaced by their union.
1100 static enum isl_change
wrap_in_facets(int i
, int j
, int *cuts
, int n
,
1101 struct isl_coalesce_info
*info
)
1103 enum isl_change change
= isl_change_none
;
1104 struct isl_wraps wraps
;
1107 isl_set
*set_i
= NULL
;
1108 unsigned total
= isl_basic_map_total_dim(info
[i
].bmap
);
1111 struct isl_tab_undo
*snap
;
1113 if (isl_tab_extend_cons(info
[j
].tab
, 1) < 0)
1116 max_wrap
= 1 + 2 * info
[j
].bmap
->n_eq
+ info
[j
].bmap
->n_ineq
;
1119 set_i
= set_from_updated_bmap(info
[i
].bmap
, info
[i
].tab
);
1120 ctx
= isl_basic_map_get_ctx(info
[i
].bmap
);
1121 mat
= isl_mat_alloc(ctx
, max_wrap
, 1 + total
);
1122 wraps_init(&wraps
, mat
, info
, i
, j
);
1123 if (!set_i
|| !wraps
.mat
)
1126 snap
= isl_tab_snap(info
[j
].tab
);
1128 wraps
.mat
->n_row
= 0;
1130 for (k
= 0; k
< n
; ++k
) {
1131 w
= wraps
.mat
->n_row
++;
1132 isl_seq_cpy(wraps
.mat
->row
[w
],
1133 info
[i
].bmap
->ineq
[cuts
[k
]], 1 + total
);
1134 isl_int_add_ui(wraps
.mat
->row
[w
][0], wraps
.mat
->row
[w
][0], 1);
1135 if (isl_tab_add_eq(info
[j
].tab
, wraps
.mat
->row
[w
]) < 0)
1137 if (isl_tab_detect_redundant(info
[j
].tab
) < 0)
1140 if (info
[j
].tab
->empty
)
1141 isl_int_sub_ui(wraps
.mat
->row
[w
][0],
1142 wraps
.mat
->row
[w
][0], 1);
1143 else if (add_wraps(&wraps
, &info
[j
],
1144 wraps
.mat
->row
[w
], set_i
) < 0)
1147 if (isl_tab_rollback(info
[j
].tab
, snap
) < 0)
1150 if (!wraps
.mat
->n_row
)
1155 change
= fuse(i
, j
, info
, wraps
.mat
, 0, 1);
1158 isl_set_free(set_i
);
1163 isl_set_free(set_i
);
1164 return isl_change_error
;
1167 /* Given two basic sets i and j such that i has no cut equalities,
1168 * check if relaxing all the cut inequalities of i by one turns
1169 * them into valid constraint for j and check if we can wrap in
1170 * the bits that are sticking out.
1171 * If so, replace the pair by their union.
1173 * We first check if all relaxed cut inequalities of i are valid for j
1174 * and then try to wrap in the intersections of the relaxed cut inequalities
1177 * During this wrapping, we consider the points of j that lie at a distance
1178 * of exactly 1 from i. In particular, we ignore the points that lie in
1179 * between this lower-dimensional space and the basic map i.
1180 * We can therefore only apply this to integer maps.
1206 * Wrapping can fail if the result of wrapping one of the facets
1207 * around its edges does not produce any new facet constraint.
1208 * In particular, this happens when we try to wrap in unbounded sets.
1210 * _______________________________________________________________________
1214 * |_| |_________________________________________________________________
1217 * The following is not an acceptable result of coalescing the above two
1218 * sets as it includes extra integer points.
1219 * _______________________________________________________________________
1224 * \______________________________________________________________________
1226 static enum isl_change
can_wrap_in_set(int i
, int j
,
1227 struct isl_coalesce_info
*info
)
1229 enum isl_change change
= isl_change_none
;
1235 if (ISL_F_ISSET(info
[i
].bmap
, ISL_BASIC_MAP_RATIONAL
) ||
1236 ISL_F_ISSET(info
[j
].bmap
, ISL_BASIC_MAP_RATIONAL
))
1237 return isl_change_none
;
1239 n
= count(info
[i
].ineq
, info
[i
].bmap
->n_ineq
, STATUS_CUT
);
1241 return isl_change_none
;
1243 ctx
= isl_basic_map_get_ctx(info
[i
].bmap
);
1244 cuts
= isl_alloc_array(ctx
, int, n
);
1246 return isl_change_error
;
1248 for (k
= 0, m
= 0; m
< n
; ++k
) {
1249 enum isl_ineq_type type
;
1251 if (info
[i
].ineq
[k
] != STATUS_CUT
)
1254 isl_int_add_ui(info
[i
].bmap
->ineq
[k
][0],
1255 info
[i
].bmap
->ineq
[k
][0], 1);
1256 type
= isl_tab_ineq_type(info
[j
].tab
, info
[i
].bmap
->ineq
[k
]);
1257 isl_int_sub_ui(info
[i
].bmap
->ineq
[k
][0],
1258 info
[i
].bmap
->ineq
[k
][0], 1);
1259 if (type
== isl_ineq_error
)
1261 if (type
!= isl_ineq_redundant
)
1268 change
= wrap_in_facets(i
, j
, cuts
, n
, info
);
1275 return isl_change_error
;
1278 /* Check if either i or j has only cut inequalities that can
1279 * be used to wrap in (a facet of) the other basic set.
1280 * if so, replace the pair by their union.
1282 static enum isl_change
check_wrap(int i
, int j
, struct isl_coalesce_info
*info
)
1284 enum isl_change change
= isl_change_none
;
1286 if (!any(info
[i
].eq
, 2 * info
[i
].bmap
->n_eq
, STATUS_CUT
))
1287 change
= can_wrap_in_set(i
, j
, info
);
1288 if (change
!= isl_change_none
)
1291 if (!any(info
[j
].eq
, 2 * info
[j
].bmap
->n_eq
, STATUS_CUT
))
1292 change
= can_wrap_in_set(j
, i
, info
);
1296 /* At least one of the basic maps has an equality that is adjacent
1297 * to inequality. Make sure that only one of the basic maps has
1298 * such an equality and that the other basic map has exactly one
1299 * inequality adjacent to an equality.
1300 * We call the basic map that has the inequality "i" and the basic
1301 * map that has the equality "j".
1302 * If "i" has any "cut" (in)equality, then relaxing the inequality
1303 * by one would not result in a basic map that contains the other
1304 * basic map. However, it may still be possible to wrap in the other
1307 static enum isl_change
check_adj_eq(int i
, int j
,
1308 struct isl_coalesce_info
*info
)
1310 enum isl_change change
= isl_change_none
;
1314 if (any(info
[i
].eq
, 2 * info
[i
].bmap
->n_eq
, STATUS_ADJ_INEQ
) &&
1315 any(info
[j
].eq
, 2 * info
[j
].bmap
->n_eq
, STATUS_ADJ_INEQ
))
1316 /* ADJ EQ TOO MANY */
1317 return isl_change_none
;
1319 if (any(info
[i
].eq
, 2 * info
[i
].bmap
->n_eq
, STATUS_ADJ_INEQ
))
1320 return check_adj_eq(j
, i
, info
);
1322 /* j has an equality adjacent to an inequality in i */
1324 if (any(info
[i
].eq
, 2 * info
[i
].bmap
->n_eq
, STATUS_CUT
))
1325 return isl_change_none
;
1326 any_cut
= any(info
[i
].ineq
, info
[i
].bmap
->n_ineq
, STATUS_CUT
);
1327 if (count(info
[i
].ineq
, info
[i
].bmap
->n_ineq
, STATUS_ADJ_EQ
) != 1 ||
1328 any(info
[j
].ineq
, info
[j
].bmap
->n_ineq
, STATUS_ADJ_EQ
) ||
1329 any(info
[i
].ineq
, info
[i
].bmap
->n_ineq
, STATUS_ADJ_INEQ
) ||
1330 any(info
[j
].ineq
, info
[j
].bmap
->n_ineq
, STATUS_ADJ_INEQ
))
1331 /* ADJ EQ TOO MANY */
1332 return isl_change_none
;
1334 for (k
= 0; k
< info
[i
].bmap
->n_ineq
; ++k
)
1335 if (info
[i
].ineq
[k
] == STATUS_ADJ_EQ
)
1339 change
= is_adj_eq_extension(i
, j
, k
, info
);
1340 if (change
!= isl_change_none
)
1344 change
= can_wrap_in_facet(i
, j
, k
, info
, any_cut
);
1349 /* The two basic maps lie on adjacent hyperplanes. In particular,
1350 * basic map "i" has an equality that lies parallel to basic map "j".
1351 * Check if we can wrap the facets around the parallel hyperplanes
1352 * to include the other set.
1354 * We perform basically the same operations as can_wrap_in_facet,
1355 * except that we don't need to select a facet of one of the sets.
1361 * If there is more than one equality of "i" adjacent to an equality of "j",
1362 * then the result will satisfy one or more equalities that are a linear
1363 * combination of these equalities. These will be encoded as pairs
1364 * of inequalities in the wrapping constraints and need to be made
1367 static enum isl_change
check_eq_adj_eq(int i
, int j
,
1368 struct isl_coalesce_info
*info
)
1371 enum isl_change change
= isl_change_none
;
1372 int detect_equalities
= 0;
1373 struct isl_wraps wraps
;
1376 struct isl_set
*set_i
= NULL
;
1377 struct isl_set
*set_j
= NULL
;
1378 struct isl_vec
*bound
= NULL
;
1379 unsigned total
= isl_basic_map_total_dim(info
[i
].bmap
);
1381 if (count(info
[i
].eq
, 2 * info
[i
].bmap
->n_eq
, STATUS_ADJ_EQ
) != 1)
1382 detect_equalities
= 1;
1384 for (k
= 0; k
< 2 * info
[i
].bmap
->n_eq
; ++k
)
1385 if (info
[i
].eq
[k
] == STATUS_ADJ_EQ
)
1388 set_i
= set_from_updated_bmap(info
[i
].bmap
, info
[i
].tab
);
1389 set_j
= set_from_updated_bmap(info
[j
].bmap
, info
[j
].tab
);
1390 ctx
= isl_basic_map_get_ctx(info
[i
].bmap
);
1391 mat
= isl_mat_alloc(ctx
, 2 * (info
[i
].bmap
->n_eq
+ info
[j
].bmap
->n_eq
) +
1392 info
[i
].bmap
->n_ineq
+ info
[j
].bmap
->n_ineq
,
1394 wraps_init(&wraps
, mat
, info
, i
, j
);
1395 bound
= isl_vec_alloc(ctx
, 1 + total
);
1396 if (!set_i
|| !set_j
|| !wraps
.mat
|| !bound
)
1400 isl_seq_neg(bound
->el
, info
[i
].bmap
->eq
[k
/ 2], 1 + total
);
1402 isl_seq_cpy(bound
->el
, info
[i
].bmap
->eq
[k
/ 2], 1 + total
);
1403 isl_int_add_ui(bound
->el
[0], bound
->el
[0], 1);
1405 isl_seq_cpy(wraps
.mat
->row
[0], bound
->el
, 1 + total
);
1406 wraps
.mat
->n_row
= 1;
1408 if (add_wraps(&wraps
, &info
[j
], bound
->el
, set_i
) < 0)
1410 if (!wraps
.mat
->n_row
)
1413 isl_int_sub_ui(bound
->el
[0], bound
->el
[0], 1);
1414 isl_seq_neg(bound
->el
, bound
->el
, 1 + total
);
1416 isl_seq_cpy(wraps
.mat
->row
[wraps
.mat
->n_row
], bound
->el
, 1 + total
);
1419 if (add_wraps(&wraps
, &info
[i
], bound
->el
, set_j
) < 0)
1421 if (!wraps
.mat
->n_row
)
1424 change
= fuse(i
, j
, info
, wraps
.mat
, detect_equalities
, 0);
1427 error
: change
= isl_change_error
;
1432 isl_set_free(set_i
);
1433 isl_set_free(set_j
);
1434 isl_vec_free(bound
);
1439 /* Check if the union of the given pair of basic maps
1440 * can be represented by a single basic map.
1441 * If so, replace the pair by the single basic map and return
1442 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
1443 * Otherwise, return isl_change_none.
1444 * The two basic maps are assumed to live in the same local space.
1446 * We first check the effect of each constraint of one basic map
1447 * on the other basic map.
1448 * The constraint may be
1449 * redundant the constraint is redundant in its own
1450 * basic map and should be ignore and removed
1452 * valid all (integer) points of the other basic map
1453 * satisfy the constraint
1454 * separate no (integer) point of the other basic map
1455 * satisfies the constraint
1456 * cut some but not all points of the other basic map
1457 * satisfy the constraint
1458 * adj_eq the given constraint is adjacent (on the outside)
1459 * to an equality of the other basic map
1460 * adj_ineq the given constraint is adjacent (on the outside)
1461 * to an inequality of the other basic map
1463 * We consider seven cases in which we can replace the pair by a single
1464 * basic map. We ignore all "redundant" constraints.
1466 * 1. all constraints of one basic map are valid
1467 * => the other basic map is a subset and can be removed
1469 * 2. all constraints of both basic maps are either "valid" or "cut"
1470 * and the facets corresponding to the "cut" constraints
1471 * of one of the basic maps lies entirely inside the other basic map
1472 * => the pair can be replaced by a basic map consisting
1473 * of the valid constraints in both basic maps
1475 * 3. there is a single pair of adjacent inequalities
1476 * (all other constraints are "valid")
1477 * => the pair can be replaced by a basic map consisting
1478 * of the valid constraints in both basic maps
1480 * 4. one basic map has a single adjacent inequality, while the other
1481 * constraints are "valid". The other basic map has some
1482 * "cut" constraints, but replacing the adjacent inequality by
1483 * its opposite and adding the valid constraints of the other
1484 * basic map results in a subset of the other basic map
1485 * => the pair can be replaced by a basic map consisting
1486 * of the valid constraints in both basic maps
1488 * 5. there is a single adjacent pair of an inequality and an equality,
1489 * the other constraints of the basic map containing the inequality are
1490 * "valid". Moreover, if the inequality the basic map is relaxed
1491 * and then turned into an equality, then resulting facet lies
1492 * entirely inside the other basic map
1493 * => the pair can be replaced by the basic map containing
1494 * the inequality, with the inequality relaxed.
1496 * 6. there is a single adjacent pair of an inequality and an equality,
1497 * the other constraints of the basic map containing the inequality are
1498 * "valid". Moreover, the facets corresponding to both
1499 * the inequality and the equality can be wrapped around their
1500 * ridges to include the other basic map
1501 * => the pair can be replaced by a basic map consisting
1502 * of the valid constraints in both basic maps together
1503 * with all wrapping constraints
1505 * 7. one of the basic maps extends beyond the other by at most one.
1506 * Moreover, the facets corresponding to the cut constraints and
1507 * the pieces of the other basic map at offset one from these cut
1508 * constraints can be wrapped around their ridges to include
1509 * the union of the two basic maps
1510 * => the pair can be replaced by a basic map consisting
1511 * of the valid constraints in both basic maps together
1512 * with all wrapping constraints
1514 * 8. the two basic maps live in adjacent hyperplanes. In principle
1515 * such sets can always be combined through wrapping, but we impose
1516 * that there is only one such pair, to avoid overeager coalescing.
1518 * Throughout the computation, we maintain a collection of tableaus
1519 * corresponding to the basic maps. When the basic maps are dropped
1520 * or combined, the tableaus are modified accordingly.
1522 static enum isl_change
coalesce_local_pair(int i
, int j
,
1523 struct isl_coalesce_info
*info
)
1525 enum isl_change change
= isl_change_none
;
1527 info
[i
].eq
= info
[i
].ineq
= NULL
;
1528 info
[j
].eq
= info
[j
].ineq
= NULL
;
1530 info
[i
].eq
= eq_status_in(info
[i
].bmap
, info
[j
].tab
);
1531 if (info
[i
].bmap
->n_eq
&& !info
[i
].eq
)
1533 if (any(info
[i
].eq
, 2 * info
[i
].bmap
->n_eq
, STATUS_ERROR
))
1535 if (any(info
[i
].eq
, 2 * info
[i
].bmap
->n_eq
, STATUS_SEPARATE
))
1538 info
[j
].eq
= eq_status_in(info
[j
].bmap
, info
[i
].tab
);
1539 if (info
[j
].bmap
->n_eq
&& !info
[j
].eq
)
1541 if (any(info
[j
].eq
, 2 * info
[j
].bmap
->n_eq
, STATUS_ERROR
))
1543 if (any(info
[j
].eq
, 2 * info
[j
].bmap
->n_eq
, STATUS_SEPARATE
))
1546 info
[i
].ineq
= ineq_status_in(info
[i
].bmap
, info
[i
].tab
, info
[j
].tab
);
1547 if (info
[i
].bmap
->n_ineq
&& !info
[i
].ineq
)
1549 if (any(info
[i
].ineq
, info
[i
].bmap
->n_ineq
, STATUS_ERROR
))
1551 if (any(info
[i
].ineq
, info
[i
].bmap
->n_ineq
, STATUS_SEPARATE
))
1554 info
[j
].ineq
= ineq_status_in(info
[j
].bmap
, info
[j
].tab
, info
[i
].tab
);
1555 if (info
[j
].bmap
->n_ineq
&& !info
[j
].ineq
)
1557 if (any(info
[j
].ineq
, info
[j
].bmap
->n_ineq
, STATUS_ERROR
))
1559 if (any(info
[j
].ineq
, info
[j
].bmap
->n_ineq
, STATUS_SEPARATE
))
1562 if (all(info
[i
].eq
, 2 * info
[i
].bmap
->n_eq
, STATUS_VALID
) &&
1563 all(info
[i
].ineq
, info
[i
].bmap
->n_ineq
, STATUS_VALID
)) {
1565 change
= isl_change_drop_second
;
1566 } else if (all(info
[j
].eq
, 2 * info
[j
].bmap
->n_eq
, STATUS_VALID
) &&
1567 all(info
[j
].ineq
, info
[j
].bmap
->n_ineq
, STATUS_VALID
)) {
1569 change
= isl_change_drop_first
;
1570 } else if (any(info
[i
].eq
, 2 * info
[i
].bmap
->n_eq
, STATUS_ADJ_EQ
)) {
1571 change
= check_eq_adj_eq(i
, j
, info
);
1572 } else if (any(info
[j
].eq
, 2 * info
[j
].bmap
->n_eq
, STATUS_ADJ_EQ
)) {
1573 change
= check_eq_adj_eq(j
, i
, info
);
1574 } else if (any(info
[i
].eq
, 2 * info
[i
].bmap
->n_eq
, STATUS_ADJ_INEQ
) ||
1575 any(info
[j
].eq
, 2 * info
[j
].bmap
->n_eq
, STATUS_ADJ_INEQ
)) {
1576 change
= check_adj_eq(i
, j
, info
);
1577 } else if (any(info
[i
].ineq
, info
[i
].bmap
->n_ineq
, STATUS_ADJ_EQ
) ||
1578 any(info
[j
].ineq
, info
[j
].bmap
->n_ineq
, STATUS_ADJ_EQ
)) {
1581 } else if (any(info
[i
].ineq
, info
[i
].bmap
->n_ineq
, STATUS_ADJ_INEQ
) ||
1582 any(info
[j
].ineq
, info
[j
].bmap
->n_ineq
, STATUS_ADJ_INEQ
)) {
1583 change
= check_adj_ineq(i
, j
, info
);
1585 if (!any(info
[i
].eq
, 2 * info
[i
].bmap
->n_eq
, STATUS_CUT
) &&
1586 !any(info
[j
].eq
, 2 * info
[j
].bmap
->n_eq
, STATUS_CUT
))
1587 change
= check_facets(i
, j
, info
);
1588 if (change
== isl_change_none
)
1589 change
= check_wrap(i
, j
, info
);
1603 return isl_change_error
;
1606 /* Do the two basic maps live in the same local space, i.e.,
1607 * do they have the same (known) divs?
1608 * If either basic map has any unknown divs, then we can only assume
1609 * that they do not live in the same local space.
1611 static int same_divs(__isl_keep isl_basic_map
*bmap1
,
1612 __isl_keep isl_basic_map
*bmap2
)
1618 if (!bmap1
|| !bmap2
)
1620 if (bmap1
->n_div
!= bmap2
->n_div
)
1623 if (bmap1
->n_div
== 0)
1626 known
= isl_basic_map_divs_known(bmap1
);
1627 if (known
< 0 || !known
)
1629 known
= isl_basic_map_divs_known(bmap2
);
1630 if (known
< 0 || !known
)
1633 total
= isl_basic_map_total_dim(bmap1
);
1634 for (i
= 0; i
< bmap1
->n_div
; ++i
)
1635 if (!isl_seq_eq(bmap1
->div
[i
], bmap2
->div
[i
], 2 + total
))
1641 /* Does "bmap" contain the basic map represented by the tableau "tab"
1642 * after expanding the divs of "bmap" to match those of "tab"?
1643 * The expansion is performed using the divs "div" and expansion "exp"
1644 * computed by the caller.
1645 * Then we check if all constraints of the expanded "bmap" are valid for "tab".
1647 static int contains_with_expanded_divs(__isl_keep isl_basic_map
*bmap
,
1648 struct isl_tab
*tab
, __isl_keep isl_mat
*div
, int *exp
)
1654 bmap
= isl_basic_map_copy(bmap
);
1655 bmap
= isl_basic_set_expand_divs(bmap
, isl_mat_copy(div
), exp
);
1660 eq_i
= eq_status_in(bmap
, tab
);
1661 if (bmap
->n_eq
&& !eq_i
)
1663 if (any(eq_i
, 2 * bmap
->n_eq
, STATUS_ERROR
))
1665 if (any(eq_i
, 2 * bmap
->n_eq
, STATUS_SEPARATE
))
1668 ineq_i
= ineq_status_in(bmap
, NULL
, tab
);
1669 if (bmap
->n_ineq
&& !ineq_i
)
1671 if (any(ineq_i
, bmap
->n_ineq
, STATUS_ERROR
))
1673 if (any(ineq_i
, bmap
->n_ineq
, STATUS_SEPARATE
))
1676 if (all(eq_i
, 2 * bmap
->n_eq
, STATUS_VALID
) &&
1677 all(ineq_i
, bmap
->n_ineq
, STATUS_VALID
))
1681 isl_basic_map_free(bmap
);
1686 isl_basic_map_free(bmap
);
1692 /* Does "bmap_i" contain the basic map represented by "info_j"
1693 * after aligning the divs of "bmap_i" to those of "info_j".
1694 * Note that this can only succeed if the number of divs of "bmap_i"
1695 * is smaller than (or equal to) the number of divs of "info_j".
1697 * We first check if the divs of "bmap_i" are all known and form a subset
1698 * of those of "bmap_j". If so, we pass control over to
1699 * contains_with_expanded_divs.
1701 static int contains_after_aligning_divs(__isl_keep isl_basic_map
*bmap_i
,
1702 struct isl_coalesce_info
*info_j
)
1705 isl_mat
*div_i
, *div_j
, *div
;
1711 known
= isl_basic_map_divs_known(bmap_i
);
1712 if (known
< 0 || !known
)
1715 ctx
= isl_basic_map_get_ctx(bmap_i
);
1717 div_i
= isl_basic_map_get_divs(bmap_i
);
1718 div_j
= isl_basic_map_get_divs(info_j
->bmap
);
1720 if (!div_i
|| !div_j
)
1723 exp1
= isl_alloc_array(ctx
, int, div_i
->n_row
);
1724 exp2
= isl_alloc_array(ctx
, int, div_j
->n_row
);
1725 if ((div_i
->n_row
&& !exp1
) || (div_j
->n_row
&& !exp2
))
1728 div
= isl_merge_divs(div_i
, div_j
, exp1
, exp2
);
1732 if (div
->n_row
== div_j
->n_row
)
1733 subset
= contains_with_expanded_divs(bmap_i
,
1734 info_j
->tab
, div
, exp1
);
1740 isl_mat_free(div_i
);
1741 isl_mat_free(div_j
);
1748 isl_mat_free(div_i
);
1749 isl_mat_free(div_j
);
1755 /* Check if the basic map "j" is a subset of basic map "i",
1756 * if "i" has fewer divs that "j".
1757 * If so, remove basic map "j".
1759 * If the two basic maps have the same number of divs, then
1760 * they must necessarily be different. Otherwise, we would have
1761 * called coalesce_local_pair. We therefore don't try anything
1764 static int coalesced_subset(int i
, int j
, struct isl_coalesce_info
*info
)
1768 if (info
[i
].bmap
->n_div
>= info
[j
].bmap
->n_div
)
1771 superset
= contains_after_aligning_divs(info
[i
].bmap
, &info
[j
]);
1780 /* Check if basic map "j" is a subset of basic map "i" after
1781 * exploiting the extra equalities of "j" to simplify the divs of "i".
1782 * If so, remove basic map "j".
1784 * If "j" does not have any equalities or if they are the same
1785 * as those of "i", then we cannot exploit them to simplify the divs.
1786 * Similarly, if there are no divs in "i", then they cannot be simplified.
1787 * If, on the other hand, the affine hulls of "i" and "j" do not intersect,
1788 * then "j" cannot be a subset of "i".
1790 * Otherwise, we intersect "i" with the affine hull of "j" and then
1791 * check if "j" is a subset of the result after aligning the divs.
1792 * If so, then "j" is definitely a subset of "i" and can be removed.
1793 * Note that if after intersection with the affine hull of "j".
1794 * "i" still has more divs than "j", then there is no way we can
1795 * align the divs of "i" to those of "j".
1797 static int coalesced_subset_with_equalities(int i
, int j
,
1798 struct isl_coalesce_info
*info
)
1800 isl_basic_map
*hull_i
, *hull_j
, *bmap_i
;
1801 int equal
, empty
, subset
;
1803 if (info
[j
].bmap
->n_eq
== 0)
1805 if (info
[i
].bmap
->n_div
== 0)
1808 hull_i
= isl_basic_map_copy(info
[i
].bmap
);
1809 hull_i
= isl_basic_map_plain_affine_hull(hull_i
);
1810 hull_j
= isl_basic_map_copy(info
[j
].bmap
);
1811 hull_j
= isl_basic_map_plain_affine_hull(hull_j
);
1813 hull_j
= isl_basic_map_intersect(hull_j
, isl_basic_map_copy(hull_i
));
1814 equal
= isl_basic_map_plain_is_equal(hull_i
, hull_j
);
1815 empty
= isl_basic_map_plain_is_empty(hull_j
);
1816 isl_basic_map_free(hull_i
);
1818 if (equal
< 0 || equal
|| empty
< 0 || empty
) {
1819 isl_basic_map_free(hull_j
);
1820 return equal
< 0 || empty
< 0 ? -1 : 0;
1823 bmap_i
= isl_basic_map_copy(info
[i
].bmap
);
1824 bmap_i
= isl_basic_map_intersect(bmap_i
, hull_j
);
1828 if (bmap_i
->n_div
> info
[j
].bmap
->n_div
) {
1829 isl_basic_map_free(bmap_i
);
1833 subset
= contains_after_aligning_divs(bmap_i
, &info
[j
]);
1835 isl_basic_map_free(bmap_i
);
1845 /* Check if one of the basic maps is a subset of the other and, if so,
1847 * Note that we only perform any test if the number of divs is different
1848 * in the two basic maps. In case the number of divs is the same,
1849 * we have already established that the divs are different
1850 * in the two basic maps.
1851 * In particular, if the number of divs of basic map i is smaller than
1852 * the number of divs of basic map j, then we check if j is a subset of i
1855 static enum isl_change
check_coalesce_subset(int i
, int j
,
1856 struct isl_coalesce_info
*info
)
1860 changed
= coalesced_subset(i
, j
, info
);
1861 if (changed
< 0 || changed
)
1862 return changed
< 0 ? isl_change_error
: isl_change_drop_second
;
1864 changed
= coalesced_subset(j
, i
, info
);
1865 if (changed
< 0 || changed
)
1866 return changed
< 0 ? isl_change_error
: isl_change_drop_first
;
1868 changed
= coalesced_subset_with_equalities(i
, j
, info
);
1869 if (changed
< 0 || changed
)
1870 return changed
< 0 ? isl_change_error
: isl_change_drop_second
;
1872 changed
= coalesced_subset_with_equalities(j
, i
, info
);
1873 if (changed
< 0 || changed
)
1874 return changed
< 0 ? isl_change_error
: isl_change_drop_first
;
1876 return isl_change_none
;
1879 /* Does "bmap" involve any divs that themselves refer to divs?
1881 static int has_nested_div(__isl_keep isl_basic_map
*bmap
)
1887 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
1888 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
1891 for (i
= 0; i
< n_div
; ++i
)
1892 if (isl_seq_first_non_zero(bmap
->div
[i
] + 2 + total
,
1899 /* Return a list of affine expressions, one for each integer division
1900 * in "bmap_i". For each integer division that also appears in "bmap_j",
1901 * the affine expression is set to NaN. The number of NaNs in the list
1902 * is equal to the number of integer divisions in "bmap_j".
1903 * For the other integer divisions of "bmap_i", the corresponding
1904 * element in the list is a purely affine expression equal to the integer
1905 * division in "hull".
1906 * If no such list can be constructed, then the number of elements
1907 * in the returned list is smaller than the number of integer divisions
1910 static __isl_give isl_aff_list
*set_up_substitutions(
1911 __isl_keep isl_basic_map
*bmap_i
, __isl_keep isl_basic_map
*bmap_j
,
1912 __isl_take isl_basic_map
*hull
)
1914 unsigned n_div_i
, n_div_j
, total
;
1916 isl_local_space
*ls
;
1917 isl_basic_set
*wrap_hull
;
1925 ctx
= isl_basic_map_get_ctx(hull
);
1927 n_div_i
= isl_basic_map_dim(bmap_i
, isl_dim_div
);
1928 n_div_j
= isl_basic_map_dim(bmap_j
, isl_dim_div
);
1929 total
= isl_basic_map_total_dim(bmap_i
) - n_div_i
;
1931 ls
= isl_basic_map_get_local_space(bmap_i
);
1932 ls
= isl_local_space_wrap(ls
);
1933 wrap_hull
= isl_basic_map_wrap(hull
);
1935 aff_nan
= isl_aff_nan_on_domain(isl_local_space_copy(ls
));
1936 list
= isl_aff_list_alloc(ctx
, n_div_i
);
1939 for (i
= 0; i
< n_div_i
; ++i
) {
1943 isl_seq_eq(bmap_i
->div
[i
], bmap_j
->div
[j
], 2 + total
)) {
1945 list
= isl_aff_list_add(list
, isl_aff_copy(aff_nan
));
1948 if (n_div_i
- i
<= n_div_j
- j
)
1951 aff
= isl_local_space_get_div(ls
, i
);
1952 aff
= isl_aff_substitute_equalities(aff
,
1953 isl_basic_set_copy(wrap_hull
));
1954 aff
= isl_aff_floor(aff
);
1957 if (isl_aff_dim(aff
, isl_dim_div
) != 0) {
1962 list
= isl_aff_list_add(list
, aff
);
1965 isl_aff_free(aff_nan
);
1966 isl_local_space_free(ls
);
1967 isl_basic_set_free(wrap_hull
);
1971 isl_local_space_free(ls
);
1972 isl_basic_set_free(wrap_hull
);
1973 isl_aff_list_free(list
);
1977 /* Add variables to "tab" corresponding to the elements in "list"
1978 * that are not set to NaN. The value of the added variable
1979 * is fixed to the purely affine expression defined by the element.
1980 * "dim" is the offset in the variables of "tab" where we should
1981 * start considering the elements in "list".
1982 * When this function returns, the total number of variables in "tab"
1983 * is equal to "dim" plus the number of elements in "list".
1985 static int add_subs(struct isl_tab
*tab
, __isl_keep isl_aff_list
*list
, int dim
)
1996 n
= isl_aff_list_n_aff(list
);
1997 extra
= n
- (tab
->n_var
- dim
);
1999 if (isl_tab_extend_vars(tab
, extra
) < 0)
2001 if (isl_tab_extend_cons(tab
, 2 * extra
) < 0)
2004 ctx
= isl_tab_get_ctx(tab
);
2005 sub
= isl_vec_alloc(ctx
, 1 + dim
+ n
);
2008 isl_seq_clr(sub
->el
+ 1 + dim
, n
);
2010 for (i
= 0; i
< n
; ++i
) {
2011 aff
= isl_aff_list_get_aff(list
, i
);
2014 if (isl_aff_is_nan(aff
)) {
2018 if (isl_tab_insert_var(tab
, dim
+ i
) < 0)
2020 isl_seq_cpy(sub
->el
, aff
->v
->el
+ 1, 1 + dim
);
2021 isl_int_neg(sub
->el
[1 + dim
+ i
], aff
->v
->el
[0]);
2022 if (isl_tab_add_eq(tab
, sub
->el
) < 0)
2024 isl_int_set_si(sub
->el
[1 + dim
+ i
], 0);
2036 /* Coalesce basic map "j" into basic map "i" after adding the extra integer
2037 * divisions in "i" but not in "j" to basic map "j", with values
2038 * specified by "list". The total number of elements in "list"
2039 * is equal to the number of integer divisions in "i", while the number
2040 * of NaN elements in the list is equal to the number of integer divisions
2042 * If no coalescing can be performed, then we need to revert basic map "j"
2043 * to its original state. We do the same if basic map "i" gets dropped
2044 * during the coalescing, even though this should not happen in practice
2045 * since we have already checked for "j" being a subset of "i"
2046 * before we reach this stage.
2048 static enum isl_change
coalesce_with_subs(int i
, int j
,
2049 struct isl_coalesce_info
*info
, __isl_keep isl_aff_list
*list
)
2051 isl_basic_map
*bmap_j
;
2052 struct isl_tab_undo
*snap
;
2054 enum isl_change change
;
2056 bmap_j
= isl_basic_map_copy(info
[j
].bmap
);
2057 info
[j
].bmap
= isl_basic_map_align_divs(info
[j
].bmap
, info
[i
].bmap
);
2061 snap
= isl_tab_snap(info
[j
].tab
);
2063 dim
= isl_basic_map_dim(bmap_j
, isl_dim_all
);
2064 dim
-= isl_basic_map_dim(bmap_j
, isl_dim_div
);
2065 if (add_subs(info
[j
].tab
, list
, dim
) < 0)
2068 change
= coalesce_local_pair(i
, j
, info
);
2069 if (change
!= isl_change_none
&& change
!= isl_change_drop_first
) {
2070 isl_basic_map_free(bmap_j
);
2072 isl_basic_map_free(info
[j
].bmap
);
2073 info
[j
].bmap
= bmap_j
;
2075 if (isl_tab_rollback(info
[j
].tab
, snap
) < 0)
2076 return isl_change_error
;
2081 isl_basic_map_free(bmap_j
);
2082 return isl_change_error
;
2085 /* Check if we can coalesce basic map "j" into basic map "i" after copying
2086 * those extra integer divisions in "i" that can be simplified away
2087 * using the extra equalities in "j".
2088 * All divs are assumed to be known and not contain any nested divs.
2090 * We first check if there are any extra equalities in "j" that we
2091 * can exploit. Then we check if every integer division in "i"
2092 * either already appears in "j" or can be simplified using the
2093 * extra equalities to a purely affine expression.
2094 * If these tests succeed, then we try to coalesce the two basic maps
2095 * by introducing extra dimensions in "j" corresponding to
2096 * the extra integer divsisions "i" fixed to the corresponding
2097 * purely affine expression.
2099 static enum isl_change
check_coalesce_into_eq(int i
, int j
,
2100 struct isl_coalesce_info
*info
)
2102 unsigned n_div_i
, n_div_j
;
2103 isl_basic_map
*hull_i
, *hull_j
;
2106 enum isl_change change
;
2108 n_div_i
= isl_basic_map_dim(info
[i
].bmap
, isl_dim_div
);
2109 n_div_j
= isl_basic_map_dim(info
[j
].bmap
, isl_dim_div
);
2110 if (n_div_i
<= n_div_j
)
2111 return isl_change_none
;
2112 if (info
[j
].bmap
->n_eq
== 0)
2113 return isl_change_none
;
2115 hull_i
= isl_basic_map_copy(info
[i
].bmap
);
2116 hull_i
= isl_basic_map_plain_affine_hull(hull_i
);
2117 hull_j
= isl_basic_map_copy(info
[j
].bmap
);
2118 hull_j
= isl_basic_map_plain_affine_hull(hull_j
);
2120 hull_j
= isl_basic_map_intersect(hull_j
, isl_basic_map_copy(hull_i
));
2121 equal
= isl_basic_map_plain_is_equal(hull_i
, hull_j
);
2122 empty
= isl_basic_map_plain_is_empty(hull_j
);
2123 isl_basic_map_free(hull_i
);
2125 if (equal
< 0 || empty
< 0)
2127 if (equal
|| empty
) {
2128 isl_basic_map_free(hull_j
);
2129 return isl_change_none
;
2132 list
= set_up_substitutions(info
[i
].bmap
, info
[j
].bmap
, hull_j
);
2135 if (isl_aff_list_n_aff(list
) < n_div_i
)
2136 change
= isl_change_none
;
2138 change
= coalesce_with_subs(i
, j
, info
, list
);
2140 isl_aff_list_free(list
);
2144 isl_basic_map_free(hull_j
);
2145 return isl_change_error
;
2148 /* Check if we can coalesce basic maps "i" and "j" after copying
2149 * those extra integer divisions in one of the basic maps that can
2150 * be simplified away using the extra equalities in the other basic map.
2151 * We require all divs to be known in both basic maps.
2152 * Furthermore, to simplify the comparison of div expressions,
2153 * we do not allow any nested integer divisions.
2155 static enum isl_change
check_coalesce_eq(int i
, int j
,
2156 struct isl_coalesce_info
*info
)
2159 enum isl_change change
;
2161 known
= isl_basic_map_divs_known(info
[i
].bmap
);
2162 if (known
< 0 || !known
)
2163 return known
< 0 ? isl_change_error
: isl_change_none
;
2164 known
= isl_basic_map_divs_known(info
[j
].bmap
);
2165 if (known
< 0 || !known
)
2166 return known
< 0 ? isl_change_error
: isl_change_none
;
2167 nested
= has_nested_div(info
[i
].bmap
);
2168 if (nested
< 0 || nested
)
2169 return nested
< 0 ? isl_change_error
: isl_change_none
;
2170 nested
= has_nested_div(info
[j
].bmap
);
2171 if (nested
< 0 || nested
)
2172 return nested
< 0 ? isl_change_error
: isl_change_none
;
2174 change
= check_coalesce_into_eq(i
, j
, info
);
2175 if (change
!= isl_change_none
)
2177 change
= check_coalesce_into_eq(j
, i
, info
);
2178 if (change
!= isl_change_none
)
2179 return invert_change(change
);
2181 return isl_change_none
;
2184 /* Check if the union of the given pair of basic maps
2185 * can be represented by a single basic map.
2186 * If so, replace the pair by the single basic map and return
2187 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
2188 * Otherwise, return isl_change_none.
2190 * We first check if the two basic maps live in the same local space.
2191 * If so, we do the complete check. Otherwise, we check if one is
2192 * an obvious subset of the other or if the extra integer divisions
2193 * of one basic map can be simplified away using the extra equalities
2194 * of the other basic map.
2196 static enum isl_change
coalesce_pair(int i
, int j
,
2197 struct isl_coalesce_info
*info
)
2200 enum isl_change change
;
2202 same
= same_divs(info
[i
].bmap
, info
[j
].bmap
);
2204 return isl_change_error
;
2206 return coalesce_local_pair(i
, j
, info
);
2208 change
= check_coalesce_subset(i
, j
, info
);
2209 if (change
!= isl_change_none
)
2212 return check_coalesce_eq(i
, j
, info
);
2215 /* Pairwise coalesce the basic maps described by the "n" elements of "info",
2216 * skipping basic maps that have been removed (either before or within
2219 * For each basic map i, we check if it can be coalesced with respect
2220 * to any previously considered basic map j.
2221 * If i gets dropped (because it was a subset of some j), then
2222 * we can move on to the next basic map.
2223 * If j gets dropped, we need to continue checking against the other
2224 * previously considered basic maps.
2225 * If the two basic maps got fused, then we recheck the fused basic map
2226 * against the previously considered basic maps.
2228 static int coalesce(isl_ctx
*ctx
, int n
, struct isl_coalesce_info
*info
)
2232 for (i
= n
- 2; i
>= 0; --i
) {
2233 if (info
[i
].removed
)
2235 for (j
= i
+ 1; j
< n
; ++j
) {
2236 enum isl_change changed
;
2238 if (info
[j
].removed
)
2240 if (info
[i
].removed
)
2241 isl_die(ctx
, isl_error_internal
,
2242 "basic map unexpectedly removed",
2244 changed
= coalesce_pair(i
, j
, info
);
2246 case isl_change_error
:
2248 case isl_change_none
:
2249 case isl_change_drop_second
:
2251 case isl_change_drop_first
:
2254 case isl_change_fuse
:
2264 /* Update the basic maps in "map" based on the information in "info".
2265 * In particular, remove the basic maps that have been marked removed and
2266 * update the others based on the information in the corresponding tableau.
2267 * Since we detected implicit equalities without calling
2268 * isl_basic_map_gauss, we need to do it now.
2270 static __isl_give isl_map
*update_basic_maps(__isl_take isl_map
*map
,
2271 int n
, struct isl_coalesce_info
*info
)
2278 for (i
= n
- 1; i
>= 0; --i
) {
2279 if (info
[i
].removed
) {
2280 isl_basic_map_free(map
->p
[i
]);
2281 if (i
!= map
->n
- 1)
2282 map
->p
[i
] = map
->p
[map
->n
- 1];
2287 info
[i
].bmap
= isl_basic_map_update_from_tab(info
[i
].bmap
,
2289 info
[i
].bmap
= isl_basic_map_gauss(info
[i
].bmap
, NULL
);
2290 info
[i
].bmap
= isl_basic_map_finalize(info
[i
].bmap
);
2292 return isl_map_free(map
);
2293 ISL_F_SET(info
[i
].bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
2294 ISL_F_SET(info
[i
].bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
2295 isl_basic_map_free(map
->p
[i
]);
2296 map
->p
[i
] = info
[i
].bmap
;
2297 info
[i
].bmap
= NULL
;
2303 /* For each pair of basic maps in the map, check if the union of the two
2304 * can be represented by a single basic map.
2305 * If so, replace the pair by the single basic map and start over.
2307 * Since we are constructing the tableaus of the basic maps anyway,
2308 * we exploit them to detect implicit equalities and redundant constraints.
2309 * This also helps the coalescing as it can ignore the redundant constraints.
2310 * In order to avoid confusion, we make all implicit equalities explicit
2311 * in the basic maps. We don't call isl_basic_map_gauss, though,
2312 * as that may affect the number of constraints.
2313 * This means that we have to call isl_basic_map_gauss at the end
2314 * of the computation (in update_basic_maps) to ensure that
2315 * the basic maps are not left in an unexpected state.
2317 struct isl_map
*isl_map_coalesce(struct isl_map
*map
)
2322 struct isl_coalesce_info
*info
= NULL
;
2324 map
= isl_map_remove_empty_parts(map
);
2331 ctx
= isl_map_get_ctx(map
);
2332 map
= isl_map_sort_divs(map
);
2333 map
= isl_map_cow(map
);
2340 info
= isl_calloc_array(map
->ctx
, struct isl_coalesce_info
, n
);
2344 for (i
= 0; i
< map
->n
; ++i
) {
2345 info
[i
].bmap
= isl_basic_map_copy(map
->p
[i
]);
2346 info
[i
].tab
= isl_tab_from_basic_map(info
[i
].bmap
, 0);
2349 if (!ISL_F_ISSET(info
[i
].bmap
, ISL_BASIC_MAP_NO_IMPLICIT
))
2350 if (isl_tab_detect_implicit_equalities(info
[i
].tab
) < 0)
2352 info
[i
].bmap
= isl_tab_make_equalities_explicit(info
[i
].tab
,
2356 if (!ISL_F_ISSET(info
[i
].bmap
, ISL_BASIC_MAP_NO_REDUNDANT
))
2357 if (isl_tab_detect_redundant(info
[i
].tab
) < 0)
2360 for (i
= map
->n
- 1; i
>= 0; --i
)
2361 if (info
[i
].tab
->empty
)
2364 if (coalesce(ctx
, n
, info
) < 0)
2367 map
= update_basic_maps(map
, n
, info
);
2369 clear_coalesce_info(n
, info
);
2373 clear_coalesce_info(n
, info
);
2378 /* For each pair of basic sets in the set, check if the union of the two
2379 * can be represented by a single basic set.
2380 * If so, replace the pair by the single basic set and start over.
2382 struct isl_set
*isl_set_coalesce(struct isl_set
*set
)
2384 return (struct isl_set
*)isl_map_coalesce((struct isl_map
*)set
);