isl_map_coalesce: optionally bound the coefficients of wrapping constraints
[isl.git] / isl_coalesce.c
blob8d06e2e843c9e0c3bb766e2a6201a1606170241a
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
5 * Use of this software is governed by the GNU LGPLv2.1 license
7 * Written by Sven Verdoolaege, K.U.Leuven, Departement
8 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
9 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
10 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
13 #include "isl_map_private.h"
14 #include <isl/seq.h>
15 #include <isl/options.h>
16 #include "isl_tab.h"
17 #include <isl_mat_private.h>
19 #define STATUS_ERROR -1
20 #define STATUS_REDUNDANT 1
21 #define STATUS_VALID 2
22 #define STATUS_SEPARATE 3
23 #define STATUS_CUT 4
24 #define STATUS_ADJ_EQ 5
25 #define STATUS_ADJ_INEQ 6
27 static int status_in(isl_int *ineq, struct isl_tab *tab)
29 enum isl_ineq_type type = isl_tab_ineq_type(tab, ineq);
30 switch (type) {
31 default:
32 case isl_ineq_error: return STATUS_ERROR;
33 case isl_ineq_redundant: return STATUS_VALID;
34 case isl_ineq_separate: return STATUS_SEPARATE;
35 case isl_ineq_cut: return STATUS_CUT;
36 case isl_ineq_adj_eq: return STATUS_ADJ_EQ;
37 case isl_ineq_adj_ineq: return STATUS_ADJ_INEQ;
41 /* Compute the position of the equalities of basic map "i"
42 * with respect to basic map "j".
43 * The resulting array has twice as many entries as the number
44 * of equalities corresponding to the two inequalties to which
45 * each equality corresponds.
47 static int *eq_status_in(struct isl_map *map, int i, int j,
48 struct isl_tab **tabs)
50 int k, l;
51 int *eq = isl_calloc_array(map->ctx, int, 2 * map->p[i]->n_eq);
52 unsigned dim;
54 dim = isl_basic_map_total_dim(map->p[i]);
55 for (k = 0; k < map->p[i]->n_eq; ++k) {
56 for (l = 0; l < 2; ++l) {
57 isl_seq_neg(map->p[i]->eq[k], map->p[i]->eq[k], 1+dim);
58 eq[2 * k + l] = status_in(map->p[i]->eq[k], tabs[j]);
59 if (eq[2 * k + l] == STATUS_ERROR)
60 goto error;
62 if (eq[2 * k] == STATUS_SEPARATE ||
63 eq[2 * k + 1] == STATUS_SEPARATE)
64 break;
67 return eq;
68 error:
69 free(eq);
70 return NULL;
73 /* Compute the position of the inequalities of basic map "i"
74 * with respect to basic map "j".
76 static int *ineq_status_in(struct isl_map *map, int i, int j,
77 struct isl_tab **tabs)
79 int k;
80 unsigned n_eq = map->p[i]->n_eq;
81 int *ineq = isl_calloc_array(map->ctx, int, map->p[i]->n_ineq);
83 for (k = 0; k < map->p[i]->n_ineq; ++k) {
84 if (isl_tab_is_redundant(tabs[i], n_eq + k)) {
85 ineq[k] = STATUS_REDUNDANT;
86 continue;
88 ineq[k] = status_in(map->p[i]->ineq[k], tabs[j]);
89 if (ineq[k] == STATUS_ERROR)
90 goto error;
91 if (ineq[k] == STATUS_SEPARATE)
92 break;
95 return ineq;
96 error:
97 free(ineq);
98 return NULL;
101 static int any(int *con, unsigned len, int status)
103 int i;
105 for (i = 0; i < len ; ++i)
106 if (con[i] == status)
107 return 1;
108 return 0;
111 static int count(int *con, unsigned len, int status)
113 int i;
114 int c = 0;
116 for (i = 0; i < len ; ++i)
117 if (con[i] == status)
118 c++;
119 return c;
122 static int all(int *con, unsigned len, int status)
124 int i;
126 for (i = 0; i < len ; ++i) {
127 if (con[i] == STATUS_REDUNDANT)
128 continue;
129 if (con[i] != status)
130 return 0;
132 return 1;
135 static void drop(struct isl_map *map, int i, struct isl_tab **tabs)
137 isl_basic_map_free(map->p[i]);
138 isl_tab_free(tabs[i]);
140 if (i != map->n - 1) {
141 map->p[i] = map->p[map->n - 1];
142 tabs[i] = tabs[map->n - 1];
144 tabs[map->n - 1] = NULL;
145 map->n--;
148 /* Replace the pair of basic maps i and j by the basic map bounded
149 * by the valid constraints in both basic maps and the constraint
150 * in extra (if not NULL).
152 static int fuse(struct isl_map *map, int i, int j,
153 struct isl_tab **tabs, int *eq_i, int *ineq_i, int *eq_j, int *ineq_j,
154 __isl_keep isl_mat *extra)
156 int k, l;
157 struct isl_basic_map *fused = NULL;
158 struct isl_tab *fused_tab = NULL;
159 unsigned total = isl_basic_map_total_dim(map->p[i]);
160 unsigned extra_rows = extra ? extra->n_row : 0;
162 fused = isl_basic_map_alloc_space(isl_space_copy(map->p[i]->dim),
163 map->p[i]->n_div,
164 map->p[i]->n_eq + map->p[j]->n_eq,
165 map->p[i]->n_ineq + map->p[j]->n_ineq + extra_rows);
166 if (!fused)
167 goto error;
169 for (k = 0; k < map->p[i]->n_eq; ++k) {
170 if (eq_i && (eq_i[2 * k] != STATUS_VALID ||
171 eq_i[2 * k + 1] != STATUS_VALID))
172 continue;
173 l = isl_basic_map_alloc_equality(fused);
174 if (l < 0)
175 goto error;
176 isl_seq_cpy(fused->eq[l], map->p[i]->eq[k], 1 + total);
179 for (k = 0; k < map->p[j]->n_eq; ++k) {
180 if (eq_j && (eq_j[2 * k] != STATUS_VALID ||
181 eq_j[2 * k + 1] != STATUS_VALID))
182 continue;
183 l = isl_basic_map_alloc_equality(fused);
184 if (l < 0)
185 goto error;
186 isl_seq_cpy(fused->eq[l], map->p[j]->eq[k], 1 + total);
189 for (k = 0; k < map->p[i]->n_ineq; ++k) {
190 if (ineq_i[k] != STATUS_VALID)
191 continue;
192 l = isl_basic_map_alloc_inequality(fused);
193 if (l < 0)
194 goto error;
195 isl_seq_cpy(fused->ineq[l], map->p[i]->ineq[k], 1 + total);
198 for (k = 0; k < map->p[j]->n_ineq; ++k) {
199 if (ineq_j[k] != STATUS_VALID)
200 continue;
201 l = isl_basic_map_alloc_inequality(fused);
202 if (l < 0)
203 goto error;
204 isl_seq_cpy(fused->ineq[l], map->p[j]->ineq[k], 1 + total);
207 for (k = 0; k < map->p[i]->n_div; ++k) {
208 int l = isl_basic_map_alloc_div(fused);
209 if (l < 0)
210 goto error;
211 isl_seq_cpy(fused->div[l], map->p[i]->div[k], 1 + 1 + total);
214 for (k = 0; k < extra_rows; ++k) {
215 l = isl_basic_map_alloc_inequality(fused);
216 if (l < 0)
217 goto error;
218 isl_seq_cpy(fused->ineq[l], extra->row[k], 1 + total);
221 fused = isl_basic_map_gauss(fused, NULL);
222 ISL_F_SET(fused, ISL_BASIC_MAP_FINAL);
223 if (ISL_F_ISSET(map->p[i], ISL_BASIC_MAP_RATIONAL) &&
224 ISL_F_ISSET(map->p[j], ISL_BASIC_MAP_RATIONAL))
225 ISL_F_SET(fused, ISL_BASIC_MAP_RATIONAL);
227 fused_tab = isl_tab_from_basic_map(fused, 0);
228 if (isl_tab_detect_redundant(fused_tab) < 0)
229 goto error;
231 isl_basic_map_free(map->p[i]);
232 map->p[i] = fused;
233 isl_tab_free(tabs[i]);
234 tabs[i] = fused_tab;
235 drop(map, j, tabs);
237 return 1;
238 error:
239 isl_tab_free(fused_tab);
240 isl_basic_map_free(fused);
241 return -1;
244 /* Given a pair of basic maps i and j such that all constraints are either
245 * "valid" or "cut", check if the facets corresponding to the "cut"
246 * constraints of i lie entirely within basic map j.
247 * If so, replace the pair by the basic map consisting of the valid
248 * constraints in both basic maps.
250 * To see that we are not introducing any extra points, call the
251 * two basic maps A and B and the resulting map U and let x
252 * be an element of U \setminus ( A \cup B ).
253 * Then there is a pair of cut constraints c_1 and c_2 in A and B such that x
254 * violates them. Let X be the intersection of U with the opposites
255 * of these constraints. Then x \in X.
256 * The facet corresponding to c_1 contains the corresponding facet of A.
257 * This facet is entirely contained in B, so c_2 is valid on the facet.
258 * However, since it is also (part of) a facet of X, -c_2 is also valid
259 * on the facet. This means c_2 is saturated on the facet, so c_1 and
260 * c_2 must be opposites of each other, but then x could not violate
261 * both of them.
263 static int check_facets(struct isl_map *map, int i, int j,
264 struct isl_tab **tabs, int *ineq_i, int *ineq_j)
266 int k, l;
267 struct isl_tab_undo *snap;
268 unsigned n_eq = map->p[i]->n_eq;
270 snap = isl_tab_snap(tabs[i]);
272 for (k = 0; k < map->p[i]->n_ineq; ++k) {
273 if (ineq_i[k] != STATUS_CUT)
274 continue;
275 if (isl_tab_select_facet(tabs[i], n_eq + k) < 0)
276 return -1;
277 for (l = 0; l < map->p[j]->n_ineq; ++l) {
278 int stat;
279 if (ineq_j[l] != STATUS_CUT)
280 continue;
281 stat = status_in(map->p[j]->ineq[l], tabs[i]);
282 if (stat != STATUS_VALID)
283 break;
285 if (isl_tab_rollback(tabs[i], snap) < 0)
286 return -1;
287 if (l < map->p[j]->n_ineq)
288 break;
291 if (k < map->p[i]->n_ineq)
292 /* BAD CUT PAIR */
293 return 0;
294 return fuse(map, i, j, tabs, NULL, ineq_i, NULL, ineq_j, NULL);
297 /* Both basic maps have at least one inequality with and adjacent
298 * (but opposite) inequality in the other basic map.
299 * Check that there are no cut constraints and that there is only
300 * a single pair of adjacent inequalities.
301 * If so, we can replace the pair by a single basic map described
302 * by all but the pair of adjacent inequalities.
303 * Any additional points introduced lie strictly between the two
304 * adjacent hyperplanes and can therefore be integral.
306 * ____ _____
307 * / ||\ / \
308 * / || \ / \
309 * \ || \ => \ \
310 * \ || / \ /
311 * \___||_/ \_____/
313 * The test for a single pair of adjancent inequalities is important
314 * for avoiding the combination of two basic maps like the following
316 * /|
317 * / |
318 * /__|
319 * _____
320 * | |
321 * | |
322 * |___|
324 static int check_adj_ineq(struct isl_map *map, int i, int j,
325 struct isl_tab **tabs, int *ineq_i, int *ineq_j)
327 int changed = 0;
329 if (any(ineq_i, map->p[i]->n_ineq, STATUS_CUT) ||
330 any(ineq_j, map->p[j]->n_ineq, STATUS_CUT))
331 /* ADJ INEQ CUT */
333 else if (count(ineq_i, map->p[i]->n_ineq, STATUS_ADJ_INEQ) == 1 &&
334 count(ineq_j, map->p[j]->n_ineq, STATUS_ADJ_INEQ) == 1)
335 changed = fuse(map, i, j, tabs, NULL, ineq_i, NULL, ineq_j, NULL);
336 /* else ADJ INEQ TOO MANY */
338 return changed;
341 /* Check if basic map "i" contains the basic map represented
342 * by the tableau "tab".
344 static int contains(struct isl_map *map, int i, int *ineq_i,
345 struct isl_tab *tab)
347 int k, l;
348 unsigned dim;
350 dim = isl_basic_map_total_dim(map->p[i]);
351 for (k = 0; k < map->p[i]->n_eq; ++k) {
352 for (l = 0; l < 2; ++l) {
353 int stat;
354 isl_seq_neg(map->p[i]->eq[k], map->p[i]->eq[k], 1+dim);
355 stat = status_in(map->p[i]->eq[k], tab);
356 if (stat != STATUS_VALID)
357 return 0;
361 for (k = 0; k < map->p[i]->n_ineq; ++k) {
362 int stat;
363 if (ineq_i[k] == STATUS_REDUNDANT)
364 continue;
365 stat = status_in(map->p[i]->ineq[k], tab);
366 if (stat != STATUS_VALID)
367 return 0;
369 return 1;
372 /* Basic map "i" has an inequality "k" that is adjacent to some equality
373 * of basic map "j". All the other inequalities are valid for "j".
374 * Check if basic map "j" forms an extension of basic map "i".
376 * In particular, we relax constraint "k", compute the corresponding
377 * facet and check whether it is included in the other basic map.
378 * If so, we know that relaxing the constraint extends the basic
379 * map with exactly the other basic map (we already know that this
380 * other basic map is included in the extension, because there
381 * were no "cut" inequalities in "i") and we can replace the
382 * two basic maps by thie extension.
383 * ____ _____
384 * / || / |
385 * / || / |
386 * \ || => \ |
387 * \ || \ |
388 * \___|| \____|
390 static int is_extension(struct isl_map *map, int i, int j, int k,
391 struct isl_tab **tabs, int *eq_i, int *ineq_i, int *eq_j, int *ineq_j)
393 int changed = 0;
394 int super;
395 struct isl_tab_undo *snap, *snap2;
396 unsigned n_eq = map->p[i]->n_eq;
398 if (isl_tab_is_equality(tabs[i], n_eq + k))
399 return 0;
401 snap = isl_tab_snap(tabs[i]);
402 tabs[i] = isl_tab_relax(tabs[i], n_eq + k);
403 snap2 = isl_tab_snap(tabs[i]);
404 if (isl_tab_select_facet(tabs[i], n_eq + k) < 0)
405 return -1;
406 super = contains(map, j, ineq_j, tabs[i]);
407 if (super) {
408 if (isl_tab_rollback(tabs[i], snap2) < 0)
409 return -1;
410 map->p[i] = isl_basic_map_cow(map->p[i]);
411 if (!map->p[i])
412 return -1;
413 isl_int_add_ui(map->p[i]->ineq[k][0], map->p[i]->ineq[k][0], 1);
414 ISL_F_SET(map->p[i], ISL_BASIC_MAP_FINAL);
415 drop(map, j, tabs);
416 changed = 1;
417 } else
418 if (isl_tab_rollback(tabs[i], snap) < 0)
419 return -1;
421 return changed;
424 /* Data structure that keeps track of the wrapping constraints
425 * and of information to bound the coefficients of those constraints.
427 * bound is set if we want to apply a bound on the coefficients
428 * mat contains the wrapping constraints
429 * max is the bound on the coefficients (if bound is set)
431 struct isl_wraps {
432 int bound;
433 isl_mat *mat;
434 isl_int max;
437 /* Update wraps->max to be greater than or equal to the coefficients
438 * in the equalities and inequalities of bmap that can be removed if we end up
439 * applying wrapping.
441 static void wraps_update_max(struct isl_wraps *wraps,
442 __isl_keep isl_basic_map *bmap, int *eq, int *ineq)
444 int k;
445 isl_int max_k;
446 unsigned total = isl_basic_map_total_dim(bmap);
448 isl_int_init(max_k);
450 for (k = 0; k < bmap->n_eq; ++k) {
451 if (eq[2 * k] == STATUS_VALID &&
452 eq[2 * k + 1] == STATUS_VALID)
453 continue;
454 isl_seq_abs_max(bmap->eq[k] + 1, total, &max_k);
455 if (isl_int_abs_gt(max_k, wraps->max))
456 isl_int_set(wraps->max, max_k);
459 for (k = 0; k < bmap->n_ineq; ++k) {
460 if (ineq[k] == STATUS_VALID || ineq[k] == STATUS_REDUNDANT)
461 continue;
462 isl_seq_abs_max(bmap->ineq[k] + 1, total, &max_k);
463 if (isl_int_abs_gt(max_k, wraps->max))
464 isl_int_set(wraps->max, max_k);
467 isl_int_clear(max_k);
470 /* Initialize the isl_wraps data structure.
471 * If we want to bound the coefficients of the wrapping constraints,
472 * we set wraps->max to the largest coefficient
473 * in the equalities and inequalities that can be removed if we end up
474 * applying wrapping.
476 static void wraps_init(struct isl_wraps *wraps, __isl_take isl_mat *mat,
477 __isl_keep isl_map *map, int i, int j,
478 int *eq_i, int *ineq_i, int *eq_j, int *ineq_j)
480 isl_ctx *ctx;
482 wraps->bound = 0;
483 wraps->mat = mat;
484 if (!mat)
485 return;
486 ctx = isl_mat_get_ctx(mat);
487 wraps->bound = isl_options_get_coalesce_bounded_wrapping(ctx);
488 if (!wraps->bound)
489 return;
490 isl_int_init(wraps->max);
491 isl_int_set_si(wraps->max, 0);
492 wraps_update_max(wraps, map->p[i], eq_i, ineq_i);
493 wraps_update_max(wraps, map->p[j], eq_j, ineq_j);
496 /* Free the contents of the isl_wraps data structure.
498 static void wraps_free(struct isl_wraps *wraps)
500 isl_mat_free(wraps->mat);
501 if (wraps->bound)
502 isl_int_clear(wraps->max);
505 /* Is the wrapping constraint in row "row" allowed?
507 * If wraps->bound is set, we check that none of the coefficients
508 * is greater than wraps->max.
510 static int allow_wrap(struct isl_wraps *wraps, int row)
512 int i;
514 if (!wraps->bound)
515 return 1;
517 for (i = 1; i < wraps->mat->n_col; ++i)
518 if (isl_int_abs_gt(wraps->mat->row[row][i], wraps->max))
519 return 0;
521 return 1;
524 /* For each non-redundant constraint in "bmap" (as determined by "tab"),
525 * wrap the constraint around "bound" such that it includes the whole
526 * set "set" and append the resulting constraint to "wraps".
527 * "wraps" is assumed to have been pre-allocated to the appropriate size.
528 * wraps->n_row is the number of actual wrapped constraints that have
529 * been added.
530 * If any of the wrapping problems results in a constraint that is
531 * identical to "bound", then this means that "set" is unbounded in such
532 * way that no wrapping is possible. If this happens then wraps->n_row
533 * is reset to zero.
534 * Similarly, if we want to bound the coefficients of the wrapping
535 * constraints and a newly added wrapping constraint does not
536 * satisfy the bound, then wraps->n_row is also reset to zero.
538 static int add_wraps(struct isl_wraps *wraps, __isl_keep isl_basic_map *bmap,
539 struct isl_tab *tab, isl_int *bound, __isl_keep isl_set *set)
541 int l;
542 int w;
543 unsigned total = isl_basic_map_total_dim(bmap);
545 w = wraps->mat->n_row;
547 for (l = 0; l < bmap->n_ineq; ++l) {
548 if (isl_seq_is_neg(bound, bmap->ineq[l], 1 + total))
549 continue;
550 if (isl_seq_eq(bound, bmap->ineq[l], 1 + total))
551 continue;
552 if (isl_tab_is_redundant(tab, bmap->n_eq + l))
553 continue;
555 isl_seq_cpy(wraps->mat->row[w], bound, 1 + total);
556 if (!isl_set_wrap_facet(set, wraps->mat->row[w], bmap->ineq[l]))
557 return -1;
558 if (isl_seq_eq(wraps->mat->row[w], bound, 1 + total))
559 goto unbounded;
560 if (!allow_wrap(wraps, w))
561 goto unbounded;
562 ++w;
564 for (l = 0; l < bmap->n_eq; ++l) {
565 if (isl_seq_is_neg(bound, bmap->eq[l], 1 + total))
566 continue;
567 if (isl_seq_eq(bound, bmap->eq[l], 1 + total))
568 continue;
570 isl_seq_cpy(wraps->mat->row[w], bound, 1 + total);
571 isl_seq_neg(wraps->mat->row[w + 1], bmap->eq[l], 1 + total);
572 if (!isl_set_wrap_facet(set, wraps->mat->row[w],
573 wraps->mat->row[w + 1]))
574 return -1;
575 if (isl_seq_eq(wraps->mat->row[w], bound, 1 + total))
576 goto unbounded;
577 if (!allow_wrap(wraps, w))
578 goto unbounded;
579 ++w;
581 isl_seq_cpy(wraps->mat->row[w], bound, 1 + total);
582 if (!isl_set_wrap_facet(set, wraps->mat->row[w], bmap->eq[l]))
583 return -1;
584 if (isl_seq_eq(wraps->mat->row[w], bound, 1 + total))
585 goto unbounded;
586 if (!allow_wrap(wraps, w))
587 goto unbounded;
588 ++w;
591 wraps->mat->n_row = w;
592 return 0;
593 unbounded:
594 wraps->mat->n_row = 0;
595 return 0;
598 /* Check if the constraints in "wraps" from "first" until the last
599 * are all valid for the basic set represented by "tab".
600 * If not, wraps->n_row is set to zero.
602 static int check_wraps(__isl_keep isl_mat *wraps, int first,
603 struct isl_tab *tab)
605 int i;
607 for (i = first; i < wraps->n_row; ++i) {
608 enum isl_ineq_type type;
609 type = isl_tab_ineq_type(tab, wraps->row[i]);
610 if (type == isl_ineq_error)
611 return -1;
612 if (type == isl_ineq_redundant)
613 continue;
614 wraps->n_row = 0;
615 return 0;
618 return 0;
621 /* Return a set that corresponds to the non-redudant constraints
622 * (as recorded in tab) of bmap.
624 * It's important to remove the redundant constraints as some
625 * of the other constraints may have been modified after the
626 * constraints were marked redundant.
627 * In particular, a constraint may have been relaxed.
628 * Redundant constraints are ignored when a constraint is relaxed
629 * and should therefore continue to be ignored ever after.
630 * Otherwise, the relaxation might be thwarted by some of
631 * these constraints.
633 static __isl_give isl_set *set_from_updated_bmap(__isl_keep isl_basic_map *bmap,
634 struct isl_tab *tab)
636 bmap = isl_basic_map_copy(bmap);
637 bmap = isl_basic_map_cow(bmap);
638 bmap = isl_basic_map_update_from_tab(bmap, tab);
639 return isl_set_from_basic_set(isl_basic_map_underlying_set(bmap));
642 /* Given a basic set i with a constraint k that is adjacent to either the
643 * whole of basic set j or a facet of basic set j, check if we can wrap
644 * both the facet corresponding to k and the facet of j (or the whole of j)
645 * around their ridges to include the other set.
646 * If so, replace the pair of basic sets by their union.
648 * All constraints of i (except k) are assumed to be valid for j.
650 * However, the constraints of j may not be valid for i and so
651 * we have to check that the wrapping constraints for j are valid for i.
653 * In the case where j has a facet adjacent to i, tab[j] is assumed
654 * to have been restricted to this facet, so that the non-redundant
655 * constraints in tab[j] are the ridges of the facet.
656 * Note that for the purpose of wrapping, it does not matter whether
657 * we wrap the ridges of i around the whole of j or just around
658 * the facet since all the other constraints are assumed to be valid for j.
659 * In practice, we wrap to include the whole of j.
660 * ____ _____
661 * / | / \
662 * / || / |
663 * \ || => \ |
664 * \ || \ |
665 * \___|| \____|
668 static int can_wrap_in_facet(struct isl_map *map, int i, int j, int k,
669 struct isl_tab **tabs, int *eq_i, int *ineq_i, int *eq_j, int *ineq_j)
671 int changed = 0;
672 struct isl_wraps wraps;
673 isl_mat *mat;
674 struct isl_set *set_i = NULL;
675 struct isl_set *set_j = NULL;
676 struct isl_vec *bound = NULL;
677 unsigned total = isl_basic_map_total_dim(map->p[i]);
678 struct isl_tab_undo *snap;
679 int n;
681 set_i = set_from_updated_bmap(map->p[i], tabs[i]);
682 set_j = set_from_updated_bmap(map->p[j], tabs[j]);
683 mat = isl_mat_alloc(map->ctx, 2 * (map->p[i]->n_eq + map->p[j]->n_eq) +
684 map->p[i]->n_ineq + map->p[j]->n_ineq,
685 1 + total);
686 wraps_init(&wraps, mat, map, i, j, eq_i, ineq_i, eq_j, ineq_j);
687 bound = isl_vec_alloc(map->ctx, 1 + total);
688 if (!set_i || !set_j || !wraps.mat || !bound)
689 goto error;
691 isl_seq_cpy(bound->el, map->p[i]->ineq[k], 1 + total);
692 isl_int_add_ui(bound->el[0], bound->el[0], 1);
694 isl_seq_cpy(wraps.mat->row[0], bound->el, 1 + total);
695 wraps.mat->n_row = 1;
697 if (add_wraps(&wraps, map->p[j], tabs[j], bound->el, set_i) < 0)
698 goto error;
699 if (!wraps.mat->n_row)
700 goto unbounded;
702 snap = isl_tab_snap(tabs[i]);
704 if (isl_tab_select_facet(tabs[i], map->p[i]->n_eq + k) < 0)
705 goto error;
706 if (isl_tab_detect_redundant(tabs[i]) < 0)
707 goto error;
709 isl_seq_neg(bound->el, map->p[i]->ineq[k], 1 + total);
711 n = wraps.mat->n_row;
712 if (add_wraps(&wraps, map->p[i], tabs[i], bound->el, set_j) < 0)
713 goto error;
715 if (isl_tab_rollback(tabs[i], snap) < 0)
716 goto error;
717 if (check_wraps(wraps.mat, n, tabs[i]) < 0)
718 goto error;
719 if (!wraps.mat->n_row)
720 goto unbounded;
722 changed = fuse(map, i, j, tabs, eq_i, ineq_i, eq_j, ineq_j, wraps.mat);
724 unbounded:
725 wraps_free(&wraps);
727 isl_set_free(set_i);
728 isl_set_free(set_j);
730 isl_vec_free(bound);
732 return changed;
733 error:
734 wraps_free(&wraps);
735 isl_vec_free(bound);
736 isl_set_free(set_i);
737 isl_set_free(set_j);
738 return -1;
741 /* Set the is_redundant property of the "n" constraints in "cuts",
742 * except "k" to "v".
743 * This is a fairly tricky operation as it bypasses isl_tab.c.
744 * The reason we want to temporarily mark some constraints redundant
745 * is that we want to ignore them in add_wraps.
747 * Initially all cut constraints are non-redundant, but the
748 * selection of a facet right before the call to this function
749 * may have made some of them redundant.
750 * Likewise, the same constraints are marked non-redundant
751 * in the second call to this function, before they are officially
752 * made non-redundant again in the subsequent rollback.
754 static void set_is_redundant(struct isl_tab *tab, unsigned n_eq,
755 int *cuts, int n, int k, int v)
757 int l;
759 for (l = 0; l < n; ++l) {
760 if (l == k)
761 continue;
762 tab->con[n_eq + cuts[l]].is_redundant = v;
766 /* Given a pair of basic maps i and j such that j sticks out
767 * of i at n cut constraints, each time by at most one,
768 * try to compute wrapping constraints and replace the two
769 * basic maps by a single basic map.
770 * The other constraints of i are assumed to be valid for j.
772 * The facets of i corresponding to the cut constraints are
773 * wrapped around their ridges, except those ridges determined
774 * by any of the other cut constraints.
775 * The intersections of cut constraints need to be ignored
776 * as the result of wrapping one cut constraint around another
777 * would result in a constraint cutting the union.
778 * In each case, the facets are wrapped to include the union
779 * of the two basic maps.
781 * The pieces of j that lie at an offset of exactly one from
782 * one of the cut constraints of i are wrapped around their edges.
783 * Here, there is no need to ignore intersections because we
784 * are wrapping around the union of the two basic maps.
786 * If any wrapping fails, i.e., if we cannot wrap to touch
787 * the union, then we give up.
788 * Otherwise, the pair of basic maps is replaced by their union.
790 static int wrap_in_facets(struct isl_map *map, int i, int j,
791 int *cuts, int n, struct isl_tab **tabs,
792 int *eq_i, int *ineq_i, int *eq_j, int *ineq_j)
794 int changed = 0;
795 struct isl_wraps wraps;
796 isl_mat *mat;
797 isl_set *set = NULL;
798 isl_vec *bound = NULL;
799 unsigned total = isl_basic_map_total_dim(map->p[i]);
800 int max_wrap;
801 int k;
802 struct isl_tab_undo *snap_i, *snap_j;
804 if (isl_tab_extend_cons(tabs[j], 1) < 0)
805 goto error;
807 max_wrap = 2 * (map->p[i]->n_eq + map->p[j]->n_eq) +
808 map->p[i]->n_ineq + map->p[j]->n_ineq;
809 max_wrap *= n;
811 set = isl_set_union(set_from_updated_bmap(map->p[i], tabs[i]),
812 set_from_updated_bmap(map->p[j], tabs[j]));
813 mat = isl_mat_alloc(map->ctx, max_wrap, 1 + total);
814 wraps_init(&wraps, mat, map, i, j, eq_i, ineq_i, eq_j, ineq_j);
815 bound = isl_vec_alloc(map->ctx, 1 + total);
816 if (!set || !wraps.mat || !bound)
817 goto error;
819 snap_i = isl_tab_snap(tabs[i]);
820 snap_j = isl_tab_snap(tabs[j]);
822 wraps.mat->n_row = 0;
824 for (k = 0; k < n; ++k) {
825 if (isl_tab_select_facet(tabs[i], map->p[i]->n_eq + cuts[k]) < 0)
826 goto error;
827 if (isl_tab_detect_redundant(tabs[i]) < 0)
828 goto error;
829 set_is_redundant(tabs[i], map->p[i]->n_eq, cuts, n, k, 1);
831 isl_seq_neg(bound->el, map->p[i]->ineq[cuts[k]], 1 + total);
832 if (!tabs[i]->empty &&
833 add_wraps(&wraps, map->p[i], tabs[i], bound->el, set) < 0)
834 goto error;
836 set_is_redundant(tabs[i], map->p[i]->n_eq, cuts, n, k, 0);
837 if (isl_tab_rollback(tabs[i], snap_i) < 0)
838 goto error;
840 if (tabs[i]->empty)
841 break;
842 if (!wraps.mat->n_row)
843 break;
845 isl_seq_cpy(bound->el, map->p[i]->ineq[cuts[k]], 1 + total);
846 isl_int_add_ui(bound->el[0], bound->el[0], 1);
847 if (isl_tab_add_eq(tabs[j], bound->el) < 0)
848 goto error;
849 if (isl_tab_detect_redundant(tabs[j]) < 0)
850 goto error;
852 if (!tabs[j]->empty &&
853 add_wraps(&wraps, map->p[j], tabs[j], bound->el, set) < 0)
854 goto error;
856 if (isl_tab_rollback(tabs[j], snap_j) < 0)
857 goto error;
859 if (!wraps.mat->n_row)
860 break;
863 if (k == n)
864 changed = fuse(map, i, j, tabs,
865 eq_i, ineq_i, eq_j, ineq_j, wraps.mat);
867 isl_vec_free(bound);
868 wraps_free(&wraps);
869 isl_set_free(set);
871 return changed;
872 error:
873 isl_vec_free(bound);
874 wraps_free(&wraps);
875 isl_set_free(set);
876 return -1;
879 /* Given two basic sets i and j such that i has no cut equalities,
880 * check if relaxing all the cut inequalities of i by one turns
881 * them into valid constraint for j and check if we can wrap in
882 * the bits that are sticking out.
883 * If so, replace the pair by their union.
885 * We first check if all relaxed cut inequalities of i are valid for j
886 * and then try to wrap in the intersections of the relaxed cut inequalities
887 * with j.
889 * During this wrapping, we consider the points of j that lie at a distance
890 * of exactly 1 from i. In particular, we ignore the points that lie in
891 * between this lower-dimensional space and the basic map i.
892 * We can therefore only apply this to integer maps.
893 * ____ _____
894 * / ___|_ / \
895 * / | | / |
896 * \ | | => \ |
897 * \|____| \ |
898 * \___| \____/
900 * _____ ______
901 * | ____|_ | \
902 * | | | | |
903 * | | | => | |
904 * |_| | | |
905 * |_____| \______|
907 * _______
908 * | |
909 * | |\ |
910 * | | \ |
911 * | | \ |
912 * | | \|
913 * | | \
914 * | |_____\
915 * | |
916 * |_______|
918 * Wrapping can fail if the result of wrapping one of the facets
919 * around its edges does not produce any new facet constraint.
920 * In particular, this happens when we try to wrap in unbounded sets.
922 * _______________________________________________________________________
924 * | ___
925 * | | |
926 * |_| |_________________________________________________________________
927 * |___|
929 * The following is not an acceptable result of coalescing the above two
930 * sets as it includes extra integer points.
931 * _______________________________________________________________________
933 * |
934 * |
936 * \______________________________________________________________________
938 static int can_wrap_in_set(struct isl_map *map, int i, int j,
939 struct isl_tab **tabs, int *eq_i, int *ineq_i, int *eq_j, int *ineq_j)
941 int changed = 0;
942 int k, m;
943 int n;
944 int *cuts = NULL;
946 if (ISL_F_ISSET(map->p[i], ISL_BASIC_MAP_RATIONAL) ||
947 ISL_F_ISSET(map->p[j], ISL_BASIC_MAP_RATIONAL))
948 return 0;
950 n = count(ineq_i, map->p[i]->n_ineq, STATUS_CUT);
951 if (n == 0)
952 return 0;
954 cuts = isl_alloc_array(map->ctx, int, n);
955 if (!cuts)
956 return -1;
958 for (k = 0, m = 0; m < n; ++k) {
959 enum isl_ineq_type type;
961 if (ineq_i[k] != STATUS_CUT)
962 continue;
964 isl_int_add_ui(map->p[i]->ineq[k][0], map->p[i]->ineq[k][0], 1);
965 type = isl_tab_ineq_type(tabs[j], map->p[i]->ineq[k]);
966 isl_int_sub_ui(map->p[i]->ineq[k][0], map->p[i]->ineq[k][0], 1);
967 if (type == isl_ineq_error)
968 goto error;
969 if (type != isl_ineq_redundant)
970 break;
971 cuts[m] = k;
972 ++m;
975 if (m == n)
976 changed = wrap_in_facets(map, i, j, cuts, n, tabs,
977 eq_i, ineq_i, eq_j, ineq_j);
979 free(cuts);
981 return changed;
982 error:
983 free(cuts);
984 return -1;
987 /* Check if either i or j has a single cut constraint that can
988 * be used to wrap in (a facet of) the other basic set.
989 * if so, replace the pair by their union.
991 static int check_wrap(struct isl_map *map, int i, int j,
992 struct isl_tab **tabs, int *eq_i, int *ineq_i, int *eq_j, int *ineq_j)
994 int changed = 0;
996 if (!any(eq_i, 2 * map->p[i]->n_eq, STATUS_CUT))
997 changed = can_wrap_in_set(map, i, j, tabs,
998 eq_i, ineq_i, eq_j, ineq_j);
999 if (changed)
1000 return changed;
1002 if (!any(eq_j, 2 * map->p[j]->n_eq, STATUS_CUT))
1003 changed = can_wrap_in_set(map, j, i, tabs,
1004 eq_j, ineq_j, eq_i, ineq_i);
1005 return changed;
1008 /* At least one of the basic maps has an equality that is adjacent
1009 * to inequality. Make sure that only one of the basic maps has
1010 * such an equality and that the other basic map has exactly one
1011 * inequality adjacent to an equality.
1012 * We call the basic map that has the inequality "i" and the basic
1013 * map that has the equality "j".
1014 * If "i" has any "cut" (in)equality, then relaxing the inequality
1015 * by one would not result in a basic map that contains the other
1016 * basic map.
1018 static int check_adj_eq(struct isl_map *map, int i, int j,
1019 struct isl_tab **tabs, int *eq_i, int *ineq_i, int *eq_j, int *ineq_j)
1021 int changed = 0;
1022 int k;
1024 if (any(eq_i, 2 * map->p[i]->n_eq, STATUS_ADJ_INEQ) &&
1025 any(eq_j, 2 * map->p[j]->n_eq, STATUS_ADJ_INEQ))
1026 /* ADJ EQ TOO MANY */
1027 return 0;
1029 if (any(eq_i, 2 * map->p[i]->n_eq, STATUS_ADJ_INEQ))
1030 return check_adj_eq(map, j, i, tabs,
1031 eq_j, ineq_j, eq_i, ineq_i);
1033 /* j has an equality adjacent to an inequality in i */
1035 if (any(eq_i, 2 * map->p[i]->n_eq, STATUS_CUT))
1036 return 0;
1037 if (any(ineq_i, map->p[i]->n_ineq, STATUS_CUT))
1038 /* ADJ EQ CUT */
1039 return 0;
1040 if (count(ineq_i, map->p[i]->n_ineq, STATUS_ADJ_EQ) != 1 ||
1041 any(ineq_j, map->p[j]->n_ineq, STATUS_ADJ_EQ) ||
1042 any(ineq_i, map->p[i]->n_ineq, STATUS_ADJ_INEQ) ||
1043 any(ineq_j, map->p[j]->n_ineq, STATUS_ADJ_INEQ))
1044 /* ADJ EQ TOO MANY */
1045 return 0;
1047 for (k = 0; k < map->p[i]->n_ineq ; ++k)
1048 if (ineq_i[k] == STATUS_ADJ_EQ)
1049 break;
1051 changed = is_extension(map, i, j, k, tabs, eq_i, ineq_i, eq_j, ineq_j);
1052 if (changed)
1053 return changed;
1055 if (count(eq_j, 2 * map->p[j]->n_eq, STATUS_ADJ_INEQ) != 1)
1056 return 0;
1058 changed = can_wrap_in_facet(map, i, j, k, tabs, eq_i, ineq_i, eq_j, ineq_j);
1060 return changed;
1063 /* The two basic maps lie on adjacent hyperplanes. In particular,
1064 * basic map "i" has an equality that lies parallel to basic map "j".
1065 * Check if we can wrap the facets around the parallel hyperplanes
1066 * to include the other set.
1068 * We perform basically the same operations as can_wrap_in_facet,
1069 * except that we don't need to select a facet of one of the sets.
1071 * \\ \\
1072 * \\ => \\
1073 * \ \|
1075 * We only allow one equality of "i" to be adjacent to an equality of "j"
1076 * to avoid coalescing
1078 * [m, n] -> { [x, y] -> [x, 1 + y] : x >= 1 and y >= 1 and
1079 * x <= 10 and y <= 10;
1080 * [x, y] -> [1 + x, y] : x >= 1 and x <= 20 and
1081 * y >= 5 and y <= 15 }
1083 * to
1085 * [m, n] -> { [x, y] -> [x2, y2] : x >= 1 and 10y2 <= 20 - x + 10y and
1086 * 4y2 >= 5 + 3y and 5y2 <= 15 + 4y and
1087 * y2 <= 1 + x + y - x2 and y2 >= y and
1088 * y2 >= 1 + x + y - x2 }
1090 static int check_eq_adj_eq(struct isl_map *map, int i, int j,
1091 struct isl_tab **tabs, int *eq_i, int *ineq_i, int *eq_j, int *ineq_j)
1093 int k;
1094 int changed = 0;
1095 struct isl_wraps wraps;
1096 isl_mat *mat;
1097 struct isl_set *set_i = NULL;
1098 struct isl_set *set_j = NULL;
1099 struct isl_vec *bound = NULL;
1100 unsigned total = isl_basic_map_total_dim(map->p[i]);
1102 if (count(eq_i, 2 * map->p[i]->n_eq, STATUS_ADJ_EQ) != 1)
1103 return 0;
1105 for (k = 0; k < 2 * map->p[i]->n_eq ; ++k)
1106 if (eq_i[k] == STATUS_ADJ_EQ)
1107 break;
1109 set_i = set_from_updated_bmap(map->p[i], tabs[i]);
1110 set_j = set_from_updated_bmap(map->p[j], tabs[j]);
1111 mat = isl_mat_alloc(map->ctx, 2 * (map->p[i]->n_eq + map->p[j]->n_eq) +
1112 map->p[i]->n_ineq + map->p[j]->n_ineq,
1113 1 + total);
1114 wraps_init(&wraps, mat, map, i, j, eq_i, ineq_i, eq_j, ineq_j);
1115 bound = isl_vec_alloc(map->ctx, 1 + total);
1116 if (!set_i || !set_j || !wraps.mat || !bound)
1117 goto error;
1119 if (k % 2 == 0)
1120 isl_seq_neg(bound->el, map->p[i]->eq[k / 2], 1 + total);
1121 else
1122 isl_seq_cpy(bound->el, map->p[i]->eq[k / 2], 1 + total);
1123 isl_int_add_ui(bound->el[0], bound->el[0], 1);
1125 isl_seq_cpy(wraps.mat->row[0], bound->el, 1 + total);
1126 wraps.mat->n_row = 1;
1128 if (add_wraps(&wraps, map->p[j], tabs[j], bound->el, set_i) < 0)
1129 goto error;
1130 if (!wraps.mat->n_row)
1131 goto unbounded;
1133 isl_int_sub_ui(bound->el[0], bound->el[0], 1);
1134 isl_seq_neg(bound->el, bound->el, 1 + total);
1136 isl_seq_cpy(wraps.mat->row[wraps.mat->n_row], bound->el, 1 + total);
1137 wraps.mat->n_row++;
1139 if (add_wraps(&wraps, map->p[i], tabs[i], bound->el, set_j) < 0)
1140 goto error;
1141 if (!wraps.mat->n_row)
1142 goto unbounded;
1144 changed = fuse(map, i, j, tabs, eq_i, ineq_i, eq_j, ineq_j, wraps.mat);
1146 if (0) {
1147 error: changed = -1;
1149 unbounded:
1151 wraps_free(&wraps);
1152 isl_set_free(set_i);
1153 isl_set_free(set_j);
1154 isl_vec_free(bound);
1156 return changed;
1159 /* Check if the union of the given pair of basic maps
1160 * can be represented by a single basic map.
1161 * If so, replace the pair by the single basic map and return 1.
1162 * Otherwise, return 0;
1164 * We first check the effect of each constraint of one basic map
1165 * on the other basic map.
1166 * The constraint may be
1167 * redundant the constraint is redundant in its own
1168 * basic map and should be ignore and removed
1169 * in the end
1170 * valid all (integer) points of the other basic map
1171 * satisfy the constraint
1172 * separate no (integer) point of the other basic map
1173 * satisfies the constraint
1174 * cut some but not all points of the other basic map
1175 * satisfy the constraint
1176 * adj_eq the given constraint is adjacent (on the outside)
1177 * to an equality of the other basic map
1178 * adj_ineq the given constraint is adjacent (on the outside)
1179 * to an inequality of the other basic map
1181 * We consider seven cases in which we can replace the pair by a single
1182 * basic map. We ignore all "redundant" constraints.
1184 * 1. all constraints of one basic map are valid
1185 * => the other basic map is a subset and can be removed
1187 * 2. all constraints of both basic maps are either "valid" or "cut"
1188 * and the facets corresponding to the "cut" constraints
1189 * of one of the basic maps lies entirely inside the other basic map
1190 * => the pair can be replaced by a basic map consisting
1191 * of the valid constraints in both basic maps
1193 * 3. there is a single pair of adjacent inequalities
1194 * (all other constraints are "valid")
1195 * => the pair can be replaced by a basic map consisting
1196 * of the valid constraints in both basic maps
1198 * 4. there is a single adjacent pair of an inequality and an equality,
1199 * the other constraints of the basic map containing the inequality are
1200 * "valid". Moreover, if the inequality the basic map is relaxed
1201 * and then turned into an equality, then resulting facet lies
1202 * entirely inside the other basic map
1203 * => the pair can be replaced by the basic map containing
1204 * the inequality, with the inequality relaxed.
1206 * 5. there is a single adjacent pair of an inequality and an equality,
1207 * the other constraints of the basic map containing the inequality are
1208 * "valid". Moreover, the facets corresponding to both
1209 * the inequality and the equality can be wrapped around their
1210 * ridges to include the other basic map
1211 * => the pair can be replaced by a basic map consisting
1212 * of the valid constraints in both basic maps together
1213 * with all wrapping constraints
1215 * 6. one of the basic maps extends beyond the other by at most one.
1216 * Moreover, the facets corresponding to the cut constraints and
1217 * the pieces of the other basic map at offset one from these cut
1218 * constraints can be wrapped around their ridges to include
1219 * the union of the two basic maps
1220 * => the pair can be replaced by a basic map consisting
1221 * of the valid constraints in both basic maps together
1222 * with all wrapping constraints
1224 * 7. the two basic maps live in adjacent hyperplanes. In principle
1225 * such sets can always be combined through wrapping, but we impose
1226 * that there is only one such pair, to avoid overeager coalescing.
1228 * Throughout the computation, we maintain a collection of tableaus
1229 * corresponding to the basic maps. When the basic maps are dropped
1230 * or combined, the tableaus are modified accordingly.
1232 static int coalesce_pair(struct isl_map *map, int i, int j,
1233 struct isl_tab **tabs)
1235 int changed = 0;
1236 int *eq_i = NULL;
1237 int *eq_j = NULL;
1238 int *ineq_i = NULL;
1239 int *ineq_j = NULL;
1241 eq_i = eq_status_in(map, i, j, tabs);
1242 if (!eq_i)
1243 goto error;
1244 if (any(eq_i, 2 * map->p[i]->n_eq, STATUS_ERROR))
1245 goto error;
1246 if (any(eq_i, 2 * map->p[i]->n_eq, STATUS_SEPARATE))
1247 goto done;
1249 eq_j = eq_status_in(map, j, i, tabs);
1250 if (!eq_j)
1251 goto error;
1252 if (any(eq_j, 2 * map->p[j]->n_eq, STATUS_ERROR))
1253 goto error;
1254 if (any(eq_j, 2 * map->p[j]->n_eq, STATUS_SEPARATE))
1255 goto done;
1257 ineq_i = ineq_status_in(map, i, j, tabs);
1258 if (!ineq_i)
1259 goto error;
1260 if (any(ineq_i, map->p[i]->n_ineq, STATUS_ERROR))
1261 goto error;
1262 if (any(ineq_i, map->p[i]->n_ineq, STATUS_SEPARATE))
1263 goto done;
1265 ineq_j = ineq_status_in(map, j, i, tabs);
1266 if (!ineq_j)
1267 goto error;
1268 if (any(ineq_j, map->p[j]->n_ineq, STATUS_ERROR))
1269 goto error;
1270 if (any(ineq_j, map->p[j]->n_ineq, STATUS_SEPARATE))
1271 goto done;
1273 if (all(eq_i, 2 * map->p[i]->n_eq, STATUS_VALID) &&
1274 all(ineq_i, map->p[i]->n_ineq, STATUS_VALID)) {
1275 drop(map, j, tabs);
1276 changed = 1;
1277 } else if (all(eq_j, 2 * map->p[j]->n_eq, STATUS_VALID) &&
1278 all(ineq_j, map->p[j]->n_ineq, STATUS_VALID)) {
1279 drop(map, i, tabs);
1280 changed = 1;
1281 } else if (any(eq_i, 2 * map->p[i]->n_eq, STATUS_ADJ_EQ)) {
1282 changed = check_eq_adj_eq(map, i, j, tabs,
1283 eq_i, ineq_i, eq_j, ineq_j);
1284 } else if (any(eq_j, 2 * map->p[j]->n_eq, STATUS_ADJ_EQ)) {
1285 changed = check_eq_adj_eq(map, j, i, tabs,
1286 eq_j, ineq_j, eq_i, ineq_i);
1287 } else if (any(eq_i, 2 * map->p[i]->n_eq, STATUS_ADJ_INEQ) ||
1288 any(eq_j, 2 * map->p[j]->n_eq, STATUS_ADJ_INEQ)) {
1289 changed = check_adj_eq(map, i, j, tabs,
1290 eq_i, ineq_i, eq_j, ineq_j);
1291 } else if (any(ineq_i, map->p[i]->n_ineq, STATUS_ADJ_EQ) ||
1292 any(ineq_j, map->p[j]->n_ineq, STATUS_ADJ_EQ)) {
1293 /* Can't happen */
1294 /* BAD ADJ INEQ */
1295 } else if (any(ineq_i, map->p[i]->n_ineq, STATUS_ADJ_INEQ) ||
1296 any(ineq_j, map->p[j]->n_ineq, STATUS_ADJ_INEQ)) {
1297 if (!any(eq_i, 2 * map->p[i]->n_eq, STATUS_CUT) &&
1298 !any(eq_j, 2 * map->p[j]->n_eq, STATUS_CUT))
1299 changed = check_adj_ineq(map, i, j, tabs,
1300 ineq_i, ineq_j);
1301 } else {
1302 if (!any(eq_i, 2 * map->p[i]->n_eq, STATUS_CUT) &&
1303 !any(eq_j, 2 * map->p[j]->n_eq, STATUS_CUT))
1304 changed = check_facets(map, i, j, tabs, ineq_i, ineq_j);
1305 if (!changed)
1306 changed = check_wrap(map, i, j, tabs,
1307 eq_i, ineq_i, eq_j, ineq_j);
1310 done:
1311 free(eq_i);
1312 free(eq_j);
1313 free(ineq_i);
1314 free(ineq_j);
1315 return changed;
1316 error:
1317 free(eq_i);
1318 free(eq_j);
1319 free(ineq_i);
1320 free(ineq_j);
1321 return -1;
1324 static struct isl_map *coalesce(struct isl_map *map, struct isl_tab **tabs)
1326 int i, j;
1328 for (i = map->n - 2; i >= 0; --i)
1329 restart:
1330 for (j = i + 1; j < map->n; ++j) {
1331 int changed;
1332 changed = coalesce_pair(map, i, j, tabs);
1333 if (changed < 0)
1334 goto error;
1335 if (changed)
1336 goto restart;
1338 return map;
1339 error:
1340 isl_map_free(map);
1341 return NULL;
1344 /* For each pair of basic maps in the map, check if the union of the two
1345 * can be represented by a single basic map.
1346 * If so, replace the pair by the single basic map and start over.
1348 struct isl_map *isl_map_coalesce(struct isl_map *map)
1350 int i;
1351 unsigned n;
1352 struct isl_tab **tabs = NULL;
1354 map = isl_map_remove_empty_parts(map);
1355 if (!map)
1356 return NULL;
1358 if (map->n <= 1)
1359 return map;
1361 map = isl_map_align_divs(map);
1363 tabs = isl_calloc_array(map->ctx, struct isl_tab *, map->n);
1364 if (!tabs)
1365 goto error;
1367 n = map->n;
1368 for (i = 0; i < map->n; ++i) {
1369 tabs[i] = isl_tab_from_basic_map(map->p[i], 0);
1370 if (!tabs[i])
1371 goto error;
1372 if (!ISL_F_ISSET(map->p[i], ISL_BASIC_MAP_NO_IMPLICIT))
1373 if (isl_tab_detect_implicit_equalities(tabs[i]) < 0)
1374 goto error;
1375 if (!ISL_F_ISSET(map->p[i], ISL_BASIC_MAP_NO_REDUNDANT))
1376 if (isl_tab_detect_redundant(tabs[i]) < 0)
1377 goto error;
1379 for (i = map->n - 1; i >= 0; --i)
1380 if (tabs[i]->empty)
1381 drop(map, i, tabs);
1383 map = coalesce(map, tabs);
1385 if (map)
1386 for (i = 0; i < map->n; ++i) {
1387 map->p[i] = isl_basic_map_update_from_tab(map->p[i],
1388 tabs[i]);
1389 map->p[i] = isl_basic_map_finalize(map->p[i]);
1390 if (!map->p[i])
1391 goto error;
1392 ISL_F_SET(map->p[i], ISL_BASIC_MAP_NO_IMPLICIT);
1393 ISL_F_SET(map->p[i], ISL_BASIC_MAP_NO_REDUNDANT);
1396 for (i = 0; i < n; ++i)
1397 isl_tab_free(tabs[i]);
1399 free(tabs);
1401 return map;
1402 error:
1403 if (tabs)
1404 for (i = 0; i < n; ++i)
1405 isl_tab_free(tabs[i]);
1406 free(tabs);
1407 isl_map_free(map);
1408 return NULL;
1411 /* For each pair of basic sets in the set, check if the union of the two
1412 * can be represented by a single basic set.
1413 * If so, replace the pair by the single basic set and start over.
1415 struct isl_set *isl_set_coalesce(struct isl_set *set)
1417 return (struct isl_set *)isl_map_coalesce((struct isl_map *)set);