isl_coalesce.c: contains: pass isl_coalesce_info instead of basic map
[isl.git] / isl_coalesce.c
blobe6b1e307119a63bedf9b35f35dd0f86e4e5b7c18
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 info->bmap contains the basic map represented
395 * by the tableau "tab".
397 static int contains(struct isl_coalesce_info *info, int *ineq_i,
398 struct isl_tab *tab)
400 int k, l;
401 unsigned dim;
402 isl_basic_map *bmap = info->bmap;
404 dim = isl_basic_map_total_dim(bmap);
405 for (k = 0; k < bmap->n_eq; ++k) {
406 for (l = 0; l < 2; ++l) {
407 int stat;
408 isl_seq_neg(bmap->eq[k], bmap->eq[k], 1+dim);
409 stat = status_in(bmap->eq[k], tab);
410 if (stat != STATUS_VALID)
411 return 0;
415 for (k = 0; k < bmap->n_ineq; ++k) {
416 int stat;
417 if (ineq_i[k] == STATUS_REDUNDANT)
418 continue;
419 stat = status_in(bmap->ineq[k], tab);
420 if (stat != STATUS_VALID)
421 return 0;
423 return 1;
426 /* Basic map "i" has an inequality (say "k") that is adjacent
427 * to some inequality of basic map "j". All the other inequalities
428 * are valid for "j".
429 * Check if basic map "j" forms an extension of basic map "i".
431 * Note that this function is only called if some of the equalities or
432 * inequalities of basic map "j" do cut basic map "i". The function is
433 * correct even if there are no such cut constraints, but in that case
434 * the additional checks performed by this function are overkill.
436 * In particular, we replace constraint k, say f >= 0, by constraint
437 * f <= -1, add the inequalities of "j" that are valid for "i"
438 * and check if the result is a subset of basic map "j".
439 * If so, then we know that this result is exactly equal to basic map "j"
440 * since all its constraints are valid for basic map "j".
441 * By combining the valid constraints of "i" (all equalities and all
442 * inequalities except "k") and the valid constraints of "j" we therefore
443 * obtain a basic map that is equal to their union.
444 * In this case, there is no need to perform a rollback of the tableau
445 * since it is going to be destroyed in fuse().
448 * |\__ |\__
449 * | \__ | \__
450 * | \_ => | \__
451 * |_______| _ |_________\
454 * |\ |\
455 * | \ | \
456 * | \ | \
457 * | | | \
458 * | ||\ => | \
459 * | || \ | \
460 * | || | | |
461 * |__||_/ |_____/
463 static enum isl_change is_adj_ineq_extension(int i, int j,
464 struct isl_coalesce_info *info, int *eq_i, int *ineq_i,
465 int *eq_j, int *ineq_j)
467 int k;
468 struct isl_tab_undo *snap;
469 unsigned n_eq = info[i].bmap->n_eq;
470 unsigned total = isl_basic_map_total_dim(info[i].bmap);
471 int r;
473 if (isl_tab_extend_cons(info[i].tab, 1 + info[j].bmap->n_ineq) < 0)
474 return isl_change_error;
476 for (k = 0; k < info[i].bmap->n_ineq; ++k)
477 if (ineq_i[k] == STATUS_ADJ_INEQ)
478 break;
479 if (k >= info[i].bmap->n_ineq)
480 isl_die(isl_basic_map_get_ctx(info[i].bmap), isl_error_internal,
481 "ineq_i should have exactly one STATUS_ADJ_INEQ",
482 return isl_change_error);
484 snap = isl_tab_snap(info[i].tab);
486 if (isl_tab_unrestrict(info[i].tab, n_eq + k) < 0)
487 return isl_change_error;
489 isl_seq_neg(info[i].bmap->ineq[k], info[i].bmap->ineq[k], 1 + total);
490 isl_int_sub_ui(info[i].bmap->ineq[k][0], info[i].bmap->ineq[k][0], 1);
491 r = isl_tab_add_ineq(info[i].tab, info[i].bmap->ineq[k]);
492 isl_seq_neg(info[i].bmap->ineq[k], info[i].bmap->ineq[k], 1 + total);
493 isl_int_sub_ui(info[i].bmap->ineq[k][0], info[i].bmap->ineq[k][0], 1);
494 if (r < 0)
495 return isl_change_error;
497 for (k = 0; k < info[j].bmap->n_ineq; ++k) {
498 if (ineq_j[k] != STATUS_VALID)
499 continue;
500 if (isl_tab_add_ineq(info[i].tab, info[j].bmap->ineq[k]) < 0)
501 return isl_change_error;
504 if (contains(&info[j], ineq_j, info[i].tab))
505 return fuse(i, j, info, eq_i, ineq_i, eq_j, ineq_j, NULL, 0);
507 if (isl_tab_rollback(info[i].tab, snap) < 0)
508 return isl_change_error;
510 return isl_change_none;
514 /* Both basic maps have at least one inequality with and adjacent
515 * (but opposite) inequality in the other basic map.
516 * Check that there are no cut constraints and that there is only
517 * a single pair of adjacent inequalities.
518 * If so, we can replace the pair by a single basic map described
519 * by all but the pair of adjacent inequalities.
520 * Any additional points introduced lie strictly between the two
521 * adjacent hyperplanes and can therefore be integral.
523 * ____ _____
524 * / ||\ / \
525 * / || \ / \
526 * \ || \ => \ \
527 * \ || / \ /
528 * \___||_/ \_____/
530 * The test for a single pair of adjancent inequalities is important
531 * for avoiding the combination of two basic maps like the following
533 * /|
534 * / |
535 * /__|
536 * _____
537 * | |
538 * | |
539 * |___|
541 * If there are some cut constraints on one side, then we may
542 * still be able to fuse the two basic maps, but we need to perform
543 * some additional checks in is_adj_ineq_extension.
545 static enum isl_change check_adj_ineq(int i, int j,
546 struct isl_coalesce_info *info, int *eq_i, int *ineq_i,
547 int *eq_j, int *ineq_j)
549 int count_i, count_j;
550 int cut_i, cut_j;
552 count_i = count(ineq_i, info[i].bmap->n_ineq, STATUS_ADJ_INEQ);
553 count_j = count(ineq_j, info[j].bmap->n_ineq, STATUS_ADJ_INEQ);
555 if (count_i != 1 && count_j != 1)
556 return isl_change_none;
558 cut_i = any(eq_i, 2 * info[i].bmap->n_eq, STATUS_CUT) ||
559 any(ineq_i, info[i].bmap->n_ineq, STATUS_CUT);
560 cut_j = any(eq_j, 2 * info[j].bmap->n_eq, STATUS_CUT) ||
561 any(ineq_j, info[j].bmap->n_ineq, STATUS_CUT);
563 if (!cut_i && !cut_j && count_i == 1 && count_j == 1)
564 return fuse(i, j, info, NULL, ineq_i, NULL, ineq_j, NULL, 0);
566 if (count_i == 1 && !cut_i)
567 return is_adj_ineq_extension(i, j, info,
568 eq_i, ineq_i, eq_j, ineq_j);
570 if (count_j == 1 && !cut_j)
571 return is_adj_ineq_extension(j, i, info,
572 eq_j, ineq_j, eq_i, ineq_i);
574 return isl_change_none;
577 /* Basic map "i" has an inequality "k" that is adjacent to some equality
578 * of basic map "j". All the other inequalities are valid for "j".
579 * Check if basic map "j" forms an extension of basic map "i".
581 * In particular, we relax constraint "k", compute the corresponding
582 * facet and check whether it is included in the other basic map.
583 * If so, we know that relaxing the constraint extends the basic
584 * map with exactly the other basic map (we already know that this
585 * other basic map is included in the extension, because there
586 * were no "cut" inequalities in "i") and we can replace the
587 * two basic maps by this extension.
588 * Place this extension in the position that is the smallest of i and j.
589 * ____ _____
590 * / || / |
591 * / || / |
592 * \ || => \ |
593 * \ || \ |
594 * \___|| \____|
596 static enum isl_change is_adj_eq_extension(int i, int j, int k,
597 struct isl_coalesce_info *info, int *eq_i, int *ineq_i,
598 int *eq_j, int *ineq_j)
600 int change = isl_change_none;
601 int super;
602 struct isl_tab_undo *snap, *snap2;
603 unsigned n_eq = info[i].bmap->n_eq;
605 if (isl_tab_is_equality(info[i].tab, n_eq + k))
606 return isl_change_none;
608 snap = isl_tab_snap(info[i].tab);
609 if (isl_tab_relax(info[i].tab, n_eq + k) < 0)
610 return isl_change_error;
611 snap2 = isl_tab_snap(info[i].tab);
612 if (isl_tab_select_facet(info[i].tab, n_eq + k) < 0)
613 return isl_change_error;
614 super = contains(&info[j], ineq_j, info[i].tab);
615 if (super) {
616 if (isl_tab_rollback(info[i].tab, snap2) < 0)
617 return isl_change_error;
618 info[i].bmap = isl_basic_map_cow(info[i].bmap);
619 if (!info[i].bmap)
620 return isl_change_error;
621 isl_int_add_ui(info[i].bmap->ineq[k][0],
622 info[i].bmap->ineq[k][0], 1);
623 ISL_F_SET(info[i].bmap, ISL_BASIC_MAP_FINAL);
624 drop(&info[j]);
625 if (j < i)
626 exchange(&info[i], &info[j]);
627 change = isl_change_fuse;
628 } else
629 if (isl_tab_rollback(info[i].tab, snap) < 0)
630 return isl_change_error;
632 return change;
635 /* Data structure that keeps track of the wrapping constraints
636 * and of information to bound the coefficients of those constraints.
638 * bound is set if we want to apply a bound on the coefficients
639 * mat contains the wrapping constraints
640 * max is the bound on the coefficients (if bound is set)
642 struct isl_wraps {
643 int bound;
644 isl_mat *mat;
645 isl_int max;
648 /* Update wraps->max to be greater than or equal to the coefficients
649 * in the equalities and inequalities of info->bmap that can be removed
650 * if we end up applying wrapping.
652 static void wraps_update_max(struct isl_wraps *wraps,
653 struct isl_coalesce_info *info, int *eq, int *ineq)
655 int k;
656 isl_int max_k;
657 unsigned total = isl_basic_map_total_dim(info->bmap);
659 isl_int_init(max_k);
661 for (k = 0; k < info->bmap->n_eq; ++k) {
662 if (eq[2 * k] == STATUS_VALID &&
663 eq[2 * k + 1] == STATUS_VALID)
664 continue;
665 isl_seq_abs_max(info->bmap->eq[k] + 1, total, &max_k);
666 if (isl_int_abs_gt(max_k, wraps->max))
667 isl_int_set(wraps->max, max_k);
670 for (k = 0; k < info->bmap->n_ineq; ++k) {
671 if (ineq[k] == STATUS_VALID || ineq[k] == STATUS_REDUNDANT)
672 continue;
673 isl_seq_abs_max(info->bmap->ineq[k] + 1, total, &max_k);
674 if (isl_int_abs_gt(max_k, wraps->max))
675 isl_int_set(wraps->max, max_k);
678 isl_int_clear(max_k);
681 /* Initialize the isl_wraps data structure.
682 * If we want to bound the coefficients of the wrapping constraints,
683 * we set wraps->max to the largest coefficient
684 * in the equalities and inequalities that can be removed if we end up
685 * applying wrapping.
687 static void wraps_init(struct isl_wraps *wraps, __isl_take isl_mat *mat,
688 struct isl_coalesce_info *info, int i, int j,
689 int *eq_i, int *ineq_i, int *eq_j, int *ineq_j)
691 isl_ctx *ctx;
693 wraps->bound = 0;
694 wraps->mat = mat;
695 if (!mat)
696 return;
697 ctx = isl_mat_get_ctx(mat);
698 wraps->bound = isl_options_get_coalesce_bounded_wrapping(ctx);
699 if (!wraps->bound)
700 return;
701 isl_int_init(wraps->max);
702 isl_int_set_si(wraps->max, 0);
703 wraps_update_max(wraps, &info[i], eq_i, ineq_i);
704 wraps_update_max(wraps, &info[j], eq_j, ineq_j);
707 /* Free the contents of the isl_wraps data structure.
709 static void wraps_free(struct isl_wraps *wraps)
711 isl_mat_free(wraps->mat);
712 if (wraps->bound)
713 isl_int_clear(wraps->max);
716 /* Is the wrapping constraint in row "row" allowed?
718 * If wraps->bound is set, we check that none of the coefficients
719 * is greater than wraps->max.
721 static int allow_wrap(struct isl_wraps *wraps, int row)
723 int i;
725 if (!wraps->bound)
726 return 1;
728 for (i = 1; i < wraps->mat->n_col; ++i)
729 if (isl_int_abs_gt(wraps->mat->row[row][i], wraps->max))
730 return 0;
732 return 1;
735 /* For each non-redundant constraint in info->bmap (as determined by info->tab),
736 * wrap the constraint around "bound" such that it includes the whole
737 * set "set" and append the resulting constraint to "wraps".
738 * "wraps" is assumed to have been pre-allocated to the appropriate size.
739 * wraps->n_row is the number of actual wrapped constraints that have
740 * been added.
741 * If any of the wrapping problems results in a constraint that is
742 * identical to "bound", then this means that "set" is unbounded in such
743 * way that no wrapping is possible. If this happens then wraps->n_row
744 * is reset to zero.
745 * Similarly, if we want to bound the coefficients of the wrapping
746 * constraints and a newly added wrapping constraint does not
747 * satisfy the bound, then wraps->n_row is also reset to zero.
749 static int add_wraps(struct isl_wraps *wraps, struct isl_coalesce_info *info,
750 isl_int *bound, __isl_keep isl_set *set)
752 int l;
753 int w;
754 isl_basic_map *bmap = info->bmap;
755 unsigned total = isl_basic_map_total_dim(bmap);
757 w = wraps->mat->n_row;
759 for (l = 0; l < bmap->n_ineq; ++l) {
760 if (isl_seq_is_neg(bound, bmap->ineq[l], 1 + total))
761 continue;
762 if (isl_seq_eq(bound, bmap->ineq[l], 1 + total))
763 continue;
764 if (isl_tab_is_redundant(info->tab, bmap->n_eq + l))
765 continue;
767 isl_seq_cpy(wraps->mat->row[w], bound, 1 + total);
768 if (!isl_set_wrap_facet(set, wraps->mat->row[w], bmap->ineq[l]))
769 return -1;
770 if (isl_seq_eq(wraps->mat->row[w], bound, 1 + total))
771 goto unbounded;
772 if (!allow_wrap(wraps, w))
773 goto unbounded;
774 ++w;
776 for (l = 0; l < bmap->n_eq; ++l) {
777 if (isl_seq_is_neg(bound, bmap->eq[l], 1 + total))
778 continue;
779 if (isl_seq_eq(bound, bmap->eq[l], 1 + total))
780 continue;
782 isl_seq_cpy(wraps->mat->row[w], bound, 1 + total);
783 isl_seq_neg(wraps->mat->row[w + 1], bmap->eq[l], 1 + total);
784 if (!isl_set_wrap_facet(set, wraps->mat->row[w],
785 wraps->mat->row[w + 1]))
786 return -1;
787 if (isl_seq_eq(wraps->mat->row[w], bound, 1 + total))
788 goto unbounded;
789 if (!allow_wrap(wraps, w))
790 goto unbounded;
791 ++w;
793 isl_seq_cpy(wraps->mat->row[w], bound, 1 + total);
794 if (!isl_set_wrap_facet(set, wraps->mat->row[w], bmap->eq[l]))
795 return -1;
796 if (isl_seq_eq(wraps->mat->row[w], bound, 1 + total))
797 goto unbounded;
798 if (!allow_wrap(wraps, w))
799 goto unbounded;
800 ++w;
803 wraps->mat->n_row = w;
804 return 0;
805 unbounded:
806 wraps->mat->n_row = 0;
807 return 0;
810 /* Check if the constraints in "wraps" from "first" until the last
811 * are all valid for the basic set represented by "tab".
812 * If not, wraps->n_row is set to zero.
814 static int check_wraps(__isl_keep isl_mat *wraps, int first,
815 struct isl_tab *tab)
817 int i;
819 for (i = first; i < wraps->n_row; ++i) {
820 enum isl_ineq_type type;
821 type = isl_tab_ineq_type(tab, wraps->row[i]);
822 if (type == isl_ineq_error)
823 return -1;
824 if (type == isl_ineq_redundant)
825 continue;
826 wraps->n_row = 0;
827 return 0;
830 return 0;
833 /* Return a set that corresponds to the non-redundant constraints
834 * (as recorded in tab) of bmap.
836 * It's important to remove the redundant constraints as some
837 * of the other constraints may have been modified after the
838 * constraints were marked redundant.
839 * In particular, a constraint may have been relaxed.
840 * Redundant constraints are ignored when a constraint is relaxed
841 * and should therefore continue to be ignored ever after.
842 * Otherwise, the relaxation might be thwarted by some of
843 * these constraints.
845 * Update the underlying set to ensure that the dimension doesn't change.
846 * Otherwise the integer divisions could get dropped if the tab
847 * turns out to be empty.
849 static __isl_give isl_set *set_from_updated_bmap(__isl_keep isl_basic_map *bmap,
850 struct isl_tab *tab)
852 isl_basic_set *bset;
854 bmap = isl_basic_map_copy(bmap);
855 bset = isl_basic_map_underlying_set(bmap);
856 bset = isl_basic_set_cow(bset);
857 bset = isl_basic_set_update_from_tab(bset, tab);
858 return isl_set_from_basic_set(bset);
861 /* Given a basic set i with a constraint k that is adjacent to
862 * basic set j, check if we can wrap
863 * both the facet corresponding to k and basic map j
864 * around their ridges to include the other set.
865 * If so, replace the pair of basic sets by their union.
867 * All constraints of i (except k) are assumed to be valid for j.
868 * This means that there is no real need to wrap the ridges of
869 * the faces of basic map i around basic map j but since we do,
870 * we have to check that the resulting wrapping constraints are valid for i.
871 * ____ _____
872 * / | / \
873 * / || / |
874 * \ || => \ |
875 * \ || \ |
876 * \___|| \____|
879 static enum isl_change can_wrap_in_facet(int i, int j, int k,
880 struct isl_coalesce_info *info, int *eq_i, int *ineq_i,
881 int *eq_j, int *ineq_j)
883 enum isl_change change = isl_change_none;
884 struct isl_wraps wraps;
885 isl_ctx *ctx;
886 isl_mat *mat;
887 struct isl_set *set_i = NULL;
888 struct isl_set *set_j = NULL;
889 struct isl_vec *bound = NULL;
890 unsigned total = isl_basic_map_total_dim(info[i].bmap);
891 struct isl_tab_undo *snap;
892 int n;
894 set_i = set_from_updated_bmap(info[i].bmap, info[i].tab);
895 set_j = set_from_updated_bmap(info[j].bmap, info[j].tab);
896 ctx = isl_basic_map_get_ctx(info[i].bmap);
897 mat = isl_mat_alloc(ctx, 2 * (info[i].bmap->n_eq + info[j].bmap->n_eq) +
898 info[i].bmap->n_ineq + info[j].bmap->n_ineq,
899 1 + total);
900 wraps_init(&wraps, mat, info, i, j, eq_i, ineq_i, eq_j, ineq_j);
901 bound = isl_vec_alloc(ctx, 1 + total);
902 if (!set_i || !set_j || !wraps.mat || !bound)
903 goto error;
905 isl_seq_cpy(bound->el, info[i].bmap->ineq[k], 1 + total);
906 isl_int_add_ui(bound->el[0], bound->el[0], 1);
908 isl_seq_cpy(wraps.mat->row[0], bound->el, 1 + total);
909 wraps.mat->n_row = 1;
911 if (add_wraps(&wraps, &info[j], bound->el, set_i) < 0)
912 goto error;
913 if (!wraps.mat->n_row)
914 goto unbounded;
916 snap = isl_tab_snap(info[i].tab);
918 if (isl_tab_select_facet(info[i].tab, info[i].bmap->n_eq + k) < 0)
919 goto error;
920 if (isl_tab_detect_redundant(info[i].tab) < 0)
921 goto error;
923 isl_seq_neg(bound->el, info[i].bmap->ineq[k], 1 + total);
925 n = wraps.mat->n_row;
926 if (add_wraps(&wraps, &info[i], bound->el, set_j) < 0)
927 goto error;
929 if (isl_tab_rollback(info[i].tab, snap) < 0)
930 goto error;
931 if (check_wraps(wraps.mat, n, info[i].tab) < 0)
932 goto error;
933 if (!wraps.mat->n_row)
934 goto unbounded;
936 change = fuse(i, j, info, eq_i, ineq_i, eq_j, ineq_j, wraps.mat, 0);
938 unbounded:
939 wraps_free(&wraps);
941 isl_set_free(set_i);
942 isl_set_free(set_j);
944 isl_vec_free(bound);
946 return change;
947 error:
948 wraps_free(&wraps);
949 isl_vec_free(bound);
950 isl_set_free(set_i);
951 isl_set_free(set_j);
952 return isl_change_error;
955 /* Given a pair of basic maps i and j such that j sticks out
956 * of i at n cut constraints, each time by at most one,
957 * try to compute wrapping constraints and replace the two
958 * basic maps by a single basic map.
959 * The other constraints of i are assumed to be valid for j.
961 * For each cut constraint t(x) >= 0 of i, we add the relaxed version
962 * t(x) + 1 >= 0, along with wrapping constraints for all constraints
963 * of basic map j that bound the part of basic map j that sticks out
964 * of the cut constraint.
965 * In particular, we first intersect basic map j with t(x) + 1 = 0.
966 * If the result is empty, then t(x) >= 0 was actually a valid constraint
967 * (with respect to the integer points), so we add t(x) >= 0 instead.
968 * Otherwise, we wrap the constraints of basic map j that are not
969 * redundant in this intersection over the union of the two basic maps.
971 * If any wrapping fails, i.e., if we cannot wrap to touch
972 * the union, then we give up.
973 * Otherwise, the pair of basic maps is replaced by their union.
975 static enum isl_change wrap_in_facets(int i, int j, int *cuts, int n,
976 struct isl_coalesce_info *info,
977 int *eq_i, int *ineq_i, int *eq_j, int *ineq_j)
979 enum isl_change change = isl_change_none;
980 struct isl_wraps wraps;
981 isl_ctx *ctx;
982 isl_mat *mat;
983 isl_set *set = NULL;
984 unsigned total = isl_basic_map_total_dim(info[i].bmap);
985 int max_wrap;
986 int k, w;
987 struct isl_tab_undo *snap;
989 if (isl_tab_extend_cons(info[j].tab, 1) < 0)
990 goto error;
992 max_wrap = 1 + 2 * info[j].bmap->n_eq + info[j].bmap->n_ineq;
993 max_wrap *= n;
995 set = isl_set_union(set_from_updated_bmap(info[i].bmap, info[i].tab),
996 set_from_updated_bmap(info[j].bmap, info[j].tab));
997 ctx = isl_basic_map_get_ctx(info[i].bmap);
998 mat = isl_mat_alloc(ctx, max_wrap, 1 + total);
999 wraps_init(&wraps, mat, info, i, j, eq_i, ineq_i, eq_j, ineq_j);
1000 if (!set || !wraps.mat)
1001 goto error;
1003 snap = isl_tab_snap(info[j].tab);
1005 wraps.mat->n_row = 0;
1007 for (k = 0; k < n; ++k) {
1008 w = wraps.mat->n_row++;
1009 isl_seq_cpy(wraps.mat->row[w],
1010 info[i].bmap->ineq[cuts[k]], 1 + total);
1011 isl_int_add_ui(wraps.mat->row[w][0], wraps.mat->row[w][0], 1);
1012 if (isl_tab_add_eq(info[j].tab, wraps.mat->row[w]) < 0)
1013 goto error;
1014 if (isl_tab_detect_redundant(info[j].tab) < 0)
1015 goto error;
1017 if (info[j].tab->empty)
1018 isl_int_sub_ui(wraps.mat->row[w][0],
1019 wraps.mat->row[w][0], 1);
1020 else if (add_wraps(&wraps, &info[j],
1021 wraps.mat->row[w], set) < 0)
1022 goto error;
1024 if (isl_tab_rollback(info[j].tab, snap) < 0)
1025 goto error;
1027 if (!wraps.mat->n_row)
1028 break;
1031 if (k == n)
1032 change = fuse(i, j, info,
1033 eq_i, ineq_i, eq_j, ineq_j, wraps.mat, 0);
1035 wraps_free(&wraps);
1036 isl_set_free(set);
1038 return change;
1039 error:
1040 wraps_free(&wraps);
1041 isl_set_free(set);
1042 return isl_change_error;
1045 /* Given two basic sets i and j such that i has no cut equalities,
1046 * check if relaxing all the cut inequalities of i by one turns
1047 * them into valid constraint for j and check if we can wrap in
1048 * the bits that are sticking out.
1049 * If so, replace the pair by their union.
1051 * We first check if all relaxed cut inequalities of i are valid for j
1052 * and then try to wrap in the intersections of the relaxed cut inequalities
1053 * with j.
1055 * During this wrapping, we consider the points of j that lie at a distance
1056 * of exactly 1 from i. In particular, we ignore the points that lie in
1057 * between this lower-dimensional space and the basic map i.
1058 * We can therefore only apply this to integer maps.
1059 * ____ _____
1060 * / ___|_ / \
1061 * / | | / |
1062 * \ | | => \ |
1063 * \|____| \ |
1064 * \___| \____/
1066 * _____ ______
1067 * | ____|_ | \
1068 * | | | | |
1069 * | | | => | |
1070 * |_| | | |
1071 * |_____| \______|
1073 * _______
1074 * | |
1075 * | |\ |
1076 * | | \ |
1077 * | | \ |
1078 * | | \|
1079 * | | \
1080 * | |_____\
1081 * | |
1082 * |_______|
1084 * Wrapping can fail if the result of wrapping one of the facets
1085 * around its edges does not produce any new facet constraint.
1086 * In particular, this happens when we try to wrap in unbounded sets.
1088 * _______________________________________________________________________
1090 * | ___
1091 * | | |
1092 * |_| |_________________________________________________________________
1093 * |___|
1095 * The following is not an acceptable result of coalescing the above two
1096 * sets as it includes extra integer points.
1097 * _______________________________________________________________________
1099 * |
1100 * |
1102 * \______________________________________________________________________
1104 static enum isl_change can_wrap_in_set(int i, int j,
1105 struct isl_coalesce_info *info, int *eq_i, int *ineq_i,
1106 int *eq_j, int *ineq_j)
1108 enum isl_change change = isl_change_none;
1109 int k, m;
1110 int n;
1111 int *cuts = NULL;
1112 isl_ctx *ctx;
1114 if (ISL_F_ISSET(info[i].bmap, ISL_BASIC_MAP_RATIONAL) ||
1115 ISL_F_ISSET(info[j].bmap, ISL_BASIC_MAP_RATIONAL))
1116 return isl_change_none;
1118 n = count(ineq_i, info[i].bmap->n_ineq, STATUS_CUT);
1119 if (n == 0)
1120 return isl_change_none;
1122 ctx = isl_basic_map_get_ctx(info[i].bmap);
1123 cuts = isl_alloc_array(ctx, int, n);
1124 if (!cuts)
1125 return isl_change_error;
1127 for (k = 0, m = 0; m < n; ++k) {
1128 enum isl_ineq_type type;
1130 if (ineq_i[k] != STATUS_CUT)
1131 continue;
1133 isl_int_add_ui(info[i].bmap->ineq[k][0],
1134 info[i].bmap->ineq[k][0], 1);
1135 type = isl_tab_ineq_type(info[j].tab, info[i].bmap->ineq[k]);
1136 isl_int_sub_ui(info[i].bmap->ineq[k][0],
1137 info[i].bmap->ineq[k][0], 1);
1138 if (type == isl_ineq_error)
1139 goto error;
1140 if (type != isl_ineq_redundant)
1141 break;
1142 cuts[m] = k;
1143 ++m;
1146 if (m == n)
1147 change = wrap_in_facets(i, j, cuts, n, info,
1148 eq_i, ineq_i, eq_j, ineq_j);
1150 free(cuts);
1152 return change;
1153 error:
1154 free(cuts);
1155 return isl_change_error;
1158 /* Check if either i or j has only cut inequalities that can
1159 * be used to wrap in (a facet of) the other basic set.
1160 * if so, replace the pair by their union.
1162 static enum isl_change check_wrap(int i, int j, struct isl_coalesce_info *info,
1163 int *eq_i, int *ineq_i, int *eq_j, int *ineq_j)
1165 enum isl_change change = isl_change_none;
1167 if (!any(eq_i, 2 * info[i].bmap->n_eq, STATUS_CUT))
1168 change = can_wrap_in_set(i, j, info,
1169 eq_i, ineq_i, eq_j, ineq_j);
1170 if (change != isl_change_none)
1171 return change;
1173 if (!any(eq_j, 2 * info[j].bmap->n_eq, STATUS_CUT))
1174 change = can_wrap_in_set(j, i, info,
1175 eq_j, ineq_j, eq_i, ineq_i);
1176 return change;
1179 /* At least one of the basic maps has an equality that is adjacent
1180 * to inequality. Make sure that only one of the basic maps has
1181 * such an equality and that the other basic map has exactly one
1182 * inequality adjacent to an equality.
1183 * We call the basic map that has the inequality "i" and the basic
1184 * map that has the equality "j".
1185 * If "i" has any "cut" (in)equality, then relaxing the inequality
1186 * by one would not result in a basic map that contains the other
1187 * basic map.
1189 static enum isl_change check_adj_eq(int i, int j,
1190 struct isl_coalesce_info *info, int *eq_i, int *ineq_i,
1191 int *eq_j, int *ineq_j)
1193 enum isl_change change = isl_change_none;
1194 int k;
1196 if (any(eq_i, 2 * info[i].bmap->n_eq, STATUS_ADJ_INEQ) &&
1197 any(eq_j, 2 * info[j].bmap->n_eq, STATUS_ADJ_INEQ))
1198 /* ADJ EQ TOO MANY */
1199 return isl_change_none;
1201 if (any(eq_i, 2 * info[i].bmap->n_eq, STATUS_ADJ_INEQ))
1202 return check_adj_eq(j, i, info, eq_j, ineq_j, eq_i, ineq_i);
1204 /* j has an equality adjacent to an inequality in i */
1206 if (any(eq_i, 2 * info[i].bmap->n_eq, STATUS_CUT))
1207 return isl_change_none;
1208 if (any(ineq_i, info[i].bmap->n_ineq, STATUS_CUT))
1209 /* ADJ EQ CUT */
1210 return isl_change_none;
1211 if (count(ineq_i, info[i].bmap->n_ineq, STATUS_ADJ_EQ) != 1 ||
1212 any(ineq_j, info[j].bmap->n_ineq, STATUS_ADJ_EQ) ||
1213 any(ineq_i, info[i].bmap->n_ineq, STATUS_ADJ_INEQ) ||
1214 any(ineq_j, info[j].bmap->n_ineq, STATUS_ADJ_INEQ))
1215 /* ADJ EQ TOO MANY */
1216 return isl_change_none;
1218 for (k = 0; k < info[i].bmap->n_ineq; ++k)
1219 if (ineq_i[k] == STATUS_ADJ_EQ)
1220 break;
1222 change = is_adj_eq_extension(i, j, k, info,
1223 eq_i, ineq_i, eq_j, ineq_j);
1224 if (change != isl_change_none)
1225 return change;
1227 if (count(eq_j, 2 * info[j].bmap->n_eq, STATUS_ADJ_INEQ) != 1)
1228 return isl_change_none;
1230 change = can_wrap_in_facet(i, j, k, info, eq_i, ineq_i, eq_j, ineq_j);
1232 return change;
1235 /* The two basic maps lie on adjacent hyperplanes. In particular,
1236 * basic map "i" has an equality that lies parallel to basic map "j".
1237 * Check if we can wrap the facets around the parallel hyperplanes
1238 * to include the other set.
1240 * We perform basically the same operations as can_wrap_in_facet,
1241 * except that we don't need to select a facet of one of the sets.
1243 * \\ \\
1244 * \\ => \\
1245 * \ \|
1247 * If there is more than one equality of "i" adjacent to an equality of "j",
1248 * then the result will satisfy one or more equalities that are a linear
1249 * combination of these equalities. These will be encoded as pairs
1250 * of inequalities in the wrapping constraints and need to be made
1251 * explicit.
1253 static enum isl_change check_eq_adj_eq(int i, int j,
1254 struct isl_coalesce_info *info, int *eq_i, int *ineq_i,
1255 int *eq_j, int *ineq_j)
1257 int k;
1258 enum isl_change change = isl_change_none;
1259 int detect_equalities = 0;
1260 struct isl_wraps wraps;
1261 isl_ctx *ctx;
1262 isl_mat *mat;
1263 struct isl_set *set_i = NULL;
1264 struct isl_set *set_j = NULL;
1265 struct isl_vec *bound = NULL;
1266 unsigned total = isl_basic_map_total_dim(info[i].bmap);
1268 if (count(eq_i, 2 * info[i].bmap->n_eq, STATUS_ADJ_EQ) != 1)
1269 detect_equalities = 1;
1271 for (k = 0; k < 2 * info[i].bmap->n_eq ; ++k)
1272 if (eq_i[k] == STATUS_ADJ_EQ)
1273 break;
1275 set_i = set_from_updated_bmap(info[i].bmap, info[i].tab);
1276 set_j = set_from_updated_bmap(info[j].bmap, info[j].tab);
1277 ctx = isl_basic_map_get_ctx(info[i].bmap);
1278 mat = isl_mat_alloc(ctx, 2 * (info[i].bmap->n_eq + info[j].bmap->n_eq) +
1279 info[i].bmap->n_ineq + info[j].bmap->n_ineq,
1280 1 + total);
1281 wraps_init(&wraps, mat, info, i, j, eq_i, ineq_i, eq_j, ineq_j);
1282 bound = isl_vec_alloc(ctx, 1 + total);
1283 if (!set_i || !set_j || !wraps.mat || !bound)
1284 goto error;
1286 if (k % 2 == 0)
1287 isl_seq_neg(bound->el, info[i].bmap->eq[k / 2], 1 + total);
1288 else
1289 isl_seq_cpy(bound->el, info[i].bmap->eq[k / 2], 1 + total);
1290 isl_int_add_ui(bound->el[0], bound->el[0], 1);
1292 isl_seq_cpy(wraps.mat->row[0], bound->el, 1 + total);
1293 wraps.mat->n_row = 1;
1295 if (add_wraps(&wraps, &info[j], bound->el, set_i) < 0)
1296 goto error;
1297 if (!wraps.mat->n_row)
1298 goto unbounded;
1300 isl_int_sub_ui(bound->el[0], bound->el[0], 1);
1301 isl_seq_neg(bound->el, bound->el, 1 + total);
1303 isl_seq_cpy(wraps.mat->row[wraps.mat->n_row], bound->el, 1 + total);
1304 wraps.mat->n_row++;
1306 if (add_wraps(&wraps, &info[i], bound->el, set_j) < 0)
1307 goto error;
1308 if (!wraps.mat->n_row)
1309 goto unbounded;
1311 change = fuse(i, j, info, eq_i, ineq_i, eq_j, ineq_j, wraps.mat,
1312 detect_equalities);
1314 if (0) {
1315 error: change = isl_change_error;
1317 unbounded:
1319 wraps_free(&wraps);
1320 isl_set_free(set_i);
1321 isl_set_free(set_j);
1322 isl_vec_free(bound);
1324 return change;
1327 /* Check if the union of the given pair of basic maps
1328 * can be represented by a single basic map.
1329 * If so, replace the pair by the single basic map and return
1330 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
1331 * Otherwise, return isl_change_none.
1332 * The two basic maps are assumed to live in the same local space.
1334 * We first check the effect of each constraint of one basic map
1335 * on the other basic map.
1336 * The constraint may be
1337 * redundant the constraint is redundant in its own
1338 * basic map and should be ignore and removed
1339 * in the end
1340 * valid all (integer) points of the other basic map
1341 * satisfy the constraint
1342 * separate no (integer) point of the other basic map
1343 * satisfies the constraint
1344 * cut some but not all points of the other basic map
1345 * satisfy the constraint
1346 * adj_eq the given constraint is adjacent (on the outside)
1347 * to an equality of the other basic map
1348 * adj_ineq the given constraint is adjacent (on the outside)
1349 * to an inequality of the other basic map
1351 * We consider seven cases in which we can replace the pair by a single
1352 * basic map. We ignore all "redundant" constraints.
1354 * 1. all constraints of one basic map are valid
1355 * => the other basic map is a subset and can be removed
1357 * 2. all constraints of both basic maps are either "valid" or "cut"
1358 * and the facets corresponding to the "cut" constraints
1359 * of one of the basic maps lies entirely inside the other basic map
1360 * => the pair can be replaced by a basic map consisting
1361 * of the valid constraints in both basic maps
1363 * 3. there is a single pair of adjacent inequalities
1364 * (all other constraints are "valid")
1365 * => the pair can be replaced by a basic map consisting
1366 * of the valid constraints in both basic maps
1368 * 4. one basic map has a single adjacent inequality, while the other
1369 * constraints are "valid". The other basic map has some
1370 * "cut" constraints, but replacing the adjacent inequality by
1371 * its opposite and adding the valid constraints of the other
1372 * basic map results in a subset of the other basic map
1373 * => the pair can be replaced by a basic map consisting
1374 * of the valid constraints in both basic maps
1376 * 5. there is a single adjacent pair of an inequality and an equality,
1377 * the other constraints of the basic map containing the inequality are
1378 * "valid". Moreover, if the inequality the basic map is relaxed
1379 * and then turned into an equality, then resulting facet lies
1380 * entirely inside the other basic map
1381 * => the pair can be replaced by the basic map containing
1382 * the inequality, with the inequality relaxed.
1384 * 6. there is a single adjacent pair of an inequality and an equality,
1385 * the other constraints of the basic map containing the inequality are
1386 * "valid". Moreover, the facets corresponding to both
1387 * the inequality and the equality can be wrapped around their
1388 * ridges to include the other basic map
1389 * => the pair can be replaced by a basic map consisting
1390 * of the valid constraints in both basic maps together
1391 * with all wrapping constraints
1393 * 7. one of the basic maps extends beyond the other by at most one.
1394 * Moreover, the facets corresponding to the cut constraints and
1395 * the pieces of the other basic map at offset one from these cut
1396 * constraints can be wrapped around their ridges to include
1397 * the union of the two basic maps
1398 * => the pair can be replaced by a basic map consisting
1399 * of the valid constraints in both basic maps together
1400 * with all wrapping constraints
1402 * 8. the two basic maps live in adjacent hyperplanes. In principle
1403 * such sets can always be combined through wrapping, but we impose
1404 * that there is only one such pair, to avoid overeager coalescing.
1406 * Throughout the computation, we maintain a collection of tableaus
1407 * corresponding to the basic maps. When the basic maps are dropped
1408 * or combined, the tableaus are modified accordingly.
1410 static enum isl_change coalesce_local_pair(int i, int j,
1411 struct isl_coalesce_info *info)
1413 enum isl_change change = isl_change_none;
1414 int *eq_i = NULL;
1415 int *eq_j = NULL;
1416 int *ineq_i = NULL;
1417 int *ineq_j = NULL;
1419 eq_i = eq_status_in(info[i].bmap, info[j].tab);
1420 if (info[i].bmap->n_eq && !eq_i)
1421 goto error;
1422 if (any(eq_i, 2 * info[i].bmap->n_eq, STATUS_ERROR))
1423 goto error;
1424 if (any(eq_i, 2 * info[i].bmap->n_eq, STATUS_SEPARATE))
1425 goto done;
1427 eq_j = eq_status_in(info[j].bmap, info[i].tab);
1428 if (info[j].bmap->n_eq && !eq_j)
1429 goto error;
1430 if (any(eq_j, 2 * info[j].bmap->n_eq, STATUS_ERROR))
1431 goto error;
1432 if (any(eq_j, 2 * info[j].bmap->n_eq, STATUS_SEPARATE))
1433 goto done;
1435 ineq_i = ineq_status_in(info[i].bmap, info[i].tab, info[j].tab);
1436 if (info[i].bmap->n_ineq && !ineq_i)
1437 goto error;
1438 if (any(ineq_i, info[i].bmap->n_ineq, STATUS_ERROR))
1439 goto error;
1440 if (any(ineq_i, info[i].bmap->n_ineq, STATUS_SEPARATE))
1441 goto done;
1443 ineq_j = ineq_status_in(info[j].bmap, info[j].tab, info[i].tab);
1444 if (info[j].bmap->n_ineq && !ineq_j)
1445 goto error;
1446 if (any(ineq_j, info[j].bmap->n_ineq, STATUS_ERROR))
1447 goto error;
1448 if (any(ineq_j, info[j].bmap->n_ineq, STATUS_SEPARATE))
1449 goto done;
1451 if (all(eq_i, 2 * info[i].bmap->n_eq, STATUS_VALID) &&
1452 all(ineq_i, info[i].bmap->n_ineq, STATUS_VALID)) {
1453 drop(&info[j]);
1454 change = isl_change_drop_second;
1455 } else if (all(eq_j, 2 * info[j].bmap->n_eq, STATUS_VALID) &&
1456 all(ineq_j, info[j].bmap->n_ineq, STATUS_VALID)) {
1457 drop(&info[i]);
1458 change = isl_change_drop_first;
1459 } else if (any(eq_i, 2 * info[i].bmap->n_eq, STATUS_ADJ_EQ)) {
1460 change = check_eq_adj_eq(i, j, info,
1461 eq_i, ineq_i, eq_j, ineq_j);
1462 } else if (any(eq_j, 2 * info[j].bmap->n_eq, STATUS_ADJ_EQ)) {
1463 change = check_eq_adj_eq(j, i, info,
1464 eq_j, ineq_j, eq_i, ineq_i);
1465 } else if (any(eq_i, 2 * info[i].bmap->n_eq, STATUS_ADJ_INEQ) ||
1466 any(eq_j, 2 * info[j].bmap->n_eq, STATUS_ADJ_INEQ)) {
1467 change = check_adj_eq(i, j, info,
1468 eq_i, ineq_i, eq_j, ineq_j);
1469 } else if (any(ineq_i, info[i].bmap->n_ineq, STATUS_ADJ_EQ) ||
1470 any(ineq_j, info[j].bmap->n_ineq, STATUS_ADJ_EQ)) {
1471 /* Can't happen */
1472 /* BAD ADJ INEQ */
1473 } else if (any(ineq_i, info[i].bmap->n_ineq, STATUS_ADJ_INEQ) ||
1474 any(ineq_j, info[j].bmap->n_ineq, STATUS_ADJ_INEQ)) {
1475 change = check_adj_ineq(i, j, info,
1476 eq_i, ineq_i, eq_j, ineq_j);
1477 } else {
1478 if (!any(eq_i, 2 * info[i].bmap->n_eq, STATUS_CUT) &&
1479 !any(eq_j, 2 * info[j].bmap->n_eq, STATUS_CUT))
1480 change = check_facets(i, j, info, ineq_i, ineq_j);
1481 if (change == isl_change_none)
1482 change = check_wrap(i, j, info,
1483 eq_i, ineq_i, eq_j, ineq_j);
1486 done:
1487 free(eq_i);
1488 free(eq_j);
1489 free(ineq_i);
1490 free(ineq_j);
1491 return change;
1492 error:
1493 free(eq_i);
1494 free(eq_j);
1495 free(ineq_i);
1496 free(ineq_j);
1497 return isl_change_error;
1500 /* Do the two basic maps live in the same local space, i.e.,
1501 * do they have the same (known) divs?
1502 * If either basic map has any unknown divs, then we can only assume
1503 * that they do not live in the same local space.
1505 static int same_divs(__isl_keep isl_basic_map *bmap1,
1506 __isl_keep isl_basic_map *bmap2)
1508 int i;
1509 int known;
1510 int total;
1512 if (!bmap1 || !bmap2)
1513 return -1;
1514 if (bmap1->n_div != bmap2->n_div)
1515 return 0;
1517 if (bmap1->n_div == 0)
1518 return 1;
1520 known = isl_basic_map_divs_known(bmap1);
1521 if (known < 0 || !known)
1522 return known;
1523 known = isl_basic_map_divs_known(bmap2);
1524 if (known < 0 || !known)
1525 return known;
1527 total = isl_basic_map_total_dim(bmap1);
1528 for (i = 0; i < bmap1->n_div; ++i)
1529 if (!isl_seq_eq(bmap1->div[i], bmap2->div[i], 2 + total))
1530 return 0;
1532 return 1;
1535 /* Does "bmap" contain the basic map represented by the tableau "tab"
1536 * after expanding the divs of "bmap" to match those of "tab"?
1537 * The expansion is performed using the divs "div" and expansion "exp"
1538 * computed by the caller.
1539 * Then we check if all constraints of the expanded "bmap" are valid for "tab".
1541 static int contains_with_expanded_divs(__isl_keep isl_basic_map *bmap,
1542 struct isl_tab *tab, __isl_keep isl_mat *div, int *exp)
1544 int superset = 0;
1545 int *eq_i = NULL;
1546 int *ineq_i = NULL;
1548 bmap = isl_basic_map_copy(bmap);
1549 bmap = isl_basic_set_expand_divs(bmap, isl_mat_copy(div), exp);
1551 if (!bmap)
1552 goto error;
1554 eq_i = eq_status_in(bmap, tab);
1555 if (bmap->n_eq && !eq_i)
1556 goto error;
1557 if (any(eq_i, 2 * bmap->n_eq, STATUS_ERROR))
1558 goto error;
1559 if (any(eq_i, 2 * bmap->n_eq, STATUS_SEPARATE))
1560 goto done;
1562 ineq_i = ineq_status_in(bmap, NULL, tab);
1563 if (bmap->n_ineq && !ineq_i)
1564 goto error;
1565 if (any(ineq_i, bmap->n_ineq, STATUS_ERROR))
1566 goto error;
1567 if (any(ineq_i, bmap->n_ineq, STATUS_SEPARATE))
1568 goto done;
1570 if (all(eq_i, 2 * bmap->n_eq, STATUS_VALID) &&
1571 all(ineq_i, bmap->n_ineq, STATUS_VALID))
1572 superset = 1;
1574 done:
1575 isl_basic_map_free(bmap);
1576 free(eq_i);
1577 free(ineq_i);
1578 return superset;
1579 error:
1580 isl_basic_map_free(bmap);
1581 free(eq_i);
1582 free(ineq_i);
1583 return -1;
1586 /* Does "bmap_i" contain the basic map represented by "info_j"
1587 * after aligning the divs of "bmap_i" to those of "info_j".
1588 * Note that this can only succeed if the number of divs of "bmap_i"
1589 * is smaller than (or equal to) the number of divs of "info_j".
1591 * We first check if the divs of "bmap_i" are all known and form a subset
1592 * of those of "bmap_j". If so, we pass control over to
1593 * contains_with_expanded_divs.
1595 static int contains_after_aligning_divs(__isl_keep isl_basic_map *bmap_i,
1596 struct isl_coalesce_info *info_j)
1598 int known;
1599 isl_mat *div_i, *div_j, *div;
1600 int *exp1 = NULL;
1601 int *exp2 = NULL;
1602 isl_ctx *ctx;
1603 int subset;
1605 known = isl_basic_map_divs_known(bmap_i);
1606 if (known < 0 || !known)
1607 return known;
1609 ctx = isl_basic_map_get_ctx(bmap_i);
1611 div_i = isl_basic_map_get_divs(bmap_i);
1612 div_j = isl_basic_map_get_divs(info_j->bmap);
1614 if (!div_i || !div_j)
1615 goto error;
1617 exp1 = isl_alloc_array(ctx, int, div_i->n_row);
1618 exp2 = isl_alloc_array(ctx, int, div_j->n_row);
1619 if ((div_i->n_row && !exp1) || (div_j->n_row && !exp2))
1620 goto error;
1622 div = isl_merge_divs(div_i, div_j, exp1, exp2);
1623 if (!div)
1624 goto error;
1626 if (div->n_row == div_j->n_row)
1627 subset = contains_with_expanded_divs(bmap_i,
1628 info_j->tab, div, exp1);
1629 else
1630 subset = 0;
1632 isl_mat_free(div);
1634 isl_mat_free(div_i);
1635 isl_mat_free(div_j);
1637 free(exp2);
1638 free(exp1);
1640 return subset;
1641 error:
1642 isl_mat_free(div_i);
1643 isl_mat_free(div_j);
1644 free(exp1);
1645 free(exp2);
1646 return -1;
1649 /* Check if the basic map "j" is a subset of basic map "i",
1650 * if "i" has fewer divs that "j".
1651 * If so, remove basic map "j".
1653 * If the two basic maps have the same number of divs, then
1654 * they must necessarily be different. Otherwise, we would have
1655 * called coalesce_local_pair. We therefore don't try anything
1656 * in this case.
1658 static int coalesced_subset(int i, int j, struct isl_coalesce_info *info)
1660 int superset;
1662 if (info[i].bmap->n_div >= info[j].bmap->n_div)
1663 return 0;
1665 superset = contains_after_aligning_divs(info[i].bmap, &info[j]);
1666 if (superset < 0)
1667 return -1;
1668 if (superset)
1669 drop(&info[j]);
1671 return superset;
1674 /* Check if one of the basic maps is a subset of the other and, if so,
1675 * drop the subset.
1676 * Note that we only perform any test if the number of divs is different
1677 * in the two basic maps. In case the number of divs is the same,
1678 * we have already established that the divs are different
1679 * in the two basic maps.
1680 * In particular, if the number of divs of basic map i is smaller than
1681 * the number of divs of basic map j, then we check if j is a subset of i
1682 * and vice versa.
1684 static enum isl_change check_coalesce_subset(int i, int j,
1685 struct isl_coalesce_info *info)
1687 int changed;
1689 changed = coalesced_subset(i, j, info);
1690 if (changed < 0 || changed)
1691 return changed < 0 ? isl_change_error : isl_change_drop_second;
1693 changed = coalesced_subset(j, i, info);
1694 if (changed < 0 || changed)
1695 return changed < 0 ? isl_change_error : isl_change_drop_first;
1697 return isl_change_none;
1700 /* Check if the union of the given pair of basic maps
1701 * can be represented by a single basic map.
1702 * If so, replace the pair by the single basic map and return
1703 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
1704 * Otherwise, return isl_change_none.
1706 * We first check if the two basic maps live in the same local space.
1707 * If so, we do the complete check. Otherwise, we check if one is
1708 * an obvious subset of the other.
1710 static enum isl_change coalesce_pair(int i, int j,
1711 struct isl_coalesce_info *info)
1713 int same;
1715 same = same_divs(info[i].bmap, info[j].bmap);
1716 if (same < 0)
1717 return isl_change_error;
1718 if (same)
1719 return coalesce_local_pair(i, j, info);
1721 return check_coalesce_subset(i, j, info);
1724 /* Pairwise coalesce the basic maps described by the "n" elements of "info",
1725 * skipping basic maps that have been removed (either before or within
1726 * this function).
1728 * For each basic map i, we check if it can be coalesced with respect
1729 * to any previously considered basic map j.
1730 * If i gets dropped (because it was a subset of some j), then
1731 * we can move on to the next basic map.
1732 * If j gets dropped, we need to continue checking against the other
1733 * previously considered basic maps.
1734 * If the two basic maps got fused, then we recheck the fused basic map
1735 * against the previously considered basic maps.
1737 static int coalesce(isl_ctx *ctx, int n, struct isl_coalesce_info *info)
1739 int i, j;
1741 for (i = n - 2; i >= 0; --i) {
1742 if (info[i].removed)
1743 continue;
1744 for (j = i + 1; j < n; ++j) {
1745 enum isl_change changed;
1747 if (info[j].removed)
1748 continue;
1749 if (info[i].removed)
1750 isl_die(ctx, isl_error_internal,
1751 "basic map unexpectedly removed",
1752 return -1);
1753 changed = coalesce_pair(i, j, info);
1754 switch (changed) {
1755 case isl_change_error:
1756 return -1;
1757 case isl_change_none:
1758 case isl_change_drop_second:
1759 continue;
1760 case isl_change_drop_first:
1761 j = n;
1762 break;
1763 case isl_change_fuse:
1764 j = i;
1765 break;
1770 return 0;
1773 /* Update the basic maps in "map" based on the information in "info".
1774 * In particular, remove the basic maps that have been marked removed and
1775 * update the others based on the information in the corresponding tableau.
1776 * Since we detected implicit equalities without calling
1777 * isl_basic_map_gauss, we need to do it now.
1779 static __isl_give isl_map *update_basic_maps(__isl_take isl_map *map,
1780 int n, struct isl_coalesce_info *info)
1782 int i;
1784 if (!map)
1785 return NULL;
1787 for (i = n - 1; i >= 0; --i) {
1788 if (info[i].removed) {
1789 isl_basic_map_free(map->p[i]);
1790 if (i != map->n - 1)
1791 map->p[i] = map->p[map->n - 1];
1792 map->n--;
1793 continue;
1796 info[i].bmap = isl_basic_map_update_from_tab(info[i].bmap,
1797 info[i].tab);
1798 info[i].bmap = isl_basic_map_gauss(info[i].bmap, NULL);
1799 info[i].bmap = isl_basic_map_finalize(info[i].bmap);
1800 if (!info[i].bmap)
1801 return isl_map_free(map);
1802 ISL_F_SET(info[i].bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1803 ISL_F_SET(info[i].bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1804 isl_basic_map_free(map->p[i]);
1805 map->p[i] = info[i].bmap;
1806 info[i].bmap = NULL;
1809 return map;
1812 /* For each pair of basic maps in the map, check if the union of the two
1813 * can be represented by a single basic map.
1814 * If so, replace the pair by the single basic map and start over.
1816 * Since we are constructing the tableaus of the basic maps anyway,
1817 * we exploit them to detect implicit equalities and redundant constraints.
1818 * This also helps the coalescing as it can ignore the redundant constraints.
1819 * In order to avoid confusion, we make all implicit equalities explicit
1820 * in the basic maps. We don't call isl_basic_map_gauss, though,
1821 * as that may affect the number of constraints.
1822 * This means that we have to call isl_basic_map_gauss at the end
1823 * of the computation (in update_basic_maps) to ensure that
1824 * the basic maps are not left in an unexpected state.
1826 struct isl_map *isl_map_coalesce(struct isl_map *map)
1828 int i;
1829 unsigned n;
1830 isl_ctx *ctx;
1831 struct isl_coalesce_info *info = NULL;
1833 map = isl_map_remove_empty_parts(map);
1834 if (!map)
1835 return NULL;
1837 if (map->n <= 1)
1838 return map;
1840 ctx = isl_map_get_ctx(map);
1841 map = isl_map_sort_divs(map);
1842 map = isl_map_cow(map);
1844 if (!map)
1845 return NULL;
1847 n = map->n;
1849 info = isl_calloc_array(map->ctx, struct isl_coalesce_info, n);
1850 if (!info)
1851 goto error;
1853 for (i = 0; i < map->n; ++i) {
1854 info[i].bmap = isl_basic_map_copy(map->p[i]);
1855 info[i].tab = isl_tab_from_basic_map(info[i].bmap, 0);
1856 if (!info[i].tab)
1857 goto error;
1858 if (!ISL_F_ISSET(info[i].bmap, ISL_BASIC_MAP_NO_IMPLICIT))
1859 if (isl_tab_detect_implicit_equalities(info[i].tab) < 0)
1860 goto error;
1861 info[i].bmap = isl_tab_make_equalities_explicit(info[i].tab,
1862 info[i].bmap);
1863 if (!info[i].bmap)
1864 goto error;
1865 if (!ISL_F_ISSET(info[i].bmap, ISL_BASIC_MAP_NO_REDUNDANT))
1866 if (isl_tab_detect_redundant(info[i].tab) < 0)
1867 goto error;
1869 for (i = map->n - 1; i >= 0; --i)
1870 if (info[i].tab->empty)
1871 drop(&info[i]);
1873 if (coalesce(ctx, n, info) < 0)
1874 goto error;
1876 map = update_basic_maps(map, n, info);
1878 clear_coalesce_info(n, info);
1880 return map;
1881 error:
1882 clear_coalesce_info(n, info);
1883 isl_map_free(map);
1884 return NULL;
1887 /* For each pair of basic sets in the set, check if the union of the two
1888 * can be represented by a single basic set.
1889 * If so, replace the pair by the single basic set and start over.
1891 struct isl_set *isl_set_coalesce(struct isl_set *set)
1893 return (struct isl_set *)isl_map_coalesce((struct isl_map *)set);