add isl_basic_set_full_compression
[isl.git] / isl_coalesce.c
blob73f44ed070f6fe1d9a65e1844a86321f01efcfb3
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_tab.h"
17 #define STATUS_ERROR -1
18 #define STATUS_REDUNDANT 1
19 #define STATUS_VALID 2
20 #define STATUS_SEPARATE 3
21 #define STATUS_CUT 4
22 #define STATUS_ADJ_EQ 5
23 #define STATUS_ADJ_INEQ 6
25 static int status_in(isl_int *ineq, struct isl_tab *tab)
27 enum isl_ineq_type type = isl_tab_ineq_type(tab, ineq);
28 switch (type) {
29 default:
30 case isl_ineq_error: return STATUS_ERROR;
31 case isl_ineq_redundant: return STATUS_VALID;
32 case isl_ineq_separate: return STATUS_SEPARATE;
33 case isl_ineq_cut: return STATUS_CUT;
34 case isl_ineq_adj_eq: return STATUS_ADJ_EQ;
35 case isl_ineq_adj_ineq: return STATUS_ADJ_INEQ;
39 /* Compute the position of the equalities of basic map "i"
40 * with respect to basic map "j".
41 * The resulting array has twice as many entries as the number
42 * of equalities corresponding to the two inequalties to which
43 * each equality corresponds.
45 static int *eq_status_in(struct isl_map *map, int i, int j,
46 struct isl_tab **tabs)
48 int k, l;
49 int *eq = isl_calloc_array(map->ctx, int, 2 * map->p[i]->n_eq);
50 unsigned dim;
52 dim = isl_basic_map_total_dim(map->p[i]);
53 for (k = 0; k < map->p[i]->n_eq; ++k) {
54 for (l = 0; l < 2; ++l) {
55 isl_seq_neg(map->p[i]->eq[k], map->p[i]->eq[k], 1+dim);
56 eq[2 * k + l] = status_in(map->p[i]->eq[k], tabs[j]);
57 if (eq[2 * k + l] == STATUS_ERROR)
58 goto error;
60 if (eq[2 * k] == STATUS_SEPARATE ||
61 eq[2 * k + 1] == STATUS_SEPARATE)
62 break;
65 return eq;
66 error:
67 free(eq);
68 return NULL;
71 /* Compute the position of the inequalities of basic map "i"
72 * with respect to basic map "j".
74 static int *ineq_status_in(struct isl_map *map, int i, int j,
75 struct isl_tab **tabs)
77 int k;
78 unsigned n_eq = map->p[i]->n_eq;
79 int *ineq = isl_calloc_array(map->ctx, int, map->p[i]->n_ineq);
81 for (k = 0; k < map->p[i]->n_ineq; ++k) {
82 if (isl_tab_is_redundant(tabs[i], n_eq + k)) {
83 ineq[k] = STATUS_REDUNDANT;
84 continue;
86 ineq[k] = status_in(map->p[i]->ineq[k], tabs[j]);
87 if (ineq[k] == STATUS_ERROR)
88 goto error;
89 if (ineq[k] == STATUS_SEPARATE)
90 break;
93 return ineq;
94 error:
95 free(ineq);
96 return NULL;
99 static int any(int *con, unsigned len, int status)
101 int i;
103 for (i = 0; i < len ; ++i)
104 if (con[i] == status)
105 return 1;
106 return 0;
109 static int count(int *con, unsigned len, int status)
111 int i;
112 int c = 0;
114 for (i = 0; i < len ; ++i)
115 if (con[i] == status)
116 c++;
117 return c;
120 static int all(int *con, unsigned len, int status)
122 int i;
124 for (i = 0; i < len ; ++i) {
125 if (con[i] == STATUS_REDUNDANT)
126 continue;
127 if (con[i] != status)
128 return 0;
130 return 1;
133 static void drop(struct isl_map *map, int i, struct isl_tab **tabs)
135 isl_basic_map_free(map->p[i]);
136 isl_tab_free(tabs[i]);
138 if (i != map->n - 1) {
139 map->p[i] = map->p[map->n - 1];
140 tabs[i] = tabs[map->n - 1];
142 tabs[map->n - 1] = NULL;
143 map->n--;
146 /* Replace the pair of basic maps i and j by the basic map bounded
147 * by the valid constraints in both basic maps and the constraint
148 * in extra (if not NULL).
150 static int fuse(struct isl_map *map, int i, int j,
151 struct isl_tab **tabs, int *eq_i, int *ineq_i, int *eq_j, int *ineq_j,
152 __isl_keep isl_mat *extra)
154 int k, l;
155 struct isl_basic_map *fused = NULL;
156 struct isl_tab *fused_tab = NULL;
157 unsigned total = isl_basic_map_total_dim(map->p[i]);
158 unsigned extra_rows = extra ? extra->n_row : 0;
160 fused = isl_basic_map_alloc_dim(isl_dim_copy(map->p[i]->dim),
161 map->p[i]->n_div,
162 map->p[i]->n_eq + map->p[j]->n_eq,
163 map->p[i]->n_ineq + map->p[j]->n_ineq + extra_rows);
164 if (!fused)
165 goto error;
167 for (k = 0; k < map->p[i]->n_eq; ++k) {
168 if (eq_i && (eq_i[2 * k] != STATUS_VALID ||
169 eq_i[2 * k + 1] != STATUS_VALID))
170 continue;
171 l = isl_basic_map_alloc_equality(fused);
172 if (l < 0)
173 goto error;
174 isl_seq_cpy(fused->eq[l], map->p[i]->eq[k], 1 + total);
177 for (k = 0; k < map->p[j]->n_eq; ++k) {
178 if (eq_j && (eq_j[2 * k] != STATUS_VALID ||
179 eq_j[2 * k + 1] != STATUS_VALID))
180 continue;
181 l = isl_basic_map_alloc_equality(fused);
182 if (l < 0)
183 goto error;
184 isl_seq_cpy(fused->eq[l], map->p[j]->eq[k], 1 + total);
187 for (k = 0; k < map->p[i]->n_ineq; ++k) {
188 if (ineq_i[k] != STATUS_VALID)
189 continue;
190 l = isl_basic_map_alloc_inequality(fused);
191 if (l < 0)
192 goto error;
193 isl_seq_cpy(fused->ineq[l], map->p[i]->ineq[k], 1 + total);
196 for (k = 0; k < map->p[j]->n_ineq; ++k) {
197 if (ineq_j[k] != STATUS_VALID)
198 continue;
199 l = isl_basic_map_alloc_inequality(fused);
200 if (l < 0)
201 goto error;
202 isl_seq_cpy(fused->ineq[l], map->p[j]->ineq[k], 1 + total);
205 for (k = 0; k < map->p[i]->n_div; ++k) {
206 int l = isl_basic_map_alloc_div(fused);
207 if (l < 0)
208 goto error;
209 isl_seq_cpy(fused->div[l], map->p[i]->div[k], 1 + 1 + total);
212 for (k = 0; k < extra_rows; ++k) {
213 l = isl_basic_map_alloc_inequality(fused);
214 if (l < 0)
215 goto error;
216 isl_seq_cpy(fused->ineq[l], extra->row[k], 1 + total);
219 fused = isl_basic_map_gauss(fused, NULL);
220 ISL_F_SET(fused, ISL_BASIC_MAP_FINAL);
221 if (ISL_F_ISSET(map->p[i], ISL_BASIC_MAP_RATIONAL) &&
222 ISL_F_ISSET(map->p[j], ISL_BASIC_MAP_RATIONAL))
223 ISL_F_SET(fused, ISL_BASIC_MAP_RATIONAL);
225 fused_tab = isl_tab_from_basic_map(fused);
226 if (isl_tab_detect_redundant(fused_tab) < 0)
227 goto error;
229 isl_basic_map_free(map->p[i]);
230 map->p[i] = fused;
231 isl_tab_free(tabs[i]);
232 tabs[i] = fused_tab;
233 drop(map, j, tabs);
235 return 1;
236 error:
237 isl_tab_free(fused_tab);
238 isl_basic_map_free(fused);
239 return -1;
242 /* Given a pair of basic maps i and j such that all constraints are either
243 * "valid" or "cut", check if the facets corresponding to the "cut"
244 * constraints of i lie entirely within basic map j.
245 * If so, replace the pair by the basic map consisting of the valid
246 * constraints in both basic maps.
248 * To see that we are not introducing any extra points, call the
249 * two basic maps A and B and the resulting map U and let x
250 * be an element of U \setminus ( A \cup B ).
251 * Then there is a pair of cut constraints c_1 and c_2 in A and B such that x
252 * violates them. Let X be the intersection of U with the opposites
253 * of these constraints. Then x \in X.
254 * The facet corresponding to c_1 contains the corresponding facet of A.
255 * This facet is entirely contained in B, so c_2 is valid on the facet.
256 * However, since it is also (part of) a facet of X, -c_2 is also valid
257 * on the facet. This means c_2 is saturated on the facet, so c_1 and
258 * c_2 must be opposites of each other, but then x could not violate
259 * both of them.
261 static int check_facets(struct isl_map *map, int i, int j,
262 struct isl_tab **tabs, int *ineq_i, int *ineq_j)
264 int k, l;
265 struct isl_tab_undo *snap;
266 unsigned n_eq = map->p[i]->n_eq;
268 snap = isl_tab_snap(tabs[i]);
270 for (k = 0; k < map->p[i]->n_ineq; ++k) {
271 if (ineq_i[k] != STATUS_CUT)
272 continue;
273 tabs[i] = isl_tab_select_facet(tabs[i], n_eq + k);
274 for (l = 0; l < map->p[j]->n_ineq; ++l) {
275 int stat;
276 if (ineq_j[l] != STATUS_CUT)
277 continue;
278 stat = status_in(map->p[j]->ineq[l], tabs[i]);
279 if (stat != STATUS_VALID)
280 break;
282 if (isl_tab_rollback(tabs[i], snap) < 0)
283 return -1;
284 if (l < map->p[j]->n_ineq)
285 break;
288 if (k < map->p[i]->n_ineq)
289 /* BAD CUT PAIR */
290 return 0;
291 return fuse(map, i, j, tabs, NULL, ineq_i, NULL, ineq_j, NULL);
294 /* Both basic maps have at least one inequality with and adjacent
295 * (but opposite) inequality in the other basic map.
296 * Check that there are no cut constraints and that there is only
297 * a single pair of adjacent inequalities.
298 * If so, we can replace the pair by a single basic map described
299 * by all but the pair of adjacent inequalities.
300 * Any additional points introduced lie strictly between the two
301 * adjacent hyperplanes and can therefore be integral.
303 * ____ _____
304 * / ||\ / \
305 * / || \ / \
306 * \ || \ => \ \
307 * \ || / \ /
308 * \___||_/ \_____/
310 * The test for a single pair of adjancent inequalities is important
311 * for avoiding the combination of two basic maps like the following
313 * /|
314 * / |
315 * /__|
316 * _____
317 * | |
318 * | |
319 * |___|
321 static int check_adj_ineq(struct isl_map *map, int i, int j,
322 struct isl_tab **tabs, int *ineq_i, int *ineq_j)
324 int changed = 0;
326 if (any(ineq_i, map->p[i]->n_ineq, STATUS_CUT) ||
327 any(ineq_j, map->p[j]->n_ineq, STATUS_CUT))
328 /* ADJ INEQ CUT */
330 else if (count(ineq_i, map->p[i]->n_ineq, STATUS_ADJ_INEQ) == 1 &&
331 count(ineq_j, map->p[j]->n_ineq, STATUS_ADJ_INEQ) == 1)
332 changed = fuse(map, i, j, tabs, NULL, ineq_i, NULL, ineq_j, NULL);
333 /* else ADJ INEQ TOO MANY */
335 return changed;
338 /* Check if basic map "i" contains the basic map represented
339 * by the tableau "tab".
341 static int contains(struct isl_map *map, int i, int *ineq_i,
342 struct isl_tab *tab)
344 int k, l;
345 unsigned dim;
347 dim = isl_basic_map_total_dim(map->p[i]);
348 for (k = 0; k < map->p[i]->n_eq; ++k) {
349 for (l = 0; l < 2; ++l) {
350 int stat;
351 isl_seq_neg(map->p[i]->eq[k], map->p[i]->eq[k], 1+dim);
352 stat = status_in(map->p[i]->eq[k], tab);
353 if (stat != STATUS_VALID)
354 return 0;
358 for (k = 0; k < map->p[i]->n_ineq; ++k) {
359 int stat;
360 if (ineq_i[k] == STATUS_REDUNDANT)
361 continue;
362 stat = status_in(map->p[i]->ineq[k], tab);
363 if (stat != STATUS_VALID)
364 return 0;
366 return 1;
369 /* Basic map "i" has an inequality "k" that is adjacent to some equality
370 * of basic map "j". All the other inequalities are valid for "j".
371 * Check if basic map "j" forms an extension of basic map "i".
373 * In particular, we relax constraint "k", compute the corresponding
374 * facet and check whether it is included in the other basic map.
375 * If so, we know that relaxing the constraint extends the basic
376 * map with exactly the other basic map (we already know that this
377 * other basic map is included in the extension, because there
378 * were no "cut" inequalities in "i") and we can replace the
379 * two basic maps by thie extension.
380 * ____ _____
381 * / || / |
382 * / || / |
383 * \ || => \ |
384 * \ || \ |
385 * \___|| \____|
387 static int is_extension(struct isl_map *map, int i, int j, int k,
388 struct isl_tab **tabs, int *eq_i, int *ineq_i, int *eq_j, int *ineq_j)
390 int changed = 0;
391 int super;
392 struct isl_tab_undo *snap, *snap2;
393 unsigned n_eq = map->p[i]->n_eq;
395 snap = isl_tab_snap(tabs[i]);
396 tabs[i] = isl_tab_relax(tabs[i], n_eq + k);
397 snap2 = isl_tab_snap(tabs[i]);
398 tabs[i] = isl_tab_select_facet(tabs[i], n_eq + k);
399 super = contains(map, j, ineq_j, tabs[i]);
400 if (super) {
401 if (isl_tab_rollback(tabs[i], snap2) < 0)
402 return -1;
403 map->p[i] = isl_basic_map_cow(map->p[i]);
404 if (!map->p[i])
405 return -1;
406 isl_int_add_ui(map->p[i]->ineq[k][0], map->p[i]->ineq[k][0], 1);
407 ISL_F_SET(map->p[i], ISL_BASIC_MAP_FINAL);
408 drop(map, j, tabs);
409 changed = 1;
410 } else
411 if (isl_tab_rollback(tabs[i], snap) < 0)
412 return -1;
414 return changed;
417 /* For each non-redundant constraint in "bmap" (as determined by "tab"),
418 * wrap the constraint around "bound" such that it includes the whole
419 * set "set" and append the resulting constraint to "wraps".
420 * "wraps" is assumed to have been pre-allocated to the appropriate size.
421 * wraps->n_row is the number of actual wrapped constraints that have
422 * been added.
423 * If any of the wrapping problems results in a constraint that is
424 * identical to "bound", then this means that "set" is unbounded in such
425 * way that no wrapping is possible. If this happens then wraps->n_row
426 * is reset to zero.
428 static int add_wraps(__isl_keep isl_mat *wraps, __isl_keep isl_basic_map *bmap,
429 struct isl_tab *tab, isl_int *bound, __isl_keep isl_set *set)
431 int l;
432 int w;
433 unsigned total = isl_basic_map_total_dim(bmap);
435 w = wraps->n_row;
437 for (l = 0; l < bmap->n_ineq; ++l) {
438 if (isl_seq_is_neg(bound, bmap->ineq[l], 1 + total))
439 continue;
440 if (isl_seq_eq(bound, bmap->ineq[l], 1 + total))
441 continue;
442 if (isl_tab_is_redundant(tab, bmap->n_eq + l))
443 continue;
445 isl_seq_cpy(wraps->row[w], bound, 1 + total);
446 if (!isl_set_wrap_facet(set, wraps->row[w], bmap->ineq[l]))
447 return -1;
448 if (isl_seq_eq(wraps->row[w], bound, 1 + total))
449 goto unbounded;
450 ++w;
452 for (l = 0; l < bmap->n_eq; ++l) {
453 if (isl_seq_is_neg(bound, bmap->eq[l], 1 + total))
454 continue;
455 if (isl_seq_eq(bound, bmap->eq[l], 1 + total))
456 continue;
458 isl_seq_cpy(wraps->row[w], bound, 1 + total);
459 isl_seq_neg(wraps->row[w + 1], bmap->eq[l], 1 + total);
460 if (!isl_set_wrap_facet(set, wraps->row[w], wraps->row[w + 1]))
461 return -1;
462 if (isl_seq_eq(wraps->row[w], bound, 1 + total))
463 goto unbounded;
464 ++w;
466 isl_seq_cpy(wraps->row[w], bound, 1 + total);
467 if (!isl_set_wrap_facet(set, wraps->row[w], bmap->eq[l]))
468 return -1;
469 if (isl_seq_eq(wraps->row[w], bound, 1 + total))
470 goto unbounded;
471 ++w;
474 wraps->n_row = w;
475 return 0;
476 unbounded:
477 wraps->n_row = 0;
478 return 0;
481 /* Check if the constraints in "wraps" from "first" until the last
482 * are all valid for the basic set represented by "tab".
483 * If not, wraps->n_row is set to zero.
485 static int check_wraps(__isl_keep isl_mat *wraps, int first,
486 struct isl_tab *tab)
488 int i;
490 for (i = first; i < wraps->n_row; ++i) {
491 enum isl_ineq_type type;
492 type = isl_tab_ineq_type(tab, wraps->row[i]);
493 if (type == isl_ineq_error)
494 return -1;
495 if (type == isl_ineq_redundant)
496 continue;
497 wraps->n_row = 0;
498 return 0;
501 return 0;
504 /* Return a set that corresponds to the non-redudant constraints
505 * (as recorded in tab) of bmap.
507 * It's important to remove the redundant constraints as some
508 * of the other constraints may have been modified after the
509 * constraints were marked redundant.
510 * In particular, a constraint may have been relaxed.
511 * Redundant constraints are ignored when a constraint is relaxed
512 * and should therefore continue to be ignored ever after.
513 * Otherwise, the relaxation might be thwarted by some of
514 * these constraints.
516 static __isl_give isl_set *set_from_updated_bmap(__isl_keep isl_basic_map *bmap,
517 struct isl_tab *tab)
519 bmap = isl_basic_map_copy(bmap);
520 bmap = isl_basic_map_cow(bmap);
521 bmap = isl_basic_map_update_from_tab(bmap, tab);
522 return isl_set_from_basic_set(isl_basic_map_underlying_set(bmap));
525 /* Given a basic set i with a constraint k that is adjacent to either the
526 * whole of basic set j or a facet of basic set j, check if we can wrap
527 * both the facet corresponding to k and the facet of j (or the whole of j)
528 * around their ridges to include the other set.
529 * If so, replace the pair of basic sets by their union.
531 * All constraints of i (except k) are assumed to be valid for j.
533 * However, the constraints of j may not be valid for i and so
534 * we have to check that the wrapping constraints for j are valid for i.
536 * In the case where j has a facet adjacent to i, tab[j] is assumed
537 * to have been restricted to this facet, so that the non-redundant
538 * constraints in tab[j] are the ridges of the facet.
539 * Note that for the purpose of wrapping, it does not matter whether
540 * we wrap the ridges of i around the whole of j or just around
541 * the facet since all the other constraints are assumed to be valid for j.
542 * In practice, we wrap to include the whole of j.
543 * ____ _____
544 * / | / \
545 * / || / |
546 * \ || => \ |
547 * \ || \ |
548 * \___|| \____|
551 static int can_wrap_in_facet(struct isl_map *map, int i, int j, int k,
552 struct isl_tab **tabs, int *eq_i, int *ineq_i, int *eq_j, int *ineq_j)
554 int changed = 0;
555 struct isl_mat *wraps = NULL;
556 struct isl_set *set_i = NULL;
557 struct isl_set *set_j = NULL;
558 struct isl_vec *bound = NULL;
559 unsigned total = isl_basic_map_total_dim(map->p[i]);
560 struct isl_tab_undo *snap;
561 int n;
563 set_i = set_from_updated_bmap(map->p[i], tabs[i]);
564 set_j = set_from_updated_bmap(map->p[j], tabs[j]);
565 wraps = isl_mat_alloc(map->ctx, 2 * (map->p[i]->n_eq + map->p[j]->n_eq) +
566 map->p[i]->n_ineq + map->p[j]->n_ineq,
567 1 + total);
568 bound = isl_vec_alloc(map->ctx, 1 + total);
569 if (!set_i || !set_j || !wraps || !bound)
570 goto error;
572 isl_seq_cpy(bound->el, map->p[i]->ineq[k], 1 + total);
573 isl_int_add_ui(bound->el[0], bound->el[0], 1);
575 isl_seq_cpy(wraps->row[0], bound->el, 1 + total);
576 wraps->n_row = 1;
578 if (add_wraps(wraps, map->p[j], tabs[j], bound->el, set_i) < 0)
579 goto error;
580 if (!wraps->n_row)
581 goto unbounded;
583 snap = isl_tab_snap(tabs[i]);
585 tabs[i] = isl_tab_select_facet(tabs[i], map->p[i]->n_eq + k);
586 if (isl_tab_detect_redundant(tabs[i]) < 0)
587 goto error;
589 isl_seq_neg(bound->el, map->p[i]->ineq[k], 1 + total);
591 n = wraps->n_row;
592 if (add_wraps(wraps, map->p[i], tabs[i], bound->el, set_j) < 0)
593 goto error;
595 if (isl_tab_rollback(tabs[i], snap) < 0)
596 goto error;
597 if (check_wraps(wraps, n, tabs[i]) < 0)
598 goto error;
599 if (!wraps->n_row)
600 goto unbounded;
602 changed = fuse(map, i, j, tabs, eq_i, ineq_i, eq_j, ineq_j, wraps);
604 unbounded:
605 isl_mat_free(wraps);
607 isl_set_free(set_i);
608 isl_set_free(set_j);
610 isl_vec_free(bound);
612 return changed;
613 error:
614 isl_vec_free(bound);
615 isl_mat_free(wraps);
616 isl_set_free(set_i);
617 isl_set_free(set_j);
618 return -1;
621 /* Set the is_redundant property of the "n" constraints in "cuts",
622 * except "k" to "v".
623 * This is a fairly tricky operation as it bypasses isl_tab.c.
624 * The reason we want to temporarily mark some constraints redundant
625 * is that we want to ignore them in add_wraps.
627 * Initially all cut constraints are non-redundant, but the
628 * selection of a facet right before the call to this function
629 * may have made some of them redundant.
630 * Likewise, the same constraints are marked non-redundant
631 * in the second call to this function, before they are officially
632 * made non-redundant again in the subsequent rollback.
634 static void set_is_redundant(struct isl_tab *tab, unsigned n_eq,
635 int *cuts, int n, int k, int v)
637 int l;
639 for (l = 0; l < n; ++l) {
640 if (l == k)
641 continue;
642 tab->con[n_eq + cuts[l]].is_redundant = v;
646 /* Given a pair of basic maps i and j such that j stick out
647 * of i at n cut constraints, each time by at most one,
648 * try to compute wrapping constraints and replace the two
649 * basic maps by a single basic map.
650 * The other constraints of i are assumed to be valid for j.
652 * The facets of i corresponding to the cut constraints are
653 * wrapped around their ridges, except those ridges determined
654 * by any of the other cut constraints.
655 * The intersections of cut constraints need to be ignored
656 * as the result of wrapping on cur constraint around another
657 * would result in a constraint cutting the union.
658 * In each case, the facets are wrapped to include the union
659 * of the two basic maps.
661 * The pieces of j that lie at an offset of exactly one from
662 * one of the cut constraints of i are wrapped around their edges.
663 * Here, there is no need to ignore intersections because we
664 * are wrapping around the union of the two basic maps.
666 * If any wrapping fails, i.e., if we cannot wrap to touch
667 * the union, then we give up.
668 * Otherwise, the pair of basic maps is replaced by their union.
670 static int wrap_in_facets(struct isl_map *map, int i, int j,
671 int *cuts, int n, struct isl_tab **tabs,
672 int *eq_i, int *ineq_i, int *eq_j, int *ineq_j)
674 int changed = 0;
675 isl_mat *wraps = NULL;
676 isl_set *set = NULL;
677 isl_vec *bound = NULL;
678 unsigned total = isl_basic_map_total_dim(map->p[i]);
679 int max_wrap;
680 int k;
681 struct isl_tab_undo *snap_i, *snap_j;
683 if (isl_tab_extend_cons(tabs[j], 1) < 0)
684 goto error;
686 max_wrap = 2 * (map->p[i]->n_eq + map->p[j]->n_eq) +
687 map->p[i]->n_ineq + map->p[j]->n_ineq;
688 max_wrap *= n;
690 set = isl_set_union(set_from_updated_bmap(map->p[i], tabs[i]),
691 set_from_updated_bmap(map->p[j], tabs[j]));
692 wraps = isl_mat_alloc(map->ctx, max_wrap, 1 + total);
693 bound = isl_vec_alloc(map->ctx, 1 + total);
694 if (!set || !wraps || !bound)
695 goto error;
697 snap_i = isl_tab_snap(tabs[i]);
698 snap_j = isl_tab_snap(tabs[j]);
700 wraps->n_row = 0;
702 for (k = 0; k < n; ++k) {
703 tabs[i] = isl_tab_select_facet(tabs[i],
704 map->p[i]->n_eq + cuts[k]);
705 if (isl_tab_detect_redundant(tabs[i]) < 0)
706 goto error;
707 set_is_redundant(tabs[i], map->p[i]->n_eq, cuts, n, k, 1);
709 isl_seq_neg(bound->el, map->p[i]->ineq[cuts[k]], 1 + total);
710 if (add_wraps(wraps, map->p[i], tabs[i], bound->el, set) < 0)
711 goto error;
713 set_is_redundant(tabs[i], map->p[i]->n_eq, cuts, n, k, 0);
714 if (isl_tab_rollback(tabs[i], snap_i) < 0)
715 goto error;
717 if (!wraps->n_row)
718 break;
720 isl_seq_cpy(bound->el, map->p[i]->ineq[cuts[k]], 1 + total);
721 isl_int_add_ui(bound->el[0], bound->el[0], 1);
722 tabs[j] = isl_tab_add_eq(tabs[j], bound->el);
723 if (isl_tab_detect_redundant(tabs[j]) < 0)
724 goto error;
726 if (!tabs[j]->empty &&
727 add_wraps(wraps, map->p[j], tabs[j], bound->el, set) < 0)
728 goto error;
730 if (isl_tab_rollback(tabs[j], snap_j) < 0)
731 goto error;
733 if (!wraps->n_row)
734 break;
737 if (k == n)
738 changed = fuse(map, i, j, tabs,
739 eq_i, ineq_i, eq_j, ineq_j, wraps);
741 isl_vec_free(bound);
742 isl_mat_free(wraps);
743 isl_set_free(set);
745 return changed;
746 error:
747 isl_vec_free(bound);
748 isl_mat_free(wraps);
749 isl_set_free(set);
750 return -1;
753 /* Given two basic sets i and j such that i has not cut equalities,
754 * check if relaxing all the cut inequalities of i by one turns
755 * them into valid constraint for j and check if we can wrap in
756 * the bits that are sticking out.
757 * If so, replace the pair by their union.
759 * We first check if all relaxed cut inequalities of i are valid for j
760 * and then try to wrap in the intersections of the relaxed cut inequalities
761 * with j.
763 * During this wrapping, we consider the points of j that lie at a distance
764 * of exactly 1 from i. In particular, we ignore the points that lie in
765 * between this lower-dimensional space and the basic map i.
766 * We can therefore only apply this to integer maps.
767 * ____ _____
768 * / ___|_ / \
769 * / | | / |
770 * \ | | => \ |
771 * \|____| \ |
772 * \___| \____/
774 * _____ ______
775 * | ____|_ | \
776 * | | | | |
777 * | | | => | |
778 * |_| | | |
779 * |_____| \______|
781 * _______
782 * | |
783 * | |\ |
784 * | | \ |
785 * | | \ |
786 * | | \|
787 * | | \
788 * | |_____\
789 * | |
790 * |_______|
792 * Wrapping can fail if the result of wrapping one of the facets
793 * around its edges does not produce any new facet constraint.
794 * In particular, this happens when we try to wrap in unbounded sets.
796 * _______________________________________________________________________
798 * | ___
799 * | | |
800 * |_| |_________________________________________________________________
801 * |___|
803 * The following is not an acceptable result of coalescing the above two
804 * sets as it includes extra integer points.
805 * _______________________________________________________________________
807 * |
808 * |
810 * \______________________________________________________________________
812 static int can_wrap_in_set(struct isl_map *map, int i, int j,
813 struct isl_tab **tabs, int *eq_i, int *ineq_i, int *eq_j, int *ineq_j)
815 int changed = 0;
816 int k, m;
817 int n;
818 int *cuts = NULL;
820 if (ISL_F_ISSET(map->p[i], ISL_BASIC_MAP_RATIONAL) ||
821 ISL_F_ISSET(map->p[j], ISL_BASIC_MAP_RATIONAL))
822 return 0;
824 n = count(ineq_i, map->p[i]->n_ineq, STATUS_CUT);
825 if (n == 0)
826 return 0;
828 cuts = isl_alloc_array(map->ctx, int, n);
829 if (!cuts)
830 return -1;
832 for (k = 0, m = 0; m < n; ++k) {
833 enum isl_ineq_type type;
835 if (ineq_i[k] != STATUS_CUT)
836 continue;
838 isl_int_add_ui(map->p[i]->ineq[k][0], map->p[i]->ineq[k][0], 1);
839 type = isl_tab_ineq_type(tabs[j], map->p[i]->ineq[k]);
840 isl_int_sub_ui(map->p[i]->ineq[k][0], map->p[i]->ineq[k][0], 1);
841 if (type == isl_ineq_error)
842 goto error;
843 if (type != isl_ineq_redundant)
844 break;
845 cuts[m] = k;
846 ++m;
849 if (m == n)
850 changed = wrap_in_facets(map, i, j, cuts, n, tabs,
851 eq_i, ineq_i, eq_j, ineq_j);
853 free(cuts);
855 return changed;
856 error:
857 free(cuts);
858 return -1;
861 /* Check if either i or j has a single cut constraint that can
862 * be used to wrap in (a facet of) the other basic set.
863 * if so, replace the pair by their union.
865 static int check_wrap(struct isl_map *map, int i, int j,
866 struct isl_tab **tabs, int *eq_i, int *ineq_i, int *eq_j, int *ineq_j)
868 int changed = 0;
870 if (!any(eq_i, 2 * map->p[i]->n_eq, STATUS_CUT))
871 changed = can_wrap_in_set(map, i, j, tabs,
872 eq_i, ineq_i, eq_j, ineq_j);
873 if (changed)
874 return changed;
876 if (!any(eq_j, 2 * map->p[j]->n_eq, STATUS_CUT))
877 changed = can_wrap_in_set(map, j, i, tabs,
878 eq_j, ineq_j, eq_i, ineq_i);
879 return changed;
882 /* At least one of the basic maps has an equality that is adjacent
883 * to inequality. Make sure that only one of the basic maps has
884 * such an equality and that the other basic map has exactly one
885 * inequality adjacent to an equality.
886 * We call the basic map that has the inequality "i" and the basic
887 * map that has the equality "j".
888 * If "i" has any "cut" (in)equality, then relaxing the inequality
889 * by one would not result in a basic map that contains the other
890 * basic map.
892 static int check_adj_eq(struct isl_map *map, int i, int j,
893 struct isl_tab **tabs, int *eq_i, int *ineq_i, int *eq_j, int *ineq_j)
895 int changed = 0;
896 int k;
898 if (any(eq_i, 2 * map->p[i]->n_eq, STATUS_ADJ_INEQ) &&
899 any(eq_j, 2 * map->p[j]->n_eq, STATUS_ADJ_INEQ))
900 /* ADJ EQ TOO MANY */
901 return 0;
903 if (any(eq_i, 2 * map->p[i]->n_eq, STATUS_ADJ_INEQ))
904 return check_adj_eq(map, j, i, tabs,
905 eq_j, ineq_j, eq_i, ineq_i);
907 /* j has an equality adjacent to an inequality in i */
909 if (any(eq_i, 2 * map->p[i]->n_eq, STATUS_CUT))
910 return 0;
911 if (any(ineq_i, map->p[i]->n_ineq, STATUS_CUT))
912 /* ADJ EQ CUT */
913 return 0;
914 if (count(eq_j, 2 * map->p[j]->n_eq, STATUS_ADJ_INEQ) != 1 ||
915 count(ineq_i, map->p[i]->n_ineq, STATUS_ADJ_EQ) != 1 ||
916 any(ineq_j, map->p[j]->n_ineq, STATUS_ADJ_EQ) ||
917 any(ineq_i, map->p[i]->n_ineq, STATUS_ADJ_INEQ) ||
918 any(ineq_j, map->p[j]->n_ineq, STATUS_ADJ_INEQ))
919 /* ADJ EQ TOO MANY */
920 return 0;
922 for (k = 0; k < map->p[i]->n_ineq ; ++k)
923 if (ineq_i[k] == STATUS_ADJ_EQ)
924 break;
926 changed = is_extension(map, i, j, k, tabs, eq_i, ineq_i, eq_j, ineq_j);
927 if (changed)
928 return changed;
930 changed = can_wrap_in_facet(map, i, j, k, tabs, eq_i, ineq_i, eq_j, ineq_j);
932 return changed;
935 /* Check if the union of the given pair of basic maps
936 * can be represented by a single basic map.
937 * If so, replace the pair by the single basic map and return 1.
938 * Otherwise, return 0;
940 * We first check the effect of each constraint of one basic map
941 * on the other basic map.
942 * The constraint may be
943 * redundant the constraint is redundant in its own
944 * basic map and should be ignore and removed
945 * in the end
946 * valid all (integer) points of the other basic map
947 * satisfy the constraint
948 * separate no (integer) point of the other basic map
949 * satisfies the constraint
950 * cut some but not all points of the other basic map
951 * satisfy the constraint
952 * adj_eq the given constraint is adjacent (on the outside)
953 * to an equality of the other basic map
954 * adj_ineq the given constraint is adjacent (on the outside)
955 * to an inequality of the other basic map
957 * We consider six cases in which we can replace the pair by a single
958 * basic map. We ignore all "redundant" constraints.
960 * 1. all constraints of one basic map are valid
961 * => the other basic map is a subset and can be removed
963 * 2. all constraints of both basic maps are either "valid" or "cut"
964 * and the facets corresponding to the "cut" constraints
965 * of one of the basic maps lies entirely inside the other basic map
966 * => the pair can be replaced by a basic map consisting
967 * of the valid constraints in both basic maps
969 * 3. there is a single pair of adjacent inequalities
970 * (all other constraints are "valid")
971 * => the pair can be replaced by a basic map consisting
972 * of the valid constraints in both basic maps
974 * 4. there is a single adjacent pair of an inequality and an equality,
975 * the other constraints of the basic map containing the inequality are
976 * "valid". Moreover, if the inequality the basic map is relaxed
977 * and then turned into an equality, then resulting facet lies
978 * entirely inside the other basic map
979 * => the pair can be replaced by the basic map containing
980 * the inequality, with the inequality relaxed.
982 * 5. there is a single adjacent pair of an inequality and an equality,
983 * the other constraints of the basic map containing the inequality are
984 * "valid". Moreover, the facets corresponding to both
985 * the inequality and the equality can be wrapped around their
986 * ridges to include the other basic map
987 * => the pair can be replaced by a basic map consisting
988 * of the valid constraints in both basic maps together
989 * with all wrapping constraints
991 * 6. one of the basic maps extends beyond the other by at most one.
992 * Moreover, the facets corresponding to the cut constraints and
993 * the pieces of the other basic map at offset one from these cut
994 * constraints can be wrapped around their ridges to include
995 * the unione of the two basic maps
996 * => the pair can be replaced by a basic map consisting
997 * of the valid constraints in both basic maps together
998 * with all wrapping constraints
1000 * Throughout the computation, we maintain a collection of tableaus
1001 * corresponding to the basic maps. When the basic maps are dropped
1002 * or combined, the tableaus are modified accordingly.
1004 static int coalesce_pair(struct isl_map *map, int i, int j,
1005 struct isl_tab **tabs)
1007 int changed = 0;
1008 int *eq_i = NULL;
1009 int *eq_j = NULL;
1010 int *ineq_i = NULL;
1011 int *ineq_j = NULL;
1013 eq_i = eq_status_in(map, i, j, tabs);
1014 if (any(eq_i, 2 * map->p[i]->n_eq, STATUS_ERROR))
1015 goto error;
1016 if (any(eq_i, 2 * map->p[i]->n_eq, STATUS_SEPARATE))
1017 goto done;
1019 eq_j = eq_status_in(map, j, i, tabs);
1020 if (any(eq_j, 2 * map->p[j]->n_eq, STATUS_ERROR))
1021 goto error;
1022 if (any(eq_j, 2 * map->p[j]->n_eq, STATUS_SEPARATE))
1023 goto done;
1025 ineq_i = ineq_status_in(map, i, j, tabs);
1026 if (any(ineq_i, map->p[i]->n_ineq, STATUS_ERROR))
1027 goto error;
1028 if (any(ineq_i, map->p[i]->n_ineq, STATUS_SEPARATE))
1029 goto done;
1031 ineq_j = ineq_status_in(map, j, i, tabs);
1032 if (any(ineq_j, map->p[j]->n_ineq, STATUS_ERROR))
1033 goto error;
1034 if (any(ineq_j, map->p[j]->n_ineq, STATUS_SEPARATE))
1035 goto done;
1037 if (all(eq_i, 2 * map->p[i]->n_eq, STATUS_VALID) &&
1038 all(ineq_i, map->p[i]->n_ineq, STATUS_VALID)) {
1039 drop(map, j, tabs);
1040 changed = 1;
1041 } else if (all(eq_j, 2 * map->p[j]->n_eq, STATUS_VALID) &&
1042 all(ineq_j, map->p[j]->n_ineq, STATUS_VALID)) {
1043 drop(map, i, tabs);
1044 changed = 1;
1045 } else if (any(eq_i, 2 * map->p[i]->n_eq, STATUS_ADJ_EQ) ||
1046 any(eq_j, 2 * map->p[j]->n_eq, STATUS_ADJ_EQ)) {
1047 /* ADJ EQ PAIR */
1048 } else if (any(eq_i, 2 * map->p[i]->n_eq, STATUS_ADJ_INEQ) ||
1049 any(eq_j, 2 * map->p[j]->n_eq, STATUS_ADJ_INEQ)) {
1050 changed = check_adj_eq(map, i, j, tabs,
1051 eq_i, ineq_i, eq_j, ineq_j);
1052 } else if (any(ineq_i, map->p[i]->n_ineq, STATUS_ADJ_EQ) ||
1053 any(ineq_j, map->p[j]->n_ineq, STATUS_ADJ_EQ)) {
1054 /* Can't happen */
1055 /* BAD ADJ INEQ */
1056 } else if (any(ineq_i, map->p[i]->n_ineq, STATUS_ADJ_INEQ) ||
1057 any(ineq_j, map->p[j]->n_ineq, STATUS_ADJ_INEQ)) {
1058 if (!any(eq_i, 2 * map->p[i]->n_eq, STATUS_CUT) &&
1059 !any(eq_j, 2 * map->p[j]->n_eq, STATUS_CUT))
1060 changed = check_adj_ineq(map, i, j, tabs,
1061 ineq_i, ineq_j);
1062 } else {
1063 if (!any(eq_i, 2 * map->p[i]->n_eq, STATUS_CUT) &&
1064 !any(eq_j, 2 * map->p[j]->n_eq, STATUS_CUT))
1065 changed = check_facets(map, i, j, tabs, ineq_i, ineq_j);
1066 if (!changed)
1067 changed = check_wrap(map, i, j, tabs,
1068 eq_i, ineq_i, eq_j, ineq_j);
1071 done:
1072 free(eq_i);
1073 free(eq_j);
1074 free(ineq_i);
1075 free(ineq_j);
1076 return changed;
1077 error:
1078 free(eq_i);
1079 free(eq_j);
1080 free(ineq_i);
1081 free(ineq_j);
1082 return -1;
1085 static struct isl_map *coalesce(struct isl_map *map, struct isl_tab **tabs)
1087 int i, j;
1089 for (i = map->n - 2; i >= 0; --i)
1090 restart:
1091 for (j = i + 1; j < map->n; ++j) {
1092 int changed;
1093 changed = coalesce_pair(map, i, j, tabs);
1094 if (changed < 0)
1095 goto error;
1096 if (changed)
1097 goto restart;
1099 return map;
1100 error:
1101 isl_map_free(map);
1102 return NULL;
1105 /* For each pair of basic maps in the map, check if the union of the two
1106 * can be represented by a single basic map.
1107 * If so, replace the pair by the single basic map and start over.
1109 struct isl_map *isl_map_coalesce(struct isl_map *map)
1111 int i;
1112 unsigned n;
1113 struct isl_tab **tabs = NULL;
1115 if (!map)
1116 return NULL;
1118 if (map->n <= 1)
1119 return map;
1121 map = isl_map_align_divs(map);
1123 tabs = isl_calloc_array(map->ctx, struct isl_tab *, map->n);
1124 if (!tabs)
1125 goto error;
1127 n = map->n;
1128 for (i = 0; i < map->n; ++i) {
1129 tabs[i] = isl_tab_from_basic_map(map->p[i]);
1130 if (!tabs[i])
1131 goto error;
1132 if (!ISL_F_ISSET(map->p[i], ISL_BASIC_MAP_NO_IMPLICIT))
1133 tabs[i] = isl_tab_detect_implicit_equalities(tabs[i]);
1134 if (!ISL_F_ISSET(map->p[i], ISL_BASIC_MAP_NO_REDUNDANT))
1135 if (isl_tab_detect_redundant(tabs[i]) < 0)
1136 goto error;
1138 for (i = map->n - 1; i >= 0; --i)
1139 if (tabs[i]->empty)
1140 drop(map, i, tabs);
1142 map = coalesce(map, tabs);
1144 if (map)
1145 for (i = 0; i < map->n; ++i) {
1146 map->p[i] = isl_basic_map_update_from_tab(map->p[i],
1147 tabs[i]);
1148 map->p[i] = isl_basic_map_finalize(map->p[i]);
1149 if (!map->p[i])
1150 goto error;
1151 ISL_F_SET(map->p[i], ISL_BASIC_MAP_NO_IMPLICIT);
1152 ISL_F_SET(map->p[i], ISL_BASIC_MAP_NO_REDUNDANT);
1155 for (i = 0; i < n; ++i)
1156 isl_tab_free(tabs[i]);
1158 free(tabs);
1160 return map;
1161 error:
1162 if (tabs)
1163 for (i = 0; i < n; ++i)
1164 isl_tab_free(tabs[i]);
1165 free(tabs);
1166 return NULL;
1169 /* For each pair of basic sets in the set, check if the union of the two
1170 * can be represented by a single basic set.
1171 * If so, replace the pair by the single basic set and start over.
1173 struct isl_set *isl_set_coalesce(struct isl_set *set)
1175 return (struct isl_set *)isl_map_coalesce((struct isl_map *)set);