isl_coalesce.c: check_facets: mark tableau rational before subset tests
[isl.git] / isl_coalesce.c
blob3e8306b4cebeaa0d435109a3f23f52c5194c59e0
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2012-2013 Ecole Normale Superieure
6 * Use of this software is governed by the MIT license
8 * Written by Sven Verdoolaege, K.U.Leuven, Departement
9 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
10 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
11 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
12 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
15 #include "isl_map_private.h"
16 #include <isl_seq.h>
17 #include <isl/options.h>
18 #include "isl_tab.h"
19 #include <isl_mat_private.h>
20 #include <isl_local_space_private.h>
21 #include <isl_vec_private.h>
23 #define STATUS_ERROR -1
24 #define STATUS_REDUNDANT 1
25 #define STATUS_VALID 2
26 #define STATUS_SEPARATE 3
27 #define STATUS_CUT 4
28 #define STATUS_ADJ_EQ 5
29 #define STATUS_ADJ_INEQ 6
31 static int status_in(isl_int *ineq, struct isl_tab *tab)
33 enum isl_ineq_type type = isl_tab_ineq_type(tab, ineq);
34 switch (type) {
35 default:
36 case isl_ineq_error: return STATUS_ERROR;
37 case isl_ineq_redundant: return STATUS_VALID;
38 case isl_ineq_separate: return STATUS_SEPARATE;
39 case isl_ineq_cut: return STATUS_CUT;
40 case isl_ineq_adj_eq: return STATUS_ADJ_EQ;
41 case isl_ineq_adj_ineq: return STATUS_ADJ_INEQ;
45 /* Compute the position of the equalities of basic map "bmap_i"
46 * with respect to the basic map represented by "tab_j".
47 * The resulting array has twice as many entries as the number
48 * of equalities corresponding to the two inequalties to which
49 * each equality corresponds.
51 static int *eq_status_in(__isl_keep isl_basic_map *bmap_i,
52 struct isl_tab *tab_j)
54 int k, l;
55 int *eq = isl_calloc_array(bmap_i->ctx, int, 2 * bmap_i->n_eq);
56 unsigned dim;
58 if (!eq)
59 return NULL;
61 dim = isl_basic_map_total_dim(bmap_i);
62 for (k = 0; k < bmap_i->n_eq; ++k) {
63 for (l = 0; l < 2; ++l) {
64 isl_seq_neg(bmap_i->eq[k], bmap_i->eq[k], 1+dim);
65 eq[2 * k + l] = status_in(bmap_i->eq[k], tab_j);
66 if (eq[2 * k + l] == STATUS_ERROR)
67 goto error;
69 if (eq[2 * k] == STATUS_SEPARATE ||
70 eq[2 * k + 1] == STATUS_SEPARATE)
71 break;
74 return eq;
75 error:
76 free(eq);
77 return NULL;
80 /* Compute the position of the inequalities of basic map "bmap_i"
81 * (also represented by "tab_i", if not NULL) with respect to the basic map
82 * represented by "tab_j".
84 static int *ineq_status_in(__isl_keep isl_basic_map *bmap_i,
85 struct isl_tab *tab_i, struct isl_tab *tab_j)
87 int k;
88 unsigned n_eq = bmap_i->n_eq;
89 int *ineq = isl_calloc_array(bmap_i->ctx, int, bmap_i->n_ineq);
91 if (!ineq)
92 return NULL;
94 for (k = 0; k < bmap_i->n_ineq; ++k) {
95 if (tab_i && isl_tab_is_redundant(tab_i, n_eq + k)) {
96 ineq[k] = STATUS_REDUNDANT;
97 continue;
99 ineq[k] = status_in(bmap_i->ineq[k], tab_j);
100 if (ineq[k] == STATUS_ERROR)
101 goto error;
102 if (ineq[k] == STATUS_SEPARATE)
103 break;
106 return ineq;
107 error:
108 free(ineq);
109 return NULL;
112 static int any(int *con, unsigned len, int status)
114 int i;
116 for (i = 0; i < len ; ++i)
117 if (con[i] == status)
118 return 1;
119 return 0;
122 static int count(int *con, unsigned len, int status)
124 int i;
125 int c = 0;
127 for (i = 0; i < len ; ++i)
128 if (con[i] == status)
129 c++;
130 return c;
133 static int all(int *con, unsigned len, int status)
135 int i;
137 for (i = 0; i < len ; ++i) {
138 if (con[i] == STATUS_REDUNDANT)
139 continue;
140 if (con[i] != status)
141 return 0;
143 return 1;
146 static void drop(struct isl_map *map, int i, struct isl_tab **tabs)
148 isl_basic_map_free(map->p[i]);
149 isl_tab_free(tabs[i]);
151 if (i != map->n - 1) {
152 map->p[i] = map->p[map->n - 1];
153 tabs[i] = tabs[map->n - 1];
155 tabs[map->n - 1] = NULL;
156 map->n--;
159 /* Replace the pair of basic maps i and j by the basic map bounded
160 * by the valid constraints in both basic maps and the constraints
161 * in extra (if not NULL).
163 static int fuse(struct isl_map *map, int i, int j,
164 struct isl_tab **tabs, int *eq_i, int *ineq_i, int *eq_j, int *ineq_j,
165 __isl_keep isl_mat *extra)
167 int k, l;
168 struct isl_basic_map *fused = NULL;
169 struct isl_tab *fused_tab = NULL;
170 unsigned total = isl_basic_map_total_dim(map->p[i]);
171 unsigned extra_rows = extra ? extra->n_row : 0;
173 fused = isl_basic_map_alloc_space(isl_space_copy(map->p[i]->dim),
174 map->p[i]->n_div,
175 map->p[i]->n_eq + map->p[j]->n_eq,
176 map->p[i]->n_ineq + map->p[j]->n_ineq + extra_rows);
177 if (!fused)
178 goto error;
180 for (k = 0; k < map->p[i]->n_eq; ++k) {
181 if (eq_i && (eq_i[2 * k] != STATUS_VALID ||
182 eq_i[2 * k + 1] != STATUS_VALID))
183 continue;
184 l = isl_basic_map_alloc_equality(fused);
185 if (l < 0)
186 goto error;
187 isl_seq_cpy(fused->eq[l], map->p[i]->eq[k], 1 + total);
190 for (k = 0; k < map->p[j]->n_eq; ++k) {
191 if (eq_j && (eq_j[2 * k] != STATUS_VALID ||
192 eq_j[2 * k + 1] != STATUS_VALID))
193 continue;
194 l = isl_basic_map_alloc_equality(fused);
195 if (l < 0)
196 goto error;
197 isl_seq_cpy(fused->eq[l], map->p[j]->eq[k], 1 + total);
200 for (k = 0; k < map->p[i]->n_ineq; ++k) {
201 if (ineq_i[k] != STATUS_VALID)
202 continue;
203 l = isl_basic_map_alloc_inequality(fused);
204 if (l < 0)
205 goto error;
206 isl_seq_cpy(fused->ineq[l], map->p[i]->ineq[k], 1 + total);
209 for (k = 0; k < map->p[j]->n_ineq; ++k) {
210 if (ineq_j[k] != STATUS_VALID)
211 continue;
212 l = isl_basic_map_alloc_inequality(fused);
213 if (l < 0)
214 goto error;
215 isl_seq_cpy(fused->ineq[l], map->p[j]->ineq[k], 1 + total);
218 for (k = 0; k < map->p[i]->n_div; ++k) {
219 int l = isl_basic_map_alloc_div(fused);
220 if (l < 0)
221 goto error;
222 isl_seq_cpy(fused->div[l], map->p[i]->div[k], 1 + 1 + total);
225 for (k = 0; k < extra_rows; ++k) {
226 l = isl_basic_map_alloc_inequality(fused);
227 if (l < 0)
228 goto error;
229 isl_seq_cpy(fused->ineq[l], extra->row[k], 1 + total);
232 fused = isl_basic_map_gauss(fused, NULL);
233 ISL_F_SET(fused, ISL_BASIC_MAP_FINAL);
234 if (ISL_F_ISSET(map->p[i], ISL_BASIC_MAP_RATIONAL) &&
235 ISL_F_ISSET(map->p[j], ISL_BASIC_MAP_RATIONAL))
236 ISL_F_SET(fused, ISL_BASIC_MAP_RATIONAL);
238 fused_tab = isl_tab_from_basic_map(fused, 0);
239 if (isl_tab_detect_redundant(fused_tab) < 0)
240 goto error;
242 isl_basic_map_free(map->p[i]);
243 map->p[i] = fused;
244 isl_tab_free(tabs[i]);
245 tabs[i] = fused_tab;
246 drop(map, j, tabs);
248 return 1;
249 error:
250 isl_tab_free(fused_tab);
251 isl_basic_map_free(fused);
252 return -1;
255 /* Given a pair of basic maps i and j such that all constraints are either
256 * "valid" or "cut", check if the facets corresponding to the "cut"
257 * constraints of i lie entirely within basic map j.
258 * If so, replace the pair by the basic map consisting of the valid
259 * constraints in both basic maps.
260 * Checking whether the facet lies entirely within basic map j
261 * is performed by checking whether the constraints of basic map j
262 * are valid for the facet. These tests are performed on a rational
263 * tableau to avoid the theoretical possibility that a constraint
264 * that was considered to be a cut constraint for the entire basic map i
265 * happens to be considered to be a valid constraint for the facet,
266 * even though it cuts off the same rational points.
268 * To see that we are not introducing any extra points, call the
269 * two basic maps A and B and the resulting map U and let x
270 * be an element of U \setminus ( A \cup B ).
271 * Then there is a pair of cut constraints c_1 and c_2 in A and B such that x
272 * violates them. Let X be the intersection of U with the opposites
273 * of these constraints. Then x \in X.
274 * The facet corresponding to c_1 contains the corresponding facet of A.
275 * This facet is entirely contained in B, so c_2 is valid on the facet.
276 * However, since it is also (part of) a facet of X, -c_2 is also valid
277 * on the facet. This means c_2 is saturated on the facet, so c_1 and
278 * c_2 must be opposites of each other, but then x could not violate
279 * both of them.
281 static int check_facets(struct isl_map *map, int i, int j,
282 struct isl_tab **tabs, int *ineq_i, int *ineq_j)
284 int k, l;
285 struct isl_tab_undo *snap, *snap2;
286 unsigned n_eq = map->p[i]->n_eq;
288 snap = isl_tab_snap(tabs[i]);
289 if (isl_tab_mark_rational(tabs[i]) < 0)
290 return -1;
291 snap2 = isl_tab_snap(tabs[i]);
293 for (k = 0; k < map->p[i]->n_ineq; ++k) {
294 if (ineq_i[k] != STATUS_CUT)
295 continue;
296 if (isl_tab_select_facet(tabs[i], n_eq + k) < 0)
297 return -1;
298 for (l = 0; l < map->p[j]->n_ineq; ++l) {
299 int stat;
300 if (ineq_j[l] != STATUS_CUT)
301 continue;
302 stat = status_in(map->p[j]->ineq[l], tabs[i]);
303 if (stat != STATUS_VALID)
304 break;
306 if (isl_tab_rollback(tabs[i], snap2) < 0)
307 return -1;
308 if (l < map->p[j]->n_ineq)
309 break;
312 if (k < map->p[i]->n_ineq) {
313 if (isl_tab_rollback(tabs[i], snap) < 0)
314 return -1;
315 return 0;
317 return fuse(map, i, j, tabs, NULL, ineq_i, NULL, ineq_j, NULL);
320 /* Check if basic map "i" contains the basic map represented
321 * by the tableau "tab".
323 static int contains(struct isl_map *map, int i, int *ineq_i,
324 struct isl_tab *tab)
326 int k, l;
327 unsigned dim;
329 dim = isl_basic_map_total_dim(map->p[i]);
330 for (k = 0; k < map->p[i]->n_eq; ++k) {
331 for (l = 0; l < 2; ++l) {
332 int stat;
333 isl_seq_neg(map->p[i]->eq[k], map->p[i]->eq[k], 1+dim);
334 stat = status_in(map->p[i]->eq[k], tab);
335 if (stat != STATUS_VALID)
336 return 0;
340 for (k = 0; k < map->p[i]->n_ineq; ++k) {
341 int stat;
342 if (ineq_i[k] == STATUS_REDUNDANT)
343 continue;
344 stat = status_in(map->p[i]->ineq[k], tab);
345 if (stat != STATUS_VALID)
346 return 0;
348 return 1;
351 /* Basic map "i" has an inequality (say "k") that is adjacent
352 * to some inequality of basic map "j". All the other inequalities
353 * are valid for "j".
354 * Check if basic map "j" forms an extension of basic map "i".
356 * Note that this function is only called if some of the equalities or
357 * inequalities of basic map "j" do cut basic map "i". The function is
358 * correct even if there are no such cut constraints, but in that case
359 * the additional checks performed by this function are overkill.
361 * In particular, we replace constraint k, say f >= 0, by constraint
362 * f <= -1, add the inequalities of "j" that are valid for "i"
363 * and check if the result is a subset of basic map "j".
364 * If so, then we know that this result is exactly equal to basic map "j"
365 * since all its constraints are valid for basic map "j".
366 * By combining the valid constraints of "i" (all equalities and all
367 * inequalities except "k") and the valid constraints of "j" we therefore
368 * obtain a basic map that is equal to their union.
369 * In this case, there is no need to perform a rollback of the tableau
370 * since it is going to be destroyed in fuse().
373 * |\__ |\__
374 * | \__ | \__
375 * | \_ => | \__
376 * |_______| _ |_________\
379 * |\ |\
380 * | \ | \
381 * | \ | \
382 * | | | \
383 * | ||\ => | \
384 * | || \ | \
385 * | || | | |
386 * |__||_/ |_____/
388 static int is_adj_ineq_extension(__isl_keep isl_map *map, int i, int j,
389 struct isl_tab **tabs, int *eq_i, int *ineq_i, int *eq_j, int *ineq_j)
391 int k;
392 struct isl_tab_undo *snap;
393 unsigned n_eq = map->p[i]->n_eq;
394 unsigned total = isl_basic_map_total_dim(map->p[i]);
395 int r;
397 if (isl_tab_extend_cons(tabs[i], 1 + map->p[j]->n_ineq) < 0)
398 return -1;
400 for (k = 0; k < map->p[i]->n_ineq; ++k)
401 if (ineq_i[k] == STATUS_ADJ_INEQ)
402 break;
403 if (k >= map->p[i]->n_ineq)
404 isl_die(isl_map_get_ctx(map), isl_error_internal,
405 "ineq_i should have exactly one STATUS_ADJ_INEQ",
406 return -1);
408 snap = isl_tab_snap(tabs[i]);
410 if (isl_tab_unrestrict(tabs[i], n_eq + k) < 0)
411 return -1;
413 isl_seq_neg(map->p[i]->ineq[k], map->p[i]->ineq[k], 1 + total);
414 isl_int_sub_ui(map->p[i]->ineq[k][0], map->p[i]->ineq[k][0], 1);
415 r = isl_tab_add_ineq(tabs[i], map->p[i]->ineq[k]);
416 isl_seq_neg(map->p[i]->ineq[k], map->p[i]->ineq[k], 1 + total);
417 isl_int_sub_ui(map->p[i]->ineq[k][0], map->p[i]->ineq[k][0], 1);
418 if (r < 0)
419 return -1;
421 for (k = 0; k < map->p[j]->n_ineq; ++k) {
422 if (ineq_j[k] != STATUS_VALID)
423 continue;
424 if (isl_tab_add_ineq(tabs[i], map->p[j]->ineq[k]) < 0)
425 return -1;
428 if (contains(map, j, ineq_j, tabs[i]))
429 return fuse(map, i, j, tabs, eq_i, ineq_i, eq_j, ineq_j, NULL);
431 if (isl_tab_rollback(tabs[i], snap) < 0)
432 return -1;
434 return 0;
438 /* Both basic maps have at least one inequality with and adjacent
439 * (but opposite) inequality in the other basic map.
440 * Check that there are no cut constraints and that there is only
441 * a single pair of adjacent inequalities.
442 * If so, we can replace the pair by a single basic map described
443 * by all but the pair of adjacent inequalities.
444 * Any additional points introduced lie strictly between the two
445 * adjacent hyperplanes and can therefore be integral.
447 * ____ _____
448 * / ||\ / \
449 * / || \ / \
450 * \ || \ => \ \
451 * \ || / \ /
452 * \___||_/ \_____/
454 * The test for a single pair of adjancent inequalities is important
455 * for avoiding the combination of two basic maps like the following
457 * /|
458 * / |
459 * /__|
460 * _____
461 * | |
462 * | |
463 * |___|
465 * If there are some cut constraints on one side, then we may
466 * still be able to fuse the two basic maps, but we need to perform
467 * some additional checks in is_adj_ineq_extension.
469 static int check_adj_ineq(struct isl_map *map, int i, int j,
470 struct isl_tab **tabs, int *eq_i, int *ineq_i, int *eq_j, int *ineq_j)
472 int count_i, count_j;
473 int cut_i, cut_j;
475 count_i = count(ineq_i, map->p[i]->n_ineq, STATUS_ADJ_INEQ);
476 count_j = count(ineq_j, map->p[j]->n_ineq, STATUS_ADJ_INEQ);
478 if (count_i != 1 && count_j != 1)
479 return 0;
481 cut_i = any(eq_i, 2 * map->p[i]->n_eq, STATUS_CUT) ||
482 any(ineq_i, map->p[i]->n_ineq, STATUS_CUT);
483 cut_j = any(eq_j, 2 * map->p[j]->n_eq, STATUS_CUT) ||
484 any(ineq_j, map->p[j]->n_ineq, STATUS_CUT);
486 if (!cut_i && !cut_j && count_i == 1 && count_j == 1)
487 return fuse(map, i, j, tabs, NULL, ineq_i, NULL, ineq_j, NULL);
489 if (count_i == 1 && !cut_i)
490 return is_adj_ineq_extension(map, i, j, tabs,
491 eq_i, ineq_i, eq_j, ineq_j);
493 if (count_j == 1 && !cut_j)
494 return is_adj_ineq_extension(map, j, i, tabs,
495 eq_j, ineq_j, eq_i, ineq_i);
497 return 0;
500 /* Basic map "i" has an inequality "k" that is adjacent to some equality
501 * of basic map "j". All the other inequalities are valid for "j".
502 * Check if basic map "j" forms an extension of basic map "i".
504 * In particular, we relax constraint "k", compute the corresponding
505 * facet and check whether it is included in the other basic map.
506 * If so, we know that relaxing the constraint extends the basic
507 * map with exactly the other basic map (we already know that this
508 * other basic map is included in the extension, because there
509 * were no "cut" inequalities in "i") and we can replace the
510 * two basic maps by this extension.
511 * ____ _____
512 * / || / |
513 * / || / |
514 * \ || => \ |
515 * \ || \ |
516 * \___|| \____|
518 static int is_adj_eq_extension(struct isl_map *map, int i, int j, int k,
519 struct isl_tab **tabs, int *eq_i, int *ineq_i, int *eq_j, int *ineq_j)
521 int changed = 0;
522 int super;
523 struct isl_tab_undo *snap, *snap2;
524 unsigned n_eq = map->p[i]->n_eq;
526 if (isl_tab_is_equality(tabs[i], n_eq + k))
527 return 0;
529 snap = isl_tab_snap(tabs[i]);
530 if (isl_tab_relax(tabs[i], n_eq + k) < 0)
531 return -1;
532 snap2 = isl_tab_snap(tabs[i]);
533 if (isl_tab_select_facet(tabs[i], n_eq + k) < 0)
534 return -1;
535 super = contains(map, j, ineq_j, tabs[i]);
536 if (super) {
537 if (isl_tab_rollback(tabs[i], snap2) < 0)
538 return -1;
539 map->p[i] = isl_basic_map_cow(map->p[i]);
540 if (!map->p[i])
541 return -1;
542 isl_int_add_ui(map->p[i]->ineq[k][0], map->p[i]->ineq[k][0], 1);
543 ISL_F_SET(map->p[i], ISL_BASIC_MAP_FINAL);
544 drop(map, j, tabs);
545 changed = 1;
546 } else
547 if (isl_tab_rollback(tabs[i], snap) < 0)
548 return -1;
550 return changed;
553 /* Data structure that keeps track of the wrapping constraints
554 * and of information to bound the coefficients of those constraints.
556 * bound is set if we want to apply a bound on the coefficients
557 * mat contains the wrapping constraints
558 * max is the bound on the coefficients (if bound is set)
560 struct isl_wraps {
561 int bound;
562 isl_mat *mat;
563 isl_int max;
566 /* Update wraps->max to be greater than or equal to the coefficients
567 * in the equalities and inequalities of bmap that can be removed if we end up
568 * applying wrapping.
570 static void wraps_update_max(struct isl_wraps *wraps,
571 __isl_keep isl_basic_map *bmap, int *eq, int *ineq)
573 int k;
574 isl_int max_k;
575 unsigned total = isl_basic_map_total_dim(bmap);
577 isl_int_init(max_k);
579 for (k = 0; k < bmap->n_eq; ++k) {
580 if (eq[2 * k] == STATUS_VALID &&
581 eq[2 * k + 1] == STATUS_VALID)
582 continue;
583 isl_seq_abs_max(bmap->eq[k] + 1, total, &max_k);
584 if (isl_int_abs_gt(max_k, wraps->max))
585 isl_int_set(wraps->max, max_k);
588 for (k = 0; k < bmap->n_ineq; ++k) {
589 if (ineq[k] == STATUS_VALID || ineq[k] == STATUS_REDUNDANT)
590 continue;
591 isl_seq_abs_max(bmap->ineq[k] + 1, total, &max_k);
592 if (isl_int_abs_gt(max_k, wraps->max))
593 isl_int_set(wraps->max, max_k);
596 isl_int_clear(max_k);
599 /* Initialize the isl_wraps data structure.
600 * If we want to bound the coefficients of the wrapping constraints,
601 * we set wraps->max to the largest coefficient
602 * in the equalities and inequalities that can be removed if we end up
603 * applying wrapping.
605 static void wraps_init(struct isl_wraps *wraps, __isl_take isl_mat *mat,
606 __isl_keep isl_map *map, int i, int j,
607 int *eq_i, int *ineq_i, int *eq_j, int *ineq_j)
609 isl_ctx *ctx;
611 wraps->bound = 0;
612 wraps->mat = mat;
613 if (!mat)
614 return;
615 ctx = isl_mat_get_ctx(mat);
616 wraps->bound = isl_options_get_coalesce_bounded_wrapping(ctx);
617 if (!wraps->bound)
618 return;
619 isl_int_init(wraps->max);
620 isl_int_set_si(wraps->max, 0);
621 wraps_update_max(wraps, map->p[i], eq_i, ineq_i);
622 wraps_update_max(wraps, map->p[j], eq_j, ineq_j);
625 /* Free the contents of the isl_wraps data structure.
627 static void wraps_free(struct isl_wraps *wraps)
629 isl_mat_free(wraps->mat);
630 if (wraps->bound)
631 isl_int_clear(wraps->max);
634 /* Is the wrapping constraint in row "row" allowed?
636 * If wraps->bound is set, we check that none of the coefficients
637 * is greater than wraps->max.
639 static int allow_wrap(struct isl_wraps *wraps, int row)
641 int i;
643 if (!wraps->bound)
644 return 1;
646 for (i = 1; i < wraps->mat->n_col; ++i)
647 if (isl_int_abs_gt(wraps->mat->row[row][i], wraps->max))
648 return 0;
650 return 1;
653 /* For each non-redundant constraint in "bmap" (as determined by "tab"),
654 * wrap the constraint around "bound" such that it includes the whole
655 * set "set" and append the resulting constraint to "wraps".
656 * "wraps" is assumed to have been pre-allocated to the appropriate size.
657 * wraps->n_row is the number of actual wrapped constraints that have
658 * been added.
659 * If any of the wrapping problems results in a constraint that is
660 * identical to "bound", then this means that "set" is unbounded in such
661 * way that no wrapping is possible. If this happens then wraps->n_row
662 * is reset to zero.
663 * Similarly, if we want to bound the coefficients of the wrapping
664 * constraints and a newly added wrapping constraint does not
665 * satisfy the bound, then wraps->n_row is also reset to zero.
667 static int add_wraps(struct isl_wraps *wraps, __isl_keep isl_basic_map *bmap,
668 struct isl_tab *tab, isl_int *bound, __isl_keep isl_set *set)
670 int l;
671 int w;
672 unsigned total = isl_basic_map_total_dim(bmap);
674 w = wraps->mat->n_row;
676 for (l = 0; l < bmap->n_ineq; ++l) {
677 if (isl_seq_is_neg(bound, bmap->ineq[l], 1 + total))
678 continue;
679 if (isl_seq_eq(bound, bmap->ineq[l], 1 + total))
680 continue;
681 if (isl_tab_is_redundant(tab, bmap->n_eq + l))
682 continue;
684 isl_seq_cpy(wraps->mat->row[w], bound, 1 + total);
685 if (!isl_set_wrap_facet(set, wraps->mat->row[w], bmap->ineq[l]))
686 return -1;
687 if (isl_seq_eq(wraps->mat->row[w], bound, 1 + total))
688 goto unbounded;
689 if (!allow_wrap(wraps, w))
690 goto unbounded;
691 ++w;
693 for (l = 0; l < bmap->n_eq; ++l) {
694 if (isl_seq_is_neg(bound, bmap->eq[l], 1 + total))
695 continue;
696 if (isl_seq_eq(bound, bmap->eq[l], 1 + total))
697 continue;
699 isl_seq_cpy(wraps->mat->row[w], bound, 1 + total);
700 isl_seq_neg(wraps->mat->row[w + 1], bmap->eq[l], 1 + total);
701 if (!isl_set_wrap_facet(set, wraps->mat->row[w],
702 wraps->mat->row[w + 1]))
703 return -1;
704 if (isl_seq_eq(wraps->mat->row[w], bound, 1 + total))
705 goto unbounded;
706 if (!allow_wrap(wraps, w))
707 goto unbounded;
708 ++w;
710 isl_seq_cpy(wraps->mat->row[w], bound, 1 + total);
711 if (!isl_set_wrap_facet(set, wraps->mat->row[w], bmap->eq[l]))
712 return -1;
713 if (isl_seq_eq(wraps->mat->row[w], bound, 1 + total))
714 goto unbounded;
715 if (!allow_wrap(wraps, w))
716 goto unbounded;
717 ++w;
720 wraps->mat->n_row = w;
721 return 0;
722 unbounded:
723 wraps->mat->n_row = 0;
724 return 0;
727 /* Check if the constraints in "wraps" from "first" until the last
728 * are all valid for the basic set represented by "tab".
729 * If not, wraps->n_row is set to zero.
731 static int check_wraps(__isl_keep isl_mat *wraps, int first,
732 struct isl_tab *tab)
734 int i;
736 for (i = first; i < wraps->n_row; ++i) {
737 enum isl_ineq_type type;
738 type = isl_tab_ineq_type(tab, wraps->row[i]);
739 if (type == isl_ineq_error)
740 return -1;
741 if (type == isl_ineq_redundant)
742 continue;
743 wraps->n_row = 0;
744 return 0;
747 return 0;
750 /* Return a set that corresponds to the non-redundant constraints
751 * (as recorded in tab) of bmap.
753 * It's important to remove the redundant constraints as some
754 * of the other constraints may have been modified after the
755 * constraints were marked redundant.
756 * In particular, a constraint may have been relaxed.
757 * Redundant constraints are ignored when a constraint is relaxed
758 * and should therefore continue to be ignored ever after.
759 * Otherwise, the relaxation might be thwarted by some of
760 * these constraints.
762 static __isl_give isl_set *set_from_updated_bmap(__isl_keep isl_basic_map *bmap,
763 struct isl_tab *tab)
765 bmap = isl_basic_map_copy(bmap);
766 bmap = isl_basic_map_cow(bmap);
767 bmap = isl_basic_map_update_from_tab(bmap, tab);
768 return isl_set_from_basic_set(isl_basic_map_underlying_set(bmap));
771 /* Given a basic set i with a constraint k that is adjacent to
772 * basic set j, check if we can wrap
773 * both the facet corresponding to k and basic map j
774 * around their ridges to include the other set.
775 * If so, replace the pair of basic sets by their union.
777 * All constraints of i (except k) are assumed to be valid for j.
778 * This means that there is no real need to wrap the ridges of
779 * the faces of basic map i around basic map j but since we do,
780 * we have to check that the resulting wrapping constraints are valid for i.
781 * ____ _____
782 * / | / \
783 * / || / |
784 * \ || => \ |
785 * \ || \ |
786 * \___|| \____|
789 static int can_wrap_in_facet(struct isl_map *map, int i, int j, int k,
790 struct isl_tab **tabs, int *eq_i, int *ineq_i, int *eq_j, int *ineq_j)
792 int changed = 0;
793 struct isl_wraps wraps;
794 isl_mat *mat;
795 struct isl_set *set_i = NULL;
796 struct isl_set *set_j = NULL;
797 struct isl_vec *bound = NULL;
798 unsigned total = isl_basic_map_total_dim(map->p[i]);
799 struct isl_tab_undo *snap;
800 int n;
802 set_i = set_from_updated_bmap(map->p[i], tabs[i]);
803 set_j = set_from_updated_bmap(map->p[j], tabs[j]);
804 mat = isl_mat_alloc(map->ctx, 2 * (map->p[i]->n_eq + map->p[j]->n_eq) +
805 map->p[i]->n_ineq + map->p[j]->n_ineq,
806 1 + total);
807 wraps_init(&wraps, mat, map, i, j, eq_i, ineq_i, eq_j, ineq_j);
808 bound = isl_vec_alloc(map->ctx, 1 + total);
809 if (!set_i || !set_j || !wraps.mat || !bound)
810 goto error;
812 isl_seq_cpy(bound->el, map->p[i]->ineq[k], 1 + total);
813 isl_int_add_ui(bound->el[0], bound->el[0], 1);
815 isl_seq_cpy(wraps.mat->row[0], bound->el, 1 + total);
816 wraps.mat->n_row = 1;
818 if (add_wraps(&wraps, map->p[j], tabs[j], bound->el, set_i) < 0)
819 goto error;
820 if (!wraps.mat->n_row)
821 goto unbounded;
823 snap = isl_tab_snap(tabs[i]);
825 if (isl_tab_select_facet(tabs[i], map->p[i]->n_eq + k) < 0)
826 goto error;
827 if (isl_tab_detect_redundant(tabs[i]) < 0)
828 goto error;
830 isl_seq_neg(bound->el, map->p[i]->ineq[k], 1 + total);
832 n = wraps.mat->n_row;
833 if (add_wraps(&wraps, map->p[i], tabs[i], bound->el, set_j) < 0)
834 goto error;
836 if (isl_tab_rollback(tabs[i], snap) < 0)
837 goto error;
838 if (check_wraps(wraps.mat, n, tabs[i]) < 0)
839 goto error;
840 if (!wraps.mat->n_row)
841 goto unbounded;
843 changed = fuse(map, i, j, tabs, eq_i, ineq_i, eq_j, ineq_j, wraps.mat);
845 unbounded:
846 wraps_free(&wraps);
848 isl_set_free(set_i);
849 isl_set_free(set_j);
851 isl_vec_free(bound);
853 return changed;
854 error:
855 wraps_free(&wraps);
856 isl_vec_free(bound);
857 isl_set_free(set_i);
858 isl_set_free(set_j);
859 return -1;
862 /* Set the is_redundant property of the "n" constraints in "cuts",
863 * except "k" to "v".
864 * This is a fairly tricky operation as it bypasses isl_tab.c.
865 * The reason we want to temporarily mark some constraints redundant
866 * is that we want to ignore them in add_wraps.
868 * Initially all cut constraints are non-redundant, but the
869 * selection of a facet right before the call to this function
870 * may have made some of them redundant.
871 * Likewise, the same constraints are marked non-redundant
872 * in the second call to this function, before they are officially
873 * made non-redundant again in the subsequent rollback.
875 static void set_is_redundant(struct isl_tab *tab, unsigned n_eq,
876 int *cuts, int n, int k, int v)
878 int l;
880 for (l = 0; l < n; ++l) {
881 if (l == k)
882 continue;
883 tab->con[n_eq + cuts[l]].is_redundant = v;
887 /* Given a pair of basic maps i and j such that j sticks out
888 * of i at n cut constraints, each time by at most one,
889 * try to compute wrapping constraints and replace the two
890 * basic maps by a single basic map.
891 * The other constraints of i are assumed to be valid for j.
893 * The facets of i corresponding to the cut constraints are
894 * wrapped around their ridges, except those ridges determined
895 * by any of the other cut constraints.
896 * The intersections of cut constraints need to be ignored
897 * as the result of wrapping one cut constraint around another
898 * would result in a constraint cutting the union.
899 * In each case, the facets are wrapped to include the union
900 * of the two basic maps.
902 * The pieces of j that lie at an offset of exactly one from
903 * one of the cut constraints of i are wrapped around their edges.
904 * Here, there is no need to ignore intersections because we
905 * are wrapping around the union of the two basic maps.
907 * If any wrapping fails, i.e., if we cannot wrap to touch
908 * the union, then we give up.
909 * Otherwise, the pair of basic maps is replaced by their union.
911 static int wrap_in_facets(struct isl_map *map, int i, int j,
912 int *cuts, int n, struct isl_tab **tabs,
913 int *eq_i, int *ineq_i, int *eq_j, int *ineq_j)
915 int changed = 0;
916 struct isl_wraps wraps;
917 isl_mat *mat;
918 isl_set *set = NULL;
919 isl_vec *bound = NULL;
920 unsigned total = isl_basic_map_total_dim(map->p[i]);
921 int max_wrap;
922 int k;
923 struct isl_tab_undo *snap_i, *snap_j;
925 if (isl_tab_extend_cons(tabs[j], 1) < 0)
926 goto error;
928 max_wrap = 2 * (map->p[i]->n_eq + map->p[j]->n_eq) +
929 map->p[i]->n_ineq + map->p[j]->n_ineq;
930 max_wrap *= n;
932 set = isl_set_union(set_from_updated_bmap(map->p[i], tabs[i]),
933 set_from_updated_bmap(map->p[j], tabs[j]));
934 mat = isl_mat_alloc(map->ctx, max_wrap, 1 + total);
935 wraps_init(&wraps, mat, map, i, j, eq_i, ineq_i, eq_j, ineq_j);
936 bound = isl_vec_alloc(map->ctx, 1 + total);
937 if (!set || !wraps.mat || !bound)
938 goto error;
940 snap_i = isl_tab_snap(tabs[i]);
941 snap_j = isl_tab_snap(tabs[j]);
943 wraps.mat->n_row = 0;
945 for (k = 0; k < n; ++k) {
946 if (isl_tab_select_facet(tabs[i], map->p[i]->n_eq + cuts[k]) < 0)
947 goto error;
948 if (isl_tab_detect_redundant(tabs[i]) < 0)
949 goto error;
950 set_is_redundant(tabs[i], map->p[i]->n_eq, cuts, n, k, 1);
952 isl_seq_neg(bound->el, map->p[i]->ineq[cuts[k]], 1 + total);
953 if (!tabs[i]->empty &&
954 add_wraps(&wraps, map->p[i], tabs[i], bound->el, set) < 0)
955 goto error;
957 set_is_redundant(tabs[i], map->p[i]->n_eq, cuts, n, k, 0);
958 if (isl_tab_rollback(tabs[i], snap_i) < 0)
959 goto error;
961 if (tabs[i]->empty)
962 break;
963 if (!wraps.mat->n_row)
964 break;
966 isl_seq_cpy(bound->el, map->p[i]->ineq[cuts[k]], 1 + total);
967 isl_int_add_ui(bound->el[0], bound->el[0], 1);
968 if (isl_tab_add_eq(tabs[j], bound->el) < 0)
969 goto error;
970 if (isl_tab_detect_redundant(tabs[j]) < 0)
971 goto error;
973 if (!tabs[j]->empty &&
974 add_wraps(&wraps, map->p[j], tabs[j], bound->el, set) < 0)
975 goto error;
977 if (isl_tab_rollback(tabs[j], snap_j) < 0)
978 goto error;
980 if (!wraps.mat->n_row)
981 break;
984 if (k == n)
985 changed = fuse(map, i, j, tabs,
986 eq_i, ineq_i, eq_j, ineq_j, wraps.mat);
988 isl_vec_free(bound);
989 wraps_free(&wraps);
990 isl_set_free(set);
992 return changed;
993 error:
994 isl_vec_free(bound);
995 wraps_free(&wraps);
996 isl_set_free(set);
997 return -1;
1000 /* Given two basic sets i and j such that i has no cut equalities,
1001 * check if relaxing all the cut inequalities of i by one turns
1002 * them into valid constraint for j and check if we can wrap in
1003 * the bits that are sticking out.
1004 * If so, replace the pair by their union.
1006 * We first check if all relaxed cut inequalities of i are valid for j
1007 * and then try to wrap in the intersections of the relaxed cut inequalities
1008 * with j.
1010 * During this wrapping, we consider the points of j that lie at a distance
1011 * of exactly 1 from i. In particular, we ignore the points that lie in
1012 * between this lower-dimensional space and the basic map i.
1013 * We can therefore only apply this to integer maps.
1014 * ____ _____
1015 * / ___|_ / \
1016 * / | | / |
1017 * \ | | => \ |
1018 * \|____| \ |
1019 * \___| \____/
1021 * _____ ______
1022 * | ____|_ | \
1023 * | | | | |
1024 * | | | => | |
1025 * |_| | | |
1026 * |_____| \______|
1028 * _______
1029 * | |
1030 * | |\ |
1031 * | | \ |
1032 * | | \ |
1033 * | | \|
1034 * | | \
1035 * | |_____\
1036 * | |
1037 * |_______|
1039 * Wrapping can fail if the result of wrapping one of the facets
1040 * around its edges does not produce any new facet constraint.
1041 * In particular, this happens when we try to wrap in unbounded sets.
1043 * _______________________________________________________________________
1045 * | ___
1046 * | | |
1047 * |_| |_________________________________________________________________
1048 * |___|
1050 * The following is not an acceptable result of coalescing the above two
1051 * sets as it includes extra integer points.
1052 * _______________________________________________________________________
1054 * |
1055 * |
1057 * \______________________________________________________________________
1059 static int can_wrap_in_set(struct isl_map *map, int i, int j,
1060 struct isl_tab **tabs, int *eq_i, int *ineq_i, int *eq_j, int *ineq_j)
1062 int changed = 0;
1063 int k, m;
1064 int n;
1065 int *cuts = NULL;
1067 if (ISL_F_ISSET(map->p[i], ISL_BASIC_MAP_RATIONAL) ||
1068 ISL_F_ISSET(map->p[j], ISL_BASIC_MAP_RATIONAL))
1069 return 0;
1071 n = count(ineq_i, map->p[i]->n_ineq, STATUS_CUT);
1072 if (n == 0)
1073 return 0;
1075 cuts = isl_alloc_array(map->ctx, int, n);
1076 if (!cuts)
1077 return -1;
1079 for (k = 0, m = 0; m < n; ++k) {
1080 enum isl_ineq_type type;
1082 if (ineq_i[k] != STATUS_CUT)
1083 continue;
1085 isl_int_add_ui(map->p[i]->ineq[k][0], map->p[i]->ineq[k][0], 1);
1086 type = isl_tab_ineq_type(tabs[j], map->p[i]->ineq[k]);
1087 isl_int_sub_ui(map->p[i]->ineq[k][0], map->p[i]->ineq[k][0], 1);
1088 if (type == isl_ineq_error)
1089 goto error;
1090 if (type != isl_ineq_redundant)
1091 break;
1092 cuts[m] = k;
1093 ++m;
1096 if (m == n)
1097 changed = wrap_in_facets(map, i, j, cuts, n, tabs,
1098 eq_i, ineq_i, eq_j, ineq_j);
1100 free(cuts);
1102 return changed;
1103 error:
1104 free(cuts);
1105 return -1;
1108 /* Check if either i or j has only cut inequalities that can
1109 * be used to wrap in (a facet of) the other basic set.
1110 * if so, replace the pair by their union.
1112 static int check_wrap(struct isl_map *map, int i, int j,
1113 struct isl_tab **tabs, int *eq_i, int *ineq_i, int *eq_j, int *ineq_j)
1115 int changed = 0;
1117 if (!any(eq_i, 2 * map->p[i]->n_eq, STATUS_CUT))
1118 changed = can_wrap_in_set(map, i, j, tabs,
1119 eq_i, ineq_i, eq_j, ineq_j);
1120 if (changed)
1121 return changed;
1123 if (!any(eq_j, 2 * map->p[j]->n_eq, STATUS_CUT))
1124 changed = can_wrap_in_set(map, j, i, tabs,
1125 eq_j, ineq_j, eq_i, ineq_i);
1126 return changed;
1129 /* At least one of the basic maps has an equality that is adjacent
1130 * to inequality. Make sure that only one of the basic maps has
1131 * such an equality and that the other basic map has exactly one
1132 * inequality adjacent to an equality.
1133 * We call the basic map that has the inequality "i" and the basic
1134 * map that has the equality "j".
1135 * If "i" has any "cut" (in)equality, then relaxing the inequality
1136 * by one would not result in a basic map that contains the other
1137 * basic map.
1139 static int check_adj_eq(struct isl_map *map, int i, int j,
1140 struct isl_tab **tabs, int *eq_i, int *ineq_i, int *eq_j, int *ineq_j)
1142 int changed = 0;
1143 int k;
1145 if (any(eq_i, 2 * map->p[i]->n_eq, STATUS_ADJ_INEQ) &&
1146 any(eq_j, 2 * map->p[j]->n_eq, STATUS_ADJ_INEQ))
1147 /* ADJ EQ TOO MANY */
1148 return 0;
1150 if (any(eq_i, 2 * map->p[i]->n_eq, STATUS_ADJ_INEQ))
1151 return check_adj_eq(map, j, i, tabs,
1152 eq_j, ineq_j, eq_i, ineq_i);
1154 /* j has an equality adjacent to an inequality in i */
1156 if (any(eq_i, 2 * map->p[i]->n_eq, STATUS_CUT))
1157 return 0;
1158 if (any(ineq_i, map->p[i]->n_ineq, STATUS_CUT))
1159 /* ADJ EQ CUT */
1160 return 0;
1161 if (count(ineq_i, map->p[i]->n_ineq, STATUS_ADJ_EQ) != 1 ||
1162 any(ineq_j, map->p[j]->n_ineq, STATUS_ADJ_EQ) ||
1163 any(ineq_i, map->p[i]->n_ineq, STATUS_ADJ_INEQ) ||
1164 any(ineq_j, map->p[j]->n_ineq, STATUS_ADJ_INEQ))
1165 /* ADJ EQ TOO MANY */
1166 return 0;
1168 for (k = 0; k < map->p[i]->n_ineq; ++k)
1169 if (ineq_i[k] == STATUS_ADJ_EQ)
1170 break;
1172 changed = is_adj_eq_extension(map, i, j, k, tabs,
1173 eq_i, ineq_i, eq_j, ineq_j);
1174 if (changed)
1175 return changed;
1177 if (count(eq_j, 2 * map->p[j]->n_eq, STATUS_ADJ_INEQ) != 1)
1178 return 0;
1180 changed = can_wrap_in_facet(map, i, j, k, tabs, eq_i, ineq_i, eq_j, ineq_j);
1182 return changed;
1185 /* The two basic maps lie on adjacent hyperplanes. In particular,
1186 * basic map "i" has an equality that lies parallel to basic map "j".
1187 * Check if we can wrap the facets around the parallel hyperplanes
1188 * to include the other set.
1190 * We perform basically the same operations as can_wrap_in_facet,
1191 * except that we don't need to select a facet of one of the sets.
1193 * \\ \\
1194 * \\ => \\
1195 * \ \|
1197 * We only allow one equality of "i" to be adjacent to an equality of "j"
1198 * to avoid coalescing
1200 * [m, n] -> { [x, y] -> [x, 1 + y] : x >= 1 and y >= 1 and
1201 * x <= 10 and y <= 10;
1202 * [x, y] -> [1 + x, y] : x >= 1 and x <= 20 and
1203 * y >= 5 and y <= 15 }
1205 * to
1207 * [m, n] -> { [x, y] -> [x2, y2] : x >= 1 and 10y2 <= 20 - x + 10y and
1208 * 4y2 >= 5 + 3y and 5y2 <= 15 + 4y and
1209 * y2 <= 1 + x + y - x2 and y2 >= y and
1210 * y2 >= 1 + x + y - x2 }
1212 static int check_eq_adj_eq(struct isl_map *map, int i, int j,
1213 struct isl_tab **tabs, int *eq_i, int *ineq_i, int *eq_j, int *ineq_j)
1215 int k;
1216 int changed = 0;
1217 struct isl_wraps wraps;
1218 isl_mat *mat;
1219 struct isl_set *set_i = NULL;
1220 struct isl_set *set_j = NULL;
1221 struct isl_vec *bound = NULL;
1222 unsigned total = isl_basic_map_total_dim(map->p[i]);
1224 if (count(eq_i, 2 * map->p[i]->n_eq, STATUS_ADJ_EQ) != 1)
1225 return 0;
1227 for (k = 0; k < 2 * map->p[i]->n_eq ; ++k)
1228 if (eq_i[k] == STATUS_ADJ_EQ)
1229 break;
1231 set_i = set_from_updated_bmap(map->p[i], tabs[i]);
1232 set_j = set_from_updated_bmap(map->p[j], tabs[j]);
1233 mat = isl_mat_alloc(map->ctx, 2 * (map->p[i]->n_eq + map->p[j]->n_eq) +
1234 map->p[i]->n_ineq + map->p[j]->n_ineq,
1235 1 + total);
1236 wraps_init(&wraps, mat, map, i, j, eq_i, ineq_i, eq_j, ineq_j);
1237 bound = isl_vec_alloc(map->ctx, 1 + total);
1238 if (!set_i || !set_j || !wraps.mat || !bound)
1239 goto error;
1241 if (k % 2 == 0)
1242 isl_seq_neg(bound->el, map->p[i]->eq[k / 2], 1 + total);
1243 else
1244 isl_seq_cpy(bound->el, map->p[i]->eq[k / 2], 1 + total);
1245 isl_int_add_ui(bound->el[0], bound->el[0], 1);
1247 isl_seq_cpy(wraps.mat->row[0], bound->el, 1 + total);
1248 wraps.mat->n_row = 1;
1250 if (add_wraps(&wraps, map->p[j], tabs[j], bound->el, set_i) < 0)
1251 goto error;
1252 if (!wraps.mat->n_row)
1253 goto unbounded;
1255 isl_int_sub_ui(bound->el[0], bound->el[0], 1);
1256 isl_seq_neg(bound->el, bound->el, 1 + total);
1258 isl_seq_cpy(wraps.mat->row[wraps.mat->n_row], bound->el, 1 + total);
1259 wraps.mat->n_row++;
1261 if (add_wraps(&wraps, map->p[i], tabs[i], bound->el, set_j) < 0)
1262 goto error;
1263 if (!wraps.mat->n_row)
1264 goto unbounded;
1266 changed = fuse(map, i, j, tabs, eq_i, ineq_i, eq_j, ineq_j, wraps.mat);
1268 if (0) {
1269 error: changed = -1;
1271 unbounded:
1273 wraps_free(&wraps);
1274 isl_set_free(set_i);
1275 isl_set_free(set_j);
1276 isl_vec_free(bound);
1278 return changed;
1281 /* Check if the union of the given pair of basic maps
1282 * can be represented by a single basic map.
1283 * If so, replace the pair by the single basic map and return 1.
1284 * Otherwise, return 0;
1285 * The two basic maps are assumed to live in the same local space.
1287 * We first check the effect of each constraint of one basic map
1288 * on the other basic map.
1289 * The constraint may be
1290 * redundant the constraint is redundant in its own
1291 * basic map and should be ignore and removed
1292 * in the end
1293 * valid all (integer) points of the other basic map
1294 * satisfy the constraint
1295 * separate no (integer) point of the other basic map
1296 * satisfies the constraint
1297 * cut some but not all points of the other basic map
1298 * satisfy the constraint
1299 * adj_eq the given constraint is adjacent (on the outside)
1300 * to an equality of the other basic map
1301 * adj_ineq the given constraint is adjacent (on the outside)
1302 * to an inequality of the other basic map
1304 * We consider seven cases in which we can replace the pair by a single
1305 * basic map. We ignore all "redundant" constraints.
1307 * 1. all constraints of one basic map are valid
1308 * => the other basic map is a subset and can be removed
1310 * 2. all constraints of both basic maps are either "valid" or "cut"
1311 * and the facets corresponding to the "cut" constraints
1312 * of one of the basic maps lies entirely inside the other basic map
1313 * => the pair can be replaced by a basic map consisting
1314 * of the valid constraints in both basic maps
1316 * 3. there is a single pair of adjacent inequalities
1317 * (all other constraints are "valid")
1318 * => the pair can be replaced by a basic map consisting
1319 * of the valid constraints in both basic maps
1321 * 4. one basic map has a single adjacent inequality, while the other
1322 * constraints are "valid". The other basic map has some
1323 * "cut" constraints, but replacing the adjacent inequality by
1324 * its opposite and adding the valid constraints of the other
1325 * basic map results in a subset of the other basic map
1326 * => the pair can be replaced by a basic map consisting
1327 * of the valid constraints in both basic maps
1329 * 5. there is a single adjacent pair of an inequality and an equality,
1330 * the other constraints of the basic map containing the inequality are
1331 * "valid". Moreover, if the inequality the basic map is relaxed
1332 * and then turned into an equality, then resulting facet lies
1333 * entirely inside the other basic map
1334 * => the pair can be replaced by the basic map containing
1335 * the inequality, with the inequality relaxed.
1337 * 6. there is a single adjacent pair of an inequality and an equality,
1338 * the other constraints of the basic map containing the inequality are
1339 * "valid". Moreover, the facets corresponding to both
1340 * the inequality and the equality can be wrapped around their
1341 * ridges to include the other basic map
1342 * => the pair can be replaced by a basic map consisting
1343 * of the valid constraints in both basic maps together
1344 * with all wrapping constraints
1346 * 7. one of the basic maps extends beyond the other by at most one.
1347 * Moreover, the facets corresponding to the cut constraints and
1348 * the pieces of the other basic map at offset one from these cut
1349 * constraints can be wrapped around their ridges to include
1350 * the union of the two basic maps
1351 * => the pair can be replaced by a basic map consisting
1352 * of the valid constraints in both basic maps together
1353 * with all wrapping constraints
1355 * 8. the two basic maps live in adjacent hyperplanes. In principle
1356 * such sets can always be combined through wrapping, but we impose
1357 * that there is only one such pair, to avoid overeager coalescing.
1359 * Throughout the computation, we maintain a collection of tableaus
1360 * corresponding to the basic maps. When the basic maps are dropped
1361 * or combined, the tableaus are modified accordingly.
1363 static int coalesce_local_pair(__isl_keep isl_map *map, int i, int j,
1364 struct isl_tab **tabs)
1366 int changed = 0;
1367 int *eq_i = NULL;
1368 int *eq_j = NULL;
1369 int *ineq_i = NULL;
1370 int *ineq_j = NULL;
1372 eq_i = eq_status_in(map->p[i], tabs[j]);
1373 if (map->p[i]->n_eq && !eq_i)
1374 goto error;
1375 if (any(eq_i, 2 * map->p[i]->n_eq, STATUS_ERROR))
1376 goto error;
1377 if (any(eq_i, 2 * map->p[i]->n_eq, STATUS_SEPARATE))
1378 goto done;
1380 eq_j = eq_status_in(map->p[j], tabs[i]);
1381 if (map->p[j]->n_eq && !eq_j)
1382 goto error;
1383 if (any(eq_j, 2 * map->p[j]->n_eq, STATUS_ERROR))
1384 goto error;
1385 if (any(eq_j, 2 * map->p[j]->n_eq, STATUS_SEPARATE))
1386 goto done;
1388 ineq_i = ineq_status_in(map->p[i], tabs[i], tabs[j]);
1389 if (map->p[i]->n_ineq && !ineq_i)
1390 goto error;
1391 if (any(ineq_i, map->p[i]->n_ineq, STATUS_ERROR))
1392 goto error;
1393 if (any(ineq_i, map->p[i]->n_ineq, STATUS_SEPARATE))
1394 goto done;
1396 ineq_j = ineq_status_in(map->p[j], tabs[j], tabs[i]);
1397 if (map->p[j]->n_ineq && !ineq_j)
1398 goto error;
1399 if (any(ineq_j, map->p[j]->n_ineq, STATUS_ERROR))
1400 goto error;
1401 if (any(ineq_j, map->p[j]->n_ineq, STATUS_SEPARATE))
1402 goto done;
1404 if (all(eq_i, 2 * map->p[i]->n_eq, STATUS_VALID) &&
1405 all(ineq_i, map->p[i]->n_ineq, STATUS_VALID)) {
1406 drop(map, j, tabs);
1407 changed = 1;
1408 } else if (all(eq_j, 2 * map->p[j]->n_eq, STATUS_VALID) &&
1409 all(ineq_j, map->p[j]->n_ineq, STATUS_VALID)) {
1410 drop(map, i, tabs);
1411 changed = 1;
1412 } else if (any(eq_i, 2 * map->p[i]->n_eq, STATUS_ADJ_EQ)) {
1413 changed = check_eq_adj_eq(map, i, j, tabs,
1414 eq_i, ineq_i, eq_j, ineq_j);
1415 } else if (any(eq_j, 2 * map->p[j]->n_eq, STATUS_ADJ_EQ)) {
1416 changed = check_eq_adj_eq(map, j, i, tabs,
1417 eq_j, ineq_j, eq_i, ineq_i);
1418 } else if (any(eq_i, 2 * map->p[i]->n_eq, STATUS_ADJ_INEQ) ||
1419 any(eq_j, 2 * map->p[j]->n_eq, STATUS_ADJ_INEQ)) {
1420 changed = check_adj_eq(map, i, j, tabs,
1421 eq_i, ineq_i, eq_j, ineq_j);
1422 } else if (any(ineq_i, map->p[i]->n_ineq, STATUS_ADJ_EQ) ||
1423 any(ineq_j, map->p[j]->n_ineq, STATUS_ADJ_EQ)) {
1424 /* Can't happen */
1425 /* BAD ADJ INEQ */
1426 } else if (any(ineq_i, map->p[i]->n_ineq, STATUS_ADJ_INEQ) ||
1427 any(ineq_j, map->p[j]->n_ineq, STATUS_ADJ_INEQ)) {
1428 changed = check_adj_ineq(map, i, j, tabs,
1429 eq_i, ineq_i, eq_j, ineq_j);
1430 } else {
1431 if (!any(eq_i, 2 * map->p[i]->n_eq, STATUS_CUT) &&
1432 !any(eq_j, 2 * map->p[j]->n_eq, STATUS_CUT))
1433 changed = check_facets(map, i, j, tabs, ineq_i, ineq_j);
1434 if (!changed)
1435 changed = check_wrap(map, i, j, tabs,
1436 eq_i, ineq_i, eq_j, ineq_j);
1439 done:
1440 free(eq_i);
1441 free(eq_j);
1442 free(ineq_i);
1443 free(ineq_j);
1444 return changed;
1445 error:
1446 free(eq_i);
1447 free(eq_j);
1448 free(ineq_i);
1449 free(ineq_j);
1450 return -1;
1453 /* Do the two basic maps live in the same local space, i.e.,
1454 * do they have the same (known) divs?
1455 * If either basic map has any unknown divs, then we can only assume
1456 * that they do not live in the same local space.
1458 static int same_divs(__isl_keep isl_basic_map *bmap1,
1459 __isl_keep isl_basic_map *bmap2)
1461 int i;
1462 int known;
1463 int total;
1465 if (!bmap1 || !bmap2)
1466 return -1;
1467 if (bmap1->n_div != bmap2->n_div)
1468 return 0;
1470 if (bmap1->n_div == 0)
1471 return 1;
1473 known = isl_basic_map_divs_known(bmap1);
1474 if (known < 0 || !known)
1475 return known;
1476 known = isl_basic_map_divs_known(bmap2);
1477 if (known < 0 || !known)
1478 return known;
1480 total = isl_basic_map_total_dim(bmap1);
1481 for (i = 0; i < bmap1->n_div; ++i)
1482 if (!isl_seq_eq(bmap1->div[i], bmap2->div[i], 2 + total))
1483 return 0;
1485 return 1;
1488 /* Given two basic maps "i" and "j", where the divs of "i" form a subset
1489 * of those of "j", check if basic map "j" is a subset of basic map "i"
1490 * and, if so, drop basic map "j".
1492 * We first expand the divs of basic map "i" to match those of basic map "j",
1493 * using the divs and expansion computed by the caller.
1494 * Then we check if all constraints of the expanded "i" are valid for "j".
1496 static int coalesce_subset(__isl_keep isl_map *map, int i, int j,
1497 struct isl_tab **tabs, __isl_keep isl_mat *div, int *exp)
1499 isl_basic_map *bmap;
1500 int changed = 0;
1501 int *eq_i = NULL;
1502 int *ineq_i = NULL;
1504 bmap = isl_basic_map_copy(map->p[i]);
1505 bmap = isl_basic_set_expand_divs(bmap, isl_mat_copy(div), exp);
1507 if (!bmap)
1508 goto error;
1510 eq_i = eq_status_in(bmap, tabs[j]);
1511 if (bmap->n_eq && !eq_i)
1512 goto error;
1513 if (any(eq_i, 2 * bmap->n_eq, STATUS_ERROR))
1514 goto error;
1515 if (any(eq_i, 2 * bmap->n_eq, STATUS_SEPARATE))
1516 goto done;
1518 ineq_i = ineq_status_in(bmap, NULL, tabs[j]);
1519 if (bmap->n_ineq && !ineq_i)
1520 goto error;
1521 if (any(ineq_i, bmap->n_ineq, STATUS_ERROR))
1522 goto error;
1523 if (any(ineq_i, bmap->n_ineq, STATUS_SEPARATE))
1524 goto done;
1526 if (all(eq_i, 2 * map->p[i]->n_eq, STATUS_VALID) &&
1527 all(ineq_i, map->p[i]->n_ineq, STATUS_VALID)) {
1528 drop(map, j, tabs);
1529 changed = 1;
1532 done:
1533 isl_basic_map_free(bmap);
1534 free(eq_i);
1535 free(ineq_i);
1536 return 0;
1537 error:
1538 isl_basic_map_free(bmap);
1539 free(eq_i);
1540 free(ineq_i);
1541 return -1;
1544 /* Check if the basic map "j" is a subset of basic map "i",
1545 * assuming that "i" has fewer divs that "j".
1546 * If not, then we change the order.
1548 * If the two basic maps have the same number of divs, then
1549 * they must necessarily be different. Otherwise, we would have
1550 * called coalesce_local_pair. We therefore don't try anything
1551 * in this case.
1553 * We first check if the divs of "i" are all known and form a subset
1554 * of those of "j". If so, we pass control over to coalesce_subset.
1556 static int check_coalesce_subset(__isl_keep isl_map *map, int i, int j,
1557 struct isl_tab **tabs)
1559 int known;
1560 isl_mat *div_i, *div_j, *div;
1561 int *exp1 = NULL;
1562 int *exp2 = NULL;
1563 isl_ctx *ctx;
1564 int subset;
1566 if (map->p[i]->n_div == map->p[j]->n_div)
1567 return 0;
1568 if (map->p[j]->n_div < map->p[i]->n_div)
1569 return check_coalesce_subset(map, j, i, tabs);
1571 known = isl_basic_map_divs_known(map->p[i]);
1572 if (known < 0 || !known)
1573 return known;
1575 ctx = isl_map_get_ctx(map);
1577 div_i = isl_basic_map_get_divs(map->p[i]);
1578 div_j = isl_basic_map_get_divs(map->p[j]);
1580 if (!div_i || !div_j)
1581 goto error;
1583 exp1 = isl_alloc_array(ctx, int, div_i->n_row);
1584 exp2 = isl_alloc_array(ctx, int, div_j->n_row);
1585 if ((div_i->n_row && !exp1) || (div_j->n_row && !exp2))
1586 goto error;
1588 div = isl_merge_divs(div_i, div_j, exp1, exp2);
1589 if (!div)
1590 goto error;
1592 if (div->n_row == div_j->n_row)
1593 subset = coalesce_subset(map, i, j, tabs, div, exp1);
1594 else
1595 subset = 0;
1597 isl_mat_free(div);
1599 isl_mat_free(div_i);
1600 isl_mat_free(div_j);
1602 free(exp2);
1603 free(exp1);
1605 return subset;
1606 error:
1607 isl_mat_free(div_i);
1608 isl_mat_free(div_j);
1609 free(exp1);
1610 free(exp2);
1611 return -1;
1614 /* Check if the union of the given pair of basic maps
1615 * can be represented by a single basic map.
1616 * If so, replace the pair by the single basic map and return 1.
1617 * Otherwise, return 0;
1619 * We first check if the two basic maps live in the same local space.
1620 * If so, we do the complete check. Otherwise, we check if one is
1621 * an obvious subset of the other.
1623 static int coalesce_pair(__isl_keep isl_map *map, int i, int j,
1624 struct isl_tab **tabs)
1626 int same;
1628 same = same_divs(map->p[i], map->p[j]);
1629 if (same < 0)
1630 return -1;
1631 if (same)
1632 return coalesce_local_pair(map, i, j, tabs);
1634 return check_coalesce_subset(map, i, j, tabs);
1637 static struct isl_map *coalesce(struct isl_map *map, struct isl_tab **tabs)
1639 int i, j;
1641 for (i = map->n - 2; i >= 0; --i)
1642 restart:
1643 for (j = i + 1; j < map->n; ++j) {
1644 int changed;
1645 changed = coalesce_pair(map, i, j, tabs);
1646 if (changed < 0)
1647 goto error;
1648 if (changed)
1649 goto restart;
1651 return map;
1652 error:
1653 isl_map_free(map);
1654 return NULL;
1657 /* For each pair of basic maps in the map, check if the union of the two
1658 * can be represented by a single basic map.
1659 * If so, replace the pair by the single basic map and start over.
1661 * Since we are constructing the tableaus of the basic maps anyway,
1662 * we exploit them to detect implicit equalities and redundant constraints.
1663 * This also helps the coalescing as it can ignore the redundant constraints.
1664 * In order to avoid confusion, we make all implicit equalities explicit
1665 * in the basic maps. We don't call isl_basic_map_gauss, though,
1666 * as that may affect the number of constraints.
1667 * This means that we have to call isl_basic_map_gauss at the end
1668 * of the computation to ensure that the basic maps are not left
1669 * in an unexpected state.
1671 struct isl_map *isl_map_coalesce(struct isl_map *map)
1673 int i;
1674 unsigned n;
1675 struct isl_tab **tabs = NULL;
1677 map = isl_map_remove_empty_parts(map);
1678 if (!map)
1679 return NULL;
1681 if (map->n <= 1)
1682 return map;
1684 map = isl_map_sort_divs(map);
1685 map = isl_map_cow(map);
1687 if (!map)
1688 return NULL;
1690 tabs = isl_calloc_array(map->ctx, struct isl_tab *, map->n);
1691 if (!tabs)
1692 goto error;
1694 n = map->n;
1695 for (i = 0; i < map->n; ++i) {
1696 tabs[i] = isl_tab_from_basic_map(map->p[i], 0);
1697 if (!tabs[i])
1698 goto error;
1699 if (!ISL_F_ISSET(map->p[i], ISL_BASIC_MAP_NO_IMPLICIT))
1700 if (isl_tab_detect_implicit_equalities(tabs[i]) < 0)
1701 goto error;
1702 map->p[i] = isl_tab_make_equalities_explicit(tabs[i],
1703 map->p[i]);
1704 if (!map->p[i])
1705 goto error;
1706 if (!ISL_F_ISSET(map->p[i], ISL_BASIC_MAP_NO_REDUNDANT))
1707 if (isl_tab_detect_redundant(tabs[i]) < 0)
1708 goto error;
1710 for (i = map->n - 1; i >= 0; --i)
1711 if (tabs[i]->empty)
1712 drop(map, i, tabs);
1714 map = coalesce(map, tabs);
1716 if (map)
1717 for (i = 0; i < map->n; ++i) {
1718 map->p[i] = isl_basic_map_update_from_tab(map->p[i],
1719 tabs[i]);
1720 map->p[i] = isl_basic_map_gauss(map->p[i], NULL);
1721 map->p[i] = isl_basic_map_finalize(map->p[i]);
1722 if (!map->p[i])
1723 goto error;
1724 ISL_F_SET(map->p[i], ISL_BASIC_MAP_NO_IMPLICIT);
1725 ISL_F_SET(map->p[i], ISL_BASIC_MAP_NO_REDUNDANT);
1728 for (i = 0; i < n; ++i)
1729 isl_tab_free(tabs[i]);
1731 free(tabs);
1733 return map;
1734 error:
1735 if (tabs)
1736 for (i = 0; i < n; ++i)
1737 isl_tab_free(tabs[i]);
1738 free(tabs);
1739 isl_map_free(map);
1740 return NULL;
1743 /* For each pair of basic sets in the set, check if the union of the two
1744 * can be represented by a single basic set.
1745 * If so, replace the pair by the single basic set and start over.
1747 struct isl_set *isl_set_coalesce(struct isl_set *set)
1749 return (struct isl_set *)isl_map_coalesce((struct isl_map *)set);