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_val_private.h>
29 #include <isl_vec_private.h>
30 #include <isl_aff_private.h>
31 #include <isl_equalities.h>
32 #include <isl_constraint_private.h>
34 #include <set_to_map.c>
35 #include <set_from_map.c>
37 #define STATUS_ERROR -1
38 #define STATUS_REDUNDANT 1
39 #define STATUS_VALID 2
40 #define STATUS_SEPARATE 3
42 #define STATUS_ADJ_EQ 5
43 #define STATUS_ADJ_INEQ 6
45 static int status_in(isl_int
*ineq
, struct isl_tab
*tab
)
47 enum isl_ineq_type type
= isl_tab_ineq_type(tab
, ineq
);
50 case isl_ineq_error
: return STATUS_ERROR
;
51 case isl_ineq_redundant
: return STATUS_VALID
;
52 case isl_ineq_separate
: return STATUS_SEPARATE
;
53 case isl_ineq_cut
: return STATUS_CUT
;
54 case isl_ineq_adj_eq
: return STATUS_ADJ_EQ
;
55 case isl_ineq_adj_ineq
: return STATUS_ADJ_INEQ
;
59 /* Compute the position of the equalities of basic map "bmap_i"
60 * with respect to the basic map represented by "tab_j".
61 * The resulting array has twice as many entries as the number
62 * of equalities corresponding to the two inequalities to which
63 * each equality corresponds.
65 static int *eq_status_in(__isl_keep isl_basic_map
*bmap_i
,
66 struct isl_tab
*tab_j
)
72 dim
= isl_basic_map_dim(bmap_i
, isl_dim_all
);
76 eq
= isl_calloc_array(bmap_i
->ctx
, int, 2 * bmap_i
->n_eq
);
80 for (k
= 0; k
< bmap_i
->n_eq
; ++k
) {
81 for (l
= 0; l
< 2; ++l
) {
82 isl_seq_neg(bmap_i
->eq
[k
], bmap_i
->eq
[k
], 1+dim
);
83 eq
[2 * k
+ l
] = status_in(bmap_i
->eq
[k
], tab_j
);
84 if (eq
[2 * k
+ l
] == STATUS_ERROR
)
95 /* Compute the position of the inequalities of basic map "bmap_i"
96 * (also represented by "tab_i", if not NULL) with respect to the basic map
97 * represented by "tab_j".
99 static int *ineq_status_in(__isl_keep isl_basic_map
*bmap_i
,
100 struct isl_tab
*tab_i
, struct isl_tab
*tab_j
)
103 unsigned n_eq
= bmap_i
->n_eq
;
104 int *ineq
= isl_calloc_array(bmap_i
->ctx
, int, bmap_i
->n_ineq
);
109 for (k
= 0; k
< bmap_i
->n_ineq
; ++k
) {
110 if (tab_i
&& isl_tab_is_redundant(tab_i
, n_eq
+ k
)) {
111 ineq
[k
] = STATUS_REDUNDANT
;
114 ineq
[k
] = status_in(bmap_i
->ineq
[k
], tab_j
);
115 if (ineq
[k
] == STATUS_ERROR
)
117 if (ineq
[k
] == STATUS_SEPARATE
)
127 static int any(int *con
, unsigned len
, int status
)
131 for (i
= 0; i
< len
; ++i
)
132 if (con
[i
] == status
)
137 /* Return the first position of "status" in the list "con" of length "len".
138 * Return -1 if there is no such entry.
140 static int find(int *con
, unsigned len
, int status
)
144 for (i
= 0; i
< len
; ++i
)
145 if (con
[i
] == status
)
150 static int count(int *con
, unsigned len
, int status
)
155 for (i
= 0; i
< len
; ++i
)
156 if (con
[i
] == status
)
161 static int all(int *con
, unsigned len
, int status
)
165 for (i
= 0; i
< len
; ++i
) {
166 if (con
[i
] == STATUS_REDUNDANT
)
168 if (con
[i
] != status
)
174 /* Internal information associated to a basic map in a map
175 * that is to be coalesced by isl_map_coalesce.
177 * "bmap" is the basic map itself (or NULL if "removed" is set)
178 * "tab" is the corresponding tableau (or NULL if "removed" is set)
179 * "hull_hash" identifies the affine space in which "bmap" lives.
180 * "removed" is set if this basic map has been removed from the map
181 * "simplify" is set if this basic map may have some unknown integer
182 * divisions that were not present in the input basic maps. The basic
183 * map should then be simplified such that we may be able to find
184 * a definition among the constraints.
186 * "eq" and "ineq" are only set if we are currently trying to coalesce
187 * this basic map with another basic map, in which case they represent
188 * the position of the inequalities of this basic map with respect to
189 * the other basic map. The number of elements in the "eq" array
190 * is twice the number of equalities in the "bmap", corresponding
191 * to the two inequalities that make up each equality.
193 struct isl_coalesce_info
{
203 /* Is there any (half of an) equality constraint in the description
204 * of the basic map represented by "info" that
205 * has position "status" with respect to the other basic map?
207 static int any_eq(struct isl_coalesce_info
*info
, int status
)
211 n_eq
= isl_basic_map_n_equality(info
->bmap
);
212 return any(info
->eq
, 2 * n_eq
, status
);
215 /* Is there any inequality constraint in the description
216 * of the basic map represented by "info" that
217 * has position "status" with respect to the other basic map?
219 static int any_ineq(struct isl_coalesce_info
*info
, int status
)
223 n_ineq
= isl_basic_map_n_inequality(info
->bmap
);
224 return any(info
->ineq
, n_ineq
, status
);
227 /* Return the position of the first half on an equality constraint
228 * in the description of the basic map represented by "info" that
229 * has position "status" with respect to the other basic map.
230 * The returned value is twice the position of the equality constraint
231 * plus zero for the negative half and plus one for the positive half.
232 * Return -1 if there is no such entry.
234 static int find_eq(struct isl_coalesce_info
*info
, int status
)
238 n_eq
= isl_basic_map_n_equality(info
->bmap
);
239 return find(info
->eq
, 2 * n_eq
, status
);
242 /* Return the position of the first inequality constraint in the description
243 * of the basic map represented by "info" that
244 * has position "status" with respect to the other basic map.
245 * Return -1 if there is no such entry.
247 static int find_ineq(struct isl_coalesce_info
*info
, int status
)
251 n_ineq
= isl_basic_map_n_inequality(info
->bmap
);
252 return find(info
->ineq
, n_ineq
, status
);
255 /* Return the number of (halves of) equality constraints in the description
256 * of the basic map represented by "info" that
257 * have position "status" with respect to the other basic map.
259 static int count_eq(struct isl_coalesce_info
*info
, int status
)
263 n_eq
= isl_basic_map_n_equality(info
->bmap
);
264 return count(info
->eq
, 2 * n_eq
, status
);
267 /* Return the number of inequality constraints in the description
268 * of the basic map represented by "info" that
269 * have position "status" with respect to the other basic map.
271 static int count_ineq(struct isl_coalesce_info
*info
, int status
)
275 n_ineq
= isl_basic_map_n_inequality(info
->bmap
);
276 return count(info
->ineq
, n_ineq
, status
);
279 /* Are all non-redundant constraints of the basic map represented by "info"
280 * either valid or cut constraints with respect to the other basic map?
282 static int all_valid_or_cut(struct isl_coalesce_info
*info
)
286 for (i
= 0; i
< 2 * info
->bmap
->n_eq
; ++i
) {
287 if (info
->eq
[i
] == STATUS_REDUNDANT
)
289 if (info
->eq
[i
] == STATUS_VALID
)
291 if (info
->eq
[i
] == STATUS_CUT
)
296 for (i
= 0; i
< info
->bmap
->n_ineq
; ++i
) {
297 if (info
->ineq
[i
] == STATUS_REDUNDANT
)
299 if (info
->ineq
[i
] == STATUS_VALID
)
301 if (info
->ineq
[i
] == STATUS_CUT
)
309 /* Compute the hash of the (apparent) affine hull of info->bmap (with
310 * the existentially quantified variables removed) and store it
313 static int coalesce_info_set_hull_hash(struct isl_coalesce_info
*info
)
318 hull
= isl_basic_map_copy(info
->bmap
);
319 hull
= isl_basic_map_plain_affine_hull(hull
);
320 n_div
= isl_basic_map_dim(hull
, isl_dim_div
);
322 hull
= isl_basic_map_free(hull
);
323 hull
= isl_basic_map_drop_constraints_involving_dims(hull
,
324 isl_dim_div
, 0, n_div
);
325 info
->hull_hash
= isl_basic_map_get_hash(hull
);
326 isl_basic_map_free(hull
);
328 return hull
? 0 : -1;
331 /* Free all the allocated memory in an array
332 * of "n" isl_coalesce_info elements.
334 static void clear_coalesce_info(int n
, struct isl_coalesce_info
*info
)
341 for (i
= 0; i
< n
; ++i
) {
342 isl_basic_map_free(info
[i
].bmap
);
343 isl_tab_free(info
[i
].tab
);
349 /* Clear the memory associated to "info".
351 static void clear(struct isl_coalesce_info
*info
)
353 info
->bmap
= isl_basic_map_free(info
->bmap
);
354 isl_tab_free(info
->tab
);
358 /* Drop the basic map represented by "info".
359 * That is, clear the memory associated to the entry and
360 * mark it as having been removed.
362 static void drop(struct isl_coalesce_info
*info
)
368 /* Exchange the information in "info1" with that in "info2".
370 static void exchange(struct isl_coalesce_info
*info1
,
371 struct isl_coalesce_info
*info2
)
373 struct isl_coalesce_info info
;
380 /* This type represents the kind of change that has been performed
381 * while trying to coalesce two basic maps.
383 * isl_change_none: nothing was changed
384 * isl_change_drop_first: the first basic map was removed
385 * isl_change_drop_second: the second basic map was removed
386 * isl_change_fuse: the two basic maps were replaced by a new basic map.
389 isl_change_error
= -1,
391 isl_change_drop_first
,
392 isl_change_drop_second
,
396 /* Update "change" based on an interchange of the first and the second
397 * basic map. That is, interchange isl_change_drop_first and
398 * isl_change_drop_second.
400 static enum isl_change
invert_change(enum isl_change change
)
403 case isl_change_error
:
404 return isl_change_error
;
405 case isl_change_none
:
406 return isl_change_none
;
407 case isl_change_drop_first
:
408 return isl_change_drop_second
;
409 case isl_change_drop_second
:
410 return isl_change_drop_first
;
411 case isl_change_fuse
:
412 return isl_change_fuse
;
415 return isl_change_error
;
418 /* Add the valid constraints of the basic map represented by "info"
419 * to "bmap". "len" is the size of the constraints.
420 * If only one of the pair of inequalities that make up an equality
421 * is valid, then add that inequality.
423 static __isl_give isl_basic_map
*add_valid_constraints(
424 __isl_take isl_basic_map
*bmap
, struct isl_coalesce_info
*info
,
432 for (k
= 0; k
< info
->bmap
->n_eq
; ++k
) {
433 if (info
->eq
[2 * k
] == STATUS_VALID
&&
434 info
->eq
[2 * k
+ 1] == STATUS_VALID
) {
435 l
= isl_basic_map_alloc_equality(bmap
);
437 return isl_basic_map_free(bmap
);
438 isl_seq_cpy(bmap
->eq
[l
], info
->bmap
->eq
[k
], len
);
439 } else if (info
->eq
[2 * k
] == STATUS_VALID
) {
440 l
= isl_basic_map_alloc_inequality(bmap
);
442 return isl_basic_map_free(bmap
);
443 isl_seq_neg(bmap
->ineq
[l
], info
->bmap
->eq
[k
], len
);
444 } else if (info
->eq
[2 * k
+ 1] == STATUS_VALID
) {
445 l
= isl_basic_map_alloc_inequality(bmap
);
447 return isl_basic_map_free(bmap
);
448 isl_seq_cpy(bmap
->ineq
[l
], info
->bmap
->eq
[k
], len
);
452 for (k
= 0; k
< info
->bmap
->n_ineq
; ++k
) {
453 if (info
->ineq
[k
] != STATUS_VALID
)
455 l
= isl_basic_map_alloc_inequality(bmap
);
457 return isl_basic_map_free(bmap
);
458 isl_seq_cpy(bmap
->ineq
[l
], info
->bmap
->ineq
[k
], len
);
464 /* Is "bmap" defined by a number of (non-redundant) constraints that
465 * is greater than the number of constraints of basic maps i and j combined?
466 * Equalities are counted as two inequalities.
468 static int number_of_constraints_increases(int i
, int j
,
469 struct isl_coalesce_info
*info
,
470 __isl_keep isl_basic_map
*bmap
, struct isl_tab
*tab
)
474 n_old
= 2 * info
[i
].bmap
->n_eq
+ info
[i
].bmap
->n_ineq
;
475 n_old
+= 2 * info
[j
].bmap
->n_eq
+ info
[j
].bmap
->n_ineq
;
477 n_new
= 2 * bmap
->n_eq
;
478 for (k
= 0; k
< bmap
->n_ineq
; ++k
)
479 if (!isl_tab_is_redundant(tab
, bmap
->n_eq
+ k
))
482 return n_new
> n_old
;
485 /* Replace the pair of basic maps i and j by the basic map bounded
486 * by the valid constraints in both basic maps and the constraints
487 * in extra (if not NULL).
488 * Place the fused basic map in the position that is the smallest of i and j.
490 * If "detect_equalities" is set, then look for equalities encoded
491 * as pairs of inequalities.
492 * If "check_number" is set, then the original basic maps are only
493 * replaced if the total number of constraints does not increase.
494 * While the number of integer divisions in the two basic maps
495 * is assumed to be the same, the actual definitions may be different.
496 * We only copy the definition from one of the basic map if it is
497 * the same as that of the other basic map. Otherwise, we mark
498 * the integer division as unknown and simplify the basic map
499 * in an attempt to recover the integer division definition.
501 static enum isl_change
fuse(int i
, int j
, struct isl_coalesce_info
*info
,
502 __isl_keep isl_mat
*extra
, int detect_equalities
, int check_number
)
505 struct isl_basic_map
*fused
= NULL
;
506 struct isl_tab
*fused_tab
= NULL
;
507 isl_size total
= isl_basic_map_dim(info
[i
].bmap
, isl_dim_all
);
508 unsigned extra_rows
= extra
? extra
->n_row
: 0;
509 unsigned n_eq
, n_ineq
;
513 return isl_change_error
;
515 return fuse(j
, i
, info
, extra
, detect_equalities
, check_number
);
517 n_eq
= info
[i
].bmap
->n_eq
+ info
[j
].bmap
->n_eq
;
518 n_ineq
= info
[i
].bmap
->n_ineq
+ info
[j
].bmap
->n_ineq
;
519 fused
= isl_basic_map_alloc_space(isl_space_copy(info
[i
].bmap
->dim
),
520 info
[i
].bmap
->n_div
, n_eq
, n_eq
+ n_ineq
+ extra_rows
);
521 fused
= add_valid_constraints(fused
, &info
[i
], 1 + total
);
522 fused
= add_valid_constraints(fused
, &info
[j
], 1 + total
);
525 if (ISL_F_ISSET(info
[i
].bmap
, ISL_BASIC_MAP_RATIONAL
) &&
526 ISL_F_ISSET(info
[j
].bmap
, ISL_BASIC_MAP_RATIONAL
))
527 ISL_F_SET(fused
, ISL_BASIC_MAP_RATIONAL
);
529 for (k
= 0; k
< info
[i
].bmap
->n_div
; ++k
) {
530 int l
= isl_basic_map_alloc_div(fused
);
533 if (isl_seq_eq(info
[i
].bmap
->div
[k
], info
[j
].bmap
->div
[k
],
535 isl_seq_cpy(fused
->div
[l
], info
[i
].bmap
->div
[k
],
538 isl_int_set_si(fused
->div
[l
][0], 0);
543 for (k
= 0; k
< extra_rows
; ++k
) {
544 l
= isl_basic_map_alloc_inequality(fused
);
547 isl_seq_cpy(fused
->ineq
[l
], extra
->row
[k
], 1 + total
);
550 if (detect_equalities
)
551 fused
= isl_basic_map_detect_inequality_pairs(fused
, NULL
);
552 fused
= isl_basic_map_gauss(fused
, NULL
);
553 if (simplify
|| info
[j
].simplify
) {
554 fused
= isl_basic_map_simplify(fused
);
555 info
[i
].simplify
= 0;
557 fused
= isl_basic_map_finalize(fused
);
559 fused_tab
= isl_tab_from_basic_map(fused
, 0);
560 if (isl_tab_detect_redundant(fused_tab
) < 0)
564 number_of_constraints_increases(i
, j
, info
, fused
, fused_tab
)) {
565 isl_tab_free(fused_tab
);
566 isl_basic_map_free(fused
);
567 return isl_change_none
;
571 info
[i
].bmap
= fused
;
572 info
[i
].tab
= fused_tab
;
575 return isl_change_fuse
;
577 isl_tab_free(fused_tab
);
578 isl_basic_map_free(fused
);
579 return isl_change_error
;
582 /* Given a pair of basic maps i and j such that all constraints are either
583 * "valid" or "cut", check if the facets corresponding to the "cut"
584 * constraints of i lie entirely within basic map j.
585 * If so, replace the pair by the basic map consisting of the valid
586 * constraints in both basic maps.
587 * Checking whether the facet lies entirely within basic map j
588 * is performed by checking whether the constraints of basic map j
589 * are valid for the facet. These tests are performed on a rational
590 * tableau to avoid the theoretical possibility that a constraint
591 * that was considered to be a cut constraint for the entire basic map i
592 * happens to be considered to be a valid constraint for the facet,
593 * even though it cuts off the same rational points.
595 * To see that we are not introducing any extra points, call the
596 * two basic maps A and B and the resulting map U and let x
597 * be an element of U \setminus ( A \cup B ).
598 * A line connecting x with an element of A \cup B meets a facet F
599 * of either A or B. Assume it is a facet of B and let c_1 be
600 * the corresponding facet constraint. We have c_1(x) < 0 and
601 * so c_1 is a cut constraint. This implies that there is some
602 * (possibly rational) point x' satisfying the constraints of A
603 * and the opposite of c_1 as otherwise c_1 would have been marked
604 * valid for A. The line connecting x and x' meets a facet of A
605 * in a (possibly rational) point that also violates c_1, but this
606 * is impossible since all cut constraints of B are valid for all
608 * In case F is a facet of A rather than B, then we can apply the
609 * above reasoning to find a facet of B separating x from A \cup B first.
611 static enum isl_change
check_facets(int i
, int j
,
612 struct isl_coalesce_info
*info
)
615 struct isl_tab_undo
*snap
, *snap2
;
616 unsigned n_eq
= info
[i
].bmap
->n_eq
;
618 snap
= isl_tab_snap(info
[i
].tab
);
619 if (isl_tab_mark_rational(info
[i
].tab
) < 0)
620 return isl_change_error
;
621 snap2
= isl_tab_snap(info
[i
].tab
);
623 for (k
= 0; k
< info
[i
].bmap
->n_ineq
; ++k
) {
624 if (info
[i
].ineq
[k
] != STATUS_CUT
)
626 if (isl_tab_select_facet(info
[i
].tab
, n_eq
+ k
) < 0)
627 return isl_change_error
;
628 for (l
= 0; l
< info
[j
].bmap
->n_ineq
; ++l
) {
630 if (info
[j
].ineq
[l
] != STATUS_CUT
)
632 stat
= status_in(info
[j
].bmap
->ineq
[l
], info
[i
].tab
);
634 return isl_change_error
;
635 if (stat
!= STATUS_VALID
)
638 if (isl_tab_rollback(info
[i
].tab
, snap2
) < 0)
639 return isl_change_error
;
640 if (l
< info
[j
].bmap
->n_ineq
)
644 if (k
< info
[i
].bmap
->n_ineq
) {
645 if (isl_tab_rollback(info
[i
].tab
, snap
) < 0)
646 return isl_change_error
;
647 return isl_change_none
;
649 return fuse(i
, j
, info
, NULL
, 0, 0);
652 /* Check if info->bmap contains the basic map represented
653 * by the tableau "tab".
654 * For each equality, we check both the constraint itself
655 * (as an inequality) and its negation. Make sure the
656 * equality is returned to its original state before returning.
658 static isl_bool
contains(struct isl_coalesce_info
*info
, struct isl_tab
*tab
)
662 isl_basic_map
*bmap
= info
->bmap
;
664 dim
= isl_basic_map_dim(bmap
, isl_dim_all
);
666 return isl_bool_error
;
667 for (k
= 0; k
< bmap
->n_eq
; ++k
) {
669 isl_seq_neg(bmap
->eq
[k
], bmap
->eq
[k
], 1 + dim
);
670 stat
= status_in(bmap
->eq
[k
], tab
);
671 isl_seq_neg(bmap
->eq
[k
], bmap
->eq
[k
], 1 + dim
);
673 return isl_bool_error
;
674 if (stat
!= STATUS_VALID
)
675 return isl_bool_false
;
676 stat
= status_in(bmap
->eq
[k
], tab
);
678 return isl_bool_error
;
679 if (stat
!= STATUS_VALID
)
680 return isl_bool_false
;
683 for (k
= 0; k
< bmap
->n_ineq
; ++k
) {
685 if (info
->ineq
[k
] == STATUS_REDUNDANT
)
687 stat
= status_in(bmap
->ineq
[k
], tab
);
689 return isl_bool_error
;
690 if (stat
!= STATUS_VALID
)
691 return isl_bool_false
;
693 return isl_bool_true
;
696 /* Basic map "i" has an inequality (say "k") that is adjacent
697 * to some inequality of basic map "j". All the other inequalities
699 * Check if basic map "j" forms an extension of basic map "i".
701 * Note that this function is only called if some of the equalities or
702 * inequalities of basic map "j" do cut basic map "i". The function is
703 * correct even if there are no such cut constraints, but in that case
704 * the additional checks performed by this function are overkill.
706 * In particular, we replace constraint k, say f >= 0, by constraint
707 * f <= -1, add the inequalities of "j" that are valid for "i"
708 * and check if the result is a subset of basic map "j".
709 * To improve the chances of the subset relation being detected,
710 * any variable that only attains a single integer value
711 * in the tableau of "i" is first fixed to that value.
712 * If the result is a subset, then we know that this result is exactly equal
713 * to basic map "j" since all its constraints are valid for basic map "j".
714 * By combining the valid constraints of "i" (all equalities and all
715 * inequalities except "k") and the valid constraints of "j" we therefore
716 * obtain a basic map that is equal to their union.
717 * In this case, there is no need to perform a rollback of the tableau
718 * since it is going to be destroyed in fuse().
724 * |_______| _ |_________\
736 static enum isl_change
is_adj_ineq_extension(int i
, int j
,
737 struct isl_coalesce_info
*info
)
740 struct isl_tab_undo
*snap
;
741 unsigned n_eq
= info
[i
].bmap
->n_eq
;
742 isl_size total
= isl_basic_map_dim(info
[i
].bmap
, isl_dim_all
);
747 return isl_change_error
;
748 if (isl_tab_extend_cons(info
[i
].tab
, 1 + info
[j
].bmap
->n_ineq
) < 0)
749 return isl_change_error
;
751 k
= find_ineq(&info
[i
], STATUS_ADJ_INEQ
);
753 isl_die(isl_basic_map_get_ctx(info
[i
].bmap
), isl_error_internal
,
754 "info[i].ineq should have exactly one STATUS_ADJ_INEQ",
755 return isl_change_error
);
757 snap
= isl_tab_snap(info
[i
].tab
);
759 if (isl_tab_unrestrict(info
[i
].tab
, n_eq
+ k
) < 0)
760 return isl_change_error
;
762 isl_seq_neg(info
[i
].bmap
->ineq
[k
], info
[i
].bmap
->ineq
[k
], 1 + total
);
763 isl_int_sub_ui(info
[i
].bmap
->ineq
[k
][0], info
[i
].bmap
->ineq
[k
][0], 1);
764 r
= isl_tab_add_ineq(info
[i
].tab
, info
[i
].bmap
->ineq
[k
]);
765 isl_seq_neg(info
[i
].bmap
->ineq
[k
], info
[i
].bmap
->ineq
[k
], 1 + total
);
766 isl_int_sub_ui(info
[i
].bmap
->ineq
[k
][0], info
[i
].bmap
->ineq
[k
][0], 1);
768 return isl_change_error
;
770 for (k
= 0; k
< info
[j
].bmap
->n_ineq
; ++k
) {
771 if (info
[j
].ineq
[k
] != STATUS_VALID
)
773 if (isl_tab_add_ineq(info
[i
].tab
, info
[j
].bmap
->ineq
[k
]) < 0)
774 return isl_change_error
;
776 if (isl_tab_detect_constants(info
[i
].tab
) < 0)
777 return isl_change_error
;
779 super
= contains(&info
[j
], info
[i
].tab
);
781 return isl_change_error
;
783 return fuse(i
, j
, info
, NULL
, 0, 0);
785 if (isl_tab_rollback(info
[i
].tab
, snap
) < 0)
786 return isl_change_error
;
788 return isl_change_none
;
792 /* Both basic maps have at least one inequality with and adjacent
793 * (but opposite) inequality in the other basic map.
794 * Check that there are no cut constraints and that there is only
795 * a single pair of adjacent inequalities.
796 * If so, we can replace the pair by a single basic map described
797 * by all but the pair of adjacent inequalities.
798 * Any additional points introduced lie strictly between the two
799 * adjacent hyperplanes and can therefore be integral.
808 * The test for a single pair of adjancent inequalities is important
809 * for avoiding the combination of two basic maps like the following
819 * If there are some cut constraints on one side, then we may
820 * still be able to fuse the two basic maps, but we need to perform
821 * some additional checks in is_adj_ineq_extension.
823 static enum isl_change
check_adj_ineq(int i
, int j
,
824 struct isl_coalesce_info
*info
)
826 int count_i
, count_j
;
829 count_i
= count_ineq(&info
[i
], STATUS_ADJ_INEQ
);
830 count_j
= count_ineq(&info
[j
], STATUS_ADJ_INEQ
);
832 if (count_i
!= 1 && count_j
!= 1)
833 return isl_change_none
;
835 cut_i
= any_eq(&info
[i
], STATUS_CUT
) || any_ineq(&info
[i
], STATUS_CUT
);
836 cut_j
= any_eq(&info
[j
], STATUS_CUT
) || any_ineq(&info
[j
], STATUS_CUT
);
838 if (!cut_i
&& !cut_j
&& count_i
== 1 && count_j
== 1)
839 return fuse(i
, j
, info
, NULL
, 0, 0);
841 if (count_i
== 1 && !cut_i
)
842 return is_adj_ineq_extension(i
, j
, info
);
844 if (count_j
== 1 && !cut_j
)
845 return is_adj_ineq_extension(j
, i
, info
);
847 return isl_change_none
;
850 /* Given an affine transformation matrix "T", does row "row" represent
851 * anything other than a unit vector (possibly shifted by a constant)
852 * that is not involved in any of the other rows?
854 * That is, if a constraint involves the variable corresponding to
855 * the row, then could its preimage by "T" have any coefficients
856 * that are different from those in the original constraint?
858 static int not_unique_unit_row(__isl_keep isl_mat
*T
, int row
)
861 int len
= T
->n_col
- 1;
863 i
= isl_seq_first_non_zero(T
->row
[row
] + 1, len
);
866 if (!isl_int_is_one(T
->row
[row
][1 + i
]) &&
867 !isl_int_is_negone(T
->row
[row
][1 + i
]))
870 j
= isl_seq_first_non_zero(T
->row
[row
] + 1 + i
+ 1, len
- (i
+ 1));
874 for (j
= 1; j
< T
->n_row
; ++j
) {
877 if (!isl_int_is_zero(T
->row
[j
][1 + i
]))
884 /* Does inequality constraint "ineq" of "bmap" involve any of
885 * the variables marked in "affected"?
886 * "total" is the total number of variables, i.e., the number
887 * of entries in "affected".
889 static isl_bool
is_affected(__isl_keep isl_basic_map
*bmap
, int ineq
,
890 int *affected
, int total
)
894 for (i
= 0; i
< total
; ++i
) {
897 if (!isl_int_is_zero(bmap
->ineq
[ineq
][1 + i
]))
898 return isl_bool_true
;
901 return isl_bool_false
;
904 /* Given the compressed version of inequality constraint "ineq"
905 * of info->bmap in "v", check if the constraint can be tightened,
906 * where the compression is based on an equality constraint valid
908 * If so, add the tightened version of the inequality constraint
909 * to info->tab. "v" may be modified by this function.
911 * That is, if the compressed constraint is of the form
915 * with 0 < c < m, then it is equivalent to
919 * This means that c can also be subtracted from the original,
920 * uncompressed constraint without affecting the integer points
921 * in info->tab. Add this tightened constraint as an extra row
922 * to info->tab to make this information explicitly available.
924 static __isl_give isl_vec
*try_tightening(struct isl_coalesce_info
*info
,
925 int ineq
, __isl_take isl_vec
*v
)
933 ctx
= isl_vec_get_ctx(v
);
934 isl_seq_gcd(v
->el
+ 1, v
->size
- 1, &ctx
->normalize_gcd
);
935 if (isl_int_is_zero(ctx
->normalize_gcd
) ||
936 isl_int_is_one(ctx
->normalize_gcd
)) {
944 isl_int_fdiv_r(v
->el
[0], v
->el
[0], ctx
->normalize_gcd
);
945 if (isl_int_is_zero(v
->el
[0]))
948 if (isl_tab_extend_cons(info
->tab
, 1) < 0)
949 return isl_vec_free(v
);
951 isl_int_sub(info
->bmap
->ineq
[ineq
][0],
952 info
->bmap
->ineq
[ineq
][0], v
->el
[0]);
953 r
= isl_tab_add_ineq(info
->tab
, info
->bmap
->ineq
[ineq
]);
954 isl_int_add(info
->bmap
->ineq
[ineq
][0],
955 info
->bmap
->ineq
[ineq
][0], v
->el
[0]);
958 return isl_vec_free(v
);
963 /* Tighten the (non-redundant) constraints on the facet represented
965 * In particular, on input, info->tab represents the result
966 * of relaxing the "n" inequality constraints of info->bmap in "relaxed"
967 * by one, i.e., replacing f_i >= 0 by f_i + 1 >= 0, and then
968 * replacing the one at index "l" by the corresponding equality,
969 * i.e., f_k + 1 = 0, with k = relaxed[l].
971 * Compute a variable compression from the equality constraint f_k + 1 = 0
972 * and use it to tighten the other constraints of info->bmap
973 * (that is, all constraints that have not been relaxed),
974 * updating info->tab (and leaving info->bmap untouched).
975 * The compression handles essentially two cases, one where a variable
976 * is assigned a fixed value and can therefore be eliminated, and one
977 * where one variable is a shifted multiple of some other variable and
978 * can therefore be replaced by that multiple.
979 * Gaussian elimination would also work for the first case, but for
980 * the second case, the effectiveness would depend on the order
982 * After compression, some of the constraints may have coefficients
983 * with a common divisor. If this divisor does not divide the constant
984 * term, then the constraint can be tightened.
985 * The tightening is performed on the tableau info->tab by introducing
986 * extra (temporary) constraints.
988 * Only constraints that are possibly affected by the compression are
989 * considered. In particular, if the constraint only involves variables
990 * that are directly mapped to a distinct set of other variables, then
991 * no common divisor can be introduced and no tightening can occur.
993 * It is important to only consider the non-redundant constraints
994 * since the facet constraint has been relaxed prior to the call
995 * to this function, meaning that the constraints that were redundant
996 * prior to the relaxation may no longer be redundant.
997 * These constraints will be ignored in the fused result, so
998 * the fusion detection should not exploit them.
1000 static isl_stat
tighten_on_relaxed_facet(struct isl_coalesce_info
*info
,
1001 int n
, int *relaxed
, int l
)
1012 ctx
= isl_basic_map_get_ctx(info
->bmap
);
1013 total
= isl_basic_map_dim(info
->bmap
, isl_dim_all
);
1015 return isl_stat_error
;
1016 isl_int_add_ui(info
->bmap
->ineq
[k
][0], info
->bmap
->ineq
[k
][0], 1);
1017 T
= isl_mat_sub_alloc6(ctx
, info
->bmap
->ineq
, k
, 1, 0, 1 + total
);
1018 T
= isl_mat_variable_compression(T
, NULL
);
1019 isl_int_sub_ui(info
->bmap
->ineq
[k
][0], info
->bmap
->ineq
[k
][0], 1);
1021 return isl_stat_error
;
1022 if (T
->n_col
== 0) {
1027 affected
= isl_alloc_array(ctx
, int, total
);
1031 for (i
= 0; i
< total
; ++i
)
1032 affected
[i
] = not_unique_unit_row(T
, 1 + i
);
1034 for (i
= 0; i
< info
->bmap
->n_ineq
; ++i
) {
1036 if (any(relaxed
, n
, i
))
1038 if (info
->ineq
[i
] == STATUS_REDUNDANT
)
1040 handle
= is_affected(info
->bmap
, i
, affected
, total
);
1045 v
= isl_vec_alloc(ctx
, 1 + total
);
1048 isl_seq_cpy(v
->el
, info
->bmap
->ineq
[i
], 1 + total
);
1049 v
= isl_vec_mat_product(v
, isl_mat_copy(T
));
1050 v
= try_tightening(info
, i
, v
);
1062 return isl_stat_error
;
1065 /* Replace the basic maps "i" and "j" by an extension of "i"
1066 * along the "n" inequality constraints in "relax" by one.
1067 * The tableau info[i].tab has already been extended.
1068 * Extend info[i].bmap accordingly by relaxing all constraints in "relax"
1070 * Each integer division that does not have exactly the same
1071 * definition in "i" and "j" is marked unknown and the basic map
1072 * is scheduled to be simplified in an attempt to recover
1073 * the integer division definition.
1074 * Place the extension in the position that is the smallest of i and j.
1076 static enum isl_change
extend(int i
, int j
, int n
, int *relax
,
1077 struct isl_coalesce_info
*info
)
1082 info
[i
].bmap
= isl_basic_map_cow(info
[i
].bmap
);
1083 total
= isl_basic_map_dim(info
[i
].bmap
, isl_dim_all
);
1085 return isl_change_error
;
1086 for (l
= 0; l
< info
[i
].bmap
->n_div
; ++l
)
1087 if (!isl_seq_eq(info
[i
].bmap
->div
[l
],
1088 info
[j
].bmap
->div
[l
], 1 + 1 + total
)) {
1089 isl_int_set_si(info
[i
].bmap
->div
[l
][0], 0);
1090 info
[i
].simplify
= 1;
1092 for (l
= 0; l
< n
; ++l
)
1093 isl_int_add_ui(info
[i
].bmap
->ineq
[relax
[l
]][0],
1094 info
[i
].bmap
->ineq
[relax
[l
]][0], 1);
1095 ISL_F_SET(info
[i
].bmap
, ISL_BASIC_MAP_FINAL
);
1098 exchange(&info
[i
], &info
[j
]);
1099 return isl_change_fuse
;
1102 /* Basic map "i" has "n" inequality constraints (collected in "relax")
1103 * that are such that they include basic map "j" if they are relaxed
1104 * by one. All the other inequalities are valid for "j".
1105 * Check if basic map "j" forms an extension of basic map "i".
1107 * In particular, relax the constraints in "relax", compute the corresponding
1108 * facets one by one and check whether each of these is included
1109 * in the other basic map.
1110 * Before testing for inclusion, the constraints on each facet
1111 * are tightened to increase the chance of an inclusion being detected.
1112 * (Adding the valid constraints of "j" to the tableau of "i", as is done
1113 * in is_adj_ineq_extension, may further increase those chances, but this
1114 * is not currently done.)
1115 * If each facet is included, we know that relaxing the constraints extends
1116 * the basic map with exactly the other basic map (we already know that this
1117 * other basic map is included in the extension, because all other
1118 * inequality constraints are valid of "j") and we can replace the
1119 * two basic maps by this extension.
1121 * If any of the relaxed constraints turn out to be redundant, then bail out.
1122 * isl_tab_select_facet refuses to handle such constraints. It may be
1123 * possible to handle them anyway by making a distinction between
1124 * redundant constraints with a corresponding facet that still intersects
1125 * the set (allowing isl_tab_select_facet to handle them) and
1126 * those where the facet does not intersect the set (which can be ignored
1127 * because the empty facet is trivially included in the other disjunct).
1128 * However, relaxed constraints that turn out to be redundant should
1129 * be fairly rare and no such instance has been reported where
1130 * coalescing would be successful.
1146 static enum isl_change
is_relaxed_extension(int i
, int j
, int n
, int *relax
,
1147 struct isl_coalesce_info
*info
)
1151 struct isl_tab_undo
*snap
, *snap2
;
1152 unsigned n_eq
= info
[i
].bmap
->n_eq
;
1154 for (l
= 0; l
< n
; ++l
)
1155 if (isl_tab_is_equality(info
[i
].tab
, n_eq
+ relax
[l
]))
1156 return isl_change_none
;
1158 snap
= isl_tab_snap(info
[i
].tab
);
1159 for (l
= 0; l
< n
; ++l
)
1160 if (isl_tab_relax(info
[i
].tab
, n_eq
+ relax
[l
]) < 0)
1161 return isl_change_error
;
1162 for (l
= 0; l
< n
; ++l
) {
1163 if (!isl_tab_is_redundant(info
[i
].tab
, n_eq
+ relax
[l
]))
1165 if (isl_tab_rollback(info
[i
].tab
, snap
) < 0)
1166 return isl_change_error
;
1167 return isl_change_none
;
1169 snap2
= isl_tab_snap(info
[i
].tab
);
1170 for (l
= 0; l
< n
; ++l
) {
1171 if (isl_tab_rollback(info
[i
].tab
, snap2
) < 0)
1172 return isl_change_error
;
1173 if (isl_tab_select_facet(info
[i
].tab
, n_eq
+ relax
[l
]) < 0)
1174 return isl_change_error
;
1175 if (tighten_on_relaxed_facet(&info
[i
], n
, relax
, l
) < 0)
1176 return isl_change_error
;
1177 super
= contains(&info
[j
], info
[i
].tab
);
1179 return isl_change_error
;
1182 if (isl_tab_rollback(info
[i
].tab
, snap
) < 0)
1183 return isl_change_error
;
1184 return isl_change_none
;
1187 if (isl_tab_rollback(info
[i
].tab
, snap2
) < 0)
1188 return isl_change_error
;
1189 return extend(i
, j
, n
, relax
, info
);
1192 /* Data structure that keeps track of the wrapping constraints
1193 * and of information to bound the coefficients of those constraints.
1195 * bound is set if we want to apply a bound on the coefficients
1196 * mat contains the wrapping constraints
1197 * max is the bound on the coefficients (if bound is set)
1205 /* Update wraps->max to be greater than or equal to the coefficients
1206 * in the equalities and inequalities of info->bmap that can be removed
1207 * if we end up applying wrapping.
1209 static isl_stat
wraps_update_max(struct isl_wraps
*wraps
,
1210 struct isl_coalesce_info
*info
)
1214 isl_size total
= isl_basic_map_dim(info
->bmap
, isl_dim_all
);
1217 return isl_stat_error
;
1218 isl_int_init(max_k
);
1220 for (k
= 0; k
< info
->bmap
->n_eq
; ++k
) {
1221 if (info
->eq
[2 * k
] == STATUS_VALID
&&
1222 info
->eq
[2 * k
+ 1] == STATUS_VALID
)
1224 isl_seq_abs_max(info
->bmap
->eq
[k
] + 1, total
, &max_k
);
1225 if (isl_int_abs_gt(max_k
, wraps
->max
))
1226 isl_int_set(wraps
->max
, max_k
);
1229 for (k
= 0; k
< info
->bmap
->n_ineq
; ++k
) {
1230 if (info
->ineq
[k
] == STATUS_VALID
||
1231 info
->ineq
[k
] == STATUS_REDUNDANT
)
1233 isl_seq_abs_max(info
->bmap
->ineq
[k
] + 1, total
, &max_k
);
1234 if (isl_int_abs_gt(max_k
, wraps
->max
))
1235 isl_int_set(wraps
->max
, max_k
);
1238 isl_int_clear(max_k
);
1243 /* Initialize the isl_wraps data structure.
1244 * If we want to bound the coefficients of the wrapping constraints,
1245 * we set wraps->max to the largest coefficient
1246 * in the equalities and inequalities that can be removed if we end up
1247 * applying wrapping.
1249 static isl_stat
wraps_init(struct isl_wraps
*wraps
, __isl_take isl_mat
*mat
,
1250 struct isl_coalesce_info
*info
, int i
, int j
)
1257 return isl_stat_error
;
1258 ctx
= isl_mat_get_ctx(mat
);
1259 wraps
->bound
= isl_options_get_coalesce_bounded_wrapping(ctx
);
1262 isl_int_init(wraps
->max
);
1263 isl_int_set_si(wraps
->max
, 0);
1264 if (wraps_update_max(wraps
, &info
[i
]) < 0)
1265 return isl_stat_error
;
1266 if (wraps_update_max(wraps
, &info
[j
]) < 0)
1267 return isl_stat_error
;
1272 /* Free the contents of the isl_wraps data structure.
1274 static void wraps_free(struct isl_wraps
*wraps
)
1276 isl_mat_free(wraps
->mat
);
1278 isl_int_clear(wraps
->max
);
1281 /* Is the wrapping constraint in row "row" allowed?
1283 * If wraps->bound is set, we check that none of the coefficients
1284 * is greater than wraps->max.
1286 static int allow_wrap(struct isl_wraps
*wraps
, int row
)
1293 for (i
= 1; i
< wraps
->mat
->n_col
; ++i
)
1294 if (isl_int_abs_gt(wraps
->mat
->row
[row
][i
], wraps
->max
))
1300 /* Wrap "ineq" (or its opposite if "negate" is set) around "bound"
1301 * to include "set" and add the result in position "w" of "wraps".
1302 * "len" is the total number of coefficients in "bound" and "ineq".
1303 * Return 1 on success, 0 on failure and -1 on error.
1304 * Wrapping can fail if the result of wrapping is equal to "bound"
1305 * or if we want to bound the sizes of the coefficients and
1306 * the wrapped constraint does not satisfy this bound.
1308 static int add_wrap(struct isl_wraps
*wraps
, int w
, isl_int
*bound
,
1309 isl_int
*ineq
, unsigned len
, __isl_keep isl_set
*set
, int negate
)
1311 isl_seq_cpy(wraps
->mat
->row
[w
], bound
, len
);
1313 isl_seq_neg(wraps
->mat
->row
[w
+ 1], ineq
, len
);
1314 ineq
= wraps
->mat
->row
[w
+ 1];
1316 if (!isl_set_wrap_facet(set
, wraps
->mat
->row
[w
], ineq
))
1318 if (isl_seq_eq(wraps
->mat
->row
[w
], bound
, len
))
1320 if (!allow_wrap(wraps
, w
))
1325 /* For each constraint in info->bmap that is not redundant (as determined
1326 * by info->tab) and that is not a valid constraint for the other basic map,
1327 * wrap the constraint around "bound" such that it includes the whole
1328 * set "set" and append the resulting constraint to "wraps".
1329 * Note that the constraints that are valid for the other basic map
1330 * will be added to the combined basic map by default, so there is
1331 * no need to wrap them.
1332 * The caller wrap_in_facets even relies on this function not wrapping
1333 * any constraints that are already valid.
1334 * "wraps" is assumed to have been pre-allocated to the appropriate size.
1335 * wraps->n_row is the number of actual wrapped constraints that have
1337 * If any of the wrapping problems results in a constraint that is
1338 * identical to "bound", then this means that "set" is unbounded in such
1339 * way that no wrapping is possible. If this happens then wraps->n_row
1341 * Similarly, if we want to bound the coefficients of the wrapping
1342 * constraints and a newly added wrapping constraint does not
1343 * satisfy the bound, then wraps->n_row is also reset to zero.
1345 static isl_stat
add_wraps(struct isl_wraps
*wraps
,
1346 struct isl_coalesce_info
*info
, isl_int
*bound
, __isl_keep isl_set
*set
)
1351 isl_basic_map
*bmap
= info
->bmap
;
1352 isl_size total
= isl_basic_map_dim(bmap
, isl_dim_all
);
1353 unsigned len
= 1 + total
;
1356 return isl_stat_error
;
1358 w
= wraps
->mat
->n_row
;
1360 for (l
= 0; l
< bmap
->n_ineq
; ++l
) {
1361 if (info
->ineq
[l
] == STATUS_VALID
||
1362 info
->ineq
[l
] == STATUS_REDUNDANT
)
1364 if (isl_seq_is_neg(bound
, bmap
->ineq
[l
], len
))
1366 if (isl_seq_eq(bound
, bmap
->ineq
[l
], len
))
1368 if (isl_tab_is_redundant(info
->tab
, bmap
->n_eq
+ l
))
1371 added
= add_wrap(wraps
, w
, bound
, bmap
->ineq
[l
], len
, set
, 0);
1373 return isl_stat_error
;
1378 for (l
= 0; l
< bmap
->n_eq
; ++l
) {
1379 if (isl_seq_is_neg(bound
, bmap
->eq
[l
], len
))
1381 if (isl_seq_eq(bound
, bmap
->eq
[l
], len
))
1384 for (m
= 0; m
< 2; ++m
) {
1385 if (info
->eq
[2 * l
+ m
] == STATUS_VALID
)
1387 added
= add_wrap(wraps
, w
, bound
, bmap
->eq
[l
], len
,
1390 return isl_stat_error
;
1397 wraps
->mat
->n_row
= w
;
1400 wraps
->mat
->n_row
= 0;
1404 /* Check if the constraints in "wraps" from "first" until the last
1405 * are all valid for the basic set represented by "tab".
1406 * If not, wraps->n_row is set to zero.
1408 static int check_wraps(__isl_keep isl_mat
*wraps
, int first
,
1409 struct isl_tab
*tab
)
1413 for (i
= first
; i
< wraps
->n_row
; ++i
) {
1414 enum isl_ineq_type type
;
1415 type
= isl_tab_ineq_type(tab
, wraps
->row
[i
]);
1416 if (type
== isl_ineq_error
)
1418 if (type
== isl_ineq_redundant
)
1427 /* Return a set that corresponds to the non-redundant constraints
1428 * (as recorded in tab) of bmap.
1430 * It's important to remove the redundant constraints as some
1431 * of the other constraints may have been modified after the
1432 * constraints were marked redundant.
1433 * In particular, a constraint may have been relaxed.
1434 * Redundant constraints are ignored when a constraint is relaxed
1435 * and should therefore continue to be ignored ever after.
1436 * Otherwise, the relaxation might be thwarted by some of
1437 * these constraints.
1439 * Update the underlying set to ensure that the dimension doesn't change.
1440 * Otherwise the integer divisions could get dropped if the tab
1441 * turns out to be empty.
1443 static __isl_give isl_set
*set_from_updated_bmap(__isl_keep isl_basic_map
*bmap
,
1444 struct isl_tab
*tab
)
1446 isl_basic_set
*bset
;
1448 bmap
= isl_basic_map_copy(bmap
);
1449 bset
= isl_basic_map_underlying_set(bmap
);
1450 bset
= isl_basic_set_cow(bset
);
1451 bset
= isl_basic_set_update_from_tab(bset
, tab
);
1452 return isl_set_from_basic_set(bset
);
1455 /* Wrap the constraints of info->bmap that bound the facet defined
1456 * by inequality "k" around (the opposite of) this inequality to
1457 * include "set". "bound" may be used to store the negated inequality.
1458 * Since the wrapped constraints are not guaranteed to contain the whole
1459 * of info->bmap, we check them in check_wraps.
1460 * If any of the wrapped constraints turn out to be invalid, then
1461 * check_wraps will reset wrap->n_row to zero.
1463 static isl_stat
add_wraps_around_facet(struct isl_wraps
*wraps
,
1464 struct isl_coalesce_info
*info
, int k
, isl_int
*bound
,
1465 __isl_keep isl_set
*set
)
1467 struct isl_tab_undo
*snap
;
1469 isl_size total
= isl_basic_map_dim(info
->bmap
, isl_dim_all
);
1472 return isl_stat_error
;
1474 snap
= isl_tab_snap(info
->tab
);
1476 if (isl_tab_select_facet(info
->tab
, info
->bmap
->n_eq
+ k
) < 0)
1477 return isl_stat_error
;
1478 if (isl_tab_detect_redundant(info
->tab
) < 0)
1479 return isl_stat_error
;
1481 isl_seq_neg(bound
, info
->bmap
->ineq
[k
], 1 + total
);
1483 n
= wraps
->mat
->n_row
;
1484 if (add_wraps(wraps
, info
, bound
, set
) < 0)
1485 return isl_stat_error
;
1487 if (isl_tab_rollback(info
->tab
, snap
) < 0)
1488 return isl_stat_error
;
1489 if (check_wraps(wraps
->mat
, n
, info
->tab
) < 0)
1490 return isl_stat_error
;
1495 /* Given a basic set i with a constraint k that is adjacent to
1496 * basic set j, check if we can wrap
1497 * both the facet corresponding to k (if "wrap_facet" is set) and basic map j
1498 * (always) around their ridges to include the other set.
1499 * If so, replace the pair of basic sets by their union.
1501 * All constraints of i (except k) are assumed to be valid or
1502 * cut constraints for j.
1503 * Wrapping the cut constraints to include basic map j may result
1504 * in constraints that are no longer valid of basic map i
1505 * we have to check that the resulting wrapping constraints are valid for i.
1506 * If "wrap_facet" is not set, then all constraints of i (except k)
1507 * are assumed to be valid for j.
1516 static enum isl_change
can_wrap_in_facet(int i
, int j
, int k
,
1517 struct isl_coalesce_info
*info
, int wrap_facet
)
1519 enum isl_change change
= isl_change_none
;
1520 struct isl_wraps wraps
;
1523 struct isl_set
*set_i
= NULL
;
1524 struct isl_set
*set_j
= NULL
;
1525 struct isl_vec
*bound
= NULL
;
1526 isl_size total
= isl_basic_map_dim(info
[i
].bmap
, isl_dim_all
);
1529 return isl_change_error
;
1530 set_i
= set_from_updated_bmap(info
[i
].bmap
, info
[i
].tab
);
1531 set_j
= set_from_updated_bmap(info
[j
].bmap
, info
[j
].tab
);
1532 ctx
= isl_basic_map_get_ctx(info
[i
].bmap
);
1533 mat
= isl_mat_alloc(ctx
, 2 * (info
[i
].bmap
->n_eq
+ info
[j
].bmap
->n_eq
) +
1534 info
[i
].bmap
->n_ineq
+ info
[j
].bmap
->n_ineq
,
1536 if (wraps_init(&wraps
, mat
, info
, i
, j
) < 0)
1538 bound
= isl_vec_alloc(ctx
, 1 + total
);
1539 if (!set_i
|| !set_j
|| !bound
)
1542 isl_seq_cpy(bound
->el
, info
[i
].bmap
->ineq
[k
], 1 + total
);
1543 isl_int_add_ui(bound
->el
[0], bound
->el
[0], 1);
1544 isl_seq_normalize(ctx
, bound
->el
, 1 + total
);
1546 isl_seq_cpy(wraps
.mat
->row
[0], bound
->el
, 1 + total
);
1547 wraps
.mat
->n_row
= 1;
1549 if (add_wraps(&wraps
, &info
[j
], bound
->el
, set_i
) < 0)
1551 if (!wraps
.mat
->n_row
)
1555 if (add_wraps_around_facet(&wraps
, &info
[i
], k
,
1556 bound
->el
, set_j
) < 0)
1558 if (!wraps
.mat
->n_row
)
1562 change
= fuse(i
, j
, info
, wraps
.mat
, 0, 0);
1567 isl_set_free(set_i
);
1568 isl_set_free(set_j
);
1570 isl_vec_free(bound
);
1575 isl_vec_free(bound
);
1576 isl_set_free(set_i
);
1577 isl_set_free(set_j
);
1578 return isl_change_error
;
1581 /* Given a cut constraint t(x) >= 0 of basic map i, stored in row "w"
1582 * of wrap.mat, replace it by its relaxed version t(x) + 1 >= 0, and
1583 * add wrapping constraints to wrap.mat for all constraints
1584 * of basic map j that bound the part of basic map j that sticks out
1585 * of the cut constraint.
1586 * "set_i" is the underlying set of basic map i.
1587 * If any wrapping fails, then wraps->mat.n_row is reset to zero.
1589 * In particular, we first intersect basic map j with t(x) + 1 = 0.
1590 * If the result is empty, then t(x) >= 0 was actually a valid constraint
1591 * (with respect to the integer points), so we add t(x) >= 0 instead.
1592 * Otherwise, we wrap the constraints of basic map j that are not
1593 * redundant in this intersection and that are not already valid
1594 * for basic map i over basic map i.
1595 * Note that it is sufficient to wrap the constraints to include
1596 * basic map i, because we will only wrap the constraints that do
1597 * not include basic map i already. The wrapped constraint will
1598 * therefore be more relaxed compared to the original constraint.
1599 * Since the original constraint is valid for basic map j, so is
1600 * the wrapped constraint.
1602 static isl_stat
wrap_in_facet(struct isl_wraps
*wraps
, int w
,
1603 struct isl_coalesce_info
*info_j
, __isl_keep isl_set
*set_i
,
1604 struct isl_tab_undo
*snap
)
1606 isl_int_add_ui(wraps
->mat
->row
[w
][0], wraps
->mat
->row
[w
][0], 1);
1607 if (isl_tab_add_eq(info_j
->tab
, wraps
->mat
->row
[w
]) < 0)
1608 return isl_stat_error
;
1609 if (isl_tab_detect_redundant(info_j
->tab
) < 0)
1610 return isl_stat_error
;
1612 if (info_j
->tab
->empty
)
1613 isl_int_sub_ui(wraps
->mat
->row
[w
][0], wraps
->mat
->row
[w
][0], 1);
1614 else if (add_wraps(wraps
, info_j
, wraps
->mat
->row
[w
], set_i
) < 0)
1615 return isl_stat_error
;
1617 if (isl_tab_rollback(info_j
->tab
, snap
) < 0)
1618 return isl_stat_error
;
1623 /* Given a pair of basic maps i and j such that j sticks out
1624 * of i at n cut constraints, each time by at most one,
1625 * try to compute wrapping constraints and replace the two
1626 * basic maps by a single basic map.
1627 * The other constraints of i are assumed to be valid for j.
1628 * "set_i" is the underlying set of basic map i.
1629 * "wraps" has been initialized to be of the right size.
1631 * For each cut constraint t(x) >= 0 of i, we add the relaxed version
1632 * t(x) + 1 >= 0, along with wrapping constraints for all constraints
1633 * of basic map j that bound the part of basic map j that sticks out
1634 * of the cut constraint.
1636 * If any wrapping fails, i.e., if we cannot wrap to touch
1637 * the union, then we give up.
1638 * Otherwise, the pair of basic maps is replaced by their union.
1640 static enum isl_change
try_wrap_in_facets(int i
, int j
,
1641 struct isl_coalesce_info
*info
, struct isl_wraps
*wraps
,
1642 __isl_keep isl_set
*set_i
)
1646 struct isl_tab_undo
*snap
;
1648 total
= isl_basic_map_dim(info
[i
].bmap
, isl_dim_all
);
1650 return isl_change_error
;
1652 snap
= isl_tab_snap(info
[j
].tab
);
1654 wraps
->mat
->n_row
= 0;
1656 for (k
= 0; k
< info
[i
].bmap
->n_eq
; ++k
) {
1657 for (l
= 0; l
< 2; ++l
) {
1658 if (info
[i
].eq
[2 * k
+ l
] != STATUS_CUT
)
1660 w
= wraps
->mat
->n_row
++;
1662 isl_seq_neg(wraps
->mat
->row
[w
],
1663 info
[i
].bmap
->eq
[k
], 1 + total
);
1665 isl_seq_cpy(wraps
->mat
->row
[w
],
1666 info
[i
].bmap
->eq
[k
], 1 + total
);
1667 if (wrap_in_facet(wraps
, w
, &info
[j
], set_i
, snap
) < 0)
1668 return isl_change_error
;
1670 if (!wraps
->mat
->n_row
)
1671 return isl_change_none
;
1675 for (k
= 0; k
< info
[i
].bmap
->n_ineq
; ++k
) {
1676 if (info
[i
].ineq
[k
] != STATUS_CUT
)
1678 w
= wraps
->mat
->n_row
++;
1679 isl_seq_cpy(wraps
->mat
->row
[w
],
1680 info
[i
].bmap
->ineq
[k
], 1 + total
);
1681 if (wrap_in_facet(wraps
, w
, &info
[j
], set_i
, snap
) < 0)
1682 return isl_change_error
;
1684 if (!wraps
->mat
->n_row
)
1685 return isl_change_none
;
1688 return fuse(i
, j
, info
, wraps
->mat
, 0, 1);
1691 /* Given a pair of basic maps i and j such that j sticks out
1692 * of i at n cut constraints, each time by at most one,
1693 * try to compute wrapping constraints and replace the two
1694 * basic maps by a single basic map.
1695 * The other constraints of i are assumed to be valid for j.
1697 * The core computation is performed by try_wrap_in_facets.
1698 * This function simply extracts an underlying set representation
1699 * of basic map i and initializes the data structure for keeping
1700 * track of wrapping constraints.
1702 static enum isl_change
wrap_in_facets(int i
, int j
, int n
,
1703 struct isl_coalesce_info
*info
)
1705 enum isl_change change
= isl_change_none
;
1706 struct isl_wraps wraps
;
1709 isl_set
*set_i
= NULL
;
1710 isl_size total
= isl_basic_map_dim(info
[i
].bmap
, isl_dim_all
);
1714 return isl_change_error
;
1715 if (isl_tab_extend_cons(info
[j
].tab
, 1) < 0)
1716 return isl_change_error
;
1718 max_wrap
= 1 + 2 * info
[j
].bmap
->n_eq
+ info
[j
].bmap
->n_ineq
;
1721 set_i
= set_from_updated_bmap(info
[i
].bmap
, info
[i
].tab
);
1722 ctx
= isl_basic_map_get_ctx(info
[i
].bmap
);
1723 mat
= isl_mat_alloc(ctx
, max_wrap
, 1 + total
);
1724 if (wraps_init(&wraps
, mat
, info
, i
, j
) < 0)
1729 change
= try_wrap_in_facets(i
, j
, info
, &wraps
, set_i
);
1732 isl_set_free(set_i
);
1737 isl_set_free(set_i
);
1738 return isl_change_error
;
1741 /* Return the effect of inequality "ineq" on the tableau "tab",
1742 * after relaxing the constant term of "ineq" by one.
1744 static enum isl_ineq_type
type_of_relaxed(struct isl_tab
*tab
, isl_int
*ineq
)
1746 enum isl_ineq_type type
;
1748 isl_int_add_ui(ineq
[0], ineq
[0], 1);
1749 type
= isl_tab_ineq_type(tab
, ineq
);
1750 isl_int_sub_ui(ineq
[0], ineq
[0], 1);
1755 /* Given two basic sets i and j,
1756 * check if relaxing all the cut constraints of i by one turns
1757 * them into valid constraint for j and check if we can wrap in
1758 * the bits that are sticking out.
1759 * If so, replace the pair by their union.
1761 * We first check if all relaxed cut inequalities of i are valid for j
1762 * and then try to wrap in the intersections of the relaxed cut inequalities
1765 * During this wrapping, we consider the points of j that lie at a distance
1766 * of exactly 1 from i. In particular, we ignore the points that lie in
1767 * between this lower-dimensional space and the basic map i.
1768 * We can therefore only apply this to integer maps.
1794 * Wrapping can fail if the result of wrapping one of the facets
1795 * around its edges does not produce any new facet constraint.
1796 * In particular, this happens when we try to wrap in unbounded sets.
1798 * _______________________________________________________________________
1802 * |_| |_________________________________________________________________
1805 * The following is not an acceptable result of coalescing the above two
1806 * sets as it includes extra integer points.
1807 * _______________________________________________________________________
1812 * \______________________________________________________________________
1814 static enum isl_change
can_wrap_in_set(int i
, int j
,
1815 struct isl_coalesce_info
*info
)
1821 if (ISL_F_ISSET(info
[i
].bmap
, ISL_BASIC_MAP_RATIONAL
) ||
1822 ISL_F_ISSET(info
[j
].bmap
, ISL_BASIC_MAP_RATIONAL
))
1823 return isl_change_none
;
1825 n
= count_eq(&info
[i
], STATUS_CUT
) + count_ineq(&info
[i
], STATUS_CUT
);
1827 return isl_change_none
;
1829 total
= isl_basic_map_dim(info
[i
].bmap
, isl_dim_all
);
1831 return isl_change_error
;
1832 for (k
= 0; k
< info
[i
].bmap
->n_eq
; ++k
) {
1833 for (l
= 0; l
< 2; ++l
) {
1834 enum isl_ineq_type type
;
1836 if (info
[i
].eq
[2 * k
+ l
] != STATUS_CUT
)
1840 isl_seq_neg(info
[i
].bmap
->eq
[k
],
1841 info
[i
].bmap
->eq
[k
], 1 + total
);
1842 type
= type_of_relaxed(info
[j
].tab
,
1843 info
[i
].bmap
->eq
[k
]);
1845 isl_seq_neg(info
[i
].bmap
->eq
[k
],
1846 info
[i
].bmap
->eq
[k
], 1 + total
);
1847 if (type
== isl_ineq_error
)
1848 return isl_change_error
;
1849 if (type
!= isl_ineq_redundant
)
1850 return isl_change_none
;
1854 for (k
= 0; k
< info
[i
].bmap
->n_ineq
; ++k
) {
1855 enum isl_ineq_type type
;
1857 if (info
[i
].ineq
[k
] != STATUS_CUT
)
1860 type
= type_of_relaxed(info
[j
].tab
, info
[i
].bmap
->ineq
[k
]);
1861 if (type
== isl_ineq_error
)
1862 return isl_change_error
;
1863 if (type
!= isl_ineq_redundant
)
1864 return isl_change_none
;
1867 return wrap_in_facets(i
, j
, n
, info
);
1870 /* Check if either i or j has only cut constraints that can
1871 * be used to wrap in (a facet of) the other basic set.
1872 * if so, replace the pair by their union.
1874 static enum isl_change
check_wrap(int i
, int j
, struct isl_coalesce_info
*info
)
1876 enum isl_change change
= isl_change_none
;
1878 change
= can_wrap_in_set(i
, j
, info
);
1879 if (change
!= isl_change_none
)
1882 change
= can_wrap_in_set(j
, i
, info
);
1886 /* Check if all inequality constraints of "i" that cut "j" cease
1887 * to be cut constraints if they are relaxed by one.
1888 * If so, collect the cut constraints in "list".
1889 * The caller is responsible for allocating "list".
1891 static isl_bool
all_cut_by_one(int i
, int j
, struct isl_coalesce_info
*info
,
1897 for (l
= 0; l
< info
[i
].bmap
->n_ineq
; ++l
) {
1898 enum isl_ineq_type type
;
1900 if (info
[i
].ineq
[l
] != STATUS_CUT
)
1902 type
= type_of_relaxed(info
[j
].tab
, info
[i
].bmap
->ineq
[l
]);
1903 if (type
== isl_ineq_error
)
1904 return isl_bool_error
;
1905 if (type
!= isl_ineq_redundant
)
1906 return isl_bool_false
;
1910 return isl_bool_true
;
1913 /* Given two basic maps such that "j" has at least one equality constraint
1914 * that is adjacent to an inequality constraint of "i" and such that "i" has
1915 * exactly one inequality constraint that is adjacent to an equality
1916 * constraint of "j", check whether "i" can be extended to include "j" or
1917 * whether "j" can be wrapped into "i".
1918 * All remaining constraints of "i" and "j" are assumed to be valid
1919 * or cut constraints of the other basic map.
1920 * However, none of the equality constraints of "i" are cut constraints.
1922 * If "i" has any "cut" inequality constraints, then check if relaxing
1923 * each of them by one is sufficient for them to become valid.
1924 * If so, check if the inequality constraint adjacent to an equality
1925 * constraint of "j" along with all these cut constraints
1926 * can be relaxed by one to contain exactly "j".
1927 * Otherwise, or if this fails, check if "j" can be wrapped into "i".
1929 static enum isl_change
check_single_adj_eq(int i
, int j
,
1930 struct isl_coalesce_info
*info
)
1932 enum isl_change change
= isl_change_none
;
1939 n_cut
= count_ineq(&info
[i
], STATUS_CUT
);
1941 k
= find_ineq(&info
[i
], STATUS_ADJ_EQ
);
1944 ctx
= isl_basic_map_get_ctx(info
[i
].bmap
);
1945 relax
= isl_calloc_array(ctx
, int, 1 + n_cut
);
1947 return isl_change_error
;
1949 try_relax
= all_cut_by_one(i
, j
, info
, relax
+ 1);
1951 change
= isl_change_error
;
1953 try_relax
= isl_bool_true
;
1956 if (try_relax
&& change
== isl_change_none
)
1957 change
= is_relaxed_extension(i
, j
, 1 + n_cut
, relax
, info
);
1960 if (change
!= isl_change_none
)
1963 change
= can_wrap_in_facet(i
, j
, k
, info
, n_cut
> 0);
1968 /* At least one of the basic maps has an equality that is adjacent
1969 * to an inequality. Make sure that only one of the basic maps has
1970 * such an equality and that the other basic map has exactly one
1971 * inequality adjacent to an equality.
1972 * If the other basic map does not have such an inequality, then
1973 * check if all its constraints are either valid or cut constraints
1974 * and, if so, try wrapping in the first map into the second.
1975 * Otherwise, try to extend one basic map with the other or
1976 * wrap one basic map in the other.
1978 static enum isl_change
check_adj_eq(int i
, int j
,
1979 struct isl_coalesce_info
*info
)
1981 if (any_eq(&info
[i
], STATUS_ADJ_INEQ
) &&
1982 any_eq(&info
[j
], STATUS_ADJ_INEQ
))
1983 /* ADJ EQ TOO MANY */
1984 return isl_change_none
;
1986 if (any_eq(&info
[i
], STATUS_ADJ_INEQ
))
1987 return check_adj_eq(j
, i
, info
);
1989 /* j has an equality adjacent to an inequality in i */
1991 if (count_ineq(&info
[i
], STATUS_ADJ_EQ
) != 1) {
1992 if (all_valid_or_cut(&info
[i
]))
1993 return can_wrap_in_set(i
, j
, info
);
1994 return isl_change_none
;
1996 if (any_eq(&info
[i
], STATUS_CUT
))
1997 return isl_change_none
;
1998 if (any_ineq(&info
[j
], STATUS_ADJ_EQ
) ||
1999 any_ineq(&info
[i
], STATUS_ADJ_INEQ
) ||
2000 any_ineq(&info
[j
], STATUS_ADJ_INEQ
))
2001 /* ADJ EQ TOO MANY */
2002 return isl_change_none
;
2004 return check_single_adj_eq(i
, j
, info
);
2007 /* Disjunct "j" lies on a hyperplane that is adjacent to disjunct "i".
2008 * In particular, disjunct "i" has an inequality constraint that is adjacent
2009 * to a (combination of) equality constraint(s) of disjunct "j",
2010 * but disjunct "j" has no explicit equality constraint adjacent
2011 * to an inequality constraint of disjunct "i".
2013 * Disjunct "i" is already known not to have any equality constraints
2014 * that are adjacent to an equality or inequality constraint.
2015 * Check that, other than the inequality constraint mentioned above,
2016 * all other constraints of disjunct "i" are valid for disjunct "j".
2017 * If so, try and wrap in disjunct "j".
2019 static enum isl_change
check_ineq_adj_eq(int i
, int j
,
2020 struct isl_coalesce_info
*info
)
2024 if (any_eq(&info
[i
], STATUS_CUT
))
2025 return isl_change_none
;
2026 if (any_ineq(&info
[i
], STATUS_CUT
))
2027 return isl_change_none
;
2028 if (any_ineq(&info
[i
], STATUS_ADJ_INEQ
))
2029 return isl_change_none
;
2030 if (count_ineq(&info
[i
], STATUS_ADJ_EQ
) != 1)
2031 return isl_change_none
;
2033 k
= find_ineq(&info
[i
], STATUS_ADJ_EQ
);
2035 return can_wrap_in_facet(i
, j
, k
, info
, 0);
2038 /* The two basic maps lie on adjacent hyperplanes. In particular,
2039 * basic map "i" has an equality that lies parallel to basic map "j".
2040 * Check if we can wrap the facets around the parallel hyperplanes
2041 * to include the other set.
2043 * We perform basically the same operations as can_wrap_in_facet,
2044 * except that we don't need to select a facet of one of the sets.
2050 * If there is more than one equality of "i" adjacent to an equality of "j",
2051 * then the result will satisfy one or more equalities that are a linear
2052 * combination of these equalities. These will be encoded as pairs
2053 * of inequalities in the wrapping constraints and need to be made
2056 static enum isl_change
check_eq_adj_eq(int i
, int j
,
2057 struct isl_coalesce_info
*info
)
2060 enum isl_change change
= isl_change_none
;
2061 int detect_equalities
= 0;
2062 struct isl_wraps wraps
;
2065 struct isl_set
*set_i
= NULL
;
2066 struct isl_set
*set_j
= NULL
;
2067 struct isl_vec
*bound
= NULL
;
2068 isl_size total
= isl_basic_map_dim(info
[i
].bmap
, isl_dim_all
);
2071 return isl_change_error
;
2072 if (count_eq(&info
[i
], STATUS_ADJ_EQ
) != 1)
2073 detect_equalities
= 1;
2075 k
= find_eq(&info
[i
], STATUS_ADJ_EQ
);
2077 set_i
= set_from_updated_bmap(info
[i
].bmap
, info
[i
].tab
);
2078 set_j
= set_from_updated_bmap(info
[j
].bmap
, info
[j
].tab
);
2079 ctx
= isl_basic_map_get_ctx(info
[i
].bmap
);
2080 mat
= isl_mat_alloc(ctx
, 2 * (info
[i
].bmap
->n_eq
+ info
[j
].bmap
->n_eq
) +
2081 info
[i
].bmap
->n_ineq
+ info
[j
].bmap
->n_ineq
,
2083 if (wraps_init(&wraps
, mat
, info
, i
, j
) < 0)
2085 bound
= isl_vec_alloc(ctx
, 1 + total
);
2086 if (!set_i
|| !set_j
|| !bound
)
2090 isl_seq_neg(bound
->el
, info
[i
].bmap
->eq
[k
/ 2], 1 + total
);
2092 isl_seq_cpy(bound
->el
, info
[i
].bmap
->eq
[k
/ 2], 1 + total
);
2093 isl_int_add_ui(bound
->el
[0], bound
->el
[0], 1);
2095 isl_seq_cpy(wraps
.mat
->row
[0], bound
->el
, 1 + total
);
2096 wraps
.mat
->n_row
= 1;
2098 if (add_wraps(&wraps
, &info
[j
], bound
->el
, set_i
) < 0)
2100 if (!wraps
.mat
->n_row
)
2103 isl_int_sub_ui(bound
->el
[0], bound
->el
[0], 1);
2104 isl_seq_neg(bound
->el
, bound
->el
, 1 + total
);
2106 isl_seq_cpy(wraps
.mat
->row
[wraps
.mat
->n_row
], bound
->el
, 1 + total
);
2109 if (add_wraps(&wraps
, &info
[i
], bound
->el
, set_j
) < 0)
2111 if (!wraps
.mat
->n_row
)
2114 change
= fuse(i
, j
, info
, wraps
.mat
, detect_equalities
, 0);
2117 error
: change
= isl_change_error
;
2122 isl_set_free(set_i
);
2123 isl_set_free(set_j
);
2124 isl_vec_free(bound
);
2129 /* Initialize the "eq" and "ineq" fields of "info".
2131 static void init_status(struct isl_coalesce_info
*info
)
2133 info
->eq
= info
->ineq
= NULL
;
2136 /* Set info->eq to the positions of the equalities of info->bmap
2137 * with respect to the basic map represented by "tab".
2138 * If info->eq has already been computed, then do not compute it again.
2140 static void set_eq_status_in(struct isl_coalesce_info
*info
,
2141 struct isl_tab
*tab
)
2145 info
->eq
= eq_status_in(info
->bmap
, tab
);
2148 /* Set info->ineq to the positions of the inequalities of info->bmap
2149 * with respect to the basic map represented by "tab".
2150 * If info->ineq has already been computed, then do not compute it again.
2152 static void set_ineq_status_in(struct isl_coalesce_info
*info
,
2153 struct isl_tab
*tab
)
2157 info
->ineq
= ineq_status_in(info
->bmap
, info
->tab
, tab
);
2160 /* Free the memory allocated by the "eq" and "ineq" fields of "info".
2161 * This function assumes that init_status has been called on "info" first,
2162 * after which the "eq" and "ineq" fields may or may not have been
2163 * assigned a newly allocated array.
2165 static void clear_status(struct isl_coalesce_info
*info
)
2171 /* Are all inequality constraints of the basic map represented by "info"
2172 * valid for the other basic map, except for a single constraint
2173 * that is adjacent to an inequality constraint of the other basic map?
2175 static int all_ineq_valid_or_single_adj_ineq(struct isl_coalesce_info
*info
)
2180 for (i
= 0; i
< info
->bmap
->n_ineq
; ++i
) {
2181 if (info
->ineq
[i
] == STATUS_REDUNDANT
)
2183 if (info
->ineq
[i
] == STATUS_VALID
)
2185 if (info
->ineq
[i
] != STATUS_ADJ_INEQ
)
2195 /* Basic map "i" has one or more equality constraints that separate it
2196 * from basic map "j". Check if it happens to be an extension
2198 * In particular, check that all constraints of "j" are valid for "i",
2199 * except for one inequality constraint that is adjacent
2200 * to an inequality constraints of "i".
2201 * If so, check for "i" being an extension of "j" by calling
2202 * is_adj_ineq_extension.
2204 * Clean up the memory allocated for keeping track of the status
2205 * of the constraints before returning.
2207 static enum isl_change
separating_equality(int i
, int j
,
2208 struct isl_coalesce_info
*info
)
2210 enum isl_change change
= isl_change_none
;
2212 if (all(info
[j
].eq
, 2 * info
[j
].bmap
->n_eq
, STATUS_VALID
) &&
2213 all_ineq_valid_or_single_adj_ineq(&info
[j
]))
2214 change
= is_adj_ineq_extension(j
, i
, info
);
2216 clear_status(&info
[i
]);
2217 clear_status(&info
[j
]);
2221 /* Check if the union of the given pair of basic maps
2222 * can be represented by a single basic map.
2223 * If so, replace the pair by the single basic map and return
2224 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
2225 * Otherwise, return isl_change_none.
2226 * The two basic maps are assumed to live in the same local space.
2227 * The "eq" and "ineq" fields of info[i] and info[j] are assumed
2228 * to have been initialized by the caller, either to NULL or
2229 * to valid information.
2231 * We first check the effect of each constraint of one basic map
2232 * on the other basic map.
2233 * The constraint may be
2234 * redundant the constraint is redundant in its own
2235 * basic map and should be ignore and removed
2237 * valid all (integer) points of the other basic map
2238 * satisfy the constraint
2239 * separate no (integer) point of the other basic map
2240 * satisfies the constraint
2241 * cut some but not all points of the other basic map
2242 * satisfy the constraint
2243 * adj_eq the given constraint is adjacent (on the outside)
2244 * to an equality of the other basic map
2245 * adj_ineq the given constraint is adjacent (on the outside)
2246 * to an inequality of the other basic map
2248 * We consider seven cases in which we can replace the pair by a single
2249 * basic map. We ignore all "redundant" constraints.
2251 * 1. all constraints of one basic map are valid
2252 * => the other basic map is a subset and can be removed
2254 * 2. all constraints of both basic maps are either "valid" or "cut"
2255 * and the facets corresponding to the "cut" constraints
2256 * of one of the basic maps lies entirely inside the other basic map
2257 * => the pair can be replaced by a basic map consisting
2258 * of the valid constraints in both basic maps
2260 * 3. there is a single pair of adjacent inequalities
2261 * (all other constraints are "valid")
2262 * => the pair can be replaced by a basic map consisting
2263 * of the valid constraints in both basic maps
2265 * 4. one basic map has a single adjacent inequality, while the other
2266 * constraints are "valid". The other basic map has some
2267 * "cut" constraints, but replacing the adjacent inequality by
2268 * its opposite and adding the valid constraints of the other
2269 * basic map results in a subset of the other basic map
2270 * => the pair can be replaced by a basic map consisting
2271 * of the valid constraints in both basic maps
2273 * 5. there is a single adjacent pair of an inequality and an equality,
2274 * the other constraints of the basic map containing the inequality are
2275 * "valid". Moreover, if the inequality the basic map is relaxed
2276 * and then turned into an equality, then resulting facet lies
2277 * entirely inside the other basic map
2278 * => the pair can be replaced by the basic map containing
2279 * the inequality, with the inequality relaxed.
2281 * 6. there is a single inequality adjacent to an equality,
2282 * the other constraints of the basic map containing the inequality are
2283 * "valid". Moreover, the facets corresponding to both
2284 * the inequality and the equality can be wrapped around their
2285 * ridges to include the other basic map
2286 * => the pair can be replaced by a basic map consisting
2287 * of the valid constraints in both basic maps together
2288 * with all wrapping constraints
2290 * 7. one of the basic maps extends beyond the other by at most one.
2291 * Moreover, the facets corresponding to the cut constraints and
2292 * the pieces of the other basic map at offset one from these cut
2293 * constraints can be wrapped around their ridges to include
2294 * the union of the two basic maps
2295 * => the pair can be replaced by a basic map consisting
2296 * of the valid constraints in both basic maps together
2297 * with all wrapping constraints
2299 * 8. the two basic maps live in adjacent hyperplanes. In principle
2300 * such sets can always be combined through wrapping, but we impose
2301 * that there is only one such pair, to avoid overeager coalescing.
2303 * Throughout the computation, we maintain a collection of tableaus
2304 * corresponding to the basic maps. When the basic maps are dropped
2305 * or combined, the tableaus are modified accordingly.
2307 static enum isl_change
coalesce_local_pair_reuse(int i
, int j
,
2308 struct isl_coalesce_info
*info
)
2310 enum isl_change change
= isl_change_none
;
2312 set_ineq_status_in(&info
[i
], info
[j
].tab
);
2313 if (info
[i
].bmap
->n_ineq
&& !info
[i
].ineq
)
2315 if (any_ineq(&info
[i
], STATUS_ERROR
))
2317 if (any_ineq(&info
[i
], STATUS_SEPARATE
))
2320 set_ineq_status_in(&info
[j
], info
[i
].tab
);
2321 if (info
[j
].bmap
->n_ineq
&& !info
[j
].ineq
)
2323 if (any_ineq(&info
[j
], STATUS_ERROR
))
2325 if (any_ineq(&info
[j
], STATUS_SEPARATE
))
2328 set_eq_status_in(&info
[i
], info
[j
].tab
);
2329 if (info
[i
].bmap
->n_eq
&& !info
[i
].eq
)
2331 if (any_eq(&info
[i
], STATUS_ERROR
))
2334 set_eq_status_in(&info
[j
], info
[i
].tab
);
2335 if (info
[j
].bmap
->n_eq
&& !info
[j
].eq
)
2337 if (any_eq(&info
[j
], STATUS_ERROR
))
2340 if (any_eq(&info
[i
], STATUS_SEPARATE
))
2341 return separating_equality(i
, j
, info
);
2342 if (any_eq(&info
[j
], STATUS_SEPARATE
))
2343 return separating_equality(j
, i
, info
);
2345 if (all(info
[i
].eq
, 2 * info
[i
].bmap
->n_eq
, STATUS_VALID
) &&
2346 all(info
[i
].ineq
, info
[i
].bmap
->n_ineq
, STATUS_VALID
)) {
2348 change
= isl_change_drop_second
;
2349 } else if (all(info
[j
].eq
, 2 * info
[j
].bmap
->n_eq
, STATUS_VALID
) &&
2350 all(info
[j
].ineq
, info
[j
].bmap
->n_ineq
, STATUS_VALID
)) {
2352 change
= isl_change_drop_first
;
2353 } else if (any_eq(&info
[i
], STATUS_ADJ_EQ
)) {
2354 change
= check_eq_adj_eq(i
, j
, info
);
2355 } else if (any_eq(&info
[j
], STATUS_ADJ_EQ
)) {
2356 change
= check_eq_adj_eq(j
, i
, info
);
2357 } else if (any_eq(&info
[i
], STATUS_ADJ_INEQ
) ||
2358 any_eq(&info
[j
], STATUS_ADJ_INEQ
)) {
2359 change
= check_adj_eq(i
, j
, info
);
2360 } else if (any_ineq(&info
[i
], STATUS_ADJ_EQ
)) {
2361 change
= check_ineq_adj_eq(i
, j
, info
);
2362 } else if (any_ineq(&info
[j
], STATUS_ADJ_EQ
)) {
2363 change
= check_ineq_adj_eq(j
, i
, info
);
2364 } else if (any_ineq(&info
[i
], STATUS_ADJ_INEQ
) ||
2365 any_ineq(&info
[j
], STATUS_ADJ_INEQ
)) {
2366 change
= check_adj_ineq(i
, j
, info
);
2368 if (!any_eq(&info
[i
], STATUS_CUT
) &&
2369 !any_eq(&info
[j
], STATUS_CUT
))
2370 change
= check_facets(i
, j
, info
);
2371 if (change
== isl_change_none
)
2372 change
= check_wrap(i
, j
, info
);
2376 clear_status(&info
[i
]);
2377 clear_status(&info
[j
]);
2380 clear_status(&info
[i
]);
2381 clear_status(&info
[j
]);
2382 return isl_change_error
;
2385 /* Check if the union of the given pair of basic maps
2386 * can be represented by a single basic map.
2387 * If so, replace the pair by the single basic map and return
2388 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
2389 * Otherwise, return isl_change_none.
2390 * The two basic maps are assumed to live in the same local space.
2392 static enum isl_change
coalesce_local_pair(int i
, int j
,
2393 struct isl_coalesce_info
*info
)
2395 init_status(&info
[i
]);
2396 init_status(&info
[j
]);
2397 return coalesce_local_pair_reuse(i
, j
, info
);
2400 /* Shift the integer division at position "div" of the basic map
2401 * represented by "info" by "shift".
2403 * That is, if the integer division has the form
2407 * then replace it by
2409 * floor((f(x) + shift * d)/d) - shift
2411 static isl_stat
shift_div(struct isl_coalesce_info
*info
, int div
,
2414 isl_size total
, n_div
;
2416 info
->bmap
= isl_basic_map_shift_div(info
->bmap
, div
, 0, shift
);
2418 return isl_stat_error
;
2420 total
= isl_basic_map_dim(info
->bmap
, isl_dim_all
);
2421 n_div
= isl_basic_map_dim(info
->bmap
, isl_dim_div
);
2422 if (total
< 0 || n_div
< 0)
2423 return isl_stat_error
;
2425 if (isl_tab_shift_var(info
->tab
, total
+ div
, shift
) < 0)
2426 return isl_stat_error
;
2431 /* If the integer division at position "div" is defined by an equality,
2432 * i.e., a stride constraint, then change the integer division expression
2433 * to have a constant term equal to zero.
2435 * Let the equality constraint be
2439 * The integer division expression is then typically of the form
2441 * a = floor((-f - c')/m)
2443 * The integer division is first shifted by t = floor(c/m),
2444 * turning the equality constraint into
2446 * c - m floor(c/m) + f + m a' = 0
2450 * (c mod m) + f + m a' = 0
2454 * a' = (-f - (c mod m))/m = floor((-f)/m)
2456 * because a' is an integer and 0 <= (c mod m) < m.
2457 * The constant term of a' can therefore be zeroed out,
2458 * but only if the integer division expression is of the expected form.
2460 static isl_stat
normalize_stride_div(struct isl_coalesce_info
*info
, int div
)
2462 isl_bool defined
, valid
;
2465 isl_int shift
, stride
;
2467 defined
= isl_basic_map_has_defining_equality(info
->bmap
, isl_dim_div
,
2470 return isl_stat_error
;
2474 return isl_stat_error
;
2475 valid
= isl_constraint_is_div_equality(c
, div
);
2476 isl_int_init(shift
);
2477 isl_int_init(stride
);
2478 isl_constraint_get_constant(c
, &shift
);
2479 isl_constraint_get_coefficient(c
, isl_dim_div
, div
, &stride
);
2480 isl_int_fdiv_q(shift
, shift
, stride
);
2481 r
= shift_div(info
, div
, shift
);
2482 isl_int_clear(stride
);
2483 isl_int_clear(shift
);
2484 isl_constraint_free(c
);
2485 if (r
< 0 || valid
< 0)
2486 return isl_stat_error
;
2489 info
->bmap
= isl_basic_map_set_div_expr_constant_num_si_inplace(
2490 info
->bmap
, div
, 0);
2492 return isl_stat_error
;
2496 /* The basic maps represented by "info1" and "info2" are known
2497 * to have the same number of integer divisions.
2498 * Check if pairs of integer divisions are equal to each other
2499 * despite the fact that they differ by a rational constant.
2501 * In particular, look for any pair of integer divisions that
2502 * only differ in their constant terms.
2503 * If either of these integer divisions is defined
2504 * by stride constraints, then modify it to have a zero constant term.
2505 * If both are defined by stride constraints then in the end they will have
2506 * the same (zero) constant term.
2508 static isl_stat
harmonize_stride_divs(struct isl_coalesce_info
*info1
,
2509 struct isl_coalesce_info
*info2
)
2514 n
= isl_basic_map_dim(info1
->bmap
, isl_dim_div
);
2516 return isl_stat_error
;
2517 for (i
= 0; i
< n
; ++i
) {
2518 isl_bool known
, harmonize
;
2520 known
= isl_basic_map_div_is_known(info1
->bmap
, i
);
2521 if (known
>= 0 && known
)
2522 known
= isl_basic_map_div_is_known(info2
->bmap
, i
);
2524 return isl_stat_error
;
2527 harmonize
= isl_basic_map_equal_div_expr_except_constant(
2528 info1
->bmap
, i
, info2
->bmap
, i
);
2530 return isl_stat_error
;
2533 if (normalize_stride_div(info1
, i
) < 0)
2534 return isl_stat_error
;
2535 if (normalize_stride_div(info2
, i
) < 0)
2536 return isl_stat_error
;
2542 /* If "shift" is an integer constant, then shift the integer division
2543 * at position "div" of the basic map represented by "info" by "shift".
2544 * If "shift" is not an integer constant, then do nothing.
2545 * If "shift" is equal to zero, then no shift needs to be performed either.
2547 * That is, if the integer division has the form
2551 * then replace it by
2553 * floor((f(x) + shift * d)/d) - shift
2555 static isl_stat
shift_if_cst_int(struct isl_coalesce_info
*info
, int div
,
2556 __isl_keep isl_aff
*shift
)
2563 cst
= isl_aff_is_cst(shift
);
2564 if (cst
< 0 || !cst
)
2565 return cst
< 0 ? isl_stat_error
: isl_stat_ok
;
2567 c
= isl_aff_get_constant_val(shift
);
2568 cst
= isl_val_is_int(c
);
2569 if (cst
>= 0 && cst
)
2570 cst
= isl_bool_not(isl_val_is_zero(c
));
2571 if (cst
< 0 || !cst
) {
2573 return cst
< 0 ? isl_stat_error
: isl_stat_ok
;
2577 r
= isl_val_get_num_isl_int(c
, &d
);
2579 r
= shift_div(info
, div
, d
);
2587 /* Check if some of the divs in the basic map represented by "info1"
2588 * are shifts of the corresponding divs in the basic map represented
2589 * by "info2", taking into account the equality constraints "eq1" of "info1"
2590 * and "eq2" of "info2". If so, align them with those of "info2".
2591 * "info1" and "info2" are assumed to have the same number
2592 * of integer divisions.
2594 * An integer division is considered to be a shift of another integer
2595 * division if, after simplification with respect to the equality
2596 * constraints of the other basic map, one is equal to the other
2599 * In particular, for each pair of integer divisions, if both are known,
2600 * have the same denominator and are not already equal to each other,
2601 * simplify each with respect to the equality constraints
2602 * of the other basic map. If the difference is an integer constant,
2603 * then move this difference outside.
2604 * That is, if, after simplification, one integer division is of the form
2606 * floor((f(x) + c_1)/d)
2608 * while the other is of the form
2610 * floor((f(x) + c_2)/d)
2612 * and n = (c_2 - c_1)/d is an integer, then replace the first
2613 * integer division by
2615 * floor((f_1(x) + c_1 + n * d)/d) - n,
2617 * where floor((f_1(x) + c_1 + n * d)/d) = floor((f2(x) + c_2)/d)
2618 * after simplification with respect to the equality constraints.
2620 static isl_stat
harmonize_divs_with_hulls(struct isl_coalesce_info
*info1
,
2621 struct isl_coalesce_info
*info2
, __isl_keep isl_basic_set
*eq1
,
2622 __isl_keep isl_basic_set
*eq2
)
2626 isl_local_space
*ls1
, *ls2
;
2628 total
= isl_basic_map_dim(info1
->bmap
, isl_dim_all
);
2630 return isl_stat_error
;
2631 ls1
= isl_local_space_wrap(isl_basic_map_get_local_space(info1
->bmap
));
2632 ls2
= isl_local_space_wrap(isl_basic_map_get_local_space(info2
->bmap
));
2633 for (i
= 0; i
< info1
->bmap
->n_div
; ++i
) {
2635 isl_aff
*div1
, *div2
;
2637 if (!isl_local_space_div_is_known(ls1
, i
) ||
2638 !isl_local_space_div_is_known(ls2
, i
))
2640 if (isl_int_ne(info1
->bmap
->div
[i
][0], info2
->bmap
->div
[i
][0]))
2642 if (isl_seq_eq(info1
->bmap
->div
[i
] + 1,
2643 info2
->bmap
->div
[i
] + 1, 1 + total
))
2645 div1
= isl_local_space_get_div(ls1
, i
);
2646 div2
= isl_local_space_get_div(ls2
, i
);
2647 div1
= isl_aff_substitute_equalities(div1
,
2648 isl_basic_set_copy(eq2
));
2649 div2
= isl_aff_substitute_equalities(div2
,
2650 isl_basic_set_copy(eq1
));
2651 div2
= isl_aff_sub(div2
, div1
);
2652 r
= shift_if_cst_int(info1
, i
, div2
);
2657 isl_local_space_free(ls1
);
2658 isl_local_space_free(ls2
);
2660 if (i
< info1
->bmap
->n_div
)
2661 return isl_stat_error
;
2665 /* Check if some of the divs in the basic map represented by "info1"
2666 * are shifts of the corresponding divs in the basic map represented
2667 * by "info2". If so, align them with those of "info2".
2668 * Only do this if "info1" and "info2" have the same number
2669 * of integer divisions.
2671 * An integer division is considered to be a shift of another integer
2672 * division if, after simplification with respect to the equality
2673 * constraints of the other basic map, one is equal to the other
2676 * First check if pairs of integer divisions are equal to each other
2677 * despite the fact that they differ by a rational constant.
2678 * If so, try and arrange for them to have the same constant term.
2680 * Then, extract the equality constraints and continue with
2681 * harmonize_divs_with_hulls.
2683 * If the equality constraints of both basic maps are the same,
2684 * then there is no need to perform any shifting since
2685 * the coefficients of the integer divisions should have been
2686 * reduced in the same way.
2688 static isl_stat
harmonize_divs(struct isl_coalesce_info
*info1
,
2689 struct isl_coalesce_info
*info2
)
2692 isl_basic_map
*bmap1
, *bmap2
;
2693 isl_basic_set
*eq1
, *eq2
;
2696 if (!info1
->bmap
|| !info2
->bmap
)
2697 return isl_stat_error
;
2699 if (info1
->bmap
->n_div
!= info2
->bmap
->n_div
)
2701 if (info1
->bmap
->n_div
== 0)
2704 if (harmonize_stride_divs(info1
, info2
) < 0)
2705 return isl_stat_error
;
2707 bmap1
= isl_basic_map_copy(info1
->bmap
);
2708 bmap2
= isl_basic_map_copy(info2
->bmap
);
2709 eq1
= isl_basic_map_wrap(isl_basic_map_plain_affine_hull(bmap1
));
2710 eq2
= isl_basic_map_wrap(isl_basic_map_plain_affine_hull(bmap2
));
2711 equal
= isl_basic_set_plain_is_equal(eq1
, eq2
);
2717 r
= harmonize_divs_with_hulls(info1
, info2
, eq1
, eq2
);
2718 isl_basic_set_free(eq1
);
2719 isl_basic_set_free(eq2
);
2724 /* Do the two basic maps live in the same local space, i.e.,
2725 * do they have the same (known) divs?
2726 * If either basic map has any unknown divs, then we can only assume
2727 * that they do not live in the same local space.
2729 static isl_bool
same_divs(__isl_keep isl_basic_map
*bmap1
,
2730 __isl_keep isl_basic_map
*bmap2
)
2736 if (!bmap1
|| !bmap2
)
2737 return isl_bool_error
;
2738 if (bmap1
->n_div
!= bmap2
->n_div
)
2739 return isl_bool_false
;
2741 if (bmap1
->n_div
== 0)
2742 return isl_bool_true
;
2744 known
= isl_basic_map_divs_known(bmap1
);
2745 if (known
< 0 || !known
)
2747 known
= isl_basic_map_divs_known(bmap2
);
2748 if (known
< 0 || !known
)
2751 total
= isl_basic_map_dim(bmap1
, isl_dim_all
);
2753 return isl_bool_error
;
2754 for (i
= 0; i
< bmap1
->n_div
; ++i
)
2755 if (!isl_seq_eq(bmap1
->div
[i
], bmap2
->div
[i
], 2 + total
))
2756 return isl_bool_false
;
2758 return isl_bool_true
;
2761 /* Assuming that "tab" contains the equality constraints and
2762 * the initial inequality constraints of "bmap", copy the remaining
2763 * inequality constraints of "bmap" to "Tab".
2765 static isl_stat
copy_ineq(struct isl_tab
*tab
, __isl_keep isl_basic_map
*bmap
)
2770 return isl_stat_error
;
2772 n_ineq
= tab
->n_con
- tab
->n_eq
;
2773 for (i
= n_ineq
; i
< bmap
->n_ineq
; ++i
)
2774 if (isl_tab_add_ineq(tab
, bmap
->ineq
[i
]) < 0)
2775 return isl_stat_error
;
2780 /* Description of an integer division that is added
2781 * during an expansion.
2782 * "pos" is the position of the corresponding variable.
2783 * "cst" indicates whether this integer division has a fixed value.
2784 * "val" contains the fixed value, if the value is fixed.
2786 struct isl_expanded
{
2792 /* For each of the "n" integer division variables "expanded",
2793 * if the variable has a fixed value, then add two inequality
2794 * constraints expressing the fixed value.
2795 * Otherwise, add the corresponding div constraints.
2796 * The caller is responsible for removing the div constraints
2797 * that it added for all these "n" integer divisions.
2799 * The div constraints and the pair of inequality constraints
2800 * forcing the fixed value cannot both be added for a given variable
2801 * as the combination may render some of the original constraints redundant.
2802 * These would then be ignored during the coalescing detection,
2803 * while they could remain in the fused result.
2805 * The two added inequality constraints are
2810 * with "a" the variable and "v" its fixed value.
2811 * The facet corresponding to one of these two constraints is selected
2812 * in the tableau to ensure that the pair of inequality constraints
2813 * is treated as an equality constraint.
2815 * The information in info->ineq is thrown away because it was
2816 * computed in terms of div constraints, while some of those
2817 * have now been replaced by these pairs of inequality constraints.
2819 static isl_stat
fix_constant_divs(struct isl_coalesce_info
*info
,
2820 int n
, struct isl_expanded
*expanded
)
2826 o_div
= isl_basic_map_offset(info
->bmap
, isl_dim_div
) - 1;
2827 ineq
= isl_vec_alloc(isl_tab_get_ctx(info
->tab
), 1 + info
->tab
->n_var
);
2829 return isl_stat_error
;
2830 isl_seq_clr(ineq
->el
+ 1, info
->tab
->n_var
);
2832 for (i
= 0; i
< n
; ++i
) {
2833 if (!expanded
[i
].cst
) {
2834 info
->bmap
= isl_basic_map_extend_constraints(
2836 info
->bmap
= isl_basic_map_add_div_constraints(
2837 info
->bmap
, expanded
[i
].pos
- o_div
);
2839 isl_int_set_si(ineq
->el
[1 + expanded
[i
].pos
], -1);
2840 isl_int_set(ineq
->el
[0], expanded
[i
].val
);
2841 info
->bmap
= isl_basic_map_add_ineq(info
->bmap
,
2843 isl_int_set_si(ineq
->el
[1 + expanded
[i
].pos
], 1);
2844 isl_int_neg(ineq
->el
[0], expanded
[i
].val
);
2845 info
->bmap
= isl_basic_map_add_ineq(info
->bmap
,
2847 isl_int_set_si(ineq
->el
[1 + expanded
[i
].pos
], 0);
2849 if (copy_ineq(info
->tab
, info
->bmap
) < 0)
2851 if (expanded
[i
].cst
&&
2852 isl_tab_select_facet(info
->tab
, info
->tab
->n_con
- 1) < 0)
2861 return i
< n
? isl_stat_error
: isl_stat_ok
;
2864 /* Insert the "n" integer division variables "expanded"
2865 * into info->tab and info->bmap and
2866 * update info->ineq with respect to the redundant constraints
2867 * in the resulting tableau.
2868 * "bmap" contains the result of this insertion in info->bmap,
2869 * while info->bmap is the original version
2870 * of "bmap", i.e., the one that corresponds to the current
2871 * state of info->tab. The number of constraints in info->bmap
2872 * is assumed to be the same as the number of constraints
2873 * in info->tab. This is required to be able to detect
2874 * the extra constraints in "bmap".
2876 * In particular, introduce extra variables corresponding
2877 * to the extra integer divisions and add the div constraints
2878 * that were added to "bmap" after info->tab was created
2880 * Furthermore, check if these extra integer divisions happen
2881 * to attain a fixed integer value in info->tab.
2882 * If so, replace the corresponding div constraints by pairs
2883 * of inequality constraints that fix these
2884 * integer divisions to their single integer values.
2885 * Replace info->bmap by "bmap" to match the changes to info->tab.
2886 * info->ineq was computed without a tableau and therefore
2887 * does not take into account the redundant constraints
2888 * in the tableau. Mark them here.
2889 * There is no need to check the newly added div constraints
2890 * since they cannot be redundant.
2891 * The redundancy check is not performed when constants have been discovered
2892 * since info->ineq is completely thrown away in this case.
2894 static isl_stat
tab_insert_divs(struct isl_coalesce_info
*info
,
2895 int n
, struct isl_expanded
*expanded
, __isl_take isl_basic_map
*bmap
)
2899 struct isl_tab_undo
*snap
;
2903 return isl_stat_error
;
2904 if (info
->bmap
->n_eq
+ info
->bmap
->n_ineq
!= info
->tab
->n_con
)
2905 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_internal
,
2906 "original tableau does not correspond "
2907 "to original basic map", goto error
);
2909 if (isl_tab_extend_vars(info
->tab
, n
) < 0)
2911 if (isl_tab_extend_cons(info
->tab
, 2 * n
) < 0)
2914 for (i
= 0; i
< n
; ++i
) {
2915 if (isl_tab_insert_var(info
->tab
, expanded
[i
].pos
) < 0)
2919 snap
= isl_tab_snap(info
->tab
);
2921 n_ineq
= info
->tab
->n_con
- info
->tab
->n_eq
;
2922 if (copy_ineq(info
->tab
, bmap
) < 0)
2925 isl_basic_map_free(info
->bmap
);
2929 for (i
= 0; i
< n
; ++i
) {
2930 expanded
[i
].cst
= isl_tab_is_constant(info
->tab
,
2931 expanded
[i
].pos
, &expanded
[i
].val
);
2932 if (expanded
[i
].cst
< 0)
2933 return isl_stat_error
;
2934 if (expanded
[i
].cst
)
2939 if (isl_tab_rollback(info
->tab
, snap
) < 0)
2940 return isl_stat_error
;
2941 info
->bmap
= isl_basic_map_cow(info
->bmap
);
2942 info
->bmap
= isl_basic_map_free_inequality(info
->bmap
, 2 * n
);
2944 return isl_stat_error
;
2946 return fix_constant_divs(info
, n
, expanded
);
2949 n_eq
= info
->bmap
->n_eq
;
2950 for (i
= 0; i
< n_ineq
; ++i
) {
2951 if (isl_tab_is_redundant(info
->tab
, n_eq
+ i
))
2952 info
->ineq
[i
] = STATUS_REDUNDANT
;
2957 isl_basic_map_free(bmap
);
2958 return isl_stat_error
;
2961 /* Expand info->tab and info->bmap in the same way "bmap" was expanded
2962 * in isl_basic_map_expand_divs using the expansion "exp" and
2963 * update info->ineq with respect to the redundant constraints
2964 * in the resulting tableau. info->bmap is the original version
2965 * of "bmap", i.e., the one that corresponds to the current
2966 * state of info->tab. The number of constraints in info->bmap
2967 * is assumed to be the same as the number of constraints
2968 * in info->tab. This is required to be able to detect
2969 * the extra constraints in "bmap".
2971 * Extract the positions where extra local variables are introduced
2972 * from "exp" and call tab_insert_divs.
2974 static isl_stat
expand_tab(struct isl_coalesce_info
*info
, int *exp
,
2975 __isl_take isl_basic_map
*bmap
)
2978 struct isl_expanded
*expanded
;
2981 isl_size total
, n_div
;
2985 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
2986 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
2987 if (total
< 0 || n_div
< 0)
2988 return isl_stat_error
;
2989 pos
= total
- n_div
;
2990 extra_var
= total
- info
->tab
->n_var
;
2991 n
= n_div
- extra_var
;
2993 ctx
= isl_basic_map_get_ctx(bmap
);
2994 expanded
= isl_calloc_array(ctx
, struct isl_expanded
, extra_var
);
2995 if (extra_var
&& !expanded
)
3000 for (j
= 0; j
< n_div
; ++j
) {
3001 if (i
< n
&& exp
[i
] == j
) {
3005 expanded
[k
++].pos
= pos
+ j
;
3008 for (k
= 0; k
< extra_var
; ++k
)
3009 isl_int_init(expanded
[k
].val
);
3011 r
= tab_insert_divs(info
, extra_var
, expanded
, bmap
);
3013 for (k
= 0; k
< extra_var
; ++k
)
3014 isl_int_clear(expanded
[k
].val
);
3019 isl_basic_map_free(bmap
);
3020 return isl_stat_error
;
3023 /* Check if the union of the basic maps represented by info[i] and info[j]
3024 * can be represented by a single basic map,
3025 * after expanding the divs of info[i] to match those of info[j].
3026 * If so, replace the pair by the single basic map and return
3027 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
3028 * Otherwise, return isl_change_none.
3030 * The caller has already checked for info[j] being a subset of info[i].
3031 * If some of the divs of info[j] are unknown, then the expanded info[i]
3032 * will not have the corresponding div constraints. The other patterns
3033 * therefore cannot apply. Skip the computation in this case.
3035 * The expansion is performed using the divs "div" and expansion "exp"
3036 * computed by the caller.
3037 * info[i].bmap has already been expanded and the result is passed in
3039 * The "eq" and "ineq" fields of info[i] reflect the status of
3040 * the constraints of the expanded "bmap" with respect to info[j].tab.
3041 * However, inequality constraints that are redundant in info[i].tab
3042 * have not yet been marked as such because no tableau was available.
3044 * Replace info[i].bmap by "bmap" and expand info[i].tab as well,
3045 * updating info[i].ineq with respect to the redundant constraints.
3046 * Then try and coalesce the expanded info[i] with info[j],
3047 * reusing the information in info[i].eq and info[i].ineq.
3048 * If this does not result in any coalescing or if it results in info[j]
3049 * getting dropped (which should not happen in practice, since the case
3050 * of info[j] being a subset of info[i] has already been checked by
3051 * the caller), then revert info[i] to its original state.
3053 static enum isl_change
coalesce_expand_tab_divs(__isl_take isl_basic_map
*bmap
,
3054 int i
, int j
, struct isl_coalesce_info
*info
, __isl_keep isl_mat
*div
,
3058 isl_basic_map
*bmap_i
;
3059 struct isl_tab_undo
*snap
;
3060 enum isl_change change
= isl_change_none
;
3062 known
= isl_basic_map_divs_known(info
[j
].bmap
);
3063 if (known
< 0 || !known
) {
3064 clear_status(&info
[i
]);
3065 isl_basic_map_free(bmap
);
3066 return known
< 0 ? isl_change_error
: isl_change_none
;
3069 bmap_i
= isl_basic_map_copy(info
[i
].bmap
);
3070 snap
= isl_tab_snap(info
[i
].tab
);
3071 if (expand_tab(&info
[i
], exp
, bmap
) < 0)
3072 change
= isl_change_error
;
3074 init_status(&info
[j
]);
3075 if (change
== isl_change_none
)
3076 change
= coalesce_local_pair_reuse(i
, j
, info
);
3078 clear_status(&info
[i
]);
3079 if (change
!= isl_change_none
&& change
!= isl_change_drop_second
) {
3080 isl_basic_map_free(bmap_i
);
3082 isl_basic_map_free(info
[i
].bmap
);
3083 info
[i
].bmap
= bmap_i
;
3085 if (isl_tab_rollback(info
[i
].tab
, snap
) < 0)
3086 change
= isl_change_error
;
3092 /* Check if the union of "bmap" and the basic map represented by info[j]
3093 * can be represented by a single basic map,
3094 * after expanding the divs of "bmap" to match those of info[j].
3095 * If so, replace the pair by the single basic map and return
3096 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
3097 * Otherwise, return isl_change_none.
3099 * In particular, check if the expanded "bmap" contains the basic map
3100 * represented by the tableau info[j].tab.
3101 * The expansion is performed using the divs "div" and expansion "exp"
3102 * computed by the caller.
3103 * Then we check if all constraints of the expanded "bmap" are valid for
3106 * If "i" is not equal to -1, then "bmap" is equal to info[i].bmap.
3107 * In this case, the positions of the constraints of info[i].bmap
3108 * with respect to the basic map represented by info[j] are stored
3111 * If the expanded "bmap" does not contain the basic map
3112 * represented by the tableau info[j].tab and if "i" is not -1,
3113 * i.e., if the original "bmap" is info[i].bmap, then expand info[i].tab
3114 * as well and check if that results in coalescing.
3116 static enum isl_change
coalesce_with_expanded_divs(
3117 __isl_keep isl_basic_map
*bmap
, int i
, int j
,
3118 struct isl_coalesce_info
*info
, __isl_keep isl_mat
*div
, int *exp
)
3120 enum isl_change change
= isl_change_none
;
3121 struct isl_coalesce_info info_local
, *info_i
;
3123 info_i
= i
>= 0 ? &info
[i
] : &info_local
;
3124 init_status(info_i
);
3125 bmap
= isl_basic_map_copy(bmap
);
3126 bmap
= isl_basic_map_expand_divs(bmap
, isl_mat_copy(div
), exp
);
3127 bmap
= isl_basic_map_mark_final(bmap
);
3132 info_local
.bmap
= bmap
;
3133 info_i
->eq
= eq_status_in(bmap
, info
[j
].tab
);
3134 if (bmap
->n_eq
&& !info_i
->eq
)
3136 if (any_eq(info_i
, STATUS_ERROR
))
3138 if (any_eq(info_i
, STATUS_SEPARATE
))
3141 info_i
->ineq
= ineq_status_in(bmap
, NULL
, info
[j
].tab
);
3142 if (bmap
->n_ineq
&& !info_i
->ineq
)
3144 if (any_ineq(info_i
, STATUS_ERROR
))
3146 if (any_ineq(info_i
, STATUS_SEPARATE
))
3149 if (all(info_i
->eq
, 2 * bmap
->n_eq
, STATUS_VALID
) &&
3150 all(info_i
->ineq
, bmap
->n_ineq
, STATUS_VALID
)) {
3152 change
= isl_change_drop_second
;
3155 if (change
== isl_change_none
&& i
!= -1)
3156 return coalesce_expand_tab_divs(bmap
, i
, j
, info
, div
, exp
);
3159 isl_basic_map_free(bmap
);
3160 clear_status(info_i
);
3163 isl_basic_map_free(bmap
);
3164 clear_status(info_i
);
3165 return isl_change_error
;
3168 /* Check if the union of "bmap_i" and the basic map represented by info[j]
3169 * can be represented by a single basic map,
3170 * after aligning the divs of "bmap_i" to match those of info[j].
3171 * If so, replace the pair by the single basic map and return
3172 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
3173 * Otherwise, return isl_change_none.
3175 * In particular, check if "bmap_i" contains the basic map represented by
3176 * info[j] after aligning the divs of "bmap_i" to those of info[j].
3177 * Note that this can only succeed if the number of divs of "bmap_i"
3178 * is smaller than (or equal to) the number of divs of info[j].
3180 * We first check if the divs of "bmap_i" are all known and form a subset
3181 * of those of info[j].bmap. If so, we pass control over to
3182 * coalesce_with_expanded_divs.
3184 * If "i" is not equal to -1, then "bmap" is equal to info[i].bmap.
3186 static enum isl_change
coalesce_after_aligning_divs(
3187 __isl_keep isl_basic_map
*bmap_i
, int i
, int j
,
3188 struct isl_coalesce_info
*info
)
3191 isl_mat
*div_i
, *div_j
, *div
;
3195 enum isl_change change
;
3197 known
= isl_basic_map_divs_known(bmap_i
);
3199 return isl_change_error
;
3201 return isl_change_none
;
3203 ctx
= isl_basic_map_get_ctx(bmap_i
);
3205 div_i
= isl_basic_map_get_divs(bmap_i
);
3206 div_j
= isl_basic_map_get_divs(info
[j
].bmap
);
3208 if (!div_i
|| !div_j
)
3211 exp1
= isl_alloc_array(ctx
, int, div_i
->n_row
);
3212 exp2
= isl_alloc_array(ctx
, int, div_j
->n_row
);
3213 if ((div_i
->n_row
&& !exp1
) || (div_j
->n_row
&& !exp2
))
3216 div
= isl_merge_divs(div_i
, div_j
, exp1
, exp2
);
3220 if (div
->n_row
== div_j
->n_row
)
3221 change
= coalesce_with_expanded_divs(bmap_i
,
3222 i
, j
, info
, div
, exp1
);
3224 change
= isl_change_none
;
3228 isl_mat_free(div_i
);
3229 isl_mat_free(div_j
);
3236 isl_mat_free(div_i
);
3237 isl_mat_free(div_j
);
3240 return isl_change_error
;
3243 /* Check if basic map "j" is a subset of basic map "i" after
3244 * exploiting the extra equalities of "j" to simplify the divs of "i".
3245 * If so, remove basic map "j" and return isl_change_drop_second.
3247 * If "j" does not have any equalities or if they are the same
3248 * as those of "i", then we cannot exploit them to simplify the divs.
3249 * Similarly, if there are no divs in "i", then they cannot be simplified.
3250 * If, on the other hand, the affine hulls of "i" and "j" do not intersect,
3251 * then "j" cannot be a subset of "i".
3253 * Otherwise, we intersect "i" with the affine hull of "j" and then
3254 * check if "j" is a subset of the result after aligning the divs.
3255 * If so, then "j" is definitely a subset of "i" and can be removed.
3256 * Note that if after intersection with the affine hull of "j".
3257 * "i" still has more divs than "j", then there is no way we can
3258 * align the divs of "i" to those of "j".
3260 static enum isl_change
coalesce_subset_with_equalities(int i
, int j
,
3261 struct isl_coalesce_info
*info
)
3263 isl_basic_map
*hull_i
, *hull_j
, *bmap_i
;
3265 enum isl_change change
;
3267 if (info
[j
].bmap
->n_eq
== 0)
3268 return isl_change_none
;
3269 if (info
[i
].bmap
->n_div
== 0)
3270 return isl_change_none
;
3272 hull_i
= isl_basic_map_copy(info
[i
].bmap
);
3273 hull_i
= isl_basic_map_plain_affine_hull(hull_i
);
3274 hull_j
= isl_basic_map_copy(info
[j
].bmap
);
3275 hull_j
= isl_basic_map_plain_affine_hull(hull_j
);
3277 hull_j
= isl_basic_map_intersect(hull_j
, isl_basic_map_copy(hull_i
));
3278 equal
= isl_basic_map_plain_is_equal(hull_i
, hull_j
);
3279 empty
= isl_basic_map_plain_is_empty(hull_j
);
3280 isl_basic_map_free(hull_i
);
3282 if (equal
< 0 || equal
|| empty
< 0 || empty
) {
3283 isl_basic_map_free(hull_j
);
3284 if (equal
< 0 || empty
< 0)
3285 return isl_change_error
;
3286 return isl_change_none
;
3289 bmap_i
= isl_basic_map_copy(info
[i
].bmap
);
3290 bmap_i
= isl_basic_map_intersect(bmap_i
, hull_j
);
3292 return isl_change_error
;
3294 if (bmap_i
->n_div
> info
[j
].bmap
->n_div
) {
3295 isl_basic_map_free(bmap_i
);
3296 return isl_change_none
;
3299 change
= coalesce_after_aligning_divs(bmap_i
, -1, j
, info
);
3301 isl_basic_map_free(bmap_i
);
3306 /* Check if the union of and the basic maps represented by info[i] and info[j]
3307 * can be represented by a single basic map, by aligning or equating
3308 * their integer divisions.
3309 * If so, replace the pair by the single basic map and return
3310 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
3311 * Otherwise, return isl_change_none.
3313 * Note that we only perform any test if the number of divs is different
3314 * in the two basic maps. In case the number of divs is the same,
3315 * we have already established that the divs are different
3316 * in the two basic maps.
3317 * In particular, if the number of divs of basic map i is smaller than
3318 * the number of divs of basic map j, then we check if j is a subset of i
3321 static enum isl_change
coalesce_divs(int i
, int j
,
3322 struct isl_coalesce_info
*info
)
3324 enum isl_change change
= isl_change_none
;
3326 if (info
[i
].bmap
->n_div
< info
[j
].bmap
->n_div
)
3327 change
= coalesce_after_aligning_divs(info
[i
].bmap
, i
, j
, info
);
3328 if (change
!= isl_change_none
)
3331 if (info
[j
].bmap
->n_div
< info
[i
].bmap
->n_div
)
3332 change
= coalesce_after_aligning_divs(info
[j
].bmap
, j
, i
, info
);
3333 if (change
!= isl_change_none
)
3334 return invert_change(change
);
3336 change
= coalesce_subset_with_equalities(i
, j
, info
);
3337 if (change
!= isl_change_none
)
3340 change
= coalesce_subset_with_equalities(j
, i
, info
);
3341 if (change
!= isl_change_none
)
3342 return invert_change(change
);
3344 return isl_change_none
;
3347 /* Does "bmap" involve any divs that themselves refer to divs?
3349 static isl_bool
has_nested_div(__isl_keep isl_basic_map
*bmap
)
3355 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
3356 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
3357 if (total
< 0 || n_div
< 0)
3358 return isl_bool_error
;
3361 for (i
= 0; i
< n_div
; ++i
)
3362 if (isl_seq_first_non_zero(bmap
->div
[i
] + 2 + total
,
3364 return isl_bool_true
;
3366 return isl_bool_false
;
3369 /* Return a list of affine expressions, one for each integer division
3370 * in "bmap_i". For each integer division that also appears in "bmap_j",
3371 * the affine expression is set to NaN. The number of NaNs in the list
3372 * is equal to the number of integer divisions in "bmap_j".
3373 * For the other integer divisions of "bmap_i", the corresponding
3374 * element in the list is a purely affine expression equal to the integer
3375 * division in "hull".
3376 * If no such list can be constructed, then the number of elements
3377 * in the returned list is smaller than the number of integer divisions
3380 static __isl_give isl_aff_list
*set_up_substitutions(
3381 __isl_keep isl_basic_map
*bmap_i
, __isl_keep isl_basic_map
*bmap_j
,
3382 __isl_take isl_basic_map
*hull
)
3384 isl_size n_div_i
, n_div_j
, total
;
3386 isl_local_space
*ls
;
3387 isl_basic_set
*wrap_hull
;
3392 n_div_i
= isl_basic_map_dim(bmap_i
, isl_dim_div
);
3393 n_div_j
= isl_basic_map_dim(bmap_j
, isl_dim_div
);
3394 total
= isl_basic_map_dim(bmap_i
, isl_dim_all
);
3395 if (!hull
|| n_div_i
< 0 || n_div_j
< 0 || total
< 0)
3398 ctx
= isl_basic_map_get_ctx(hull
);
3401 ls
= isl_basic_map_get_local_space(bmap_i
);
3402 ls
= isl_local_space_wrap(ls
);
3403 wrap_hull
= isl_basic_map_wrap(hull
);
3405 aff_nan
= isl_aff_nan_on_domain(isl_local_space_copy(ls
));
3406 list
= isl_aff_list_alloc(ctx
, n_div_i
);
3409 for (i
= 0; i
< n_div_i
; ++i
) {
3414 isl_basic_map_equal_div_expr_part(bmap_i
, i
, bmap_j
, j
,
3417 list
= isl_aff_list_add(list
, isl_aff_copy(aff_nan
));
3420 if (n_div_i
- i
<= n_div_j
- j
)
3423 aff
= isl_local_space_get_div(ls
, i
);
3424 aff
= isl_aff_substitute_equalities(aff
,
3425 isl_basic_set_copy(wrap_hull
));
3426 aff
= isl_aff_floor(aff
);
3427 n_div
= isl_aff_dim(aff
, isl_dim_div
);
3435 list
= isl_aff_list_add(list
, aff
);
3438 isl_aff_free(aff_nan
);
3439 isl_local_space_free(ls
);
3440 isl_basic_set_free(wrap_hull
);
3444 isl_aff_free(aff_nan
);
3445 isl_local_space_free(ls
);
3446 isl_basic_set_free(wrap_hull
);
3447 isl_aff_list_free(list
);
3451 /* Add variables to info->bmap and info->tab corresponding to the elements
3452 * in "list" that are not set to NaN.
3453 * "extra_var" is the number of these elements.
3454 * "dim" is the offset in the variables of "tab" where we should
3455 * start considering the elements in "list".
3456 * When this function returns, the total number of variables in "tab"
3457 * is equal to "dim" plus the number of elements in "list".
3459 * The newly added existentially quantified variables are not given
3460 * an explicit representation because the corresponding div constraints
3461 * do not appear in info->bmap. These constraints are not added
3462 * to info->bmap because for internal consistency, they would need to
3463 * be added to info->tab as well, where they could combine with the equality
3464 * that is added later to result in constraints that do not hold
3465 * in the original input.
3467 static isl_stat
add_sub_vars(struct isl_coalesce_info
*info
,
3468 __isl_keep isl_aff_list
*list
, int dim
, int extra_var
)
3474 space
= isl_basic_map_get_space(info
->bmap
);
3475 info
->bmap
= isl_basic_map_cow(info
->bmap
);
3476 info
->bmap
= isl_basic_map_extend_space(info
->bmap
, space
,
3478 n
= isl_aff_list_n_aff(list
);
3479 if (!info
->bmap
|| n
< 0)
3480 return isl_stat_error
;
3481 for (i
= 0; i
< n
; ++i
) {
3485 aff
= isl_aff_list_get_aff(list
, i
);
3486 is_nan
= isl_aff_is_nan(aff
);
3489 return isl_stat_error
;
3493 if (isl_tab_insert_var(info
->tab
, dim
+ i
) < 0)
3494 return isl_stat_error
;
3495 d
= isl_basic_map_alloc_div(info
->bmap
);
3497 return isl_stat_error
;
3498 info
->bmap
= isl_basic_map_mark_div_unknown(info
->bmap
, d
);
3499 for (j
= d
; j
> i
; --j
)
3500 info
->bmap
= isl_basic_map_swap_div(info
->bmap
,
3503 return isl_stat_error
;
3509 /* For each element in "list" that is not set to NaN, fix the corresponding
3510 * variable in "tab" to the purely affine expression defined by the element.
3511 * "dim" is the offset in the variables of "tab" where we should
3512 * start considering the elements in "list".
3514 * This function assumes that a sufficient number of rows and
3515 * elements in the constraint array are available in the tableau.
3517 static isl_stat
add_sub_equalities(struct isl_tab
*tab
,
3518 __isl_keep isl_aff_list
*list
, int dim
)
3526 n
= isl_aff_list_n_aff(list
);
3528 return isl_stat_error
;
3530 ctx
= isl_tab_get_ctx(tab
);
3531 sub
= isl_vec_alloc(ctx
, 1 + dim
+ n
);
3533 return isl_stat_error
;
3534 isl_seq_clr(sub
->el
+ 1 + dim
, n
);
3536 for (i
= 0; i
< n
; ++i
) {
3537 aff
= isl_aff_list_get_aff(list
, i
);
3540 if (isl_aff_is_nan(aff
)) {
3544 isl_seq_cpy(sub
->el
, aff
->v
->el
+ 1, 1 + dim
);
3545 isl_int_neg(sub
->el
[1 + dim
+ i
], aff
->v
->el
[0]);
3546 if (isl_tab_add_eq(tab
, sub
->el
) < 0)
3548 isl_int_set_si(sub
->el
[1 + dim
+ i
], 0);
3557 return isl_stat_error
;
3560 /* Add variables to info->tab and info->bmap corresponding to the elements
3561 * in "list" that are not set to NaN. The value of the added variable
3562 * in info->tab is fixed to the purely affine expression defined by the element.
3563 * "dim" is the offset in the variables of info->tab where we should
3564 * start considering the elements in "list".
3565 * When this function returns, the total number of variables in info->tab
3566 * is equal to "dim" plus the number of elements in "list".
3568 static isl_stat
add_subs(struct isl_coalesce_info
*info
,
3569 __isl_keep isl_aff_list
*list
, int dim
)
3574 n
= isl_aff_list_n_aff(list
);
3576 return isl_stat_error
;
3578 extra_var
= n
- (info
->tab
->n_var
- dim
);
3580 if (isl_tab_extend_vars(info
->tab
, extra_var
) < 0)
3581 return isl_stat_error
;
3582 if (isl_tab_extend_cons(info
->tab
, 2 * extra_var
) < 0)
3583 return isl_stat_error
;
3584 if (add_sub_vars(info
, list
, dim
, extra_var
) < 0)
3585 return isl_stat_error
;
3587 return add_sub_equalities(info
->tab
, list
, dim
);
3590 /* Coalesce basic map "j" into basic map "i" after adding the extra integer
3591 * divisions in "i" but not in "j" to basic map "j", with values
3592 * specified by "list". The total number of elements in "list"
3593 * is equal to the number of integer divisions in "i", while the number
3594 * of NaN elements in the list is equal to the number of integer divisions
3597 * If no coalescing can be performed, then we need to revert basic map "j"
3598 * to its original state. We do the same if basic map "i" gets dropped
3599 * during the coalescing, even though this should not happen in practice
3600 * since we have already checked for "j" being a subset of "i"
3601 * before we reach this stage.
3603 static enum isl_change
coalesce_with_subs(int i
, int j
,
3604 struct isl_coalesce_info
*info
, __isl_keep isl_aff_list
*list
)
3606 isl_basic_map
*bmap_j
;
3607 struct isl_tab_undo
*snap
;
3608 isl_size dim
, n_div
;
3609 enum isl_change change
;
3611 bmap_j
= isl_basic_map_copy(info
[j
].bmap
);
3612 snap
= isl_tab_snap(info
[j
].tab
);
3614 dim
= isl_basic_map_dim(bmap_j
, isl_dim_all
);
3615 n_div
= isl_basic_map_dim(bmap_j
, isl_dim_div
);
3616 if (dim
< 0 || n_div
< 0)
3619 if (add_subs(&info
[j
], list
, dim
) < 0)
3622 change
= coalesce_local_pair(i
, j
, info
);
3623 if (change
!= isl_change_none
&& change
!= isl_change_drop_first
) {
3624 isl_basic_map_free(bmap_j
);
3626 isl_basic_map_free(info
[j
].bmap
);
3627 info
[j
].bmap
= bmap_j
;
3629 if (isl_tab_rollback(info
[j
].tab
, snap
) < 0)
3630 return isl_change_error
;
3635 isl_basic_map_free(bmap_j
);
3636 return isl_change_error
;
3639 /* Check if we can coalesce basic map "j" into basic map "i" after copying
3640 * those extra integer divisions in "i" that can be simplified away
3641 * using the extra equalities in "j".
3642 * All divs are assumed to be known and not contain any nested divs.
3644 * We first check if there are any extra equalities in "j" that we
3645 * can exploit. Then we check if every integer division in "i"
3646 * either already appears in "j" or can be simplified using the
3647 * extra equalities to a purely affine expression.
3648 * If these tests succeed, then we try to coalesce the two basic maps
3649 * by introducing extra dimensions in "j" corresponding to
3650 * the extra integer divsisions "i" fixed to the corresponding
3651 * purely affine expression.
3653 static enum isl_change
check_coalesce_into_eq(int i
, int j
,
3654 struct isl_coalesce_info
*info
)
3656 isl_size n_div_i
, n_div_j
, n
;
3657 isl_basic_map
*hull_i
, *hull_j
;
3658 isl_bool equal
, empty
;
3660 enum isl_change change
;
3662 n_div_i
= isl_basic_map_dim(info
[i
].bmap
, isl_dim_div
);
3663 n_div_j
= isl_basic_map_dim(info
[j
].bmap
, isl_dim_div
);
3664 if (n_div_i
< 0 || n_div_j
< 0)
3665 return isl_change_error
;
3666 if (n_div_i
<= n_div_j
)
3667 return isl_change_none
;
3668 if (info
[j
].bmap
->n_eq
== 0)
3669 return isl_change_none
;
3671 hull_i
= isl_basic_map_copy(info
[i
].bmap
);
3672 hull_i
= isl_basic_map_plain_affine_hull(hull_i
);
3673 hull_j
= isl_basic_map_copy(info
[j
].bmap
);
3674 hull_j
= isl_basic_map_plain_affine_hull(hull_j
);
3676 hull_j
= isl_basic_map_intersect(hull_j
, isl_basic_map_copy(hull_i
));
3677 equal
= isl_basic_map_plain_is_equal(hull_i
, hull_j
);
3678 empty
= isl_basic_map_plain_is_empty(hull_j
);
3679 isl_basic_map_free(hull_i
);
3681 if (equal
< 0 || empty
< 0)
3683 if (equal
|| empty
) {
3684 isl_basic_map_free(hull_j
);
3685 return isl_change_none
;
3688 list
= set_up_substitutions(info
[i
].bmap
, info
[j
].bmap
, hull_j
);
3690 return isl_change_error
;
3691 n
= isl_aff_list_n_aff(list
);
3693 change
= isl_change_error
;
3694 else if (n
< n_div_i
)
3695 change
= isl_change_none
;
3697 change
= coalesce_with_subs(i
, j
, info
, list
);
3699 isl_aff_list_free(list
);
3703 isl_basic_map_free(hull_j
);
3704 return isl_change_error
;
3707 /* Check if we can coalesce basic maps "i" and "j" after copying
3708 * those extra integer divisions in one of the basic maps that can
3709 * be simplified away using the extra equalities in the other basic map.
3710 * We require all divs to be known in both basic maps.
3711 * Furthermore, to simplify the comparison of div expressions,
3712 * we do not allow any nested integer divisions.
3714 static enum isl_change
check_coalesce_eq(int i
, int j
,
3715 struct isl_coalesce_info
*info
)
3717 isl_bool known
, nested
;
3718 enum isl_change change
;
3720 known
= isl_basic_map_divs_known(info
[i
].bmap
);
3721 if (known
< 0 || !known
)
3722 return known
< 0 ? isl_change_error
: isl_change_none
;
3723 known
= isl_basic_map_divs_known(info
[j
].bmap
);
3724 if (known
< 0 || !known
)
3725 return known
< 0 ? isl_change_error
: isl_change_none
;
3726 nested
= has_nested_div(info
[i
].bmap
);
3727 if (nested
< 0 || nested
)
3728 return nested
< 0 ? isl_change_error
: isl_change_none
;
3729 nested
= has_nested_div(info
[j
].bmap
);
3730 if (nested
< 0 || nested
)
3731 return nested
< 0 ? isl_change_error
: isl_change_none
;
3733 change
= check_coalesce_into_eq(i
, j
, info
);
3734 if (change
!= isl_change_none
)
3736 change
= check_coalesce_into_eq(j
, i
, info
);
3737 if (change
!= isl_change_none
)
3738 return invert_change(change
);
3740 return isl_change_none
;
3743 /* Check if the union of the given pair of basic maps
3744 * can be represented by a single basic map.
3745 * If so, replace the pair by the single basic map and return
3746 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
3747 * Otherwise, return isl_change_none.
3749 * We first check if the two basic maps live in the same local space,
3750 * after aligning the divs that differ by only an integer constant.
3751 * If so, we do the complete check. Otherwise, we check if they have
3752 * the same number of integer divisions and can be coalesced, if one is
3753 * an obvious subset of the other or if the extra integer divisions
3754 * of one basic map can be simplified away using the extra equalities
3755 * of the other basic map.
3757 * Note that trying to coalesce pairs of disjuncts with the same
3758 * number, but different local variables may drop the explicit
3759 * representation of some of these local variables.
3760 * This operation is therefore not performed when
3761 * the "coalesce_preserve_locals" option is set.
3763 static enum isl_change
coalesce_pair(int i
, int j
,
3764 struct isl_coalesce_info
*info
)
3768 enum isl_change change
;
3771 if (harmonize_divs(&info
[i
], &info
[j
]) < 0)
3772 return isl_change_error
;
3773 same
= same_divs(info
[i
].bmap
, info
[j
].bmap
);
3775 return isl_change_error
;
3777 return coalesce_local_pair(i
, j
, info
);
3779 ctx
= isl_basic_map_get_ctx(info
[i
].bmap
);
3780 preserve
= isl_options_get_coalesce_preserve_locals(ctx
);
3781 if (!preserve
&& info
[i
].bmap
->n_div
== info
[j
].bmap
->n_div
) {
3782 change
= coalesce_local_pair(i
, j
, info
);
3783 if (change
!= isl_change_none
)
3787 change
= coalesce_divs(i
, j
, info
);
3788 if (change
!= isl_change_none
)
3791 return check_coalesce_eq(i
, j
, info
);
3794 /* Return the maximum of "a" and "b".
3796 static int isl_max(int a
, int b
)
3798 return a
> b
? a
: b
;
3801 /* Pairwise coalesce the basic maps in the range [start1, end1[ of "info"
3802 * with those in the range [start2, end2[, skipping basic maps
3803 * that have been removed (either before or within this function).
3805 * For each basic map i in the first range, we check if it can be coalesced
3806 * with respect to any previously considered basic map j in the second range.
3807 * If i gets dropped (because it was a subset of some j), then
3808 * we can move on to the next basic map.
3809 * If j gets dropped, we need to continue checking against the other
3810 * previously considered basic maps.
3811 * If the two basic maps got fused, then we recheck the fused basic map
3812 * against the previously considered basic maps, starting at i + 1
3813 * (even if start2 is greater than i + 1).
3815 static int coalesce_range(isl_ctx
*ctx
, struct isl_coalesce_info
*info
,
3816 int start1
, int end1
, int start2
, int end2
)
3820 for (i
= end1
- 1; i
>= start1
; --i
) {
3821 if (info
[i
].removed
)
3823 for (j
= isl_max(i
+ 1, start2
); j
< end2
; ++j
) {
3824 enum isl_change changed
;
3826 if (info
[j
].removed
)
3828 if (info
[i
].removed
)
3829 isl_die(ctx
, isl_error_internal
,
3830 "basic map unexpectedly removed",
3832 changed
= coalesce_pair(i
, j
, info
);
3834 case isl_change_error
:
3836 case isl_change_none
:
3837 case isl_change_drop_second
:
3839 case isl_change_drop_first
:
3842 case isl_change_fuse
:
3852 /* Pairwise coalesce the basic maps described by the "n" elements of "info".
3854 * We consider groups of basic maps that live in the same apparent
3855 * affine hull and we first coalesce within such a group before we
3856 * coalesce the elements in the group with elements of previously
3857 * considered groups. If a fuse happens during the second phase,
3858 * then we also reconsider the elements within the group.
3860 static int coalesce(isl_ctx
*ctx
, int n
, struct isl_coalesce_info
*info
)
3864 for (end
= n
; end
> 0; end
= start
) {
3866 while (start
>= 1 &&
3867 info
[start
- 1].hull_hash
== info
[start
].hull_hash
)
3869 if (coalesce_range(ctx
, info
, start
, end
, start
, end
) < 0)
3871 if (coalesce_range(ctx
, info
, start
, end
, end
, n
) < 0)
3878 /* Update the basic maps in "map" based on the information in "info".
3879 * In particular, remove the basic maps that have been marked removed and
3880 * update the others based on the information in the corresponding tableau.
3881 * Since we detected implicit equalities without calling
3882 * isl_basic_map_gauss, we need to do it now.
3883 * Also call isl_basic_map_simplify if we may have lost the definition
3884 * of one or more integer divisions.
3886 static __isl_give isl_map
*update_basic_maps(__isl_take isl_map
*map
,
3887 int n
, struct isl_coalesce_info
*info
)
3894 for (i
= n
- 1; i
>= 0; --i
) {
3895 if (info
[i
].removed
) {
3896 isl_basic_map_free(map
->p
[i
]);
3897 if (i
!= map
->n
- 1)
3898 map
->p
[i
] = map
->p
[map
->n
- 1];
3903 info
[i
].bmap
= isl_basic_map_update_from_tab(info
[i
].bmap
,
3905 info
[i
].bmap
= isl_basic_map_gauss(info
[i
].bmap
, NULL
);
3906 if (info
[i
].simplify
)
3907 info
[i
].bmap
= isl_basic_map_simplify(info
[i
].bmap
);
3908 info
[i
].bmap
= isl_basic_map_finalize(info
[i
].bmap
);
3910 return isl_map_free(map
);
3911 ISL_F_SET(info
[i
].bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
3912 ISL_F_SET(info
[i
].bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
3913 isl_basic_map_free(map
->p
[i
]);
3914 map
->p
[i
] = info
[i
].bmap
;
3915 info
[i
].bmap
= NULL
;
3921 /* For each pair of basic maps in the map, check if the union of the two
3922 * can be represented by a single basic map.
3923 * If so, replace the pair by the single basic map and start over.
3925 * We factor out any (hidden) common factor from the constraint
3926 * coefficients to improve the detection of adjacent constraints.
3927 * Note that this function does not call isl_basic_map_gauss,
3928 * but it does make sure that only a single copy of the basic map
3929 * is affected. This means that isl_basic_map_gauss may have
3930 * to be called at the end of the computation (in update_basic_maps)
3931 * on this single copy to ensure that
3932 * the basic maps are not left in an unexpected state.
3934 * Since we are constructing the tableaus of the basic maps anyway,
3935 * we exploit them to detect implicit equalities and redundant constraints.
3936 * This also helps the coalescing as it can ignore the redundant constraints.
3937 * In order to avoid confusion, we make all implicit equalities explicit
3938 * in the basic maps. If the basic map only has a single reference
3939 * (this happens in particular if it was modified by
3940 * isl_basic_map_reduce_coefficients), then isl_basic_map_gauss
3941 * does not get called on the result. The call to
3942 * isl_basic_map_gauss in update_basic_maps resolves this as well.
3943 * For each basic map, we also compute the hash of the apparent affine hull
3944 * for use in coalesce.
3946 __isl_give isl_map
*isl_map_coalesce(__isl_take isl_map
*map
)
3951 struct isl_coalesce_info
*info
= NULL
;
3953 map
= isl_map_remove_empty_parts(map
);
3960 ctx
= isl_map_get_ctx(map
);
3961 map
= isl_map_sort_divs(map
);
3962 map
= isl_map_cow(map
);
3969 info
= isl_calloc_array(map
->ctx
, struct isl_coalesce_info
, n
);
3973 for (i
= 0; i
< map
->n
; ++i
) {
3974 map
->p
[i
] = isl_basic_map_reduce_coefficients(map
->p
[i
]);
3977 info
[i
].bmap
= isl_basic_map_copy(map
->p
[i
]);
3978 info
[i
].tab
= isl_tab_from_basic_map(info
[i
].bmap
, 0);
3981 if (!ISL_F_ISSET(info
[i
].bmap
, ISL_BASIC_MAP_NO_IMPLICIT
))
3982 if (isl_tab_detect_implicit_equalities(info
[i
].tab
) < 0)
3984 info
[i
].bmap
= isl_tab_make_equalities_explicit(info
[i
].tab
,
3988 if (!ISL_F_ISSET(info
[i
].bmap
, ISL_BASIC_MAP_NO_REDUNDANT
))
3989 if (isl_tab_detect_redundant(info
[i
].tab
) < 0)
3991 if (coalesce_info_set_hull_hash(&info
[i
]) < 0)
3994 for (i
= map
->n
- 1; i
>= 0; --i
)
3995 if (info
[i
].tab
->empty
)
3998 if (coalesce(ctx
, n
, info
) < 0)
4001 map
= update_basic_maps(map
, n
, info
);
4003 clear_coalesce_info(n
, info
);
4007 clear_coalesce_info(n
, info
);
4012 /* For each pair of basic sets in the set, check if the union of the two
4013 * can be represented by a single basic set.
4014 * If so, replace the pair by the single basic set and start over.
4016 struct isl_set
*isl_set_coalesce(struct isl_set
*set
)
4018 return set_from_map(isl_map_coalesce(set_to_map(set
)));