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 static __isl_give isl_set
*set_from_updated_bmap(__isl_keep isl_basic_map
*bmap
,
768 bmap
= isl_basic_map_copy(bmap
);
769 bmap
= isl_basic_map_cow(bmap
);
770 bmap
= isl_basic_map_update_from_tab(bmap
, tab
);
771 return isl_set_from_basic_set(isl_basic_map_underlying_set(bmap
));
774 /* Given a basic set i with a constraint k that is adjacent to
775 * basic set j, check if we can wrap
776 * both the facet corresponding to k and basic map j
777 * around their ridges to include the other set.
778 * If so, replace the pair of basic sets by their union.
780 * All constraints of i (except k) are assumed to be valid for j.
781 * This means that there is no real need to wrap the ridges of
782 * the faces of basic map i around basic map j but since we do,
783 * we have to check that the resulting wrapping constraints are valid for i.
792 static int can_wrap_in_facet(struct isl_map
*map
, int i
, int j
, int k
,
793 struct isl_tab
**tabs
, int *eq_i
, int *ineq_i
, int *eq_j
, int *ineq_j
)
796 struct isl_wraps wraps
;
798 struct isl_set
*set_i
= NULL
;
799 struct isl_set
*set_j
= NULL
;
800 struct isl_vec
*bound
= NULL
;
801 unsigned total
= isl_basic_map_total_dim(map
->p
[i
]);
802 struct isl_tab_undo
*snap
;
805 set_i
= set_from_updated_bmap(map
->p
[i
], tabs
[i
]);
806 set_j
= set_from_updated_bmap(map
->p
[j
], tabs
[j
]);
807 mat
= isl_mat_alloc(map
->ctx
, 2 * (map
->p
[i
]->n_eq
+ map
->p
[j
]->n_eq
) +
808 map
->p
[i
]->n_ineq
+ map
->p
[j
]->n_ineq
,
810 wraps_init(&wraps
, mat
, map
, i
, j
, eq_i
, ineq_i
, eq_j
, ineq_j
);
811 bound
= isl_vec_alloc(map
->ctx
, 1 + total
);
812 if (!set_i
|| !set_j
|| !wraps
.mat
|| !bound
)
815 isl_seq_cpy(bound
->el
, map
->p
[i
]->ineq
[k
], 1 + total
);
816 isl_int_add_ui(bound
->el
[0], bound
->el
[0], 1);
818 isl_seq_cpy(wraps
.mat
->row
[0], bound
->el
, 1 + total
);
819 wraps
.mat
->n_row
= 1;
821 if (add_wraps(&wraps
, map
->p
[j
], tabs
[j
], bound
->el
, set_i
) < 0)
823 if (!wraps
.mat
->n_row
)
826 snap
= isl_tab_snap(tabs
[i
]);
828 if (isl_tab_select_facet(tabs
[i
], map
->p
[i
]->n_eq
+ k
) < 0)
830 if (isl_tab_detect_redundant(tabs
[i
]) < 0)
833 isl_seq_neg(bound
->el
, map
->p
[i
]->ineq
[k
], 1 + total
);
835 n
= wraps
.mat
->n_row
;
836 if (add_wraps(&wraps
, map
->p
[i
], tabs
[i
], bound
->el
, set_j
) < 0)
839 if (isl_tab_rollback(tabs
[i
], snap
) < 0)
841 if (check_wraps(wraps
.mat
, n
, tabs
[i
]) < 0)
843 if (!wraps
.mat
->n_row
)
846 changed
= fuse(map
, i
, j
, tabs
, eq_i
, ineq_i
, eq_j
, ineq_j
, wraps
.mat
);
865 /* Set the is_redundant property of the "n" constraints in "cuts",
867 * This is a fairly tricky operation as it bypasses isl_tab.c.
868 * The reason we want to temporarily mark some constraints redundant
869 * is that we want to ignore them in add_wraps.
871 * Initially all cut constraints are non-redundant, but the
872 * selection of a facet right before the call to this function
873 * may have made some of them redundant.
874 * Likewise, the same constraints are marked non-redundant
875 * in the second call to this function, before they are officially
876 * made non-redundant again in the subsequent rollback.
878 static void set_is_redundant(struct isl_tab
*tab
, unsigned n_eq
,
879 int *cuts
, int n
, int k
, int v
)
883 for (l
= 0; l
< n
; ++l
) {
886 tab
->con
[n_eq
+ cuts
[l
]].is_redundant
= v
;
890 /* Given a pair of basic maps i and j such that j sticks out
891 * of i at n cut constraints, each time by at most one,
892 * try to compute wrapping constraints and replace the two
893 * basic maps by a single basic map.
894 * The other constraints of i are assumed to be valid for j.
896 * The facets of i corresponding to the cut constraints are
897 * wrapped around their ridges, except those ridges determined
898 * by any of the other cut constraints.
899 * The intersections of cut constraints need to be ignored
900 * as the result of wrapping one cut constraint around another
901 * would result in a constraint cutting the union.
902 * In each case, the facets are wrapped to include the union
903 * of the two basic maps.
905 * The pieces of j that lie at an offset of exactly one from
906 * one of the cut constraints of i are wrapped around their edges.
907 * Here, there is no need to ignore intersections because we
908 * are wrapping around the union of the two basic maps.
910 * If any wrapping fails, i.e., if we cannot wrap to touch
911 * the union, then we give up.
912 * Otherwise, the pair of basic maps is replaced by their union.
914 static int wrap_in_facets(struct isl_map
*map
, int i
, int j
,
915 int *cuts
, int n
, struct isl_tab
**tabs
,
916 int *eq_i
, int *ineq_i
, int *eq_j
, int *ineq_j
)
919 struct isl_wraps wraps
;
922 isl_vec
*bound
= NULL
;
923 unsigned total
= isl_basic_map_total_dim(map
->p
[i
]);
926 struct isl_tab_undo
*snap_i
, *snap_j
;
928 if (isl_tab_extend_cons(tabs
[j
], 1) < 0)
931 max_wrap
= 2 * (map
->p
[i
]->n_eq
+ map
->p
[j
]->n_eq
) +
932 map
->p
[i
]->n_ineq
+ map
->p
[j
]->n_ineq
;
935 set
= isl_set_union(set_from_updated_bmap(map
->p
[i
], tabs
[i
]),
936 set_from_updated_bmap(map
->p
[j
], tabs
[j
]));
937 mat
= isl_mat_alloc(map
->ctx
, max_wrap
, 1 + total
);
938 wraps_init(&wraps
, mat
, map
, i
, j
, eq_i
, ineq_i
, eq_j
, ineq_j
);
939 bound
= isl_vec_alloc(map
->ctx
, 1 + total
);
940 if (!set
|| !wraps
.mat
|| !bound
)
943 snap_i
= isl_tab_snap(tabs
[i
]);
944 snap_j
= isl_tab_snap(tabs
[j
]);
946 wraps
.mat
->n_row
= 0;
948 for (k
= 0; k
< n
; ++k
) {
949 if (isl_tab_select_facet(tabs
[i
], map
->p
[i
]->n_eq
+ cuts
[k
]) < 0)
951 if (isl_tab_detect_redundant(tabs
[i
]) < 0)
953 set_is_redundant(tabs
[i
], map
->p
[i
]->n_eq
, cuts
, n
, k
, 1);
955 isl_seq_neg(bound
->el
, map
->p
[i
]->ineq
[cuts
[k
]], 1 + total
);
956 if (!tabs
[i
]->empty
&&
957 add_wraps(&wraps
, map
->p
[i
], tabs
[i
], bound
->el
, set
) < 0)
960 set_is_redundant(tabs
[i
], map
->p
[i
]->n_eq
, cuts
, n
, k
, 0);
961 if (isl_tab_rollback(tabs
[i
], snap_i
) < 0)
966 if (!wraps
.mat
->n_row
)
969 isl_seq_cpy(bound
->el
, map
->p
[i
]->ineq
[cuts
[k
]], 1 + total
);
970 isl_int_add_ui(bound
->el
[0], bound
->el
[0], 1);
971 if (isl_tab_add_eq(tabs
[j
], bound
->el
) < 0)
973 if (isl_tab_detect_redundant(tabs
[j
]) < 0)
976 if (!tabs
[j
]->empty
&&
977 add_wraps(&wraps
, map
->p
[j
], tabs
[j
], bound
->el
, set
) < 0)
980 if (isl_tab_rollback(tabs
[j
], snap_j
) < 0)
983 if (!wraps
.mat
->n_row
)
988 changed
= fuse(map
, i
, j
, tabs
,
989 eq_i
, ineq_i
, eq_j
, ineq_j
, wraps
.mat
);
1003 /* Given two basic sets i and j such that i has no cut equalities,
1004 * check if relaxing all the cut inequalities of i by one turns
1005 * them into valid constraint for j and check if we can wrap in
1006 * the bits that are sticking out.
1007 * If so, replace the pair by their union.
1009 * We first check if all relaxed cut inequalities of i are valid for j
1010 * and then try to wrap in the intersections of the relaxed cut inequalities
1013 * During this wrapping, we consider the points of j that lie at a distance
1014 * of exactly 1 from i. In particular, we ignore the points that lie in
1015 * between this lower-dimensional space and the basic map i.
1016 * We can therefore only apply this to integer maps.
1042 * Wrapping can fail if the result of wrapping one of the facets
1043 * around its edges does not produce any new facet constraint.
1044 * In particular, this happens when we try to wrap in unbounded sets.
1046 * _______________________________________________________________________
1050 * |_| |_________________________________________________________________
1053 * The following is not an acceptable result of coalescing the above two
1054 * sets as it includes extra integer points.
1055 * _______________________________________________________________________
1060 * \______________________________________________________________________
1062 static int can_wrap_in_set(struct isl_map
*map
, int i
, int j
,
1063 struct isl_tab
**tabs
, int *eq_i
, int *ineq_i
, int *eq_j
, int *ineq_j
)
1070 if (ISL_F_ISSET(map
->p
[i
], ISL_BASIC_MAP_RATIONAL
) ||
1071 ISL_F_ISSET(map
->p
[j
], ISL_BASIC_MAP_RATIONAL
))
1074 n
= count(ineq_i
, map
->p
[i
]->n_ineq
, STATUS_CUT
);
1078 cuts
= isl_alloc_array(map
->ctx
, int, n
);
1082 for (k
= 0, m
= 0; m
< n
; ++k
) {
1083 enum isl_ineq_type type
;
1085 if (ineq_i
[k
] != STATUS_CUT
)
1088 isl_int_add_ui(map
->p
[i
]->ineq
[k
][0], map
->p
[i
]->ineq
[k
][0], 1);
1089 type
= isl_tab_ineq_type(tabs
[j
], map
->p
[i
]->ineq
[k
]);
1090 isl_int_sub_ui(map
->p
[i
]->ineq
[k
][0], map
->p
[i
]->ineq
[k
][0], 1);
1091 if (type
== isl_ineq_error
)
1093 if (type
!= isl_ineq_redundant
)
1100 changed
= wrap_in_facets(map
, i
, j
, cuts
, n
, tabs
,
1101 eq_i
, ineq_i
, eq_j
, ineq_j
);
1111 /* Check if either i or j has only cut inequalities that can
1112 * be used to wrap in (a facet of) the other basic set.
1113 * if so, replace the pair by their union.
1115 static int check_wrap(struct isl_map
*map
, int i
, int j
,
1116 struct isl_tab
**tabs
, int *eq_i
, int *ineq_i
, int *eq_j
, int *ineq_j
)
1120 if (!any(eq_i
, 2 * map
->p
[i
]->n_eq
, STATUS_CUT
))
1121 changed
= can_wrap_in_set(map
, i
, j
, tabs
,
1122 eq_i
, ineq_i
, eq_j
, ineq_j
);
1126 if (!any(eq_j
, 2 * map
->p
[j
]->n_eq
, STATUS_CUT
))
1127 changed
= can_wrap_in_set(map
, j
, i
, tabs
,
1128 eq_j
, ineq_j
, eq_i
, ineq_i
);
1132 /* At least one of the basic maps has an equality that is adjacent
1133 * to inequality. Make sure that only one of the basic maps has
1134 * such an equality and that the other basic map has exactly one
1135 * inequality adjacent to an equality.
1136 * We call the basic map that has the inequality "i" and the basic
1137 * map that has the equality "j".
1138 * If "i" has any "cut" (in)equality, then relaxing the inequality
1139 * by one would not result in a basic map that contains the other
1142 static int check_adj_eq(struct isl_map
*map
, int i
, int j
,
1143 struct isl_tab
**tabs
, int *eq_i
, int *ineq_i
, int *eq_j
, int *ineq_j
)
1148 if (any(eq_i
, 2 * map
->p
[i
]->n_eq
, STATUS_ADJ_INEQ
) &&
1149 any(eq_j
, 2 * map
->p
[j
]->n_eq
, STATUS_ADJ_INEQ
))
1150 /* ADJ EQ TOO MANY */
1153 if (any(eq_i
, 2 * map
->p
[i
]->n_eq
, STATUS_ADJ_INEQ
))
1154 return check_adj_eq(map
, j
, i
, tabs
,
1155 eq_j
, ineq_j
, eq_i
, ineq_i
);
1157 /* j has an equality adjacent to an inequality in i */
1159 if (any(eq_i
, 2 * map
->p
[i
]->n_eq
, STATUS_CUT
))
1161 if (any(ineq_i
, map
->p
[i
]->n_ineq
, STATUS_CUT
))
1164 if (count(ineq_i
, map
->p
[i
]->n_ineq
, STATUS_ADJ_EQ
) != 1 ||
1165 any(ineq_j
, map
->p
[j
]->n_ineq
, STATUS_ADJ_EQ
) ||
1166 any(ineq_i
, map
->p
[i
]->n_ineq
, STATUS_ADJ_INEQ
) ||
1167 any(ineq_j
, map
->p
[j
]->n_ineq
, STATUS_ADJ_INEQ
))
1168 /* ADJ EQ TOO MANY */
1171 for (k
= 0; k
< map
->p
[i
]->n_ineq
; ++k
)
1172 if (ineq_i
[k
] == STATUS_ADJ_EQ
)
1175 changed
= is_adj_eq_extension(map
, i
, j
, k
, tabs
,
1176 eq_i
, ineq_i
, eq_j
, ineq_j
);
1180 if (count(eq_j
, 2 * map
->p
[j
]->n_eq
, STATUS_ADJ_INEQ
) != 1)
1183 changed
= can_wrap_in_facet(map
, i
, j
, k
, tabs
, eq_i
, ineq_i
, eq_j
, ineq_j
);
1188 /* The two basic maps lie on adjacent hyperplanes. In particular,
1189 * basic map "i" has an equality that lies parallel to basic map "j".
1190 * Check if we can wrap the facets around the parallel hyperplanes
1191 * to include the other set.
1193 * We perform basically the same operations as can_wrap_in_facet,
1194 * except that we don't need to select a facet of one of the sets.
1200 * We only allow one equality of "i" to be adjacent to an equality of "j"
1201 * to avoid coalescing
1203 * [m, n] -> { [x, y] -> [x, 1 + y] : x >= 1 and y >= 1 and
1204 * x <= 10 and y <= 10;
1205 * [x, y] -> [1 + x, y] : x >= 1 and x <= 20 and
1206 * y >= 5 and y <= 15 }
1210 * [m, n] -> { [x, y] -> [x2, y2] : x >= 1 and 10y2 <= 20 - x + 10y and
1211 * 4y2 >= 5 + 3y and 5y2 <= 15 + 4y and
1212 * y2 <= 1 + x + y - x2 and y2 >= y and
1213 * y2 >= 1 + x + y - x2 }
1215 static int check_eq_adj_eq(struct isl_map
*map
, int i
, int j
,
1216 struct isl_tab
**tabs
, int *eq_i
, int *ineq_i
, int *eq_j
, int *ineq_j
)
1220 struct isl_wraps wraps
;
1222 struct isl_set
*set_i
= NULL
;
1223 struct isl_set
*set_j
= NULL
;
1224 struct isl_vec
*bound
= NULL
;
1225 unsigned total
= isl_basic_map_total_dim(map
->p
[i
]);
1227 if (count(eq_i
, 2 * map
->p
[i
]->n_eq
, STATUS_ADJ_EQ
) != 1)
1230 for (k
= 0; k
< 2 * map
->p
[i
]->n_eq
; ++k
)
1231 if (eq_i
[k
] == STATUS_ADJ_EQ
)
1234 set_i
= set_from_updated_bmap(map
->p
[i
], tabs
[i
]);
1235 set_j
= set_from_updated_bmap(map
->p
[j
], tabs
[j
]);
1236 mat
= isl_mat_alloc(map
->ctx
, 2 * (map
->p
[i
]->n_eq
+ map
->p
[j
]->n_eq
) +
1237 map
->p
[i
]->n_ineq
+ map
->p
[j
]->n_ineq
,
1239 wraps_init(&wraps
, mat
, map
, i
, j
, eq_i
, ineq_i
, eq_j
, ineq_j
);
1240 bound
= isl_vec_alloc(map
->ctx
, 1 + total
);
1241 if (!set_i
|| !set_j
|| !wraps
.mat
|| !bound
)
1245 isl_seq_neg(bound
->el
, map
->p
[i
]->eq
[k
/ 2], 1 + total
);
1247 isl_seq_cpy(bound
->el
, map
->p
[i
]->eq
[k
/ 2], 1 + total
);
1248 isl_int_add_ui(bound
->el
[0], bound
->el
[0], 1);
1250 isl_seq_cpy(wraps
.mat
->row
[0], bound
->el
, 1 + total
);
1251 wraps
.mat
->n_row
= 1;
1253 if (add_wraps(&wraps
, map
->p
[j
], tabs
[j
], bound
->el
, set_i
) < 0)
1255 if (!wraps
.mat
->n_row
)
1258 isl_int_sub_ui(bound
->el
[0], bound
->el
[0], 1);
1259 isl_seq_neg(bound
->el
, bound
->el
, 1 + total
);
1261 isl_seq_cpy(wraps
.mat
->row
[wraps
.mat
->n_row
], bound
->el
, 1 + total
);
1264 if (add_wraps(&wraps
, map
->p
[i
], tabs
[i
], bound
->el
, set_j
) < 0)
1266 if (!wraps
.mat
->n_row
)
1269 changed
= fuse(map
, i
, j
, tabs
, eq_i
, ineq_i
, eq_j
, ineq_j
, wraps
.mat
);
1272 error
: changed
= -1;
1277 isl_set_free(set_i
);
1278 isl_set_free(set_j
);
1279 isl_vec_free(bound
);
1284 /* Check if the union of the given pair of basic maps
1285 * can be represented by a single basic map.
1286 * If so, replace the pair by the single basic map and return 1.
1287 * Otherwise, return 0;
1288 * The two basic maps are assumed to live in the same local space.
1290 * We first check the effect of each constraint of one basic map
1291 * on the other basic map.
1292 * The constraint may be
1293 * redundant the constraint is redundant in its own
1294 * basic map and should be ignore and removed
1296 * valid all (integer) points of the other basic map
1297 * satisfy the constraint
1298 * separate no (integer) point of the other basic map
1299 * satisfies the constraint
1300 * cut some but not all points of the other basic map
1301 * satisfy the constraint
1302 * adj_eq the given constraint is adjacent (on the outside)
1303 * to an equality of the other basic map
1304 * adj_ineq the given constraint is adjacent (on the outside)
1305 * to an inequality of the other basic map
1307 * We consider seven cases in which we can replace the pair by a single
1308 * basic map. We ignore all "redundant" constraints.
1310 * 1. all constraints of one basic map are valid
1311 * => the other basic map is a subset and can be removed
1313 * 2. all constraints of both basic maps are either "valid" or "cut"
1314 * and the facets corresponding to the "cut" constraints
1315 * of one of the basic maps lies entirely inside the other basic map
1316 * => the pair can be replaced by a basic map consisting
1317 * of the valid constraints in both basic maps
1319 * 3. there is a single pair of adjacent inequalities
1320 * (all other constraints are "valid")
1321 * => the pair can be replaced by a basic map consisting
1322 * of the valid constraints in both basic maps
1324 * 4. one basic map has a single adjacent inequality, while the other
1325 * constraints are "valid". The other basic map has some
1326 * "cut" constraints, but replacing the adjacent inequality by
1327 * its opposite and adding the valid constraints of the other
1328 * basic map results in a subset of the other basic map
1329 * => the pair can be replaced by a basic map consisting
1330 * of the valid constraints in both basic maps
1332 * 5. there is a single adjacent pair of an inequality and an equality,
1333 * the other constraints of the basic map containing the inequality are
1334 * "valid". Moreover, if the inequality the basic map is relaxed
1335 * and then turned into an equality, then resulting facet lies
1336 * entirely inside the other basic map
1337 * => the pair can be replaced by the basic map containing
1338 * the inequality, with the inequality relaxed.
1340 * 6. there is a single adjacent pair of an inequality and an equality,
1341 * the other constraints of the basic map containing the inequality are
1342 * "valid". Moreover, the facets corresponding to both
1343 * the inequality and the equality can be wrapped around their
1344 * ridges to include the other basic map
1345 * => the pair can be replaced by a basic map consisting
1346 * of the valid constraints in both basic maps together
1347 * with all wrapping constraints
1349 * 7. one of the basic maps extends beyond the other by at most one.
1350 * Moreover, the facets corresponding to the cut constraints and
1351 * the pieces of the other basic map at offset one from these cut
1352 * constraints can be wrapped around their ridges to include
1353 * the union of the two basic maps
1354 * => the pair can be replaced by a basic map consisting
1355 * of the valid constraints in both basic maps together
1356 * with all wrapping constraints
1358 * 8. the two basic maps live in adjacent hyperplanes. In principle
1359 * such sets can always be combined through wrapping, but we impose
1360 * that there is only one such pair, to avoid overeager coalescing.
1362 * Throughout the computation, we maintain a collection of tableaus
1363 * corresponding to the basic maps. When the basic maps are dropped
1364 * or combined, the tableaus are modified accordingly.
1366 static int coalesce_local_pair(__isl_keep isl_map
*map
, int i
, int j
,
1367 struct isl_tab
**tabs
)
1375 eq_i
= eq_status_in(map
->p
[i
], tabs
[j
]);
1376 if (map
->p
[i
]->n_eq
&& !eq_i
)
1378 if (any(eq_i
, 2 * map
->p
[i
]->n_eq
, STATUS_ERROR
))
1380 if (any(eq_i
, 2 * map
->p
[i
]->n_eq
, STATUS_SEPARATE
))
1383 eq_j
= eq_status_in(map
->p
[j
], tabs
[i
]);
1384 if (map
->p
[j
]->n_eq
&& !eq_j
)
1386 if (any(eq_j
, 2 * map
->p
[j
]->n_eq
, STATUS_ERROR
))
1388 if (any(eq_j
, 2 * map
->p
[j
]->n_eq
, STATUS_SEPARATE
))
1391 ineq_i
= ineq_status_in(map
->p
[i
], tabs
[i
], tabs
[j
]);
1392 if (map
->p
[i
]->n_ineq
&& !ineq_i
)
1394 if (any(ineq_i
, map
->p
[i
]->n_ineq
, STATUS_ERROR
))
1396 if (any(ineq_i
, map
->p
[i
]->n_ineq
, STATUS_SEPARATE
))
1399 ineq_j
= ineq_status_in(map
->p
[j
], tabs
[j
], tabs
[i
]);
1400 if (map
->p
[j
]->n_ineq
&& !ineq_j
)
1402 if (any(ineq_j
, map
->p
[j
]->n_ineq
, STATUS_ERROR
))
1404 if (any(ineq_j
, map
->p
[j
]->n_ineq
, STATUS_SEPARATE
))
1407 if (all(eq_i
, 2 * map
->p
[i
]->n_eq
, STATUS_VALID
) &&
1408 all(ineq_i
, map
->p
[i
]->n_ineq
, STATUS_VALID
)) {
1411 } else if (all(eq_j
, 2 * map
->p
[j
]->n_eq
, STATUS_VALID
) &&
1412 all(ineq_j
, map
->p
[j
]->n_ineq
, STATUS_VALID
)) {
1415 } else if (any(eq_i
, 2 * map
->p
[i
]->n_eq
, STATUS_ADJ_EQ
)) {
1416 changed
= check_eq_adj_eq(map
, i
, j
, tabs
,
1417 eq_i
, ineq_i
, eq_j
, ineq_j
);
1418 } else if (any(eq_j
, 2 * map
->p
[j
]->n_eq
, STATUS_ADJ_EQ
)) {
1419 changed
= check_eq_adj_eq(map
, j
, i
, tabs
,
1420 eq_j
, ineq_j
, eq_i
, ineq_i
);
1421 } else if (any(eq_i
, 2 * map
->p
[i
]->n_eq
, STATUS_ADJ_INEQ
) ||
1422 any(eq_j
, 2 * map
->p
[j
]->n_eq
, STATUS_ADJ_INEQ
)) {
1423 changed
= check_adj_eq(map
, i
, j
, tabs
,
1424 eq_i
, ineq_i
, eq_j
, ineq_j
);
1425 } else if (any(ineq_i
, map
->p
[i
]->n_ineq
, STATUS_ADJ_EQ
) ||
1426 any(ineq_j
, map
->p
[j
]->n_ineq
, STATUS_ADJ_EQ
)) {
1429 } else if (any(ineq_i
, map
->p
[i
]->n_ineq
, STATUS_ADJ_INEQ
) ||
1430 any(ineq_j
, map
->p
[j
]->n_ineq
, STATUS_ADJ_INEQ
)) {
1431 changed
= check_adj_ineq(map
, i
, j
, tabs
,
1432 eq_i
, ineq_i
, eq_j
, ineq_j
);
1434 if (!any(eq_i
, 2 * map
->p
[i
]->n_eq
, STATUS_CUT
) &&
1435 !any(eq_j
, 2 * map
->p
[j
]->n_eq
, STATUS_CUT
))
1436 changed
= check_facets(map
, i
, j
, tabs
, ineq_i
, ineq_j
);
1438 changed
= check_wrap(map
, i
, j
, tabs
,
1439 eq_i
, ineq_i
, eq_j
, ineq_j
);
1456 /* Do the two basic maps live in the same local space, i.e.,
1457 * do they have the same (known) divs?
1458 * If either basic map has any unknown divs, then we can only assume
1459 * that they do not live in the same local space.
1461 static int same_divs(__isl_keep isl_basic_map
*bmap1
,
1462 __isl_keep isl_basic_map
*bmap2
)
1468 if (!bmap1
|| !bmap2
)
1470 if (bmap1
->n_div
!= bmap2
->n_div
)
1473 if (bmap1
->n_div
== 0)
1476 known
= isl_basic_map_divs_known(bmap1
);
1477 if (known
< 0 || !known
)
1479 known
= isl_basic_map_divs_known(bmap2
);
1480 if (known
< 0 || !known
)
1483 total
= isl_basic_map_total_dim(bmap1
);
1484 for (i
= 0; i
< bmap1
->n_div
; ++i
)
1485 if (!isl_seq_eq(bmap1
->div
[i
], bmap2
->div
[i
], 2 + total
))
1491 /* Given two basic maps "i" and "j", where the divs of "i" form a subset
1492 * of those of "j", check if basic map "j" is a subset of basic map "i"
1493 * and, if so, drop basic map "j".
1495 * We first expand the divs of basic map "i" to match those of basic map "j",
1496 * using the divs and expansion computed by the caller.
1497 * Then we check if all constraints of the expanded "i" are valid for "j".
1499 static int coalesce_subset(__isl_keep isl_map
*map
, int i
, int j
,
1500 struct isl_tab
**tabs
, __isl_keep isl_mat
*div
, int *exp
)
1502 isl_basic_map
*bmap
;
1507 bmap
= isl_basic_map_copy(map
->p
[i
]);
1508 bmap
= isl_basic_set_expand_divs(bmap
, isl_mat_copy(div
), exp
);
1513 eq_i
= eq_status_in(bmap
, tabs
[j
]);
1514 if (bmap
->n_eq
&& !eq_i
)
1516 if (any(eq_i
, 2 * bmap
->n_eq
, STATUS_ERROR
))
1518 if (any(eq_i
, 2 * bmap
->n_eq
, STATUS_SEPARATE
))
1521 ineq_i
= ineq_status_in(bmap
, NULL
, tabs
[j
]);
1522 if (bmap
->n_ineq
&& !ineq_i
)
1524 if (any(ineq_i
, bmap
->n_ineq
, STATUS_ERROR
))
1526 if (any(ineq_i
, bmap
->n_ineq
, STATUS_SEPARATE
))
1529 if (all(eq_i
, 2 * map
->p
[i
]->n_eq
, STATUS_VALID
) &&
1530 all(ineq_i
, map
->p
[i
]->n_ineq
, STATUS_VALID
)) {
1536 isl_basic_map_free(bmap
);
1541 isl_basic_map_free(bmap
);
1547 /* Check if the basic map "j" is a subset of basic map "i",
1548 * assuming that "i" has fewer divs that "j".
1549 * If not, then we change the order.
1551 * If the two basic maps have the same number of divs, then
1552 * they must necessarily be different. Otherwise, we would have
1553 * called coalesce_local_pair. We therefore don't try anything
1556 * We first check if the divs of "i" are all known and form a subset
1557 * of those of "j". If so, we pass control over to coalesce_subset.
1559 static int check_coalesce_subset(__isl_keep isl_map
*map
, int i
, int j
,
1560 struct isl_tab
**tabs
)
1563 isl_mat
*div_i
, *div_j
, *div
;
1569 if (map
->p
[i
]->n_div
== map
->p
[j
]->n_div
)
1571 if (map
->p
[j
]->n_div
< map
->p
[i
]->n_div
)
1572 return check_coalesce_subset(map
, j
, i
, tabs
);
1574 known
= isl_basic_map_divs_known(map
->p
[i
]);
1575 if (known
< 0 || !known
)
1578 ctx
= isl_map_get_ctx(map
);
1580 div_i
= isl_basic_map_get_divs(map
->p
[i
]);
1581 div_j
= isl_basic_map_get_divs(map
->p
[j
]);
1583 if (!div_i
|| !div_j
)
1586 exp1
= isl_alloc_array(ctx
, int, div_i
->n_row
);
1587 exp2
= isl_alloc_array(ctx
, int, div_j
->n_row
);
1588 if ((div_i
->n_row
&& !exp1
) || (div_j
->n_row
&& !exp2
))
1591 div
= isl_merge_divs(div_i
, div_j
, exp1
, exp2
);
1595 if (div
->n_row
== div_j
->n_row
)
1596 subset
= coalesce_subset(map
, i
, j
, tabs
, div
, exp1
);
1602 isl_mat_free(div_i
);
1603 isl_mat_free(div_j
);
1610 isl_mat_free(div_i
);
1611 isl_mat_free(div_j
);
1617 /* Check if the union of the given pair of basic maps
1618 * can be represented by a single basic map.
1619 * If so, replace the pair by the single basic map and return 1.
1620 * Otherwise, return 0;
1622 * We first check if the two basic maps live in the same local space.
1623 * If so, we do the complete check. Otherwise, we check if one is
1624 * an obvious subset of the other.
1626 static int coalesce_pair(__isl_keep isl_map
*map
, int i
, int j
,
1627 struct isl_tab
**tabs
)
1631 same
= same_divs(map
->p
[i
], map
->p
[j
]);
1635 return coalesce_local_pair(map
, i
, j
, tabs
);
1637 return check_coalesce_subset(map
, i
, j
, tabs
);
1640 static struct isl_map
*coalesce(struct isl_map
*map
, struct isl_tab
**tabs
)
1644 for (i
= map
->n
- 2; i
>= 0; --i
)
1646 for (j
= i
+ 1; j
< map
->n
; ++j
) {
1648 changed
= coalesce_pair(map
, i
, j
, tabs
);
1660 /* For each pair of basic maps in the map, check if the union of the two
1661 * can be represented by a single basic map.
1662 * If so, replace the pair by the single basic map and start over.
1664 * Since we are constructing the tableaus of the basic maps anyway,
1665 * we exploit them to detect implicit equalities and redundant constraints.
1666 * This also helps the coalescing as it can ignore the redundant constraints.
1667 * In order to avoid confusion, we make all implicit equalities explicit
1668 * in the basic maps. We don't call isl_basic_map_gauss, though,
1669 * as that may affect the number of constraints.
1670 * This means that we have to call isl_basic_map_gauss at the end
1671 * of the computation to ensure that the basic maps are not left
1672 * in an unexpected state.
1674 struct isl_map
*isl_map_coalesce(struct isl_map
*map
)
1678 struct isl_tab
**tabs
= NULL
;
1680 map
= isl_map_remove_empty_parts(map
);
1687 map
= isl_map_sort_divs(map
);
1688 map
= isl_map_cow(map
);
1693 tabs
= isl_calloc_array(map
->ctx
, struct isl_tab
*, map
->n
);
1698 for (i
= 0; i
< map
->n
; ++i
) {
1699 tabs
[i
] = isl_tab_from_basic_map(map
->p
[i
], 0);
1702 if (!ISL_F_ISSET(map
->p
[i
], ISL_BASIC_MAP_NO_IMPLICIT
))
1703 if (isl_tab_detect_implicit_equalities(tabs
[i
]) < 0)
1705 map
->p
[i
] = isl_tab_make_equalities_explicit(tabs
[i
],
1709 if (!ISL_F_ISSET(map
->p
[i
], ISL_BASIC_MAP_NO_REDUNDANT
))
1710 if (isl_tab_detect_redundant(tabs
[i
]) < 0)
1713 for (i
= map
->n
- 1; i
>= 0; --i
)
1717 map
= coalesce(map
, tabs
);
1720 for (i
= 0; i
< map
->n
; ++i
) {
1721 map
->p
[i
] = isl_basic_map_update_from_tab(map
->p
[i
],
1723 map
->p
[i
] = isl_basic_map_gauss(map
->p
[i
], NULL
);
1724 map
->p
[i
] = isl_basic_map_finalize(map
->p
[i
]);
1727 ISL_F_SET(map
->p
[i
], ISL_BASIC_MAP_NO_IMPLICIT
);
1728 ISL_F_SET(map
->p
[i
], ISL_BASIC_MAP_NO_REDUNDANT
);
1731 for (i
= 0; i
< n
; ++i
)
1732 isl_tab_free(tabs
[i
]);
1739 for (i
= 0; i
< n
; ++i
)
1740 isl_tab_free(tabs
[i
]);
1746 /* For each pair of basic sets in the set, check if the union of the two
1747 * can be represented by a single basic set.
1748 * If so, replace the pair by the single basic set and start over.
1750 struct isl_set
*isl_set_coalesce(struct isl_set
*set
)
1752 return (struct isl_set
*)isl_map_coalesce((struct isl_map
*)set
);