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 static int count(int *con
, unsigned len
, int status
)
139 for (i
= 0; i
< len
; ++i
)
140 if (con
[i
] == status
)
145 static int all(int *con
, unsigned len
, int status
)
149 for (i
= 0; i
< len
; ++i
) {
150 if (con
[i
] == STATUS_REDUNDANT
)
152 if (con
[i
] != status
)
158 /* Internal information associated to a basic map in a map
159 * that is to be coalesced by isl_map_coalesce.
161 * "bmap" is the basic map itself (or NULL if "removed" is set)
162 * "tab" is the corresponding tableau (or NULL if "removed" is set)
163 * "hull_hash" identifies the affine space in which "bmap" lives.
164 * "removed" is set if this basic map has been removed from the map
165 * "simplify" is set if this basic map may have some unknown integer
166 * divisions that were not present in the input basic maps. The basic
167 * map should then be simplified such that we may be able to find
168 * a definition among the constraints.
170 * "eq" and "ineq" are only set if we are currently trying to coalesce
171 * this basic map with another basic map, in which case they represent
172 * the position of the inequalities of this basic map with respect to
173 * the other basic map. The number of elements in the "eq" array
174 * is twice the number of equalities in the "bmap", corresponding
175 * to the two inequalities that make up each equality.
177 struct isl_coalesce_info
{
187 /* Are all non-redundant constraints of the basic map represented by "info"
188 * either valid or cut constraints with respect to the other basic map?
190 static int all_valid_or_cut(struct isl_coalesce_info
*info
)
194 for (i
= 0; i
< 2 * info
->bmap
->n_eq
; ++i
) {
195 if (info
->eq
[i
] == STATUS_REDUNDANT
)
197 if (info
->eq
[i
] == STATUS_VALID
)
199 if (info
->eq
[i
] == STATUS_CUT
)
204 for (i
= 0; i
< info
->bmap
->n_ineq
; ++i
) {
205 if (info
->ineq
[i
] == STATUS_REDUNDANT
)
207 if (info
->ineq
[i
] == STATUS_VALID
)
209 if (info
->ineq
[i
] == STATUS_CUT
)
217 /* Compute the hash of the (apparent) affine hull of info->bmap (with
218 * the existentially quantified variables removed) and store it
221 static int coalesce_info_set_hull_hash(struct isl_coalesce_info
*info
)
226 hull
= isl_basic_map_copy(info
->bmap
);
227 hull
= isl_basic_map_plain_affine_hull(hull
);
228 n_div
= isl_basic_map_dim(hull
, isl_dim_div
);
229 hull
= isl_basic_map_drop_constraints_involving_dims(hull
,
230 isl_dim_div
, 0, n_div
);
231 info
->hull_hash
= isl_basic_map_get_hash(hull
);
232 isl_basic_map_free(hull
);
234 return hull
? 0 : -1;
237 /* Free all the allocated memory in an array
238 * of "n" isl_coalesce_info elements.
240 static void clear_coalesce_info(int n
, struct isl_coalesce_info
*info
)
247 for (i
= 0; i
< n
; ++i
) {
248 isl_basic_map_free(info
[i
].bmap
);
249 isl_tab_free(info
[i
].tab
);
255 /* Drop the basic map represented by "info".
256 * That is, clear the memory associated to the entry and
257 * mark it as having been removed.
259 static void drop(struct isl_coalesce_info
*info
)
261 info
->bmap
= isl_basic_map_free(info
->bmap
);
262 isl_tab_free(info
->tab
);
267 /* Exchange the information in "info1" with that in "info2".
269 static void exchange(struct isl_coalesce_info
*info1
,
270 struct isl_coalesce_info
*info2
)
272 struct isl_coalesce_info info
;
279 /* This type represents the kind of change that has been performed
280 * while trying to coalesce two basic maps.
282 * isl_change_none: nothing was changed
283 * isl_change_drop_first: the first basic map was removed
284 * isl_change_drop_second: the second basic map was removed
285 * isl_change_fuse: the two basic maps were replaced by a new basic map.
288 isl_change_error
= -1,
290 isl_change_drop_first
,
291 isl_change_drop_second
,
295 /* Update "change" based on an interchange of the first and the second
296 * basic map. That is, interchange isl_change_drop_first and
297 * isl_change_drop_second.
299 static enum isl_change
invert_change(enum isl_change change
)
302 case isl_change_error
:
303 return isl_change_error
;
304 case isl_change_none
:
305 return isl_change_none
;
306 case isl_change_drop_first
:
307 return isl_change_drop_second
;
308 case isl_change_drop_second
:
309 return isl_change_drop_first
;
310 case isl_change_fuse
:
311 return isl_change_fuse
;
314 return isl_change_error
;
317 /* Add the valid constraints of the basic map represented by "info"
318 * to "bmap". "len" is the size of the constraints.
319 * If only one of the pair of inequalities that make up an equality
320 * is valid, then add that inequality.
322 static __isl_give isl_basic_map
*add_valid_constraints(
323 __isl_take isl_basic_map
*bmap
, struct isl_coalesce_info
*info
,
331 for (k
= 0; k
< info
->bmap
->n_eq
; ++k
) {
332 if (info
->eq
[2 * k
] == STATUS_VALID
&&
333 info
->eq
[2 * k
+ 1] == STATUS_VALID
) {
334 l
= isl_basic_map_alloc_equality(bmap
);
336 return isl_basic_map_free(bmap
);
337 isl_seq_cpy(bmap
->eq
[l
], info
->bmap
->eq
[k
], len
);
338 } else if (info
->eq
[2 * k
] == STATUS_VALID
) {
339 l
= isl_basic_map_alloc_inequality(bmap
);
341 return isl_basic_map_free(bmap
);
342 isl_seq_neg(bmap
->ineq
[l
], info
->bmap
->eq
[k
], len
);
343 } else if (info
->eq
[2 * k
+ 1] == STATUS_VALID
) {
344 l
= isl_basic_map_alloc_inequality(bmap
);
346 return isl_basic_map_free(bmap
);
347 isl_seq_cpy(bmap
->ineq
[l
], info
->bmap
->eq
[k
], len
);
351 for (k
= 0; k
< info
->bmap
->n_ineq
; ++k
) {
352 if (info
->ineq
[k
] != STATUS_VALID
)
354 l
= isl_basic_map_alloc_inequality(bmap
);
356 return isl_basic_map_free(bmap
);
357 isl_seq_cpy(bmap
->ineq
[l
], info
->bmap
->ineq
[k
], len
);
363 /* Is "bmap" defined by a number of (non-redundant) constraints that
364 * is greater than the number of constraints of basic maps i and j combined?
365 * Equalities are counted as two inequalities.
367 static int number_of_constraints_increases(int i
, int j
,
368 struct isl_coalesce_info
*info
,
369 __isl_keep isl_basic_map
*bmap
, struct isl_tab
*tab
)
373 n_old
= 2 * info
[i
].bmap
->n_eq
+ info
[i
].bmap
->n_ineq
;
374 n_old
+= 2 * info
[j
].bmap
->n_eq
+ info
[j
].bmap
->n_ineq
;
376 n_new
= 2 * bmap
->n_eq
;
377 for (k
= 0; k
< bmap
->n_ineq
; ++k
)
378 if (!isl_tab_is_redundant(tab
, bmap
->n_eq
+ k
))
381 return n_new
> n_old
;
384 /* Replace the pair of basic maps i and j by the basic map bounded
385 * by the valid constraints in both basic maps and the constraints
386 * in extra (if not NULL).
387 * Place the fused basic map in the position that is the smallest of i and j.
389 * If "detect_equalities" is set, then look for equalities encoded
390 * as pairs of inequalities.
391 * If "check_number" is set, then the original basic maps are only
392 * replaced if the total number of constraints does not increase.
393 * While the number of integer divisions in the two basic maps
394 * is assumed to be the same, the actual definitions may be different.
395 * We only copy the definition from one of the basic map if it is
396 * the same as that of the other basic map. Otherwise, we mark
397 * the integer division as unknown and simplify the basic map
398 * in an attempt to recover the integer division definition.
400 static enum isl_change
fuse(int i
, int j
, struct isl_coalesce_info
*info
,
401 __isl_keep isl_mat
*extra
, int detect_equalities
, int check_number
)
404 struct isl_basic_map
*fused
= NULL
;
405 struct isl_tab
*fused_tab
= NULL
;
406 unsigned total
= isl_basic_map_total_dim(info
[i
].bmap
);
407 unsigned extra_rows
= extra
? extra
->n_row
: 0;
408 unsigned n_eq
, n_ineq
;
412 return fuse(j
, i
, info
, extra
, detect_equalities
, check_number
);
414 n_eq
= info
[i
].bmap
->n_eq
+ info
[j
].bmap
->n_eq
;
415 n_ineq
= info
[i
].bmap
->n_ineq
+ info
[j
].bmap
->n_ineq
;
416 fused
= isl_basic_map_alloc_space(isl_space_copy(info
[i
].bmap
->dim
),
417 info
[i
].bmap
->n_div
, n_eq
, n_eq
+ n_ineq
+ extra_rows
);
418 fused
= add_valid_constraints(fused
, &info
[i
], 1 + total
);
419 fused
= add_valid_constraints(fused
, &info
[j
], 1 + total
);
422 if (ISL_F_ISSET(info
[i
].bmap
, ISL_BASIC_MAP_RATIONAL
) &&
423 ISL_F_ISSET(info
[j
].bmap
, ISL_BASIC_MAP_RATIONAL
))
424 ISL_F_SET(fused
, ISL_BASIC_MAP_RATIONAL
);
426 for (k
= 0; k
< info
[i
].bmap
->n_div
; ++k
) {
427 int l
= isl_basic_map_alloc_div(fused
);
430 if (isl_seq_eq(info
[i
].bmap
->div
[k
], info
[j
].bmap
->div
[k
],
432 isl_seq_cpy(fused
->div
[l
], info
[i
].bmap
->div
[k
],
435 isl_int_set_si(fused
->div
[l
][0], 0);
440 for (k
= 0; k
< extra_rows
; ++k
) {
441 l
= isl_basic_map_alloc_inequality(fused
);
444 isl_seq_cpy(fused
->ineq
[l
], extra
->row
[k
], 1 + total
);
447 if (detect_equalities
)
448 fused
= isl_basic_map_detect_inequality_pairs(fused
, NULL
);
449 fused
= isl_basic_map_gauss(fused
, NULL
);
450 if (simplify
|| info
[j
].simplify
) {
451 fused
= isl_basic_map_simplify(fused
);
452 info
[i
].simplify
= 0;
454 fused
= isl_basic_map_finalize(fused
);
456 fused_tab
= isl_tab_from_basic_map(fused
, 0);
457 if (isl_tab_detect_redundant(fused_tab
) < 0)
461 number_of_constraints_increases(i
, j
, info
, fused
, fused_tab
)) {
462 isl_tab_free(fused_tab
);
463 isl_basic_map_free(fused
);
464 return isl_change_none
;
467 isl_basic_map_free(info
[i
].bmap
);
468 info
[i
].bmap
= fused
;
469 isl_tab_free(info
[i
].tab
);
470 info
[i
].tab
= fused_tab
;
473 return isl_change_fuse
;
475 isl_tab_free(fused_tab
);
476 isl_basic_map_free(fused
);
477 return isl_change_error
;
480 /* Given a pair of basic maps i and j such that all constraints are either
481 * "valid" or "cut", check if the facets corresponding to the "cut"
482 * constraints of i lie entirely within basic map j.
483 * If so, replace the pair by the basic map consisting of the valid
484 * constraints in both basic maps.
485 * Checking whether the facet lies entirely within basic map j
486 * is performed by checking whether the constraints of basic map j
487 * are valid for the facet. These tests are performed on a rational
488 * tableau to avoid the theoretical possibility that a constraint
489 * that was considered to be a cut constraint for the entire basic map i
490 * happens to be considered to be a valid constraint for the facet,
491 * even though it cuts off the same rational points.
493 * To see that we are not introducing any extra points, call the
494 * two basic maps A and B and the resulting map U and let x
495 * be an element of U \setminus ( A \cup B ).
496 * A line connecting x with an element of A \cup B meets a facet F
497 * of either A or B. Assume it is a facet of B and let c_1 be
498 * the corresponding facet constraint. We have c_1(x) < 0 and
499 * so c_1 is a cut constraint. This implies that there is some
500 * (possibly rational) point x' satisfying the constraints of A
501 * and the opposite of c_1 as otherwise c_1 would have been marked
502 * valid for A. The line connecting x and x' meets a facet of A
503 * in a (possibly rational) point that also violates c_1, but this
504 * is impossible since all cut constraints of B are valid for all
506 * In case F is a facet of A rather than B, then we can apply the
507 * above reasoning to find a facet of B separating x from A \cup B first.
509 static enum isl_change
check_facets(int i
, int j
,
510 struct isl_coalesce_info
*info
)
513 struct isl_tab_undo
*snap
, *snap2
;
514 unsigned n_eq
= info
[i
].bmap
->n_eq
;
516 snap
= isl_tab_snap(info
[i
].tab
);
517 if (isl_tab_mark_rational(info
[i
].tab
) < 0)
518 return isl_change_error
;
519 snap2
= isl_tab_snap(info
[i
].tab
);
521 for (k
= 0; k
< info
[i
].bmap
->n_ineq
; ++k
) {
522 if (info
[i
].ineq
[k
] != STATUS_CUT
)
524 if (isl_tab_select_facet(info
[i
].tab
, n_eq
+ k
) < 0)
525 return isl_change_error
;
526 for (l
= 0; l
< info
[j
].bmap
->n_ineq
; ++l
) {
528 if (info
[j
].ineq
[l
] != STATUS_CUT
)
530 stat
= status_in(info
[j
].bmap
->ineq
[l
], info
[i
].tab
);
532 return isl_change_error
;
533 if (stat
!= STATUS_VALID
)
536 if (isl_tab_rollback(info
[i
].tab
, snap2
) < 0)
537 return isl_change_error
;
538 if (l
< info
[j
].bmap
->n_ineq
)
542 if (k
< info
[i
].bmap
->n_ineq
) {
543 if (isl_tab_rollback(info
[i
].tab
, snap
) < 0)
544 return isl_change_error
;
545 return isl_change_none
;
547 return fuse(i
, j
, info
, NULL
, 0, 0);
550 /* Check if info->bmap contains the basic map represented
551 * by the tableau "tab".
552 * For each equality, we check both the constraint itself
553 * (as an inequality) and its negation. Make sure the
554 * equality is returned to its original state before returning.
556 static int contains(struct isl_coalesce_info
*info
, struct isl_tab
*tab
)
560 isl_basic_map
*bmap
= info
->bmap
;
562 dim
= isl_basic_map_total_dim(bmap
);
563 for (k
= 0; k
< bmap
->n_eq
; ++k
) {
565 isl_seq_neg(bmap
->eq
[k
], bmap
->eq
[k
], 1 + dim
);
566 stat
= status_in(bmap
->eq
[k
], tab
);
567 isl_seq_neg(bmap
->eq
[k
], bmap
->eq
[k
], 1 + dim
);
570 if (stat
!= STATUS_VALID
)
572 stat
= status_in(bmap
->eq
[k
], tab
);
575 if (stat
!= STATUS_VALID
)
579 for (k
= 0; k
< bmap
->n_ineq
; ++k
) {
581 if (info
->ineq
[k
] == STATUS_REDUNDANT
)
583 stat
= status_in(bmap
->ineq
[k
], tab
);
586 if (stat
!= STATUS_VALID
)
592 /* Basic map "i" has an inequality (say "k") that is adjacent
593 * to some inequality of basic map "j". All the other inequalities
595 * Check if basic map "j" forms an extension of basic map "i".
597 * Note that this function is only called if some of the equalities or
598 * inequalities of basic map "j" do cut basic map "i". The function is
599 * correct even if there are no such cut constraints, but in that case
600 * the additional checks performed by this function are overkill.
602 * In particular, we replace constraint k, say f >= 0, by constraint
603 * f <= -1, add the inequalities of "j" that are valid for "i"
604 * and check if the result is a subset of basic map "j".
605 * If so, then we know that this result is exactly equal to basic map "j"
606 * since all its constraints are valid for basic map "j".
607 * By combining the valid constraints of "i" (all equalities and all
608 * inequalities except "k") and the valid constraints of "j" we therefore
609 * obtain a basic map that is equal to their union.
610 * In this case, there is no need to perform a rollback of the tableau
611 * since it is going to be destroyed in fuse().
617 * |_______| _ |_________\
629 static enum isl_change
is_adj_ineq_extension(int i
, int j
,
630 struct isl_coalesce_info
*info
)
633 struct isl_tab_undo
*snap
;
634 unsigned n_eq
= info
[i
].bmap
->n_eq
;
635 unsigned total
= isl_basic_map_total_dim(info
[i
].bmap
);
639 if (isl_tab_extend_cons(info
[i
].tab
, 1 + info
[j
].bmap
->n_ineq
) < 0)
640 return isl_change_error
;
642 for (k
= 0; k
< info
[i
].bmap
->n_ineq
; ++k
)
643 if (info
[i
].ineq
[k
] == STATUS_ADJ_INEQ
)
645 if (k
>= info
[i
].bmap
->n_ineq
)
646 isl_die(isl_basic_map_get_ctx(info
[i
].bmap
), isl_error_internal
,
647 "info[i].ineq should have exactly one STATUS_ADJ_INEQ",
648 return isl_change_error
);
650 snap
= isl_tab_snap(info
[i
].tab
);
652 if (isl_tab_unrestrict(info
[i
].tab
, n_eq
+ k
) < 0)
653 return isl_change_error
;
655 isl_seq_neg(info
[i
].bmap
->ineq
[k
], info
[i
].bmap
->ineq
[k
], 1 + total
);
656 isl_int_sub_ui(info
[i
].bmap
->ineq
[k
][0], info
[i
].bmap
->ineq
[k
][0], 1);
657 r
= isl_tab_add_ineq(info
[i
].tab
, info
[i
].bmap
->ineq
[k
]);
658 isl_seq_neg(info
[i
].bmap
->ineq
[k
], info
[i
].bmap
->ineq
[k
], 1 + total
);
659 isl_int_sub_ui(info
[i
].bmap
->ineq
[k
][0], info
[i
].bmap
->ineq
[k
][0], 1);
661 return isl_change_error
;
663 for (k
= 0; k
< info
[j
].bmap
->n_ineq
; ++k
) {
664 if (info
[j
].ineq
[k
] != STATUS_VALID
)
666 if (isl_tab_add_ineq(info
[i
].tab
, info
[j
].bmap
->ineq
[k
]) < 0)
667 return isl_change_error
;
670 super
= contains(&info
[j
], info
[i
].tab
);
672 return isl_change_error
;
674 return fuse(i
, j
, info
, NULL
, 0, 0);
676 if (isl_tab_rollback(info
[i
].tab
, snap
) < 0)
677 return isl_change_error
;
679 return isl_change_none
;
683 /* Both basic maps have at least one inequality with and adjacent
684 * (but opposite) inequality in the other basic map.
685 * Check that there are no cut constraints and that there is only
686 * a single pair of adjacent inequalities.
687 * If so, we can replace the pair by a single basic map described
688 * by all but the pair of adjacent inequalities.
689 * Any additional points introduced lie strictly between the two
690 * adjacent hyperplanes and can therefore be integral.
699 * The test for a single pair of adjancent inequalities is important
700 * for avoiding the combination of two basic maps like the following
710 * If there are some cut constraints on one side, then we may
711 * still be able to fuse the two basic maps, but we need to perform
712 * some additional checks in is_adj_ineq_extension.
714 static enum isl_change
check_adj_ineq(int i
, int j
,
715 struct isl_coalesce_info
*info
)
717 int count_i
, count_j
;
720 count_i
= count(info
[i
].ineq
, info
[i
].bmap
->n_ineq
, STATUS_ADJ_INEQ
);
721 count_j
= count(info
[j
].ineq
, info
[j
].bmap
->n_ineq
, STATUS_ADJ_INEQ
);
723 if (count_i
!= 1 && count_j
!= 1)
724 return isl_change_none
;
726 cut_i
= any(info
[i
].eq
, 2 * info
[i
].bmap
->n_eq
, STATUS_CUT
) ||
727 any(info
[i
].ineq
, info
[i
].bmap
->n_ineq
, STATUS_CUT
);
728 cut_j
= any(info
[j
].eq
, 2 * info
[j
].bmap
->n_eq
, STATUS_CUT
) ||
729 any(info
[j
].ineq
, info
[j
].bmap
->n_ineq
, STATUS_CUT
);
731 if (!cut_i
&& !cut_j
&& count_i
== 1 && count_j
== 1)
732 return fuse(i
, j
, info
, NULL
, 0, 0);
734 if (count_i
== 1 && !cut_i
)
735 return is_adj_ineq_extension(i
, j
, info
);
737 if (count_j
== 1 && !cut_j
)
738 return is_adj_ineq_extension(j
, i
, info
);
740 return isl_change_none
;
743 /* Given an affine transformation matrix "T", does row "row" represent
744 * anything other than a unit vector (possibly shifted by a constant)
745 * that is not involved in any of the other rows?
747 * That is, if a constraint involves the variable corresponding to
748 * the row, then could its preimage by "T" have any coefficients
749 * that are different from those in the original constraint?
751 static int not_unique_unit_row(__isl_keep isl_mat
*T
, int row
)
754 int len
= T
->n_col
- 1;
756 i
= isl_seq_first_non_zero(T
->row
[row
] + 1, len
);
759 if (!isl_int_is_one(T
->row
[row
][1 + i
]) &&
760 !isl_int_is_negone(T
->row
[row
][1 + i
]))
763 j
= isl_seq_first_non_zero(T
->row
[row
] + 1 + i
+ 1, len
- (i
+ 1));
767 for (j
= 1; j
< T
->n_row
; ++j
) {
770 if (!isl_int_is_zero(T
->row
[j
][1 + i
]))
777 /* Does inequality constraint "ineq" of "bmap" involve any of
778 * the variables marked in "affected"?
779 * "total" is the total number of variables, i.e., the number
780 * of entries in "affected".
782 static int is_affected(__isl_keep isl_basic_map
*bmap
, int ineq
, int *affected
,
787 for (i
= 0; i
< total
; ++i
) {
790 if (!isl_int_is_zero(bmap
->ineq
[ineq
][1 + i
]))
797 /* Given the compressed version of inequality constraint "ineq"
798 * of info->bmap in "v", check if the constraint can be tightened,
799 * where the compression is based on an equality constraint valid
801 * If so, add the tightened version of the inequality constraint
802 * to info->tab. "v" may be modified by this function.
804 * That is, if the compressed constraint is of the form
808 * with 0 < c < m, then it is equivalent to
812 * This means that c can also be subtracted from the original,
813 * uncompressed constraint without affecting the integer points
814 * in info->tab. Add this tightened constraint as an extra row
815 * to info->tab to make this information explicitly available.
817 static __isl_give isl_vec
*try_tightening(struct isl_coalesce_info
*info
,
818 int ineq
, __isl_take isl_vec
*v
)
826 ctx
= isl_vec_get_ctx(v
);
827 isl_seq_gcd(v
->el
+ 1, v
->size
- 1, &ctx
->normalize_gcd
);
828 if (isl_int_is_zero(ctx
->normalize_gcd
) ||
829 isl_int_is_one(ctx
->normalize_gcd
)) {
837 isl_int_fdiv_r(v
->el
[0], v
->el
[0], ctx
->normalize_gcd
);
838 if (isl_int_is_zero(v
->el
[0]))
841 if (isl_tab_extend_cons(info
->tab
, 1) < 0)
842 return isl_vec_free(v
);
844 isl_int_sub(info
->bmap
->ineq
[ineq
][0],
845 info
->bmap
->ineq
[ineq
][0], v
->el
[0]);
846 r
= isl_tab_add_ineq(info
->tab
, info
->bmap
->ineq
[ineq
]);
847 isl_int_add(info
->bmap
->ineq
[ineq
][0],
848 info
->bmap
->ineq
[ineq
][0], v
->el
[0]);
851 return isl_vec_free(v
);
856 /* Tighten the (non-redundant) constraints on the facet represented
858 * In particular, on input, info->tab represents the result
859 * of replacing constraint k of info->bmap, i.e., f_k >= 0,
860 * by the adjacent equality, i.e., f_k + 1 = 0.
862 * Compute a variable compression from the equality constraint f_k + 1 = 0
863 * and use it to tighten the other constraints of info->bmap,
864 * updating info->tab (and leaving info->bmap untouched).
865 * The compression handles essentially two cases, one where a variable
866 * is assigned a fixed value and can therefore be eliminated, and one
867 * where one variable is a shifted multiple of some other variable and
868 * can therefore be replaced by that multiple.
869 * Gaussian elimination would also work for the first case, but for
870 * the second case, the effectiveness would depend on the order
872 * After compression, some of the constraints may have coefficients
873 * with a common divisor. If this divisor does not divide the constant
874 * term, then the constraint can be tightened.
875 * The tightening is performed on the tableau info->tab by introducing
876 * extra (temporary) constraints.
878 * Only constraints that are possibly affected by the compression are
879 * considered. In particular, if the constraint only involves variables
880 * that are directly mapped to a distinct set of other variables, then
881 * no common divisor can be introduced and no tightening can occur.
883 * It is important to only consider the non-redundant constraints
884 * since the facet constraint has been relaxed prior to the call
885 * to this function, meaning that the constraints that were redundant
886 * prior to the relaxation may no longer be redundant.
887 * These constraints will be ignored in the fused result, so
888 * the fusion detection should not exploit them.
890 static isl_stat
tighten_on_relaxed_facet(struct isl_coalesce_info
*info
,
900 ctx
= isl_basic_map_get_ctx(info
->bmap
);
901 total
= isl_basic_map_total_dim(info
->bmap
);
902 isl_int_add_ui(info
->bmap
->ineq
[k
][0], info
->bmap
->ineq
[k
][0], 1);
903 T
= isl_mat_sub_alloc6(ctx
, info
->bmap
->ineq
, k
, 1, 0, 1 + total
);
904 T
= isl_mat_variable_compression(T
, NULL
);
905 isl_int_sub_ui(info
->bmap
->ineq
[k
][0], info
->bmap
->ineq
[k
][0], 1);
907 return isl_stat_error
;
913 affected
= isl_alloc_array(ctx
, int, total
);
917 for (i
= 0; i
< total
; ++i
)
918 affected
[i
] = not_unique_unit_row(T
, 1 + i
);
920 for (i
= 0; i
< info
->bmap
->n_ineq
; ++i
) {
923 if (info
->ineq
[i
] == STATUS_REDUNDANT
)
925 if (!is_affected(info
->bmap
, i
, affected
, total
))
927 v
= isl_vec_alloc(ctx
, 1 + total
);
930 isl_seq_cpy(v
->el
, info
->bmap
->ineq
[i
], 1 + total
);
931 v
= isl_vec_mat_product(v
, isl_mat_copy(T
));
932 v
= try_tightening(info
, i
, v
);
944 return isl_stat_error
;
947 /* Basic map "i" has an inequality "k" that is adjacent to some equality
948 * of basic map "j". All the other inequalities are valid for "j".
949 * Check if basic map "j" forms an extension of basic map "i".
951 * In particular, we relax constraint "k", compute the corresponding
952 * facet and check whether it is included in the other basic map.
953 * Before testing for inclusion, the constraints on the facet
954 * are tightened to increase the chance of an inclusion being detected.
955 * If the facet is included, we know that relaxing the constraint extends
956 * the basic map with exactly the other basic map (we already know that this
957 * other basic map is included in the extension, because there
958 * were no "cut" inequalities in "i") and we can replace the
959 * two basic maps by this extension.
960 * Each integer division that does not have exactly the same
961 * definition in "i" and "j" is marked unknown and the basic map
962 * is scheduled to be simplified in an attempt to recover
963 * the integer division definition.
964 * Place this extension in the position that is the smallest of i and j.
972 static enum isl_change
is_adj_eq_extension(int i
, int j
, int k
,
973 struct isl_coalesce_info
*info
)
975 int change
= isl_change_none
;
977 struct isl_tab_undo
*snap
, *snap2
;
978 unsigned n_eq
= info
[i
].bmap
->n_eq
;
980 if (isl_tab_is_equality(info
[i
].tab
, n_eq
+ k
))
981 return isl_change_none
;
983 snap
= isl_tab_snap(info
[i
].tab
);
984 if (isl_tab_relax(info
[i
].tab
, n_eq
+ k
) < 0)
985 return isl_change_error
;
986 snap2
= isl_tab_snap(info
[i
].tab
);
987 if (isl_tab_select_facet(info
[i
].tab
, n_eq
+ k
) < 0)
988 return isl_change_error
;
989 if (tighten_on_relaxed_facet(&info
[i
], k
) < 0)
990 return isl_change_error
;
991 super
= contains(&info
[j
], info
[i
].tab
);
993 return isl_change_error
;
998 if (isl_tab_rollback(info
[i
].tab
, snap2
) < 0)
999 return isl_change_error
;
1000 info
[i
].bmap
= isl_basic_map_cow(info
[i
].bmap
);
1002 return isl_change_error
;
1003 total
= isl_basic_map_total_dim(info
[i
].bmap
);
1004 for (l
= 0; l
< info
[i
].bmap
->n_div
; ++l
)
1005 if (!isl_seq_eq(info
[i
].bmap
->div
[l
],
1006 info
[j
].bmap
->div
[l
], 1 + 1 + total
)) {
1007 isl_int_set_si(info
[i
].bmap
->div
[l
][0], 0);
1008 info
[i
].simplify
= 1;
1010 isl_int_add_ui(info
[i
].bmap
->ineq
[k
][0],
1011 info
[i
].bmap
->ineq
[k
][0], 1);
1012 ISL_F_SET(info
[i
].bmap
, ISL_BASIC_MAP_FINAL
);
1015 exchange(&info
[i
], &info
[j
]);
1016 change
= isl_change_fuse
;
1018 if (isl_tab_rollback(info
[i
].tab
, snap
) < 0)
1019 return isl_change_error
;
1024 /* Data structure that keeps track of the wrapping constraints
1025 * and of information to bound the coefficients of those constraints.
1027 * bound is set if we want to apply a bound on the coefficients
1028 * mat contains the wrapping constraints
1029 * max is the bound on the coefficients (if bound is set)
1037 /* Update wraps->max to be greater than or equal to the coefficients
1038 * in the equalities and inequalities of info->bmap that can be removed
1039 * if we end up applying wrapping.
1041 static void wraps_update_max(struct isl_wraps
*wraps
,
1042 struct isl_coalesce_info
*info
)
1046 unsigned total
= isl_basic_map_total_dim(info
->bmap
);
1048 isl_int_init(max_k
);
1050 for (k
= 0; k
< info
->bmap
->n_eq
; ++k
) {
1051 if (info
->eq
[2 * k
] == STATUS_VALID
&&
1052 info
->eq
[2 * k
+ 1] == STATUS_VALID
)
1054 isl_seq_abs_max(info
->bmap
->eq
[k
] + 1, total
, &max_k
);
1055 if (isl_int_abs_gt(max_k
, wraps
->max
))
1056 isl_int_set(wraps
->max
, max_k
);
1059 for (k
= 0; k
< info
->bmap
->n_ineq
; ++k
) {
1060 if (info
->ineq
[k
] == STATUS_VALID
||
1061 info
->ineq
[k
] == STATUS_REDUNDANT
)
1063 isl_seq_abs_max(info
->bmap
->ineq
[k
] + 1, total
, &max_k
);
1064 if (isl_int_abs_gt(max_k
, wraps
->max
))
1065 isl_int_set(wraps
->max
, max_k
);
1068 isl_int_clear(max_k
);
1071 /* Initialize the isl_wraps data structure.
1072 * If we want to bound the coefficients of the wrapping constraints,
1073 * we set wraps->max to the largest coefficient
1074 * in the equalities and inequalities that can be removed if we end up
1075 * applying wrapping.
1077 static void wraps_init(struct isl_wraps
*wraps
, __isl_take isl_mat
*mat
,
1078 struct isl_coalesce_info
*info
, int i
, int j
)
1086 ctx
= isl_mat_get_ctx(mat
);
1087 wraps
->bound
= isl_options_get_coalesce_bounded_wrapping(ctx
);
1090 isl_int_init(wraps
->max
);
1091 isl_int_set_si(wraps
->max
, 0);
1092 wraps_update_max(wraps
, &info
[i
]);
1093 wraps_update_max(wraps
, &info
[j
]);
1096 /* Free the contents of the isl_wraps data structure.
1098 static void wraps_free(struct isl_wraps
*wraps
)
1100 isl_mat_free(wraps
->mat
);
1102 isl_int_clear(wraps
->max
);
1105 /* Is the wrapping constraint in row "row" allowed?
1107 * If wraps->bound is set, we check that none of the coefficients
1108 * is greater than wraps->max.
1110 static int allow_wrap(struct isl_wraps
*wraps
, int row
)
1117 for (i
= 1; i
< wraps
->mat
->n_col
; ++i
)
1118 if (isl_int_abs_gt(wraps
->mat
->row
[row
][i
], wraps
->max
))
1124 /* Wrap "ineq" (or its opposite if "negate" is set) around "bound"
1125 * to include "set" and add the result in position "w" of "wraps".
1126 * "len" is the total number of coefficients in "bound" and "ineq".
1127 * Return 1 on success, 0 on failure and -1 on error.
1128 * Wrapping can fail if the result of wrapping is equal to "bound"
1129 * or if we want to bound the sizes of the coefficients and
1130 * the wrapped constraint does not satisfy this bound.
1132 static int add_wrap(struct isl_wraps
*wraps
, int w
, isl_int
*bound
,
1133 isl_int
*ineq
, unsigned len
, __isl_keep isl_set
*set
, int negate
)
1135 isl_seq_cpy(wraps
->mat
->row
[w
], bound
, len
);
1137 isl_seq_neg(wraps
->mat
->row
[w
+ 1], ineq
, len
);
1138 ineq
= wraps
->mat
->row
[w
+ 1];
1140 if (!isl_set_wrap_facet(set
, wraps
->mat
->row
[w
], ineq
))
1142 if (isl_seq_eq(wraps
->mat
->row
[w
], bound
, len
))
1144 if (!allow_wrap(wraps
, w
))
1149 /* For each constraint in info->bmap that is not redundant (as determined
1150 * by info->tab) and that is not a valid constraint for the other basic map,
1151 * wrap the constraint around "bound" such that it includes the whole
1152 * set "set" and append the resulting constraint to "wraps".
1153 * Note that the constraints that are valid for the other basic map
1154 * will be added to the combined basic map by default, so there is
1155 * no need to wrap them.
1156 * The caller wrap_in_facets even relies on this function not wrapping
1157 * any constraints that are already valid.
1158 * "wraps" is assumed to have been pre-allocated to the appropriate size.
1159 * wraps->n_row is the number of actual wrapped constraints that have
1161 * If any of the wrapping problems results in a constraint that is
1162 * identical to "bound", then this means that "set" is unbounded in such
1163 * way that no wrapping is possible. If this happens then wraps->n_row
1165 * Similarly, if we want to bound the coefficients of the wrapping
1166 * constraints and a newly added wrapping constraint does not
1167 * satisfy the bound, then wraps->n_row is also reset to zero.
1169 static int add_wraps(struct isl_wraps
*wraps
, struct isl_coalesce_info
*info
,
1170 isl_int
*bound
, __isl_keep isl_set
*set
)
1175 isl_basic_map
*bmap
= info
->bmap
;
1176 unsigned len
= 1 + isl_basic_map_total_dim(bmap
);
1178 w
= wraps
->mat
->n_row
;
1180 for (l
= 0; l
< bmap
->n_ineq
; ++l
) {
1181 if (info
->ineq
[l
] == STATUS_VALID
||
1182 info
->ineq
[l
] == STATUS_REDUNDANT
)
1184 if (isl_seq_is_neg(bound
, bmap
->ineq
[l
], len
))
1186 if (isl_seq_eq(bound
, bmap
->ineq
[l
], len
))
1188 if (isl_tab_is_redundant(info
->tab
, bmap
->n_eq
+ l
))
1191 added
= add_wrap(wraps
, w
, bound
, bmap
->ineq
[l
], len
, set
, 0);
1198 for (l
= 0; l
< bmap
->n_eq
; ++l
) {
1199 if (isl_seq_is_neg(bound
, bmap
->eq
[l
], len
))
1201 if (isl_seq_eq(bound
, bmap
->eq
[l
], len
))
1204 for (m
= 0; m
< 2; ++m
) {
1205 if (info
->eq
[2 * l
+ m
] == STATUS_VALID
)
1207 added
= add_wrap(wraps
, w
, bound
, bmap
->eq
[l
], len
,
1217 wraps
->mat
->n_row
= w
;
1220 wraps
->mat
->n_row
= 0;
1224 /* Check if the constraints in "wraps" from "first" until the last
1225 * are all valid for the basic set represented by "tab".
1226 * If not, wraps->n_row is set to zero.
1228 static int check_wraps(__isl_keep isl_mat
*wraps
, int first
,
1229 struct isl_tab
*tab
)
1233 for (i
= first
; i
< wraps
->n_row
; ++i
) {
1234 enum isl_ineq_type type
;
1235 type
= isl_tab_ineq_type(tab
, wraps
->row
[i
]);
1236 if (type
== isl_ineq_error
)
1238 if (type
== isl_ineq_redundant
)
1247 /* Return a set that corresponds to the non-redundant constraints
1248 * (as recorded in tab) of bmap.
1250 * It's important to remove the redundant constraints as some
1251 * of the other constraints may have been modified after the
1252 * constraints were marked redundant.
1253 * In particular, a constraint may have been relaxed.
1254 * Redundant constraints are ignored when a constraint is relaxed
1255 * and should therefore continue to be ignored ever after.
1256 * Otherwise, the relaxation might be thwarted by some of
1257 * these constraints.
1259 * Update the underlying set to ensure that the dimension doesn't change.
1260 * Otherwise the integer divisions could get dropped if the tab
1261 * turns out to be empty.
1263 static __isl_give isl_set
*set_from_updated_bmap(__isl_keep isl_basic_map
*bmap
,
1264 struct isl_tab
*tab
)
1266 isl_basic_set
*bset
;
1268 bmap
= isl_basic_map_copy(bmap
);
1269 bset
= isl_basic_map_underlying_set(bmap
);
1270 bset
= isl_basic_set_cow(bset
);
1271 bset
= isl_basic_set_update_from_tab(bset
, tab
);
1272 return isl_set_from_basic_set(bset
);
1275 /* Wrap the constraints of info->bmap that bound the facet defined
1276 * by inequality "k" around (the opposite of) this inequality to
1277 * include "set". "bound" may be used to store the negated inequality.
1278 * Since the wrapped constraints are not guaranteed to contain the whole
1279 * of info->bmap, we check them in check_wraps.
1280 * If any of the wrapped constraints turn out to be invalid, then
1281 * check_wraps will reset wrap->n_row to zero.
1283 static int add_wraps_around_facet(struct isl_wraps
*wraps
,
1284 struct isl_coalesce_info
*info
, int k
, isl_int
*bound
,
1285 __isl_keep isl_set
*set
)
1287 struct isl_tab_undo
*snap
;
1289 unsigned total
= isl_basic_map_total_dim(info
->bmap
);
1291 snap
= isl_tab_snap(info
->tab
);
1293 if (isl_tab_select_facet(info
->tab
, info
->bmap
->n_eq
+ k
) < 0)
1295 if (isl_tab_detect_redundant(info
->tab
) < 0)
1298 isl_seq_neg(bound
, info
->bmap
->ineq
[k
], 1 + total
);
1300 n
= wraps
->mat
->n_row
;
1301 if (add_wraps(wraps
, info
, bound
, set
) < 0)
1304 if (isl_tab_rollback(info
->tab
, snap
) < 0)
1306 if (check_wraps(wraps
->mat
, n
, info
->tab
) < 0)
1312 /* Given a basic set i with a constraint k that is adjacent to
1313 * basic set j, check if we can wrap
1314 * both the facet corresponding to k (if "wrap_facet" is set) and basic map j
1315 * (always) around their ridges to include the other set.
1316 * If so, replace the pair of basic sets by their union.
1318 * All constraints of i (except k) are assumed to be valid or
1319 * cut constraints for j.
1320 * Wrapping the cut constraints to include basic map j may result
1321 * in constraints that are no longer valid of basic map i
1322 * we have to check that the resulting wrapping constraints are valid for i.
1323 * If "wrap_facet" is not set, then all constraints of i (except k)
1324 * are assumed to be valid for j.
1333 static enum isl_change
can_wrap_in_facet(int i
, int j
, int k
,
1334 struct isl_coalesce_info
*info
, int wrap_facet
)
1336 enum isl_change change
= isl_change_none
;
1337 struct isl_wraps wraps
;
1340 struct isl_set
*set_i
= NULL
;
1341 struct isl_set
*set_j
= NULL
;
1342 struct isl_vec
*bound
= NULL
;
1343 unsigned total
= isl_basic_map_total_dim(info
[i
].bmap
);
1345 set_i
= set_from_updated_bmap(info
[i
].bmap
, info
[i
].tab
);
1346 set_j
= set_from_updated_bmap(info
[j
].bmap
, info
[j
].tab
);
1347 ctx
= isl_basic_map_get_ctx(info
[i
].bmap
);
1348 mat
= isl_mat_alloc(ctx
, 2 * (info
[i
].bmap
->n_eq
+ info
[j
].bmap
->n_eq
) +
1349 info
[i
].bmap
->n_ineq
+ info
[j
].bmap
->n_ineq
,
1351 wraps_init(&wraps
, mat
, info
, i
, j
);
1352 bound
= isl_vec_alloc(ctx
, 1 + total
);
1353 if (!set_i
|| !set_j
|| !wraps
.mat
|| !bound
)
1356 isl_seq_cpy(bound
->el
, info
[i
].bmap
->ineq
[k
], 1 + total
);
1357 isl_int_add_ui(bound
->el
[0], bound
->el
[0], 1);
1359 isl_seq_cpy(wraps
.mat
->row
[0], bound
->el
, 1 + total
);
1360 wraps
.mat
->n_row
= 1;
1362 if (add_wraps(&wraps
, &info
[j
], bound
->el
, set_i
) < 0)
1364 if (!wraps
.mat
->n_row
)
1368 if (add_wraps_around_facet(&wraps
, &info
[i
], k
,
1369 bound
->el
, set_j
) < 0)
1371 if (!wraps
.mat
->n_row
)
1375 change
= fuse(i
, j
, info
, wraps
.mat
, 0, 0);
1380 isl_set_free(set_i
);
1381 isl_set_free(set_j
);
1383 isl_vec_free(bound
);
1388 isl_vec_free(bound
);
1389 isl_set_free(set_i
);
1390 isl_set_free(set_j
);
1391 return isl_change_error
;
1394 /* Given a cut constraint t(x) >= 0 of basic map i, stored in row "w"
1395 * of wrap.mat, replace it by its relaxed version t(x) + 1 >= 0, and
1396 * add wrapping constraints to wrap.mat for all constraints
1397 * of basic map j that bound the part of basic map j that sticks out
1398 * of the cut constraint.
1399 * "set_i" is the underlying set of basic map i.
1400 * If any wrapping fails, then wraps->mat.n_row is reset to zero.
1402 * In particular, we first intersect basic map j with t(x) + 1 = 0.
1403 * If the result is empty, then t(x) >= 0 was actually a valid constraint
1404 * (with respect to the integer points), so we add t(x) >= 0 instead.
1405 * Otherwise, we wrap the constraints of basic map j that are not
1406 * redundant in this intersection and that are not already valid
1407 * for basic map i over basic map i.
1408 * Note that it is sufficient to wrap the constraints to include
1409 * basic map i, because we will only wrap the constraints that do
1410 * not include basic map i already. The wrapped constraint will
1411 * therefore be more relaxed compared to the original constraint.
1412 * Since the original constraint is valid for basic map j, so is
1413 * the wrapped constraint.
1415 static isl_stat
wrap_in_facet(struct isl_wraps
*wraps
, int w
,
1416 struct isl_coalesce_info
*info_j
, __isl_keep isl_set
*set_i
,
1417 struct isl_tab_undo
*snap
)
1419 isl_int_add_ui(wraps
->mat
->row
[w
][0], wraps
->mat
->row
[w
][0], 1);
1420 if (isl_tab_add_eq(info_j
->tab
, wraps
->mat
->row
[w
]) < 0)
1421 return isl_stat_error
;
1422 if (isl_tab_detect_redundant(info_j
->tab
) < 0)
1423 return isl_stat_error
;
1425 if (info_j
->tab
->empty
)
1426 isl_int_sub_ui(wraps
->mat
->row
[w
][0], wraps
->mat
->row
[w
][0], 1);
1427 else if (add_wraps(wraps
, info_j
, wraps
->mat
->row
[w
], set_i
) < 0)
1428 return isl_stat_error
;
1430 if (isl_tab_rollback(info_j
->tab
, snap
) < 0)
1431 return isl_stat_error
;
1436 /* Given a pair of basic maps i and j such that j sticks out
1437 * of i at n cut constraints, each time by at most one,
1438 * try to compute wrapping constraints and replace the two
1439 * basic maps by a single basic map.
1440 * The other constraints of i are assumed to be valid for j.
1441 * "set_i" is the underlying set of basic map i.
1442 * "wraps" has been initialized to be of the right size.
1444 * For each cut constraint t(x) >= 0 of i, we add the relaxed version
1445 * t(x) + 1 >= 0, along with wrapping constraints for all constraints
1446 * of basic map j that bound the part of basic map j that sticks out
1447 * of the cut constraint.
1449 * If any wrapping fails, i.e., if we cannot wrap to touch
1450 * the union, then we give up.
1451 * Otherwise, the pair of basic maps is replaced by their union.
1453 static enum isl_change
try_wrap_in_facets(int i
, int j
,
1454 struct isl_coalesce_info
*info
, struct isl_wraps
*wraps
,
1455 __isl_keep isl_set
*set_i
)
1459 struct isl_tab_undo
*snap
;
1461 total
= isl_basic_map_total_dim(info
[i
].bmap
);
1463 snap
= isl_tab_snap(info
[j
].tab
);
1465 wraps
->mat
->n_row
= 0;
1467 for (k
= 0; k
< info
[i
].bmap
->n_eq
; ++k
) {
1468 for (l
= 0; l
< 2; ++l
) {
1469 if (info
[i
].eq
[2 * k
+ l
] != STATUS_CUT
)
1471 w
= wraps
->mat
->n_row
++;
1473 isl_seq_neg(wraps
->mat
->row
[w
],
1474 info
[i
].bmap
->eq
[k
], 1 + total
);
1476 isl_seq_cpy(wraps
->mat
->row
[w
],
1477 info
[i
].bmap
->eq
[k
], 1 + total
);
1478 if (wrap_in_facet(wraps
, w
, &info
[j
], set_i
, snap
) < 0)
1479 return isl_change_error
;
1481 if (!wraps
->mat
->n_row
)
1482 return isl_change_none
;
1486 for (k
= 0; k
< info
[i
].bmap
->n_ineq
; ++k
) {
1487 if (info
[i
].ineq
[k
] != STATUS_CUT
)
1489 w
= wraps
->mat
->n_row
++;
1490 isl_seq_cpy(wraps
->mat
->row
[w
],
1491 info
[i
].bmap
->ineq
[k
], 1 + total
);
1492 if (wrap_in_facet(wraps
, w
, &info
[j
], set_i
, snap
) < 0)
1493 return isl_change_error
;
1495 if (!wraps
->mat
->n_row
)
1496 return isl_change_none
;
1499 return fuse(i
, j
, info
, wraps
->mat
, 0, 1);
1502 /* Given a pair of basic maps i and j such that j sticks out
1503 * of i at n cut constraints, each time by at most one,
1504 * try to compute wrapping constraints and replace the two
1505 * basic maps by a single basic map.
1506 * The other constraints of i are assumed to be valid for j.
1508 * The core computation is performed by try_wrap_in_facets.
1509 * This function simply extracts an underlying set representation
1510 * of basic map i and initializes the data structure for keeping
1511 * track of wrapping constraints.
1513 static enum isl_change
wrap_in_facets(int i
, int j
, int n
,
1514 struct isl_coalesce_info
*info
)
1516 enum isl_change change
= isl_change_none
;
1517 struct isl_wraps wraps
;
1520 isl_set
*set_i
= NULL
;
1521 unsigned total
= isl_basic_map_total_dim(info
[i
].bmap
);
1524 if (isl_tab_extend_cons(info
[j
].tab
, 1) < 0)
1525 return isl_change_error
;
1527 max_wrap
= 1 + 2 * info
[j
].bmap
->n_eq
+ info
[j
].bmap
->n_ineq
;
1530 set_i
= set_from_updated_bmap(info
[i
].bmap
, info
[i
].tab
);
1531 ctx
= isl_basic_map_get_ctx(info
[i
].bmap
);
1532 mat
= isl_mat_alloc(ctx
, max_wrap
, 1 + total
);
1533 wraps_init(&wraps
, mat
, info
, i
, j
);
1534 if (!set_i
|| !wraps
.mat
)
1537 change
= try_wrap_in_facets(i
, j
, info
, &wraps
, set_i
);
1540 isl_set_free(set_i
);
1545 isl_set_free(set_i
);
1546 return isl_change_error
;
1549 /* Return the effect of inequality "ineq" on the tableau "tab",
1550 * after relaxing the constant term of "ineq" by one.
1552 static enum isl_ineq_type
type_of_relaxed(struct isl_tab
*tab
, isl_int
*ineq
)
1554 enum isl_ineq_type type
;
1556 isl_int_add_ui(ineq
[0], ineq
[0], 1);
1557 type
= isl_tab_ineq_type(tab
, ineq
);
1558 isl_int_sub_ui(ineq
[0], ineq
[0], 1);
1563 /* Given two basic sets i and j,
1564 * check if relaxing all the cut constraints of i by one turns
1565 * them into valid constraint for j and check if we can wrap in
1566 * the bits that are sticking out.
1567 * If so, replace the pair by their union.
1569 * We first check if all relaxed cut inequalities of i are valid for j
1570 * and then try to wrap in the intersections of the relaxed cut inequalities
1573 * During this wrapping, we consider the points of j that lie at a distance
1574 * of exactly 1 from i. In particular, we ignore the points that lie in
1575 * between this lower-dimensional space and the basic map i.
1576 * We can therefore only apply this to integer maps.
1602 * Wrapping can fail if the result of wrapping one of the facets
1603 * around its edges does not produce any new facet constraint.
1604 * In particular, this happens when we try to wrap in unbounded sets.
1606 * _______________________________________________________________________
1610 * |_| |_________________________________________________________________
1613 * The following is not an acceptable result of coalescing the above two
1614 * sets as it includes extra integer points.
1615 * _______________________________________________________________________
1620 * \______________________________________________________________________
1622 static enum isl_change
can_wrap_in_set(int i
, int j
,
1623 struct isl_coalesce_info
*info
)
1629 if (ISL_F_ISSET(info
[i
].bmap
, ISL_BASIC_MAP_RATIONAL
) ||
1630 ISL_F_ISSET(info
[j
].bmap
, ISL_BASIC_MAP_RATIONAL
))
1631 return isl_change_none
;
1633 n
= count(info
[i
].eq
, 2 * info
[i
].bmap
->n_eq
, STATUS_CUT
);
1634 n
+= count(info
[i
].ineq
, info
[i
].bmap
->n_ineq
, STATUS_CUT
);
1636 return isl_change_none
;
1638 total
= isl_basic_map_total_dim(info
[i
].bmap
);
1639 for (k
= 0; k
< info
[i
].bmap
->n_eq
; ++k
) {
1640 for (l
= 0; l
< 2; ++l
) {
1641 enum isl_ineq_type type
;
1643 if (info
[i
].eq
[2 * k
+ l
] != STATUS_CUT
)
1647 isl_seq_neg(info
[i
].bmap
->eq
[k
],
1648 info
[i
].bmap
->eq
[k
], 1 + total
);
1649 type
= type_of_relaxed(info
[j
].tab
,
1650 info
[i
].bmap
->eq
[k
]);
1652 isl_seq_neg(info
[i
].bmap
->eq
[k
],
1653 info
[i
].bmap
->eq
[k
], 1 + total
);
1654 if (type
== isl_ineq_error
)
1655 return isl_change_error
;
1656 if (type
!= isl_ineq_redundant
)
1657 return isl_change_none
;
1661 for (k
= 0; k
< info
[i
].bmap
->n_ineq
; ++k
) {
1662 enum isl_ineq_type type
;
1664 if (info
[i
].ineq
[k
] != STATUS_CUT
)
1667 type
= type_of_relaxed(info
[j
].tab
, info
[i
].bmap
->ineq
[k
]);
1668 if (type
== isl_ineq_error
)
1669 return isl_change_error
;
1670 if (type
!= isl_ineq_redundant
)
1671 return isl_change_none
;
1674 return wrap_in_facets(i
, j
, n
, info
);
1677 /* Check if either i or j has only cut constraints that can
1678 * be used to wrap in (a facet of) the other basic set.
1679 * if so, replace the pair by their union.
1681 static enum isl_change
check_wrap(int i
, int j
, struct isl_coalesce_info
*info
)
1683 enum isl_change change
= isl_change_none
;
1685 change
= can_wrap_in_set(i
, j
, info
);
1686 if (change
!= isl_change_none
)
1689 change
= can_wrap_in_set(j
, i
, info
);
1693 /* At least one of the basic maps has an equality that is adjacent
1694 * to inequality. Make sure that only one of the basic maps has
1695 * such an equality and that the other basic map has exactly one
1696 * inequality adjacent to an equality.
1697 * If the other basic map does not have such an inequality, then
1698 * check if all its constraints are either valid or cut constraints
1699 * and, if so, try wrapping in the first map into the second.
1701 * We call the basic map that has the inequality "i" and the basic
1702 * map that has the equality "j".
1703 * If "i" has any "cut" (in)equality, then relaxing the inequality
1704 * by one would not result in a basic map that contains the other
1705 * basic map. However, it may still be possible to wrap in the other
1708 static enum isl_change
check_adj_eq(int i
, int j
,
1709 struct isl_coalesce_info
*info
)
1711 enum isl_change change
= isl_change_none
;
1715 if (any(info
[i
].eq
, 2 * info
[i
].bmap
->n_eq
, STATUS_ADJ_INEQ
) &&
1716 any(info
[j
].eq
, 2 * info
[j
].bmap
->n_eq
, STATUS_ADJ_INEQ
))
1717 /* ADJ EQ TOO MANY */
1718 return isl_change_none
;
1720 if (any(info
[i
].eq
, 2 * info
[i
].bmap
->n_eq
, STATUS_ADJ_INEQ
))
1721 return check_adj_eq(j
, i
, info
);
1723 /* j has an equality adjacent to an inequality in i */
1725 if (count(info
[i
].ineq
, info
[i
].bmap
->n_ineq
, STATUS_ADJ_EQ
) != 1) {
1726 if (all_valid_or_cut(&info
[i
]))
1727 return can_wrap_in_set(i
, j
, info
);
1728 return isl_change_none
;
1730 if (any(info
[i
].eq
, 2 * info
[i
].bmap
->n_eq
, STATUS_CUT
))
1731 return isl_change_none
;
1732 any_cut
= any(info
[i
].ineq
, info
[i
].bmap
->n_ineq
, STATUS_CUT
);
1733 if (any(info
[j
].ineq
, info
[j
].bmap
->n_ineq
, STATUS_ADJ_EQ
) ||
1734 any(info
[i
].ineq
, info
[i
].bmap
->n_ineq
, STATUS_ADJ_INEQ
) ||
1735 any(info
[j
].ineq
, info
[j
].bmap
->n_ineq
, STATUS_ADJ_INEQ
))
1736 /* ADJ EQ TOO MANY */
1737 return isl_change_none
;
1739 for (k
= 0; k
< info
[i
].bmap
->n_ineq
; ++k
)
1740 if (info
[i
].ineq
[k
] == STATUS_ADJ_EQ
)
1744 change
= is_adj_eq_extension(i
, j
, k
, info
);
1745 if (change
!= isl_change_none
)
1749 change
= can_wrap_in_facet(i
, j
, k
, info
, any_cut
);
1754 /* The two basic maps lie on adjacent hyperplanes. In particular,
1755 * basic map "i" has an equality that lies parallel to basic map "j".
1756 * Check if we can wrap the facets around the parallel hyperplanes
1757 * to include the other set.
1759 * We perform basically the same operations as can_wrap_in_facet,
1760 * except that we don't need to select a facet of one of the sets.
1766 * If there is more than one equality of "i" adjacent to an equality of "j",
1767 * then the result will satisfy one or more equalities that are a linear
1768 * combination of these equalities. These will be encoded as pairs
1769 * of inequalities in the wrapping constraints and need to be made
1772 static enum isl_change
check_eq_adj_eq(int i
, int j
,
1773 struct isl_coalesce_info
*info
)
1776 enum isl_change change
= isl_change_none
;
1777 int detect_equalities
= 0;
1778 struct isl_wraps wraps
;
1781 struct isl_set
*set_i
= NULL
;
1782 struct isl_set
*set_j
= NULL
;
1783 struct isl_vec
*bound
= NULL
;
1784 unsigned total
= isl_basic_map_total_dim(info
[i
].bmap
);
1786 if (count(info
[i
].eq
, 2 * info
[i
].bmap
->n_eq
, STATUS_ADJ_EQ
) != 1)
1787 detect_equalities
= 1;
1789 for (k
= 0; k
< 2 * info
[i
].bmap
->n_eq
; ++k
)
1790 if (info
[i
].eq
[k
] == STATUS_ADJ_EQ
)
1793 set_i
= set_from_updated_bmap(info
[i
].bmap
, info
[i
].tab
);
1794 set_j
= set_from_updated_bmap(info
[j
].bmap
, info
[j
].tab
);
1795 ctx
= isl_basic_map_get_ctx(info
[i
].bmap
);
1796 mat
= isl_mat_alloc(ctx
, 2 * (info
[i
].bmap
->n_eq
+ info
[j
].bmap
->n_eq
) +
1797 info
[i
].bmap
->n_ineq
+ info
[j
].bmap
->n_ineq
,
1799 wraps_init(&wraps
, mat
, info
, i
, j
);
1800 bound
= isl_vec_alloc(ctx
, 1 + total
);
1801 if (!set_i
|| !set_j
|| !wraps
.mat
|| !bound
)
1805 isl_seq_neg(bound
->el
, info
[i
].bmap
->eq
[k
/ 2], 1 + total
);
1807 isl_seq_cpy(bound
->el
, info
[i
].bmap
->eq
[k
/ 2], 1 + total
);
1808 isl_int_add_ui(bound
->el
[0], bound
->el
[0], 1);
1810 isl_seq_cpy(wraps
.mat
->row
[0], bound
->el
, 1 + total
);
1811 wraps
.mat
->n_row
= 1;
1813 if (add_wraps(&wraps
, &info
[j
], bound
->el
, set_i
) < 0)
1815 if (!wraps
.mat
->n_row
)
1818 isl_int_sub_ui(bound
->el
[0], bound
->el
[0], 1);
1819 isl_seq_neg(bound
->el
, bound
->el
, 1 + total
);
1821 isl_seq_cpy(wraps
.mat
->row
[wraps
.mat
->n_row
], bound
->el
, 1 + total
);
1824 if (add_wraps(&wraps
, &info
[i
], bound
->el
, set_j
) < 0)
1826 if (!wraps
.mat
->n_row
)
1829 change
= fuse(i
, j
, info
, wraps
.mat
, detect_equalities
, 0);
1832 error
: change
= isl_change_error
;
1837 isl_set_free(set_i
);
1838 isl_set_free(set_j
);
1839 isl_vec_free(bound
);
1844 /* Initialize the "eq" and "ineq" fields of "info".
1846 static void init_status(struct isl_coalesce_info
*info
)
1848 info
->eq
= info
->ineq
= NULL
;
1851 /* Set info->eq to the positions of the equalities of info->bmap
1852 * with respect to the basic map represented by "tab".
1853 * If info->eq has already been computed, then do not compute it again.
1855 static void set_eq_status_in(struct isl_coalesce_info
*info
,
1856 struct isl_tab
*tab
)
1860 info
->eq
= eq_status_in(info
->bmap
, tab
);
1863 /* Set info->ineq to the positions of the inequalities of info->bmap
1864 * with respect to the basic map represented by "tab".
1865 * If info->ineq has already been computed, then do not compute it again.
1867 static void set_ineq_status_in(struct isl_coalesce_info
*info
,
1868 struct isl_tab
*tab
)
1872 info
->ineq
= ineq_status_in(info
->bmap
, info
->tab
, tab
);
1875 /* Free the memory allocated by the "eq" and "ineq" fields of "info".
1876 * This function assumes that init_status has been called on "info" first,
1877 * after which the "eq" and "ineq" fields may or may not have been
1878 * assigned a newly allocated array.
1880 static void clear_status(struct isl_coalesce_info
*info
)
1886 /* Check if the union of the given pair of basic maps
1887 * can be represented by a single basic map.
1888 * If so, replace the pair by the single basic map and return
1889 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
1890 * Otherwise, return isl_change_none.
1891 * The two basic maps are assumed to live in the same local space.
1892 * The "eq" and "ineq" fields of info[i] and info[j] are assumed
1893 * to have been initialized by the caller, either to NULL or
1894 * to valid information.
1896 * We first check the effect of each constraint of one basic map
1897 * on the other basic map.
1898 * The constraint may be
1899 * redundant the constraint is redundant in its own
1900 * basic map and should be ignore and removed
1902 * valid all (integer) points of the other basic map
1903 * satisfy the constraint
1904 * separate no (integer) point of the other basic map
1905 * satisfies the constraint
1906 * cut some but not all points of the other basic map
1907 * satisfy the constraint
1908 * adj_eq the given constraint is adjacent (on the outside)
1909 * to an equality of the other basic map
1910 * adj_ineq the given constraint is adjacent (on the outside)
1911 * to an inequality of the other basic map
1913 * We consider seven cases in which we can replace the pair by a single
1914 * basic map. We ignore all "redundant" constraints.
1916 * 1. all constraints of one basic map are valid
1917 * => the other basic map is a subset and can be removed
1919 * 2. all constraints of both basic maps are either "valid" or "cut"
1920 * and the facets corresponding to the "cut" constraints
1921 * of one of the basic maps lies entirely inside the other basic map
1922 * => the pair can be replaced by a basic map consisting
1923 * of the valid constraints in both basic maps
1925 * 3. there is a single pair of adjacent inequalities
1926 * (all other constraints are "valid")
1927 * => the pair can be replaced by a basic map consisting
1928 * of the valid constraints in both basic maps
1930 * 4. one basic map has a single adjacent inequality, while the other
1931 * constraints are "valid". The other basic map has some
1932 * "cut" constraints, but replacing the adjacent inequality by
1933 * its opposite and adding the valid constraints of the other
1934 * basic map results in a subset of the other basic map
1935 * => the pair can be replaced by a basic map consisting
1936 * of the valid constraints in both basic maps
1938 * 5. there is a single adjacent pair of an inequality and an equality,
1939 * the other constraints of the basic map containing the inequality are
1940 * "valid". Moreover, if the inequality the basic map is relaxed
1941 * and then turned into an equality, then resulting facet lies
1942 * entirely inside the other basic map
1943 * => the pair can be replaced by the basic map containing
1944 * the inequality, with the inequality relaxed.
1946 * 6. there is a single adjacent pair of an inequality and an equality,
1947 * the other constraints of the basic map containing the inequality are
1948 * "valid". Moreover, the facets corresponding to both
1949 * the inequality and the equality can be wrapped around their
1950 * ridges to include the other basic map
1951 * => the pair can be replaced by a basic map consisting
1952 * of the valid constraints in both basic maps together
1953 * with all wrapping constraints
1955 * 7. one of the basic maps extends beyond the other by at most one.
1956 * Moreover, the facets corresponding to the cut constraints and
1957 * the pieces of the other basic map at offset one from these cut
1958 * constraints can be wrapped around their ridges to include
1959 * the union of the two basic maps
1960 * => the pair can be replaced by a basic map consisting
1961 * of the valid constraints in both basic maps together
1962 * with all wrapping constraints
1964 * 8. the two basic maps live in adjacent hyperplanes. In principle
1965 * such sets can always be combined through wrapping, but we impose
1966 * that there is only one such pair, to avoid overeager coalescing.
1968 * Throughout the computation, we maintain a collection of tableaus
1969 * corresponding to the basic maps. When the basic maps are dropped
1970 * or combined, the tableaus are modified accordingly.
1972 static enum isl_change
coalesce_local_pair_reuse(int i
, int j
,
1973 struct isl_coalesce_info
*info
)
1975 enum isl_change change
= isl_change_none
;
1977 set_eq_status_in(&info
[i
], info
[j
].tab
);
1978 if (info
[i
].bmap
->n_eq
&& !info
[i
].eq
)
1980 if (any(info
[i
].eq
, 2 * info
[i
].bmap
->n_eq
, STATUS_ERROR
))
1982 if (any(info
[i
].eq
, 2 * info
[i
].bmap
->n_eq
, STATUS_SEPARATE
))
1985 set_eq_status_in(&info
[j
], info
[i
].tab
);
1986 if (info
[j
].bmap
->n_eq
&& !info
[j
].eq
)
1988 if (any(info
[j
].eq
, 2 * info
[j
].bmap
->n_eq
, STATUS_ERROR
))
1990 if (any(info
[j
].eq
, 2 * info
[j
].bmap
->n_eq
, STATUS_SEPARATE
))
1993 set_ineq_status_in(&info
[i
], info
[j
].tab
);
1994 if (info
[i
].bmap
->n_ineq
&& !info
[i
].ineq
)
1996 if (any(info
[i
].ineq
, info
[i
].bmap
->n_ineq
, STATUS_ERROR
))
1998 if (any(info
[i
].ineq
, info
[i
].bmap
->n_ineq
, STATUS_SEPARATE
))
2001 set_ineq_status_in(&info
[j
], info
[i
].tab
);
2002 if (info
[j
].bmap
->n_ineq
&& !info
[j
].ineq
)
2004 if (any(info
[j
].ineq
, info
[j
].bmap
->n_ineq
, STATUS_ERROR
))
2006 if (any(info
[j
].ineq
, info
[j
].bmap
->n_ineq
, STATUS_SEPARATE
))
2009 if (all(info
[i
].eq
, 2 * info
[i
].bmap
->n_eq
, STATUS_VALID
) &&
2010 all(info
[i
].ineq
, info
[i
].bmap
->n_ineq
, STATUS_VALID
)) {
2012 change
= isl_change_drop_second
;
2013 } else if (all(info
[j
].eq
, 2 * info
[j
].bmap
->n_eq
, STATUS_VALID
) &&
2014 all(info
[j
].ineq
, info
[j
].bmap
->n_ineq
, STATUS_VALID
)) {
2016 change
= isl_change_drop_first
;
2017 } else if (any(info
[i
].eq
, 2 * info
[i
].bmap
->n_eq
, STATUS_ADJ_EQ
)) {
2018 change
= check_eq_adj_eq(i
, j
, info
);
2019 } else if (any(info
[j
].eq
, 2 * info
[j
].bmap
->n_eq
, STATUS_ADJ_EQ
)) {
2020 change
= check_eq_adj_eq(j
, i
, info
);
2021 } else if (any(info
[i
].eq
, 2 * info
[i
].bmap
->n_eq
, STATUS_ADJ_INEQ
) ||
2022 any(info
[j
].eq
, 2 * info
[j
].bmap
->n_eq
, STATUS_ADJ_INEQ
)) {
2023 change
= check_adj_eq(i
, j
, info
);
2024 } else if (any(info
[i
].ineq
, info
[i
].bmap
->n_ineq
, STATUS_ADJ_EQ
) ||
2025 any(info
[j
].ineq
, info
[j
].bmap
->n_ineq
, STATUS_ADJ_EQ
)) {
2028 } else if (any(info
[i
].ineq
, info
[i
].bmap
->n_ineq
, STATUS_ADJ_INEQ
) ||
2029 any(info
[j
].ineq
, info
[j
].bmap
->n_ineq
, STATUS_ADJ_INEQ
)) {
2030 change
= check_adj_ineq(i
, j
, info
);
2032 if (!any(info
[i
].eq
, 2 * info
[i
].bmap
->n_eq
, STATUS_CUT
) &&
2033 !any(info
[j
].eq
, 2 * info
[j
].bmap
->n_eq
, STATUS_CUT
))
2034 change
= check_facets(i
, j
, info
);
2035 if (change
== isl_change_none
)
2036 change
= check_wrap(i
, j
, info
);
2040 clear_status(&info
[i
]);
2041 clear_status(&info
[j
]);
2044 clear_status(&info
[i
]);
2045 clear_status(&info
[j
]);
2046 return isl_change_error
;
2049 /* Check if the union of the given pair of basic maps
2050 * can be represented by a single basic map.
2051 * If so, replace the pair by the single basic map and return
2052 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
2053 * Otherwise, return isl_change_none.
2054 * The two basic maps are assumed to live in the same local space.
2056 static enum isl_change
coalesce_local_pair(int i
, int j
,
2057 struct isl_coalesce_info
*info
)
2059 init_status(&info
[i
]);
2060 init_status(&info
[j
]);
2061 return coalesce_local_pair_reuse(i
, j
, info
);
2064 /* Shift the integer division at position "div" of the basic map
2065 * represented by "info" by "shift".
2067 * That is, if the integer division has the form
2071 * then replace it by
2073 * floor((f(x) + shift * d)/d) - shift
2075 static int shift_div(struct isl_coalesce_info
*info
, int div
, isl_int shift
)
2079 info
->bmap
= isl_basic_map_shift_div(info
->bmap
, div
, 0, shift
);
2083 total
= isl_basic_map_dim(info
->bmap
, isl_dim_all
);
2084 total
-= isl_basic_map_dim(info
->bmap
, isl_dim_div
);
2085 if (isl_tab_shift_var(info
->tab
, total
+ div
, shift
) < 0)
2091 /* Check if some of the divs in the basic map represented by "info1"
2092 * are shifts of the corresponding divs in the basic map represented
2093 * by "info2". If so, align them with those of "info2".
2094 * Only do this if "info1" and "info2" have the same number
2095 * of integer divisions.
2097 * An integer division is considered to be a shift of another integer
2098 * division if one is equal to the other plus a constant.
2100 * In particular, for each pair of integer divisions, if both are known,
2101 * have identical coefficients (apart from the constant term) and
2102 * if the difference between the constant terms (taking into account
2103 * the denominator) is an integer, then move the difference outside.
2104 * That is, if one integer division is of the form
2106 * floor((f(x) + c_1)/d)
2108 * while the other is of the form
2110 * floor((f(x) + c_2)/d)
2112 * and n = (c_2 - c_1)/d is an integer, then replace the first
2113 * integer division by
2115 * floor((f(x) + c_1 + n * d)/d) - n = floor((f(x) + c_2)/d) - n
2117 static int harmonize_divs(struct isl_coalesce_info
*info1
,
2118 struct isl_coalesce_info
*info2
)
2123 if (!info1
->bmap
|| !info2
->bmap
)
2126 if (info1
->bmap
->n_div
!= info2
->bmap
->n_div
)
2128 if (info1
->bmap
->n_div
== 0)
2131 total
= isl_basic_map_total_dim(info1
->bmap
);
2132 for (i
= 0; i
< info1
->bmap
->n_div
; ++i
) {
2136 if (isl_int_is_zero(info1
->bmap
->div
[i
][0]) ||
2137 isl_int_is_zero(info2
->bmap
->div
[i
][0]))
2139 if (isl_int_ne(info1
->bmap
->div
[i
][0], info2
->bmap
->div
[i
][0]))
2141 if (isl_int_eq(info1
->bmap
->div
[i
][1], info2
->bmap
->div
[i
][1]))
2143 if (!isl_seq_eq(info1
->bmap
->div
[i
] + 2,
2144 info2
->bmap
->div
[i
] + 2, total
))
2147 isl_int_sub(d
, info2
->bmap
->div
[i
][1], info1
->bmap
->div
[i
][1]);
2148 if (isl_int_is_divisible_by(d
, info1
->bmap
->div
[i
][0])) {
2149 isl_int_divexact(d
, d
, info1
->bmap
->div
[i
][0]);
2150 r
= shift_div(info1
, i
, d
);
2160 /* Do the two basic maps live in the same local space, i.e.,
2161 * do they have the same (known) divs?
2162 * If either basic map has any unknown divs, then we can only assume
2163 * that they do not live in the same local space.
2165 static int same_divs(__isl_keep isl_basic_map
*bmap1
,
2166 __isl_keep isl_basic_map
*bmap2
)
2172 if (!bmap1
|| !bmap2
)
2174 if (bmap1
->n_div
!= bmap2
->n_div
)
2177 if (bmap1
->n_div
== 0)
2180 known
= isl_basic_map_divs_known(bmap1
);
2181 if (known
< 0 || !known
)
2183 known
= isl_basic_map_divs_known(bmap2
);
2184 if (known
< 0 || !known
)
2187 total
= isl_basic_map_total_dim(bmap1
);
2188 for (i
= 0; i
< bmap1
->n_div
; ++i
)
2189 if (!isl_seq_eq(bmap1
->div
[i
], bmap2
->div
[i
], 2 + total
))
2195 /* Expand info->tab in the same way info->bmap was expanded in
2196 * isl_basic_map_expand_divs using the expansion "exp" and
2197 * update info->ineq with respect to the redundant constraints
2198 * in the resulting tableau. "bmap" is the original version
2199 * of info->bmap, i.e., the one that corresponds to the current
2200 * state of info->tab. The number of constraints in "bmap"
2201 * is assumed to be the same as the number of constraints
2202 * in info->tab. This is required to be able to detect
2203 * the extra constraints in info->bmap.
2205 * In particular, introduce extra variables corresponding
2206 * to the extra integer divisions and add the div constraints
2207 * that were added to info->bmap after info->tab was created
2208 * from the original info->bmap.
2209 * info->ineq was computed without a tableau and therefore
2210 * does not take into account the redundant constraints
2211 * in the tableau. Mark them here.
2213 static isl_stat
expand_tab(struct isl_coalesce_info
*info
, int *exp
,
2214 __isl_keep isl_basic_map
*bmap
)
2216 unsigned total
, pos
, n_div
;
2218 int i
, n
, j
, n_ineq
;
2222 return isl_stat_error
;
2223 if (bmap
->n_eq
+ bmap
->n_ineq
!= info
->tab
->n_con
)
2224 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_internal
,
2225 "original tableau does not correspond "
2226 "to original basic map", return isl_stat_error
);
2228 total
= isl_basic_map_dim(info
->bmap
, isl_dim_all
);
2229 n_div
= isl_basic_map_dim(info
->bmap
, isl_dim_div
);
2230 pos
= total
- n_div
;
2231 extra_var
= total
- info
->tab
->n_var
;
2232 n
= n_div
- extra_var
;
2234 if (isl_tab_extend_vars(info
->tab
, extra_var
) < 0)
2235 return isl_stat_error
;
2236 if (isl_tab_extend_cons(info
->tab
, 2 * extra_var
) < 0)
2237 return isl_stat_error
;
2240 for (j
= 0; j
< n_div
; ++j
) {
2241 if (i
< n
&& exp
[i
] == j
) {
2245 if (isl_tab_insert_var(info
->tab
, pos
+ j
) < 0)
2246 return isl_stat_error
;
2249 n_ineq
= info
->tab
->n_con
- info
->tab
->n_eq
;
2250 for (i
= n_ineq
; i
< info
->bmap
->n_ineq
; ++i
)
2251 if (isl_tab_add_ineq(info
->tab
, info
->bmap
->ineq
[i
]) < 0)
2252 return isl_stat_error
;
2254 n_eq
= info
->bmap
->n_eq
;
2255 for (i
= 0; i
< info
->bmap
->n_ineq
; ++i
) {
2256 if (isl_tab_is_redundant(info
->tab
, n_eq
+ i
))
2257 info
->ineq
[i
] = STATUS_REDUNDANT
;
2263 /* Check if the union of the basic maps represented by info[i] and info[j]
2264 * can be represented by a single basic map,
2265 * after expanding the divs of info[i] to match those of info[j].
2266 * If so, replace the pair by the single basic map and return
2267 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
2268 * Otherwise, return isl_change_none.
2270 * The caller has already checked for info[j] being a subset of info[i].
2271 * If some of the divs of info[j] are unknown, then the expanded info[i]
2272 * will not have the corresponding div constraints. The other patterns
2273 * therefore cannot apply. Skip the computation in this case.
2275 * The expansion is performed using the divs "div" and expansion "exp"
2276 * computed by the caller.
2277 * info[i].bmap has already been expanded and the result is passed in
2279 * The "eq" and "ineq" fields of info[i] reflect the status of
2280 * the constraints of the expanded "bmap" with respect to info[j].tab.
2281 * However, inequality constraints that are redundant in info[i].tab
2282 * have not yet been marked as such because no tableau was available.
2284 * Replace info[i].bmap by "bmap" and expand info[i].tab as well,
2285 * updating info[i].ineq with respect to the redundant constraints.
2286 * Then try and coalesce the expanded info[i] with info[j],
2287 * reusing the information in info[i].eq and info[i].ineq.
2288 * If this does not result in any coalescing or if it results in info[j]
2289 * getting dropped (which should not happen in practice, since the case
2290 * of info[j] being a subset of info[i] has already been checked by
2291 * the caller), then revert info[i] to its original state.
2293 static enum isl_change
coalesce_expand_tab_divs(__isl_take isl_basic_map
*bmap
,
2294 int i
, int j
, struct isl_coalesce_info
*info
, __isl_keep isl_mat
*div
,
2298 isl_basic_map
*bmap_i
;
2299 struct isl_tab_undo
*snap
;
2300 enum isl_change change
= isl_change_none
;
2302 known
= isl_basic_map_divs_known(info
[j
].bmap
);
2303 if (known
< 0 || !known
) {
2304 clear_status(&info
[i
]);
2305 isl_basic_map_free(bmap
);
2306 return known
< 0 ? isl_change_error
: isl_change_none
;
2309 bmap_i
= info
[i
].bmap
;
2310 info
[i
].bmap
= isl_basic_map_copy(bmap
);
2311 snap
= isl_tab_snap(info
[i
].tab
);
2312 if (!info
[i
].bmap
|| expand_tab(&info
[i
], exp
, bmap_i
) < 0)
2313 change
= isl_change_error
;
2315 init_status(&info
[j
]);
2316 if (change
== isl_change_none
)
2317 change
= coalesce_local_pair_reuse(i
, j
, info
);
2319 clear_status(&info
[i
]);
2320 if (change
!= isl_change_none
&& change
!= isl_change_drop_second
) {
2321 isl_basic_map_free(bmap_i
);
2323 isl_basic_map_free(info
[i
].bmap
);
2324 info
[i
].bmap
= bmap_i
;
2326 if (isl_tab_rollback(info
[i
].tab
, snap
) < 0)
2327 change
= isl_change_error
;
2330 isl_basic_map_free(bmap
);
2334 /* Check if the union of "bmap" and the basic map represented by info[j]
2335 * can be represented by a single basic map,
2336 * after expanding the divs of "bmap" to match those of info[j].
2337 * If so, replace the pair by the single basic map and return
2338 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
2339 * Otherwise, return isl_change_none.
2341 * In particular, check if the expanded "bmap" contains the basic map
2342 * represented by the tableau info[j].tab.
2343 * The expansion is performed using the divs "div" and expansion "exp"
2344 * computed by the caller.
2345 * Then we check if all constraints of the expanded "bmap" are valid for
2348 * If "i" is not equal to -1, then "bmap" is equal to info[i].bmap.
2349 * In this case, the positions of the constraints of info[i].bmap
2350 * with respect to the basic map represented by info[j] are stored
2353 * If the expanded "bmap" does not contain the basic map
2354 * represented by the tableau info[j].tab and if "i" is not -1,
2355 * i.e., if the original "bmap" is info[i].bmap, then expand info[i].tab
2356 * as well and check if that results in coalescing.
2358 static enum isl_change
coalesce_with_expanded_divs(
2359 __isl_keep isl_basic_map
*bmap
, int i
, int j
,
2360 struct isl_coalesce_info
*info
, __isl_keep isl_mat
*div
, int *exp
)
2362 enum isl_change change
= isl_change_none
;
2363 struct isl_coalesce_info info_local
, *info_i
;
2365 info_i
= i
>= 0 ? &info
[i
] : &info_local
;
2366 init_status(info_i
);
2367 bmap
= isl_basic_map_copy(bmap
);
2368 bmap
= isl_basic_map_expand_divs(bmap
, isl_mat_copy(div
), exp
);
2373 info_i
->eq
= eq_status_in(bmap
, info
[j
].tab
);
2374 if (bmap
->n_eq
&& !info_i
->eq
)
2376 if (any(info_i
->eq
, 2 * bmap
->n_eq
, STATUS_ERROR
))
2378 if (any(info_i
->eq
, 2 * bmap
->n_eq
, STATUS_SEPARATE
))
2381 info_i
->ineq
= ineq_status_in(bmap
, NULL
, info
[j
].tab
);
2382 if (bmap
->n_ineq
&& !info_i
->ineq
)
2384 if (any(info_i
->ineq
, bmap
->n_ineq
, STATUS_ERROR
))
2386 if (any(info_i
->ineq
, bmap
->n_ineq
, STATUS_SEPARATE
))
2389 if (all(info_i
->eq
, 2 * bmap
->n_eq
, STATUS_VALID
) &&
2390 all(info_i
->ineq
, bmap
->n_ineq
, STATUS_VALID
)) {
2392 change
= isl_change_drop_second
;
2395 if (change
== isl_change_none
&& i
!= -1)
2396 return coalesce_expand_tab_divs(bmap
, i
, j
, info
, div
, exp
);
2399 isl_basic_map_free(bmap
);
2400 clear_status(info_i
);
2403 isl_basic_map_free(bmap
);
2404 clear_status(info_i
);
2405 return isl_change_error
;
2408 /* Check if the union of "bmap_i" and the basic map represented by info[j]
2409 * can be represented by a single basic map,
2410 * after aligning the divs of "bmap_i" to match those of info[j].
2411 * If so, replace the pair by the single basic map and return
2412 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
2413 * Otherwise, return isl_change_none.
2415 * In particular, check if "bmap_i" contains the basic map represented by
2416 * info[j] after aligning the divs of "bmap_i" to those of info[j].
2417 * Note that this can only succeed if the number of divs of "bmap_i"
2418 * is smaller than (or equal to) the number of divs of info[j].
2420 * We first check if the divs of "bmap_i" are all known and form a subset
2421 * of those of info[j].bmap. If so, we pass control over to
2422 * coalesce_with_expanded_divs.
2424 * If "i" is not equal to -1, then "bmap" is equal to info[i].bmap.
2426 static enum isl_change
coalesce_after_aligning_divs(
2427 __isl_keep isl_basic_map
*bmap_i
, int i
, int j
,
2428 struct isl_coalesce_info
*info
)
2431 isl_mat
*div_i
, *div_j
, *div
;
2435 enum isl_change change
;
2437 known
= isl_basic_map_divs_known(bmap_i
);
2438 if (known
< 0 || !known
)
2441 ctx
= isl_basic_map_get_ctx(bmap_i
);
2443 div_i
= isl_basic_map_get_divs(bmap_i
);
2444 div_j
= isl_basic_map_get_divs(info
[j
].bmap
);
2446 if (!div_i
|| !div_j
)
2449 exp1
= isl_alloc_array(ctx
, int, div_i
->n_row
);
2450 exp2
= isl_alloc_array(ctx
, int, div_j
->n_row
);
2451 if ((div_i
->n_row
&& !exp1
) || (div_j
->n_row
&& !exp2
))
2454 div
= isl_merge_divs(div_i
, div_j
, exp1
, exp2
);
2458 if (div
->n_row
== div_j
->n_row
)
2459 change
= coalesce_with_expanded_divs(bmap_i
,
2460 i
, j
, info
, div
, exp1
);
2462 change
= isl_change_none
;
2466 isl_mat_free(div_i
);
2467 isl_mat_free(div_j
);
2474 isl_mat_free(div_i
);
2475 isl_mat_free(div_j
);
2478 return isl_change_error
;
2481 /* Check if basic map "j" is a subset of basic map "i" after
2482 * exploiting the extra equalities of "j" to simplify the divs of "i".
2483 * If so, remove basic map "j" and return isl_change_drop_second.
2485 * If "j" does not have any equalities or if they are the same
2486 * as those of "i", then we cannot exploit them to simplify the divs.
2487 * Similarly, if there are no divs in "i", then they cannot be simplified.
2488 * If, on the other hand, the affine hulls of "i" and "j" do not intersect,
2489 * then "j" cannot be a subset of "i".
2491 * Otherwise, we intersect "i" with the affine hull of "j" and then
2492 * check if "j" is a subset of the result after aligning the divs.
2493 * If so, then "j" is definitely a subset of "i" and can be removed.
2494 * Note that if after intersection with the affine hull of "j".
2495 * "i" still has more divs than "j", then there is no way we can
2496 * align the divs of "i" to those of "j".
2498 static enum isl_change
coalesce_subset_with_equalities(int i
, int j
,
2499 struct isl_coalesce_info
*info
)
2501 isl_basic_map
*hull_i
, *hull_j
, *bmap_i
;
2503 enum isl_change change
;
2505 if (info
[j
].bmap
->n_eq
== 0)
2506 return isl_change_none
;
2507 if (info
[i
].bmap
->n_div
== 0)
2508 return isl_change_none
;
2510 hull_i
= isl_basic_map_copy(info
[i
].bmap
);
2511 hull_i
= isl_basic_map_plain_affine_hull(hull_i
);
2512 hull_j
= isl_basic_map_copy(info
[j
].bmap
);
2513 hull_j
= isl_basic_map_plain_affine_hull(hull_j
);
2515 hull_j
= isl_basic_map_intersect(hull_j
, isl_basic_map_copy(hull_i
));
2516 equal
= isl_basic_map_plain_is_equal(hull_i
, hull_j
);
2517 empty
= isl_basic_map_plain_is_empty(hull_j
);
2518 isl_basic_map_free(hull_i
);
2520 if (equal
< 0 || equal
|| empty
< 0 || empty
) {
2521 isl_basic_map_free(hull_j
);
2522 if (equal
< 0 || empty
< 0)
2523 return isl_change_error
;
2524 return isl_change_none
;
2527 bmap_i
= isl_basic_map_copy(info
[i
].bmap
);
2528 bmap_i
= isl_basic_map_intersect(bmap_i
, hull_j
);
2530 return isl_change_error
;
2532 if (bmap_i
->n_div
> info
[j
].bmap
->n_div
) {
2533 isl_basic_map_free(bmap_i
);
2534 return isl_change_none
;
2537 change
= coalesce_after_aligning_divs(bmap_i
, -1, j
, info
);
2539 isl_basic_map_free(bmap_i
);
2544 /* Check if the union of and the basic maps represented by info[i] and info[j]
2545 * can be represented by a single basic map, by aligning or equating
2546 * their integer divisions.
2547 * If so, replace the pair by the single basic map and return
2548 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
2549 * Otherwise, return isl_change_none.
2551 * Note that we only perform any test if the number of divs is different
2552 * in the two basic maps. In case the number of divs is the same,
2553 * we have already established that the divs are different
2554 * in the two basic maps.
2555 * In particular, if the number of divs of basic map i is smaller than
2556 * the number of divs of basic map j, then we check if j is a subset of i
2559 static enum isl_change
coalesce_divs(int i
, int j
,
2560 struct isl_coalesce_info
*info
)
2562 enum isl_change change
= isl_change_none
;
2564 if (info
[i
].bmap
->n_div
< info
[j
].bmap
->n_div
)
2565 change
= coalesce_after_aligning_divs(info
[i
].bmap
, i
, j
, info
);
2566 if (change
!= isl_change_none
)
2569 if (info
[j
].bmap
->n_div
< info
[i
].bmap
->n_div
)
2570 change
= coalesce_after_aligning_divs(info
[j
].bmap
, j
, i
, info
);
2571 if (change
!= isl_change_none
)
2572 return invert_change(change
);
2574 change
= coalesce_subset_with_equalities(i
, j
, info
);
2575 if (change
!= isl_change_none
)
2578 change
= coalesce_subset_with_equalities(j
, i
, info
);
2579 if (change
!= isl_change_none
)
2580 return invert_change(change
);
2582 return isl_change_none
;
2585 /* Does "bmap" involve any divs that themselves refer to divs?
2587 static int has_nested_div(__isl_keep isl_basic_map
*bmap
)
2593 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
2594 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
2597 for (i
= 0; i
< n_div
; ++i
)
2598 if (isl_seq_first_non_zero(bmap
->div
[i
] + 2 + total
,
2605 /* Return a list of affine expressions, one for each integer division
2606 * in "bmap_i". For each integer division that also appears in "bmap_j",
2607 * the affine expression is set to NaN. The number of NaNs in the list
2608 * is equal to the number of integer divisions in "bmap_j".
2609 * For the other integer divisions of "bmap_i", the corresponding
2610 * element in the list is a purely affine expression equal to the integer
2611 * division in "hull".
2612 * If no such list can be constructed, then the number of elements
2613 * in the returned list is smaller than the number of integer divisions
2616 static __isl_give isl_aff_list
*set_up_substitutions(
2617 __isl_keep isl_basic_map
*bmap_i
, __isl_keep isl_basic_map
*bmap_j
,
2618 __isl_take isl_basic_map
*hull
)
2620 unsigned n_div_i
, n_div_j
, total
;
2622 isl_local_space
*ls
;
2623 isl_basic_set
*wrap_hull
;
2631 ctx
= isl_basic_map_get_ctx(hull
);
2633 n_div_i
= isl_basic_map_dim(bmap_i
, isl_dim_div
);
2634 n_div_j
= isl_basic_map_dim(bmap_j
, isl_dim_div
);
2635 total
= isl_basic_map_total_dim(bmap_i
) - n_div_i
;
2637 ls
= isl_basic_map_get_local_space(bmap_i
);
2638 ls
= isl_local_space_wrap(ls
);
2639 wrap_hull
= isl_basic_map_wrap(hull
);
2641 aff_nan
= isl_aff_nan_on_domain(isl_local_space_copy(ls
));
2642 list
= isl_aff_list_alloc(ctx
, n_div_i
);
2645 for (i
= 0; i
< n_div_i
; ++i
) {
2649 isl_seq_eq(bmap_i
->div
[i
], bmap_j
->div
[j
], 2 + total
)) {
2651 list
= isl_aff_list_add(list
, isl_aff_copy(aff_nan
));
2654 if (n_div_i
- i
<= n_div_j
- j
)
2657 aff
= isl_local_space_get_div(ls
, i
);
2658 aff
= isl_aff_substitute_equalities(aff
,
2659 isl_basic_set_copy(wrap_hull
));
2660 aff
= isl_aff_floor(aff
);
2663 if (isl_aff_dim(aff
, isl_dim_div
) != 0) {
2668 list
= isl_aff_list_add(list
, aff
);
2671 isl_aff_free(aff_nan
);
2672 isl_local_space_free(ls
);
2673 isl_basic_set_free(wrap_hull
);
2677 isl_aff_free(aff_nan
);
2678 isl_local_space_free(ls
);
2679 isl_basic_set_free(wrap_hull
);
2680 isl_aff_list_free(list
);
2684 /* Add variables to info->bmap and info->tab corresponding to the elements
2685 * in "list" that are not set to NaN.
2686 * "extra_var" is the number of these elements.
2687 * "dim" is the offset in the variables of "tab" where we should
2688 * start considering the elements in "list".
2689 * When this function returns, the total number of variables in "tab"
2690 * is equal to "dim" plus the number of elements in "list".
2692 * The newly added existentially quantified variables are not given
2693 * an explicit representation because the corresponding div constraints
2694 * do not appear in info->bmap. These constraints are not added
2695 * to info->bmap because for internal consistency, they would need to
2696 * be added to info->tab as well, where they could combine with the equality
2697 * that is added later to result in constraints that do not hold
2698 * in the original input.
2700 static int add_sub_vars(struct isl_coalesce_info
*info
,
2701 __isl_keep isl_aff_list
*list
, int dim
, int extra_var
)
2706 space
= isl_basic_map_get_space(info
->bmap
);
2707 info
->bmap
= isl_basic_map_cow(info
->bmap
);
2708 info
->bmap
= isl_basic_map_extend_space(info
->bmap
, space
,
2712 n
= isl_aff_list_n_aff(list
);
2713 for (i
= 0; i
< n
; ++i
) {
2717 aff
= isl_aff_list_get_aff(list
, i
);
2718 is_nan
= isl_aff_is_nan(aff
);
2725 if (isl_tab_insert_var(info
->tab
, dim
+ i
) < 0)
2727 d
= isl_basic_map_alloc_div(info
->bmap
);
2730 info
->bmap
= isl_basic_map_mark_div_unknown(info
->bmap
, d
);
2733 for (j
= d
; j
> i
; --j
)
2734 isl_basic_map_swap_div(info
->bmap
, j
- 1, j
);
2740 /* For each element in "list" that is not set to NaN, fix the corresponding
2741 * variable in "tab" to the purely affine expression defined by the element.
2742 * "dim" is the offset in the variables of "tab" where we should
2743 * start considering the elements in "list".
2745 * This function assumes that a sufficient number of rows and
2746 * elements in the constraint array are available in the tableau.
2748 static int add_sub_equalities(struct isl_tab
*tab
,
2749 __isl_keep isl_aff_list
*list
, int dim
)
2756 n
= isl_aff_list_n_aff(list
);
2758 ctx
= isl_tab_get_ctx(tab
);
2759 sub
= isl_vec_alloc(ctx
, 1 + dim
+ n
);
2762 isl_seq_clr(sub
->el
+ 1 + dim
, n
);
2764 for (i
= 0; i
< n
; ++i
) {
2765 aff
= isl_aff_list_get_aff(list
, i
);
2768 if (isl_aff_is_nan(aff
)) {
2772 isl_seq_cpy(sub
->el
, aff
->v
->el
+ 1, 1 + dim
);
2773 isl_int_neg(sub
->el
[1 + dim
+ i
], aff
->v
->el
[0]);
2774 if (isl_tab_add_eq(tab
, sub
->el
) < 0)
2776 isl_int_set_si(sub
->el
[1 + dim
+ i
], 0);
2788 /* Add variables to info->tab and info->bmap corresponding to the elements
2789 * in "list" that are not set to NaN. The value of the added variable
2790 * in info->tab is fixed to the purely affine expression defined by the element.
2791 * "dim" is the offset in the variables of info->tab where we should
2792 * start considering the elements in "list".
2793 * When this function returns, the total number of variables in info->tab
2794 * is equal to "dim" plus the number of elements in "list".
2796 static int add_subs(struct isl_coalesce_info
*info
,
2797 __isl_keep isl_aff_list
*list
, int dim
)
2805 n
= isl_aff_list_n_aff(list
);
2806 extra_var
= n
- (info
->tab
->n_var
- dim
);
2808 if (isl_tab_extend_vars(info
->tab
, extra_var
) < 0)
2810 if (isl_tab_extend_cons(info
->tab
, 2 * extra_var
) < 0)
2812 if (add_sub_vars(info
, list
, dim
, extra_var
) < 0)
2815 return add_sub_equalities(info
->tab
, list
, dim
);
2818 /* Coalesce basic map "j" into basic map "i" after adding the extra integer
2819 * divisions in "i" but not in "j" to basic map "j", with values
2820 * specified by "list". The total number of elements in "list"
2821 * is equal to the number of integer divisions in "i", while the number
2822 * of NaN elements in the list is equal to the number of integer divisions
2825 * If no coalescing can be performed, then we need to revert basic map "j"
2826 * to its original state. We do the same if basic map "i" gets dropped
2827 * during the coalescing, even though this should not happen in practice
2828 * since we have already checked for "j" being a subset of "i"
2829 * before we reach this stage.
2831 static enum isl_change
coalesce_with_subs(int i
, int j
,
2832 struct isl_coalesce_info
*info
, __isl_keep isl_aff_list
*list
)
2834 isl_basic_map
*bmap_j
;
2835 struct isl_tab_undo
*snap
;
2837 enum isl_change change
;
2839 bmap_j
= isl_basic_map_copy(info
[j
].bmap
);
2840 snap
= isl_tab_snap(info
[j
].tab
);
2842 dim
= isl_basic_map_dim(bmap_j
, isl_dim_all
);
2843 dim
-= isl_basic_map_dim(bmap_j
, isl_dim_div
);
2844 if (add_subs(&info
[j
], list
, dim
) < 0)
2847 change
= coalesce_local_pair(i
, j
, info
);
2848 if (change
!= isl_change_none
&& change
!= isl_change_drop_first
) {
2849 isl_basic_map_free(bmap_j
);
2851 isl_basic_map_free(info
[j
].bmap
);
2852 info
[j
].bmap
= bmap_j
;
2854 if (isl_tab_rollback(info
[j
].tab
, snap
) < 0)
2855 return isl_change_error
;
2860 isl_basic_map_free(bmap_j
);
2861 return isl_change_error
;
2864 /* Check if we can coalesce basic map "j" into basic map "i" after copying
2865 * those extra integer divisions in "i" that can be simplified away
2866 * using the extra equalities in "j".
2867 * All divs are assumed to be known and not contain any nested divs.
2869 * We first check if there are any extra equalities in "j" that we
2870 * can exploit. Then we check if every integer division in "i"
2871 * either already appears in "j" or can be simplified using the
2872 * extra equalities to a purely affine expression.
2873 * If these tests succeed, then we try to coalesce the two basic maps
2874 * by introducing extra dimensions in "j" corresponding to
2875 * the extra integer divsisions "i" fixed to the corresponding
2876 * purely affine expression.
2878 static enum isl_change
check_coalesce_into_eq(int i
, int j
,
2879 struct isl_coalesce_info
*info
)
2881 unsigned n_div_i
, n_div_j
;
2882 isl_basic_map
*hull_i
, *hull_j
;
2885 enum isl_change change
;
2887 n_div_i
= isl_basic_map_dim(info
[i
].bmap
, isl_dim_div
);
2888 n_div_j
= isl_basic_map_dim(info
[j
].bmap
, isl_dim_div
);
2889 if (n_div_i
<= n_div_j
)
2890 return isl_change_none
;
2891 if (info
[j
].bmap
->n_eq
== 0)
2892 return isl_change_none
;
2894 hull_i
= isl_basic_map_copy(info
[i
].bmap
);
2895 hull_i
= isl_basic_map_plain_affine_hull(hull_i
);
2896 hull_j
= isl_basic_map_copy(info
[j
].bmap
);
2897 hull_j
= isl_basic_map_plain_affine_hull(hull_j
);
2899 hull_j
= isl_basic_map_intersect(hull_j
, isl_basic_map_copy(hull_i
));
2900 equal
= isl_basic_map_plain_is_equal(hull_i
, hull_j
);
2901 empty
= isl_basic_map_plain_is_empty(hull_j
);
2902 isl_basic_map_free(hull_i
);
2904 if (equal
< 0 || empty
< 0)
2906 if (equal
|| empty
) {
2907 isl_basic_map_free(hull_j
);
2908 return isl_change_none
;
2911 list
= set_up_substitutions(info
[i
].bmap
, info
[j
].bmap
, hull_j
);
2913 return isl_change_error
;
2914 if (isl_aff_list_n_aff(list
) < n_div_i
)
2915 change
= isl_change_none
;
2917 change
= coalesce_with_subs(i
, j
, info
, list
);
2919 isl_aff_list_free(list
);
2923 isl_basic_map_free(hull_j
);
2924 return isl_change_error
;
2927 /* Check if we can coalesce basic maps "i" and "j" after copying
2928 * those extra integer divisions in one of the basic maps that can
2929 * be simplified away using the extra equalities in the other basic map.
2930 * We require all divs to be known in both basic maps.
2931 * Furthermore, to simplify the comparison of div expressions,
2932 * we do not allow any nested integer divisions.
2934 static enum isl_change
check_coalesce_eq(int i
, int j
,
2935 struct isl_coalesce_info
*info
)
2938 enum isl_change change
;
2940 known
= isl_basic_map_divs_known(info
[i
].bmap
);
2941 if (known
< 0 || !known
)
2942 return known
< 0 ? isl_change_error
: isl_change_none
;
2943 known
= isl_basic_map_divs_known(info
[j
].bmap
);
2944 if (known
< 0 || !known
)
2945 return known
< 0 ? isl_change_error
: isl_change_none
;
2946 nested
= has_nested_div(info
[i
].bmap
);
2947 if (nested
< 0 || nested
)
2948 return nested
< 0 ? isl_change_error
: isl_change_none
;
2949 nested
= has_nested_div(info
[j
].bmap
);
2950 if (nested
< 0 || nested
)
2951 return nested
< 0 ? isl_change_error
: isl_change_none
;
2953 change
= check_coalesce_into_eq(i
, j
, info
);
2954 if (change
!= isl_change_none
)
2956 change
= check_coalesce_into_eq(j
, i
, info
);
2957 if (change
!= isl_change_none
)
2958 return invert_change(change
);
2960 return isl_change_none
;
2963 /* Check if the union of the given pair of basic maps
2964 * can be represented by a single basic map.
2965 * If so, replace the pair by the single basic map and return
2966 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
2967 * Otherwise, return isl_change_none.
2969 * We first check if the two basic maps live in the same local space,
2970 * after aligning the divs that differ by only an integer constant.
2971 * If so, we do the complete check. Otherwise, we check if they have
2972 * the same number of integer divisions and can be coalesced, if one is
2973 * an obvious subset of the other or if the extra integer divisions
2974 * of one basic map can be simplified away using the extra equalities
2975 * of the other basic map.
2977 static enum isl_change
coalesce_pair(int i
, int j
,
2978 struct isl_coalesce_info
*info
)
2981 enum isl_change change
;
2983 if (harmonize_divs(&info
[i
], &info
[j
]) < 0)
2984 return isl_change_error
;
2985 same
= same_divs(info
[i
].bmap
, info
[j
].bmap
);
2987 return isl_change_error
;
2989 return coalesce_local_pair(i
, j
, info
);
2991 if (info
[i
].bmap
->n_div
== info
[j
].bmap
->n_div
) {
2992 change
= coalesce_local_pair(i
, j
, info
);
2993 if (change
!= isl_change_none
)
2997 change
= coalesce_divs(i
, j
, info
);
2998 if (change
!= isl_change_none
)
3001 return check_coalesce_eq(i
, j
, info
);
3004 /* Return the maximum of "a" and "b".
3006 static int isl_max(int a
, int b
)
3008 return a
> b
? a
: b
;
3011 /* Pairwise coalesce the basic maps in the range [start1, end1[ of "info"
3012 * with those in the range [start2, end2[, skipping basic maps
3013 * that have been removed (either before or within this function).
3015 * For each basic map i in the first range, we check if it can be coalesced
3016 * with respect to any previously considered basic map j in the second range.
3017 * If i gets dropped (because it was a subset of some j), then
3018 * we can move on to the next basic map.
3019 * If j gets dropped, we need to continue checking against the other
3020 * previously considered basic maps.
3021 * If the two basic maps got fused, then we recheck the fused basic map
3022 * against the previously considered basic maps, starting at i + 1
3023 * (even if start2 is greater than i + 1).
3025 static int coalesce_range(isl_ctx
*ctx
, struct isl_coalesce_info
*info
,
3026 int start1
, int end1
, int start2
, int end2
)
3030 for (i
= end1
- 1; i
>= start1
; --i
) {
3031 if (info
[i
].removed
)
3033 for (j
= isl_max(i
+ 1, start2
); j
< end2
; ++j
) {
3034 enum isl_change changed
;
3036 if (info
[j
].removed
)
3038 if (info
[i
].removed
)
3039 isl_die(ctx
, isl_error_internal
,
3040 "basic map unexpectedly removed",
3042 changed
= coalesce_pair(i
, j
, info
);
3044 case isl_change_error
:
3046 case isl_change_none
:
3047 case isl_change_drop_second
:
3049 case isl_change_drop_first
:
3052 case isl_change_fuse
:
3062 /* Pairwise coalesce the basic maps described by the "n" elements of "info".
3064 * We consider groups of basic maps that live in the same apparent
3065 * affine hull and we first coalesce within such a group before we
3066 * coalesce the elements in the group with elements of previously
3067 * considered groups. If a fuse happens during the second phase,
3068 * then we also reconsider the elements within the group.
3070 static int coalesce(isl_ctx
*ctx
, int n
, struct isl_coalesce_info
*info
)
3074 for (end
= n
; end
> 0; end
= start
) {
3076 while (start
>= 1 &&
3077 info
[start
- 1].hull_hash
== info
[start
].hull_hash
)
3079 if (coalesce_range(ctx
, info
, start
, end
, start
, end
) < 0)
3081 if (coalesce_range(ctx
, info
, start
, end
, end
, n
) < 0)
3088 /* Update the basic maps in "map" based on the information in "info".
3089 * In particular, remove the basic maps that have been marked removed and
3090 * update the others based on the information in the corresponding tableau.
3091 * Since we detected implicit equalities without calling
3092 * isl_basic_map_gauss, we need to do it now.
3093 * Also call isl_basic_map_simplify if we may have lost the definition
3094 * of one or more integer divisions.
3096 static __isl_give isl_map
*update_basic_maps(__isl_take isl_map
*map
,
3097 int n
, struct isl_coalesce_info
*info
)
3104 for (i
= n
- 1; i
>= 0; --i
) {
3105 if (info
[i
].removed
) {
3106 isl_basic_map_free(map
->p
[i
]);
3107 if (i
!= map
->n
- 1)
3108 map
->p
[i
] = map
->p
[map
->n
- 1];
3113 info
[i
].bmap
= isl_basic_map_update_from_tab(info
[i
].bmap
,
3115 info
[i
].bmap
= isl_basic_map_gauss(info
[i
].bmap
, NULL
);
3116 if (info
[i
].simplify
)
3117 info
[i
].bmap
= isl_basic_map_simplify(info
[i
].bmap
);
3118 info
[i
].bmap
= isl_basic_map_finalize(info
[i
].bmap
);
3120 return isl_map_free(map
);
3121 ISL_F_SET(info
[i
].bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
3122 ISL_F_SET(info
[i
].bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
3123 isl_basic_map_free(map
->p
[i
]);
3124 map
->p
[i
] = info
[i
].bmap
;
3125 info
[i
].bmap
= NULL
;
3131 /* For each pair of basic maps in the map, check if the union of the two
3132 * can be represented by a single basic map.
3133 * If so, replace the pair by the single basic map and start over.
3135 * We factor out any (hidden) common factor from the constraint
3136 * coefficients to improve the detection of adjacent constraints.
3138 * Since we are constructing the tableaus of the basic maps anyway,
3139 * we exploit them to detect implicit equalities and redundant constraints.
3140 * This also helps the coalescing as it can ignore the redundant constraints.
3141 * In order to avoid confusion, we make all implicit equalities explicit
3142 * in the basic maps. We don't call isl_basic_map_gauss, though,
3143 * as that may affect the number of constraints.
3144 * This means that we have to call isl_basic_map_gauss at the end
3145 * of the computation (in update_basic_maps) to ensure that
3146 * the basic maps are not left in an unexpected state.
3147 * For each basic map, we also compute the hash of the apparent affine hull
3148 * for use in coalesce.
3150 struct isl_map
*isl_map_coalesce(struct isl_map
*map
)
3155 struct isl_coalesce_info
*info
= NULL
;
3157 map
= isl_map_remove_empty_parts(map
);
3164 ctx
= isl_map_get_ctx(map
);
3165 map
= isl_map_sort_divs(map
);
3166 map
= isl_map_cow(map
);
3173 info
= isl_calloc_array(map
->ctx
, struct isl_coalesce_info
, n
);
3177 for (i
= 0; i
< map
->n
; ++i
) {
3178 map
->p
[i
] = isl_basic_map_reduce_coefficients(map
->p
[i
]);
3181 info
[i
].bmap
= isl_basic_map_copy(map
->p
[i
]);
3182 info
[i
].tab
= isl_tab_from_basic_map(info
[i
].bmap
, 0);
3185 if (!ISL_F_ISSET(info
[i
].bmap
, ISL_BASIC_MAP_NO_IMPLICIT
))
3186 if (isl_tab_detect_implicit_equalities(info
[i
].tab
) < 0)
3188 info
[i
].bmap
= isl_tab_make_equalities_explicit(info
[i
].tab
,
3192 if (!ISL_F_ISSET(info
[i
].bmap
, ISL_BASIC_MAP_NO_REDUNDANT
))
3193 if (isl_tab_detect_redundant(info
[i
].tab
) < 0)
3195 if (coalesce_info_set_hull_hash(&info
[i
]) < 0)
3198 for (i
= map
->n
- 1; i
>= 0; --i
)
3199 if (info
[i
].tab
->empty
)
3202 if (coalesce(ctx
, n
, info
) < 0)
3205 map
= update_basic_maps(map
, n
, info
);
3207 clear_coalesce_info(n
, info
);
3211 clear_coalesce_info(n
, info
);
3216 /* For each pair of basic sets in the set, check if the union of the two
3217 * can be represented by a single basic set.
3218 * If so, replace the pair by the single basic set and start over.
3220 struct isl_set
*isl_set_coalesce(struct isl_set
*set
)
3222 return set_from_map(isl_map_coalesce(set_to_map(set
)));