isl_map_union_disjoint: check spaces before handling special cases
[isl.git] / isl_coalesce.c
blob2899e03b2d320c2713ad7583bdd82c0b154a5375
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 constraint
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.
261 * To see that we are not introducing any extra points, call the
262 * two basic maps A and B and the resulting map U and let x
263 * be an element of U \setminus ( A \cup B ).
264 * Then there is a pair of cut constraints c_1 and c_2 in A and B such that x
265 * violates them. Let X be the intersection of U with the opposites
266 * of these constraints. Then x \in X.
267 * The facet corresponding to c_1 contains the corresponding facet of A.
268 * This facet is entirely contained in B, so c_2 is valid on the facet.
269 * However, since it is also (part of) a facet of X, -c_2 is also valid
270 * on the facet. This means c_2 is saturated on the facet, so c_1 and
271 * c_2 must be opposites of each other, but then x could not violate
272 * both of them.
274 static int check_facets(struct isl_map *map, int i, int j,
275 struct isl_tab **tabs, int *ineq_i, int *ineq_j)
277 int k, l;
278 struct isl_tab_undo *snap;
279 unsigned n_eq = map->p[i]->n_eq;
281 snap = isl_tab_snap(tabs[i]);
283 for (k = 0; k < map->p[i]->n_ineq; ++k) {
284 if (ineq_i[k] != STATUS_CUT)
285 continue;
286 if (isl_tab_select_facet(tabs[i], n_eq + k) < 0)
287 return -1;
288 for (l = 0; l < map->p[j]->n_ineq; ++l) {
289 int stat;
290 if (ineq_j[l] != STATUS_CUT)
291 continue;
292 stat = status_in(map->p[j]->ineq[l], tabs[i]);
293 if (stat != STATUS_VALID)
294 break;
296 if (isl_tab_rollback(tabs[i], snap) < 0)
297 return -1;
298 if (l < map->p[j]->n_ineq)
299 break;
302 if (k < map->p[i]->n_ineq)
303 /* BAD CUT PAIR */
304 return 0;
305 return fuse(map, i, j, tabs, NULL, ineq_i, NULL, ineq_j, NULL);
308 /* Check if basic map "i" contains the basic map represented
309 * by the tableau "tab".
311 static int contains(struct isl_map *map, int i, int *ineq_i,
312 struct isl_tab *tab)
314 int k, l;
315 unsigned dim;
317 dim = isl_basic_map_total_dim(map->p[i]);
318 for (k = 0; k < map->p[i]->n_eq; ++k) {
319 for (l = 0; l < 2; ++l) {
320 int stat;
321 isl_seq_neg(map->p[i]->eq[k], map->p[i]->eq[k], 1+dim);
322 stat = status_in(map->p[i]->eq[k], tab);
323 if (stat != STATUS_VALID)
324 return 0;
328 for (k = 0; k < map->p[i]->n_ineq; ++k) {
329 int stat;
330 if (ineq_i[k] == STATUS_REDUNDANT)
331 continue;
332 stat = status_in(map->p[i]->ineq[k], tab);
333 if (stat != STATUS_VALID)
334 return 0;
336 return 1;
339 /* Basic map "i" has an inequality (say "k") that is adjacent
340 * to some inequality of basic map "j". All the other inequalities
341 * are valid for "j".
342 * Check if basic map "j" forms an extension of basic map "i".
344 * Note that this function is only called if some of the equalities or
345 * inequalities of basic map "j" do cut basic map "i". The function is
346 * correct even if there are no such cut constraints, but in that case
347 * the additional checks performed by this function are overkill.
349 * In particular, we replace constraint k, say f >= 0, by constraint
350 * f <= -1, add the inequalities of "j" that are valid for "i"
351 * and check if the result is a subset of basic map "j".
352 * If so, then we know that this result is exactly equal to basic map "j"
353 * since all its constraints are valid for basic map "j".
354 * By combining the valid constraints of "i" (all equalities and all
355 * inequalities except "k") and the valid constraints of "j" we therefore
356 * obtain a basic map that is equal to their union.
357 * In this case, there is no need to perform a rollback of the tableau
358 * since it is going to be destroyed in fuse().
361 * |\__ |\__
362 * | \__ | \__
363 * | \_ => | \__
364 * |_______| _ |_________\
367 * |\ |\
368 * | \ | \
369 * | \ | \
370 * | | | \
371 * | ||\ => | \
372 * | || \ | \
373 * | || | | |
374 * |__||_/ |_____/
376 static int is_adj_ineq_extension(__isl_keep isl_map *map, int i, int j,
377 struct isl_tab **tabs, int *eq_i, int *ineq_i, int *eq_j, int *ineq_j)
379 int k;
380 struct isl_tab_undo *snap;
381 unsigned n_eq = map->p[i]->n_eq;
382 unsigned total = isl_basic_map_total_dim(map->p[i]);
383 int r;
385 if (isl_tab_extend_cons(tabs[i], 1 + map->p[j]->n_ineq) < 0)
386 return -1;
388 for (k = 0; k < map->p[i]->n_ineq; ++k)
389 if (ineq_i[k] == STATUS_ADJ_INEQ)
390 break;
391 if (k >= map->p[i]->n_ineq)
392 isl_die(isl_map_get_ctx(map), isl_error_internal,
393 "ineq_i should have exactly one STATUS_ADJ_INEQ",
394 return -1);
396 snap = isl_tab_snap(tabs[i]);
398 if (isl_tab_unrestrict(tabs[i], n_eq + k) < 0)
399 return -1;
401 isl_seq_neg(map->p[i]->ineq[k], map->p[i]->ineq[k], 1 + total);
402 isl_int_sub_ui(map->p[i]->ineq[k][0], map->p[i]->ineq[k][0], 1);
403 r = isl_tab_add_ineq(tabs[i], map->p[i]->ineq[k]);
404 isl_seq_neg(map->p[i]->ineq[k], map->p[i]->ineq[k], 1 + total);
405 isl_int_sub_ui(map->p[i]->ineq[k][0], map->p[i]->ineq[k][0], 1);
406 if (r < 0)
407 return -1;
409 for (k = 0; k < map->p[j]->n_ineq; ++k) {
410 if (ineq_j[k] != STATUS_VALID)
411 continue;
412 if (isl_tab_add_ineq(tabs[i], map->p[j]->ineq[k]) < 0)
413 return -1;
416 if (contains(map, j, ineq_j, tabs[i]))
417 return fuse(map, i, j, tabs, eq_i, ineq_i, eq_j, ineq_j, NULL);
419 if (isl_tab_rollback(tabs[i], snap) < 0)
420 return -1;
422 return 0;
426 /* Both basic maps have at least one inequality with and adjacent
427 * (but opposite) inequality in the other basic map.
428 * Check that there are no cut constraints and that there is only
429 * a single pair of adjacent inequalities.
430 * If so, we can replace the pair by a single basic map described
431 * by all but the pair of adjacent inequalities.
432 * Any additional points introduced lie strictly between the two
433 * adjacent hyperplanes and can therefore be integral.
435 * ____ _____
436 * / ||\ / \
437 * / || \ / \
438 * \ || \ => \ \
439 * \ || / \ /
440 * \___||_/ \_____/
442 * The test for a single pair of adjancent inequalities is important
443 * for avoiding the combination of two basic maps like the following
445 * /|
446 * / |
447 * /__|
448 * _____
449 * | |
450 * | |
451 * |___|
453 * If there are some cut constraints on one side, then we may
454 * still be able to fuse the two basic maps, but we need to perform
455 * some additional checks in is_adj_ineq_extension.
457 static int check_adj_ineq(struct isl_map *map, int i, int j,
458 struct isl_tab **tabs, int *eq_i, int *ineq_i, int *eq_j, int *ineq_j)
460 int count_i, count_j;
461 int cut_i, cut_j;
463 count_i = count(ineq_i, map->p[i]->n_ineq, STATUS_ADJ_INEQ);
464 count_j = count(ineq_j, map->p[j]->n_ineq, STATUS_ADJ_INEQ);
466 if (count_i != 1 && count_j != 1)
467 return 0;
469 cut_i = any(eq_i, 2 * map->p[i]->n_eq, STATUS_CUT) ||
470 any(ineq_i, map->p[i]->n_ineq, STATUS_CUT);
471 cut_j = any(eq_j, 2 * map->p[j]->n_eq, STATUS_CUT) ||
472 any(ineq_j, map->p[j]->n_ineq, STATUS_CUT);
474 if (!cut_i && !cut_j && count_i == 1 && count_j == 1)
475 return fuse(map, i, j, tabs, NULL, ineq_i, NULL, ineq_j, NULL);
477 if (count_i == 1 && !cut_i)
478 return is_adj_ineq_extension(map, i, j, tabs,
479 eq_i, ineq_i, eq_j, ineq_j);
481 if (count_j == 1 && !cut_j)
482 return is_adj_ineq_extension(map, j, i, tabs,
483 eq_j, ineq_j, eq_i, ineq_i);
485 return 0;
488 /* Basic map "i" has an inequality "k" that is adjacent to some equality
489 * of basic map "j". All the other inequalities are valid for "j".
490 * Check if basic map "j" forms an extension of basic map "i".
492 * In particular, we relax constraint "k", compute the corresponding
493 * facet and check whether it is included in the other basic map.
494 * If so, we know that relaxing the constraint extends the basic
495 * map with exactly the other basic map (we already know that this
496 * other basic map is included in the extension, because there
497 * were no "cut" inequalities in "i") and we can replace the
498 * two basic maps by this extension.
499 * ____ _____
500 * / || / |
501 * / || / |
502 * \ || => \ |
503 * \ || \ |
504 * \___|| \____|
506 static int is_adj_eq_extension(struct isl_map *map, int i, int j, int k,
507 struct isl_tab **tabs, int *eq_i, int *ineq_i, int *eq_j, int *ineq_j)
509 int changed = 0;
510 int super;
511 struct isl_tab_undo *snap, *snap2;
512 unsigned n_eq = map->p[i]->n_eq;
514 if (isl_tab_is_equality(tabs[i], n_eq + k))
515 return 0;
517 snap = isl_tab_snap(tabs[i]);
518 tabs[i] = isl_tab_relax(tabs[i], n_eq + k);
519 snap2 = isl_tab_snap(tabs[i]);
520 if (isl_tab_select_facet(tabs[i], n_eq + k) < 0)
521 return -1;
522 super = contains(map, j, ineq_j, tabs[i]);
523 if (super) {
524 if (isl_tab_rollback(tabs[i], snap2) < 0)
525 return -1;
526 map->p[i] = isl_basic_map_cow(map->p[i]);
527 if (!map->p[i])
528 return -1;
529 isl_int_add_ui(map->p[i]->ineq[k][0], map->p[i]->ineq[k][0], 1);
530 ISL_F_SET(map->p[i], ISL_BASIC_MAP_FINAL);
531 drop(map, j, tabs);
532 changed = 1;
533 } else
534 if (isl_tab_rollback(tabs[i], snap) < 0)
535 return -1;
537 return changed;
540 /* Data structure that keeps track of the wrapping constraints
541 * and of information to bound the coefficients of those constraints.
543 * bound is set if we want to apply a bound on the coefficients
544 * mat contains the wrapping constraints
545 * max is the bound on the coefficients (if bound is set)
547 struct isl_wraps {
548 int bound;
549 isl_mat *mat;
550 isl_int max;
553 /* Update wraps->max to be greater than or equal to the coefficients
554 * in the equalities and inequalities of bmap that can be removed if we end up
555 * applying wrapping.
557 static void wraps_update_max(struct isl_wraps *wraps,
558 __isl_keep isl_basic_map *bmap, int *eq, int *ineq)
560 int k;
561 isl_int max_k;
562 unsigned total = isl_basic_map_total_dim(bmap);
564 isl_int_init(max_k);
566 for (k = 0; k < bmap->n_eq; ++k) {
567 if (eq[2 * k] == STATUS_VALID &&
568 eq[2 * k + 1] == STATUS_VALID)
569 continue;
570 isl_seq_abs_max(bmap->eq[k] + 1, total, &max_k);
571 if (isl_int_abs_gt(max_k, wraps->max))
572 isl_int_set(wraps->max, max_k);
575 for (k = 0; k < bmap->n_ineq; ++k) {
576 if (ineq[k] == STATUS_VALID || ineq[k] == STATUS_REDUNDANT)
577 continue;
578 isl_seq_abs_max(bmap->ineq[k] + 1, total, &max_k);
579 if (isl_int_abs_gt(max_k, wraps->max))
580 isl_int_set(wraps->max, max_k);
583 isl_int_clear(max_k);
586 /* Initialize the isl_wraps data structure.
587 * If we want to bound the coefficients of the wrapping constraints,
588 * we set wraps->max to the largest coefficient
589 * in the equalities and inequalities that can be removed if we end up
590 * applying wrapping.
592 static void wraps_init(struct isl_wraps *wraps, __isl_take isl_mat *mat,
593 __isl_keep isl_map *map, int i, int j,
594 int *eq_i, int *ineq_i, int *eq_j, int *ineq_j)
596 isl_ctx *ctx;
598 wraps->bound = 0;
599 wraps->mat = mat;
600 if (!mat)
601 return;
602 ctx = isl_mat_get_ctx(mat);
603 wraps->bound = isl_options_get_coalesce_bounded_wrapping(ctx);
604 if (!wraps->bound)
605 return;
606 isl_int_init(wraps->max);
607 isl_int_set_si(wraps->max, 0);
608 wraps_update_max(wraps, map->p[i], eq_i, ineq_i);
609 wraps_update_max(wraps, map->p[j], eq_j, ineq_j);
612 /* Free the contents of the isl_wraps data structure.
614 static void wraps_free(struct isl_wraps *wraps)
616 isl_mat_free(wraps->mat);
617 if (wraps->bound)
618 isl_int_clear(wraps->max);
621 /* Is the wrapping constraint in row "row" allowed?
623 * If wraps->bound is set, we check that none of the coefficients
624 * is greater than wraps->max.
626 static int allow_wrap(struct isl_wraps *wraps, int row)
628 int i;
630 if (!wraps->bound)
631 return 1;
633 for (i = 1; i < wraps->mat->n_col; ++i)
634 if (isl_int_abs_gt(wraps->mat->row[row][i], wraps->max))
635 return 0;
637 return 1;
640 /* For each non-redundant constraint in "bmap" (as determined by "tab"),
641 * wrap the constraint around "bound" such that it includes the whole
642 * set "set" and append the resulting constraint to "wraps".
643 * "wraps" is assumed to have been pre-allocated to the appropriate size.
644 * wraps->n_row is the number of actual wrapped constraints that have
645 * been added.
646 * If any of the wrapping problems results in a constraint that is
647 * identical to "bound", then this means that "set" is unbounded in such
648 * way that no wrapping is possible. If this happens then wraps->n_row
649 * is reset to zero.
650 * Similarly, if we want to bound the coefficients of the wrapping
651 * constraints and a newly added wrapping constraint does not
652 * satisfy the bound, then wraps->n_row is also reset to zero.
654 static int add_wraps(struct isl_wraps *wraps, __isl_keep isl_basic_map *bmap,
655 struct isl_tab *tab, isl_int *bound, __isl_keep isl_set *set)
657 int l;
658 int w;
659 unsigned total = isl_basic_map_total_dim(bmap);
661 w = wraps->mat->n_row;
663 for (l = 0; l < bmap->n_ineq; ++l) {
664 if (isl_seq_is_neg(bound, bmap->ineq[l], 1 + total))
665 continue;
666 if (isl_seq_eq(bound, bmap->ineq[l], 1 + total))
667 continue;
668 if (isl_tab_is_redundant(tab, bmap->n_eq + l))
669 continue;
671 isl_seq_cpy(wraps->mat->row[w], bound, 1 + total);
672 if (!isl_set_wrap_facet(set, wraps->mat->row[w], bmap->ineq[l]))
673 return -1;
674 if (isl_seq_eq(wraps->mat->row[w], bound, 1 + total))
675 goto unbounded;
676 if (!allow_wrap(wraps, w))
677 goto unbounded;
678 ++w;
680 for (l = 0; l < bmap->n_eq; ++l) {
681 if (isl_seq_is_neg(bound, bmap->eq[l], 1 + total))
682 continue;
683 if (isl_seq_eq(bound, bmap->eq[l], 1 + total))
684 continue;
686 isl_seq_cpy(wraps->mat->row[w], bound, 1 + total);
687 isl_seq_neg(wraps->mat->row[w + 1], bmap->eq[l], 1 + total);
688 if (!isl_set_wrap_facet(set, wraps->mat->row[w],
689 wraps->mat->row[w + 1]))
690 return -1;
691 if (isl_seq_eq(wraps->mat->row[w], bound, 1 + total))
692 goto unbounded;
693 if (!allow_wrap(wraps, w))
694 goto unbounded;
695 ++w;
697 isl_seq_cpy(wraps->mat->row[w], bound, 1 + total);
698 if (!isl_set_wrap_facet(set, wraps->mat->row[w], bmap->eq[l]))
699 return -1;
700 if (isl_seq_eq(wraps->mat->row[w], bound, 1 + total))
701 goto unbounded;
702 if (!allow_wrap(wraps, w))
703 goto unbounded;
704 ++w;
707 wraps->mat->n_row = w;
708 return 0;
709 unbounded:
710 wraps->mat->n_row = 0;
711 return 0;
714 /* Check if the constraints in "wraps" from "first" until the last
715 * are all valid for the basic set represented by "tab".
716 * If not, wraps->n_row is set to zero.
718 static int check_wraps(__isl_keep isl_mat *wraps, int first,
719 struct isl_tab *tab)
721 int i;
723 for (i = first; i < wraps->n_row; ++i) {
724 enum isl_ineq_type type;
725 type = isl_tab_ineq_type(tab, wraps->row[i]);
726 if (type == isl_ineq_error)
727 return -1;
728 if (type == isl_ineq_redundant)
729 continue;
730 wraps->n_row = 0;
731 return 0;
734 return 0;
737 /* Return a set that corresponds to the non-redudant constraints
738 * (as recorded in tab) of bmap.
740 * It's important to remove the redundant constraints as some
741 * of the other constraints may have been modified after the
742 * constraints were marked redundant.
743 * In particular, a constraint may have been relaxed.
744 * Redundant constraints are ignored when a constraint is relaxed
745 * and should therefore continue to be ignored ever after.
746 * Otherwise, the relaxation might be thwarted by some of
747 * these constraints.
749 static __isl_give isl_set *set_from_updated_bmap(__isl_keep isl_basic_map *bmap,
750 struct isl_tab *tab)
752 bmap = isl_basic_map_copy(bmap);
753 bmap = isl_basic_map_cow(bmap);
754 bmap = isl_basic_map_update_from_tab(bmap, tab);
755 return isl_set_from_basic_set(isl_basic_map_underlying_set(bmap));
758 /* Given a basic set i with a constraint k that is adjacent to either the
759 * whole of basic set j or a facet of basic set j, check if we can wrap
760 * both the facet corresponding to k and the facet of j (or the whole of j)
761 * around their ridges to include the other set.
762 * If so, replace the pair of basic sets by their union.
764 * All constraints of i (except k) are assumed to be valid for j.
766 * However, the constraints of j may not be valid for i and so
767 * we have to check that the wrapping constraints for j are valid for i.
769 * In the case where j has a facet adjacent to i, tab[j] is assumed
770 * to have been restricted to this facet, so that the non-redundant
771 * constraints in tab[j] are the ridges of the facet.
772 * Note that for the purpose of wrapping, it does not matter whether
773 * we wrap the ridges of i around the whole of j or just around
774 * the facet since all the other constraints are assumed to be valid for j.
775 * In practice, we wrap to include the whole of j.
776 * ____ _____
777 * / | / \
778 * / || / |
779 * \ || => \ |
780 * \ || \ |
781 * \___|| \____|
784 static int can_wrap_in_facet(struct isl_map *map, int i, int j, int k,
785 struct isl_tab **tabs, int *eq_i, int *ineq_i, int *eq_j, int *ineq_j)
787 int changed = 0;
788 struct isl_wraps wraps;
789 isl_mat *mat;
790 struct isl_set *set_i = NULL;
791 struct isl_set *set_j = NULL;
792 struct isl_vec *bound = NULL;
793 unsigned total = isl_basic_map_total_dim(map->p[i]);
794 struct isl_tab_undo *snap;
795 int n;
797 set_i = set_from_updated_bmap(map->p[i], tabs[i]);
798 set_j = set_from_updated_bmap(map->p[j], tabs[j]);
799 mat = isl_mat_alloc(map->ctx, 2 * (map->p[i]->n_eq + map->p[j]->n_eq) +
800 map->p[i]->n_ineq + map->p[j]->n_ineq,
801 1 + total);
802 wraps_init(&wraps, mat, map, i, j, eq_i, ineq_i, eq_j, ineq_j);
803 bound = isl_vec_alloc(map->ctx, 1 + total);
804 if (!set_i || !set_j || !wraps.mat || !bound)
805 goto error;
807 isl_seq_cpy(bound->el, map->p[i]->ineq[k], 1 + total);
808 isl_int_add_ui(bound->el[0], bound->el[0], 1);
810 isl_seq_cpy(wraps.mat->row[0], bound->el, 1 + total);
811 wraps.mat->n_row = 1;
813 if (add_wraps(&wraps, map->p[j], tabs[j], bound->el, set_i) < 0)
814 goto error;
815 if (!wraps.mat->n_row)
816 goto unbounded;
818 snap = isl_tab_snap(tabs[i]);
820 if (isl_tab_select_facet(tabs[i], map->p[i]->n_eq + k) < 0)
821 goto error;
822 if (isl_tab_detect_redundant(tabs[i]) < 0)
823 goto error;
825 isl_seq_neg(bound->el, map->p[i]->ineq[k], 1 + total);
827 n = wraps.mat->n_row;
828 if (add_wraps(&wraps, map->p[i], tabs[i], bound->el, set_j) < 0)
829 goto error;
831 if (isl_tab_rollback(tabs[i], snap) < 0)
832 goto error;
833 if (check_wraps(wraps.mat, n, tabs[i]) < 0)
834 goto error;
835 if (!wraps.mat->n_row)
836 goto unbounded;
838 changed = fuse(map, i, j, tabs, eq_i, ineq_i, eq_j, ineq_j, wraps.mat);
840 unbounded:
841 wraps_free(&wraps);
843 isl_set_free(set_i);
844 isl_set_free(set_j);
846 isl_vec_free(bound);
848 return changed;
849 error:
850 wraps_free(&wraps);
851 isl_vec_free(bound);
852 isl_set_free(set_i);
853 isl_set_free(set_j);
854 return -1;
857 /* Set the is_redundant property of the "n" constraints in "cuts",
858 * except "k" to "v".
859 * This is a fairly tricky operation as it bypasses isl_tab.c.
860 * The reason we want to temporarily mark some constraints redundant
861 * is that we want to ignore them in add_wraps.
863 * Initially all cut constraints are non-redundant, but the
864 * selection of a facet right before the call to this function
865 * may have made some of them redundant.
866 * Likewise, the same constraints are marked non-redundant
867 * in the second call to this function, before they are officially
868 * made non-redundant again in the subsequent rollback.
870 static void set_is_redundant(struct isl_tab *tab, unsigned n_eq,
871 int *cuts, int n, int k, int v)
873 int l;
875 for (l = 0; l < n; ++l) {
876 if (l == k)
877 continue;
878 tab->con[n_eq + cuts[l]].is_redundant = v;
882 /* Given a pair of basic maps i and j such that j sticks out
883 * of i at n cut constraints, each time by at most one,
884 * try to compute wrapping constraints and replace the two
885 * basic maps by a single basic map.
886 * The other constraints of i are assumed to be valid for j.
888 * The facets of i corresponding to the cut constraints are
889 * wrapped around their ridges, except those ridges determined
890 * by any of the other cut constraints.
891 * The intersections of cut constraints need to be ignored
892 * as the result of wrapping one cut constraint around another
893 * would result in a constraint cutting the union.
894 * In each case, the facets are wrapped to include the union
895 * of the two basic maps.
897 * The pieces of j that lie at an offset of exactly one from
898 * one of the cut constraints of i are wrapped around their edges.
899 * Here, there is no need to ignore intersections because we
900 * are wrapping around the union of the two basic maps.
902 * If any wrapping fails, i.e., if we cannot wrap to touch
903 * the union, then we give up.
904 * Otherwise, the pair of basic maps is replaced by their union.
906 static int wrap_in_facets(struct isl_map *map, int i, int j,
907 int *cuts, int n, struct isl_tab **tabs,
908 int *eq_i, int *ineq_i, int *eq_j, int *ineq_j)
910 int changed = 0;
911 struct isl_wraps wraps;
912 isl_mat *mat;
913 isl_set *set = NULL;
914 isl_vec *bound = NULL;
915 unsigned total = isl_basic_map_total_dim(map->p[i]);
916 int max_wrap;
917 int k;
918 struct isl_tab_undo *snap_i, *snap_j;
920 if (isl_tab_extend_cons(tabs[j], 1) < 0)
921 goto error;
923 max_wrap = 2 * (map->p[i]->n_eq + map->p[j]->n_eq) +
924 map->p[i]->n_ineq + map->p[j]->n_ineq;
925 max_wrap *= n;
927 set = isl_set_union(set_from_updated_bmap(map->p[i], tabs[i]),
928 set_from_updated_bmap(map->p[j], tabs[j]));
929 mat = isl_mat_alloc(map->ctx, max_wrap, 1 + total);
930 wraps_init(&wraps, mat, map, i, j, eq_i, ineq_i, eq_j, ineq_j);
931 bound = isl_vec_alloc(map->ctx, 1 + total);
932 if (!set || !wraps.mat || !bound)
933 goto error;
935 snap_i = isl_tab_snap(tabs[i]);
936 snap_j = isl_tab_snap(tabs[j]);
938 wraps.mat->n_row = 0;
940 for (k = 0; k < n; ++k) {
941 if (isl_tab_select_facet(tabs[i], map->p[i]->n_eq + cuts[k]) < 0)
942 goto error;
943 if (isl_tab_detect_redundant(tabs[i]) < 0)
944 goto error;
945 set_is_redundant(tabs[i], map->p[i]->n_eq, cuts, n, k, 1);
947 isl_seq_neg(bound->el, map->p[i]->ineq[cuts[k]], 1 + total);
948 if (!tabs[i]->empty &&
949 add_wraps(&wraps, map->p[i], tabs[i], bound->el, set) < 0)
950 goto error;
952 set_is_redundant(tabs[i], map->p[i]->n_eq, cuts, n, k, 0);
953 if (isl_tab_rollback(tabs[i], snap_i) < 0)
954 goto error;
956 if (tabs[i]->empty)
957 break;
958 if (!wraps.mat->n_row)
959 break;
961 isl_seq_cpy(bound->el, map->p[i]->ineq[cuts[k]], 1 + total);
962 isl_int_add_ui(bound->el[0], bound->el[0], 1);
963 if (isl_tab_add_eq(tabs[j], bound->el) < 0)
964 goto error;
965 if (isl_tab_detect_redundant(tabs[j]) < 0)
966 goto error;
968 if (!tabs[j]->empty &&
969 add_wraps(&wraps, map->p[j], tabs[j], bound->el, set) < 0)
970 goto error;
972 if (isl_tab_rollback(tabs[j], snap_j) < 0)
973 goto error;
975 if (!wraps.mat->n_row)
976 break;
979 if (k == n)
980 changed = fuse(map, i, j, tabs,
981 eq_i, ineq_i, eq_j, ineq_j, wraps.mat);
983 isl_vec_free(bound);
984 wraps_free(&wraps);
985 isl_set_free(set);
987 return changed;
988 error:
989 isl_vec_free(bound);
990 wraps_free(&wraps);
991 isl_set_free(set);
992 return -1;
995 /* Given two basic sets i and j such that i has no cut equalities,
996 * check if relaxing all the cut inequalities of i by one turns
997 * them into valid constraint for j and check if we can wrap in
998 * the bits that are sticking out.
999 * If so, replace the pair by their union.
1001 * We first check if all relaxed cut inequalities of i are valid for j
1002 * and then try to wrap in the intersections of the relaxed cut inequalities
1003 * with j.
1005 * During this wrapping, we consider the points of j that lie at a distance
1006 * of exactly 1 from i. In particular, we ignore the points that lie in
1007 * between this lower-dimensional space and the basic map i.
1008 * We can therefore only apply this to integer maps.
1009 * ____ _____
1010 * / ___|_ / \
1011 * / | | / |
1012 * \ | | => \ |
1013 * \|____| \ |
1014 * \___| \____/
1016 * _____ ______
1017 * | ____|_ | \
1018 * | | | | |
1019 * | | | => | |
1020 * |_| | | |
1021 * |_____| \______|
1023 * _______
1024 * | |
1025 * | |\ |
1026 * | | \ |
1027 * | | \ |
1028 * | | \|
1029 * | | \
1030 * | |_____\
1031 * | |
1032 * |_______|
1034 * Wrapping can fail if the result of wrapping one of the facets
1035 * around its edges does not produce any new facet constraint.
1036 * In particular, this happens when we try to wrap in unbounded sets.
1038 * _______________________________________________________________________
1040 * | ___
1041 * | | |
1042 * |_| |_________________________________________________________________
1043 * |___|
1045 * The following is not an acceptable result of coalescing the above two
1046 * sets as it includes extra integer points.
1047 * _______________________________________________________________________
1049 * |
1050 * |
1052 * \______________________________________________________________________
1054 static int can_wrap_in_set(struct isl_map *map, int i, int j,
1055 struct isl_tab **tabs, int *eq_i, int *ineq_i, int *eq_j, int *ineq_j)
1057 int changed = 0;
1058 int k, m;
1059 int n;
1060 int *cuts = NULL;
1062 if (ISL_F_ISSET(map->p[i], ISL_BASIC_MAP_RATIONAL) ||
1063 ISL_F_ISSET(map->p[j], ISL_BASIC_MAP_RATIONAL))
1064 return 0;
1066 n = count(ineq_i, map->p[i]->n_ineq, STATUS_CUT);
1067 if (n == 0)
1068 return 0;
1070 cuts = isl_alloc_array(map->ctx, int, n);
1071 if (!cuts)
1072 return -1;
1074 for (k = 0, m = 0; m < n; ++k) {
1075 enum isl_ineq_type type;
1077 if (ineq_i[k] != STATUS_CUT)
1078 continue;
1080 isl_int_add_ui(map->p[i]->ineq[k][0], map->p[i]->ineq[k][0], 1);
1081 type = isl_tab_ineq_type(tabs[j], map->p[i]->ineq[k]);
1082 isl_int_sub_ui(map->p[i]->ineq[k][0], map->p[i]->ineq[k][0], 1);
1083 if (type == isl_ineq_error)
1084 goto error;
1085 if (type != isl_ineq_redundant)
1086 break;
1087 cuts[m] = k;
1088 ++m;
1091 if (m == n)
1092 changed = wrap_in_facets(map, i, j, cuts, n, tabs,
1093 eq_i, ineq_i, eq_j, ineq_j);
1095 free(cuts);
1097 return changed;
1098 error:
1099 free(cuts);
1100 return -1;
1103 /* Check if either i or j has a single cut constraint that can
1104 * be used to wrap in (a facet of) the other basic set.
1105 * if so, replace the pair by their union.
1107 static int check_wrap(struct isl_map *map, int i, int j,
1108 struct isl_tab **tabs, int *eq_i, int *ineq_i, int *eq_j, int *ineq_j)
1110 int changed = 0;
1112 if (!any(eq_i, 2 * map->p[i]->n_eq, STATUS_CUT))
1113 changed = can_wrap_in_set(map, i, j, tabs,
1114 eq_i, ineq_i, eq_j, ineq_j);
1115 if (changed)
1116 return changed;
1118 if (!any(eq_j, 2 * map->p[j]->n_eq, STATUS_CUT))
1119 changed = can_wrap_in_set(map, j, i, tabs,
1120 eq_j, ineq_j, eq_i, ineq_i);
1121 return changed;
1124 /* At least one of the basic maps has an equality that is adjacent
1125 * to inequality. Make sure that only one of the basic maps has
1126 * such an equality and that the other basic map has exactly one
1127 * inequality adjacent to an equality.
1128 * We call the basic map that has the inequality "i" and the basic
1129 * map that has the equality "j".
1130 * If "i" has any "cut" (in)equality, then relaxing the inequality
1131 * by one would not result in a basic map that contains the other
1132 * basic map.
1134 static int check_adj_eq(struct isl_map *map, int i, int j,
1135 struct isl_tab **tabs, int *eq_i, int *ineq_i, int *eq_j, int *ineq_j)
1137 int changed = 0;
1138 int k;
1140 if (any(eq_i, 2 * map->p[i]->n_eq, STATUS_ADJ_INEQ) &&
1141 any(eq_j, 2 * map->p[j]->n_eq, STATUS_ADJ_INEQ))
1142 /* ADJ EQ TOO MANY */
1143 return 0;
1145 if (any(eq_i, 2 * map->p[i]->n_eq, STATUS_ADJ_INEQ))
1146 return check_adj_eq(map, j, i, tabs,
1147 eq_j, ineq_j, eq_i, ineq_i);
1149 /* j has an equality adjacent to an inequality in i */
1151 if (any(eq_i, 2 * map->p[i]->n_eq, STATUS_CUT))
1152 return 0;
1153 if (any(ineq_i, map->p[i]->n_ineq, STATUS_CUT))
1154 /* ADJ EQ CUT */
1155 return 0;
1156 if (count(ineq_i, map->p[i]->n_ineq, STATUS_ADJ_EQ) != 1 ||
1157 any(ineq_j, map->p[j]->n_ineq, STATUS_ADJ_EQ) ||
1158 any(ineq_i, map->p[i]->n_ineq, STATUS_ADJ_INEQ) ||
1159 any(ineq_j, map->p[j]->n_ineq, STATUS_ADJ_INEQ))
1160 /* ADJ EQ TOO MANY */
1161 return 0;
1163 for (k = 0; k < map->p[i]->n_ineq; ++k)
1164 if (ineq_i[k] == STATUS_ADJ_EQ)
1165 break;
1167 changed = is_adj_eq_extension(map, i, j, k, tabs,
1168 eq_i, ineq_i, eq_j, ineq_j);
1169 if (changed)
1170 return changed;
1172 if (count(eq_j, 2 * map->p[j]->n_eq, STATUS_ADJ_INEQ) != 1)
1173 return 0;
1175 changed = can_wrap_in_facet(map, i, j, k, tabs, eq_i, ineq_i, eq_j, ineq_j);
1177 return changed;
1180 /* The two basic maps lie on adjacent hyperplanes. In particular,
1181 * basic map "i" has an equality that lies parallel to basic map "j".
1182 * Check if we can wrap the facets around the parallel hyperplanes
1183 * to include the other set.
1185 * We perform basically the same operations as can_wrap_in_facet,
1186 * except that we don't need to select a facet of one of the sets.
1188 * \\ \\
1189 * \\ => \\
1190 * \ \|
1192 * We only allow one equality of "i" to be adjacent to an equality of "j"
1193 * to avoid coalescing
1195 * [m, n] -> { [x, y] -> [x, 1 + y] : x >= 1 and y >= 1 and
1196 * x <= 10 and y <= 10;
1197 * [x, y] -> [1 + x, y] : x >= 1 and x <= 20 and
1198 * y >= 5 and y <= 15 }
1200 * to
1202 * [m, n] -> { [x, y] -> [x2, y2] : x >= 1 and 10y2 <= 20 - x + 10y and
1203 * 4y2 >= 5 + 3y and 5y2 <= 15 + 4y and
1204 * y2 <= 1 + x + y - x2 and y2 >= y and
1205 * y2 >= 1 + x + y - x2 }
1207 static int check_eq_adj_eq(struct isl_map *map, int i, int j,
1208 struct isl_tab **tabs, int *eq_i, int *ineq_i, int *eq_j, int *ineq_j)
1210 int k;
1211 int changed = 0;
1212 struct isl_wraps wraps;
1213 isl_mat *mat;
1214 struct isl_set *set_i = NULL;
1215 struct isl_set *set_j = NULL;
1216 struct isl_vec *bound = NULL;
1217 unsigned total = isl_basic_map_total_dim(map->p[i]);
1219 if (count(eq_i, 2 * map->p[i]->n_eq, STATUS_ADJ_EQ) != 1)
1220 return 0;
1222 for (k = 0; k < 2 * map->p[i]->n_eq ; ++k)
1223 if (eq_i[k] == STATUS_ADJ_EQ)
1224 break;
1226 set_i = set_from_updated_bmap(map->p[i], tabs[i]);
1227 set_j = set_from_updated_bmap(map->p[j], tabs[j]);
1228 mat = isl_mat_alloc(map->ctx, 2 * (map->p[i]->n_eq + map->p[j]->n_eq) +
1229 map->p[i]->n_ineq + map->p[j]->n_ineq,
1230 1 + total);
1231 wraps_init(&wraps, mat, map, i, j, eq_i, ineq_i, eq_j, ineq_j);
1232 bound = isl_vec_alloc(map->ctx, 1 + total);
1233 if (!set_i || !set_j || !wraps.mat || !bound)
1234 goto error;
1236 if (k % 2 == 0)
1237 isl_seq_neg(bound->el, map->p[i]->eq[k / 2], 1 + total);
1238 else
1239 isl_seq_cpy(bound->el, map->p[i]->eq[k / 2], 1 + total);
1240 isl_int_add_ui(bound->el[0], bound->el[0], 1);
1242 isl_seq_cpy(wraps.mat->row[0], bound->el, 1 + total);
1243 wraps.mat->n_row = 1;
1245 if (add_wraps(&wraps, map->p[j], tabs[j], bound->el, set_i) < 0)
1246 goto error;
1247 if (!wraps.mat->n_row)
1248 goto unbounded;
1250 isl_int_sub_ui(bound->el[0], bound->el[0], 1);
1251 isl_seq_neg(bound->el, bound->el, 1 + total);
1253 isl_seq_cpy(wraps.mat->row[wraps.mat->n_row], bound->el, 1 + total);
1254 wraps.mat->n_row++;
1256 if (add_wraps(&wraps, map->p[i], tabs[i], bound->el, set_j) < 0)
1257 goto error;
1258 if (!wraps.mat->n_row)
1259 goto unbounded;
1261 changed = fuse(map, i, j, tabs, eq_i, ineq_i, eq_j, ineq_j, wraps.mat);
1263 if (0) {
1264 error: changed = -1;
1266 unbounded:
1268 wraps_free(&wraps);
1269 isl_set_free(set_i);
1270 isl_set_free(set_j);
1271 isl_vec_free(bound);
1273 return changed;
1276 /* Check if the union of the given pair of basic maps
1277 * can be represented by a single basic map.
1278 * If so, replace the pair by the single basic map and return 1.
1279 * Otherwise, return 0;
1280 * The two basic maps are assumed to live in the same local space.
1282 * We first check the effect of each constraint of one basic map
1283 * on the other basic map.
1284 * The constraint may be
1285 * redundant the constraint is redundant in its own
1286 * basic map and should be ignore and removed
1287 * in the end
1288 * valid all (integer) points of the other basic map
1289 * satisfy the constraint
1290 * separate no (integer) point of the other basic map
1291 * satisfies the constraint
1292 * cut some but not all points of the other basic map
1293 * satisfy the constraint
1294 * adj_eq the given constraint is adjacent (on the outside)
1295 * to an equality of the other basic map
1296 * adj_ineq the given constraint is adjacent (on the outside)
1297 * to an inequality of the other basic map
1299 * We consider seven cases in which we can replace the pair by a single
1300 * basic map. We ignore all "redundant" constraints.
1302 * 1. all constraints of one basic map are valid
1303 * => the other basic map is a subset and can be removed
1305 * 2. all constraints of both basic maps are either "valid" or "cut"
1306 * and the facets corresponding to the "cut" constraints
1307 * of one of the basic maps lies entirely inside the other basic map
1308 * => the pair can be replaced by a basic map consisting
1309 * of the valid constraints in both basic maps
1311 * 3. there is a single pair of adjacent inequalities
1312 * (all other constraints are "valid")
1313 * => the pair can be replaced by a basic map consisting
1314 * of the valid constraints in both basic maps
1316 * 4. one basic map has a single adjacent inequality, while the other
1317 * constraints are "valid". The other basic map has some
1318 * "cut" constraints, but replacing the adjacent inequality by
1319 * its opposite and adding the valid constraints of the other
1320 * basic map results in a subset of the other basic map
1321 * => the pair can be replaced by a basic map consisting
1322 * of the valid constraints in both basic maps
1324 * 5. there is a single adjacent pair of an inequality and an equality,
1325 * the other constraints of the basic map containing the inequality are
1326 * "valid". Moreover, if the inequality the basic map is relaxed
1327 * and then turned into an equality, then resulting facet lies
1328 * entirely inside the other basic map
1329 * => the pair can be replaced by the basic map containing
1330 * the inequality, with the inequality relaxed.
1332 * 6. 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, the facets corresponding to both
1335 * the inequality and the equality can be wrapped around their
1336 * ridges to include the other basic map
1337 * => the pair can be replaced by a basic map consisting
1338 * of the valid constraints in both basic maps together
1339 * with all wrapping constraints
1341 * 7. one of the basic maps extends beyond the other by at most one.
1342 * Moreover, the facets corresponding to the cut constraints and
1343 * the pieces of the other basic map at offset one from these cut
1344 * constraints can be wrapped around their ridges to include
1345 * the union of the two basic maps
1346 * => the pair can be replaced by a basic map consisting
1347 * of the valid constraints in both basic maps together
1348 * with all wrapping constraints
1350 * 8. the two basic maps live in adjacent hyperplanes. In principle
1351 * such sets can always be combined through wrapping, but we impose
1352 * that there is only one such pair, to avoid overeager coalescing.
1354 * Throughout the computation, we maintain a collection of tableaus
1355 * corresponding to the basic maps. When the basic maps are dropped
1356 * or combined, the tableaus are modified accordingly.
1358 static int coalesce_local_pair(__isl_keep isl_map *map, int i, int j,
1359 struct isl_tab **tabs)
1361 int changed = 0;
1362 int *eq_i = NULL;
1363 int *eq_j = NULL;
1364 int *ineq_i = NULL;
1365 int *ineq_j = NULL;
1367 eq_i = eq_status_in(map->p[i], tabs[j]);
1368 if (map->p[i]->n_eq && !eq_i)
1369 goto error;
1370 if (any(eq_i, 2 * map->p[i]->n_eq, STATUS_ERROR))
1371 goto error;
1372 if (any(eq_i, 2 * map->p[i]->n_eq, STATUS_SEPARATE))
1373 goto done;
1375 eq_j = eq_status_in(map->p[j], tabs[i]);
1376 if (map->p[j]->n_eq && !eq_j)
1377 goto error;
1378 if (any(eq_j, 2 * map->p[j]->n_eq, STATUS_ERROR))
1379 goto error;
1380 if (any(eq_j, 2 * map->p[j]->n_eq, STATUS_SEPARATE))
1381 goto done;
1383 ineq_i = ineq_status_in(map->p[i], tabs[i], tabs[j]);
1384 if (map->p[i]->n_ineq && !ineq_i)
1385 goto error;
1386 if (any(ineq_i, map->p[i]->n_ineq, STATUS_ERROR))
1387 goto error;
1388 if (any(ineq_i, map->p[i]->n_ineq, STATUS_SEPARATE))
1389 goto done;
1391 ineq_j = ineq_status_in(map->p[j], tabs[j], tabs[i]);
1392 if (map->p[j]->n_ineq && !ineq_j)
1393 goto error;
1394 if (any(ineq_j, map->p[j]->n_ineq, STATUS_ERROR))
1395 goto error;
1396 if (any(ineq_j, map->p[j]->n_ineq, STATUS_SEPARATE))
1397 goto done;
1399 if (all(eq_i, 2 * map->p[i]->n_eq, STATUS_VALID) &&
1400 all(ineq_i, map->p[i]->n_ineq, STATUS_VALID)) {
1401 drop(map, j, tabs);
1402 changed = 1;
1403 } else if (all(eq_j, 2 * map->p[j]->n_eq, STATUS_VALID) &&
1404 all(ineq_j, map->p[j]->n_ineq, STATUS_VALID)) {
1405 drop(map, i, tabs);
1406 changed = 1;
1407 } else if (any(eq_i, 2 * map->p[i]->n_eq, STATUS_ADJ_EQ)) {
1408 changed = check_eq_adj_eq(map, i, j, tabs,
1409 eq_i, ineq_i, eq_j, ineq_j);
1410 } else if (any(eq_j, 2 * map->p[j]->n_eq, STATUS_ADJ_EQ)) {
1411 changed = check_eq_adj_eq(map, j, i, tabs,
1412 eq_j, ineq_j, eq_i, ineq_i);
1413 } else if (any(eq_i, 2 * map->p[i]->n_eq, STATUS_ADJ_INEQ) ||
1414 any(eq_j, 2 * map->p[j]->n_eq, STATUS_ADJ_INEQ)) {
1415 changed = check_adj_eq(map, i, j, tabs,
1416 eq_i, ineq_i, eq_j, ineq_j);
1417 } else if (any(ineq_i, map->p[i]->n_ineq, STATUS_ADJ_EQ) ||
1418 any(ineq_j, map->p[j]->n_ineq, STATUS_ADJ_EQ)) {
1419 /* Can't happen */
1420 /* BAD ADJ INEQ */
1421 } else if (any(ineq_i, map->p[i]->n_ineq, STATUS_ADJ_INEQ) ||
1422 any(ineq_j, map->p[j]->n_ineq, STATUS_ADJ_INEQ)) {
1423 changed = check_adj_ineq(map, i, j, tabs,
1424 eq_i, ineq_i, eq_j, ineq_j);
1425 } else {
1426 if (!any(eq_i, 2 * map->p[i]->n_eq, STATUS_CUT) &&
1427 !any(eq_j, 2 * map->p[j]->n_eq, STATUS_CUT))
1428 changed = check_facets(map, i, j, tabs, ineq_i, ineq_j);
1429 if (!changed)
1430 changed = check_wrap(map, i, j, tabs,
1431 eq_i, ineq_i, eq_j, ineq_j);
1434 done:
1435 free(eq_i);
1436 free(eq_j);
1437 free(ineq_i);
1438 free(ineq_j);
1439 return changed;
1440 error:
1441 free(eq_i);
1442 free(eq_j);
1443 free(ineq_i);
1444 free(ineq_j);
1445 return -1;
1448 /* Do the two basic maps live in the same local space, i.e.,
1449 * do they have the same (known) divs?
1450 * If either basic map has any unknown divs, then we can only assume
1451 * that they do not live in the same local space.
1453 static int same_divs(__isl_keep isl_basic_map *bmap1,
1454 __isl_keep isl_basic_map *bmap2)
1456 int i;
1457 int known;
1458 int total;
1460 if (!bmap1 || !bmap2)
1461 return -1;
1462 if (bmap1->n_div != bmap2->n_div)
1463 return 0;
1465 if (bmap1->n_div == 0)
1466 return 1;
1468 known = isl_basic_map_divs_known(bmap1);
1469 if (known < 0 || !known)
1470 return known;
1471 known = isl_basic_map_divs_known(bmap2);
1472 if (known < 0 || !known)
1473 return known;
1475 total = isl_basic_map_total_dim(bmap1);
1476 for (i = 0; i < bmap1->n_div; ++i)
1477 if (!isl_seq_eq(bmap1->div[i], bmap2->div[i], 2 + total))
1478 return 0;
1480 return 1;
1483 /* Given two basic maps "i" and "j", where the divs of "i" form a subset
1484 * of those of "j", check if basic map "j" is a subset of basic map "i"
1485 * and, if so, drop basic map "j".
1487 * We first expand the divs of basic map "i" to match those of basic map "j",
1488 * using the divs and expansion computed by the caller.
1489 * Then we check if all constraints of the expanded "i" are valid for "j".
1491 static int coalesce_subset(__isl_keep isl_map *map, int i, int j,
1492 struct isl_tab **tabs, __isl_keep isl_mat *div, int *exp)
1494 isl_basic_map *bmap;
1495 int changed = 0;
1496 int *eq_i = NULL;
1497 int *ineq_i = NULL;
1499 bmap = isl_basic_map_copy(map->p[i]);
1500 bmap = isl_basic_set_expand_divs(bmap, isl_mat_copy(div), exp);
1502 if (!bmap)
1503 goto error;
1505 eq_i = eq_status_in(bmap, tabs[j]);
1506 if (bmap->n_eq && !eq_i)
1507 goto error;
1508 if (any(eq_i, 2 * bmap->n_eq, STATUS_ERROR))
1509 goto error;
1510 if (any(eq_i, 2 * bmap->n_eq, STATUS_SEPARATE))
1511 goto done;
1513 ineq_i = ineq_status_in(bmap, NULL, tabs[j]);
1514 if (bmap->n_ineq && !ineq_i)
1515 goto error;
1516 if (any(ineq_i, bmap->n_ineq, STATUS_ERROR))
1517 goto error;
1518 if (any(ineq_i, bmap->n_ineq, STATUS_SEPARATE))
1519 goto done;
1521 if (all(eq_i, 2 * map->p[i]->n_eq, STATUS_VALID) &&
1522 all(ineq_i, map->p[i]->n_ineq, STATUS_VALID)) {
1523 drop(map, j, tabs);
1524 changed = 1;
1527 done:
1528 isl_basic_map_free(bmap);
1529 free(eq_i);
1530 free(ineq_i);
1531 return 0;
1532 error:
1533 isl_basic_map_free(bmap);
1534 free(eq_i);
1535 free(ineq_i);
1536 return -1;
1539 /* Check if the basic map "j" is a subset of basic map "i",
1540 * assuming that "i" has fewer divs that "j".
1541 * If not, then we change the order.
1543 * If the two basic maps have the same number of divs, then
1544 * they must necessarily be different. Otherwise, we would have
1545 * called coalesce_local_pair. We therefore don't try anything
1546 * in this case.
1548 * We first check if the divs of "i" are all known and form a subset
1549 * of those of "j". If so, we pass control over to coalesce_subset.
1551 static int check_coalesce_subset(__isl_keep isl_map *map, int i, int j,
1552 struct isl_tab **tabs)
1554 int known;
1555 isl_mat *div_i, *div_j, *div;
1556 int *exp1 = NULL;
1557 int *exp2 = NULL;
1558 isl_ctx *ctx;
1559 int subset;
1561 if (map->p[i]->n_div == map->p[j]->n_div)
1562 return 0;
1563 if (map->p[j]->n_div < map->p[i]->n_div)
1564 return check_coalesce_subset(map, j, i, tabs);
1566 known = isl_basic_map_divs_known(map->p[i]);
1567 if (known < 0 || !known)
1568 return known;
1570 ctx = isl_map_get_ctx(map);
1572 div_i = isl_basic_map_get_divs(map->p[i]);
1573 div_j = isl_basic_map_get_divs(map->p[j]);
1575 if (!div_i || !div_j)
1576 goto error;
1578 exp1 = isl_alloc_array(ctx, int, div_i->n_row);
1579 exp2 = isl_alloc_array(ctx, int, div_j->n_row);
1580 if ((div_i->n_row && !exp1) || (div_j->n_row && !exp2))
1581 goto error;
1583 div = isl_merge_divs(div_i, div_j, exp1, exp2);
1584 if (!div)
1585 goto error;
1587 if (div->n_row == div_j->n_row)
1588 subset = coalesce_subset(map, i, j, tabs, div, exp1);
1589 else
1590 subset = 0;
1592 isl_mat_free(div);
1594 isl_mat_free(div_i);
1595 isl_mat_free(div_j);
1597 free(exp2);
1598 free(exp1);
1600 return subset;
1601 error:
1602 isl_mat_free(div_i);
1603 isl_mat_free(div_j);
1604 free(exp1);
1605 free(exp2);
1606 return -1;
1609 /* Check if the union of the given pair of basic maps
1610 * can be represented by a single basic map.
1611 * If so, replace the pair by the single basic map and return 1.
1612 * Otherwise, return 0;
1614 * We first check if the two basic maps live in the same local space.
1615 * If so, we do the complete check. Otherwise, we check if one is
1616 * an obvious subset of the other.
1618 static int coalesce_pair(__isl_keep isl_map *map, int i, int j,
1619 struct isl_tab **tabs)
1621 int same;
1623 same = same_divs(map->p[i], map->p[j]);
1624 if (same < 0)
1625 return -1;
1626 if (same)
1627 return coalesce_local_pair(map, i, j, tabs);
1629 return check_coalesce_subset(map, i, j, tabs);
1632 static struct isl_map *coalesce(struct isl_map *map, struct isl_tab **tabs)
1634 int i, j;
1636 for (i = map->n - 2; i >= 0; --i)
1637 restart:
1638 for (j = i + 1; j < map->n; ++j) {
1639 int changed;
1640 changed = coalesce_pair(map, i, j, tabs);
1641 if (changed < 0)
1642 goto error;
1643 if (changed)
1644 goto restart;
1646 return map;
1647 error:
1648 isl_map_free(map);
1649 return NULL;
1652 /* For each pair of basic maps in the map, check if the union of the two
1653 * can be represented by a single basic map.
1654 * If so, replace the pair by the single basic map and start over.
1656 * Since we are constructing the tableaus of the basic maps anyway,
1657 * we exploit them to detect implicit equalities and redundant constraints.
1658 * This also helps the coalescing as it can ignore the redundant constraints.
1659 * In order to avoid confusion, we make all implicit equalities explicit
1660 * in the basic maps. We don't call isl_basic_map_gauss, though,
1661 * as that may affect the number of constraints.
1662 * This means that we have to call isl_basic_map_gauss at the end
1663 * of the computation to ensure that the basic maps are not left
1664 * in an unexpected state.
1666 struct isl_map *isl_map_coalesce(struct isl_map *map)
1668 int i;
1669 unsigned n;
1670 struct isl_tab **tabs = NULL;
1672 map = isl_map_remove_empty_parts(map);
1673 if (!map)
1674 return NULL;
1676 if (map->n <= 1)
1677 return map;
1679 map = isl_map_sort_divs(map);
1680 map = isl_map_cow(map);
1682 if (!map)
1683 return NULL;
1685 tabs = isl_calloc_array(map->ctx, struct isl_tab *, map->n);
1686 if (!tabs)
1687 goto error;
1689 n = map->n;
1690 for (i = 0; i < map->n; ++i) {
1691 tabs[i] = isl_tab_from_basic_map(map->p[i], 0);
1692 if (!tabs[i])
1693 goto error;
1694 if (!ISL_F_ISSET(map->p[i], ISL_BASIC_MAP_NO_IMPLICIT))
1695 if (isl_tab_detect_implicit_equalities(tabs[i]) < 0)
1696 goto error;
1697 map->p[i] = isl_tab_make_equalities_explicit(tabs[i],
1698 map->p[i]);
1699 if (!map->p[i])
1700 goto error;
1701 if (!ISL_F_ISSET(map->p[i], ISL_BASIC_MAP_NO_REDUNDANT))
1702 if (isl_tab_detect_redundant(tabs[i]) < 0)
1703 goto error;
1705 for (i = map->n - 1; i >= 0; --i)
1706 if (tabs[i]->empty)
1707 drop(map, i, tabs);
1709 map = coalesce(map, tabs);
1711 if (map)
1712 for (i = 0; i < map->n; ++i) {
1713 map->p[i] = isl_basic_map_update_from_tab(map->p[i],
1714 tabs[i]);
1715 map->p[i] = isl_basic_map_gauss(map->p[i], NULL);
1716 map->p[i] = isl_basic_map_finalize(map->p[i]);
1717 if (!map->p[i])
1718 goto error;
1719 ISL_F_SET(map->p[i], ISL_BASIC_MAP_NO_IMPLICIT);
1720 ISL_F_SET(map->p[i], ISL_BASIC_MAP_NO_REDUNDANT);
1723 for (i = 0; i < n; ++i)
1724 isl_tab_free(tabs[i]);
1726 free(tabs);
1728 return map;
1729 error:
1730 if (tabs)
1731 for (i = 0; i < n; ++i)
1732 isl_tab_free(tabs[i]);
1733 free(tabs);
1734 isl_map_free(map);
1735 return NULL;
1738 /* For each pair of basic sets in the set, check if the union of the two
1739 * can be represented by a single basic set.
1740 * If so, replace the pair by the single basic set and start over.
1742 struct isl_set *isl_set_coalesce(struct isl_set *set)
1744 return (struct isl_set *)isl_map_coalesce((struct isl_map *)set);