isl_ast_expr_free: improve error handling
[isl.git] / isl_coalesce.c
blob78e2834f3b1ef9bd08e60112595dddde81a40b37
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>
22 #define STATUS_ERROR -1
23 #define STATUS_REDUNDANT 1
24 #define STATUS_VALID 2
25 #define STATUS_SEPARATE 3
26 #define STATUS_CUT 4
27 #define STATUS_ADJ_EQ 5
28 #define STATUS_ADJ_INEQ 6
30 static int status_in(isl_int *ineq, struct isl_tab *tab)
32 enum isl_ineq_type type = isl_tab_ineq_type(tab, ineq);
33 switch (type) {
34 default:
35 case isl_ineq_error: return STATUS_ERROR;
36 case isl_ineq_redundant: return STATUS_VALID;
37 case isl_ineq_separate: return STATUS_SEPARATE;
38 case isl_ineq_cut: return STATUS_CUT;
39 case isl_ineq_adj_eq: return STATUS_ADJ_EQ;
40 case isl_ineq_adj_ineq: return STATUS_ADJ_INEQ;
44 /* Compute the position of the equalities of basic map "bmap_i"
45 * with respect to the basic map represented by "tab_j".
46 * The resulting array has twice as many entries as the number
47 * of equalities corresponding to the two inequalties to which
48 * each equality corresponds.
50 static int *eq_status_in(__isl_keep isl_basic_map *bmap_i,
51 struct isl_tab *tab_j)
53 int k, l;
54 int *eq = isl_calloc_array(bmap_i->ctx, int, 2 * bmap_i->n_eq);
55 unsigned dim;
57 if (!eq)
58 return NULL;
60 dim = isl_basic_map_total_dim(bmap_i);
61 for (k = 0; k < bmap_i->n_eq; ++k) {
62 for (l = 0; l < 2; ++l) {
63 isl_seq_neg(bmap_i->eq[k], bmap_i->eq[k], 1+dim);
64 eq[2 * k + l] = status_in(bmap_i->eq[k], tab_j);
65 if (eq[2 * k + l] == STATUS_ERROR)
66 goto error;
68 if (eq[2 * k] == STATUS_SEPARATE ||
69 eq[2 * k + 1] == STATUS_SEPARATE)
70 break;
73 return eq;
74 error:
75 free(eq);
76 return NULL;
79 /* Compute the position of the inequalities of basic map "bmap_i"
80 * (also represented by "tab_i", if not NULL) with respect to the basic map
81 * represented by "tab_j".
83 static int *ineq_status_in(__isl_keep isl_basic_map *bmap_i,
84 struct isl_tab *tab_i, struct isl_tab *tab_j)
86 int k;
87 unsigned n_eq = bmap_i->n_eq;
88 int *ineq = isl_calloc_array(bmap_i->ctx, int, bmap_i->n_ineq);
90 if (!ineq)
91 return NULL;
93 for (k = 0; k < bmap_i->n_ineq; ++k) {
94 if (tab_i && isl_tab_is_redundant(tab_i, n_eq + k)) {
95 ineq[k] = STATUS_REDUNDANT;
96 continue;
98 ineq[k] = status_in(bmap_i->ineq[k], tab_j);
99 if (ineq[k] == STATUS_ERROR)
100 goto error;
101 if (ineq[k] == STATUS_SEPARATE)
102 break;
105 return ineq;
106 error:
107 free(ineq);
108 return NULL;
111 static int any(int *con, unsigned len, int status)
113 int i;
115 for (i = 0; i < len ; ++i)
116 if (con[i] == status)
117 return 1;
118 return 0;
121 static int count(int *con, unsigned len, int status)
123 int i;
124 int c = 0;
126 for (i = 0; i < len ; ++i)
127 if (con[i] == status)
128 c++;
129 return c;
132 static int all(int *con, unsigned len, int status)
134 int i;
136 for (i = 0; i < len ; ++i) {
137 if (con[i] == STATUS_REDUNDANT)
138 continue;
139 if (con[i] != status)
140 return 0;
142 return 1;
145 static void drop(struct isl_map *map, int i, struct isl_tab **tabs)
147 isl_basic_map_free(map->p[i]);
148 isl_tab_free(tabs[i]);
150 if (i != map->n - 1) {
151 map->p[i] = map->p[map->n - 1];
152 tabs[i] = tabs[map->n - 1];
154 tabs[map->n - 1] = NULL;
155 map->n--;
158 /* Replace the pair of basic maps i and j by the basic map bounded
159 * by the valid constraints in both basic maps and the constraint
160 * in extra (if not NULL).
162 static int fuse(struct isl_map *map, int i, int j,
163 struct isl_tab **tabs, int *eq_i, int *ineq_i, int *eq_j, int *ineq_j,
164 __isl_keep isl_mat *extra)
166 int k, l;
167 struct isl_basic_map *fused = NULL;
168 struct isl_tab *fused_tab = NULL;
169 unsigned total = isl_basic_map_total_dim(map->p[i]);
170 unsigned extra_rows = extra ? extra->n_row : 0;
172 fused = isl_basic_map_alloc_space(isl_space_copy(map->p[i]->dim),
173 map->p[i]->n_div,
174 map->p[i]->n_eq + map->p[j]->n_eq,
175 map->p[i]->n_ineq + map->p[j]->n_ineq + extra_rows);
176 if (!fused)
177 goto error;
179 for (k = 0; k < map->p[i]->n_eq; ++k) {
180 if (eq_i && (eq_i[2 * k] != STATUS_VALID ||
181 eq_i[2 * k + 1] != STATUS_VALID))
182 continue;
183 l = isl_basic_map_alloc_equality(fused);
184 if (l < 0)
185 goto error;
186 isl_seq_cpy(fused->eq[l], map->p[i]->eq[k], 1 + total);
189 for (k = 0; k < map->p[j]->n_eq; ++k) {
190 if (eq_j && (eq_j[2 * k] != STATUS_VALID ||
191 eq_j[2 * k + 1] != STATUS_VALID))
192 continue;
193 l = isl_basic_map_alloc_equality(fused);
194 if (l < 0)
195 goto error;
196 isl_seq_cpy(fused->eq[l], map->p[j]->eq[k], 1 + total);
199 for (k = 0; k < map->p[i]->n_ineq; ++k) {
200 if (ineq_i[k] != STATUS_VALID)
201 continue;
202 l = isl_basic_map_alloc_inequality(fused);
203 if (l < 0)
204 goto error;
205 isl_seq_cpy(fused->ineq[l], map->p[i]->ineq[k], 1 + total);
208 for (k = 0; k < map->p[j]->n_ineq; ++k) {
209 if (ineq_j[k] != STATUS_VALID)
210 continue;
211 l = isl_basic_map_alloc_inequality(fused);
212 if (l < 0)
213 goto error;
214 isl_seq_cpy(fused->ineq[l], map->p[j]->ineq[k], 1 + total);
217 for (k = 0; k < map->p[i]->n_div; ++k) {
218 int l = isl_basic_map_alloc_div(fused);
219 if (l < 0)
220 goto error;
221 isl_seq_cpy(fused->div[l], map->p[i]->div[k], 1 + 1 + total);
224 for (k = 0; k < extra_rows; ++k) {
225 l = isl_basic_map_alloc_inequality(fused);
226 if (l < 0)
227 goto error;
228 isl_seq_cpy(fused->ineq[l], extra->row[k], 1 + total);
231 fused = isl_basic_map_gauss(fused, NULL);
232 ISL_F_SET(fused, ISL_BASIC_MAP_FINAL);
233 if (ISL_F_ISSET(map->p[i], ISL_BASIC_MAP_RATIONAL) &&
234 ISL_F_ISSET(map->p[j], ISL_BASIC_MAP_RATIONAL))
235 ISL_F_SET(fused, ISL_BASIC_MAP_RATIONAL);
237 fused_tab = isl_tab_from_basic_map(fused, 0);
238 if (isl_tab_detect_redundant(fused_tab) < 0)
239 goto error;
241 isl_basic_map_free(map->p[i]);
242 map->p[i] = fused;
243 isl_tab_free(tabs[i]);
244 tabs[i] = fused_tab;
245 drop(map, j, tabs);
247 return 1;
248 error:
249 isl_tab_free(fused_tab);
250 isl_basic_map_free(fused);
251 return -1;
254 /* Given a pair of basic maps i and j such that all constraints are either
255 * "valid" or "cut", check if the facets corresponding to the "cut"
256 * constraints of i lie entirely within basic map j.
257 * If so, replace the pair by the basic map consisting of the valid
258 * constraints in both basic maps.
260 * To see that we are not introducing any extra points, call the
261 * two basic maps A and B and the resulting map U and let x
262 * be an element of U \setminus ( A \cup B ).
263 * Then there is a pair of cut constraints c_1 and c_2 in A and B such that x
264 * violates them. Let X be the intersection of U with the opposites
265 * of these constraints. Then x \in X.
266 * The facet corresponding to c_1 contains the corresponding facet of A.
267 * This facet is entirely contained in B, so c_2 is valid on the facet.
268 * However, since it is also (part of) a facet of X, -c_2 is also valid
269 * on the facet. This means c_2 is saturated on the facet, so c_1 and
270 * c_2 must be opposites of each other, but then x could not violate
271 * both of them.
273 static int check_facets(struct isl_map *map, int i, int j,
274 struct isl_tab **tabs, int *ineq_i, int *ineq_j)
276 int k, l;
277 struct isl_tab_undo *snap;
278 unsigned n_eq = map->p[i]->n_eq;
280 snap = isl_tab_snap(tabs[i]);
282 for (k = 0; k < map->p[i]->n_ineq; ++k) {
283 if (ineq_i[k] != STATUS_CUT)
284 continue;
285 if (isl_tab_select_facet(tabs[i], n_eq + k) < 0)
286 return -1;
287 for (l = 0; l < map->p[j]->n_ineq; ++l) {
288 int stat;
289 if (ineq_j[l] != STATUS_CUT)
290 continue;
291 stat = status_in(map->p[j]->ineq[l], tabs[i]);
292 if (stat != STATUS_VALID)
293 break;
295 if (isl_tab_rollback(tabs[i], snap) < 0)
296 return -1;
297 if (l < map->p[j]->n_ineq)
298 break;
301 if (k < map->p[i]->n_ineq)
302 /* BAD CUT PAIR */
303 return 0;
304 return fuse(map, i, j, tabs, NULL, ineq_i, NULL, ineq_j, NULL);
307 /* Check if basic map "i" contains the basic map represented
308 * by the tableau "tab".
310 static int contains(struct isl_map *map, int i, int *ineq_i,
311 struct isl_tab *tab)
313 int k, l;
314 unsigned dim;
316 dim = isl_basic_map_total_dim(map->p[i]);
317 for (k = 0; k < map->p[i]->n_eq; ++k) {
318 for (l = 0; l < 2; ++l) {
319 int stat;
320 isl_seq_neg(map->p[i]->eq[k], map->p[i]->eq[k], 1+dim);
321 stat = status_in(map->p[i]->eq[k], tab);
322 if (stat != STATUS_VALID)
323 return 0;
327 for (k = 0; k < map->p[i]->n_ineq; ++k) {
328 int stat;
329 if (ineq_i[k] == STATUS_REDUNDANT)
330 continue;
331 stat = status_in(map->p[i]->ineq[k], tab);
332 if (stat != STATUS_VALID)
333 return 0;
335 return 1;
338 /* Basic map "i" has an inequality (say "k") that is adjacent
339 * to some inequality of basic map "j". All the other inequalities
340 * are valid for "j".
341 * Check if basic map "j" forms an extension of basic map "i".
343 * Note that this function is only called if some of the equalities or
344 * inequalities of basic map "j" do cut basic map "i". The function is
345 * correct even if there are no such cut constraints, but in that case
346 * the additional checks performed by this function are overkill.
348 * In particular, we replace constraint k, say f >= 0, by constraint
349 * f <= -1, add the inequalities of "j" that are valid for "i"
350 * and check if the result is a subset of basic map "j".
351 * If so, then we know that this result is exactly equal to basic map "j"
352 * since all its constraints are valid for basic map "j".
353 * By combining the valid constraints of "i" (all equalities and all
354 * inequalities except "k") and the valid constraints of "j" we therefore
355 * obtain a basic map that is equal to their union.
356 * In this case, there is no need to perform a rollback of the tableau
357 * since it is going to be destroyed in fuse().
360 * |\__ |\__
361 * | \__ | \__
362 * | \_ => | \__
363 * |_______| _ |_________\
366 * |\ |\
367 * | \ | \
368 * | \ | \
369 * | | | \
370 * | ||\ => | \
371 * | || \ | \
372 * | || | | |
373 * |__||_/ |_____/
375 static int is_adj_ineq_extension(__isl_keep isl_map *map, int i, int j,
376 struct isl_tab **tabs, int *eq_i, int *ineq_i, int *eq_j, int *ineq_j)
378 int k;
379 struct isl_tab_undo *snap;
380 unsigned n_eq = map->p[i]->n_eq;
381 unsigned total = isl_basic_map_total_dim(map->p[i]);
382 int r;
384 if (isl_tab_extend_cons(tabs[i], 1 + map->p[j]->n_ineq) < 0)
385 return -1;
387 for (k = 0; k < map->p[i]->n_ineq; ++k)
388 if (ineq_i[k] == STATUS_ADJ_INEQ)
389 break;
390 if (k >= map->p[i]->n_ineq)
391 isl_die(isl_map_get_ctx(map), isl_error_internal,
392 "ineq_i should have exactly one STATUS_ADJ_INEQ",
393 return -1);
395 snap = isl_tab_snap(tabs[i]);
397 if (isl_tab_unrestrict(tabs[i], n_eq + k) < 0)
398 return -1;
400 isl_seq_neg(map->p[i]->ineq[k], map->p[i]->ineq[k], 1 + total);
401 isl_int_sub_ui(map->p[i]->ineq[k][0], map->p[i]->ineq[k][0], 1);
402 r = isl_tab_add_ineq(tabs[i], map->p[i]->ineq[k]);
403 isl_seq_neg(map->p[i]->ineq[k], map->p[i]->ineq[k], 1 + total);
404 isl_int_sub_ui(map->p[i]->ineq[k][0], map->p[i]->ineq[k][0], 1);
405 if (r < 0)
406 return -1;
408 for (k = 0; k < map->p[j]->n_ineq; ++k) {
409 if (ineq_j[k] != STATUS_VALID)
410 continue;
411 if (isl_tab_add_ineq(tabs[i], map->p[j]->ineq[k]) < 0)
412 return -1;
415 if (contains(map, j, ineq_j, tabs[i]))
416 return fuse(map, i, j, tabs, eq_i, ineq_i, eq_j, ineq_j, NULL);
418 if (isl_tab_rollback(tabs[i], snap) < 0)
419 return -1;
421 return 0;
425 /* Both basic maps have at least one inequality with and adjacent
426 * (but opposite) inequality in the other basic map.
427 * Check that there are no cut constraints and that there is only
428 * a single pair of adjacent inequalities.
429 * If so, we can replace the pair by a single basic map described
430 * by all but the pair of adjacent inequalities.
431 * Any additional points introduced lie strictly between the two
432 * adjacent hyperplanes and can therefore be integral.
434 * ____ _____
435 * / ||\ / \
436 * / || \ / \
437 * \ || \ => \ \
438 * \ || / \ /
439 * \___||_/ \_____/
441 * The test for a single pair of adjancent inequalities is important
442 * for avoiding the combination of two basic maps like the following
444 * /|
445 * / |
446 * /__|
447 * _____
448 * | |
449 * | |
450 * |___|
452 * If there are some cut constraints on one side, then we may
453 * still be able to fuse the two basic maps, but we need to perform
454 * some additional checks in is_adj_ineq_extension.
456 static int check_adj_ineq(struct isl_map *map, int i, int j,
457 struct isl_tab **tabs, int *eq_i, int *ineq_i, int *eq_j, int *ineq_j)
459 int count_i, count_j;
460 int cut_i, cut_j;
462 count_i = count(ineq_i, map->p[i]->n_ineq, STATUS_ADJ_INEQ);
463 count_j = count(ineq_j, map->p[j]->n_ineq, STATUS_ADJ_INEQ);
465 if (count_i != 1 && count_j != 1)
466 return 0;
468 cut_i = any(eq_i, 2 * map->p[i]->n_eq, STATUS_CUT) ||
469 any(ineq_i, map->p[i]->n_ineq, STATUS_CUT);
470 cut_j = any(eq_j, 2 * map->p[j]->n_eq, STATUS_CUT) ||
471 any(ineq_j, map->p[j]->n_ineq, STATUS_CUT);
473 if (!cut_i && !cut_j && count_i == 1 && count_j == 1)
474 return fuse(map, i, j, tabs, NULL, ineq_i, NULL, ineq_j, NULL);
476 if (count_i == 1 && !cut_i)
477 return is_adj_ineq_extension(map, i, j, tabs,
478 eq_i, ineq_i, eq_j, ineq_j);
480 if (count_j == 1 && !cut_j)
481 return is_adj_ineq_extension(map, j, i, tabs,
482 eq_j, ineq_j, eq_i, ineq_i);
484 return 0;
487 /* Basic map "i" has an inequality "k" that is adjacent to some equality
488 * of basic map "j". All the other inequalities are valid for "j".
489 * Check if basic map "j" forms an extension of basic map "i".
491 * In particular, we relax constraint "k", compute the corresponding
492 * facet and check whether it is included in the other basic map.
493 * If so, we know that relaxing the constraint extends the basic
494 * map with exactly the other basic map (we already know that this
495 * other basic map is included in the extension, because there
496 * were no "cut" inequalities in "i") and we can replace the
497 * two basic maps by this extension.
498 * ____ _____
499 * / || / |
500 * / || / |
501 * \ || => \ |
502 * \ || \ |
503 * \___|| \____|
505 static int is_adj_eq_extension(struct isl_map *map, int i, int j, int k,
506 struct isl_tab **tabs, int *eq_i, int *ineq_i, int *eq_j, int *ineq_j)
508 int changed = 0;
509 int super;
510 struct isl_tab_undo *snap, *snap2;
511 unsigned n_eq = map->p[i]->n_eq;
513 if (isl_tab_is_equality(tabs[i], n_eq + k))
514 return 0;
516 snap = isl_tab_snap(tabs[i]);
517 tabs[i] = isl_tab_relax(tabs[i], n_eq + k);
518 snap2 = isl_tab_snap(tabs[i]);
519 if (isl_tab_select_facet(tabs[i], n_eq + k) < 0)
520 return -1;
521 super = contains(map, j, ineq_j, tabs[i]);
522 if (super) {
523 if (isl_tab_rollback(tabs[i], snap2) < 0)
524 return -1;
525 map->p[i] = isl_basic_map_cow(map->p[i]);
526 if (!map->p[i])
527 return -1;
528 isl_int_add_ui(map->p[i]->ineq[k][0], map->p[i]->ineq[k][0], 1);
529 ISL_F_SET(map->p[i], ISL_BASIC_MAP_FINAL);
530 drop(map, j, tabs);
531 changed = 1;
532 } else
533 if (isl_tab_rollback(tabs[i], snap) < 0)
534 return -1;
536 return changed;
539 /* Data structure that keeps track of the wrapping constraints
540 * and of information to bound the coefficients of those constraints.
542 * bound is set if we want to apply a bound on the coefficients
543 * mat contains the wrapping constraints
544 * max is the bound on the coefficients (if bound is set)
546 struct isl_wraps {
547 int bound;
548 isl_mat *mat;
549 isl_int max;
552 /* Update wraps->max to be greater than or equal to the coefficients
553 * in the equalities and inequalities of bmap that can be removed if we end up
554 * applying wrapping.
556 static void wraps_update_max(struct isl_wraps *wraps,
557 __isl_keep isl_basic_map *bmap, int *eq, int *ineq)
559 int k;
560 isl_int max_k;
561 unsigned total = isl_basic_map_total_dim(bmap);
563 isl_int_init(max_k);
565 for (k = 0; k < bmap->n_eq; ++k) {
566 if (eq[2 * k] == STATUS_VALID &&
567 eq[2 * k + 1] == STATUS_VALID)
568 continue;
569 isl_seq_abs_max(bmap->eq[k] + 1, total, &max_k);
570 if (isl_int_abs_gt(max_k, wraps->max))
571 isl_int_set(wraps->max, max_k);
574 for (k = 0; k < bmap->n_ineq; ++k) {
575 if (ineq[k] == STATUS_VALID || ineq[k] == STATUS_REDUNDANT)
576 continue;
577 isl_seq_abs_max(bmap->ineq[k] + 1, total, &max_k);
578 if (isl_int_abs_gt(max_k, wraps->max))
579 isl_int_set(wraps->max, max_k);
582 isl_int_clear(max_k);
585 /* Initialize the isl_wraps data structure.
586 * If we want to bound the coefficients of the wrapping constraints,
587 * we set wraps->max to the largest coefficient
588 * in the equalities and inequalities that can be removed if we end up
589 * applying wrapping.
591 static void wraps_init(struct isl_wraps *wraps, __isl_take isl_mat *mat,
592 __isl_keep isl_map *map, int i, int j,
593 int *eq_i, int *ineq_i, int *eq_j, int *ineq_j)
595 isl_ctx *ctx;
597 wraps->bound = 0;
598 wraps->mat = mat;
599 if (!mat)
600 return;
601 ctx = isl_mat_get_ctx(mat);
602 wraps->bound = isl_options_get_coalesce_bounded_wrapping(ctx);
603 if (!wraps->bound)
604 return;
605 isl_int_init(wraps->max);
606 isl_int_set_si(wraps->max, 0);
607 wraps_update_max(wraps, map->p[i], eq_i, ineq_i);
608 wraps_update_max(wraps, map->p[j], eq_j, ineq_j);
611 /* Free the contents of the isl_wraps data structure.
613 static void wraps_free(struct isl_wraps *wraps)
615 isl_mat_free(wraps->mat);
616 if (wraps->bound)
617 isl_int_clear(wraps->max);
620 /* Is the wrapping constraint in row "row" allowed?
622 * If wraps->bound is set, we check that none of the coefficients
623 * is greater than wraps->max.
625 static int allow_wrap(struct isl_wraps *wraps, int row)
627 int i;
629 if (!wraps->bound)
630 return 1;
632 for (i = 1; i < wraps->mat->n_col; ++i)
633 if (isl_int_abs_gt(wraps->mat->row[row][i], wraps->max))
634 return 0;
636 return 1;
639 /* For each non-redundant constraint in "bmap" (as determined by "tab"),
640 * wrap the constraint around "bound" such that it includes the whole
641 * set "set" and append the resulting constraint to "wraps".
642 * "wraps" is assumed to have been pre-allocated to the appropriate size.
643 * wraps->n_row is the number of actual wrapped constraints that have
644 * been added.
645 * If any of the wrapping problems results in a constraint that is
646 * identical to "bound", then this means that "set" is unbounded in such
647 * way that no wrapping is possible. If this happens then wraps->n_row
648 * is reset to zero.
649 * Similarly, if we want to bound the coefficients of the wrapping
650 * constraints and a newly added wrapping constraint does not
651 * satisfy the bound, then wraps->n_row is also reset to zero.
653 static int add_wraps(struct isl_wraps *wraps, __isl_keep isl_basic_map *bmap,
654 struct isl_tab *tab, isl_int *bound, __isl_keep isl_set *set)
656 int l;
657 int w;
658 unsigned total = isl_basic_map_total_dim(bmap);
660 w = wraps->mat->n_row;
662 for (l = 0; l < bmap->n_ineq; ++l) {
663 if (isl_seq_is_neg(bound, bmap->ineq[l], 1 + total))
664 continue;
665 if (isl_seq_eq(bound, bmap->ineq[l], 1 + total))
666 continue;
667 if (isl_tab_is_redundant(tab, bmap->n_eq + l))
668 continue;
670 isl_seq_cpy(wraps->mat->row[w], bound, 1 + total);
671 if (!isl_set_wrap_facet(set, wraps->mat->row[w], bmap->ineq[l]))
672 return -1;
673 if (isl_seq_eq(wraps->mat->row[w], bound, 1 + total))
674 goto unbounded;
675 if (!allow_wrap(wraps, w))
676 goto unbounded;
677 ++w;
679 for (l = 0; l < bmap->n_eq; ++l) {
680 if (isl_seq_is_neg(bound, bmap->eq[l], 1 + total))
681 continue;
682 if (isl_seq_eq(bound, bmap->eq[l], 1 + total))
683 continue;
685 isl_seq_cpy(wraps->mat->row[w], bound, 1 + total);
686 isl_seq_neg(wraps->mat->row[w + 1], bmap->eq[l], 1 + total);
687 if (!isl_set_wrap_facet(set, wraps->mat->row[w],
688 wraps->mat->row[w + 1]))
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 isl_seq_cpy(wraps->mat->row[w], bound, 1 + total);
697 if (!isl_set_wrap_facet(set, wraps->mat->row[w], bmap->eq[l]))
698 return -1;
699 if (isl_seq_eq(wraps->mat->row[w], bound, 1 + total))
700 goto unbounded;
701 if (!allow_wrap(wraps, w))
702 goto unbounded;
703 ++w;
706 wraps->mat->n_row = w;
707 return 0;
708 unbounded:
709 wraps->mat->n_row = 0;
710 return 0;
713 /* Check if the constraints in "wraps" from "first" until the last
714 * are all valid for the basic set represented by "tab".
715 * If not, wraps->n_row is set to zero.
717 static int check_wraps(__isl_keep isl_mat *wraps, int first,
718 struct isl_tab *tab)
720 int i;
722 for (i = first; i < wraps->n_row; ++i) {
723 enum isl_ineq_type type;
724 type = isl_tab_ineq_type(tab, wraps->row[i]);
725 if (type == isl_ineq_error)
726 return -1;
727 if (type == isl_ineq_redundant)
728 continue;
729 wraps->n_row = 0;
730 return 0;
733 return 0;
736 /* Return a set that corresponds to the non-redudant constraints
737 * (as recorded in tab) of bmap.
739 * It's important to remove the redundant constraints as some
740 * of the other constraints may have been modified after the
741 * constraints were marked redundant.
742 * In particular, a constraint may have been relaxed.
743 * Redundant constraints are ignored when a constraint is relaxed
744 * and should therefore continue to be ignored ever after.
745 * Otherwise, the relaxation might be thwarted by some of
746 * these constraints.
748 static __isl_give isl_set *set_from_updated_bmap(__isl_keep isl_basic_map *bmap,
749 struct isl_tab *tab)
751 bmap = isl_basic_map_copy(bmap);
752 bmap = isl_basic_map_cow(bmap);
753 bmap = isl_basic_map_update_from_tab(bmap, tab);
754 return isl_set_from_basic_set(isl_basic_map_underlying_set(bmap));
757 /* Given a basic set i with a constraint k that is adjacent to either the
758 * whole of basic set j or a facet of basic set j, check if we can wrap
759 * both the facet corresponding to k and the facet of j (or the whole of j)
760 * around their ridges to include the other set.
761 * If so, replace the pair of basic sets by their union.
763 * All constraints of i (except k) are assumed to be valid for j.
765 * However, the constraints of j may not be valid for i and so
766 * we have to check that the wrapping constraints for j are valid for i.
768 * In the case where j has a facet adjacent to i, tab[j] is assumed
769 * to have been restricted to this facet, so that the non-redundant
770 * constraints in tab[j] are the ridges of the facet.
771 * Note that for the purpose of wrapping, it does not matter whether
772 * we wrap the ridges of i around the whole of j or just around
773 * the facet since all the other constraints are assumed to be valid for j.
774 * In practice, we wrap to include the whole of j.
775 * ____ _____
776 * / | / \
777 * / || / |
778 * \ || => \ |
779 * \ || \ |
780 * \___|| \____|
783 static int can_wrap_in_facet(struct isl_map *map, int i, int j, int k,
784 struct isl_tab **tabs, int *eq_i, int *ineq_i, int *eq_j, int *ineq_j)
786 int changed = 0;
787 struct isl_wraps wraps;
788 isl_mat *mat;
789 struct isl_set *set_i = NULL;
790 struct isl_set *set_j = NULL;
791 struct isl_vec *bound = NULL;
792 unsigned total = isl_basic_map_total_dim(map->p[i]);
793 struct isl_tab_undo *snap;
794 int n;
796 set_i = set_from_updated_bmap(map->p[i], tabs[i]);
797 set_j = set_from_updated_bmap(map->p[j], tabs[j]);
798 mat = isl_mat_alloc(map->ctx, 2 * (map->p[i]->n_eq + map->p[j]->n_eq) +
799 map->p[i]->n_ineq + map->p[j]->n_ineq,
800 1 + total);
801 wraps_init(&wraps, mat, map, i, j, eq_i, ineq_i, eq_j, ineq_j);
802 bound = isl_vec_alloc(map->ctx, 1 + total);
803 if (!set_i || !set_j || !wraps.mat || !bound)
804 goto error;
806 isl_seq_cpy(bound->el, map->p[i]->ineq[k], 1 + total);
807 isl_int_add_ui(bound->el[0], bound->el[0], 1);
809 isl_seq_cpy(wraps.mat->row[0], bound->el, 1 + total);
810 wraps.mat->n_row = 1;
812 if (add_wraps(&wraps, map->p[j], tabs[j], bound->el, set_i) < 0)
813 goto error;
814 if (!wraps.mat->n_row)
815 goto unbounded;
817 snap = isl_tab_snap(tabs[i]);
819 if (isl_tab_select_facet(tabs[i], map->p[i]->n_eq + k) < 0)
820 goto error;
821 if (isl_tab_detect_redundant(tabs[i]) < 0)
822 goto error;
824 isl_seq_neg(bound->el, map->p[i]->ineq[k], 1 + total);
826 n = wraps.mat->n_row;
827 if (add_wraps(&wraps, map->p[i], tabs[i], bound->el, set_j) < 0)
828 goto error;
830 if (isl_tab_rollback(tabs[i], snap) < 0)
831 goto error;
832 if (check_wraps(wraps.mat, n, tabs[i]) < 0)
833 goto error;
834 if (!wraps.mat->n_row)
835 goto unbounded;
837 changed = fuse(map, i, j, tabs, eq_i, ineq_i, eq_j, ineq_j, wraps.mat);
839 unbounded:
840 wraps_free(&wraps);
842 isl_set_free(set_i);
843 isl_set_free(set_j);
845 isl_vec_free(bound);
847 return changed;
848 error:
849 wraps_free(&wraps);
850 isl_vec_free(bound);
851 isl_set_free(set_i);
852 isl_set_free(set_j);
853 return -1;
856 /* Set the is_redundant property of the "n" constraints in "cuts",
857 * except "k" to "v".
858 * This is a fairly tricky operation as it bypasses isl_tab.c.
859 * The reason we want to temporarily mark some constraints redundant
860 * is that we want to ignore them in add_wraps.
862 * Initially all cut constraints are non-redundant, but the
863 * selection of a facet right before the call to this function
864 * may have made some of them redundant.
865 * Likewise, the same constraints are marked non-redundant
866 * in the second call to this function, before they are officially
867 * made non-redundant again in the subsequent rollback.
869 static void set_is_redundant(struct isl_tab *tab, unsigned n_eq,
870 int *cuts, int n, int k, int v)
872 int l;
874 for (l = 0; l < n; ++l) {
875 if (l == k)
876 continue;
877 tab->con[n_eq + cuts[l]].is_redundant = v;
881 /* Given a pair of basic maps i and j such that j sticks out
882 * of i at n cut constraints, each time by at most one,
883 * try to compute wrapping constraints and replace the two
884 * basic maps by a single basic map.
885 * The other constraints of i are assumed to be valid for j.
887 * The facets of i corresponding to the cut constraints are
888 * wrapped around their ridges, except those ridges determined
889 * by any of the other cut constraints.
890 * The intersections of cut constraints need to be ignored
891 * as the result of wrapping one cut constraint around another
892 * would result in a constraint cutting the union.
893 * In each case, the facets are wrapped to include the union
894 * of the two basic maps.
896 * The pieces of j that lie at an offset of exactly one from
897 * one of the cut constraints of i are wrapped around their edges.
898 * Here, there is no need to ignore intersections because we
899 * are wrapping around the union of the two basic maps.
901 * If any wrapping fails, i.e., if we cannot wrap to touch
902 * the union, then we give up.
903 * Otherwise, the pair of basic maps is replaced by their union.
905 static int wrap_in_facets(struct isl_map *map, int i, int j,
906 int *cuts, int n, struct isl_tab **tabs,
907 int *eq_i, int *ineq_i, int *eq_j, int *ineq_j)
909 int changed = 0;
910 struct isl_wraps wraps;
911 isl_mat *mat;
912 isl_set *set = NULL;
913 isl_vec *bound = NULL;
914 unsigned total = isl_basic_map_total_dim(map->p[i]);
915 int max_wrap;
916 int k;
917 struct isl_tab_undo *snap_i, *snap_j;
919 if (isl_tab_extend_cons(tabs[j], 1) < 0)
920 goto error;
922 max_wrap = 2 * (map->p[i]->n_eq + map->p[j]->n_eq) +
923 map->p[i]->n_ineq + map->p[j]->n_ineq;
924 max_wrap *= n;
926 set = isl_set_union(set_from_updated_bmap(map->p[i], tabs[i]),
927 set_from_updated_bmap(map->p[j], tabs[j]));
928 mat = isl_mat_alloc(map->ctx, max_wrap, 1 + total);
929 wraps_init(&wraps, mat, map, i, j, eq_i, ineq_i, eq_j, ineq_j);
930 bound = isl_vec_alloc(map->ctx, 1 + total);
931 if (!set || !wraps.mat || !bound)
932 goto error;
934 snap_i = isl_tab_snap(tabs[i]);
935 snap_j = isl_tab_snap(tabs[j]);
937 wraps.mat->n_row = 0;
939 for (k = 0; k < n; ++k) {
940 if (isl_tab_select_facet(tabs[i], map->p[i]->n_eq + cuts[k]) < 0)
941 goto error;
942 if (isl_tab_detect_redundant(tabs[i]) < 0)
943 goto error;
944 set_is_redundant(tabs[i], map->p[i]->n_eq, cuts, n, k, 1);
946 isl_seq_neg(bound->el, map->p[i]->ineq[cuts[k]], 1 + total);
947 if (!tabs[i]->empty &&
948 add_wraps(&wraps, map->p[i], tabs[i], bound->el, set) < 0)
949 goto error;
951 set_is_redundant(tabs[i], map->p[i]->n_eq, cuts, n, k, 0);
952 if (isl_tab_rollback(tabs[i], snap_i) < 0)
953 goto error;
955 if (tabs[i]->empty)
956 break;
957 if (!wraps.mat->n_row)
958 break;
960 isl_seq_cpy(bound->el, map->p[i]->ineq[cuts[k]], 1 + total);
961 isl_int_add_ui(bound->el[0], bound->el[0], 1);
962 if (isl_tab_add_eq(tabs[j], bound->el) < 0)
963 goto error;
964 if (isl_tab_detect_redundant(tabs[j]) < 0)
965 goto error;
967 if (!tabs[j]->empty &&
968 add_wraps(&wraps, map->p[j], tabs[j], bound->el, set) < 0)
969 goto error;
971 if (isl_tab_rollback(tabs[j], snap_j) < 0)
972 goto error;
974 if (!wraps.mat->n_row)
975 break;
978 if (k == n)
979 changed = fuse(map, i, j, tabs,
980 eq_i, ineq_i, eq_j, ineq_j, wraps.mat);
982 isl_vec_free(bound);
983 wraps_free(&wraps);
984 isl_set_free(set);
986 return changed;
987 error:
988 isl_vec_free(bound);
989 wraps_free(&wraps);
990 isl_set_free(set);
991 return -1;
994 /* Given two basic sets i and j such that i has no cut equalities,
995 * check if relaxing all the cut inequalities of i by one turns
996 * them into valid constraint for j and check if we can wrap in
997 * the bits that are sticking out.
998 * If so, replace the pair by their union.
1000 * We first check if all relaxed cut inequalities of i are valid for j
1001 * and then try to wrap in the intersections of the relaxed cut inequalities
1002 * with j.
1004 * During this wrapping, we consider the points of j that lie at a distance
1005 * of exactly 1 from i. In particular, we ignore the points that lie in
1006 * between this lower-dimensional space and the basic map i.
1007 * We can therefore only apply this to integer maps.
1008 * ____ _____
1009 * / ___|_ / \
1010 * / | | / |
1011 * \ | | => \ |
1012 * \|____| \ |
1013 * \___| \____/
1015 * _____ ______
1016 * | ____|_ | \
1017 * | | | | |
1018 * | | | => | |
1019 * |_| | | |
1020 * |_____| \______|
1022 * _______
1023 * | |
1024 * | |\ |
1025 * | | \ |
1026 * | | \ |
1027 * | | \|
1028 * | | \
1029 * | |_____\
1030 * | |
1031 * |_______|
1033 * Wrapping can fail if the result of wrapping one of the facets
1034 * around its edges does not produce any new facet constraint.
1035 * In particular, this happens when we try to wrap in unbounded sets.
1037 * _______________________________________________________________________
1039 * | ___
1040 * | | |
1041 * |_| |_________________________________________________________________
1042 * |___|
1044 * The following is not an acceptable result of coalescing the above two
1045 * sets as it includes extra integer points.
1046 * _______________________________________________________________________
1048 * |
1049 * |
1051 * \______________________________________________________________________
1053 static int can_wrap_in_set(struct isl_map *map, int i, int j,
1054 struct isl_tab **tabs, int *eq_i, int *ineq_i, int *eq_j, int *ineq_j)
1056 int changed = 0;
1057 int k, m;
1058 int n;
1059 int *cuts = NULL;
1061 if (ISL_F_ISSET(map->p[i], ISL_BASIC_MAP_RATIONAL) ||
1062 ISL_F_ISSET(map->p[j], ISL_BASIC_MAP_RATIONAL))
1063 return 0;
1065 n = count(ineq_i, map->p[i]->n_ineq, STATUS_CUT);
1066 if (n == 0)
1067 return 0;
1069 cuts = isl_alloc_array(map->ctx, int, n);
1070 if (!cuts)
1071 return -1;
1073 for (k = 0, m = 0; m < n; ++k) {
1074 enum isl_ineq_type type;
1076 if (ineq_i[k] != STATUS_CUT)
1077 continue;
1079 isl_int_add_ui(map->p[i]->ineq[k][0], map->p[i]->ineq[k][0], 1);
1080 type = isl_tab_ineq_type(tabs[j], map->p[i]->ineq[k]);
1081 isl_int_sub_ui(map->p[i]->ineq[k][0], map->p[i]->ineq[k][0], 1);
1082 if (type == isl_ineq_error)
1083 goto error;
1084 if (type != isl_ineq_redundant)
1085 break;
1086 cuts[m] = k;
1087 ++m;
1090 if (m == n)
1091 changed = wrap_in_facets(map, i, j, cuts, n, tabs,
1092 eq_i, ineq_i, eq_j, ineq_j);
1094 free(cuts);
1096 return changed;
1097 error:
1098 free(cuts);
1099 return -1;
1102 /* Check if either i or j has a single cut constraint that can
1103 * be used to wrap in (a facet of) the other basic set.
1104 * if so, replace the pair by their union.
1106 static int check_wrap(struct isl_map *map, int i, int j,
1107 struct isl_tab **tabs, int *eq_i, int *ineq_i, int *eq_j, int *ineq_j)
1109 int changed = 0;
1111 if (!any(eq_i, 2 * map->p[i]->n_eq, STATUS_CUT))
1112 changed = can_wrap_in_set(map, i, j, tabs,
1113 eq_i, ineq_i, eq_j, ineq_j);
1114 if (changed)
1115 return changed;
1117 if (!any(eq_j, 2 * map->p[j]->n_eq, STATUS_CUT))
1118 changed = can_wrap_in_set(map, j, i, tabs,
1119 eq_j, ineq_j, eq_i, ineq_i);
1120 return changed;
1123 /* At least one of the basic maps has an equality that is adjacent
1124 * to inequality. Make sure that only one of the basic maps has
1125 * such an equality and that the other basic map has exactly one
1126 * inequality adjacent to an equality.
1127 * We call the basic map that has the inequality "i" and the basic
1128 * map that has the equality "j".
1129 * If "i" has any "cut" (in)equality, then relaxing the inequality
1130 * by one would not result in a basic map that contains the other
1131 * basic map.
1133 static int check_adj_eq(struct isl_map *map, int i, int j,
1134 struct isl_tab **tabs, int *eq_i, int *ineq_i, int *eq_j, int *ineq_j)
1136 int changed = 0;
1137 int k;
1139 if (any(eq_i, 2 * map->p[i]->n_eq, STATUS_ADJ_INEQ) &&
1140 any(eq_j, 2 * map->p[j]->n_eq, STATUS_ADJ_INEQ))
1141 /* ADJ EQ TOO MANY */
1142 return 0;
1144 if (any(eq_i, 2 * map->p[i]->n_eq, STATUS_ADJ_INEQ))
1145 return check_adj_eq(map, j, i, tabs,
1146 eq_j, ineq_j, eq_i, ineq_i);
1148 /* j has an equality adjacent to an inequality in i */
1150 if (any(eq_i, 2 * map->p[i]->n_eq, STATUS_CUT))
1151 return 0;
1152 if (any(ineq_i, map->p[i]->n_ineq, STATUS_CUT))
1153 /* ADJ EQ CUT */
1154 return 0;
1155 if (count(ineq_i, map->p[i]->n_ineq, STATUS_ADJ_EQ) != 1 ||
1156 any(ineq_j, map->p[j]->n_ineq, STATUS_ADJ_EQ) ||
1157 any(ineq_i, map->p[i]->n_ineq, STATUS_ADJ_INEQ) ||
1158 any(ineq_j, map->p[j]->n_ineq, STATUS_ADJ_INEQ))
1159 /* ADJ EQ TOO MANY */
1160 return 0;
1162 for (k = 0; k < map->p[i]->n_ineq; ++k)
1163 if (ineq_i[k] == STATUS_ADJ_EQ)
1164 break;
1166 changed = is_adj_eq_extension(map, i, j, k, tabs,
1167 eq_i, ineq_i, eq_j, ineq_j);
1168 if (changed)
1169 return changed;
1171 if (count(eq_j, 2 * map->p[j]->n_eq, STATUS_ADJ_INEQ) != 1)
1172 return 0;
1174 changed = can_wrap_in_facet(map, i, j, k, tabs, eq_i, ineq_i, eq_j, ineq_j);
1176 return changed;
1179 /* The two basic maps lie on adjacent hyperplanes. In particular,
1180 * basic map "i" has an equality that lies parallel to basic map "j".
1181 * Check if we can wrap the facets around the parallel hyperplanes
1182 * to include the other set.
1184 * We perform basically the same operations as can_wrap_in_facet,
1185 * except that we don't need to select a facet of one of the sets.
1187 * \\ \\
1188 * \\ => \\
1189 * \ \|
1191 * We only allow one equality of "i" to be adjacent to an equality of "j"
1192 * to avoid coalescing
1194 * [m, n] -> { [x, y] -> [x, 1 + y] : x >= 1 and y >= 1 and
1195 * x <= 10 and y <= 10;
1196 * [x, y] -> [1 + x, y] : x >= 1 and x <= 20 and
1197 * y >= 5 and y <= 15 }
1199 * to
1201 * [m, n] -> { [x, y] -> [x2, y2] : x >= 1 and 10y2 <= 20 - x + 10y and
1202 * 4y2 >= 5 + 3y and 5y2 <= 15 + 4y and
1203 * y2 <= 1 + x + y - x2 and y2 >= y and
1204 * y2 >= 1 + x + y - x2 }
1206 static int check_eq_adj_eq(struct isl_map *map, int i, int j,
1207 struct isl_tab **tabs, int *eq_i, int *ineq_i, int *eq_j, int *ineq_j)
1209 int k;
1210 int changed = 0;
1211 struct isl_wraps wraps;
1212 isl_mat *mat;
1213 struct isl_set *set_i = NULL;
1214 struct isl_set *set_j = NULL;
1215 struct isl_vec *bound = NULL;
1216 unsigned total = isl_basic_map_total_dim(map->p[i]);
1218 if (count(eq_i, 2 * map->p[i]->n_eq, STATUS_ADJ_EQ) != 1)
1219 return 0;
1221 for (k = 0; k < 2 * map->p[i]->n_eq ; ++k)
1222 if (eq_i[k] == STATUS_ADJ_EQ)
1223 break;
1225 set_i = set_from_updated_bmap(map->p[i], tabs[i]);
1226 set_j = set_from_updated_bmap(map->p[j], tabs[j]);
1227 mat = isl_mat_alloc(map->ctx, 2 * (map->p[i]->n_eq + map->p[j]->n_eq) +
1228 map->p[i]->n_ineq + map->p[j]->n_ineq,
1229 1 + total);
1230 wraps_init(&wraps, mat, map, i, j, eq_i, ineq_i, eq_j, ineq_j);
1231 bound = isl_vec_alloc(map->ctx, 1 + total);
1232 if (!set_i || !set_j || !wraps.mat || !bound)
1233 goto error;
1235 if (k % 2 == 0)
1236 isl_seq_neg(bound->el, map->p[i]->eq[k / 2], 1 + total);
1237 else
1238 isl_seq_cpy(bound->el, map->p[i]->eq[k / 2], 1 + total);
1239 isl_int_add_ui(bound->el[0], bound->el[0], 1);
1241 isl_seq_cpy(wraps.mat->row[0], bound->el, 1 + total);
1242 wraps.mat->n_row = 1;
1244 if (add_wraps(&wraps, map->p[j], tabs[j], bound->el, set_i) < 0)
1245 goto error;
1246 if (!wraps.mat->n_row)
1247 goto unbounded;
1249 isl_int_sub_ui(bound->el[0], bound->el[0], 1);
1250 isl_seq_neg(bound->el, bound->el, 1 + total);
1252 isl_seq_cpy(wraps.mat->row[wraps.mat->n_row], bound->el, 1 + total);
1253 wraps.mat->n_row++;
1255 if (add_wraps(&wraps, map->p[i], tabs[i], bound->el, set_j) < 0)
1256 goto error;
1257 if (!wraps.mat->n_row)
1258 goto unbounded;
1260 changed = fuse(map, i, j, tabs, eq_i, ineq_i, eq_j, ineq_j, wraps.mat);
1262 if (0) {
1263 error: changed = -1;
1265 unbounded:
1267 wraps_free(&wraps);
1268 isl_set_free(set_i);
1269 isl_set_free(set_j);
1270 isl_vec_free(bound);
1272 return changed;
1275 /* Check if the union of the given pair of basic maps
1276 * can be represented by a single basic map.
1277 * If so, replace the pair by the single basic map and return 1.
1278 * Otherwise, return 0;
1279 * The two basic maps are assumed to live in the same local space.
1281 * We first check the effect of each constraint of one basic map
1282 * on the other basic map.
1283 * The constraint may be
1284 * redundant the constraint is redundant in its own
1285 * basic map and should be ignore and removed
1286 * in the end
1287 * valid all (integer) points of the other basic map
1288 * satisfy the constraint
1289 * separate no (integer) point of the other basic map
1290 * satisfies the constraint
1291 * cut some but not all points of the other basic map
1292 * satisfy the constraint
1293 * adj_eq the given constraint is adjacent (on the outside)
1294 * to an equality of the other basic map
1295 * adj_ineq the given constraint is adjacent (on the outside)
1296 * to an inequality of the other basic map
1298 * We consider seven cases in which we can replace the pair by a single
1299 * basic map. We ignore all "redundant" constraints.
1301 * 1. all constraints of one basic map are valid
1302 * => the other basic map is a subset and can be removed
1304 * 2. all constraints of both basic maps are either "valid" or "cut"
1305 * and the facets corresponding to the "cut" constraints
1306 * of one of the basic maps lies entirely inside the other basic map
1307 * => the pair can be replaced by a basic map consisting
1308 * of the valid constraints in both basic maps
1310 * 3. there is a single pair of adjacent inequalities
1311 * (all other constraints are "valid")
1312 * => the pair can be replaced by a basic map consisting
1313 * of the valid constraints in both basic maps
1315 * 4. one basic map has a single adjacent inequality, while the other
1316 * constraints are "valid". The other basic map has some
1317 * "cut" constraints, but replacing the adjacent inequality by
1318 * its opposite and adding the valid constraints of the other
1319 * basic map results in a subset of the other basic map
1320 * => the pair can be replaced by a basic map consisting
1321 * of the valid constraints in both basic maps
1323 * 5. there is a single adjacent pair of an inequality and an equality,
1324 * the other constraints of the basic map containing the inequality are
1325 * "valid". Moreover, if the inequality the basic map is relaxed
1326 * and then turned into an equality, then resulting facet lies
1327 * entirely inside the other basic map
1328 * => the pair can be replaced by the basic map containing
1329 * the inequality, with the inequality relaxed.
1331 * 6. there is a single adjacent pair of an inequality and an equality,
1332 * the other constraints of the basic map containing the inequality are
1333 * "valid". Moreover, the facets corresponding to both
1334 * the inequality and the equality can be wrapped around their
1335 * ridges to include the other basic map
1336 * => the pair can be replaced by a basic map consisting
1337 * of the valid constraints in both basic maps together
1338 * with all wrapping constraints
1340 * 7. one of the basic maps extends beyond the other by at most one.
1341 * Moreover, the facets corresponding to the cut constraints and
1342 * the pieces of the other basic map at offset one from these cut
1343 * constraints can be wrapped around their ridges to include
1344 * the union of the two basic maps
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 * 8. the two basic maps live in adjacent hyperplanes. In principle
1350 * such sets can always be combined through wrapping, but we impose
1351 * that there is only one such pair, to avoid overeager coalescing.
1353 * Throughout the computation, we maintain a collection of tableaus
1354 * corresponding to the basic maps. When the basic maps are dropped
1355 * or combined, the tableaus are modified accordingly.
1357 static int coalesce_local_pair(__isl_keep isl_map *map, int i, int j,
1358 struct isl_tab **tabs)
1360 int changed = 0;
1361 int *eq_i = NULL;
1362 int *eq_j = NULL;
1363 int *ineq_i = NULL;
1364 int *ineq_j = NULL;
1366 eq_i = eq_status_in(map->p[i], tabs[j]);
1367 if (map->p[i]->n_eq && !eq_i)
1368 goto error;
1369 if (any(eq_i, 2 * map->p[i]->n_eq, STATUS_ERROR))
1370 goto error;
1371 if (any(eq_i, 2 * map->p[i]->n_eq, STATUS_SEPARATE))
1372 goto done;
1374 eq_j = eq_status_in(map->p[j], tabs[i]);
1375 if (map->p[j]->n_eq && !eq_j)
1376 goto error;
1377 if (any(eq_j, 2 * map->p[j]->n_eq, STATUS_ERROR))
1378 goto error;
1379 if (any(eq_j, 2 * map->p[j]->n_eq, STATUS_SEPARATE))
1380 goto done;
1382 ineq_i = ineq_status_in(map->p[i], tabs[i], tabs[j]);
1383 if (map->p[i]->n_ineq && !ineq_i)
1384 goto error;
1385 if (any(ineq_i, map->p[i]->n_ineq, STATUS_ERROR))
1386 goto error;
1387 if (any(ineq_i, map->p[i]->n_ineq, STATUS_SEPARATE))
1388 goto done;
1390 ineq_j = ineq_status_in(map->p[j], tabs[j], tabs[i]);
1391 if (map->p[j]->n_ineq && !ineq_j)
1392 goto error;
1393 if (any(ineq_j, map->p[j]->n_ineq, STATUS_ERROR))
1394 goto error;
1395 if (any(ineq_j, map->p[j]->n_ineq, STATUS_SEPARATE))
1396 goto done;
1398 if (all(eq_i, 2 * map->p[i]->n_eq, STATUS_VALID) &&
1399 all(ineq_i, map->p[i]->n_ineq, STATUS_VALID)) {
1400 drop(map, j, tabs);
1401 changed = 1;
1402 } else if (all(eq_j, 2 * map->p[j]->n_eq, STATUS_VALID) &&
1403 all(ineq_j, map->p[j]->n_ineq, STATUS_VALID)) {
1404 drop(map, i, tabs);
1405 changed = 1;
1406 } else if (any(eq_i, 2 * map->p[i]->n_eq, STATUS_ADJ_EQ)) {
1407 changed = check_eq_adj_eq(map, i, j, tabs,
1408 eq_i, ineq_i, eq_j, ineq_j);
1409 } else if (any(eq_j, 2 * map->p[j]->n_eq, STATUS_ADJ_EQ)) {
1410 changed = check_eq_adj_eq(map, j, i, tabs,
1411 eq_j, ineq_j, eq_i, ineq_i);
1412 } else if (any(eq_i, 2 * map->p[i]->n_eq, STATUS_ADJ_INEQ) ||
1413 any(eq_j, 2 * map->p[j]->n_eq, STATUS_ADJ_INEQ)) {
1414 changed = check_adj_eq(map, i, j, tabs,
1415 eq_i, ineq_i, eq_j, ineq_j);
1416 } else if (any(ineq_i, map->p[i]->n_ineq, STATUS_ADJ_EQ) ||
1417 any(ineq_j, map->p[j]->n_ineq, STATUS_ADJ_EQ)) {
1418 /* Can't happen */
1419 /* BAD ADJ INEQ */
1420 } else if (any(ineq_i, map->p[i]->n_ineq, STATUS_ADJ_INEQ) ||
1421 any(ineq_j, map->p[j]->n_ineq, STATUS_ADJ_INEQ)) {
1422 changed = check_adj_ineq(map, i, j, tabs,
1423 eq_i, ineq_i, eq_j, ineq_j);
1424 } else {
1425 if (!any(eq_i, 2 * map->p[i]->n_eq, STATUS_CUT) &&
1426 !any(eq_j, 2 * map->p[j]->n_eq, STATUS_CUT))
1427 changed = check_facets(map, i, j, tabs, ineq_i, ineq_j);
1428 if (!changed)
1429 changed = check_wrap(map, i, j, tabs,
1430 eq_i, ineq_i, eq_j, ineq_j);
1433 done:
1434 free(eq_i);
1435 free(eq_j);
1436 free(ineq_i);
1437 free(ineq_j);
1438 return changed;
1439 error:
1440 free(eq_i);
1441 free(eq_j);
1442 free(ineq_i);
1443 free(ineq_j);
1444 return -1;
1447 /* Do the two basic maps live in the same local space, i.e.,
1448 * do they have the same (known) divs?
1449 * If either basic map has any unknown divs, then we can only assume
1450 * that they do not live in the same local space.
1452 static int same_divs(__isl_keep isl_basic_map *bmap1,
1453 __isl_keep isl_basic_map *bmap2)
1455 int i;
1456 int known;
1457 int total;
1459 if (!bmap1 || !bmap2)
1460 return -1;
1461 if (bmap1->n_div != bmap2->n_div)
1462 return 0;
1464 if (bmap1->n_div == 0)
1465 return 1;
1467 known = isl_basic_map_divs_known(bmap1);
1468 if (known < 0 || !known)
1469 return known;
1470 known = isl_basic_map_divs_known(bmap2);
1471 if (known < 0 || !known)
1472 return known;
1474 total = isl_basic_map_total_dim(bmap1);
1475 for (i = 0; i < bmap1->n_div; ++i)
1476 if (!isl_seq_eq(bmap1->div[i], bmap2->div[i], 2 + total))
1477 return 0;
1479 return 1;
1482 /* Given two basic maps "i" and "j", where the divs of "i" form a subset
1483 * of those of "j", check if basic map "j" is a subset of basic map "i"
1484 * and, if so, drop basic map "j".
1486 * We first expand the divs of basic map "i" to match those of basic map "j",
1487 * using the divs and expansion computed by the caller.
1488 * Then we check if all constraints of the expanded "i" are valid for "j".
1490 static int coalesce_subset(__isl_keep isl_map *map, int i, int j,
1491 struct isl_tab **tabs, __isl_keep isl_mat *div, int *exp)
1493 isl_basic_map *bmap;
1494 int changed = 0;
1495 int *eq_i = NULL;
1496 int *ineq_i = NULL;
1498 bmap = isl_basic_map_copy(map->p[i]);
1499 bmap = isl_basic_set_expand_divs(bmap, isl_mat_copy(div), exp);
1501 if (!bmap)
1502 goto error;
1504 eq_i = eq_status_in(bmap, tabs[j]);
1505 if (bmap->n_eq && !eq_i)
1506 goto error;
1507 if (any(eq_i, 2 * bmap->n_eq, STATUS_ERROR))
1508 goto error;
1509 if (any(eq_i, 2 * bmap->n_eq, STATUS_SEPARATE))
1510 goto done;
1512 ineq_i = ineq_status_in(bmap, NULL, tabs[j]);
1513 if (bmap->n_ineq && !ineq_i)
1514 goto error;
1515 if (any(ineq_i, bmap->n_ineq, STATUS_ERROR))
1516 goto error;
1517 if (any(ineq_i, bmap->n_ineq, STATUS_SEPARATE))
1518 goto done;
1520 if (all(eq_i, 2 * map->p[i]->n_eq, STATUS_VALID) &&
1521 all(ineq_i, map->p[i]->n_ineq, STATUS_VALID)) {
1522 drop(map, j, tabs);
1523 changed = 1;
1526 done:
1527 isl_basic_map_free(bmap);
1528 free(eq_i);
1529 free(ineq_i);
1530 return 0;
1531 error:
1532 isl_basic_map_free(bmap);
1533 free(eq_i);
1534 free(ineq_i);
1535 return -1;
1538 /* Check if the basic map "j" is a subset of basic map "i",
1539 * assuming that "i" has fewer divs that "j".
1540 * If not, then we change the order.
1542 * If the two basic maps have the same number of divs, then
1543 * they must necessarily be different. Otherwise, we would have
1544 * called coalesce_local_pair. We therefore don't try anything
1545 * in this case.
1547 * We first check if the divs of "i" are all known and form a subset
1548 * of those of "j". If so, we pass control over to coalesce_subset.
1550 static int check_coalesce_subset(__isl_keep isl_map *map, int i, int j,
1551 struct isl_tab **tabs)
1553 int known;
1554 isl_mat *div_i, *div_j, *div;
1555 int *exp1 = NULL;
1556 int *exp2 = NULL;
1557 isl_ctx *ctx;
1558 int subset;
1560 if (map->p[i]->n_div == map->p[j]->n_div)
1561 return 0;
1562 if (map->p[j]->n_div < map->p[i]->n_div)
1563 return check_coalesce_subset(map, j, i, tabs);
1565 known = isl_basic_map_divs_known(map->p[i]);
1566 if (known < 0 || !known)
1567 return known;
1569 ctx = isl_map_get_ctx(map);
1571 div_i = isl_basic_map_get_divs(map->p[i]);
1572 div_j = isl_basic_map_get_divs(map->p[j]);
1574 if (!div_i || !div_j)
1575 goto error;
1577 exp1 = isl_alloc_array(ctx, int, div_i->n_row);
1578 exp2 = isl_alloc_array(ctx, int, div_j->n_row);
1579 if ((div_i->n_row && !exp1) || (div_j->n_row && !exp2))
1580 goto error;
1582 div = isl_merge_divs(div_i, div_j, exp1, exp2);
1583 if (!div)
1584 goto error;
1586 if (div->n_row == div_j->n_row)
1587 subset = coalesce_subset(map, i, j, tabs, div, exp1);
1588 else
1589 subset = 0;
1591 isl_mat_free(div);
1593 isl_mat_free(div_i);
1594 isl_mat_free(div_j);
1596 free(exp2);
1597 free(exp1);
1599 return subset;
1600 error:
1601 isl_mat_free(div_i);
1602 isl_mat_free(div_j);
1603 free(exp1);
1604 free(exp2);
1605 return -1;
1608 /* Check if the union of the given pair of basic maps
1609 * can be represented by a single basic map.
1610 * If so, replace the pair by the single basic map and return 1.
1611 * Otherwise, return 0;
1613 * We first check if the two basic maps live in the same local space.
1614 * If so, we do the complete check. Otherwise, we check if one is
1615 * an obvious subset of the other.
1617 static int coalesce_pair(__isl_keep isl_map *map, int i, int j,
1618 struct isl_tab **tabs)
1620 int same;
1622 same = same_divs(map->p[i], map->p[j]);
1623 if (same < 0)
1624 return -1;
1625 if (same)
1626 return coalesce_local_pair(map, i, j, tabs);
1628 return check_coalesce_subset(map, i, j, tabs);
1631 static struct isl_map *coalesce(struct isl_map *map, struct isl_tab **tabs)
1633 int i, j;
1635 for (i = map->n - 2; i >= 0; --i)
1636 restart:
1637 for (j = i + 1; j < map->n; ++j) {
1638 int changed;
1639 changed = coalesce_pair(map, i, j, tabs);
1640 if (changed < 0)
1641 goto error;
1642 if (changed)
1643 goto restart;
1645 return map;
1646 error:
1647 isl_map_free(map);
1648 return NULL;
1651 /* For each pair of basic maps in the map, check if the union of the two
1652 * can be represented by a single basic map.
1653 * If so, replace the pair by the single basic map and start over.
1655 * Since we are constructing the tableaus of the basic maps anyway,
1656 * we exploit them to detect implicit equalities and redundant constraints.
1657 * This also helps the coalescing as it can ignore the redundant constraints.
1658 * In order to avoid confusion, we make all implicit equalities explicit
1659 * in the basic maps. We don't call isl_basic_map_gauss, though,
1660 * as that may affect the number of constraints.
1661 * This means that we have to call isl_basic_map_gauss at the end
1662 * of the computation to ensure that the basic maps are not left
1663 * in an unexpected state.
1665 struct isl_map *isl_map_coalesce(struct isl_map *map)
1667 int i;
1668 unsigned n;
1669 struct isl_tab **tabs = NULL;
1671 map = isl_map_remove_empty_parts(map);
1672 if (!map)
1673 return NULL;
1675 if (map->n <= 1)
1676 return map;
1678 map = isl_map_sort_divs(map);
1679 map = isl_map_cow(map);
1681 tabs = isl_calloc_array(map->ctx, struct isl_tab *, map->n);
1682 if (!tabs)
1683 goto error;
1685 n = map->n;
1686 for (i = 0; i < map->n; ++i) {
1687 tabs[i] = isl_tab_from_basic_map(map->p[i], 0);
1688 if (!tabs[i])
1689 goto error;
1690 if (!ISL_F_ISSET(map->p[i], ISL_BASIC_MAP_NO_IMPLICIT))
1691 if (isl_tab_detect_implicit_equalities(tabs[i]) < 0)
1692 goto error;
1693 map->p[i] = isl_tab_make_equalities_explicit(tabs[i],
1694 map->p[i]);
1695 if (!map->p[i])
1696 goto error;
1697 if (!ISL_F_ISSET(map->p[i], ISL_BASIC_MAP_NO_REDUNDANT))
1698 if (isl_tab_detect_redundant(tabs[i]) < 0)
1699 goto error;
1701 for (i = map->n - 1; i >= 0; --i)
1702 if (tabs[i]->empty)
1703 drop(map, i, tabs);
1705 map = coalesce(map, tabs);
1707 if (map)
1708 for (i = 0; i < map->n; ++i) {
1709 map->p[i] = isl_basic_map_update_from_tab(map->p[i],
1710 tabs[i]);
1711 map->p[i] = isl_basic_map_gauss(map->p[i], NULL);
1712 map->p[i] = isl_basic_map_finalize(map->p[i]);
1713 if (!map->p[i])
1714 goto error;
1715 ISL_F_SET(map->p[i], ISL_BASIC_MAP_NO_IMPLICIT);
1716 ISL_F_SET(map->p[i], ISL_BASIC_MAP_NO_REDUNDANT);
1719 for (i = 0; i < n; ++i)
1720 isl_tab_free(tabs[i]);
1722 free(tabs);
1724 return map;
1725 error:
1726 if (tabs)
1727 for (i = 0; i < n; ++i)
1728 isl_tab_free(tabs[i]);
1729 free(tabs);
1730 isl_map_free(map);
1731 return NULL;
1734 /* For each pair of basic sets in the set, check if the union of the two
1735 * can be represented by a single basic set.
1736 * If so, replace the pair by the single basic set and start over.
1738 struct isl_set *isl_set_coalesce(struct isl_set *set)
1740 return (struct isl_set *)isl_map_coalesce((struct isl_map *)set);