2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2012-2013 Ecole Normale Superieure
5 * Copyright 2014 INRIA Rocquencourt
6 * Copyright 2016 INRIA Paris
8 * Use of this software is governed by the MIT license
10 * Written by Sven Verdoolaege, K.U.Leuven, Departement
11 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
12 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
13 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
14 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
15 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
16 * B.P. 105 - 78153 Le Chesnay, France
17 * and Centre de Recherche Inria de Paris, 2 rue Simone Iff - Voie DQ12,
18 * CS 42112, 75589 Paris Cedex 12, France
21 #include <isl_ctx_private.h>
22 #include "isl_map_private.h"
24 #include <isl/options.h>
26 #include <isl_mat_private.h>
27 #include <isl_local_space_private.h>
28 #include <isl_val_private.h>
29 #include <isl_vec_private.h>
30 #include <isl_aff_private.h>
31 #include <isl_equalities.h>
32 #include <isl_constraint_private.h>
34 #include <set_to_map.c>
35 #include <set_from_map.c>
37 #define STATUS_ERROR -1
38 #define STATUS_REDUNDANT 1
39 #define STATUS_VALID 2
40 #define STATUS_SEPARATE 3
42 #define STATUS_ADJ_EQ 5
43 #define STATUS_ADJ_INEQ 6
45 static int status_in(isl_int
*ineq
, struct isl_tab
*tab
)
47 enum isl_ineq_type type
= isl_tab_ineq_type(tab
, ineq
);
50 case isl_ineq_error
: return STATUS_ERROR
;
51 case isl_ineq_redundant
: return STATUS_VALID
;
52 case isl_ineq_separate
: return STATUS_SEPARATE
;
53 case isl_ineq_cut
: return STATUS_CUT
;
54 case isl_ineq_adj_eq
: return STATUS_ADJ_EQ
;
55 case isl_ineq_adj_ineq
: return STATUS_ADJ_INEQ
;
59 /* Compute the position of the equalities of basic map "bmap_i"
60 * with respect to the basic map represented by "tab_j".
61 * The resulting array has twice as many entries as the number
62 * of equalities corresponding to the two inequalities to which
63 * each equality corresponds.
65 static int *eq_status_in(__isl_keep isl_basic_map
*bmap_i
,
66 struct isl_tab
*tab_j
)
72 dim
= isl_basic_map_dim(bmap_i
, isl_dim_all
);
76 eq
= isl_calloc_array(bmap_i
->ctx
, int, 2 * bmap_i
->n_eq
);
80 for (k
= 0; k
< bmap_i
->n_eq
; ++k
) {
81 for (l
= 0; l
< 2; ++l
) {
82 isl_seq_neg(bmap_i
->eq
[k
], bmap_i
->eq
[k
], 1+dim
);
83 eq
[2 * k
+ l
] = status_in(bmap_i
->eq
[k
], tab_j
);
84 if (eq
[2 * k
+ l
] == STATUS_ERROR
)
95 /* Compute the position of the inequalities of basic map "bmap_i"
96 * (also represented by "tab_i", if not NULL) with respect to the basic map
97 * represented by "tab_j".
99 static int *ineq_status_in(__isl_keep isl_basic_map
*bmap_i
,
100 struct isl_tab
*tab_i
, struct isl_tab
*tab_j
)
103 unsigned n_eq
= bmap_i
->n_eq
;
104 int *ineq
= isl_calloc_array(bmap_i
->ctx
, int, bmap_i
->n_ineq
);
109 for (k
= 0; k
< bmap_i
->n_ineq
; ++k
) {
110 if (tab_i
&& isl_tab_is_redundant(tab_i
, n_eq
+ k
)) {
111 ineq
[k
] = STATUS_REDUNDANT
;
114 ineq
[k
] = status_in(bmap_i
->ineq
[k
], tab_j
);
115 if (ineq
[k
] == STATUS_ERROR
)
117 if (ineq
[k
] == STATUS_SEPARATE
)
127 static int any(int *con
, unsigned len
, int status
)
131 for (i
= 0; i
< len
; ++i
)
132 if (con
[i
] == status
)
137 /* Return the first position of "status" in the list "con" of length "len".
138 * Return -1 if there is no such entry.
140 static int find(int *con
, unsigned len
, int status
)
144 for (i
= 0; i
< len
; ++i
)
145 if (con
[i
] == status
)
150 static int count(int *con
, unsigned len
, int status
)
155 for (i
= 0; i
< len
; ++i
)
156 if (con
[i
] == status
)
161 static int all(int *con
, unsigned len
, int status
)
165 for (i
= 0; i
< len
; ++i
) {
166 if (con
[i
] == STATUS_REDUNDANT
)
168 if (con
[i
] != status
)
174 /* Internal information associated to a basic map in a map
175 * that is to be coalesced by isl_map_coalesce.
177 * "bmap" is the basic map itself (or NULL if "removed" is set)
178 * "tab" is the corresponding tableau (or NULL if "removed" is set)
179 * "hull_hash" identifies the affine space in which "bmap" lives.
180 * "removed" is set if this basic map has been removed from the map
181 * "simplify" is set if this basic map may have some unknown integer
182 * divisions that were not present in the input basic maps. The basic
183 * map should then be simplified such that we may be able to find
184 * a definition among the constraints.
186 * "eq" and "ineq" are only set if we are currently trying to coalesce
187 * this basic map with another basic map, in which case they represent
188 * the position of the inequalities of this basic map with respect to
189 * the other basic map. The number of elements in the "eq" array
190 * is twice the number of equalities in the "bmap", corresponding
191 * to the two inequalities that make up each equality.
193 struct isl_coalesce_info
{
203 /* Is there any (half of an) equality constraint in the description
204 * of the basic map represented by "info" that
205 * has position "status" with respect to the other basic map?
207 static int any_eq(struct isl_coalesce_info
*info
, int status
)
211 n_eq
= isl_basic_map_n_equality(info
->bmap
);
212 return any(info
->eq
, 2 * n_eq
, status
);
215 /* Is there any inequality constraint in the description
216 * of the basic map represented by "info" that
217 * has position "status" with respect to the other basic map?
219 static int any_ineq(struct isl_coalesce_info
*info
, int status
)
223 n_ineq
= isl_basic_map_n_inequality(info
->bmap
);
224 return any(info
->ineq
, n_ineq
, status
);
227 /* Return the position of the first half on an equality constraint
228 * in the description of the basic map represented by "info" that
229 * has position "status" with respect to the other basic map.
230 * The returned value is twice the position of the equality constraint
231 * plus zero for the negative half and plus one for the positive half.
232 * Return -1 if there is no such entry.
234 static int find_eq(struct isl_coalesce_info
*info
, int status
)
238 n_eq
= isl_basic_map_n_equality(info
->bmap
);
239 return find(info
->eq
, 2 * n_eq
, status
);
242 /* Return the position of the first inequality constraint in the description
243 * of the basic map represented by "info" that
244 * has position "status" with respect to the other basic map.
245 * Return -1 if there is no such entry.
247 static int find_ineq(struct isl_coalesce_info
*info
, int status
)
251 n_ineq
= isl_basic_map_n_inequality(info
->bmap
);
252 return find(info
->ineq
, n_ineq
, status
);
255 /* Return the number of (halves of) equality constraints in the description
256 * of the basic map represented by "info" that
257 * have position "status" with respect to the other basic map.
259 static int count_eq(struct isl_coalesce_info
*info
, int status
)
263 n_eq
= isl_basic_map_n_equality(info
->bmap
);
264 return count(info
->eq
, 2 * n_eq
, status
);
267 /* Return the number of inequality constraints in the description
268 * of the basic map represented by "info" that
269 * have position "status" with respect to the other basic map.
271 static int count_ineq(struct isl_coalesce_info
*info
, int status
)
275 n_ineq
= isl_basic_map_n_inequality(info
->bmap
);
276 return count(info
->ineq
, n_ineq
, status
);
279 /* Are all non-redundant constraints of the basic map represented by "info"
280 * either valid or cut constraints with respect to the other basic map?
282 static int all_valid_or_cut(struct isl_coalesce_info
*info
)
286 for (i
= 0; i
< 2 * info
->bmap
->n_eq
; ++i
) {
287 if (info
->eq
[i
] == STATUS_REDUNDANT
)
289 if (info
->eq
[i
] == STATUS_VALID
)
291 if (info
->eq
[i
] == STATUS_CUT
)
296 for (i
= 0; i
< info
->bmap
->n_ineq
; ++i
) {
297 if (info
->ineq
[i
] == STATUS_REDUNDANT
)
299 if (info
->ineq
[i
] == STATUS_VALID
)
301 if (info
->ineq
[i
] == STATUS_CUT
)
309 /* Compute the hash of the (apparent) affine hull of info->bmap (with
310 * the existentially quantified variables removed) and store it
313 static int coalesce_info_set_hull_hash(struct isl_coalesce_info
*info
)
318 hull
= isl_basic_map_copy(info
->bmap
);
319 hull
= isl_basic_map_plain_affine_hull(hull
);
320 n_div
= isl_basic_map_dim(hull
, isl_dim_div
);
322 hull
= isl_basic_map_free(hull
);
323 hull
= isl_basic_map_drop_constraints_involving_dims(hull
,
324 isl_dim_div
, 0, n_div
);
325 info
->hull_hash
= isl_basic_map_get_hash(hull
);
326 isl_basic_map_free(hull
);
328 return hull
? 0 : -1;
331 /* Free all the allocated memory in an array
332 * of "n" isl_coalesce_info elements.
334 static void clear_coalesce_info(int n
, struct isl_coalesce_info
*info
)
341 for (i
= 0; i
< n
; ++i
) {
342 isl_basic_map_free(info
[i
].bmap
);
343 isl_tab_free(info
[i
].tab
);
349 /* Clear the memory associated to"info".
350 * Gaussian elimination needs to be performed on the basic map
351 * before it gets freed because it may have been put
352 * in an inconsistent state in isl_map_coalesce while it may
353 * be shared with other maps.
355 static void clear(struct isl_coalesce_info
*info
)
357 info
->bmap
= isl_basic_map_gauss(info
->bmap
, NULL
);
358 info
->bmap
= isl_basic_map_free(info
->bmap
);
359 isl_tab_free(info
->tab
);
363 /* Drop the basic map represented by "info".
364 * That is, clear the memory associated to the entry and
365 * mark it as having been removed.
367 static void drop(struct isl_coalesce_info
*info
)
373 /* Exchange the information in "info1" with that in "info2".
375 static void exchange(struct isl_coalesce_info
*info1
,
376 struct isl_coalesce_info
*info2
)
378 struct isl_coalesce_info info
;
385 /* This type represents the kind of change that has been performed
386 * while trying to coalesce two basic maps.
388 * isl_change_none: nothing was changed
389 * isl_change_drop_first: the first basic map was removed
390 * isl_change_drop_second: the second basic map was removed
391 * isl_change_fuse: the two basic maps were replaced by a new basic map.
394 isl_change_error
= -1,
396 isl_change_drop_first
,
397 isl_change_drop_second
,
401 /* Update "change" based on an interchange of the first and the second
402 * basic map. That is, interchange isl_change_drop_first and
403 * isl_change_drop_second.
405 static enum isl_change
invert_change(enum isl_change change
)
408 case isl_change_error
:
409 return isl_change_error
;
410 case isl_change_none
:
411 return isl_change_none
;
412 case isl_change_drop_first
:
413 return isl_change_drop_second
;
414 case isl_change_drop_second
:
415 return isl_change_drop_first
;
416 case isl_change_fuse
:
417 return isl_change_fuse
;
420 return isl_change_error
;
423 /* Add the valid constraints of the basic map represented by "info"
424 * to "bmap". "len" is the size of the constraints.
425 * If only one of the pair of inequalities that make up an equality
426 * is valid, then add that inequality.
428 static __isl_give isl_basic_map
*add_valid_constraints(
429 __isl_take isl_basic_map
*bmap
, struct isl_coalesce_info
*info
,
437 for (k
= 0; k
< info
->bmap
->n_eq
; ++k
) {
438 if (info
->eq
[2 * k
] == STATUS_VALID
&&
439 info
->eq
[2 * k
+ 1] == STATUS_VALID
) {
440 l
= isl_basic_map_alloc_equality(bmap
);
442 return isl_basic_map_free(bmap
);
443 isl_seq_cpy(bmap
->eq
[l
], info
->bmap
->eq
[k
], len
);
444 } else if (info
->eq
[2 * k
] == STATUS_VALID
) {
445 l
= isl_basic_map_alloc_inequality(bmap
);
447 return isl_basic_map_free(bmap
);
448 isl_seq_neg(bmap
->ineq
[l
], info
->bmap
->eq
[k
], len
);
449 } else if (info
->eq
[2 * k
+ 1] == STATUS_VALID
) {
450 l
= isl_basic_map_alloc_inequality(bmap
);
452 return isl_basic_map_free(bmap
);
453 isl_seq_cpy(bmap
->ineq
[l
], info
->bmap
->eq
[k
], len
);
457 for (k
= 0; k
< info
->bmap
->n_ineq
; ++k
) {
458 if (info
->ineq
[k
] != STATUS_VALID
)
460 l
= isl_basic_map_alloc_inequality(bmap
);
462 return isl_basic_map_free(bmap
);
463 isl_seq_cpy(bmap
->ineq
[l
], info
->bmap
->ineq
[k
], len
);
469 /* Is "bmap" defined by a number of (non-redundant) constraints that
470 * is greater than the number of constraints of basic maps i and j combined?
471 * Equalities are counted as two inequalities.
473 static int number_of_constraints_increases(int i
, int j
,
474 struct isl_coalesce_info
*info
,
475 __isl_keep isl_basic_map
*bmap
, struct isl_tab
*tab
)
479 n_old
= 2 * info
[i
].bmap
->n_eq
+ info
[i
].bmap
->n_ineq
;
480 n_old
+= 2 * info
[j
].bmap
->n_eq
+ info
[j
].bmap
->n_ineq
;
482 n_new
= 2 * bmap
->n_eq
;
483 for (k
= 0; k
< bmap
->n_ineq
; ++k
)
484 if (!isl_tab_is_redundant(tab
, bmap
->n_eq
+ k
))
487 return n_new
> n_old
;
490 /* Replace the pair of basic maps i and j by the basic map bounded
491 * by the valid constraints in both basic maps and the constraints
492 * in extra (if not NULL).
493 * Place the fused basic map in the position that is the smallest of i and j.
495 * If "detect_equalities" is set, then look for equalities encoded
496 * as pairs of inequalities.
497 * If "check_number" is set, then the original basic maps are only
498 * replaced if the total number of constraints does not increase.
499 * While the number of integer divisions in the two basic maps
500 * is assumed to be the same, the actual definitions may be different.
501 * We only copy the definition from one of the basic map if it is
502 * the same as that of the other basic map. Otherwise, we mark
503 * the integer division as unknown and simplify the basic map
504 * in an attempt to recover the integer division definition.
506 static enum isl_change
fuse(int i
, int j
, struct isl_coalesce_info
*info
,
507 __isl_keep isl_mat
*extra
, int detect_equalities
, int check_number
)
510 struct isl_basic_map
*fused
= NULL
;
511 struct isl_tab
*fused_tab
= NULL
;
512 isl_size total
= isl_basic_map_dim(info
[i
].bmap
, isl_dim_all
);
513 unsigned extra_rows
= extra
? extra
->n_row
: 0;
514 unsigned n_eq
, n_ineq
;
518 return isl_change_error
;
520 return fuse(j
, i
, info
, extra
, detect_equalities
, check_number
);
522 n_eq
= info
[i
].bmap
->n_eq
+ info
[j
].bmap
->n_eq
;
523 n_ineq
= info
[i
].bmap
->n_ineq
+ info
[j
].bmap
->n_ineq
;
524 fused
= isl_basic_map_alloc_space(isl_space_copy(info
[i
].bmap
->dim
),
525 info
[i
].bmap
->n_div
, n_eq
, n_eq
+ n_ineq
+ extra_rows
);
526 fused
= add_valid_constraints(fused
, &info
[i
], 1 + total
);
527 fused
= add_valid_constraints(fused
, &info
[j
], 1 + total
);
530 if (ISL_F_ISSET(info
[i
].bmap
, ISL_BASIC_MAP_RATIONAL
) &&
531 ISL_F_ISSET(info
[j
].bmap
, ISL_BASIC_MAP_RATIONAL
))
532 ISL_F_SET(fused
, ISL_BASIC_MAP_RATIONAL
);
534 for (k
= 0; k
< info
[i
].bmap
->n_div
; ++k
) {
535 int l
= isl_basic_map_alloc_div(fused
);
538 if (isl_seq_eq(info
[i
].bmap
->div
[k
], info
[j
].bmap
->div
[k
],
540 isl_seq_cpy(fused
->div
[l
], info
[i
].bmap
->div
[k
],
543 isl_int_set_si(fused
->div
[l
][0], 0);
548 for (k
= 0; k
< extra_rows
; ++k
) {
549 l
= isl_basic_map_alloc_inequality(fused
);
552 isl_seq_cpy(fused
->ineq
[l
], extra
->row
[k
], 1 + total
);
555 if (detect_equalities
)
556 fused
= isl_basic_map_detect_inequality_pairs(fused
, NULL
);
557 fused
= isl_basic_map_gauss(fused
, NULL
);
558 if (simplify
|| info
[j
].simplify
) {
559 fused
= isl_basic_map_simplify(fused
);
560 info
[i
].simplify
= 0;
562 fused
= isl_basic_map_finalize(fused
);
564 fused_tab
= isl_tab_from_basic_map(fused
, 0);
565 if (isl_tab_detect_redundant(fused_tab
) < 0)
569 number_of_constraints_increases(i
, j
, info
, fused
, fused_tab
)) {
570 isl_tab_free(fused_tab
);
571 isl_basic_map_free(fused
);
572 return isl_change_none
;
576 info
[i
].bmap
= fused
;
577 info
[i
].tab
= fused_tab
;
580 return isl_change_fuse
;
582 isl_tab_free(fused_tab
);
583 isl_basic_map_free(fused
);
584 return isl_change_error
;
587 /* Given a pair of basic maps i and j such that all constraints are either
588 * "valid" or "cut", check if the facets corresponding to the "cut"
589 * constraints of i lie entirely within basic map j.
590 * If so, replace the pair by the basic map consisting of the valid
591 * constraints in both basic maps.
592 * Checking whether the facet lies entirely within basic map j
593 * is performed by checking whether the constraints of basic map j
594 * are valid for the facet. These tests are performed on a rational
595 * tableau to avoid the theoretical possibility that a constraint
596 * that was considered to be a cut constraint for the entire basic map i
597 * happens to be considered to be a valid constraint for the facet,
598 * even though it cuts off the same rational points.
600 * To see that we are not introducing any extra points, call the
601 * two basic maps A and B and the resulting map U and let x
602 * be an element of U \setminus ( A \cup B ).
603 * A line connecting x with an element of A \cup B meets a facet F
604 * of either A or B. Assume it is a facet of B and let c_1 be
605 * the corresponding facet constraint. We have c_1(x) < 0 and
606 * so c_1 is a cut constraint. This implies that there is some
607 * (possibly rational) point x' satisfying the constraints of A
608 * and the opposite of c_1 as otherwise c_1 would have been marked
609 * valid for A. The line connecting x and x' meets a facet of A
610 * in a (possibly rational) point that also violates c_1, but this
611 * is impossible since all cut constraints of B are valid for all
613 * In case F is a facet of A rather than B, then we can apply the
614 * above reasoning to find a facet of B separating x from A \cup B first.
616 static enum isl_change
check_facets(int i
, int j
,
617 struct isl_coalesce_info
*info
)
620 struct isl_tab_undo
*snap
, *snap2
;
621 unsigned n_eq
= info
[i
].bmap
->n_eq
;
623 snap
= isl_tab_snap(info
[i
].tab
);
624 if (isl_tab_mark_rational(info
[i
].tab
) < 0)
625 return isl_change_error
;
626 snap2
= isl_tab_snap(info
[i
].tab
);
628 for (k
= 0; k
< info
[i
].bmap
->n_ineq
; ++k
) {
629 if (info
[i
].ineq
[k
] != STATUS_CUT
)
631 if (isl_tab_select_facet(info
[i
].tab
, n_eq
+ k
) < 0)
632 return isl_change_error
;
633 for (l
= 0; l
< info
[j
].bmap
->n_ineq
; ++l
) {
635 if (info
[j
].ineq
[l
] != STATUS_CUT
)
637 stat
= status_in(info
[j
].bmap
->ineq
[l
], info
[i
].tab
);
639 return isl_change_error
;
640 if (stat
!= STATUS_VALID
)
643 if (isl_tab_rollback(info
[i
].tab
, snap2
) < 0)
644 return isl_change_error
;
645 if (l
< info
[j
].bmap
->n_ineq
)
649 if (k
< info
[i
].bmap
->n_ineq
) {
650 if (isl_tab_rollback(info
[i
].tab
, snap
) < 0)
651 return isl_change_error
;
652 return isl_change_none
;
654 return fuse(i
, j
, info
, NULL
, 0, 0);
657 /* Check if info->bmap contains the basic map represented
658 * by the tableau "tab".
659 * For each equality, we check both the constraint itself
660 * (as an inequality) and its negation. Make sure the
661 * equality is returned to its original state before returning.
663 static isl_bool
contains(struct isl_coalesce_info
*info
, struct isl_tab
*tab
)
667 isl_basic_map
*bmap
= info
->bmap
;
669 dim
= isl_basic_map_dim(bmap
, isl_dim_all
);
671 return isl_bool_error
;
672 for (k
= 0; k
< bmap
->n_eq
; ++k
) {
674 isl_seq_neg(bmap
->eq
[k
], bmap
->eq
[k
], 1 + dim
);
675 stat
= status_in(bmap
->eq
[k
], tab
);
676 isl_seq_neg(bmap
->eq
[k
], bmap
->eq
[k
], 1 + dim
);
678 return isl_bool_error
;
679 if (stat
!= STATUS_VALID
)
680 return isl_bool_false
;
681 stat
= status_in(bmap
->eq
[k
], tab
);
683 return isl_bool_error
;
684 if (stat
!= STATUS_VALID
)
685 return isl_bool_false
;
688 for (k
= 0; k
< bmap
->n_ineq
; ++k
) {
690 if (info
->ineq
[k
] == STATUS_REDUNDANT
)
692 stat
= status_in(bmap
->ineq
[k
], tab
);
694 return isl_bool_error
;
695 if (stat
!= STATUS_VALID
)
696 return isl_bool_false
;
698 return isl_bool_true
;
701 /* Basic map "i" has an inequality (say "k") that is adjacent
702 * to some inequality of basic map "j". All the other inequalities
704 * Check if basic map "j" forms an extension of basic map "i".
706 * Note that this function is only called if some of the equalities or
707 * inequalities of basic map "j" do cut basic map "i". The function is
708 * correct even if there are no such cut constraints, but in that case
709 * the additional checks performed by this function are overkill.
711 * In particular, we replace constraint k, say f >= 0, by constraint
712 * f <= -1, add the inequalities of "j" that are valid for "i"
713 * and check if the result is a subset of basic map "j".
714 * To improve the chances of the subset relation being detected,
715 * any variable that only attains a single integer value
716 * in the tableau of "i" is first fixed to that value.
717 * If the result is a subset, then we know that this result is exactly equal
718 * to basic map "j" since all its constraints are valid for basic map "j".
719 * By combining the valid constraints of "i" (all equalities and all
720 * inequalities except "k") and the valid constraints of "j" we therefore
721 * obtain a basic map that is equal to their union.
722 * In this case, there is no need to perform a rollback of the tableau
723 * since it is going to be destroyed in fuse().
729 * |_______| _ |_________\
741 static enum isl_change
is_adj_ineq_extension(int i
, int j
,
742 struct isl_coalesce_info
*info
)
745 struct isl_tab_undo
*snap
;
746 unsigned n_eq
= info
[i
].bmap
->n_eq
;
747 isl_size total
= isl_basic_map_dim(info
[i
].bmap
, isl_dim_all
);
752 return isl_change_error
;
753 if (isl_tab_extend_cons(info
[i
].tab
, 1 + info
[j
].bmap
->n_ineq
) < 0)
754 return isl_change_error
;
756 k
= find_ineq(&info
[i
], STATUS_ADJ_INEQ
);
758 isl_die(isl_basic_map_get_ctx(info
[i
].bmap
), isl_error_internal
,
759 "info[i].ineq should have exactly one STATUS_ADJ_INEQ",
760 return isl_change_error
);
762 snap
= isl_tab_snap(info
[i
].tab
);
764 if (isl_tab_unrestrict(info
[i
].tab
, n_eq
+ k
) < 0)
765 return isl_change_error
;
767 isl_seq_neg(info
[i
].bmap
->ineq
[k
], info
[i
].bmap
->ineq
[k
], 1 + total
);
768 isl_int_sub_ui(info
[i
].bmap
->ineq
[k
][0], info
[i
].bmap
->ineq
[k
][0], 1);
769 r
= isl_tab_add_ineq(info
[i
].tab
, info
[i
].bmap
->ineq
[k
]);
770 isl_seq_neg(info
[i
].bmap
->ineq
[k
], info
[i
].bmap
->ineq
[k
], 1 + total
);
771 isl_int_sub_ui(info
[i
].bmap
->ineq
[k
][0], info
[i
].bmap
->ineq
[k
][0], 1);
773 return isl_change_error
;
775 for (k
= 0; k
< info
[j
].bmap
->n_ineq
; ++k
) {
776 if (info
[j
].ineq
[k
] != STATUS_VALID
)
778 if (isl_tab_add_ineq(info
[i
].tab
, info
[j
].bmap
->ineq
[k
]) < 0)
779 return isl_change_error
;
781 if (isl_tab_detect_constants(info
[i
].tab
) < 0)
782 return isl_change_error
;
784 super
= contains(&info
[j
], info
[i
].tab
);
786 return isl_change_error
;
788 return fuse(i
, j
, info
, NULL
, 0, 0);
790 if (isl_tab_rollback(info
[i
].tab
, snap
) < 0)
791 return isl_change_error
;
793 return isl_change_none
;
797 /* Both basic maps have at least one inequality with and adjacent
798 * (but opposite) inequality in the other basic map.
799 * Check that there are no cut constraints and that there is only
800 * a single pair of adjacent inequalities.
801 * If so, we can replace the pair by a single basic map described
802 * by all but the pair of adjacent inequalities.
803 * Any additional points introduced lie strictly between the two
804 * adjacent hyperplanes and can therefore be integral.
813 * The test for a single pair of adjancent inequalities is important
814 * for avoiding the combination of two basic maps like the following
824 * If there are some cut constraints on one side, then we may
825 * still be able to fuse the two basic maps, but we need to perform
826 * some additional checks in is_adj_ineq_extension.
828 static enum isl_change
check_adj_ineq(int i
, int j
,
829 struct isl_coalesce_info
*info
)
831 int count_i
, count_j
;
834 count_i
= count_ineq(&info
[i
], STATUS_ADJ_INEQ
);
835 count_j
= count_ineq(&info
[j
], STATUS_ADJ_INEQ
);
837 if (count_i
!= 1 && count_j
!= 1)
838 return isl_change_none
;
840 cut_i
= any_eq(&info
[i
], STATUS_CUT
) || any_ineq(&info
[i
], STATUS_CUT
);
841 cut_j
= any_eq(&info
[j
], STATUS_CUT
) || any_ineq(&info
[j
], STATUS_CUT
);
843 if (!cut_i
&& !cut_j
&& count_i
== 1 && count_j
== 1)
844 return fuse(i
, j
, info
, NULL
, 0, 0);
846 if (count_i
== 1 && !cut_i
)
847 return is_adj_ineq_extension(i
, j
, info
);
849 if (count_j
== 1 && !cut_j
)
850 return is_adj_ineq_extension(j
, i
, info
);
852 return isl_change_none
;
855 /* Given an affine transformation matrix "T", does row "row" represent
856 * anything other than a unit vector (possibly shifted by a constant)
857 * that is not involved in any of the other rows?
859 * That is, if a constraint involves the variable corresponding to
860 * the row, then could its preimage by "T" have any coefficients
861 * that are different from those in the original constraint?
863 static int not_unique_unit_row(__isl_keep isl_mat
*T
, int row
)
866 int len
= T
->n_col
- 1;
868 i
= isl_seq_first_non_zero(T
->row
[row
] + 1, len
);
871 if (!isl_int_is_one(T
->row
[row
][1 + i
]) &&
872 !isl_int_is_negone(T
->row
[row
][1 + i
]))
875 j
= isl_seq_first_non_zero(T
->row
[row
] + 1 + i
+ 1, len
- (i
+ 1));
879 for (j
= 1; j
< T
->n_row
; ++j
) {
882 if (!isl_int_is_zero(T
->row
[j
][1 + i
]))
889 /* Does inequality constraint "ineq" of "bmap" involve any of
890 * the variables marked in "affected"?
891 * "total" is the total number of variables, i.e., the number
892 * of entries in "affected".
894 static isl_bool
is_affected(__isl_keep isl_basic_map
*bmap
, int ineq
,
895 int *affected
, int total
)
899 for (i
= 0; i
< total
; ++i
) {
902 if (!isl_int_is_zero(bmap
->ineq
[ineq
][1 + i
]))
903 return isl_bool_true
;
906 return isl_bool_false
;
909 /* Given the compressed version of inequality constraint "ineq"
910 * of info->bmap in "v", check if the constraint can be tightened,
911 * where the compression is based on an equality constraint valid
913 * If so, add the tightened version of the inequality constraint
914 * to info->tab. "v" may be modified by this function.
916 * That is, if the compressed constraint is of the form
920 * with 0 < c < m, then it is equivalent to
924 * This means that c can also be subtracted from the original,
925 * uncompressed constraint without affecting the integer points
926 * in info->tab. Add this tightened constraint as an extra row
927 * to info->tab to make this information explicitly available.
929 static __isl_give isl_vec
*try_tightening(struct isl_coalesce_info
*info
,
930 int ineq
, __isl_take isl_vec
*v
)
938 ctx
= isl_vec_get_ctx(v
);
939 isl_seq_gcd(v
->el
+ 1, v
->size
- 1, &ctx
->normalize_gcd
);
940 if (isl_int_is_zero(ctx
->normalize_gcd
) ||
941 isl_int_is_one(ctx
->normalize_gcd
)) {
949 isl_int_fdiv_r(v
->el
[0], v
->el
[0], ctx
->normalize_gcd
);
950 if (isl_int_is_zero(v
->el
[0]))
953 if (isl_tab_extend_cons(info
->tab
, 1) < 0)
954 return isl_vec_free(v
);
956 isl_int_sub(info
->bmap
->ineq
[ineq
][0],
957 info
->bmap
->ineq
[ineq
][0], v
->el
[0]);
958 r
= isl_tab_add_ineq(info
->tab
, info
->bmap
->ineq
[ineq
]);
959 isl_int_add(info
->bmap
->ineq
[ineq
][0],
960 info
->bmap
->ineq
[ineq
][0], v
->el
[0]);
963 return isl_vec_free(v
);
968 /* Tighten the (non-redundant) constraints on the facet represented
970 * In particular, on input, info->tab represents the result
971 * of relaxing the "n" inequality constraints of info->bmap in "relaxed"
972 * by one, i.e., replacing f_i >= 0 by f_i + 1 >= 0, and then
973 * replacing the one at index "l" by the corresponding equality,
974 * i.e., f_k + 1 = 0, with k = relaxed[l].
976 * Compute a variable compression from the equality constraint f_k + 1 = 0
977 * and use it to tighten the other constraints of info->bmap
978 * (that is, all constraints that have not been relaxed),
979 * updating info->tab (and leaving info->bmap untouched).
980 * The compression handles essentially two cases, one where a variable
981 * is assigned a fixed value and can therefore be eliminated, and one
982 * where one variable is a shifted multiple of some other variable and
983 * can therefore be replaced by that multiple.
984 * Gaussian elimination would also work for the first case, but for
985 * the second case, the effectiveness would depend on the order
987 * After compression, some of the constraints may have coefficients
988 * with a common divisor. If this divisor does not divide the constant
989 * term, then the constraint can be tightened.
990 * The tightening is performed on the tableau info->tab by introducing
991 * extra (temporary) constraints.
993 * Only constraints that are possibly affected by the compression are
994 * considered. In particular, if the constraint only involves variables
995 * that are directly mapped to a distinct set of other variables, then
996 * no common divisor can be introduced and no tightening can occur.
998 * It is important to only consider the non-redundant constraints
999 * since the facet constraint has been relaxed prior to the call
1000 * to this function, meaning that the constraints that were redundant
1001 * prior to the relaxation may no longer be redundant.
1002 * These constraints will be ignored in the fused result, so
1003 * the fusion detection should not exploit them.
1005 static isl_stat
tighten_on_relaxed_facet(struct isl_coalesce_info
*info
,
1006 int n
, int *relaxed
, int l
)
1017 ctx
= isl_basic_map_get_ctx(info
->bmap
);
1018 total
= isl_basic_map_dim(info
->bmap
, isl_dim_all
);
1020 return isl_stat_error
;
1021 isl_int_add_ui(info
->bmap
->ineq
[k
][0], info
->bmap
->ineq
[k
][0], 1);
1022 T
= isl_mat_sub_alloc6(ctx
, info
->bmap
->ineq
, k
, 1, 0, 1 + total
);
1023 T
= isl_mat_variable_compression(T
, NULL
);
1024 isl_int_sub_ui(info
->bmap
->ineq
[k
][0], info
->bmap
->ineq
[k
][0], 1);
1026 return isl_stat_error
;
1027 if (T
->n_col
== 0) {
1032 affected
= isl_alloc_array(ctx
, int, total
);
1036 for (i
= 0; i
< total
; ++i
)
1037 affected
[i
] = not_unique_unit_row(T
, 1 + i
);
1039 for (i
= 0; i
< info
->bmap
->n_ineq
; ++i
) {
1041 if (any(relaxed
, n
, i
))
1043 if (info
->ineq
[i
] == STATUS_REDUNDANT
)
1045 handle
= is_affected(info
->bmap
, i
, affected
, total
);
1050 v
= isl_vec_alloc(ctx
, 1 + total
);
1053 isl_seq_cpy(v
->el
, info
->bmap
->ineq
[i
], 1 + total
);
1054 v
= isl_vec_mat_product(v
, isl_mat_copy(T
));
1055 v
= try_tightening(info
, i
, v
);
1067 return isl_stat_error
;
1070 /* Replace the basic maps "i" and "j" by an extension of "i"
1071 * along the "n" inequality constraints in "relax" by one.
1072 * The tableau info[i].tab has already been extended.
1073 * Extend info[i].bmap accordingly by relaxing all constraints in "relax"
1075 * Each integer division that does not have exactly the same
1076 * definition in "i" and "j" is marked unknown and the basic map
1077 * is scheduled to be simplified in an attempt to recover
1078 * the integer division definition.
1079 * Place the extension in the position that is the smallest of i and j.
1081 static enum isl_change
extend(int i
, int j
, int n
, int *relax
,
1082 struct isl_coalesce_info
*info
)
1087 info
[i
].bmap
= isl_basic_map_cow(info
[i
].bmap
);
1088 total
= isl_basic_map_dim(info
[i
].bmap
, isl_dim_all
);
1090 return isl_change_error
;
1091 for (l
= 0; l
< info
[i
].bmap
->n_div
; ++l
)
1092 if (!isl_seq_eq(info
[i
].bmap
->div
[l
],
1093 info
[j
].bmap
->div
[l
], 1 + 1 + total
)) {
1094 isl_int_set_si(info
[i
].bmap
->div
[l
][0], 0);
1095 info
[i
].simplify
= 1;
1097 for (l
= 0; l
< n
; ++l
)
1098 isl_int_add_ui(info
[i
].bmap
->ineq
[relax
[l
]][0],
1099 info
[i
].bmap
->ineq
[relax
[l
]][0], 1);
1100 ISL_F_SET(info
[i
].bmap
, ISL_BASIC_MAP_FINAL
);
1103 exchange(&info
[i
], &info
[j
]);
1104 return isl_change_fuse
;
1107 /* Basic map "i" has "n" inequality constraints (collected in "relax")
1108 * that are such that they include basic map "j" if they are relaxed
1109 * by one. All the other inequalities are valid for "j".
1110 * Check if basic map "j" forms an extension of basic map "i".
1112 * In particular, relax the constraints in "relax", compute the corresponding
1113 * facets one by one and check whether each of these is included
1114 * in the other basic map.
1115 * Before testing for inclusion, the constraints on each facet
1116 * are tightened to increase the chance of an inclusion being detected.
1117 * (Adding the valid constraints of "j" to the tableau of "i", as is done
1118 * in is_adj_ineq_extension, may further increase those chances, but this
1119 * is not currently done.)
1120 * If each facet is included, we know that relaxing the constraints extends
1121 * the basic map with exactly the other basic map (we already know that this
1122 * other basic map is included in the extension, because all other
1123 * inequality constraints are valid of "j") and we can replace the
1124 * two basic maps by this extension.
1126 * If any of the relaxed constraints turn out to be redundant, then bail out.
1127 * isl_tab_select_facet refuses to handle such constraints. It may be
1128 * possible to handle them anyway by making a distinction between
1129 * redundant constraints with a corresponding facet that still intersects
1130 * the set (allowing isl_tab_select_facet to handle them) and
1131 * those where the facet does not intersect the set (which can be ignored
1132 * because the empty facet is trivially included in the other disjunct).
1133 * However, relaxed constraints that turn out to be redundant should
1134 * be fairly rare and no such instance has been reported where
1135 * coalescing would be successful.
1151 static enum isl_change
is_relaxed_extension(int i
, int j
, int n
, int *relax
,
1152 struct isl_coalesce_info
*info
)
1156 struct isl_tab_undo
*snap
, *snap2
;
1157 unsigned n_eq
= info
[i
].bmap
->n_eq
;
1159 for (l
= 0; l
< n
; ++l
)
1160 if (isl_tab_is_equality(info
[i
].tab
, n_eq
+ relax
[l
]))
1161 return isl_change_none
;
1163 snap
= isl_tab_snap(info
[i
].tab
);
1164 for (l
= 0; l
< n
; ++l
)
1165 if (isl_tab_relax(info
[i
].tab
, n_eq
+ relax
[l
]) < 0)
1166 return isl_change_error
;
1167 for (l
= 0; l
< n
; ++l
) {
1168 if (!isl_tab_is_redundant(info
[i
].tab
, n_eq
+ relax
[l
]))
1170 if (isl_tab_rollback(info
[i
].tab
, snap
) < 0)
1171 return isl_change_error
;
1172 return isl_change_none
;
1174 snap2
= isl_tab_snap(info
[i
].tab
);
1175 for (l
= 0; l
< n
; ++l
) {
1176 if (isl_tab_rollback(info
[i
].tab
, snap2
) < 0)
1177 return isl_change_error
;
1178 if (isl_tab_select_facet(info
[i
].tab
, n_eq
+ relax
[l
]) < 0)
1179 return isl_change_error
;
1180 if (tighten_on_relaxed_facet(&info
[i
], n
, relax
, l
) < 0)
1181 return isl_change_error
;
1182 super
= contains(&info
[j
], info
[i
].tab
);
1184 return isl_change_error
;
1187 if (isl_tab_rollback(info
[i
].tab
, snap
) < 0)
1188 return isl_change_error
;
1189 return isl_change_none
;
1192 if (isl_tab_rollback(info
[i
].tab
, snap2
) < 0)
1193 return isl_change_error
;
1194 return extend(i
, j
, n
, relax
, info
);
1197 /* Data structure that keeps track of the wrapping constraints
1198 * and of information to bound the coefficients of those constraints.
1200 * bound is set if we want to apply a bound on the coefficients
1201 * mat contains the wrapping constraints
1202 * max is the bound on the coefficients (if bound is set)
1210 /* Update wraps->max to be greater than or equal to the coefficients
1211 * in the equalities and inequalities of info->bmap that can be removed
1212 * if we end up applying wrapping.
1214 static isl_stat
wraps_update_max(struct isl_wraps
*wraps
,
1215 struct isl_coalesce_info
*info
)
1219 isl_size total
= isl_basic_map_dim(info
->bmap
, isl_dim_all
);
1222 return isl_stat_error
;
1223 isl_int_init(max_k
);
1225 for (k
= 0; k
< info
->bmap
->n_eq
; ++k
) {
1226 if (info
->eq
[2 * k
] == STATUS_VALID
&&
1227 info
->eq
[2 * k
+ 1] == STATUS_VALID
)
1229 isl_seq_abs_max(info
->bmap
->eq
[k
] + 1, total
, &max_k
);
1230 if (isl_int_abs_gt(max_k
, wraps
->max
))
1231 isl_int_set(wraps
->max
, max_k
);
1234 for (k
= 0; k
< info
->bmap
->n_ineq
; ++k
) {
1235 if (info
->ineq
[k
] == STATUS_VALID
||
1236 info
->ineq
[k
] == STATUS_REDUNDANT
)
1238 isl_seq_abs_max(info
->bmap
->ineq
[k
] + 1, total
, &max_k
);
1239 if (isl_int_abs_gt(max_k
, wraps
->max
))
1240 isl_int_set(wraps
->max
, max_k
);
1243 isl_int_clear(max_k
);
1248 /* Initialize the isl_wraps data structure.
1249 * If we want to bound the coefficients of the wrapping constraints,
1250 * we set wraps->max to the largest coefficient
1251 * in the equalities and inequalities that can be removed if we end up
1252 * applying wrapping.
1254 static isl_stat
wraps_init(struct isl_wraps
*wraps
, __isl_take isl_mat
*mat
,
1255 struct isl_coalesce_info
*info
, int i
, int j
)
1262 return isl_stat_error
;
1263 ctx
= isl_mat_get_ctx(mat
);
1264 wraps
->bound
= isl_options_get_coalesce_bounded_wrapping(ctx
);
1267 isl_int_init(wraps
->max
);
1268 isl_int_set_si(wraps
->max
, 0);
1269 if (wraps_update_max(wraps
, &info
[i
]) < 0)
1270 return isl_stat_error
;
1271 if (wraps_update_max(wraps
, &info
[j
]) < 0)
1272 return isl_stat_error
;
1277 /* Free the contents of the isl_wraps data structure.
1279 static void wraps_free(struct isl_wraps
*wraps
)
1281 isl_mat_free(wraps
->mat
);
1283 isl_int_clear(wraps
->max
);
1286 /* Is the wrapping constraint in row "row" allowed?
1288 * If wraps->bound is set, we check that none of the coefficients
1289 * is greater than wraps->max.
1291 static int allow_wrap(struct isl_wraps
*wraps
, int row
)
1298 for (i
= 1; i
< wraps
->mat
->n_col
; ++i
)
1299 if (isl_int_abs_gt(wraps
->mat
->row
[row
][i
], wraps
->max
))
1305 /* Wrap "ineq" (or its opposite if "negate" is set) around "bound"
1306 * to include "set" and add the result in position "w" of "wraps".
1307 * "len" is the total number of coefficients in "bound" and "ineq".
1308 * Return 1 on success, 0 on failure and -1 on error.
1309 * Wrapping can fail if the result of wrapping is equal to "bound"
1310 * or if we want to bound the sizes of the coefficients and
1311 * the wrapped constraint does not satisfy this bound.
1313 static int add_wrap(struct isl_wraps
*wraps
, int w
, isl_int
*bound
,
1314 isl_int
*ineq
, unsigned len
, __isl_keep isl_set
*set
, int negate
)
1316 isl_seq_cpy(wraps
->mat
->row
[w
], bound
, len
);
1318 isl_seq_neg(wraps
->mat
->row
[w
+ 1], ineq
, len
);
1319 ineq
= wraps
->mat
->row
[w
+ 1];
1321 if (!isl_set_wrap_facet(set
, wraps
->mat
->row
[w
], ineq
))
1323 if (isl_seq_eq(wraps
->mat
->row
[w
], bound
, len
))
1325 if (!allow_wrap(wraps
, w
))
1330 /* For each constraint in info->bmap that is not redundant (as determined
1331 * by info->tab) and that is not a valid constraint for the other basic map,
1332 * wrap the constraint around "bound" such that it includes the whole
1333 * set "set" and append the resulting constraint to "wraps".
1334 * Note that the constraints that are valid for the other basic map
1335 * will be added to the combined basic map by default, so there is
1336 * no need to wrap them.
1337 * The caller wrap_in_facets even relies on this function not wrapping
1338 * any constraints that are already valid.
1339 * "wraps" is assumed to have been pre-allocated to the appropriate size.
1340 * wraps->n_row is the number of actual wrapped constraints that have
1342 * If any of the wrapping problems results in a constraint that is
1343 * identical to "bound", then this means that "set" is unbounded in such
1344 * way that no wrapping is possible. If this happens then wraps->n_row
1346 * Similarly, if we want to bound the coefficients of the wrapping
1347 * constraints and a newly added wrapping constraint does not
1348 * satisfy the bound, then wraps->n_row is also reset to zero.
1350 static isl_stat
add_wraps(struct isl_wraps
*wraps
,
1351 struct isl_coalesce_info
*info
, isl_int
*bound
, __isl_keep isl_set
*set
)
1356 isl_basic_map
*bmap
= info
->bmap
;
1357 isl_size total
= isl_basic_map_dim(bmap
, isl_dim_all
);
1358 unsigned len
= 1 + total
;
1361 return isl_stat_error
;
1363 w
= wraps
->mat
->n_row
;
1365 for (l
= 0; l
< bmap
->n_ineq
; ++l
) {
1366 if (info
->ineq
[l
] == STATUS_VALID
||
1367 info
->ineq
[l
] == STATUS_REDUNDANT
)
1369 if (isl_seq_is_neg(bound
, bmap
->ineq
[l
], len
))
1371 if (isl_seq_eq(bound
, bmap
->ineq
[l
], len
))
1373 if (isl_tab_is_redundant(info
->tab
, bmap
->n_eq
+ l
))
1376 added
= add_wrap(wraps
, w
, bound
, bmap
->ineq
[l
], len
, set
, 0);
1378 return isl_stat_error
;
1383 for (l
= 0; l
< bmap
->n_eq
; ++l
) {
1384 if (isl_seq_is_neg(bound
, bmap
->eq
[l
], len
))
1386 if (isl_seq_eq(bound
, bmap
->eq
[l
], len
))
1389 for (m
= 0; m
< 2; ++m
) {
1390 if (info
->eq
[2 * l
+ m
] == STATUS_VALID
)
1392 added
= add_wrap(wraps
, w
, bound
, bmap
->eq
[l
], len
,
1395 return isl_stat_error
;
1402 wraps
->mat
->n_row
= w
;
1405 wraps
->mat
->n_row
= 0;
1409 /* Check if the constraints in "wraps" from "first" until the last
1410 * are all valid for the basic set represented by "tab".
1411 * If not, wraps->n_row is set to zero.
1413 static int check_wraps(__isl_keep isl_mat
*wraps
, int first
,
1414 struct isl_tab
*tab
)
1418 for (i
= first
; i
< wraps
->n_row
; ++i
) {
1419 enum isl_ineq_type type
;
1420 type
= isl_tab_ineq_type(tab
, wraps
->row
[i
]);
1421 if (type
== isl_ineq_error
)
1423 if (type
== isl_ineq_redundant
)
1432 /* Return a set that corresponds to the non-redundant constraints
1433 * (as recorded in tab) of bmap.
1435 * It's important to remove the redundant constraints as some
1436 * of the other constraints may have been modified after the
1437 * constraints were marked redundant.
1438 * In particular, a constraint may have been relaxed.
1439 * Redundant constraints are ignored when a constraint is relaxed
1440 * and should therefore continue to be ignored ever after.
1441 * Otherwise, the relaxation might be thwarted by some of
1442 * these constraints.
1444 * Update the underlying set to ensure that the dimension doesn't change.
1445 * Otherwise the integer divisions could get dropped if the tab
1446 * turns out to be empty.
1448 static __isl_give isl_set
*set_from_updated_bmap(__isl_keep isl_basic_map
*bmap
,
1449 struct isl_tab
*tab
)
1451 isl_basic_set
*bset
;
1453 bmap
= isl_basic_map_copy(bmap
);
1454 bset
= isl_basic_map_underlying_set(bmap
);
1455 bset
= isl_basic_set_cow(bset
);
1456 bset
= isl_basic_set_update_from_tab(bset
, tab
);
1457 return isl_set_from_basic_set(bset
);
1460 /* Wrap the constraints of info->bmap that bound the facet defined
1461 * by inequality "k" around (the opposite of) this inequality to
1462 * include "set". "bound" may be used to store the negated inequality.
1463 * Since the wrapped constraints are not guaranteed to contain the whole
1464 * of info->bmap, we check them in check_wraps.
1465 * If any of the wrapped constraints turn out to be invalid, then
1466 * check_wraps will reset wrap->n_row to zero.
1468 static isl_stat
add_wraps_around_facet(struct isl_wraps
*wraps
,
1469 struct isl_coalesce_info
*info
, int k
, isl_int
*bound
,
1470 __isl_keep isl_set
*set
)
1472 struct isl_tab_undo
*snap
;
1474 isl_size total
= isl_basic_map_dim(info
->bmap
, isl_dim_all
);
1477 return isl_stat_error
;
1479 snap
= isl_tab_snap(info
->tab
);
1481 if (isl_tab_select_facet(info
->tab
, info
->bmap
->n_eq
+ k
) < 0)
1482 return isl_stat_error
;
1483 if (isl_tab_detect_redundant(info
->tab
) < 0)
1484 return isl_stat_error
;
1486 isl_seq_neg(bound
, info
->bmap
->ineq
[k
], 1 + total
);
1488 n
= wraps
->mat
->n_row
;
1489 if (add_wraps(wraps
, info
, bound
, set
) < 0)
1490 return isl_stat_error
;
1492 if (isl_tab_rollback(info
->tab
, snap
) < 0)
1493 return isl_stat_error
;
1494 if (check_wraps(wraps
->mat
, n
, info
->tab
) < 0)
1495 return isl_stat_error
;
1500 /* Given a basic set i with a constraint k that is adjacent to
1501 * basic set j, check if we can wrap
1502 * both the facet corresponding to k (if "wrap_facet" is set) and basic map j
1503 * (always) around their ridges to include the other set.
1504 * If so, replace the pair of basic sets by their union.
1506 * All constraints of i (except k) are assumed to be valid or
1507 * cut constraints for j.
1508 * Wrapping the cut constraints to include basic map j may result
1509 * in constraints that are no longer valid of basic map i
1510 * we have to check that the resulting wrapping constraints are valid for i.
1511 * If "wrap_facet" is not set, then all constraints of i (except k)
1512 * are assumed to be valid for j.
1521 static enum isl_change
can_wrap_in_facet(int i
, int j
, int k
,
1522 struct isl_coalesce_info
*info
, int wrap_facet
)
1524 enum isl_change change
= isl_change_none
;
1525 struct isl_wraps wraps
;
1528 struct isl_set
*set_i
= NULL
;
1529 struct isl_set
*set_j
= NULL
;
1530 struct isl_vec
*bound
= NULL
;
1531 isl_size total
= isl_basic_map_dim(info
[i
].bmap
, isl_dim_all
);
1534 return isl_change_error
;
1535 set_i
= set_from_updated_bmap(info
[i
].bmap
, info
[i
].tab
);
1536 set_j
= set_from_updated_bmap(info
[j
].bmap
, info
[j
].tab
);
1537 ctx
= isl_basic_map_get_ctx(info
[i
].bmap
);
1538 mat
= isl_mat_alloc(ctx
, 2 * (info
[i
].bmap
->n_eq
+ info
[j
].bmap
->n_eq
) +
1539 info
[i
].bmap
->n_ineq
+ info
[j
].bmap
->n_ineq
,
1541 if (wraps_init(&wraps
, mat
, info
, i
, j
) < 0)
1543 bound
= isl_vec_alloc(ctx
, 1 + total
);
1544 if (!set_i
|| !set_j
|| !bound
)
1547 isl_seq_cpy(bound
->el
, info
[i
].bmap
->ineq
[k
], 1 + total
);
1548 isl_int_add_ui(bound
->el
[0], bound
->el
[0], 1);
1549 isl_seq_normalize(ctx
, bound
->el
, 1 + total
);
1551 isl_seq_cpy(wraps
.mat
->row
[0], bound
->el
, 1 + total
);
1552 wraps
.mat
->n_row
= 1;
1554 if (add_wraps(&wraps
, &info
[j
], bound
->el
, set_i
) < 0)
1556 if (!wraps
.mat
->n_row
)
1560 if (add_wraps_around_facet(&wraps
, &info
[i
], k
,
1561 bound
->el
, set_j
) < 0)
1563 if (!wraps
.mat
->n_row
)
1567 change
= fuse(i
, j
, info
, wraps
.mat
, 0, 0);
1572 isl_set_free(set_i
);
1573 isl_set_free(set_j
);
1575 isl_vec_free(bound
);
1580 isl_vec_free(bound
);
1581 isl_set_free(set_i
);
1582 isl_set_free(set_j
);
1583 return isl_change_error
;
1586 /* Given a cut constraint t(x) >= 0 of basic map i, stored in row "w"
1587 * of wrap.mat, replace it by its relaxed version t(x) + 1 >= 0, and
1588 * add wrapping constraints to wrap.mat for all constraints
1589 * of basic map j that bound the part of basic map j that sticks out
1590 * of the cut constraint.
1591 * "set_i" is the underlying set of basic map i.
1592 * If any wrapping fails, then wraps->mat.n_row is reset to zero.
1594 * In particular, we first intersect basic map j with t(x) + 1 = 0.
1595 * If the result is empty, then t(x) >= 0 was actually a valid constraint
1596 * (with respect to the integer points), so we add t(x) >= 0 instead.
1597 * Otherwise, we wrap the constraints of basic map j that are not
1598 * redundant in this intersection and that are not already valid
1599 * for basic map i over basic map i.
1600 * Note that it is sufficient to wrap the constraints to include
1601 * basic map i, because we will only wrap the constraints that do
1602 * not include basic map i already. The wrapped constraint will
1603 * therefore be more relaxed compared to the original constraint.
1604 * Since the original constraint is valid for basic map j, so is
1605 * the wrapped constraint.
1607 static isl_stat
wrap_in_facet(struct isl_wraps
*wraps
, int w
,
1608 struct isl_coalesce_info
*info_j
, __isl_keep isl_set
*set_i
,
1609 struct isl_tab_undo
*snap
)
1611 isl_int_add_ui(wraps
->mat
->row
[w
][0], wraps
->mat
->row
[w
][0], 1);
1612 if (isl_tab_add_eq(info_j
->tab
, wraps
->mat
->row
[w
]) < 0)
1613 return isl_stat_error
;
1614 if (isl_tab_detect_redundant(info_j
->tab
) < 0)
1615 return isl_stat_error
;
1617 if (info_j
->tab
->empty
)
1618 isl_int_sub_ui(wraps
->mat
->row
[w
][0], wraps
->mat
->row
[w
][0], 1);
1619 else if (add_wraps(wraps
, info_j
, wraps
->mat
->row
[w
], set_i
) < 0)
1620 return isl_stat_error
;
1622 if (isl_tab_rollback(info_j
->tab
, snap
) < 0)
1623 return isl_stat_error
;
1628 /* Given a pair of basic maps i and j such that j sticks out
1629 * of i at n cut constraints, each time by at most one,
1630 * try to compute wrapping constraints and replace the two
1631 * basic maps by a single basic map.
1632 * The other constraints of i are assumed to be valid for j.
1633 * "set_i" is the underlying set of basic map i.
1634 * "wraps" has been initialized to be of the right size.
1636 * For each cut constraint t(x) >= 0 of i, we add the relaxed version
1637 * t(x) + 1 >= 0, along with wrapping constraints for all constraints
1638 * of basic map j that bound the part of basic map j that sticks out
1639 * of the cut constraint.
1641 * If any wrapping fails, i.e., if we cannot wrap to touch
1642 * the union, then we give up.
1643 * Otherwise, the pair of basic maps is replaced by their union.
1645 static enum isl_change
try_wrap_in_facets(int i
, int j
,
1646 struct isl_coalesce_info
*info
, struct isl_wraps
*wraps
,
1647 __isl_keep isl_set
*set_i
)
1651 struct isl_tab_undo
*snap
;
1653 total
= isl_basic_map_dim(info
[i
].bmap
, isl_dim_all
);
1655 return isl_change_error
;
1657 snap
= isl_tab_snap(info
[j
].tab
);
1659 wraps
->mat
->n_row
= 0;
1661 for (k
= 0; k
< info
[i
].bmap
->n_eq
; ++k
) {
1662 for (l
= 0; l
< 2; ++l
) {
1663 if (info
[i
].eq
[2 * k
+ l
] != STATUS_CUT
)
1665 w
= wraps
->mat
->n_row
++;
1667 isl_seq_neg(wraps
->mat
->row
[w
],
1668 info
[i
].bmap
->eq
[k
], 1 + total
);
1670 isl_seq_cpy(wraps
->mat
->row
[w
],
1671 info
[i
].bmap
->eq
[k
], 1 + total
);
1672 if (wrap_in_facet(wraps
, w
, &info
[j
], set_i
, snap
) < 0)
1673 return isl_change_error
;
1675 if (!wraps
->mat
->n_row
)
1676 return isl_change_none
;
1680 for (k
= 0; k
< info
[i
].bmap
->n_ineq
; ++k
) {
1681 if (info
[i
].ineq
[k
] != STATUS_CUT
)
1683 w
= wraps
->mat
->n_row
++;
1684 isl_seq_cpy(wraps
->mat
->row
[w
],
1685 info
[i
].bmap
->ineq
[k
], 1 + total
);
1686 if (wrap_in_facet(wraps
, w
, &info
[j
], set_i
, snap
) < 0)
1687 return isl_change_error
;
1689 if (!wraps
->mat
->n_row
)
1690 return isl_change_none
;
1693 return fuse(i
, j
, info
, wraps
->mat
, 0, 1);
1696 /* Given a pair of basic maps i and j such that j sticks out
1697 * of i at n cut constraints, each time by at most one,
1698 * try to compute wrapping constraints and replace the two
1699 * basic maps by a single basic map.
1700 * The other constraints of i are assumed to be valid for j.
1702 * The core computation is performed by try_wrap_in_facets.
1703 * This function simply extracts an underlying set representation
1704 * of basic map i and initializes the data structure for keeping
1705 * track of wrapping constraints.
1707 static enum isl_change
wrap_in_facets(int i
, int j
, int n
,
1708 struct isl_coalesce_info
*info
)
1710 enum isl_change change
= isl_change_none
;
1711 struct isl_wraps wraps
;
1714 isl_set
*set_i
= NULL
;
1715 isl_size total
= isl_basic_map_dim(info
[i
].bmap
, isl_dim_all
);
1719 return isl_change_error
;
1720 if (isl_tab_extend_cons(info
[j
].tab
, 1) < 0)
1721 return isl_change_error
;
1723 max_wrap
= 1 + 2 * info
[j
].bmap
->n_eq
+ info
[j
].bmap
->n_ineq
;
1726 set_i
= set_from_updated_bmap(info
[i
].bmap
, info
[i
].tab
);
1727 ctx
= isl_basic_map_get_ctx(info
[i
].bmap
);
1728 mat
= isl_mat_alloc(ctx
, max_wrap
, 1 + total
);
1729 if (wraps_init(&wraps
, mat
, info
, i
, j
) < 0)
1734 change
= try_wrap_in_facets(i
, j
, info
, &wraps
, set_i
);
1737 isl_set_free(set_i
);
1742 isl_set_free(set_i
);
1743 return isl_change_error
;
1746 /* Return the effect of inequality "ineq" on the tableau "tab",
1747 * after relaxing the constant term of "ineq" by one.
1749 static enum isl_ineq_type
type_of_relaxed(struct isl_tab
*tab
, isl_int
*ineq
)
1751 enum isl_ineq_type type
;
1753 isl_int_add_ui(ineq
[0], ineq
[0], 1);
1754 type
= isl_tab_ineq_type(tab
, ineq
);
1755 isl_int_sub_ui(ineq
[0], ineq
[0], 1);
1760 /* Given two basic sets i and j,
1761 * check if relaxing all the cut constraints of i by one turns
1762 * them into valid constraint for j and check if we can wrap in
1763 * the bits that are sticking out.
1764 * If so, replace the pair by their union.
1766 * We first check if all relaxed cut inequalities of i are valid for j
1767 * and then try to wrap in the intersections of the relaxed cut inequalities
1770 * During this wrapping, we consider the points of j that lie at a distance
1771 * of exactly 1 from i. In particular, we ignore the points that lie in
1772 * between this lower-dimensional space and the basic map i.
1773 * We can therefore only apply this to integer maps.
1799 * Wrapping can fail if the result of wrapping one of the facets
1800 * around its edges does not produce any new facet constraint.
1801 * In particular, this happens when we try to wrap in unbounded sets.
1803 * _______________________________________________________________________
1807 * |_| |_________________________________________________________________
1810 * The following is not an acceptable result of coalescing the above two
1811 * sets as it includes extra integer points.
1812 * _______________________________________________________________________
1817 * \______________________________________________________________________
1819 static enum isl_change
can_wrap_in_set(int i
, int j
,
1820 struct isl_coalesce_info
*info
)
1826 if (ISL_F_ISSET(info
[i
].bmap
, ISL_BASIC_MAP_RATIONAL
) ||
1827 ISL_F_ISSET(info
[j
].bmap
, ISL_BASIC_MAP_RATIONAL
))
1828 return isl_change_none
;
1830 n
= count_eq(&info
[i
], STATUS_CUT
) + count_ineq(&info
[i
], STATUS_CUT
);
1832 return isl_change_none
;
1834 total
= isl_basic_map_dim(info
[i
].bmap
, isl_dim_all
);
1836 return isl_change_error
;
1837 for (k
= 0; k
< info
[i
].bmap
->n_eq
; ++k
) {
1838 for (l
= 0; l
< 2; ++l
) {
1839 enum isl_ineq_type type
;
1841 if (info
[i
].eq
[2 * k
+ l
] != STATUS_CUT
)
1845 isl_seq_neg(info
[i
].bmap
->eq
[k
],
1846 info
[i
].bmap
->eq
[k
], 1 + total
);
1847 type
= type_of_relaxed(info
[j
].tab
,
1848 info
[i
].bmap
->eq
[k
]);
1850 isl_seq_neg(info
[i
].bmap
->eq
[k
],
1851 info
[i
].bmap
->eq
[k
], 1 + total
);
1852 if (type
== isl_ineq_error
)
1853 return isl_change_error
;
1854 if (type
!= isl_ineq_redundant
)
1855 return isl_change_none
;
1859 for (k
= 0; k
< info
[i
].bmap
->n_ineq
; ++k
) {
1860 enum isl_ineq_type type
;
1862 if (info
[i
].ineq
[k
] != STATUS_CUT
)
1865 type
= type_of_relaxed(info
[j
].tab
, info
[i
].bmap
->ineq
[k
]);
1866 if (type
== isl_ineq_error
)
1867 return isl_change_error
;
1868 if (type
!= isl_ineq_redundant
)
1869 return isl_change_none
;
1872 return wrap_in_facets(i
, j
, n
, info
);
1875 /* Check if either i or j has only cut constraints that can
1876 * be used to wrap in (a facet of) the other basic set.
1877 * if so, replace the pair by their union.
1879 static enum isl_change
check_wrap(int i
, int j
, struct isl_coalesce_info
*info
)
1881 enum isl_change change
= isl_change_none
;
1883 change
= can_wrap_in_set(i
, j
, info
);
1884 if (change
!= isl_change_none
)
1887 change
= can_wrap_in_set(j
, i
, info
);
1891 /* Check if all inequality constraints of "i" that cut "j" cease
1892 * to be cut constraints if they are relaxed by one.
1893 * If so, collect the cut constraints in "list".
1894 * The caller is responsible for allocating "list".
1896 static isl_bool
all_cut_by_one(int i
, int j
, struct isl_coalesce_info
*info
,
1902 for (l
= 0; l
< info
[i
].bmap
->n_ineq
; ++l
) {
1903 enum isl_ineq_type type
;
1905 if (info
[i
].ineq
[l
] != STATUS_CUT
)
1907 type
= type_of_relaxed(info
[j
].tab
, info
[i
].bmap
->ineq
[l
]);
1908 if (type
== isl_ineq_error
)
1909 return isl_bool_error
;
1910 if (type
!= isl_ineq_redundant
)
1911 return isl_bool_false
;
1915 return isl_bool_true
;
1918 /* Given two basic maps such that "j" has at least one equality constraint
1919 * that is adjacent to an inequality constraint of "i" and such that "i" has
1920 * exactly one inequality constraint that is adjacent to an equality
1921 * constraint of "j", check whether "i" can be extended to include "j" or
1922 * whether "j" can be wrapped into "i".
1923 * All remaining constraints of "i" and "j" are assumed to be valid
1924 * or cut constraints of the other basic map.
1925 * However, none of the equality constraints of "i" are cut constraints.
1927 * If "i" has any "cut" inequality constraints, then check if relaxing
1928 * each of them by one is sufficient for them to become valid.
1929 * If so, check if the inequality constraint adjacent to an equality
1930 * constraint of "j" along with all these cut constraints
1931 * can be relaxed by one to contain exactly "j".
1932 * Otherwise, or if this fails, check if "j" can be wrapped into "i".
1934 static enum isl_change
check_single_adj_eq(int i
, int j
,
1935 struct isl_coalesce_info
*info
)
1937 enum isl_change change
= isl_change_none
;
1944 n_cut
= count_ineq(&info
[i
], STATUS_CUT
);
1946 k
= find_ineq(&info
[i
], STATUS_ADJ_EQ
);
1949 ctx
= isl_basic_map_get_ctx(info
[i
].bmap
);
1950 relax
= isl_calloc_array(ctx
, int, 1 + n_cut
);
1952 return isl_change_error
;
1954 try_relax
= all_cut_by_one(i
, j
, info
, relax
+ 1);
1956 change
= isl_change_error
;
1958 try_relax
= isl_bool_true
;
1961 if (try_relax
&& change
== isl_change_none
)
1962 change
= is_relaxed_extension(i
, j
, 1 + n_cut
, relax
, info
);
1965 if (change
!= isl_change_none
)
1968 change
= can_wrap_in_facet(i
, j
, k
, info
, n_cut
> 0);
1973 /* At least one of the basic maps has an equality that is adjacent
1974 * to an inequality. Make sure that only one of the basic maps has
1975 * such an equality and that the other basic map has exactly one
1976 * inequality adjacent to an equality.
1977 * If the other basic map does not have such an inequality, then
1978 * check if all its constraints are either valid or cut constraints
1979 * and, if so, try wrapping in the first map into the second.
1980 * Otherwise, try to extend one basic map with the other or
1981 * wrap one basic map in the other.
1983 static enum isl_change
check_adj_eq(int i
, int j
,
1984 struct isl_coalesce_info
*info
)
1986 if (any_eq(&info
[i
], STATUS_ADJ_INEQ
) &&
1987 any_eq(&info
[j
], STATUS_ADJ_INEQ
))
1988 /* ADJ EQ TOO MANY */
1989 return isl_change_none
;
1991 if (any_eq(&info
[i
], STATUS_ADJ_INEQ
))
1992 return check_adj_eq(j
, i
, info
);
1994 /* j has an equality adjacent to an inequality in i */
1996 if (count_ineq(&info
[i
], STATUS_ADJ_EQ
) != 1) {
1997 if (all_valid_or_cut(&info
[i
]))
1998 return can_wrap_in_set(i
, j
, info
);
1999 return isl_change_none
;
2001 if (any_eq(&info
[i
], STATUS_CUT
))
2002 return isl_change_none
;
2003 if (any_ineq(&info
[j
], STATUS_ADJ_EQ
) ||
2004 any_ineq(&info
[i
], STATUS_ADJ_INEQ
) ||
2005 any_ineq(&info
[j
], STATUS_ADJ_INEQ
))
2006 /* ADJ EQ TOO MANY */
2007 return isl_change_none
;
2009 return check_single_adj_eq(i
, j
, info
);
2012 /* Disjunct "j" lies on a hyperplane that is adjacent to disjunct "i".
2013 * In particular, disjunct "i" has an inequality constraint that is adjacent
2014 * to a (combination of) equality constraint(s) of disjunct "j",
2015 * but disjunct "j" has no explicit equality constraint adjacent
2016 * to an inequality constraint of disjunct "i".
2018 * Disjunct "i" is already known not to have any equality constraints
2019 * that are adjacent to an equality or inequality constraint.
2020 * Check that, other than the inequality constraint mentioned above,
2021 * all other constraints of disjunct "i" are valid for disjunct "j".
2022 * If so, try and wrap in disjunct "j".
2024 static enum isl_change
check_ineq_adj_eq(int i
, int j
,
2025 struct isl_coalesce_info
*info
)
2029 if (any_eq(&info
[i
], STATUS_CUT
))
2030 return isl_change_none
;
2031 if (any_ineq(&info
[i
], STATUS_CUT
))
2032 return isl_change_none
;
2033 if (any_ineq(&info
[i
], STATUS_ADJ_INEQ
))
2034 return isl_change_none
;
2035 if (count_ineq(&info
[i
], STATUS_ADJ_EQ
) != 1)
2036 return isl_change_none
;
2038 k
= find_ineq(&info
[i
], STATUS_ADJ_EQ
);
2040 return can_wrap_in_facet(i
, j
, k
, info
, 0);
2043 /* The two basic maps lie on adjacent hyperplanes. In particular,
2044 * basic map "i" has an equality that lies parallel to basic map "j".
2045 * Check if we can wrap the facets around the parallel hyperplanes
2046 * to include the other set.
2048 * We perform basically the same operations as can_wrap_in_facet,
2049 * except that we don't need to select a facet of one of the sets.
2055 * If there is more than one equality of "i" adjacent to an equality of "j",
2056 * then the result will satisfy one or more equalities that are a linear
2057 * combination of these equalities. These will be encoded as pairs
2058 * of inequalities in the wrapping constraints and need to be made
2061 static enum isl_change
check_eq_adj_eq(int i
, int j
,
2062 struct isl_coalesce_info
*info
)
2065 enum isl_change change
= isl_change_none
;
2066 int detect_equalities
= 0;
2067 struct isl_wraps wraps
;
2070 struct isl_set
*set_i
= NULL
;
2071 struct isl_set
*set_j
= NULL
;
2072 struct isl_vec
*bound
= NULL
;
2073 isl_size total
= isl_basic_map_dim(info
[i
].bmap
, isl_dim_all
);
2076 return isl_change_error
;
2077 if (count_eq(&info
[i
], STATUS_ADJ_EQ
) != 1)
2078 detect_equalities
= 1;
2080 k
= find_eq(&info
[i
], STATUS_ADJ_EQ
);
2082 set_i
= set_from_updated_bmap(info
[i
].bmap
, info
[i
].tab
);
2083 set_j
= set_from_updated_bmap(info
[j
].bmap
, info
[j
].tab
);
2084 ctx
= isl_basic_map_get_ctx(info
[i
].bmap
);
2085 mat
= isl_mat_alloc(ctx
, 2 * (info
[i
].bmap
->n_eq
+ info
[j
].bmap
->n_eq
) +
2086 info
[i
].bmap
->n_ineq
+ info
[j
].bmap
->n_ineq
,
2088 if (wraps_init(&wraps
, mat
, info
, i
, j
) < 0)
2090 bound
= isl_vec_alloc(ctx
, 1 + total
);
2091 if (!set_i
|| !set_j
|| !bound
)
2095 isl_seq_neg(bound
->el
, info
[i
].bmap
->eq
[k
/ 2], 1 + total
);
2097 isl_seq_cpy(bound
->el
, info
[i
].bmap
->eq
[k
/ 2], 1 + total
);
2098 isl_int_add_ui(bound
->el
[0], bound
->el
[0], 1);
2100 isl_seq_cpy(wraps
.mat
->row
[0], bound
->el
, 1 + total
);
2101 wraps
.mat
->n_row
= 1;
2103 if (add_wraps(&wraps
, &info
[j
], bound
->el
, set_i
) < 0)
2105 if (!wraps
.mat
->n_row
)
2108 isl_int_sub_ui(bound
->el
[0], bound
->el
[0], 1);
2109 isl_seq_neg(bound
->el
, bound
->el
, 1 + total
);
2111 isl_seq_cpy(wraps
.mat
->row
[wraps
.mat
->n_row
], bound
->el
, 1 + total
);
2114 if (add_wraps(&wraps
, &info
[i
], bound
->el
, set_j
) < 0)
2116 if (!wraps
.mat
->n_row
)
2119 change
= fuse(i
, j
, info
, wraps
.mat
, detect_equalities
, 0);
2122 error
: change
= isl_change_error
;
2127 isl_set_free(set_i
);
2128 isl_set_free(set_j
);
2129 isl_vec_free(bound
);
2134 /* Initialize the "eq" and "ineq" fields of "info".
2136 static void init_status(struct isl_coalesce_info
*info
)
2138 info
->eq
= info
->ineq
= NULL
;
2141 /* Set info->eq to the positions of the equalities of info->bmap
2142 * with respect to the basic map represented by "tab".
2143 * If info->eq has already been computed, then do not compute it again.
2145 static void set_eq_status_in(struct isl_coalesce_info
*info
,
2146 struct isl_tab
*tab
)
2150 info
->eq
= eq_status_in(info
->bmap
, tab
);
2153 /* Set info->ineq to the positions of the inequalities of info->bmap
2154 * with respect to the basic map represented by "tab".
2155 * If info->ineq has already been computed, then do not compute it again.
2157 static void set_ineq_status_in(struct isl_coalesce_info
*info
,
2158 struct isl_tab
*tab
)
2162 info
->ineq
= ineq_status_in(info
->bmap
, info
->tab
, tab
);
2165 /* Free the memory allocated by the "eq" and "ineq" fields of "info".
2166 * This function assumes that init_status has been called on "info" first,
2167 * after which the "eq" and "ineq" fields may or may not have been
2168 * assigned a newly allocated array.
2170 static void clear_status(struct isl_coalesce_info
*info
)
2176 /* Are all inequality constraints of the basic map represented by "info"
2177 * valid for the other basic map, except for a single constraint
2178 * that is adjacent to an inequality constraint of the other basic map?
2180 static int all_ineq_valid_or_single_adj_ineq(struct isl_coalesce_info
*info
)
2185 for (i
= 0; i
< info
->bmap
->n_ineq
; ++i
) {
2186 if (info
->ineq
[i
] == STATUS_REDUNDANT
)
2188 if (info
->ineq
[i
] == STATUS_VALID
)
2190 if (info
->ineq
[i
] != STATUS_ADJ_INEQ
)
2200 /* Basic map "i" has one or more equality constraints that separate it
2201 * from basic map "j". Check if it happens to be an extension
2203 * In particular, check that all constraints of "j" are valid for "i",
2204 * except for one inequality constraint that is adjacent
2205 * to an inequality constraints of "i".
2206 * If so, check for "i" being an extension of "j" by calling
2207 * is_adj_ineq_extension.
2209 * Clean up the memory allocated for keeping track of the status
2210 * of the constraints before returning.
2212 static enum isl_change
separating_equality(int i
, int j
,
2213 struct isl_coalesce_info
*info
)
2215 enum isl_change change
= isl_change_none
;
2217 if (all(info
[j
].eq
, 2 * info
[j
].bmap
->n_eq
, STATUS_VALID
) &&
2218 all_ineq_valid_or_single_adj_ineq(&info
[j
]))
2219 change
= is_adj_ineq_extension(j
, i
, info
);
2221 clear_status(&info
[i
]);
2222 clear_status(&info
[j
]);
2226 /* Check if the union of the given pair of basic maps
2227 * can be represented by a single basic map.
2228 * If so, replace the pair by the single basic map and return
2229 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
2230 * Otherwise, return isl_change_none.
2231 * The two basic maps are assumed to live in the same local space.
2232 * The "eq" and "ineq" fields of info[i] and info[j] are assumed
2233 * to have been initialized by the caller, either to NULL or
2234 * to valid information.
2236 * We first check the effect of each constraint of one basic map
2237 * on the other basic map.
2238 * The constraint may be
2239 * redundant the constraint is redundant in its own
2240 * basic map and should be ignore and removed
2242 * valid all (integer) points of the other basic map
2243 * satisfy the constraint
2244 * separate no (integer) point of the other basic map
2245 * satisfies the constraint
2246 * cut some but not all points of the other basic map
2247 * satisfy the constraint
2248 * adj_eq the given constraint is adjacent (on the outside)
2249 * to an equality of the other basic map
2250 * adj_ineq the given constraint is adjacent (on the outside)
2251 * to an inequality of the other basic map
2253 * We consider seven cases in which we can replace the pair by a single
2254 * basic map. We ignore all "redundant" constraints.
2256 * 1. all constraints of one basic map are valid
2257 * => the other basic map is a subset and can be removed
2259 * 2. all constraints of both basic maps are either "valid" or "cut"
2260 * and the facets corresponding to the "cut" constraints
2261 * of one of the basic maps lies entirely inside the other basic map
2262 * => the pair can be replaced by a basic map consisting
2263 * of the valid constraints in both basic maps
2265 * 3. there is a single pair of adjacent inequalities
2266 * (all other constraints are "valid")
2267 * => the pair can be replaced by a basic map consisting
2268 * of the valid constraints in both basic maps
2270 * 4. one basic map has a single adjacent inequality, while the other
2271 * constraints are "valid". The other basic map has some
2272 * "cut" constraints, but replacing the adjacent inequality by
2273 * its opposite and adding the valid constraints of the other
2274 * basic map results in a subset of the other basic map
2275 * => the pair can be replaced by a basic map consisting
2276 * of the valid constraints in both basic maps
2278 * 5. there is a single adjacent pair of an inequality and an equality,
2279 * the other constraints of the basic map containing the inequality are
2280 * "valid". Moreover, if the inequality the basic map is relaxed
2281 * and then turned into an equality, then resulting facet lies
2282 * entirely inside the other basic map
2283 * => the pair can be replaced by the basic map containing
2284 * the inequality, with the inequality relaxed.
2286 * 6. there is a single inequality adjacent to an equality,
2287 * the other constraints of the basic map containing the inequality are
2288 * "valid". Moreover, the facets corresponding to both
2289 * the inequality and the equality can be wrapped around their
2290 * ridges to include the other basic map
2291 * => the pair can be replaced by a basic map consisting
2292 * of the valid constraints in both basic maps together
2293 * with all wrapping constraints
2295 * 7. one of the basic maps extends beyond the other by at most one.
2296 * Moreover, the facets corresponding to the cut constraints and
2297 * the pieces of the other basic map at offset one from these cut
2298 * constraints can be wrapped around their ridges to include
2299 * the union of the two basic maps
2300 * => the pair can be replaced by a basic map consisting
2301 * of the valid constraints in both basic maps together
2302 * with all wrapping constraints
2304 * 8. the two basic maps live in adjacent hyperplanes. In principle
2305 * such sets can always be combined through wrapping, but we impose
2306 * that there is only one such pair, to avoid overeager coalescing.
2308 * Throughout the computation, we maintain a collection of tableaus
2309 * corresponding to the basic maps. When the basic maps are dropped
2310 * or combined, the tableaus are modified accordingly.
2312 static enum isl_change
coalesce_local_pair_reuse(int i
, int j
,
2313 struct isl_coalesce_info
*info
)
2315 enum isl_change change
= isl_change_none
;
2317 set_ineq_status_in(&info
[i
], info
[j
].tab
);
2318 if (info
[i
].bmap
->n_ineq
&& !info
[i
].ineq
)
2320 if (any_ineq(&info
[i
], STATUS_ERROR
))
2322 if (any_ineq(&info
[i
], STATUS_SEPARATE
))
2325 set_ineq_status_in(&info
[j
], info
[i
].tab
);
2326 if (info
[j
].bmap
->n_ineq
&& !info
[j
].ineq
)
2328 if (any_ineq(&info
[j
], STATUS_ERROR
))
2330 if (any_ineq(&info
[j
], STATUS_SEPARATE
))
2333 set_eq_status_in(&info
[i
], info
[j
].tab
);
2334 if (info
[i
].bmap
->n_eq
&& !info
[i
].eq
)
2336 if (any_eq(&info
[i
], STATUS_ERROR
))
2339 set_eq_status_in(&info
[j
], info
[i
].tab
);
2340 if (info
[j
].bmap
->n_eq
&& !info
[j
].eq
)
2342 if (any_eq(&info
[j
], STATUS_ERROR
))
2345 if (any_eq(&info
[i
], STATUS_SEPARATE
))
2346 return separating_equality(i
, j
, info
);
2347 if (any_eq(&info
[j
], STATUS_SEPARATE
))
2348 return separating_equality(j
, i
, info
);
2350 if (all(info
[i
].eq
, 2 * info
[i
].bmap
->n_eq
, STATUS_VALID
) &&
2351 all(info
[i
].ineq
, info
[i
].bmap
->n_ineq
, STATUS_VALID
)) {
2353 change
= isl_change_drop_second
;
2354 } else if (all(info
[j
].eq
, 2 * info
[j
].bmap
->n_eq
, STATUS_VALID
) &&
2355 all(info
[j
].ineq
, info
[j
].bmap
->n_ineq
, STATUS_VALID
)) {
2357 change
= isl_change_drop_first
;
2358 } else if (any_eq(&info
[i
], STATUS_ADJ_EQ
)) {
2359 change
= check_eq_adj_eq(i
, j
, info
);
2360 } else if (any_eq(&info
[j
], STATUS_ADJ_EQ
)) {
2361 change
= check_eq_adj_eq(j
, i
, info
);
2362 } else if (any_eq(&info
[i
], STATUS_ADJ_INEQ
) ||
2363 any_eq(&info
[j
], STATUS_ADJ_INEQ
)) {
2364 change
= check_adj_eq(i
, j
, info
);
2365 } else if (any_ineq(&info
[i
], STATUS_ADJ_EQ
)) {
2366 change
= check_ineq_adj_eq(i
, j
, info
);
2367 } else if (any_ineq(&info
[j
], STATUS_ADJ_EQ
)) {
2368 change
= check_ineq_adj_eq(j
, i
, info
);
2369 } else if (any_ineq(&info
[i
], STATUS_ADJ_INEQ
) ||
2370 any_ineq(&info
[j
], STATUS_ADJ_INEQ
)) {
2371 change
= check_adj_ineq(i
, j
, info
);
2373 if (!any_eq(&info
[i
], STATUS_CUT
) &&
2374 !any_eq(&info
[j
], STATUS_CUT
))
2375 change
= check_facets(i
, j
, info
);
2376 if (change
== isl_change_none
)
2377 change
= check_wrap(i
, j
, info
);
2381 clear_status(&info
[i
]);
2382 clear_status(&info
[j
]);
2385 clear_status(&info
[i
]);
2386 clear_status(&info
[j
]);
2387 return isl_change_error
;
2390 /* Check if the union of the given pair of basic maps
2391 * can be represented by a single basic map.
2392 * If so, replace the pair by the single basic map and return
2393 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
2394 * Otherwise, return isl_change_none.
2395 * The two basic maps are assumed to live in the same local space.
2397 static enum isl_change
coalesce_local_pair(int i
, int j
,
2398 struct isl_coalesce_info
*info
)
2400 init_status(&info
[i
]);
2401 init_status(&info
[j
]);
2402 return coalesce_local_pair_reuse(i
, j
, info
);
2405 /* Shift the integer division at position "div" of the basic map
2406 * represented by "info" by "shift".
2408 * That is, if the integer division has the form
2412 * then replace it by
2414 * floor((f(x) + shift * d)/d) - shift
2416 static isl_stat
shift_div(struct isl_coalesce_info
*info
, int div
,
2419 isl_size total
, n_div
;
2421 info
->bmap
= isl_basic_map_shift_div(info
->bmap
, div
, 0, shift
);
2423 return isl_stat_error
;
2425 total
= isl_basic_map_dim(info
->bmap
, isl_dim_all
);
2426 n_div
= isl_basic_map_dim(info
->bmap
, isl_dim_div
);
2427 if (total
< 0 || n_div
< 0)
2428 return isl_stat_error
;
2430 if (isl_tab_shift_var(info
->tab
, total
+ div
, shift
) < 0)
2431 return isl_stat_error
;
2436 /* If the integer division at position "div" is defined by an equality,
2437 * i.e., a stride constraint, then change the integer division expression
2438 * to have a constant term equal to zero.
2440 * Let the equality constraint be
2444 * The integer division expression is then typically of the form
2446 * a = floor((-f - c')/m)
2448 * The integer division is first shifted by t = floor(c/m),
2449 * turning the equality constraint into
2451 * c - m floor(c/m) + f + m a' = 0
2455 * (c mod m) + f + m a' = 0
2459 * a' = (-f - (c mod m))/m = floor((-f)/m)
2461 * because a' is an integer and 0 <= (c mod m) < m.
2462 * The constant term of a' can therefore be zeroed out,
2463 * but only if the integer division expression is of the expected form.
2465 static isl_stat
normalize_stride_div(struct isl_coalesce_info
*info
, int div
)
2467 isl_bool defined
, valid
;
2470 isl_int shift
, stride
;
2472 defined
= isl_basic_map_has_defining_equality(info
->bmap
, isl_dim_div
,
2475 return isl_stat_error
;
2479 return isl_stat_error
;
2480 valid
= isl_constraint_is_div_equality(c
, div
);
2481 isl_int_init(shift
);
2482 isl_int_init(stride
);
2483 isl_constraint_get_constant(c
, &shift
);
2484 isl_constraint_get_coefficient(c
, isl_dim_div
, div
, &stride
);
2485 isl_int_fdiv_q(shift
, shift
, stride
);
2486 r
= shift_div(info
, div
, shift
);
2487 isl_int_clear(stride
);
2488 isl_int_clear(shift
);
2489 isl_constraint_free(c
);
2490 if (r
< 0 || valid
< 0)
2491 return isl_stat_error
;
2494 info
->bmap
= isl_basic_map_set_div_expr_constant_num_si_inplace(
2495 info
->bmap
, div
, 0);
2497 return isl_stat_error
;
2501 /* The basic maps represented by "info1" and "info2" are known
2502 * to have the same number of integer divisions.
2503 * Check if pairs of integer divisions are equal to each other
2504 * despite the fact that they differ by a rational constant.
2506 * In particular, look for any pair of integer divisions that
2507 * only differ in their constant terms.
2508 * If either of these integer divisions is defined
2509 * by stride constraints, then modify it to have a zero constant term.
2510 * If both are defined by stride constraints then in the end they will have
2511 * the same (zero) constant term.
2513 static isl_stat
harmonize_stride_divs(struct isl_coalesce_info
*info1
,
2514 struct isl_coalesce_info
*info2
)
2519 n
= isl_basic_map_dim(info1
->bmap
, isl_dim_div
);
2521 return isl_stat_error
;
2522 for (i
= 0; i
< n
; ++i
) {
2523 isl_bool known
, harmonize
;
2525 known
= isl_basic_map_div_is_known(info1
->bmap
, i
);
2526 if (known
>= 0 && known
)
2527 known
= isl_basic_map_div_is_known(info2
->bmap
, i
);
2529 return isl_stat_error
;
2532 harmonize
= isl_basic_map_equal_div_expr_except_constant(
2533 info1
->bmap
, i
, info2
->bmap
, i
);
2535 return isl_stat_error
;
2538 if (normalize_stride_div(info1
, i
) < 0)
2539 return isl_stat_error
;
2540 if (normalize_stride_div(info2
, i
) < 0)
2541 return isl_stat_error
;
2547 /* If "shift" is an integer constant, then shift the integer division
2548 * at position "div" of the basic map represented by "info" by "shift".
2549 * If "shift" is not an integer constant, then do nothing.
2550 * If "shift" is equal to zero, then no shift needs to be performed either.
2552 * That is, if the integer division has the form
2556 * then replace it by
2558 * floor((f(x) + shift * d)/d) - shift
2560 static isl_stat
shift_if_cst_int(struct isl_coalesce_info
*info
, int div
,
2561 __isl_keep isl_aff
*shift
)
2568 cst
= isl_aff_is_cst(shift
);
2569 if (cst
< 0 || !cst
)
2570 return cst
< 0 ? isl_stat_error
: isl_stat_ok
;
2572 c
= isl_aff_get_constant_val(shift
);
2573 cst
= isl_val_is_int(c
);
2574 if (cst
>= 0 && cst
)
2575 cst
= isl_bool_not(isl_val_is_zero(c
));
2576 if (cst
< 0 || !cst
) {
2578 return cst
< 0 ? isl_stat_error
: isl_stat_ok
;
2582 r
= isl_val_get_num_isl_int(c
, &d
);
2584 r
= shift_div(info
, div
, d
);
2592 /* Check if some of the divs in the basic map represented by "info1"
2593 * are shifts of the corresponding divs in the basic map represented
2594 * by "info2", taking into account the equality constraints "eq1" of "info1"
2595 * and "eq2" of "info2". If so, align them with those of "info2".
2596 * "info1" and "info2" are assumed to have the same number
2597 * of integer divisions.
2599 * An integer division is considered to be a shift of another integer
2600 * division if, after simplification with respect to the equality
2601 * constraints of the other basic map, one is equal to the other
2604 * In particular, for each pair of integer divisions, if both are known,
2605 * have the same denominator and are not already equal to each other,
2606 * simplify each with respect to the equality constraints
2607 * of the other basic map. If the difference is an integer constant,
2608 * then move this difference outside.
2609 * That is, if, after simplification, one integer division is of the form
2611 * floor((f(x) + c_1)/d)
2613 * while the other is of the form
2615 * floor((f(x) + c_2)/d)
2617 * and n = (c_2 - c_1)/d is an integer, then replace the first
2618 * integer division by
2620 * floor((f_1(x) + c_1 + n * d)/d) - n,
2622 * where floor((f_1(x) + c_1 + n * d)/d) = floor((f2(x) + c_2)/d)
2623 * after simplification with respect to the equality constraints.
2625 static isl_stat
harmonize_divs_with_hulls(struct isl_coalesce_info
*info1
,
2626 struct isl_coalesce_info
*info2
, __isl_keep isl_basic_set
*eq1
,
2627 __isl_keep isl_basic_set
*eq2
)
2631 isl_local_space
*ls1
, *ls2
;
2633 total
= isl_basic_map_dim(info1
->bmap
, isl_dim_all
);
2635 return isl_stat_error
;
2636 ls1
= isl_local_space_wrap(isl_basic_map_get_local_space(info1
->bmap
));
2637 ls2
= isl_local_space_wrap(isl_basic_map_get_local_space(info2
->bmap
));
2638 for (i
= 0; i
< info1
->bmap
->n_div
; ++i
) {
2640 isl_aff
*div1
, *div2
;
2642 if (!isl_local_space_div_is_known(ls1
, i
) ||
2643 !isl_local_space_div_is_known(ls2
, i
))
2645 if (isl_int_ne(info1
->bmap
->div
[i
][0], info2
->bmap
->div
[i
][0]))
2647 if (isl_seq_eq(info1
->bmap
->div
[i
] + 1,
2648 info2
->bmap
->div
[i
] + 1, 1 + total
))
2650 div1
= isl_local_space_get_div(ls1
, i
);
2651 div2
= isl_local_space_get_div(ls2
, i
);
2652 div1
= isl_aff_substitute_equalities(div1
,
2653 isl_basic_set_copy(eq2
));
2654 div2
= isl_aff_substitute_equalities(div2
,
2655 isl_basic_set_copy(eq1
));
2656 div2
= isl_aff_sub(div2
, div1
);
2657 r
= shift_if_cst_int(info1
, i
, div2
);
2662 isl_local_space_free(ls1
);
2663 isl_local_space_free(ls2
);
2665 if (i
< info1
->bmap
->n_div
)
2666 return isl_stat_error
;
2670 /* Check if some of the divs in the basic map represented by "info1"
2671 * are shifts of the corresponding divs in the basic map represented
2672 * by "info2". If so, align them with those of "info2".
2673 * Only do this if "info1" and "info2" have the same number
2674 * of integer divisions.
2676 * An integer division is considered to be a shift of another integer
2677 * division if, after simplification with respect to the equality
2678 * constraints of the other basic map, one is equal to the other
2681 * First check if pairs of integer divisions are equal to each other
2682 * despite the fact that they differ by a rational constant.
2683 * If so, try and arrange for them to have the same constant term.
2685 * Then, extract the equality constraints and continue with
2686 * harmonize_divs_with_hulls.
2688 * If the equality constraints of both basic maps are the same,
2689 * then there is no need to perform any shifting since
2690 * the coefficients of the integer divisions should have been
2691 * reduced in the same way.
2693 static isl_stat
harmonize_divs(struct isl_coalesce_info
*info1
,
2694 struct isl_coalesce_info
*info2
)
2697 isl_basic_map
*bmap1
, *bmap2
;
2698 isl_basic_set
*eq1
, *eq2
;
2701 if (!info1
->bmap
|| !info2
->bmap
)
2702 return isl_stat_error
;
2704 if (info1
->bmap
->n_div
!= info2
->bmap
->n_div
)
2706 if (info1
->bmap
->n_div
== 0)
2709 if (harmonize_stride_divs(info1
, info2
) < 0)
2710 return isl_stat_error
;
2712 bmap1
= isl_basic_map_copy(info1
->bmap
);
2713 bmap2
= isl_basic_map_copy(info2
->bmap
);
2714 eq1
= isl_basic_map_wrap(isl_basic_map_plain_affine_hull(bmap1
));
2715 eq2
= isl_basic_map_wrap(isl_basic_map_plain_affine_hull(bmap2
));
2716 equal
= isl_basic_set_plain_is_equal(eq1
, eq2
);
2722 r
= harmonize_divs_with_hulls(info1
, info2
, eq1
, eq2
);
2723 isl_basic_set_free(eq1
);
2724 isl_basic_set_free(eq2
);
2729 /* Do the two basic maps live in the same local space, i.e.,
2730 * do they have the same (known) divs?
2731 * If either basic map has any unknown divs, then we can only assume
2732 * that they do not live in the same local space.
2734 static isl_bool
same_divs(__isl_keep isl_basic_map
*bmap1
,
2735 __isl_keep isl_basic_map
*bmap2
)
2741 if (!bmap1
|| !bmap2
)
2742 return isl_bool_error
;
2743 if (bmap1
->n_div
!= bmap2
->n_div
)
2744 return isl_bool_false
;
2746 if (bmap1
->n_div
== 0)
2747 return isl_bool_true
;
2749 known
= isl_basic_map_divs_known(bmap1
);
2750 if (known
< 0 || !known
)
2752 known
= isl_basic_map_divs_known(bmap2
);
2753 if (known
< 0 || !known
)
2756 total
= isl_basic_map_dim(bmap1
, isl_dim_all
);
2758 return isl_bool_error
;
2759 for (i
= 0; i
< bmap1
->n_div
; ++i
)
2760 if (!isl_seq_eq(bmap1
->div
[i
], bmap2
->div
[i
], 2 + total
))
2761 return isl_bool_false
;
2763 return isl_bool_true
;
2766 /* Assuming that "tab" contains the equality constraints and
2767 * the initial inequality constraints of "bmap", copy the remaining
2768 * inequality constraints of "bmap" to "Tab".
2770 static isl_stat
copy_ineq(struct isl_tab
*tab
, __isl_keep isl_basic_map
*bmap
)
2775 return isl_stat_error
;
2777 n_ineq
= tab
->n_con
- tab
->n_eq
;
2778 for (i
= n_ineq
; i
< bmap
->n_ineq
; ++i
)
2779 if (isl_tab_add_ineq(tab
, bmap
->ineq
[i
]) < 0)
2780 return isl_stat_error
;
2785 /* Description of an integer division that is added
2786 * during an expansion.
2787 * "pos" is the position of the corresponding variable.
2788 * "cst" indicates whether this integer division has a fixed value.
2789 * "val" contains the fixed value, if the value is fixed.
2791 struct isl_expanded
{
2797 /* For each of the "n" integer division variables "expanded",
2798 * if the variable has a fixed value, then add two inequality
2799 * constraints expressing the fixed value.
2800 * Otherwise, add the corresponding div constraints.
2801 * The caller is responsible for removing the div constraints
2802 * that it added for all these "n" integer divisions.
2804 * The div constraints and the pair of inequality constraints
2805 * forcing the fixed value cannot both be added for a given variable
2806 * as the combination may render some of the original constraints redundant.
2807 * These would then be ignored during the coalescing detection,
2808 * while they could remain in the fused result.
2810 * The two added inequality constraints are
2815 * with "a" the variable and "v" its fixed value.
2816 * The facet corresponding to one of these two constraints is selected
2817 * in the tableau to ensure that the pair of inequality constraints
2818 * is treated as an equality constraint.
2820 * The information in info->ineq is thrown away because it was
2821 * computed in terms of div constraints, while some of those
2822 * have now been replaced by these pairs of inequality constraints.
2824 static isl_stat
fix_constant_divs(struct isl_coalesce_info
*info
,
2825 int n
, struct isl_expanded
*expanded
)
2831 o_div
= isl_basic_map_offset(info
->bmap
, isl_dim_div
) - 1;
2832 ineq
= isl_vec_alloc(isl_tab_get_ctx(info
->tab
), 1 + info
->tab
->n_var
);
2834 return isl_stat_error
;
2835 isl_seq_clr(ineq
->el
+ 1, info
->tab
->n_var
);
2837 for (i
= 0; i
< n
; ++i
) {
2838 if (!expanded
[i
].cst
) {
2839 info
->bmap
= isl_basic_map_extend_constraints(
2841 info
->bmap
= isl_basic_map_add_div_constraints(
2842 info
->bmap
, expanded
[i
].pos
- o_div
);
2844 isl_int_set_si(ineq
->el
[1 + expanded
[i
].pos
], -1);
2845 isl_int_set(ineq
->el
[0], expanded
[i
].val
);
2846 info
->bmap
= isl_basic_map_add_ineq(info
->bmap
,
2848 isl_int_set_si(ineq
->el
[1 + expanded
[i
].pos
], 1);
2849 isl_int_neg(ineq
->el
[0], expanded
[i
].val
);
2850 info
->bmap
= isl_basic_map_add_ineq(info
->bmap
,
2852 isl_int_set_si(ineq
->el
[1 + expanded
[i
].pos
], 0);
2854 if (copy_ineq(info
->tab
, info
->bmap
) < 0)
2856 if (expanded
[i
].cst
&&
2857 isl_tab_select_facet(info
->tab
, info
->tab
->n_con
- 1) < 0)
2866 return i
< n
? isl_stat_error
: isl_stat_ok
;
2869 /* Insert the "n" integer division variables "expanded"
2870 * into info->tab and info->bmap and
2871 * update info->ineq with respect to the redundant constraints
2872 * in the resulting tableau.
2873 * "bmap" contains the result of this insertion in info->bmap,
2874 * while info->bmap is the original version
2875 * of "bmap", i.e., the one that corresponds to the current
2876 * state of info->tab. The number of constraints in info->bmap
2877 * is assumed to be the same as the number of constraints
2878 * in info->tab. This is required to be able to detect
2879 * the extra constraints in "bmap".
2881 * In particular, introduce extra variables corresponding
2882 * to the extra integer divisions and add the div constraints
2883 * that were added to "bmap" after info->tab was created
2885 * Furthermore, check if these extra integer divisions happen
2886 * to attain a fixed integer value in info->tab.
2887 * If so, replace the corresponding div constraints by pairs
2888 * of inequality constraints that fix these
2889 * integer divisions to their single integer values.
2890 * Replace info->bmap by "bmap" to match the changes to info->tab.
2891 * info->ineq was computed without a tableau and therefore
2892 * does not take into account the redundant constraints
2893 * in the tableau. Mark them here.
2894 * There is no need to check the newly added div constraints
2895 * since they cannot be redundant.
2896 * The redundancy check is not performed when constants have been discovered
2897 * since info->ineq is completely thrown away in this case.
2899 static isl_stat
tab_insert_divs(struct isl_coalesce_info
*info
,
2900 int n
, struct isl_expanded
*expanded
, __isl_take isl_basic_map
*bmap
)
2904 struct isl_tab_undo
*snap
;
2908 return isl_stat_error
;
2909 if (info
->bmap
->n_eq
+ info
->bmap
->n_ineq
!= info
->tab
->n_con
)
2910 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_internal
,
2911 "original tableau does not correspond "
2912 "to original basic map", goto error
);
2914 if (isl_tab_extend_vars(info
->tab
, n
) < 0)
2916 if (isl_tab_extend_cons(info
->tab
, 2 * n
) < 0)
2919 for (i
= 0; i
< n
; ++i
) {
2920 if (isl_tab_insert_var(info
->tab
, expanded
[i
].pos
) < 0)
2924 snap
= isl_tab_snap(info
->tab
);
2926 n_ineq
= info
->tab
->n_con
- info
->tab
->n_eq
;
2927 if (copy_ineq(info
->tab
, bmap
) < 0)
2930 isl_basic_map_free(info
->bmap
);
2934 for (i
= 0; i
< n
; ++i
) {
2935 expanded
[i
].cst
= isl_tab_is_constant(info
->tab
,
2936 expanded
[i
].pos
, &expanded
[i
].val
);
2937 if (expanded
[i
].cst
< 0)
2938 return isl_stat_error
;
2939 if (expanded
[i
].cst
)
2944 if (isl_tab_rollback(info
->tab
, snap
) < 0)
2945 return isl_stat_error
;
2946 info
->bmap
= isl_basic_map_cow(info
->bmap
);
2947 if (isl_basic_map_free_inequality(info
->bmap
, 2 * n
) < 0)
2948 return isl_stat_error
;
2950 return fix_constant_divs(info
, n
, expanded
);
2953 n_eq
= info
->bmap
->n_eq
;
2954 for (i
= 0; i
< n_ineq
; ++i
) {
2955 if (isl_tab_is_redundant(info
->tab
, n_eq
+ i
))
2956 info
->ineq
[i
] = STATUS_REDUNDANT
;
2961 isl_basic_map_free(bmap
);
2962 return isl_stat_error
;
2965 /* Expand info->tab and info->bmap in the same way "bmap" was expanded
2966 * in isl_basic_map_expand_divs using the expansion "exp" and
2967 * update info->ineq with respect to the redundant constraints
2968 * in the resulting tableau. info->bmap is the original version
2969 * of "bmap", i.e., the one that corresponds to the current
2970 * state of info->tab. The number of constraints in info->bmap
2971 * is assumed to be the same as the number of constraints
2972 * in info->tab. This is required to be able to detect
2973 * the extra constraints in "bmap".
2975 * Extract the positions where extra local variables are introduced
2976 * from "exp" and call tab_insert_divs.
2978 static isl_stat
expand_tab(struct isl_coalesce_info
*info
, int *exp
,
2979 __isl_take isl_basic_map
*bmap
)
2982 struct isl_expanded
*expanded
;
2985 isl_size total
, n_div
;
2989 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
2990 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
2991 if (total
< 0 || n_div
< 0)
2992 return isl_stat_error
;
2993 pos
= total
- n_div
;
2994 extra_var
= total
- info
->tab
->n_var
;
2995 n
= n_div
- extra_var
;
2997 ctx
= isl_basic_map_get_ctx(bmap
);
2998 expanded
= isl_calloc_array(ctx
, struct isl_expanded
, extra_var
);
2999 if (extra_var
&& !expanded
)
3004 for (j
= 0; j
< n_div
; ++j
) {
3005 if (i
< n
&& exp
[i
] == j
) {
3009 expanded
[k
++].pos
= pos
+ j
;
3012 for (k
= 0; k
< extra_var
; ++k
)
3013 isl_int_init(expanded
[k
].val
);
3015 r
= tab_insert_divs(info
, extra_var
, expanded
, bmap
);
3017 for (k
= 0; k
< extra_var
; ++k
)
3018 isl_int_clear(expanded
[k
].val
);
3023 isl_basic_map_free(bmap
);
3024 return isl_stat_error
;
3027 /* Check if the union of the basic maps represented by info[i] and info[j]
3028 * can be represented by a single basic map,
3029 * after expanding the divs of info[i] to match those of info[j].
3030 * If so, replace the pair by the single basic map and return
3031 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
3032 * Otherwise, return isl_change_none.
3034 * The caller has already checked for info[j] being a subset of info[i].
3035 * If some of the divs of info[j] are unknown, then the expanded info[i]
3036 * will not have the corresponding div constraints. The other patterns
3037 * therefore cannot apply. Skip the computation in this case.
3039 * The expansion is performed using the divs "div" and expansion "exp"
3040 * computed by the caller.
3041 * info[i].bmap has already been expanded and the result is passed in
3043 * The "eq" and "ineq" fields of info[i] reflect the status of
3044 * the constraints of the expanded "bmap" with respect to info[j].tab.
3045 * However, inequality constraints that are redundant in info[i].tab
3046 * have not yet been marked as such because no tableau was available.
3048 * Replace info[i].bmap by "bmap" and expand info[i].tab as well,
3049 * updating info[i].ineq with respect to the redundant constraints.
3050 * Then try and coalesce the expanded info[i] with info[j],
3051 * reusing the information in info[i].eq and info[i].ineq.
3052 * If this does not result in any coalescing or if it results in info[j]
3053 * getting dropped (which should not happen in practice, since the case
3054 * of info[j] being a subset of info[i] has already been checked by
3055 * the caller), then revert info[i] to its original state.
3057 static enum isl_change
coalesce_expand_tab_divs(__isl_take isl_basic_map
*bmap
,
3058 int i
, int j
, struct isl_coalesce_info
*info
, __isl_keep isl_mat
*div
,
3062 isl_basic_map
*bmap_i
;
3063 struct isl_tab_undo
*snap
;
3064 enum isl_change change
= isl_change_none
;
3066 known
= isl_basic_map_divs_known(info
[j
].bmap
);
3067 if (known
< 0 || !known
) {
3068 clear_status(&info
[i
]);
3069 isl_basic_map_free(bmap
);
3070 return known
< 0 ? isl_change_error
: isl_change_none
;
3073 bmap_i
= isl_basic_map_copy(info
[i
].bmap
);
3074 snap
= isl_tab_snap(info
[i
].tab
);
3075 if (expand_tab(&info
[i
], exp
, bmap
) < 0)
3076 change
= isl_change_error
;
3078 init_status(&info
[j
]);
3079 if (change
== isl_change_none
)
3080 change
= coalesce_local_pair_reuse(i
, j
, info
);
3082 clear_status(&info
[i
]);
3083 if (change
!= isl_change_none
&& change
!= isl_change_drop_second
) {
3084 isl_basic_map_free(bmap_i
);
3086 isl_basic_map_free(info
[i
].bmap
);
3087 info
[i
].bmap
= bmap_i
;
3089 if (isl_tab_rollback(info
[i
].tab
, snap
) < 0)
3090 change
= isl_change_error
;
3096 /* Check if the union of "bmap" and the basic map represented by info[j]
3097 * can be represented by a single basic map,
3098 * after expanding the divs of "bmap" to match those of info[j].
3099 * If so, replace the pair by the single basic map and return
3100 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
3101 * Otherwise, return isl_change_none.
3103 * In particular, check if the expanded "bmap" contains the basic map
3104 * represented by the tableau info[j].tab.
3105 * The expansion is performed using the divs "div" and expansion "exp"
3106 * computed by the caller.
3107 * Then we check if all constraints of the expanded "bmap" are valid for
3110 * If "i" is not equal to -1, then "bmap" is equal to info[i].bmap.
3111 * In this case, the positions of the constraints of info[i].bmap
3112 * with respect to the basic map represented by info[j] are stored
3115 * If the expanded "bmap" does not contain the basic map
3116 * represented by the tableau info[j].tab and if "i" is not -1,
3117 * i.e., if the original "bmap" is info[i].bmap, then expand info[i].tab
3118 * as well and check if that results in coalescing.
3120 static enum isl_change
coalesce_with_expanded_divs(
3121 __isl_keep isl_basic_map
*bmap
, int i
, int j
,
3122 struct isl_coalesce_info
*info
, __isl_keep isl_mat
*div
, int *exp
)
3124 enum isl_change change
= isl_change_none
;
3125 struct isl_coalesce_info info_local
, *info_i
;
3127 info_i
= i
>= 0 ? &info
[i
] : &info_local
;
3128 init_status(info_i
);
3129 bmap
= isl_basic_map_copy(bmap
);
3130 bmap
= isl_basic_map_expand_divs(bmap
, isl_mat_copy(div
), exp
);
3131 bmap
= isl_basic_map_mark_final(bmap
);
3136 info_local
.bmap
= bmap
;
3137 info_i
->eq
= eq_status_in(bmap
, info
[j
].tab
);
3138 if (bmap
->n_eq
&& !info_i
->eq
)
3140 if (any_eq(info_i
, STATUS_ERROR
))
3142 if (any_eq(info_i
, STATUS_SEPARATE
))
3145 info_i
->ineq
= ineq_status_in(bmap
, NULL
, info
[j
].tab
);
3146 if (bmap
->n_ineq
&& !info_i
->ineq
)
3148 if (any_ineq(info_i
, STATUS_ERROR
))
3150 if (any_ineq(info_i
, STATUS_SEPARATE
))
3153 if (all(info_i
->eq
, 2 * bmap
->n_eq
, STATUS_VALID
) &&
3154 all(info_i
->ineq
, bmap
->n_ineq
, STATUS_VALID
)) {
3156 change
= isl_change_drop_second
;
3159 if (change
== isl_change_none
&& i
!= -1)
3160 return coalesce_expand_tab_divs(bmap
, i
, j
, info
, div
, exp
);
3163 isl_basic_map_free(bmap
);
3164 clear_status(info_i
);
3167 isl_basic_map_free(bmap
);
3168 clear_status(info_i
);
3169 return isl_change_error
;
3172 /* Check if the union of "bmap_i" and the basic map represented by info[j]
3173 * can be represented by a single basic map,
3174 * after aligning the divs of "bmap_i" to match those of info[j].
3175 * If so, replace the pair by the single basic map and return
3176 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
3177 * Otherwise, return isl_change_none.
3179 * In particular, check if "bmap_i" contains the basic map represented by
3180 * info[j] after aligning the divs of "bmap_i" to those of info[j].
3181 * Note that this can only succeed if the number of divs of "bmap_i"
3182 * is smaller than (or equal to) the number of divs of info[j].
3184 * We first check if the divs of "bmap_i" are all known and form a subset
3185 * of those of info[j].bmap. If so, we pass control over to
3186 * coalesce_with_expanded_divs.
3188 * If "i" is not equal to -1, then "bmap" is equal to info[i].bmap.
3190 static enum isl_change
coalesce_after_aligning_divs(
3191 __isl_keep isl_basic_map
*bmap_i
, int i
, int j
,
3192 struct isl_coalesce_info
*info
)
3195 isl_mat
*div_i
, *div_j
, *div
;
3199 enum isl_change change
;
3201 known
= isl_basic_map_divs_known(bmap_i
);
3203 return isl_change_error
;
3205 return isl_change_none
;
3207 ctx
= isl_basic_map_get_ctx(bmap_i
);
3209 div_i
= isl_basic_map_get_divs(bmap_i
);
3210 div_j
= isl_basic_map_get_divs(info
[j
].bmap
);
3212 if (!div_i
|| !div_j
)
3215 exp1
= isl_alloc_array(ctx
, int, div_i
->n_row
);
3216 exp2
= isl_alloc_array(ctx
, int, div_j
->n_row
);
3217 if ((div_i
->n_row
&& !exp1
) || (div_j
->n_row
&& !exp2
))
3220 div
= isl_merge_divs(div_i
, div_j
, exp1
, exp2
);
3224 if (div
->n_row
== div_j
->n_row
)
3225 change
= coalesce_with_expanded_divs(bmap_i
,
3226 i
, j
, info
, div
, exp1
);
3228 change
= isl_change_none
;
3232 isl_mat_free(div_i
);
3233 isl_mat_free(div_j
);
3240 isl_mat_free(div_i
);
3241 isl_mat_free(div_j
);
3244 return isl_change_error
;
3247 /* Check if basic map "j" is a subset of basic map "i" after
3248 * exploiting the extra equalities of "j" to simplify the divs of "i".
3249 * If so, remove basic map "j" and return isl_change_drop_second.
3251 * If "j" does not have any equalities or if they are the same
3252 * as those of "i", then we cannot exploit them to simplify the divs.
3253 * Similarly, if there are no divs in "i", then they cannot be simplified.
3254 * If, on the other hand, the affine hulls of "i" and "j" do not intersect,
3255 * then "j" cannot be a subset of "i".
3257 * Otherwise, we intersect "i" with the affine hull of "j" and then
3258 * check if "j" is a subset of the result after aligning the divs.
3259 * If so, then "j" is definitely a subset of "i" and can be removed.
3260 * Note that if after intersection with the affine hull of "j".
3261 * "i" still has more divs than "j", then there is no way we can
3262 * align the divs of "i" to those of "j".
3264 static enum isl_change
coalesce_subset_with_equalities(int i
, int j
,
3265 struct isl_coalesce_info
*info
)
3267 isl_basic_map
*hull_i
, *hull_j
, *bmap_i
;
3269 enum isl_change change
;
3271 if (info
[j
].bmap
->n_eq
== 0)
3272 return isl_change_none
;
3273 if (info
[i
].bmap
->n_div
== 0)
3274 return isl_change_none
;
3276 hull_i
= isl_basic_map_copy(info
[i
].bmap
);
3277 hull_i
= isl_basic_map_plain_affine_hull(hull_i
);
3278 hull_j
= isl_basic_map_copy(info
[j
].bmap
);
3279 hull_j
= isl_basic_map_plain_affine_hull(hull_j
);
3281 hull_j
= isl_basic_map_intersect(hull_j
, isl_basic_map_copy(hull_i
));
3282 equal
= isl_basic_map_plain_is_equal(hull_i
, hull_j
);
3283 empty
= isl_basic_map_plain_is_empty(hull_j
);
3284 isl_basic_map_free(hull_i
);
3286 if (equal
< 0 || equal
|| empty
< 0 || empty
) {
3287 isl_basic_map_free(hull_j
);
3288 if (equal
< 0 || empty
< 0)
3289 return isl_change_error
;
3290 return isl_change_none
;
3293 bmap_i
= isl_basic_map_copy(info
[i
].bmap
);
3294 bmap_i
= isl_basic_map_intersect(bmap_i
, hull_j
);
3296 return isl_change_error
;
3298 if (bmap_i
->n_div
> info
[j
].bmap
->n_div
) {
3299 isl_basic_map_free(bmap_i
);
3300 return isl_change_none
;
3303 change
= coalesce_after_aligning_divs(bmap_i
, -1, j
, info
);
3305 isl_basic_map_free(bmap_i
);
3310 /* Check if the union of and the basic maps represented by info[i] and info[j]
3311 * can be represented by a single basic map, by aligning or equating
3312 * their integer divisions.
3313 * If so, replace the pair by the single basic map and return
3314 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
3315 * Otherwise, return isl_change_none.
3317 * Note that we only perform any test if the number of divs is different
3318 * in the two basic maps. In case the number of divs is the same,
3319 * we have already established that the divs are different
3320 * in the two basic maps.
3321 * In particular, if the number of divs of basic map i is smaller than
3322 * the number of divs of basic map j, then we check if j is a subset of i
3325 static enum isl_change
coalesce_divs(int i
, int j
,
3326 struct isl_coalesce_info
*info
)
3328 enum isl_change change
= isl_change_none
;
3330 if (info
[i
].bmap
->n_div
< info
[j
].bmap
->n_div
)
3331 change
= coalesce_after_aligning_divs(info
[i
].bmap
, i
, j
, info
);
3332 if (change
!= isl_change_none
)
3335 if (info
[j
].bmap
->n_div
< info
[i
].bmap
->n_div
)
3336 change
= coalesce_after_aligning_divs(info
[j
].bmap
, j
, i
, info
);
3337 if (change
!= isl_change_none
)
3338 return invert_change(change
);
3340 change
= coalesce_subset_with_equalities(i
, j
, info
);
3341 if (change
!= isl_change_none
)
3344 change
= coalesce_subset_with_equalities(j
, i
, info
);
3345 if (change
!= isl_change_none
)
3346 return invert_change(change
);
3348 return isl_change_none
;
3351 /* Does "bmap" involve any divs that themselves refer to divs?
3353 static isl_bool
has_nested_div(__isl_keep isl_basic_map
*bmap
)
3359 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
3360 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
3361 if (total
< 0 || n_div
< 0)
3362 return isl_bool_error
;
3365 for (i
= 0; i
< n_div
; ++i
)
3366 if (isl_seq_first_non_zero(bmap
->div
[i
] + 2 + total
,
3368 return isl_bool_true
;
3370 return isl_bool_false
;
3373 /* Return a list of affine expressions, one for each integer division
3374 * in "bmap_i". For each integer division that also appears in "bmap_j",
3375 * the affine expression is set to NaN. The number of NaNs in the list
3376 * is equal to the number of integer divisions in "bmap_j".
3377 * For the other integer divisions of "bmap_i", the corresponding
3378 * element in the list is a purely affine expression equal to the integer
3379 * division in "hull".
3380 * If no such list can be constructed, then the number of elements
3381 * in the returned list is smaller than the number of integer divisions
3384 static __isl_give isl_aff_list
*set_up_substitutions(
3385 __isl_keep isl_basic_map
*bmap_i
, __isl_keep isl_basic_map
*bmap_j
,
3386 __isl_take isl_basic_map
*hull
)
3388 isl_size n_div_i
, n_div_j
, total
;
3390 isl_local_space
*ls
;
3391 isl_basic_set
*wrap_hull
;
3396 n_div_i
= isl_basic_map_dim(bmap_i
, isl_dim_div
);
3397 n_div_j
= isl_basic_map_dim(bmap_j
, isl_dim_div
);
3398 total
= isl_basic_map_dim(bmap_i
, isl_dim_all
);
3399 if (!hull
|| n_div_i
< 0 || n_div_j
< 0 || total
< 0)
3402 ctx
= isl_basic_map_get_ctx(hull
);
3405 ls
= isl_basic_map_get_local_space(bmap_i
);
3406 ls
= isl_local_space_wrap(ls
);
3407 wrap_hull
= isl_basic_map_wrap(hull
);
3409 aff_nan
= isl_aff_nan_on_domain(isl_local_space_copy(ls
));
3410 list
= isl_aff_list_alloc(ctx
, n_div_i
);
3413 for (i
= 0; i
< n_div_i
; ++i
) {
3418 isl_basic_map_equal_div_expr_part(bmap_i
, i
, bmap_j
, j
,
3421 list
= isl_aff_list_add(list
, isl_aff_copy(aff_nan
));
3424 if (n_div_i
- i
<= n_div_j
- j
)
3427 aff
= isl_local_space_get_div(ls
, i
);
3428 aff
= isl_aff_substitute_equalities(aff
,
3429 isl_basic_set_copy(wrap_hull
));
3430 aff
= isl_aff_floor(aff
);
3431 n_div
= isl_aff_dim(aff
, isl_dim_div
);
3439 list
= isl_aff_list_add(list
, aff
);
3442 isl_aff_free(aff_nan
);
3443 isl_local_space_free(ls
);
3444 isl_basic_set_free(wrap_hull
);
3448 isl_aff_free(aff_nan
);
3449 isl_local_space_free(ls
);
3450 isl_basic_set_free(wrap_hull
);
3451 isl_aff_list_free(list
);
3455 /* Add variables to info->bmap and info->tab corresponding to the elements
3456 * in "list" that are not set to NaN.
3457 * "extra_var" is the number of these elements.
3458 * "dim" is the offset in the variables of "tab" where we should
3459 * start considering the elements in "list".
3460 * When this function returns, the total number of variables in "tab"
3461 * is equal to "dim" plus the number of elements in "list".
3463 * The newly added existentially quantified variables are not given
3464 * an explicit representation because the corresponding div constraints
3465 * do not appear in info->bmap. These constraints are not added
3466 * to info->bmap because for internal consistency, they would need to
3467 * be added to info->tab as well, where they could combine with the equality
3468 * that is added later to result in constraints that do not hold
3469 * in the original input.
3471 static isl_stat
add_sub_vars(struct isl_coalesce_info
*info
,
3472 __isl_keep isl_aff_list
*list
, int dim
, int extra_var
)
3478 space
= isl_basic_map_get_space(info
->bmap
);
3479 info
->bmap
= isl_basic_map_cow(info
->bmap
);
3480 info
->bmap
= isl_basic_map_extend_space(info
->bmap
, space
,
3482 n
= isl_aff_list_n_aff(list
);
3483 if (!info
->bmap
|| n
< 0)
3484 return isl_stat_error
;
3485 for (i
= 0; i
< n
; ++i
) {
3489 aff
= isl_aff_list_get_aff(list
, i
);
3490 is_nan
= isl_aff_is_nan(aff
);
3493 return isl_stat_error
;
3497 if (isl_tab_insert_var(info
->tab
, dim
+ i
) < 0)
3498 return isl_stat_error
;
3499 d
= isl_basic_map_alloc_div(info
->bmap
);
3501 return isl_stat_error
;
3502 info
->bmap
= isl_basic_map_mark_div_unknown(info
->bmap
, d
);
3503 for (j
= d
; j
> i
; --j
)
3504 info
->bmap
= isl_basic_map_swap_div(info
->bmap
,
3507 return isl_stat_error
;
3513 /* For each element in "list" that is not set to NaN, fix the corresponding
3514 * variable in "tab" to the purely affine expression defined by the element.
3515 * "dim" is the offset in the variables of "tab" where we should
3516 * start considering the elements in "list".
3518 * This function assumes that a sufficient number of rows and
3519 * elements in the constraint array are available in the tableau.
3521 static isl_stat
add_sub_equalities(struct isl_tab
*tab
,
3522 __isl_keep isl_aff_list
*list
, int dim
)
3530 n
= isl_aff_list_n_aff(list
);
3532 return isl_stat_error
;
3534 ctx
= isl_tab_get_ctx(tab
);
3535 sub
= isl_vec_alloc(ctx
, 1 + dim
+ n
);
3537 return isl_stat_error
;
3538 isl_seq_clr(sub
->el
+ 1 + dim
, n
);
3540 for (i
= 0; i
< n
; ++i
) {
3541 aff
= isl_aff_list_get_aff(list
, i
);
3544 if (isl_aff_is_nan(aff
)) {
3548 isl_seq_cpy(sub
->el
, aff
->v
->el
+ 1, 1 + dim
);
3549 isl_int_neg(sub
->el
[1 + dim
+ i
], aff
->v
->el
[0]);
3550 if (isl_tab_add_eq(tab
, sub
->el
) < 0)
3552 isl_int_set_si(sub
->el
[1 + dim
+ i
], 0);
3561 return isl_stat_error
;
3564 /* Add variables to info->tab and info->bmap corresponding to the elements
3565 * in "list" that are not set to NaN. The value of the added variable
3566 * in info->tab is fixed to the purely affine expression defined by the element.
3567 * "dim" is the offset in the variables of info->tab where we should
3568 * start considering the elements in "list".
3569 * When this function returns, the total number of variables in info->tab
3570 * is equal to "dim" plus the number of elements in "list".
3572 static isl_stat
add_subs(struct isl_coalesce_info
*info
,
3573 __isl_keep isl_aff_list
*list
, int dim
)
3578 n
= isl_aff_list_n_aff(list
);
3580 return isl_stat_error
;
3582 extra_var
= n
- (info
->tab
->n_var
- dim
);
3584 if (isl_tab_extend_vars(info
->tab
, extra_var
) < 0)
3585 return isl_stat_error
;
3586 if (isl_tab_extend_cons(info
->tab
, 2 * extra_var
) < 0)
3587 return isl_stat_error
;
3588 if (add_sub_vars(info
, list
, dim
, extra_var
) < 0)
3589 return isl_stat_error
;
3591 return add_sub_equalities(info
->tab
, list
, dim
);
3594 /* Coalesce basic map "j" into basic map "i" after adding the extra integer
3595 * divisions in "i" but not in "j" to basic map "j", with values
3596 * specified by "list". The total number of elements in "list"
3597 * is equal to the number of integer divisions in "i", while the number
3598 * of NaN elements in the list is equal to the number of integer divisions
3601 * If no coalescing can be performed, then we need to revert basic map "j"
3602 * to its original state. We do the same if basic map "i" gets dropped
3603 * during the coalescing, even though this should not happen in practice
3604 * since we have already checked for "j" being a subset of "i"
3605 * before we reach this stage.
3607 static enum isl_change
coalesce_with_subs(int i
, int j
,
3608 struct isl_coalesce_info
*info
, __isl_keep isl_aff_list
*list
)
3610 isl_basic_map
*bmap_j
;
3611 struct isl_tab_undo
*snap
;
3612 isl_size dim
, n_div
;
3613 enum isl_change change
;
3615 bmap_j
= isl_basic_map_copy(info
[j
].bmap
);
3616 snap
= isl_tab_snap(info
[j
].tab
);
3618 dim
= isl_basic_map_dim(bmap_j
, isl_dim_all
);
3619 n_div
= isl_basic_map_dim(bmap_j
, isl_dim_div
);
3620 if (dim
< 0 || n_div
< 0)
3623 if (add_subs(&info
[j
], list
, dim
) < 0)
3626 change
= coalesce_local_pair(i
, j
, info
);
3627 if (change
!= isl_change_none
&& change
!= isl_change_drop_first
) {
3628 isl_basic_map_free(bmap_j
);
3630 isl_basic_map_free(info
[j
].bmap
);
3631 info
[j
].bmap
= bmap_j
;
3633 if (isl_tab_rollback(info
[j
].tab
, snap
) < 0)
3634 return isl_change_error
;
3639 isl_basic_map_free(bmap_j
);
3640 return isl_change_error
;
3643 /* Check if we can coalesce basic map "j" into basic map "i" after copying
3644 * those extra integer divisions in "i" that can be simplified away
3645 * using the extra equalities in "j".
3646 * All divs are assumed to be known and not contain any nested divs.
3648 * We first check if there are any extra equalities in "j" that we
3649 * can exploit. Then we check if every integer division in "i"
3650 * either already appears in "j" or can be simplified using the
3651 * extra equalities to a purely affine expression.
3652 * If these tests succeed, then we try to coalesce the two basic maps
3653 * by introducing extra dimensions in "j" corresponding to
3654 * the extra integer divsisions "i" fixed to the corresponding
3655 * purely affine expression.
3657 static enum isl_change
check_coalesce_into_eq(int i
, int j
,
3658 struct isl_coalesce_info
*info
)
3660 isl_size n_div_i
, n_div_j
, n
;
3661 isl_basic_map
*hull_i
, *hull_j
;
3662 isl_bool equal
, empty
;
3664 enum isl_change change
;
3666 n_div_i
= isl_basic_map_dim(info
[i
].bmap
, isl_dim_div
);
3667 n_div_j
= isl_basic_map_dim(info
[j
].bmap
, isl_dim_div
);
3668 if (n_div_i
< 0 || n_div_j
< 0)
3669 return isl_change_error
;
3670 if (n_div_i
<= n_div_j
)
3671 return isl_change_none
;
3672 if (info
[j
].bmap
->n_eq
== 0)
3673 return isl_change_none
;
3675 hull_i
= isl_basic_map_copy(info
[i
].bmap
);
3676 hull_i
= isl_basic_map_plain_affine_hull(hull_i
);
3677 hull_j
= isl_basic_map_copy(info
[j
].bmap
);
3678 hull_j
= isl_basic_map_plain_affine_hull(hull_j
);
3680 hull_j
= isl_basic_map_intersect(hull_j
, isl_basic_map_copy(hull_i
));
3681 equal
= isl_basic_map_plain_is_equal(hull_i
, hull_j
);
3682 empty
= isl_basic_map_plain_is_empty(hull_j
);
3683 isl_basic_map_free(hull_i
);
3685 if (equal
< 0 || empty
< 0)
3687 if (equal
|| empty
) {
3688 isl_basic_map_free(hull_j
);
3689 return isl_change_none
;
3692 list
= set_up_substitutions(info
[i
].bmap
, info
[j
].bmap
, hull_j
);
3694 return isl_change_error
;
3695 n
= isl_aff_list_n_aff(list
);
3697 change
= isl_change_error
;
3698 else if (n
< n_div_i
)
3699 change
= isl_change_none
;
3701 change
= coalesce_with_subs(i
, j
, info
, list
);
3703 isl_aff_list_free(list
);
3707 isl_basic_map_free(hull_j
);
3708 return isl_change_error
;
3711 /* Check if we can coalesce basic maps "i" and "j" after copying
3712 * those extra integer divisions in one of the basic maps that can
3713 * be simplified away using the extra equalities in the other basic map.
3714 * We require all divs to be known in both basic maps.
3715 * Furthermore, to simplify the comparison of div expressions,
3716 * we do not allow any nested integer divisions.
3718 static enum isl_change
check_coalesce_eq(int i
, int j
,
3719 struct isl_coalesce_info
*info
)
3721 isl_bool known
, nested
;
3722 enum isl_change change
;
3724 known
= isl_basic_map_divs_known(info
[i
].bmap
);
3725 if (known
< 0 || !known
)
3726 return known
< 0 ? isl_change_error
: isl_change_none
;
3727 known
= isl_basic_map_divs_known(info
[j
].bmap
);
3728 if (known
< 0 || !known
)
3729 return known
< 0 ? isl_change_error
: isl_change_none
;
3730 nested
= has_nested_div(info
[i
].bmap
);
3731 if (nested
< 0 || nested
)
3732 return nested
< 0 ? isl_change_error
: isl_change_none
;
3733 nested
= has_nested_div(info
[j
].bmap
);
3734 if (nested
< 0 || nested
)
3735 return nested
< 0 ? isl_change_error
: isl_change_none
;
3737 change
= check_coalesce_into_eq(i
, j
, info
);
3738 if (change
!= isl_change_none
)
3740 change
= check_coalesce_into_eq(j
, i
, info
);
3741 if (change
!= isl_change_none
)
3742 return invert_change(change
);
3744 return isl_change_none
;
3747 /* Check if the union of the given pair of basic maps
3748 * can be represented by a single basic map.
3749 * If so, replace the pair by the single basic map and return
3750 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
3751 * Otherwise, return isl_change_none.
3753 * We first check if the two basic maps live in the same local space,
3754 * after aligning the divs that differ by only an integer constant.
3755 * If so, we do the complete check. Otherwise, we check if they have
3756 * the same number of integer divisions and can be coalesced, if one is
3757 * an obvious subset of the other or if the extra integer divisions
3758 * of one basic map can be simplified away using the extra equalities
3759 * of the other basic map.
3761 * Note that trying to coalesce pairs of disjuncts with the same
3762 * number, but different local variables may drop the explicit
3763 * representation of some of these local variables.
3764 * This operation is therefore not performed when
3765 * the "coalesce_preserve_locals" option is set.
3767 static enum isl_change
coalesce_pair(int i
, int j
,
3768 struct isl_coalesce_info
*info
)
3772 enum isl_change change
;
3775 if (harmonize_divs(&info
[i
], &info
[j
]) < 0)
3776 return isl_change_error
;
3777 same
= same_divs(info
[i
].bmap
, info
[j
].bmap
);
3779 return isl_change_error
;
3781 return coalesce_local_pair(i
, j
, info
);
3783 ctx
= isl_basic_map_get_ctx(info
[i
].bmap
);
3784 preserve
= isl_options_get_coalesce_preserve_locals(ctx
);
3785 if (!preserve
&& info
[i
].bmap
->n_div
== info
[j
].bmap
->n_div
) {
3786 change
= coalesce_local_pair(i
, j
, info
);
3787 if (change
!= isl_change_none
)
3791 change
= coalesce_divs(i
, j
, info
);
3792 if (change
!= isl_change_none
)
3795 return check_coalesce_eq(i
, j
, info
);
3798 /* Return the maximum of "a" and "b".
3800 static int isl_max(int a
, int b
)
3802 return a
> b
? a
: b
;
3805 /* Pairwise coalesce the basic maps in the range [start1, end1[ of "info"
3806 * with those in the range [start2, end2[, skipping basic maps
3807 * that have been removed (either before or within this function).
3809 * For each basic map i in the first range, we check if it can be coalesced
3810 * with respect to any previously considered basic map j in the second range.
3811 * If i gets dropped (because it was a subset of some j), then
3812 * we can move on to the next basic map.
3813 * If j gets dropped, we need to continue checking against the other
3814 * previously considered basic maps.
3815 * If the two basic maps got fused, then we recheck the fused basic map
3816 * against the previously considered basic maps, starting at i + 1
3817 * (even if start2 is greater than i + 1).
3819 static int coalesce_range(isl_ctx
*ctx
, struct isl_coalesce_info
*info
,
3820 int start1
, int end1
, int start2
, int end2
)
3824 for (i
= end1
- 1; i
>= start1
; --i
) {
3825 if (info
[i
].removed
)
3827 for (j
= isl_max(i
+ 1, start2
); j
< end2
; ++j
) {
3828 enum isl_change changed
;
3830 if (info
[j
].removed
)
3832 if (info
[i
].removed
)
3833 isl_die(ctx
, isl_error_internal
,
3834 "basic map unexpectedly removed",
3836 changed
= coalesce_pair(i
, j
, info
);
3838 case isl_change_error
:
3840 case isl_change_none
:
3841 case isl_change_drop_second
:
3843 case isl_change_drop_first
:
3846 case isl_change_fuse
:
3856 /* Pairwise coalesce the basic maps described by the "n" elements of "info".
3858 * We consider groups of basic maps that live in the same apparent
3859 * affine hull and we first coalesce within such a group before we
3860 * coalesce the elements in the group with elements of previously
3861 * considered groups. If a fuse happens during the second phase,
3862 * then we also reconsider the elements within the group.
3864 static int coalesce(isl_ctx
*ctx
, int n
, struct isl_coalesce_info
*info
)
3868 for (end
= n
; end
> 0; end
= start
) {
3870 while (start
>= 1 &&
3871 info
[start
- 1].hull_hash
== info
[start
].hull_hash
)
3873 if (coalesce_range(ctx
, info
, start
, end
, start
, end
) < 0)
3875 if (coalesce_range(ctx
, info
, start
, end
, end
, n
) < 0)
3882 /* Update the basic maps in "map" based on the information in "info".
3883 * In particular, remove the basic maps that have been marked removed and
3884 * update the others based on the information in the corresponding tableau.
3885 * Since we detected implicit equalities without calling
3886 * isl_basic_map_gauss, we need to do it now.
3887 * Also call isl_basic_map_simplify if we may have lost the definition
3888 * of one or more integer divisions.
3890 static __isl_give isl_map
*update_basic_maps(__isl_take isl_map
*map
,
3891 int n
, struct isl_coalesce_info
*info
)
3898 for (i
= n
- 1; i
>= 0; --i
) {
3899 if (info
[i
].removed
) {
3900 isl_basic_map_free(map
->p
[i
]);
3901 if (i
!= map
->n
- 1)
3902 map
->p
[i
] = map
->p
[map
->n
- 1];
3907 info
[i
].bmap
= isl_basic_map_update_from_tab(info
[i
].bmap
,
3909 info
[i
].bmap
= isl_basic_map_gauss(info
[i
].bmap
, NULL
);
3910 if (info
[i
].simplify
)
3911 info
[i
].bmap
= isl_basic_map_simplify(info
[i
].bmap
);
3912 info
[i
].bmap
= isl_basic_map_finalize(info
[i
].bmap
);
3914 return isl_map_free(map
);
3915 ISL_F_SET(info
[i
].bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
3916 ISL_F_SET(info
[i
].bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
3917 isl_basic_map_free(map
->p
[i
]);
3918 map
->p
[i
] = info
[i
].bmap
;
3919 info
[i
].bmap
= NULL
;
3925 /* For each pair of basic maps in the map, check if the union of the two
3926 * can be represented by a single basic map.
3927 * If so, replace the pair by the single basic map and start over.
3929 * We factor out any (hidden) common factor from the constraint
3930 * coefficients to improve the detection of adjacent constraints.
3932 * Since we are constructing the tableaus of the basic maps anyway,
3933 * we exploit them to detect implicit equalities and redundant constraints.
3934 * This also helps the coalescing as it can ignore the redundant constraints.
3935 * In order to avoid confusion, we make all implicit equalities explicit
3936 * in the basic maps. We don't call isl_basic_map_gauss, though,
3937 * as that may affect the number of constraints.
3938 * This means that we have to call isl_basic_map_gauss at the end
3939 * of the computation (in update_basic_maps and in clear) to ensure that
3940 * the basic maps are not left in an unexpected state.
3941 * For each basic map, we also compute the hash of the apparent affine hull
3942 * for use in coalesce.
3944 __isl_give isl_map
*isl_map_coalesce(__isl_take isl_map
*map
)
3949 struct isl_coalesce_info
*info
= NULL
;
3951 map
= isl_map_remove_empty_parts(map
);
3958 ctx
= isl_map_get_ctx(map
);
3959 map
= isl_map_sort_divs(map
);
3960 map
= isl_map_cow(map
);
3967 info
= isl_calloc_array(map
->ctx
, struct isl_coalesce_info
, n
);
3971 for (i
= 0; i
< map
->n
; ++i
) {
3972 map
->p
[i
] = isl_basic_map_reduce_coefficients(map
->p
[i
]);
3975 info
[i
].bmap
= isl_basic_map_copy(map
->p
[i
]);
3976 info
[i
].tab
= isl_tab_from_basic_map(info
[i
].bmap
, 0);
3979 if (!ISL_F_ISSET(info
[i
].bmap
, ISL_BASIC_MAP_NO_IMPLICIT
))
3980 if (isl_tab_detect_implicit_equalities(info
[i
].tab
) < 0)
3982 info
[i
].bmap
= isl_tab_make_equalities_explicit(info
[i
].tab
,
3986 if (!ISL_F_ISSET(info
[i
].bmap
, ISL_BASIC_MAP_NO_REDUNDANT
))
3987 if (isl_tab_detect_redundant(info
[i
].tab
) < 0)
3989 if (coalesce_info_set_hull_hash(&info
[i
]) < 0)
3992 for (i
= map
->n
- 1; i
>= 0; --i
)
3993 if (info
[i
].tab
->empty
)
3996 if (coalesce(ctx
, n
, info
) < 0)
3999 map
= update_basic_maps(map
, n
, info
);
4001 clear_coalesce_info(n
, info
);
4005 clear_coalesce_info(n
, info
);
4010 /* For each pair of basic sets in the set, check if the union of the two
4011 * can be represented by a single basic set.
4012 * If so, replace the pair by the single basic set and start over.
4014 struct isl_set
*isl_set_coalesce(struct isl_set
*set
)
4016 return set_from_map(isl_map_coalesce(set_to_map(set
)));