2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2012-2013 Ecole Normale Superieure
5 * Copyright 2014 INRIA Rocquencourt
6 * Copyright 2016 INRIA Paris
8 * Use of this software is governed by the MIT license
10 * Written by Sven Verdoolaege, K.U.Leuven, Departement
11 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
12 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
13 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
14 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
15 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
16 * B.P. 105 - 78153 Le Chesnay, France
17 * and Centre de Recherche Inria de Paris, 2 rue Simone Iff - Voie DQ12,
18 * CS 42112, 75589 Paris Cedex 12, France
21 #include <isl_ctx_private.h>
22 #include "isl_map_private.h"
24 #include <isl/options.h>
26 #include <isl_mat_private.h>
27 #include <isl_local_space_private.h>
28 #include <isl_vec_private.h>
29 #include <isl_aff_private.h>
30 #include <isl_equalities.h>
32 #include <set_to_map.c>
33 #include <set_from_map.c>
35 #define STATUS_ERROR -1
36 #define STATUS_REDUNDANT 1
37 #define STATUS_VALID 2
38 #define STATUS_SEPARATE 3
40 #define STATUS_ADJ_EQ 5
41 #define STATUS_ADJ_INEQ 6
43 static int status_in(isl_int
*ineq
, struct isl_tab
*tab
)
45 enum isl_ineq_type type
= isl_tab_ineq_type(tab
, ineq
);
48 case isl_ineq_error
: return STATUS_ERROR
;
49 case isl_ineq_redundant
: return STATUS_VALID
;
50 case isl_ineq_separate
: return STATUS_SEPARATE
;
51 case isl_ineq_cut
: return STATUS_CUT
;
52 case isl_ineq_adj_eq
: return STATUS_ADJ_EQ
;
53 case isl_ineq_adj_ineq
: return STATUS_ADJ_INEQ
;
57 /* Compute the position of the equalities of basic map "bmap_i"
58 * with respect to the basic map represented by "tab_j".
59 * The resulting array has twice as many entries as the number
60 * of equalities corresponding to the two inequalties to which
61 * each equality corresponds.
63 static int *eq_status_in(__isl_keep isl_basic_map
*bmap_i
,
64 struct isl_tab
*tab_j
)
67 int *eq
= isl_calloc_array(bmap_i
->ctx
, int, 2 * bmap_i
->n_eq
);
73 dim
= isl_basic_map_total_dim(bmap_i
);
74 for (k
= 0; k
< bmap_i
->n_eq
; ++k
) {
75 for (l
= 0; l
< 2; ++l
) {
76 isl_seq_neg(bmap_i
->eq
[k
], bmap_i
->eq
[k
], 1+dim
);
77 eq
[2 * k
+ l
] = status_in(bmap_i
->eq
[k
], tab_j
);
78 if (eq
[2 * k
+ l
] == STATUS_ERROR
)
81 if (eq
[2 * k
] == STATUS_SEPARATE
||
82 eq
[2 * k
+ 1] == STATUS_SEPARATE
)
92 /* Compute the position of the inequalities of basic map "bmap_i"
93 * (also represented by "tab_i", if not NULL) with respect to the basic map
94 * represented by "tab_j".
96 static int *ineq_status_in(__isl_keep isl_basic_map
*bmap_i
,
97 struct isl_tab
*tab_i
, struct isl_tab
*tab_j
)
100 unsigned n_eq
= bmap_i
->n_eq
;
101 int *ineq
= isl_calloc_array(bmap_i
->ctx
, int, bmap_i
->n_ineq
);
106 for (k
= 0; k
< bmap_i
->n_ineq
; ++k
) {
107 if (tab_i
&& isl_tab_is_redundant(tab_i
, n_eq
+ k
)) {
108 ineq
[k
] = STATUS_REDUNDANT
;
111 ineq
[k
] = status_in(bmap_i
->ineq
[k
], tab_j
);
112 if (ineq
[k
] == STATUS_ERROR
)
114 if (ineq
[k
] == STATUS_SEPARATE
)
124 static int any(int *con
, unsigned len
, int status
)
128 for (i
= 0; i
< len
; ++i
)
129 if (con
[i
] == status
)
134 /* Return the first position of "status" in the list "con" of length "len".
135 * Return -1 if there is no such entry.
137 static int find(int *con
, unsigned len
, int status
)
141 for (i
= 0; i
< len
; ++i
)
142 if (con
[i
] == status
)
147 static int count(int *con
, unsigned len
, int status
)
152 for (i
= 0; i
< len
; ++i
)
153 if (con
[i
] == status
)
158 static int all(int *con
, unsigned len
, int status
)
162 for (i
= 0; i
< len
; ++i
) {
163 if (con
[i
] == STATUS_REDUNDANT
)
165 if (con
[i
] != status
)
171 /* Internal information associated to a basic map in a map
172 * that is to be coalesced by isl_map_coalesce.
174 * "bmap" is the basic map itself (or NULL if "removed" is set)
175 * "tab" is the corresponding tableau (or NULL if "removed" is set)
176 * "hull_hash" identifies the affine space in which "bmap" lives.
177 * "removed" is set if this basic map has been removed from the map
178 * "simplify" is set if this basic map may have some unknown integer
179 * divisions that were not present in the input basic maps. The basic
180 * map should then be simplified such that we may be able to find
181 * a definition among the constraints.
183 * "eq" and "ineq" are only set if we are currently trying to coalesce
184 * this basic map with another basic map, in which case they represent
185 * the position of the inequalities of this basic map with respect to
186 * the other basic map. The number of elements in the "eq" array
187 * is twice the number of equalities in the "bmap", corresponding
188 * to the two inequalities that make up each equality.
190 struct isl_coalesce_info
{
200 /* Are all non-redundant constraints of the basic map represented by "info"
201 * either valid or cut constraints with respect to the other basic map?
203 static int all_valid_or_cut(struct isl_coalesce_info
*info
)
207 for (i
= 0; i
< 2 * info
->bmap
->n_eq
; ++i
) {
208 if (info
->eq
[i
] == STATUS_REDUNDANT
)
210 if (info
->eq
[i
] == STATUS_VALID
)
212 if (info
->eq
[i
] == STATUS_CUT
)
217 for (i
= 0; i
< info
->bmap
->n_ineq
; ++i
) {
218 if (info
->ineq
[i
] == STATUS_REDUNDANT
)
220 if (info
->ineq
[i
] == STATUS_VALID
)
222 if (info
->ineq
[i
] == STATUS_CUT
)
230 /* Compute the hash of the (apparent) affine hull of info->bmap (with
231 * the existentially quantified variables removed) and store it
234 static int coalesce_info_set_hull_hash(struct isl_coalesce_info
*info
)
239 hull
= isl_basic_map_copy(info
->bmap
);
240 hull
= isl_basic_map_plain_affine_hull(hull
);
241 n_div
= isl_basic_map_dim(hull
, isl_dim_div
);
242 hull
= isl_basic_map_drop_constraints_involving_dims(hull
,
243 isl_dim_div
, 0, n_div
);
244 info
->hull_hash
= isl_basic_map_get_hash(hull
);
245 isl_basic_map_free(hull
);
247 return hull
? 0 : -1;
250 /* Free all the allocated memory in an array
251 * of "n" isl_coalesce_info elements.
253 static void clear_coalesce_info(int n
, struct isl_coalesce_info
*info
)
260 for (i
= 0; i
< n
; ++i
) {
261 isl_basic_map_free(info
[i
].bmap
);
262 isl_tab_free(info
[i
].tab
);
268 /* Drop the basic map represented by "info".
269 * That is, clear the memory associated to the entry and
270 * mark it as having been removed.
272 static void drop(struct isl_coalesce_info
*info
)
274 info
->bmap
= isl_basic_map_free(info
->bmap
);
275 isl_tab_free(info
->tab
);
280 /* Exchange the information in "info1" with that in "info2".
282 static void exchange(struct isl_coalesce_info
*info1
,
283 struct isl_coalesce_info
*info2
)
285 struct isl_coalesce_info info
;
292 /* This type represents the kind of change that has been performed
293 * while trying to coalesce two basic maps.
295 * isl_change_none: nothing was changed
296 * isl_change_drop_first: the first basic map was removed
297 * isl_change_drop_second: the second basic map was removed
298 * isl_change_fuse: the two basic maps were replaced by a new basic map.
301 isl_change_error
= -1,
303 isl_change_drop_first
,
304 isl_change_drop_second
,
308 /* Update "change" based on an interchange of the first and the second
309 * basic map. That is, interchange isl_change_drop_first and
310 * isl_change_drop_second.
312 static enum isl_change
invert_change(enum isl_change change
)
315 case isl_change_error
:
316 return isl_change_error
;
317 case isl_change_none
:
318 return isl_change_none
;
319 case isl_change_drop_first
:
320 return isl_change_drop_second
;
321 case isl_change_drop_second
:
322 return isl_change_drop_first
;
323 case isl_change_fuse
:
324 return isl_change_fuse
;
327 return isl_change_error
;
330 /* Add the valid constraints of the basic map represented by "info"
331 * to "bmap". "len" is the size of the constraints.
332 * If only one of the pair of inequalities that make up an equality
333 * is valid, then add that inequality.
335 static __isl_give isl_basic_map
*add_valid_constraints(
336 __isl_take isl_basic_map
*bmap
, struct isl_coalesce_info
*info
,
344 for (k
= 0; k
< info
->bmap
->n_eq
; ++k
) {
345 if (info
->eq
[2 * k
] == STATUS_VALID
&&
346 info
->eq
[2 * k
+ 1] == STATUS_VALID
) {
347 l
= isl_basic_map_alloc_equality(bmap
);
349 return isl_basic_map_free(bmap
);
350 isl_seq_cpy(bmap
->eq
[l
], info
->bmap
->eq
[k
], len
);
351 } else if (info
->eq
[2 * k
] == STATUS_VALID
) {
352 l
= isl_basic_map_alloc_inequality(bmap
);
354 return isl_basic_map_free(bmap
);
355 isl_seq_neg(bmap
->ineq
[l
], info
->bmap
->eq
[k
], len
);
356 } else if (info
->eq
[2 * k
+ 1] == STATUS_VALID
) {
357 l
= isl_basic_map_alloc_inequality(bmap
);
359 return isl_basic_map_free(bmap
);
360 isl_seq_cpy(bmap
->ineq
[l
], info
->bmap
->eq
[k
], len
);
364 for (k
= 0; k
< info
->bmap
->n_ineq
; ++k
) {
365 if (info
->ineq
[k
] != STATUS_VALID
)
367 l
= isl_basic_map_alloc_inequality(bmap
);
369 return isl_basic_map_free(bmap
);
370 isl_seq_cpy(bmap
->ineq
[l
], info
->bmap
->ineq
[k
], len
);
376 /* Is "bmap" defined by a number of (non-redundant) constraints that
377 * is greater than the number of constraints of basic maps i and j combined?
378 * Equalities are counted as two inequalities.
380 static int number_of_constraints_increases(int i
, int j
,
381 struct isl_coalesce_info
*info
,
382 __isl_keep isl_basic_map
*bmap
, struct isl_tab
*tab
)
386 n_old
= 2 * info
[i
].bmap
->n_eq
+ info
[i
].bmap
->n_ineq
;
387 n_old
+= 2 * info
[j
].bmap
->n_eq
+ info
[j
].bmap
->n_ineq
;
389 n_new
= 2 * bmap
->n_eq
;
390 for (k
= 0; k
< bmap
->n_ineq
; ++k
)
391 if (!isl_tab_is_redundant(tab
, bmap
->n_eq
+ k
))
394 return n_new
> n_old
;
397 /* Replace the pair of basic maps i and j by the basic map bounded
398 * by the valid constraints in both basic maps and the constraints
399 * in extra (if not NULL).
400 * Place the fused basic map in the position that is the smallest of i and j.
402 * If "detect_equalities" is set, then look for equalities encoded
403 * as pairs of inequalities.
404 * If "check_number" is set, then the original basic maps are only
405 * replaced if the total number of constraints does not increase.
406 * While the number of integer divisions in the two basic maps
407 * is assumed to be the same, the actual definitions may be different.
408 * We only copy the definition from one of the basic map if it is
409 * the same as that of the other basic map. Otherwise, we mark
410 * the integer division as unknown and simplify the basic map
411 * in an attempt to recover the integer division definition.
413 static enum isl_change
fuse(int i
, int j
, struct isl_coalesce_info
*info
,
414 __isl_keep isl_mat
*extra
, int detect_equalities
, int check_number
)
417 struct isl_basic_map
*fused
= NULL
;
418 struct isl_tab
*fused_tab
= NULL
;
419 unsigned total
= isl_basic_map_total_dim(info
[i
].bmap
);
420 unsigned extra_rows
= extra
? extra
->n_row
: 0;
421 unsigned n_eq
, n_ineq
;
425 return fuse(j
, i
, info
, extra
, detect_equalities
, check_number
);
427 n_eq
= info
[i
].bmap
->n_eq
+ info
[j
].bmap
->n_eq
;
428 n_ineq
= info
[i
].bmap
->n_ineq
+ info
[j
].bmap
->n_ineq
;
429 fused
= isl_basic_map_alloc_space(isl_space_copy(info
[i
].bmap
->dim
),
430 info
[i
].bmap
->n_div
, n_eq
, n_eq
+ n_ineq
+ extra_rows
);
431 fused
= add_valid_constraints(fused
, &info
[i
], 1 + total
);
432 fused
= add_valid_constraints(fused
, &info
[j
], 1 + total
);
435 if (ISL_F_ISSET(info
[i
].bmap
, ISL_BASIC_MAP_RATIONAL
) &&
436 ISL_F_ISSET(info
[j
].bmap
, ISL_BASIC_MAP_RATIONAL
))
437 ISL_F_SET(fused
, ISL_BASIC_MAP_RATIONAL
);
439 for (k
= 0; k
< info
[i
].bmap
->n_div
; ++k
) {
440 int l
= isl_basic_map_alloc_div(fused
);
443 if (isl_seq_eq(info
[i
].bmap
->div
[k
], info
[j
].bmap
->div
[k
],
445 isl_seq_cpy(fused
->div
[l
], info
[i
].bmap
->div
[k
],
448 isl_int_set_si(fused
->div
[l
][0], 0);
453 for (k
= 0; k
< extra_rows
; ++k
) {
454 l
= isl_basic_map_alloc_inequality(fused
);
457 isl_seq_cpy(fused
->ineq
[l
], extra
->row
[k
], 1 + total
);
460 if (detect_equalities
)
461 fused
= isl_basic_map_detect_inequality_pairs(fused
, NULL
);
462 fused
= isl_basic_map_gauss(fused
, NULL
);
463 if (simplify
|| info
[j
].simplify
) {
464 fused
= isl_basic_map_simplify(fused
);
465 info
[i
].simplify
= 0;
467 fused
= isl_basic_map_finalize(fused
);
469 fused_tab
= isl_tab_from_basic_map(fused
, 0);
470 if (isl_tab_detect_redundant(fused_tab
) < 0)
474 number_of_constraints_increases(i
, j
, info
, fused
, fused_tab
)) {
475 isl_tab_free(fused_tab
);
476 isl_basic_map_free(fused
);
477 return isl_change_none
;
480 isl_basic_map_free(info
[i
].bmap
);
481 info
[i
].bmap
= fused
;
482 isl_tab_free(info
[i
].tab
);
483 info
[i
].tab
= fused_tab
;
486 return isl_change_fuse
;
488 isl_tab_free(fused_tab
);
489 isl_basic_map_free(fused
);
490 return isl_change_error
;
493 /* Given a pair of basic maps i and j such that all constraints are either
494 * "valid" or "cut", check if the facets corresponding to the "cut"
495 * constraints of i lie entirely within basic map j.
496 * If so, replace the pair by the basic map consisting of the valid
497 * constraints in both basic maps.
498 * Checking whether the facet lies entirely within basic map j
499 * is performed by checking whether the constraints of basic map j
500 * are valid for the facet. These tests are performed on a rational
501 * tableau to avoid the theoretical possibility that a constraint
502 * that was considered to be a cut constraint for the entire basic map i
503 * happens to be considered to be a valid constraint for the facet,
504 * even though it cuts off the same rational points.
506 * To see that we are not introducing any extra points, call the
507 * two basic maps A and B and the resulting map U and let x
508 * be an element of U \setminus ( A \cup B ).
509 * A line connecting x with an element of A \cup B meets a facet F
510 * of either A or B. Assume it is a facet of B and let c_1 be
511 * the corresponding facet constraint. We have c_1(x) < 0 and
512 * so c_1 is a cut constraint. This implies that there is some
513 * (possibly rational) point x' satisfying the constraints of A
514 * and the opposite of c_1 as otherwise c_1 would have been marked
515 * valid for A. The line connecting x and x' meets a facet of A
516 * in a (possibly rational) point that also violates c_1, but this
517 * is impossible since all cut constraints of B are valid for all
519 * In case F is a facet of A rather than B, then we can apply the
520 * above reasoning to find a facet of B separating x from A \cup B first.
522 static enum isl_change
check_facets(int i
, int j
,
523 struct isl_coalesce_info
*info
)
526 struct isl_tab_undo
*snap
, *snap2
;
527 unsigned n_eq
= info
[i
].bmap
->n_eq
;
529 snap
= isl_tab_snap(info
[i
].tab
);
530 if (isl_tab_mark_rational(info
[i
].tab
) < 0)
531 return isl_change_error
;
532 snap2
= isl_tab_snap(info
[i
].tab
);
534 for (k
= 0; k
< info
[i
].bmap
->n_ineq
; ++k
) {
535 if (info
[i
].ineq
[k
] != STATUS_CUT
)
537 if (isl_tab_select_facet(info
[i
].tab
, n_eq
+ k
) < 0)
538 return isl_change_error
;
539 for (l
= 0; l
< info
[j
].bmap
->n_ineq
; ++l
) {
541 if (info
[j
].ineq
[l
] != STATUS_CUT
)
543 stat
= status_in(info
[j
].bmap
->ineq
[l
], info
[i
].tab
);
545 return isl_change_error
;
546 if (stat
!= STATUS_VALID
)
549 if (isl_tab_rollback(info
[i
].tab
, snap2
) < 0)
550 return isl_change_error
;
551 if (l
< info
[j
].bmap
->n_ineq
)
555 if (k
< info
[i
].bmap
->n_ineq
) {
556 if (isl_tab_rollback(info
[i
].tab
, snap
) < 0)
557 return isl_change_error
;
558 return isl_change_none
;
560 return fuse(i
, j
, info
, NULL
, 0, 0);
563 /* Check if info->bmap contains the basic map represented
564 * by the tableau "tab".
565 * For each equality, we check both the constraint itself
566 * (as an inequality) and its negation. Make sure the
567 * equality is returned to its original state before returning.
569 static int contains(struct isl_coalesce_info
*info
, struct isl_tab
*tab
)
573 isl_basic_map
*bmap
= info
->bmap
;
575 dim
= isl_basic_map_total_dim(bmap
);
576 for (k
= 0; k
< bmap
->n_eq
; ++k
) {
578 isl_seq_neg(bmap
->eq
[k
], bmap
->eq
[k
], 1 + dim
);
579 stat
= status_in(bmap
->eq
[k
], tab
);
580 isl_seq_neg(bmap
->eq
[k
], bmap
->eq
[k
], 1 + dim
);
583 if (stat
!= STATUS_VALID
)
585 stat
= status_in(bmap
->eq
[k
], tab
);
588 if (stat
!= STATUS_VALID
)
592 for (k
= 0; k
< bmap
->n_ineq
; ++k
) {
594 if (info
->ineq
[k
] == STATUS_REDUNDANT
)
596 stat
= status_in(bmap
->ineq
[k
], tab
);
599 if (stat
!= STATUS_VALID
)
605 /* Basic map "i" has an inequality (say "k") that is adjacent
606 * to some inequality of basic map "j". All the other inequalities
608 * Check if basic map "j" forms an extension of basic map "i".
610 * Note that this function is only called if some of the equalities or
611 * inequalities of basic map "j" do cut basic map "i". The function is
612 * correct even if there are no such cut constraints, but in that case
613 * the additional checks performed by this function are overkill.
615 * In particular, we replace constraint k, say f >= 0, by constraint
616 * f <= -1, add the inequalities of "j" that are valid for "i"
617 * and check if the result is a subset of basic map "j".
618 * To improve the chances of the subset relation being detected,
619 * any variable that only attains a single integer value
620 * in the tableau of "i" is first fixed to that value.
621 * If the result is a subset, then we know that this result is exactly equal
622 * to basic map "j" since all its constraints are valid for basic map "j".
623 * By combining the valid constraints of "i" (all equalities and all
624 * inequalities except "k") and the valid constraints of "j" we therefore
625 * obtain a basic map that is equal to their union.
626 * In this case, there is no need to perform a rollback of the tableau
627 * since it is going to be destroyed in fuse().
633 * |_______| _ |_________\
645 static enum isl_change
is_adj_ineq_extension(int i
, int j
,
646 struct isl_coalesce_info
*info
)
649 struct isl_tab_undo
*snap
;
650 unsigned n_eq
= info
[i
].bmap
->n_eq
;
651 unsigned total
= isl_basic_map_total_dim(info
[i
].bmap
);
655 if (isl_tab_extend_cons(info
[i
].tab
, 1 + info
[j
].bmap
->n_ineq
) < 0)
656 return isl_change_error
;
658 k
= find(info
[i
].ineq
, info
[i
].bmap
->n_ineq
, STATUS_ADJ_INEQ
);
660 isl_die(isl_basic_map_get_ctx(info
[i
].bmap
), isl_error_internal
,
661 "info[i].ineq should have exactly one STATUS_ADJ_INEQ",
662 return isl_change_error
);
664 snap
= isl_tab_snap(info
[i
].tab
);
666 if (isl_tab_unrestrict(info
[i
].tab
, n_eq
+ k
) < 0)
667 return isl_change_error
;
669 isl_seq_neg(info
[i
].bmap
->ineq
[k
], info
[i
].bmap
->ineq
[k
], 1 + total
);
670 isl_int_sub_ui(info
[i
].bmap
->ineq
[k
][0], info
[i
].bmap
->ineq
[k
][0], 1);
671 r
= isl_tab_add_ineq(info
[i
].tab
, info
[i
].bmap
->ineq
[k
]);
672 isl_seq_neg(info
[i
].bmap
->ineq
[k
], info
[i
].bmap
->ineq
[k
], 1 + total
);
673 isl_int_sub_ui(info
[i
].bmap
->ineq
[k
][0], info
[i
].bmap
->ineq
[k
][0], 1);
675 return isl_change_error
;
677 for (k
= 0; k
< info
[j
].bmap
->n_ineq
; ++k
) {
678 if (info
[j
].ineq
[k
] != STATUS_VALID
)
680 if (isl_tab_add_ineq(info
[i
].tab
, info
[j
].bmap
->ineq
[k
]) < 0)
681 return isl_change_error
;
683 if (isl_tab_detect_constants(info
[i
].tab
) < 0)
684 return isl_change_error
;
686 super
= contains(&info
[j
], info
[i
].tab
);
688 return isl_change_error
;
690 return fuse(i
, j
, info
, NULL
, 0, 0);
692 if (isl_tab_rollback(info
[i
].tab
, snap
) < 0)
693 return isl_change_error
;
695 return isl_change_none
;
699 /* Both basic maps have at least one inequality with and adjacent
700 * (but opposite) inequality in the other basic map.
701 * Check that there are no cut constraints and that there is only
702 * a single pair of adjacent inequalities.
703 * If so, we can replace the pair by a single basic map described
704 * by all but the pair of adjacent inequalities.
705 * Any additional points introduced lie strictly between the two
706 * adjacent hyperplanes and can therefore be integral.
715 * The test for a single pair of adjancent inequalities is important
716 * for avoiding the combination of two basic maps like the following
726 * If there are some cut constraints on one side, then we may
727 * still be able to fuse the two basic maps, but we need to perform
728 * some additional checks in is_adj_ineq_extension.
730 static enum isl_change
check_adj_ineq(int i
, int j
,
731 struct isl_coalesce_info
*info
)
733 int count_i
, count_j
;
736 count_i
= count(info
[i
].ineq
, info
[i
].bmap
->n_ineq
, STATUS_ADJ_INEQ
);
737 count_j
= count(info
[j
].ineq
, info
[j
].bmap
->n_ineq
, STATUS_ADJ_INEQ
);
739 if (count_i
!= 1 && count_j
!= 1)
740 return isl_change_none
;
742 cut_i
= any(info
[i
].eq
, 2 * info
[i
].bmap
->n_eq
, STATUS_CUT
) ||
743 any(info
[i
].ineq
, info
[i
].bmap
->n_ineq
, STATUS_CUT
);
744 cut_j
= any(info
[j
].eq
, 2 * info
[j
].bmap
->n_eq
, STATUS_CUT
) ||
745 any(info
[j
].ineq
, info
[j
].bmap
->n_ineq
, STATUS_CUT
);
747 if (!cut_i
&& !cut_j
&& count_i
== 1 && count_j
== 1)
748 return fuse(i
, j
, info
, NULL
, 0, 0);
750 if (count_i
== 1 && !cut_i
)
751 return is_adj_ineq_extension(i
, j
, info
);
753 if (count_j
== 1 && !cut_j
)
754 return is_adj_ineq_extension(j
, i
, info
);
756 return isl_change_none
;
759 /* Given an affine transformation matrix "T", does row "row" represent
760 * anything other than a unit vector (possibly shifted by a constant)
761 * that is not involved in any of the other rows?
763 * That is, if a constraint involves the variable corresponding to
764 * the row, then could its preimage by "T" have any coefficients
765 * that are different from those in the original constraint?
767 static int not_unique_unit_row(__isl_keep isl_mat
*T
, int row
)
770 int len
= T
->n_col
- 1;
772 i
= isl_seq_first_non_zero(T
->row
[row
] + 1, len
);
775 if (!isl_int_is_one(T
->row
[row
][1 + i
]) &&
776 !isl_int_is_negone(T
->row
[row
][1 + i
]))
779 j
= isl_seq_first_non_zero(T
->row
[row
] + 1 + i
+ 1, len
- (i
+ 1));
783 for (j
= 1; j
< T
->n_row
; ++j
) {
786 if (!isl_int_is_zero(T
->row
[j
][1 + i
]))
793 /* Does inequality constraint "ineq" of "bmap" involve any of
794 * the variables marked in "affected"?
795 * "total" is the total number of variables, i.e., the number
796 * of entries in "affected".
798 static int is_affected(__isl_keep isl_basic_map
*bmap
, int ineq
, int *affected
,
803 for (i
= 0; i
< total
; ++i
) {
806 if (!isl_int_is_zero(bmap
->ineq
[ineq
][1 + i
]))
813 /* Given the compressed version of inequality constraint "ineq"
814 * of info->bmap in "v", check if the constraint can be tightened,
815 * where the compression is based on an equality constraint valid
817 * If so, add the tightened version of the inequality constraint
818 * to info->tab. "v" may be modified by this function.
820 * That is, if the compressed constraint is of the form
824 * with 0 < c < m, then it is equivalent to
828 * This means that c can also be subtracted from the original,
829 * uncompressed constraint without affecting the integer points
830 * in info->tab. Add this tightened constraint as an extra row
831 * to info->tab to make this information explicitly available.
833 static __isl_give isl_vec
*try_tightening(struct isl_coalesce_info
*info
,
834 int ineq
, __isl_take isl_vec
*v
)
842 ctx
= isl_vec_get_ctx(v
);
843 isl_seq_gcd(v
->el
+ 1, v
->size
- 1, &ctx
->normalize_gcd
);
844 if (isl_int_is_zero(ctx
->normalize_gcd
) ||
845 isl_int_is_one(ctx
->normalize_gcd
)) {
853 isl_int_fdiv_r(v
->el
[0], v
->el
[0], ctx
->normalize_gcd
);
854 if (isl_int_is_zero(v
->el
[0]))
857 if (isl_tab_extend_cons(info
->tab
, 1) < 0)
858 return isl_vec_free(v
);
860 isl_int_sub(info
->bmap
->ineq
[ineq
][0],
861 info
->bmap
->ineq
[ineq
][0], v
->el
[0]);
862 r
= isl_tab_add_ineq(info
->tab
, info
->bmap
->ineq
[ineq
]);
863 isl_int_add(info
->bmap
->ineq
[ineq
][0],
864 info
->bmap
->ineq
[ineq
][0], v
->el
[0]);
867 return isl_vec_free(v
);
872 /* Tighten the (non-redundant) constraints on the facet represented
874 * In particular, on input, info->tab represents the result
875 * of relaxing the "n" inequality constraints of info->bmap in "relaxed"
876 * by one, i.e., replacing f_i >= 0 by f_i + 1 >= 0, and then
877 * replacing the one at index "l" by the corresponding equality,
878 * i.e., f_k + 1 = 0, with k = relaxed[l].
880 * Compute a variable compression from the equality constraint f_k + 1 = 0
881 * and use it to tighten the other constraints of info->bmap
882 * (that is, all constraints that have not been relaxed),
883 * updating info->tab (and leaving info->bmap untouched).
884 * The compression handles essentially two cases, one where a variable
885 * is assigned a fixed value and can therefore be eliminated, and one
886 * where one variable is a shifted multiple of some other variable and
887 * can therefore be replaced by that multiple.
888 * Gaussian elimination would also work for the first case, but for
889 * the second case, the effectiveness would depend on the order
891 * After compression, some of the constraints may have coefficients
892 * with a common divisor. If this divisor does not divide the constant
893 * term, then the constraint can be tightened.
894 * The tightening is performed on the tableau info->tab by introducing
895 * extra (temporary) constraints.
897 * Only constraints that are possibly affected by the compression are
898 * considered. In particular, if the constraint only involves variables
899 * that are directly mapped to a distinct set of other variables, then
900 * no common divisor can be introduced and no tightening can occur.
902 * It is important to only consider the non-redundant constraints
903 * since the facet constraint has been relaxed prior to the call
904 * to this function, meaning that the constraints that were redundant
905 * prior to the relaxation may no longer be redundant.
906 * These constraints will be ignored in the fused result, so
907 * the fusion detection should not exploit them.
909 static isl_stat
tighten_on_relaxed_facet(struct isl_coalesce_info
*info
,
910 int n
, int *relaxed
, int l
)
921 ctx
= isl_basic_map_get_ctx(info
->bmap
);
922 total
= isl_basic_map_total_dim(info
->bmap
);
923 isl_int_add_ui(info
->bmap
->ineq
[k
][0], info
->bmap
->ineq
[k
][0], 1);
924 T
= isl_mat_sub_alloc6(ctx
, info
->bmap
->ineq
, k
, 1, 0, 1 + total
);
925 T
= isl_mat_variable_compression(T
, NULL
);
926 isl_int_sub_ui(info
->bmap
->ineq
[k
][0], info
->bmap
->ineq
[k
][0], 1);
928 return isl_stat_error
;
934 affected
= isl_alloc_array(ctx
, int, total
);
938 for (i
= 0; i
< total
; ++i
)
939 affected
[i
] = not_unique_unit_row(T
, 1 + i
);
941 for (i
= 0; i
< info
->bmap
->n_ineq
; ++i
) {
942 if (any(relaxed
, n
, i
))
944 if (info
->ineq
[i
] == STATUS_REDUNDANT
)
946 if (!is_affected(info
->bmap
, i
, affected
, total
))
948 v
= isl_vec_alloc(ctx
, 1 + total
);
951 isl_seq_cpy(v
->el
, info
->bmap
->ineq
[i
], 1 + total
);
952 v
= isl_vec_mat_product(v
, isl_mat_copy(T
));
953 v
= try_tightening(info
, i
, v
);
965 return isl_stat_error
;
968 /* Replace the basic maps "i" and "j" by an extension of "i"
969 * along the "n" inequality constraints in "relax" by one.
970 * The tableau info[i].tab has already been extended.
971 * Extend info[i].bmap accordingly by relaxing all constraints in "relax"
973 * Each integer division that does not have exactly the same
974 * definition in "i" and "j" is marked unknown and the basic map
975 * is scheduled to be simplified in an attempt to recover
976 * the integer division definition.
977 * Place the extension in the position that is the smallest of i and j.
979 static enum isl_change
extend(int i
, int j
, int n
, int *relax
,
980 struct isl_coalesce_info
*info
)
985 info
[i
].bmap
= isl_basic_map_cow(info
[i
].bmap
);
987 return isl_change_error
;
988 total
= isl_basic_map_total_dim(info
[i
].bmap
);
989 for (l
= 0; l
< info
[i
].bmap
->n_div
; ++l
)
990 if (!isl_seq_eq(info
[i
].bmap
->div
[l
],
991 info
[j
].bmap
->div
[l
], 1 + 1 + total
)) {
992 isl_int_set_si(info
[i
].bmap
->div
[l
][0], 0);
993 info
[i
].simplify
= 1;
995 for (l
= 0; l
< n
; ++l
)
996 isl_int_add_ui(info
[i
].bmap
->ineq
[relax
[l
]][0],
997 info
[i
].bmap
->ineq
[relax
[l
]][0], 1);
998 ISL_F_SET(info
[i
].bmap
, ISL_BASIC_MAP_FINAL
);
1001 exchange(&info
[i
], &info
[j
]);
1002 return isl_change_fuse
;
1005 /* Basic map "i" has "n" inequality constraints (collected in "relax")
1006 * that are such that they include basic map "j" if they are relaxed
1007 * by one. All the other inequalities are valid for "j".
1008 * Check if basic map "j" forms an extension of basic map "i".
1010 * In particular, relax the constraints in "relax", compute the corresponding
1011 * facets one by one and check whether each of these is included
1012 * in the other basic map.
1013 * Before testing for inclusion, the constraints on each facet
1014 * are tightened to increase the chance of an inclusion being detected.
1015 * (Adding the valid constraints of "j" to the tableau of "i", as is done
1016 * in is_adj_ineq_extension, may further increase those chances, but this
1017 * is not currently done.)
1018 * If each facet is included, we know that relaxing the constraints extends
1019 * the basic map with exactly the other basic map (we already know that this
1020 * other basic map is included in the extension, because all other
1021 * inequality constraints are valid of "j") and we can replace the
1022 * two basic maps by this extension.
1038 static enum isl_change
is_relaxed_extension(int i
, int j
, int n
, int *relax
,
1039 struct isl_coalesce_info
*info
)
1043 struct isl_tab_undo
*snap
, *snap2
;
1044 unsigned n_eq
= info
[i
].bmap
->n_eq
;
1046 for (l
= 0; l
< n
; ++l
)
1047 if (isl_tab_is_equality(info
[i
].tab
, n_eq
+ relax
[l
]))
1048 return isl_change_none
;
1050 snap
= isl_tab_snap(info
[i
].tab
);
1051 for (l
= 0; l
< n
; ++l
)
1052 if (isl_tab_relax(info
[i
].tab
, n_eq
+ relax
[l
]) < 0)
1053 return isl_change_error
;
1054 snap2
= isl_tab_snap(info
[i
].tab
);
1055 for (l
= 0; l
< n
; ++l
) {
1056 if (isl_tab_rollback(info
[i
].tab
, snap2
) < 0)
1057 return isl_change_error
;
1058 if (isl_tab_select_facet(info
[i
].tab
, n_eq
+ relax
[l
]) < 0)
1059 return isl_change_error
;
1060 if (tighten_on_relaxed_facet(&info
[i
], n
, relax
, l
) < 0)
1061 return isl_change_error
;
1062 super
= contains(&info
[j
], info
[i
].tab
);
1064 return isl_change_error
;
1067 if (isl_tab_rollback(info
[i
].tab
, snap
) < 0)
1068 return isl_change_error
;
1069 return isl_change_none
;
1072 if (isl_tab_rollback(info
[i
].tab
, snap2
) < 0)
1073 return isl_change_error
;
1074 return extend(i
, j
, n
, relax
, info
);
1077 /* Data structure that keeps track of the wrapping constraints
1078 * and of information to bound the coefficients of those constraints.
1080 * bound is set if we want to apply a bound on the coefficients
1081 * mat contains the wrapping constraints
1082 * max is the bound on the coefficients (if bound is set)
1090 /* Update wraps->max to be greater than or equal to the coefficients
1091 * in the equalities and inequalities of info->bmap that can be removed
1092 * if we end up applying wrapping.
1094 static void wraps_update_max(struct isl_wraps
*wraps
,
1095 struct isl_coalesce_info
*info
)
1099 unsigned total
= isl_basic_map_total_dim(info
->bmap
);
1101 isl_int_init(max_k
);
1103 for (k
= 0; k
< info
->bmap
->n_eq
; ++k
) {
1104 if (info
->eq
[2 * k
] == STATUS_VALID
&&
1105 info
->eq
[2 * k
+ 1] == STATUS_VALID
)
1107 isl_seq_abs_max(info
->bmap
->eq
[k
] + 1, total
, &max_k
);
1108 if (isl_int_abs_gt(max_k
, wraps
->max
))
1109 isl_int_set(wraps
->max
, max_k
);
1112 for (k
= 0; k
< info
->bmap
->n_ineq
; ++k
) {
1113 if (info
->ineq
[k
] == STATUS_VALID
||
1114 info
->ineq
[k
] == STATUS_REDUNDANT
)
1116 isl_seq_abs_max(info
->bmap
->ineq
[k
] + 1, total
, &max_k
);
1117 if (isl_int_abs_gt(max_k
, wraps
->max
))
1118 isl_int_set(wraps
->max
, max_k
);
1121 isl_int_clear(max_k
);
1124 /* Initialize the isl_wraps data structure.
1125 * If we want to bound the coefficients of the wrapping constraints,
1126 * we set wraps->max to the largest coefficient
1127 * in the equalities and inequalities that can be removed if we end up
1128 * applying wrapping.
1130 static void wraps_init(struct isl_wraps
*wraps
, __isl_take isl_mat
*mat
,
1131 struct isl_coalesce_info
*info
, int i
, int j
)
1139 ctx
= isl_mat_get_ctx(mat
);
1140 wraps
->bound
= isl_options_get_coalesce_bounded_wrapping(ctx
);
1143 isl_int_init(wraps
->max
);
1144 isl_int_set_si(wraps
->max
, 0);
1145 wraps_update_max(wraps
, &info
[i
]);
1146 wraps_update_max(wraps
, &info
[j
]);
1149 /* Free the contents of the isl_wraps data structure.
1151 static void wraps_free(struct isl_wraps
*wraps
)
1153 isl_mat_free(wraps
->mat
);
1155 isl_int_clear(wraps
->max
);
1158 /* Is the wrapping constraint in row "row" allowed?
1160 * If wraps->bound is set, we check that none of the coefficients
1161 * is greater than wraps->max.
1163 static int allow_wrap(struct isl_wraps
*wraps
, int row
)
1170 for (i
= 1; i
< wraps
->mat
->n_col
; ++i
)
1171 if (isl_int_abs_gt(wraps
->mat
->row
[row
][i
], wraps
->max
))
1177 /* Wrap "ineq" (or its opposite if "negate" is set) around "bound"
1178 * to include "set" and add the result in position "w" of "wraps".
1179 * "len" is the total number of coefficients in "bound" and "ineq".
1180 * Return 1 on success, 0 on failure and -1 on error.
1181 * Wrapping can fail if the result of wrapping is equal to "bound"
1182 * or if we want to bound the sizes of the coefficients and
1183 * the wrapped constraint does not satisfy this bound.
1185 static int add_wrap(struct isl_wraps
*wraps
, int w
, isl_int
*bound
,
1186 isl_int
*ineq
, unsigned len
, __isl_keep isl_set
*set
, int negate
)
1188 isl_seq_cpy(wraps
->mat
->row
[w
], bound
, len
);
1190 isl_seq_neg(wraps
->mat
->row
[w
+ 1], ineq
, len
);
1191 ineq
= wraps
->mat
->row
[w
+ 1];
1193 if (!isl_set_wrap_facet(set
, wraps
->mat
->row
[w
], ineq
))
1195 if (isl_seq_eq(wraps
->mat
->row
[w
], bound
, len
))
1197 if (!allow_wrap(wraps
, w
))
1202 /* For each constraint in info->bmap that is not redundant (as determined
1203 * by info->tab) and that is not a valid constraint for the other basic map,
1204 * wrap the constraint around "bound" such that it includes the whole
1205 * set "set" and append the resulting constraint to "wraps".
1206 * Note that the constraints that are valid for the other basic map
1207 * will be added to the combined basic map by default, so there is
1208 * no need to wrap them.
1209 * The caller wrap_in_facets even relies on this function not wrapping
1210 * any constraints that are already valid.
1211 * "wraps" is assumed to have been pre-allocated to the appropriate size.
1212 * wraps->n_row is the number of actual wrapped constraints that have
1214 * If any of the wrapping problems results in a constraint that is
1215 * identical to "bound", then this means that "set" is unbounded in such
1216 * way that no wrapping is possible. If this happens then wraps->n_row
1218 * Similarly, if we want to bound the coefficients of the wrapping
1219 * constraints and a newly added wrapping constraint does not
1220 * satisfy the bound, then wraps->n_row is also reset to zero.
1222 static int add_wraps(struct isl_wraps
*wraps
, struct isl_coalesce_info
*info
,
1223 isl_int
*bound
, __isl_keep isl_set
*set
)
1228 isl_basic_map
*bmap
= info
->bmap
;
1229 unsigned len
= 1 + isl_basic_map_total_dim(bmap
);
1231 w
= wraps
->mat
->n_row
;
1233 for (l
= 0; l
< bmap
->n_ineq
; ++l
) {
1234 if (info
->ineq
[l
] == STATUS_VALID
||
1235 info
->ineq
[l
] == STATUS_REDUNDANT
)
1237 if (isl_seq_is_neg(bound
, bmap
->ineq
[l
], len
))
1239 if (isl_seq_eq(bound
, bmap
->ineq
[l
], len
))
1241 if (isl_tab_is_redundant(info
->tab
, bmap
->n_eq
+ l
))
1244 added
= add_wrap(wraps
, w
, bound
, bmap
->ineq
[l
], len
, set
, 0);
1251 for (l
= 0; l
< bmap
->n_eq
; ++l
) {
1252 if (isl_seq_is_neg(bound
, bmap
->eq
[l
], len
))
1254 if (isl_seq_eq(bound
, bmap
->eq
[l
], len
))
1257 for (m
= 0; m
< 2; ++m
) {
1258 if (info
->eq
[2 * l
+ m
] == STATUS_VALID
)
1260 added
= add_wrap(wraps
, w
, bound
, bmap
->eq
[l
], len
,
1270 wraps
->mat
->n_row
= w
;
1273 wraps
->mat
->n_row
= 0;
1277 /* Check if the constraints in "wraps" from "first" until the last
1278 * are all valid for the basic set represented by "tab".
1279 * If not, wraps->n_row is set to zero.
1281 static int check_wraps(__isl_keep isl_mat
*wraps
, int first
,
1282 struct isl_tab
*tab
)
1286 for (i
= first
; i
< wraps
->n_row
; ++i
) {
1287 enum isl_ineq_type type
;
1288 type
= isl_tab_ineq_type(tab
, wraps
->row
[i
]);
1289 if (type
== isl_ineq_error
)
1291 if (type
== isl_ineq_redundant
)
1300 /* Return a set that corresponds to the non-redundant constraints
1301 * (as recorded in tab) of bmap.
1303 * It's important to remove the redundant constraints as some
1304 * of the other constraints may have been modified after the
1305 * constraints were marked redundant.
1306 * In particular, a constraint may have been relaxed.
1307 * Redundant constraints are ignored when a constraint is relaxed
1308 * and should therefore continue to be ignored ever after.
1309 * Otherwise, the relaxation might be thwarted by some of
1310 * these constraints.
1312 * Update the underlying set to ensure that the dimension doesn't change.
1313 * Otherwise the integer divisions could get dropped if the tab
1314 * turns out to be empty.
1316 static __isl_give isl_set
*set_from_updated_bmap(__isl_keep isl_basic_map
*bmap
,
1317 struct isl_tab
*tab
)
1319 isl_basic_set
*bset
;
1321 bmap
= isl_basic_map_copy(bmap
);
1322 bset
= isl_basic_map_underlying_set(bmap
);
1323 bset
= isl_basic_set_cow(bset
);
1324 bset
= isl_basic_set_update_from_tab(bset
, tab
);
1325 return isl_set_from_basic_set(bset
);
1328 /* Wrap the constraints of info->bmap that bound the facet defined
1329 * by inequality "k" around (the opposite of) this inequality to
1330 * include "set". "bound" may be used to store the negated inequality.
1331 * Since the wrapped constraints are not guaranteed to contain the whole
1332 * of info->bmap, we check them in check_wraps.
1333 * If any of the wrapped constraints turn out to be invalid, then
1334 * check_wraps will reset wrap->n_row to zero.
1336 static int add_wraps_around_facet(struct isl_wraps
*wraps
,
1337 struct isl_coalesce_info
*info
, int k
, isl_int
*bound
,
1338 __isl_keep isl_set
*set
)
1340 struct isl_tab_undo
*snap
;
1342 unsigned total
= isl_basic_map_total_dim(info
->bmap
);
1344 snap
= isl_tab_snap(info
->tab
);
1346 if (isl_tab_select_facet(info
->tab
, info
->bmap
->n_eq
+ k
) < 0)
1348 if (isl_tab_detect_redundant(info
->tab
) < 0)
1351 isl_seq_neg(bound
, info
->bmap
->ineq
[k
], 1 + total
);
1353 n
= wraps
->mat
->n_row
;
1354 if (add_wraps(wraps
, info
, bound
, set
) < 0)
1357 if (isl_tab_rollback(info
->tab
, snap
) < 0)
1359 if (check_wraps(wraps
->mat
, n
, info
->tab
) < 0)
1365 /* Given a basic set i with a constraint k that is adjacent to
1366 * basic set j, check if we can wrap
1367 * both the facet corresponding to k (if "wrap_facet" is set) and basic map j
1368 * (always) around their ridges to include the other set.
1369 * If so, replace the pair of basic sets by their union.
1371 * All constraints of i (except k) are assumed to be valid or
1372 * cut constraints for j.
1373 * Wrapping the cut constraints to include basic map j may result
1374 * in constraints that are no longer valid of basic map i
1375 * we have to check that the resulting wrapping constraints are valid for i.
1376 * If "wrap_facet" is not set, then all constraints of i (except k)
1377 * are assumed to be valid for j.
1386 static enum isl_change
can_wrap_in_facet(int i
, int j
, int k
,
1387 struct isl_coalesce_info
*info
, int wrap_facet
)
1389 enum isl_change change
= isl_change_none
;
1390 struct isl_wraps wraps
;
1393 struct isl_set
*set_i
= NULL
;
1394 struct isl_set
*set_j
= NULL
;
1395 struct isl_vec
*bound
= NULL
;
1396 unsigned total
= isl_basic_map_total_dim(info
[i
].bmap
);
1398 set_i
= set_from_updated_bmap(info
[i
].bmap
, info
[i
].tab
);
1399 set_j
= set_from_updated_bmap(info
[j
].bmap
, info
[j
].tab
);
1400 ctx
= isl_basic_map_get_ctx(info
[i
].bmap
);
1401 mat
= isl_mat_alloc(ctx
, 2 * (info
[i
].bmap
->n_eq
+ info
[j
].bmap
->n_eq
) +
1402 info
[i
].bmap
->n_ineq
+ info
[j
].bmap
->n_ineq
,
1404 wraps_init(&wraps
, mat
, info
, i
, j
);
1405 bound
= isl_vec_alloc(ctx
, 1 + total
);
1406 if (!set_i
|| !set_j
|| !wraps
.mat
|| !bound
)
1409 isl_seq_cpy(bound
->el
, info
[i
].bmap
->ineq
[k
], 1 + total
);
1410 isl_int_add_ui(bound
->el
[0], bound
->el
[0], 1);
1412 isl_seq_cpy(wraps
.mat
->row
[0], bound
->el
, 1 + total
);
1413 wraps
.mat
->n_row
= 1;
1415 if (add_wraps(&wraps
, &info
[j
], bound
->el
, set_i
) < 0)
1417 if (!wraps
.mat
->n_row
)
1421 if (add_wraps_around_facet(&wraps
, &info
[i
], k
,
1422 bound
->el
, set_j
) < 0)
1424 if (!wraps
.mat
->n_row
)
1428 change
= fuse(i
, j
, info
, wraps
.mat
, 0, 0);
1433 isl_set_free(set_i
);
1434 isl_set_free(set_j
);
1436 isl_vec_free(bound
);
1441 isl_vec_free(bound
);
1442 isl_set_free(set_i
);
1443 isl_set_free(set_j
);
1444 return isl_change_error
;
1447 /* Given a cut constraint t(x) >= 0 of basic map i, stored in row "w"
1448 * of wrap.mat, replace it by its relaxed version t(x) + 1 >= 0, and
1449 * add wrapping constraints to wrap.mat for all constraints
1450 * of basic map j that bound the part of basic map j that sticks out
1451 * of the cut constraint.
1452 * "set_i" is the underlying set of basic map i.
1453 * If any wrapping fails, then wraps->mat.n_row is reset to zero.
1455 * In particular, we first intersect basic map j with t(x) + 1 = 0.
1456 * If the result is empty, then t(x) >= 0 was actually a valid constraint
1457 * (with respect to the integer points), so we add t(x) >= 0 instead.
1458 * Otherwise, we wrap the constraints of basic map j that are not
1459 * redundant in this intersection and that are not already valid
1460 * for basic map i over basic map i.
1461 * Note that it is sufficient to wrap the constraints to include
1462 * basic map i, because we will only wrap the constraints that do
1463 * not include basic map i already. The wrapped constraint will
1464 * therefore be more relaxed compared to the original constraint.
1465 * Since the original constraint is valid for basic map j, so is
1466 * the wrapped constraint.
1468 static isl_stat
wrap_in_facet(struct isl_wraps
*wraps
, int w
,
1469 struct isl_coalesce_info
*info_j
, __isl_keep isl_set
*set_i
,
1470 struct isl_tab_undo
*snap
)
1472 isl_int_add_ui(wraps
->mat
->row
[w
][0], wraps
->mat
->row
[w
][0], 1);
1473 if (isl_tab_add_eq(info_j
->tab
, wraps
->mat
->row
[w
]) < 0)
1474 return isl_stat_error
;
1475 if (isl_tab_detect_redundant(info_j
->tab
) < 0)
1476 return isl_stat_error
;
1478 if (info_j
->tab
->empty
)
1479 isl_int_sub_ui(wraps
->mat
->row
[w
][0], wraps
->mat
->row
[w
][0], 1);
1480 else if (add_wraps(wraps
, info_j
, wraps
->mat
->row
[w
], set_i
) < 0)
1481 return isl_stat_error
;
1483 if (isl_tab_rollback(info_j
->tab
, snap
) < 0)
1484 return isl_stat_error
;
1489 /* Given a pair of basic maps i and j such that j sticks out
1490 * of i at n cut constraints, each time by at most one,
1491 * try to compute wrapping constraints and replace the two
1492 * basic maps by a single basic map.
1493 * The other constraints of i are assumed to be valid for j.
1494 * "set_i" is the underlying set of basic map i.
1495 * "wraps" has been initialized to be of the right size.
1497 * For each cut constraint t(x) >= 0 of i, we add the relaxed version
1498 * t(x) + 1 >= 0, along with wrapping constraints for all constraints
1499 * of basic map j that bound the part of basic map j that sticks out
1500 * of the cut constraint.
1502 * If any wrapping fails, i.e., if we cannot wrap to touch
1503 * the union, then we give up.
1504 * Otherwise, the pair of basic maps is replaced by their union.
1506 static enum isl_change
try_wrap_in_facets(int i
, int j
,
1507 struct isl_coalesce_info
*info
, struct isl_wraps
*wraps
,
1508 __isl_keep isl_set
*set_i
)
1512 struct isl_tab_undo
*snap
;
1514 total
= isl_basic_map_total_dim(info
[i
].bmap
);
1516 snap
= isl_tab_snap(info
[j
].tab
);
1518 wraps
->mat
->n_row
= 0;
1520 for (k
= 0; k
< info
[i
].bmap
->n_eq
; ++k
) {
1521 for (l
= 0; l
< 2; ++l
) {
1522 if (info
[i
].eq
[2 * k
+ l
] != STATUS_CUT
)
1524 w
= wraps
->mat
->n_row
++;
1526 isl_seq_neg(wraps
->mat
->row
[w
],
1527 info
[i
].bmap
->eq
[k
], 1 + total
);
1529 isl_seq_cpy(wraps
->mat
->row
[w
],
1530 info
[i
].bmap
->eq
[k
], 1 + total
);
1531 if (wrap_in_facet(wraps
, w
, &info
[j
], set_i
, snap
) < 0)
1532 return isl_change_error
;
1534 if (!wraps
->mat
->n_row
)
1535 return isl_change_none
;
1539 for (k
= 0; k
< info
[i
].bmap
->n_ineq
; ++k
) {
1540 if (info
[i
].ineq
[k
] != STATUS_CUT
)
1542 w
= wraps
->mat
->n_row
++;
1543 isl_seq_cpy(wraps
->mat
->row
[w
],
1544 info
[i
].bmap
->ineq
[k
], 1 + total
);
1545 if (wrap_in_facet(wraps
, w
, &info
[j
], set_i
, snap
) < 0)
1546 return isl_change_error
;
1548 if (!wraps
->mat
->n_row
)
1549 return isl_change_none
;
1552 return fuse(i
, j
, info
, wraps
->mat
, 0, 1);
1555 /* Given a pair of basic maps i and j such that j sticks out
1556 * of i at n cut constraints, each time by at most one,
1557 * try to compute wrapping constraints and replace the two
1558 * basic maps by a single basic map.
1559 * The other constraints of i are assumed to be valid for j.
1561 * The core computation is performed by try_wrap_in_facets.
1562 * This function simply extracts an underlying set representation
1563 * of basic map i and initializes the data structure for keeping
1564 * track of wrapping constraints.
1566 static enum isl_change
wrap_in_facets(int i
, int j
, int n
,
1567 struct isl_coalesce_info
*info
)
1569 enum isl_change change
= isl_change_none
;
1570 struct isl_wraps wraps
;
1573 isl_set
*set_i
= NULL
;
1574 unsigned total
= isl_basic_map_total_dim(info
[i
].bmap
);
1577 if (isl_tab_extend_cons(info
[j
].tab
, 1) < 0)
1578 return isl_change_error
;
1580 max_wrap
= 1 + 2 * info
[j
].bmap
->n_eq
+ info
[j
].bmap
->n_ineq
;
1583 set_i
= set_from_updated_bmap(info
[i
].bmap
, info
[i
].tab
);
1584 ctx
= isl_basic_map_get_ctx(info
[i
].bmap
);
1585 mat
= isl_mat_alloc(ctx
, max_wrap
, 1 + total
);
1586 wraps_init(&wraps
, mat
, info
, i
, j
);
1587 if (!set_i
|| !wraps
.mat
)
1590 change
= try_wrap_in_facets(i
, j
, info
, &wraps
, set_i
);
1593 isl_set_free(set_i
);
1598 isl_set_free(set_i
);
1599 return isl_change_error
;
1602 /* Return the effect of inequality "ineq" on the tableau "tab",
1603 * after relaxing the constant term of "ineq" by one.
1605 static enum isl_ineq_type
type_of_relaxed(struct isl_tab
*tab
, isl_int
*ineq
)
1607 enum isl_ineq_type type
;
1609 isl_int_add_ui(ineq
[0], ineq
[0], 1);
1610 type
= isl_tab_ineq_type(tab
, ineq
);
1611 isl_int_sub_ui(ineq
[0], ineq
[0], 1);
1616 /* Given two basic sets i and j,
1617 * check if relaxing all the cut constraints of i by one turns
1618 * them into valid constraint for j and check if we can wrap in
1619 * the bits that are sticking out.
1620 * If so, replace the pair by their union.
1622 * We first check if all relaxed cut inequalities of i are valid for j
1623 * and then try to wrap in the intersections of the relaxed cut inequalities
1626 * During this wrapping, we consider the points of j that lie at a distance
1627 * of exactly 1 from i. In particular, we ignore the points that lie in
1628 * between this lower-dimensional space and the basic map i.
1629 * We can therefore only apply this to integer maps.
1655 * Wrapping can fail if the result of wrapping one of the facets
1656 * around its edges does not produce any new facet constraint.
1657 * In particular, this happens when we try to wrap in unbounded sets.
1659 * _______________________________________________________________________
1663 * |_| |_________________________________________________________________
1666 * The following is not an acceptable result of coalescing the above two
1667 * sets as it includes extra integer points.
1668 * _______________________________________________________________________
1673 * \______________________________________________________________________
1675 static enum isl_change
can_wrap_in_set(int i
, int j
,
1676 struct isl_coalesce_info
*info
)
1682 if (ISL_F_ISSET(info
[i
].bmap
, ISL_BASIC_MAP_RATIONAL
) ||
1683 ISL_F_ISSET(info
[j
].bmap
, ISL_BASIC_MAP_RATIONAL
))
1684 return isl_change_none
;
1686 n
= count(info
[i
].eq
, 2 * info
[i
].bmap
->n_eq
, STATUS_CUT
);
1687 n
+= count(info
[i
].ineq
, info
[i
].bmap
->n_ineq
, STATUS_CUT
);
1689 return isl_change_none
;
1691 total
= isl_basic_map_total_dim(info
[i
].bmap
);
1692 for (k
= 0; k
< info
[i
].bmap
->n_eq
; ++k
) {
1693 for (l
= 0; l
< 2; ++l
) {
1694 enum isl_ineq_type type
;
1696 if (info
[i
].eq
[2 * k
+ l
] != STATUS_CUT
)
1700 isl_seq_neg(info
[i
].bmap
->eq
[k
],
1701 info
[i
].bmap
->eq
[k
], 1 + total
);
1702 type
= type_of_relaxed(info
[j
].tab
,
1703 info
[i
].bmap
->eq
[k
]);
1705 isl_seq_neg(info
[i
].bmap
->eq
[k
],
1706 info
[i
].bmap
->eq
[k
], 1 + total
);
1707 if (type
== isl_ineq_error
)
1708 return isl_change_error
;
1709 if (type
!= isl_ineq_redundant
)
1710 return isl_change_none
;
1714 for (k
= 0; k
< info
[i
].bmap
->n_ineq
; ++k
) {
1715 enum isl_ineq_type type
;
1717 if (info
[i
].ineq
[k
] != STATUS_CUT
)
1720 type
= type_of_relaxed(info
[j
].tab
, info
[i
].bmap
->ineq
[k
]);
1721 if (type
== isl_ineq_error
)
1722 return isl_change_error
;
1723 if (type
!= isl_ineq_redundant
)
1724 return isl_change_none
;
1727 return wrap_in_facets(i
, j
, n
, info
);
1730 /* Check if either i or j has only cut constraints that can
1731 * be used to wrap in (a facet of) the other basic set.
1732 * if so, replace the pair by their union.
1734 static enum isl_change
check_wrap(int i
, int j
, struct isl_coalesce_info
*info
)
1736 enum isl_change change
= isl_change_none
;
1738 change
= can_wrap_in_set(i
, j
, info
);
1739 if (change
!= isl_change_none
)
1742 change
= can_wrap_in_set(j
, i
, info
);
1746 /* Check if all inequality constraints of "i" that cut "j" cease
1747 * to be cut constraints if they are relaxed by one.
1748 * If so, collect the cut constraints in "list".
1749 * The caller is responsible for allocating "list".
1751 static isl_bool
all_cut_by_one(int i
, int j
, struct isl_coalesce_info
*info
,
1757 for (l
= 0; l
< info
[i
].bmap
->n_ineq
; ++l
) {
1758 enum isl_ineq_type type
;
1760 if (info
[i
].ineq
[l
] != STATUS_CUT
)
1762 type
= type_of_relaxed(info
[j
].tab
, info
[i
].bmap
->ineq
[l
]);
1763 if (type
== isl_ineq_error
)
1764 return isl_bool_error
;
1765 if (type
!= isl_ineq_redundant
)
1766 return isl_bool_false
;
1770 return isl_bool_true
;
1773 /* Given two basic maps such that "j" has at least one equality constraint
1774 * that is adjacent to an inequality constraint of "i" and such that "i" has
1775 * exactly one inequality constraint that is adjacent to an equality
1776 * constraint of "j", check whether "i" can be extended to include "j" or
1777 * whether "j" can be wrapped into "i".
1778 * All remaining constraints of "i" and "j" are assumed to be valid
1779 * or cut constraints of the other basic map.
1780 * However, none of the equality constraints of "i" are cut constraints.
1782 * If "i" has any "cut" inequality constraints, then check if relaxing
1783 * each of them by one is sufficient for them to become valid.
1784 * If so, check if the inequality constraint adjacent to an equality
1785 * constraint of "j" along with all these cut constraints
1786 * can be relaxed by one to contain exactly "j".
1787 * Otherwise, or if this fails, check if "j" can be wrapped into "i".
1789 static enum isl_change
check_single_adj_eq(int i
, int j
,
1790 struct isl_coalesce_info
*info
)
1792 enum isl_change change
= isl_change_none
;
1799 n_cut
= count(info
[i
].ineq
, info
[i
].bmap
->n_ineq
, STATUS_CUT
);
1801 k
= find(info
[i
].ineq
, info
[i
].bmap
->n_ineq
, STATUS_ADJ_EQ
);
1804 ctx
= isl_basic_map_get_ctx(info
[i
].bmap
);
1805 relax
= isl_calloc_array(ctx
, int, 1 + n_cut
);
1807 return isl_change_error
;
1809 try_relax
= all_cut_by_one(i
, j
, info
, relax
+ 1);
1811 change
= isl_change_error
;
1813 try_relax
= isl_bool_true
;
1816 if (try_relax
&& change
== isl_change_none
)
1817 change
= is_relaxed_extension(i
, j
, 1 + n_cut
, relax
, info
);
1820 if (change
!= isl_change_none
)
1823 change
= can_wrap_in_facet(i
, j
, k
, info
, n_cut
> 0);
1828 /* At least one of the basic maps has an equality that is adjacent
1829 * to inequality. Make sure that only one of the basic maps has
1830 * such an equality and that the other basic map has exactly one
1831 * inequality adjacent to an equality.
1832 * If the other basic map does not have such an inequality, then
1833 * check if all its constraints are either valid or cut constraints
1834 * and, if so, try wrapping in the first map into the second.
1835 * Otherwise, try to extend one basic map with the other or
1836 * wrap one basic map in the other.
1838 static enum isl_change
check_adj_eq(int i
, int j
,
1839 struct isl_coalesce_info
*info
)
1841 if (any(info
[i
].eq
, 2 * info
[i
].bmap
->n_eq
, STATUS_ADJ_INEQ
) &&
1842 any(info
[j
].eq
, 2 * info
[j
].bmap
->n_eq
, STATUS_ADJ_INEQ
))
1843 /* ADJ EQ TOO MANY */
1844 return isl_change_none
;
1846 if (any(info
[i
].eq
, 2 * info
[i
].bmap
->n_eq
, STATUS_ADJ_INEQ
))
1847 return check_adj_eq(j
, i
, info
);
1849 /* j has an equality adjacent to an inequality in i */
1851 if (count(info
[i
].ineq
, info
[i
].bmap
->n_ineq
, STATUS_ADJ_EQ
) != 1) {
1852 if (all_valid_or_cut(&info
[i
]))
1853 return can_wrap_in_set(i
, j
, info
);
1854 return isl_change_none
;
1856 if (any(info
[i
].eq
, 2 * info
[i
].bmap
->n_eq
, STATUS_CUT
))
1857 return isl_change_none
;
1858 if (any(info
[j
].ineq
, info
[j
].bmap
->n_ineq
, STATUS_ADJ_EQ
) ||
1859 any(info
[i
].ineq
, info
[i
].bmap
->n_ineq
, STATUS_ADJ_INEQ
) ||
1860 any(info
[j
].ineq
, info
[j
].bmap
->n_ineq
, STATUS_ADJ_INEQ
))
1861 /* ADJ EQ TOO MANY */
1862 return isl_change_none
;
1864 return check_single_adj_eq(i
, j
, info
);
1867 /* The two basic maps lie on adjacent hyperplanes. In particular,
1868 * basic map "i" has an equality that lies parallel to basic map "j".
1869 * Check if we can wrap the facets around the parallel hyperplanes
1870 * to include the other set.
1872 * We perform basically the same operations as can_wrap_in_facet,
1873 * except that we don't need to select a facet of one of the sets.
1879 * If there is more than one equality of "i" adjacent to an equality of "j",
1880 * then the result will satisfy one or more equalities that are a linear
1881 * combination of these equalities. These will be encoded as pairs
1882 * of inequalities in the wrapping constraints and need to be made
1885 static enum isl_change
check_eq_adj_eq(int i
, int j
,
1886 struct isl_coalesce_info
*info
)
1889 enum isl_change change
= isl_change_none
;
1890 int detect_equalities
= 0;
1891 struct isl_wraps wraps
;
1894 struct isl_set
*set_i
= NULL
;
1895 struct isl_set
*set_j
= NULL
;
1896 struct isl_vec
*bound
= NULL
;
1897 unsigned total
= isl_basic_map_total_dim(info
[i
].bmap
);
1899 if (count(info
[i
].eq
, 2 * info
[i
].bmap
->n_eq
, STATUS_ADJ_EQ
) != 1)
1900 detect_equalities
= 1;
1902 k
= find(info
[i
].eq
, 2 * info
[i
].bmap
->n_eq
, STATUS_ADJ_EQ
);
1904 set_i
= set_from_updated_bmap(info
[i
].bmap
, info
[i
].tab
);
1905 set_j
= set_from_updated_bmap(info
[j
].bmap
, info
[j
].tab
);
1906 ctx
= isl_basic_map_get_ctx(info
[i
].bmap
);
1907 mat
= isl_mat_alloc(ctx
, 2 * (info
[i
].bmap
->n_eq
+ info
[j
].bmap
->n_eq
) +
1908 info
[i
].bmap
->n_ineq
+ info
[j
].bmap
->n_ineq
,
1910 wraps_init(&wraps
, mat
, info
, i
, j
);
1911 bound
= isl_vec_alloc(ctx
, 1 + total
);
1912 if (!set_i
|| !set_j
|| !wraps
.mat
|| !bound
)
1916 isl_seq_neg(bound
->el
, info
[i
].bmap
->eq
[k
/ 2], 1 + total
);
1918 isl_seq_cpy(bound
->el
, info
[i
].bmap
->eq
[k
/ 2], 1 + total
);
1919 isl_int_add_ui(bound
->el
[0], bound
->el
[0], 1);
1921 isl_seq_cpy(wraps
.mat
->row
[0], bound
->el
, 1 + total
);
1922 wraps
.mat
->n_row
= 1;
1924 if (add_wraps(&wraps
, &info
[j
], bound
->el
, set_i
) < 0)
1926 if (!wraps
.mat
->n_row
)
1929 isl_int_sub_ui(bound
->el
[0], bound
->el
[0], 1);
1930 isl_seq_neg(bound
->el
, bound
->el
, 1 + total
);
1932 isl_seq_cpy(wraps
.mat
->row
[wraps
.mat
->n_row
], bound
->el
, 1 + total
);
1935 if (add_wraps(&wraps
, &info
[i
], bound
->el
, set_j
) < 0)
1937 if (!wraps
.mat
->n_row
)
1940 change
= fuse(i
, j
, info
, wraps
.mat
, detect_equalities
, 0);
1943 error
: change
= isl_change_error
;
1948 isl_set_free(set_i
);
1949 isl_set_free(set_j
);
1950 isl_vec_free(bound
);
1955 /* Initialize the "eq" and "ineq" fields of "info".
1957 static void init_status(struct isl_coalesce_info
*info
)
1959 info
->eq
= info
->ineq
= NULL
;
1962 /* Set info->eq to the positions of the equalities of info->bmap
1963 * with respect to the basic map represented by "tab".
1964 * If info->eq has already been computed, then do not compute it again.
1966 static void set_eq_status_in(struct isl_coalesce_info
*info
,
1967 struct isl_tab
*tab
)
1971 info
->eq
= eq_status_in(info
->bmap
, tab
);
1974 /* Set info->ineq to the positions of the inequalities of info->bmap
1975 * with respect to the basic map represented by "tab".
1976 * If info->ineq has already been computed, then do not compute it again.
1978 static void set_ineq_status_in(struct isl_coalesce_info
*info
,
1979 struct isl_tab
*tab
)
1983 info
->ineq
= ineq_status_in(info
->bmap
, info
->tab
, tab
);
1986 /* Free the memory allocated by the "eq" and "ineq" fields of "info".
1987 * This function assumes that init_status has been called on "info" first,
1988 * after which the "eq" and "ineq" fields may or may not have been
1989 * assigned a newly allocated array.
1991 static void clear_status(struct isl_coalesce_info
*info
)
1997 /* Check if the union of the given pair of basic maps
1998 * can be represented by a single basic map.
1999 * If so, replace the pair by the single basic map and return
2000 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
2001 * Otherwise, return isl_change_none.
2002 * The two basic maps are assumed to live in the same local space.
2003 * The "eq" and "ineq" fields of info[i] and info[j] are assumed
2004 * to have been initialized by the caller, either to NULL or
2005 * to valid information.
2007 * We first check the effect of each constraint of one basic map
2008 * on the other basic map.
2009 * The constraint may be
2010 * redundant the constraint is redundant in its own
2011 * basic map and should be ignore and removed
2013 * valid all (integer) points of the other basic map
2014 * satisfy the constraint
2015 * separate no (integer) point of the other basic map
2016 * satisfies the constraint
2017 * cut some but not all points of the other basic map
2018 * satisfy the constraint
2019 * adj_eq the given constraint is adjacent (on the outside)
2020 * to an equality of the other basic map
2021 * adj_ineq the given constraint is adjacent (on the outside)
2022 * to an inequality of the other basic map
2024 * We consider seven cases in which we can replace the pair by a single
2025 * basic map. We ignore all "redundant" constraints.
2027 * 1. all constraints of one basic map are valid
2028 * => the other basic map is a subset and can be removed
2030 * 2. all constraints of both basic maps are either "valid" or "cut"
2031 * and the facets corresponding to the "cut" constraints
2032 * of one of the basic maps lies entirely inside the other basic map
2033 * => the pair can be replaced by a basic map consisting
2034 * of the valid constraints in both basic maps
2036 * 3. there is a single pair of adjacent inequalities
2037 * (all other constraints are "valid")
2038 * => the pair can be replaced by a basic map consisting
2039 * of the valid constraints in both basic maps
2041 * 4. one basic map has a single adjacent inequality, while the other
2042 * constraints are "valid". The other basic map has some
2043 * "cut" constraints, but replacing the adjacent inequality by
2044 * its opposite and adding the valid constraints of the other
2045 * basic map results in a subset of the other basic map
2046 * => the pair can be replaced by a basic map consisting
2047 * of the valid constraints in both basic maps
2049 * 5. there is a single adjacent pair of an inequality and an equality,
2050 * the other constraints of the basic map containing the inequality are
2051 * "valid". Moreover, if the inequality the basic map is relaxed
2052 * and then turned into an equality, then resulting facet lies
2053 * entirely inside the other basic map
2054 * => the pair can be replaced by the basic map containing
2055 * the inequality, with the inequality relaxed.
2057 * 6. there is a single adjacent pair of an inequality and an equality,
2058 * the other constraints of the basic map containing the inequality are
2059 * "valid". Moreover, the facets corresponding to both
2060 * the inequality and the equality can be wrapped around their
2061 * ridges to include the other basic map
2062 * => the pair can be replaced by a basic map consisting
2063 * of the valid constraints in both basic maps together
2064 * with all wrapping constraints
2066 * 7. one of the basic maps extends beyond the other by at most one.
2067 * Moreover, the facets corresponding to the cut constraints and
2068 * the pieces of the other basic map at offset one from these cut
2069 * constraints can be wrapped around their ridges to include
2070 * the union of the two basic maps
2071 * => the pair can be replaced by a basic map consisting
2072 * of the valid constraints in both basic maps together
2073 * with all wrapping constraints
2075 * 8. the two basic maps live in adjacent hyperplanes. In principle
2076 * such sets can always be combined through wrapping, but we impose
2077 * that there is only one such pair, to avoid overeager coalescing.
2079 * Throughout the computation, we maintain a collection of tableaus
2080 * corresponding to the basic maps. When the basic maps are dropped
2081 * or combined, the tableaus are modified accordingly.
2083 static enum isl_change
coalesce_local_pair_reuse(int i
, int j
,
2084 struct isl_coalesce_info
*info
)
2086 enum isl_change change
= isl_change_none
;
2088 set_eq_status_in(&info
[i
], info
[j
].tab
);
2089 if (info
[i
].bmap
->n_eq
&& !info
[i
].eq
)
2091 if (any(info
[i
].eq
, 2 * info
[i
].bmap
->n_eq
, STATUS_ERROR
))
2093 if (any(info
[i
].eq
, 2 * info
[i
].bmap
->n_eq
, STATUS_SEPARATE
))
2096 set_eq_status_in(&info
[j
], info
[i
].tab
);
2097 if (info
[j
].bmap
->n_eq
&& !info
[j
].eq
)
2099 if (any(info
[j
].eq
, 2 * info
[j
].bmap
->n_eq
, STATUS_ERROR
))
2101 if (any(info
[j
].eq
, 2 * info
[j
].bmap
->n_eq
, STATUS_SEPARATE
))
2104 set_ineq_status_in(&info
[i
], info
[j
].tab
);
2105 if (info
[i
].bmap
->n_ineq
&& !info
[i
].ineq
)
2107 if (any(info
[i
].ineq
, info
[i
].bmap
->n_ineq
, STATUS_ERROR
))
2109 if (any(info
[i
].ineq
, info
[i
].bmap
->n_ineq
, STATUS_SEPARATE
))
2112 set_ineq_status_in(&info
[j
], info
[i
].tab
);
2113 if (info
[j
].bmap
->n_ineq
&& !info
[j
].ineq
)
2115 if (any(info
[j
].ineq
, info
[j
].bmap
->n_ineq
, STATUS_ERROR
))
2117 if (any(info
[j
].ineq
, info
[j
].bmap
->n_ineq
, STATUS_SEPARATE
))
2120 if (all(info
[i
].eq
, 2 * info
[i
].bmap
->n_eq
, STATUS_VALID
) &&
2121 all(info
[i
].ineq
, info
[i
].bmap
->n_ineq
, STATUS_VALID
)) {
2123 change
= isl_change_drop_second
;
2124 } else if (all(info
[j
].eq
, 2 * info
[j
].bmap
->n_eq
, STATUS_VALID
) &&
2125 all(info
[j
].ineq
, info
[j
].bmap
->n_ineq
, STATUS_VALID
)) {
2127 change
= isl_change_drop_first
;
2128 } else if (any(info
[i
].eq
, 2 * info
[i
].bmap
->n_eq
, STATUS_ADJ_EQ
)) {
2129 change
= check_eq_adj_eq(i
, j
, info
);
2130 } else if (any(info
[j
].eq
, 2 * info
[j
].bmap
->n_eq
, STATUS_ADJ_EQ
)) {
2131 change
= check_eq_adj_eq(j
, i
, info
);
2132 } else if (any(info
[i
].eq
, 2 * info
[i
].bmap
->n_eq
, STATUS_ADJ_INEQ
) ||
2133 any(info
[j
].eq
, 2 * info
[j
].bmap
->n_eq
, STATUS_ADJ_INEQ
)) {
2134 change
= check_adj_eq(i
, j
, info
);
2135 } else if (any(info
[i
].ineq
, info
[i
].bmap
->n_ineq
, STATUS_ADJ_EQ
) ||
2136 any(info
[j
].ineq
, info
[j
].bmap
->n_ineq
, STATUS_ADJ_EQ
)) {
2139 } else if (any(info
[i
].ineq
, info
[i
].bmap
->n_ineq
, STATUS_ADJ_INEQ
) ||
2140 any(info
[j
].ineq
, info
[j
].bmap
->n_ineq
, STATUS_ADJ_INEQ
)) {
2141 change
= check_adj_ineq(i
, j
, info
);
2143 if (!any(info
[i
].eq
, 2 * info
[i
].bmap
->n_eq
, STATUS_CUT
) &&
2144 !any(info
[j
].eq
, 2 * info
[j
].bmap
->n_eq
, STATUS_CUT
))
2145 change
= check_facets(i
, j
, info
);
2146 if (change
== isl_change_none
)
2147 change
= check_wrap(i
, j
, info
);
2151 clear_status(&info
[i
]);
2152 clear_status(&info
[j
]);
2155 clear_status(&info
[i
]);
2156 clear_status(&info
[j
]);
2157 return isl_change_error
;
2160 /* Check if the union of the given pair of basic maps
2161 * can be represented by a single basic map.
2162 * If so, replace the pair by the single basic map and return
2163 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
2164 * Otherwise, return isl_change_none.
2165 * The two basic maps are assumed to live in the same local space.
2167 static enum isl_change
coalesce_local_pair(int i
, int j
,
2168 struct isl_coalesce_info
*info
)
2170 init_status(&info
[i
]);
2171 init_status(&info
[j
]);
2172 return coalesce_local_pair_reuse(i
, j
, info
);
2175 /* Shift the integer division at position "div" of the basic map
2176 * represented by "info" by "shift".
2178 * That is, if the integer division has the form
2182 * then replace it by
2184 * floor((f(x) + shift * d)/d) - shift
2186 static int shift_div(struct isl_coalesce_info
*info
, int div
, isl_int shift
)
2190 info
->bmap
= isl_basic_map_shift_div(info
->bmap
, div
, 0, shift
);
2194 total
= isl_basic_map_dim(info
->bmap
, isl_dim_all
);
2195 total
-= isl_basic_map_dim(info
->bmap
, isl_dim_div
);
2196 if (isl_tab_shift_var(info
->tab
, total
+ div
, shift
) < 0)
2202 /* Check if some of the divs in the basic map represented by "info1"
2203 * are shifts of the corresponding divs in the basic map represented
2204 * by "info2". If so, align them with those of "info2".
2205 * Only do this if "info1" and "info2" have the same number
2206 * of integer divisions.
2208 * An integer division is considered to be a shift of another integer
2209 * division if one is equal to the other plus a constant.
2211 * In particular, for each pair of integer divisions, if both are known,
2212 * have identical coefficients (apart from the constant term) and
2213 * if the difference between the constant terms (taking into account
2214 * the denominator) is an integer, then move the difference outside.
2215 * That is, if one integer division is of the form
2217 * floor((f(x) + c_1)/d)
2219 * while the other is of the form
2221 * floor((f(x) + c_2)/d)
2223 * and n = (c_2 - c_1)/d is an integer, then replace the first
2224 * integer division by
2226 * floor((f(x) + c_1 + n * d)/d) - n = floor((f(x) + c_2)/d) - n
2228 static int harmonize_divs(struct isl_coalesce_info
*info1
,
2229 struct isl_coalesce_info
*info2
)
2234 if (!info1
->bmap
|| !info2
->bmap
)
2237 if (info1
->bmap
->n_div
!= info2
->bmap
->n_div
)
2239 if (info1
->bmap
->n_div
== 0)
2242 total
= isl_basic_map_total_dim(info1
->bmap
);
2243 for (i
= 0; i
< info1
->bmap
->n_div
; ++i
) {
2247 if (isl_int_is_zero(info1
->bmap
->div
[i
][0]) ||
2248 isl_int_is_zero(info2
->bmap
->div
[i
][0]))
2250 if (isl_int_ne(info1
->bmap
->div
[i
][0], info2
->bmap
->div
[i
][0]))
2252 if (isl_int_eq(info1
->bmap
->div
[i
][1], info2
->bmap
->div
[i
][1]))
2254 if (!isl_seq_eq(info1
->bmap
->div
[i
] + 2,
2255 info2
->bmap
->div
[i
] + 2, total
))
2258 isl_int_sub(d
, info2
->bmap
->div
[i
][1], info1
->bmap
->div
[i
][1]);
2259 if (isl_int_is_divisible_by(d
, info1
->bmap
->div
[i
][0])) {
2260 isl_int_divexact(d
, d
, info1
->bmap
->div
[i
][0]);
2261 r
= shift_div(info1
, i
, d
);
2271 /* Do the two basic maps live in the same local space, i.e.,
2272 * do they have the same (known) divs?
2273 * If either basic map has any unknown divs, then we can only assume
2274 * that they do not live in the same local space.
2276 static int same_divs(__isl_keep isl_basic_map
*bmap1
,
2277 __isl_keep isl_basic_map
*bmap2
)
2283 if (!bmap1
|| !bmap2
)
2285 if (bmap1
->n_div
!= bmap2
->n_div
)
2288 if (bmap1
->n_div
== 0)
2291 known
= isl_basic_map_divs_known(bmap1
);
2292 if (known
< 0 || !known
)
2294 known
= isl_basic_map_divs_known(bmap2
);
2295 if (known
< 0 || !known
)
2298 total
= isl_basic_map_total_dim(bmap1
);
2299 for (i
= 0; i
< bmap1
->n_div
; ++i
)
2300 if (!isl_seq_eq(bmap1
->div
[i
], bmap2
->div
[i
], 2 + total
))
2306 /* Assuming that "tab" contains the equality constraints and
2307 * the initial inequality constraints of "bmap", copy the remaining
2308 * inequality constraints of "bmap" to "Tab".
2310 static isl_stat
copy_ineq(struct isl_tab
*tab
, __isl_keep isl_basic_map
*bmap
)
2315 return isl_stat_error
;
2317 n_ineq
= tab
->n_con
- tab
->n_eq
;
2318 for (i
= n_ineq
; i
< bmap
->n_ineq
; ++i
)
2319 if (isl_tab_add_ineq(tab
, bmap
->ineq
[i
]) < 0)
2320 return isl_stat_error
;
2325 /* Description of an integer division that is added
2326 * during an expansion.
2327 * "pos" is the position of the corresponding variable.
2328 * "cst" indicates whether this integer division has a fixed value.
2329 * "val" contains the fixed value, if the value is fixed.
2331 struct isl_expanded
{
2337 /* For each of the "n" integer division variables "expanded",
2338 * if the variable has a fixed value, then add two inequality
2339 * constraints expressing the fixed value.
2340 * Otherwise, add the corresponding div constraints.
2341 * The caller is responsible for removing the div constraints
2342 * that it added for all these "n" integer divisions.
2344 * The div constraints and the pair of inequality constraints
2345 * forcing the fixed value cannot both be added for a given variable
2346 * as the combination may render some of the original constraints redundant.
2347 * These would then be ignored during the coalescing detection,
2348 * while they could remain in the fused result.
2350 * The two added inequality constraints are
2355 * with "a" the variable and "v" its fixed value.
2356 * The facet corresponding to one of these two constraints is selected
2357 * in the tableau to ensure that the pair of inequality constraints
2358 * is treated as an equality constraint.
2360 * The information in info->ineq is thrown away because it was
2361 * computed in terms of div constraints, while some of those
2362 * have now been replaced by these pairs of inequality constraints.
2364 static isl_stat
fix_constant_divs(struct isl_coalesce_info
*info
,
2365 int n
, struct isl_expanded
*expanded
)
2371 o_div
= isl_basic_map_offset(info
->bmap
, isl_dim_div
) - 1;
2372 ineq
= isl_vec_alloc(isl_tab_get_ctx(info
->tab
), 1 + info
->tab
->n_var
);
2374 return isl_stat_error
;
2375 isl_seq_clr(ineq
->el
+ 1, info
->tab
->n_var
);
2377 for (i
= 0; i
< n
; ++i
) {
2378 if (!expanded
[i
].cst
) {
2379 info
->bmap
= isl_basic_map_extend_constraints(
2381 if (isl_basic_map_add_div_constraints(info
->bmap
,
2382 expanded
[i
].pos
- o_div
) < 0)
2385 isl_int_set_si(ineq
->el
[1 + expanded
[i
].pos
], -1);
2386 isl_int_set(ineq
->el
[0], expanded
[i
].val
);
2387 info
->bmap
= isl_basic_map_add_ineq(info
->bmap
,
2389 isl_int_set_si(ineq
->el
[1 + expanded
[i
].pos
], 1);
2390 isl_int_neg(ineq
->el
[0], expanded
[i
].val
);
2391 info
->bmap
= isl_basic_map_add_ineq(info
->bmap
,
2393 isl_int_set_si(ineq
->el
[1 + expanded
[i
].pos
], 0);
2395 if (copy_ineq(info
->tab
, info
->bmap
) < 0)
2397 if (expanded
[i
].cst
&&
2398 isl_tab_select_facet(info
->tab
, info
->tab
->n_con
- 1) < 0)
2407 return i
< n
? isl_stat_error
: isl_stat_ok
;
2410 /* Insert the "n" integer division variables "expanded"
2411 * into info->tab and info->bmap and
2412 * update info->ineq with respect to the redundant constraints
2413 * in the resulting tableau.
2414 * "bmap" contains the result of this insertion in info->bmap,
2415 * while info->bmap is the original version
2416 * of "bmap", i.e., the one that corresponds to the current
2417 * state of info->tab. The number of constraints in info->bmap
2418 * is assumed to be the same as the number of constraints
2419 * in info->tab. This is required to be able to detect
2420 * the extra constraints in "bmap".
2422 * In particular, introduce extra variables corresponding
2423 * to the extra integer divisions and add the div constraints
2424 * that were added to "bmap" after info->tab was created
2426 * Furthermore, check if these extra integer divisions happen
2427 * to attain a fixed integer value in info->tab.
2428 * If so, replace the corresponding div constraints by pairs
2429 * of inequality constraints that fix these
2430 * integer divisions to their single integer values.
2431 * Replace info->bmap by "bmap" to match the changes to info->tab.
2432 * info->ineq was computed without a tableau and therefore
2433 * does not take into account the redundant constraints
2434 * in the tableau. Mark them here.
2435 * There is no need to check the newly added div constraints
2436 * since they cannot be redundant.
2437 * The redundancy check is not performed when constants have been discovered
2438 * since info->ineq is completely thrown away in this case.
2440 static isl_stat
tab_insert_divs(struct isl_coalesce_info
*info
,
2441 int n
, struct isl_expanded
*expanded
, __isl_take isl_basic_map
*bmap
)
2445 struct isl_tab_undo
*snap
;
2449 return isl_stat_error
;
2450 if (info
->bmap
->n_eq
+ info
->bmap
->n_ineq
!= info
->tab
->n_con
)
2451 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_internal
,
2452 "original tableau does not correspond "
2453 "to original basic map", goto error
);
2455 if (isl_tab_extend_vars(info
->tab
, n
) < 0)
2457 if (isl_tab_extend_cons(info
->tab
, 2 * n
) < 0)
2460 for (i
= 0; i
< n
; ++i
) {
2461 if (isl_tab_insert_var(info
->tab
, expanded
[i
].pos
) < 0)
2465 snap
= isl_tab_snap(info
->tab
);
2467 n_ineq
= info
->tab
->n_con
- info
->tab
->n_eq
;
2468 if (copy_ineq(info
->tab
, bmap
) < 0)
2471 isl_basic_map_free(info
->bmap
);
2475 for (i
= 0; i
< n
; ++i
) {
2476 expanded
[i
].cst
= isl_tab_is_constant(info
->tab
,
2477 expanded
[i
].pos
, &expanded
[i
].val
);
2478 if (expanded
[i
].cst
< 0)
2479 return isl_stat_error
;
2480 if (expanded
[i
].cst
)
2485 if (isl_tab_rollback(info
->tab
, snap
) < 0)
2486 return isl_stat_error
;
2487 info
->bmap
= isl_basic_map_cow(info
->bmap
);
2488 if (isl_basic_map_free_inequality(info
->bmap
, 2 * n
) < 0)
2489 return isl_stat_error
;
2491 return fix_constant_divs(info
, n
, expanded
);
2494 n_eq
= info
->bmap
->n_eq
;
2495 for (i
= 0; i
< n_ineq
; ++i
) {
2496 if (isl_tab_is_redundant(info
->tab
, n_eq
+ i
))
2497 info
->ineq
[i
] = STATUS_REDUNDANT
;
2502 isl_basic_map_free(bmap
);
2503 return isl_stat_error
;
2506 /* Expand info->tab and info->bmap in the same way "bmap" was expanded
2507 * in isl_basic_map_expand_divs using the expansion "exp" and
2508 * update info->ineq with respect to the redundant constraints
2509 * in the resulting tableau. info->bmap is the original version
2510 * of "bmap", i.e., the one that corresponds to the current
2511 * state of info->tab. The number of constraints in info->bmap
2512 * is assumed to be the same as the number of constraints
2513 * in info->tab. This is required to be able to detect
2514 * the extra constraints in "bmap".
2516 * Extract the positions where extra local variables are introduced
2517 * from "exp" and call tab_insert_divs.
2519 static isl_stat
expand_tab(struct isl_coalesce_info
*info
, int *exp
,
2520 __isl_take isl_basic_map
*bmap
)
2523 struct isl_expanded
*expanded
;
2526 unsigned total
, pos
, n_div
;
2529 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
2530 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
2531 pos
= total
- n_div
;
2532 extra_var
= total
- info
->tab
->n_var
;
2533 n
= n_div
- extra_var
;
2535 ctx
= isl_basic_map_get_ctx(bmap
);
2536 expanded
= isl_calloc_array(ctx
, struct isl_expanded
, extra_var
);
2537 if (extra_var
&& !expanded
)
2542 for (j
= 0; j
< n_div
; ++j
) {
2543 if (i
< n
&& exp
[i
] == j
) {
2547 expanded
[k
++].pos
= pos
+ j
;
2550 for (k
= 0; k
< extra_var
; ++k
)
2551 isl_int_init(expanded
[k
].val
);
2553 r
= tab_insert_divs(info
, extra_var
, expanded
, bmap
);
2555 for (k
= 0; k
< extra_var
; ++k
)
2556 isl_int_clear(expanded
[k
].val
);
2561 isl_basic_map_free(bmap
);
2562 return isl_stat_error
;
2565 /* Check if the union of the basic maps represented by info[i] and info[j]
2566 * can be represented by a single basic map,
2567 * after expanding the divs of info[i] to match those of info[j].
2568 * If so, replace the pair by the single basic map and return
2569 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
2570 * Otherwise, return isl_change_none.
2572 * The caller has already checked for info[j] being a subset of info[i].
2573 * If some of the divs of info[j] are unknown, then the expanded info[i]
2574 * will not have the corresponding div constraints. The other patterns
2575 * therefore cannot apply. Skip the computation in this case.
2577 * The expansion is performed using the divs "div" and expansion "exp"
2578 * computed by the caller.
2579 * info[i].bmap has already been expanded and the result is passed in
2581 * The "eq" and "ineq" fields of info[i] reflect the status of
2582 * the constraints of the expanded "bmap" with respect to info[j].tab.
2583 * However, inequality constraints that are redundant in info[i].tab
2584 * have not yet been marked as such because no tableau was available.
2586 * Replace info[i].bmap by "bmap" and expand info[i].tab as well,
2587 * updating info[i].ineq with respect to the redundant constraints.
2588 * Then try and coalesce the expanded info[i] with info[j],
2589 * reusing the information in info[i].eq and info[i].ineq.
2590 * If this does not result in any coalescing or if it results in info[j]
2591 * getting dropped (which should not happen in practice, since the case
2592 * of info[j] being a subset of info[i] has already been checked by
2593 * the caller), then revert info[i] to its original state.
2595 static enum isl_change
coalesce_expand_tab_divs(__isl_take isl_basic_map
*bmap
,
2596 int i
, int j
, struct isl_coalesce_info
*info
, __isl_keep isl_mat
*div
,
2600 isl_basic_map
*bmap_i
;
2601 struct isl_tab_undo
*snap
;
2602 enum isl_change change
= isl_change_none
;
2604 known
= isl_basic_map_divs_known(info
[j
].bmap
);
2605 if (known
< 0 || !known
) {
2606 clear_status(&info
[i
]);
2607 isl_basic_map_free(bmap
);
2608 return known
< 0 ? isl_change_error
: isl_change_none
;
2611 bmap_i
= isl_basic_map_copy(info
[i
].bmap
);
2612 snap
= isl_tab_snap(info
[i
].tab
);
2613 if (expand_tab(&info
[i
], exp
, bmap
) < 0)
2614 change
= isl_change_error
;
2616 init_status(&info
[j
]);
2617 if (change
== isl_change_none
)
2618 change
= coalesce_local_pair_reuse(i
, j
, info
);
2620 clear_status(&info
[i
]);
2621 if (change
!= isl_change_none
&& change
!= isl_change_drop_second
) {
2622 isl_basic_map_free(bmap_i
);
2624 isl_basic_map_free(info
[i
].bmap
);
2625 info
[i
].bmap
= bmap_i
;
2627 if (isl_tab_rollback(info
[i
].tab
, snap
) < 0)
2628 change
= isl_change_error
;
2634 /* Check if the union of "bmap" and the basic map represented by info[j]
2635 * can be represented by a single basic map,
2636 * after expanding the divs of "bmap" to match those of info[j].
2637 * If so, replace the pair by the single basic map and return
2638 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
2639 * Otherwise, return isl_change_none.
2641 * In particular, check if the expanded "bmap" contains the basic map
2642 * represented by the tableau info[j].tab.
2643 * The expansion is performed using the divs "div" and expansion "exp"
2644 * computed by the caller.
2645 * Then we check if all constraints of the expanded "bmap" are valid for
2648 * If "i" is not equal to -1, then "bmap" is equal to info[i].bmap.
2649 * In this case, the positions of the constraints of info[i].bmap
2650 * with respect to the basic map represented by info[j] are stored
2653 * If the expanded "bmap" does not contain the basic map
2654 * represented by the tableau info[j].tab and if "i" is not -1,
2655 * i.e., if the original "bmap" is info[i].bmap, then expand info[i].tab
2656 * as well and check if that results in coalescing.
2658 static enum isl_change
coalesce_with_expanded_divs(
2659 __isl_keep isl_basic_map
*bmap
, int i
, int j
,
2660 struct isl_coalesce_info
*info
, __isl_keep isl_mat
*div
, int *exp
)
2662 enum isl_change change
= isl_change_none
;
2663 struct isl_coalesce_info info_local
, *info_i
;
2665 info_i
= i
>= 0 ? &info
[i
] : &info_local
;
2666 init_status(info_i
);
2667 bmap
= isl_basic_map_copy(bmap
);
2668 bmap
= isl_basic_map_expand_divs(bmap
, isl_mat_copy(div
), exp
);
2669 bmap
= isl_basic_map_mark_final(bmap
);
2674 info_i
->eq
= eq_status_in(bmap
, info
[j
].tab
);
2675 if (bmap
->n_eq
&& !info_i
->eq
)
2677 if (any(info_i
->eq
, 2 * bmap
->n_eq
, STATUS_ERROR
))
2679 if (any(info_i
->eq
, 2 * bmap
->n_eq
, STATUS_SEPARATE
))
2682 info_i
->ineq
= ineq_status_in(bmap
, NULL
, info
[j
].tab
);
2683 if (bmap
->n_ineq
&& !info_i
->ineq
)
2685 if (any(info_i
->ineq
, bmap
->n_ineq
, STATUS_ERROR
))
2687 if (any(info_i
->ineq
, bmap
->n_ineq
, STATUS_SEPARATE
))
2690 if (all(info_i
->eq
, 2 * bmap
->n_eq
, STATUS_VALID
) &&
2691 all(info_i
->ineq
, bmap
->n_ineq
, STATUS_VALID
)) {
2693 change
= isl_change_drop_second
;
2696 if (change
== isl_change_none
&& i
!= -1)
2697 return coalesce_expand_tab_divs(bmap
, i
, j
, info
, div
, exp
);
2700 isl_basic_map_free(bmap
);
2701 clear_status(info_i
);
2704 isl_basic_map_free(bmap
);
2705 clear_status(info_i
);
2706 return isl_change_error
;
2709 /* Check if the union of "bmap_i" and the basic map represented by info[j]
2710 * can be represented by a single basic map,
2711 * after aligning the divs of "bmap_i" to match those of info[j].
2712 * If so, replace the pair by the single basic map and return
2713 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
2714 * Otherwise, return isl_change_none.
2716 * In particular, check if "bmap_i" contains the basic map represented by
2717 * info[j] after aligning the divs of "bmap_i" to those of info[j].
2718 * Note that this can only succeed if the number of divs of "bmap_i"
2719 * is smaller than (or equal to) the number of divs of info[j].
2721 * We first check if the divs of "bmap_i" are all known and form a subset
2722 * of those of info[j].bmap. If so, we pass control over to
2723 * coalesce_with_expanded_divs.
2725 * If "i" is not equal to -1, then "bmap" is equal to info[i].bmap.
2727 static enum isl_change
coalesce_after_aligning_divs(
2728 __isl_keep isl_basic_map
*bmap_i
, int i
, int j
,
2729 struct isl_coalesce_info
*info
)
2732 isl_mat
*div_i
, *div_j
, *div
;
2736 enum isl_change change
;
2738 known
= isl_basic_map_divs_known(bmap_i
);
2739 if (known
< 0 || !known
)
2742 ctx
= isl_basic_map_get_ctx(bmap_i
);
2744 div_i
= isl_basic_map_get_divs(bmap_i
);
2745 div_j
= isl_basic_map_get_divs(info
[j
].bmap
);
2747 if (!div_i
|| !div_j
)
2750 exp1
= isl_alloc_array(ctx
, int, div_i
->n_row
);
2751 exp2
= isl_alloc_array(ctx
, int, div_j
->n_row
);
2752 if ((div_i
->n_row
&& !exp1
) || (div_j
->n_row
&& !exp2
))
2755 div
= isl_merge_divs(div_i
, div_j
, exp1
, exp2
);
2759 if (div
->n_row
== div_j
->n_row
)
2760 change
= coalesce_with_expanded_divs(bmap_i
,
2761 i
, j
, info
, div
, exp1
);
2763 change
= isl_change_none
;
2767 isl_mat_free(div_i
);
2768 isl_mat_free(div_j
);
2775 isl_mat_free(div_i
);
2776 isl_mat_free(div_j
);
2779 return isl_change_error
;
2782 /* Check if basic map "j" is a subset of basic map "i" after
2783 * exploiting the extra equalities of "j" to simplify the divs of "i".
2784 * If so, remove basic map "j" and return isl_change_drop_second.
2786 * If "j" does not have any equalities or if they are the same
2787 * as those of "i", then we cannot exploit them to simplify the divs.
2788 * Similarly, if there are no divs in "i", then they cannot be simplified.
2789 * If, on the other hand, the affine hulls of "i" and "j" do not intersect,
2790 * then "j" cannot be a subset of "i".
2792 * Otherwise, we intersect "i" with the affine hull of "j" and then
2793 * check if "j" is a subset of the result after aligning the divs.
2794 * If so, then "j" is definitely a subset of "i" and can be removed.
2795 * Note that if after intersection with the affine hull of "j".
2796 * "i" still has more divs than "j", then there is no way we can
2797 * align the divs of "i" to those of "j".
2799 static enum isl_change
coalesce_subset_with_equalities(int i
, int j
,
2800 struct isl_coalesce_info
*info
)
2802 isl_basic_map
*hull_i
, *hull_j
, *bmap_i
;
2804 enum isl_change change
;
2806 if (info
[j
].bmap
->n_eq
== 0)
2807 return isl_change_none
;
2808 if (info
[i
].bmap
->n_div
== 0)
2809 return isl_change_none
;
2811 hull_i
= isl_basic_map_copy(info
[i
].bmap
);
2812 hull_i
= isl_basic_map_plain_affine_hull(hull_i
);
2813 hull_j
= isl_basic_map_copy(info
[j
].bmap
);
2814 hull_j
= isl_basic_map_plain_affine_hull(hull_j
);
2816 hull_j
= isl_basic_map_intersect(hull_j
, isl_basic_map_copy(hull_i
));
2817 equal
= isl_basic_map_plain_is_equal(hull_i
, hull_j
);
2818 empty
= isl_basic_map_plain_is_empty(hull_j
);
2819 isl_basic_map_free(hull_i
);
2821 if (equal
< 0 || equal
|| empty
< 0 || empty
) {
2822 isl_basic_map_free(hull_j
);
2823 if (equal
< 0 || empty
< 0)
2824 return isl_change_error
;
2825 return isl_change_none
;
2828 bmap_i
= isl_basic_map_copy(info
[i
].bmap
);
2829 bmap_i
= isl_basic_map_intersect(bmap_i
, hull_j
);
2831 return isl_change_error
;
2833 if (bmap_i
->n_div
> info
[j
].bmap
->n_div
) {
2834 isl_basic_map_free(bmap_i
);
2835 return isl_change_none
;
2838 change
= coalesce_after_aligning_divs(bmap_i
, -1, j
, info
);
2840 isl_basic_map_free(bmap_i
);
2845 /* Check if the union of and the basic maps represented by info[i] and info[j]
2846 * can be represented by a single basic map, by aligning or equating
2847 * their integer divisions.
2848 * If so, replace the pair by the single basic map and return
2849 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
2850 * Otherwise, return isl_change_none.
2852 * Note that we only perform any test if the number of divs is different
2853 * in the two basic maps. In case the number of divs is the same,
2854 * we have already established that the divs are different
2855 * in the two basic maps.
2856 * In particular, if the number of divs of basic map i is smaller than
2857 * the number of divs of basic map j, then we check if j is a subset of i
2860 static enum isl_change
coalesce_divs(int i
, int j
,
2861 struct isl_coalesce_info
*info
)
2863 enum isl_change change
= isl_change_none
;
2865 if (info
[i
].bmap
->n_div
< info
[j
].bmap
->n_div
)
2866 change
= coalesce_after_aligning_divs(info
[i
].bmap
, i
, j
, info
);
2867 if (change
!= isl_change_none
)
2870 if (info
[j
].bmap
->n_div
< info
[i
].bmap
->n_div
)
2871 change
= coalesce_after_aligning_divs(info
[j
].bmap
, j
, i
, info
);
2872 if (change
!= isl_change_none
)
2873 return invert_change(change
);
2875 change
= coalesce_subset_with_equalities(i
, j
, info
);
2876 if (change
!= isl_change_none
)
2879 change
= coalesce_subset_with_equalities(j
, i
, info
);
2880 if (change
!= isl_change_none
)
2881 return invert_change(change
);
2883 return isl_change_none
;
2886 /* Does "bmap" involve any divs that themselves refer to divs?
2888 static int has_nested_div(__isl_keep isl_basic_map
*bmap
)
2894 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
2895 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
2898 for (i
= 0; i
< n_div
; ++i
)
2899 if (isl_seq_first_non_zero(bmap
->div
[i
] + 2 + total
,
2906 /* Return a list of affine expressions, one for each integer division
2907 * in "bmap_i". For each integer division that also appears in "bmap_j",
2908 * the affine expression is set to NaN. The number of NaNs in the list
2909 * is equal to the number of integer divisions in "bmap_j".
2910 * For the other integer divisions of "bmap_i", the corresponding
2911 * element in the list is a purely affine expression equal to the integer
2912 * division in "hull".
2913 * If no such list can be constructed, then the number of elements
2914 * in the returned list is smaller than the number of integer divisions
2917 static __isl_give isl_aff_list
*set_up_substitutions(
2918 __isl_keep isl_basic_map
*bmap_i
, __isl_keep isl_basic_map
*bmap_j
,
2919 __isl_take isl_basic_map
*hull
)
2921 unsigned n_div_i
, n_div_j
, total
;
2923 isl_local_space
*ls
;
2924 isl_basic_set
*wrap_hull
;
2932 ctx
= isl_basic_map_get_ctx(hull
);
2934 n_div_i
= isl_basic_map_dim(bmap_i
, isl_dim_div
);
2935 n_div_j
= isl_basic_map_dim(bmap_j
, isl_dim_div
);
2936 total
= isl_basic_map_total_dim(bmap_i
) - n_div_i
;
2938 ls
= isl_basic_map_get_local_space(bmap_i
);
2939 ls
= isl_local_space_wrap(ls
);
2940 wrap_hull
= isl_basic_map_wrap(hull
);
2942 aff_nan
= isl_aff_nan_on_domain(isl_local_space_copy(ls
));
2943 list
= isl_aff_list_alloc(ctx
, n_div_i
);
2946 for (i
= 0; i
< n_div_i
; ++i
) {
2950 isl_seq_eq(bmap_i
->div
[i
], bmap_j
->div
[j
], 2 + total
)) {
2952 list
= isl_aff_list_add(list
, isl_aff_copy(aff_nan
));
2955 if (n_div_i
- i
<= n_div_j
- j
)
2958 aff
= isl_local_space_get_div(ls
, i
);
2959 aff
= isl_aff_substitute_equalities(aff
,
2960 isl_basic_set_copy(wrap_hull
));
2961 aff
= isl_aff_floor(aff
);
2964 if (isl_aff_dim(aff
, isl_dim_div
) != 0) {
2969 list
= isl_aff_list_add(list
, aff
);
2972 isl_aff_free(aff_nan
);
2973 isl_local_space_free(ls
);
2974 isl_basic_set_free(wrap_hull
);
2978 isl_aff_free(aff_nan
);
2979 isl_local_space_free(ls
);
2980 isl_basic_set_free(wrap_hull
);
2981 isl_aff_list_free(list
);
2985 /* Add variables to info->bmap and info->tab corresponding to the elements
2986 * in "list" that are not set to NaN.
2987 * "extra_var" is the number of these elements.
2988 * "dim" is the offset in the variables of "tab" where we should
2989 * start considering the elements in "list".
2990 * When this function returns, the total number of variables in "tab"
2991 * is equal to "dim" plus the number of elements in "list".
2993 * The newly added existentially quantified variables are not given
2994 * an explicit representation because the corresponding div constraints
2995 * do not appear in info->bmap. These constraints are not added
2996 * to info->bmap because for internal consistency, they would need to
2997 * be added to info->tab as well, where they could combine with the equality
2998 * that is added later to result in constraints that do not hold
2999 * in the original input.
3001 static int add_sub_vars(struct isl_coalesce_info
*info
,
3002 __isl_keep isl_aff_list
*list
, int dim
, int extra_var
)
3007 space
= isl_basic_map_get_space(info
->bmap
);
3008 info
->bmap
= isl_basic_map_cow(info
->bmap
);
3009 info
->bmap
= isl_basic_map_extend_space(info
->bmap
, space
,
3013 n
= isl_aff_list_n_aff(list
);
3014 for (i
= 0; i
< n
; ++i
) {
3018 aff
= isl_aff_list_get_aff(list
, i
);
3019 is_nan
= isl_aff_is_nan(aff
);
3026 if (isl_tab_insert_var(info
->tab
, dim
+ i
) < 0)
3028 d
= isl_basic_map_alloc_div(info
->bmap
);
3031 info
->bmap
= isl_basic_map_mark_div_unknown(info
->bmap
, d
);
3034 for (j
= d
; j
> i
; --j
)
3035 isl_basic_map_swap_div(info
->bmap
, j
- 1, j
);
3041 /* For each element in "list" that is not set to NaN, fix the corresponding
3042 * variable in "tab" to the purely affine expression defined by the element.
3043 * "dim" is the offset in the variables of "tab" where we should
3044 * start considering the elements in "list".
3046 * This function assumes that a sufficient number of rows and
3047 * elements in the constraint array are available in the tableau.
3049 static int add_sub_equalities(struct isl_tab
*tab
,
3050 __isl_keep isl_aff_list
*list
, int dim
)
3057 n
= isl_aff_list_n_aff(list
);
3059 ctx
= isl_tab_get_ctx(tab
);
3060 sub
= isl_vec_alloc(ctx
, 1 + dim
+ n
);
3063 isl_seq_clr(sub
->el
+ 1 + dim
, n
);
3065 for (i
= 0; i
< n
; ++i
) {
3066 aff
= isl_aff_list_get_aff(list
, i
);
3069 if (isl_aff_is_nan(aff
)) {
3073 isl_seq_cpy(sub
->el
, aff
->v
->el
+ 1, 1 + dim
);
3074 isl_int_neg(sub
->el
[1 + dim
+ i
], aff
->v
->el
[0]);
3075 if (isl_tab_add_eq(tab
, sub
->el
) < 0)
3077 isl_int_set_si(sub
->el
[1 + dim
+ i
], 0);
3089 /* Add variables to info->tab and info->bmap corresponding to the elements
3090 * in "list" that are not set to NaN. The value of the added variable
3091 * in info->tab is fixed to the purely affine expression defined by the element.
3092 * "dim" is the offset in the variables of info->tab where we should
3093 * start considering the elements in "list".
3094 * When this function returns, the total number of variables in info->tab
3095 * is equal to "dim" plus the number of elements in "list".
3097 static int add_subs(struct isl_coalesce_info
*info
,
3098 __isl_keep isl_aff_list
*list
, int dim
)
3106 n
= isl_aff_list_n_aff(list
);
3107 extra_var
= n
- (info
->tab
->n_var
- dim
);
3109 if (isl_tab_extend_vars(info
->tab
, extra_var
) < 0)
3111 if (isl_tab_extend_cons(info
->tab
, 2 * extra_var
) < 0)
3113 if (add_sub_vars(info
, list
, dim
, extra_var
) < 0)
3116 return add_sub_equalities(info
->tab
, list
, dim
);
3119 /* Coalesce basic map "j" into basic map "i" after adding the extra integer
3120 * divisions in "i" but not in "j" to basic map "j", with values
3121 * specified by "list". The total number of elements in "list"
3122 * is equal to the number of integer divisions in "i", while the number
3123 * of NaN elements in the list is equal to the number of integer divisions
3126 * If no coalescing can be performed, then we need to revert basic map "j"
3127 * to its original state. We do the same if basic map "i" gets dropped
3128 * during the coalescing, even though this should not happen in practice
3129 * since we have already checked for "j" being a subset of "i"
3130 * before we reach this stage.
3132 static enum isl_change
coalesce_with_subs(int i
, int j
,
3133 struct isl_coalesce_info
*info
, __isl_keep isl_aff_list
*list
)
3135 isl_basic_map
*bmap_j
;
3136 struct isl_tab_undo
*snap
;
3138 enum isl_change change
;
3140 bmap_j
= isl_basic_map_copy(info
[j
].bmap
);
3141 snap
= isl_tab_snap(info
[j
].tab
);
3143 dim
= isl_basic_map_dim(bmap_j
, isl_dim_all
);
3144 dim
-= isl_basic_map_dim(bmap_j
, isl_dim_div
);
3145 if (add_subs(&info
[j
], list
, dim
) < 0)
3148 change
= coalesce_local_pair(i
, j
, info
);
3149 if (change
!= isl_change_none
&& change
!= isl_change_drop_first
) {
3150 isl_basic_map_free(bmap_j
);
3152 isl_basic_map_free(info
[j
].bmap
);
3153 info
[j
].bmap
= bmap_j
;
3155 if (isl_tab_rollback(info
[j
].tab
, snap
) < 0)
3156 return isl_change_error
;
3161 isl_basic_map_free(bmap_j
);
3162 return isl_change_error
;
3165 /* Check if we can coalesce basic map "j" into basic map "i" after copying
3166 * those extra integer divisions in "i" that can be simplified away
3167 * using the extra equalities in "j".
3168 * All divs are assumed to be known and not contain any nested divs.
3170 * We first check if there are any extra equalities in "j" that we
3171 * can exploit. Then we check if every integer division in "i"
3172 * either already appears in "j" or can be simplified using the
3173 * extra equalities to a purely affine expression.
3174 * If these tests succeed, then we try to coalesce the two basic maps
3175 * by introducing extra dimensions in "j" corresponding to
3176 * the extra integer divsisions "i" fixed to the corresponding
3177 * purely affine expression.
3179 static enum isl_change
check_coalesce_into_eq(int i
, int j
,
3180 struct isl_coalesce_info
*info
)
3182 unsigned n_div_i
, n_div_j
;
3183 isl_basic_map
*hull_i
, *hull_j
;
3186 enum isl_change change
;
3188 n_div_i
= isl_basic_map_dim(info
[i
].bmap
, isl_dim_div
);
3189 n_div_j
= isl_basic_map_dim(info
[j
].bmap
, isl_dim_div
);
3190 if (n_div_i
<= n_div_j
)
3191 return isl_change_none
;
3192 if (info
[j
].bmap
->n_eq
== 0)
3193 return isl_change_none
;
3195 hull_i
= isl_basic_map_copy(info
[i
].bmap
);
3196 hull_i
= isl_basic_map_plain_affine_hull(hull_i
);
3197 hull_j
= isl_basic_map_copy(info
[j
].bmap
);
3198 hull_j
= isl_basic_map_plain_affine_hull(hull_j
);
3200 hull_j
= isl_basic_map_intersect(hull_j
, isl_basic_map_copy(hull_i
));
3201 equal
= isl_basic_map_plain_is_equal(hull_i
, hull_j
);
3202 empty
= isl_basic_map_plain_is_empty(hull_j
);
3203 isl_basic_map_free(hull_i
);
3205 if (equal
< 0 || empty
< 0)
3207 if (equal
|| empty
) {
3208 isl_basic_map_free(hull_j
);
3209 return isl_change_none
;
3212 list
= set_up_substitutions(info
[i
].bmap
, info
[j
].bmap
, hull_j
);
3214 return isl_change_error
;
3215 if (isl_aff_list_n_aff(list
) < n_div_i
)
3216 change
= isl_change_none
;
3218 change
= coalesce_with_subs(i
, j
, info
, list
);
3220 isl_aff_list_free(list
);
3224 isl_basic_map_free(hull_j
);
3225 return isl_change_error
;
3228 /* Check if we can coalesce basic maps "i" and "j" after copying
3229 * those extra integer divisions in one of the basic maps that can
3230 * be simplified away using the extra equalities in the other basic map.
3231 * We require all divs to be known in both basic maps.
3232 * Furthermore, to simplify the comparison of div expressions,
3233 * we do not allow any nested integer divisions.
3235 static enum isl_change
check_coalesce_eq(int i
, int j
,
3236 struct isl_coalesce_info
*info
)
3239 enum isl_change change
;
3241 known
= isl_basic_map_divs_known(info
[i
].bmap
);
3242 if (known
< 0 || !known
)
3243 return known
< 0 ? isl_change_error
: isl_change_none
;
3244 known
= isl_basic_map_divs_known(info
[j
].bmap
);
3245 if (known
< 0 || !known
)
3246 return known
< 0 ? isl_change_error
: isl_change_none
;
3247 nested
= has_nested_div(info
[i
].bmap
);
3248 if (nested
< 0 || nested
)
3249 return nested
< 0 ? isl_change_error
: isl_change_none
;
3250 nested
= has_nested_div(info
[j
].bmap
);
3251 if (nested
< 0 || nested
)
3252 return nested
< 0 ? isl_change_error
: isl_change_none
;
3254 change
= check_coalesce_into_eq(i
, j
, info
);
3255 if (change
!= isl_change_none
)
3257 change
= check_coalesce_into_eq(j
, i
, info
);
3258 if (change
!= isl_change_none
)
3259 return invert_change(change
);
3261 return isl_change_none
;
3264 /* Check if the union of the given pair of basic maps
3265 * can be represented by a single basic map.
3266 * If so, replace the pair by the single basic map and return
3267 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
3268 * Otherwise, return isl_change_none.
3270 * We first check if the two basic maps live in the same local space,
3271 * after aligning the divs that differ by only an integer constant.
3272 * If so, we do the complete check. Otherwise, we check if they have
3273 * the same number of integer divisions and can be coalesced, if one is
3274 * an obvious subset of the other or if the extra integer divisions
3275 * of one basic map can be simplified away using the extra equalities
3276 * of the other basic map.
3278 static enum isl_change
coalesce_pair(int i
, int j
,
3279 struct isl_coalesce_info
*info
)
3282 enum isl_change change
;
3284 if (harmonize_divs(&info
[i
], &info
[j
]) < 0)
3285 return isl_change_error
;
3286 same
= same_divs(info
[i
].bmap
, info
[j
].bmap
);
3288 return isl_change_error
;
3290 return coalesce_local_pair(i
, j
, info
);
3292 if (info
[i
].bmap
->n_div
== info
[j
].bmap
->n_div
) {
3293 change
= coalesce_local_pair(i
, j
, info
);
3294 if (change
!= isl_change_none
)
3298 change
= coalesce_divs(i
, j
, info
);
3299 if (change
!= isl_change_none
)
3302 return check_coalesce_eq(i
, j
, info
);
3305 /* Return the maximum of "a" and "b".
3307 static int isl_max(int a
, int b
)
3309 return a
> b
? a
: b
;
3312 /* Pairwise coalesce the basic maps in the range [start1, end1[ of "info"
3313 * with those in the range [start2, end2[, skipping basic maps
3314 * that have been removed (either before or within this function).
3316 * For each basic map i in the first range, we check if it can be coalesced
3317 * with respect to any previously considered basic map j in the second range.
3318 * If i gets dropped (because it was a subset of some j), then
3319 * we can move on to the next basic map.
3320 * If j gets dropped, we need to continue checking against the other
3321 * previously considered basic maps.
3322 * If the two basic maps got fused, then we recheck the fused basic map
3323 * against the previously considered basic maps, starting at i + 1
3324 * (even if start2 is greater than i + 1).
3326 static int coalesce_range(isl_ctx
*ctx
, struct isl_coalesce_info
*info
,
3327 int start1
, int end1
, int start2
, int end2
)
3331 for (i
= end1
- 1; i
>= start1
; --i
) {
3332 if (info
[i
].removed
)
3334 for (j
= isl_max(i
+ 1, start2
); j
< end2
; ++j
) {
3335 enum isl_change changed
;
3337 if (info
[j
].removed
)
3339 if (info
[i
].removed
)
3340 isl_die(ctx
, isl_error_internal
,
3341 "basic map unexpectedly removed",
3343 changed
= coalesce_pair(i
, j
, info
);
3345 case isl_change_error
:
3347 case isl_change_none
:
3348 case isl_change_drop_second
:
3350 case isl_change_drop_first
:
3353 case isl_change_fuse
:
3363 /* Pairwise coalesce the basic maps described by the "n" elements of "info".
3365 * We consider groups of basic maps that live in the same apparent
3366 * affine hull and we first coalesce within such a group before we
3367 * coalesce the elements in the group with elements of previously
3368 * considered groups. If a fuse happens during the second phase,
3369 * then we also reconsider the elements within the group.
3371 static int coalesce(isl_ctx
*ctx
, int n
, struct isl_coalesce_info
*info
)
3375 for (end
= n
; end
> 0; end
= start
) {
3377 while (start
>= 1 &&
3378 info
[start
- 1].hull_hash
== info
[start
].hull_hash
)
3380 if (coalesce_range(ctx
, info
, start
, end
, start
, end
) < 0)
3382 if (coalesce_range(ctx
, info
, start
, end
, end
, n
) < 0)
3389 /* Update the basic maps in "map" based on the information in "info".
3390 * In particular, remove the basic maps that have been marked removed and
3391 * update the others based on the information in the corresponding tableau.
3392 * Since we detected implicit equalities without calling
3393 * isl_basic_map_gauss, we need to do it now.
3394 * Also call isl_basic_map_simplify if we may have lost the definition
3395 * of one or more integer divisions.
3397 static __isl_give isl_map
*update_basic_maps(__isl_take isl_map
*map
,
3398 int n
, struct isl_coalesce_info
*info
)
3405 for (i
= n
- 1; i
>= 0; --i
) {
3406 if (info
[i
].removed
) {
3407 isl_basic_map_free(map
->p
[i
]);
3408 if (i
!= map
->n
- 1)
3409 map
->p
[i
] = map
->p
[map
->n
- 1];
3414 info
[i
].bmap
= isl_basic_map_update_from_tab(info
[i
].bmap
,
3416 info
[i
].bmap
= isl_basic_map_gauss(info
[i
].bmap
, NULL
);
3417 if (info
[i
].simplify
)
3418 info
[i
].bmap
= isl_basic_map_simplify(info
[i
].bmap
);
3419 info
[i
].bmap
= isl_basic_map_finalize(info
[i
].bmap
);
3421 return isl_map_free(map
);
3422 ISL_F_SET(info
[i
].bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
3423 ISL_F_SET(info
[i
].bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
3424 isl_basic_map_free(map
->p
[i
]);
3425 map
->p
[i
] = info
[i
].bmap
;
3426 info
[i
].bmap
= NULL
;
3432 /* For each pair of basic maps in the map, check if the union of the two
3433 * can be represented by a single basic map.
3434 * If so, replace the pair by the single basic map and start over.
3436 * We factor out any (hidden) common factor from the constraint
3437 * coefficients to improve the detection of adjacent constraints.
3439 * Since we are constructing the tableaus of the basic maps anyway,
3440 * we exploit them to detect implicit equalities and redundant constraints.
3441 * This also helps the coalescing as it can ignore the redundant constraints.
3442 * In order to avoid confusion, we make all implicit equalities explicit
3443 * in the basic maps. We don't call isl_basic_map_gauss, though,
3444 * as that may affect the number of constraints.
3445 * This means that we have to call isl_basic_map_gauss at the end
3446 * of the computation (in update_basic_maps) to ensure that
3447 * the basic maps are not left in an unexpected state.
3448 * For each basic map, we also compute the hash of the apparent affine hull
3449 * for use in coalesce.
3451 struct isl_map
*isl_map_coalesce(struct isl_map
*map
)
3456 struct isl_coalesce_info
*info
= NULL
;
3458 map
= isl_map_remove_empty_parts(map
);
3465 ctx
= isl_map_get_ctx(map
);
3466 map
= isl_map_sort_divs(map
);
3467 map
= isl_map_cow(map
);
3474 info
= isl_calloc_array(map
->ctx
, struct isl_coalesce_info
, n
);
3478 for (i
= 0; i
< map
->n
; ++i
) {
3479 map
->p
[i
] = isl_basic_map_reduce_coefficients(map
->p
[i
]);
3482 info
[i
].bmap
= isl_basic_map_copy(map
->p
[i
]);
3483 info
[i
].tab
= isl_tab_from_basic_map(info
[i
].bmap
, 0);
3486 if (!ISL_F_ISSET(info
[i
].bmap
, ISL_BASIC_MAP_NO_IMPLICIT
))
3487 if (isl_tab_detect_implicit_equalities(info
[i
].tab
) < 0)
3489 info
[i
].bmap
= isl_tab_make_equalities_explicit(info
[i
].tab
,
3493 if (!ISL_F_ISSET(info
[i
].bmap
, ISL_BASIC_MAP_NO_REDUNDANT
))
3494 if (isl_tab_detect_redundant(info
[i
].tab
) < 0)
3496 if (coalesce_info_set_hull_hash(&info
[i
]) < 0)
3499 for (i
= map
->n
- 1; i
>= 0; --i
)
3500 if (info
[i
].tab
->empty
)
3503 if (coalesce(ctx
, n
, info
) < 0)
3506 map
= update_basic_maps(map
, n
, info
);
3508 clear_coalesce_info(n
, info
);
3512 clear_coalesce_info(n
, info
);
3517 /* For each pair of basic sets in the set, check if the union of the two
3518 * can be represented by a single basic set.
3519 * If so, replace the pair by the single basic set and start over.
3521 struct isl_set
*isl_set_coalesce(struct isl_set
*set
)
3523 return set_from_map(isl_map_coalesce(set_to_map(set
)));