isl_map_coalesce: keep track of information on basic maps in separate struct
[isl.git] / isl_coalesce.c
blob6e2c2551ca9c19fdac5e7377d209826369cbca55
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2012-2013 Ecole Normale Superieure
5 * Copyright 2014 INRIA Rocquencourt
7 * Use of this software is governed by the MIT license
9 * Written by Sven Verdoolaege, K.U.Leuven, Departement
10 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
11 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
12 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
13 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
14 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
15 * B.P. 105 - 78153 Le Chesnay, France
18 #include "isl_map_private.h"
19 #include <isl_seq.h>
20 #include <isl/options.h>
21 #include "isl_tab.h"
22 #include <isl_mat_private.h>
23 #include <isl_local_space_private.h>
24 #include <isl_vec_private.h>
26 #define STATUS_ERROR -1
27 #define STATUS_REDUNDANT 1
28 #define STATUS_VALID 2
29 #define STATUS_SEPARATE 3
30 #define STATUS_CUT 4
31 #define STATUS_ADJ_EQ 5
32 #define STATUS_ADJ_INEQ 6
34 static int status_in(isl_int *ineq, struct isl_tab *tab)
36 enum isl_ineq_type type = isl_tab_ineq_type(tab, ineq);
37 switch (type) {
38 default:
39 case isl_ineq_error: return STATUS_ERROR;
40 case isl_ineq_redundant: return STATUS_VALID;
41 case isl_ineq_separate: return STATUS_SEPARATE;
42 case isl_ineq_cut: return STATUS_CUT;
43 case isl_ineq_adj_eq: return STATUS_ADJ_EQ;
44 case isl_ineq_adj_ineq: return STATUS_ADJ_INEQ;
48 /* Compute the position of the equalities of basic map "bmap_i"
49 * with respect to the basic map represented by "tab_j".
50 * The resulting array has twice as many entries as the number
51 * of equalities corresponding to the two inequalties to which
52 * each equality corresponds.
54 static int *eq_status_in(__isl_keep isl_basic_map *bmap_i,
55 struct isl_tab *tab_j)
57 int k, l;
58 int *eq = isl_calloc_array(bmap_i->ctx, int, 2 * bmap_i->n_eq);
59 unsigned dim;
61 if (!eq)
62 return NULL;
64 dim = isl_basic_map_total_dim(bmap_i);
65 for (k = 0; k < bmap_i->n_eq; ++k) {
66 for (l = 0; l < 2; ++l) {
67 isl_seq_neg(bmap_i->eq[k], bmap_i->eq[k], 1+dim);
68 eq[2 * k + l] = status_in(bmap_i->eq[k], tab_j);
69 if (eq[2 * k + l] == STATUS_ERROR)
70 goto error;
72 if (eq[2 * k] == STATUS_SEPARATE ||
73 eq[2 * k + 1] == STATUS_SEPARATE)
74 break;
77 return eq;
78 error:
79 free(eq);
80 return NULL;
83 /* Compute the position of the inequalities of basic map "bmap_i"
84 * (also represented by "tab_i", if not NULL) with respect to the basic map
85 * represented by "tab_j".
87 static int *ineq_status_in(__isl_keep isl_basic_map *bmap_i,
88 struct isl_tab *tab_i, struct isl_tab *tab_j)
90 int k;
91 unsigned n_eq = bmap_i->n_eq;
92 int *ineq = isl_calloc_array(bmap_i->ctx, int, bmap_i->n_ineq);
94 if (!ineq)
95 return NULL;
97 for (k = 0; k < bmap_i->n_ineq; ++k) {
98 if (tab_i && isl_tab_is_redundant(tab_i, n_eq + k)) {
99 ineq[k] = STATUS_REDUNDANT;
100 continue;
102 ineq[k] = status_in(bmap_i->ineq[k], tab_j);
103 if (ineq[k] == STATUS_ERROR)
104 goto error;
105 if (ineq[k] == STATUS_SEPARATE)
106 break;
109 return ineq;
110 error:
111 free(ineq);
112 return NULL;
115 static int any(int *con, unsigned len, int status)
117 int i;
119 for (i = 0; i < len ; ++i)
120 if (con[i] == status)
121 return 1;
122 return 0;
125 static int count(int *con, unsigned len, int status)
127 int i;
128 int c = 0;
130 for (i = 0; i < len ; ++i)
131 if (con[i] == status)
132 c++;
133 return c;
136 static int all(int *con, unsigned len, int status)
138 int i;
140 for (i = 0; i < len ; ++i) {
141 if (con[i] == STATUS_REDUNDANT)
142 continue;
143 if (con[i] != status)
144 return 0;
146 return 1;
149 /* Internal information associated to a basic map in a map
150 * that is to be coalesced by isl_map_coalesce.
152 * "bmap" is the basic map itself (or NULL if "removed" is set)
153 * "tab" is the corresponding tableau (or NULL if "removed" is set)
154 * "removed" is set if this basic map has been removed from the map
156 struct isl_coalesce_info {
157 isl_basic_map *bmap;
158 struct isl_tab *tab;
159 int removed;
162 /* Free all the allocated memory in an array
163 * of "n" isl_coalesce_info elements.
165 static void clear_coalesce_info(int n, struct isl_coalesce_info *info)
167 int i;
169 if (!info)
170 return;
172 for (i = 0; i < n; ++i) {
173 isl_basic_map_free(info[i].bmap);
174 isl_tab_free(info[i].tab);
177 free(info);
180 /* Drop the basic map represented by "info".
181 * That is, clear the memory associated to the entry and
182 * mark it as having been removed.
184 static void drop(struct isl_coalesce_info *info)
186 info->bmap = isl_basic_map_free(info->bmap);
187 isl_tab_free(info->tab);
188 info->tab = NULL;
189 info->removed = 1;
192 /* Exchange the information in "info1" with that in "info2".
194 static void exchange(struct isl_coalesce_info *info1,
195 struct isl_coalesce_info *info2)
197 struct isl_coalesce_info info;
199 info = *info1;
200 *info1 = *info2;
201 *info2 = info;
204 /* This type represents the kind of change that has been performed
205 * while trying to coalesce two basic maps.
207 * isl_change_none: nothing was changed
208 * isl_change_drop_first: the first basic map was removed
209 * isl_change_drop_second: the second basic map was removed
210 * isl_change_fuse: the two basic maps were replaced by a new basic map.
212 enum isl_change {
213 isl_change_error = -1,
214 isl_change_none = 0,
215 isl_change_drop_first,
216 isl_change_drop_second,
217 isl_change_fuse,
220 /* Replace the pair of basic maps i and j by the basic map bounded
221 * by the valid constraints in both basic maps and the constraints
222 * in extra (if not NULL).
223 * Place the fused basic map in the position that is the smallest of i and j.
225 * If "detect_equalities" is set, then look for equalities encoded
226 * as pairs of inequalities.
228 static enum isl_change fuse(int i, int j, struct isl_coalesce_info *info,
229 int *eq_i, int *ineq_i, int *eq_j, int *ineq_j,
230 __isl_keep isl_mat *extra, int detect_equalities)
232 int k, l;
233 struct isl_basic_map *fused = NULL;
234 struct isl_tab *fused_tab = NULL;
235 unsigned total = isl_basic_map_total_dim(info[i].bmap);
236 unsigned extra_rows = extra ? extra->n_row : 0;
238 if (j < i)
239 return fuse(j, i, info, eq_j, ineq_j, eq_i, ineq_i, extra,
240 detect_equalities);
242 fused = isl_basic_map_alloc_space(isl_space_copy(info[i].bmap->dim),
243 info[i].bmap->n_div,
244 info[i].bmap->n_eq + info[j].bmap->n_eq,
245 info[i].bmap->n_ineq + info[j].bmap->n_ineq + extra_rows);
246 if (!fused)
247 goto error;
249 for (k = 0; k < info[i].bmap->n_eq; ++k) {
250 if (eq_i && (eq_i[2 * k] != STATUS_VALID ||
251 eq_i[2 * k + 1] != STATUS_VALID))
252 continue;
253 l = isl_basic_map_alloc_equality(fused);
254 if (l < 0)
255 goto error;
256 isl_seq_cpy(fused->eq[l], info[i].bmap->eq[k], 1 + total);
259 for (k = 0; k < info[j].bmap->n_eq; ++k) {
260 if (eq_j && (eq_j[2 * k] != STATUS_VALID ||
261 eq_j[2 * k + 1] != STATUS_VALID))
262 continue;
263 l = isl_basic_map_alloc_equality(fused);
264 if (l < 0)
265 goto error;
266 isl_seq_cpy(fused->eq[l], info[j].bmap->eq[k], 1 + total);
269 for (k = 0; k < info[i].bmap->n_ineq; ++k) {
270 if (ineq_i[k] != STATUS_VALID)
271 continue;
272 l = isl_basic_map_alloc_inequality(fused);
273 if (l < 0)
274 goto error;
275 isl_seq_cpy(fused->ineq[l], info[i].bmap->ineq[k], 1 + total);
278 for (k = 0; k < info[j].bmap->n_ineq; ++k) {
279 if (ineq_j[k] != STATUS_VALID)
280 continue;
281 l = isl_basic_map_alloc_inequality(fused);
282 if (l < 0)
283 goto error;
284 isl_seq_cpy(fused->ineq[l], info[j].bmap->ineq[k], 1 + total);
287 for (k = 0; k < info[i].bmap->n_div; ++k) {
288 int l = isl_basic_map_alloc_div(fused);
289 if (l < 0)
290 goto error;
291 isl_seq_cpy(fused->div[l], info[i].bmap->div[k], 1 + 1 + total);
294 for (k = 0; k < extra_rows; ++k) {
295 l = isl_basic_map_alloc_inequality(fused);
296 if (l < 0)
297 goto error;
298 isl_seq_cpy(fused->ineq[l], extra->row[k], 1 + total);
301 if (detect_equalities)
302 fused = isl_basic_map_detect_inequality_pairs(fused, NULL);
303 fused = isl_basic_map_gauss(fused, NULL);
304 ISL_F_SET(fused, ISL_BASIC_MAP_FINAL);
305 if (ISL_F_ISSET(info[i].bmap, ISL_BASIC_MAP_RATIONAL) &&
306 ISL_F_ISSET(info[j].bmap, ISL_BASIC_MAP_RATIONAL))
307 ISL_F_SET(fused, ISL_BASIC_MAP_RATIONAL);
309 fused_tab = isl_tab_from_basic_map(fused, 0);
310 if (isl_tab_detect_redundant(fused_tab) < 0)
311 goto error;
313 isl_basic_map_free(info[i].bmap);
314 info[i].bmap = fused;
315 isl_tab_free(info[i].tab);
316 info[i].tab = fused_tab;
317 drop(&info[j]);
319 return isl_change_fuse;
320 error:
321 isl_tab_free(fused_tab);
322 isl_basic_map_free(fused);
323 return isl_change_error;
326 /* Given a pair of basic maps i and j such that all constraints are either
327 * "valid" or "cut", check if the facets corresponding to the "cut"
328 * constraints of i lie entirely within basic map j.
329 * If so, replace the pair by the basic map consisting of the valid
330 * constraints in both basic maps.
331 * Checking whether the facet lies entirely within basic map j
332 * is performed by checking whether the constraints of basic map j
333 * are valid for the facet. These tests are performed on a rational
334 * tableau to avoid the theoretical possibility that a constraint
335 * that was considered to be a cut constraint for the entire basic map i
336 * happens to be considered to be a valid constraint for the facet,
337 * even though it cuts off the same rational points.
339 * To see that we are not introducing any extra points, call the
340 * two basic maps A and B and the resulting map U and let x
341 * be an element of U \setminus ( A \cup B ).
342 * A line connecting x with an element of A \cup B meets a facet F
343 * of either A or B. Assume it is a facet of B and let c_1 be
344 * the corresponding facet constraint. We have c_1(x) < 0 and
345 * so c_1 is a cut constraint. This implies that there is some
346 * (possibly rational) point x' satisfying the constraints of A
347 * and the opposite of c_1 as otherwise c_1 would have been marked
348 * valid for A. The line connecting x and x' meets a facet of A
349 * in a (possibly rational) point that also violates c_1, but this
350 * is impossible since all cut constraints of B are valid for all
351 * cut facets of A.
352 * In case F is a facet of A rather than B, then we can apply the
353 * above reasoning to find a facet of B separating x from A \cup B first.
355 static enum isl_change check_facets(int i, int j,
356 struct isl_coalesce_info *info, int *ineq_i, int *ineq_j)
358 int k, l;
359 struct isl_tab_undo *snap, *snap2;
360 unsigned n_eq = info[i].bmap->n_eq;
362 snap = isl_tab_snap(info[i].tab);
363 if (isl_tab_mark_rational(info[i].tab) < 0)
364 return isl_change_error;
365 snap2 = isl_tab_snap(info[i].tab);
367 for (k = 0; k < info[i].bmap->n_ineq; ++k) {
368 if (ineq_i[k] != STATUS_CUT)
369 continue;
370 if (isl_tab_select_facet(info[i].tab, n_eq + k) < 0)
371 return isl_change_error;
372 for (l = 0; l < info[j].bmap->n_ineq; ++l) {
373 int stat;
374 if (ineq_j[l] != STATUS_CUT)
375 continue;
376 stat = status_in(info[j].bmap->ineq[l], info[i].tab);
377 if (stat != STATUS_VALID)
378 break;
380 if (isl_tab_rollback(info[i].tab, snap2) < 0)
381 return isl_change_error;
382 if (l < info[j].bmap->n_ineq)
383 break;
386 if (k < info[i].bmap->n_ineq) {
387 if (isl_tab_rollback(info[i].tab, snap) < 0)
388 return isl_change_error;
389 return isl_change_none;
391 return fuse(i, j, info, NULL, ineq_i, NULL, ineq_j, NULL, 0);
394 /* Check if "bmap" contains the basic map represented
395 * by the tableau "tab".
397 static int contains(__isl_keep isl_basic_map *bmap, int *ineq_i,
398 struct isl_tab *tab)
400 int k, l;
401 unsigned dim;
403 dim = isl_basic_map_total_dim(bmap);
404 for (k = 0; k < bmap->n_eq; ++k) {
405 for (l = 0; l < 2; ++l) {
406 int stat;
407 isl_seq_neg(bmap->eq[k], bmap->eq[k], 1+dim);
408 stat = status_in(bmap->eq[k], tab);
409 if (stat != STATUS_VALID)
410 return 0;
414 for (k = 0; k < bmap->n_ineq; ++k) {
415 int stat;
416 if (ineq_i[k] == STATUS_REDUNDANT)
417 continue;
418 stat = status_in(bmap->ineq[k], tab);
419 if (stat != STATUS_VALID)
420 return 0;
422 return 1;
425 /* Basic map "i" has an inequality (say "k") that is adjacent
426 * to some inequality of basic map "j". All the other inequalities
427 * are valid for "j".
428 * Check if basic map "j" forms an extension of basic map "i".
430 * Note that this function is only called if some of the equalities or
431 * inequalities of basic map "j" do cut basic map "i". The function is
432 * correct even if there are no such cut constraints, but in that case
433 * the additional checks performed by this function are overkill.
435 * In particular, we replace constraint k, say f >= 0, by constraint
436 * f <= -1, add the inequalities of "j" that are valid for "i"
437 * and check if the result is a subset of basic map "j".
438 * If so, then we know that this result is exactly equal to basic map "j"
439 * since all its constraints are valid for basic map "j".
440 * By combining the valid constraints of "i" (all equalities and all
441 * inequalities except "k") and the valid constraints of "j" we therefore
442 * obtain a basic map that is equal to their union.
443 * In this case, there is no need to perform a rollback of the tableau
444 * since it is going to be destroyed in fuse().
447 * |\__ |\__
448 * | \__ | \__
449 * | \_ => | \__
450 * |_______| _ |_________\
453 * |\ |\
454 * | \ | \
455 * | \ | \
456 * | | | \
457 * | ||\ => | \
458 * | || \ | \
459 * | || | | |
460 * |__||_/ |_____/
462 static enum isl_change is_adj_ineq_extension(int i, int j,
463 struct isl_coalesce_info *info, int *eq_i, int *ineq_i,
464 int *eq_j, int *ineq_j)
466 int k;
467 struct isl_tab_undo *snap;
468 unsigned n_eq = info[i].bmap->n_eq;
469 unsigned total = isl_basic_map_total_dim(info[i].bmap);
470 int r;
472 if (isl_tab_extend_cons(info[i].tab, 1 + info[j].bmap->n_ineq) < 0)
473 return isl_change_error;
475 for (k = 0; k < info[i].bmap->n_ineq; ++k)
476 if (ineq_i[k] == STATUS_ADJ_INEQ)
477 break;
478 if (k >= info[i].bmap->n_ineq)
479 isl_die(isl_basic_map_get_ctx(info[i].bmap), isl_error_internal,
480 "ineq_i should have exactly one STATUS_ADJ_INEQ",
481 return isl_change_error);
483 snap = isl_tab_snap(info[i].tab);
485 if (isl_tab_unrestrict(info[i].tab, n_eq + k) < 0)
486 return isl_change_error;
488 isl_seq_neg(info[i].bmap->ineq[k], info[i].bmap->ineq[k], 1 + total);
489 isl_int_sub_ui(info[i].bmap->ineq[k][0], info[i].bmap->ineq[k][0], 1);
490 r = isl_tab_add_ineq(info[i].tab, info[i].bmap->ineq[k]);
491 isl_seq_neg(info[i].bmap->ineq[k], info[i].bmap->ineq[k], 1 + total);
492 isl_int_sub_ui(info[i].bmap->ineq[k][0], info[i].bmap->ineq[k][0], 1);
493 if (r < 0)
494 return isl_change_error;
496 for (k = 0; k < info[j].bmap->n_ineq; ++k) {
497 if (ineq_j[k] != STATUS_VALID)
498 continue;
499 if (isl_tab_add_ineq(info[i].tab, info[j].bmap->ineq[k]) < 0)
500 return isl_change_error;
503 if (contains(info[j].bmap, ineq_j, info[i].tab))
504 return fuse(i, j, info, eq_i, ineq_i, eq_j, ineq_j, NULL, 0);
506 if (isl_tab_rollback(info[i].tab, snap) < 0)
507 return isl_change_error;
509 return isl_change_none;
513 /* Both basic maps have at least one inequality with and adjacent
514 * (but opposite) inequality in the other basic map.
515 * Check that there are no cut constraints and that there is only
516 * a single pair of adjacent inequalities.
517 * If so, we can replace the pair by a single basic map described
518 * by all but the pair of adjacent inequalities.
519 * Any additional points introduced lie strictly between the two
520 * adjacent hyperplanes and can therefore be integral.
522 * ____ _____
523 * / ||\ / \
524 * / || \ / \
525 * \ || \ => \ \
526 * \ || / \ /
527 * \___||_/ \_____/
529 * The test for a single pair of adjancent inequalities is important
530 * for avoiding the combination of two basic maps like the following
532 * /|
533 * / |
534 * /__|
535 * _____
536 * | |
537 * | |
538 * |___|
540 * If there are some cut constraints on one side, then we may
541 * still be able to fuse the two basic maps, but we need to perform
542 * some additional checks in is_adj_ineq_extension.
544 static enum isl_change check_adj_ineq(int i, int j,
545 struct isl_coalesce_info *info, int *eq_i, int *ineq_i,
546 int *eq_j, int *ineq_j)
548 int count_i, count_j;
549 int cut_i, cut_j;
551 count_i = count(ineq_i, info[i].bmap->n_ineq, STATUS_ADJ_INEQ);
552 count_j = count(ineq_j, info[j].bmap->n_ineq, STATUS_ADJ_INEQ);
554 if (count_i != 1 && count_j != 1)
555 return isl_change_none;
557 cut_i = any(eq_i, 2 * info[i].bmap->n_eq, STATUS_CUT) ||
558 any(ineq_i, info[i].bmap->n_ineq, STATUS_CUT);
559 cut_j = any(eq_j, 2 * info[j].bmap->n_eq, STATUS_CUT) ||
560 any(ineq_j, info[j].bmap->n_ineq, STATUS_CUT);
562 if (!cut_i && !cut_j && count_i == 1 && count_j == 1)
563 return fuse(i, j, info, NULL, ineq_i, NULL, ineq_j, NULL, 0);
565 if (count_i == 1 && !cut_i)
566 return is_adj_ineq_extension(i, j, info,
567 eq_i, ineq_i, eq_j, ineq_j);
569 if (count_j == 1 && !cut_j)
570 return is_adj_ineq_extension(j, i, info,
571 eq_j, ineq_j, eq_i, ineq_i);
573 return isl_change_none;
576 /* Basic map "i" has an inequality "k" that is adjacent to some equality
577 * of basic map "j". All the other inequalities are valid for "j".
578 * Check if basic map "j" forms an extension of basic map "i".
580 * In particular, we relax constraint "k", compute the corresponding
581 * facet and check whether it is included in the other basic map.
582 * If so, we know that relaxing the constraint extends the basic
583 * map with exactly the other basic map (we already know that this
584 * other basic map is included in the extension, because there
585 * were no "cut" inequalities in "i") and we can replace the
586 * two basic maps by this extension.
587 * Place this extension in the position that is the smallest of i and j.
588 * ____ _____
589 * / || / |
590 * / || / |
591 * \ || => \ |
592 * \ || \ |
593 * \___|| \____|
595 static enum isl_change is_adj_eq_extension(int i, int j, int k,
596 struct isl_coalesce_info *info, int *eq_i, int *ineq_i,
597 int *eq_j, int *ineq_j)
599 int change = isl_change_none;
600 int super;
601 struct isl_tab_undo *snap, *snap2;
602 unsigned n_eq = info[i].bmap->n_eq;
604 if (isl_tab_is_equality(info[i].tab, n_eq + k))
605 return isl_change_none;
607 snap = isl_tab_snap(info[i].tab);
608 if (isl_tab_relax(info[i].tab, n_eq + k) < 0)
609 return isl_change_error;
610 snap2 = isl_tab_snap(info[i].tab);
611 if (isl_tab_select_facet(info[i].tab, n_eq + k) < 0)
612 return isl_change_error;
613 super = contains(info[j].bmap, ineq_j, info[i].tab);
614 if (super) {
615 if (isl_tab_rollback(info[i].tab, snap2) < 0)
616 return isl_change_error;
617 info[i].bmap = isl_basic_map_cow(info[i].bmap);
618 if (!info[i].bmap)
619 return isl_change_error;
620 isl_int_add_ui(info[i].bmap->ineq[k][0],
621 info[i].bmap->ineq[k][0], 1);
622 ISL_F_SET(info[i].bmap, ISL_BASIC_MAP_FINAL);
623 drop(&info[j]);
624 if (j < i)
625 exchange(&info[i], &info[j]);
626 change = isl_change_fuse;
627 } else
628 if (isl_tab_rollback(info[i].tab, snap) < 0)
629 return isl_change_error;
631 return change;
634 /* Data structure that keeps track of the wrapping constraints
635 * and of information to bound the coefficients of those constraints.
637 * bound is set if we want to apply a bound on the coefficients
638 * mat contains the wrapping constraints
639 * max is the bound on the coefficients (if bound is set)
641 struct isl_wraps {
642 int bound;
643 isl_mat *mat;
644 isl_int max;
647 /* Update wraps->max to be greater than or equal to the coefficients
648 * in the equalities and inequalities of bmap that can be removed if we end up
649 * applying wrapping.
651 static void wraps_update_max(struct isl_wraps *wraps,
652 __isl_keep isl_basic_map *bmap, int *eq, int *ineq)
654 int k;
655 isl_int max_k;
656 unsigned total = isl_basic_map_total_dim(bmap);
658 isl_int_init(max_k);
660 for (k = 0; k < bmap->n_eq; ++k) {
661 if (eq[2 * k] == STATUS_VALID &&
662 eq[2 * k + 1] == STATUS_VALID)
663 continue;
664 isl_seq_abs_max(bmap->eq[k] + 1, total, &max_k);
665 if (isl_int_abs_gt(max_k, wraps->max))
666 isl_int_set(wraps->max, max_k);
669 for (k = 0; k < bmap->n_ineq; ++k) {
670 if (ineq[k] == STATUS_VALID || ineq[k] == STATUS_REDUNDANT)
671 continue;
672 isl_seq_abs_max(bmap->ineq[k] + 1, total, &max_k);
673 if (isl_int_abs_gt(max_k, wraps->max))
674 isl_int_set(wraps->max, max_k);
677 isl_int_clear(max_k);
680 /* Initialize the isl_wraps data structure.
681 * If we want to bound the coefficients of the wrapping constraints,
682 * we set wraps->max to the largest coefficient
683 * in the equalities and inequalities that can be removed if we end up
684 * applying wrapping.
686 static void wraps_init(struct isl_wraps *wraps, __isl_take isl_mat *mat,
687 struct isl_coalesce_info *info, int i, int j,
688 int *eq_i, int *ineq_i, int *eq_j, int *ineq_j)
690 isl_ctx *ctx;
692 wraps->bound = 0;
693 wraps->mat = mat;
694 if (!mat)
695 return;
696 ctx = isl_mat_get_ctx(mat);
697 wraps->bound = isl_options_get_coalesce_bounded_wrapping(ctx);
698 if (!wraps->bound)
699 return;
700 isl_int_init(wraps->max);
701 isl_int_set_si(wraps->max, 0);
702 wraps_update_max(wraps, info[i].bmap, eq_i, ineq_i);
703 wraps_update_max(wraps, info[j].bmap, eq_j, ineq_j);
706 /* Free the contents of the isl_wraps data structure.
708 static void wraps_free(struct isl_wraps *wraps)
710 isl_mat_free(wraps->mat);
711 if (wraps->bound)
712 isl_int_clear(wraps->max);
715 /* Is the wrapping constraint in row "row" allowed?
717 * If wraps->bound is set, we check that none of the coefficients
718 * is greater than wraps->max.
720 static int allow_wrap(struct isl_wraps *wraps, int row)
722 int i;
724 if (!wraps->bound)
725 return 1;
727 for (i = 1; i < wraps->mat->n_col; ++i)
728 if (isl_int_abs_gt(wraps->mat->row[row][i], wraps->max))
729 return 0;
731 return 1;
734 /* For each non-redundant constraint in "bmap" (as determined by "tab"),
735 * wrap the constraint around "bound" such that it includes the whole
736 * set "set" and append the resulting constraint to "wraps".
737 * "wraps" is assumed to have been pre-allocated to the appropriate size.
738 * wraps->n_row is the number of actual wrapped constraints that have
739 * been added.
740 * If any of the wrapping problems results in a constraint that is
741 * identical to "bound", then this means that "set" is unbounded in such
742 * way that no wrapping is possible. If this happens then wraps->n_row
743 * is reset to zero.
744 * Similarly, if we want to bound the coefficients of the wrapping
745 * constraints and a newly added wrapping constraint does not
746 * satisfy the bound, then wraps->n_row is also reset to zero.
748 static int add_wraps(struct isl_wraps *wraps, __isl_keep isl_basic_map *bmap,
749 struct isl_tab *tab, isl_int *bound, __isl_keep isl_set *set)
751 int l;
752 int w;
753 unsigned total = isl_basic_map_total_dim(bmap);
755 w = wraps->mat->n_row;
757 for (l = 0; l < bmap->n_ineq; ++l) {
758 if (isl_seq_is_neg(bound, bmap->ineq[l], 1 + total))
759 continue;
760 if (isl_seq_eq(bound, bmap->ineq[l], 1 + total))
761 continue;
762 if (isl_tab_is_redundant(tab, bmap->n_eq + l))
763 continue;
765 isl_seq_cpy(wraps->mat->row[w], bound, 1 + total);
766 if (!isl_set_wrap_facet(set, wraps->mat->row[w], bmap->ineq[l]))
767 return -1;
768 if (isl_seq_eq(wraps->mat->row[w], bound, 1 + total))
769 goto unbounded;
770 if (!allow_wrap(wraps, w))
771 goto unbounded;
772 ++w;
774 for (l = 0; l < bmap->n_eq; ++l) {
775 if (isl_seq_is_neg(bound, bmap->eq[l], 1 + total))
776 continue;
777 if (isl_seq_eq(bound, bmap->eq[l], 1 + total))
778 continue;
780 isl_seq_cpy(wraps->mat->row[w], bound, 1 + total);
781 isl_seq_neg(wraps->mat->row[w + 1], bmap->eq[l], 1 + total);
782 if (!isl_set_wrap_facet(set, wraps->mat->row[w],
783 wraps->mat->row[w + 1]))
784 return -1;
785 if (isl_seq_eq(wraps->mat->row[w], bound, 1 + total))
786 goto unbounded;
787 if (!allow_wrap(wraps, w))
788 goto unbounded;
789 ++w;
791 isl_seq_cpy(wraps->mat->row[w], bound, 1 + total);
792 if (!isl_set_wrap_facet(set, wraps->mat->row[w], bmap->eq[l]))
793 return -1;
794 if (isl_seq_eq(wraps->mat->row[w], bound, 1 + total))
795 goto unbounded;
796 if (!allow_wrap(wraps, w))
797 goto unbounded;
798 ++w;
801 wraps->mat->n_row = w;
802 return 0;
803 unbounded:
804 wraps->mat->n_row = 0;
805 return 0;
808 /* Check if the constraints in "wraps" from "first" until the last
809 * are all valid for the basic set represented by "tab".
810 * If not, wraps->n_row is set to zero.
812 static int check_wraps(__isl_keep isl_mat *wraps, int first,
813 struct isl_tab *tab)
815 int i;
817 for (i = first; i < wraps->n_row; ++i) {
818 enum isl_ineq_type type;
819 type = isl_tab_ineq_type(tab, wraps->row[i]);
820 if (type == isl_ineq_error)
821 return -1;
822 if (type == isl_ineq_redundant)
823 continue;
824 wraps->n_row = 0;
825 return 0;
828 return 0;
831 /* Return a set that corresponds to the non-redundant constraints
832 * (as recorded in tab) of bmap.
834 * It's important to remove the redundant constraints as some
835 * of the other constraints may have been modified after the
836 * constraints were marked redundant.
837 * In particular, a constraint may have been relaxed.
838 * Redundant constraints are ignored when a constraint is relaxed
839 * and should therefore continue to be ignored ever after.
840 * Otherwise, the relaxation might be thwarted by some of
841 * these constraints.
843 * Update the underlying set to ensure that the dimension doesn't change.
844 * Otherwise the integer divisions could get dropped if the tab
845 * turns out to be empty.
847 static __isl_give isl_set *set_from_updated_bmap(__isl_keep isl_basic_map *bmap,
848 struct isl_tab *tab)
850 isl_basic_set *bset;
852 bmap = isl_basic_map_copy(bmap);
853 bset = isl_basic_map_underlying_set(bmap);
854 bset = isl_basic_set_cow(bset);
855 bset = isl_basic_set_update_from_tab(bset, tab);
856 return isl_set_from_basic_set(bset);
859 /* Given a basic set i with a constraint k that is adjacent to
860 * basic set j, check if we can wrap
861 * both the facet corresponding to k and basic map j
862 * around their ridges to include the other set.
863 * If so, replace the pair of basic sets by their union.
865 * All constraints of i (except k) are assumed to be valid for j.
866 * This means that there is no real need to wrap the ridges of
867 * the faces of basic map i around basic map j but since we do,
868 * we have to check that the resulting wrapping constraints are valid for i.
869 * ____ _____
870 * / | / \
871 * / || / |
872 * \ || => \ |
873 * \ || \ |
874 * \___|| \____|
877 static enum isl_change can_wrap_in_facet(int i, int j, int k,
878 struct isl_coalesce_info *info, int *eq_i, int *ineq_i,
879 int *eq_j, int *ineq_j)
881 enum isl_change change = isl_change_none;
882 struct isl_wraps wraps;
883 isl_ctx *ctx;
884 isl_mat *mat;
885 struct isl_set *set_i = NULL;
886 struct isl_set *set_j = NULL;
887 struct isl_vec *bound = NULL;
888 unsigned total = isl_basic_map_total_dim(info[i].bmap);
889 struct isl_tab_undo *snap;
890 int n;
892 set_i = set_from_updated_bmap(info[i].bmap, info[i].tab);
893 set_j = set_from_updated_bmap(info[j].bmap, info[j].tab);
894 ctx = isl_basic_map_get_ctx(info[i].bmap);
895 mat = isl_mat_alloc(ctx, 2 * (info[i].bmap->n_eq + info[j].bmap->n_eq) +
896 info[i].bmap->n_ineq + info[j].bmap->n_ineq,
897 1 + total);
898 wraps_init(&wraps, mat, info, i, j, eq_i, ineq_i, eq_j, ineq_j);
899 bound = isl_vec_alloc(ctx, 1 + total);
900 if (!set_i || !set_j || !wraps.mat || !bound)
901 goto error;
903 isl_seq_cpy(bound->el, info[i].bmap->ineq[k], 1 + total);
904 isl_int_add_ui(bound->el[0], bound->el[0], 1);
906 isl_seq_cpy(wraps.mat->row[0], bound->el, 1 + total);
907 wraps.mat->n_row = 1;
909 if (add_wraps(&wraps, info[j].bmap, info[j].tab, bound->el, set_i) < 0)
910 goto error;
911 if (!wraps.mat->n_row)
912 goto unbounded;
914 snap = isl_tab_snap(info[i].tab);
916 if (isl_tab_select_facet(info[i].tab, info[i].bmap->n_eq + k) < 0)
917 goto error;
918 if (isl_tab_detect_redundant(info[i].tab) < 0)
919 goto error;
921 isl_seq_neg(bound->el, info[i].bmap->ineq[k], 1 + total);
923 n = wraps.mat->n_row;
924 if (add_wraps(&wraps, info[i].bmap, info[i].tab, bound->el, set_j) < 0)
925 goto error;
927 if (isl_tab_rollback(info[i].tab, snap) < 0)
928 goto error;
929 if (check_wraps(wraps.mat, n, info[i].tab) < 0)
930 goto error;
931 if (!wraps.mat->n_row)
932 goto unbounded;
934 change = fuse(i, j, info, eq_i, ineq_i, eq_j, ineq_j, wraps.mat, 0);
936 unbounded:
937 wraps_free(&wraps);
939 isl_set_free(set_i);
940 isl_set_free(set_j);
942 isl_vec_free(bound);
944 return change;
945 error:
946 wraps_free(&wraps);
947 isl_vec_free(bound);
948 isl_set_free(set_i);
949 isl_set_free(set_j);
950 return isl_change_error;
953 /* Given a pair of basic maps i and j such that j sticks out
954 * of i at n cut constraints, each time by at most one,
955 * try to compute wrapping constraints and replace the two
956 * basic maps by a single basic map.
957 * The other constraints of i are assumed to be valid for j.
959 * For each cut constraint t(x) >= 0 of i, we add the relaxed version
960 * t(x) + 1 >= 0, along with wrapping constraints for all constraints
961 * of basic map j that bound the part of basic map j that sticks out
962 * of the cut constraint.
963 * In particular, we first intersect basic map j with t(x) + 1 = 0.
964 * If the result is empty, then t(x) >= 0 was actually a valid constraint
965 * (with respect to the integer points), so we add t(x) >= 0 instead.
966 * Otherwise, we wrap the constraints of basic map j that are not
967 * redundant in this intersection over the union of the two basic maps.
969 * If any wrapping fails, i.e., if we cannot wrap to touch
970 * the union, then we give up.
971 * Otherwise, the pair of basic maps is replaced by their union.
973 static enum isl_change wrap_in_facets(int i, int j, int *cuts, int n,
974 struct isl_coalesce_info *info,
975 int *eq_i, int *ineq_i, int *eq_j, int *ineq_j)
977 enum isl_change change = isl_change_none;
978 struct isl_wraps wraps;
979 isl_ctx *ctx;
980 isl_mat *mat;
981 isl_set *set = NULL;
982 unsigned total = isl_basic_map_total_dim(info[i].bmap);
983 int max_wrap;
984 int k, w;
985 struct isl_tab_undo *snap;
987 if (isl_tab_extend_cons(info[j].tab, 1) < 0)
988 goto error;
990 max_wrap = 1 + 2 * info[j].bmap->n_eq + info[j].bmap->n_ineq;
991 max_wrap *= n;
993 set = isl_set_union(set_from_updated_bmap(info[i].bmap, info[i].tab),
994 set_from_updated_bmap(info[j].bmap, info[j].tab));
995 ctx = isl_basic_map_get_ctx(info[i].bmap);
996 mat = isl_mat_alloc(ctx, max_wrap, 1 + total);
997 wraps_init(&wraps, mat, info, i, j, eq_i, ineq_i, eq_j, ineq_j);
998 if (!set || !wraps.mat)
999 goto error;
1001 snap = isl_tab_snap(info[j].tab);
1003 wraps.mat->n_row = 0;
1005 for (k = 0; k < n; ++k) {
1006 w = wraps.mat->n_row++;
1007 isl_seq_cpy(wraps.mat->row[w],
1008 info[i].bmap->ineq[cuts[k]], 1 + total);
1009 isl_int_add_ui(wraps.mat->row[w][0], wraps.mat->row[w][0], 1);
1010 if (isl_tab_add_eq(info[j].tab, wraps.mat->row[w]) < 0)
1011 goto error;
1012 if (isl_tab_detect_redundant(info[j].tab) < 0)
1013 goto error;
1015 if (info[j].tab->empty)
1016 isl_int_sub_ui(wraps.mat->row[w][0],
1017 wraps.mat->row[w][0], 1);
1018 else if (add_wraps(&wraps, info[j].bmap, info[j].tab,
1019 wraps.mat->row[w], set) < 0)
1020 goto error;
1022 if (isl_tab_rollback(info[j].tab, snap) < 0)
1023 goto error;
1025 if (!wraps.mat->n_row)
1026 break;
1029 if (k == n)
1030 change = fuse(i, j, info,
1031 eq_i, ineq_i, eq_j, ineq_j, wraps.mat, 0);
1033 wraps_free(&wraps);
1034 isl_set_free(set);
1036 return change;
1037 error:
1038 wraps_free(&wraps);
1039 isl_set_free(set);
1040 return isl_change_error;
1043 /* Given two basic sets i and j such that i has no cut equalities,
1044 * check if relaxing all the cut inequalities of i by one turns
1045 * them into valid constraint for j and check if we can wrap in
1046 * the bits that are sticking out.
1047 * If so, replace the pair by their union.
1049 * We first check if all relaxed cut inequalities of i are valid for j
1050 * and then try to wrap in the intersections of the relaxed cut inequalities
1051 * with j.
1053 * During this wrapping, we consider the points of j that lie at a distance
1054 * of exactly 1 from i. In particular, we ignore the points that lie in
1055 * between this lower-dimensional space and the basic map i.
1056 * We can therefore only apply this to integer maps.
1057 * ____ _____
1058 * / ___|_ / \
1059 * / | | / |
1060 * \ | | => \ |
1061 * \|____| \ |
1062 * \___| \____/
1064 * _____ ______
1065 * | ____|_ | \
1066 * | | | | |
1067 * | | | => | |
1068 * |_| | | |
1069 * |_____| \______|
1071 * _______
1072 * | |
1073 * | |\ |
1074 * | | \ |
1075 * | | \ |
1076 * | | \|
1077 * | | \
1078 * | |_____\
1079 * | |
1080 * |_______|
1082 * Wrapping can fail if the result of wrapping one of the facets
1083 * around its edges does not produce any new facet constraint.
1084 * In particular, this happens when we try to wrap in unbounded sets.
1086 * _______________________________________________________________________
1088 * | ___
1089 * | | |
1090 * |_| |_________________________________________________________________
1091 * |___|
1093 * The following is not an acceptable result of coalescing the above two
1094 * sets as it includes extra integer points.
1095 * _______________________________________________________________________
1097 * |
1098 * |
1100 * \______________________________________________________________________
1102 static enum isl_change can_wrap_in_set(int i, int j,
1103 struct isl_coalesce_info *info, int *eq_i, int *ineq_i,
1104 int *eq_j, int *ineq_j)
1106 enum isl_change change = isl_change_none;
1107 int k, m;
1108 int n;
1109 int *cuts = NULL;
1110 isl_ctx *ctx;
1112 if (ISL_F_ISSET(info[i].bmap, ISL_BASIC_MAP_RATIONAL) ||
1113 ISL_F_ISSET(info[j].bmap, ISL_BASIC_MAP_RATIONAL))
1114 return isl_change_none;
1116 n = count(ineq_i, info[i].bmap->n_ineq, STATUS_CUT);
1117 if (n == 0)
1118 return isl_change_none;
1120 ctx = isl_basic_map_get_ctx(info[i].bmap);
1121 cuts = isl_alloc_array(ctx, int, n);
1122 if (!cuts)
1123 return isl_change_error;
1125 for (k = 0, m = 0; m < n; ++k) {
1126 enum isl_ineq_type type;
1128 if (ineq_i[k] != STATUS_CUT)
1129 continue;
1131 isl_int_add_ui(info[i].bmap->ineq[k][0],
1132 info[i].bmap->ineq[k][0], 1);
1133 type = isl_tab_ineq_type(info[j].tab, info[i].bmap->ineq[k]);
1134 isl_int_sub_ui(info[i].bmap->ineq[k][0],
1135 info[i].bmap->ineq[k][0], 1);
1136 if (type == isl_ineq_error)
1137 goto error;
1138 if (type != isl_ineq_redundant)
1139 break;
1140 cuts[m] = k;
1141 ++m;
1144 if (m == n)
1145 change = wrap_in_facets(i, j, cuts, n, info,
1146 eq_i, ineq_i, eq_j, ineq_j);
1148 free(cuts);
1150 return change;
1151 error:
1152 free(cuts);
1153 return isl_change_error;
1156 /* Check if either i or j has only cut inequalities that can
1157 * be used to wrap in (a facet of) the other basic set.
1158 * if so, replace the pair by their union.
1160 static enum isl_change check_wrap(int i, int j, struct isl_coalesce_info *info,
1161 int *eq_i, int *ineq_i, int *eq_j, int *ineq_j)
1163 enum isl_change change = isl_change_none;
1165 if (!any(eq_i, 2 * info[i].bmap->n_eq, STATUS_CUT))
1166 change = can_wrap_in_set(i, j, info,
1167 eq_i, ineq_i, eq_j, ineq_j);
1168 if (change != isl_change_none)
1169 return change;
1171 if (!any(eq_j, 2 * info[j].bmap->n_eq, STATUS_CUT))
1172 change = can_wrap_in_set(j, i, info,
1173 eq_j, ineq_j, eq_i, ineq_i);
1174 return change;
1177 /* At least one of the basic maps has an equality that is adjacent
1178 * to inequality. Make sure that only one of the basic maps has
1179 * such an equality and that the other basic map has exactly one
1180 * inequality adjacent to an equality.
1181 * We call the basic map that has the inequality "i" and the basic
1182 * map that has the equality "j".
1183 * If "i" has any "cut" (in)equality, then relaxing the inequality
1184 * by one would not result in a basic map that contains the other
1185 * basic map.
1187 static enum isl_change check_adj_eq(int i, int j,
1188 struct isl_coalesce_info *info, int *eq_i, int *ineq_i,
1189 int *eq_j, int *ineq_j)
1191 enum isl_change change = isl_change_none;
1192 int k;
1194 if (any(eq_i, 2 * info[i].bmap->n_eq, STATUS_ADJ_INEQ) &&
1195 any(eq_j, 2 * info[j].bmap->n_eq, STATUS_ADJ_INEQ))
1196 /* ADJ EQ TOO MANY */
1197 return isl_change_none;
1199 if (any(eq_i, 2 * info[i].bmap->n_eq, STATUS_ADJ_INEQ))
1200 return check_adj_eq(j, i, info, eq_j, ineq_j, eq_i, ineq_i);
1202 /* j has an equality adjacent to an inequality in i */
1204 if (any(eq_i, 2 * info[i].bmap->n_eq, STATUS_CUT))
1205 return isl_change_none;
1206 if (any(ineq_i, info[i].bmap->n_ineq, STATUS_CUT))
1207 /* ADJ EQ CUT */
1208 return isl_change_none;
1209 if (count(ineq_i, info[i].bmap->n_ineq, STATUS_ADJ_EQ) != 1 ||
1210 any(ineq_j, info[j].bmap->n_ineq, STATUS_ADJ_EQ) ||
1211 any(ineq_i, info[i].bmap->n_ineq, STATUS_ADJ_INEQ) ||
1212 any(ineq_j, info[j].bmap->n_ineq, STATUS_ADJ_INEQ))
1213 /* ADJ EQ TOO MANY */
1214 return isl_change_none;
1216 for (k = 0; k < info[i].bmap->n_ineq; ++k)
1217 if (ineq_i[k] == STATUS_ADJ_EQ)
1218 break;
1220 change = is_adj_eq_extension(i, j, k, info,
1221 eq_i, ineq_i, eq_j, ineq_j);
1222 if (change != isl_change_none)
1223 return change;
1225 if (count(eq_j, 2 * info[j].bmap->n_eq, STATUS_ADJ_INEQ) != 1)
1226 return isl_change_none;
1228 change = can_wrap_in_facet(i, j, k, info, eq_i, ineq_i, eq_j, ineq_j);
1230 return change;
1233 /* The two basic maps lie on adjacent hyperplanes. In particular,
1234 * basic map "i" has an equality that lies parallel to basic map "j".
1235 * Check if we can wrap the facets around the parallel hyperplanes
1236 * to include the other set.
1238 * We perform basically the same operations as can_wrap_in_facet,
1239 * except that we don't need to select a facet of one of the sets.
1241 * \\ \\
1242 * \\ => \\
1243 * \ \|
1245 * If there is more than one equality of "i" adjacent to an equality of "j",
1246 * then the result will satisfy one or more equalities that are a linear
1247 * combination of these equalities. These will be encoded as pairs
1248 * of inequalities in the wrapping constraints and need to be made
1249 * explicit.
1251 static enum isl_change check_eq_adj_eq(int i, int j,
1252 struct isl_coalesce_info *info, int *eq_i, int *ineq_i,
1253 int *eq_j, int *ineq_j)
1255 int k;
1256 enum isl_change change = isl_change_none;
1257 int detect_equalities = 0;
1258 struct isl_wraps wraps;
1259 isl_ctx *ctx;
1260 isl_mat *mat;
1261 struct isl_set *set_i = NULL;
1262 struct isl_set *set_j = NULL;
1263 struct isl_vec *bound = NULL;
1264 unsigned total = isl_basic_map_total_dim(info[i].bmap);
1266 if (count(eq_i, 2 * info[i].bmap->n_eq, STATUS_ADJ_EQ) != 1)
1267 detect_equalities = 1;
1269 for (k = 0; k < 2 * info[i].bmap->n_eq ; ++k)
1270 if (eq_i[k] == STATUS_ADJ_EQ)
1271 break;
1273 set_i = set_from_updated_bmap(info[i].bmap, info[i].tab);
1274 set_j = set_from_updated_bmap(info[j].bmap, info[j].tab);
1275 ctx = isl_basic_map_get_ctx(info[i].bmap);
1276 mat = isl_mat_alloc(ctx, 2 * (info[i].bmap->n_eq + info[j].bmap->n_eq) +
1277 info[i].bmap->n_ineq + info[j].bmap->n_ineq,
1278 1 + total);
1279 wraps_init(&wraps, mat, info, i, j, eq_i, ineq_i, eq_j, ineq_j);
1280 bound = isl_vec_alloc(ctx, 1 + total);
1281 if (!set_i || !set_j || !wraps.mat || !bound)
1282 goto error;
1284 if (k % 2 == 0)
1285 isl_seq_neg(bound->el, info[i].bmap->eq[k / 2], 1 + total);
1286 else
1287 isl_seq_cpy(bound->el, info[i].bmap->eq[k / 2], 1 + total);
1288 isl_int_add_ui(bound->el[0], bound->el[0], 1);
1290 isl_seq_cpy(wraps.mat->row[0], bound->el, 1 + total);
1291 wraps.mat->n_row = 1;
1293 if (add_wraps(&wraps, info[j].bmap, info[j].tab, bound->el, set_i) < 0)
1294 goto error;
1295 if (!wraps.mat->n_row)
1296 goto unbounded;
1298 isl_int_sub_ui(bound->el[0], bound->el[0], 1);
1299 isl_seq_neg(bound->el, bound->el, 1 + total);
1301 isl_seq_cpy(wraps.mat->row[wraps.mat->n_row], bound->el, 1 + total);
1302 wraps.mat->n_row++;
1304 if (add_wraps(&wraps, info[i].bmap, info[i].tab, bound->el, set_j) < 0)
1305 goto error;
1306 if (!wraps.mat->n_row)
1307 goto unbounded;
1309 change = fuse(i, j, info, eq_i, ineq_i, eq_j, ineq_j, wraps.mat,
1310 detect_equalities);
1312 if (0) {
1313 error: change = isl_change_error;
1315 unbounded:
1317 wraps_free(&wraps);
1318 isl_set_free(set_i);
1319 isl_set_free(set_j);
1320 isl_vec_free(bound);
1322 return change;
1325 /* Check if the union of the given pair of basic maps
1326 * can be represented by a single basic map.
1327 * If so, replace the pair by the single basic map and return
1328 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
1329 * Otherwise, return isl_change_none.
1330 * The two basic maps are assumed to live in the same local space.
1332 * We first check the effect of each constraint of one basic map
1333 * on the other basic map.
1334 * The constraint may be
1335 * redundant the constraint is redundant in its own
1336 * basic map and should be ignore and removed
1337 * in the end
1338 * valid all (integer) points of the other basic map
1339 * satisfy the constraint
1340 * separate no (integer) point of the other basic map
1341 * satisfies the constraint
1342 * cut some but not all points of the other basic map
1343 * satisfy the constraint
1344 * adj_eq the given constraint is adjacent (on the outside)
1345 * to an equality of the other basic map
1346 * adj_ineq the given constraint is adjacent (on the outside)
1347 * to an inequality of the other basic map
1349 * We consider seven cases in which we can replace the pair by a single
1350 * basic map. We ignore all "redundant" constraints.
1352 * 1. all constraints of one basic map are valid
1353 * => the other basic map is a subset and can be removed
1355 * 2. all constraints of both basic maps are either "valid" or "cut"
1356 * and the facets corresponding to the "cut" constraints
1357 * of one of the basic maps lies entirely inside the other basic map
1358 * => the pair can be replaced by a basic map consisting
1359 * of the valid constraints in both basic maps
1361 * 3. there is a single pair of adjacent inequalities
1362 * (all other constraints are "valid")
1363 * => the pair can be replaced by a basic map consisting
1364 * of the valid constraints in both basic maps
1366 * 4. one basic map has a single adjacent inequality, while the other
1367 * constraints are "valid". The other basic map has some
1368 * "cut" constraints, but replacing the adjacent inequality by
1369 * its opposite and adding the valid constraints of the other
1370 * basic map results in a subset of the other basic map
1371 * => the pair can be replaced by a basic map consisting
1372 * of the valid constraints in both basic maps
1374 * 5. there is a single adjacent pair of an inequality and an equality,
1375 * the other constraints of the basic map containing the inequality are
1376 * "valid". Moreover, if the inequality the basic map is relaxed
1377 * and then turned into an equality, then resulting facet lies
1378 * entirely inside the other basic map
1379 * => the pair can be replaced by the basic map containing
1380 * the inequality, with the inequality relaxed.
1382 * 6. there is a single adjacent pair of an inequality and an equality,
1383 * the other constraints of the basic map containing the inequality are
1384 * "valid". Moreover, the facets corresponding to both
1385 * the inequality and the equality can be wrapped around their
1386 * ridges to include the other basic map
1387 * => the pair can be replaced by a basic map consisting
1388 * of the valid constraints in both basic maps together
1389 * with all wrapping constraints
1391 * 7. one of the basic maps extends beyond the other by at most one.
1392 * Moreover, the facets corresponding to the cut constraints and
1393 * the pieces of the other basic map at offset one from these cut
1394 * constraints can be wrapped around their ridges to include
1395 * the union of the two basic maps
1396 * => the pair can be replaced by a basic map consisting
1397 * of the valid constraints in both basic maps together
1398 * with all wrapping constraints
1400 * 8. the two basic maps live in adjacent hyperplanes. In principle
1401 * such sets can always be combined through wrapping, but we impose
1402 * that there is only one such pair, to avoid overeager coalescing.
1404 * Throughout the computation, we maintain a collection of tableaus
1405 * corresponding to the basic maps. When the basic maps are dropped
1406 * or combined, the tableaus are modified accordingly.
1408 static enum isl_change coalesce_local_pair(int i, int j,
1409 struct isl_coalesce_info *info)
1411 enum isl_change change = isl_change_none;
1412 int *eq_i = NULL;
1413 int *eq_j = NULL;
1414 int *ineq_i = NULL;
1415 int *ineq_j = NULL;
1417 eq_i = eq_status_in(info[i].bmap, info[j].tab);
1418 if (info[i].bmap->n_eq && !eq_i)
1419 goto error;
1420 if (any(eq_i, 2 * info[i].bmap->n_eq, STATUS_ERROR))
1421 goto error;
1422 if (any(eq_i, 2 * info[i].bmap->n_eq, STATUS_SEPARATE))
1423 goto done;
1425 eq_j = eq_status_in(info[j].bmap, info[i].tab);
1426 if (info[j].bmap->n_eq && !eq_j)
1427 goto error;
1428 if (any(eq_j, 2 * info[j].bmap->n_eq, STATUS_ERROR))
1429 goto error;
1430 if (any(eq_j, 2 * info[j].bmap->n_eq, STATUS_SEPARATE))
1431 goto done;
1433 ineq_i = ineq_status_in(info[i].bmap, info[i].tab, info[j].tab);
1434 if (info[i].bmap->n_ineq && !ineq_i)
1435 goto error;
1436 if (any(ineq_i, info[i].bmap->n_ineq, STATUS_ERROR))
1437 goto error;
1438 if (any(ineq_i, info[i].bmap->n_ineq, STATUS_SEPARATE))
1439 goto done;
1441 ineq_j = ineq_status_in(info[j].bmap, info[j].tab, info[i].tab);
1442 if (info[j].bmap->n_ineq && !ineq_j)
1443 goto error;
1444 if (any(ineq_j, info[j].bmap->n_ineq, STATUS_ERROR))
1445 goto error;
1446 if (any(ineq_j, info[j].bmap->n_ineq, STATUS_SEPARATE))
1447 goto done;
1449 if (all(eq_i, 2 * info[i].bmap->n_eq, STATUS_VALID) &&
1450 all(ineq_i, info[i].bmap->n_ineq, STATUS_VALID)) {
1451 drop(&info[j]);
1452 change = isl_change_drop_second;
1453 } else if (all(eq_j, 2 * info[j].bmap->n_eq, STATUS_VALID) &&
1454 all(ineq_j, info[j].bmap->n_ineq, STATUS_VALID)) {
1455 drop(&info[i]);
1456 change = isl_change_drop_first;
1457 } else if (any(eq_i, 2 * info[i].bmap->n_eq, STATUS_ADJ_EQ)) {
1458 change = check_eq_adj_eq(i, j, info,
1459 eq_i, ineq_i, eq_j, ineq_j);
1460 } else if (any(eq_j, 2 * info[j].bmap->n_eq, STATUS_ADJ_EQ)) {
1461 change = check_eq_adj_eq(j, i, info,
1462 eq_j, ineq_j, eq_i, ineq_i);
1463 } else if (any(eq_i, 2 * info[i].bmap->n_eq, STATUS_ADJ_INEQ) ||
1464 any(eq_j, 2 * info[j].bmap->n_eq, STATUS_ADJ_INEQ)) {
1465 change = check_adj_eq(i, j, info,
1466 eq_i, ineq_i, eq_j, ineq_j);
1467 } else if (any(ineq_i, info[i].bmap->n_ineq, STATUS_ADJ_EQ) ||
1468 any(ineq_j, info[j].bmap->n_ineq, STATUS_ADJ_EQ)) {
1469 /* Can't happen */
1470 /* BAD ADJ INEQ */
1471 } else if (any(ineq_i, info[i].bmap->n_ineq, STATUS_ADJ_INEQ) ||
1472 any(ineq_j, info[j].bmap->n_ineq, STATUS_ADJ_INEQ)) {
1473 change = check_adj_ineq(i, j, info,
1474 eq_i, ineq_i, eq_j, ineq_j);
1475 } else {
1476 if (!any(eq_i, 2 * info[i].bmap->n_eq, STATUS_CUT) &&
1477 !any(eq_j, 2 * info[j].bmap->n_eq, STATUS_CUT))
1478 change = check_facets(i, j, info, ineq_i, ineq_j);
1479 if (change == isl_change_none)
1480 change = check_wrap(i, j, info,
1481 eq_i, ineq_i, eq_j, ineq_j);
1484 done:
1485 free(eq_i);
1486 free(eq_j);
1487 free(ineq_i);
1488 free(ineq_j);
1489 return change;
1490 error:
1491 free(eq_i);
1492 free(eq_j);
1493 free(ineq_i);
1494 free(ineq_j);
1495 return isl_change_error;
1498 /* Do the two basic maps live in the same local space, i.e.,
1499 * do they have the same (known) divs?
1500 * If either basic map has any unknown divs, then we can only assume
1501 * that they do not live in the same local space.
1503 static int same_divs(__isl_keep isl_basic_map *bmap1,
1504 __isl_keep isl_basic_map *bmap2)
1506 int i;
1507 int known;
1508 int total;
1510 if (!bmap1 || !bmap2)
1511 return -1;
1512 if (bmap1->n_div != bmap2->n_div)
1513 return 0;
1515 if (bmap1->n_div == 0)
1516 return 1;
1518 known = isl_basic_map_divs_known(bmap1);
1519 if (known < 0 || !known)
1520 return known;
1521 known = isl_basic_map_divs_known(bmap2);
1522 if (known < 0 || !known)
1523 return known;
1525 total = isl_basic_map_total_dim(bmap1);
1526 for (i = 0; i < bmap1->n_div; ++i)
1527 if (!isl_seq_eq(bmap1->div[i], bmap2->div[i], 2 + total))
1528 return 0;
1530 return 1;
1533 /* Does "bmap" contain the basic map represented by the tableau "tab"
1534 * after expanding the divs of "bmap" to match those of "tab"?
1535 * The expansion is performed using the divs "div" and expansion "exp"
1536 * computed by the caller.
1537 * Then we check if all constraints of the expanded "bmap" are valid for "tab".
1539 static int contains_with_expanded_divs(__isl_keep isl_basic_map *bmap,
1540 struct isl_tab *tab, __isl_keep isl_mat *div, int *exp)
1542 int superset = 0;
1543 int *eq_i = NULL;
1544 int *ineq_i = NULL;
1546 bmap = isl_basic_map_copy(bmap);
1547 bmap = isl_basic_set_expand_divs(bmap, isl_mat_copy(div), exp);
1549 if (!bmap)
1550 goto error;
1552 eq_i = eq_status_in(bmap, tab);
1553 if (bmap->n_eq && !eq_i)
1554 goto error;
1555 if (any(eq_i, 2 * bmap->n_eq, STATUS_ERROR))
1556 goto error;
1557 if (any(eq_i, 2 * bmap->n_eq, STATUS_SEPARATE))
1558 goto done;
1560 ineq_i = ineq_status_in(bmap, NULL, tab);
1561 if (bmap->n_ineq && !ineq_i)
1562 goto error;
1563 if (any(ineq_i, bmap->n_ineq, STATUS_ERROR))
1564 goto error;
1565 if (any(ineq_i, bmap->n_ineq, STATUS_SEPARATE))
1566 goto done;
1568 if (all(eq_i, 2 * bmap->n_eq, STATUS_VALID) &&
1569 all(ineq_i, bmap->n_ineq, STATUS_VALID))
1570 superset = 1;
1572 done:
1573 isl_basic_map_free(bmap);
1574 free(eq_i);
1575 free(ineq_i);
1576 return superset;
1577 error:
1578 isl_basic_map_free(bmap);
1579 free(eq_i);
1580 free(ineq_i);
1581 return -1;
1584 /* Does "bmap_i" contain the basic map represented by "info_j"
1585 * after aligning the divs of "bmap_i" to those of "info_j".
1586 * Note that this can only succeed if the number of divs of "bmap_i"
1587 * is smaller than (or equal to) the number of divs of "info_j".
1589 * We first check if the divs of "bmap_i" are all known and form a subset
1590 * of those of "bmap_j". If so, we pass control over to
1591 * contains_with_expanded_divs.
1593 static int contains_after_aligning_divs(__isl_keep isl_basic_map *bmap_i,
1594 struct isl_coalesce_info *info_j)
1596 int known;
1597 isl_mat *div_i, *div_j, *div;
1598 int *exp1 = NULL;
1599 int *exp2 = NULL;
1600 isl_ctx *ctx;
1601 int subset;
1603 known = isl_basic_map_divs_known(bmap_i);
1604 if (known < 0 || !known)
1605 return known;
1607 ctx = isl_basic_map_get_ctx(bmap_i);
1609 div_i = isl_basic_map_get_divs(bmap_i);
1610 div_j = isl_basic_map_get_divs(info_j->bmap);
1612 if (!div_i || !div_j)
1613 goto error;
1615 exp1 = isl_alloc_array(ctx, int, div_i->n_row);
1616 exp2 = isl_alloc_array(ctx, int, div_j->n_row);
1617 if ((div_i->n_row && !exp1) || (div_j->n_row && !exp2))
1618 goto error;
1620 div = isl_merge_divs(div_i, div_j, exp1, exp2);
1621 if (!div)
1622 goto error;
1624 if (div->n_row == div_j->n_row)
1625 subset = contains_with_expanded_divs(bmap_i,
1626 info_j->tab, div, exp1);
1627 else
1628 subset = 0;
1630 isl_mat_free(div);
1632 isl_mat_free(div_i);
1633 isl_mat_free(div_j);
1635 free(exp2);
1636 free(exp1);
1638 return subset;
1639 error:
1640 isl_mat_free(div_i);
1641 isl_mat_free(div_j);
1642 free(exp1);
1643 free(exp2);
1644 return -1;
1647 /* Check if the basic map "j" is a subset of basic map "i",
1648 * if "i" has fewer divs that "j".
1649 * If so, remove basic map "j".
1651 * If the two basic maps have the same number of divs, then
1652 * they must necessarily be different. Otherwise, we would have
1653 * called coalesce_local_pair. We therefore don't try anything
1654 * in this case.
1656 static int coalesced_subset(int i, int j, struct isl_coalesce_info *info)
1658 int superset;
1660 if (info[i].bmap->n_div >= info[j].bmap->n_div)
1661 return 0;
1663 superset = contains_after_aligning_divs(info[i].bmap, &info[j]);
1664 if (superset < 0)
1665 return -1;
1666 if (superset)
1667 drop(&info[j]);
1669 return superset;
1672 /* Check if one of the basic maps is a subset of the other and, if so,
1673 * drop the subset.
1674 * Note that we only perform any test if the number of divs is different
1675 * in the two basic maps. In case the number of divs is the same,
1676 * we have already established that the divs are different
1677 * in the two basic maps.
1678 * In particular, if the number of divs of basic map i is smaller than
1679 * the number of divs of basic map j, then we check if j is a subset of i
1680 * and vice versa.
1682 static enum isl_change check_coalesce_subset(int i, int j,
1683 struct isl_coalesce_info *info)
1685 int changed;
1687 changed = coalesced_subset(i, j, info);
1688 if (changed < 0 || changed)
1689 return changed < 0 ? isl_change_error : isl_change_drop_second;
1691 changed = coalesced_subset(j, i, info);
1692 if (changed < 0 || changed)
1693 return changed < 0 ? isl_change_error : isl_change_drop_first;
1695 return isl_change_none;
1698 /* Check if the union of the given pair of basic maps
1699 * can be represented by a single basic map.
1700 * If so, replace the pair by the single basic map and return
1701 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
1702 * Otherwise, return isl_change_none.
1704 * We first check if the two basic maps live in the same local space.
1705 * If so, we do the complete check. Otherwise, we check if one is
1706 * an obvious subset of the other.
1708 static enum isl_change coalesce_pair(int i, int j,
1709 struct isl_coalesce_info *info)
1711 int same;
1713 same = same_divs(info[i].bmap, info[j].bmap);
1714 if (same < 0)
1715 return isl_change_error;
1716 if (same)
1717 return coalesce_local_pair(i, j, info);
1719 return check_coalesce_subset(i, j, info);
1722 /* Pairwise coalesce the basic maps described by the "n" elements of "info",
1723 * skipping basic maps that have been removed (either before or within
1724 * this function).
1726 * For each basic map i, we check if it can be coalesced with respect
1727 * to any previously considered basic map j.
1728 * If i gets dropped (because it was a subset of some j), then
1729 * we can move on to the next basic map.
1730 * If j gets dropped, we need to continue checking against the other
1731 * previously considered basic maps.
1732 * If the two basic maps got fused, then we recheck the fused basic map
1733 * against the previously considered basic maps.
1735 static int coalesce(isl_ctx *ctx, int n, struct isl_coalesce_info *info)
1737 int i, j;
1739 for (i = n - 2; i >= 0; --i) {
1740 if (info[i].removed)
1741 continue;
1742 for (j = i + 1; j < n; ++j) {
1743 enum isl_change changed;
1745 if (info[j].removed)
1746 continue;
1747 if (info[i].removed)
1748 isl_die(ctx, isl_error_internal,
1749 "basic map unexpectedly removed",
1750 return -1);
1751 changed = coalesce_pair(i, j, info);
1752 switch (changed) {
1753 case isl_change_error:
1754 return -1;
1755 case isl_change_none:
1756 case isl_change_drop_second:
1757 continue;
1758 case isl_change_drop_first:
1759 j = n;
1760 break;
1761 case isl_change_fuse:
1762 j = i;
1763 break;
1768 return 0;
1771 /* Update the basic maps in "map" based on the information in "info".
1772 * In particular, remove the basic maps that have been marked removed and
1773 * update the others based on the information in the corresponding tableau.
1774 * Since we detected implicit equalities without calling
1775 * isl_basic_map_gauss, we need to do it now.
1777 static __isl_give isl_map *update_basic_maps(__isl_take isl_map *map,
1778 int n, struct isl_coalesce_info *info)
1780 int i;
1782 if (!map)
1783 return NULL;
1785 for (i = n - 1; i >= 0; --i) {
1786 if (info[i].removed) {
1787 isl_basic_map_free(map->p[i]);
1788 if (i != map->n - 1)
1789 map->p[i] = map->p[map->n - 1];
1790 map->n--;
1791 continue;
1794 info[i].bmap = isl_basic_map_update_from_tab(info[i].bmap,
1795 info[i].tab);
1796 info[i].bmap = isl_basic_map_gauss(info[i].bmap, NULL);
1797 info[i].bmap = isl_basic_map_finalize(info[i].bmap);
1798 if (!info[i].bmap)
1799 return isl_map_free(map);
1800 ISL_F_SET(info[i].bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1801 ISL_F_SET(info[i].bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1802 isl_basic_map_free(map->p[i]);
1803 map->p[i] = info[i].bmap;
1804 info[i].bmap = NULL;
1807 return map;
1810 /* For each pair of basic maps in the map, check if the union of the two
1811 * can be represented by a single basic map.
1812 * If so, replace the pair by the single basic map and start over.
1814 * Since we are constructing the tableaus of the basic maps anyway,
1815 * we exploit them to detect implicit equalities and redundant constraints.
1816 * This also helps the coalescing as it can ignore the redundant constraints.
1817 * In order to avoid confusion, we make all implicit equalities explicit
1818 * in the basic maps. We don't call isl_basic_map_gauss, though,
1819 * as that may affect the number of constraints.
1820 * This means that we have to call isl_basic_map_gauss at the end
1821 * of the computation (in update_basic_maps) to ensure that
1822 * the basic maps are not left in an unexpected state.
1824 struct isl_map *isl_map_coalesce(struct isl_map *map)
1826 int i;
1827 unsigned n;
1828 isl_ctx *ctx;
1829 struct isl_coalesce_info *info = NULL;
1831 map = isl_map_remove_empty_parts(map);
1832 if (!map)
1833 return NULL;
1835 if (map->n <= 1)
1836 return map;
1838 ctx = isl_map_get_ctx(map);
1839 map = isl_map_sort_divs(map);
1840 map = isl_map_cow(map);
1842 if (!map)
1843 return NULL;
1845 n = map->n;
1847 info = isl_calloc_array(map->ctx, struct isl_coalesce_info, n);
1848 if (!info)
1849 goto error;
1851 for (i = 0; i < map->n; ++i) {
1852 info[i].bmap = isl_basic_map_copy(map->p[i]);
1853 info[i].tab = isl_tab_from_basic_map(info[i].bmap, 0);
1854 if (!info[i].tab)
1855 goto error;
1856 if (!ISL_F_ISSET(info[i].bmap, ISL_BASIC_MAP_NO_IMPLICIT))
1857 if (isl_tab_detect_implicit_equalities(info[i].tab) < 0)
1858 goto error;
1859 info[i].bmap = isl_tab_make_equalities_explicit(info[i].tab,
1860 info[i].bmap);
1861 if (!info[i].bmap)
1862 goto error;
1863 if (!ISL_F_ISSET(info[i].bmap, ISL_BASIC_MAP_NO_REDUNDANT))
1864 if (isl_tab_detect_redundant(info[i].tab) < 0)
1865 goto error;
1867 for (i = map->n - 1; i >= 0; --i)
1868 if (info[i].tab->empty)
1869 drop(&info[i]);
1871 if (coalesce(ctx, n, info) < 0)
1872 goto error;
1874 map = update_basic_maps(map, n, info);
1876 clear_coalesce_info(n, info);
1878 return map;
1879 error:
1880 clear_coalesce_info(n, info);
1881 isl_map_free(map);
1882 return NULL;
1885 /* For each pair of basic sets in the set, check if the union of the two
1886 * can be represented by a single basic set.
1887 * If so, replace the pair by the single basic set and start over.
1889 struct isl_set *isl_set_coalesce(struct isl_set *set)
1891 return (struct isl_set *)isl_map_coalesce((struct isl_map *)set);