Merge branch 'maint'
[isl.git] / isl_coalesce.c
blobfb2e5cfe5e4369bb49ff73b4873dee1453fad037
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 dim = isl_basic_map_total_dim(bmap_i);
59 for (k = 0; k < bmap_i->n_eq; ++k) {
60 for (l = 0; l < 2; ++l) {
61 isl_seq_neg(bmap_i->eq[k], bmap_i->eq[k], 1+dim);
62 eq[2 * k + l] = status_in(bmap_i->eq[k], tab_j);
63 if (eq[2 * k + l] == STATUS_ERROR)
64 goto error;
66 if (eq[2 * k] == STATUS_SEPARATE ||
67 eq[2 * k + 1] == STATUS_SEPARATE)
68 break;
71 return eq;
72 error:
73 free(eq);
74 return NULL;
77 /* Compute the position of the inequalities of basic map "bmap_i"
78 * (also represented by "tab_i", if not NULL) with respect to the basic map
79 * represented by "tab_j".
81 static int *ineq_status_in(__isl_keep isl_basic_map *bmap_i,
82 struct isl_tab *tab_i, struct isl_tab *tab_j)
84 int k;
85 unsigned n_eq = bmap_i->n_eq;
86 int *ineq = isl_calloc_array(bmap_i->ctx, int, bmap_i->n_ineq);
88 for (k = 0; k < bmap_i->n_ineq; ++k) {
89 if (tab_i && isl_tab_is_redundant(tab_i, n_eq + k)) {
90 ineq[k] = STATUS_REDUNDANT;
91 continue;
93 ineq[k] = status_in(bmap_i->ineq[k], tab_j);
94 if (ineq[k] == STATUS_ERROR)
95 goto error;
96 if (ineq[k] == STATUS_SEPARATE)
97 break;
100 return ineq;
101 error:
102 free(ineq);
103 return NULL;
106 static int any(int *con, unsigned len, int status)
108 int i;
110 for (i = 0; i < len ; ++i)
111 if (con[i] == status)
112 return 1;
113 return 0;
116 static int count(int *con, unsigned len, int status)
118 int i;
119 int c = 0;
121 for (i = 0; i < len ; ++i)
122 if (con[i] == status)
123 c++;
124 return c;
127 static int all(int *con, unsigned len, int status)
129 int i;
131 for (i = 0; i < len ; ++i) {
132 if (con[i] == STATUS_REDUNDANT)
133 continue;
134 if (con[i] != status)
135 return 0;
137 return 1;
140 static void drop(struct isl_map *map, int i, struct isl_tab **tabs)
142 isl_basic_map_free(map->p[i]);
143 isl_tab_free(tabs[i]);
145 if (i != map->n - 1) {
146 map->p[i] = map->p[map->n - 1];
147 tabs[i] = tabs[map->n - 1];
149 tabs[map->n - 1] = NULL;
150 map->n--;
153 /* Replace the pair of basic maps i and j by the basic map bounded
154 * by the valid constraints in both basic maps and the constraint
155 * in extra (if not NULL).
157 static int fuse(struct isl_map *map, int i, int j,
158 struct isl_tab **tabs, int *eq_i, int *ineq_i, int *eq_j, int *ineq_j,
159 __isl_keep isl_mat *extra)
161 int k, l;
162 struct isl_basic_map *fused = NULL;
163 struct isl_tab *fused_tab = NULL;
164 unsigned total = isl_basic_map_total_dim(map->p[i]);
165 unsigned extra_rows = extra ? extra->n_row : 0;
167 fused = isl_basic_map_alloc_space(isl_space_copy(map->p[i]->dim),
168 map->p[i]->n_div,
169 map->p[i]->n_eq + map->p[j]->n_eq,
170 map->p[i]->n_ineq + map->p[j]->n_ineq + extra_rows);
171 if (!fused)
172 goto error;
174 for (k = 0; k < map->p[i]->n_eq; ++k) {
175 if (eq_i && (eq_i[2 * k] != STATUS_VALID ||
176 eq_i[2 * k + 1] != STATUS_VALID))
177 continue;
178 l = isl_basic_map_alloc_equality(fused);
179 if (l < 0)
180 goto error;
181 isl_seq_cpy(fused->eq[l], map->p[i]->eq[k], 1 + total);
184 for (k = 0; k < map->p[j]->n_eq; ++k) {
185 if (eq_j && (eq_j[2 * k] != STATUS_VALID ||
186 eq_j[2 * k + 1] != STATUS_VALID))
187 continue;
188 l = isl_basic_map_alloc_equality(fused);
189 if (l < 0)
190 goto error;
191 isl_seq_cpy(fused->eq[l], map->p[j]->eq[k], 1 + total);
194 for (k = 0; k < map->p[i]->n_ineq; ++k) {
195 if (ineq_i[k] != STATUS_VALID)
196 continue;
197 l = isl_basic_map_alloc_inequality(fused);
198 if (l < 0)
199 goto error;
200 isl_seq_cpy(fused->ineq[l], map->p[i]->ineq[k], 1 + total);
203 for (k = 0; k < map->p[j]->n_ineq; ++k) {
204 if (ineq_j[k] != STATUS_VALID)
205 continue;
206 l = isl_basic_map_alloc_inequality(fused);
207 if (l < 0)
208 goto error;
209 isl_seq_cpy(fused->ineq[l], map->p[j]->ineq[k], 1 + total);
212 for (k = 0; k < map->p[i]->n_div; ++k) {
213 int l = isl_basic_map_alloc_div(fused);
214 if (l < 0)
215 goto error;
216 isl_seq_cpy(fused->div[l], map->p[i]->div[k], 1 + 1 + total);
219 for (k = 0; k < extra_rows; ++k) {
220 l = isl_basic_map_alloc_inequality(fused);
221 if (l < 0)
222 goto error;
223 isl_seq_cpy(fused->ineq[l], extra->row[k], 1 + total);
226 fused = isl_basic_map_gauss(fused, NULL);
227 ISL_F_SET(fused, ISL_BASIC_MAP_FINAL);
228 if (ISL_F_ISSET(map->p[i], ISL_BASIC_MAP_RATIONAL) &&
229 ISL_F_ISSET(map->p[j], ISL_BASIC_MAP_RATIONAL))
230 ISL_F_SET(fused, ISL_BASIC_MAP_RATIONAL);
232 fused_tab = isl_tab_from_basic_map(fused, 0);
233 if (isl_tab_detect_redundant(fused_tab) < 0)
234 goto error;
236 isl_basic_map_free(map->p[i]);
237 map->p[i] = fused;
238 isl_tab_free(tabs[i]);
239 tabs[i] = fused_tab;
240 drop(map, j, tabs);
242 return 1;
243 error:
244 isl_tab_free(fused_tab);
245 isl_basic_map_free(fused);
246 return -1;
249 /* Given a pair of basic maps i and j such that all constraints are either
250 * "valid" or "cut", check if the facets corresponding to the "cut"
251 * constraints of i lie entirely within basic map j.
252 * If so, replace the pair by the basic map consisting of the valid
253 * constraints in both basic maps.
255 * To see that we are not introducing any extra points, call the
256 * two basic maps A and B and the resulting map U and let x
257 * be an element of U \setminus ( A \cup B ).
258 * Then there is a pair of cut constraints c_1 and c_2 in A and B such that x
259 * violates them. Let X be the intersection of U with the opposites
260 * of these constraints. Then x \in X.
261 * The facet corresponding to c_1 contains the corresponding facet of A.
262 * This facet is entirely contained in B, so c_2 is valid on the facet.
263 * However, since it is also (part of) a facet of X, -c_2 is also valid
264 * on the facet. This means c_2 is saturated on the facet, so c_1 and
265 * c_2 must be opposites of each other, but then x could not violate
266 * both of them.
268 static int check_facets(struct isl_map *map, int i, int j,
269 struct isl_tab **tabs, int *ineq_i, int *ineq_j)
271 int k, l;
272 struct isl_tab_undo *snap;
273 unsigned n_eq = map->p[i]->n_eq;
275 snap = isl_tab_snap(tabs[i]);
277 for (k = 0; k < map->p[i]->n_ineq; ++k) {
278 if (ineq_i[k] != STATUS_CUT)
279 continue;
280 if (isl_tab_select_facet(tabs[i], n_eq + k) < 0)
281 return -1;
282 for (l = 0; l < map->p[j]->n_ineq; ++l) {
283 int stat;
284 if (ineq_j[l] != STATUS_CUT)
285 continue;
286 stat = status_in(map->p[j]->ineq[l], tabs[i]);
287 if (stat != STATUS_VALID)
288 break;
290 if (isl_tab_rollback(tabs[i], snap) < 0)
291 return -1;
292 if (l < map->p[j]->n_ineq)
293 break;
296 if (k < map->p[i]->n_ineq)
297 /* BAD CUT PAIR */
298 return 0;
299 return fuse(map, i, j, tabs, NULL, ineq_i, NULL, ineq_j, NULL);
302 /* Check if basic map "i" contains the basic map represented
303 * by the tableau "tab".
305 static int contains(struct isl_map *map, int i, int *ineq_i,
306 struct isl_tab *tab)
308 int k, l;
309 unsigned dim;
311 dim = isl_basic_map_total_dim(map->p[i]);
312 for (k = 0; k < map->p[i]->n_eq; ++k) {
313 for (l = 0; l < 2; ++l) {
314 int stat;
315 isl_seq_neg(map->p[i]->eq[k], map->p[i]->eq[k], 1+dim);
316 stat = status_in(map->p[i]->eq[k], tab);
317 if (stat != STATUS_VALID)
318 return 0;
322 for (k = 0; k < map->p[i]->n_ineq; ++k) {
323 int stat;
324 if (ineq_i[k] == STATUS_REDUNDANT)
325 continue;
326 stat = status_in(map->p[i]->ineq[k], tab);
327 if (stat != STATUS_VALID)
328 return 0;
330 return 1;
333 /* Basic map "i" has an inequality (say "k") that is adjacent
334 * to some inequality of basic map "j". All the other inequalities
335 * are valid for "j".
336 * Check if basic map "j" forms an extension of basic map "i".
338 * Note that this function is only called if some of the equalities or
339 * inequalities of basic map "j" do cut basic map "i". The function is
340 * correct even if there are no such cut constraints, but in that case
341 * the additional checks performed by this function are overkill.
343 * In particular, we replace constraint k, say f >= 0, by constraint
344 * f <= -1, add the inequalities of "j" that are valid for "i"
345 * and check if the result is a subset of basic map "j".
346 * If so, then we know that this result is exactly equal to basic map "j"
347 * since all its constraints are valid for basic map "j".
348 * By combining the valid constraints of "i" (all equalities and all
349 * inequalities except "k") and the valid constraints of "j" we therefore
350 * obtain a basic map that is equal to their union.
351 * In this case, there is no need to perform a rollback of the tableau
352 * since it is going to be destroyed in fuse().
355 * |\__ |\__
356 * | \__ | \__
357 * | \_ => | \__
358 * |_______| _ |_________\
361 * |\ |\
362 * | \ | \
363 * | \ | \
364 * | | | \
365 * | ||\ => | \
366 * | || \ | \
367 * | || | | |
368 * |__||_/ |_____/
370 static int is_adj_ineq_extension(__isl_keep isl_map *map, int i, int j,
371 struct isl_tab **tabs, int *eq_i, int *ineq_i, int *eq_j, int *ineq_j)
373 int k;
374 struct isl_tab_undo *snap;
375 unsigned n_eq = map->p[i]->n_eq;
376 unsigned total = isl_basic_map_total_dim(map->p[i]);
377 int r;
379 if (isl_tab_extend_cons(tabs[i], 1 + map->p[j]->n_ineq) < 0)
380 return -1;
382 for (k = 0; k < map->p[i]->n_ineq; ++k)
383 if (ineq_i[k] == STATUS_ADJ_INEQ)
384 break;
385 if (k >= map->p[i]->n_ineq)
386 isl_die(isl_map_get_ctx(map), isl_error_internal,
387 "ineq_i should have exactly one STATUS_ADJ_INEQ",
388 return -1);
390 snap = isl_tab_snap(tabs[i]);
392 if (isl_tab_unrestrict(tabs[i], n_eq + k) < 0)
393 return -1;
395 isl_seq_neg(map->p[i]->ineq[k], map->p[i]->ineq[k], 1 + total);
396 isl_int_sub_ui(map->p[i]->ineq[k][0], map->p[i]->ineq[k][0], 1);
397 r = isl_tab_add_ineq(tabs[i], map->p[i]->ineq[k]);
398 isl_seq_neg(map->p[i]->ineq[k], map->p[i]->ineq[k], 1 + total);
399 isl_int_sub_ui(map->p[i]->ineq[k][0], map->p[i]->ineq[k][0], 1);
400 if (r < 0)
401 return -1;
403 for (k = 0; k < map->p[j]->n_ineq; ++k) {
404 if (ineq_j[k] != STATUS_VALID)
405 continue;
406 if (isl_tab_add_ineq(tabs[i], map->p[j]->ineq[k]) < 0)
407 return -1;
410 if (contains(map, j, ineq_j, tabs[i]))
411 return fuse(map, i, j, tabs, eq_i, ineq_i, eq_j, ineq_j, NULL);
413 if (isl_tab_rollback(tabs[i], snap) < 0)
414 return -1;
416 return 0;
420 /* Both basic maps have at least one inequality with and adjacent
421 * (but opposite) inequality in the other basic map.
422 * Check that there are no cut constraints and that there is only
423 * a single pair of adjacent inequalities.
424 * If so, we can replace the pair by a single basic map described
425 * by all but the pair of adjacent inequalities.
426 * Any additional points introduced lie strictly between the two
427 * adjacent hyperplanes and can therefore be integral.
429 * ____ _____
430 * / ||\ / \
431 * / || \ / \
432 * \ || \ => \ \
433 * \ || / \ /
434 * \___||_/ \_____/
436 * The test for a single pair of adjancent inequalities is important
437 * for avoiding the combination of two basic maps like the following
439 * /|
440 * / |
441 * /__|
442 * _____
443 * | |
444 * | |
445 * |___|
447 * If there are some cut constraints on one side, then we may
448 * still be able to fuse the two basic maps, but we need to perform
449 * some additional checks in is_adj_ineq_extension.
451 static int check_adj_ineq(struct isl_map *map, int i, int j,
452 struct isl_tab **tabs, int *eq_i, int *ineq_i, int *eq_j, int *ineq_j)
454 int count_i, count_j;
455 int cut_i, cut_j;
457 count_i = count(ineq_i, map->p[i]->n_ineq, STATUS_ADJ_INEQ);
458 count_j = count(ineq_j, map->p[j]->n_ineq, STATUS_ADJ_INEQ);
460 if (count_i != 1 && count_j != 1)
461 return 0;
463 cut_i = any(eq_i, 2 * map->p[i]->n_eq, STATUS_CUT) ||
464 any(ineq_i, map->p[i]->n_ineq, STATUS_CUT);
465 cut_j = any(eq_j, 2 * map->p[j]->n_eq, STATUS_CUT) ||
466 any(ineq_j, map->p[j]->n_ineq, STATUS_CUT);
468 if (!cut_i && !cut_j && count_i == 1 && count_j == 1)
469 return fuse(map, i, j, tabs, NULL, ineq_i, NULL, ineq_j, NULL);
471 if (count_i == 1 && !cut_i)
472 return is_adj_ineq_extension(map, i, j, tabs,
473 eq_i, ineq_i, eq_j, ineq_j);
475 if (count_j == 1 && !cut_j)
476 return is_adj_ineq_extension(map, j, i, tabs,
477 eq_j, ineq_j, eq_i, ineq_i);
479 return 0;
482 /* Basic map "i" has an inequality "k" that is adjacent to some equality
483 * of basic map "j". All the other inequalities are valid for "j".
484 * Check if basic map "j" forms an extension of basic map "i".
486 * In particular, we relax constraint "k", compute the corresponding
487 * facet and check whether it is included in the other basic map.
488 * If so, we know that relaxing the constraint extends the basic
489 * map with exactly the other basic map (we already know that this
490 * other basic map is included in the extension, because there
491 * were no "cut" inequalities in "i") and we can replace the
492 * two basic maps by this extension.
493 * ____ _____
494 * / || / |
495 * / || / |
496 * \ || => \ |
497 * \ || \ |
498 * \___|| \____|
500 static int is_adj_eq_extension(struct isl_map *map, int i, int j, int k,
501 struct isl_tab **tabs, int *eq_i, int *ineq_i, int *eq_j, int *ineq_j)
503 int changed = 0;
504 int super;
505 struct isl_tab_undo *snap, *snap2;
506 unsigned n_eq = map->p[i]->n_eq;
508 if (isl_tab_is_equality(tabs[i], n_eq + k))
509 return 0;
511 snap = isl_tab_snap(tabs[i]);
512 tabs[i] = isl_tab_relax(tabs[i], n_eq + k);
513 snap2 = isl_tab_snap(tabs[i]);
514 if (isl_tab_select_facet(tabs[i], n_eq + k) < 0)
515 return -1;
516 super = contains(map, j, ineq_j, tabs[i]);
517 if (super) {
518 if (isl_tab_rollback(tabs[i], snap2) < 0)
519 return -1;
520 map->p[i] = isl_basic_map_cow(map->p[i]);
521 if (!map->p[i])
522 return -1;
523 isl_int_add_ui(map->p[i]->ineq[k][0], map->p[i]->ineq[k][0], 1);
524 ISL_F_SET(map->p[i], ISL_BASIC_MAP_FINAL);
525 drop(map, j, tabs);
526 changed = 1;
527 } else
528 if (isl_tab_rollback(tabs[i], snap) < 0)
529 return -1;
531 return changed;
534 /* Data structure that keeps track of the wrapping constraints
535 * and of information to bound the coefficients of those constraints.
537 * bound is set if we want to apply a bound on the coefficients
538 * mat contains the wrapping constraints
539 * max is the bound on the coefficients (if bound is set)
541 struct isl_wraps {
542 int bound;
543 isl_mat *mat;
544 isl_int max;
547 /* Update wraps->max to be greater than or equal to the coefficients
548 * in the equalities and inequalities of bmap that can be removed if we end up
549 * applying wrapping.
551 static void wraps_update_max(struct isl_wraps *wraps,
552 __isl_keep isl_basic_map *bmap, int *eq, int *ineq)
554 int k;
555 isl_int max_k;
556 unsigned total = isl_basic_map_total_dim(bmap);
558 isl_int_init(max_k);
560 for (k = 0; k < bmap->n_eq; ++k) {
561 if (eq[2 * k] == STATUS_VALID &&
562 eq[2 * k + 1] == STATUS_VALID)
563 continue;
564 isl_seq_abs_max(bmap->eq[k] + 1, total, &max_k);
565 if (isl_int_abs_gt(max_k, wraps->max))
566 isl_int_set(wraps->max, max_k);
569 for (k = 0; k < bmap->n_ineq; ++k) {
570 if (ineq[k] == STATUS_VALID || ineq[k] == STATUS_REDUNDANT)
571 continue;
572 isl_seq_abs_max(bmap->ineq[k] + 1, total, &max_k);
573 if (isl_int_abs_gt(max_k, wraps->max))
574 isl_int_set(wraps->max, max_k);
577 isl_int_clear(max_k);
580 /* Initialize the isl_wraps data structure.
581 * If we want to bound the coefficients of the wrapping constraints,
582 * we set wraps->max to the largest coefficient
583 * in the equalities and inequalities that can be removed if we end up
584 * applying wrapping.
586 static void wraps_init(struct isl_wraps *wraps, __isl_take isl_mat *mat,
587 __isl_keep isl_map *map, int i, int j,
588 int *eq_i, int *ineq_i, int *eq_j, int *ineq_j)
590 isl_ctx *ctx;
592 wraps->bound = 0;
593 wraps->mat = mat;
594 if (!mat)
595 return;
596 ctx = isl_mat_get_ctx(mat);
597 wraps->bound = isl_options_get_coalesce_bounded_wrapping(ctx);
598 if (!wraps->bound)
599 return;
600 isl_int_init(wraps->max);
601 isl_int_set_si(wraps->max, 0);
602 wraps_update_max(wraps, map->p[i], eq_i, ineq_i);
603 wraps_update_max(wraps, map->p[j], eq_j, ineq_j);
606 /* Free the contents of the isl_wraps data structure.
608 static void wraps_free(struct isl_wraps *wraps)
610 isl_mat_free(wraps->mat);
611 if (wraps->bound)
612 isl_int_clear(wraps->max);
615 /* Is the wrapping constraint in row "row" allowed?
617 * If wraps->bound is set, we check that none of the coefficients
618 * is greater than wraps->max.
620 static int allow_wrap(struct isl_wraps *wraps, int row)
622 int i;
624 if (!wraps->bound)
625 return 1;
627 for (i = 1; i < wraps->mat->n_col; ++i)
628 if (isl_int_abs_gt(wraps->mat->row[row][i], wraps->max))
629 return 0;
631 return 1;
634 /* For each non-redundant constraint in "bmap" (as determined by "tab"),
635 * wrap the constraint around "bound" such that it includes the whole
636 * set "set" and append the resulting constraint to "wraps".
637 * "wraps" is assumed to have been pre-allocated to the appropriate size.
638 * wraps->n_row is the number of actual wrapped constraints that have
639 * been added.
640 * If any of the wrapping problems results in a constraint that is
641 * identical to "bound", then this means that "set" is unbounded in such
642 * way that no wrapping is possible. If this happens then wraps->n_row
643 * is reset to zero.
644 * Similarly, if we want to bound the coefficients of the wrapping
645 * constraints and a newly added wrapping constraint does not
646 * satisfy the bound, then wraps->n_row is also reset to zero.
648 static int add_wraps(struct isl_wraps *wraps, __isl_keep isl_basic_map *bmap,
649 struct isl_tab *tab, isl_int *bound, __isl_keep isl_set *set)
651 int l;
652 int w;
653 unsigned total = isl_basic_map_total_dim(bmap);
655 w = wraps->mat->n_row;
657 for (l = 0; l < bmap->n_ineq; ++l) {
658 if (isl_seq_is_neg(bound, bmap->ineq[l], 1 + total))
659 continue;
660 if (isl_seq_eq(bound, bmap->ineq[l], 1 + total))
661 continue;
662 if (isl_tab_is_redundant(tab, bmap->n_eq + l))
663 continue;
665 isl_seq_cpy(wraps->mat->row[w], bound, 1 + total);
666 if (!isl_set_wrap_facet(set, wraps->mat->row[w], bmap->ineq[l]))
667 return -1;
668 if (isl_seq_eq(wraps->mat->row[w], bound, 1 + total))
669 goto unbounded;
670 if (!allow_wrap(wraps, w))
671 goto unbounded;
672 ++w;
674 for (l = 0; l < bmap->n_eq; ++l) {
675 if (isl_seq_is_neg(bound, bmap->eq[l], 1 + total))
676 continue;
677 if (isl_seq_eq(bound, bmap->eq[l], 1 + total))
678 continue;
680 isl_seq_cpy(wraps->mat->row[w], bound, 1 + total);
681 isl_seq_neg(wraps->mat->row[w + 1], bmap->eq[l], 1 + total);
682 if (!isl_set_wrap_facet(set, wraps->mat->row[w],
683 wraps->mat->row[w + 1]))
684 return -1;
685 if (isl_seq_eq(wraps->mat->row[w], bound, 1 + total))
686 goto unbounded;
687 if (!allow_wrap(wraps, w))
688 goto unbounded;
689 ++w;
691 isl_seq_cpy(wraps->mat->row[w], bound, 1 + total);
692 if (!isl_set_wrap_facet(set, wraps->mat->row[w], bmap->eq[l]))
693 return -1;
694 if (isl_seq_eq(wraps->mat->row[w], bound, 1 + total))
695 goto unbounded;
696 if (!allow_wrap(wraps, w))
697 goto unbounded;
698 ++w;
701 wraps->mat->n_row = w;
702 return 0;
703 unbounded:
704 wraps->mat->n_row = 0;
705 return 0;
708 /* Check if the constraints in "wraps" from "first" until the last
709 * are all valid for the basic set represented by "tab".
710 * If not, wraps->n_row is set to zero.
712 static int check_wraps(__isl_keep isl_mat *wraps, int first,
713 struct isl_tab *tab)
715 int i;
717 for (i = first; i < wraps->n_row; ++i) {
718 enum isl_ineq_type type;
719 type = isl_tab_ineq_type(tab, wraps->row[i]);
720 if (type == isl_ineq_error)
721 return -1;
722 if (type == isl_ineq_redundant)
723 continue;
724 wraps->n_row = 0;
725 return 0;
728 return 0;
731 /* Return a set that corresponds to the non-redudant constraints
732 * (as recorded in tab) of bmap.
734 * It's important to remove the redundant constraints as some
735 * of the other constraints may have been modified after the
736 * constraints were marked redundant.
737 * In particular, a constraint may have been relaxed.
738 * Redundant constraints are ignored when a constraint is relaxed
739 * and should therefore continue to be ignored ever after.
740 * Otherwise, the relaxation might be thwarted by some of
741 * these constraints.
743 static __isl_give isl_set *set_from_updated_bmap(__isl_keep isl_basic_map *bmap,
744 struct isl_tab *tab)
746 bmap = isl_basic_map_copy(bmap);
747 bmap = isl_basic_map_cow(bmap);
748 bmap = isl_basic_map_update_from_tab(bmap, tab);
749 return isl_set_from_basic_set(isl_basic_map_underlying_set(bmap));
752 /* Given a basic set i with a constraint k that is adjacent to either the
753 * whole of basic set j or a facet of basic set j, check if we can wrap
754 * both the facet corresponding to k and the facet of j (or the whole of j)
755 * around their ridges to include the other set.
756 * If so, replace the pair of basic sets by their union.
758 * All constraints of i (except k) are assumed to be valid for j.
760 * However, the constraints of j may not be valid for i and so
761 * we have to check that the wrapping constraints for j are valid for i.
763 * In the case where j has a facet adjacent to i, tab[j] is assumed
764 * to have been restricted to this facet, so that the non-redundant
765 * constraints in tab[j] are the ridges of the facet.
766 * Note that for the purpose of wrapping, it does not matter whether
767 * we wrap the ridges of i around the whole of j or just around
768 * the facet since all the other constraints are assumed to be valid for j.
769 * In practice, we wrap to include the whole of j.
770 * ____ _____
771 * / | / \
772 * / || / |
773 * \ || => \ |
774 * \ || \ |
775 * \___|| \____|
778 static int can_wrap_in_facet(struct isl_map *map, int i, int j, int k,
779 struct isl_tab **tabs, int *eq_i, int *ineq_i, int *eq_j, int *ineq_j)
781 int changed = 0;
782 struct isl_wraps wraps;
783 isl_mat *mat;
784 struct isl_set *set_i = NULL;
785 struct isl_set *set_j = NULL;
786 struct isl_vec *bound = NULL;
787 unsigned total = isl_basic_map_total_dim(map->p[i]);
788 struct isl_tab_undo *snap;
789 int n;
791 set_i = set_from_updated_bmap(map->p[i], tabs[i]);
792 set_j = set_from_updated_bmap(map->p[j], tabs[j]);
793 mat = isl_mat_alloc(map->ctx, 2 * (map->p[i]->n_eq + map->p[j]->n_eq) +
794 map->p[i]->n_ineq + map->p[j]->n_ineq,
795 1 + total);
796 wraps_init(&wraps, mat, map, i, j, eq_i, ineq_i, eq_j, ineq_j);
797 bound = isl_vec_alloc(map->ctx, 1 + total);
798 if (!set_i || !set_j || !wraps.mat || !bound)
799 goto error;
801 isl_seq_cpy(bound->el, map->p[i]->ineq[k], 1 + total);
802 isl_int_add_ui(bound->el[0], bound->el[0], 1);
804 isl_seq_cpy(wraps.mat->row[0], bound->el, 1 + total);
805 wraps.mat->n_row = 1;
807 if (add_wraps(&wraps, map->p[j], tabs[j], bound->el, set_i) < 0)
808 goto error;
809 if (!wraps.mat->n_row)
810 goto unbounded;
812 snap = isl_tab_snap(tabs[i]);
814 if (isl_tab_select_facet(tabs[i], map->p[i]->n_eq + k) < 0)
815 goto error;
816 if (isl_tab_detect_redundant(tabs[i]) < 0)
817 goto error;
819 isl_seq_neg(bound->el, map->p[i]->ineq[k], 1 + total);
821 n = wraps.mat->n_row;
822 if (add_wraps(&wraps, map->p[i], tabs[i], bound->el, set_j) < 0)
823 goto error;
825 if (isl_tab_rollback(tabs[i], snap) < 0)
826 goto error;
827 if (check_wraps(wraps.mat, n, tabs[i]) < 0)
828 goto error;
829 if (!wraps.mat->n_row)
830 goto unbounded;
832 changed = fuse(map, i, j, tabs, eq_i, ineq_i, eq_j, ineq_j, wraps.mat);
834 unbounded:
835 wraps_free(&wraps);
837 isl_set_free(set_i);
838 isl_set_free(set_j);
840 isl_vec_free(bound);
842 return changed;
843 error:
844 wraps_free(&wraps);
845 isl_vec_free(bound);
846 isl_set_free(set_i);
847 isl_set_free(set_j);
848 return -1;
851 /* Set the is_redundant property of the "n" constraints in "cuts",
852 * except "k" to "v".
853 * This is a fairly tricky operation as it bypasses isl_tab.c.
854 * The reason we want to temporarily mark some constraints redundant
855 * is that we want to ignore them in add_wraps.
857 * Initially all cut constraints are non-redundant, but the
858 * selection of a facet right before the call to this function
859 * may have made some of them redundant.
860 * Likewise, the same constraints are marked non-redundant
861 * in the second call to this function, before they are officially
862 * made non-redundant again in the subsequent rollback.
864 static void set_is_redundant(struct isl_tab *tab, unsigned n_eq,
865 int *cuts, int n, int k, int v)
867 int l;
869 for (l = 0; l < n; ++l) {
870 if (l == k)
871 continue;
872 tab->con[n_eq + cuts[l]].is_redundant = v;
876 /* Given a pair of basic maps i and j such that j sticks out
877 * of i at n cut constraints, each time by at most one,
878 * try to compute wrapping constraints and replace the two
879 * basic maps by a single basic map.
880 * The other constraints of i are assumed to be valid for j.
882 * The facets of i corresponding to the cut constraints are
883 * wrapped around their ridges, except those ridges determined
884 * by any of the other cut constraints.
885 * The intersections of cut constraints need to be ignored
886 * as the result of wrapping one cut constraint around another
887 * would result in a constraint cutting the union.
888 * In each case, the facets are wrapped to include the union
889 * of the two basic maps.
891 * The pieces of j that lie at an offset of exactly one from
892 * one of the cut constraints of i are wrapped around their edges.
893 * Here, there is no need to ignore intersections because we
894 * are wrapping around the union of the two basic maps.
896 * If any wrapping fails, i.e., if we cannot wrap to touch
897 * the union, then we give up.
898 * Otherwise, the pair of basic maps is replaced by their union.
900 static int wrap_in_facets(struct isl_map *map, int i, int j,
901 int *cuts, int n, struct isl_tab **tabs,
902 int *eq_i, int *ineq_i, int *eq_j, int *ineq_j)
904 int changed = 0;
905 struct isl_wraps wraps;
906 isl_mat *mat;
907 isl_set *set = NULL;
908 isl_vec *bound = NULL;
909 unsigned total = isl_basic_map_total_dim(map->p[i]);
910 int max_wrap;
911 int k;
912 struct isl_tab_undo *snap_i, *snap_j;
914 if (isl_tab_extend_cons(tabs[j], 1) < 0)
915 goto error;
917 max_wrap = 2 * (map->p[i]->n_eq + map->p[j]->n_eq) +
918 map->p[i]->n_ineq + map->p[j]->n_ineq;
919 max_wrap *= n;
921 set = isl_set_union(set_from_updated_bmap(map->p[i], tabs[i]),
922 set_from_updated_bmap(map->p[j], tabs[j]));
923 mat = isl_mat_alloc(map->ctx, max_wrap, 1 + total);
924 wraps_init(&wraps, mat, map, i, j, eq_i, ineq_i, eq_j, ineq_j);
925 bound = isl_vec_alloc(map->ctx, 1 + total);
926 if (!set || !wraps.mat || !bound)
927 goto error;
929 snap_i = isl_tab_snap(tabs[i]);
930 snap_j = isl_tab_snap(tabs[j]);
932 wraps.mat->n_row = 0;
934 for (k = 0; k < n; ++k) {
935 if (isl_tab_select_facet(tabs[i], map->p[i]->n_eq + cuts[k]) < 0)
936 goto error;
937 if (isl_tab_detect_redundant(tabs[i]) < 0)
938 goto error;
939 set_is_redundant(tabs[i], map->p[i]->n_eq, cuts, n, k, 1);
941 isl_seq_neg(bound->el, map->p[i]->ineq[cuts[k]], 1 + total);
942 if (!tabs[i]->empty &&
943 add_wraps(&wraps, map->p[i], tabs[i], bound->el, set) < 0)
944 goto error;
946 set_is_redundant(tabs[i], map->p[i]->n_eq, cuts, n, k, 0);
947 if (isl_tab_rollback(tabs[i], snap_i) < 0)
948 goto error;
950 if (tabs[i]->empty)
951 break;
952 if (!wraps.mat->n_row)
953 break;
955 isl_seq_cpy(bound->el, map->p[i]->ineq[cuts[k]], 1 + total);
956 isl_int_add_ui(bound->el[0], bound->el[0], 1);
957 if (isl_tab_add_eq(tabs[j], bound->el) < 0)
958 goto error;
959 if (isl_tab_detect_redundant(tabs[j]) < 0)
960 goto error;
962 if (!tabs[j]->empty &&
963 add_wraps(&wraps, map->p[j], tabs[j], bound->el, set) < 0)
964 goto error;
966 if (isl_tab_rollback(tabs[j], snap_j) < 0)
967 goto error;
969 if (!wraps.mat->n_row)
970 break;
973 if (k == n)
974 changed = fuse(map, i, j, tabs,
975 eq_i, ineq_i, eq_j, ineq_j, wraps.mat);
977 isl_vec_free(bound);
978 wraps_free(&wraps);
979 isl_set_free(set);
981 return changed;
982 error:
983 isl_vec_free(bound);
984 wraps_free(&wraps);
985 isl_set_free(set);
986 return -1;
989 /* Given two basic sets i and j such that i has no cut equalities,
990 * check if relaxing all the cut inequalities of i by one turns
991 * them into valid constraint for j and check if we can wrap in
992 * the bits that are sticking out.
993 * If so, replace the pair by their union.
995 * We first check if all relaxed cut inequalities of i are valid for j
996 * and then try to wrap in the intersections of the relaxed cut inequalities
997 * with j.
999 * During this wrapping, we consider the points of j that lie at a distance
1000 * of exactly 1 from i. In particular, we ignore the points that lie in
1001 * between this lower-dimensional space and the basic map i.
1002 * We can therefore only apply this to integer maps.
1003 * ____ _____
1004 * / ___|_ / \
1005 * / | | / |
1006 * \ | | => \ |
1007 * \|____| \ |
1008 * \___| \____/
1010 * _____ ______
1011 * | ____|_ | \
1012 * | | | | |
1013 * | | | => | |
1014 * |_| | | |
1015 * |_____| \______|
1017 * _______
1018 * | |
1019 * | |\ |
1020 * | | \ |
1021 * | | \ |
1022 * | | \|
1023 * | | \
1024 * | |_____\
1025 * | |
1026 * |_______|
1028 * Wrapping can fail if the result of wrapping one of the facets
1029 * around its edges does not produce any new facet constraint.
1030 * In particular, this happens when we try to wrap in unbounded sets.
1032 * _______________________________________________________________________
1034 * | ___
1035 * | | |
1036 * |_| |_________________________________________________________________
1037 * |___|
1039 * The following is not an acceptable result of coalescing the above two
1040 * sets as it includes extra integer points.
1041 * _______________________________________________________________________
1043 * |
1044 * |
1046 * \______________________________________________________________________
1048 static int can_wrap_in_set(struct isl_map *map, int i, int j,
1049 struct isl_tab **tabs, int *eq_i, int *ineq_i, int *eq_j, int *ineq_j)
1051 int changed = 0;
1052 int k, m;
1053 int n;
1054 int *cuts = NULL;
1056 if (ISL_F_ISSET(map->p[i], ISL_BASIC_MAP_RATIONAL) ||
1057 ISL_F_ISSET(map->p[j], ISL_BASIC_MAP_RATIONAL))
1058 return 0;
1060 n = count(ineq_i, map->p[i]->n_ineq, STATUS_CUT);
1061 if (n == 0)
1062 return 0;
1064 cuts = isl_alloc_array(map->ctx, int, n);
1065 if (!cuts)
1066 return -1;
1068 for (k = 0, m = 0; m < n; ++k) {
1069 enum isl_ineq_type type;
1071 if (ineq_i[k] != STATUS_CUT)
1072 continue;
1074 isl_int_add_ui(map->p[i]->ineq[k][0], map->p[i]->ineq[k][0], 1);
1075 type = isl_tab_ineq_type(tabs[j], map->p[i]->ineq[k]);
1076 isl_int_sub_ui(map->p[i]->ineq[k][0], map->p[i]->ineq[k][0], 1);
1077 if (type == isl_ineq_error)
1078 goto error;
1079 if (type != isl_ineq_redundant)
1080 break;
1081 cuts[m] = k;
1082 ++m;
1085 if (m == n)
1086 changed = wrap_in_facets(map, i, j, cuts, n, tabs,
1087 eq_i, ineq_i, eq_j, ineq_j);
1089 free(cuts);
1091 return changed;
1092 error:
1093 free(cuts);
1094 return -1;
1097 /* Check if either i or j has a single cut constraint that can
1098 * be used to wrap in (a facet of) the other basic set.
1099 * if so, replace the pair by their union.
1101 static int check_wrap(struct isl_map *map, int i, int j,
1102 struct isl_tab **tabs, int *eq_i, int *ineq_i, int *eq_j, int *ineq_j)
1104 int changed = 0;
1106 if (!any(eq_i, 2 * map->p[i]->n_eq, STATUS_CUT))
1107 changed = can_wrap_in_set(map, i, j, tabs,
1108 eq_i, ineq_i, eq_j, ineq_j);
1109 if (changed)
1110 return changed;
1112 if (!any(eq_j, 2 * map->p[j]->n_eq, STATUS_CUT))
1113 changed = can_wrap_in_set(map, j, i, tabs,
1114 eq_j, ineq_j, eq_i, ineq_i);
1115 return changed;
1118 /* At least one of the basic maps has an equality that is adjacent
1119 * to inequality. Make sure that only one of the basic maps has
1120 * such an equality and that the other basic map has exactly one
1121 * inequality adjacent to an equality.
1122 * We call the basic map that has the inequality "i" and the basic
1123 * map that has the equality "j".
1124 * If "i" has any "cut" (in)equality, then relaxing the inequality
1125 * by one would not result in a basic map that contains the other
1126 * basic map.
1128 static int check_adj_eq(struct isl_map *map, int i, int j,
1129 struct isl_tab **tabs, int *eq_i, int *ineq_i, int *eq_j, int *ineq_j)
1131 int changed = 0;
1132 int k;
1134 if (any(eq_i, 2 * map->p[i]->n_eq, STATUS_ADJ_INEQ) &&
1135 any(eq_j, 2 * map->p[j]->n_eq, STATUS_ADJ_INEQ))
1136 /* ADJ EQ TOO MANY */
1137 return 0;
1139 if (any(eq_i, 2 * map->p[i]->n_eq, STATUS_ADJ_INEQ))
1140 return check_adj_eq(map, j, i, tabs,
1141 eq_j, ineq_j, eq_i, ineq_i);
1143 /* j has an equality adjacent to an inequality in i */
1145 if (any(eq_i, 2 * map->p[i]->n_eq, STATUS_CUT))
1146 return 0;
1147 if (any(ineq_i, map->p[i]->n_ineq, STATUS_CUT))
1148 /* ADJ EQ CUT */
1149 return 0;
1150 if (count(ineq_i, map->p[i]->n_ineq, STATUS_ADJ_EQ) != 1 ||
1151 any(ineq_j, map->p[j]->n_ineq, STATUS_ADJ_EQ) ||
1152 any(ineq_i, map->p[i]->n_ineq, STATUS_ADJ_INEQ) ||
1153 any(ineq_j, map->p[j]->n_ineq, STATUS_ADJ_INEQ))
1154 /* ADJ EQ TOO MANY */
1155 return 0;
1157 for (k = 0; k < map->p[i]->n_ineq; ++k)
1158 if (ineq_i[k] == STATUS_ADJ_EQ)
1159 break;
1161 changed = is_adj_eq_extension(map, i, j, k, tabs,
1162 eq_i, ineq_i, eq_j, ineq_j);
1163 if (changed)
1164 return changed;
1166 if (count(eq_j, 2 * map->p[j]->n_eq, STATUS_ADJ_INEQ) != 1)
1167 return 0;
1169 changed = can_wrap_in_facet(map, i, j, k, tabs, eq_i, ineq_i, eq_j, ineq_j);
1171 return changed;
1174 /* The two basic maps lie on adjacent hyperplanes. In particular,
1175 * basic map "i" has an equality that lies parallel to basic map "j".
1176 * Check if we can wrap the facets around the parallel hyperplanes
1177 * to include the other set.
1179 * We perform basically the same operations as can_wrap_in_facet,
1180 * except that we don't need to select a facet of one of the sets.
1182 * \\ \\
1183 * \\ => \\
1184 * \ \|
1186 * We only allow one equality of "i" to be adjacent to an equality of "j"
1187 * to avoid coalescing
1189 * [m, n] -> { [x, y] -> [x, 1 + y] : x >= 1 and y >= 1 and
1190 * x <= 10 and y <= 10;
1191 * [x, y] -> [1 + x, y] : x >= 1 and x <= 20 and
1192 * y >= 5 and y <= 15 }
1194 * to
1196 * [m, n] -> { [x, y] -> [x2, y2] : x >= 1 and 10y2 <= 20 - x + 10y and
1197 * 4y2 >= 5 + 3y and 5y2 <= 15 + 4y and
1198 * y2 <= 1 + x + y - x2 and y2 >= y and
1199 * y2 >= 1 + x + y - x2 }
1201 static int check_eq_adj_eq(struct isl_map *map, int i, int j,
1202 struct isl_tab **tabs, int *eq_i, int *ineq_i, int *eq_j, int *ineq_j)
1204 int k;
1205 int changed = 0;
1206 struct isl_wraps wraps;
1207 isl_mat *mat;
1208 struct isl_set *set_i = NULL;
1209 struct isl_set *set_j = NULL;
1210 struct isl_vec *bound = NULL;
1211 unsigned total = isl_basic_map_total_dim(map->p[i]);
1213 if (count(eq_i, 2 * map->p[i]->n_eq, STATUS_ADJ_EQ) != 1)
1214 return 0;
1216 for (k = 0; k < 2 * map->p[i]->n_eq ; ++k)
1217 if (eq_i[k] == STATUS_ADJ_EQ)
1218 break;
1220 set_i = set_from_updated_bmap(map->p[i], tabs[i]);
1221 set_j = set_from_updated_bmap(map->p[j], tabs[j]);
1222 mat = isl_mat_alloc(map->ctx, 2 * (map->p[i]->n_eq + map->p[j]->n_eq) +
1223 map->p[i]->n_ineq + map->p[j]->n_ineq,
1224 1 + total);
1225 wraps_init(&wraps, mat, map, i, j, eq_i, ineq_i, eq_j, ineq_j);
1226 bound = isl_vec_alloc(map->ctx, 1 + total);
1227 if (!set_i || !set_j || !wraps.mat || !bound)
1228 goto error;
1230 if (k % 2 == 0)
1231 isl_seq_neg(bound->el, map->p[i]->eq[k / 2], 1 + total);
1232 else
1233 isl_seq_cpy(bound->el, map->p[i]->eq[k / 2], 1 + total);
1234 isl_int_add_ui(bound->el[0], bound->el[0], 1);
1236 isl_seq_cpy(wraps.mat->row[0], bound->el, 1 + total);
1237 wraps.mat->n_row = 1;
1239 if (add_wraps(&wraps, map->p[j], tabs[j], bound->el, set_i) < 0)
1240 goto error;
1241 if (!wraps.mat->n_row)
1242 goto unbounded;
1244 isl_int_sub_ui(bound->el[0], bound->el[0], 1);
1245 isl_seq_neg(bound->el, bound->el, 1 + total);
1247 isl_seq_cpy(wraps.mat->row[wraps.mat->n_row], bound->el, 1 + total);
1248 wraps.mat->n_row++;
1250 if (add_wraps(&wraps, map->p[i], tabs[i], bound->el, set_j) < 0)
1251 goto error;
1252 if (!wraps.mat->n_row)
1253 goto unbounded;
1255 changed = fuse(map, i, j, tabs, eq_i, ineq_i, eq_j, ineq_j, wraps.mat);
1257 if (0) {
1258 error: changed = -1;
1260 unbounded:
1262 wraps_free(&wraps);
1263 isl_set_free(set_i);
1264 isl_set_free(set_j);
1265 isl_vec_free(bound);
1267 return changed;
1270 /* Check if the union of the given pair of basic maps
1271 * can be represented by a single basic map.
1272 * If so, replace the pair by the single basic map and return 1.
1273 * Otherwise, return 0;
1274 * The two basic maps are assumed to live in the same local space.
1276 * We first check the effect of each constraint of one basic map
1277 * on the other basic map.
1278 * The constraint may be
1279 * redundant the constraint is redundant in its own
1280 * basic map and should be ignore and removed
1281 * in the end
1282 * valid all (integer) points of the other basic map
1283 * satisfy the constraint
1284 * separate no (integer) point of the other basic map
1285 * satisfies the constraint
1286 * cut some but not all points of the other basic map
1287 * satisfy the constraint
1288 * adj_eq the given constraint is adjacent (on the outside)
1289 * to an equality of the other basic map
1290 * adj_ineq the given constraint is adjacent (on the outside)
1291 * to an inequality of the other basic map
1293 * We consider seven cases in which we can replace the pair by a single
1294 * basic map. We ignore all "redundant" constraints.
1296 * 1. all constraints of one basic map are valid
1297 * => the other basic map is a subset and can be removed
1299 * 2. all constraints of both basic maps are either "valid" or "cut"
1300 * and the facets corresponding to the "cut" constraints
1301 * of one of the basic maps lies entirely inside the other basic map
1302 * => the pair can be replaced by a basic map consisting
1303 * of the valid constraints in both basic maps
1305 * 3. there is a single pair of adjacent inequalities
1306 * (all other constraints are "valid")
1307 * => the pair can be replaced by a basic map consisting
1308 * of the valid constraints in both basic maps
1310 * 4. one basic map has a single adjacent inequality, while the other
1311 * constraints are "valid". The other basic map has some
1312 * "cut" constraints, but replacing the adjacent inequality by
1313 * its opposite and adding the valid constraints of the other
1314 * basic map results in a subset of the other basic map
1315 * => the pair can be replaced by a basic map consisting
1316 * of the valid constraints in both basic maps
1318 * 5. there is a single adjacent pair of an inequality and an equality,
1319 * the other constraints of the basic map containing the inequality are
1320 * "valid". Moreover, if the inequality the basic map is relaxed
1321 * and then turned into an equality, then resulting facet lies
1322 * entirely inside the other basic map
1323 * => the pair can be replaced by the basic map containing
1324 * the inequality, with the inequality relaxed.
1326 * 6. there is a single adjacent pair of an inequality and an equality,
1327 * the other constraints of the basic map containing the inequality are
1328 * "valid". Moreover, the facets corresponding to both
1329 * the inequality and the equality can be wrapped around their
1330 * ridges to include the other basic map
1331 * => the pair can be replaced by a basic map consisting
1332 * of the valid constraints in both basic maps together
1333 * with all wrapping constraints
1335 * 7. one of the basic maps extends beyond the other by at most one.
1336 * Moreover, the facets corresponding to the cut constraints and
1337 * the pieces of the other basic map at offset one from these cut
1338 * constraints can be wrapped around their ridges to include
1339 * the union of the two basic maps
1340 * => the pair can be replaced by a basic map consisting
1341 * of the valid constraints in both basic maps together
1342 * with all wrapping constraints
1344 * 8. the two basic maps live in adjacent hyperplanes. In principle
1345 * such sets can always be combined through wrapping, but we impose
1346 * that there is only one such pair, to avoid overeager coalescing.
1348 * Throughout the computation, we maintain a collection of tableaus
1349 * corresponding to the basic maps. When the basic maps are dropped
1350 * or combined, the tableaus are modified accordingly.
1352 static int coalesce_local_pair(__isl_keep isl_map *map, int i, int j,
1353 struct isl_tab **tabs)
1355 int changed = 0;
1356 int *eq_i = NULL;
1357 int *eq_j = NULL;
1358 int *ineq_i = NULL;
1359 int *ineq_j = NULL;
1361 eq_i = eq_status_in(map->p[i], tabs[j]);
1362 if (map->p[i]->n_eq && !eq_i)
1363 goto error;
1364 if (any(eq_i, 2 * map->p[i]->n_eq, STATUS_ERROR))
1365 goto error;
1366 if (any(eq_i, 2 * map->p[i]->n_eq, STATUS_SEPARATE))
1367 goto done;
1369 eq_j = eq_status_in(map->p[j], tabs[i]);
1370 if (map->p[j]->n_eq && !eq_j)
1371 goto error;
1372 if (any(eq_j, 2 * map->p[j]->n_eq, STATUS_ERROR))
1373 goto error;
1374 if (any(eq_j, 2 * map->p[j]->n_eq, STATUS_SEPARATE))
1375 goto done;
1377 ineq_i = ineq_status_in(map->p[i], tabs[i], tabs[j]);
1378 if (map->p[i]->n_ineq && !ineq_i)
1379 goto error;
1380 if (any(ineq_i, map->p[i]->n_ineq, STATUS_ERROR))
1381 goto error;
1382 if (any(ineq_i, map->p[i]->n_ineq, STATUS_SEPARATE))
1383 goto done;
1385 ineq_j = ineq_status_in(map->p[j], tabs[j], tabs[i]);
1386 if (map->p[j]->n_ineq && !ineq_j)
1387 goto error;
1388 if (any(ineq_j, map->p[j]->n_ineq, STATUS_ERROR))
1389 goto error;
1390 if (any(ineq_j, map->p[j]->n_ineq, STATUS_SEPARATE))
1391 goto done;
1393 if (all(eq_i, 2 * map->p[i]->n_eq, STATUS_VALID) &&
1394 all(ineq_i, map->p[i]->n_ineq, STATUS_VALID)) {
1395 drop(map, j, tabs);
1396 changed = 1;
1397 } else if (all(eq_j, 2 * map->p[j]->n_eq, STATUS_VALID) &&
1398 all(ineq_j, map->p[j]->n_ineq, STATUS_VALID)) {
1399 drop(map, i, tabs);
1400 changed = 1;
1401 } else if (any(eq_i, 2 * map->p[i]->n_eq, STATUS_ADJ_EQ)) {
1402 changed = check_eq_adj_eq(map, i, j, tabs,
1403 eq_i, ineq_i, eq_j, ineq_j);
1404 } else if (any(eq_j, 2 * map->p[j]->n_eq, STATUS_ADJ_EQ)) {
1405 changed = check_eq_adj_eq(map, j, i, tabs,
1406 eq_j, ineq_j, eq_i, ineq_i);
1407 } else if (any(eq_i, 2 * map->p[i]->n_eq, STATUS_ADJ_INEQ) ||
1408 any(eq_j, 2 * map->p[j]->n_eq, STATUS_ADJ_INEQ)) {
1409 changed = check_adj_eq(map, i, j, tabs,
1410 eq_i, ineq_i, eq_j, ineq_j);
1411 } else if (any(ineq_i, map->p[i]->n_ineq, STATUS_ADJ_EQ) ||
1412 any(ineq_j, map->p[j]->n_ineq, STATUS_ADJ_EQ)) {
1413 /* Can't happen */
1414 /* BAD ADJ INEQ */
1415 } else if (any(ineq_i, map->p[i]->n_ineq, STATUS_ADJ_INEQ) ||
1416 any(ineq_j, map->p[j]->n_ineq, STATUS_ADJ_INEQ)) {
1417 changed = check_adj_ineq(map, i, j, tabs,
1418 eq_i, ineq_i, eq_j, ineq_j);
1419 } else {
1420 if (!any(eq_i, 2 * map->p[i]->n_eq, STATUS_CUT) &&
1421 !any(eq_j, 2 * map->p[j]->n_eq, STATUS_CUT))
1422 changed = check_facets(map, i, j, tabs, ineq_i, ineq_j);
1423 if (!changed)
1424 changed = check_wrap(map, i, j, tabs,
1425 eq_i, ineq_i, eq_j, ineq_j);
1428 done:
1429 free(eq_i);
1430 free(eq_j);
1431 free(ineq_i);
1432 free(ineq_j);
1433 return changed;
1434 error:
1435 free(eq_i);
1436 free(eq_j);
1437 free(ineq_i);
1438 free(ineq_j);
1439 return -1;
1442 /* Do the two basic maps live in the same local space, i.e.,
1443 * do they have the same (known) divs?
1444 * If either basic map has any unknown divs, then we can only assume
1445 * that they do not live in the same local space.
1447 static int same_divs(__isl_keep isl_basic_map *bmap1,
1448 __isl_keep isl_basic_map *bmap2)
1450 int i;
1451 int known;
1452 int total;
1454 if (!bmap1 || !bmap2)
1455 return -1;
1456 if (bmap1->n_div != bmap2->n_div)
1457 return 0;
1459 if (bmap1->n_div == 0)
1460 return 1;
1462 known = isl_basic_map_divs_known(bmap1);
1463 if (known < 0 || !known)
1464 return known;
1465 known = isl_basic_map_divs_known(bmap2);
1466 if (known < 0 || !known)
1467 return known;
1469 total = isl_basic_map_total_dim(bmap1);
1470 for (i = 0; i < bmap1->n_div; ++i)
1471 if (!isl_seq_eq(bmap1->div[i], bmap2->div[i], 2 + total))
1472 return 0;
1474 return 1;
1477 /* Given two basic maps "i" and "j", where the divs of "i" form a subset
1478 * of those of "j", check if basic map "j" is a subset of basic map "i"
1479 * and, if so, drop basic map "j".
1481 * We first expand the divs of basic map "i" to match those of basic map "j",
1482 * using the divs and expansion computed by the caller.
1483 * Then we check if all constraints of the expanded "i" are valid for "j".
1485 static int coalesce_subset(__isl_keep isl_map *map, int i, int j,
1486 struct isl_tab **tabs, __isl_keep isl_mat *div, int *exp)
1488 isl_basic_map *bmap;
1489 int changed = 0;
1490 int *eq_i = NULL;
1491 int *ineq_i = NULL;
1493 bmap = isl_basic_map_copy(map->p[i]);
1494 bmap = isl_basic_set_expand_divs(bmap, isl_mat_copy(div), exp);
1496 if (!bmap)
1497 goto error;
1499 eq_i = eq_status_in(bmap, tabs[j]);
1500 if (bmap->n_eq && !eq_i)
1501 goto error;
1502 if (any(eq_i, 2 * bmap->n_eq, STATUS_ERROR))
1503 goto error;
1504 if (any(eq_i, 2 * bmap->n_eq, STATUS_SEPARATE))
1505 goto done;
1507 ineq_i = ineq_status_in(bmap, NULL, tabs[j]);
1508 if (bmap->n_ineq && !ineq_i)
1509 goto error;
1510 if (any(ineq_i, bmap->n_ineq, STATUS_ERROR))
1511 goto error;
1512 if (any(ineq_i, bmap->n_ineq, STATUS_SEPARATE))
1513 goto done;
1515 if (all(eq_i, 2 * map->p[i]->n_eq, STATUS_VALID) &&
1516 all(ineq_i, map->p[i]->n_ineq, STATUS_VALID)) {
1517 drop(map, j, tabs);
1518 changed = 1;
1521 done:
1522 isl_basic_map_free(bmap);
1523 free(eq_i);
1524 free(ineq_i);
1525 return 0;
1526 error:
1527 isl_basic_map_free(bmap);
1528 free(eq_i);
1529 free(ineq_i);
1530 return -1;
1533 /* Check if the basic map "j" is a subset of basic map "i",
1534 * assuming that "i" has fewer divs that "j".
1535 * If not, then we change the order.
1537 * If the two basic maps have the same number of divs, then
1538 * they must necessarily be different. Otherwise, we would have
1539 * called coalesce_local_pair. We therefore don't try anything
1540 * in this case.
1542 * We first check if the divs of "i" are all known and form a subset
1543 * of those of "j". If so, we pass control over to coalesce_subset.
1545 static int check_coalesce_subset(__isl_keep isl_map *map, int i, int j,
1546 struct isl_tab **tabs)
1548 int known;
1549 isl_mat *div_i, *div_j, *div;
1550 int *exp1 = NULL;
1551 int *exp2 = NULL;
1552 isl_ctx *ctx;
1553 int subset;
1555 if (map->p[i]->n_div == map->p[j]->n_div)
1556 return 0;
1557 if (map->p[j]->n_div < map->p[i]->n_div)
1558 return check_coalesce_subset(map, j, i, tabs);
1560 known = isl_basic_map_divs_known(map->p[i]);
1561 if (known < 0 || !known)
1562 return known;
1564 ctx = isl_map_get_ctx(map);
1566 div_i = isl_basic_map_get_divs(map->p[i]);
1567 div_j = isl_basic_map_get_divs(map->p[j]);
1569 if (!div_i || !div_j)
1570 goto error;
1572 exp1 = isl_alloc_array(ctx, int, div_i->n_row);
1573 exp2 = isl_alloc_array(ctx, int, div_j->n_row);
1574 if ((div_i->n_row && !exp1) || (div_j->n_row && !exp2))
1575 goto error;
1577 div = isl_merge_divs(div_i, div_j, exp1, exp2);
1578 if (!div)
1579 goto error;
1581 if (div->n_row == div_j->n_row)
1582 subset = coalesce_subset(map, i, j, tabs, div, exp1);
1583 else
1584 subset = 0;
1586 isl_mat_free(div);
1588 isl_mat_free(div_i);
1589 isl_mat_free(div_j);
1591 free(exp2);
1592 free(exp1);
1594 return subset;
1595 error:
1596 isl_mat_free(div_i);
1597 isl_mat_free(div_j);
1598 free(exp1);
1599 free(exp2);
1600 return -1;
1603 /* Check if the union of the given pair of basic maps
1604 * can be represented by a single basic map.
1605 * If so, replace the pair by the single basic map and return 1.
1606 * Otherwise, return 0;
1608 * We first check if the two basic maps live in the same local space.
1609 * If so, we do the complete check. Otherwise, we check if one is
1610 * an obvious subset of the other.
1612 static int coalesce_pair(__isl_keep isl_map *map, int i, int j,
1613 struct isl_tab **tabs)
1615 int same;
1617 same = same_divs(map->p[i], map->p[j]);
1618 if (same < 0)
1619 return -1;
1620 if (same)
1621 return coalesce_local_pair(map, i, j, tabs);
1623 return check_coalesce_subset(map, i, j, tabs);
1626 static struct isl_map *coalesce(struct isl_map *map, struct isl_tab **tabs)
1628 int i, j;
1630 for (i = map->n - 2; i >= 0; --i)
1631 restart:
1632 for (j = i + 1; j < map->n; ++j) {
1633 int changed;
1634 changed = coalesce_pair(map, i, j, tabs);
1635 if (changed < 0)
1636 goto error;
1637 if (changed)
1638 goto restart;
1640 return map;
1641 error:
1642 isl_map_free(map);
1643 return NULL;
1646 /* For each pair of basic maps in the map, check if the union of the two
1647 * can be represented by a single basic map.
1648 * If so, replace the pair by the single basic map and start over.
1650 * Since we are constructing the tableaus of the basic maps anyway,
1651 * we exploit them to detect implicit equalities and redundant constraints.
1652 * This also helps the coalescing as it can ignore the redundant constraints.
1653 * In order to avoid confusion, we make all implicit equalities explicit
1654 * in the basic maps. We don't call isl_basic_map_gauss, though,
1655 * as that may affect the number of constraints.
1656 * This means that we have to call isl_basic_map_gauss at the end
1657 * of the computation to ensure that the basic maps are not left
1658 * in an unexpected state.
1660 struct isl_map *isl_map_coalesce(struct isl_map *map)
1662 int i;
1663 unsigned n;
1664 struct isl_tab **tabs = NULL;
1666 map = isl_map_remove_empty_parts(map);
1667 if (!map)
1668 return NULL;
1670 if (map->n <= 1)
1671 return map;
1673 map = isl_map_sort_divs(map);
1674 map = isl_map_cow(map);
1676 tabs = isl_calloc_array(map->ctx, struct isl_tab *, map->n);
1677 if (!tabs)
1678 goto error;
1680 n = map->n;
1681 for (i = 0; i < map->n; ++i) {
1682 tabs[i] = isl_tab_from_basic_map(map->p[i], 0);
1683 if (!tabs[i])
1684 goto error;
1685 if (!ISL_F_ISSET(map->p[i], ISL_BASIC_MAP_NO_IMPLICIT))
1686 if (isl_tab_detect_implicit_equalities(tabs[i]) < 0)
1687 goto error;
1688 map->p[i] = isl_tab_make_equalities_explicit(tabs[i],
1689 map->p[i]);
1690 if (!map->p[i])
1691 goto error;
1692 if (!ISL_F_ISSET(map->p[i], ISL_BASIC_MAP_NO_REDUNDANT))
1693 if (isl_tab_detect_redundant(tabs[i]) < 0)
1694 goto error;
1696 for (i = map->n - 1; i >= 0; --i)
1697 if (tabs[i]->empty)
1698 drop(map, i, tabs);
1700 map = coalesce(map, tabs);
1702 if (map)
1703 for (i = 0; i < map->n; ++i) {
1704 map->p[i] = isl_basic_map_update_from_tab(map->p[i],
1705 tabs[i]);
1706 map->p[i] = isl_basic_map_gauss(map->p[i], NULL);
1707 map->p[i] = isl_basic_map_finalize(map->p[i]);
1708 if (!map->p[i])
1709 goto error;
1710 ISL_F_SET(map->p[i], ISL_BASIC_MAP_NO_IMPLICIT);
1711 ISL_F_SET(map->p[i], ISL_BASIC_MAP_NO_REDUNDANT);
1714 for (i = 0; i < n; ++i)
1715 isl_tab_free(tabs[i]);
1717 free(tabs);
1719 return map;
1720 error:
1721 if (tabs)
1722 for (i = 0; i < n; ++i)
1723 isl_tab_free(tabs[i]);
1724 free(tabs);
1725 isl_map_free(map);
1726 return NULL;
1729 /* For each pair of basic sets in the set, check if the union of the two
1730 * can be represented by a single basic set.
1731 * If so, replace the pair by the single basic set and start over.
1733 struct isl_set *isl_set_coalesce(struct isl_set *set)
1735 return (struct isl_set *)isl_map_coalesce((struct isl_map *)set);