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
)
69 int *eq
= isl_calloc_array(bmap_i
->ctx
, int, 2 * bmap_i
->n_eq
);
75 dim
= isl_basic_map_total_dim(bmap_i
);
76 for (k
= 0; k
< bmap_i
->n_eq
; ++k
) {
77 for (l
= 0; l
< 2; ++l
) {
78 isl_seq_neg(bmap_i
->eq
[k
], bmap_i
->eq
[k
], 1+dim
);
79 eq
[2 * k
+ l
] = status_in(bmap_i
->eq
[k
], tab_j
);
80 if (eq
[2 * k
+ l
] == STATUS_ERROR
)
91 /* Compute the position of the inequalities of basic map "bmap_i"
92 * (also represented by "tab_i", if not NULL) with respect to the basic map
93 * represented by "tab_j".
95 static int *ineq_status_in(__isl_keep isl_basic_map
*bmap_i
,
96 struct isl_tab
*tab_i
, struct isl_tab
*tab_j
)
99 unsigned n_eq
= bmap_i
->n_eq
;
100 int *ineq
= isl_calloc_array(bmap_i
->ctx
, int, bmap_i
->n_ineq
);
105 for (k
= 0; k
< bmap_i
->n_ineq
; ++k
) {
106 if (tab_i
&& isl_tab_is_redundant(tab_i
, n_eq
+ k
)) {
107 ineq
[k
] = STATUS_REDUNDANT
;
110 ineq
[k
] = status_in(bmap_i
->ineq
[k
], tab_j
);
111 if (ineq
[k
] == STATUS_ERROR
)
113 if (ineq
[k
] == STATUS_SEPARATE
)
123 static int any(int *con
, unsigned len
, int status
)
127 for (i
= 0; i
< len
; ++i
)
128 if (con
[i
] == status
)
133 /* Return the first position of "status" in the list "con" of length "len".
134 * Return -1 if there is no such entry.
136 static int find(int *con
, unsigned len
, int status
)
140 for (i
= 0; i
< len
; ++i
)
141 if (con
[i
] == status
)
146 static int count(int *con
, unsigned len
, int status
)
151 for (i
= 0; i
< len
; ++i
)
152 if (con
[i
] == status
)
157 static int all(int *con
, unsigned len
, int status
)
161 for (i
= 0; i
< len
; ++i
) {
162 if (con
[i
] == STATUS_REDUNDANT
)
164 if (con
[i
] != status
)
170 /* Internal information associated to a basic map in a map
171 * that is to be coalesced by isl_map_coalesce.
173 * "bmap" is the basic map itself (or NULL if "removed" is set)
174 * "tab" is the corresponding tableau (or NULL if "removed" is set)
175 * "hull_hash" identifies the affine space in which "bmap" lives.
176 * "removed" is set if this basic map has been removed from the map
177 * "simplify" is set if this basic map may have some unknown integer
178 * divisions that were not present in the input basic maps. The basic
179 * map should then be simplified such that we may be able to find
180 * a definition among the constraints.
182 * "eq" and "ineq" are only set if we are currently trying to coalesce
183 * this basic map with another basic map, in which case they represent
184 * the position of the inequalities of this basic map with respect to
185 * the other basic map. The number of elements in the "eq" array
186 * is twice the number of equalities in the "bmap", corresponding
187 * to the two inequalities that make up each equality.
189 struct isl_coalesce_info
{
199 /* Is there any (half of an) equality constraint in the description
200 * of the basic map represented by "info" that
201 * has position "status" with respect to the other basic map?
203 static int any_eq(struct isl_coalesce_info
*info
, int status
)
207 n_eq
= isl_basic_map_n_equality(info
->bmap
);
208 return any(info
->eq
, 2 * n_eq
, status
);
211 /* Is there any inequality constraint in the description
212 * of the basic map represented by "info" that
213 * has position "status" with respect to the other basic map?
215 static int any_ineq(struct isl_coalesce_info
*info
, int status
)
219 n_ineq
= isl_basic_map_n_inequality(info
->bmap
);
220 return any(info
->ineq
, n_ineq
, status
);
223 /* Return the position of the first half on an equality constraint
224 * in the description of the basic map represented by "info" that
225 * has position "status" with respect to the other basic map.
226 * The returned value is twice the position of the equality constraint
227 * plus zero for the negative half and plus one for the positive half.
228 * Return -1 if there is no such entry.
230 static int find_eq(struct isl_coalesce_info
*info
, int status
)
234 n_eq
= isl_basic_map_n_equality(info
->bmap
);
235 return find(info
->eq
, 2 * n_eq
, status
);
238 /* Return the position of the first inequality constraint in the description
239 * of the basic map represented by "info" that
240 * has position "status" with respect to the other basic map.
241 * Return -1 if there is no such entry.
243 static int find_ineq(struct isl_coalesce_info
*info
, int status
)
247 n_ineq
= isl_basic_map_n_inequality(info
->bmap
);
248 return find(info
->ineq
, n_ineq
, status
);
251 /* Return the number of (halves of) equality constraints in the description
252 * of the basic map represented by "info" that
253 * have position "status" with respect to the other basic map.
255 static int count_eq(struct isl_coalesce_info
*info
, int status
)
259 n_eq
= isl_basic_map_n_equality(info
->bmap
);
260 return count(info
->eq
, 2 * n_eq
, status
);
263 /* Return the number of inequality constraints in the description
264 * of the basic map represented by "info" that
265 * have position "status" with respect to the other basic map.
267 static int count_ineq(struct isl_coalesce_info
*info
, int status
)
271 n_ineq
= isl_basic_map_n_inequality(info
->bmap
);
272 return count(info
->ineq
, n_ineq
, status
);
275 /* Are all non-redundant constraints of the basic map represented by "info"
276 * either valid or cut constraints with respect to the other basic map?
278 static int all_valid_or_cut(struct isl_coalesce_info
*info
)
282 for (i
= 0; i
< 2 * info
->bmap
->n_eq
; ++i
) {
283 if (info
->eq
[i
] == STATUS_REDUNDANT
)
285 if (info
->eq
[i
] == STATUS_VALID
)
287 if (info
->eq
[i
] == STATUS_CUT
)
292 for (i
= 0; i
< info
->bmap
->n_ineq
; ++i
) {
293 if (info
->ineq
[i
] == STATUS_REDUNDANT
)
295 if (info
->ineq
[i
] == STATUS_VALID
)
297 if (info
->ineq
[i
] == STATUS_CUT
)
305 /* Compute the hash of the (apparent) affine hull of info->bmap (with
306 * the existentially quantified variables removed) and store it
309 static int coalesce_info_set_hull_hash(struct isl_coalesce_info
*info
)
314 hull
= isl_basic_map_copy(info
->bmap
);
315 hull
= isl_basic_map_plain_affine_hull(hull
);
316 n_div
= isl_basic_map_dim(hull
, isl_dim_div
);
317 hull
= isl_basic_map_drop_constraints_involving_dims(hull
,
318 isl_dim_div
, 0, n_div
);
319 info
->hull_hash
= isl_basic_map_get_hash(hull
);
320 isl_basic_map_free(hull
);
322 return hull
? 0 : -1;
325 /* Free all the allocated memory in an array
326 * of "n" isl_coalesce_info elements.
328 static void clear_coalesce_info(int n
, struct isl_coalesce_info
*info
)
335 for (i
= 0; i
< n
; ++i
) {
336 isl_basic_map_free(info
[i
].bmap
);
337 isl_tab_free(info
[i
].tab
);
343 /* Clear the memory associated to"info".
344 * Gaussian elimination needs to be performed on the basic map
345 * before it gets freed because it may have been put
346 * in an inconsistent state in isl_map_coalesce while it may
347 * be shared with other maps.
349 static void clear(struct isl_coalesce_info
*info
)
351 info
->bmap
= isl_basic_map_gauss(info
->bmap
, NULL
);
352 info
->bmap
= isl_basic_map_free(info
->bmap
);
353 isl_tab_free(info
->tab
);
357 /* Drop the basic map represented by "info".
358 * That is, clear the memory associated to the entry and
359 * mark it as having been removed.
361 static void drop(struct isl_coalesce_info
*info
)
367 /* Exchange the information in "info1" with that in "info2".
369 static void exchange(struct isl_coalesce_info
*info1
,
370 struct isl_coalesce_info
*info2
)
372 struct isl_coalesce_info info
;
379 /* This type represents the kind of change that has been performed
380 * while trying to coalesce two basic maps.
382 * isl_change_none: nothing was changed
383 * isl_change_drop_first: the first basic map was removed
384 * isl_change_drop_second: the second basic map was removed
385 * isl_change_fuse: the two basic maps were replaced by a new basic map.
388 isl_change_error
= -1,
390 isl_change_drop_first
,
391 isl_change_drop_second
,
395 /* Update "change" based on an interchange of the first and the second
396 * basic map. That is, interchange isl_change_drop_first and
397 * isl_change_drop_second.
399 static enum isl_change
invert_change(enum isl_change change
)
402 case isl_change_error
:
403 return isl_change_error
;
404 case isl_change_none
:
405 return isl_change_none
;
406 case isl_change_drop_first
:
407 return isl_change_drop_second
;
408 case isl_change_drop_second
:
409 return isl_change_drop_first
;
410 case isl_change_fuse
:
411 return isl_change_fuse
;
414 return isl_change_error
;
417 /* Add the valid constraints of the basic map represented by "info"
418 * to "bmap". "len" is the size of the constraints.
419 * If only one of the pair of inequalities that make up an equality
420 * is valid, then add that inequality.
422 static __isl_give isl_basic_map
*add_valid_constraints(
423 __isl_take isl_basic_map
*bmap
, struct isl_coalesce_info
*info
,
431 for (k
= 0; k
< info
->bmap
->n_eq
; ++k
) {
432 if (info
->eq
[2 * k
] == STATUS_VALID
&&
433 info
->eq
[2 * k
+ 1] == STATUS_VALID
) {
434 l
= isl_basic_map_alloc_equality(bmap
);
436 return isl_basic_map_free(bmap
);
437 isl_seq_cpy(bmap
->eq
[l
], info
->bmap
->eq
[k
], len
);
438 } else if (info
->eq
[2 * k
] == STATUS_VALID
) {
439 l
= isl_basic_map_alloc_inequality(bmap
);
441 return isl_basic_map_free(bmap
);
442 isl_seq_neg(bmap
->ineq
[l
], info
->bmap
->eq
[k
], len
);
443 } else if (info
->eq
[2 * k
+ 1] == STATUS_VALID
) {
444 l
= isl_basic_map_alloc_inequality(bmap
);
446 return isl_basic_map_free(bmap
);
447 isl_seq_cpy(bmap
->ineq
[l
], info
->bmap
->eq
[k
], len
);
451 for (k
= 0; k
< info
->bmap
->n_ineq
; ++k
) {
452 if (info
->ineq
[k
] != STATUS_VALID
)
454 l
= isl_basic_map_alloc_inequality(bmap
);
456 return isl_basic_map_free(bmap
);
457 isl_seq_cpy(bmap
->ineq
[l
], info
->bmap
->ineq
[k
], len
);
463 /* Is "bmap" defined by a number of (non-redundant) constraints that
464 * is greater than the number of constraints of basic maps i and j combined?
465 * Equalities are counted as two inequalities.
467 static int number_of_constraints_increases(int i
, int j
,
468 struct isl_coalesce_info
*info
,
469 __isl_keep isl_basic_map
*bmap
, struct isl_tab
*tab
)
473 n_old
= 2 * info
[i
].bmap
->n_eq
+ info
[i
].bmap
->n_ineq
;
474 n_old
+= 2 * info
[j
].bmap
->n_eq
+ info
[j
].bmap
->n_ineq
;
476 n_new
= 2 * bmap
->n_eq
;
477 for (k
= 0; k
< bmap
->n_ineq
; ++k
)
478 if (!isl_tab_is_redundant(tab
, bmap
->n_eq
+ k
))
481 return n_new
> n_old
;
484 /* Replace the pair of basic maps i and j by the basic map bounded
485 * by the valid constraints in both basic maps and the constraints
486 * in extra (if not NULL).
487 * Place the fused basic map in the position that is the smallest of i and j.
489 * If "detect_equalities" is set, then look for equalities encoded
490 * as pairs of inequalities.
491 * If "check_number" is set, then the original basic maps are only
492 * replaced if the total number of constraints does not increase.
493 * While the number of integer divisions in the two basic maps
494 * is assumed to be the same, the actual definitions may be different.
495 * We only copy the definition from one of the basic map if it is
496 * the same as that of the other basic map. Otherwise, we mark
497 * the integer division as unknown and simplify the basic map
498 * in an attempt to recover the integer division definition.
500 static enum isl_change
fuse(int i
, int j
, struct isl_coalesce_info
*info
,
501 __isl_keep isl_mat
*extra
, int detect_equalities
, int check_number
)
504 struct isl_basic_map
*fused
= NULL
;
505 struct isl_tab
*fused_tab
= NULL
;
506 unsigned total
= isl_basic_map_total_dim(info
[i
].bmap
);
507 unsigned extra_rows
= extra
? extra
->n_row
: 0;
508 unsigned n_eq
, n_ineq
;
512 return fuse(j
, i
, info
, extra
, detect_equalities
, check_number
);
514 n_eq
= info
[i
].bmap
->n_eq
+ info
[j
].bmap
->n_eq
;
515 n_ineq
= info
[i
].bmap
->n_ineq
+ info
[j
].bmap
->n_ineq
;
516 fused
= isl_basic_map_alloc_space(isl_space_copy(info
[i
].bmap
->dim
),
517 info
[i
].bmap
->n_div
, n_eq
, n_eq
+ n_ineq
+ extra_rows
);
518 fused
= add_valid_constraints(fused
, &info
[i
], 1 + total
);
519 fused
= add_valid_constraints(fused
, &info
[j
], 1 + total
);
522 if (ISL_F_ISSET(info
[i
].bmap
, ISL_BASIC_MAP_RATIONAL
) &&
523 ISL_F_ISSET(info
[j
].bmap
, ISL_BASIC_MAP_RATIONAL
))
524 ISL_F_SET(fused
, ISL_BASIC_MAP_RATIONAL
);
526 for (k
= 0; k
< info
[i
].bmap
->n_div
; ++k
) {
527 int l
= isl_basic_map_alloc_div(fused
);
530 if (isl_seq_eq(info
[i
].bmap
->div
[k
], info
[j
].bmap
->div
[k
],
532 isl_seq_cpy(fused
->div
[l
], info
[i
].bmap
->div
[k
],
535 isl_int_set_si(fused
->div
[l
][0], 0);
540 for (k
= 0; k
< extra_rows
; ++k
) {
541 l
= isl_basic_map_alloc_inequality(fused
);
544 isl_seq_cpy(fused
->ineq
[l
], extra
->row
[k
], 1 + total
);
547 if (detect_equalities
)
548 fused
= isl_basic_map_detect_inequality_pairs(fused
, NULL
);
549 fused
= isl_basic_map_gauss(fused
, NULL
);
550 if (simplify
|| info
[j
].simplify
) {
551 fused
= isl_basic_map_simplify(fused
);
552 info
[i
].simplify
= 0;
554 fused
= isl_basic_map_finalize(fused
);
556 fused_tab
= isl_tab_from_basic_map(fused
, 0);
557 if (isl_tab_detect_redundant(fused_tab
) < 0)
561 number_of_constraints_increases(i
, j
, info
, fused
, fused_tab
)) {
562 isl_tab_free(fused_tab
);
563 isl_basic_map_free(fused
);
564 return isl_change_none
;
568 info
[i
].bmap
= fused
;
569 info
[i
].tab
= fused_tab
;
572 return isl_change_fuse
;
574 isl_tab_free(fused_tab
);
575 isl_basic_map_free(fused
);
576 return isl_change_error
;
579 /* Given a pair of basic maps i and j such that all constraints are either
580 * "valid" or "cut", check if the facets corresponding to the "cut"
581 * constraints of i lie entirely within basic map j.
582 * If so, replace the pair by the basic map consisting of the valid
583 * constraints in both basic maps.
584 * Checking whether the facet lies entirely within basic map j
585 * is performed by checking whether the constraints of basic map j
586 * are valid for the facet. These tests are performed on a rational
587 * tableau to avoid the theoretical possibility that a constraint
588 * that was considered to be a cut constraint for the entire basic map i
589 * happens to be considered to be a valid constraint for the facet,
590 * even though it cuts off the same rational points.
592 * To see that we are not introducing any extra points, call the
593 * two basic maps A and B and the resulting map U and let x
594 * be an element of U \setminus ( A \cup B ).
595 * A line connecting x with an element of A \cup B meets a facet F
596 * of either A or B. Assume it is a facet of B and let c_1 be
597 * the corresponding facet constraint. We have c_1(x) < 0 and
598 * so c_1 is a cut constraint. This implies that there is some
599 * (possibly rational) point x' satisfying the constraints of A
600 * and the opposite of c_1 as otherwise c_1 would have been marked
601 * valid for A. The line connecting x and x' meets a facet of A
602 * in a (possibly rational) point that also violates c_1, but this
603 * is impossible since all cut constraints of B are valid for all
605 * In case F is a facet of A rather than B, then we can apply the
606 * above reasoning to find a facet of B separating x from A \cup B first.
608 static enum isl_change
check_facets(int i
, int j
,
609 struct isl_coalesce_info
*info
)
612 struct isl_tab_undo
*snap
, *snap2
;
613 unsigned n_eq
= info
[i
].bmap
->n_eq
;
615 snap
= isl_tab_snap(info
[i
].tab
);
616 if (isl_tab_mark_rational(info
[i
].tab
) < 0)
617 return isl_change_error
;
618 snap2
= isl_tab_snap(info
[i
].tab
);
620 for (k
= 0; k
< info
[i
].bmap
->n_ineq
; ++k
) {
621 if (info
[i
].ineq
[k
] != STATUS_CUT
)
623 if (isl_tab_select_facet(info
[i
].tab
, n_eq
+ k
) < 0)
624 return isl_change_error
;
625 for (l
= 0; l
< info
[j
].bmap
->n_ineq
; ++l
) {
627 if (info
[j
].ineq
[l
] != STATUS_CUT
)
629 stat
= status_in(info
[j
].bmap
->ineq
[l
], info
[i
].tab
);
631 return isl_change_error
;
632 if (stat
!= STATUS_VALID
)
635 if (isl_tab_rollback(info
[i
].tab
, snap2
) < 0)
636 return isl_change_error
;
637 if (l
< info
[j
].bmap
->n_ineq
)
641 if (k
< info
[i
].bmap
->n_ineq
) {
642 if (isl_tab_rollback(info
[i
].tab
, snap
) < 0)
643 return isl_change_error
;
644 return isl_change_none
;
646 return fuse(i
, j
, info
, NULL
, 0, 0);
649 /* Check if info->bmap contains the basic map represented
650 * by the tableau "tab".
651 * For each equality, we check both the constraint itself
652 * (as an inequality) and its negation. Make sure the
653 * equality is returned to its original state before returning.
655 static isl_bool
contains(struct isl_coalesce_info
*info
, struct isl_tab
*tab
)
659 isl_basic_map
*bmap
= info
->bmap
;
661 dim
= isl_basic_map_total_dim(bmap
);
662 for (k
= 0; k
< bmap
->n_eq
; ++k
) {
664 isl_seq_neg(bmap
->eq
[k
], bmap
->eq
[k
], 1 + dim
);
665 stat
= status_in(bmap
->eq
[k
], tab
);
666 isl_seq_neg(bmap
->eq
[k
], bmap
->eq
[k
], 1 + dim
);
668 return isl_bool_error
;
669 if (stat
!= STATUS_VALID
)
670 return isl_bool_false
;
671 stat
= status_in(bmap
->eq
[k
], tab
);
673 return isl_bool_error
;
674 if (stat
!= STATUS_VALID
)
675 return isl_bool_false
;
678 for (k
= 0; k
< bmap
->n_ineq
; ++k
) {
680 if (info
->ineq
[k
] == STATUS_REDUNDANT
)
682 stat
= status_in(bmap
->ineq
[k
], tab
);
684 return isl_bool_error
;
685 if (stat
!= STATUS_VALID
)
686 return isl_bool_false
;
688 return isl_bool_true
;
691 /* Basic map "i" has an inequality (say "k") that is adjacent
692 * to some inequality of basic map "j". All the other inequalities
694 * Check if basic map "j" forms an extension of basic map "i".
696 * Note that this function is only called if some of the equalities or
697 * inequalities of basic map "j" do cut basic map "i". The function is
698 * correct even if there are no such cut constraints, but in that case
699 * the additional checks performed by this function are overkill.
701 * In particular, we replace constraint k, say f >= 0, by constraint
702 * f <= -1, add the inequalities of "j" that are valid for "i"
703 * and check if the result is a subset of basic map "j".
704 * To improve the chances of the subset relation being detected,
705 * any variable that only attains a single integer value
706 * in the tableau of "i" is first fixed to that value.
707 * If the result is a subset, then we know that this result is exactly equal
708 * to basic map "j" since all its constraints are valid for basic map "j".
709 * By combining the valid constraints of "i" (all equalities and all
710 * inequalities except "k") and the valid constraints of "j" we therefore
711 * obtain a basic map that is equal to their union.
712 * In this case, there is no need to perform a rollback of the tableau
713 * since it is going to be destroyed in fuse().
719 * |_______| _ |_________\
731 static enum isl_change
is_adj_ineq_extension(int i
, int j
,
732 struct isl_coalesce_info
*info
)
735 struct isl_tab_undo
*snap
;
736 unsigned n_eq
= info
[i
].bmap
->n_eq
;
737 unsigned total
= isl_basic_map_total_dim(info
[i
].bmap
);
741 if (isl_tab_extend_cons(info
[i
].tab
, 1 + info
[j
].bmap
->n_ineq
) < 0)
742 return isl_change_error
;
744 k
= find_ineq(&info
[i
], STATUS_ADJ_INEQ
);
746 isl_die(isl_basic_map_get_ctx(info
[i
].bmap
), isl_error_internal
,
747 "info[i].ineq should have exactly one STATUS_ADJ_INEQ",
748 return isl_change_error
);
750 snap
= isl_tab_snap(info
[i
].tab
);
752 if (isl_tab_unrestrict(info
[i
].tab
, n_eq
+ k
) < 0)
753 return isl_change_error
;
755 isl_seq_neg(info
[i
].bmap
->ineq
[k
], info
[i
].bmap
->ineq
[k
], 1 + total
);
756 isl_int_sub_ui(info
[i
].bmap
->ineq
[k
][0], info
[i
].bmap
->ineq
[k
][0], 1);
757 r
= isl_tab_add_ineq(info
[i
].tab
, info
[i
].bmap
->ineq
[k
]);
758 isl_seq_neg(info
[i
].bmap
->ineq
[k
], info
[i
].bmap
->ineq
[k
], 1 + total
);
759 isl_int_sub_ui(info
[i
].bmap
->ineq
[k
][0], info
[i
].bmap
->ineq
[k
][0], 1);
761 return isl_change_error
;
763 for (k
= 0; k
< info
[j
].bmap
->n_ineq
; ++k
) {
764 if (info
[j
].ineq
[k
] != STATUS_VALID
)
766 if (isl_tab_add_ineq(info
[i
].tab
, info
[j
].bmap
->ineq
[k
]) < 0)
767 return isl_change_error
;
769 if (isl_tab_detect_constants(info
[i
].tab
) < 0)
770 return isl_change_error
;
772 super
= contains(&info
[j
], info
[i
].tab
);
774 return isl_change_error
;
776 return fuse(i
, j
, info
, NULL
, 0, 0);
778 if (isl_tab_rollback(info
[i
].tab
, snap
) < 0)
779 return isl_change_error
;
781 return isl_change_none
;
785 /* Both basic maps have at least one inequality with and adjacent
786 * (but opposite) inequality in the other basic map.
787 * Check that there are no cut constraints and that there is only
788 * a single pair of adjacent inequalities.
789 * If so, we can replace the pair by a single basic map described
790 * by all but the pair of adjacent inequalities.
791 * Any additional points introduced lie strictly between the two
792 * adjacent hyperplanes and can therefore be integral.
801 * The test for a single pair of adjancent inequalities is important
802 * for avoiding the combination of two basic maps like the following
812 * If there are some cut constraints on one side, then we may
813 * still be able to fuse the two basic maps, but we need to perform
814 * some additional checks in is_adj_ineq_extension.
816 static enum isl_change
check_adj_ineq(int i
, int j
,
817 struct isl_coalesce_info
*info
)
819 int count_i
, count_j
;
822 count_i
= count_ineq(&info
[i
], STATUS_ADJ_INEQ
);
823 count_j
= count_ineq(&info
[j
], STATUS_ADJ_INEQ
);
825 if (count_i
!= 1 && count_j
!= 1)
826 return isl_change_none
;
828 cut_i
= any_eq(&info
[i
], STATUS_CUT
) || any_ineq(&info
[i
], STATUS_CUT
);
829 cut_j
= any_eq(&info
[j
], STATUS_CUT
) || any_ineq(&info
[j
], STATUS_CUT
);
831 if (!cut_i
&& !cut_j
&& count_i
== 1 && count_j
== 1)
832 return fuse(i
, j
, info
, NULL
, 0, 0);
834 if (count_i
== 1 && !cut_i
)
835 return is_adj_ineq_extension(i
, j
, info
);
837 if (count_j
== 1 && !cut_j
)
838 return is_adj_ineq_extension(j
, i
, info
);
840 return isl_change_none
;
843 /* Given an affine transformation matrix "T", does row "row" represent
844 * anything other than a unit vector (possibly shifted by a constant)
845 * that is not involved in any of the other rows?
847 * That is, if a constraint involves the variable corresponding to
848 * the row, then could its preimage by "T" have any coefficients
849 * that are different from those in the original constraint?
851 static int not_unique_unit_row(__isl_keep isl_mat
*T
, int row
)
854 int len
= T
->n_col
- 1;
856 i
= isl_seq_first_non_zero(T
->row
[row
] + 1, len
);
859 if (!isl_int_is_one(T
->row
[row
][1 + i
]) &&
860 !isl_int_is_negone(T
->row
[row
][1 + i
]))
863 j
= isl_seq_first_non_zero(T
->row
[row
] + 1 + i
+ 1, len
- (i
+ 1));
867 for (j
= 1; j
< T
->n_row
; ++j
) {
870 if (!isl_int_is_zero(T
->row
[j
][1 + i
]))
877 /* Does inequality constraint "ineq" of "bmap" involve any of
878 * the variables marked in "affected"?
879 * "total" is the total number of variables, i.e., the number
880 * of entries in "affected".
882 static isl_bool
is_affected(__isl_keep isl_basic_map
*bmap
, int ineq
,
883 int *affected
, int total
)
887 for (i
= 0; i
< total
; ++i
) {
890 if (!isl_int_is_zero(bmap
->ineq
[ineq
][1 + i
]))
891 return isl_bool_true
;
894 return isl_bool_false
;
897 /* Given the compressed version of inequality constraint "ineq"
898 * of info->bmap in "v", check if the constraint can be tightened,
899 * where the compression is based on an equality constraint valid
901 * If so, add the tightened version of the inequality constraint
902 * to info->tab. "v" may be modified by this function.
904 * That is, if the compressed constraint is of the form
908 * with 0 < c < m, then it is equivalent to
912 * This means that c can also be subtracted from the original,
913 * uncompressed constraint without affecting the integer points
914 * in info->tab. Add this tightened constraint as an extra row
915 * to info->tab to make this information explicitly available.
917 static __isl_give isl_vec
*try_tightening(struct isl_coalesce_info
*info
,
918 int ineq
, __isl_take isl_vec
*v
)
926 ctx
= isl_vec_get_ctx(v
);
927 isl_seq_gcd(v
->el
+ 1, v
->size
- 1, &ctx
->normalize_gcd
);
928 if (isl_int_is_zero(ctx
->normalize_gcd
) ||
929 isl_int_is_one(ctx
->normalize_gcd
)) {
937 isl_int_fdiv_r(v
->el
[0], v
->el
[0], ctx
->normalize_gcd
);
938 if (isl_int_is_zero(v
->el
[0]))
941 if (isl_tab_extend_cons(info
->tab
, 1) < 0)
942 return isl_vec_free(v
);
944 isl_int_sub(info
->bmap
->ineq
[ineq
][0],
945 info
->bmap
->ineq
[ineq
][0], v
->el
[0]);
946 r
= isl_tab_add_ineq(info
->tab
, info
->bmap
->ineq
[ineq
]);
947 isl_int_add(info
->bmap
->ineq
[ineq
][0],
948 info
->bmap
->ineq
[ineq
][0], v
->el
[0]);
951 return isl_vec_free(v
);
956 /* Tighten the (non-redundant) constraints on the facet represented
958 * In particular, on input, info->tab represents the result
959 * of relaxing the "n" inequality constraints of info->bmap in "relaxed"
960 * by one, i.e., replacing f_i >= 0 by f_i + 1 >= 0, and then
961 * replacing the one at index "l" by the corresponding equality,
962 * i.e., f_k + 1 = 0, with k = relaxed[l].
964 * Compute a variable compression from the equality constraint f_k + 1 = 0
965 * and use it to tighten the other constraints of info->bmap
966 * (that is, all constraints that have not been relaxed),
967 * updating info->tab (and leaving info->bmap untouched).
968 * The compression handles essentially two cases, one where a variable
969 * is assigned a fixed value and can therefore be eliminated, and one
970 * where one variable is a shifted multiple of some other variable and
971 * can therefore be replaced by that multiple.
972 * Gaussian elimination would also work for the first case, but for
973 * the second case, the effectiveness would depend on the order
975 * After compression, some of the constraints may have coefficients
976 * with a common divisor. If this divisor does not divide the constant
977 * term, then the constraint can be tightened.
978 * The tightening is performed on the tableau info->tab by introducing
979 * extra (temporary) constraints.
981 * Only constraints that are possibly affected by the compression are
982 * considered. In particular, if the constraint only involves variables
983 * that are directly mapped to a distinct set of other variables, then
984 * no common divisor can be introduced and no tightening can occur.
986 * It is important to only consider the non-redundant constraints
987 * since the facet constraint has been relaxed prior to the call
988 * to this function, meaning that the constraints that were redundant
989 * prior to the relaxation may no longer be redundant.
990 * These constraints will be ignored in the fused result, so
991 * the fusion detection should not exploit them.
993 static isl_stat
tighten_on_relaxed_facet(struct isl_coalesce_info
*info
,
994 int n
, int *relaxed
, int l
)
1005 ctx
= isl_basic_map_get_ctx(info
->bmap
);
1006 total
= isl_basic_map_total_dim(info
->bmap
);
1007 isl_int_add_ui(info
->bmap
->ineq
[k
][0], info
->bmap
->ineq
[k
][0], 1);
1008 T
= isl_mat_sub_alloc6(ctx
, info
->bmap
->ineq
, k
, 1, 0, 1 + total
);
1009 T
= isl_mat_variable_compression(T
, NULL
);
1010 isl_int_sub_ui(info
->bmap
->ineq
[k
][0], info
->bmap
->ineq
[k
][0], 1);
1012 return isl_stat_error
;
1013 if (T
->n_col
== 0) {
1018 affected
= isl_alloc_array(ctx
, int, total
);
1022 for (i
= 0; i
< total
; ++i
)
1023 affected
[i
] = not_unique_unit_row(T
, 1 + i
);
1025 for (i
= 0; i
< info
->bmap
->n_ineq
; ++i
) {
1027 if (any(relaxed
, n
, i
))
1029 if (info
->ineq
[i
] == STATUS_REDUNDANT
)
1031 handle
= is_affected(info
->bmap
, i
, affected
, total
);
1036 v
= isl_vec_alloc(ctx
, 1 + total
);
1039 isl_seq_cpy(v
->el
, info
->bmap
->ineq
[i
], 1 + total
);
1040 v
= isl_vec_mat_product(v
, isl_mat_copy(T
));
1041 v
= try_tightening(info
, i
, v
);
1053 return isl_stat_error
;
1056 /* Replace the basic maps "i" and "j" by an extension of "i"
1057 * along the "n" inequality constraints in "relax" by one.
1058 * The tableau info[i].tab has already been extended.
1059 * Extend info[i].bmap accordingly by relaxing all constraints in "relax"
1061 * Each integer division that does not have exactly the same
1062 * definition in "i" and "j" is marked unknown and the basic map
1063 * is scheduled to be simplified in an attempt to recover
1064 * the integer division definition.
1065 * Place the extension in the position that is the smallest of i and j.
1067 static enum isl_change
extend(int i
, int j
, int n
, int *relax
,
1068 struct isl_coalesce_info
*info
)
1073 info
[i
].bmap
= isl_basic_map_cow(info
[i
].bmap
);
1075 return isl_change_error
;
1076 total
= isl_basic_map_total_dim(info
[i
].bmap
);
1077 for (l
= 0; l
< info
[i
].bmap
->n_div
; ++l
)
1078 if (!isl_seq_eq(info
[i
].bmap
->div
[l
],
1079 info
[j
].bmap
->div
[l
], 1 + 1 + total
)) {
1080 isl_int_set_si(info
[i
].bmap
->div
[l
][0], 0);
1081 info
[i
].simplify
= 1;
1083 for (l
= 0; l
< n
; ++l
)
1084 isl_int_add_ui(info
[i
].bmap
->ineq
[relax
[l
]][0],
1085 info
[i
].bmap
->ineq
[relax
[l
]][0], 1);
1086 ISL_F_SET(info
[i
].bmap
, ISL_BASIC_MAP_FINAL
);
1089 exchange(&info
[i
], &info
[j
]);
1090 return isl_change_fuse
;
1093 /* Basic map "i" has "n" inequality constraints (collected in "relax")
1094 * that are such that they include basic map "j" if they are relaxed
1095 * by one. All the other inequalities are valid for "j".
1096 * Check if basic map "j" forms an extension of basic map "i".
1098 * In particular, relax the constraints in "relax", compute the corresponding
1099 * facets one by one and check whether each of these is included
1100 * in the other basic map.
1101 * Before testing for inclusion, the constraints on each facet
1102 * are tightened to increase the chance of an inclusion being detected.
1103 * (Adding the valid constraints of "j" to the tableau of "i", as is done
1104 * in is_adj_ineq_extension, may further increase those chances, but this
1105 * is not currently done.)
1106 * If each facet is included, we know that relaxing the constraints extends
1107 * the basic map with exactly the other basic map (we already know that this
1108 * other basic map is included in the extension, because all other
1109 * inequality constraints are valid of "j") and we can replace the
1110 * two basic maps by this extension.
1112 * If any of the relaxed constraints turn out to be redundant, then bail out.
1113 * isl_tab_select_facet refuses to handle such constraints. It may be
1114 * possible to handle them anyway by making a distinction between
1115 * redundant constraints with a corresponding facet that still intersects
1116 * the set (allowing isl_tab_select_facet to handle them) and
1117 * those where the facet does not intersect the set (which can be ignored
1118 * because the empty facet is trivially included in the other disjunct).
1119 * However, relaxed constraints that turn out to be redundant should
1120 * be fairly rare and no such instance has been reported where
1121 * coalescing would be successful.
1137 static enum isl_change
is_relaxed_extension(int i
, int j
, int n
, int *relax
,
1138 struct isl_coalesce_info
*info
)
1142 struct isl_tab_undo
*snap
, *snap2
;
1143 unsigned n_eq
= info
[i
].bmap
->n_eq
;
1145 for (l
= 0; l
< n
; ++l
)
1146 if (isl_tab_is_equality(info
[i
].tab
, n_eq
+ relax
[l
]))
1147 return isl_change_none
;
1149 snap
= isl_tab_snap(info
[i
].tab
);
1150 for (l
= 0; l
< n
; ++l
)
1151 if (isl_tab_relax(info
[i
].tab
, n_eq
+ relax
[l
]) < 0)
1152 return isl_change_error
;
1153 for (l
= 0; l
< n
; ++l
) {
1154 if (!isl_tab_is_redundant(info
[i
].tab
, n_eq
+ relax
[l
]))
1156 if (isl_tab_rollback(info
[i
].tab
, snap
) < 0)
1157 return isl_change_error
;
1158 return isl_change_none
;
1160 snap2
= isl_tab_snap(info
[i
].tab
);
1161 for (l
= 0; l
< n
; ++l
) {
1162 if (isl_tab_rollback(info
[i
].tab
, snap2
) < 0)
1163 return isl_change_error
;
1164 if (isl_tab_select_facet(info
[i
].tab
, n_eq
+ relax
[l
]) < 0)
1165 return isl_change_error
;
1166 if (tighten_on_relaxed_facet(&info
[i
], n
, relax
, l
) < 0)
1167 return isl_change_error
;
1168 super
= contains(&info
[j
], info
[i
].tab
);
1170 return isl_change_error
;
1173 if (isl_tab_rollback(info
[i
].tab
, snap
) < 0)
1174 return isl_change_error
;
1175 return isl_change_none
;
1178 if (isl_tab_rollback(info
[i
].tab
, snap2
) < 0)
1179 return isl_change_error
;
1180 return extend(i
, j
, n
, relax
, info
);
1183 /* Data structure that keeps track of the wrapping constraints
1184 * and of information to bound the coefficients of those constraints.
1186 * bound is set if we want to apply a bound on the coefficients
1187 * mat contains the wrapping constraints
1188 * max is the bound on the coefficients (if bound is set)
1196 /* Update wraps->max to be greater than or equal to the coefficients
1197 * in the equalities and inequalities of info->bmap that can be removed
1198 * if we end up applying wrapping.
1200 static isl_stat
wraps_update_max(struct isl_wraps
*wraps
,
1201 struct isl_coalesce_info
*info
)
1205 unsigned total
= isl_basic_map_total_dim(info
->bmap
);
1207 isl_int_init(max_k
);
1209 for (k
= 0; k
< info
->bmap
->n_eq
; ++k
) {
1210 if (info
->eq
[2 * k
] == STATUS_VALID
&&
1211 info
->eq
[2 * k
+ 1] == STATUS_VALID
)
1213 isl_seq_abs_max(info
->bmap
->eq
[k
] + 1, total
, &max_k
);
1214 if (isl_int_abs_gt(max_k
, wraps
->max
))
1215 isl_int_set(wraps
->max
, max_k
);
1218 for (k
= 0; k
< info
->bmap
->n_ineq
; ++k
) {
1219 if (info
->ineq
[k
] == STATUS_VALID
||
1220 info
->ineq
[k
] == STATUS_REDUNDANT
)
1222 isl_seq_abs_max(info
->bmap
->ineq
[k
] + 1, total
, &max_k
);
1223 if (isl_int_abs_gt(max_k
, wraps
->max
))
1224 isl_int_set(wraps
->max
, max_k
);
1227 isl_int_clear(max_k
);
1232 /* Initialize the isl_wraps data structure.
1233 * If we want to bound the coefficients of the wrapping constraints,
1234 * we set wraps->max to the largest coefficient
1235 * in the equalities and inequalities that can be removed if we end up
1236 * applying wrapping.
1238 static isl_stat
wraps_init(struct isl_wraps
*wraps
, __isl_take isl_mat
*mat
,
1239 struct isl_coalesce_info
*info
, int i
, int j
)
1246 return isl_stat_error
;
1247 ctx
= isl_mat_get_ctx(mat
);
1248 wraps
->bound
= isl_options_get_coalesce_bounded_wrapping(ctx
);
1251 isl_int_init(wraps
->max
);
1252 isl_int_set_si(wraps
->max
, 0);
1253 if (wraps_update_max(wraps
, &info
[i
]) < 0)
1254 return isl_stat_error
;
1255 if (wraps_update_max(wraps
, &info
[j
]) < 0)
1256 return isl_stat_error
;
1261 /* Free the contents of the isl_wraps data structure.
1263 static void wraps_free(struct isl_wraps
*wraps
)
1265 isl_mat_free(wraps
->mat
);
1267 isl_int_clear(wraps
->max
);
1270 /* Is the wrapping constraint in row "row" allowed?
1272 * If wraps->bound is set, we check that none of the coefficients
1273 * is greater than wraps->max.
1275 static int allow_wrap(struct isl_wraps
*wraps
, int row
)
1282 for (i
= 1; i
< wraps
->mat
->n_col
; ++i
)
1283 if (isl_int_abs_gt(wraps
->mat
->row
[row
][i
], wraps
->max
))
1289 /* Wrap "ineq" (or its opposite if "negate" is set) around "bound"
1290 * to include "set" and add the result in position "w" of "wraps".
1291 * "len" is the total number of coefficients in "bound" and "ineq".
1292 * Return 1 on success, 0 on failure and -1 on error.
1293 * Wrapping can fail if the result of wrapping is equal to "bound"
1294 * or if we want to bound the sizes of the coefficients and
1295 * the wrapped constraint does not satisfy this bound.
1297 static int add_wrap(struct isl_wraps
*wraps
, int w
, isl_int
*bound
,
1298 isl_int
*ineq
, unsigned len
, __isl_keep isl_set
*set
, int negate
)
1300 isl_seq_cpy(wraps
->mat
->row
[w
], bound
, len
);
1302 isl_seq_neg(wraps
->mat
->row
[w
+ 1], ineq
, len
);
1303 ineq
= wraps
->mat
->row
[w
+ 1];
1305 if (!isl_set_wrap_facet(set
, wraps
->mat
->row
[w
], ineq
))
1307 if (isl_seq_eq(wraps
->mat
->row
[w
], bound
, len
))
1309 if (!allow_wrap(wraps
, w
))
1314 /* For each constraint in info->bmap that is not redundant (as determined
1315 * by info->tab) and that is not a valid constraint for the other basic map,
1316 * wrap the constraint around "bound" such that it includes the whole
1317 * set "set" and append the resulting constraint to "wraps".
1318 * Note that the constraints that are valid for the other basic map
1319 * will be added to the combined basic map by default, so there is
1320 * no need to wrap them.
1321 * The caller wrap_in_facets even relies on this function not wrapping
1322 * any constraints that are already valid.
1323 * "wraps" is assumed to have been pre-allocated to the appropriate size.
1324 * wraps->n_row is the number of actual wrapped constraints that have
1326 * If any of the wrapping problems results in a constraint that is
1327 * identical to "bound", then this means that "set" is unbounded in such
1328 * way that no wrapping is possible. If this happens then wraps->n_row
1330 * Similarly, if we want to bound the coefficients of the wrapping
1331 * constraints and a newly added wrapping constraint does not
1332 * satisfy the bound, then wraps->n_row is also reset to zero.
1334 static isl_stat
add_wraps(struct isl_wraps
*wraps
,
1335 struct isl_coalesce_info
*info
, isl_int
*bound
, __isl_keep isl_set
*set
)
1340 isl_basic_map
*bmap
= info
->bmap
;
1341 unsigned len
= 1 + isl_basic_map_total_dim(bmap
);
1343 w
= wraps
->mat
->n_row
;
1345 for (l
= 0; l
< bmap
->n_ineq
; ++l
) {
1346 if (info
->ineq
[l
] == STATUS_VALID
||
1347 info
->ineq
[l
] == STATUS_REDUNDANT
)
1349 if (isl_seq_is_neg(bound
, bmap
->ineq
[l
], len
))
1351 if (isl_seq_eq(bound
, bmap
->ineq
[l
], len
))
1353 if (isl_tab_is_redundant(info
->tab
, bmap
->n_eq
+ l
))
1356 added
= add_wrap(wraps
, w
, bound
, bmap
->ineq
[l
], len
, set
, 0);
1358 return isl_stat_error
;
1363 for (l
= 0; l
< bmap
->n_eq
; ++l
) {
1364 if (isl_seq_is_neg(bound
, bmap
->eq
[l
], len
))
1366 if (isl_seq_eq(bound
, bmap
->eq
[l
], len
))
1369 for (m
= 0; m
< 2; ++m
) {
1370 if (info
->eq
[2 * l
+ m
] == STATUS_VALID
)
1372 added
= add_wrap(wraps
, w
, bound
, bmap
->eq
[l
], len
,
1375 return isl_stat_error
;
1382 wraps
->mat
->n_row
= w
;
1385 wraps
->mat
->n_row
= 0;
1389 /* Check if the constraints in "wraps" from "first" until the last
1390 * are all valid for the basic set represented by "tab".
1391 * If not, wraps->n_row is set to zero.
1393 static int check_wraps(__isl_keep isl_mat
*wraps
, int first
,
1394 struct isl_tab
*tab
)
1398 for (i
= first
; i
< wraps
->n_row
; ++i
) {
1399 enum isl_ineq_type type
;
1400 type
= isl_tab_ineq_type(tab
, wraps
->row
[i
]);
1401 if (type
== isl_ineq_error
)
1403 if (type
== isl_ineq_redundant
)
1412 /* Return a set that corresponds to the non-redundant constraints
1413 * (as recorded in tab) of bmap.
1415 * It's important to remove the redundant constraints as some
1416 * of the other constraints may have been modified after the
1417 * constraints were marked redundant.
1418 * In particular, a constraint may have been relaxed.
1419 * Redundant constraints are ignored when a constraint is relaxed
1420 * and should therefore continue to be ignored ever after.
1421 * Otherwise, the relaxation might be thwarted by some of
1422 * these constraints.
1424 * Update the underlying set to ensure that the dimension doesn't change.
1425 * Otherwise the integer divisions could get dropped if the tab
1426 * turns out to be empty.
1428 static __isl_give isl_set
*set_from_updated_bmap(__isl_keep isl_basic_map
*bmap
,
1429 struct isl_tab
*tab
)
1431 isl_basic_set
*bset
;
1433 bmap
= isl_basic_map_copy(bmap
);
1434 bset
= isl_basic_map_underlying_set(bmap
);
1435 bset
= isl_basic_set_cow(bset
);
1436 bset
= isl_basic_set_update_from_tab(bset
, tab
);
1437 return isl_set_from_basic_set(bset
);
1440 /* Wrap the constraints of info->bmap that bound the facet defined
1441 * by inequality "k" around (the opposite of) this inequality to
1442 * include "set". "bound" may be used to store the negated inequality.
1443 * Since the wrapped constraints are not guaranteed to contain the whole
1444 * of info->bmap, we check them in check_wraps.
1445 * If any of the wrapped constraints turn out to be invalid, then
1446 * check_wraps will reset wrap->n_row to zero.
1448 static isl_stat
add_wraps_around_facet(struct isl_wraps
*wraps
,
1449 struct isl_coalesce_info
*info
, int k
, isl_int
*bound
,
1450 __isl_keep isl_set
*set
)
1452 struct isl_tab_undo
*snap
;
1454 unsigned total
= isl_basic_map_total_dim(info
->bmap
);
1456 snap
= isl_tab_snap(info
->tab
);
1458 if (isl_tab_select_facet(info
->tab
, info
->bmap
->n_eq
+ k
) < 0)
1459 return isl_stat_error
;
1460 if (isl_tab_detect_redundant(info
->tab
) < 0)
1461 return isl_stat_error
;
1463 isl_seq_neg(bound
, info
->bmap
->ineq
[k
], 1 + total
);
1465 n
= wraps
->mat
->n_row
;
1466 if (add_wraps(wraps
, info
, bound
, set
) < 0)
1467 return isl_stat_error
;
1469 if (isl_tab_rollback(info
->tab
, snap
) < 0)
1470 return isl_stat_error
;
1471 if (check_wraps(wraps
->mat
, n
, info
->tab
) < 0)
1472 return isl_stat_error
;
1477 /* Given a basic set i with a constraint k that is adjacent to
1478 * basic set j, check if we can wrap
1479 * both the facet corresponding to k (if "wrap_facet" is set) and basic map j
1480 * (always) around their ridges to include the other set.
1481 * If so, replace the pair of basic sets by their union.
1483 * All constraints of i (except k) are assumed to be valid or
1484 * cut constraints for j.
1485 * Wrapping the cut constraints to include basic map j may result
1486 * in constraints that are no longer valid of basic map i
1487 * we have to check that the resulting wrapping constraints are valid for i.
1488 * If "wrap_facet" is not set, then all constraints of i (except k)
1489 * are assumed to be valid for j.
1498 static enum isl_change
can_wrap_in_facet(int i
, int j
, int k
,
1499 struct isl_coalesce_info
*info
, int wrap_facet
)
1501 enum isl_change change
= isl_change_none
;
1502 struct isl_wraps wraps
;
1505 struct isl_set
*set_i
= NULL
;
1506 struct isl_set
*set_j
= NULL
;
1507 struct isl_vec
*bound
= NULL
;
1508 unsigned total
= isl_basic_map_total_dim(info
[i
].bmap
);
1510 set_i
= set_from_updated_bmap(info
[i
].bmap
, info
[i
].tab
);
1511 set_j
= set_from_updated_bmap(info
[j
].bmap
, info
[j
].tab
);
1512 ctx
= isl_basic_map_get_ctx(info
[i
].bmap
);
1513 mat
= isl_mat_alloc(ctx
, 2 * (info
[i
].bmap
->n_eq
+ info
[j
].bmap
->n_eq
) +
1514 info
[i
].bmap
->n_ineq
+ info
[j
].bmap
->n_ineq
,
1516 if (wraps_init(&wraps
, mat
, info
, i
, j
) < 0)
1518 bound
= isl_vec_alloc(ctx
, 1 + total
);
1519 if (!set_i
|| !set_j
|| !bound
)
1522 isl_seq_cpy(bound
->el
, info
[i
].bmap
->ineq
[k
], 1 + total
);
1523 isl_int_add_ui(bound
->el
[0], bound
->el
[0], 1);
1524 isl_seq_normalize(ctx
, bound
->el
, 1 + total
);
1526 isl_seq_cpy(wraps
.mat
->row
[0], bound
->el
, 1 + total
);
1527 wraps
.mat
->n_row
= 1;
1529 if (add_wraps(&wraps
, &info
[j
], bound
->el
, set_i
) < 0)
1531 if (!wraps
.mat
->n_row
)
1535 if (add_wraps_around_facet(&wraps
, &info
[i
], k
,
1536 bound
->el
, set_j
) < 0)
1538 if (!wraps
.mat
->n_row
)
1542 change
= fuse(i
, j
, info
, wraps
.mat
, 0, 0);
1547 isl_set_free(set_i
);
1548 isl_set_free(set_j
);
1550 isl_vec_free(bound
);
1555 isl_vec_free(bound
);
1556 isl_set_free(set_i
);
1557 isl_set_free(set_j
);
1558 return isl_change_error
;
1561 /* Given a cut constraint t(x) >= 0 of basic map i, stored in row "w"
1562 * of wrap.mat, replace it by its relaxed version t(x) + 1 >= 0, and
1563 * add wrapping constraints to wrap.mat for all constraints
1564 * of basic map j that bound the part of basic map j that sticks out
1565 * of the cut constraint.
1566 * "set_i" is the underlying set of basic map i.
1567 * If any wrapping fails, then wraps->mat.n_row is reset to zero.
1569 * In particular, we first intersect basic map j with t(x) + 1 = 0.
1570 * If the result is empty, then t(x) >= 0 was actually a valid constraint
1571 * (with respect to the integer points), so we add t(x) >= 0 instead.
1572 * Otherwise, we wrap the constraints of basic map j that are not
1573 * redundant in this intersection and that are not already valid
1574 * for basic map i over basic map i.
1575 * Note that it is sufficient to wrap the constraints to include
1576 * basic map i, because we will only wrap the constraints that do
1577 * not include basic map i already. The wrapped constraint will
1578 * therefore be more relaxed compared to the original constraint.
1579 * Since the original constraint is valid for basic map j, so is
1580 * the wrapped constraint.
1582 static isl_stat
wrap_in_facet(struct isl_wraps
*wraps
, int w
,
1583 struct isl_coalesce_info
*info_j
, __isl_keep isl_set
*set_i
,
1584 struct isl_tab_undo
*snap
)
1586 isl_int_add_ui(wraps
->mat
->row
[w
][0], wraps
->mat
->row
[w
][0], 1);
1587 if (isl_tab_add_eq(info_j
->tab
, wraps
->mat
->row
[w
]) < 0)
1588 return isl_stat_error
;
1589 if (isl_tab_detect_redundant(info_j
->tab
) < 0)
1590 return isl_stat_error
;
1592 if (info_j
->tab
->empty
)
1593 isl_int_sub_ui(wraps
->mat
->row
[w
][0], wraps
->mat
->row
[w
][0], 1);
1594 else if (add_wraps(wraps
, info_j
, wraps
->mat
->row
[w
], set_i
) < 0)
1595 return isl_stat_error
;
1597 if (isl_tab_rollback(info_j
->tab
, snap
) < 0)
1598 return isl_stat_error
;
1603 /* Given a pair of basic maps i and j such that j sticks out
1604 * of i at n cut constraints, each time by at most one,
1605 * try to compute wrapping constraints and replace the two
1606 * basic maps by a single basic map.
1607 * The other constraints of i are assumed to be valid for j.
1608 * "set_i" is the underlying set of basic map i.
1609 * "wraps" has been initialized to be of the right size.
1611 * For each cut constraint t(x) >= 0 of i, we add the relaxed version
1612 * t(x) + 1 >= 0, along with wrapping constraints for all constraints
1613 * of basic map j that bound the part of basic map j that sticks out
1614 * of the cut constraint.
1616 * If any wrapping fails, i.e., if we cannot wrap to touch
1617 * the union, then we give up.
1618 * Otherwise, the pair of basic maps is replaced by their union.
1620 static enum isl_change
try_wrap_in_facets(int i
, int j
,
1621 struct isl_coalesce_info
*info
, struct isl_wraps
*wraps
,
1622 __isl_keep isl_set
*set_i
)
1626 struct isl_tab_undo
*snap
;
1628 total
= isl_basic_map_total_dim(info
[i
].bmap
);
1630 snap
= isl_tab_snap(info
[j
].tab
);
1632 wraps
->mat
->n_row
= 0;
1634 for (k
= 0; k
< info
[i
].bmap
->n_eq
; ++k
) {
1635 for (l
= 0; l
< 2; ++l
) {
1636 if (info
[i
].eq
[2 * k
+ l
] != STATUS_CUT
)
1638 w
= wraps
->mat
->n_row
++;
1640 isl_seq_neg(wraps
->mat
->row
[w
],
1641 info
[i
].bmap
->eq
[k
], 1 + total
);
1643 isl_seq_cpy(wraps
->mat
->row
[w
],
1644 info
[i
].bmap
->eq
[k
], 1 + total
);
1645 if (wrap_in_facet(wraps
, w
, &info
[j
], set_i
, snap
) < 0)
1646 return isl_change_error
;
1648 if (!wraps
->mat
->n_row
)
1649 return isl_change_none
;
1653 for (k
= 0; k
< info
[i
].bmap
->n_ineq
; ++k
) {
1654 if (info
[i
].ineq
[k
] != STATUS_CUT
)
1656 w
= wraps
->mat
->n_row
++;
1657 isl_seq_cpy(wraps
->mat
->row
[w
],
1658 info
[i
].bmap
->ineq
[k
], 1 + total
);
1659 if (wrap_in_facet(wraps
, w
, &info
[j
], set_i
, snap
) < 0)
1660 return isl_change_error
;
1662 if (!wraps
->mat
->n_row
)
1663 return isl_change_none
;
1666 return fuse(i
, j
, info
, wraps
->mat
, 0, 1);
1669 /* Given a pair of basic maps i and j such that j sticks out
1670 * of i at n cut constraints, each time by at most one,
1671 * try to compute wrapping constraints and replace the two
1672 * basic maps by a single basic map.
1673 * The other constraints of i are assumed to be valid for j.
1675 * The core computation is performed by try_wrap_in_facets.
1676 * This function simply extracts an underlying set representation
1677 * of basic map i and initializes the data structure for keeping
1678 * track of wrapping constraints.
1680 static enum isl_change
wrap_in_facets(int i
, int j
, int n
,
1681 struct isl_coalesce_info
*info
)
1683 enum isl_change change
= isl_change_none
;
1684 struct isl_wraps wraps
;
1687 isl_set
*set_i
= NULL
;
1688 unsigned total
= isl_basic_map_total_dim(info
[i
].bmap
);
1691 if (isl_tab_extend_cons(info
[j
].tab
, 1) < 0)
1692 return isl_change_error
;
1694 max_wrap
= 1 + 2 * info
[j
].bmap
->n_eq
+ info
[j
].bmap
->n_ineq
;
1697 set_i
= set_from_updated_bmap(info
[i
].bmap
, info
[i
].tab
);
1698 ctx
= isl_basic_map_get_ctx(info
[i
].bmap
);
1699 mat
= isl_mat_alloc(ctx
, max_wrap
, 1 + total
);
1700 if (wraps_init(&wraps
, mat
, info
, i
, j
) < 0)
1705 change
= try_wrap_in_facets(i
, j
, info
, &wraps
, set_i
);
1708 isl_set_free(set_i
);
1713 isl_set_free(set_i
);
1714 return isl_change_error
;
1717 /* Return the effect of inequality "ineq" on the tableau "tab",
1718 * after relaxing the constant term of "ineq" by one.
1720 static enum isl_ineq_type
type_of_relaxed(struct isl_tab
*tab
, isl_int
*ineq
)
1722 enum isl_ineq_type type
;
1724 isl_int_add_ui(ineq
[0], ineq
[0], 1);
1725 type
= isl_tab_ineq_type(tab
, ineq
);
1726 isl_int_sub_ui(ineq
[0], ineq
[0], 1);
1731 /* Given two basic sets i and j,
1732 * check if relaxing all the cut constraints of i by one turns
1733 * them into valid constraint for j and check if we can wrap in
1734 * the bits that are sticking out.
1735 * If so, replace the pair by their union.
1737 * We first check if all relaxed cut inequalities of i are valid for j
1738 * and then try to wrap in the intersections of the relaxed cut inequalities
1741 * During this wrapping, we consider the points of j that lie at a distance
1742 * of exactly 1 from i. In particular, we ignore the points that lie in
1743 * between this lower-dimensional space and the basic map i.
1744 * We can therefore only apply this to integer maps.
1770 * Wrapping can fail if the result of wrapping one of the facets
1771 * around its edges does not produce any new facet constraint.
1772 * In particular, this happens when we try to wrap in unbounded sets.
1774 * _______________________________________________________________________
1778 * |_| |_________________________________________________________________
1781 * The following is not an acceptable result of coalescing the above two
1782 * sets as it includes extra integer points.
1783 * _______________________________________________________________________
1788 * \______________________________________________________________________
1790 static enum isl_change
can_wrap_in_set(int i
, int j
,
1791 struct isl_coalesce_info
*info
)
1797 if (ISL_F_ISSET(info
[i
].bmap
, ISL_BASIC_MAP_RATIONAL
) ||
1798 ISL_F_ISSET(info
[j
].bmap
, ISL_BASIC_MAP_RATIONAL
))
1799 return isl_change_none
;
1801 n
= count_eq(&info
[i
], STATUS_CUT
) + count_ineq(&info
[i
], STATUS_CUT
);
1803 return isl_change_none
;
1805 total
= isl_basic_map_total_dim(info
[i
].bmap
);
1806 for (k
= 0; k
< info
[i
].bmap
->n_eq
; ++k
) {
1807 for (l
= 0; l
< 2; ++l
) {
1808 enum isl_ineq_type type
;
1810 if (info
[i
].eq
[2 * k
+ l
] != STATUS_CUT
)
1814 isl_seq_neg(info
[i
].bmap
->eq
[k
],
1815 info
[i
].bmap
->eq
[k
], 1 + total
);
1816 type
= type_of_relaxed(info
[j
].tab
,
1817 info
[i
].bmap
->eq
[k
]);
1819 isl_seq_neg(info
[i
].bmap
->eq
[k
],
1820 info
[i
].bmap
->eq
[k
], 1 + total
);
1821 if (type
== isl_ineq_error
)
1822 return isl_change_error
;
1823 if (type
!= isl_ineq_redundant
)
1824 return isl_change_none
;
1828 for (k
= 0; k
< info
[i
].bmap
->n_ineq
; ++k
) {
1829 enum isl_ineq_type type
;
1831 if (info
[i
].ineq
[k
] != STATUS_CUT
)
1834 type
= type_of_relaxed(info
[j
].tab
, info
[i
].bmap
->ineq
[k
]);
1835 if (type
== isl_ineq_error
)
1836 return isl_change_error
;
1837 if (type
!= isl_ineq_redundant
)
1838 return isl_change_none
;
1841 return wrap_in_facets(i
, j
, n
, info
);
1844 /* Check if either i or j has only cut constraints that can
1845 * be used to wrap in (a facet of) the other basic set.
1846 * if so, replace the pair by their union.
1848 static enum isl_change
check_wrap(int i
, int j
, struct isl_coalesce_info
*info
)
1850 enum isl_change change
= isl_change_none
;
1852 change
= can_wrap_in_set(i
, j
, info
);
1853 if (change
!= isl_change_none
)
1856 change
= can_wrap_in_set(j
, i
, info
);
1860 /* Check if all inequality constraints of "i" that cut "j" cease
1861 * to be cut constraints if they are relaxed by one.
1862 * If so, collect the cut constraints in "list".
1863 * The caller is responsible for allocating "list".
1865 static isl_bool
all_cut_by_one(int i
, int j
, struct isl_coalesce_info
*info
,
1871 for (l
= 0; l
< info
[i
].bmap
->n_ineq
; ++l
) {
1872 enum isl_ineq_type type
;
1874 if (info
[i
].ineq
[l
] != STATUS_CUT
)
1876 type
= type_of_relaxed(info
[j
].tab
, info
[i
].bmap
->ineq
[l
]);
1877 if (type
== isl_ineq_error
)
1878 return isl_bool_error
;
1879 if (type
!= isl_ineq_redundant
)
1880 return isl_bool_false
;
1884 return isl_bool_true
;
1887 /* Given two basic maps such that "j" has at least one equality constraint
1888 * that is adjacent to an inequality constraint of "i" and such that "i" has
1889 * exactly one inequality constraint that is adjacent to an equality
1890 * constraint of "j", check whether "i" can be extended to include "j" or
1891 * whether "j" can be wrapped into "i".
1892 * All remaining constraints of "i" and "j" are assumed to be valid
1893 * or cut constraints of the other basic map.
1894 * However, none of the equality constraints of "i" are cut constraints.
1896 * If "i" has any "cut" inequality constraints, then check if relaxing
1897 * each of them by one is sufficient for them to become valid.
1898 * If so, check if the inequality constraint adjacent to an equality
1899 * constraint of "j" along with all these cut constraints
1900 * can be relaxed by one to contain exactly "j".
1901 * Otherwise, or if this fails, check if "j" can be wrapped into "i".
1903 static enum isl_change
check_single_adj_eq(int i
, int j
,
1904 struct isl_coalesce_info
*info
)
1906 enum isl_change change
= isl_change_none
;
1913 n_cut
= count_ineq(&info
[i
], STATUS_CUT
);
1915 k
= find_ineq(&info
[i
], STATUS_ADJ_EQ
);
1918 ctx
= isl_basic_map_get_ctx(info
[i
].bmap
);
1919 relax
= isl_calloc_array(ctx
, int, 1 + n_cut
);
1921 return isl_change_error
;
1923 try_relax
= all_cut_by_one(i
, j
, info
, relax
+ 1);
1925 change
= isl_change_error
;
1927 try_relax
= isl_bool_true
;
1930 if (try_relax
&& change
== isl_change_none
)
1931 change
= is_relaxed_extension(i
, j
, 1 + n_cut
, relax
, info
);
1934 if (change
!= isl_change_none
)
1937 change
= can_wrap_in_facet(i
, j
, k
, info
, n_cut
> 0);
1942 /* At least one of the basic maps has an equality that is adjacent
1943 * to an inequality. Make sure that only one of the basic maps has
1944 * such an equality and that the other basic map has exactly one
1945 * inequality adjacent to an equality.
1946 * If the other basic map does not have such an inequality, then
1947 * check if all its constraints are either valid or cut constraints
1948 * and, if so, try wrapping in the first map into the second.
1949 * Otherwise, try to extend one basic map with the other or
1950 * wrap one basic map in the other.
1952 static enum isl_change
check_adj_eq(int i
, int j
,
1953 struct isl_coalesce_info
*info
)
1955 if (any_eq(&info
[i
], STATUS_ADJ_INEQ
) &&
1956 any_eq(&info
[j
], STATUS_ADJ_INEQ
))
1957 /* ADJ EQ TOO MANY */
1958 return isl_change_none
;
1960 if (any_eq(&info
[i
], STATUS_ADJ_INEQ
))
1961 return check_adj_eq(j
, i
, info
);
1963 /* j has an equality adjacent to an inequality in i */
1965 if (count_ineq(&info
[i
], STATUS_ADJ_EQ
) != 1) {
1966 if (all_valid_or_cut(&info
[i
]))
1967 return can_wrap_in_set(i
, j
, info
);
1968 return isl_change_none
;
1970 if (any_eq(&info
[i
], STATUS_CUT
))
1971 return isl_change_none
;
1972 if (any_ineq(&info
[j
], STATUS_ADJ_EQ
) ||
1973 any_ineq(&info
[i
], STATUS_ADJ_INEQ
) ||
1974 any_ineq(&info
[j
], STATUS_ADJ_INEQ
))
1975 /* ADJ EQ TOO MANY */
1976 return isl_change_none
;
1978 return check_single_adj_eq(i
, j
, info
);
1981 /* Disjunct "j" lies on a hyperplane that is adjacent to disjunct "i".
1982 * In particular, disjunct "i" has an inequality constraint that is adjacent
1983 * to a (combination of) equality constraint(s) of disjunct "j",
1984 * but disjunct "j" has no explicit equality constraint adjacent
1985 * to an inequality constraint of disjunct "i".
1987 * Disjunct "i" is already known not to have any equality constraints
1988 * that are adjacent to an equality or inequality constraint.
1989 * Check that, other than the inequality constraint mentioned above,
1990 * all other constraints of disjunct "i" are valid for disjunct "j".
1991 * If so, try and wrap in disjunct "j".
1993 static enum isl_change
check_ineq_adj_eq(int i
, int j
,
1994 struct isl_coalesce_info
*info
)
1998 if (any_eq(&info
[i
], STATUS_CUT
))
1999 return isl_change_none
;
2000 if (any_ineq(&info
[i
], STATUS_CUT
))
2001 return isl_change_none
;
2002 if (any_ineq(&info
[i
], STATUS_ADJ_INEQ
))
2003 return isl_change_none
;
2004 if (count_ineq(&info
[i
], STATUS_ADJ_EQ
) != 1)
2005 return isl_change_none
;
2007 k
= find_ineq(&info
[i
], STATUS_ADJ_EQ
);
2009 return can_wrap_in_facet(i
, j
, k
, info
, 0);
2012 /* The two basic maps lie on adjacent hyperplanes. In particular,
2013 * basic map "i" has an equality that lies parallel to basic map "j".
2014 * Check if we can wrap the facets around the parallel hyperplanes
2015 * to include the other set.
2017 * We perform basically the same operations as can_wrap_in_facet,
2018 * except that we don't need to select a facet of one of the sets.
2024 * If there is more than one equality of "i" adjacent to an equality of "j",
2025 * then the result will satisfy one or more equalities that are a linear
2026 * combination of these equalities. These will be encoded as pairs
2027 * of inequalities in the wrapping constraints and need to be made
2030 static enum isl_change
check_eq_adj_eq(int i
, int j
,
2031 struct isl_coalesce_info
*info
)
2034 enum isl_change change
= isl_change_none
;
2035 int detect_equalities
= 0;
2036 struct isl_wraps wraps
;
2039 struct isl_set
*set_i
= NULL
;
2040 struct isl_set
*set_j
= NULL
;
2041 struct isl_vec
*bound
= NULL
;
2042 unsigned total
= isl_basic_map_total_dim(info
[i
].bmap
);
2044 if (count_eq(&info
[i
], STATUS_ADJ_EQ
) != 1)
2045 detect_equalities
= 1;
2047 k
= find_eq(&info
[i
], STATUS_ADJ_EQ
);
2049 set_i
= set_from_updated_bmap(info
[i
].bmap
, info
[i
].tab
);
2050 set_j
= set_from_updated_bmap(info
[j
].bmap
, info
[j
].tab
);
2051 ctx
= isl_basic_map_get_ctx(info
[i
].bmap
);
2052 mat
= isl_mat_alloc(ctx
, 2 * (info
[i
].bmap
->n_eq
+ info
[j
].bmap
->n_eq
) +
2053 info
[i
].bmap
->n_ineq
+ info
[j
].bmap
->n_ineq
,
2055 if (wraps_init(&wraps
, mat
, info
, i
, j
) < 0)
2057 bound
= isl_vec_alloc(ctx
, 1 + total
);
2058 if (!set_i
|| !set_j
|| !bound
)
2062 isl_seq_neg(bound
->el
, info
[i
].bmap
->eq
[k
/ 2], 1 + total
);
2064 isl_seq_cpy(bound
->el
, info
[i
].bmap
->eq
[k
/ 2], 1 + total
);
2065 isl_int_add_ui(bound
->el
[0], bound
->el
[0], 1);
2067 isl_seq_cpy(wraps
.mat
->row
[0], bound
->el
, 1 + total
);
2068 wraps
.mat
->n_row
= 1;
2070 if (add_wraps(&wraps
, &info
[j
], bound
->el
, set_i
) < 0)
2072 if (!wraps
.mat
->n_row
)
2075 isl_int_sub_ui(bound
->el
[0], bound
->el
[0], 1);
2076 isl_seq_neg(bound
->el
, bound
->el
, 1 + total
);
2078 isl_seq_cpy(wraps
.mat
->row
[wraps
.mat
->n_row
], bound
->el
, 1 + total
);
2081 if (add_wraps(&wraps
, &info
[i
], bound
->el
, set_j
) < 0)
2083 if (!wraps
.mat
->n_row
)
2086 change
= fuse(i
, j
, info
, wraps
.mat
, detect_equalities
, 0);
2089 error
: change
= isl_change_error
;
2094 isl_set_free(set_i
);
2095 isl_set_free(set_j
);
2096 isl_vec_free(bound
);
2101 /* Initialize the "eq" and "ineq" fields of "info".
2103 static void init_status(struct isl_coalesce_info
*info
)
2105 info
->eq
= info
->ineq
= NULL
;
2108 /* Set info->eq to the positions of the equalities of info->bmap
2109 * with respect to the basic map represented by "tab".
2110 * If info->eq has already been computed, then do not compute it again.
2112 static void set_eq_status_in(struct isl_coalesce_info
*info
,
2113 struct isl_tab
*tab
)
2117 info
->eq
= eq_status_in(info
->bmap
, tab
);
2120 /* Set info->ineq to the positions of the inequalities of info->bmap
2121 * with respect to the basic map represented by "tab".
2122 * If info->ineq has already been computed, then do not compute it again.
2124 static void set_ineq_status_in(struct isl_coalesce_info
*info
,
2125 struct isl_tab
*tab
)
2129 info
->ineq
= ineq_status_in(info
->bmap
, info
->tab
, tab
);
2132 /* Free the memory allocated by the "eq" and "ineq" fields of "info".
2133 * This function assumes that init_status has been called on "info" first,
2134 * after which the "eq" and "ineq" fields may or may not have been
2135 * assigned a newly allocated array.
2137 static void clear_status(struct isl_coalesce_info
*info
)
2143 /* Are all inequality constraints of the basic map represented by "info"
2144 * valid for the other basic map, except for a single constraint
2145 * that is adjacent to an inequality constraint of the other basic map?
2147 static int all_ineq_valid_or_single_adj_ineq(struct isl_coalesce_info
*info
)
2152 for (i
= 0; i
< info
->bmap
->n_ineq
; ++i
) {
2153 if (info
->ineq
[i
] == STATUS_REDUNDANT
)
2155 if (info
->ineq
[i
] == STATUS_VALID
)
2157 if (info
->ineq
[i
] != STATUS_ADJ_INEQ
)
2167 /* Basic map "i" has one or more equality constraints that separate it
2168 * from basic map "j". Check if it happens to be an extension
2170 * In particular, check that all constraints of "j" are valid for "i",
2171 * except for one inequality constraint that is adjacent
2172 * to an inequality constraints of "i".
2173 * If so, check for "i" being an extension of "j" by calling
2174 * is_adj_ineq_extension.
2176 * Clean up the memory allocated for keeping track of the status
2177 * of the constraints before returning.
2179 static enum isl_change
separating_equality(int i
, int j
,
2180 struct isl_coalesce_info
*info
)
2182 enum isl_change change
= isl_change_none
;
2184 if (all(info
[j
].eq
, 2 * info
[j
].bmap
->n_eq
, STATUS_VALID
) &&
2185 all_ineq_valid_or_single_adj_ineq(&info
[j
]))
2186 change
= is_adj_ineq_extension(j
, i
, info
);
2188 clear_status(&info
[i
]);
2189 clear_status(&info
[j
]);
2193 /* Check if the union of the given pair of basic maps
2194 * can be represented by a single basic map.
2195 * If so, replace the pair by the single basic map and return
2196 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
2197 * Otherwise, return isl_change_none.
2198 * The two basic maps are assumed to live in the same local space.
2199 * The "eq" and "ineq" fields of info[i] and info[j] are assumed
2200 * to have been initialized by the caller, either to NULL or
2201 * to valid information.
2203 * We first check the effect of each constraint of one basic map
2204 * on the other basic map.
2205 * The constraint may be
2206 * redundant the constraint is redundant in its own
2207 * basic map and should be ignore and removed
2209 * valid all (integer) points of the other basic map
2210 * satisfy the constraint
2211 * separate no (integer) point of the other basic map
2212 * satisfies the constraint
2213 * cut some but not all points of the other basic map
2214 * satisfy the constraint
2215 * adj_eq the given constraint is adjacent (on the outside)
2216 * to an equality of the other basic map
2217 * adj_ineq the given constraint is adjacent (on the outside)
2218 * to an inequality of the other basic map
2220 * We consider seven cases in which we can replace the pair by a single
2221 * basic map. We ignore all "redundant" constraints.
2223 * 1. all constraints of one basic map are valid
2224 * => the other basic map is a subset and can be removed
2226 * 2. all constraints of both basic maps are either "valid" or "cut"
2227 * and the facets corresponding to the "cut" constraints
2228 * of one of the basic maps lies entirely inside the other basic map
2229 * => the pair can be replaced by a basic map consisting
2230 * of the valid constraints in both basic maps
2232 * 3. there is a single pair of adjacent inequalities
2233 * (all other constraints are "valid")
2234 * => the pair can be replaced by a basic map consisting
2235 * of the valid constraints in both basic maps
2237 * 4. one basic map has a single adjacent inequality, while the other
2238 * constraints are "valid". The other basic map has some
2239 * "cut" constraints, but replacing the adjacent inequality by
2240 * its opposite and adding the valid constraints of the other
2241 * basic map results in a subset of the other basic map
2242 * => the pair can be replaced by a basic map consisting
2243 * of the valid constraints in both basic maps
2245 * 5. there is a single adjacent pair of an inequality and an equality,
2246 * the other constraints of the basic map containing the inequality are
2247 * "valid". Moreover, if the inequality the basic map is relaxed
2248 * and then turned into an equality, then resulting facet lies
2249 * entirely inside the other basic map
2250 * => the pair can be replaced by the basic map containing
2251 * the inequality, with the inequality relaxed.
2253 * 6. there is a single inequality adjacent to an equality,
2254 * the other constraints of the basic map containing the inequality are
2255 * "valid". Moreover, the facets corresponding to both
2256 * the inequality and the equality can be wrapped around their
2257 * ridges to include the other basic map
2258 * => the pair can be replaced by a basic map consisting
2259 * of the valid constraints in both basic maps together
2260 * with all wrapping constraints
2262 * 7. one of the basic maps extends beyond the other by at most one.
2263 * Moreover, the facets corresponding to the cut constraints and
2264 * the pieces of the other basic map at offset one from these cut
2265 * constraints can be wrapped around their ridges to include
2266 * the union of the two basic maps
2267 * => the pair can be replaced by a basic map consisting
2268 * of the valid constraints in both basic maps together
2269 * with all wrapping constraints
2271 * 8. the two basic maps live in adjacent hyperplanes. In principle
2272 * such sets can always be combined through wrapping, but we impose
2273 * that there is only one such pair, to avoid overeager coalescing.
2275 * Throughout the computation, we maintain a collection of tableaus
2276 * corresponding to the basic maps. When the basic maps are dropped
2277 * or combined, the tableaus are modified accordingly.
2279 static enum isl_change
coalesce_local_pair_reuse(int i
, int j
,
2280 struct isl_coalesce_info
*info
)
2282 enum isl_change change
= isl_change_none
;
2284 set_ineq_status_in(&info
[i
], info
[j
].tab
);
2285 if (info
[i
].bmap
->n_ineq
&& !info
[i
].ineq
)
2287 if (any_ineq(&info
[i
], STATUS_ERROR
))
2289 if (any_ineq(&info
[i
], STATUS_SEPARATE
))
2292 set_ineq_status_in(&info
[j
], info
[i
].tab
);
2293 if (info
[j
].bmap
->n_ineq
&& !info
[j
].ineq
)
2295 if (any_ineq(&info
[j
], STATUS_ERROR
))
2297 if (any_ineq(&info
[j
], STATUS_SEPARATE
))
2300 set_eq_status_in(&info
[i
], info
[j
].tab
);
2301 if (info
[i
].bmap
->n_eq
&& !info
[i
].eq
)
2303 if (any_eq(&info
[i
], STATUS_ERROR
))
2306 set_eq_status_in(&info
[j
], info
[i
].tab
);
2307 if (info
[j
].bmap
->n_eq
&& !info
[j
].eq
)
2309 if (any_eq(&info
[j
], STATUS_ERROR
))
2312 if (any_eq(&info
[i
], STATUS_SEPARATE
))
2313 return separating_equality(i
, j
, info
);
2314 if (any_eq(&info
[j
], STATUS_SEPARATE
))
2315 return separating_equality(j
, i
, info
);
2317 if (all(info
[i
].eq
, 2 * info
[i
].bmap
->n_eq
, STATUS_VALID
) &&
2318 all(info
[i
].ineq
, info
[i
].bmap
->n_ineq
, STATUS_VALID
)) {
2320 change
= isl_change_drop_second
;
2321 } else if (all(info
[j
].eq
, 2 * info
[j
].bmap
->n_eq
, STATUS_VALID
) &&
2322 all(info
[j
].ineq
, info
[j
].bmap
->n_ineq
, STATUS_VALID
)) {
2324 change
= isl_change_drop_first
;
2325 } else if (any_eq(&info
[i
], STATUS_ADJ_EQ
)) {
2326 change
= check_eq_adj_eq(i
, j
, info
);
2327 } else if (any_eq(&info
[j
], STATUS_ADJ_EQ
)) {
2328 change
= check_eq_adj_eq(j
, i
, info
);
2329 } else if (any_eq(&info
[i
], STATUS_ADJ_INEQ
) ||
2330 any_eq(&info
[j
], STATUS_ADJ_INEQ
)) {
2331 change
= check_adj_eq(i
, j
, info
);
2332 } else if (any_ineq(&info
[i
], STATUS_ADJ_EQ
)) {
2333 change
= check_ineq_adj_eq(i
, j
, info
);
2334 } else if (any_ineq(&info
[j
], STATUS_ADJ_EQ
)) {
2335 change
= check_ineq_adj_eq(j
, i
, info
);
2336 } else if (any_ineq(&info
[i
], STATUS_ADJ_INEQ
) ||
2337 any_ineq(&info
[j
], STATUS_ADJ_INEQ
)) {
2338 change
= check_adj_ineq(i
, j
, info
);
2340 if (!any_eq(&info
[i
], STATUS_CUT
) &&
2341 !any_eq(&info
[j
], STATUS_CUT
))
2342 change
= check_facets(i
, j
, info
);
2343 if (change
== isl_change_none
)
2344 change
= check_wrap(i
, j
, info
);
2348 clear_status(&info
[i
]);
2349 clear_status(&info
[j
]);
2352 clear_status(&info
[i
]);
2353 clear_status(&info
[j
]);
2354 return isl_change_error
;
2357 /* Check if the union of the given pair of basic maps
2358 * can be represented by a single basic map.
2359 * If so, replace the pair by the single basic map and return
2360 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
2361 * Otherwise, return isl_change_none.
2362 * The two basic maps are assumed to live in the same local space.
2364 static enum isl_change
coalesce_local_pair(int i
, int j
,
2365 struct isl_coalesce_info
*info
)
2367 init_status(&info
[i
]);
2368 init_status(&info
[j
]);
2369 return coalesce_local_pair_reuse(i
, j
, info
);
2372 /* Shift the integer division at position "div" of the basic map
2373 * represented by "info" by "shift".
2375 * That is, if the integer division has the form
2379 * then replace it by
2381 * floor((f(x) + shift * d)/d) - shift
2383 static isl_stat
shift_div(struct isl_coalesce_info
*info
, int div
,
2388 info
->bmap
= isl_basic_map_shift_div(info
->bmap
, div
, 0, shift
);
2390 return isl_stat_error
;
2392 total
= isl_basic_map_dim(info
->bmap
, isl_dim_all
);
2393 total
-= isl_basic_map_dim(info
->bmap
, isl_dim_div
);
2394 if (isl_tab_shift_var(info
->tab
, total
+ div
, shift
) < 0)
2395 return isl_stat_error
;
2400 /* If the integer division at position "div" is defined by an equality,
2401 * i.e., a stride constraint, then change the integer division expression
2402 * to have a constant term equal to zero.
2404 * Let the equality constraint be
2408 * The integer division expression is then typically of the form
2410 * a = floor((-f - c')/m)
2412 * The integer division is first shifted by t = floor(c/m),
2413 * turning the equality constraint into
2415 * c - m floor(c/m) + f + m a' = 0
2419 * (c mod m) + f + m a' = 0
2423 * a' = (-f - (c mod m))/m = floor((-f)/m)
2425 * because a' is an integer and 0 <= (c mod m) < m.
2426 * The constant term of a' can therefore be zeroed out,
2427 * but only if the integer division expression is of the expected form.
2429 static isl_stat
normalize_stride_div(struct isl_coalesce_info
*info
, int div
)
2431 isl_bool defined
, valid
;
2434 isl_int shift
, stride
;
2436 defined
= isl_basic_map_has_defining_equality(info
->bmap
, isl_dim_div
,
2439 return isl_stat_error
;
2443 return isl_stat_error
;
2444 valid
= isl_constraint_is_div_equality(c
, div
);
2445 isl_int_init(shift
);
2446 isl_int_init(stride
);
2447 isl_constraint_get_constant(c
, &shift
);
2448 isl_constraint_get_coefficient(c
, isl_dim_div
, div
, &stride
);
2449 isl_int_fdiv_q(shift
, shift
, stride
);
2450 r
= shift_div(info
, div
, shift
);
2451 isl_int_clear(stride
);
2452 isl_int_clear(shift
);
2453 isl_constraint_free(c
);
2454 if (r
< 0 || valid
< 0)
2455 return isl_stat_error
;
2458 info
->bmap
= isl_basic_map_set_div_expr_constant_num_si_inplace(
2459 info
->bmap
, div
, 0);
2461 return isl_stat_error
;
2465 /* The basic maps represented by "info1" and "info2" are known
2466 * to have the same number of integer divisions.
2467 * Check if pairs of integer divisions are equal to each other
2468 * despite the fact that they differ by a rational constant.
2470 * In particular, look for any pair of integer divisions that
2471 * only differ in their constant terms.
2472 * If either of these integer divisions is defined
2473 * by stride constraints, then modify it to have a zero constant term.
2474 * If both are defined by stride constraints then in the end they will have
2475 * the same (zero) constant term.
2477 static isl_stat
harmonize_stride_divs(struct isl_coalesce_info
*info1
,
2478 struct isl_coalesce_info
*info2
)
2482 n
= isl_basic_map_dim(info1
->bmap
, isl_dim_div
);
2483 for (i
= 0; i
< n
; ++i
) {
2484 isl_bool known
, harmonize
;
2486 known
= isl_basic_map_div_is_known(info1
->bmap
, i
);
2487 if (known
>= 0 && known
)
2488 known
= isl_basic_map_div_is_known(info2
->bmap
, i
);
2490 return isl_stat_error
;
2493 harmonize
= isl_basic_map_equal_div_expr_except_constant(
2494 info1
->bmap
, i
, info2
->bmap
, i
);
2496 return isl_stat_error
;
2499 if (normalize_stride_div(info1
, i
) < 0)
2500 return isl_stat_error
;
2501 if (normalize_stride_div(info2
, i
) < 0)
2502 return isl_stat_error
;
2508 /* If "shift" is an integer constant, then shift the integer division
2509 * at position "div" of the basic map represented by "info" by "shift".
2510 * If "shift" is not an integer constant, then do nothing.
2511 * If "shift" is equal to zero, then no shift needs to be performed either.
2513 * That is, if the integer division has the form
2517 * then replace it by
2519 * floor((f(x) + shift * d)/d) - shift
2521 static isl_stat
shift_if_cst_int(struct isl_coalesce_info
*info
, int div
,
2522 __isl_keep isl_aff
*shift
)
2529 cst
= isl_aff_is_cst(shift
);
2530 if (cst
< 0 || !cst
)
2531 return cst
< 0 ? isl_stat_error
: isl_stat_ok
;
2533 c
= isl_aff_get_constant_val(shift
);
2534 cst
= isl_val_is_int(c
);
2535 if (cst
>= 0 && cst
)
2536 cst
= isl_bool_not(isl_val_is_zero(c
));
2537 if (cst
< 0 || !cst
) {
2539 return cst
< 0 ? isl_stat_error
: isl_stat_ok
;
2543 r
= isl_val_get_num_isl_int(c
, &d
);
2545 r
= shift_div(info
, div
, d
);
2553 /* Check if some of the divs in the basic map represented by "info1"
2554 * are shifts of the corresponding divs in the basic map represented
2555 * by "info2", taking into account the equality constraints "eq1" of "info1"
2556 * and "eq2" of "info2". If so, align them with those of "info2".
2557 * "info1" and "info2" are assumed to have the same number
2558 * of integer divisions.
2560 * An integer division is considered to be a shift of another integer
2561 * division if, after simplification with respect to the equality
2562 * constraints of the other basic map, one is equal to the other
2565 * In particular, for each pair of integer divisions, if both are known,
2566 * have the same denominator and are not already equal to each other,
2567 * simplify each with respect to the equality constraints
2568 * of the other basic map. If the difference is an integer constant,
2569 * then move this difference outside.
2570 * That is, if, after simplification, one integer division is of the form
2572 * floor((f(x) + c_1)/d)
2574 * while the other is of the form
2576 * floor((f(x) + c_2)/d)
2578 * and n = (c_2 - c_1)/d is an integer, then replace the first
2579 * integer division by
2581 * floor((f_1(x) + c_1 + n * d)/d) - n,
2583 * where floor((f_1(x) + c_1 + n * d)/d) = floor((f2(x) + c_2)/d)
2584 * after simplification with respect to the equality constraints.
2586 static isl_stat
harmonize_divs_with_hulls(struct isl_coalesce_info
*info1
,
2587 struct isl_coalesce_info
*info2
, __isl_keep isl_basic_set
*eq1
,
2588 __isl_keep isl_basic_set
*eq2
)
2592 isl_local_space
*ls1
, *ls2
;
2594 total
= isl_basic_map_total_dim(info1
->bmap
);
2595 ls1
= isl_local_space_wrap(isl_basic_map_get_local_space(info1
->bmap
));
2596 ls2
= isl_local_space_wrap(isl_basic_map_get_local_space(info2
->bmap
));
2597 for (i
= 0; i
< info1
->bmap
->n_div
; ++i
) {
2599 isl_aff
*div1
, *div2
;
2601 if (!isl_local_space_div_is_known(ls1
, i
) ||
2602 !isl_local_space_div_is_known(ls2
, i
))
2604 if (isl_int_ne(info1
->bmap
->div
[i
][0], info2
->bmap
->div
[i
][0]))
2606 if (isl_seq_eq(info1
->bmap
->div
[i
] + 1,
2607 info2
->bmap
->div
[i
] + 1, 1 + total
))
2609 div1
= isl_local_space_get_div(ls1
, i
);
2610 div2
= isl_local_space_get_div(ls2
, i
);
2611 div1
= isl_aff_substitute_equalities(div1
,
2612 isl_basic_set_copy(eq2
));
2613 div2
= isl_aff_substitute_equalities(div2
,
2614 isl_basic_set_copy(eq1
));
2615 div2
= isl_aff_sub(div2
, div1
);
2616 r
= shift_if_cst_int(info1
, i
, div2
);
2621 isl_local_space_free(ls1
);
2622 isl_local_space_free(ls2
);
2624 if (i
< info1
->bmap
->n_div
)
2625 return isl_stat_error
;
2629 /* Check if some of the divs in the basic map represented by "info1"
2630 * are shifts of the corresponding divs in the basic map represented
2631 * by "info2". If so, align them with those of "info2".
2632 * Only do this if "info1" and "info2" have the same number
2633 * of integer divisions.
2635 * An integer division is considered to be a shift of another integer
2636 * division if, after simplification with respect to the equality
2637 * constraints of the other basic map, one is equal to the other
2640 * First check if pairs of integer divisions are equal to each other
2641 * despite the fact that they differ by a rational constant.
2642 * If so, try and arrange for them to have the same constant term.
2644 * Then, extract the equality constraints and continue with
2645 * harmonize_divs_with_hulls.
2647 * If the equality constraints of both basic maps are the same,
2648 * then there is no need to perform any shifting since
2649 * the coefficients of the integer divisions should have been
2650 * reduced in the same way.
2652 static isl_stat
harmonize_divs(struct isl_coalesce_info
*info1
,
2653 struct isl_coalesce_info
*info2
)
2656 isl_basic_map
*bmap1
, *bmap2
;
2657 isl_basic_set
*eq1
, *eq2
;
2660 if (!info1
->bmap
|| !info2
->bmap
)
2661 return isl_stat_error
;
2663 if (info1
->bmap
->n_div
!= info2
->bmap
->n_div
)
2665 if (info1
->bmap
->n_div
== 0)
2668 if (harmonize_stride_divs(info1
, info2
) < 0)
2669 return isl_stat_error
;
2671 bmap1
= isl_basic_map_copy(info1
->bmap
);
2672 bmap2
= isl_basic_map_copy(info2
->bmap
);
2673 eq1
= isl_basic_map_wrap(isl_basic_map_plain_affine_hull(bmap1
));
2674 eq2
= isl_basic_map_wrap(isl_basic_map_plain_affine_hull(bmap2
));
2675 equal
= isl_basic_set_plain_is_equal(eq1
, eq2
);
2681 r
= harmonize_divs_with_hulls(info1
, info2
, eq1
, eq2
);
2682 isl_basic_set_free(eq1
);
2683 isl_basic_set_free(eq2
);
2688 /* Do the two basic maps live in the same local space, i.e.,
2689 * do they have the same (known) divs?
2690 * If either basic map has any unknown divs, then we can only assume
2691 * that they do not live in the same local space.
2693 static isl_bool
same_divs(__isl_keep isl_basic_map
*bmap1
,
2694 __isl_keep isl_basic_map
*bmap2
)
2700 if (!bmap1
|| !bmap2
)
2701 return isl_bool_error
;
2702 if (bmap1
->n_div
!= bmap2
->n_div
)
2703 return isl_bool_false
;
2705 if (bmap1
->n_div
== 0)
2706 return isl_bool_true
;
2708 known
= isl_basic_map_divs_known(bmap1
);
2709 if (known
< 0 || !known
)
2711 known
= isl_basic_map_divs_known(bmap2
);
2712 if (known
< 0 || !known
)
2715 total
= isl_basic_map_total_dim(bmap1
);
2716 for (i
= 0; i
< bmap1
->n_div
; ++i
)
2717 if (!isl_seq_eq(bmap1
->div
[i
], bmap2
->div
[i
], 2 + total
))
2718 return isl_bool_false
;
2720 return isl_bool_true
;
2723 /* Assuming that "tab" contains the equality constraints and
2724 * the initial inequality constraints of "bmap", copy the remaining
2725 * inequality constraints of "bmap" to "Tab".
2727 static isl_stat
copy_ineq(struct isl_tab
*tab
, __isl_keep isl_basic_map
*bmap
)
2732 return isl_stat_error
;
2734 n_ineq
= tab
->n_con
- tab
->n_eq
;
2735 for (i
= n_ineq
; i
< bmap
->n_ineq
; ++i
)
2736 if (isl_tab_add_ineq(tab
, bmap
->ineq
[i
]) < 0)
2737 return isl_stat_error
;
2742 /* Description of an integer division that is added
2743 * during an expansion.
2744 * "pos" is the position of the corresponding variable.
2745 * "cst" indicates whether this integer division has a fixed value.
2746 * "val" contains the fixed value, if the value is fixed.
2748 struct isl_expanded
{
2754 /* For each of the "n" integer division variables "expanded",
2755 * if the variable has a fixed value, then add two inequality
2756 * constraints expressing the fixed value.
2757 * Otherwise, add the corresponding div constraints.
2758 * The caller is responsible for removing the div constraints
2759 * that it added for all these "n" integer divisions.
2761 * The div constraints and the pair of inequality constraints
2762 * forcing the fixed value cannot both be added for a given variable
2763 * as the combination may render some of the original constraints redundant.
2764 * These would then be ignored during the coalescing detection,
2765 * while they could remain in the fused result.
2767 * The two added inequality constraints are
2772 * with "a" the variable and "v" its fixed value.
2773 * The facet corresponding to one of these two constraints is selected
2774 * in the tableau to ensure that the pair of inequality constraints
2775 * is treated as an equality constraint.
2777 * The information in info->ineq is thrown away because it was
2778 * computed in terms of div constraints, while some of those
2779 * have now been replaced by these pairs of inequality constraints.
2781 static isl_stat
fix_constant_divs(struct isl_coalesce_info
*info
,
2782 int n
, struct isl_expanded
*expanded
)
2788 o_div
= isl_basic_map_offset(info
->bmap
, isl_dim_div
) - 1;
2789 ineq
= isl_vec_alloc(isl_tab_get_ctx(info
->tab
), 1 + info
->tab
->n_var
);
2791 return isl_stat_error
;
2792 isl_seq_clr(ineq
->el
+ 1, info
->tab
->n_var
);
2794 for (i
= 0; i
< n
; ++i
) {
2795 if (!expanded
[i
].cst
) {
2796 info
->bmap
= isl_basic_map_extend_constraints(
2798 if (isl_basic_map_add_div_constraints(info
->bmap
,
2799 expanded
[i
].pos
- o_div
) < 0)
2802 isl_int_set_si(ineq
->el
[1 + expanded
[i
].pos
], -1);
2803 isl_int_set(ineq
->el
[0], expanded
[i
].val
);
2804 info
->bmap
= isl_basic_map_add_ineq(info
->bmap
,
2806 isl_int_set_si(ineq
->el
[1 + expanded
[i
].pos
], 1);
2807 isl_int_neg(ineq
->el
[0], expanded
[i
].val
);
2808 info
->bmap
= isl_basic_map_add_ineq(info
->bmap
,
2810 isl_int_set_si(ineq
->el
[1 + expanded
[i
].pos
], 0);
2812 if (copy_ineq(info
->tab
, info
->bmap
) < 0)
2814 if (expanded
[i
].cst
&&
2815 isl_tab_select_facet(info
->tab
, info
->tab
->n_con
- 1) < 0)
2824 return i
< n
? isl_stat_error
: isl_stat_ok
;
2827 /* Insert the "n" integer division variables "expanded"
2828 * into info->tab and info->bmap and
2829 * update info->ineq with respect to the redundant constraints
2830 * in the resulting tableau.
2831 * "bmap" contains the result of this insertion in info->bmap,
2832 * while info->bmap is the original version
2833 * of "bmap", i.e., the one that corresponds to the current
2834 * state of info->tab. The number of constraints in info->bmap
2835 * is assumed to be the same as the number of constraints
2836 * in info->tab. This is required to be able to detect
2837 * the extra constraints in "bmap".
2839 * In particular, introduce extra variables corresponding
2840 * to the extra integer divisions and add the div constraints
2841 * that were added to "bmap" after info->tab was created
2843 * Furthermore, check if these extra integer divisions happen
2844 * to attain a fixed integer value in info->tab.
2845 * If so, replace the corresponding div constraints by pairs
2846 * of inequality constraints that fix these
2847 * integer divisions to their single integer values.
2848 * Replace info->bmap by "bmap" to match the changes to info->tab.
2849 * info->ineq was computed without a tableau and therefore
2850 * does not take into account the redundant constraints
2851 * in the tableau. Mark them here.
2852 * There is no need to check the newly added div constraints
2853 * since they cannot be redundant.
2854 * The redundancy check is not performed when constants have been discovered
2855 * since info->ineq is completely thrown away in this case.
2857 static isl_stat
tab_insert_divs(struct isl_coalesce_info
*info
,
2858 int n
, struct isl_expanded
*expanded
, __isl_take isl_basic_map
*bmap
)
2862 struct isl_tab_undo
*snap
;
2866 return isl_stat_error
;
2867 if (info
->bmap
->n_eq
+ info
->bmap
->n_ineq
!= info
->tab
->n_con
)
2868 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_internal
,
2869 "original tableau does not correspond "
2870 "to original basic map", goto error
);
2872 if (isl_tab_extend_vars(info
->tab
, n
) < 0)
2874 if (isl_tab_extend_cons(info
->tab
, 2 * n
) < 0)
2877 for (i
= 0; i
< n
; ++i
) {
2878 if (isl_tab_insert_var(info
->tab
, expanded
[i
].pos
) < 0)
2882 snap
= isl_tab_snap(info
->tab
);
2884 n_ineq
= info
->tab
->n_con
- info
->tab
->n_eq
;
2885 if (copy_ineq(info
->tab
, bmap
) < 0)
2888 isl_basic_map_free(info
->bmap
);
2892 for (i
= 0; i
< n
; ++i
) {
2893 expanded
[i
].cst
= isl_tab_is_constant(info
->tab
,
2894 expanded
[i
].pos
, &expanded
[i
].val
);
2895 if (expanded
[i
].cst
< 0)
2896 return isl_stat_error
;
2897 if (expanded
[i
].cst
)
2902 if (isl_tab_rollback(info
->tab
, snap
) < 0)
2903 return isl_stat_error
;
2904 info
->bmap
= isl_basic_map_cow(info
->bmap
);
2905 if (isl_basic_map_free_inequality(info
->bmap
, 2 * n
) < 0)
2906 return isl_stat_error
;
2908 return fix_constant_divs(info
, n
, expanded
);
2911 n_eq
= info
->bmap
->n_eq
;
2912 for (i
= 0; i
< n_ineq
; ++i
) {
2913 if (isl_tab_is_redundant(info
->tab
, n_eq
+ i
))
2914 info
->ineq
[i
] = STATUS_REDUNDANT
;
2919 isl_basic_map_free(bmap
);
2920 return isl_stat_error
;
2923 /* Expand info->tab and info->bmap in the same way "bmap" was expanded
2924 * in isl_basic_map_expand_divs using the expansion "exp" and
2925 * update info->ineq with respect to the redundant constraints
2926 * in the resulting tableau. info->bmap is the original version
2927 * of "bmap", i.e., the one that corresponds to the current
2928 * state of info->tab. The number of constraints in info->bmap
2929 * is assumed to be the same as the number of constraints
2930 * in info->tab. This is required to be able to detect
2931 * the extra constraints in "bmap".
2933 * Extract the positions where extra local variables are introduced
2934 * from "exp" and call tab_insert_divs.
2936 static isl_stat
expand_tab(struct isl_coalesce_info
*info
, int *exp
,
2937 __isl_take isl_basic_map
*bmap
)
2940 struct isl_expanded
*expanded
;
2943 unsigned total
, pos
, n_div
;
2946 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
2947 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
2948 pos
= total
- n_div
;
2949 extra_var
= total
- info
->tab
->n_var
;
2950 n
= n_div
- extra_var
;
2952 ctx
= isl_basic_map_get_ctx(bmap
);
2953 expanded
= isl_calloc_array(ctx
, struct isl_expanded
, extra_var
);
2954 if (extra_var
&& !expanded
)
2959 for (j
= 0; j
< n_div
; ++j
) {
2960 if (i
< n
&& exp
[i
] == j
) {
2964 expanded
[k
++].pos
= pos
+ j
;
2967 for (k
= 0; k
< extra_var
; ++k
)
2968 isl_int_init(expanded
[k
].val
);
2970 r
= tab_insert_divs(info
, extra_var
, expanded
, bmap
);
2972 for (k
= 0; k
< extra_var
; ++k
)
2973 isl_int_clear(expanded
[k
].val
);
2978 isl_basic_map_free(bmap
);
2979 return isl_stat_error
;
2982 /* Check if the union of the basic maps represented by info[i] and info[j]
2983 * can be represented by a single basic map,
2984 * after expanding the divs of info[i] to match those of info[j].
2985 * If so, replace the pair by the single basic map and return
2986 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
2987 * Otherwise, return isl_change_none.
2989 * The caller has already checked for info[j] being a subset of info[i].
2990 * If some of the divs of info[j] are unknown, then the expanded info[i]
2991 * will not have the corresponding div constraints. The other patterns
2992 * therefore cannot apply. Skip the computation in this case.
2994 * The expansion is performed using the divs "div" and expansion "exp"
2995 * computed by the caller.
2996 * info[i].bmap has already been expanded and the result is passed in
2998 * The "eq" and "ineq" fields of info[i] reflect the status of
2999 * the constraints of the expanded "bmap" with respect to info[j].tab.
3000 * However, inequality constraints that are redundant in info[i].tab
3001 * have not yet been marked as such because no tableau was available.
3003 * Replace info[i].bmap by "bmap" and expand info[i].tab as well,
3004 * updating info[i].ineq with respect to the redundant constraints.
3005 * Then try and coalesce the expanded info[i] with info[j],
3006 * reusing the information in info[i].eq and info[i].ineq.
3007 * If this does not result in any coalescing or if it results in info[j]
3008 * getting dropped (which should not happen in practice, since the case
3009 * of info[j] being a subset of info[i] has already been checked by
3010 * the caller), then revert info[i] to its original state.
3012 static enum isl_change
coalesce_expand_tab_divs(__isl_take isl_basic_map
*bmap
,
3013 int i
, int j
, struct isl_coalesce_info
*info
, __isl_keep isl_mat
*div
,
3017 isl_basic_map
*bmap_i
;
3018 struct isl_tab_undo
*snap
;
3019 enum isl_change change
= isl_change_none
;
3021 known
= isl_basic_map_divs_known(info
[j
].bmap
);
3022 if (known
< 0 || !known
) {
3023 clear_status(&info
[i
]);
3024 isl_basic_map_free(bmap
);
3025 return known
< 0 ? isl_change_error
: isl_change_none
;
3028 bmap_i
= isl_basic_map_copy(info
[i
].bmap
);
3029 snap
= isl_tab_snap(info
[i
].tab
);
3030 if (expand_tab(&info
[i
], exp
, bmap
) < 0)
3031 change
= isl_change_error
;
3033 init_status(&info
[j
]);
3034 if (change
== isl_change_none
)
3035 change
= coalesce_local_pair_reuse(i
, j
, info
);
3037 clear_status(&info
[i
]);
3038 if (change
!= isl_change_none
&& change
!= isl_change_drop_second
) {
3039 isl_basic_map_free(bmap_i
);
3041 isl_basic_map_free(info
[i
].bmap
);
3042 info
[i
].bmap
= bmap_i
;
3044 if (isl_tab_rollback(info
[i
].tab
, snap
) < 0)
3045 change
= isl_change_error
;
3051 /* Check if the union of "bmap" and the basic map represented by info[j]
3052 * can be represented by a single basic map,
3053 * after expanding the divs of "bmap" to match those of info[j].
3054 * If so, replace the pair by the single basic map and return
3055 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
3056 * Otherwise, return isl_change_none.
3058 * In particular, check if the expanded "bmap" contains the basic map
3059 * represented by the tableau info[j].tab.
3060 * The expansion is performed using the divs "div" and expansion "exp"
3061 * computed by the caller.
3062 * Then we check if all constraints of the expanded "bmap" are valid for
3065 * If "i" is not equal to -1, then "bmap" is equal to info[i].bmap.
3066 * In this case, the positions of the constraints of info[i].bmap
3067 * with respect to the basic map represented by info[j] are stored
3070 * If the expanded "bmap" does not contain the basic map
3071 * represented by the tableau info[j].tab and if "i" is not -1,
3072 * i.e., if the original "bmap" is info[i].bmap, then expand info[i].tab
3073 * as well and check if that results in coalescing.
3075 static enum isl_change
coalesce_with_expanded_divs(
3076 __isl_keep isl_basic_map
*bmap
, int i
, int j
,
3077 struct isl_coalesce_info
*info
, __isl_keep isl_mat
*div
, int *exp
)
3079 enum isl_change change
= isl_change_none
;
3080 struct isl_coalesce_info info_local
, *info_i
;
3082 info_i
= i
>= 0 ? &info
[i
] : &info_local
;
3083 init_status(info_i
);
3084 bmap
= isl_basic_map_copy(bmap
);
3085 bmap
= isl_basic_map_expand_divs(bmap
, isl_mat_copy(div
), exp
);
3086 bmap
= isl_basic_map_mark_final(bmap
);
3091 info_local
.bmap
= bmap
;
3092 info_i
->eq
= eq_status_in(bmap
, info
[j
].tab
);
3093 if (bmap
->n_eq
&& !info_i
->eq
)
3095 if (any_eq(info_i
, STATUS_ERROR
))
3097 if (any_eq(info_i
, STATUS_SEPARATE
))
3100 info_i
->ineq
= ineq_status_in(bmap
, NULL
, info
[j
].tab
);
3101 if (bmap
->n_ineq
&& !info_i
->ineq
)
3103 if (any_ineq(info_i
, STATUS_ERROR
))
3105 if (any_ineq(info_i
, STATUS_SEPARATE
))
3108 if (all(info_i
->eq
, 2 * bmap
->n_eq
, STATUS_VALID
) &&
3109 all(info_i
->ineq
, bmap
->n_ineq
, STATUS_VALID
)) {
3111 change
= isl_change_drop_second
;
3114 if (change
== isl_change_none
&& i
!= -1)
3115 return coalesce_expand_tab_divs(bmap
, i
, j
, info
, div
, exp
);
3118 isl_basic_map_free(bmap
);
3119 clear_status(info_i
);
3122 isl_basic_map_free(bmap
);
3123 clear_status(info_i
);
3124 return isl_change_error
;
3127 /* Check if the union of "bmap_i" and the basic map represented by info[j]
3128 * can be represented by a single basic map,
3129 * after aligning the divs of "bmap_i" to match those of info[j].
3130 * If so, replace the pair by the single basic map and return
3131 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
3132 * Otherwise, return isl_change_none.
3134 * In particular, check if "bmap_i" contains the basic map represented by
3135 * info[j] after aligning the divs of "bmap_i" to those of info[j].
3136 * Note that this can only succeed if the number of divs of "bmap_i"
3137 * is smaller than (or equal to) the number of divs of info[j].
3139 * We first check if the divs of "bmap_i" are all known and form a subset
3140 * of those of info[j].bmap. If so, we pass control over to
3141 * coalesce_with_expanded_divs.
3143 * If "i" is not equal to -1, then "bmap" is equal to info[i].bmap.
3145 static enum isl_change
coalesce_after_aligning_divs(
3146 __isl_keep isl_basic_map
*bmap_i
, int i
, int j
,
3147 struct isl_coalesce_info
*info
)
3150 isl_mat
*div_i
, *div_j
, *div
;
3154 enum isl_change change
;
3156 known
= isl_basic_map_divs_known(bmap_i
);
3158 return isl_change_error
;
3160 return isl_change_none
;
3162 ctx
= isl_basic_map_get_ctx(bmap_i
);
3164 div_i
= isl_basic_map_get_divs(bmap_i
);
3165 div_j
= isl_basic_map_get_divs(info
[j
].bmap
);
3167 if (!div_i
|| !div_j
)
3170 exp1
= isl_alloc_array(ctx
, int, div_i
->n_row
);
3171 exp2
= isl_alloc_array(ctx
, int, div_j
->n_row
);
3172 if ((div_i
->n_row
&& !exp1
) || (div_j
->n_row
&& !exp2
))
3175 div
= isl_merge_divs(div_i
, div_j
, exp1
, exp2
);
3179 if (div
->n_row
== div_j
->n_row
)
3180 change
= coalesce_with_expanded_divs(bmap_i
,
3181 i
, j
, info
, div
, exp1
);
3183 change
= isl_change_none
;
3187 isl_mat_free(div_i
);
3188 isl_mat_free(div_j
);
3195 isl_mat_free(div_i
);
3196 isl_mat_free(div_j
);
3199 return isl_change_error
;
3202 /* Check if basic map "j" is a subset of basic map "i" after
3203 * exploiting the extra equalities of "j" to simplify the divs of "i".
3204 * If so, remove basic map "j" and return isl_change_drop_second.
3206 * If "j" does not have any equalities or if they are the same
3207 * as those of "i", then we cannot exploit them to simplify the divs.
3208 * Similarly, if there are no divs in "i", then they cannot be simplified.
3209 * If, on the other hand, the affine hulls of "i" and "j" do not intersect,
3210 * then "j" cannot be a subset of "i".
3212 * Otherwise, we intersect "i" with the affine hull of "j" and then
3213 * check if "j" is a subset of the result after aligning the divs.
3214 * If so, then "j" is definitely a subset of "i" and can be removed.
3215 * Note that if after intersection with the affine hull of "j".
3216 * "i" still has more divs than "j", then there is no way we can
3217 * align the divs of "i" to those of "j".
3219 static enum isl_change
coalesce_subset_with_equalities(int i
, int j
,
3220 struct isl_coalesce_info
*info
)
3222 isl_basic_map
*hull_i
, *hull_j
, *bmap_i
;
3224 enum isl_change change
;
3226 if (info
[j
].bmap
->n_eq
== 0)
3227 return isl_change_none
;
3228 if (info
[i
].bmap
->n_div
== 0)
3229 return isl_change_none
;
3231 hull_i
= isl_basic_map_copy(info
[i
].bmap
);
3232 hull_i
= isl_basic_map_plain_affine_hull(hull_i
);
3233 hull_j
= isl_basic_map_copy(info
[j
].bmap
);
3234 hull_j
= isl_basic_map_plain_affine_hull(hull_j
);
3236 hull_j
= isl_basic_map_intersect(hull_j
, isl_basic_map_copy(hull_i
));
3237 equal
= isl_basic_map_plain_is_equal(hull_i
, hull_j
);
3238 empty
= isl_basic_map_plain_is_empty(hull_j
);
3239 isl_basic_map_free(hull_i
);
3241 if (equal
< 0 || equal
|| empty
< 0 || empty
) {
3242 isl_basic_map_free(hull_j
);
3243 if (equal
< 0 || empty
< 0)
3244 return isl_change_error
;
3245 return isl_change_none
;
3248 bmap_i
= isl_basic_map_copy(info
[i
].bmap
);
3249 bmap_i
= isl_basic_map_intersect(bmap_i
, hull_j
);
3251 return isl_change_error
;
3253 if (bmap_i
->n_div
> info
[j
].bmap
->n_div
) {
3254 isl_basic_map_free(bmap_i
);
3255 return isl_change_none
;
3258 change
= coalesce_after_aligning_divs(bmap_i
, -1, j
, info
);
3260 isl_basic_map_free(bmap_i
);
3265 /* Check if the union of and the basic maps represented by info[i] and info[j]
3266 * can be represented by a single basic map, by aligning or equating
3267 * their integer divisions.
3268 * If so, replace the pair by the single basic map and return
3269 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
3270 * Otherwise, return isl_change_none.
3272 * Note that we only perform any test if the number of divs is different
3273 * in the two basic maps. In case the number of divs is the same,
3274 * we have already established that the divs are different
3275 * in the two basic maps.
3276 * In particular, if the number of divs of basic map i is smaller than
3277 * the number of divs of basic map j, then we check if j is a subset of i
3280 static enum isl_change
coalesce_divs(int i
, int j
,
3281 struct isl_coalesce_info
*info
)
3283 enum isl_change change
= isl_change_none
;
3285 if (info
[i
].bmap
->n_div
< info
[j
].bmap
->n_div
)
3286 change
= coalesce_after_aligning_divs(info
[i
].bmap
, i
, j
, info
);
3287 if (change
!= isl_change_none
)
3290 if (info
[j
].bmap
->n_div
< info
[i
].bmap
->n_div
)
3291 change
= coalesce_after_aligning_divs(info
[j
].bmap
, j
, i
, info
);
3292 if (change
!= isl_change_none
)
3293 return invert_change(change
);
3295 change
= coalesce_subset_with_equalities(i
, j
, info
);
3296 if (change
!= isl_change_none
)
3299 change
= coalesce_subset_with_equalities(j
, i
, info
);
3300 if (change
!= isl_change_none
)
3301 return invert_change(change
);
3303 return isl_change_none
;
3306 /* Does "bmap" involve any divs that themselves refer to divs?
3308 static isl_bool
has_nested_div(__isl_keep isl_basic_map
*bmap
)
3314 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
3315 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
3318 for (i
= 0; i
< n_div
; ++i
)
3319 if (isl_seq_first_non_zero(bmap
->div
[i
] + 2 + total
,
3321 return isl_bool_true
;
3323 return isl_bool_false
;
3326 /* Return a list of affine expressions, one for each integer division
3327 * in "bmap_i". For each integer division that also appears in "bmap_j",
3328 * the affine expression is set to NaN. The number of NaNs in the list
3329 * is equal to the number of integer divisions in "bmap_j".
3330 * For the other integer divisions of "bmap_i", the corresponding
3331 * element in the list is a purely affine expression equal to the integer
3332 * division in "hull".
3333 * If no such list can be constructed, then the number of elements
3334 * in the returned list is smaller than the number of integer divisions
3337 static __isl_give isl_aff_list
*set_up_substitutions(
3338 __isl_keep isl_basic_map
*bmap_i
, __isl_keep isl_basic_map
*bmap_j
,
3339 __isl_take isl_basic_map
*hull
)
3341 unsigned n_div_i
, n_div_j
, total
;
3343 isl_local_space
*ls
;
3344 isl_basic_set
*wrap_hull
;
3352 ctx
= isl_basic_map_get_ctx(hull
);
3354 n_div_i
= isl_basic_map_dim(bmap_i
, isl_dim_div
);
3355 n_div_j
= isl_basic_map_dim(bmap_j
, isl_dim_div
);
3356 total
= isl_basic_map_total_dim(bmap_i
) - n_div_i
;
3358 ls
= isl_basic_map_get_local_space(bmap_i
);
3359 ls
= isl_local_space_wrap(ls
);
3360 wrap_hull
= isl_basic_map_wrap(hull
);
3362 aff_nan
= isl_aff_nan_on_domain(isl_local_space_copy(ls
));
3363 list
= isl_aff_list_alloc(ctx
, n_div_i
);
3366 for (i
= 0; i
< n_div_i
; ++i
) {
3370 isl_basic_map_equal_div_expr_part(bmap_i
, i
, bmap_j
, j
,
3373 list
= isl_aff_list_add(list
, isl_aff_copy(aff_nan
));
3376 if (n_div_i
- i
<= n_div_j
- j
)
3379 aff
= isl_local_space_get_div(ls
, i
);
3380 aff
= isl_aff_substitute_equalities(aff
,
3381 isl_basic_set_copy(wrap_hull
));
3382 aff
= isl_aff_floor(aff
);
3385 if (isl_aff_dim(aff
, isl_dim_div
) != 0) {
3390 list
= isl_aff_list_add(list
, aff
);
3393 isl_aff_free(aff_nan
);
3394 isl_local_space_free(ls
);
3395 isl_basic_set_free(wrap_hull
);
3399 isl_aff_free(aff_nan
);
3400 isl_local_space_free(ls
);
3401 isl_basic_set_free(wrap_hull
);
3402 isl_aff_list_free(list
);
3406 /* Add variables to info->bmap and info->tab corresponding to the elements
3407 * in "list" that are not set to NaN.
3408 * "extra_var" is the number of these elements.
3409 * "dim" is the offset in the variables of "tab" where we should
3410 * start considering the elements in "list".
3411 * When this function returns, the total number of variables in "tab"
3412 * is equal to "dim" plus the number of elements in "list".
3414 * The newly added existentially quantified variables are not given
3415 * an explicit representation because the corresponding div constraints
3416 * do not appear in info->bmap. These constraints are not added
3417 * to info->bmap because for internal consistency, they would need to
3418 * be added to info->tab as well, where they could combine with the equality
3419 * that is added later to result in constraints that do not hold
3420 * in the original input.
3422 static isl_stat
add_sub_vars(struct isl_coalesce_info
*info
,
3423 __isl_keep isl_aff_list
*list
, int dim
, int extra_var
)
3428 space
= isl_basic_map_get_space(info
->bmap
);
3429 info
->bmap
= isl_basic_map_cow(info
->bmap
);
3430 info
->bmap
= isl_basic_map_extend_space(info
->bmap
, space
,
3433 return isl_stat_error
;
3434 n
= isl_aff_list_n_aff(list
);
3435 for (i
= 0; i
< n
; ++i
) {
3439 aff
= isl_aff_list_get_aff(list
, i
);
3440 is_nan
= isl_aff_is_nan(aff
);
3443 return isl_stat_error
;
3447 if (isl_tab_insert_var(info
->tab
, dim
+ i
) < 0)
3448 return isl_stat_error
;
3449 d
= isl_basic_map_alloc_div(info
->bmap
);
3451 return isl_stat_error
;
3452 info
->bmap
= isl_basic_map_mark_div_unknown(info
->bmap
, d
);
3454 return isl_stat_error
;
3455 for (j
= d
; j
> i
; --j
)
3456 isl_basic_map_swap_div(info
->bmap
, j
- 1, j
);
3462 /* For each element in "list" that is not set to NaN, fix the corresponding
3463 * variable in "tab" to the purely affine expression defined by the element.
3464 * "dim" is the offset in the variables of "tab" where we should
3465 * start considering the elements in "list".
3467 * This function assumes that a sufficient number of rows and
3468 * elements in the constraint array are available in the tableau.
3470 static int add_sub_equalities(struct isl_tab
*tab
,
3471 __isl_keep isl_aff_list
*list
, int dim
)
3478 n
= isl_aff_list_n_aff(list
);
3480 ctx
= isl_tab_get_ctx(tab
);
3481 sub
= isl_vec_alloc(ctx
, 1 + dim
+ n
);
3484 isl_seq_clr(sub
->el
+ 1 + dim
, n
);
3486 for (i
= 0; i
< n
; ++i
) {
3487 aff
= isl_aff_list_get_aff(list
, i
);
3490 if (isl_aff_is_nan(aff
)) {
3494 isl_seq_cpy(sub
->el
, aff
->v
->el
+ 1, 1 + dim
);
3495 isl_int_neg(sub
->el
[1 + dim
+ i
], aff
->v
->el
[0]);
3496 if (isl_tab_add_eq(tab
, sub
->el
) < 0)
3498 isl_int_set_si(sub
->el
[1 + dim
+ i
], 0);
3510 /* Add variables to info->tab and info->bmap corresponding to the elements
3511 * in "list" that are not set to NaN. The value of the added variable
3512 * in info->tab is fixed to the purely affine expression defined by the element.
3513 * "dim" is the offset in the variables of info->tab where we should
3514 * start considering the elements in "list".
3515 * When this function returns, the total number of variables in info->tab
3516 * is equal to "dim" plus the number of elements in "list".
3518 static int add_subs(struct isl_coalesce_info
*info
,
3519 __isl_keep isl_aff_list
*list
, int dim
)
3527 n
= isl_aff_list_n_aff(list
);
3528 extra_var
= n
- (info
->tab
->n_var
- dim
);
3530 if (isl_tab_extend_vars(info
->tab
, extra_var
) < 0)
3532 if (isl_tab_extend_cons(info
->tab
, 2 * extra_var
) < 0)
3534 if (add_sub_vars(info
, list
, dim
, extra_var
) < 0)
3537 return add_sub_equalities(info
->tab
, list
, dim
);
3540 /* Coalesce basic map "j" into basic map "i" after adding the extra integer
3541 * divisions in "i" but not in "j" to basic map "j", with values
3542 * specified by "list". The total number of elements in "list"
3543 * is equal to the number of integer divisions in "i", while the number
3544 * of NaN elements in the list is equal to the number of integer divisions
3547 * If no coalescing can be performed, then we need to revert basic map "j"
3548 * to its original state. We do the same if basic map "i" gets dropped
3549 * during the coalescing, even though this should not happen in practice
3550 * since we have already checked for "j" being a subset of "i"
3551 * before we reach this stage.
3553 static enum isl_change
coalesce_with_subs(int i
, int j
,
3554 struct isl_coalesce_info
*info
, __isl_keep isl_aff_list
*list
)
3556 isl_basic_map
*bmap_j
;
3557 struct isl_tab_undo
*snap
;
3559 enum isl_change change
;
3561 bmap_j
= isl_basic_map_copy(info
[j
].bmap
);
3562 snap
= isl_tab_snap(info
[j
].tab
);
3564 dim
= isl_basic_map_dim(bmap_j
, isl_dim_all
);
3565 dim
-= isl_basic_map_dim(bmap_j
, isl_dim_div
);
3566 if (add_subs(&info
[j
], list
, dim
) < 0)
3569 change
= coalesce_local_pair(i
, j
, info
);
3570 if (change
!= isl_change_none
&& change
!= isl_change_drop_first
) {
3571 isl_basic_map_free(bmap_j
);
3573 isl_basic_map_free(info
[j
].bmap
);
3574 info
[j
].bmap
= bmap_j
;
3576 if (isl_tab_rollback(info
[j
].tab
, snap
) < 0)
3577 return isl_change_error
;
3582 isl_basic_map_free(bmap_j
);
3583 return isl_change_error
;
3586 /* Check if we can coalesce basic map "j" into basic map "i" after copying
3587 * those extra integer divisions in "i" that can be simplified away
3588 * using the extra equalities in "j".
3589 * All divs are assumed to be known and not contain any nested divs.
3591 * We first check if there are any extra equalities in "j" that we
3592 * can exploit. Then we check if every integer division in "i"
3593 * either already appears in "j" or can be simplified using the
3594 * extra equalities to a purely affine expression.
3595 * If these tests succeed, then we try to coalesce the two basic maps
3596 * by introducing extra dimensions in "j" corresponding to
3597 * the extra integer divsisions "i" fixed to the corresponding
3598 * purely affine expression.
3600 static enum isl_change
check_coalesce_into_eq(int i
, int j
,
3601 struct isl_coalesce_info
*info
)
3603 unsigned n_div_i
, n_div_j
;
3604 isl_basic_map
*hull_i
, *hull_j
;
3607 enum isl_change change
;
3609 n_div_i
= isl_basic_map_dim(info
[i
].bmap
, isl_dim_div
);
3610 n_div_j
= isl_basic_map_dim(info
[j
].bmap
, isl_dim_div
);
3611 if (n_div_i
<= n_div_j
)
3612 return isl_change_none
;
3613 if (info
[j
].bmap
->n_eq
== 0)
3614 return isl_change_none
;
3616 hull_i
= isl_basic_map_copy(info
[i
].bmap
);
3617 hull_i
= isl_basic_map_plain_affine_hull(hull_i
);
3618 hull_j
= isl_basic_map_copy(info
[j
].bmap
);
3619 hull_j
= isl_basic_map_plain_affine_hull(hull_j
);
3621 hull_j
= isl_basic_map_intersect(hull_j
, isl_basic_map_copy(hull_i
));
3622 equal
= isl_basic_map_plain_is_equal(hull_i
, hull_j
);
3623 empty
= isl_basic_map_plain_is_empty(hull_j
);
3624 isl_basic_map_free(hull_i
);
3626 if (equal
< 0 || empty
< 0)
3628 if (equal
|| empty
) {
3629 isl_basic_map_free(hull_j
);
3630 return isl_change_none
;
3633 list
= set_up_substitutions(info
[i
].bmap
, info
[j
].bmap
, hull_j
);
3635 return isl_change_error
;
3636 if (isl_aff_list_n_aff(list
) < n_div_i
)
3637 change
= isl_change_none
;
3639 change
= coalesce_with_subs(i
, j
, info
, list
);
3641 isl_aff_list_free(list
);
3645 isl_basic_map_free(hull_j
);
3646 return isl_change_error
;
3649 /* Check if we can coalesce basic maps "i" and "j" after copying
3650 * those extra integer divisions in one of the basic maps that can
3651 * be simplified away using the extra equalities in the other basic map.
3652 * We require all divs to be known in both basic maps.
3653 * Furthermore, to simplify the comparison of div expressions,
3654 * we do not allow any nested integer divisions.
3656 static enum isl_change
check_coalesce_eq(int i
, int j
,
3657 struct isl_coalesce_info
*info
)
3659 isl_bool known
, nested
;
3660 enum isl_change change
;
3662 known
= isl_basic_map_divs_known(info
[i
].bmap
);
3663 if (known
< 0 || !known
)
3664 return known
< 0 ? isl_change_error
: isl_change_none
;
3665 known
= isl_basic_map_divs_known(info
[j
].bmap
);
3666 if (known
< 0 || !known
)
3667 return known
< 0 ? isl_change_error
: isl_change_none
;
3668 nested
= has_nested_div(info
[i
].bmap
);
3669 if (nested
< 0 || nested
)
3670 return nested
< 0 ? isl_change_error
: isl_change_none
;
3671 nested
= has_nested_div(info
[j
].bmap
);
3672 if (nested
< 0 || nested
)
3673 return nested
< 0 ? isl_change_error
: isl_change_none
;
3675 change
= check_coalesce_into_eq(i
, j
, info
);
3676 if (change
!= isl_change_none
)
3678 change
= check_coalesce_into_eq(j
, i
, info
);
3679 if (change
!= isl_change_none
)
3680 return invert_change(change
);
3682 return isl_change_none
;
3685 /* Check if the union of the given pair of basic maps
3686 * can be represented by a single basic map.
3687 * If so, replace the pair by the single basic map and return
3688 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
3689 * Otherwise, return isl_change_none.
3691 * We first check if the two basic maps live in the same local space,
3692 * after aligning the divs that differ by only an integer constant.
3693 * If so, we do the complete check. Otherwise, we check if they have
3694 * the same number of integer divisions and can be coalesced, if one is
3695 * an obvious subset of the other or if the extra integer divisions
3696 * of one basic map can be simplified away using the extra equalities
3697 * of the other basic map.
3699 * Note that trying to coalesce pairs of disjuncts with the same
3700 * number, but different local variables may drop the explicit
3701 * representation of some of these local variables.
3702 * This operation is therefore not performed when
3703 * the "coalesce_preserve_locals" option is set.
3705 static enum isl_change
coalesce_pair(int i
, int j
,
3706 struct isl_coalesce_info
*info
)
3710 enum isl_change change
;
3713 if (harmonize_divs(&info
[i
], &info
[j
]) < 0)
3714 return isl_change_error
;
3715 same
= same_divs(info
[i
].bmap
, info
[j
].bmap
);
3717 return isl_change_error
;
3719 return coalesce_local_pair(i
, j
, info
);
3721 ctx
= isl_basic_map_get_ctx(info
[i
].bmap
);
3722 preserve
= isl_options_get_coalesce_preserve_locals(ctx
);
3723 if (!preserve
&& info
[i
].bmap
->n_div
== info
[j
].bmap
->n_div
) {
3724 change
= coalesce_local_pair(i
, j
, info
);
3725 if (change
!= isl_change_none
)
3729 change
= coalesce_divs(i
, j
, info
);
3730 if (change
!= isl_change_none
)
3733 return check_coalesce_eq(i
, j
, info
);
3736 /* Return the maximum of "a" and "b".
3738 static int isl_max(int a
, int b
)
3740 return a
> b
? a
: b
;
3743 /* Pairwise coalesce the basic maps in the range [start1, end1[ of "info"
3744 * with those in the range [start2, end2[, skipping basic maps
3745 * that have been removed (either before or within this function).
3747 * For each basic map i in the first range, we check if it can be coalesced
3748 * with respect to any previously considered basic map j in the second range.
3749 * If i gets dropped (because it was a subset of some j), then
3750 * we can move on to the next basic map.
3751 * If j gets dropped, we need to continue checking against the other
3752 * previously considered basic maps.
3753 * If the two basic maps got fused, then we recheck the fused basic map
3754 * against the previously considered basic maps, starting at i + 1
3755 * (even if start2 is greater than i + 1).
3757 static int coalesce_range(isl_ctx
*ctx
, struct isl_coalesce_info
*info
,
3758 int start1
, int end1
, int start2
, int end2
)
3762 for (i
= end1
- 1; i
>= start1
; --i
) {
3763 if (info
[i
].removed
)
3765 for (j
= isl_max(i
+ 1, start2
); j
< end2
; ++j
) {
3766 enum isl_change changed
;
3768 if (info
[j
].removed
)
3770 if (info
[i
].removed
)
3771 isl_die(ctx
, isl_error_internal
,
3772 "basic map unexpectedly removed",
3774 changed
= coalesce_pair(i
, j
, info
);
3776 case isl_change_error
:
3778 case isl_change_none
:
3779 case isl_change_drop_second
:
3781 case isl_change_drop_first
:
3784 case isl_change_fuse
:
3794 /* Pairwise coalesce the basic maps described by the "n" elements of "info".
3796 * We consider groups of basic maps that live in the same apparent
3797 * affine hull and we first coalesce within such a group before we
3798 * coalesce the elements in the group with elements of previously
3799 * considered groups. If a fuse happens during the second phase,
3800 * then we also reconsider the elements within the group.
3802 static int coalesce(isl_ctx
*ctx
, int n
, struct isl_coalesce_info
*info
)
3806 for (end
= n
; end
> 0; end
= start
) {
3808 while (start
>= 1 &&
3809 info
[start
- 1].hull_hash
== info
[start
].hull_hash
)
3811 if (coalesce_range(ctx
, info
, start
, end
, start
, end
) < 0)
3813 if (coalesce_range(ctx
, info
, start
, end
, end
, n
) < 0)
3820 /* Update the basic maps in "map" based on the information in "info".
3821 * In particular, remove the basic maps that have been marked removed and
3822 * update the others based on the information in the corresponding tableau.
3823 * Since we detected implicit equalities without calling
3824 * isl_basic_map_gauss, we need to do it now.
3825 * Also call isl_basic_map_simplify if we may have lost the definition
3826 * of one or more integer divisions.
3828 static __isl_give isl_map
*update_basic_maps(__isl_take isl_map
*map
,
3829 int n
, struct isl_coalesce_info
*info
)
3836 for (i
= n
- 1; i
>= 0; --i
) {
3837 if (info
[i
].removed
) {
3838 isl_basic_map_free(map
->p
[i
]);
3839 if (i
!= map
->n
- 1)
3840 map
->p
[i
] = map
->p
[map
->n
- 1];
3845 info
[i
].bmap
= isl_basic_map_update_from_tab(info
[i
].bmap
,
3847 info
[i
].bmap
= isl_basic_map_gauss(info
[i
].bmap
, NULL
);
3848 if (info
[i
].simplify
)
3849 info
[i
].bmap
= isl_basic_map_simplify(info
[i
].bmap
);
3850 info
[i
].bmap
= isl_basic_map_finalize(info
[i
].bmap
);
3852 return isl_map_free(map
);
3853 ISL_F_SET(info
[i
].bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
3854 ISL_F_SET(info
[i
].bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
3855 isl_basic_map_free(map
->p
[i
]);
3856 map
->p
[i
] = info
[i
].bmap
;
3857 info
[i
].bmap
= NULL
;
3863 /* For each pair of basic maps in the map, check if the union of the two
3864 * can be represented by a single basic map.
3865 * If so, replace the pair by the single basic map and start over.
3867 * We factor out any (hidden) common factor from the constraint
3868 * coefficients to improve the detection of adjacent constraints.
3870 * Since we are constructing the tableaus of the basic maps anyway,
3871 * we exploit them to detect implicit equalities and redundant constraints.
3872 * This also helps the coalescing as it can ignore the redundant constraints.
3873 * In order to avoid confusion, we make all implicit equalities explicit
3874 * in the basic maps. We don't call isl_basic_map_gauss, though,
3875 * as that may affect the number of constraints.
3876 * This means that we have to call isl_basic_map_gauss at the end
3877 * of the computation (in update_basic_maps and in clear) to ensure that
3878 * the basic maps are not left in an unexpected state.
3879 * For each basic map, we also compute the hash of the apparent affine hull
3880 * for use in coalesce.
3882 __isl_give isl_map
*isl_map_coalesce(__isl_take isl_map
*map
)
3887 struct isl_coalesce_info
*info
= NULL
;
3889 map
= isl_map_remove_empty_parts(map
);
3896 ctx
= isl_map_get_ctx(map
);
3897 map
= isl_map_sort_divs(map
);
3898 map
= isl_map_cow(map
);
3905 info
= isl_calloc_array(map
->ctx
, struct isl_coalesce_info
, n
);
3909 for (i
= 0; i
< map
->n
; ++i
) {
3910 map
->p
[i
] = isl_basic_map_reduce_coefficients(map
->p
[i
]);
3913 info
[i
].bmap
= isl_basic_map_copy(map
->p
[i
]);
3914 info
[i
].tab
= isl_tab_from_basic_map(info
[i
].bmap
, 0);
3917 if (!ISL_F_ISSET(info
[i
].bmap
, ISL_BASIC_MAP_NO_IMPLICIT
))
3918 if (isl_tab_detect_implicit_equalities(info
[i
].tab
) < 0)
3920 info
[i
].bmap
= isl_tab_make_equalities_explicit(info
[i
].tab
,
3924 if (!ISL_F_ISSET(info
[i
].bmap
, ISL_BASIC_MAP_NO_REDUNDANT
))
3925 if (isl_tab_detect_redundant(info
[i
].tab
) < 0)
3927 if (coalesce_info_set_hull_hash(&info
[i
]) < 0)
3930 for (i
= map
->n
- 1; i
>= 0; --i
)
3931 if (info
[i
].tab
->empty
)
3934 if (coalesce(ctx
, n
, info
) < 0)
3937 map
= update_basic_maps(map
, n
, info
);
3939 clear_coalesce_info(n
, info
);
3943 clear_coalesce_info(n
, info
);
3948 /* For each pair of basic sets in the set, check if the union of the two
3949 * can be represented by a single basic set.
3950 * If so, replace the pair by the single basic set and start over.
3952 struct isl_set
*isl_set_coalesce(struct isl_set
*set
)
3954 return set_from_map(isl_map_coalesce(set_to_map(set
)));