isl_coalesce.c: check_facets: update justification
[isl.git] / isl_coalesce.c
blobcc8f515b491bf8559e9d2306559591cc4eff78e9
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 static __isl_give isl_set *set_from_updated_bmap(__isl_keep isl_basic_map *bmap,
766 struct isl_tab *tab)
768 bmap = isl_basic_map_copy(bmap);
769 bmap = isl_basic_map_cow(bmap);
770 bmap = isl_basic_map_update_from_tab(bmap, tab);
771 return isl_set_from_basic_set(isl_basic_map_underlying_set(bmap));
774 /* Given a basic set i with a constraint k that is adjacent to
775 * basic set j, check if we can wrap
776 * both the facet corresponding to k and basic map j
777 * around their ridges to include the other set.
778 * If so, replace the pair of basic sets by their union.
780 * All constraints of i (except k) are assumed to be valid for j.
781 * This means that there is no real need to wrap the ridges of
782 * the faces of basic map i around basic map j but since we do,
783 * we have to check that the resulting wrapping constraints are valid for i.
784 * ____ _____
785 * / | / \
786 * / || / |
787 * \ || => \ |
788 * \ || \ |
789 * \___|| \____|
792 static int can_wrap_in_facet(struct isl_map *map, int i, int j, int k,
793 struct isl_tab **tabs, int *eq_i, int *ineq_i, int *eq_j, int *ineq_j)
795 int changed = 0;
796 struct isl_wraps wraps;
797 isl_mat *mat;
798 struct isl_set *set_i = NULL;
799 struct isl_set *set_j = NULL;
800 struct isl_vec *bound = NULL;
801 unsigned total = isl_basic_map_total_dim(map->p[i]);
802 struct isl_tab_undo *snap;
803 int n;
805 set_i = set_from_updated_bmap(map->p[i], tabs[i]);
806 set_j = set_from_updated_bmap(map->p[j], tabs[j]);
807 mat = isl_mat_alloc(map->ctx, 2 * (map->p[i]->n_eq + map->p[j]->n_eq) +
808 map->p[i]->n_ineq + map->p[j]->n_ineq,
809 1 + total);
810 wraps_init(&wraps, mat, map, i, j, eq_i, ineq_i, eq_j, ineq_j);
811 bound = isl_vec_alloc(map->ctx, 1 + total);
812 if (!set_i || !set_j || !wraps.mat || !bound)
813 goto error;
815 isl_seq_cpy(bound->el, map->p[i]->ineq[k], 1 + total);
816 isl_int_add_ui(bound->el[0], bound->el[0], 1);
818 isl_seq_cpy(wraps.mat->row[0], bound->el, 1 + total);
819 wraps.mat->n_row = 1;
821 if (add_wraps(&wraps, map->p[j], tabs[j], bound->el, set_i) < 0)
822 goto error;
823 if (!wraps.mat->n_row)
824 goto unbounded;
826 snap = isl_tab_snap(tabs[i]);
828 if (isl_tab_select_facet(tabs[i], map->p[i]->n_eq + k) < 0)
829 goto error;
830 if (isl_tab_detect_redundant(tabs[i]) < 0)
831 goto error;
833 isl_seq_neg(bound->el, map->p[i]->ineq[k], 1 + total);
835 n = wraps.mat->n_row;
836 if (add_wraps(&wraps, map->p[i], tabs[i], bound->el, set_j) < 0)
837 goto error;
839 if (isl_tab_rollback(tabs[i], snap) < 0)
840 goto error;
841 if (check_wraps(wraps.mat, n, tabs[i]) < 0)
842 goto error;
843 if (!wraps.mat->n_row)
844 goto unbounded;
846 changed = fuse(map, i, j, tabs, eq_i, ineq_i, eq_j, ineq_j, wraps.mat);
848 unbounded:
849 wraps_free(&wraps);
851 isl_set_free(set_i);
852 isl_set_free(set_j);
854 isl_vec_free(bound);
856 return changed;
857 error:
858 wraps_free(&wraps);
859 isl_vec_free(bound);
860 isl_set_free(set_i);
861 isl_set_free(set_j);
862 return -1;
865 /* Set the is_redundant property of the "n" constraints in "cuts",
866 * except "k" to "v".
867 * This is a fairly tricky operation as it bypasses isl_tab.c.
868 * The reason we want to temporarily mark some constraints redundant
869 * is that we want to ignore them in add_wraps.
871 * Initially all cut constraints are non-redundant, but the
872 * selection of a facet right before the call to this function
873 * may have made some of them redundant.
874 * Likewise, the same constraints are marked non-redundant
875 * in the second call to this function, before they are officially
876 * made non-redundant again in the subsequent rollback.
878 static void set_is_redundant(struct isl_tab *tab, unsigned n_eq,
879 int *cuts, int n, int k, int v)
881 int l;
883 for (l = 0; l < n; ++l) {
884 if (l == k)
885 continue;
886 tab->con[n_eq + cuts[l]].is_redundant = v;
890 /* Given a pair of basic maps i and j such that j sticks out
891 * of i at n cut constraints, each time by at most one,
892 * try to compute wrapping constraints and replace the two
893 * basic maps by a single basic map.
894 * The other constraints of i are assumed to be valid for j.
896 * The facets of i corresponding to the cut constraints are
897 * wrapped around their ridges, except those ridges determined
898 * by any of the other cut constraints.
899 * The intersections of cut constraints need to be ignored
900 * as the result of wrapping one cut constraint around another
901 * would result in a constraint cutting the union.
902 * In each case, the facets are wrapped to include the union
903 * of the two basic maps.
905 * The pieces of j that lie at an offset of exactly one from
906 * one of the cut constraints of i are wrapped around their edges.
907 * Here, there is no need to ignore intersections because we
908 * are wrapping around the union of the two basic maps.
910 * If any wrapping fails, i.e., if we cannot wrap to touch
911 * the union, then we give up.
912 * Otherwise, the pair of basic maps is replaced by their union.
914 static int wrap_in_facets(struct isl_map *map, int i, int j,
915 int *cuts, int n, struct isl_tab **tabs,
916 int *eq_i, int *ineq_i, int *eq_j, int *ineq_j)
918 int changed = 0;
919 struct isl_wraps wraps;
920 isl_mat *mat;
921 isl_set *set = NULL;
922 isl_vec *bound = NULL;
923 unsigned total = isl_basic_map_total_dim(map->p[i]);
924 int max_wrap;
925 int k;
926 struct isl_tab_undo *snap_i, *snap_j;
928 if (isl_tab_extend_cons(tabs[j], 1) < 0)
929 goto error;
931 max_wrap = 2 * (map->p[i]->n_eq + map->p[j]->n_eq) +
932 map->p[i]->n_ineq + map->p[j]->n_ineq;
933 max_wrap *= n;
935 set = isl_set_union(set_from_updated_bmap(map->p[i], tabs[i]),
936 set_from_updated_bmap(map->p[j], tabs[j]));
937 mat = isl_mat_alloc(map->ctx, max_wrap, 1 + total);
938 wraps_init(&wraps, mat, map, i, j, eq_i, ineq_i, eq_j, ineq_j);
939 bound = isl_vec_alloc(map->ctx, 1 + total);
940 if (!set || !wraps.mat || !bound)
941 goto error;
943 snap_i = isl_tab_snap(tabs[i]);
944 snap_j = isl_tab_snap(tabs[j]);
946 wraps.mat->n_row = 0;
948 for (k = 0; k < n; ++k) {
949 if (isl_tab_select_facet(tabs[i], map->p[i]->n_eq + cuts[k]) < 0)
950 goto error;
951 if (isl_tab_detect_redundant(tabs[i]) < 0)
952 goto error;
953 set_is_redundant(tabs[i], map->p[i]->n_eq, cuts, n, k, 1);
955 isl_seq_neg(bound->el, map->p[i]->ineq[cuts[k]], 1 + total);
956 if (!tabs[i]->empty &&
957 add_wraps(&wraps, map->p[i], tabs[i], bound->el, set) < 0)
958 goto error;
960 set_is_redundant(tabs[i], map->p[i]->n_eq, cuts, n, k, 0);
961 if (isl_tab_rollback(tabs[i], snap_i) < 0)
962 goto error;
964 if (tabs[i]->empty)
965 break;
966 if (!wraps.mat->n_row)
967 break;
969 isl_seq_cpy(bound->el, map->p[i]->ineq[cuts[k]], 1 + total);
970 isl_int_add_ui(bound->el[0], bound->el[0], 1);
971 if (isl_tab_add_eq(tabs[j], bound->el) < 0)
972 goto error;
973 if (isl_tab_detect_redundant(tabs[j]) < 0)
974 goto error;
976 if (!tabs[j]->empty &&
977 add_wraps(&wraps, map->p[j], tabs[j], bound->el, set) < 0)
978 goto error;
980 if (isl_tab_rollback(tabs[j], snap_j) < 0)
981 goto error;
983 if (!wraps.mat->n_row)
984 break;
987 if (k == n)
988 changed = fuse(map, i, j, tabs,
989 eq_i, ineq_i, eq_j, ineq_j, wraps.mat);
991 isl_vec_free(bound);
992 wraps_free(&wraps);
993 isl_set_free(set);
995 return changed;
996 error:
997 isl_vec_free(bound);
998 wraps_free(&wraps);
999 isl_set_free(set);
1000 return -1;
1003 /* Given two basic sets i and j such that i has no cut equalities,
1004 * check if relaxing all the cut inequalities of i by one turns
1005 * them into valid constraint for j and check if we can wrap in
1006 * the bits that are sticking out.
1007 * If so, replace the pair by their union.
1009 * We first check if all relaxed cut inequalities of i are valid for j
1010 * and then try to wrap in the intersections of the relaxed cut inequalities
1011 * with j.
1013 * During this wrapping, we consider the points of j that lie at a distance
1014 * of exactly 1 from i. In particular, we ignore the points that lie in
1015 * between this lower-dimensional space and the basic map i.
1016 * We can therefore only apply this to integer maps.
1017 * ____ _____
1018 * / ___|_ / \
1019 * / | | / |
1020 * \ | | => \ |
1021 * \|____| \ |
1022 * \___| \____/
1024 * _____ ______
1025 * | ____|_ | \
1026 * | | | | |
1027 * | | | => | |
1028 * |_| | | |
1029 * |_____| \______|
1031 * _______
1032 * | |
1033 * | |\ |
1034 * | | \ |
1035 * | | \ |
1036 * | | \|
1037 * | | \
1038 * | |_____\
1039 * | |
1040 * |_______|
1042 * Wrapping can fail if the result of wrapping one of the facets
1043 * around its edges does not produce any new facet constraint.
1044 * In particular, this happens when we try to wrap in unbounded sets.
1046 * _______________________________________________________________________
1048 * | ___
1049 * | | |
1050 * |_| |_________________________________________________________________
1051 * |___|
1053 * The following is not an acceptable result of coalescing the above two
1054 * sets as it includes extra integer points.
1055 * _______________________________________________________________________
1057 * |
1058 * |
1060 * \______________________________________________________________________
1062 static int can_wrap_in_set(struct isl_map *map, int i, int j,
1063 struct isl_tab **tabs, int *eq_i, int *ineq_i, int *eq_j, int *ineq_j)
1065 int changed = 0;
1066 int k, m;
1067 int n;
1068 int *cuts = NULL;
1070 if (ISL_F_ISSET(map->p[i], ISL_BASIC_MAP_RATIONAL) ||
1071 ISL_F_ISSET(map->p[j], ISL_BASIC_MAP_RATIONAL))
1072 return 0;
1074 n = count(ineq_i, map->p[i]->n_ineq, STATUS_CUT);
1075 if (n == 0)
1076 return 0;
1078 cuts = isl_alloc_array(map->ctx, int, n);
1079 if (!cuts)
1080 return -1;
1082 for (k = 0, m = 0; m < n; ++k) {
1083 enum isl_ineq_type type;
1085 if (ineq_i[k] != STATUS_CUT)
1086 continue;
1088 isl_int_add_ui(map->p[i]->ineq[k][0], map->p[i]->ineq[k][0], 1);
1089 type = isl_tab_ineq_type(tabs[j], map->p[i]->ineq[k]);
1090 isl_int_sub_ui(map->p[i]->ineq[k][0], map->p[i]->ineq[k][0], 1);
1091 if (type == isl_ineq_error)
1092 goto error;
1093 if (type != isl_ineq_redundant)
1094 break;
1095 cuts[m] = k;
1096 ++m;
1099 if (m == n)
1100 changed = wrap_in_facets(map, i, j, cuts, n, tabs,
1101 eq_i, ineq_i, eq_j, ineq_j);
1103 free(cuts);
1105 return changed;
1106 error:
1107 free(cuts);
1108 return -1;
1111 /* Check if either i or j has only cut inequalities that can
1112 * be used to wrap in (a facet of) the other basic set.
1113 * if so, replace the pair by their union.
1115 static int check_wrap(struct isl_map *map, int i, int j,
1116 struct isl_tab **tabs, int *eq_i, int *ineq_i, int *eq_j, int *ineq_j)
1118 int changed = 0;
1120 if (!any(eq_i, 2 * map->p[i]->n_eq, STATUS_CUT))
1121 changed = can_wrap_in_set(map, i, j, tabs,
1122 eq_i, ineq_i, eq_j, ineq_j);
1123 if (changed)
1124 return changed;
1126 if (!any(eq_j, 2 * map->p[j]->n_eq, STATUS_CUT))
1127 changed = can_wrap_in_set(map, j, i, tabs,
1128 eq_j, ineq_j, eq_i, ineq_i);
1129 return changed;
1132 /* At least one of the basic maps has an equality that is adjacent
1133 * to inequality. Make sure that only one of the basic maps has
1134 * such an equality and that the other basic map has exactly one
1135 * inequality adjacent to an equality.
1136 * We call the basic map that has the inequality "i" and the basic
1137 * map that has the equality "j".
1138 * If "i" has any "cut" (in)equality, then relaxing the inequality
1139 * by one would not result in a basic map that contains the other
1140 * basic map.
1142 static int check_adj_eq(struct isl_map *map, int i, int j,
1143 struct isl_tab **tabs, int *eq_i, int *ineq_i, int *eq_j, int *ineq_j)
1145 int changed = 0;
1146 int k;
1148 if (any(eq_i, 2 * map->p[i]->n_eq, STATUS_ADJ_INEQ) &&
1149 any(eq_j, 2 * map->p[j]->n_eq, STATUS_ADJ_INEQ))
1150 /* ADJ EQ TOO MANY */
1151 return 0;
1153 if (any(eq_i, 2 * map->p[i]->n_eq, STATUS_ADJ_INEQ))
1154 return check_adj_eq(map, j, i, tabs,
1155 eq_j, ineq_j, eq_i, ineq_i);
1157 /* j has an equality adjacent to an inequality in i */
1159 if (any(eq_i, 2 * map->p[i]->n_eq, STATUS_CUT))
1160 return 0;
1161 if (any(ineq_i, map->p[i]->n_ineq, STATUS_CUT))
1162 /* ADJ EQ CUT */
1163 return 0;
1164 if (count(ineq_i, map->p[i]->n_ineq, STATUS_ADJ_EQ) != 1 ||
1165 any(ineq_j, map->p[j]->n_ineq, STATUS_ADJ_EQ) ||
1166 any(ineq_i, map->p[i]->n_ineq, STATUS_ADJ_INEQ) ||
1167 any(ineq_j, map->p[j]->n_ineq, STATUS_ADJ_INEQ))
1168 /* ADJ EQ TOO MANY */
1169 return 0;
1171 for (k = 0; k < map->p[i]->n_ineq; ++k)
1172 if (ineq_i[k] == STATUS_ADJ_EQ)
1173 break;
1175 changed = is_adj_eq_extension(map, i, j, k, tabs,
1176 eq_i, ineq_i, eq_j, ineq_j);
1177 if (changed)
1178 return changed;
1180 if (count(eq_j, 2 * map->p[j]->n_eq, STATUS_ADJ_INEQ) != 1)
1181 return 0;
1183 changed = can_wrap_in_facet(map, i, j, k, tabs, eq_i, ineq_i, eq_j, ineq_j);
1185 return changed;
1188 /* The two basic maps lie on adjacent hyperplanes. In particular,
1189 * basic map "i" has an equality that lies parallel to basic map "j".
1190 * Check if we can wrap the facets around the parallel hyperplanes
1191 * to include the other set.
1193 * We perform basically the same operations as can_wrap_in_facet,
1194 * except that we don't need to select a facet of one of the sets.
1196 * \\ \\
1197 * \\ => \\
1198 * \ \|
1200 * We only allow one equality of "i" to be adjacent to an equality of "j"
1201 * to avoid coalescing
1203 * [m, n] -> { [x, y] -> [x, 1 + y] : x >= 1 and y >= 1 and
1204 * x <= 10 and y <= 10;
1205 * [x, y] -> [1 + x, y] : x >= 1 and x <= 20 and
1206 * y >= 5 and y <= 15 }
1208 * to
1210 * [m, n] -> { [x, y] -> [x2, y2] : x >= 1 and 10y2 <= 20 - x + 10y and
1211 * 4y2 >= 5 + 3y and 5y2 <= 15 + 4y and
1212 * y2 <= 1 + x + y - x2 and y2 >= y and
1213 * y2 >= 1 + x + y - x2 }
1215 static int check_eq_adj_eq(struct isl_map *map, int i, int j,
1216 struct isl_tab **tabs, int *eq_i, int *ineq_i, int *eq_j, int *ineq_j)
1218 int k;
1219 int changed = 0;
1220 struct isl_wraps wraps;
1221 isl_mat *mat;
1222 struct isl_set *set_i = NULL;
1223 struct isl_set *set_j = NULL;
1224 struct isl_vec *bound = NULL;
1225 unsigned total = isl_basic_map_total_dim(map->p[i]);
1227 if (count(eq_i, 2 * map->p[i]->n_eq, STATUS_ADJ_EQ) != 1)
1228 return 0;
1230 for (k = 0; k < 2 * map->p[i]->n_eq ; ++k)
1231 if (eq_i[k] == STATUS_ADJ_EQ)
1232 break;
1234 set_i = set_from_updated_bmap(map->p[i], tabs[i]);
1235 set_j = set_from_updated_bmap(map->p[j], tabs[j]);
1236 mat = isl_mat_alloc(map->ctx, 2 * (map->p[i]->n_eq + map->p[j]->n_eq) +
1237 map->p[i]->n_ineq + map->p[j]->n_ineq,
1238 1 + total);
1239 wraps_init(&wraps, mat, map, i, j, eq_i, ineq_i, eq_j, ineq_j);
1240 bound = isl_vec_alloc(map->ctx, 1 + total);
1241 if (!set_i || !set_j || !wraps.mat || !bound)
1242 goto error;
1244 if (k % 2 == 0)
1245 isl_seq_neg(bound->el, map->p[i]->eq[k / 2], 1 + total);
1246 else
1247 isl_seq_cpy(bound->el, map->p[i]->eq[k / 2], 1 + total);
1248 isl_int_add_ui(bound->el[0], bound->el[0], 1);
1250 isl_seq_cpy(wraps.mat->row[0], bound->el, 1 + total);
1251 wraps.mat->n_row = 1;
1253 if (add_wraps(&wraps, map->p[j], tabs[j], bound->el, set_i) < 0)
1254 goto error;
1255 if (!wraps.mat->n_row)
1256 goto unbounded;
1258 isl_int_sub_ui(bound->el[0], bound->el[0], 1);
1259 isl_seq_neg(bound->el, bound->el, 1 + total);
1261 isl_seq_cpy(wraps.mat->row[wraps.mat->n_row], bound->el, 1 + total);
1262 wraps.mat->n_row++;
1264 if (add_wraps(&wraps, map->p[i], tabs[i], bound->el, set_j) < 0)
1265 goto error;
1266 if (!wraps.mat->n_row)
1267 goto unbounded;
1269 changed = fuse(map, i, j, tabs, eq_i, ineq_i, eq_j, ineq_j, wraps.mat);
1271 if (0) {
1272 error: changed = -1;
1274 unbounded:
1276 wraps_free(&wraps);
1277 isl_set_free(set_i);
1278 isl_set_free(set_j);
1279 isl_vec_free(bound);
1281 return changed;
1284 /* Check if the union of the given pair of basic maps
1285 * can be represented by a single basic map.
1286 * If so, replace the pair by the single basic map and return 1.
1287 * Otherwise, return 0;
1288 * The two basic maps are assumed to live in the same local space.
1290 * We first check the effect of each constraint of one basic map
1291 * on the other basic map.
1292 * The constraint may be
1293 * redundant the constraint is redundant in its own
1294 * basic map and should be ignore and removed
1295 * in the end
1296 * valid all (integer) points of the other basic map
1297 * satisfy the constraint
1298 * separate no (integer) point of the other basic map
1299 * satisfies the constraint
1300 * cut some but not all points of the other basic map
1301 * satisfy the constraint
1302 * adj_eq the given constraint is adjacent (on the outside)
1303 * to an equality of the other basic map
1304 * adj_ineq the given constraint is adjacent (on the outside)
1305 * to an inequality of the other basic map
1307 * We consider seven cases in which we can replace the pair by a single
1308 * basic map. We ignore all "redundant" constraints.
1310 * 1. all constraints of one basic map are valid
1311 * => the other basic map is a subset and can be removed
1313 * 2. all constraints of both basic maps are either "valid" or "cut"
1314 * and the facets corresponding to the "cut" constraints
1315 * of one of the basic maps lies entirely inside the other basic map
1316 * => the pair can be replaced by a basic map consisting
1317 * of the valid constraints in both basic maps
1319 * 3. there is a single pair of adjacent inequalities
1320 * (all other constraints are "valid")
1321 * => the pair can be replaced by a basic map consisting
1322 * of the valid constraints in both basic maps
1324 * 4. one basic map has a single adjacent inequality, while the other
1325 * constraints are "valid". The other basic map has some
1326 * "cut" constraints, but replacing the adjacent inequality by
1327 * its opposite and adding the valid constraints of the other
1328 * basic map results in a subset of the other basic map
1329 * => the pair can be replaced by a basic map consisting
1330 * of the valid constraints in both basic maps
1332 * 5. there is a single adjacent pair of an inequality and an equality,
1333 * the other constraints of the basic map containing the inequality are
1334 * "valid". Moreover, if the inequality the basic map is relaxed
1335 * and then turned into an equality, then resulting facet lies
1336 * entirely inside the other basic map
1337 * => the pair can be replaced by the basic map containing
1338 * the inequality, with the inequality relaxed.
1340 * 6. there is a single adjacent pair of an inequality and an equality,
1341 * the other constraints of the basic map containing the inequality are
1342 * "valid". Moreover, the facets corresponding to both
1343 * the inequality and the equality can be wrapped around their
1344 * ridges to include the other basic map
1345 * => the pair can be replaced by a basic map consisting
1346 * of the valid constraints in both basic maps together
1347 * with all wrapping constraints
1349 * 7. one of the basic maps extends beyond the other by at most one.
1350 * Moreover, the facets corresponding to the cut constraints and
1351 * the pieces of the other basic map at offset one from these cut
1352 * constraints can be wrapped around their ridges to include
1353 * the union of the two basic maps
1354 * => the pair can be replaced by a basic map consisting
1355 * of the valid constraints in both basic maps together
1356 * with all wrapping constraints
1358 * 8. the two basic maps live in adjacent hyperplanes. In principle
1359 * such sets can always be combined through wrapping, but we impose
1360 * that there is only one such pair, to avoid overeager coalescing.
1362 * Throughout the computation, we maintain a collection of tableaus
1363 * corresponding to the basic maps. When the basic maps are dropped
1364 * or combined, the tableaus are modified accordingly.
1366 static int coalesce_local_pair(__isl_keep isl_map *map, int i, int j,
1367 struct isl_tab **tabs)
1369 int changed = 0;
1370 int *eq_i = NULL;
1371 int *eq_j = NULL;
1372 int *ineq_i = NULL;
1373 int *ineq_j = NULL;
1375 eq_i = eq_status_in(map->p[i], tabs[j]);
1376 if (map->p[i]->n_eq && !eq_i)
1377 goto error;
1378 if (any(eq_i, 2 * map->p[i]->n_eq, STATUS_ERROR))
1379 goto error;
1380 if (any(eq_i, 2 * map->p[i]->n_eq, STATUS_SEPARATE))
1381 goto done;
1383 eq_j = eq_status_in(map->p[j], tabs[i]);
1384 if (map->p[j]->n_eq && !eq_j)
1385 goto error;
1386 if (any(eq_j, 2 * map->p[j]->n_eq, STATUS_ERROR))
1387 goto error;
1388 if (any(eq_j, 2 * map->p[j]->n_eq, STATUS_SEPARATE))
1389 goto done;
1391 ineq_i = ineq_status_in(map->p[i], tabs[i], tabs[j]);
1392 if (map->p[i]->n_ineq && !ineq_i)
1393 goto error;
1394 if (any(ineq_i, map->p[i]->n_ineq, STATUS_ERROR))
1395 goto error;
1396 if (any(ineq_i, map->p[i]->n_ineq, STATUS_SEPARATE))
1397 goto done;
1399 ineq_j = ineq_status_in(map->p[j], tabs[j], tabs[i]);
1400 if (map->p[j]->n_ineq && !ineq_j)
1401 goto error;
1402 if (any(ineq_j, map->p[j]->n_ineq, STATUS_ERROR))
1403 goto error;
1404 if (any(ineq_j, map->p[j]->n_ineq, STATUS_SEPARATE))
1405 goto done;
1407 if (all(eq_i, 2 * map->p[i]->n_eq, STATUS_VALID) &&
1408 all(ineq_i, map->p[i]->n_ineq, STATUS_VALID)) {
1409 drop(map, j, tabs);
1410 changed = 1;
1411 } else if (all(eq_j, 2 * map->p[j]->n_eq, STATUS_VALID) &&
1412 all(ineq_j, map->p[j]->n_ineq, STATUS_VALID)) {
1413 drop(map, i, tabs);
1414 changed = 1;
1415 } else if (any(eq_i, 2 * map->p[i]->n_eq, STATUS_ADJ_EQ)) {
1416 changed = check_eq_adj_eq(map, i, j, tabs,
1417 eq_i, ineq_i, eq_j, ineq_j);
1418 } else if (any(eq_j, 2 * map->p[j]->n_eq, STATUS_ADJ_EQ)) {
1419 changed = check_eq_adj_eq(map, j, i, tabs,
1420 eq_j, ineq_j, eq_i, ineq_i);
1421 } else if (any(eq_i, 2 * map->p[i]->n_eq, STATUS_ADJ_INEQ) ||
1422 any(eq_j, 2 * map->p[j]->n_eq, STATUS_ADJ_INEQ)) {
1423 changed = check_adj_eq(map, i, j, tabs,
1424 eq_i, ineq_i, eq_j, ineq_j);
1425 } else if (any(ineq_i, map->p[i]->n_ineq, STATUS_ADJ_EQ) ||
1426 any(ineq_j, map->p[j]->n_ineq, STATUS_ADJ_EQ)) {
1427 /* Can't happen */
1428 /* BAD ADJ INEQ */
1429 } else if (any(ineq_i, map->p[i]->n_ineq, STATUS_ADJ_INEQ) ||
1430 any(ineq_j, map->p[j]->n_ineq, STATUS_ADJ_INEQ)) {
1431 changed = check_adj_ineq(map, i, j, tabs,
1432 eq_i, ineq_i, eq_j, ineq_j);
1433 } else {
1434 if (!any(eq_i, 2 * map->p[i]->n_eq, STATUS_CUT) &&
1435 !any(eq_j, 2 * map->p[j]->n_eq, STATUS_CUT))
1436 changed = check_facets(map, i, j, tabs, ineq_i, ineq_j);
1437 if (!changed)
1438 changed = check_wrap(map, i, j, tabs,
1439 eq_i, ineq_i, eq_j, ineq_j);
1442 done:
1443 free(eq_i);
1444 free(eq_j);
1445 free(ineq_i);
1446 free(ineq_j);
1447 return changed;
1448 error:
1449 free(eq_i);
1450 free(eq_j);
1451 free(ineq_i);
1452 free(ineq_j);
1453 return -1;
1456 /* Do the two basic maps live in the same local space, i.e.,
1457 * do they have the same (known) divs?
1458 * If either basic map has any unknown divs, then we can only assume
1459 * that they do not live in the same local space.
1461 static int same_divs(__isl_keep isl_basic_map *bmap1,
1462 __isl_keep isl_basic_map *bmap2)
1464 int i;
1465 int known;
1466 int total;
1468 if (!bmap1 || !bmap2)
1469 return -1;
1470 if (bmap1->n_div != bmap2->n_div)
1471 return 0;
1473 if (bmap1->n_div == 0)
1474 return 1;
1476 known = isl_basic_map_divs_known(bmap1);
1477 if (known < 0 || !known)
1478 return known;
1479 known = isl_basic_map_divs_known(bmap2);
1480 if (known < 0 || !known)
1481 return known;
1483 total = isl_basic_map_total_dim(bmap1);
1484 for (i = 0; i < bmap1->n_div; ++i)
1485 if (!isl_seq_eq(bmap1->div[i], bmap2->div[i], 2 + total))
1486 return 0;
1488 return 1;
1491 /* Given two basic maps "i" and "j", where the divs of "i" form a subset
1492 * of those of "j", check if basic map "j" is a subset of basic map "i"
1493 * and, if so, drop basic map "j".
1495 * We first expand the divs of basic map "i" to match those of basic map "j",
1496 * using the divs and expansion computed by the caller.
1497 * Then we check if all constraints of the expanded "i" are valid for "j".
1499 static int coalesce_subset(__isl_keep isl_map *map, int i, int j,
1500 struct isl_tab **tabs, __isl_keep isl_mat *div, int *exp)
1502 isl_basic_map *bmap;
1503 int changed = 0;
1504 int *eq_i = NULL;
1505 int *ineq_i = NULL;
1507 bmap = isl_basic_map_copy(map->p[i]);
1508 bmap = isl_basic_set_expand_divs(bmap, isl_mat_copy(div), exp);
1510 if (!bmap)
1511 goto error;
1513 eq_i = eq_status_in(bmap, tabs[j]);
1514 if (bmap->n_eq && !eq_i)
1515 goto error;
1516 if (any(eq_i, 2 * bmap->n_eq, STATUS_ERROR))
1517 goto error;
1518 if (any(eq_i, 2 * bmap->n_eq, STATUS_SEPARATE))
1519 goto done;
1521 ineq_i = ineq_status_in(bmap, NULL, tabs[j]);
1522 if (bmap->n_ineq && !ineq_i)
1523 goto error;
1524 if (any(ineq_i, bmap->n_ineq, STATUS_ERROR))
1525 goto error;
1526 if (any(ineq_i, bmap->n_ineq, STATUS_SEPARATE))
1527 goto done;
1529 if (all(eq_i, 2 * map->p[i]->n_eq, STATUS_VALID) &&
1530 all(ineq_i, map->p[i]->n_ineq, STATUS_VALID)) {
1531 drop(map, j, tabs);
1532 changed = 1;
1535 done:
1536 isl_basic_map_free(bmap);
1537 free(eq_i);
1538 free(ineq_i);
1539 return 0;
1540 error:
1541 isl_basic_map_free(bmap);
1542 free(eq_i);
1543 free(ineq_i);
1544 return -1;
1547 /* Check if the basic map "j" is a subset of basic map "i",
1548 * assuming that "i" has fewer divs that "j".
1549 * If not, then we change the order.
1551 * If the two basic maps have the same number of divs, then
1552 * they must necessarily be different. Otherwise, we would have
1553 * called coalesce_local_pair. We therefore don't try anything
1554 * in this case.
1556 * We first check if the divs of "i" are all known and form a subset
1557 * of those of "j". If so, we pass control over to coalesce_subset.
1559 static int check_coalesce_subset(__isl_keep isl_map *map, int i, int j,
1560 struct isl_tab **tabs)
1562 int known;
1563 isl_mat *div_i, *div_j, *div;
1564 int *exp1 = NULL;
1565 int *exp2 = NULL;
1566 isl_ctx *ctx;
1567 int subset;
1569 if (map->p[i]->n_div == map->p[j]->n_div)
1570 return 0;
1571 if (map->p[j]->n_div < map->p[i]->n_div)
1572 return check_coalesce_subset(map, j, i, tabs);
1574 known = isl_basic_map_divs_known(map->p[i]);
1575 if (known < 0 || !known)
1576 return known;
1578 ctx = isl_map_get_ctx(map);
1580 div_i = isl_basic_map_get_divs(map->p[i]);
1581 div_j = isl_basic_map_get_divs(map->p[j]);
1583 if (!div_i || !div_j)
1584 goto error;
1586 exp1 = isl_alloc_array(ctx, int, div_i->n_row);
1587 exp2 = isl_alloc_array(ctx, int, div_j->n_row);
1588 if ((div_i->n_row && !exp1) || (div_j->n_row && !exp2))
1589 goto error;
1591 div = isl_merge_divs(div_i, div_j, exp1, exp2);
1592 if (!div)
1593 goto error;
1595 if (div->n_row == div_j->n_row)
1596 subset = coalesce_subset(map, i, j, tabs, div, exp1);
1597 else
1598 subset = 0;
1600 isl_mat_free(div);
1602 isl_mat_free(div_i);
1603 isl_mat_free(div_j);
1605 free(exp2);
1606 free(exp1);
1608 return subset;
1609 error:
1610 isl_mat_free(div_i);
1611 isl_mat_free(div_j);
1612 free(exp1);
1613 free(exp2);
1614 return -1;
1617 /* Check if the union of the given pair of basic maps
1618 * can be represented by a single basic map.
1619 * If so, replace the pair by the single basic map and return 1.
1620 * Otherwise, return 0;
1622 * We first check if the two basic maps live in the same local space.
1623 * If so, we do the complete check. Otherwise, we check if one is
1624 * an obvious subset of the other.
1626 static int coalesce_pair(__isl_keep isl_map *map, int i, int j,
1627 struct isl_tab **tabs)
1629 int same;
1631 same = same_divs(map->p[i], map->p[j]);
1632 if (same < 0)
1633 return -1;
1634 if (same)
1635 return coalesce_local_pair(map, i, j, tabs);
1637 return check_coalesce_subset(map, i, j, tabs);
1640 static struct isl_map *coalesce(struct isl_map *map, struct isl_tab **tabs)
1642 int i, j;
1644 for (i = map->n - 2; i >= 0; --i)
1645 restart:
1646 for (j = i + 1; j < map->n; ++j) {
1647 int changed;
1648 changed = coalesce_pair(map, i, j, tabs);
1649 if (changed < 0)
1650 goto error;
1651 if (changed)
1652 goto restart;
1654 return map;
1655 error:
1656 isl_map_free(map);
1657 return NULL;
1660 /* For each pair of basic maps in the map, check if the union of the two
1661 * can be represented by a single basic map.
1662 * If so, replace the pair by the single basic map and start over.
1664 * Since we are constructing the tableaus of the basic maps anyway,
1665 * we exploit them to detect implicit equalities and redundant constraints.
1666 * This also helps the coalescing as it can ignore the redundant constraints.
1667 * In order to avoid confusion, we make all implicit equalities explicit
1668 * in the basic maps. We don't call isl_basic_map_gauss, though,
1669 * as that may affect the number of constraints.
1670 * This means that we have to call isl_basic_map_gauss at the end
1671 * of the computation to ensure that the basic maps are not left
1672 * in an unexpected state.
1674 struct isl_map *isl_map_coalesce(struct isl_map *map)
1676 int i;
1677 unsigned n;
1678 struct isl_tab **tabs = NULL;
1680 map = isl_map_remove_empty_parts(map);
1681 if (!map)
1682 return NULL;
1684 if (map->n <= 1)
1685 return map;
1687 map = isl_map_sort_divs(map);
1688 map = isl_map_cow(map);
1690 if (!map)
1691 return NULL;
1693 tabs = isl_calloc_array(map->ctx, struct isl_tab *, map->n);
1694 if (!tabs)
1695 goto error;
1697 n = map->n;
1698 for (i = 0; i < map->n; ++i) {
1699 tabs[i] = isl_tab_from_basic_map(map->p[i], 0);
1700 if (!tabs[i])
1701 goto error;
1702 if (!ISL_F_ISSET(map->p[i], ISL_BASIC_MAP_NO_IMPLICIT))
1703 if (isl_tab_detect_implicit_equalities(tabs[i]) < 0)
1704 goto error;
1705 map->p[i] = isl_tab_make_equalities_explicit(tabs[i],
1706 map->p[i]);
1707 if (!map->p[i])
1708 goto error;
1709 if (!ISL_F_ISSET(map->p[i], ISL_BASIC_MAP_NO_REDUNDANT))
1710 if (isl_tab_detect_redundant(tabs[i]) < 0)
1711 goto error;
1713 for (i = map->n - 1; i >= 0; --i)
1714 if (tabs[i]->empty)
1715 drop(map, i, tabs);
1717 map = coalesce(map, tabs);
1719 if (map)
1720 for (i = 0; i < map->n; ++i) {
1721 map->p[i] = isl_basic_map_update_from_tab(map->p[i],
1722 tabs[i]);
1723 map->p[i] = isl_basic_map_gauss(map->p[i], NULL);
1724 map->p[i] = isl_basic_map_finalize(map->p[i]);
1725 if (!map->p[i])
1726 goto error;
1727 ISL_F_SET(map->p[i], ISL_BASIC_MAP_NO_IMPLICIT);
1728 ISL_F_SET(map->p[i], ISL_BASIC_MAP_NO_REDUNDANT);
1731 for (i = 0; i < n; ++i)
1732 isl_tab_free(tabs[i]);
1734 free(tabs);
1736 return map;
1737 error:
1738 if (tabs)
1739 for (i = 0; i < n; ++i)
1740 isl_tab_free(tabs[i]);
1741 free(tabs);
1742 isl_map_free(map);
1743 return NULL;
1746 /* For each pair of basic sets in the set, check if the union of the two
1747 * can be represented by a single basic set.
1748 * If so, replace the pair by the single basic set and start over.
1750 struct isl_set *isl_set_coalesce(struct isl_set *set)
1752 return (struct isl_set *)isl_map_coalesce((struct isl_map *)set);