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
);
58 dim
= isl_basic_map_total_dim(bmap_i
);
59 for (k
= 0; k
< bmap_i
->n_eq
; ++k
) {
60 for (l
= 0; l
< 2; ++l
) {
61 isl_seq_neg(bmap_i
->eq
[k
], bmap_i
->eq
[k
], 1+dim
);
62 eq
[2 * k
+ l
] = status_in(bmap_i
->eq
[k
], tab_j
);
63 if (eq
[2 * k
+ l
] == STATUS_ERROR
)
66 if (eq
[2 * k
] == STATUS_SEPARATE
||
67 eq
[2 * k
+ 1] == STATUS_SEPARATE
)
77 /* Compute the position of the inequalities of basic map "bmap_i"
78 * (also represented by "tab_i", if not NULL) with respect to the basic map
79 * represented by "tab_j".
81 static int *ineq_status_in(__isl_keep isl_basic_map
*bmap_i
,
82 struct isl_tab
*tab_i
, struct isl_tab
*tab_j
)
85 unsigned n_eq
= bmap_i
->n_eq
;
86 int *ineq
= isl_calloc_array(bmap_i
->ctx
, int, bmap_i
->n_ineq
);
88 for (k
= 0; k
< bmap_i
->n_ineq
; ++k
) {
89 if (tab_i
&& isl_tab_is_redundant(tab_i
, n_eq
+ k
)) {
90 ineq
[k
] = STATUS_REDUNDANT
;
93 ineq
[k
] = status_in(bmap_i
->ineq
[k
], tab_j
);
94 if (ineq
[k
] == STATUS_ERROR
)
96 if (ineq
[k
] == STATUS_SEPARATE
)
106 static int any(int *con
, unsigned len
, int status
)
110 for (i
= 0; i
< len
; ++i
)
111 if (con
[i
] == status
)
116 static int count(int *con
, unsigned len
, int status
)
121 for (i
= 0; i
< len
; ++i
)
122 if (con
[i
] == status
)
127 static int all(int *con
, unsigned len
, int status
)
131 for (i
= 0; i
< len
; ++i
) {
132 if (con
[i
] == STATUS_REDUNDANT
)
134 if (con
[i
] != status
)
140 static void drop(struct isl_map
*map
, int i
, struct isl_tab
**tabs
)
142 isl_basic_map_free(map
->p
[i
]);
143 isl_tab_free(tabs
[i
]);
145 if (i
!= map
->n
- 1) {
146 map
->p
[i
] = map
->p
[map
->n
- 1];
147 tabs
[i
] = tabs
[map
->n
- 1];
149 tabs
[map
->n
- 1] = NULL
;
153 /* Replace the pair of basic maps i and j by the basic map bounded
154 * by the valid constraints in both basic maps and the constraint
155 * in extra (if not NULL).
157 static int fuse(struct isl_map
*map
, int i
, int j
,
158 struct isl_tab
**tabs
, int *eq_i
, int *ineq_i
, int *eq_j
, int *ineq_j
,
159 __isl_keep isl_mat
*extra
)
162 struct isl_basic_map
*fused
= NULL
;
163 struct isl_tab
*fused_tab
= NULL
;
164 unsigned total
= isl_basic_map_total_dim(map
->p
[i
]);
165 unsigned extra_rows
= extra
? extra
->n_row
: 0;
167 fused
= isl_basic_map_alloc_space(isl_space_copy(map
->p
[i
]->dim
),
169 map
->p
[i
]->n_eq
+ map
->p
[j
]->n_eq
,
170 map
->p
[i
]->n_ineq
+ map
->p
[j
]->n_ineq
+ extra_rows
);
174 for (k
= 0; k
< map
->p
[i
]->n_eq
; ++k
) {
175 if (eq_i
&& (eq_i
[2 * k
] != STATUS_VALID
||
176 eq_i
[2 * k
+ 1] != STATUS_VALID
))
178 l
= isl_basic_map_alloc_equality(fused
);
181 isl_seq_cpy(fused
->eq
[l
], map
->p
[i
]->eq
[k
], 1 + total
);
184 for (k
= 0; k
< map
->p
[j
]->n_eq
; ++k
) {
185 if (eq_j
&& (eq_j
[2 * k
] != STATUS_VALID
||
186 eq_j
[2 * k
+ 1] != STATUS_VALID
))
188 l
= isl_basic_map_alloc_equality(fused
);
191 isl_seq_cpy(fused
->eq
[l
], map
->p
[j
]->eq
[k
], 1 + total
);
194 for (k
= 0; k
< map
->p
[i
]->n_ineq
; ++k
) {
195 if (ineq_i
[k
] != STATUS_VALID
)
197 l
= isl_basic_map_alloc_inequality(fused
);
200 isl_seq_cpy(fused
->ineq
[l
], map
->p
[i
]->ineq
[k
], 1 + total
);
203 for (k
= 0; k
< map
->p
[j
]->n_ineq
; ++k
) {
204 if (ineq_j
[k
] != STATUS_VALID
)
206 l
= isl_basic_map_alloc_inequality(fused
);
209 isl_seq_cpy(fused
->ineq
[l
], map
->p
[j
]->ineq
[k
], 1 + total
);
212 for (k
= 0; k
< map
->p
[i
]->n_div
; ++k
) {
213 int l
= isl_basic_map_alloc_div(fused
);
216 isl_seq_cpy(fused
->div
[l
], map
->p
[i
]->div
[k
], 1 + 1 + total
);
219 for (k
= 0; k
< extra_rows
; ++k
) {
220 l
= isl_basic_map_alloc_inequality(fused
);
223 isl_seq_cpy(fused
->ineq
[l
], extra
->row
[k
], 1 + total
);
226 fused
= isl_basic_map_gauss(fused
, NULL
);
227 ISL_F_SET(fused
, ISL_BASIC_MAP_FINAL
);
228 if (ISL_F_ISSET(map
->p
[i
], ISL_BASIC_MAP_RATIONAL
) &&
229 ISL_F_ISSET(map
->p
[j
], ISL_BASIC_MAP_RATIONAL
))
230 ISL_F_SET(fused
, ISL_BASIC_MAP_RATIONAL
);
232 fused_tab
= isl_tab_from_basic_map(fused
, 0);
233 if (isl_tab_detect_redundant(fused_tab
) < 0)
236 isl_basic_map_free(map
->p
[i
]);
238 isl_tab_free(tabs
[i
]);
244 isl_tab_free(fused_tab
);
245 isl_basic_map_free(fused
);
249 /* Given a pair of basic maps i and j such that all constraints are either
250 * "valid" or "cut", check if the facets corresponding to the "cut"
251 * constraints of i lie entirely within basic map j.
252 * If so, replace the pair by the basic map consisting of the valid
253 * constraints in both basic maps.
255 * To see that we are not introducing any extra points, call the
256 * two basic maps A and B and the resulting map U and let x
257 * be an element of U \setminus ( A \cup B ).
258 * Then there is a pair of cut constraints c_1 and c_2 in A and B such that x
259 * violates them. Let X be the intersection of U with the opposites
260 * of these constraints. Then x \in X.
261 * The facet corresponding to c_1 contains the corresponding facet of A.
262 * This facet is entirely contained in B, so c_2 is valid on the facet.
263 * However, since it is also (part of) a facet of X, -c_2 is also valid
264 * on the facet. This means c_2 is saturated on the facet, so c_1 and
265 * c_2 must be opposites of each other, but then x could not violate
268 static int check_facets(struct isl_map
*map
, int i
, int j
,
269 struct isl_tab
**tabs
, int *ineq_i
, int *ineq_j
)
272 struct isl_tab_undo
*snap
;
273 unsigned n_eq
= map
->p
[i
]->n_eq
;
275 snap
= isl_tab_snap(tabs
[i
]);
277 for (k
= 0; k
< map
->p
[i
]->n_ineq
; ++k
) {
278 if (ineq_i
[k
] != STATUS_CUT
)
280 if (isl_tab_select_facet(tabs
[i
], n_eq
+ k
) < 0)
282 for (l
= 0; l
< map
->p
[j
]->n_ineq
; ++l
) {
284 if (ineq_j
[l
] != STATUS_CUT
)
286 stat
= status_in(map
->p
[j
]->ineq
[l
], tabs
[i
]);
287 if (stat
!= STATUS_VALID
)
290 if (isl_tab_rollback(tabs
[i
], snap
) < 0)
292 if (l
< map
->p
[j
]->n_ineq
)
296 if (k
< map
->p
[i
]->n_ineq
)
299 return fuse(map
, i
, j
, tabs
, NULL
, ineq_i
, NULL
, ineq_j
, NULL
);
302 /* Check if basic map "i" contains the basic map represented
303 * by the tableau "tab".
305 static int contains(struct isl_map
*map
, int i
, int *ineq_i
,
311 dim
= isl_basic_map_total_dim(map
->p
[i
]);
312 for (k
= 0; k
< map
->p
[i
]->n_eq
; ++k
) {
313 for (l
= 0; l
< 2; ++l
) {
315 isl_seq_neg(map
->p
[i
]->eq
[k
], map
->p
[i
]->eq
[k
], 1+dim
);
316 stat
= status_in(map
->p
[i
]->eq
[k
], tab
);
317 if (stat
!= STATUS_VALID
)
322 for (k
= 0; k
< map
->p
[i
]->n_ineq
; ++k
) {
324 if (ineq_i
[k
] == STATUS_REDUNDANT
)
326 stat
= status_in(map
->p
[i
]->ineq
[k
], tab
);
327 if (stat
!= STATUS_VALID
)
333 /* Basic map "i" has an inequality (say "k") that is adjacent
334 * to some inequality of basic map "j". All the other inequalities
336 * Check if basic map "j" forms an extension of basic map "i".
338 * Note that this function is only called if some of the equalities or
339 * inequalities of basic map "j" do cut basic map "i". The function is
340 * correct even if there are no such cut constraints, but in that case
341 * the additional checks performed by this function are overkill.
343 * In particular, we replace constraint k, say f >= 0, by constraint
344 * f <= -1, add the inequalities of "j" that are valid for "i"
345 * and check if the result is a subset of basic map "j".
346 * If so, then we know that this result is exactly equal to basic map "j"
347 * since all its constraints are valid for basic map "j".
348 * By combining the valid constraints of "i" (all equalities and all
349 * inequalities except "k") and the valid constraints of "j" we therefore
350 * obtain a basic map that is equal to their union.
351 * In this case, there is no need to perform a rollback of the tableau
352 * since it is going to be destroyed in fuse().
358 * |_______| _ |_________\
370 static int is_adj_ineq_extension(__isl_keep isl_map
*map
, int i
, int j
,
371 struct isl_tab
**tabs
, int *eq_i
, int *ineq_i
, int *eq_j
, int *ineq_j
)
374 struct isl_tab_undo
*snap
;
375 unsigned n_eq
= map
->p
[i
]->n_eq
;
376 unsigned total
= isl_basic_map_total_dim(map
->p
[i
]);
379 if (isl_tab_extend_cons(tabs
[i
], 1 + map
->p
[j
]->n_ineq
) < 0)
382 for (k
= 0; k
< map
->p
[i
]->n_ineq
; ++k
)
383 if (ineq_i
[k
] == STATUS_ADJ_INEQ
)
385 if (k
>= map
->p
[i
]->n_ineq
)
386 isl_die(isl_map_get_ctx(map
), isl_error_internal
,
387 "ineq_i should have exactly one STATUS_ADJ_INEQ",
390 snap
= isl_tab_snap(tabs
[i
]);
392 if (isl_tab_unrestrict(tabs
[i
], n_eq
+ k
) < 0)
395 isl_seq_neg(map
->p
[i
]->ineq
[k
], map
->p
[i
]->ineq
[k
], 1 + total
);
396 isl_int_sub_ui(map
->p
[i
]->ineq
[k
][0], map
->p
[i
]->ineq
[k
][0], 1);
397 r
= isl_tab_add_ineq(tabs
[i
], map
->p
[i
]->ineq
[k
]);
398 isl_seq_neg(map
->p
[i
]->ineq
[k
], map
->p
[i
]->ineq
[k
], 1 + total
);
399 isl_int_sub_ui(map
->p
[i
]->ineq
[k
][0], map
->p
[i
]->ineq
[k
][0], 1);
403 for (k
= 0; k
< map
->p
[j
]->n_ineq
; ++k
) {
404 if (ineq_j
[k
] != STATUS_VALID
)
406 if (isl_tab_add_ineq(tabs
[i
], map
->p
[j
]->ineq
[k
]) < 0)
410 if (contains(map
, j
, ineq_j
, tabs
[i
]))
411 return fuse(map
, i
, j
, tabs
, eq_i
, ineq_i
, eq_j
, ineq_j
, NULL
);
413 if (isl_tab_rollback(tabs
[i
], snap
) < 0)
420 /* Both basic maps have at least one inequality with and adjacent
421 * (but opposite) inequality in the other basic map.
422 * Check that there are no cut constraints and that there is only
423 * a single pair of adjacent inequalities.
424 * If so, we can replace the pair by a single basic map described
425 * by all but the pair of adjacent inequalities.
426 * Any additional points introduced lie strictly between the two
427 * adjacent hyperplanes and can therefore be integral.
436 * The test for a single pair of adjancent inequalities is important
437 * for avoiding the combination of two basic maps like the following
447 * If there are some cut constraints on one side, then we may
448 * still be able to fuse the two basic maps, but we need to perform
449 * some additional checks in is_adj_ineq_extension.
451 static int check_adj_ineq(struct isl_map
*map
, int i
, int j
,
452 struct isl_tab
**tabs
, int *eq_i
, int *ineq_i
, int *eq_j
, int *ineq_j
)
454 int count_i
, count_j
;
457 count_i
= count(ineq_i
, map
->p
[i
]->n_ineq
, STATUS_ADJ_INEQ
);
458 count_j
= count(ineq_j
, map
->p
[j
]->n_ineq
, STATUS_ADJ_INEQ
);
460 if (count_i
!= 1 && count_j
!= 1)
463 cut_i
= any(eq_i
, 2 * map
->p
[i
]->n_eq
, STATUS_CUT
) ||
464 any(ineq_i
, map
->p
[i
]->n_ineq
, STATUS_CUT
);
465 cut_j
= any(eq_j
, 2 * map
->p
[j
]->n_eq
, STATUS_CUT
) ||
466 any(ineq_j
, map
->p
[j
]->n_ineq
, STATUS_CUT
);
468 if (!cut_i
&& !cut_j
&& count_i
== 1 && count_j
== 1)
469 return fuse(map
, i
, j
, tabs
, NULL
, ineq_i
, NULL
, ineq_j
, NULL
);
471 if (count_i
== 1 && !cut_i
)
472 return is_adj_ineq_extension(map
, i
, j
, tabs
,
473 eq_i
, ineq_i
, eq_j
, ineq_j
);
475 if (count_j
== 1 && !cut_j
)
476 return is_adj_ineq_extension(map
, j
, i
, tabs
,
477 eq_j
, ineq_j
, eq_i
, ineq_i
);
482 /* Basic map "i" has an inequality "k" that is adjacent to some equality
483 * of basic map "j". All the other inequalities are valid for "j".
484 * Check if basic map "j" forms an extension of basic map "i".
486 * In particular, we relax constraint "k", compute the corresponding
487 * facet and check whether it is included in the other basic map.
488 * If so, we know that relaxing the constraint extends the basic
489 * map with exactly the other basic map (we already know that this
490 * other basic map is included in the extension, because there
491 * were no "cut" inequalities in "i") and we can replace the
492 * two basic maps by this extension.
500 static int is_adj_eq_extension(struct isl_map
*map
, int i
, int j
, int k
,
501 struct isl_tab
**tabs
, int *eq_i
, int *ineq_i
, int *eq_j
, int *ineq_j
)
505 struct isl_tab_undo
*snap
, *snap2
;
506 unsigned n_eq
= map
->p
[i
]->n_eq
;
508 if (isl_tab_is_equality(tabs
[i
], n_eq
+ k
))
511 snap
= isl_tab_snap(tabs
[i
]);
512 tabs
[i
] = isl_tab_relax(tabs
[i
], n_eq
+ k
);
513 snap2
= isl_tab_snap(tabs
[i
]);
514 if (isl_tab_select_facet(tabs
[i
], n_eq
+ k
) < 0)
516 super
= contains(map
, j
, ineq_j
, tabs
[i
]);
518 if (isl_tab_rollback(tabs
[i
], snap2
) < 0)
520 map
->p
[i
] = isl_basic_map_cow(map
->p
[i
]);
523 isl_int_add_ui(map
->p
[i
]->ineq
[k
][0], map
->p
[i
]->ineq
[k
][0], 1);
524 ISL_F_SET(map
->p
[i
], ISL_BASIC_MAP_FINAL
);
528 if (isl_tab_rollback(tabs
[i
], snap
) < 0)
534 /* Data structure that keeps track of the wrapping constraints
535 * and of information to bound the coefficients of those constraints.
537 * bound is set if we want to apply a bound on the coefficients
538 * mat contains the wrapping constraints
539 * max is the bound on the coefficients (if bound is set)
547 /* Update wraps->max to be greater than or equal to the coefficients
548 * in the equalities and inequalities of bmap that can be removed if we end up
551 static void wraps_update_max(struct isl_wraps
*wraps
,
552 __isl_keep isl_basic_map
*bmap
, int *eq
, int *ineq
)
556 unsigned total
= isl_basic_map_total_dim(bmap
);
560 for (k
= 0; k
< bmap
->n_eq
; ++k
) {
561 if (eq
[2 * k
] == STATUS_VALID
&&
562 eq
[2 * k
+ 1] == STATUS_VALID
)
564 isl_seq_abs_max(bmap
->eq
[k
] + 1, total
, &max_k
);
565 if (isl_int_abs_gt(max_k
, wraps
->max
))
566 isl_int_set(wraps
->max
, max_k
);
569 for (k
= 0; k
< bmap
->n_ineq
; ++k
) {
570 if (ineq
[k
] == STATUS_VALID
|| ineq
[k
] == STATUS_REDUNDANT
)
572 isl_seq_abs_max(bmap
->ineq
[k
] + 1, total
, &max_k
);
573 if (isl_int_abs_gt(max_k
, wraps
->max
))
574 isl_int_set(wraps
->max
, max_k
);
577 isl_int_clear(max_k
);
580 /* Initialize the isl_wraps data structure.
581 * If we want to bound the coefficients of the wrapping constraints,
582 * we set wraps->max to the largest coefficient
583 * in the equalities and inequalities that can be removed if we end up
586 static void wraps_init(struct isl_wraps
*wraps
, __isl_take isl_mat
*mat
,
587 __isl_keep isl_map
*map
, int i
, int j
,
588 int *eq_i
, int *ineq_i
, int *eq_j
, int *ineq_j
)
596 ctx
= isl_mat_get_ctx(mat
);
597 wraps
->bound
= isl_options_get_coalesce_bounded_wrapping(ctx
);
600 isl_int_init(wraps
->max
);
601 isl_int_set_si(wraps
->max
, 0);
602 wraps_update_max(wraps
, map
->p
[i
], eq_i
, ineq_i
);
603 wraps_update_max(wraps
, map
->p
[j
], eq_j
, ineq_j
);
606 /* Free the contents of the isl_wraps data structure.
608 static void wraps_free(struct isl_wraps
*wraps
)
610 isl_mat_free(wraps
->mat
);
612 isl_int_clear(wraps
->max
);
615 /* Is the wrapping constraint in row "row" allowed?
617 * If wraps->bound is set, we check that none of the coefficients
618 * is greater than wraps->max.
620 static int allow_wrap(struct isl_wraps
*wraps
, int row
)
627 for (i
= 1; i
< wraps
->mat
->n_col
; ++i
)
628 if (isl_int_abs_gt(wraps
->mat
->row
[row
][i
], wraps
->max
))
634 /* For each non-redundant constraint in "bmap" (as determined by "tab"),
635 * wrap the constraint around "bound" such that it includes the whole
636 * set "set" and append the resulting constraint to "wraps".
637 * "wraps" is assumed to have been pre-allocated to the appropriate size.
638 * wraps->n_row is the number of actual wrapped constraints that have
640 * If any of the wrapping problems results in a constraint that is
641 * identical to "bound", then this means that "set" is unbounded in such
642 * way that no wrapping is possible. If this happens then wraps->n_row
644 * Similarly, if we want to bound the coefficients of the wrapping
645 * constraints and a newly added wrapping constraint does not
646 * satisfy the bound, then wraps->n_row is also reset to zero.
648 static int add_wraps(struct isl_wraps
*wraps
, __isl_keep isl_basic_map
*bmap
,
649 struct isl_tab
*tab
, isl_int
*bound
, __isl_keep isl_set
*set
)
653 unsigned total
= isl_basic_map_total_dim(bmap
);
655 w
= wraps
->mat
->n_row
;
657 for (l
= 0; l
< bmap
->n_ineq
; ++l
) {
658 if (isl_seq_is_neg(bound
, bmap
->ineq
[l
], 1 + total
))
660 if (isl_seq_eq(bound
, bmap
->ineq
[l
], 1 + total
))
662 if (isl_tab_is_redundant(tab
, bmap
->n_eq
+ l
))
665 isl_seq_cpy(wraps
->mat
->row
[w
], bound
, 1 + total
);
666 if (!isl_set_wrap_facet(set
, wraps
->mat
->row
[w
], bmap
->ineq
[l
]))
668 if (isl_seq_eq(wraps
->mat
->row
[w
], bound
, 1 + total
))
670 if (!allow_wrap(wraps
, w
))
674 for (l
= 0; l
< bmap
->n_eq
; ++l
) {
675 if (isl_seq_is_neg(bound
, bmap
->eq
[l
], 1 + total
))
677 if (isl_seq_eq(bound
, bmap
->eq
[l
], 1 + total
))
680 isl_seq_cpy(wraps
->mat
->row
[w
], bound
, 1 + total
);
681 isl_seq_neg(wraps
->mat
->row
[w
+ 1], bmap
->eq
[l
], 1 + total
);
682 if (!isl_set_wrap_facet(set
, wraps
->mat
->row
[w
],
683 wraps
->mat
->row
[w
+ 1]))
685 if (isl_seq_eq(wraps
->mat
->row
[w
], bound
, 1 + total
))
687 if (!allow_wrap(wraps
, w
))
691 isl_seq_cpy(wraps
->mat
->row
[w
], bound
, 1 + total
);
692 if (!isl_set_wrap_facet(set
, wraps
->mat
->row
[w
], bmap
->eq
[l
]))
694 if (isl_seq_eq(wraps
->mat
->row
[w
], bound
, 1 + total
))
696 if (!allow_wrap(wraps
, w
))
701 wraps
->mat
->n_row
= w
;
704 wraps
->mat
->n_row
= 0;
708 /* Check if the constraints in "wraps" from "first" until the last
709 * are all valid for the basic set represented by "tab".
710 * If not, wraps->n_row is set to zero.
712 static int check_wraps(__isl_keep isl_mat
*wraps
, int first
,
717 for (i
= first
; i
< wraps
->n_row
; ++i
) {
718 enum isl_ineq_type type
;
719 type
= isl_tab_ineq_type(tab
, wraps
->row
[i
]);
720 if (type
== isl_ineq_error
)
722 if (type
== isl_ineq_redundant
)
731 /* Return a set that corresponds to the non-redudant constraints
732 * (as recorded in tab) of bmap.
734 * It's important to remove the redundant constraints as some
735 * of the other constraints may have been modified after the
736 * constraints were marked redundant.
737 * In particular, a constraint may have been relaxed.
738 * Redundant constraints are ignored when a constraint is relaxed
739 * and should therefore continue to be ignored ever after.
740 * Otherwise, the relaxation might be thwarted by some of
743 static __isl_give isl_set
*set_from_updated_bmap(__isl_keep isl_basic_map
*bmap
,
746 bmap
= isl_basic_map_copy(bmap
);
747 bmap
= isl_basic_map_cow(bmap
);
748 bmap
= isl_basic_map_update_from_tab(bmap
, tab
);
749 return isl_set_from_basic_set(isl_basic_map_underlying_set(bmap
));
752 /* Given a basic set i with a constraint k that is adjacent to either the
753 * whole of basic set j or a facet of basic set j, check if we can wrap
754 * both the facet corresponding to k and the facet of j (or the whole of j)
755 * around their ridges to include the other set.
756 * If so, replace the pair of basic sets by their union.
758 * All constraints of i (except k) are assumed to be valid for j.
760 * However, the constraints of j may not be valid for i and so
761 * we have to check that the wrapping constraints for j are valid for i.
763 * In the case where j has a facet adjacent to i, tab[j] is assumed
764 * to have been restricted to this facet, so that the non-redundant
765 * constraints in tab[j] are the ridges of the facet.
766 * Note that for the purpose of wrapping, it does not matter whether
767 * we wrap the ridges of i around the whole of j or just around
768 * the facet since all the other constraints are assumed to be valid for j.
769 * In practice, we wrap to include the whole of j.
778 static int can_wrap_in_facet(struct isl_map
*map
, int i
, int j
, int k
,
779 struct isl_tab
**tabs
, int *eq_i
, int *ineq_i
, int *eq_j
, int *ineq_j
)
782 struct isl_wraps wraps
;
784 struct isl_set
*set_i
= NULL
;
785 struct isl_set
*set_j
= NULL
;
786 struct isl_vec
*bound
= NULL
;
787 unsigned total
= isl_basic_map_total_dim(map
->p
[i
]);
788 struct isl_tab_undo
*snap
;
791 set_i
= set_from_updated_bmap(map
->p
[i
], tabs
[i
]);
792 set_j
= set_from_updated_bmap(map
->p
[j
], tabs
[j
]);
793 mat
= isl_mat_alloc(map
->ctx
, 2 * (map
->p
[i
]->n_eq
+ map
->p
[j
]->n_eq
) +
794 map
->p
[i
]->n_ineq
+ map
->p
[j
]->n_ineq
,
796 wraps_init(&wraps
, mat
, map
, i
, j
, eq_i
, ineq_i
, eq_j
, ineq_j
);
797 bound
= isl_vec_alloc(map
->ctx
, 1 + total
);
798 if (!set_i
|| !set_j
|| !wraps
.mat
|| !bound
)
801 isl_seq_cpy(bound
->el
, map
->p
[i
]->ineq
[k
], 1 + total
);
802 isl_int_add_ui(bound
->el
[0], bound
->el
[0], 1);
804 isl_seq_cpy(wraps
.mat
->row
[0], bound
->el
, 1 + total
);
805 wraps
.mat
->n_row
= 1;
807 if (add_wraps(&wraps
, map
->p
[j
], tabs
[j
], bound
->el
, set_i
) < 0)
809 if (!wraps
.mat
->n_row
)
812 snap
= isl_tab_snap(tabs
[i
]);
814 if (isl_tab_select_facet(tabs
[i
], map
->p
[i
]->n_eq
+ k
) < 0)
816 if (isl_tab_detect_redundant(tabs
[i
]) < 0)
819 isl_seq_neg(bound
->el
, map
->p
[i
]->ineq
[k
], 1 + total
);
821 n
= wraps
.mat
->n_row
;
822 if (add_wraps(&wraps
, map
->p
[i
], tabs
[i
], bound
->el
, set_j
) < 0)
825 if (isl_tab_rollback(tabs
[i
], snap
) < 0)
827 if (check_wraps(wraps
.mat
, n
, tabs
[i
]) < 0)
829 if (!wraps
.mat
->n_row
)
832 changed
= fuse(map
, i
, j
, tabs
, eq_i
, ineq_i
, eq_j
, ineq_j
, wraps
.mat
);
851 /* Set the is_redundant property of the "n" constraints in "cuts",
853 * This is a fairly tricky operation as it bypasses isl_tab.c.
854 * The reason we want to temporarily mark some constraints redundant
855 * is that we want to ignore them in add_wraps.
857 * Initially all cut constraints are non-redundant, but the
858 * selection of a facet right before the call to this function
859 * may have made some of them redundant.
860 * Likewise, the same constraints are marked non-redundant
861 * in the second call to this function, before they are officially
862 * made non-redundant again in the subsequent rollback.
864 static void set_is_redundant(struct isl_tab
*tab
, unsigned n_eq
,
865 int *cuts
, int n
, int k
, int v
)
869 for (l
= 0; l
< n
; ++l
) {
872 tab
->con
[n_eq
+ cuts
[l
]].is_redundant
= v
;
876 /* Given a pair of basic maps i and j such that j sticks out
877 * of i at n cut constraints, each time by at most one,
878 * try to compute wrapping constraints and replace the two
879 * basic maps by a single basic map.
880 * The other constraints of i are assumed to be valid for j.
882 * The facets of i corresponding to the cut constraints are
883 * wrapped around their ridges, except those ridges determined
884 * by any of the other cut constraints.
885 * The intersections of cut constraints need to be ignored
886 * as the result of wrapping one cut constraint around another
887 * would result in a constraint cutting the union.
888 * In each case, the facets are wrapped to include the union
889 * of the two basic maps.
891 * The pieces of j that lie at an offset of exactly one from
892 * one of the cut constraints of i are wrapped around their edges.
893 * Here, there is no need to ignore intersections because we
894 * are wrapping around the union of the two basic maps.
896 * If any wrapping fails, i.e., if we cannot wrap to touch
897 * the union, then we give up.
898 * Otherwise, the pair of basic maps is replaced by their union.
900 static int wrap_in_facets(struct isl_map
*map
, int i
, int j
,
901 int *cuts
, int n
, struct isl_tab
**tabs
,
902 int *eq_i
, int *ineq_i
, int *eq_j
, int *ineq_j
)
905 struct isl_wraps wraps
;
908 isl_vec
*bound
= NULL
;
909 unsigned total
= isl_basic_map_total_dim(map
->p
[i
]);
912 struct isl_tab_undo
*snap_i
, *snap_j
;
914 if (isl_tab_extend_cons(tabs
[j
], 1) < 0)
917 max_wrap
= 2 * (map
->p
[i
]->n_eq
+ map
->p
[j
]->n_eq
) +
918 map
->p
[i
]->n_ineq
+ map
->p
[j
]->n_ineq
;
921 set
= isl_set_union(set_from_updated_bmap(map
->p
[i
], tabs
[i
]),
922 set_from_updated_bmap(map
->p
[j
], tabs
[j
]));
923 mat
= isl_mat_alloc(map
->ctx
, max_wrap
, 1 + total
);
924 wraps_init(&wraps
, mat
, map
, i
, j
, eq_i
, ineq_i
, eq_j
, ineq_j
);
925 bound
= isl_vec_alloc(map
->ctx
, 1 + total
);
926 if (!set
|| !wraps
.mat
|| !bound
)
929 snap_i
= isl_tab_snap(tabs
[i
]);
930 snap_j
= isl_tab_snap(tabs
[j
]);
932 wraps
.mat
->n_row
= 0;
934 for (k
= 0; k
< n
; ++k
) {
935 if (isl_tab_select_facet(tabs
[i
], map
->p
[i
]->n_eq
+ cuts
[k
]) < 0)
937 if (isl_tab_detect_redundant(tabs
[i
]) < 0)
939 set_is_redundant(tabs
[i
], map
->p
[i
]->n_eq
, cuts
, n
, k
, 1);
941 isl_seq_neg(bound
->el
, map
->p
[i
]->ineq
[cuts
[k
]], 1 + total
);
942 if (!tabs
[i
]->empty
&&
943 add_wraps(&wraps
, map
->p
[i
], tabs
[i
], bound
->el
, set
) < 0)
946 set_is_redundant(tabs
[i
], map
->p
[i
]->n_eq
, cuts
, n
, k
, 0);
947 if (isl_tab_rollback(tabs
[i
], snap_i
) < 0)
952 if (!wraps
.mat
->n_row
)
955 isl_seq_cpy(bound
->el
, map
->p
[i
]->ineq
[cuts
[k
]], 1 + total
);
956 isl_int_add_ui(bound
->el
[0], bound
->el
[0], 1);
957 if (isl_tab_add_eq(tabs
[j
], bound
->el
) < 0)
959 if (isl_tab_detect_redundant(tabs
[j
]) < 0)
962 if (!tabs
[j
]->empty
&&
963 add_wraps(&wraps
, map
->p
[j
], tabs
[j
], bound
->el
, set
) < 0)
966 if (isl_tab_rollback(tabs
[j
], snap_j
) < 0)
969 if (!wraps
.mat
->n_row
)
974 changed
= fuse(map
, i
, j
, tabs
,
975 eq_i
, ineq_i
, eq_j
, ineq_j
, wraps
.mat
);
989 /* Given two basic sets i and j such that i has no cut equalities,
990 * check if relaxing all the cut inequalities of i by one turns
991 * them into valid constraint for j and check if we can wrap in
992 * the bits that are sticking out.
993 * If so, replace the pair by their union.
995 * We first check if all relaxed cut inequalities of i are valid for j
996 * and then try to wrap in the intersections of the relaxed cut inequalities
999 * During this wrapping, we consider the points of j that lie at a distance
1000 * of exactly 1 from i. In particular, we ignore the points that lie in
1001 * between this lower-dimensional space and the basic map i.
1002 * We can therefore only apply this to integer maps.
1028 * Wrapping can fail if the result of wrapping one of the facets
1029 * around its edges does not produce any new facet constraint.
1030 * In particular, this happens when we try to wrap in unbounded sets.
1032 * _______________________________________________________________________
1036 * |_| |_________________________________________________________________
1039 * The following is not an acceptable result of coalescing the above two
1040 * sets as it includes extra integer points.
1041 * _______________________________________________________________________
1046 * \______________________________________________________________________
1048 static int can_wrap_in_set(struct isl_map
*map
, int i
, int j
,
1049 struct isl_tab
**tabs
, int *eq_i
, int *ineq_i
, int *eq_j
, int *ineq_j
)
1056 if (ISL_F_ISSET(map
->p
[i
], ISL_BASIC_MAP_RATIONAL
) ||
1057 ISL_F_ISSET(map
->p
[j
], ISL_BASIC_MAP_RATIONAL
))
1060 n
= count(ineq_i
, map
->p
[i
]->n_ineq
, STATUS_CUT
);
1064 cuts
= isl_alloc_array(map
->ctx
, int, n
);
1068 for (k
= 0, m
= 0; m
< n
; ++k
) {
1069 enum isl_ineq_type type
;
1071 if (ineq_i
[k
] != STATUS_CUT
)
1074 isl_int_add_ui(map
->p
[i
]->ineq
[k
][0], map
->p
[i
]->ineq
[k
][0], 1);
1075 type
= isl_tab_ineq_type(tabs
[j
], map
->p
[i
]->ineq
[k
]);
1076 isl_int_sub_ui(map
->p
[i
]->ineq
[k
][0], map
->p
[i
]->ineq
[k
][0], 1);
1077 if (type
== isl_ineq_error
)
1079 if (type
!= isl_ineq_redundant
)
1086 changed
= wrap_in_facets(map
, i
, j
, cuts
, n
, tabs
,
1087 eq_i
, ineq_i
, eq_j
, ineq_j
);
1097 /* Check if either i or j has a single cut constraint that can
1098 * be used to wrap in (a facet of) the other basic set.
1099 * if so, replace the pair by their union.
1101 static int check_wrap(struct isl_map
*map
, int i
, int j
,
1102 struct isl_tab
**tabs
, int *eq_i
, int *ineq_i
, int *eq_j
, int *ineq_j
)
1106 if (!any(eq_i
, 2 * map
->p
[i
]->n_eq
, STATUS_CUT
))
1107 changed
= can_wrap_in_set(map
, i
, j
, tabs
,
1108 eq_i
, ineq_i
, eq_j
, ineq_j
);
1112 if (!any(eq_j
, 2 * map
->p
[j
]->n_eq
, STATUS_CUT
))
1113 changed
= can_wrap_in_set(map
, j
, i
, tabs
,
1114 eq_j
, ineq_j
, eq_i
, ineq_i
);
1118 /* At least one of the basic maps has an equality that is adjacent
1119 * to inequality. Make sure that only one of the basic maps has
1120 * such an equality and that the other basic map has exactly one
1121 * inequality adjacent to an equality.
1122 * We call the basic map that has the inequality "i" and the basic
1123 * map that has the equality "j".
1124 * If "i" has any "cut" (in)equality, then relaxing the inequality
1125 * by one would not result in a basic map that contains the other
1128 static int check_adj_eq(struct isl_map
*map
, int i
, int j
,
1129 struct isl_tab
**tabs
, int *eq_i
, int *ineq_i
, int *eq_j
, int *ineq_j
)
1134 if (any(eq_i
, 2 * map
->p
[i
]->n_eq
, STATUS_ADJ_INEQ
) &&
1135 any(eq_j
, 2 * map
->p
[j
]->n_eq
, STATUS_ADJ_INEQ
))
1136 /* ADJ EQ TOO MANY */
1139 if (any(eq_i
, 2 * map
->p
[i
]->n_eq
, STATUS_ADJ_INEQ
))
1140 return check_adj_eq(map
, j
, i
, tabs
,
1141 eq_j
, ineq_j
, eq_i
, ineq_i
);
1143 /* j has an equality adjacent to an inequality in i */
1145 if (any(eq_i
, 2 * map
->p
[i
]->n_eq
, STATUS_CUT
))
1147 if (any(ineq_i
, map
->p
[i
]->n_ineq
, STATUS_CUT
))
1150 if (count(ineq_i
, map
->p
[i
]->n_ineq
, STATUS_ADJ_EQ
) != 1 ||
1151 any(ineq_j
, map
->p
[j
]->n_ineq
, STATUS_ADJ_EQ
) ||
1152 any(ineq_i
, map
->p
[i
]->n_ineq
, STATUS_ADJ_INEQ
) ||
1153 any(ineq_j
, map
->p
[j
]->n_ineq
, STATUS_ADJ_INEQ
))
1154 /* ADJ EQ TOO MANY */
1157 for (k
= 0; k
< map
->p
[i
]->n_ineq
; ++k
)
1158 if (ineq_i
[k
] == STATUS_ADJ_EQ
)
1161 changed
= is_adj_eq_extension(map
, i
, j
, k
, tabs
,
1162 eq_i
, ineq_i
, eq_j
, ineq_j
);
1166 if (count(eq_j
, 2 * map
->p
[j
]->n_eq
, STATUS_ADJ_INEQ
) != 1)
1169 changed
= can_wrap_in_facet(map
, i
, j
, k
, tabs
, eq_i
, ineq_i
, eq_j
, ineq_j
);
1174 /* The two basic maps lie on adjacent hyperplanes. In particular,
1175 * basic map "i" has an equality that lies parallel to basic map "j".
1176 * Check if we can wrap the facets around the parallel hyperplanes
1177 * to include the other set.
1179 * We perform basically the same operations as can_wrap_in_facet,
1180 * except that we don't need to select a facet of one of the sets.
1186 * We only allow one equality of "i" to be adjacent to an equality of "j"
1187 * to avoid coalescing
1189 * [m, n] -> { [x, y] -> [x, 1 + y] : x >= 1 and y >= 1 and
1190 * x <= 10 and y <= 10;
1191 * [x, y] -> [1 + x, y] : x >= 1 and x <= 20 and
1192 * y >= 5 and y <= 15 }
1196 * [m, n] -> { [x, y] -> [x2, y2] : x >= 1 and 10y2 <= 20 - x + 10y and
1197 * 4y2 >= 5 + 3y and 5y2 <= 15 + 4y and
1198 * y2 <= 1 + x + y - x2 and y2 >= y and
1199 * y2 >= 1 + x + y - x2 }
1201 static int check_eq_adj_eq(struct isl_map
*map
, int i
, int j
,
1202 struct isl_tab
**tabs
, int *eq_i
, int *ineq_i
, int *eq_j
, int *ineq_j
)
1206 struct isl_wraps wraps
;
1208 struct isl_set
*set_i
= NULL
;
1209 struct isl_set
*set_j
= NULL
;
1210 struct isl_vec
*bound
= NULL
;
1211 unsigned total
= isl_basic_map_total_dim(map
->p
[i
]);
1213 if (count(eq_i
, 2 * map
->p
[i
]->n_eq
, STATUS_ADJ_EQ
) != 1)
1216 for (k
= 0; k
< 2 * map
->p
[i
]->n_eq
; ++k
)
1217 if (eq_i
[k
] == STATUS_ADJ_EQ
)
1220 set_i
= set_from_updated_bmap(map
->p
[i
], tabs
[i
]);
1221 set_j
= set_from_updated_bmap(map
->p
[j
], tabs
[j
]);
1222 mat
= isl_mat_alloc(map
->ctx
, 2 * (map
->p
[i
]->n_eq
+ map
->p
[j
]->n_eq
) +
1223 map
->p
[i
]->n_ineq
+ map
->p
[j
]->n_ineq
,
1225 wraps_init(&wraps
, mat
, map
, i
, j
, eq_i
, ineq_i
, eq_j
, ineq_j
);
1226 bound
= isl_vec_alloc(map
->ctx
, 1 + total
);
1227 if (!set_i
|| !set_j
|| !wraps
.mat
|| !bound
)
1231 isl_seq_neg(bound
->el
, map
->p
[i
]->eq
[k
/ 2], 1 + total
);
1233 isl_seq_cpy(bound
->el
, map
->p
[i
]->eq
[k
/ 2], 1 + total
);
1234 isl_int_add_ui(bound
->el
[0], bound
->el
[0], 1);
1236 isl_seq_cpy(wraps
.mat
->row
[0], bound
->el
, 1 + total
);
1237 wraps
.mat
->n_row
= 1;
1239 if (add_wraps(&wraps
, map
->p
[j
], tabs
[j
], bound
->el
, set_i
) < 0)
1241 if (!wraps
.mat
->n_row
)
1244 isl_int_sub_ui(bound
->el
[0], bound
->el
[0], 1);
1245 isl_seq_neg(bound
->el
, bound
->el
, 1 + total
);
1247 isl_seq_cpy(wraps
.mat
->row
[wraps
.mat
->n_row
], bound
->el
, 1 + total
);
1250 if (add_wraps(&wraps
, map
->p
[i
], tabs
[i
], bound
->el
, set_j
) < 0)
1252 if (!wraps
.mat
->n_row
)
1255 changed
= fuse(map
, i
, j
, tabs
, eq_i
, ineq_i
, eq_j
, ineq_j
, wraps
.mat
);
1258 error
: changed
= -1;
1263 isl_set_free(set_i
);
1264 isl_set_free(set_j
);
1265 isl_vec_free(bound
);
1270 /* Check if the union of the given pair of basic maps
1271 * can be represented by a single basic map.
1272 * If so, replace the pair by the single basic map and return 1.
1273 * Otherwise, return 0;
1274 * The two basic maps are assumed to live in the same local space.
1276 * We first check the effect of each constraint of one basic map
1277 * on the other basic map.
1278 * The constraint may be
1279 * redundant the constraint is redundant in its own
1280 * basic map and should be ignore and removed
1282 * valid all (integer) points of the other basic map
1283 * satisfy the constraint
1284 * separate no (integer) point of the other basic map
1285 * satisfies the constraint
1286 * cut some but not all points of the other basic map
1287 * satisfy the constraint
1288 * adj_eq the given constraint is adjacent (on the outside)
1289 * to an equality of the other basic map
1290 * adj_ineq the given constraint is adjacent (on the outside)
1291 * to an inequality of the other basic map
1293 * We consider seven cases in which we can replace the pair by a single
1294 * basic map. We ignore all "redundant" constraints.
1296 * 1. all constraints of one basic map are valid
1297 * => the other basic map is a subset and can be removed
1299 * 2. all constraints of both basic maps are either "valid" or "cut"
1300 * and the facets corresponding to the "cut" constraints
1301 * of one of the basic maps lies entirely inside the other basic map
1302 * => the pair can be replaced by a basic map consisting
1303 * of the valid constraints in both basic maps
1305 * 3. there is a single pair of adjacent inequalities
1306 * (all other constraints are "valid")
1307 * => the pair can be replaced by a basic map consisting
1308 * of the valid constraints in both basic maps
1310 * 4. one basic map has a single adjacent inequality, while the other
1311 * constraints are "valid". The other basic map has some
1312 * "cut" constraints, but replacing the adjacent inequality by
1313 * its opposite and adding the valid constraints of the other
1314 * basic map results in a subset of the other basic map
1315 * => the pair can be replaced by a basic map consisting
1316 * of the valid constraints in both basic maps
1318 * 5. there is a single adjacent pair of an inequality and an equality,
1319 * the other constraints of the basic map containing the inequality are
1320 * "valid". Moreover, if the inequality the basic map is relaxed
1321 * and then turned into an equality, then resulting facet lies
1322 * entirely inside the other basic map
1323 * => the pair can be replaced by the basic map containing
1324 * the inequality, with the inequality relaxed.
1326 * 6. there is a single adjacent pair of an inequality and an equality,
1327 * the other constraints of the basic map containing the inequality are
1328 * "valid". Moreover, the facets corresponding to both
1329 * the inequality and the equality can be wrapped around their
1330 * ridges to include the other basic map
1331 * => the pair can be replaced by a basic map consisting
1332 * of the valid constraints in both basic maps together
1333 * with all wrapping constraints
1335 * 7. one of the basic maps extends beyond the other by at most one.
1336 * Moreover, the facets corresponding to the cut constraints and
1337 * the pieces of the other basic map at offset one from these cut
1338 * constraints can be wrapped around their ridges to include
1339 * the union of the two basic maps
1340 * => the pair can be replaced by a basic map consisting
1341 * of the valid constraints in both basic maps together
1342 * with all wrapping constraints
1344 * 8. the two basic maps live in adjacent hyperplanes. In principle
1345 * such sets can always be combined through wrapping, but we impose
1346 * that there is only one such pair, to avoid overeager coalescing.
1348 * Throughout the computation, we maintain a collection of tableaus
1349 * corresponding to the basic maps. When the basic maps are dropped
1350 * or combined, the tableaus are modified accordingly.
1352 static int coalesce_local_pair(__isl_keep isl_map
*map
, int i
, int j
,
1353 struct isl_tab
**tabs
)
1361 eq_i
= eq_status_in(map
->p
[i
], tabs
[j
]);
1362 if (map
->p
[i
]->n_eq
&& !eq_i
)
1364 if (any(eq_i
, 2 * map
->p
[i
]->n_eq
, STATUS_ERROR
))
1366 if (any(eq_i
, 2 * map
->p
[i
]->n_eq
, STATUS_SEPARATE
))
1369 eq_j
= eq_status_in(map
->p
[j
], tabs
[i
]);
1370 if (map
->p
[j
]->n_eq
&& !eq_j
)
1372 if (any(eq_j
, 2 * map
->p
[j
]->n_eq
, STATUS_ERROR
))
1374 if (any(eq_j
, 2 * map
->p
[j
]->n_eq
, STATUS_SEPARATE
))
1377 ineq_i
= ineq_status_in(map
->p
[i
], tabs
[i
], tabs
[j
]);
1378 if (map
->p
[i
]->n_ineq
&& !ineq_i
)
1380 if (any(ineq_i
, map
->p
[i
]->n_ineq
, STATUS_ERROR
))
1382 if (any(ineq_i
, map
->p
[i
]->n_ineq
, STATUS_SEPARATE
))
1385 ineq_j
= ineq_status_in(map
->p
[j
], tabs
[j
], tabs
[i
]);
1386 if (map
->p
[j
]->n_ineq
&& !ineq_j
)
1388 if (any(ineq_j
, map
->p
[j
]->n_ineq
, STATUS_ERROR
))
1390 if (any(ineq_j
, map
->p
[j
]->n_ineq
, STATUS_SEPARATE
))
1393 if (all(eq_i
, 2 * map
->p
[i
]->n_eq
, STATUS_VALID
) &&
1394 all(ineq_i
, map
->p
[i
]->n_ineq
, STATUS_VALID
)) {
1397 } else if (all(eq_j
, 2 * map
->p
[j
]->n_eq
, STATUS_VALID
) &&
1398 all(ineq_j
, map
->p
[j
]->n_ineq
, STATUS_VALID
)) {
1401 } else if (any(eq_i
, 2 * map
->p
[i
]->n_eq
, STATUS_ADJ_EQ
)) {
1402 changed
= check_eq_adj_eq(map
, i
, j
, tabs
,
1403 eq_i
, ineq_i
, eq_j
, ineq_j
);
1404 } else if (any(eq_j
, 2 * map
->p
[j
]->n_eq
, STATUS_ADJ_EQ
)) {
1405 changed
= check_eq_adj_eq(map
, j
, i
, tabs
,
1406 eq_j
, ineq_j
, eq_i
, ineq_i
);
1407 } else if (any(eq_i
, 2 * map
->p
[i
]->n_eq
, STATUS_ADJ_INEQ
) ||
1408 any(eq_j
, 2 * map
->p
[j
]->n_eq
, STATUS_ADJ_INEQ
)) {
1409 changed
= check_adj_eq(map
, i
, j
, tabs
,
1410 eq_i
, ineq_i
, eq_j
, ineq_j
);
1411 } else if (any(ineq_i
, map
->p
[i
]->n_ineq
, STATUS_ADJ_EQ
) ||
1412 any(ineq_j
, map
->p
[j
]->n_ineq
, STATUS_ADJ_EQ
)) {
1415 } else if (any(ineq_i
, map
->p
[i
]->n_ineq
, STATUS_ADJ_INEQ
) ||
1416 any(ineq_j
, map
->p
[j
]->n_ineq
, STATUS_ADJ_INEQ
)) {
1417 changed
= check_adj_ineq(map
, i
, j
, tabs
,
1418 eq_i
, ineq_i
, eq_j
, ineq_j
);
1420 if (!any(eq_i
, 2 * map
->p
[i
]->n_eq
, STATUS_CUT
) &&
1421 !any(eq_j
, 2 * map
->p
[j
]->n_eq
, STATUS_CUT
))
1422 changed
= check_facets(map
, i
, j
, tabs
, ineq_i
, ineq_j
);
1424 changed
= check_wrap(map
, i
, j
, tabs
,
1425 eq_i
, ineq_i
, eq_j
, ineq_j
);
1442 /* Do the two basic maps live in the same local space, i.e.,
1443 * do they have the same (known) divs?
1444 * If either basic map has any unknown divs, then we can only assume
1445 * that they do not live in the same local space.
1447 static int same_divs(__isl_keep isl_basic_map
*bmap1
,
1448 __isl_keep isl_basic_map
*bmap2
)
1454 if (!bmap1
|| !bmap2
)
1456 if (bmap1
->n_div
!= bmap2
->n_div
)
1459 if (bmap1
->n_div
== 0)
1462 known
= isl_basic_map_divs_known(bmap1
);
1463 if (known
< 0 || !known
)
1465 known
= isl_basic_map_divs_known(bmap2
);
1466 if (known
< 0 || !known
)
1469 total
= isl_basic_map_total_dim(bmap1
);
1470 for (i
= 0; i
< bmap1
->n_div
; ++i
)
1471 if (!isl_seq_eq(bmap1
->div
[i
], bmap2
->div
[i
], 2 + total
))
1477 /* Given two basic maps "i" and "j", where the divs of "i" form a subset
1478 * of those of "j", check if basic map "j" is a subset of basic map "i"
1479 * and, if so, drop basic map "j".
1481 * We first expand the divs of basic map "i" to match those of basic map "j",
1482 * using the divs and expansion computed by the caller.
1483 * Then we check if all constraints of the expanded "i" are valid for "j".
1485 static int coalesce_subset(__isl_keep isl_map
*map
, int i
, int j
,
1486 struct isl_tab
**tabs
, __isl_keep isl_mat
*div
, int *exp
)
1488 isl_basic_map
*bmap
;
1493 bmap
= isl_basic_map_copy(map
->p
[i
]);
1494 bmap
= isl_basic_set_expand_divs(bmap
, isl_mat_copy(div
), exp
);
1499 eq_i
= eq_status_in(bmap
, tabs
[j
]);
1500 if (bmap
->n_eq
&& !eq_i
)
1502 if (any(eq_i
, 2 * bmap
->n_eq
, STATUS_ERROR
))
1504 if (any(eq_i
, 2 * bmap
->n_eq
, STATUS_SEPARATE
))
1507 ineq_i
= ineq_status_in(bmap
, NULL
, tabs
[j
]);
1508 if (bmap
->n_ineq
&& !ineq_i
)
1510 if (any(ineq_i
, bmap
->n_ineq
, STATUS_ERROR
))
1512 if (any(ineq_i
, bmap
->n_ineq
, STATUS_SEPARATE
))
1515 if (all(eq_i
, 2 * map
->p
[i
]->n_eq
, STATUS_VALID
) &&
1516 all(ineq_i
, map
->p
[i
]->n_ineq
, STATUS_VALID
)) {
1522 isl_basic_map_free(bmap
);
1527 isl_basic_map_free(bmap
);
1533 /* Check if the basic map "j" is a subset of basic map "i",
1534 * assuming that "i" has fewer divs that "j".
1535 * If not, then we change the order.
1537 * If the two basic maps have the same number of divs, then
1538 * they must necessarily be different. Otherwise, we would have
1539 * called coalesce_local_pair. We therefore don't try anything
1542 * We first check if the divs of "i" are all known and form a subset
1543 * of those of "j". If so, we pass control over to coalesce_subset.
1545 static int check_coalesce_subset(__isl_keep isl_map
*map
, int i
, int j
,
1546 struct isl_tab
**tabs
)
1549 isl_mat
*div_i
, *div_j
, *div
;
1555 if (map
->p
[i
]->n_div
== map
->p
[j
]->n_div
)
1557 if (map
->p
[j
]->n_div
< map
->p
[i
]->n_div
)
1558 return check_coalesce_subset(map
, j
, i
, tabs
);
1560 known
= isl_basic_map_divs_known(map
->p
[i
]);
1561 if (known
< 0 || !known
)
1564 ctx
= isl_map_get_ctx(map
);
1566 div_i
= isl_basic_map_get_divs(map
->p
[i
]);
1567 div_j
= isl_basic_map_get_divs(map
->p
[j
]);
1569 if (!div_i
|| !div_j
)
1572 exp1
= isl_alloc_array(ctx
, int, div_i
->n_row
);
1573 exp2
= isl_alloc_array(ctx
, int, div_j
->n_row
);
1574 if ((div_i
->n_row
&& !exp1
) || (div_j
->n_row
&& !exp2
))
1577 div
= isl_merge_divs(div_i
, div_j
, exp1
, exp2
);
1581 if (div
->n_row
== div_j
->n_row
)
1582 subset
= coalesce_subset(map
, i
, j
, tabs
, div
, exp1
);
1588 isl_mat_free(div_i
);
1589 isl_mat_free(div_j
);
1596 isl_mat_free(div_i
);
1597 isl_mat_free(div_j
);
1603 /* Check if the union of the given pair of basic maps
1604 * can be represented by a single basic map.
1605 * If so, replace the pair by the single basic map and return 1.
1606 * Otherwise, return 0;
1608 * We first check if the two basic maps live in the same local space.
1609 * If so, we do the complete check. Otherwise, we check if one is
1610 * an obvious subset of the other.
1612 static int coalesce_pair(__isl_keep isl_map
*map
, int i
, int j
,
1613 struct isl_tab
**tabs
)
1617 same
= same_divs(map
->p
[i
], map
->p
[j
]);
1621 return coalesce_local_pair(map
, i
, j
, tabs
);
1623 return check_coalesce_subset(map
, i
, j
, tabs
);
1626 static struct isl_map
*coalesce(struct isl_map
*map
, struct isl_tab
**tabs
)
1630 for (i
= map
->n
- 2; i
>= 0; --i
)
1632 for (j
= i
+ 1; j
< map
->n
; ++j
) {
1634 changed
= coalesce_pair(map
, i
, j
, tabs
);
1646 /* For each pair of basic maps in the map, check if the union of the two
1647 * can be represented by a single basic map.
1648 * If so, replace the pair by the single basic map and start over.
1650 * Since we are constructing the tableaus of the basic maps anyway,
1651 * we exploit them to detect implicit equalities and redundant constraints.
1652 * This also helps the coalescing as it can ignore the redundant constraints.
1653 * In order to avoid confusion, we make all implicit equalities explicit
1654 * in the basic maps. We don't call isl_basic_map_gauss, though,
1655 * as that may affect the number of constraints.
1656 * This means that we have to call isl_basic_map_gauss at the end
1657 * of the computation to ensure that the basic maps are not left
1658 * in an unexpected state.
1660 struct isl_map
*isl_map_coalesce(struct isl_map
*map
)
1664 struct isl_tab
**tabs
= NULL
;
1666 map
= isl_map_remove_empty_parts(map
);
1673 map
= isl_map_sort_divs(map
);
1674 map
= isl_map_cow(map
);
1676 tabs
= isl_calloc_array(map
->ctx
, struct isl_tab
*, map
->n
);
1681 for (i
= 0; i
< map
->n
; ++i
) {
1682 tabs
[i
] = isl_tab_from_basic_map(map
->p
[i
], 0);
1685 if (!ISL_F_ISSET(map
->p
[i
], ISL_BASIC_MAP_NO_IMPLICIT
))
1686 if (isl_tab_detect_implicit_equalities(tabs
[i
]) < 0)
1688 map
->p
[i
] = isl_tab_make_equalities_explicit(tabs
[i
],
1692 if (!ISL_F_ISSET(map
->p
[i
], ISL_BASIC_MAP_NO_REDUNDANT
))
1693 if (isl_tab_detect_redundant(tabs
[i
]) < 0)
1696 for (i
= map
->n
- 1; i
>= 0; --i
)
1700 map
= coalesce(map
, tabs
);
1703 for (i
= 0; i
< map
->n
; ++i
) {
1704 map
->p
[i
] = isl_basic_map_update_from_tab(map
->p
[i
],
1706 map
->p
[i
] = isl_basic_map_gauss(map
->p
[i
], NULL
);
1707 map
->p
[i
] = isl_basic_map_finalize(map
->p
[i
]);
1710 ISL_F_SET(map
->p
[i
], ISL_BASIC_MAP_NO_IMPLICIT
);
1711 ISL_F_SET(map
->p
[i
], ISL_BASIC_MAP_NO_REDUNDANT
);
1714 for (i
= 0; i
< n
; ++i
)
1715 isl_tab_free(tabs
[i
]);
1722 for (i
= 0; i
< n
; ++i
)
1723 isl_tab_free(tabs
[i
]);
1729 /* For each pair of basic sets in the set, check if the union of the two
1730 * can be represented by a single basic set.
1731 * If so, replace the pair by the single basic set and start over.
1733 struct isl_set
*isl_set_coalesce(struct isl_set
*set
)
1735 return (struct isl_set
*)isl_map_coalesce((struct isl_map
*)set
);