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 * "hull_hash" identifies the affine space in which "bmap" lives.
156 * "removed" is set if this basic map has been removed from the map
157 * "simplify" is set if this basic map may have some unknown integer
158 * divisions that were not present in the input basic maps. The basic
159 * map should then be simplified such that we may be able to find
160 * a definition among the constraints.
162 * "eq" and "ineq" are only set if we are currently trying to coalesce
163 * this basic map with another basic map, in which case they represent
164 * the position of the inequalities of this basic map with respect to
165 * the other basic map. The number of elements in the "eq" array
166 * is twice the number of equalities in the "bmap", corresponding
167 * to the two inequalities that make up each equality.
169 struct isl_coalesce_info
{
179 /* Compute the hash of the (apparent) affine hull of info->bmap (with
180 * the existentially quantified variables removed) and store it
183 static int coalesce_info_set_hull_hash(struct isl_coalesce_info
*info
)
188 hull
= isl_basic_map_copy(info
->bmap
);
189 hull
= isl_basic_map_plain_affine_hull(hull
);
190 n_div
= isl_basic_map_dim(hull
, isl_dim_div
);
191 hull
= isl_basic_map_drop_constraints_involving_dims(hull
,
192 isl_dim_div
, 0, n_div
);
193 info
->hull_hash
= isl_basic_map_get_hash(hull
);
194 isl_basic_map_free(hull
);
196 return hull
? 0 : -1;
199 /* Free all the allocated memory in an array
200 * of "n" isl_coalesce_info elements.
202 static void clear_coalesce_info(int n
, struct isl_coalesce_info
*info
)
209 for (i
= 0; i
< n
; ++i
) {
210 isl_basic_map_free(info
[i
].bmap
);
211 isl_tab_free(info
[i
].tab
);
217 /* Drop the basic map represented by "info".
218 * That is, clear the memory associated to the entry and
219 * mark it as having been removed.
221 static void drop(struct isl_coalesce_info
*info
)
223 info
->bmap
= isl_basic_map_free(info
->bmap
);
224 isl_tab_free(info
->tab
);
229 /* Exchange the information in "info1" with that in "info2".
231 static void exchange(struct isl_coalesce_info
*info1
,
232 struct isl_coalesce_info
*info2
)
234 struct isl_coalesce_info info
;
241 /* This type represents the kind of change that has been performed
242 * while trying to coalesce two basic maps.
244 * isl_change_none: nothing was changed
245 * isl_change_drop_first: the first basic map was removed
246 * isl_change_drop_second: the second basic map was removed
247 * isl_change_fuse: the two basic maps were replaced by a new basic map.
250 isl_change_error
= -1,
252 isl_change_drop_first
,
253 isl_change_drop_second
,
257 /* Update "change" based on an interchange of the first and the second
258 * basic map. That is, interchange isl_change_drop_first and
259 * isl_change_drop_second.
261 static enum isl_change
invert_change(enum isl_change change
)
264 case isl_change_error
:
265 return isl_change_error
;
266 case isl_change_none
:
267 return isl_change_none
;
268 case isl_change_drop_first
:
269 return isl_change_drop_second
;
270 case isl_change_drop_second
:
271 return isl_change_drop_first
;
272 case isl_change_fuse
:
273 return isl_change_fuse
;
277 /* Add the valid constraints of the basic map represented by "info"
278 * to "bmap". "len" is the size of the constraints.
279 * If only one of the pair of inequalities that make up an equality
280 * is valid, then add that inequality.
282 static __isl_give isl_basic_map
*add_valid_constraints(
283 __isl_take isl_basic_map
*bmap
, struct isl_coalesce_info
*info
,
291 for (k
= 0; k
< info
->bmap
->n_eq
; ++k
) {
292 if (info
->eq
[2 * k
] == STATUS_VALID
&&
293 info
->eq
[2 * k
+ 1] == STATUS_VALID
) {
294 l
= isl_basic_map_alloc_equality(bmap
);
296 return isl_basic_map_free(bmap
);
297 isl_seq_cpy(bmap
->eq
[l
], info
->bmap
->eq
[k
], len
);
298 } else if (info
->eq
[2 * k
] == STATUS_VALID
) {
299 l
= isl_basic_map_alloc_inequality(bmap
);
301 return isl_basic_map_free(bmap
);
302 isl_seq_neg(bmap
->ineq
[l
], info
->bmap
->eq
[k
], len
);
303 } else if (info
->eq
[2 * k
+ 1] == STATUS_VALID
) {
304 l
= isl_basic_map_alloc_inequality(bmap
);
306 return isl_basic_map_free(bmap
);
307 isl_seq_cpy(bmap
->ineq
[l
], info
->bmap
->eq
[k
], len
);
311 for (k
= 0; k
< info
->bmap
->n_ineq
; ++k
) {
312 if (info
->ineq
[k
] != STATUS_VALID
)
314 l
= isl_basic_map_alloc_inequality(bmap
);
316 return isl_basic_map_free(bmap
);
317 isl_seq_cpy(bmap
->ineq
[l
], info
->bmap
->ineq
[k
], len
);
323 /* Is "bmap" defined by a number of (non-redundant) constraints that
324 * is greater than the number of constraints of basic maps i and j combined?
325 * Equalities are counted as two inequalities.
327 static int number_of_constraints_increases(int i
, int j
,
328 struct isl_coalesce_info
*info
,
329 __isl_keep isl_basic_map
*bmap
, struct isl_tab
*tab
)
333 n_old
= 2 * info
[i
].bmap
->n_eq
+ info
[i
].bmap
->n_ineq
;
334 n_old
+= 2 * info
[j
].bmap
->n_eq
+ info
[j
].bmap
->n_ineq
;
336 n_new
= 2 * bmap
->n_eq
;
337 for (k
= 0; k
< bmap
->n_ineq
; ++k
)
338 if (!isl_tab_is_redundant(tab
, bmap
->n_eq
+ k
))
341 return n_new
> n_old
;
344 /* Replace the pair of basic maps i and j by the basic map bounded
345 * by the valid constraints in both basic maps and the constraints
346 * in extra (if not NULL).
347 * Place the fused basic map in the position that is the smallest of i and j.
349 * If "detect_equalities" is set, then look for equalities encoded
350 * as pairs of inequalities.
351 * If "check_number" is set, then the original basic maps are only
352 * replaced if the total number of constraints does not increase.
353 * While the number of integer divisions in the two basic maps
354 * is assumed to be the same, the actual definitions may be different.
355 * We only copy the definition from one of the basic map if it is
356 * the same as that of the other basic map. Otherwise, we mark
357 * the integer division as unknown and schedule for the basic map
358 * to be simplified in an attempt to recover the integer division definition.
360 static enum isl_change
fuse(int i
, int j
, struct isl_coalesce_info
*info
,
361 __isl_keep isl_mat
*extra
, int detect_equalities
, int check_number
)
364 struct isl_basic_map
*fused
= NULL
;
365 struct isl_tab
*fused_tab
= NULL
;
366 unsigned total
= isl_basic_map_total_dim(info
[i
].bmap
);
367 unsigned extra_rows
= extra
? extra
->n_row
: 0;
368 unsigned n_eq
, n_ineq
;
371 return fuse(j
, i
, info
, extra
, detect_equalities
, check_number
);
373 n_eq
= info
[i
].bmap
->n_eq
+ info
[j
].bmap
->n_eq
;
374 n_ineq
= info
[i
].bmap
->n_ineq
+ info
[j
].bmap
->n_ineq
;
375 fused
= isl_basic_map_alloc_space(isl_space_copy(info
[i
].bmap
->dim
),
376 info
[i
].bmap
->n_div
, n_eq
, n_eq
+ n_ineq
+ extra_rows
);
377 fused
= add_valid_constraints(fused
, &info
[i
], 1 + total
);
378 fused
= add_valid_constraints(fused
, &info
[j
], 1 + total
);
382 for (k
= 0; k
< info
[i
].bmap
->n_div
; ++k
) {
383 int l
= isl_basic_map_alloc_div(fused
);
386 if (isl_seq_eq(info
[i
].bmap
->div
[k
], info
[j
].bmap
->div
[k
],
388 isl_seq_cpy(fused
->div
[l
], info
[i
].bmap
->div
[k
],
391 isl_int_set_si(fused
->div
[l
][0], 0);
392 info
[i
].simplify
= 1;
396 for (k
= 0; k
< extra_rows
; ++k
) {
397 l
= isl_basic_map_alloc_inequality(fused
);
400 isl_seq_cpy(fused
->ineq
[l
], extra
->row
[k
], 1 + total
);
403 if (detect_equalities
)
404 fused
= isl_basic_map_detect_inequality_pairs(fused
, NULL
);
405 fused
= isl_basic_map_gauss(fused
, NULL
);
406 ISL_F_SET(fused
, ISL_BASIC_MAP_FINAL
);
407 if (ISL_F_ISSET(info
[i
].bmap
, ISL_BASIC_MAP_RATIONAL
) &&
408 ISL_F_ISSET(info
[j
].bmap
, ISL_BASIC_MAP_RATIONAL
))
409 ISL_F_SET(fused
, ISL_BASIC_MAP_RATIONAL
);
411 fused_tab
= isl_tab_from_basic_map(fused
, 0);
412 if (isl_tab_detect_redundant(fused_tab
) < 0)
416 number_of_constraints_increases(i
, j
, info
, fused
, fused_tab
)) {
417 isl_tab_free(fused_tab
);
418 isl_basic_map_free(fused
);
419 return isl_change_none
;
422 info
[i
].simplify
|= info
[j
].simplify
;
423 isl_basic_map_free(info
[i
].bmap
);
424 info
[i
].bmap
= fused
;
425 isl_tab_free(info
[i
].tab
);
426 info
[i
].tab
= fused_tab
;
429 return isl_change_fuse
;
431 isl_tab_free(fused_tab
);
432 isl_basic_map_free(fused
);
433 return isl_change_error
;
436 /* Given a pair of basic maps i and j such that all constraints are either
437 * "valid" or "cut", check if the facets corresponding to the "cut"
438 * constraints of i lie entirely within basic map j.
439 * If so, replace the pair by the basic map consisting of the valid
440 * constraints in both basic maps.
441 * Checking whether the facet lies entirely within basic map j
442 * is performed by checking whether the constraints of basic map j
443 * are valid for the facet. These tests are performed on a rational
444 * tableau to avoid the theoretical possibility that a constraint
445 * that was considered to be a cut constraint for the entire basic map i
446 * happens to be considered to be a valid constraint for the facet,
447 * even though it cuts off the same rational points.
449 * To see that we are not introducing any extra points, call the
450 * two basic maps A and B and the resulting map U and let x
451 * be an element of U \setminus ( A \cup B ).
452 * A line connecting x with an element of A \cup B meets a facet F
453 * of either A or B. Assume it is a facet of B and let c_1 be
454 * the corresponding facet constraint. We have c_1(x) < 0 and
455 * so c_1 is a cut constraint. This implies that there is some
456 * (possibly rational) point x' satisfying the constraints of A
457 * and the opposite of c_1 as otherwise c_1 would have been marked
458 * valid for A. The line connecting x and x' meets a facet of A
459 * in a (possibly rational) point that also violates c_1, but this
460 * is impossible since all cut constraints of B are valid for all
462 * In case F is a facet of A rather than B, then we can apply the
463 * above reasoning to find a facet of B separating x from A \cup B first.
465 static enum isl_change
check_facets(int i
, int j
,
466 struct isl_coalesce_info
*info
)
469 struct isl_tab_undo
*snap
, *snap2
;
470 unsigned n_eq
= info
[i
].bmap
->n_eq
;
472 snap
= isl_tab_snap(info
[i
].tab
);
473 if (isl_tab_mark_rational(info
[i
].tab
) < 0)
474 return isl_change_error
;
475 snap2
= isl_tab_snap(info
[i
].tab
);
477 for (k
= 0; k
< info
[i
].bmap
->n_ineq
; ++k
) {
478 if (info
[i
].ineq
[k
] != STATUS_CUT
)
480 if (isl_tab_select_facet(info
[i
].tab
, n_eq
+ k
) < 0)
481 return isl_change_error
;
482 for (l
= 0; l
< info
[j
].bmap
->n_ineq
; ++l
) {
484 if (info
[j
].ineq
[l
] != STATUS_CUT
)
486 stat
= status_in(info
[j
].bmap
->ineq
[l
], info
[i
].tab
);
487 if (stat
!= STATUS_VALID
)
490 if (isl_tab_rollback(info
[i
].tab
, snap2
) < 0)
491 return isl_change_error
;
492 if (l
< info
[j
].bmap
->n_ineq
)
496 if (k
< info
[i
].bmap
->n_ineq
) {
497 if (isl_tab_rollback(info
[i
].tab
, snap
) < 0)
498 return isl_change_error
;
499 return isl_change_none
;
501 return fuse(i
, j
, info
, NULL
, 0, 0);
504 /* Check if info->bmap contains the basic map represented
505 * by the tableau "tab".
506 * For each equality, we check both the constraint itself
507 * (as an inequality) and its negation. Make sure the
508 * equality is returned to its original state before returning.
510 static int contains(struct isl_coalesce_info
*info
, struct isl_tab
*tab
)
514 isl_basic_map
*bmap
= info
->bmap
;
516 dim
= isl_basic_map_total_dim(bmap
);
517 for (k
= 0; k
< bmap
->n_eq
; ++k
) {
519 isl_seq_neg(bmap
->eq
[k
], bmap
->eq
[k
], 1 + dim
);
520 stat
= status_in(bmap
->eq
[k
], tab
);
521 isl_seq_neg(bmap
->eq
[k
], bmap
->eq
[k
], 1 + dim
);
522 if (stat
!= STATUS_VALID
)
524 stat
= status_in(bmap
->eq
[k
], tab
);
525 if (stat
!= STATUS_VALID
)
529 for (k
= 0; k
< bmap
->n_ineq
; ++k
) {
531 if (info
->ineq
[k
] == STATUS_REDUNDANT
)
533 stat
= status_in(bmap
->ineq
[k
], tab
);
534 if (stat
!= STATUS_VALID
)
540 /* Basic map "i" has an inequality (say "k") that is adjacent
541 * to some inequality of basic map "j". All the other inequalities
543 * Check if basic map "j" forms an extension of basic map "i".
545 * Note that this function is only called if some of the equalities or
546 * inequalities of basic map "j" do cut basic map "i". The function is
547 * correct even if there are no such cut constraints, but in that case
548 * the additional checks performed by this function are overkill.
550 * In particular, we replace constraint k, say f >= 0, by constraint
551 * f <= -1, add the inequalities of "j" that are valid for "i"
552 * and check if the result is a subset of basic map "j".
553 * If so, then we know that this result is exactly equal to basic map "j"
554 * since all its constraints are valid for basic map "j".
555 * By combining the valid constraints of "i" (all equalities and all
556 * inequalities except "k") and the valid constraints of "j" we therefore
557 * obtain a basic map that is equal to their union.
558 * In this case, there is no need to perform a rollback of the tableau
559 * since it is going to be destroyed in fuse().
565 * |_______| _ |_________\
577 static enum isl_change
is_adj_ineq_extension(int i
, int j
,
578 struct isl_coalesce_info
*info
)
581 struct isl_tab_undo
*snap
;
582 unsigned n_eq
= info
[i
].bmap
->n_eq
;
583 unsigned total
= isl_basic_map_total_dim(info
[i
].bmap
);
586 if (isl_tab_extend_cons(info
[i
].tab
, 1 + info
[j
].bmap
->n_ineq
) < 0)
587 return isl_change_error
;
589 for (k
= 0; k
< info
[i
].bmap
->n_ineq
; ++k
)
590 if (info
[i
].ineq
[k
] == STATUS_ADJ_INEQ
)
592 if (k
>= info
[i
].bmap
->n_ineq
)
593 isl_die(isl_basic_map_get_ctx(info
[i
].bmap
), isl_error_internal
,
594 "info[i].ineq should have exactly one STATUS_ADJ_INEQ",
595 return isl_change_error
);
597 snap
= isl_tab_snap(info
[i
].tab
);
599 if (isl_tab_unrestrict(info
[i
].tab
, n_eq
+ k
) < 0)
600 return isl_change_error
;
602 isl_seq_neg(info
[i
].bmap
->ineq
[k
], info
[i
].bmap
->ineq
[k
], 1 + total
);
603 isl_int_sub_ui(info
[i
].bmap
->ineq
[k
][0], info
[i
].bmap
->ineq
[k
][0], 1);
604 r
= isl_tab_add_ineq(info
[i
].tab
, info
[i
].bmap
->ineq
[k
]);
605 isl_seq_neg(info
[i
].bmap
->ineq
[k
], info
[i
].bmap
->ineq
[k
], 1 + total
);
606 isl_int_sub_ui(info
[i
].bmap
->ineq
[k
][0], info
[i
].bmap
->ineq
[k
][0], 1);
608 return isl_change_error
;
610 for (k
= 0; k
< info
[j
].bmap
->n_ineq
; ++k
) {
611 if (info
[j
].ineq
[k
] != STATUS_VALID
)
613 if (isl_tab_add_ineq(info
[i
].tab
, info
[j
].bmap
->ineq
[k
]) < 0)
614 return isl_change_error
;
617 if (contains(&info
[j
], info
[i
].tab
))
618 return fuse(i
, j
, info
, NULL
, 0, 0);
620 if (isl_tab_rollback(info
[i
].tab
, snap
) < 0)
621 return isl_change_error
;
623 return isl_change_none
;
627 /* Both basic maps have at least one inequality with and adjacent
628 * (but opposite) inequality in the other basic map.
629 * Check that there are no cut constraints and that there is only
630 * a single pair of adjacent inequalities.
631 * If so, we can replace the pair by a single basic map described
632 * by all but the pair of adjacent inequalities.
633 * Any additional points introduced lie strictly between the two
634 * adjacent hyperplanes and can therefore be integral.
643 * The test for a single pair of adjancent inequalities is important
644 * for avoiding the combination of two basic maps like the following
654 * If there are some cut constraints on one side, then we may
655 * still be able to fuse the two basic maps, but we need to perform
656 * some additional checks in is_adj_ineq_extension.
658 static enum isl_change
check_adj_ineq(int i
, int j
,
659 struct isl_coalesce_info
*info
)
661 int count_i
, count_j
;
664 count_i
= count(info
[i
].ineq
, info
[i
].bmap
->n_ineq
, STATUS_ADJ_INEQ
);
665 count_j
= count(info
[j
].ineq
, info
[j
].bmap
->n_ineq
, STATUS_ADJ_INEQ
);
667 if (count_i
!= 1 && count_j
!= 1)
668 return isl_change_none
;
670 cut_i
= any(info
[i
].eq
, 2 * info
[i
].bmap
->n_eq
, STATUS_CUT
) ||
671 any(info
[i
].ineq
, info
[i
].bmap
->n_ineq
, STATUS_CUT
);
672 cut_j
= any(info
[j
].eq
, 2 * info
[j
].bmap
->n_eq
, STATUS_CUT
) ||
673 any(info
[j
].ineq
, info
[j
].bmap
->n_ineq
, STATUS_CUT
);
675 if (!cut_i
&& !cut_j
&& count_i
== 1 && count_j
== 1)
676 return fuse(i
, j
, info
, NULL
, 0, 0);
678 if (count_i
== 1 && !cut_i
)
679 return is_adj_ineq_extension(i
, j
, info
);
681 if (count_j
== 1 && !cut_j
)
682 return is_adj_ineq_extension(j
, i
, info
);
684 return isl_change_none
;
687 /* Basic map "i" has an inequality "k" that is adjacent to some equality
688 * of basic map "j". All the other inequalities are valid for "j".
689 * Check if basic map "j" forms an extension of basic map "i".
691 * In particular, we relax constraint "k", compute the corresponding
692 * facet and check whether it is included in the other basic map.
693 * If so, we know that relaxing the constraint extends the basic
694 * map with exactly the other basic map (we already know that this
695 * other basic map is included in the extension, because there
696 * were no "cut" inequalities in "i") and we can replace the
697 * two basic maps by this extension.
698 * Each integer division that does not have exactly the same
699 * definition in "i" and "j" is marked unknown and the basic map
700 * is scheduled to be simplified in an attempt to recover
701 * the integer division definition.
702 * Place this extension in the position that is the smallest of i and j.
710 static enum isl_change
is_adj_eq_extension(int i
, int j
, int k
,
711 struct isl_coalesce_info
*info
)
713 int change
= isl_change_none
;
715 struct isl_tab_undo
*snap
, *snap2
;
716 unsigned n_eq
= info
[i
].bmap
->n_eq
;
718 if (isl_tab_is_equality(info
[i
].tab
, n_eq
+ k
))
719 return isl_change_none
;
721 snap
= isl_tab_snap(info
[i
].tab
);
722 if (isl_tab_relax(info
[i
].tab
, n_eq
+ k
) < 0)
723 return isl_change_error
;
724 snap2
= isl_tab_snap(info
[i
].tab
);
725 if (isl_tab_select_facet(info
[i
].tab
, n_eq
+ k
) < 0)
726 return isl_change_error
;
727 super
= contains(&info
[j
], info
[i
].tab
);
732 if (isl_tab_rollback(info
[i
].tab
, snap2
) < 0)
733 return isl_change_error
;
734 info
[i
].bmap
= isl_basic_map_cow(info
[i
].bmap
);
736 return isl_change_error
;
737 total
= isl_basic_map_total_dim(info
[i
].bmap
);
738 for (l
= 0; l
< info
[i
].bmap
->n_div
; ++l
)
739 if (!isl_seq_eq(info
[i
].bmap
->div
[l
],
740 info
[j
].bmap
->div
[l
], 1 + 1 + total
)) {
741 isl_int_set_si(info
[i
].bmap
->div
[l
][0], 0);
742 info
[i
].simplify
= 1;
744 isl_int_add_ui(info
[i
].bmap
->ineq
[k
][0],
745 info
[i
].bmap
->ineq
[k
][0], 1);
746 ISL_F_SET(info
[i
].bmap
, ISL_BASIC_MAP_FINAL
);
749 exchange(&info
[i
], &info
[j
]);
750 change
= isl_change_fuse
;
752 if (isl_tab_rollback(info
[i
].tab
, snap
) < 0)
753 return isl_change_error
;
758 /* Data structure that keeps track of the wrapping constraints
759 * and of information to bound the coefficients of those constraints.
761 * bound is set if we want to apply a bound on the coefficients
762 * mat contains the wrapping constraints
763 * max is the bound on the coefficients (if bound is set)
771 /* Update wraps->max to be greater than or equal to the coefficients
772 * in the equalities and inequalities of info->bmap that can be removed
773 * if we end up applying wrapping.
775 static void wraps_update_max(struct isl_wraps
*wraps
,
776 struct isl_coalesce_info
*info
)
780 unsigned total
= isl_basic_map_total_dim(info
->bmap
);
784 for (k
= 0; k
< info
->bmap
->n_eq
; ++k
) {
785 if (info
->eq
[2 * k
] == STATUS_VALID
&&
786 info
->eq
[2 * k
+ 1] == STATUS_VALID
)
788 isl_seq_abs_max(info
->bmap
->eq
[k
] + 1, total
, &max_k
);
789 if (isl_int_abs_gt(max_k
, wraps
->max
))
790 isl_int_set(wraps
->max
, max_k
);
793 for (k
= 0; k
< info
->bmap
->n_ineq
; ++k
) {
794 if (info
->ineq
[k
] == STATUS_VALID
||
795 info
->ineq
[k
] == STATUS_REDUNDANT
)
797 isl_seq_abs_max(info
->bmap
->ineq
[k
] + 1, total
, &max_k
);
798 if (isl_int_abs_gt(max_k
, wraps
->max
))
799 isl_int_set(wraps
->max
, max_k
);
802 isl_int_clear(max_k
);
805 /* Initialize the isl_wraps data structure.
806 * If we want to bound the coefficients of the wrapping constraints,
807 * we set wraps->max to the largest coefficient
808 * in the equalities and inequalities that can be removed if we end up
811 static void wraps_init(struct isl_wraps
*wraps
, __isl_take isl_mat
*mat
,
812 struct isl_coalesce_info
*info
, int i
, int j
)
820 ctx
= isl_mat_get_ctx(mat
);
821 wraps
->bound
= isl_options_get_coalesce_bounded_wrapping(ctx
);
824 isl_int_init(wraps
->max
);
825 isl_int_set_si(wraps
->max
, 0);
826 wraps_update_max(wraps
, &info
[i
]);
827 wraps_update_max(wraps
, &info
[j
]);
830 /* Free the contents of the isl_wraps data structure.
832 static void wraps_free(struct isl_wraps
*wraps
)
834 isl_mat_free(wraps
->mat
);
836 isl_int_clear(wraps
->max
);
839 /* Is the wrapping constraint in row "row" allowed?
841 * If wraps->bound is set, we check that none of the coefficients
842 * is greater than wraps->max.
844 static int allow_wrap(struct isl_wraps
*wraps
, int row
)
851 for (i
= 1; i
< wraps
->mat
->n_col
; ++i
)
852 if (isl_int_abs_gt(wraps
->mat
->row
[row
][i
], wraps
->max
))
858 /* Wrap "ineq" (or its opposite if "negate" is set) around "bound"
859 * to include "set" and add the result in position "w" of "wraps".
860 * "len" is the total number of coefficients in "bound" and "ineq".
861 * Return 1 on success, 0 on failure and -1 on error.
862 * Wrapping can fail if the result of wrapping is equal to "bound"
863 * or if we want to bound the sizes of the coefficients and
864 * the wrapped constraint does not satisfy this bound.
866 static int add_wrap(struct isl_wraps
*wraps
, int w
, isl_int
*bound
,
867 isl_int
*ineq
, unsigned len
, __isl_keep isl_set
*set
, int negate
)
869 isl_seq_cpy(wraps
->mat
->row
[w
], bound
, len
);
871 isl_seq_neg(wraps
->mat
->row
[w
+ 1], ineq
, len
);
872 ineq
= wraps
->mat
->row
[w
+ 1];
874 if (!isl_set_wrap_facet(set
, wraps
->mat
->row
[w
], ineq
))
876 if (isl_seq_eq(wraps
->mat
->row
[w
], bound
, len
))
878 if (!allow_wrap(wraps
, w
))
883 /* For each constraint in info->bmap that is not redundant (as determined
884 * by info->tab) and that is not a valid constraint for the other basic map,
885 * wrap the constraint around "bound" such that it includes the whole
886 * set "set" and append the resulting constraint to "wraps".
887 * Note that the constraints that are valid for the other basic map
888 * will be added to the combined basic map by default, so there is
889 * no need to wrap them.
890 * The caller wrap_in_facets even relies on this function not wrapping
891 * any constraints that are already valid.
892 * "wraps" is assumed to have been pre-allocated to the appropriate size.
893 * wraps->n_row is the number of actual wrapped constraints that have
895 * If any of the wrapping problems results in a constraint that is
896 * identical to "bound", then this means that "set" is unbounded in such
897 * way that no wrapping is possible. If this happens then wraps->n_row
899 * Similarly, if we want to bound the coefficients of the wrapping
900 * constraints and a newly added wrapping constraint does not
901 * satisfy the bound, then wraps->n_row is also reset to zero.
903 static int add_wraps(struct isl_wraps
*wraps
, struct isl_coalesce_info
*info
,
904 isl_int
*bound
, __isl_keep isl_set
*set
)
909 isl_basic_map
*bmap
= info
->bmap
;
910 unsigned len
= 1 + isl_basic_map_total_dim(bmap
);
912 w
= wraps
->mat
->n_row
;
914 for (l
= 0; l
< bmap
->n_ineq
; ++l
) {
915 if (info
->ineq
[l
] == STATUS_VALID
||
916 info
->ineq
[l
] == STATUS_REDUNDANT
)
918 if (isl_seq_is_neg(bound
, bmap
->ineq
[l
], len
))
920 if (isl_seq_eq(bound
, bmap
->ineq
[l
], len
))
922 if (isl_tab_is_redundant(info
->tab
, bmap
->n_eq
+ l
))
925 added
= add_wrap(wraps
, w
, bound
, bmap
->ineq
[l
], len
, set
, 0);
932 for (l
= 0; l
< bmap
->n_eq
; ++l
) {
933 if (isl_seq_is_neg(bound
, bmap
->eq
[l
], len
))
935 if (isl_seq_eq(bound
, bmap
->eq
[l
], len
))
938 for (m
= 0; m
< 2; ++m
) {
939 if (info
->eq
[2 * l
+ m
] == STATUS_VALID
)
941 added
= add_wrap(wraps
, w
, bound
, bmap
->eq
[l
], len
,
951 wraps
->mat
->n_row
= w
;
954 wraps
->mat
->n_row
= 0;
958 /* Check if the constraints in "wraps" from "first" until the last
959 * are all valid for the basic set represented by "tab".
960 * If not, wraps->n_row is set to zero.
962 static int check_wraps(__isl_keep isl_mat
*wraps
, int first
,
967 for (i
= first
; i
< wraps
->n_row
; ++i
) {
968 enum isl_ineq_type type
;
969 type
= isl_tab_ineq_type(tab
, wraps
->row
[i
]);
970 if (type
== isl_ineq_error
)
972 if (type
== isl_ineq_redundant
)
981 /* Return a set that corresponds to the non-redundant constraints
982 * (as recorded in tab) of bmap.
984 * It's important to remove the redundant constraints as some
985 * of the other constraints may have been modified after the
986 * constraints were marked redundant.
987 * In particular, a constraint may have been relaxed.
988 * Redundant constraints are ignored when a constraint is relaxed
989 * and should therefore continue to be ignored ever after.
990 * Otherwise, the relaxation might be thwarted by some of
993 * Update the underlying set to ensure that the dimension doesn't change.
994 * Otherwise the integer divisions could get dropped if the tab
995 * turns out to be empty.
997 static __isl_give isl_set
*set_from_updated_bmap(__isl_keep isl_basic_map
*bmap
,
1000 isl_basic_set
*bset
;
1002 bmap
= isl_basic_map_copy(bmap
);
1003 bset
= isl_basic_map_underlying_set(bmap
);
1004 bset
= isl_basic_set_cow(bset
);
1005 bset
= isl_basic_set_update_from_tab(bset
, tab
);
1006 return isl_set_from_basic_set(bset
);
1009 /* Wrap the constraints of info->bmap that bound the facet defined
1010 * by inequality "k" around (the opposite of) this inequality to
1011 * include "set". "bound" may be used to store the negated inequality.
1012 * Since the wrapped constraints are not guaranteed to contain the whole
1013 * of info->bmap, we check them in check_wraps.
1014 * If any of the wrapped constraints turn out to be invalid, then
1015 * check_wraps will reset wrap->n_row to zero.
1017 static int add_wraps_around_facet(struct isl_wraps
*wraps
,
1018 struct isl_coalesce_info
*info
, int k
, isl_int
*bound
,
1019 __isl_keep isl_set
*set
)
1021 struct isl_tab_undo
*snap
;
1023 unsigned total
= isl_basic_map_total_dim(info
->bmap
);
1025 snap
= isl_tab_snap(info
->tab
);
1027 if (isl_tab_select_facet(info
->tab
, info
->bmap
->n_eq
+ k
) < 0)
1029 if (isl_tab_detect_redundant(info
->tab
) < 0)
1032 isl_seq_neg(bound
, info
->bmap
->ineq
[k
], 1 + total
);
1034 n
= wraps
->mat
->n_row
;
1035 if (add_wraps(wraps
, info
, bound
, set
) < 0)
1038 if (isl_tab_rollback(info
->tab
, snap
) < 0)
1040 if (check_wraps(wraps
->mat
, n
, info
->tab
) < 0)
1046 /* Given a basic set i with a constraint k that is adjacent to
1047 * basic set j, check if we can wrap
1048 * both the facet corresponding to k (if "wrap_facet" is set) and basic map j
1049 * (always) around their ridges to include the other set.
1050 * If so, replace the pair of basic sets by their union.
1052 * All constraints of i (except k) are assumed to be valid or
1053 * cut constraints for j.
1054 * Wrapping the cut constraints to include basic map j may result
1055 * in constraints that are no longer valid of basic map i
1056 * we have to check that the resulting wrapping constraints are valid for i.
1057 * If "wrap_facet" is not set, then all constraints of i (except k)
1058 * are assumed to be valid for j.
1067 static enum isl_change
can_wrap_in_facet(int i
, int j
, int k
,
1068 struct isl_coalesce_info
*info
, int wrap_facet
)
1070 enum isl_change change
= isl_change_none
;
1071 struct isl_wraps wraps
;
1074 struct isl_set
*set_i
= NULL
;
1075 struct isl_set
*set_j
= NULL
;
1076 struct isl_vec
*bound
= NULL
;
1077 unsigned total
= isl_basic_map_total_dim(info
[i
].bmap
);
1079 set_i
= set_from_updated_bmap(info
[i
].bmap
, info
[i
].tab
);
1080 set_j
= set_from_updated_bmap(info
[j
].bmap
, info
[j
].tab
);
1081 ctx
= isl_basic_map_get_ctx(info
[i
].bmap
);
1082 mat
= isl_mat_alloc(ctx
, 2 * (info
[i
].bmap
->n_eq
+ info
[j
].bmap
->n_eq
) +
1083 info
[i
].bmap
->n_ineq
+ info
[j
].bmap
->n_ineq
,
1085 wraps_init(&wraps
, mat
, info
, i
, j
);
1086 bound
= isl_vec_alloc(ctx
, 1 + total
);
1087 if (!set_i
|| !set_j
|| !wraps
.mat
|| !bound
)
1090 isl_seq_cpy(bound
->el
, info
[i
].bmap
->ineq
[k
], 1 + total
);
1091 isl_int_add_ui(bound
->el
[0], bound
->el
[0], 1);
1093 isl_seq_cpy(wraps
.mat
->row
[0], bound
->el
, 1 + total
);
1094 wraps
.mat
->n_row
= 1;
1096 if (add_wraps(&wraps
, &info
[j
], bound
->el
, set_i
) < 0)
1098 if (!wraps
.mat
->n_row
)
1102 if (add_wraps_around_facet(&wraps
, &info
[i
], k
,
1103 bound
->el
, set_j
) < 0)
1105 if (!wraps
.mat
->n_row
)
1109 change
= fuse(i
, j
, info
, wraps
.mat
, 0, 0);
1114 isl_set_free(set_i
);
1115 isl_set_free(set_j
);
1117 isl_vec_free(bound
);
1122 isl_vec_free(bound
);
1123 isl_set_free(set_i
);
1124 isl_set_free(set_j
);
1125 return isl_change_error
;
1128 /* Given a pair of basic maps i and j such that j sticks out
1129 * of i at n cut constraints, each time by at most one,
1130 * try to compute wrapping constraints and replace the two
1131 * basic maps by a single basic map.
1132 * The other constraints of i are assumed to be valid for j.
1134 * For each cut constraint t(x) >= 0 of i, we add the relaxed version
1135 * t(x) + 1 >= 0, along with wrapping constraints for all constraints
1136 * of basic map j that bound the part of basic map j that sticks out
1137 * of the cut constraint.
1138 * In particular, we first intersect basic map j with t(x) + 1 = 0.
1139 * If the result is empty, then t(x) >= 0 was actually a valid constraint
1140 * (with respect to the integer points), so we add t(x) >= 0 instead.
1141 * Otherwise, we wrap the constraints of basic map j that are not
1142 * redundant in this intersection and that are not already valid
1143 * for basic map i over basic map i.
1144 * Note that it is sufficient to wrap the constraints to include
1145 * basic map i, because we will only wrap the constraints that do
1146 * not include basic map i already. The wrapped constraint will
1147 * therefore be more relaxed compared to the original constraint.
1148 * Since the original constraint is valid for basic map j, so is
1149 * the wrapped constraint.
1151 * If any wrapping fails, i.e., if we cannot wrap to touch
1152 * the union, then we give up.
1153 * Otherwise, the pair of basic maps is replaced by their union.
1155 static enum isl_change
wrap_in_facets(int i
, int j
, int *cuts
, int n
,
1156 struct isl_coalesce_info
*info
)
1158 enum isl_change change
= isl_change_none
;
1159 struct isl_wraps wraps
;
1162 isl_set
*set_i
= NULL
;
1163 unsigned total
= isl_basic_map_total_dim(info
[i
].bmap
);
1166 struct isl_tab_undo
*snap
;
1168 if (isl_tab_extend_cons(info
[j
].tab
, 1) < 0)
1171 max_wrap
= 1 + 2 * info
[j
].bmap
->n_eq
+ info
[j
].bmap
->n_ineq
;
1174 set_i
= set_from_updated_bmap(info
[i
].bmap
, info
[i
].tab
);
1175 ctx
= isl_basic_map_get_ctx(info
[i
].bmap
);
1176 mat
= isl_mat_alloc(ctx
, max_wrap
, 1 + total
);
1177 wraps_init(&wraps
, mat
, info
, i
, j
);
1178 if (!set_i
|| !wraps
.mat
)
1181 snap
= isl_tab_snap(info
[j
].tab
);
1183 wraps
.mat
->n_row
= 0;
1185 for (k
= 0; k
< n
; ++k
) {
1186 w
= wraps
.mat
->n_row
++;
1187 isl_seq_cpy(wraps
.mat
->row
[w
],
1188 info
[i
].bmap
->ineq
[cuts
[k
]], 1 + total
);
1189 isl_int_add_ui(wraps
.mat
->row
[w
][0], wraps
.mat
->row
[w
][0], 1);
1190 if (isl_tab_add_eq(info
[j
].tab
, wraps
.mat
->row
[w
]) < 0)
1192 if (isl_tab_detect_redundant(info
[j
].tab
) < 0)
1195 if (info
[j
].tab
->empty
)
1196 isl_int_sub_ui(wraps
.mat
->row
[w
][0],
1197 wraps
.mat
->row
[w
][0], 1);
1198 else if (add_wraps(&wraps
, &info
[j
],
1199 wraps
.mat
->row
[w
], set_i
) < 0)
1202 if (isl_tab_rollback(info
[j
].tab
, snap
) < 0)
1205 if (!wraps
.mat
->n_row
)
1210 change
= fuse(i
, j
, info
, wraps
.mat
, 0, 1);
1213 isl_set_free(set_i
);
1218 isl_set_free(set_i
);
1219 return isl_change_error
;
1222 /* Given two basic sets i and j such that i has no cut equalities,
1223 * check if relaxing all the cut inequalities of i by one turns
1224 * them into valid constraint for j and check if we can wrap in
1225 * the bits that are sticking out.
1226 * If so, replace the pair by their union.
1228 * We first check if all relaxed cut inequalities of i are valid for j
1229 * and then try to wrap in the intersections of the relaxed cut inequalities
1232 * During this wrapping, we consider the points of j that lie at a distance
1233 * of exactly 1 from i. In particular, we ignore the points that lie in
1234 * between this lower-dimensional space and the basic map i.
1235 * We can therefore only apply this to integer maps.
1261 * Wrapping can fail if the result of wrapping one of the facets
1262 * around its edges does not produce any new facet constraint.
1263 * In particular, this happens when we try to wrap in unbounded sets.
1265 * _______________________________________________________________________
1269 * |_| |_________________________________________________________________
1272 * The following is not an acceptable result of coalescing the above two
1273 * sets as it includes extra integer points.
1274 * _______________________________________________________________________
1279 * \______________________________________________________________________
1281 static enum isl_change
can_wrap_in_set(int i
, int j
,
1282 struct isl_coalesce_info
*info
)
1284 enum isl_change change
= isl_change_none
;
1290 if (ISL_F_ISSET(info
[i
].bmap
, ISL_BASIC_MAP_RATIONAL
) ||
1291 ISL_F_ISSET(info
[j
].bmap
, ISL_BASIC_MAP_RATIONAL
))
1292 return isl_change_none
;
1294 n
= count(info
[i
].ineq
, info
[i
].bmap
->n_ineq
, STATUS_CUT
);
1296 return isl_change_none
;
1298 ctx
= isl_basic_map_get_ctx(info
[i
].bmap
);
1299 cuts
= isl_alloc_array(ctx
, int, n
);
1301 return isl_change_error
;
1303 for (k
= 0, m
= 0; m
< n
; ++k
) {
1304 enum isl_ineq_type type
;
1306 if (info
[i
].ineq
[k
] != STATUS_CUT
)
1309 isl_int_add_ui(info
[i
].bmap
->ineq
[k
][0],
1310 info
[i
].bmap
->ineq
[k
][0], 1);
1311 type
= isl_tab_ineq_type(info
[j
].tab
, info
[i
].bmap
->ineq
[k
]);
1312 isl_int_sub_ui(info
[i
].bmap
->ineq
[k
][0],
1313 info
[i
].bmap
->ineq
[k
][0], 1);
1314 if (type
== isl_ineq_error
)
1316 if (type
!= isl_ineq_redundant
)
1323 change
= wrap_in_facets(i
, j
, cuts
, n
, info
);
1330 return isl_change_error
;
1333 /* Check if either i or j has only cut inequalities that can
1334 * be used to wrap in (a facet of) the other basic set.
1335 * if so, replace the pair by their union.
1337 static enum isl_change
check_wrap(int i
, int j
, struct isl_coalesce_info
*info
)
1339 enum isl_change change
= isl_change_none
;
1341 if (!any(info
[i
].eq
, 2 * info
[i
].bmap
->n_eq
, STATUS_CUT
))
1342 change
= can_wrap_in_set(i
, j
, info
);
1343 if (change
!= isl_change_none
)
1346 if (!any(info
[j
].eq
, 2 * info
[j
].bmap
->n_eq
, STATUS_CUT
))
1347 change
= can_wrap_in_set(j
, i
, info
);
1351 /* At least one of the basic maps has an equality that is adjacent
1352 * to inequality. Make sure that only one of the basic maps has
1353 * such an equality and that the other basic map has exactly one
1354 * inequality adjacent to an equality.
1355 * We call the basic map that has the inequality "i" and the basic
1356 * map that has the equality "j".
1357 * If "i" has any "cut" (in)equality, then relaxing the inequality
1358 * by one would not result in a basic map that contains the other
1359 * basic map. However, it may still be possible to wrap in the other
1362 static enum isl_change
check_adj_eq(int i
, int j
,
1363 struct isl_coalesce_info
*info
)
1365 enum isl_change change
= isl_change_none
;
1369 if (any(info
[i
].eq
, 2 * info
[i
].bmap
->n_eq
, STATUS_ADJ_INEQ
) &&
1370 any(info
[j
].eq
, 2 * info
[j
].bmap
->n_eq
, STATUS_ADJ_INEQ
))
1371 /* ADJ EQ TOO MANY */
1372 return isl_change_none
;
1374 if (any(info
[i
].eq
, 2 * info
[i
].bmap
->n_eq
, STATUS_ADJ_INEQ
))
1375 return check_adj_eq(j
, i
, info
);
1377 /* j has an equality adjacent to an inequality in i */
1379 if (any(info
[i
].eq
, 2 * info
[i
].bmap
->n_eq
, STATUS_CUT
))
1380 return isl_change_none
;
1381 any_cut
= any(info
[i
].ineq
, info
[i
].bmap
->n_ineq
, STATUS_CUT
);
1382 if (count(info
[i
].ineq
, info
[i
].bmap
->n_ineq
, STATUS_ADJ_EQ
) != 1 ||
1383 any(info
[j
].ineq
, info
[j
].bmap
->n_ineq
, STATUS_ADJ_EQ
) ||
1384 any(info
[i
].ineq
, info
[i
].bmap
->n_ineq
, STATUS_ADJ_INEQ
) ||
1385 any(info
[j
].ineq
, info
[j
].bmap
->n_ineq
, STATUS_ADJ_INEQ
))
1386 /* ADJ EQ TOO MANY */
1387 return isl_change_none
;
1389 for (k
= 0; k
< info
[i
].bmap
->n_ineq
; ++k
)
1390 if (info
[i
].ineq
[k
] == STATUS_ADJ_EQ
)
1394 change
= is_adj_eq_extension(i
, j
, k
, info
);
1395 if (change
!= isl_change_none
)
1399 change
= can_wrap_in_facet(i
, j
, k
, info
, any_cut
);
1404 /* The two basic maps lie on adjacent hyperplanes. In particular,
1405 * basic map "i" has an equality that lies parallel to basic map "j".
1406 * Check if we can wrap the facets around the parallel hyperplanes
1407 * to include the other set.
1409 * We perform basically the same operations as can_wrap_in_facet,
1410 * except that we don't need to select a facet of one of the sets.
1416 * If there is more than one equality of "i" adjacent to an equality of "j",
1417 * then the result will satisfy one or more equalities that are a linear
1418 * combination of these equalities. These will be encoded as pairs
1419 * of inequalities in the wrapping constraints and need to be made
1422 static enum isl_change
check_eq_adj_eq(int i
, int j
,
1423 struct isl_coalesce_info
*info
)
1426 enum isl_change change
= isl_change_none
;
1427 int detect_equalities
= 0;
1428 struct isl_wraps wraps
;
1431 struct isl_set
*set_i
= NULL
;
1432 struct isl_set
*set_j
= NULL
;
1433 struct isl_vec
*bound
= NULL
;
1434 unsigned total
= isl_basic_map_total_dim(info
[i
].bmap
);
1436 if (count(info
[i
].eq
, 2 * info
[i
].bmap
->n_eq
, STATUS_ADJ_EQ
) != 1)
1437 detect_equalities
= 1;
1439 for (k
= 0; k
< 2 * info
[i
].bmap
->n_eq
; ++k
)
1440 if (info
[i
].eq
[k
] == STATUS_ADJ_EQ
)
1443 set_i
= set_from_updated_bmap(info
[i
].bmap
, info
[i
].tab
);
1444 set_j
= set_from_updated_bmap(info
[j
].bmap
, info
[j
].tab
);
1445 ctx
= isl_basic_map_get_ctx(info
[i
].bmap
);
1446 mat
= isl_mat_alloc(ctx
, 2 * (info
[i
].bmap
->n_eq
+ info
[j
].bmap
->n_eq
) +
1447 info
[i
].bmap
->n_ineq
+ info
[j
].bmap
->n_ineq
,
1449 wraps_init(&wraps
, mat
, info
, i
, j
);
1450 bound
= isl_vec_alloc(ctx
, 1 + total
);
1451 if (!set_i
|| !set_j
|| !wraps
.mat
|| !bound
)
1455 isl_seq_neg(bound
->el
, info
[i
].bmap
->eq
[k
/ 2], 1 + total
);
1457 isl_seq_cpy(bound
->el
, info
[i
].bmap
->eq
[k
/ 2], 1 + total
);
1458 isl_int_add_ui(bound
->el
[0], bound
->el
[0], 1);
1460 isl_seq_cpy(wraps
.mat
->row
[0], bound
->el
, 1 + total
);
1461 wraps
.mat
->n_row
= 1;
1463 if (add_wraps(&wraps
, &info
[j
], bound
->el
, set_i
) < 0)
1465 if (!wraps
.mat
->n_row
)
1468 isl_int_sub_ui(bound
->el
[0], bound
->el
[0], 1);
1469 isl_seq_neg(bound
->el
, bound
->el
, 1 + total
);
1471 isl_seq_cpy(wraps
.mat
->row
[wraps
.mat
->n_row
], bound
->el
, 1 + total
);
1474 if (add_wraps(&wraps
, &info
[i
], bound
->el
, set_j
) < 0)
1476 if (!wraps
.mat
->n_row
)
1479 change
= fuse(i
, j
, info
, wraps
.mat
, detect_equalities
, 0);
1482 error
: change
= isl_change_error
;
1487 isl_set_free(set_i
);
1488 isl_set_free(set_j
);
1489 isl_vec_free(bound
);
1494 /* Check if the union of the given pair of basic maps
1495 * can be represented by a single basic map.
1496 * If so, replace the pair by the single basic map and return
1497 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
1498 * Otherwise, return isl_change_none.
1499 * The two basic maps are assumed to live in the same local space.
1501 * We first check the effect of each constraint of one basic map
1502 * on the other basic map.
1503 * The constraint may be
1504 * redundant the constraint is redundant in its own
1505 * basic map and should be ignore and removed
1507 * valid all (integer) points of the other basic map
1508 * satisfy the constraint
1509 * separate no (integer) point of the other basic map
1510 * satisfies the constraint
1511 * cut some but not all points of the other basic map
1512 * satisfy the constraint
1513 * adj_eq the given constraint is adjacent (on the outside)
1514 * to an equality of the other basic map
1515 * adj_ineq the given constraint is adjacent (on the outside)
1516 * to an inequality of the other basic map
1518 * We consider seven cases in which we can replace the pair by a single
1519 * basic map. We ignore all "redundant" constraints.
1521 * 1. all constraints of one basic map are valid
1522 * => the other basic map is a subset and can be removed
1524 * 2. all constraints of both basic maps are either "valid" or "cut"
1525 * and the facets corresponding to the "cut" constraints
1526 * of one of the basic maps lies entirely inside the other basic map
1527 * => the pair can be replaced by a basic map consisting
1528 * of the valid constraints in both basic maps
1530 * 3. there is a single pair of adjacent inequalities
1531 * (all other constraints are "valid")
1532 * => the pair can be replaced by a basic map consisting
1533 * of the valid constraints in both basic maps
1535 * 4. one basic map has a single adjacent inequality, while the other
1536 * constraints are "valid". The other basic map has some
1537 * "cut" constraints, but replacing the adjacent inequality by
1538 * its opposite and adding the valid constraints of the other
1539 * basic map results in a subset of the other basic map
1540 * => the pair can be replaced by a basic map consisting
1541 * of the valid constraints in both basic maps
1543 * 5. there is a single adjacent pair of an inequality and an equality,
1544 * the other constraints of the basic map containing the inequality are
1545 * "valid". Moreover, if the inequality the basic map is relaxed
1546 * and then turned into an equality, then resulting facet lies
1547 * entirely inside the other basic map
1548 * => the pair can be replaced by the basic map containing
1549 * the inequality, with the inequality relaxed.
1551 * 6. there is a single adjacent pair of an inequality and an equality,
1552 * the other constraints of the basic map containing the inequality are
1553 * "valid". Moreover, the facets corresponding to both
1554 * the inequality and the equality can be wrapped around their
1555 * ridges to include the other basic map
1556 * => the pair can be replaced by a basic map consisting
1557 * of the valid constraints in both basic maps together
1558 * with all wrapping constraints
1560 * 7. one of the basic maps extends beyond the other by at most one.
1561 * Moreover, the facets corresponding to the cut constraints and
1562 * the pieces of the other basic map at offset one from these cut
1563 * constraints can be wrapped around their ridges to include
1564 * the union of the two basic maps
1565 * => the pair can be replaced by a basic map consisting
1566 * of the valid constraints in both basic maps together
1567 * with all wrapping constraints
1569 * 8. the two basic maps live in adjacent hyperplanes. In principle
1570 * such sets can always be combined through wrapping, but we impose
1571 * that there is only one such pair, to avoid overeager coalescing.
1573 * Throughout the computation, we maintain a collection of tableaus
1574 * corresponding to the basic maps. When the basic maps are dropped
1575 * or combined, the tableaus are modified accordingly.
1577 static enum isl_change
coalesce_local_pair(int i
, int j
,
1578 struct isl_coalesce_info
*info
)
1580 enum isl_change change
= isl_change_none
;
1582 info
[i
].eq
= info
[i
].ineq
= NULL
;
1583 info
[j
].eq
= info
[j
].ineq
= NULL
;
1585 info
[i
].eq
= eq_status_in(info
[i
].bmap
, info
[j
].tab
);
1586 if (info
[i
].bmap
->n_eq
&& !info
[i
].eq
)
1588 if (any(info
[i
].eq
, 2 * info
[i
].bmap
->n_eq
, STATUS_ERROR
))
1590 if (any(info
[i
].eq
, 2 * info
[i
].bmap
->n_eq
, STATUS_SEPARATE
))
1593 info
[j
].eq
= eq_status_in(info
[j
].bmap
, info
[i
].tab
);
1594 if (info
[j
].bmap
->n_eq
&& !info
[j
].eq
)
1596 if (any(info
[j
].eq
, 2 * info
[j
].bmap
->n_eq
, STATUS_ERROR
))
1598 if (any(info
[j
].eq
, 2 * info
[j
].bmap
->n_eq
, STATUS_SEPARATE
))
1601 info
[i
].ineq
= ineq_status_in(info
[i
].bmap
, info
[i
].tab
, info
[j
].tab
);
1602 if (info
[i
].bmap
->n_ineq
&& !info
[i
].ineq
)
1604 if (any(info
[i
].ineq
, info
[i
].bmap
->n_ineq
, STATUS_ERROR
))
1606 if (any(info
[i
].ineq
, info
[i
].bmap
->n_ineq
, STATUS_SEPARATE
))
1609 info
[j
].ineq
= ineq_status_in(info
[j
].bmap
, info
[j
].tab
, info
[i
].tab
);
1610 if (info
[j
].bmap
->n_ineq
&& !info
[j
].ineq
)
1612 if (any(info
[j
].ineq
, info
[j
].bmap
->n_ineq
, STATUS_ERROR
))
1614 if (any(info
[j
].ineq
, info
[j
].bmap
->n_ineq
, STATUS_SEPARATE
))
1617 if (all(info
[i
].eq
, 2 * info
[i
].bmap
->n_eq
, STATUS_VALID
) &&
1618 all(info
[i
].ineq
, info
[i
].bmap
->n_ineq
, STATUS_VALID
)) {
1620 change
= isl_change_drop_second
;
1621 } else if (all(info
[j
].eq
, 2 * info
[j
].bmap
->n_eq
, STATUS_VALID
) &&
1622 all(info
[j
].ineq
, info
[j
].bmap
->n_ineq
, STATUS_VALID
)) {
1624 change
= isl_change_drop_first
;
1625 } else if (any(info
[i
].eq
, 2 * info
[i
].bmap
->n_eq
, STATUS_ADJ_EQ
)) {
1626 change
= check_eq_adj_eq(i
, j
, info
);
1627 } else if (any(info
[j
].eq
, 2 * info
[j
].bmap
->n_eq
, STATUS_ADJ_EQ
)) {
1628 change
= check_eq_adj_eq(j
, i
, info
);
1629 } else if (any(info
[i
].eq
, 2 * info
[i
].bmap
->n_eq
, STATUS_ADJ_INEQ
) ||
1630 any(info
[j
].eq
, 2 * info
[j
].bmap
->n_eq
, STATUS_ADJ_INEQ
)) {
1631 change
= check_adj_eq(i
, j
, info
);
1632 } else if (any(info
[i
].ineq
, info
[i
].bmap
->n_ineq
, STATUS_ADJ_EQ
) ||
1633 any(info
[j
].ineq
, info
[j
].bmap
->n_ineq
, STATUS_ADJ_EQ
)) {
1636 } else if (any(info
[i
].ineq
, info
[i
].bmap
->n_ineq
, STATUS_ADJ_INEQ
) ||
1637 any(info
[j
].ineq
, info
[j
].bmap
->n_ineq
, STATUS_ADJ_INEQ
)) {
1638 change
= check_adj_ineq(i
, j
, info
);
1640 if (!any(info
[i
].eq
, 2 * info
[i
].bmap
->n_eq
, STATUS_CUT
) &&
1641 !any(info
[j
].eq
, 2 * info
[j
].bmap
->n_eq
, STATUS_CUT
))
1642 change
= check_facets(i
, j
, info
);
1643 if (change
== isl_change_none
)
1644 change
= check_wrap(i
, j
, info
);
1658 return isl_change_error
;
1661 /* Shift the integer division at position "div" of the basic map
1662 * represented by "info" by "shift".
1664 * That is, if the integer division has the form
1668 * then replace it by
1670 * floor((f(x) + shift * d)/d) - shift
1672 static int shift_div(struct isl_coalesce_info
*info
, int div
, isl_int shift
)
1676 info
->bmap
= isl_basic_map_shift_div(info
->bmap
, div
, shift
);
1680 total
= isl_basic_map_dim(info
->bmap
, isl_dim_all
);
1681 total
-= isl_basic_map_dim(info
->bmap
, isl_dim_div
);
1682 if (isl_tab_shift_var(info
->tab
, total
+ div
, shift
) < 0)
1688 /* Check if some of the divs in the basic map represented by "info1"
1689 * are shifts of the corresponding divs in the basic map represented
1690 * by "info2". If so, align them with those of "info2".
1691 * Only do this if "info1" and "info2" have the same number
1692 * of integer divisions.
1694 * An integer division is considered to be a shift of another integer
1695 * division if one is equal to the other plus a constant.
1697 * In particular, for each pair of integer divisions, if both are known,
1698 * have identical coefficients (apart from the constant term) and
1699 * if the difference between the constant terms (taking into account
1700 * the denominator) is an integer, then move the difference outside.
1701 * That is, if one integer division is of the form
1703 * floor((f(x) + c_1)/d)
1705 * while the other is of the form
1707 * floor((f(x) + c_2)/d)
1709 * and n = (c_2 - c_1)/d is an integer, then replace the first
1710 * integer division by
1712 * floor((f(x) + c_1 + n * d)/d) - n = floor((f(x) + c_2)/d) - n
1714 static int harmonize_divs(struct isl_coalesce_info
*info1
,
1715 struct isl_coalesce_info
*info2
)
1720 if (!info1
->bmap
|| !info2
->bmap
)
1723 if (info1
->bmap
->n_div
!= info2
->bmap
->n_div
)
1725 if (info1
->bmap
->n_div
== 0)
1728 total
= isl_basic_map_total_dim(info1
->bmap
);
1729 for (i
= 0; i
< info1
->bmap
->n_div
; ++i
) {
1733 if (isl_int_is_zero(info1
->bmap
->div
[i
][0]) ||
1734 isl_int_is_zero(info2
->bmap
->div
[i
][0]))
1736 if (isl_int_ne(info1
->bmap
->div
[i
][0], info2
->bmap
->div
[i
][0]))
1738 if (isl_int_eq(info1
->bmap
->div
[i
][1], info2
->bmap
->div
[i
][1]))
1740 if (!isl_seq_eq(info1
->bmap
->div
[i
] + 2,
1741 info2
->bmap
->div
[i
] + 2, total
))
1744 isl_int_sub(d
, info2
->bmap
->div
[i
][1], info1
->bmap
->div
[i
][1]);
1745 if (isl_int_is_divisible_by(d
, info1
->bmap
->div
[i
][0])) {
1746 isl_int_divexact(d
, d
, info1
->bmap
->div
[i
][0]);
1747 r
= shift_div(info1
, i
, d
);
1757 /* Do the two basic maps live in the same local space, i.e.,
1758 * do they have the same (known) divs?
1759 * If either basic map has any unknown divs, then we can only assume
1760 * that they do not live in the same local space.
1762 static int same_divs(__isl_keep isl_basic_map
*bmap1
,
1763 __isl_keep isl_basic_map
*bmap2
)
1769 if (!bmap1
|| !bmap2
)
1771 if (bmap1
->n_div
!= bmap2
->n_div
)
1774 if (bmap1
->n_div
== 0)
1777 known
= isl_basic_map_divs_known(bmap1
);
1778 if (known
< 0 || !known
)
1780 known
= isl_basic_map_divs_known(bmap2
);
1781 if (known
< 0 || !known
)
1784 total
= isl_basic_map_total_dim(bmap1
);
1785 for (i
= 0; i
< bmap1
->n_div
; ++i
)
1786 if (!isl_seq_eq(bmap1
->div
[i
], bmap2
->div
[i
], 2 + total
))
1792 /* Does "bmap" contain the basic map represented by the tableau "tab"
1793 * after expanding the divs of "bmap" to match those of "tab"?
1794 * The expansion is performed using the divs "div" and expansion "exp"
1795 * computed by the caller.
1796 * Then we check if all constraints of the expanded "bmap" are valid for "tab".
1798 static int contains_with_expanded_divs(__isl_keep isl_basic_map
*bmap
,
1799 struct isl_tab
*tab
, __isl_keep isl_mat
*div
, int *exp
)
1805 bmap
= isl_basic_map_copy(bmap
);
1806 bmap
= isl_basic_set_expand_divs(bmap
, isl_mat_copy(div
), exp
);
1811 eq_i
= eq_status_in(bmap
, tab
);
1812 if (bmap
->n_eq
&& !eq_i
)
1814 if (any(eq_i
, 2 * bmap
->n_eq
, STATUS_ERROR
))
1816 if (any(eq_i
, 2 * bmap
->n_eq
, STATUS_SEPARATE
))
1819 ineq_i
= ineq_status_in(bmap
, NULL
, tab
);
1820 if (bmap
->n_ineq
&& !ineq_i
)
1822 if (any(ineq_i
, bmap
->n_ineq
, STATUS_ERROR
))
1824 if (any(ineq_i
, bmap
->n_ineq
, STATUS_SEPARATE
))
1827 if (all(eq_i
, 2 * bmap
->n_eq
, STATUS_VALID
) &&
1828 all(ineq_i
, bmap
->n_ineq
, STATUS_VALID
))
1832 isl_basic_map_free(bmap
);
1837 isl_basic_map_free(bmap
);
1843 /* Does "bmap_i" contain the basic map represented by "info_j"
1844 * after aligning the divs of "bmap_i" to those of "info_j".
1845 * Note that this can only succeed if the number of divs of "bmap_i"
1846 * is smaller than (or equal to) the number of divs of "info_j".
1848 * We first check if the divs of "bmap_i" are all known and form a subset
1849 * of those of "bmap_j". If so, we pass control over to
1850 * contains_with_expanded_divs.
1852 static int contains_after_aligning_divs(__isl_keep isl_basic_map
*bmap_i
,
1853 struct isl_coalesce_info
*info_j
)
1856 isl_mat
*div_i
, *div_j
, *div
;
1862 known
= isl_basic_map_divs_known(bmap_i
);
1863 if (known
< 0 || !known
)
1866 ctx
= isl_basic_map_get_ctx(bmap_i
);
1868 div_i
= isl_basic_map_get_divs(bmap_i
);
1869 div_j
= isl_basic_map_get_divs(info_j
->bmap
);
1871 if (!div_i
|| !div_j
)
1874 exp1
= isl_alloc_array(ctx
, int, div_i
->n_row
);
1875 exp2
= isl_alloc_array(ctx
, int, div_j
->n_row
);
1876 if ((div_i
->n_row
&& !exp1
) || (div_j
->n_row
&& !exp2
))
1879 div
= isl_merge_divs(div_i
, div_j
, exp1
, exp2
);
1883 if (div
->n_row
== div_j
->n_row
)
1884 subset
= contains_with_expanded_divs(bmap_i
,
1885 info_j
->tab
, div
, exp1
);
1891 isl_mat_free(div_i
);
1892 isl_mat_free(div_j
);
1899 isl_mat_free(div_i
);
1900 isl_mat_free(div_j
);
1906 /* Check if the basic map "j" is a subset of basic map "i",
1907 * if "i" has fewer divs that "j".
1908 * If so, remove basic map "j".
1910 * If the two basic maps have the same number of divs, then
1911 * they must necessarily be different. Otherwise, we would have
1912 * called coalesce_local_pair. We therefore don't try anything
1915 static int coalesced_subset(int i
, int j
, struct isl_coalesce_info
*info
)
1919 if (info
[i
].bmap
->n_div
>= info
[j
].bmap
->n_div
)
1922 superset
= contains_after_aligning_divs(info
[i
].bmap
, &info
[j
]);
1931 /* Check if basic map "j" is a subset of basic map "i" after
1932 * exploiting the extra equalities of "j" to simplify the divs of "i".
1933 * If so, remove basic map "j".
1935 * If "j" does not have any equalities or if they are the same
1936 * as those of "i", then we cannot exploit them to simplify the divs.
1937 * Similarly, if there are no divs in "i", then they cannot be simplified.
1938 * If, on the other hand, the affine hulls of "i" and "j" do not intersect,
1939 * then "j" cannot be a subset of "i".
1941 * Otherwise, we intersect "i" with the affine hull of "j" and then
1942 * check if "j" is a subset of the result after aligning the divs.
1943 * If so, then "j" is definitely a subset of "i" and can be removed.
1944 * Note that if after intersection with the affine hull of "j".
1945 * "i" still has more divs than "j", then there is no way we can
1946 * align the divs of "i" to those of "j".
1948 static int coalesced_subset_with_equalities(int i
, int j
,
1949 struct isl_coalesce_info
*info
)
1951 isl_basic_map
*hull_i
, *hull_j
, *bmap_i
;
1952 int equal
, empty
, subset
;
1954 if (info
[j
].bmap
->n_eq
== 0)
1956 if (info
[i
].bmap
->n_div
== 0)
1959 hull_i
= isl_basic_map_copy(info
[i
].bmap
);
1960 hull_i
= isl_basic_map_plain_affine_hull(hull_i
);
1961 hull_j
= isl_basic_map_copy(info
[j
].bmap
);
1962 hull_j
= isl_basic_map_plain_affine_hull(hull_j
);
1964 hull_j
= isl_basic_map_intersect(hull_j
, isl_basic_map_copy(hull_i
));
1965 equal
= isl_basic_map_plain_is_equal(hull_i
, hull_j
);
1966 empty
= isl_basic_map_plain_is_empty(hull_j
);
1967 isl_basic_map_free(hull_i
);
1969 if (equal
< 0 || equal
|| empty
< 0 || empty
) {
1970 isl_basic_map_free(hull_j
);
1971 return equal
< 0 || empty
< 0 ? -1 : 0;
1974 bmap_i
= isl_basic_map_copy(info
[i
].bmap
);
1975 bmap_i
= isl_basic_map_intersect(bmap_i
, hull_j
);
1979 if (bmap_i
->n_div
> info
[j
].bmap
->n_div
) {
1980 isl_basic_map_free(bmap_i
);
1984 subset
= contains_after_aligning_divs(bmap_i
, &info
[j
]);
1986 isl_basic_map_free(bmap_i
);
1996 /* Check if one of the basic maps is a subset of the other and, if so,
1998 * Note that we only perform any test if the number of divs is different
1999 * in the two basic maps. In case the number of divs is the same,
2000 * we have already established that the divs are different
2001 * in the two basic maps.
2002 * In particular, if the number of divs of basic map i is smaller than
2003 * the number of divs of basic map j, then we check if j is a subset of i
2006 static enum isl_change
check_coalesce_subset(int i
, int j
,
2007 struct isl_coalesce_info
*info
)
2011 changed
= coalesced_subset(i
, j
, info
);
2012 if (changed
< 0 || changed
)
2013 return changed
< 0 ? isl_change_error
: isl_change_drop_second
;
2015 changed
= coalesced_subset(j
, i
, info
);
2016 if (changed
< 0 || changed
)
2017 return changed
< 0 ? isl_change_error
: isl_change_drop_first
;
2019 changed
= coalesced_subset_with_equalities(i
, j
, info
);
2020 if (changed
< 0 || changed
)
2021 return changed
< 0 ? isl_change_error
: isl_change_drop_second
;
2023 changed
= coalesced_subset_with_equalities(j
, i
, info
);
2024 if (changed
< 0 || changed
)
2025 return changed
< 0 ? isl_change_error
: isl_change_drop_first
;
2027 return isl_change_none
;
2030 /* Does "bmap" involve any divs that themselves refer to divs?
2032 static int has_nested_div(__isl_keep isl_basic_map
*bmap
)
2038 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
2039 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
2042 for (i
= 0; i
< n_div
; ++i
)
2043 if (isl_seq_first_non_zero(bmap
->div
[i
] + 2 + total
,
2050 /* Return a list of affine expressions, one for each integer division
2051 * in "bmap_i". For each integer division that also appears in "bmap_j",
2052 * the affine expression is set to NaN. The number of NaNs in the list
2053 * is equal to the number of integer divisions in "bmap_j".
2054 * For the other integer divisions of "bmap_i", the corresponding
2055 * element in the list is a purely affine expression equal to the integer
2056 * division in "hull".
2057 * If no such list can be constructed, then the number of elements
2058 * in the returned list is smaller than the number of integer divisions
2061 static __isl_give isl_aff_list
*set_up_substitutions(
2062 __isl_keep isl_basic_map
*bmap_i
, __isl_keep isl_basic_map
*bmap_j
,
2063 __isl_take isl_basic_map
*hull
)
2065 unsigned n_div_i
, n_div_j
, total
;
2067 isl_local_space
*ls
;
2068 isl_basic_set
*wrap_hull
;
2076 ctx
= isl_basic_map_get_ctx(hull
);
2078 n_div_i
= isl_basic_map_dim(bmap_i
, isl_dim_div
);
2079 n_div_j
= isl_basic_map_dim(bmap_j
, isl_dim_div
);
2080 total
= isl_basic_map_total_dim(bmap_i
) - n_div_i
;
2082 ls
= isl_basic_map_get_local_space(bmap_i
);
2083 ls
= isl_local_space_wrap(ls
);
2084 wrap_hull
= isl_basic_map_wrap(hull
);
2086 aff_nan
= isl_aff_nan_on_domain(isl_local_space_copy(ls
));
2087 list
= isl_aff_list_alloc(ctx
, n_div_i
);
2090 for (i
= 0; i
< n_div_i
; ++i
) {
2094 isl_seq_eq(bmap_i
->div
[i
], bmap_j
->div
[j
], 2 + total
)) {
2096 list
= isl_aff_list_add(list
, isl_aff_copy(aff_nan
));
2099 if (n_div_i
- i
<= n_div_j
- j
)
2102 aff
= isl_local_space_get_div(ls
, i
);
2103 aff
= isl_aff_substitute_equalities(aff
,
2104 isl_basic_set_copy(wrap_hull
));
2105 aff
= isl_aff_floor(aff
);
2108 if (isl_aff_dim(aff
, isl_dim_div
) != 0) {
2113 list
= isl_aff_list_add(list
, aff
);
2116 isl_aff_free(aff_nan
);
2117 isl_local_space_free(ls
);
2118 isl_basic_set_free(wrap_hull
);
2122 isl_local_space_free(ls
);
2123 isl_basic_set_free(wrap_hull
);
2124 isl_aff_list_free(list
);
2128 /* Add variables to "tab" corresponding to the elements in "list"
2129 * that are not set to NaN. The value of the added variable
2130 * is fixed to the purely affine expression defined by the element.
2131 * "dim" is the offset in the variables of "tab" where we should
2132 * start considering the elements in "list".
2133 * When this function returns, the total number of variables in "tab"
2134 * is equal to "dim" plus the number of elements in "list".
2136 static int add_subs(struct isl_tab
*tab
, __isl_keep isl_aff_list
*list
, int dim
)
2147 n
= isl_aff_list_n_aff(list
);
2148 extra
= n
- (tab
->n_var
- dim
);
2150 if (isl_tab_extend_vars(tab
, extra
) < 0)
2152 if (isl_tab_extend_cons(tab
, 2 * extra
) < 0)
2155 ctx
= isl_tab_get_ctx(tab
);
2156 sub
= isl_vec_alloc(ctx
, 1 + dim
+ n
);
2159 isl_seq_clr(sub
->el
+ 1 + dim
, n
);
2161 for (i
= 0; i
< n
; ++i
) {
2162 aff
= isl_aff_list_get_aff(list
, i
);
2165 if (isl_aff_is_nan(aff
)) {
2169 if (isl_tab_insert_var(tab
, dim
+ i
) < 0)
2171 isl_seq_cpy(sub
->el
, aff
->v
->el
+ 1, 1 + dim
);
2172 isl_int_neg(sub
->el
[1 + dim
+ i
], aff
->v
->el
[0]);
2173 if (isl_tab_add_eq(tab
, sub
->el
) < 0)
2175 isl_int_set_si(sub
->el
[1 + dim
+ i
], 0);
2187 /* Coalesce basic map "j" into basic map "i" after adding the extra integer
2188 * divisions in "i" but not in "j" to basic map "j", with values
2189 * specified by "list". The total number of elements in "list"
2190 * is equal to the number of integer divisions in "i", while the number
2191 * of NaN elements in the list is equal to the number of integer divisions
2193 * If no coalescing can be performed, then we need to revert basic map "j"
2194 * to its original state. We do the same if basic map "i" gets dropped
2195 * during the coalescing, even though this should not happen in practice
2196 * since we have already checked for "j" being a subset of "i"
2197 * before we reach this stage.
2199 static enum isl_change
coalesce_with_subs(int i
, int j
,
2200 struct isl_coalesce_info
*info
, __isl_keep isl_aff_list
*list
)
2202 isl_basic_map
*bmap_j
;
2203 struct isl_tab_undo
*snap
;
2205 enum isl_change change
;
2207 bmap_j
= isl_basic_map_copy(info
[j
].bmap
);
2208 info
[j
].bmap
= isl_basic_map_align_divs(info
[j
].bmap
, info
[i
].bmap
);
2212 snap
= isl_tab_snap(info
[j
].tab
);
2214 dim
= isl_basic_map_dim(bmap_j
, isl_dim_all
);
2215 dim
-= isl_basic_map_dim(bmap_j
, isl_dim_div
);
2216 if (add_subs(info
[j
].tab
, list
, dim
) < 0)
2219 change
= coalesce_local_pair(i
, j
, info
);
2220 if (change
!= isl_change_none
&& change
!= isl_change_drop_first
) {
2221 isl_basic_map_free(bmap_j
);
2223 isl_basic_map_free(info
[j
].bmap
);
2224 info
[j
].bmap
= bmap_j
;
2226 if (isl_tab_rollback(info
[j
].tab
, snap
) < 0)
2227 return isl_change_error
;
2232 isl_basic_map_free(bmap_j
);
2233 return isl_change_error
;
2236 /* Check if we can coalesce basic map "j" into basic map "i" after copying
2237 * those extra integer divisions in "i" that can be simplified away
2238 * using the extra equalities in "j".
2239 * All divs are assumed to be known and not contain any nested divs.
2241 * We first check if there are any extra equalities in "j" that we
2242 * can exploit. Then we check if every integer division in "i"
2243 * either already appears in "j" or can be simplified using the
2244 * extra equalities to a purely affine expression.
2245 * If these tests succeed, then we try to coalesce the two basic maps
2246 * by introducing extra dimensions in "j" corresponding to
2247 * the extra integer divsisions "i" fixed to the corresponding
2248 * purely affine expression.
2250 static enum isl_change
check_coalesce_into_eq(int i
, int j
,
2251 struct isl_coalesce_info
*info
)
2253 unsigned n_div_i
, n_div_j
;
2254 isl_basic_map
*hull_i
, *hull_j
;
2257 enum isl_change change
;
2259 n_div_i
= isl_basic_map_dim(info
[i
].bmap
, isl_dim_div
);
2260 n_div_j
= isl_basic_map_dim(info
[j
].bmap
, isl_dim_div
);
2261 if (n_div_i
<= n_div_j
)
2262 return isl_change_none
;
2263 if (info
[j
].bmap
->n_eq
== 0)
2264 return isl_change_none
;
2266 hull_i
= isl_basic_map_copy(info
[i
].bmap
);
2267 hull_i
= isl_basic_map_plain_affine_hull(hull_i
);
2268 hull_j
= isl_basic_map_copy(info
[j
].bmap
);
2269 hull_j
= isl_basic_map_plain_affine_hull(hull_j
);
2271 hull_j
= isl_basic_map_intersect(hull_j
, isl_basic_map_copy(hull_i
));
2272 equal
= isl_basic_map_plain_is_equal(hull_i
, hull_j
);
2273 empty
= isl_basic_map_plain_is_empty(hull_j
);
2274 isl_basic_map_free(hull_i
);
2276 if (equal
< 0 || empty
< 0)
2278 if (equal
|| empty
) {
2279 isl_basic_map_free(hull_j
);
2280 return isl_change_none
;
2283 list
= set_up_substitutions(info
[i
].bmap
, info
[j
].bmap
, hull_j
);
2286 if (isl_aff_list_n_aff(list
) < n_div_i
)
2287 change
= isl_change_none
;
2289 change
= coalesce_with_subs(i
, j
, info
, list
);
2291 isl_aff_list_free(list
);
2295 isl_basic_map_free(hull_j
);
2296 return isl_change_error
;
2299 /* Check if we can coalesce basic maps "i" and "j" after copying
2300 * those extra integer divisions in one of the basic maps that can
2301 * be simplified away using the extra equalities in the other basic map.
2302 * We require all divs to be known in both basic maps.
2303 * Furthermore, to simplify the comparison of div expressions,
2304 * we do not allow any nested integer divisions.
2306 static enum isl_change
check_coalesce_eq(int i
, int j
,
2307 struct isl_coalesce_info
*info
)
2310 enum isl_change change
;
2312 known
= isl_basic_map_divs_known(info
[i
].bmap
);
2313 if (known
< 0 || !known
)
2314 return known
< 0 ? isl_change_error
: isl_change_none
;
2315 known
= isl_basic_map_divs_known(info
[j
].bmap
);
2316 if (known
< 0 || !known
)
2317 return known
< 0 ? isl_change_error
: isl_change_none
;
2318 nested
= has_nested_div(info
[i
].bmap
);
2319 if (nested
< 0 || nested
)
2320 return nested
< 0 ? isl_change_error
: isl_change_none
;
2321 nested
= has_nested_div(info
[j
].bmap
);
2322 if (nested
< 0 || nested
)
2323 return nested
< 0 ? isl_change_error
: isl_change_none
;
2325 change
= check_coalesce_into_eq(i
, j
, info
);
2326 if (change
!= isl_change_none
)
2328 change
= check_coalesce_into_eq(j
, i
, info
);
2329 if (change
!= isl_change_none
)
2330 return invert_change(change
);
2332 return isl_change_none
;
2335 /* Check if the union of the given pair of basic maps
2336 * can be represented by a single basic map.
2337 * If so, replace the pair by the single basic map and return
2338 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
2339 * Otherwise, return isl_change_none.
2341 * We first check if the two basic maps live in the same local space,
2342 * after aligning the divs that differ by only an integer constant.
2343 * If so, we do the complete check. Otherwise, we check if they have
2344 * the same number of integer divisions and can be coalesced, if one is
2345 * an obvious subset of the other or if the extra integer divisions
2346 * of one basic map can be simplified away using the extra equalities
2347 * of the other basic map.
2349 static enum isl_change
coalesce_pair(int i
, int j
,
2350 struct isl_coalesce_info
*info
)
2353 enum isl_change change
;
2355 if (harmonize_divs(&info
[i
], &info
[j
]) < 0)
2356 return isl_change_error
;
2357 same
= same_divs(info
[i
].bmap
, info
[j
].bmap
);
2359 return isl_change_error
;
2361 return coalesce_local_pair(i
, j
, info
);
2363 if (info
[i
].bmap
->n_div
== info
[j
].bmap
->n_div
) {
2364 change
= coalesce_local_pair(i
, j
, info
);
2365 if (change
!= isl_change_none
)
2369 change
= check_coalesce_subset(i
, j
, info
);
2370 if (change
!= isl_change_none
)
2373 return check_coalesce_eq(i
, j
, info
);
2376 /* Return the maximum of "a" and "b".
2378 static inline int max(int a
, int b
)
2380 return a
> b
? a
: b
;
2383 /* Pairwise coalesce the basic maps in the range [start1, end1[ of "info"
2384 * with those in the range [start2, end2[, skipping basic maps
2385 * that have been removed (either before or within this function).
2387 * For each basic map i in the first range, we check if it can be coalesced
2388 * with respect to any previously considered basic map j in the second range.
2389 * If i gets dropped (because it was a subset of some j), then
2390 * we can move on to the next basic map.
2391 * If j gets dropped, we need to continue checking against the other
2392 * previously considered basic maps.
2393 * If the two basic maps got fused, then we recheck the fused basic map
2394 * against the previously considered basic maps, starting at i + 1
2395 * (even if start2 is greater than i + 1).
2397 static int coalesce_range(isl_ctx
*ctx
, struct isl_coalesce_info
*info
,
2398 int start1
, int end1
, int start2
, int end2
)
2402 for (i
= end1
- 1; i
>= start1
; --i
) {
2403 if (info
[i
].removed
)
2405 for (j
= max(i
+ 1, start2
); j
< end2
; ++j
) {
2406 enum isl_change changed
;
2408 if (info
[j
].removed
)
2410 if (info
[i
].removed
)
2411 isl_die(ctx
, isl_error_internal
,
2412 "basic map unexpectedly removed",
2414 changed
= coalesce_pair(i
, j
, info
);
2416 case isl_change_error
:
2418 case isl_change_none
:
2419 case isl_change_drop_second
:
2421 case isl_change_drop_first
:
2424 case isl_change_fuse
:
2434 /* Pairwise coalesce the basic maps described by the "n" elements of "info".
2436 * We consider groups of basic maps that live in the same apparent
2437 * affine hull and we first coalesce within such a group before we
2438 * coalesce the elements in the group with elements of previously
2439 * considered groups. If a fuse happens during the second phase,
2440 * then we also reconsider the elements within the group.
2442 static int coalesce(isl_ctx
*ctx
, int n
, struct isl_coalesce_info
*info
)
2446 for (end
= n
; end
> 0; end
= start
) {
2448 while (start
>= 1 &&
2449 info
[start
- 1].hull_hash
== info
[start
].hull_hash
)
2451 if (coalesce_range(ctx
, info
, start
, end
, start
, end
) < 0)
2453 if (coalesce_range(ctx
, info
, start
, end
, end
, n
) < 0)
2460 /* Update the basic maps in "map" based on the information in "info".
2461 * In particular, remove the basic maps that have been marked removed and
2462 * update the others based on the information in the corresponding tableau.
2463 * Since we detected implicit equalities without calling
2464 * isl_basic_map_gauss, we need to do it now.
2465 * Also call isl_basic_map_simplify if we may have lost the definition
2466 * of one or more integer divisions.
2468 static __isl_give isl_map
*update_basic_maps(__isl_take isl_map
*map
,
2469 int n
, struct isl_coalesce_info
*info
)
2476 for (i
= n
- 1; i
>= 0; --i
) {
2477 if (info
[i
].removed
) {
2478 isl_basic_map_free(map
->p
[i
]);
2479 if (i
!= map
->n
- 1)
2480 map
->p
[i
] = map
->p
[map
->n
- 1];
2485 info
[i
].bmap
= isl_basic_map_update_from_tab(info
[i
].bmap
,
2487 info
[i
].bmap
= isl_basic_map_gauss(info
[i
].bmap
, NULL
);
2488 if (info
[i
].simplify
)
2489 info
[i
].bmap
= isl_basic_map_simplify(info
[i
].bmap
);
2490 info
[i
].bmap
= isl_basic_map_finalize(info
[i
].bmap
);
2492 return isl_map_free(map
);
2493 ISL_F_SET(info
[i
].bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
2494 ISL_F_SET(info
[i
].bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
2495 isl_basic_map_free(map
->p
[i
]);
2496 map
->p
[i
] = info
[i
].bmap
;
2497 info
[i
].bmap
= NULL
;
2503 /* For each pair of basic maps in the map, check if the union of the two
2504 * can be represented by a single basic map.
2505 * If so, replace the pair by the single basic map and start over.
2507 * We factor out any (hidden) common factor from the constraint
2508 * coefficients to improve the detection of adjacent constraints.
2510 * Since we are constructing the tableaus of the basic maps anyway,
2511 * we exploit them to detect implicit equalities and redundant constraints.
2512 * This also helps the coalescing as it can ignore the redundant constraints.
2513 * In order to avoid confusion, we make all implicit equalities explicit
2514 * in the basic maps. We don't call isl_basic_map_gauss, though,
2515 * as that may affect the number of constraints.
2516 * This means that we have to call isl_basic_map_gauss at the end
2517 * of the computation (in update_basic_maps) to ensure that
2518 * the basic maps are not left in an unexpected state.
2519 * For each basic map, we also compute the hash of the apparent affine hull
2520 * for use in coalesce.
2522 struct isl_map
*isl_map_coalesce(struct isl_map
*map
)
2527 struct isl_coalesce_info
*info
= NULL
;
2529 map
= isl_map_remove_empty_parts(map
);
2536 ctx
= isl_map_get_ctx(map
);
2537 map
= isl_map_sort_divs(map
);
2538 map
= isl_map_cow(map
);
2545 info
= isl_calloc_array(map
->ctx
, struct isl_coalesce_info
, n
);
2549 for (i
= 0; i
< map
->n
; ++i
) {
2550 map
->p
[i
] = isl_basic_map_reduce_coefficients(map
->p
[i
]);
2553 info
[i
].bmap
= isl_basic_map_copy(map
->p
[i
]);
2554 info
[i
].tab
= isl_tab_from_basic_map(info
[i
].bmap
, 0);
2557 if (!ISL_F_ISSET(info
[i
].bmap
, ISL_BASIC_MAP_NO_IMPLICIT
))
2558 if (isl_tab_detect_implicit_equalities(info
[i
].tab
) < 0)
2560 info
[i
].bmap
= isl_tab_make_equalities_explicit(info
[i
].tab
,
2564 if (!ISL_F_ISSET(info
[i
].bmap
, ISL_BASIC_MAP_NO_REDUNDANT
))
2565 if (isl_tab_detect_redundant(info
[i
].tab
) < 0)
2567 if (coalesce_info_set_hull_hash(&info
[i
]) < 0)
2570 for (i
= map
->n
- 1; i
>= 0; --i
)
2571 if (info
[i
].tab
->empty
)
2574 if (coalesce(ctx
, n
, info
) < 0)
2577 map
= update_basic_maps(map
, n
, info
);
2579 clear_coalesce_info(n
, info
);
2583 clear_coalesce_info(n
, info
);
2588 /* For each pair of basic sets in the set, check if the union of the two
2589 * can be represented by a single basic set.
2590 * If so, replace the pair by the single basic set and start over.
2592 struct isl_set
*isl_set_coalesce(struct isl_set
*set
)
2594 return (struct isl_set
*)isl_map_coalesce((struct isl_map
*)set
);