2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2012-2013 Ecole Normale Superieure
6 * Use of this software is governed by the MIT license
8 * Written by Sven Verdoolaege, K.U.Leuven, Departement
9 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
10 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
11 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
12 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
15 #include "isl_map_private.h"
17 #include <isl/options.h>
19 #include <isl_mat_private.h>
20 #include <isl_local_space_private.h>
21 #include <isl_vec_private.h>
23 #define STATUS_ERROR -1
24 #define STATUS_REDUNDANT 1
25 #define STATUS_VALID 2
26 #define STATUS_SEPARATE 3
28 #define STATUS_ADJ_EQ 5
29 #define STATUS_ADJ_INEQ 6
31 static int status_in(isl_int
*ineq
, struct isl_tab
*tab
)
33 enum isl_ineq_type type
= isl_tab_ineq_type(tab
, ineq
);
36 case isl_ineq_error
: return STATUS_ERROR
;
37 case isl_ineq_redundant
: return STATUS_VALID
;
38 case isl_ineq_separate
: return STATUS_SEPARATE
;
39 case isl_ineq_cut
: return STATUS_CUT
;
40 case isl_ineq_adj_eq
: return STATUS_ADJ_EQ
;
41 case isl_ineq_adj_ineq
: return STATUS_ADJ_INEQ
;
45 /* Compute the position of the equalities of basic map "bmap_i"
46 * with respect to the basic map represented by "tab_j".
47 * The resulting array has twice as many entries as the number
48 * of equalities corresponding to the two inequalties to which
49 * each equality corresponds.
51 static int *eq_status_in(__isl_keep isl_basic_map
*bmap_i
,
52 struct isl_tab
*tab_j
)
55 int *eq
= isl_calloc_array(bmap_i
->ctx
, int, 2 * bmap_i
->n_eq
);
61 dim
= isl_basic_map_total_dim(bmap_i
);
62 for (k
= 0; k
< bmap_i
->n_eq
; ++k
) {
63 for (l
= 0; l
< 2; ++l
) {
64 isl_seq_neg(bmap_i
->eq
[k
], bmap_i
->eq
[k
], 1+dim
);
65 eq
[2 * k
+ l
] = status_in(bmap_i
->eq
[k
], tab_j
);
66 if (eq
[2 * k
+ l
] == STATUS_ERROR
)
69 if (eq
[2 * k
] == STATUS_SEPARATE
||
70 eq
[2 * k
+ 1] == STATUS_SEPARATE
)
80 /* Compute the position of the inequalities of basic map "bmap_i"
81 * (also represented by "tab_i", if not NULL) with respect to the basic map
82 * represented by "tab_j".
84 static int *ineq_status_in(__isl_keep isl_basic_map
*bmap_i
,
85 struct isl_tab
*tab_i
, struct isl_tab
*tab_j
)
88 unsigned n_eq
= bmap_i
->n_eq
;
89 int *ineq
= isl_calloc_array(bmap_i
->ctx
, int, bmap_i
->n_ineq
);
94 for (k
= 0; k
< bmap_i
->n_ineq
; ++k
) {
95 if (tab_i
&& isl_tab_is_redundant(tab_i
, n_eq
+ k
)) {
96 ineq
[k
] = STATUS_REDUNDANT
;
99 ineq
[k
] = status_in(bmap_i
->ineq
[k
], tab_j
);
100 if (ineq
[k
] == STATUS_ERROR
)
102 if (ineq
[k
] == STATUS_SEPARATE
)
112 static int any(int *con
, unsigned len
, int status
)
116 for (i
= 0; i
< len
; ++i
)
117 if (con
[i
] == status
)
122 static int count(int *con
, unsigned len
, int status
)
127 for (i
= 0; i
< len
; ++i
)
128 if (con
[i
] == status
)
133 static int all(int *con
, unsigned len
, int status
)
137 for (i
= 0; i
< len
; ++i
) {
138 if (con
[i
] == STATUS_REDUNDANT
)
140 if (con
[i
] != status
)
146 static void drop(struct isl_map
*map
, int i
, struct isl_tab
**tabs
)
148 isl_basic_map_free(map
->p
[i
]);
149 isl_tab_free(tabs
[i
]);
151 if (i
!= map
->n
- 1) {
152 map
->p
[i
] = map
->p
[map
->n
- 1];
153 tabs
[i
] = tabs
[map
->n
- 1];
155 tabs
[map
->n
- 1] = NULL
;
159 /* Replace the pair of basic maps i and j by the basic map bounded
160 * by the valid constraints in both basic maps and the constraints
161 * in extra (if not NULL).
163 static int fuse(struct isl_map
*map
, int i
, int j
,
164 struct isl_tab
**tabs
, int *eq_i
, int *ineq_i
, int *eq_j
, int *ineq_j
,
165 __isl_keep isl_mat
*extra
)
168 struct isl_basic_map
*fused
= NULL
;
169 struct isl_tab
*fused_tab
= NULL
;
170 unsigned total
= isl_basic_map_total_dim(map
->p
[i
]);
171 unsigned extra_rows
= extra
? extra
->n_row
: 0;
173 fused
= isl_basic_map_alloc_space(isl_space_copy(map
->p
[i
]->dim
),
175 map
->p
[i
]->n_eq
+ map
->p
[j
]->n_eq
,
176 map
->p
[i
]->n_ineq
+ map
->p
[j
]->n_ineq
+ extra_rows
);
180 for (k
= 0; k
< map
->p
[i
]->n_eq
; ++k
) {
181 if (eq_i
&& (eq_i
[2 * k
] != STATUS_VALID
||
182 eq_i
[2 * k
+ 1] != STATUS_VALID
))
184 l
= isl_basic_map_alloc_equality(fused
);
187 isl_seq_cpy(fused
->eq
[l
], map
->p
[i
]->eq
[k
], 1 + total
);
190 for (k
= 0; k
< map
->p
[j
]->n_eq
; ++k
) {
191 if (eq_j
&& (eq_j
[2 * k
] != STATUS_VALID
||
192 eq_j
[2 * k
+ 1] != STATUS_VALID
))
194 l
= isl_basic_map_alloc_equality(fused
);
197 isl_seq_cpy(fused
->eq
[l
], map
->p
[j
]->eq
[k
], 1 + total
);
200 for (k
= 0; k
< map
->p
[i
]->n_ineq
; ++k
) {
201 if (ineq_i
[k
] != STATUS_VALID
)
203 l
= isl_basic_map_alloc_inequality(fused
);
206 isl_seq_cpy(fused
->ineq
[l
], map
->p
[i
]->ineq
[k
], 1 + total
);
209 for (k
= 0; k
< map
->p
[j
]->n_ineq
; ++k
) {
210 if (ineq_j
[k
] != STATUS_VALID
)
212 l
= isl_basic_map_alloc_inequality(fused
);
215 isl_seq_cpy(fused
->ineq
[l
], map
->p
[j
]->ineq
[k
], 1 + total
);
218 for (k
= 0; k
< map
->p
[i
]->n_div
; ++k
) {
219 int l
= isl_basic_map_alloc_div(fused
);
222 isl_seq_cpy(fused
->div
[l
], map
->p
[i
]->div
[k
], 1 + 1 + total
);
225 for (k
= 0; k
< extra_rows
; ++k
) {
226 l
= isl_basic_map_alloc_inequality(fused
);
229 isl_seq_cpy(fused
->ineq
[l
], extra
->row
[k
], 1 + total
);
232 fused
= isl_basic_map_gauss(fused
, NULL
);
233 ISL_F_SET(fused
, ISL_BASIC_MAP_FINAL
);
234 if (ISL_F_ISSET(map
->p
[i
], ISL_BASIC_MAP_RATIONAL
) &&
235 ISL_F_ISSET(map
->p
[j
], ISL_BASIC_MAP_RATIONAL
))
236 ISL_F_SET(fused
, ISL_BASIC_MAP_RATIONAL
);
238 fused_tab
= isl_tab_from_basic_map(fused
, 0);
239 if (isl_tab_detect_redundant(fused_tab
) < 0)
242 isl_basic_map_free(map
->p
[i
]);
244 isl_tab_free(tabs
[i
]);
250 isl_tab_free(fused_tab
);
251 isl_basic_map_free(fused
);
255 /* Given a pair of basic maps i and j such that all constraints are either
256 * "valid" or "cut", check if the facets corresponding to the "cut"
257 * constraints of i lie entirely within basic map j.
258 * If so, replace the pair by the basic map consisting of the valid
259 * constraints in both basic maps.
260 * Checking whether the facet lies entirely within basic map j
261 * is performed by checking whether the constraints of basic map j
262 * are valid for the facet. These tests are performed on a rational
263 * tableau to avoid the theoretical possibility that a constraint
264 * that was considered to be a cut constraint for the entire basic map i
265 * happens to be considered to be a valid constraint for the facet,
266 * even though it cuts off the same rational points.
268 * To see that we are not introducing any extra points, call the
269 * two basic maps A and B and the resulting map U and let x
270 * be an element of U \setminus ( A \cup B ).
271 * A line connecting x with an element of A \cup B meets a facet F
272 * of either A or B. Assume it is a facet of B and let c_1 be
273 * the corresponding facet constraint. We have c_1(x) < 0 and
274 * so c_1 is a cut constraint. This implies that there is some
275 * (possibly rational) point x' satisfying the constraints of A
276 * and the opposite of c_1 as otherwise c_1 would have been marked
277 * valid for A. The line connecting x and x' meets a facet of A
278 * in a (possibly rational) point that also violates c_1, but this
279 * is impossible since all cut constraints of B are valid for all
281 * In case F is a facet of A rather than B, then we can apply the
282 * above reasoning to find a facet of B separating x from A \cup B first.
284 static int check_facets(struct isl_map
*map
, int i
, int j
,
285 struct isl_tab
**tabs
, int *ineq_i
, int *ineq_j
)
288 struct isl_tab_undo
*snap
, *snap2
;
289 unsigned n_eq
= map
->p
[i
]->n_eq
;
291 snap
= isl_tab_snap(tabs
[i
]);
292 if (isl_tab_mark_rational(tabs
[i
]) < 0)
294 snap2
= isl_tab_snap(tabs
[i
]);
296 for (k
= 0; k
< map
->p
[i
]->n_ineq
; ++k
) {
297 if (ineq_i
[k
] != STATUS_CUT
)
299 if (isl_tab_select_facet(tabs
[i
], n_eq
+ k
) < 0)
301 for (l
= 0; l
< map
->p
[j
]->n_ineq
; ++l
) {
303 if (ineq_j
[l
] != STATUS_CUT
)
305 stat
= status_in(map
->p
[j
]->ineq
[l
], tabs
[i
]);
306 if (stat
!= STATUS_VALID
)
309 if (isl_tab_rollback(tabs
[i
], snap2
) < 0)
311 if (l
< map
->p
[j
]->n_ineq
)
315 if (k
< map
->p
[i
]->n_ineq
) {
316 if (isl_tab_rollback(tabs
[i
], snap
) < 0)
320 return fuse(map
, i
, j
, tabs
, NULL
, ineq_i
, NULL
, ineq_j
, NULL
);
323 /* Check if basic map "i" contains the basic map represented
324 * by the tableau "tab".
326 static int contains(struct isl_map
*map
, int i
, int *ineq_i
,
332 dim
= isl_basic_map_total_dim(map
->p
[i
]);
333 for (k
= 0; k
< map
->p
[i
]->n_eq
; ++k
) {
334 for (l
= 0; l
< 2; ++l
) {
336 isl_seq_neg(map
->p
[i
]->eq
[k
], map
->p
[i
]->eq
[k
], 1+dim
);
337 stat
= status_in(map
->p
[i
]->eq
[k
], tab
);
338 if (stat
!= STATUS_VALID
)
343 for (k
= 0; k
< map
->p
[i
]->n_ineq
; ++k
) {
345 if (ineq_i
[k
] == STATUS_REDUNDANT
)
347 stat
= status_in(map
->p
[i
]->ineq
[k
], tab
);
348 if (stat
!= STATUS_VALID
)
354 /* Basic map "i" has an inequality (say "k") that is adjacent
355 * to some inequality of basic map "j". All the other inequalities
357 * Check if basic map "j" forms an extension of basic map "i".
359 * Note that this function is only called if some of the equalities or
360 * inequalities of basic map "j" do cut basic map "i". The function is
361 * correct even if there are no such cut constraints, but in that case
362 * the additional checks performed by this function are overkill.
364 * In particular, we replace constraint k, say f >= 0, by constraint
365 * f <= -1, add the inequalities of "j" that are valid for "i"
366 * and check if the result is a subset of basic map "j".
367 * If so, then we know that this result is exactly equal to basic map "j"
368 * since all its constraints are valid for basic map "j".
369 * By combining the valid constraints of "i" (all equalities and all
370 * inequalities except "k") and the valid constraints of "j" we therefore
371 * obtain a basic map that is equal to their union.
372 * In this case, there is no need to perform a rollback of the tableau
373 * since it is going to be destroyed in fuse().
379 * |_______| _ |_________\
391 static int is_adj_ineq_extension(__isl_keep isl_map
*map
, int i
, int j
,
392 struct isl_tab
**tabs
, int *eq_i
, int *ineq_i
, int *eq_j
, int *ineq_j
)
395 struct isl_tab_undo
*snap
;
396 unsigned n_eq
= map
->p
[i
]->n_eq
;
397 unsigned total
= isl_basic_map_total_dim(map
->p
[i
]);
400 if (isl_tab_extend_cons(tabs
[i
], 1 + map
->p
[j
]->n_ineq
) < 0)
403 for (k
= 0; k
< map
->p
[i
]->n_ineq
; ++k
)
404 if (ineq_i
[k
] == STATUS_ADJ_INEQ
)
406 if (k
>= map
->p
[i
]->n_ineq
)
407 isl_die(isl_map_get_ctx(map
), isl_error_internal
,
408 "ineq_i should have exactly one STATUS_ADJ_INEQ",
411 snap
= isl_tab_snap(tabs
[i
]);
413 if (isl_tab_unrestrict(tabs
[i
], n_eq
+ k
) < 0)
416 isl_seq_neg(map
->p
[i
]->ineq
[k
], map
->p
[i
]->ineq
[k
], 1 + total
);
417 isl_int_sub_ui(map
->p
[i
]->ineq
[k
][0], map
->p
[i
]->ineq
[k
][0], 1);
418 r
= isl_tab_add_ineq(tabs
[i
], map
->p
[i
]->ineq
[k
]);
419 isl_seq_neg(map
->p
[i
]->ineq
[k
], map
->p
[i
]->ineq
[k
], 1 + total
);
420 isl_int_sub_ui(map
->p
[i
]->ineq
[k
][0], map
->p
[i
]->ineq
[k
][0], 1);
424 for (k
= 0; k
< map
->p
[j
]->n_ineq
; ++k
) {
425 if (ineq_j
[k
] != STATUS_VALID
)
427 if (isl_tab_add_ineq(tabs
[i
], map
->p
[j
]->ineq
[k
]) < 0)
431 if (contains(map
, j
, ineq_j
, tabs
[i
]))
432 return fuse(map
, i
, j
, tabs
, eq_i
, ineq_i
, eq_j
, ineq_j
, NULL
);
434 if (isl_tab_rollback(tabs
[i
], snap
) < 0)
441 /* Both basic maps have at least one inequality with and adjacent
442 * (but opposite) inequality in the other basic map.
443 * Check that there are no cut constraints and that there is only
444 * a single pair of adjacent inequalities.
445 * If so, we can replace the pair by a single basic map described
446 * by all but the pair of adjacent inequalities.
447 * Any additional points introduced lie strictly between the two
448 * adjacent hyperplanes and can therefore be integral.
457 * The test for a single pair of adjancent inequalities is important
458 * for avoiding the combination of two basic maps like the following
468 * If there are some cut constraints on one side, then we may
469 * still be able to fuse the two basic maps, but we need to perform
470 * some additional checks in is_adj_ineq_extension.
472 static int check_adj_ineq(struct isl_map
*map
, int i
, int j
,
473 struct isl_tab
**tabs
, int *eq_i
, int *ineq_i
, int *eq_j
, int *ineq_j
)
475 int count_i
, count_j
;
478 count_i
= count(ineq_i
, map
->p
[i
]->n_ineq
, STATUS_ADJ_INEQ
);
479 count_j
= count(ineq_j
, map
->p
[j
]->n_ineq
, STATUS_ADJ_INEQ
);
481 if (count_i
!= 1 && count_j
!= 1)
484 cut_i
= any(eq_i
, 2 * map
->p
[i
]->n_eq
, STATUS_CUT
) ||
485 any(ineq_i
, map
->p
[i
]->n_ineq
, STATUS_CUT
);
486 cut_j
= any(eq_j
, 2 * map
->p
[j
]->n_eq
, STATUS_CUT
) ||
487 any(ineq_j
, map
->p
[j
]->n_ineq
, STATUS_CUT
);
489 if (!cut_i
&& !cut_j
&& count_i
== 1 && count_j
== 1)
490 return fuse(map
, i
, j
, tabs
, NULL
, ineq_i
, NULL
, ineq_j
, NULL
);
492 if (count_i
== 1 && !cut_i
)
493 return is_adj_ineq_extension(map
, i
, j
, tabs
,
494 eq_i
, ineq_i
, eq_j
, ineq_j
);
496 if (count_j
== 1 && !cut_j
)
497 return is_adj_ineq_extension(map
, j
, i
, tabs
,
498 eq_j
, ineq_j
, eq_i
, ineq_i
);
503 /* Basic map "i" has an inequality "k" that is adjacent to some equality
504 * of basic map "j". All the other inequalities are valid for "j".
505 * Check if basic map "j" forms an extension of basic map "i".
507 * In particular, we relax constraint "k", compute the corresponding
508 * facet and check whether it is included in the other basic map.
509 * If so, we know that relaxing the constraint extends the basic
510 * map with exactly the other basic map (we already know that this
511 * other basic map is included in the extension, because there
512 * were no "cut" inequalities in "i") and we can replace the
513 * two basic maps by this extension.
521 static int is_adj_eq_extension(struct isl_map
*map
, int i
, int j
, int k
,
522 struct isl_tab
**tabs
, int *eq_i
, int *ineq_i
, int *eq_j
, int *ineq_j
)
526 struct isl_tab_undo
*snap
, *snap2
;
527 unsigned n_eq
= map
->p
[i
]->n_eq
;
529 if (isl_tab_is_equality(tabs
[i
], n_eq
+ k
))
532 snap
= isl_tab_snap(tabs
[i
]);
533 if (isl_tab_relax(tabs
[i
], n_eq
+ k
) < 0)
535 snap2
= isl_tab_snap(tabs
[i
]);
536 if (isl_tab_select_facet(tabs
[i
], n_eq
+ k
) < 0)
538 super
= contains(map
, j
, ineq_j
, tabs
[i
]);
540 if (isl_tab_rollback(tabs
[i
], snap2
) < 0)
542 map
->p
[i
] = isl_basic_map_cow(map
->p
[i
]);
545 isl_int_add_ui(map
->p
[i
]->ineq
[k
][0], map
->p
[i
]->ineq
[k
][0], 1);
546 ISL_F_SET(map
->p
[i
], ISL_BASIC_MAP_FINAL
);
550 if (isl_tab_rollback(tabs
[i
], snap
) < 0)
556 /* Data structure that keeps track of the wrapping constraints
557 * and of information to bound the coefficients of those constraints.
559 * bound is set if we want to apply a bound on the coefficients
560 * mat contains the wrapping constraints
561 * max is the bound on the coefficients (if bound is set)
569 /* Update wraps->max to be greater than or equal to the coefficients
570 * in the equalities and inequalities of bmap that can be removed if we end up
573 static void wraps_update_max(struct isl_wraps
*wraps
,
574 __isl_keep isl_basic_map
*bmap
, int *eq
, int *ineq
)
578 unsigned total
= isl_basic_map_total_dim(bmap
);
582 for (k
= 0; k
< bmap
->n_eq
; ++k
) {
583 if (eq
[2 * k
] == STATUS_VALID
&&
584 eq
[2 * k
+ 1] == STATUS_VALID
)
586 isl_seq_abs_max(bmap
->eq
[k
] + 1, total
, &max_k
);
587 if (isl_int_abs_gt(max_k
, wraps
->max
))
588 isl_int_set(wraps
->max
, max_k
);
591 for (k
= 0; k
< bmap
->n_ineq
; ++k
) {
592 if (ineq
[k
] == STATUS_VALID
|| ineq
[k
] == STATUS_REDUNDANT
)
594 isl_seq_abs_max(bmap
->ineq
[k
] + 1, total
, &max_k
);
595 if (isl_int_abs_gt(max_k
, wraps
->max
))
596 isl_int_set(wraps
->max
, max_k
);
599 isl_int_clear(max_k
);
602 /* Initialize the isl_wraps data structure.
603 * If we want to bound the coefficients of the wrapping constraints,
604 * we set wraps->max to the largest coefficient
605 * in the equalities and inequalities that can be removed if we end up
608 static void wraps_init(struct isl_wraps
*wraps
, __isl_take isl_mat
*mat
,
609 __isl_keep isl_map
*map
, int i
, int j
,
610 int *eq_i
, int *ineq_i
, int *eq_j
, int *ineq_j
)
618 ctx
= isl_mat_get_ctx(mat
);
619 wraps
->bound
= isl_options_get_coalesce_bounded_wrapping(ctx
);
622 isl_int_init(wraps
->max
);
623 isl_int_set_si(wraps
->max
, 0);
624 wraps_update_max(wraps
, map
->p
[i
], eq_i
, ineq_i
);
625 wraps_update_max(wraps
, map
->p
[j
], eq_j
, ineq_j
);
628 /* Free the contents of the isl_wraps data structure.
630 static void wraps_free(struct isl_wraps
*wraps
)
632 isl_mat_free(wraps
->mat
);
634 isl_int_clear(wraps
->max
);
637 /* Is the wrapping constraint in row "row" allowed?
639 * If wraps->bound is set, we check that none of the coefficients
640 * is greater than wraps->max.
642 static int allow_wrap(struct isl_wraps
*wraps
, int row
)
649 for (i
= 1; i
< wraps
->mat
->n_col
; ++i
)
650 if (isl_int_abs_gt(wraps
->mat
->row
[row
][i
], wraps
->max
))
656 /* For each non-redundant constraint in "bmap" (as determined by "tab"),
657 * wrap the constraint around "bound" such that it includes the whole
658 * set "set" and append the resulting constraint to "wraps".
659 * "wraps" is assumed to have been pre-allocated to the appropriate size.
660 * wraps->n_row is the number of actual wrapped constraints that have
662 * If any of the wrapping problems results in a constraint that is
663 * identical to "bound", then this means that "set" is unbounded in such
664 * way that no wrapping is possible. If this happens then wraps->n_row
666 * Similarly, if we want to bound the coefficients of the wrapping
667 * constraints and a newly added wrapping constraint does not
668 * satisfy the bound, then wraps->n_row is also reset to zero.
670 static int add_wraps(struct isl_wraps
*wraps
, __isl_keep isl_basic_map
*bmap
,
671 struct isl_tab
*tab
, isl_int
*bound
, __isl_keep isl_set
*set
)
675 unsigned total
= isl_basic_map_total_dim(bmap
);
677 w
= wraps
->mat
->n_row
;
679 for (l
= 0; l
< bmap
->n_ineq
; ++l
) {
680 if (isl_seq_is_neg(bound
, bmap
->ineq
[l
], 1 + total
))
682 if (isl_seq_eq(bound
, bmap
->ineq
[l
], 1 + total
))
684 if (isl_tab_is_redundant(tab
, bmap
->n_eq
+ l
))
687 isl_seq_cpy(wraps
->mat
->row
[w
], bound
, 1 + total
);
688 if (!isl_set_wrap_facet(set
, wraps
->mat
->row
[w
], bmap
->ineq
[l
]))
690 if (isl_seq_eq(wraps
->mat
->row
[w
], bound
, 1 + total
))
692 if (!allow_wrap(wraps
, w
))
696 for (l
= 0; l
< bmap
->n_eq
; ++l
) {
697 if (isl_seq_is_neg(bound
, bmap
->eq
[l
], 1 + total
))
699 if (isl_seq_eq(bound
, bmap
->eq
[l
], 1 + total
))
702 isl_seq_cpy(wraps
->mat
->row
[w
], bound
, 1 + total
);
703 isl_seq_neg(wraps
->mat
->row
[w
+ 1], bmap
->eq
[l
], 1 + total
);
704 if (!isl_set_wrap_facet(set
, wraps
->mat
->row
[w
],
705 wraps
->mat
->row
[w
+ 1]))
707 if (isl_seq_eq(wraps
->mat
->row
[w
], bound
, 1 + total
))
709 if (!allow_wrap(wraps
, w
))
713 isl_seq_cpy(wraps
->mat
->row
[w
], bound
, 1 + total
);
714 if (!isl_set_wrap_facet(set
, wraps
->mat
->row
[w
], bmap
->eq
[l
]))
716 if (isl_seq_eq(wraps
->mat
->row
[w
], bound
, 1 + total
))
718 if (!allow_wrap(wraps
, w
))
723 wraps
->mat
->n_row
= w
;
726 wraps
->mat
->n_row
= 0;
730 /* Check if the constraints in "wraps" from "first" until the last
731 * are all valid for the basic set represented by "tab".
732 * If not, wraps->n_row is set to zero.
734 static int check_wraps(__isl_keep isl_mat
*wraps
, int first
,
739 for (i
= first
; i
< wraps
->n_row
; ++i
) {
740 enum isl_ineq_type type
;
741 type
= isl_tab_ineq_type(tab
, wraps
->row
[i
]);
742 if (type
== isl_ineq_error
)
744 if (type
== isl_ineq_redundant
)
753 /* Return a set that corresponds to the non-redundant constraints
754 * (as recorded in tab) of bmap.
756 * It's important to remove the redundant constraints as some
757 * of the other constraints may have been modified after the
758 * constraints were marked redundant.
759 * In particular, a constraint may have been relaxed.
760 * Redundant constraints are ignored when a constraint is relaxed
761 * and should therefore continue to be ignored ever after.
762 * Otherwise, the relaxation might be thwarted by some of
765 * Update the underlying set to ensure that the dimension doesn't change.
766 * Otherwise the integer divisions could get dropped if the tab
767 * turns out to be empty.
769 static __isl_give isl_set
*set_from_updated_bmap(__isl_keep isl_basic_map
*bmap
,
774 bmap
= isl_basic_map_copy(bmap
);
775 bset
= isl_basic_map_underlying_set(bmap
);
776 bset
= isl_basic_set_cow(bset
);
777 bset
= isl_basic_set_update_from_tab(bset
, tab
);
778 return isl_set_from_basic_set(bset
);
781 /* Given a basic set i with a constraint k that is adjacent to
782 * basic set j, check if we can wrap
783 * both the facet corresponding to k and basic map j
784 * around their ridges to include the other set.
785 * If so, replace the pair of basic sets by their union.
787 * All constraints of i (except k) are assumed to be valid for j.
788 * This means that there is no real need to wrap the ridges of
789 * the faces of basic map i around basic map j but since we do,
790 * we have to check that the resulting wrapping constraints are valid for i.
799 static int can_wrap_in_facet(struct isl_map
*map
, int i
, int j
, int k
,
800 struct isl_tab
**tabs
, int *eq_i
, int *ineq_i
, int *eq_j
, int *ineq_j
)
803 struct isl_wraps wraps
;
805 struct isl_set
*set_i
= NULL
;
806 struct isl_set
*set_j
= NULL
;
807 struct isl_vec
*bound
= NULL
;
808 unsigned total
= isl_basic_map_total_dim(map
->p
[i
]);
809 struct isl_tab_undo
*snap
;
812 set_i
= set_from_updated_bmap(map
->p
[i
], tabs
[i
]);
813 set_j
= set_from_updated_bmap(map
->p
[j
], tabs
[j
]);
814 mat
= isl_mat_alloc(map
->ctx
, 2 * (map
->p
[i
]->n_eq
+ map
->p
[j
]->n_eq
) +
815 map
->p
[i
]->n_ineq
+ map
->p
[j
]->n_ineq
,
817 wraps_init(&wraps
, mat
, map
, i
, j
, eq_i
, ineq_i
, eq_j
, ineq_j
);
818 bound
= isl_vec_alloc(map
->ctx
, 1 + total
);
819 if (!set_i
|| !set_j
|| !wraps
.mat
|| !bound
)
822 isl_seq_cpy(bound
->el
, map
->p
[i
]->ineq
[k
], 1 + total
);
823 isl_int_add_ui(bound
->el
[0], bound
->el
[0], 1);
825 isl_seq_cpy(wraps
.mat
->row
[0], bound
->el
, 1 + total
);
826 wraps
.mat
->n_row
= 1;
828 if (add_wraps(&wraps
, map
->p
[j
], tabs
[j
], bound
->el
, set_i
) < 0)
830 if (!wraps
.mat
->n_row
)
833 snap
= isl_tab_snap(tabs
[i
]);
835 if (isl_tab_select_facet(tabs
[i
], map
->p
[i
]->n_eq
+ k
) < 0)
837 if (isl_tab_detect_redundant(tabs
[i
]) < 0)
840 isl_seq_neg(bound
->el
, map
->p
[i
]->ineq
[k
], 1 + total
);
842 n
= wraps
.mat
->n_row
;
843 if (add_wraps(&wraps
, map
->p
[i
], tabs
[i
], bound
->el
, set_j
) < 0)
846 if (isl_tab_rollback(tabs
[i
], snap
) < 0)
848 if (check_wraps(wraps
.mat
, n
, tabs
[i
]) < 0)
850 if (!wraps
.mat
->n_row
)
853 changed
= fuse(map
, i
, j
, tabs
, eq_i
, ineq_i
, eq_j
, ineq_j
, wraps
.mat
);
872 /* Set the is_redundant property of the "n" constraints in "cuts",
874 * This is a fairly tricky operation as it bypasses isl_tab.c.
875 * The reason we want to temporarily mark some constraints redundant
876 * is that we want to ignore them in add_wraps.
878 * Initially all cut constraints are non-redundant, but the
879 * selection of a facet right before the call to this function
880 * may have made some of them redundant.
881 * Likewise, the same constraints are marked non-redundant
882 * in the second call to this function, before they are officially
883 * made non-redundant again in the subsequent rollback.
885 static void set_is_redundant(struct isl_tab
*tab
, unsigned n_eq
,
886 int *cuts
, int n
, int k
, int v
)
890 for (l
= 0; l
< n
; ++l
) {
893 tab
->con
[n_eq
+ cuts
[l
]].is_redundant
= v
;
897 /* Given a pair of basic maps i and j such that j sticks out
898 * of i at n cut constraints, each time by at most one,
899 * try to compute wrapping constraints and replace the two
900 * basic maps by a single basic map.
901 * The other constraints of i are assumed to be valid for j.
903 * The facets of i corresponding to the cut constraints are
904 * wrapped around their ridges, except those ridges determined
905 * by any of the other cut constraints.
906 * The intersections of cut constraints need to be ignored
907 * as the result of wrapping one cut constraint around another
908 * would result in a constraint cutting the union.
909 * In each case, the facets are wrapped to include the union
910 * of the two basic maps.
912 * The pieces of j that lie at an offset of exactly one from
913 * one of the cut constraints of i are wrapped around their edges.
914 * Here, there is no need to ignore intersections because we
915 * are wrapping around the union of the two basic maps.
917 * If any wrapping fails, i.e., if we cannot wrap to touch
918 * the union, then we give up.
919 * Otherwise, the pair of basic maps is replaced by their union.
921 static int wrap_in_facets(struct isl_map
*map
, int i
, int j
,
922 int *cuts
, int n
, struct isl_tab
**tabs
,
923 int *eq_i
, int *ineq_i
, int *eq_j
, int *ineq_j
)
926 struct isl_wraps wraps
;
929 isl_vec
*bound
= NULL
;
930 unsigned total
= isl_basic_map_total_dim(map
->p
[i
]);
933 struct isl_tab_undo
*snap_i
, *snap_j
;
935 if (isl_tab_extend_cons(tabs
[j
], 1) < 0)
938 max_wrap
= 2 * (map
->p
[i
]->n_eq
+ map
->p
[j
]->n_eq
) +
939 map
->p
[i
]->n_ineq
+ map
->p
[j
]->n_ineq
;
942 set
= isl_set_union(set_from_updated_bmap(map
->p
[i
], tabs
[i
]),
943 set_from_updated_bmap(map
->p
[j
], tabs
[j
]));
944 mat
= isl_mat_alloc(map
->ctx
, max_wrap
, 1 + total
);
945 wraps_init(&wraps
, mat
, map
, i
, j
, eq_i
, ineq_i
, eq_j
, ineq_j
);
946 bound
= isl_vec_alloc(map
->ctx
, 1 + total
);
947 if (!set
|| !wraps
.mat
|| !bound
)
950 snap_i
= isl_tab_snap(tabs
[i
]);
951 snap_j
= isl_tab_snap(tabs
[j
]);
953 wraps
.mat
->n_row
= 0;
955 for (k
= 0; k
< n
; ++k
) {
956 if (isl_tab_select_facet(tabs
[i
], map
->p
[i
]->n_eq
+ cuts
[k
]) < 0)
958 if (isl_tab_detect_redundant(tabs
[i
]) < 0)
960 set_is_redundant(tabs
[i
], map
->p
[i
]->n_eq
, cuts
, n
, k
, 1);
962 isl_seq_neg(bound
->el
, map
->p
[i
]->ineq
[cuts
[k
]], 1 + total
);
963 if (!tabs
[i
]->empty
&&
964 add_wraps(&wraps
, map
->p
[i
], tabs
[i
], bound
->el
, set
) < 0)
967 set_is_redundant(tabs
[i
], map
->p
[i
]->n_eq
, cuts
, n
, k
, 0);
968 if (isl_tab_rollback(tabs
[i
], snap_i
) < 0)
973 if (!wraps
.mat
->n_row
)
976 isl_seq_cpy(bound
->el
, map
->p
[i
]->ineq
[cuts
[k
]], 1 + total
);
977 isl_int_add_ui(bound
->el
[0], bound
->el
[0], 1);
978 if (isl_tab_add_eq(tabs
[j
], bound
->el
) < 0)
980 if (isl_tab_detect_redundant(tabs
[j
]) < 0)
983 if (!tabs
[j
]->empty
&&
984 add_wraps(&wraps
, map
->p
[j
], tabs
[j
], bound
->el
, set
) < 0)
987 if (isl_tab_rollback(tabs
[j
], snap_j
) < 0)
990 if (!wraps
.mat
->n_row
)
995 changed
= fuse(map
, i
, j
, tabs
,
996 eq_i
, ineq_i
, eq_j
, ineq_j
, wraps
.mat
);
1004 isl_vec_free(bound
);
1010 /* Given two basic sets i and j such that i has no cut equalities,
1011 * check if relaxing all the cut inequalities of i by one turns
1012 * them into valid constraint for j and check if we can wrap in
1013 * the bits that are sticking out.
1014 * If so, replace the pair by their union.
1016 * We first check if all relaxed cut inequalities of i are valid for j
1017 * and then try to wrap in the intersections of the relaxed cut inequalities
1020 * During this wrapping, we consider the points of j that lie at a distance
1021 * of exactly 1 from i. In particular, we ignore the points that lie in
1022 * between this lower-dimensional space and the basic map i.
1023 * We can therefore only apply this to integer maps.
1049 * Wrapping can fail if the result of wrapping one of the facets
1050 * around its edges does not produce any new facet constraint.
1051 * In particular, this happens when we try to wrap in unbounded sets.
1053 * _______________________________________________________________________
1057 * |_| |_________________________________________________________________
1060 * The following is not an acceptable result of coalescing the above two
1061 * sets as it includes extra integer points.
1062 * _______________________________________________________________________
1067 * \______________________________________________________________________
1069 static int can_wrap_in_set(struct isl_map
*map
, int i
, int j
,
1070 struct isl_tab
**tabs
, int *eq_i
, int *ineq_i
, int *eq_j
, int *ineq_j
)
1077 if (ISL_F_ISSET(map
->p
[i
], ISL_BASIC_MAP_RATIONAL
) ||
1078 ISL_F_ISSET(map
->p
[j
], ISL_BASIC_MAP_RATIONAL
))
1081 n
= count(ineq_i
, map
->p
[i
]->n_ineq
, STATUS_CUT
);
1085 cuts
= isl_alloc_array(map
->ctx
, int, n
);
1089 for (k
= 0, m
= 0; m
< n
; ++k
) {
1090 enum isl_ineq_type type
;
1092 if (ineq_i
[k
] != STATUS_CUT
)
1095 isl_int_add_ui(map
->p
[i
]->ineq
[k
][0], map
->p
[i
]->ineq
[k
][0], 1);
1096 type
= isl_tab_ineq_type(tabs
[j
], map
->p
[i
]->ineq
[k
]);
1097 isl_int_sub_ui(map
->p
[i
]->ineq
[k
][0], map
->p
[i
]->ineq
[k
][0], 1);
1098 if (type
== isl_ineq_error
)
1100 if (type
!= isl_ineq_redundant
)
1107 changed
= wrap_in_facets(map
, i
, j
, cuts
, n
, tabs
,
1108 eq_i
, ineq_i
, eq_j
, ineq_j
);
1118 /* Check if either i or j has only cut inequalities that can
1119 * be used to wrap in (a facet of) the other basic set.
1120 * if so, replace the pair by their union.
1122 static int check_wrap(struct isl_map
*map
, int i
, int j
,
1123 struct isl_tab
**tabs
, int *eq_i
, int *ineq_i
, int *eq_j
, int *ineq_j
)
1127 if (!any(eq_i
, 2 * map
->p
[i
]->n_eq
, STATUS_CUT
))
1128 changed
= can_wrap_in_set(map
, i
, j
, tabs
,
1129 eq_i
, ineq_i
, eq_j
, ineq_j
);
1133 if (!any(eq_j
, 2 * map
->p
[j
]->n_eq
, STATUS_CUT
))
1134 changed
= can_wrap_in_set(map
, j
, i
, tabs
,
1135 eq_j
, ineq_j
, eq_i
, ineq_i
);
1139 /* At least one of the basic maps has an equality that is adjacent
1140 * to inequality. Make sure that only one of the basic maps has
1141 * such an equality and that the other basic map has exactly one
1142 * inequality adjacent to an equality.
1143 * We call the basic map that has the inequality "i" and the basic
1144 * map that has the equality "j".
1145 * If "i" has any "cut" (in)equality, then relaxing the inequality
1146 * by one would not result in a basic map that contains the other
1149 static int check_adj_eq(struct isl_map
*map
, int i
, int j
,
1150 struct isl_tab
**tabs
, int *eq_i
, int *ineq_i
, int *eq_j
, int *ineq_j
)
1155 if (any(eq_i
, 2 * map
->p
[i
]->n_eq
, STATUS_ADJ_INEQ
) &&
1156 any(eq_j
, 2 * map
->p
[j
]->n_eq
, STATUS_ADJ_INEQ
))
1157 /* ADJ EQ TOO MANY */
1160 if (any(eq_i
, 2 * map
->p
[i
]->n_eq
, STATUS_ADJ_INEQ
))
1161 return check_adj_eq(map
, j
, i
, tabs
,
1162 eq_j
, ineq_j
, eq_i
, ineq_i
);
1164 /* j has an equality adjacent to an inequality in i */
1166 if (any(eq_i
, 2 * map
->p
[i
]->n_eq
, STATUS_CUT
))
1168 if (any(ineq_i
, map
->p
[i
]->n_ineq
, STATUS_CUT
))
1171 if (count(ineq_i
, map
->p
[i
]->n_ineq
, STATUS_ADJ_EQ
) != 1 ||
1172 any(ineq_j
, map
->p
[j
]->n_ineq
, STATUS_ADJ_EQ
) ||
1173 any(ineq_i
, map
->p
[i
]->n_ineq
, STATUS_ADJ_INEQ
) ||
1174 any(ineq_j
, map
->p
[j
]->n_ineq
, STATUS_ADJ_INEQ
))
1175 /* ADJ EQ TOO MANY */
1178 for (k
= 0; k
< map
->p
[i
]->n_ineq
; ++k
)
1179 if (ineq_i
[k
] == STATUS_ADJ_EQ
)
1182 changed
= is_adj_eq_extension(map
, i
, j
, k
, tabs
,
1183 eq_i
, ineq_i
, eq_j
, ineq_j
);
1187 if (count(eq_j
, 2 * map
->p
[j
]->n_eq
, STATUS_ADJ_INEQ
) != 1)
1190 changed
= can_wrap_in_facet(map
, i
, j
, k
, tabs
, eq_i
, ineq_i
, eq_j
, ineq_j
);
1195 /* The two basic maps lie on adjacent hyperplanes. In particular,
1196 * basic map "i" has an equality that lies parallel to basic map "j".
1197 * Check if we can wrap the facets around the parallel hyperplanes
1198 * to include the other set.
1200 * We perform basically the same operations as can_wrap_in_facet,
1201 * except that we don't need to select a facet of one of the sets.
1207 * We only allow one equality of "i" to be adjacent to an equality of "j"
1208 * to avoid coalescing
1210 * [m, n] -> { [x, y] -> [x, 1 + y] : x >= 1 and y >= 1 and
1211 * x <= 10 and y <= 10;
1212 * [x, y] -> [1 + x, y] : x >= 1 and x <= 20 and
1213 * y >= 5 and y <= 15 }
1217 * [m, n] -> { [x, y] -> [x2, y2] : x >= 1 and 10y2 <= 20 - x + 10y and
1218 * 4y2 >= 5 + 3y and 5y2 <= 15 + 4y and
1219 * y2 <= 1 + x + y - x2 and y2 >= y and
1220 * y2 >= 1 + x + y - x2 }
1222 static int check_eq_adj_eq(struct isl_map
*map
, int i
, int j
,
1223 struct isl_tab
**tabs
, int *eq_i
, int *ineq_i
, int *eq_j
, int *ineq_j
)
1227 struct isl_wraps wraps
;
1229 struct isl_set
*set_i
= NULL
;
1230 struct isl_set
*set_j
= NULL
;
1231 struct isl_vec
*bound
= NULL
;
1232 unsigned total
= isl_basic_map_total_dim(map
->p
[i
]);
1234 if (count(eq_i
, 2 * map
->p
[i
]->n_eq
, STATUS_ADJ_EQ
) != 1)
1237 for (k
= 0; k
< 2 * map
->p
[i
]->n_eq
; ++k
)
1238 if (eq_i
[k
] == STATUS_ADJ_EQ
)
1241 set_i
= set_from_updated_bmap(map
->p
[i
], tabs
[i
]);
1242 set_j
= set_from_updated_bmap(map
->p
[j
], tabs
[j
]);
1243 mat
= isl_mat_alloc(map
->ctx
, 2 * (map
->p
[i
]->n_eq
+ map
->p
[j
]->n_eq
) +
1244 map
->p
[i
]->n_ineq
+ map
->p
[j
]->n_ineq
,
1246 wraps_init(&wraps
, mat
, map
, i
, j
, eq_i
, ineq_i
, eq_j
, ineq_j
);
1247 bound
= isl_vec_alloc(map
->ctx
, 1 + total
);
1248 if (!set_i
|| !set_j
|| !wraps
.mat
|| !bound
)
1252 isl_seq_neg(bound
->el
, map
->p
[i
]->eq
[k
/ 2], 1 + total
);
1254 isl_seq_cpy(bound
->el
, map
->p
[i
]->eq
[k
/ 2], 1 + total
);
1255 isl_int_add_ui(bound
->el
[0], bound
->el
[0], 1);
1257 isl_seq_cpy(wraps
.mat
->row
[0], bound
->el
, 1 + total
);
1258 wraps
.mat
->n_row
= 1;
1260 if (add_wraps(&wraps
, map
->p
[j
], tabs
[j
], bound
->el
, set_i
) < 0)
1262 if (!wraps
.mat
->n_row
)
1265 isl_int_sub_ui(bound
->el
[0], bound
->el
[0], 1);
1266 isl_seq_neg(bound
->el
, bound
->el
, 1 + total
);
1268 isl_seq_cpy(wraps
.mat
->row
[wraps
.mat
->n_row
], bound
->el
, 1 + total
);
1271 if (add_wraps(&wraps
, map
->p
[i
], tabs
[i
], bound
->el
, set_j
) < 0)
1273 if (!wraps
.mat
->n_row
)
1276 changed
= fuse(map
, i
, j
, tabs
, eq_i
, ineq_i
, eq_j
, ineq_j
, wraps
.mat
);
1279 error
: changed
= -1;
1284 isl_set_free(set_i
);
1285 isl_set_free(set_j
);
1286 isl_vec_free(bound
);
1291 /* Check if the union of the given pair of basic maps
1292 * can be represented by a single basic map.
1293 * If so, replace the pair by the single basic map and return 1.
1294 * Otherwise, return 0;
1295 * The two basic maps are assumed to live in the same local space.
1297 * We first check the effect of each constraint of one basic map
1298 * on the other basic map.
1299 * The constraint may be
1300 * redundant the constraint is redundant in its own
1301 * basic map and should be ignore and removed
1303 * valid all (integer) points of the other basic map
1304 * satisfy the constraint
1305 * separate no (integer) point of the other basic map
1306 * satisfies the constraint
1307 * cut some but not all points of the other basic map
1308 * satisfy the constraint
1309 * adj_eq the given constraint is adjacent (on the outside)
1310 * to an equality of the other basic map
1311 * adj_ineq the given constraint is adjacent (on the outside)
1312 * to an inequality of the other basic map
1314 * We consider seven cases in which we can replace the pair by a single
1315 * basic map. We ignore all "redundant" constraints.
1317 * 1. all constraints of one basic map are valid
1318 * => the other basic map is a subset and can be removed
1320 * 2. all constraints of both basic maps are either "valid" or "cut"
1321 * and the facets corresponding to the "cut" constraints
1322 * of one of the basic maps lies entirely inside the other basic map
1323 * => the pair can be replaced by a basic map consisting
1324 * of the valid constraints in both basic maps
1326 * 3. there is a single pair of adjacent inequalities
1327 * (all other constraints are "valid")
1328 * => the pair can be replaced by a basic map consisting
1329 * of the valid constraints in both basic maps
1331 * 4. one basic map has a single adjacent inequality, while the other
1332 * constraints are "valid". The other basic map has some
1333 * "cut" constraints, but replacing the adjacent inequality by
1334 * its opposite and adding the valid constraints of the other
1335 * basic map results in a subset of the other basic map
1336 * => the pair can be replaced by a basic map consisting
1337 * of the valid constraints in both basic maps
1339 * 5. there is a single adjacent pair of an inequality and an equality,
1340 * the other constraints of the basic map containing the inequality are
1341 * "valid". Moreover, if the inequality the basic map is relaxed
1342 * and then turned into an equality, then resulting facet lies
1343 * entirely inside the other basic map
1344 * => the pair can be replaced by the basic map containing
1345 * the inequality, with the inequality relaxed.
1347 * 6. there is a single adjacent pair of an inequality and an equality,
1348 * the other constraints of the basic map containing the inequality are
1349 * "valid". Moreover, the facets corresponding to both
1350 * the inequality and the equality can be wrapped around their
1351 * ridges to include the other basic map
1352 * => the pair can be replaced by a basic map consisting
1353 * of the valid constraints in both basic maps together
1354 * with all wrapping constraints
1356 * 7. one of the basic maps extends beyond the other by at most one.
1357 * Moreover, the facets corresponding to the cut constraints and
1358 * the pieces of the other basic map at offset one from these cut
1359 * constraints can be wrapped around their ridges to include
1360 * the union of the two basic maps
1361 * => the pair can be replaced by a basic map consisting
1362 * of the valid constraints in both basic maps together
1363 * with all wrapping constraints
1365 * 8. the two basic maps live in adjacent hyperplanes. In principle
1366 * such sets can always be combined through wrapping, but we impose
1367 * that there is only one such pair, to avoid overeager coalescing.
1369 * Throughout the computation, we maintain a collection of tableaus
1370 * corresponding to the basic maps. When the basic maps are dropped
1371 * or combined, the tableaus are modified accordingly.
1373 static int coalesce_local_pair(__isl_keep isl_map
*map
, int i
, int j
,
1374 struct isl_tab
**tabs
)
1382 eq_i
= eq_status_in(map
->p
[i
], tabs
[j
]);
1383 if (map
->p
[i
]->n_eq
&& !eq_i
)
1385 if (any(eq_i
, 2 * map
->p
[i
]->n_eq
, STATUS_ERROR
))
1387 if (any(eq_i
, 2 * map
->p
[i
]->n_eq
, STATUS_SEPARATE
))
1390 eq_j
= eq_status_in(map
->p
[j
], tabs
[i
]);
1391 if (map
->p
[j
]->n_eq
&& !eq_j
)
1393 if (any(eq_j
, 2 * map
->p
[j
]->n_eq
, STATUS_ERROR
))
1395 if (any(eq_j
, 2 * map
->p
[j
]->n_eq
, STATUS_SEPARATE
))
1398 ineq_i
= ineq_status_in(map
->p
[i
], tabs
[i
], tabs
[j
]);
1399 if (map
->p
[i
]->n_ineq
&& !ineq_i
)
1401 if (any(ineq_i
, map
->p
[i
]->n_ineq
, STATUS_ERROR
))
1403 if (any(ineq_i
, map
->p
[i
]->n_ineq
, STATUS_SEPARATE
))
1406 ineq_j
= ineq_status_in(map
->p
[j
], tabs
[j
], tabs
[i
]);
1407 if (map
->p
[j
]->n_ineq
&& !ineq_j
)
1409 if (any(ineq_j
, map
->p
[j
]->n_ineq
, STATUS_ERROR
))
1411 if (any(ineq_j
, map
->p
[j
]->n_ineq
, STATUS_SEPARATE
))
1414 if (all(eq_i
, 2 * map
->p
[i
]->n_eq
, STATUS_VALID
) &&
1415 all(ineq_i
, map
->p
[i
]->n_ineq
, STATUS_VALID
)) {
1418 } else if (all(eq_j
, 2 * map
->p
[j
]->n_eq
, STATUS_VALID
) &&
1419 all(ineq_j
, map
->p
[j
]->n_ineq
, STATUS_VALID
)) {
1422 } else if (any(eq_i
, 2 * map
->p
[i
]->n_eq
, STATUS_ADJ_EQ
)) {
1423 changed
= check_eq_adj_eq(map
, i
, j
, tabs
,
1424 eq_i
, ineq_i
, eq_j
, ineq_j
);
1425 } else if (any(eq_j
, 2 * map
->p
[j
]->n_eq
, STATUS_ADJ_EQ
)) {
1426 changed
= check_eq_adj_eq(map
, j
, i
, tabs
,
1427 eq_j
, ineq_j
, eq_i
, ineq_i
);
1428 } else if (any(eq_i
, 2 * map
->p
[i
]->n_eq
, STATUS_ADJ_INEQ
) ||
1429 any(eq_j
, 2 * map
->p
[j
]->n_eq
, STATUS_ADJ_INEQ
)) {
1430 changed
= check_adj_eq(map
, i
, j
, tabs
,
1431 eq_i
, ineq_i
, eq_j
, ineq_j
);
1432 } else if (any(ineq_i
, map
->p
[i
]->n_ineq
, STATUS_ADJ_EQ
) ||
1433 any(ineq_j
, map
->p
[j
]->n_ineq
, STATUS_ADJ_EQ
)) {
1436 } else if (any(ineq_i
, map
->p
[i
]->n_ineq
, STATUS_ADJ_INEQ
) ||
1437 any(ineq_j
, map
->p
[j
]->n_ineq
, STATUS_ADJ_INEQ
)) {
1438 changed
= check_adj_ineq(map
, i
, j
, tabs
,
1439 eq_i
, ineq_i
, eq_j
, ineq_j
);
1441 if (!any(eq_i
, 2 * map
->p
[i
]->n_eq
, STATUS_CUT
) &&
1442 !any(eq_j
, 2 * map
->p
[j
]->n_eq
, STATUS_CUT
))
1443 changed
= check_facets(map
, i
, j
, tabs
, ineq_i
, ineq_j
);
1445 changed
= check_wrap(map
, i
, j
, tabs
,
1446 eq_i
, ineq_i
, eq_j
, ineq_j
);
1463 /* Do the two basic maps live in the same local space, i.e.,
1464 * do they have the same (known) divs?
1465 * If either basic map has any unknown divs, then we can only assume
1466 * that they do not live in the same local space.
1468 static int same_divs(__isl_keep isl_basic_map
*bmap1
,
1469 __isl_keep isl_basic_map
*bmap2
)
1475 if (!bmap1
|| !bmap2
)
1477 if (bmap1
->n_div
!= bmap2
->n_div
)
1480 if (bmap1
->n_div
== 0)
1483 known
= isl_basic_map_divs_known(bmap1
);
1484 if (known
< 0 || !known
)
1486 known
= isl_basic_map_divs_known(bmap2
);
1487 if (known
< 0 || !known
)
1490 total
= isl_basic_map_total_dim(bmap1
);
1491 for (i
= 0; i
< bmap1
->n_div
; ++i
)
1492 if (!isl_seq_eq(bmap1
->div
[i
], bmap2
->div
[i
], 2 + total
))
1498 /* Given two basic maps "i" and "j", where the divs of "i" form a subset
1499 * of those of "j", check if basic map "j" is a subset of basic map "i"
1500 * and, if so, drop basic map "j".
1502 * We first expand the divs of basic map "i" to match those of basic map "j",
1503 * using the divs and expansion computed by the caller.
1504 * Then we check if all constraints of the expanded "i" are valid for "j".
1506 static int coalesce_subset(__isl_keep isl_map
*map
, int i
, int j
,
1507 struct isl_tab
**tabs
, __isl_keep isl_mat
*div
, int *exp
)
1509 isl_basic_map
*bmap
;
1514 bmap
= isl_basic_map_copy(map
->p
[i
]);
1515 bmap
= isl_basic_set_expand_divs(bmap
, isl_mat_copy(div
), exp
);
1520 eq_i
= eq_status_in(bmap
, tabs
[j
]);
1521 if (bmap
->n_eq
&& !eq_i
)
1523 if (any(eq_i
, 2 * bmap
->n_eq
, STATUS_ERROR
))
1525 if (any(eq_i
, 2 * bmap
->n_eq
, STATUS_SEPARATE
))
1528 ineq_i
= ineq_status_in(bmap
, NULL
, tabs
[j
]);
1529 if (bmap
->n_ineq
&& !ineq_i
)
1531 if (any(ineq_i
, bmap
->n_ineq
, STATUS_ERROR
))
1533 if (any(ineq_i
, bmap
->n_ineq
, STATUS_SEPARATE
))
1536 if (all(eq_i
, 2 * map
->p
[i
]->n_eq
, STATUS_VALID
) &&
1537 all(ineq_i
, map
->p
[i
]->n_ineq
, STATUS_VALID
)) {
1543 isl_basic_map_free(bmap
);
1548 isl_basic_map_free(bmap
);
1554 /* Check if the basic map "j" is a subset of basic map "i",
1555 * assuming that "i" has fewer divs that "j".
1556 * If not, then we change the order.
1558 * If the two basic maps have the same number of divs, then
1559 * they must necessarily be different. Otherwise, we would have
1560 * called coalesce_local_pair. We therefore don't try anything
1563 * We first check if the divs of "i" are all known and form a subset
1564 * of those of "j". If so, we pass control over to coalesce_subset.
1566 static int check_coalesce_subset(__isl_keep isl_map
*map
, int i
, int j
,
1567 struct isl_tab
**tabs
)
1570 isl_mat
*div_i
, *div_j
, *div
;
1576 if (map
->p
[i
]->n_div
== map
->p
[j
]->n_div
)
1578 if (map
->p
[j
]->n_div
< map
->p
[i
]->n_div
)
1579 return check_coalesce_subset(map
, j
, i
, tabs
);
1581 known
= isl_basic_map_divs_known(map
->p
[i
]);
1582 if (known
< 0 || !known
)
1585 ctx
= isl_map_get_ctx(map
);
1587 div_i
= isl_basic_map_get_divs(map
->p
[i
]);
1588 div_j
= isl_basic_map_get_divs(map
->p
[j
]);
1590 if (!div_i
|| !div_j
)
1593 exp1
= isl_alloc_array(ctx
, int, div_i
->n_row
);
1594 exp2
= isl_alloc_array(ctx
, int, div_j
->n_row
);
1595 if ((div_i
->n_row
&& !exp1
) || (div_j
->n_row
&& !exp2
))
1598 div
= isl_merge_divs(div_i
, div_j
, exp1
, exp2
);
1602 if (div
->n_row
== div_j
->n_row
)
1603 subset
= coalesce_subset(map
, i
, j
, tabs
, div
, exp1
);
1609 isl_mat_free(div_i
);
1610 isl_mat_free(div_j
);
1617 isl_mat_free(div_i
);
1618 isl_mat_free(div_j
);
1624 /* Check if the union of the given pair of basic maps
1625 * can be represented by a single basic map.
1626 * If so, replace the pair by the single basic map and return 1.
1627 * Otherwise, return 0;
1629 * We first check if the two basic maps live in the same local space.
1630 * If so, we do the complete check. Otherwise, we check if one is
1631 * an obvious subset of the other.
1633 static int coalesce_pair(__isl_keep isl_map
*map
, int i
, int j
,
1634 struct isl_tab
**tabs
)
1638 same
= same_divs(map
->p
[i
], map
->p
[j
]);
1642 return coalesce_local_pair(map
, i
, j
, tabs
);
1644 return check_coalesce_subset(map
, i
, j
, tabs
);
1647 static struct isl_map
*coalesce(struct isl_map
*map
, struct isl_tab
**tabs
)
1651 for (i
= map
->n
- 2; i
>= 0; --i
)
1653 for (j
= i
+ 1; j
< map
->n
; ++j
) {
1655 changed
= coalesce_pair(map
, i
, j
, tabs
);
1667 /* For each pair of basic maps in the map, check if the union of the two
1668 * can be represented by a single basic map.
1669 * If so, replace the pair by the single basic map and start over.
1671 * Since we are constructing the tableaus of the basic maps anyway,
1672 * we exploit them to detect implicit equalities and redundant constraints.
1673 * This also helps the coalescing as it can ignore the redundant constraints.
1674 * In order to avoid confusion, we make all implicit equalities explicit
1675 * in the basic maps. We don't call isl_basic_map_gauss, though,
1676 * as that may affect the number of constraints.
1677 * This means that we have to call isl_basic_map_gauss at the end
1678 * of the computation to ensure that the basic maps are not left
1679 * in an unexpected state.
1681 struct isl_map
*isl_map_coalesce(struct isl_map
*map
)
1685 struct isl_tab
**tabs
= NULL
;
1687 map
= isl_map_remove_empty_parts(map
);
1694 map
= isl_map_sort_divs(map
);
1695 map
= isl_map_cow(map
);
1700 tabs
= isl_calloc_array(map
->ctx
, struct isl_tab
*, map
->n
);
1705 for (i
= 0; i
< map
->n
; ++i
) {
1706 tabs
[i
] = isl_tab_from_basic_map(map
->p
[i
], 0);
1709 if (!ISL_F_ISSET(map
->p
[i
], ISL_BASIC_MAP_NO_IMPLICIT
))
1710 if (isl_tab_detect_implicit_equalities(tabs
[i
]) < 0)
1712 map
->p
[i
] = isl_tab_make_equalities_explicit(tabs
[i
],
1716 if (!ISL_F_ISSET(map
->p
[i
], ISL_BASIC_MAP_NO_REDUNDANT
))
1717 if (isl_tab_detect_redundant(tabs
[i
]) < 0)
1720 for (i
= map
->n
- 1; i
>= 0; --i
)
1724 map
= coalesce(map
, tabs
);
1727 for (i
= 0; i
< map
->n
; ++i
) {
1728 map
->p
[i
] = isl_basic_map_update_from_tab(map
->p
[i
],
1730 map
->p
[i
] = isl_basic_map_gauss(map
->p
[i
], NULL
);
1731 map
->p
[i
] = isl_basic_map_finalize(map
->p
[i
]);
1734 ISL_F_SET(map
->p
[i
], ISL_BASIC_MAP_NO_IMPLICIT
);
1735 ISL_F_SET(map
->p
[i
], ISL_BASIC_MAP_NO_REDUNDANT
);
1738 for (i
= 0; i
< n
; ++i
)
1739 isl_tab_free(tabs
[i
]);
1746 for (i
= 0; i
< n
; ++i
)
1747 isl_tab_free(tabs
[i
]);
1753 /* For each pair of basic sets in the set, check if the union of the two
1754 * can be represented by a single basic set.
1755 * If so, replace the pair by the single basic set and start over.
1757 struct isl_set
*isl_set_coalesce(struct isl_set
*set
)
1759 return (struct isl_set
*)isl_map_coalesce((struct isl_map
*)set
);