add isl_basic_map_detect_inequality_pairs
[isl.git] / isl_coalesce.c
blobb8194985cdad5dc8175f3ee699eb23fa87f111d0
1 /*
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"
16 #include <isl_seq.h>
17 #include <isl/options.h>
18 #include "isl_tab.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
27 #define STATUS_CUT 4
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);
34 switch (type) {
35 default:
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)
54 int k, l;
55 int *eq = isl_calloc_array(bmap_i->ctx, int, 2 * bmap_i->n_eq);
56 unsigned dim;
58 if (!eq)
59 return NULL;
61 dim = isl_basic_map_total_dim(bmap_i);
62 for (k = 0; k < bmap_i->n_eq; ++k) {
63 for (l = 0; l < 2; ++l) {
64 isl_seq_neg(bmap_i->eq[k], bmap_i->eq[k], 1+dim);
65 eq[2 * k + l] = status_in(bmap_i->eq[k], tab_j);
66 if (eq[2 * k + l] == STATUS_ERROR)
67 goto error;
69 if (eq[2 * k] == STATUS_SEPARATE ||
70 eq[2 * k + 1] == STATUS_SEPARATE)
71 break;
74 return eq;
75 error:
76 free(eq);
77 return NULL;
80 /* Compute the position of the inequalities of basic map "bmap_i"
81 * (also represented by "tab_i", if not NULL) with respect to the basic map
82 * represented by "tab_j".
84 static int *ineq_status_in(__isl_keep isl_basic_map *bmap_i,
85 struct isl_tab *tab_i, struct isl_tab *tab_j)
87 int k;
88 unsigned n_eq = bmap_i->n_eq;
89 int *ineq = isl_calloc_array(bmap_i->ctx, int, bmap_i->n_ineq);
91 if (!ineq)
92 return NULL;
94 for (k = 0; k < bmap_i->n_ineq; ++k) {
95 if (tab_i && isl_tab_is_redundant(tab_i, n_eq + k)) {
96 ineq[k] = STATUS_REDUNDANT;
97 continue;
99 ineq[k] = status_in(bmap_i->ineq[k], tab_j);
100 if (ineq[k] == STATUS_ERROR)
101 goto error;
102 if (ineq[k] == STATUS_SEPARATE)
103 break;
106 return ineq;
107 error:
108 free(ineq);
109 return NULL;
112 static int any(int *con, unsigned len, int status)
114 int i;
116 for (i = 0; i < len ; ++i)
117 if (con[i] == status)
118 return 1;
119 return 0;
122 static int count(int *con, unsigned len, int status)
124 int i;
125 int c = 0;
127 for (i = 0; i < len ; ++i)
128 if (con[i] == status)
129 c++;
130 return c;
133 static int all(int *con, unsigned len, int status)
135 int i;
137 for (i = 0; i < len ; ++i) {
138 if (con[i] == STATUS_REDUNDANT)
139 continue;
140 if (con[i] != status)
141 return 0;
143 return 1;
146 static void drop(struct isl_map *map, int i, struct isl_tab **tabs)
148 isl_basic_map_free(map->p[i]);
149 isl_tab_free(tabs[i]);
151 if (i != map->n - 1) {
152 map->p[i] = map->p[map->n - 1];
153 tabs[i] = tabs[map->n - 1];
155 tabs[map->n - 1] = NULL;
156 map->n--;
159 /* Replace the pair of basic maps i and j by the basic map bounded
160 * by the valid constraints in both basic maps and the constraints
161 * in extra (if not NULL).
163 static int fuse(struct isl_map *map, int i, int j,
164 struct isl_tab **tabs, int *eq_i, int *ineq_i, int *eq_j, int *ineq_j,
165 __isl_keep isl_mat *extra)
167 int k, l;
168 struct isl_basic_map *fused = NULL;
169 struct isl_tab *fused_tab = NULL;
170 unsigned total = isl_basic_map_total_dim(map->p[i]);
171 unsigned extra_rows = extra ? extra->n_row : 0;
173 fused = isl_basic_map_alloc_space(isl_space_copy(map->p[i]->dim),
174 map->p[i]->n_div,
175 map->p[i]->n_eq + map->p[j]->n_eq,
176 map->p[i]->n_ineq + map->p[j]->n_ineq + extra_rows);
177 if (!fused)
178 goto error;
180 for (k = 0; k < map->p[i]->n_eq; ++k) {
181 if (eq_i && (eq_i[2 * k] != STATUS_VALID ||
182 eq_i[2 * k + 1] != STATUS_VALID))
183 continue;
184 l = isl_basic_map_alloc_equality(fused);
185 if (l < 0)
186 goto error;
187 isl_seq_cpy(fused->eq[l], map->p[i]->eq[k], 1 + total);
190 for (k = 0; k < map->p[j]->n_eq; ++k) {
191 if (eq_j && (eq_j[2 * k] != STATUS_VALID ||
192 eq_j[2 * k + 1] != STATUS_VALID))
193 continue;
194 l = isl_basic_map_alloc_equality(fused);
195 if (l < 0)
196 goto error;
197 isl_seq_cpy(fused->eq[l], map->p[j]->eq[k], 1 + total);
200 for (k = 0; k < map->p[i]->n_ineq; ++k) {
201 if (ineq_i[k] != STATUS_VALID)
202 continue;
203 l = isl_basic_map_alloc_inequality(fused);
204 if (l < 0)
205 goto error;
206 isl_seq_cpy(fused->ineq[l], map->p[i]->ineq[k], 1 + total);
209 for (k = 0; k < map->p[j]->n_ineq; ++k) {
210 if (ineq_j[k] != STATUS_VALID)
211 continue;
212 l = isl_basic_map_alloc_inequality(fused);
213 if (l < 0)
214 goto error;
215 isl_seq_cpy(fused->ineq[l], map->p[j]->ineq[k], 1 + total);
218 for (k = 0; k < map->p[i]->n_div; ++k) {
219 int l = isl_basic_map_alloc_div(fused);
220 if (l < 0)
221 goto error;
222 isl_seq_cpy(fused->div[l], map->p[i]->div[k], 1 + 1 + total);
225 for (k = 0; k < extra_rows; ++k) {
226 l = isl_basic_map_alloc_inequality(fused);
227 if (l < 0)
228 goto error;
229 isl_seq_cpy(fused->ineq[l], extra->row[k], 1 + total);
232 fused = isl_basic_map_gauss(fused, NULL);
233 ISL_F_SET(fused, ISL_BASIC_MAP_FINAL);
234 if (ISL_F_ISSET(map->p[i], ISL_BASIC_MAP_RATIONAL) &&
235 ISL_F_ISSET(map->p[j], ISL_BASIC_MAP_RATIONAL))
236 ISL_F_SET(fused, ISL_BASIC_MAP_RATIONAL);
238 fused_tab = isl_tab_from_basic_map(fused, 0);
239 if (isl_tab_detect_redundant(fused_tab) < 0)
240 goto error;
242 isl_basic_map_free(map->p[i]);
243 map->p[i] = fused;
244 isl_tab_free(tabs[i]);
245 tabs[i] = fused_tab;
246 drop(map, j, tabs);
248 return 1;
249 error:
250 isl_tab_free(fused_tab);
251 isl_basic_map_free(fused);
252 return -1;
255 /* Given a pair of basic maps i and j such that all constraints are either
256 * "valid" or "cut", check if the facets corresponding to the "cut"
257 * constraints of i lie entirely within basic map j.
258 * If so, replace the pair by the basic map consisting of the valid
259 * constraints in both basic maps.
260 * Checking whether the facet lies entirely within basic map j
261 * is performed by checking whether the constraints of basic map j
262 * are valid for the facet. These tests are performed on a rational
263 * tableau to avoid the theoretical possibility that a constraint
264 * that was considered to be a cut constraint for the entire basic map i
265 * happens to be considered to be a valid constraint for the facet,
266 * even though it cuts off the same rational points.
268 * To see that we are not introducing any extra points, call the
269 * two basic maps A and B and the resulting map U and let x
270 * be an element of U \setminus ( A \cup B ).
271 * A line connecting x with an element of A \cup B meets a facet F
272 * of either A or B. Assume it is a facet of B and let c_1 be
273 * the corresponding facet constraint. We have c_1(x) < 0 and
274 * so c_1 is a cut constraint. This implies that there is some
275 * (possibly rational) point x' satisfying the constraints of A
276 * and the opposite of c_1 as otherwise c_1 would have been marked
277 * valid for A. The line connecting x and x' meets a facet of A
278 * in a (possibly rational) point that also violates c_1, but this
279 * is impossible since all cut constraints of B are valid for all
280 * cut facets of A.
281 * In case F is a facet of A rather than B, then we can apply the
282 * above reasoning to find a facet of B separating x from A \cup B first.
284 static int check_facets(struct isl_map *map, int i, int j,
285 struct isl_tab **tabs, int *ineq_i, int *ineq_j)
287 int k, l;
288 struct isl_tab_undo *snap, *snap2;
289 unsigned n_eq = map->p[i]->n_eq;
291 snap = isl_tab_snap(tabs[i]);
292 if (isl_tab_mark_rational(tabs[i]) < 0)
293 return -1;
294 snap2 = isl_tab_snap(tabs[i]);
296 for (k = 0; k < map->p[i]->n_ineq; ++k) {
297 if (ineq_i[k] != STATUS_CUT)
298 continue;
299 if (isl_tab_select_facet(tabs[i], n_eq + k) < 0)
300 return -1;
301 for (l = 0; l < map->p[j]->n_ineq; ++l) {
302 int stat;
303 if (ineq_j[l] != STATUS_CUT)
304 continue;
305 stat = status_in(map->p[j]->ineq[l], tabs[i]);
306 if (stat != STATUS_VALID)
307 break;
309 if (isl_tab_rollback(tabs[i], snap2) < 0)
310 return -1;
311 if (l < map->p[j]->n_ineq)
312 break;
315 if (k < map->p[i]->n_ineq) {
316 if (isl_tab_rollback(tabs[i], snap) < 0)
317 return -1;
318 return 0;
320 return fuse(map, i, j, tabs, NULL, ineq_i, NULL, ineq_j, NULL);
323 /* Check if basic map "i" contains the basic map represented
324 * by the tableau "tab".
326 static int contains(struct isl_map *map, int i, int *ineq_i,
327 struct isl_tab *tab)
329 int k, l;
330 unsigned dim;
332 dim = isl_basic_map_total_dim(map->p[i]);
333 for (k = 0; k < map->p[i]->n_eq; ++k) {
334 for (l = 0; l < 2; ++l) {
335 int stat;
336 isl_seq_neg(map->p[i]->eq[k], map->p[i]->eq[k], 1+dim);
337 stat = status_in(map->p[i]->eq[k], tab);
338 if (stat != STATUS_VALID)
339 return 0;
343 for (k = 0; k < map->p[i]->n_ineq; ++k) {
344 int stat;
345 if (ineq_i[k] == STATUS_REDUNDANT)
346 continue;
347 stat = status_in(map->p[i]->ineq[k], tab);
348 if (stat != STATUS_VALID)
349 return 0;
351 return 1;
354 /* Basic map "i" has an inequality (say "k") that is adjacent
355 * to some inequality of basic map "j". All the other inequalities
356 * are valid for "j".
357 * Check if basic map "j" forms an extension of basic map "i".
359 * Note that this function is only called if some of the equalities or
360 * inequalities of basic map "j" do cut basic map "i". The function is
361 * correct even if there are no such cut constraints, but in that case
362 * the additional checks performed by this function are overkill.
364 * In particular, we replace constraint k, say f >= 0, by constraint
365 * f <= -1, add the inequalities of "j" that are valid for "i"
366 * and check if the result is a subset of basic map "j".
367 * If so, then we know that this result is exactly equal to basic map "j"
368 * since all its constraints are valid for basic map "j".
369 * By combining the valid constraints of "i" (all equalities and all
370 * inequalities except "k") and the valid constraints of "j" we therefore
371 * obtain a basic map that is equal to their union.
372 * In this case, there is no need to perform a rollback of the tableau
373 * since it is going to be destroyed in fuse().
376 * |\__ |\__
377 * | \__ | \__
378 * | \_ => | \__
379 * |_______| _ |_________\
382 * |\ |\
383 * | \ | \
384 * | \ | \
385 * | | | \
386 * | ||\ => | \
387 * | || \ | \
388 * | || | | |
389 * |__||_/ |_____/
391 static int is_adj_ineq_extension(__isl_keep isl_map *map, int i, int j,
392 struct isl_tab **tabs, int *eq_i, int *ineq_i, int *eq_j, int *ineq_j)
394 int k;
395 struct isl_tab_undo *snap;
396 unsigned n_eq = map->p[i]->n_eq;
397 unsigned total = isl_basic_map_total_dim(map->p[i]);
398 int r;
400 if (isl_tab_extend_cons(tabs[i], 1 + map->p[j]->n_ineq) < 0)
401 return -1;
403 for (k = 0; k < map->p[i]->n_ineq; ++k)
404 if (ineq_i[k] == STATUS_ADJ_INEQ)
405 break;
406 if (k >= map->p[i]->n_ineq)
407 isl_die(isl_map_get_ctx(map), isl_error_internal,
408 "ineq_i should have exactly one STATUS_ADJ_INEQ",
409 return -1);
411 snap = isl_tab_snap(tabs[i]);
413 if (isl_tab_unrestrict(tabs[i], n_eq + k) < 0)
414 return -1;
416 isl_seq_neg(map->p[i]->ineq[k], map->p[i]->ineq[k], 1 + total);
417 isl_int_sub_ui(map->p[i]->ineq[k][0], map->p[i]->ineq[k][0], 1);
418 r = isl_tab_add_ineq(tabs[i], map->p[i]->ineq[k]);
419 isl_seq_neg(map->p[i]->ineq[k], map->p[i]->ineq[k], 1 + total);
420 isl_int_sub_ui(map->p[i]->ineq[k][0], map->p[i]->ineq[k][0], 1);
421 if (r < 0)
422 return -1;
424 for (k = 0; k < map->p[j]->n_ineq; ++k) {
425 if (ineq_j[k] != STATUS_VALID)
426 continue;
427 if (isl_tab_add_ineq(tabs[i], map->p[j]->ineq[k]) < 0)
428 return -1;
431 if (contains(map, j, ineq_j, tabs[i]))
432 return fuse(map, i, j, tabs, eq_i, ineq_i, eq_j, ineq_j, NULL);
434 if (isl_tab_rollback(tabs[i], snap) < 0)
435 return -1;
437 return 0;
441 /* Both basic maps have at least one inequality with and adjacent
442 * (but opposite) inequality in the other basic map.
443 * Check that there are no cut constraints and that there is only
444 * a single pair of adjacent inequalities.
445 * If so, we can replace the pair by a single basic map described
446 * by all but the pair of adjacent inequalities.
447 * Any additional points introduced lie strictly between the two
448 * adjacent hyperplanes and can therefore be integral.
450 * ____ _____
451 * / ||\ / \
452 * / || \ / \
453 * \ || \ => \ \
454 * \ || / \ /
455 * \___||_/ \_____/
457 * The test for a single pair of adjancent inequalities is important
458 * for avoiding the combination of two basic maps like the following
460 * /|
461 * / |
462 * /__|
463 * _____
464 * | |
465 * | |
466 * |___|
468 * If there are some cut constraints on one side, then we may
469 * still be able to fuse the two basic maps, but we need to perform
470 * some additional checks in is_adj_ineq_extension.
472 static int check_adj_ineq(struct isl_map *map, int i, int j,
473 struct isl_tab **tabs, int *eq_i, int *ineq_i, int *eq_j, int *ineq_j)
475 int count_i, count_j;
476 int cut_i, cut_j;
478 count_i = count(ineq_i, map->p[i]->n_ineq, STATUS_ADJ_INEQ);
479 count_j = count(ineq_j, map->p[j]->n_ineq, STATUS_ADJ_INEQ);
481 if (count_i != 1 && count_j != 1)
482 return 0;
484 cut_i = any(eq_i, 2 * map->p[i]->n_eq, STATUS_CUT) ||
485 any(ineq_i, map->p[i]->n_ineq, STATUS_CUT);
486 cut_j = any(eq_j, 2 * map->p[j]->n_eq, STATUS_CUT) ||
487 any(ineq_j, map->p[j]->n_ineq, STATUS_CUT);
489 if (!cut_i && !cut_j && count_i == 1 && count_j == 1)
490 return fuse(map, i, j, tabs, NULL, ineq_i, NULL, ineq_j, NULL);
492 if (count_i == 1 && !cut_i)
493 return is_adj_ineq_extension(map, i, j, tabs,
494 eq_i, ineq_i, eq_j, ineq_j);
496 if (count_j == 1 && !cut_j)
497 return is_adj_ineq_extension(map, j, i, tabs,
498 eq_j, ineq_j, eq_i, ineq_i);
500 return 0;
503 /* Basic map "i" has an inequality "k" that is adjacent to some equality
504 * of basic map "j". All the other inequalities are valid for "j".
505 * Check if basic map "j" forms an extension of basic map "i".
507 * In particular, we relax constraint "k", compute the corresponding
508 * facet and check whether it is included in the other basic map.
509 * If so, we know that relaxing the constraint extends the basic
510 * map with exactly the other basic map (we already know that this
511 * other basic map is included in the extension, because there
512 * were no "cut" inequalities in "i") and we can replace the
513 * two basic maps by this extension.
514 * ____ _____
515 * / || / |
516 * / || / |
517 * \ || => \ |
518 * \ || \ |
519 * \___|| \____|
521 static int is_adj_eq_extension(struct isl_map *map, int i, int j, int k,
522 struct isl_tab **tabs, int *eq_i, int *ineq_i, int *eq_j, int *ineq_j)
524 int changed = 0;
525 int super;
526 struct isl_tab_undo *snap, *snap2;
527 unsigned n_eq = map->p[i]->n_eq;
529 if (isl_tab_is_equality(tabs[i], n_eq + k))
530 return 0;
532 snap = isl_tab_snap(tabs[i]);
533 if (isl_tab_relax(tabs[i], n_eq + k) < 0)
534 return -1;
535 snap2 = isl_tab_snap(tabs[i]);
536 if (isl_tab_select_facet(tabs[i], n_eq + k) < 0)
537 return -1;
538 super = contains(map, j, ineq_j, tabs[i]);
539 if (super) {
540 if (isl_tab_rollback(tabs[i], snap2) < 0)
541 return -1;
542 map->p[i] = isl_basic_map_cow(map->p[i]);
543 if (!map->p[i])
544 return -1;
545 isl_int_add_ui(map->p[i]->ineq[k][0], map->p[i]->ineq[k][0], 1);
546 ISL_F_SET(map->p[i], ISL_BASIC_MAP_FINAL);
547 drop(map, j, tabs);
548 changed = 1;
549 } else
550 if (isl_tab_rollback(tabs[i], snap) < 0)
551 return -1;
553 return changed;
556 /* Data structure that keeps track of the wrapping constraints
557 * and of information to bound the coefficients of those constraints.
559 * bound is set if we want to apply a bound on the coefficients
560 * mat contains the wrapping constraints
561 * max is the bound on the coefficients (if bound is set)
563 struct isl_wraps {
564 int bound;
565 isl_mat *mat;
566 isl_int max;
569 /* Update wraps->max to be greater than or equal to the coefficients
570 * in the equalities and inequalities of bmap that can be removed if we end up
571 * applying wrapping.
573 static void wraps_update_max(struct isl_wraps *wraps,
574 __isl_keep isl_basic_map *bmap, int *eq, int *ineq)
576 int k;
577 isl_int max_k;
578 unsigned total = isl_basic_map_total_dim(bmap);
580 isl_int_init(max_k);
582 for (k = 0; k < bmap->n_eq; ++k) {
583 if (eq[2 * k] == STATUS_VALID &&
584 eq[2 * k + 1] == STATUS_VALID)
585 continue;
586 isl_seq_abs_max(bmap->eq[k] + 1, total, &max_k);
587 if (isl_int_abs_gt(max_k, wraps->max))
588 isl_int_set(wraps->max, max_k);
591 for (k = 0; k < bmap->n_ineq; ++k) {
592 if (ineq[k] == STATUS_VALID || ineq[k] == STATUS_REDUNDANT)
593 continue;
594 isl_seq_abs_max(bmap->ineq[k] + 1, total, &max_k);
595 if (isl_int_abs_gt(max_k, wraps->max))
596 isl_int_set(wraps->max, max_k);
599 isl_int_clear(max_k);
602 /* Initialize the isl_wraps data structure.
603 * If we want to bound the coefficients of the wrapping constraints,
604 * we set wraps->max to the largest coefficient
605 * in the equalities and inequalities that can be removed if we end up
606 * applying wrapping.
608 static void wraps_init(struct isl_wraps *wraps, __isl_take isl_mat *mat,
609 __isl_keep isl_map *map, int i, int j,
610 int *eq_i, int *ineq_i, int *eq_j, int *ineq_j)
612 isl_ctx *ctx;
614 wraps->bound = 0;
615 wraps->mat = mat;
616 if (!mat)
617 return;
618 ctx = isl_mat_get_ctx(mat);
619 wraps->bound = isl_options_get_coalesce_bounded_wrapping(ctx);
620 if (!wraps->bound)
621 return;
622 isl_int_init(wraps->max);
623 isl_int_set_si(wraps->max, 0);
624 wraps_update_max(wraps, map->p[i], eq_i, ineq_i);
625 wraps_update_max(wraps, map->p[j], eq_j, ineq_j);
628 /* Free the contents of the isl_wraps data structure.
630 static void wraps_free(struct isl_wraps *wraps)
632 isl_mat_free(wraps->mat);
633 if (wraps->bound)
634 isl_int_clear(wraps->max);
637 /* Is the wrapping constraint in row "row" allowed?
639 * If wraps->bound is set, we check that none of the coefficients
640 * is greater than wraps->max.
642 static int allow_wrap(struct isl_wraps *wraps, int row)
644 int i;
646 if (!wraps->bound)
647 return 1;
649 for (i = 1; i < wraps->mat->n_col; ++i)
650 if (isl_int_abs_gt(wraps->mat->row[row][i], wraps->max))
651 return 0;
653 return 1;
656 /* For each non-redundant constraint in "bmap" (as determined by "tab"),
657 * wrap the constraint around "bound" such that it includes the whole
658 * set "set" and append the resulting constraint to "wraps".
659 * "wraps" is assumed to have been pre-allocated to the appropriate size.
660 * wraps->n_row is the number of actual wrapped constraints that have
661 * been added.
662 * If any of the wrapping problems results in a constraint that is
663 * identical to "bound", then this means that "set" is unbounded in such
664 * way that no wrapping is possible. If this happens then wraps->n_row
665 * is reset to zero.
666 * Similarly, if we want to bound the coefficients of the wrapping
667 * constraints and a newly added wrapping constraint does not
668 * satisfy the bound, then wraps->n_row is also reset to zero.
670 static int add_wraps(struct isl_wraps *wraps, __isl_keep isl_basic_map *bmap,
671 struct isl_tab *tab, isl_int *bound, __isl_keep isl_set *set)
673 int l;
674 int w;
675 unsigned total = isl_basic_map_total_dim(bmap);
677 w = wraps->mat->n_row;
679 for (l = 0; l < bmap->n_ineq; ++l) {
680 if (isl_seq_is_neg(bound, bmap->ineq[l], 1 + total))
681 continue;
682 if (isl_seq_eq(bound, bmap->ineq[l], 1 + total))
683 continue;
684 if (isl_tab_is_redundant(tab, bmap->n_eq + l))
685 continue;
687 isl_seq_cpy(wraps->mat->row[w], bound, 1 + total);
688 if (!isl_set_wrap_facet(set, wraps->mat->row[w], bmap->ineq[l]))
689 return -1;
690 if (isl_seq_eq(wraps->mat->row[w], bound, 1 + total))
691 goto unbounded;
692 if (!allow_wrap(wraps, w))
693 goto unbounded;
694 ++w;
696 for (l = 0; l < bmap->n_eq; ++l) {
697 if (isl_seq_is_neg(bound, bmap->eq[l], 1 + total))
698 continue;
699 if (isl_seq_eq(bound, bmap->eq[l], 1 + total))
700 continue;
702 isl_seq_cpy(wraps->mat->row[w], bound, 1 + total);
703 isl_seq_neg(wraps->mat->row[w + 1], bmap->eq[l], 1 + total);
704 if (!isl_set_wrap_facet(set, wraps->mat->row[w],
705 wraps->mat->row[w + 1]))
706 return -1;
707 if (isl_seq_eq(wraps->mat->row[w], bound, 1 + total))
708 goto unbounded;
709 if (!allow_wrap(wraps, w))
710 goto unbounded;
711 ++w;
713 isl_seq_cpy(wraps->mat->row[w], bound, 1 + total);
714 if (!isl_set_wrap_facet(set, wraps->mat->row[w], bmap->eq[l]))
715 return -1;
716 if (isl_seq_eq(wraps->mat->row[w], bound, 1 + total))
717 goto unbounded;
718 if (!allow_wrap(wraps, w))
719 goto unbounded;
720 ++w;
723 wraps->mat->n_row = w;
724 return 0;
725 unbounded:
726 wraps->mat->n_row = 0;
727 return 0;
730 /* Check if the constraints in "wraps" from "first" until the last
731 * are all valid for the basic set represented by "tab".
732 * If not, wraps->n_row is set to zero.
734 static int check_wraps(__isl_keep isl_mat *wraps, int first,
735 struct isl_tab *tab)
737 int i;
739 for (i = first; i < wraps->n_row; ++i) {
740 enum isl_ineq_type type;
741 type = isl_tab_ineq_type(tab, wraps->row[i]);
742 if (type == isl_ineq_error)
743 return -1;
744 if (type == isl_ineq_redundant)
745 continue;
746 wraps->n_row = 0;
747 return 0;
750 return 0;
753 /* Return a set that corresponds to the non-redundant constraints
754 * (as recorded in tab) of bmap.
756 * It's important to remove the redundant constraints as some
757 * of the other constraints may have been modified after the
758 * constraints were marked redundant.
759 * In particular, a constraint may have been relaxed.
760 * Redundant constraints are ignored when a constraint is relaxed
761 * and should therefore continue to be ignored ever after.
762 * Otherwise, the relaxation might be thwarted by some of
763 * these constraints.
765 * Update the underlying set to ensure that the dimension doesn't change.
766 * Otherwise the integer divisions could get dropped if the tab
767 * turns out to be empty.
769 static __isl_give isl_set *set_from_updated_bmap(__isl_keep isl_basic_map *bmap,
770 struct isl_tab *tab)
772 isl_basic_set *bset;
774 bmap = isl_basic_map_copy(bmap);
775 bset = isl_basic_map_underlying_set(bmap);
776 bset = isl_basic_set_cow(bset);
777 bset = isl_basic_set_update_from_tab(bset, tab);
778 return isl_set_from_basic_set(bset);
781 /* Given a basic set i with a constraint k that is adjacent to
782 * basic set j, check if we can wrap
783 * both the facet corresponding to k and basic map j
784 * around their ridges to include the other set.
785 * If so, replace the pair of basic sets by their union.
787 * All constraints of i (except k) are assumed to be valid for j.
788 * This means that there is no real need to wrap the ridges of
789 * the faces of basic map i around basic map j but since we do,
790 * we have to check that the resulting wrapping constraints are valid for i.
791 * ____ _____
792 * / | / \
793 * / || / |
794 * \ || => \ |
795 * \ || \ |
796 * \___|| \____|
799 static int can_wrap_in_facet(struct isl_map *map, int i, int j, int k,
800 struct isl_tab **tabs, int *eq_i, int *ineq_i, int *eq_j, int *ineq_j)
802 int changed = 0;
803 struct isl_wraps wraps;
804 isl_mat *mat;
805 struct isl_set *set_i = NULL;
806 struct isl_set *set_j = NULL;
807 struct isl_vec *bound = NULL;
808 unsigned total = isl_basic_map_total_dim(map->p[i]);
809 struct isl_tab_undo *snap;
810 int n;
812 set_i = set_from_updated_bmap(map->p[i], tabs[i]);
813 set_j = set_from_updated_bmap(map->p[j], tabs[j]);
814 mat = isl_mat_alloc(map->ctx, 2 * (map->p[i]->n_eq + map->p[j]->n_eq) +
815 map->p[i]->n_ineq + map->p[j]->n_ineq,
816 1 + total);
817 wraps_init(&wraps, mat, map, i, j, eq_i, ineq_i, eq_j, ineq_j);
818 bound = isl_vec_alloc(map->ctx, 1 + total);
819 if (!set_i || !set_j || !wraps.mat || !bound)
820 goto error;
822 isl_seq_cpy(bound->el, map->p[i]->ineq[k], 1 + total);
823 isl_int_add_ui(bound->el[0], bound->el[0], 1);
825 isl_seq_cpy(wraps.mat->row[0], bound->el, 1 + total);
826 wraps.mat->n_row = 1;
828 if (add_wraps(&wraps, map->p[j], tabs[j], bound->el, set_i) < 0)
829 goto error;
830 if (!wraps.mat->n_row)
831 goto unbounded;
833 snap = isl_tab_snap(tabs[i]);
835 if (isl_tab_select_facet(tabs[i], map->p[i]->n_eq + k) < 0)
836 goto error;
837 if (isl_tab_detect_redundant(tabs[i]) < 0)
838 goto error;
840 isl_seq_neg(bound->el, map->p[i]->ineq[k], 1 + total);
842 n = wraps.mat->n_row;
843 if (add_wraps(&wraps, map->p[i], tabs[i], bound->el, set_j) < 0)
844 goto error;
846 if (isl_tab_rollback(tabs[i], snap) < 0)
847 goto error;
848 if (check_wraps(wraps.mat, n, tabs[i]) < 0)
849 goto error;
850 if (!wraps.mat->n_row)
851 goto unbounded;
853 changed = fuse(map, i, j, tabs, eq_i, ineq_i, eq_j, ineq_j, wraps.mat);
855 unbounded:
856 wraps_free(&wraps);
858 isl_set_free(set_i);
859 isl_set_free(set_j);
861 isl_vec_free(bound);
863 return changed;
864 error:
865 wraps_free(&wraps);
866 isl_vec_free(bound);
867 isl_set_free(set_i);
868 isl_set_free(set_j);
869 return -1;
872 /* Set the is_redundant property of the "n" constraints in "cuts",
873 * except "k" to "v".
874 * This is a fairly tricky operation as it bypasses isl_tab.c.
875 * The reason we want to temporarily mark some constraints redundant
876 * is that we want to ignore them in add_wraps.
878 * Initially all cut constraints are non-redundant, but the
879 * selection of a facet right before the call to this function
880 * may have made some of them redundant.
881 * Likewise, the same constraints are marked non-redundant
882 * in the second call to this function, before they are officially
883 * made non-redundant again in the subsequent rollback.
885 static void set_is_redundant(struct isl_tab *tab, unsigned n_eq,
886 int *cuts, int n, int k, int v)
888 int l;
890 for (l = 0; l < n; ++l) {
891 if (l == k)
892 continue;
893 tab->con[n_eq + cuts[l]].is_redundant = v;
897 /* Given a pair of basic maps i and j such that j sticks out
898 * of i at n cut constraints, each time by at most one,
899 * try to compute wrapping constraints and replace the two
900 * basic maps by a single basic map.
901 * The other constraints of i are assumed to be valid for j.
903 * The facets of i corresponding to the cut constraints are
904 * wrapped around their ridges, except those ridges determined
905 * by any of the other cut constraints.
906 * The intersections of cut constraints need to be ignored
907 * as the result of wrapping one cut constraint around another
908 * would result in a constraint cutting the union.
909 * In each case, the facets are wrapped to include the union
910 * of the two basic maps.
912 * The pieces of j that lie at an offset of exactly one from
913 * one of the cut constraints of i are wrapped around their edges.
914 * Here, there is no need to ignore intersections because we
915 * are wrapping around the union of the two basic maps.
917 * If any wrapping fails, i.e., if we cannot wrap to touch
918 * the union, then we give up.
919 * Otherwise, the pair of basic maps is replaced by their union.
921 static int wrap_in_facets(struct isl_map *map, int i, int j,
922 int *cuts, int n, struct isl_tab **tabs,
923 int *eq_i, int *ineq_i, int *eq_j, int *ineq_j)
925 int changed = 0;
926 struct isl_wraps wraps;
927 isl_mat *mat;
928 isl_set *set = NULL;
929 isl_vec *bound = NULL;
930 unsigned total = isl_basic_map_total_dim(map->p[i]);
931 int max_wrap;
932 int k;
933 struct isl_tab_undo *snap_i, *snap_j;
935 if (isl_tab_extend_cons(tabs[j], 1) < 0)
936 goto error;
938 max_wrap = 2 * (map->p[i]->n_eq + map->p[j]->n_eq) +
939 map->p[i]->n_ineq + map->p[j]->n_ineq;
940 max_wrap *= n;
942 set = isl_set_union(set_from_updated_bmap(map->p[i], tabs[i]),
943 set_from_updated_bmap(map->p[j], tabs[j]));
944 mat = isl_mat_alloc(map->ctx, max_wrap, 1 + total);
945 wraps_init(&wraps, mat, map, i, j, eq_i, ineq_i, eq_j, ineq_j);
946 bound = isl_vec_alloc(map->ctx, 1 + total);
947 if (!set || !wraps.mat || !bound)
948 goto error;
950 snap_i = isl_tab_snap(tabs[i]);
951 snap_j = isl_tab_snap(tabs[j]);
953 wraps.mat->n_row = 0;
955 for (k = 0; k < n; ++k) {
956 if (isl_tab_select_facet(tabs[i], map->p[i]->n_eq + cuts[k]) < 0)
957 goto error;
958 if (isl_tab_detect_redundant(tabs[i]) < 0)
959 goto error;
960 set_is_redundant(tabs[i], map->p[i]->n_eq, cuts, n, k, 1);
962 isl_seq_neg(bound->el, map->p[i]->ineq[cuts[k]], 1 + total);
963 if (!tabs[i]->empty &&
964 add_wraps(&wraps, map->p[i], tabs[i], bound->el, set) < 0)
965 goto error;
967 set_is_redundant(tabs[i], map->p[i]->n_eq, cuts, n, k, 0);
968 if (isl_tab_rollback(tabs[i], snap_i) < 0)
969 goto error;
971 if (tabs[i]->empty)
972 break;
973 if (!wraps.mat->n_row)
974 break;
976 isl_seq_cpy(bound->el, map->p[i]->ineq[cuts[k]], 1 + total);
977 isl_int_add_ui(bound->el[0], bound->el[0], 1);
978 if (isl_tab_add_eq(tabs[j], bound->el) < 0)
979 goto error;
980 if (isl_tab_detect_redundant(tabs[j]) < 0)
981 goto error;
983 if (!tabs[j]->empty &&
984 add_wraps(&wraps, map->p[j], tabs[j], bound->el, set) < 0)
985 goto error;
987 if (isl_tab_rollback(tabs[j], snap_j) < 0)
988 goto error;
990 if (!wraps.mat->n_row)
991 break;
994 if (k == n)
995 changed = fuse(map, i, j, tabs,
996 eq_i, ineq_i, eq_j, ineq_j, wraps.mat);
998 isl_vec_free(bound);
999 wraps_free(&wraps);
1000 isl_set_free(set);
1002 return changed;
1003 error:
1004 isl_vec_free(bound);
1005 wraps_free(&wraps);
1006 isl_set_free(set);
1007 return -1;
1010 /* Given two basic sets i and j such that i has no cut equalities,
1011 * check if relaxing all the cut inequalities of i by one turns
1012 * them into valid constraint for j and check if we can wrap in
1013 * the bits that are sticking out.
1014 * If so, replace the pair by their union.
1016 * We first check if all relaxed cut inequalities of i are valid for j
1017 * and then try to wrap in the intersections of the relaxed cut inequalities
1018 * with j.
1020 * During this wrapping, we consider the points of j that lie at a distance
1021 * of exactly 1 from i. In particular, we ignore the points that lie in
1022 * between this lower-dimensional space and the basic map i.
1023 * We can therefore only apply this to integer maps.
1024 * ____ _____
1025 * / ___|_ / \
1026 * / | | / |
1027 * \ | | => \ |
1028 * \|____| \ |
1029 * \___| \____/
1031 * _____ ______
1032 * | ____|_ | \
1033 * | | | | |
1034 * | | | => | |
1035 * |_| | | |
1036 * |_____| \______|
1038 * _______
1039 * | |
1040 * | |\ |
1041 * | | \ |
1042 * | | \ |
1043 * | | \|
1044 * | | \
1045 * | |_____\
1046 * | |
1047 * |_______|
1049 * Wrapping can fail if the result of wrapping one of the facets
1050 * around its edges does not produce any new facet constraint.
1051 * In particular, this happens when we try to wrap in unbounded sets.
1053 * _______________________________________________________________________
1055 * | ___
1056 * | | |
1057 * |_| |_________________________________________________________________
1058 * |___|
1060 * The following is not an acceptable result of coalescing the above two
1061 * sets as it includes extra integer points.
1062 * _______________________________________________________________________
1064 * |
1065 * |
1067 * \______________________________________________________________________
1069 static int can_wrap_in_set(struct isl_map *map, int i, int j,
1070 struct isl_tab **tabs, int *eq_i, int *ineq_i, int *eq_j, int *ineq_j)
1072 int changed = 0;
1073 int k, m;
1074 int n;
1075 int *cuts = NULL;
1077 if (ISL_F_ISSET(map->p[i], ISL_BASIC_MAP_RATIONAL) ||
1078 ISL_F_ISSET(map->p[j], ISL_BASIC_MAP_RATIONAL))
1079 return 0;
1081 n = count(ineq_i, map->p[i]->n_ineq, STATUS_CUT);
1082 if (n == 0)
1083 return 0;
1085 cuts = isl_alloc_array(map->ctx, int, n);
1086 if (!cuts)
1087 return -1;
1089 for (k = 0, m = 0; m < n; ++k) {
1090 enum isl_ineq_type type;
1092 if (ineq_i[k] != STATUS_CUT)
1093 continue;
1095 isl_int_add_ui(map->p[i]->ineq[k][0], map->p[i]->ineq[k][0], 1);
1096 type = isl_tab_ineq_type(tabs[j], map->p[i]->ineq[k]);
1097 isl_int_sub_ui(map->p[i]->ineq[k][0], map->p[i]->ineq[k][0], 1);
1098 if (type == isl_ineq_error)
1099 goto error;
1100 if (type != isl_ineq_redundant)
1101 break;
1102 cuts[m] = k;
1103 ++m;
1106 if (m == n)
1107 changed = wrap_in_facets(map, i, j, cuts, n, tabs,
1108 eq_i, ineq_i, eq_j, ineq_j);
1110 free(cuts);
1112 return changed;
1113 error:
1114 free(cuts);
1115 return -1;
1118 /* Check if either i or j has only cut inequalities that can
1119 * be used to wrap in (a facet of) the other basic set.
1120 * if so, replace the pair by their union.
1122 static int check_wrap(struct isl_map *map, int i, int j,
1123 struct isl_tab **tabs, int *eq_i, int *ineq_i, int *eq_j, int *ineq_j)
1125 int changed = 0;
1127 if (!any(eq_i, 2 * map->p[i]->n_eq, STATUS_CUT))
1128 changed = can_wrap_in_set(map, i, j, tabs,
1129 eq_i, ineq_i, eq_j, ineq_j);
1130 if (changed)
1131 return changed;
1133 if (!any(eq_j, 2 * map->p[j]->n_eq, STATUS_CUT))
1134 changed = can_wrap_in_set(map, j, i, tabs,
1135 eq_j, ineq_j, eq_i, ineq_i);
1136 return changed;
1139 /* At least one of the basic maps has an equality that is adjacent
1140 * to inequality. Make sure that only one of the basic maps has
1141 * such an equality and that the other basic map has exactly one
1142 * inequality adjacent to an equality.
1143 * We call the basic map that has the inequality "i" and the basic
1144 * map that has the equality "j".
1145 * If "i" has any "cut" (in)equality, then relaxing the inequality
1146 * by one would not result in a basic map that contains the other
1147 * basic map.
1149 static int check_adj_eq(struct isl_map *map, int i, int j,
1150 struct isl_tab **tabs, int *eq_i, int *ineq_i, int *eq_j, int *ineq_j)
1152 int changed = 0;
1153 int k;
1155 if (any(eq_i, 2 * map->p[i]->n_eq, STATUS_ADJ_INEQ) &&
1156 any(eq_j, 2 * map->p[j]->n_eq, STATUS_ADJ_INEQ))
1157 /* ADJ EQ TOO MANY */
1158 return 0;
1160 if (any(eq_i, 2 * map->p[i]->n_eq, STATUS_ADJ_INEQ))
1161 return check_adj_eq(map, j, i, tabs,
1162 eq_j, ineq_j, eq_i, ineq_i);
1164 /* j has an equality adjacent to an inequality in i */
1166 if (any(eq_i, 2 * map->p[i]->n_eq, STATUS_CUT))
1167 return 0;
1168 if (any(ineq_i, map->p[i]->n_ineq, STATUS_CUT))
1169 /* ADJ EQ CUT */
1170 return 0;
1171 if (count(ineq_i, map->p[i]->n_ineq, STATUS_ADJ_EQ) != 1 ||
1172 any(ineq_j, map->p[j]->n_ineq, STATUS_ADJ_EQ) ||
1173 any(ineq_i, map->p[i]->n_ineq, STATUS_ADJ_INEQ) ||
1174 any(ineq_j, map->p[j]->n_ineq, STATUS_ADJ_INEQ))
1175 /* ADJ EQ TOO MANY */
1176 return 0;
1178 for (k = 0; k < map->p[i]->n_ineq; ++k)
1179 if (ineq_i[k] == STATUS_ADJ_EQ)
1180 break;
1182 changed = is_adj_eq_extension(map, i, j, k, tabs,
1183 eq_i, ineq_i, eq_j, ineq_j);
1184 if (changed)
1185 return changed;
1187 if (count(eq_j, 2 * map->p[j]->n_eq, STATUS_ADJ_INEQ) != 1)
1188 return 0;
1190 changed = can_wrap_in_facet(map, i, j, k, tabs, eq_i, ineq_i, eq_j, ineq_j);
1192 return changed;
1195 /* The two basic maps lie on adjacent hyperplanes. In particular,
1196 * basic map "i" has an equality that lies parallel to basic map "j".
1197 * Check if we can wrap the facets around the parallel hyperplanes
1198 * to include the other set.
1200 * We perform basically the same operations as can_wrap_in_facet,
1201 * except that we don't need to select a facet of one of the sets.
1203 * \\ \\
1204 * \\ => \\
1205 * \ \|
1207 * We only allow one equality of "i" to be adjacent to an equality of "j"
1208 * to avoid coalescing
1210 * [m, n] -> { [x, y] -> [x, 1 + y] : x >= 1 and y >= 1 and
1211 * x <= 10 and y <= 10;
1212 * [x, y] -> [1 + x, y] : x >= 1 and x <= 20 and
1213 * y >= 5 and y <= 15 }
1215 * to
1217 * [m, n] -> { [x, y] -> [x2, y2] : x >= 1 and 10y2 <= 20 - x + 10y and
1218 * 4y2 >= 5 + 3y and 5y2 <= 15 + 4y and
1219 * y2 <= 1 + x + y - x2 and y2 >= y and
1220 * y2 >= 1 + x + y - x2 }
1222 static int check_eq_adj_eq(struct isl_map *map, int i, int j,
1223 struct isl_tab **tabs, int *eq_i, int *ineq_i, int *eq_j, int *ineq_j)
1225 int k;
1226 int changed = 0;
1227 struct isl_wraps wraps;
1228 isl_mat *mat;
1229 struct isl_set *set_i = NULL;
1230 struct isl_set *set_j = NULL;
1231 struct isl_vec *bound = NULL;
1232 unsigned total = isl_basic_map_total_dim(map->p[i]);
1234 if (count(eq_i, 2 * map->p[i]->n_eq, STATUS_ADJ_EQ) != 1)
1235 return 0;
1237 for (k = 0; k < 2 * map->p[i]->n_eq ; ++k)
1238 if (eq_i[k] == STATUS_ADJ_EQ)
1239 break;
1241 set_i = set_from_updated_bmap(map->p[i], tabs[i]);
1242 set_j = set_from_updated_bmap(map->p[j], tabs[j]);
1243 mat = isl_mat_alloc(map->ctx, 2 * (map->p[i]->n_eq + map->p[j]->n_eq) +
1244 map->p[i]->n_ineq + map->p[j]->n_ineq,
1245 1 + total);
1246 wraps_init(&wraps, mat, map, i, j, eq_i, ineq_i, eq_j, ineq_j);
1247 bound = isl_vec_alloc(map->ctx, 1 + total);
1248 if (!set_i || !set_j || !wraps.mat || !bound)
1249 goto error;
1251 if (k % 2 == 0)
1252 isl_seq_neg(bound->el, map->p[i]->eq[k / 2], 1 + total);
1253 else
1254 isl_seq_cpy(bound->el, map->p[i]->eq[k / 2], 1 + total);
1255 isl_int_add_ui(bound->el[0], bound->el[0], 1);
1257 isl_seq_cpy(wraps.mat->row[0], bound->el, 1 + total);
1258 wraps.mat->n_row = 1;
1260 if (add_wraps(&wraps, map->p[j], tabs[j], bound->el, set_i) < 0)
1261 goto error;
1262 if (!wraps.mat->n_row)
1263 goto unbounded;
1265 isl_int_sub_ui(bound->el[0], bound->el[0], 1);
1266 isl_seq_neg(bound->el, bound->el, 1 + total);
1268 isl_seq_cpy(wraps.mat->row[wraps.mat->n_row], bound->el, 1 + total);
1269 wraps.mat->n_row++;
1271 if (add_wraps(&wraps, map->p[i], tabs[i], bound->el, set_j) < 0)
1272 goto error;
1273 if (!wraps.mat->n_row)
1274 goto unbounded;
1276 changed = fuse(map, i, j, tabs, eq_i, ineq_i, eq_j, ineq_j, wraps.mat);
1278 if (0) {
1279 error: changed = -1;
1281 unbounded:
1283 wraps_free(&wraps);
1284 isl_set_free(set_i);
1285 isl_set_free(set_j);
1286 isl_vec_free(bound);
1288 return changed;
1291 /* Check if the union of the given pair of basic maps
1292 * can be represented by a single basic map.
1293 * If so, replace the pair by the single basic map and return 1.
1294 * Otherwise, return 0;
1295 * The two basic maps are assumed to live in the same local space.
1297 * We first check the effect of each constraint of one basic map
1298 * on the other basic map.
1299 * The constraint may be
1300 * redundant the constraint is redundant in its own
1301 * basic map and should be ignore and removed
1302 * in the end
1303 * valid all (integer) points of the other basic map
1304 * satisfy the constraint
1305 * separate no (integer) point of the other basic map
1306 * satisfies the constraint
1307 * cut some but not all points of the other basic map
1308 * satisfy the constraint
1309 * adj_eq the given constraint is adjacent (on the outside)
1310 * to an equality of the other basic map
1311 * adj_ineq the given constraint is adjacent (on the outside)
1312 * to an inequality of the other basic map
1314 * We consider seven cases in which we can replace the pair by a single
1315 * basic map. We ignore all "redundant" constraints.
1317 * 1. all constraints of one basic map are valid
1318 * => the other basic map is a subset and can be removed
1320 * 2. all constraints of both basic maps are either "valid" or "cut"
1321 * and the facets corresponding to the "cut" constraints
1322 * of one of the basic maps lies entirely inside the other basic map
1323 * => the pair can be replaced by a basic map consisting
1324 * of the valid constraints in both basic maps
1326 * 3. there is a single pair of adjacent inequalities
1327 * (all other constraints are "valid")
1328 * => the pair can be replaced by a basic map consisting
1329 * of the valid constraints in both basic maps
1331 * 4. one basic map has a single adjacent inequality, while the other
1332 * constraints are "valid". The other basic map has some
1333 * "cut" constraints, but replacing the adjacent inequality by
1334 * its opposite and adding the valid constraints of the other
1335 * basic map results in a subset of the other basic map
1336 * => the pair can be replaced by a basic map consisting
1337 * of the valid constraints in both basic maps
1339 * 5. there is a single adjacent pair of an inequality and an equality,
1340 * the other constraints of the basic map containing the inequality are
1341 * "valid". Moreover, if the inequality the basic map is relaxed
1342 * and then turned into an equality, then resulting facet lies
1343 * entirely inside the other basic map
1344 * => the pair can be replaced by the basic map containing
1345 * the inequality, with the inequality relaxed.
1347 * 6. there is a single adjacent pair of an inequality and an equality,
1348 * the other constraints of the basic map containing the inequality are
1349 * "valid". Moreover, the facets corresponding to both
1350 * the inequality and the equality can be wrapped around their
1351 * ridges to include the other basic map
1352 * => the pair can be replaced by a basic map consisting
1353 * of the valid constraints in both basic maps together
1354 * with all wrapping constraints
1356 * 7. one of the basic maps extends beyond the other by at most one.
1357 * Moreover, the facets corresponding to the cut constraints and
1358 * the pieces of the other basic map at offset one from these cut
1359 * constraints can be wrapped around their ridges to include
1360 * the union of the two basic maps
1361 * => the pair can be replaced by a basic map consisting
1362 * of the valid constraints in both basic maps together
1363 * with all wrapping constraints
1365 * 8. the two basic maps live in adjacent hyperplanes. In principle
1366 * such sets can always be combined through wrapping, but we impose
1367 * that there is only one such pair, to avoid overeager coalescing.
1369 * Throughout the computation, we maintain a collection of tableaus
1370 * corresponding to the basic maps. When the basic maps are dropped
1371 * or combined, the tableaus are modified accordingly.
1373 static int coalesce_local_pair(__isl_keep isl_map *map, int i, int j,
1374 struct isl_tab **tabs)
1376 int changed = 0;
1377 int *eq_i = NULL;
1378 int *eq_j = NULL;
1379 int *ineq_i = NULL;
1380 int *ineq_j = NULL;
1382 eq_i = eq_status_in(map->p[i], tabs[j]);
1383 if (map->p[i]->n_eq && !eq_i)
1384 goto error;
1385 if (any(eq_i, 2 * map->p[i]->n_eq, STATUS_ERROR))
1386 goto error;
1387 if (any(eq_i, 2 * map->p[i]->n_eq, STATUS_SEPARATE))
1388 goto done;
1390 eq_j = eq_status_in(map->p[j], tabs[i]);
1391 if (map->p[j]->n_eq && !eq_j)
1392 goto error;
1393 if (any(eq_j, 2 * map->p[j]->n_eq, STATUS_ERROR))
1394 goto error;
1395 if (any(eq_j, 2 * map->p[j]->n_eq, STATUS_SEPARATE))
1396 goto done;
1398 ineq_i = ineq_status_in(map->p[i], tabs[i], tabs[j]);
1399 if (map->p[i]->n_ineq && !ineq_i)
1400 goto error;
1401 if (any(ineq_i, map->p[i]->n_ineq, STATUS_ERROR))
1402 goto error;
1403 if (any(ineq_i, map->p[i]->n_ineq, STATUS_SEPARATE))
1404 goto done;
1406 ineq_j = ineq_status_in(map->p[j], tabs[j], tabs[i]);
1407 if (map->p[j]->n_ineq && !ineq_j)
1408 goto error;
1409 if (any(ineq_j, map->p[j]->n_ineq, STATUS_ERROR))
1410 goto error;
1411 if (any(ineq_j, map->p[j]->n_ineq, STATUS_SEPARATE))
1412 goto done;
1414 if (all(eq_i, 2 * map->p[i]->n_eq, STATUS_VALID) &&
1415 all(ineq_i, map->p[i]->n_ineq, STATUS_VALID)) {
1416 drop(map, j, tabs);
1417 changed = 1;
1418 } else if (all(eq_j, 2 * map->p[j]->n_eq, STATUS_VALID) &&
1419 all(ineq_j, map->p[j]->n_ineq, STATUS_VALID)) {
1420 drop(map, i, tabs);
1421 changed = 1;
1422 } else if (any(eq_i, 2 * map->p[i]->n_eq, STATUS_ADJ_EQ)) {
1423 changed = check_eq_adj_eq(map, i, j, tabs,
1424 eq_i, ineq_i, eq_j, ineq_j);
1425 } else if (any(eq_j, 2 * map->p[j]->n_eq, STATUS_ADJ_EQ)) {
1426 changed = check_eq_adj_eq(map, j, i, tabs,
1427 eq_j, ineq_j, eq_i, ineq_i);
1428 } else if (any(eq_i, 2 * map->p[i]->n_eq, STATUS_ADJ_INEQ) ||
1429 any(eq_j, 2 * map->p[j]->n_eq, STATUS_ADJ_INEQ)) {
1430 changed = check_adj_eq(map, i, j, tabs,
1431 eq_i, ineq_i, eq_j, ineq_j);
1432 } else if (any(ineq_i, map->p[i]->n_ineq, STATUS_ADJ_EQ) ||
1433 any(ineq_j, map->p[j]->n_ineq, STATUS_ADJ_EQ)) {
1434 /* Can't happen */
1435 /* BAD ADJ INEQ */
1436 } else if (any(ineq_i, map->p[i]->n_ineq, STATUS_ADJ_INEQ) ||
1437 any(ineq_j, map->p[j]->n_ineq, STATUS_ADJ_INEQ)) {
1438 changed = check_adj_ineq(map, i, j, tabs,
1439 eq_i, ineq_i, eq_j, ineq_j);
1440 } else {
1441 if (!any(eq_i, 2 * map->p[i]->n_eq, STATUS_CUT) &&
1442 !any(eq_j, 2 * map->p[j]->n_eq, STATUS_CUT))
1443 changed = check_facets(map, i, j, tabs, ineq_i, ineq_j);
1444 if (!changed)
1445 changed = check_wrap(map, i, j, tabs,
1446 eq_i, ineq_i, eq_j, ineq_j);
1449 done:
1450 free(eq_i);
1451 free(eq_j);
1452 free(ineq_i);
1453 free(ineq_j);
1454 return changed;
1455 error:
1456 free(eq_i);
1457 free(eq_j);
1458 free(ineq_i);
1459 free(ineq_j);
1460 return -1;
1463 /* Do the two basic maps live in the same local space, i.e.,
1464 * do they have the same (known) divs?
1465 * If either basic map has any unknown divs, then we can only assume
1466 * that they do not live in the same local space.
1468 static int same_divs(__isl_keep isl_basic_map *bmap1,
1469 __isl_keep isl_basic_map *bmap2)
1471 int i;
1472 int known;
1473 int total;
1475 if (!bmap1 || !bmap2)
1476 return -1;
1477 if (bmap1->n_div != bmap2->n_div)
1478 return 0;
1480 if (bmap1->n_div == 0)
1481 return 1;
1483 known = isl_basic_map_divs_known(bmap1);
1484 if (known < 0 || !known)
1485 return known;
1486 known = isl_basic_map_divs_known(bmap2);
1487 if (known < 0 || !known)
1488 return known;
1490 total = isl_basic_map_total_dim(bmap1);
1491 for (i = 0; i < bmap1->n_div; ++i)
1492 if (!isl_seq_eq(bmap1->div[i], bmap2->div[i], 2 + total))
1493 return 0;
1495 return 1;
1498 /* Given two basic maps "i" and "j", where the divs of "i" form a subset
1499 * of those of "j", check if basic map "j" is a subset of basic map "i"
1500 * and, if so, drop basic map "j".
1502 * We first expand the divs of basic map "i" to match those of basic map "j",
1503 * using the divs and expansion computed by the caller.
1504 * Then we check if all constraints of the expanded "i" are valid for "j".
1506 static int coalesce_subset(__isl_keep isl_map *map, int i, int j,
1507 struct isl_tab **tabs, __isl_keep isl_mat *div, int *exp)
1509 isl_basic_map *bmap;
1510 int changed = 0;
1511 int *eq_i = NULL;
1512 int *ineq_i = NULL;
1514 bmap = isl_basic_map_copy(map->p[i]);
1515 bmap = isl_basic_set_expand_divs(bmap, isl_mat_copy(div), exp);
1517 if (!bmap)
1518 goto error;
1520 eq_i = eq_status_in(bmap, tabs[j]);
1521 if (bmap->n_eq && !eq_i)
1522 goto error;
1523 if (any(eq_i, 2 * bmap->n_eq, STATUS_ERROR))
1524 goto error;
1525 if (any(eq_i, 2 * bmap->n_eq, STATUS_SEPARATE))
1526 goto done;
1528 ineq_i = ineq_status_in(bmap, NULL, tabs[j]);
1529 if (bmap->n_ineq && !ineq_i)
1530 goto error;
1531 if (any(ineq_i, bmap->n_ineq, STATUS_ERROR))
1532 goto error;
1533 if (any(ineq_i, bmap->n_ineq, STATUS_SEPARATE))
1534 goto done;
1536 if (all(eq_i, 2 * map->p[i]->n_eq, STATUS_VALID) &&
1537 all(ineq_i, map->p[i]->n_ineq, STATUS_VALID)) {
1538 drop(map, j, tabs);
1539 changed = 1;
1542 done:
1543 isl_basic_map_free(bmap);
1544 free(eq_i);
1545 free(ineq_i);
1546 return changed;
1547 error:
1548 isl_basic_map_free(bmap);
1549 free(eq_i);
1550 free(ineq_i);
1551 return -1;
1554 /* Check if the basic map "j" is a subset of basic map "i",
1555 * assuming that "i" has fewer divs that "j".
1556 * If not, then we change the order.
1558 * If the two basic maps have the same number of divs, then
1559 * they must necessarily be different. Otherwise, we would have
1560 * called coalesce_local_pair. We therefore don't try anything
1561 * in this case.
1563 * We first check if the divs of "i" are all known and form a subset
1564 * of those of "j". If so, we pass control over to coalesce_subset.
1566 static int check_coalesce_subset(__isl_keep isl_map *map, int i, int j,
1567 struct isl_tab **tabs)
1569 int known;
1570 isl_mat *div_i, *div_j, *div;
1571 int *exp1 = NULL;
1572 int *exp2 = NULL;
1573 isl_ctx *ctx;
1574 int subset;
1576 if (map->p[i]->n_div == map->p[j]->n_div)
1577 return 0;
1578 if (map->p[j]->n_div < map->p[i]->n_div)
1579 return check_coalesce_subset(map, j, i, tabs);
1581 known = isl_basic_map_divs_known(map->p[i]);
1582 if (known < 0 || !known)
1583 return known;
1585 ctx = isl_map_get_ctx(map);
1587 div_i = isl_basic_map_get_divs(map->p[i]);
1588 div_j = isl_basic_map_get_divs(map->p[j]);
1590 if (!div_i || !div_j)
1591 goto error;
1593 exp1 = isl_alloc_array(ctx, int, div_i->n_row);
1594 exp2 = isl_alloc_array(ctx, int, div_j->n_row);
1595 if ((div_i->n_row && !exp1) || (div_j->n_row && !exp2))
1596 goto error;
1598 div = isl_merge_divs(div_i, div_j, exp1, exp2);
1599 if (!div)
1600 goto error;
1602 if (div->n_row == div_j->n_row)
1603 subset = coalesce_subset(map, i, j, tabs, div, exp1);
1604 else
1605 subset = 0;
1607 isl_mat_free(div);
1609 isl_mat_free(div_i);
1610 isl_mat_free(div_j);
1612 free(exp2);
1613 free(exp1);
1615 return subset;
1616 error:
1617 isl_mat_free(div_i);
1618 isl_mat_free(div_j);
1619 free(exp1);
1620 free(exp2);
1621 return -1;
1624 /* Check if the union of the given pair of basic maps
1625 * can be represented by a single basic map.
1626 * If so, replace the pair by the single basic map and return 1.
1627 * Otherwise, return 0;
1629 * We first check if the two basic maps live in the same local space.
1630 * If so, we do the complete check. Otherwise, we check if one is
1631 * an obvious subset of the other.
1633 static int coalesce_pair(__isl_keep isl_map *map, int i, int j,
1634 struct isl_tab **tabs)
1636 int same;
1638 same = same_divs(map->p[i], map->p[j]);
1639 if (same < 0)
1640 return -1;
1641 if (same)
1642 return coalesce_local_pair(map, i, j, tabs);
1644 return check_coalesce_subset(map, i, j, tabs);
1647 static struct isl_map *coalesce(struct isl_map *map, struct isl_tab **tabs)
1649 int i, j;
1651 for (i = map->n - 2; i >= 0; --i)
1652 restart:
1653 for (j = i + 1; j < map->n; ++j) {
1654 int changed;
1655 changed = coalesce_pair(map, i, j, tabs);
1656 if (changed < 0)
1657 goto error;
1658 if (changed)
1659 goto restart;
1661 return map;
1662 error:
1663 isl_map_free(map);
1664 return NULL;
1667 /* For each pair of basic maps in the map, check if the union of the two
1668 * can be represented by a single basic map.
1669 * If so, replace the pair by the single basic map and start over.
1671 * Since we are constructing the tableaus of the basic maps anyway,
1672 * we exploit them to detect implicit equalities and redundant constraints.
1673 * This also helps the coalescing as it can ignore the redundant constraints.
1674 * In order to avoid confusion, we make all implicit equalities explicit
1675 * in the basic maps. We don't call isl_basic_map_gauss, though,
1676 * as that may affect the number of constraints.
1677 * This means that we have to call isl_basic_map_gauss at the end
1678 * of the computation to ensure that the basic maps are not left
1679 * in an unexpected state.
1681 struct isl_map *isl_map_coalesce(struct isl_map *map)
1683 int i;
1684 unsigned n;
1685 struct isl_tab **tabs = NULL;
1687 map = isl_map_remove_empty_parts(map);
1688 if (!map)
1689 return NULL;
1691 if (map->n <= 1)
1692 return map;
1694 map = isl_map_sort_divs(map);
1695 map = isl_map_cow(map);
1697 if (!map)
1698 return NULL;
1700 tabs = isl_calloc_array(map->ctx, struct isl_tab *, map->n);
1701 if (!tabs)
1702 goto error;
1704 n = map->n;
1705 for (i = 0; i < map->n; ++i) {
1706 tabs[i] = isl_tab_from_basic_map(map->p[i], 0);
1707 if (!tabs[i])
1708 goto error;
1709 if (!ISL_F_ISSET(map->p[i], ISL_BASIC_MAP_NO_IMPLICIT))
1710 if (isl_tab_detect_implicit_equalities(tabs[i]) < 0)
1711 goto error;
1712 map->p[i] = isl_tab_make_equalities_explicit(tabs[i],
1713 map->p[i]);
1714 if (!map->p[i])
1715 goto error;
1716 if (!ISL_F_ISSET(map->p[i], ISL_BASIC_MAP_NO_REDUNDANT))
1717 if (isl_tab_detect_redundant(tabs[i]) < 0)
1718 goto error;
1720 for (i = map->n - 1; i >= 0; --i)
1721 if (tabs[i]->empty)
1722 drop(map, i, tabs);
1724 map = coalesce(map, tabs);
1726 if (map)
1727 for (i = 0; i < map->n; ++i) {
1728 map->p[i] = isl_basic_map_update_from_tab(map->p[i],
1729 tabs[i]);
1730 map->p[i] = isl_basic_map_gauss(map->p[i], NULL);
1731 map->p[i] = isl_basic_map_finalize(map->p[i]);
1732 if (!map->p[i])
1733 goto error;
1734 ISL_F_SET(map->p[i], ISL_BASIC_MAP_NO_IMPLICIT);
1735 ISL_F_SET(map->p[i], ISL_BASIC_MAP_NO_REDUNDANT);
1738 for (i = 0; i < n; ++i)
1739 isl_tab_free(tabs[i]);
1741 free(tabs);
1743 return map;
1744 error:
1745 if (tabs)
1746 for (i = 0; i < n; ++i)
1747 isl_tab_free(tabs[i]);
1748 free(tabs);
1749 isl_map_free(map);
1750 return NULL;
1753 /* For each pair of basic sets in the set, check if the union of the two
1754 * can be represented by a single basic set.
1755 * If so, replace the pair by the single basic set and start over.
1757 struct isl_set *isl_set_coalesce(struct isl_set *set)
1759 return (struct isl_set *)isl_map_coalesce((struct isl_map *)set);