add extra isl_set_coalesce test case
[isl.git] / isl_coalesce.c
blob6021bf22f1c218ea030030744b3002a2386b6ab7
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 * "eq" and "ineq" are only set if we are currently trying to coalesce
157 * this basic map with another basic map, in which case they represent
158 * the position of the inequalities of this basic map with respect to
159 * the other basic map. The number of elements in the "eq" array
160 * is twice the number of equalities in the "bmap", corresponding
161 * to the two inequalities that make up each equality.
163 struct isl_coalesce_info {
164 isl_basic_map *bmap;
165 struct isl_tab *tab;
166 int removed;
167 int *eq;
168 int *ineq;
171 /* Free all the allocated memory in an array
172 * of "n" isl_coalesce_info elements.
174 static void clear_coalesce_info(int n, struct isl_coalesce_info *info)
176 int i;
178 if (!info)
179 return;
181 for (i = 0; i < n; ++i) {
182 isl_basic_map_free(info[i].bmap);
183 isl_tab_free(info[i].tab);
186 free(info);
189 /* Drop the basic map represented by "info".
190 * That is, clear the memory associated to the entry and
191 * mark it as having been removed.
193 static void drop(struct isl_coalesce_info *info)
195 info->bmap = isl_basic_map_free(info->bmap);
196 isl_tab_free(info->tab);
197 info->tab = NULL;
198 info->removed = 1;
201 /* Exchange the information in "info1" with that in "info2".
203 static void exchange(struct isl_coalesce_info *info1,
204 struct isl_coalesce_info *info2)
206 struct isl_coalesce_info info;
208 info = *info1;
209 *info1 = *info2;
210 *info2 = info;
213 /* This type represents the kind of change that has been performed
214 * while trying to coalesce two basic maps.
216 * isl_change_none: nothing was changed
217 * isl_change_drop_first: the first basic map was removed
218 * isl_change_drop_second: the second basic map was removed
219 * isl_change_fuse: the two basic maps were replaced by a new basic map.
221 enum isl_change {
222 isl_change_error = -1,
223 isl_change_none = 0,
224 isl_change_drop_first,
225 isl_change_drop_second,
226 isl_change_fuse,
229 /* Replace the pair of basic maps i and j by the basic map bounded
230 * by the valid constraints in both basic maps and the constraints
231 * in extra (if not NULL).
232 * Place the fused basic map in the position that is the smallest of i and j.
234 * If "detect_equalities" is set, then look for equalities encoded
235 * as pairs of inequalities.
237 static enum isl_change fuse(int i, int j, struct isl_coalesce_info *info,
238 __isl_keep isl_mat *extra, int detect_equalities)
240 int k, l;
241 struct isl_basic_map *fused = NULL;
242 struct isl_tab *fused_tab = NULL;
243 unsigned total = isl_basic_map_total_dim(info[i].bmap);
244 unsigned extra_rows = extra ? extra->n_row : 0;
246 if (j < i)
247 return fuse(j, i, info, extra, detect_equalities);
249 fused = isl_basic_map_alloc_space(isl_space_copy(info[i].bmap->dim),
250 info[i].bmap->n_div,
251 info[i].bmap->n_eq + info[j].bmap->n_eq,
252 info[i].bmap->n_ineq + info[j].bmap->n_ineq + extra_rows);
253 if (!fused)
254 goto error;
256 for (k = 0; k < info[i].bmap->n_eq; ++k) {
257 if (info[i].eq[2 * k] != STATUS_VALID ||
258 info[i].eq[2 * k + 1] != STATUS_VALID)
259 continue;
260 l = isl_basic_map_alloc_equality(fused);
261 if (l < 0)
262 goto error;
263 isl_seq_cpy(fused->eq[l], info[i].bmap->eq[k], 1 + total);
266 for (k = 0; k < info[j].bmap->n_eq; ++k) {
267 if (info[j].eq[2 * k] != STATUS_VALID ||
268 info[j].eq[2 * k + 1] != STATUS_VALID)
269 continue;
270 l = isl_basic_map_alloc_equality(fused);
271 if (l < 0)
272 goto error;
273 isl_seq_cpy(fused->eq[l], info[j].bmap->eq[k], 1 + total);
276 for (k = 0; k < info[i].bmap->n_ineq; ++k) {
277 if (info[i].ineq[k] != STATUS_VALID)
278 continue;
279 l = isl_basic_map_alloc_inequality(fused);
280 if (l < 0)
281 goto error;
282 isl_seq_cpy(fused->ineq[l], info[i].bmap->ineq[k], 1 + total);
285 for (k = 0; k < info[j].bmap->n_ineq; ++k) {
286 if (info[j].ineq[k] != STATUS_VALID)
287 continue;
288 l = isl_basic_map_alloc_inequality(fused);
289 if (l < 0)
290 goto error;
291 isl_seq_cpy(fused->ineq[l], info[j].bmap->ineq[k], 1 + total);
294 for (k = 0; k < info[i].bmap->n_div; ++k) {
295 int l = isl_basic_map_alloc_div(fused);
296 if (l < 0)
297 goto error;
298 isl_seq_cpy(fused->div[l], info[i].bmap->div[k], 1 + 1 + total);
301 for (k = 0; k < extra_rows; ++k) {
302 l = isl_basic_map_alloc_inequality(fused);
303 if (l < 0)
304 goto error;
305 isl_seq_cpy(fused->ineq[l], extra->row[k], 1 + total);
308 if (detect_equalities)
309 fused = isl_basic_map_detect_inequality_pairs(fused, NULL);
310 fused = isl_basic_map_gauss(fused, NULL);
311 ISL_F_SET(fused, ISL_BASIC_MAP_FINAL);
312 if (ISL_F_ISSET(info[i].bmap, ISL_BASIC_MAP_RATIONAL) &&
313 ISL_F_ISSET(info[j].bmap, ISL_BASIC_MAP_RATIONAL))
314 ISL_F_SET(fused, ISL_BASIC_MAP_RATIONAL);
316 fused_tab = isl_tab_from_basic_map(fused, 0);
317 if (isl_tab_detect_redundant(fused_tab) < 0)
318 goto error;
320 isl_basic_map_free(info[i].bmap);
321 info[i].bmap = fused;
322 isl_tab_free(info[i].tab);
323 info[i].tab = fused_tab;
324 drop(&info[j]);
326 return isl_change_fuse;
327 error:
328 isl_tab_free(fused_tab);
329 isl_basic_map_free(fused);
330 return isl_change_error;
333 /* Given a pair of basic maps i and j such that all constraints are either
334 * "valid" or "cut", check if the facets corresponding to the "cut"
335 * constraints of i lie entirely within basic map j.
336 * If so, replace the pair by the basic map consisting of the valid
337 * constraints in both basic maps.
338 * Checking whether the facet lies entirely within basic map j
339 * is performed by checking whether the constraints of basic map j
340 * are valid for the facet. These tests are performed on a rational
341 * tableau to avoid the theoretical possibility that a constraint
342 * that was considered to be a cut constraint for the entire basic map i
343 * happens to be considered to be a valid constraint for the facet,
344 * even though it cuts off the same rational points.
346 * To see that we are not introducing any extra points, call the
347 * two basic maps A and B and the resulting map U and let x
348 * be an element of U \setminus ( A \cup B ).
349 * A line connecting x with an element of A \cup B meets a facet F
350 * of either A or B. Assume it is a facet of B and let c_1 be
351 * the corresponding facet constraint. We have c_1(x) < 0 and
352 * so c_1 is a cut constraint. This implies that there is some
353 * (possibly rational) point x' satisfying the constraints of A
354 * and the opposite of c_1 as otherwise c_1 would have been marked
355 * valid for A. The line connecting x and x' meets a facet of A
356 * in a (possibly rational) point that also violates c_1, but this
357 * is impossible since all cut constraints of B are valid for all
358 * cut facets of A.
359 * In case F is a facet of A rather than B, then we can apply the
360 * above reasoning to find a facet of B separating x from A \cup B first.
362 static enum isl_change check_facets(int i, int j,
363 struct isl_coalesce_info *info)
365 int k, l;
366 struct isl_tab_undo *snap, *snap2;
367 unsigned n_eq = info[i].bmap->n_eq;
369 snap = isl_tab_snap(info[i].tab);
370 if (isl_tab_mark_rational(info[i].tab) < 0)
371 return isl_change_error;
372 snap2 = isl_tab_snap(info[i].tab);
374 for (k = 0; k < info[i].bmap->n_ineq; ++k) {
375 if (info[i].ineq[k] != STATUS_CUT)
376 continue;
377 if (isl_tab_select_facet(info[i].tab, n_eq + k) < 0)
378 return isl_change_error;
379 for (l = 0; l < info[j].bmap->n_ineq; ++l) {
380 int stat;
381 if (info[j].ineq[l] != STATUS_CUT)
382 continue;
383 stat = status_in(info[j].bmap->ineq[l], info[i].tab);
384 if (stat != STATUS_VALID)
385 break;
387 if (isl_tab_rollback(info[i].tab, snap2) < 0)
388 return isl_change_error;
389 if (l < info[j].bmap->n_ineq)
390 break;
393 if (k < info[i].bmap->n_ineq) {
394 if (isl_tab_rollback(info[i].tab, snap) < 0)
395 return isl_change_error;
396 return isl_change_none;
398 return fuse(i, j, info, NULL, 0);
401 /* Check if info->bmap contains the basic map represented
402 * by the tableau "tab".
403 * For each equality, we check both the constraint itself
404 * (as an inequality) and its negation. Make sure the
405 * equality is returned to its original state before returning.
407 static int contains(struct isl_coalesce_info *info, struct isl_tab *tab)
409 int k;
410 unsigned dim;
411 isl_basic_map *bmap = info->bmap;
413 dim = isl_basic_map_total_dim(bmap);
414 for (k = 0; k < bmap->n_eq; ++k) {
415 int stat;
416 isl_seq_neg(bmap->eq[k], bmap->eq[k], 1 + dim);
417 stat = status_in(bmap->eq[k], tab);
418 isl_seq_neg(bmap->eq[k], bmap->eq[k], 1 + dim);
419 if (stat != STATUS_VALID)
420 return 0;
421 stat = status_in(bmap->eq[k], tab);
422 if (stat != STATUS_VALID)
423 return 0;
426 for (k = 0; k < bmap->n_ineq; ++k) {
427 int stat;
428 if (info->ineq[k] == STATUS_REDUNDANT)
429 continue;
430 stat = status_in(bmap->ineq[k], tab);
431 if (stat != STATUS_VALID)
432 return 0;
434 return 1;
437 /* Basic map "i" has an inequality (say "k") that is adjacent
438 * to some inequality of basic map "j". All the other inequalities
439 * are valid for "j".
440 * Check if basic map "j" forms an extension of basic map "i".
442 * Note that this function is only called if some of the equalities or
443 * inequalities of basic map "j" do cut basic map "i". The function is
444 * correct even if there are no such cut constraints, but in that case
445 * the additional checks performed by this function are overkill.
447 * In particular, we replace constraint k, say f >= 0, by constraint
448 * f <= -1, add the inequalities of "j" that are valid for "i"
449 * and check if the result is a subset of basic map "j".
450 * If so, then we know that this result is exactly equal to basic map "j"
451 * since all its constraints are valid for basic map "j".
452 * By combining the valid constraints of "i" (all equalities and all
453 * inequalities except "k") and the valid constraints of "j" we therefore
454 * obtain a basic map that is equal to their union.
455 * In this case, there is no need to perform a rollback of the tableau
456 * since it is going to be destroyed in fuse().
459 * |\__ |\__
460 * | \__ | \__
461 * | \_ => | \__
462 * |_______| _ |_________\
465 * |\ |\
466 * | \ | \
467 * | \ | \
468 * | | | \
469 * | ||\ => | \
470 * | || \ | \
471 * | || | | |
472 * |__||_/ |_____/
474 static enum isl_change is_adj_ineq_extension(int i, int j,
475 struct isl_coalesce_info *info)
477 int k;
478 struct isl_tab_undo *snap;
479 unsigned n_eq = info[i].bmap->n_eq;
480 unsigned total = isl_basic_map_total_dim(info[i].bmap);
481 int r;
483 if (isl_tab_extend_cons(info[i].tab, 1 + info[j].bmap->n_ineq) < 0)
484 return isl_change_error;
486 for (k = 0; k < info[i].bmap->n_ineq; ++k)
487 if (info[i].ineq[k] == STATUS_ADJ_INEQ)
488 break;
489 if (k >= info[i].bmap->n_ineq)
490 isl_die(isl_basic_map_get_ctx(info[i].bmap), isl_error_internal,
491 "info[i].ineq should have exactly one STATUS_ADJ_INEQ",
492 return isl_change_error);
494 snap = isl_tab_snap(info[i].tab);
496 if (isl_tab_unrestrict(info[i].tab, n_eq + k) < 0)
497 return isl_change_error;
499 isl_seq_neg(info[i].bmap->ineq[k], info[i].bmap->ineq[k], 1 + total);
500 isl_int_sub_ui(info[i].bmap->ineq[k][0], info[i].bmap->ineq[k][0], 1);
501 r = isl_tab_add_ineq(info[i].tab, info[i].bmap->ineq[k]);
502 isl_seq_neg(info[i].bmap->ineq[k], info[i].bmap->ineq[k], 1 + total);
503 isl_int_sub_ui(info[i].bmap->ineq[k][0], info[i].bmap->ineq[k][0], 1);
504 if (r < 0)
505 return isl_change_error;
507 for (k = 0; k < info[j].bmap->n_ineq; ++k) {
508 if (info[j].ineq[k] != STATUS_VALID)
509 continue;
510 if (isl_tab_add_ineq(info[i].tab, info[j].bmap->ineq[k]) < 0)
511 return isl_change_error;
514 if (contains(&info[j], info[i].tab))
515 return fuse(i, j, info, NULL, 0);
517 if (isl_tab_rollback(info[i].tab, snap) < 0)
518 return isl_change_error;
520 return isl_change_none;
524 /* Both basic maps have at least one inequality with and adjacent
525 * (but opposite) inequality in the other basic map.
526 * Check that there are no cut constraints and that there is only
527 * a single pair of adjacent inequalities.
528 * If so, we can replace the pair by a single basic map described
529 * by all but the pair of adjacent inequalities.
530 * Any additional points introduced lie strictly between the two
531 * adjacent hyperplanes and can therefore be integral.
533 * ____ _____
534 * / ||\ / \
535 * / || \ / \
536 * \ || \ => \ \
537 * \ || / \ /
538 * \___||_/ \_____/
540 * The test for a single pair of adjancent inequalities is important
541 * for avoiding the combination of two basic maps like the following
543 * /|
544 * / |
545 * /__|
546 * _____
547 * | |
548 * | |
549 * |___|
551 * If there are some cut constraints on one side, then we may
552 * still be able to fuse the two basic maps, but we need to perform
553 * some additional checks in is_adj_ineq_extension.
555 static enum isl_change check_adj_ineq(int i, int j,
556 struct isl_coalesce_info *info)
558 int count_i, count_j;
559 int cut_i, cut_j;
561 count_i = count(info[i].ineq, info[i].bmap->n_ineq, STATUS_ADJ_INEQ);
562 count_j = count(info[j].ineq, info[j].bmap->n_ineq, STATUS_ADJ_INEQ);
564 if (count_i != 1 && count_j != 1)
565 return isl_change_none;
567 cut_i = any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_CUT) ||
568 any(info[i].ineq, info[i].bmap->n_ineq, STATUS_CUT);
569 cut_j = any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_CUT) ||
570 any(info[j].ineq, info[j].bmap->n_ineq, STATUS_CUT);
572 if (!cut_i && !cut_j && count_i == 1 && count_j == 1)
573 return fuse(i, j, info, NULL, 0);
575 if (count_i == 1 && !cut_i)
576 return is_adj_ineq_extension(i, j, info);
578 if (count_j == 1 && !cut_j)
579 return is_adj_ineq_extension(j, i, info);
581 return isl_change_none;
584 /* Basic map "i" has an inequality "k" that is adjacent to some equality
585 * of basic map "j". All the other inequalities are valid for "j".
586 * Check if basic map "j" forms an extension of basic map "i".
588 * In particular, we relax constraint "k", compute the corresponding
589 * facet and check whether it is included in the other basic map.
590 * If so, we know that relaxing the constraint extends the basic
591 * map with exactly the other basic map (we already know that this
592 * other basic map is included in the extension, because there
593 * were no "cut" inequalities in "i") and we can replace the
594 * two basic maps by this extension.
595 * Place this extension in the position that is the smallest of i and j.
596 * ____ _____
597 * / || / |
598 * / || / |
599 * \ || => \ |
600 * \ || \ |
601 * \___|| \____|
603 static enum isl_change is_adj_eq_extension(int i, int j, int k,
604 struct isl_coalesce_info *info)
606 int change = isl_change_none;
607 int super;
608 struct isl_tab_undo *snap, *snap2;
609 unsigned n_eq = info[i].bmap->n_eq;
611 if (isl_tab_is_equality(info[i].tab, n_eq + k))
612 return isl_change_none;
614 snap = isl_tab_snap(info[i].tab);
615 if (isl_tab_relax(info[i].tab, n_eq + k) < 0)
616 return isl_change_error;
617 snap2 = isl_tab_snap(info[i].tab);
618 if (isl_tab_select_facet(info[i].tab, n_eq + k) < 0)
619 return isl_change_error;
620 super = contains(&info[j], info[i].tab);
621 if (super) {
622 if (isl_tab_rollback(info[i].tab, snap2) < 0)
623 return isl_change_error;
624 info[i].bmap = isl_basic_map_cow(info[i].bmap);
625 if (!info[i].bmap)
626 return isl_change_error;
627 isl_int_add_ui(info[i].bmap->ineq[k][0],
628 info[i].bmap->ineq[k][0], 1);
629 ISL_F_SET(info[i].bmap, ISL_BASIC_MAP_FINAL);
630 drop(&info[j]);
631 if (j < i)
632 exchange(&info[i], &info[j]);
633 change = isl_change_fuse;
634 } else
635 if (isl_tab_rollback(info[i].tab, snap) < 0)
636 return isl_change_error;
638 return change;
641 /* Data structure that keeps track of the wrapping constraints
642 * and of information to bound the coefficients of those constraints.
644 * bound is set if we want to apply a bound on the coefficients
645 * mat contains the wrapping constraints
646 * max is the bound on the coefficients (if bound is set)
648 struct isl_wraps {
649 int bound;
650 isl_mat *mat;
651 isl_int max;
654 /* Update wraps->max to be greater than or equal to the coefficients
655 * in the equalities and inequalities of info->bmap that can be removed
656 * if we end up applying wrapping.
658 static void wraps_update_max(struct isl_wraps *wraps,
659 struct isl_coalesce_info *info)
661 int k;
662 isl_int max_k;
663 unsigned total = isl_basic_map_total_dim(info->bmap);
665 isl_int_init(max_k);
667 for (k = 0; k < info->bmap->n_eq; ++k) {
668 if (info->eq[2 * k] == STATUS_VALID &&
669 info->eq[2 * k + 1] == STATUS_VALID)
670 continue;
671 isl_seq_abs_max(info->bmap->eq[k] + 1, total, &max_k);
672 if (isl_int_abs_gt(max_k, wraps->max))
673 isl_int_set(wraps->max, max_k);
676 for (k = 0; k < info->bmap->n_ineq; ++k) {
677 if (info->ineq[k] == STATUS_VALID ||
678 info->ineq[k] == STATUS_REDUNDANT)
679 continue;
680 isl_seq_abs_max(info->bmap->ineq[k] + 1, total, &max_k);
681 if (isl_int_abs_gt(max_k, wraps->max))
682 isl_int_set(wraps->max, max_k);
685 isl_int_clear(max_k);
688 /* Initialize the isl_wraps data structure.
689 * If we want to bound the coefficients of the wrapping constraints,
690 * we set wraps->max to the largest coefficient
691 * in the equalities and inequalities that can be removed if we end up
692 * applying wrapping.
694 static void wraps_init(struct isl_wraps *wraps, __isl_take isl_mat *mat,
695 struct isl_coalesce_info *info, int i, int j)
697 isl_ctx *ctx;
699 wraps->bound = 0;
700 wraps->mat = mat;
701 if (!mat)
702 return;
703 ctx = isl_mat_get_ctx(mat);
704 wraps->bound = isl_options_get_coalesce_bounded_wrapping(ctx);
705 if (!wraps->bound)
706 return;
707 isl_int_init(wraps->max);
708 isl_int_set_si(wraps->max, 0);
709 wraps_update_max(wraps, &info[i]);
710 wraps_update_max(wraps, &info[j]);
713 /* Free the contents of the isl_wraps data structure.
715 static void wraps_free(struct isl_wraps *wraps)
717 isl_mat_free(wraps->mat);
718 if (wraps->bound)
719 isl_int_clear(wraps->max);
722 /* Is the wrapping constraint in row "row" allowed?
724 * If wraps->bound is set, we check that none of the coefficients
725 * is greater than wraps->max.
727 static int allow_wrap(struct isl_wraps *wraps, int row)
729 int i;
731 if (!wraps->bound)
732 return 1;
734 for (i = 1; i < wraps->mat->n_col; ++i)
735 if (isl_int_abs_gt(wraps->mat->row[row][i], wraps->max))
736 return 0;
738 return 1;
741 /* For each non-redundant constraint in info->bmap (as determined by info->tab),
742 * wrap the constraint around "bound" such that it includes the whole
743 * set "set" and append the resulting constraint to "wraps".
744 * "wraps" is assumed to have been pre-allocated to the appropriate size.
745 * wraps->n_row is the number of actual wrapped constraints that have
746 * been added.
747 * If any of the wrapping problems results in a constraint that is
748 * identical to "bound", then this means that "set" is unbounded in such
749 * way that no wrapping is possible. If this happens then wraps->n_row
750 * is reset to zero.
751 * Similarly, if we want to bound the coefficients of the wrapping
752 * constraints and a newly added wrapping constraint does not
753 * satisfy the bound, then wraps->n_row is also reset to zero.
755 static int add_wraps(struct isl_wraps *wraps, struct isl_coalesce_info *info,
756 isl_int *bound, __isl_keep isl_set *set)
758 int l;
759 int w;
760 isl_basic_map *bmap = info->bmap;
761 unsigned total = isl_basic_map_total_dim(bmap);
763 w = wraps->mat->n_row;
765 for (l = 0; l < bmap->n_ineq; ++l) {
766 if (isl_seq_is_neg(bound, bmap->ineq[l], 1 + total))
767 continue;
768 if (isl_seq_eq(bound, bmap->ineq[l], 1 + total))
769 continue;
770 if (isl_tab_is_redundant(info->tab, bmap->n_eq + l))
771 continue;
773 isl_seq_cpy(wraps->mat->row[w], bound, 1 + total);
774 if (!isl_set_wrap_facet(set, wraps->mat->row[w], bmap->ineq[l]))
775 return -1;
776 if (isl_seq_eq(wraps->mat->row[w], bound, 1 + total))
777 goto unbounded;
778 if (!allow_wrap(wraps, w))
779 goto unbounded;
780 ++w;
782 for (l = 0; l < bmap->n_eq; ++l) {
783 if (isl_seq_is_neg(bound, bmap->eq[l], 1 + total))
784 continue;
785 if (isl_seq_eq(bound, bmap->eq[l], 1 + total))
786 continue;
788 isl_seq_cpy(wraps->mat->row[w], bound, 1 + total);
789 isl_seq_neg(wraps->mat->row[w + 1], bmap->eq[l], 1 + total);
790 if (!isl_set_wrap_facet(set, wraps->mat->row[w],
791 wraps->mat->row[w + 1]))
792 return -1;
793 if (isl_seq_eq(wraps->mat->row[w], bound, 1 + total))
794 goto unbounded;
795 if (!allow_wrap(wraps, w))
796 goto unbounded;
797 ++w;
799 isl_seq_cpy(wraps->mat->row[w], bound, 1 + total);
800 if (!isl_set_wrap_facet(set, wraps->mat->row[w], bmap->eq[l]))
801 return -1;
802 if (isl_seq_eq(wraps->mat->row[w], bound, 1 + total))
803 goto unbounded;
804 if (!allow_wrap(wraps, w))
805 goto unbounded;
806 ++w;
809 wraps->mat->n_row = w;
810 return 0;
811 unbounded:
812 wraps->mat->n_row = 0;
813 return 0;
816 /* Check if the constraints in "wraps" from "first" until the last
817 * are all valid for the basic set represented by "tab".
818 * If not, wraps->n_row is set to zero.
820 static int check_wraps(__isl_keep isl_mat *wraps, int first,
821 struct isl_tab *tab)
823 int i;
825 for (i = first; i < wraps->n_row; ++i) {
826 enum isl_ineq_type type;
827 type = isl_tab_ineq_type(tab, wraps->row[i]);
828 if (type == isl_ineq_error)
829 return -1;
830 if (type == isl_ineq_redundant)
831 continue;
832 wraps->n_row = 0;
833 return 0;
836 return 0;
839 /* Return a set that corresponds to the non-redundant constraints
840 * (as recorded in tab) of bmap.
842 * It's important to remove the redundant constraints as some
843 * of the other constraints may have been modified after the
844 * constraints were marked redundant.
845 * In particular, a constraint may have been relaxed.
846 * Redundant constraints are ignored when a constraint is relaxed
847 * and should therefore continue to be ignored ever after.
848 * Otherwise, the relaxation might be thwarted by some of
849 * these constraints.
851 * Update the underlying set to ensure that the dimension doesn't change.
852 * Otherwise the integer divisions could get dropped if the tab
853 * turns out to be empty.
855 static __isl_give isl_set *set_from_updated_bmap(__isl_keep isl_basic_map *bmap,
856 struct isl_tab *tab)
858 isl_basic_set *bset;
860 bmap = isl_basic_map_copy(bmap);
861 bset = isl_basic_map_underlying_set(bmap);
862 bset = isl_basic_set_cow(bset);
863 bset = isl_basic_set_update_from_tab(bset, tab);
864 return isl_set_from_basic_set(bset);
867 /* Given a basic set i with a constraint k that is adjacent to
868 * basic set j, check if we can wrap
869 * both the facet corresponding to k and basic map j
870 * around their ridges to include the other set.
871 * If so, replace the pair of basic sets by their union.
873 * All constraints of i (except k) are assumed to be valid for j.
874 * This means that there is no real need to wrap the ridges of
875 * the faces of basic map i around basic map j but since we do,
876 * we have to check that the resulting wrapping constraints are valid for i.
877 * ____ _____
878 * / | / \
879 * / || / |
880 * \ || => \ |
881 * \ || \ |
882 * \___|| \____|
885 static enum isl_change can_wrap_in_facet(int i, int j, int k,
886 struct isl_coalesce_info *info)
888 enum isl_change change = isl_change_none;
889 struct isl_wraps wraps;
890 isl_ctx *ctx;
891 isl_mat *mat;
892 struct isl_set *set_i = NULL;
893 struct isl_set *set_j = NULL;
894 struct isl_vec *bound = NULL;
895 unsigned total = isl_basic_map_total_dim(info[i].bmap);
896 struct isl_tab_undo *snap;
897 int n;
899 set_i = set_from_updated_bmap(info[i].bmap, info[i].tab);
900 set_j = set_from_updated_bmap(info[j].bmap, info[j].tab);
901 ctx = isl_basic_map_get_ctx(info[i].bmap);
902 mat = isl_mat_alloc(ctx, 2 * (info[i].bmap->n_eq + info[j].bmap->n_eq) +
903 info[i].bmap->n_ineq + info[j].bmap->n_ineq,
904 1 + total);
905 wraps_init(&wraps, mat, info, i, j);
906 bound = isl_vec_alloc(ctx, 1 + total);
907 if (!set_i || !set_j || !wraps.mat || !bound)
908 goto error;
910 isl_seq_cpy(bound->el, info[i].bmap->ineq[k], 1 + total);
911 isl_int_add_ui(bound->el[0], bound->el[0], 1);
913 isl_seq_cpy(wraps.mat->row[0], bound->el, 1 + total);
914 wraps.mat->n_row = 1;
916 if (add_wraps(&wraps, &info[j], bound->el, set_i) < 0)
917 goto error;
918 if (!wraps.mat->n_row)
919 goto unbounded;
921 snap = isl_tab_snap(info[i].tab);
923 if (isl_tab_select_facet(info[i].tab, info[i].bmap->n_eq + k) < 0)
924 goto error;
925 if (isl_tab_detect_redundant(info[i].tab) < 0)
926 goto error;
928 isl_seq_neg(bound->el, info[i].bmap->ineq[k], 1 + total);
930 n = wraps.mat->n_row;
931 if (add_wraps(&wraps, &info[i], bound->el, set_j) < 0)
932 goto error;
934 if (isl_tab_rollback(info[i].tab, snap) < 0)
935 goto error;
936 if (check_wraps(wraps.mat, n, info[i].tab) < 0)
937 goto error;
938 if (!wraps.mat->n_row)
939 goto unbounded;
941 change = fuse(i, j, info, wraps.mat, 0);
943 unbounded:
944 wraps_free(&wraps);
946 isl_set_free(set_i);
947 isl_set_free(set_j);
949 isl_vec_free(bound);
951 return change;
952 error:
953 wraps_free(&wraps);
954 isl_vec_free(bound);
955 isl_set_free(set_i);
956 isl_set_free(set_j);
957 return isl_change_error;
960 /* Given a pair of basic maps i and j such that j sticks out
961 * of i at n cut constraints, each time by at most one,
962 * try to compute wrapping constraints and replace the two
963 * basic maps by a single basic map.
964 * The other constraints of i are assumed to be valid for j.
966 * For each cut constraint t(x) >= 0 of i, we add the relaxed version
967 * t(x) + 1 >= 0, along with wrapping constraints for all constraints
968 * of basic map j that bound the part of basic map j that sticks out
969 * of the cut constraint.
970 * In particular, we first intersect basic map j with t(x) + 1 = 0.
971 * If the result is empty, then t(x) >= 0 was actually a valid constraint
972 * (with respect to the integer points), so we add t(x) >= 0 instead.
973 * Otherwise, we wrap the constraints of basic map j that are not
974 * redundant in this intersection over the union of the two basic maps.
976 * If any wrapping fails, i.e., if we cannot wrap to touch
977 * the union, then we give up.
978 * Otherwise, the pair of basic maps is replaced by their union.
980 static enum isl_change wrap_in_facets(int i, int j, int *cuts, int n,
981 struct isl_coalesce_info *info)
983 enum isl_change change = isl_change_none;
984 struct isl_wraps wraps;
985 isl_ctx *ctx;
986 isl_mat *mat;
987 isl_set *set = NULL;
988 unsigned total = isl_basic_map_total_dim(info[i].bmap);
989 int max_wrap;
990 int k, w;
991 struct isl_tab_undo *snap;
993 if (isl_tab_extend_cons(info[j].tab, 1) < 0)
994 goto error;
996 max_wrap = 1 + 2 * info[j].bmap->n_eq + info[j].bmap->n_ineq;
997 max_wrap *= n;
999 set = isl_set_union(set_from_updated_bmap(info[i].bmap, info[i].tab),
1000 set_from_updated_bmap(info[j].bmap, info[j].tab));
1001 ctx = isl_basic_map_get_ctx(info[i].bmap);
1002 mat = isl_mat_alloc(ctx, max_wrap, 1 + total);
1003 wraps_init(&wraps, mat, info, i, j);
1004 if (!set || !wraps.mat)
1005 goto error;
1007 snap = isl_tab_snap(info[j].tab);
1009 wraps.mat->n_row = 0;
1011 for (k = 0; k < n; ++k) {
1012 w = wraps.mat->n_row++;
1013 isl_seq_cpy(wraps.mat->row[w],
1014 info[i].bmap->ineq[cuts[k]], 1 + total);
1015 isl_int_add_ui(wraps.mat->row[w][0], wraps.mat->row[w][0], 1);
1016 if (isl_tab_add_eq(info[j].tab, wraps.mat->row[w]) < 0)
1017 goto error;
1018 if (isl_tab_detect_redundant(info[j].tab) < 0)
1019 goto error;
1021 if (info[j].tab->empty)
1022 isl_int_sub_ui(wraps.mat->row[w][0],
1023 wraps.mat->row[w][0], 1);
1024 else if (add_wraps(&wraps, &info[j],
1025 wraps.mat->row[w], set) < 0)
1026 goto error;
1028 if (isl_tab_rollback(info[j].tab, snap) < 0)
1029 goto error;
1031 if (!wraps.mat->n_row)
1032 break;
1035 if (k == n)
1036 change = fuse(i, j, info, wraps.mat, 0);
1038 wraps_free(&wraps);
1039 isl_set_free(set);
1041 return change;
1042 error:
1043 wraps_free(&wraps);
1044 isl_set_free(set);
1045 return isl_change_error;
1048 /* Given two basic sets i and j such that i has no cut equalities,
1049 * check if relaxing all the cut inequalities of i by one turns
1050 * them into valid constraint for j and check if we can wrap in
1051 * the bits that are sticking out.
1052 * If so, replace the pair by their union.
1054 * We first check if all relaxed cut inequalities of i are valid for j
1055 * and then try to wrap in the intersections of the relaxed cut inequalities
1056 * with j.
1058 * During this wrapping, we consider the points of j that lie at a distance
1059 * of exactly 1 from i. In particular, we ignore the points that lie in
1060 * between this lower-dimensional space and the basic map i.
1061 * We can therefore only apply this to integer maps.
1062 * ____ _____
1063 * / ___|_ / \
1064 * / | | / |
1065 * \ | | => \ |
1066 * \|____| \ |
1067 * \___| \____/
1069 * _____ ______
1070 * | ____|_ | \
1071 * | | | | |
1072 * | | | => | |
1073 * |_| | | |
1074 * |_____| \______|
1076 * _______
1077 * | |
1078 * | |\ |
1079 * | | \ |
1080 * | | \ |
1081 * | | \|
1082 * | | \
1083 * | |_____\
1084 * | |
1085 * |_______|
1087 * Wrapping can fail if the result of wrapping one of the facets
1088 * around its edges does not produce any new facet constraint.
1089 * In particular, this happens when we try to wrap in unbounded sets.
1091 * _______________________________________________________________________
1093 * | ___
1094 * | | |
1095 * |_| |_________________________________________________________________
1096 * |___|
1098 * The following is not an acceptable result of coalescing the above two
1099 * sets as it includes extra integer points.
1100 * _______________________________________________________________________
1102 * |
1103 * |
1105 * \______________________________________________________________________
1107 static enum isl_change can_wrap_in_set(int i, int j,
1108 struct isl_coalesce_info *info)
1110 enum isl_change change = isl_change_none;
1111 int k, m;
1112 int n;
1113 int *cuts = NULL;
1114 isl_ctx *ctx;
1116 if (ISL_F_ISSET(info[i].bmap, ISL_BASIC_MAP_RATIONAL) ||
1117 ISL_F_ISSET(info[j].bmap, ISL_BASIC_MAP_RATIONAL))
1118 return isl_change_none;
1120 n = count(info[i].ineq, info[i].bmap->n_ineq, STATUS_CUT);
1121 if (n == 0)
1122 return isl_change_none;
1124 ctx = isl_basic_map_get_ctx(info[i].bmap);
1125 cuts = isl_alloc_array(ctx, int, n);
1126 if (!cuts)
1127 return isl_change_error;
1129 for (k = 0, m = 0; m < n; ++k) {
1130 enum isl_ineq_type type;
1132 if (info[i].ineq[k] != STATUS_CUT)
1133 continue;
1135 isl_int_add_ui(info[i].bmap->ineq[k][0],
1136 info[i].bmap->ineq[k][0], 1);
1137 type = isl_tab_ineq_type(info[j].tab, info[i].bmap->ineq[k]);
1138 isl_int_sub_ui(info[i].bmap->ineq[k][0],
1139 info[i].bmap->ineq[k][0], 1);
1140 if (type == isl_ineq_error)
1141 goto error;
1142 if (type != isl_ineq_redundant)
1143 break;
1144 cuts[m] = k;
1145 ++m;
1148 if (m == n)
1149 change = wrap_in_facets(i, j, cuts, n, info);
1151 free(cuts);
1153 return change;
1154 error:
1155 free(cuts);
1156 return isl_change_error;
1159 /* Check if either i or j has only cut inequalities that can
1160 * be used to wrap in (a facet of) the other basic set.
1161 * if so, replace the pair by their union.
1163 static enum isl_change check_wrap(int i, int j, struct isl_coalesce_info *info)
1165 enum isl_change change = isl_change_none;
1167 if (!any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_CUT))
1168 change = can_wrap_in_set(i, j, info);
1169 if (change != isl_change_none)
1170 return change;
1172 if (!any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_CUT))
1173 change = can_wrap_in_set(j, i, info);
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)
1190 enum isl_change change = isl_change_none;
1191 int k;
1193 if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ADJ_INEQ) &&
1194 any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_ADJ_INEQ))
1195 /* ADJ EQ TOO MANY */
1196 return isl_change_none;
1198 if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ADJ_INEQ))
1199 return check_adj_eq(j, i, info);
1201 /* j has an equality adjacent to an inequality in i */
1203 if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_CUT))
1204 return isl_change_none;
1205 if (any(info[i].ineq, info[i].bmap->n_ineq, STATUS_CUT))
1206 /* ADJ EQ CUT */
1207 return isl_change_none;
1208 if (count(info[i].ineq, info[i].bmap->n_ineq, STATUS_ADJ_EQ) != 1 ||
1209 any(info[j].ineq, info[j].bmap->n_ineq, STATUS_ADJ_EQ) ||
1210 any(info[i].ineq, info[i].bmap->n_ineq, STATUS_ADJ_INEQ) ||
1211 any(info[j].ineq, info[j].bmap->n_ineq, STATUS_ADJ_INEQ))
1212 /* ADJ EQ TOO MANY */
1213 return isl_change_none;
1215 for (k = 0; k < info[i].bmap->n_ineq; ++k)
1216 if (info[i].ineq[k] == STATUS_ADJ_EQ)
1217 break;
1219 change = is_adj_eq_extension(i, j, k, info);
1220 if (change != isl_change_none)
1221 return change;
1223 if (count(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_ADJ_INEQ) != 1)
1224 return isl_change_none;
1226 change = can_wrap_in_facet(i, j, k, info);
1228 return change;
1231 /* The two basic maps lie on adjacent hyperplanes. In particular,
1232 * basic map "i" has an equality that lies parallel to basic map "j".
1233 * Check if we can wrap the facets around the parallel hyperplanes
1234 * to include the other set.
1236 * We perform basically the same operations as can_wrap_in_facet,
1237 * except that we don't need to select a facet of one of the sets.
1239 * \\ \\
1240 * \\ => \\
1241 * \ \|
1243 * If there is more than one equality of "i" adjacent to an equality of "j",
1244 * then the result will satisfy one or more equalities that are a linear
1245 * combination of these equalities. These will be encoded as pairs
1246 * of inequalities in the wrapping constraints and need to be made
1247 * explicit.
1249 static enum isl_change check_eq_adj_eq(int i, int j,
1250 struct isl_coalesce_info *info)
1252 int k;
1253 enum isl_change change = isl_change_none;
1254 int detect_equalities = 0;
1255 struct isl_wraps wraps;
1256 isl_ctx *ctx;
1257 isl_mat *mat;
1258 struct isl_set *set_i = NULL;
1259 struct isl_set *set_j = NULL;
1260 struct isl_vec *bound = NULL;
1261 unsigned total = isl_basic_map_total_dim(info[i].bmap);
1263 if (count(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ADJ_EQ) != 1)
1264 detect_equalities = 1;
1266 for (k = 0; k < 2 * info[i].bmap->n_eq ; ++k)
1267 if (info[i].eq[k] == STATUS_ADJ_EQ)
1268 break;
1270 set_i = set_from_updated_bmap(info[i].bmap, info[i].tab);
1271 set_j = set_from_updated_bmap(info[j].bmap, info[j].tab);
1272 ctx = isl_basic_map_get_ctx(info[i].bmap);
1273 mat = isl_mat_alloc(ctx, 2 * (info[i].bmap->n_eq + info[j].bmap->n_eq) +
1274 info[i].bmap->n_ineq + info[j].bmap->n_ineq,
1275 1 + total);
1276 wraps_init(&wraps, mat, info, i, j);
1277 bound = isl_vec_alloc(ctx, 1 + total);
1278 if (!set_i || !set_j || !wraps.mat || !bound)
1279 goto error;
1281 if (k % 2 == 0)
1282 isl_seq_neg(bound->el, info[i].bmap->eq[k / 2], 1 + total);
1283 else
1284 isl_seq_cpy(bound->el, info[i].bmap->eq[k / 2], 1 + total);
1285 isl_int_add_ui(bound->el[0], bound->el[0], 1);
1287 isl_seq_cpy(wraps.mat->row[0], bound->el, 1 + total);
1288 wraps.mat->n_row = 1;
1290 if (add_wraps(&wraps, &info[j], bound->el, set_i) < 0)
1291 goto error;
1292 if (!wraps.mat->n_row)
1293 goto unbounded;
1295 isl_int_sub_ui(bound->el[0], bound->el[0], 1);
1296 isl_seq_neg(bound->el, bound->el, 1 + total);
1298 isl_seq_cpy(wraps.mat->row[wraps.mat->n_row], bound->el, 1 + total);
1299 wraps.mat->n_row++;
1301 if (add_wraps(&wraps, &info[i], bound->el, set_j) < 0)
1302 goto error;
1303 if (!wraps.mat->n_row)
1304 goto unbounded;
1306 change = fuse(i, j, info, wraps.mat, detect_equalities);
1308 if (0) {
1309 error: change = isl_change_error;
1311 unbounded:
1313 wraps_free(&wraps);
1314 isl_set_free(set_i);
1315 isl_set_free(set_j);
1316 isl_vec_free(bound);
1318 return change;
1321 /* Check if the union of the given pair of basic maps
1322 * can be represented by a single basic map.
1323 * If so, replace the pair by the single basic map and return
1324 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
1325 * Otherwise, return isl_change_none.
1326 * The two basic maps are assumed to live in the same local space.
1328 * We first check the effect of each constraint of one basic map
1329 * on the other basic map.
1330 * The constraint may be
1331 * redundant the constraint is redundant in its own
1332 * basic map and should be ignore and removed
1333 * in the end
1334 * valid all (integer) points of the other basic map
1335 * satisfy the constraint
1336 * separate no (integer) point of the other basic map
1337 * satisfies the constraint
1338 * cut some but not all points of the other basic map
1339 * satisfy the constraint
1340 * adj_eq the given constraint is adjacent (on the outside)
1341 * to an equality of the other basic map
1342 * adj_ineq the given constraint is adjacent (on the outside)
1343 * to an inequality of the other basic map
1345 * We consider seven cases in which we can replace the pair by a single
1346 * basic map. We ignore all "redundant" constraints.
1348 * 1. all constraints of one basic map are valid
1349 * => the other basic map is a subset and can be removed
1351 * 2. all constraints of both basic maps are either "valid" or "cut"
1352 * and the facets corresponding to the "cut" constraints
1353 * of one of the basic maps lies entirely inside the other basic map
1354 * => the pair can be replaced by a basic map consisting
1355 * of the valid constraints in both basic maps
1357 * 3. there is a single pair of adjacent inequalities
1358 * (all other constraints are "valid")
1359 * => the pair can be replaced by a basic map consisting
1360 * of the valid constraints in both basic maps
1362 * 4. one basic map has a single adjacent inequality, while the other
1363 * constraints are "valid". The other basic map has some
1364 * "cut" constraints, but replacing the adjacent inequality by
1365 * its opposite and adding the valid constraints of the other
1366 * basic map results in a subset of the other basic map
1367 * => the pair can be replaced by a basic map consisting
1368 * of the valid constraints in both basic maps
1370 * 5. there is a single adjacent pair of an inequality and an equality,
1371 * the other constraints of the basic map containing the inequality are
1372 * "valid". Moreover, if the inequality the basic map is relaxed
1373 * and then turned into an equality, then resulting facet lies
1374 * entirely inside the other basic map
1375 * => the pair can be replaced by the basic map containing
1376 * the inequality, with the inequality relaxed.
1378 * 6. there is a single adjacent pair of an inequality and an equality,
1379 * the other constraints of the basic map containing the inequality are
1380 * "valid". Moreover, the facets corresponding to both
1381 * the inequality and the equality can be wrapped around their
1382 * ridges to include the other basic map
1383 * => the pair can be replaced by a basic map consisting
1384 * of the valid constraints in both basic maps together
1385 * with all wrapping constraints
1387 * 7. one of the basic maps extends beyond the other by at most one.
1388 * Moreover, the facets corresponding to the cut constraints and
1389 * the pieces of the other basic map at offset one from these cut
1390 * constraints can be wrapped around their ridges to include
1391 * the union of the two basic maps
1392 * => the pair can be replaced by a basic map consisting
1393 * of the valid constraints in both basic maps together
1394 * with all wrapping constraints
1396 * 8. the two basic maps live in adjacent hyperplanes. In principle
1397 * such sets can always be combined through wrapping, but we impose
1398 * that there is only one such pair, to avoid overeager coalescing.
1400 * Throughout the computation, we maintain a collection of tableaus
1401 * corresponding to the basic maps. When the basic maps are dropped
1402 * or combined, the tableaus are modified accordingly.
1404 static enum isl_change coalesce_local_pair(int i, int j,
1405 struct isl_coalesce_info *info)
1407 enum isl_change change = isl_change_none;
1409 info[i].eq = info[i].ineq = NULL;
1410 info[j].eq = info[j].ineq = NULL;
1412 info[i].eq = eq_status_in(info[i].bmap, info[j].tab);
1413 if (info[i].bmap->n_eq && !info[i].eq)
1414 goto error;
1415 if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ERROR))
1416 goto error;
1417 if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_SEPARATE))
1418 goto done;
1420 info[j].eq = eq_status_in(info[j].bmap, info[i].tab);
1421 if (info[j].bmap->n_eq && !info[j].eq)
1422 goto error;
1423 if (any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_ERROR))
1424 goto error;
1425 if (any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_SEPARATE))
1426 goto done;
1428 info[i].ineq = ineq_status_in(info[i].bmap, info[i].tab, info[j].tab);
1429 if (info[i].bmap->n_ineq && !info[i].ineq)
1430 goto error;
1431 if (any(info[i].ineq, info[i].bmap->n_ineq, STATUS_ERROR))
1432 goto error;
1433 if (any(info[i].ineq, info[i].bmap->n_ineq, STATUS_SEPARATE))
1434 goto done;
1436 info[j].ineq = ineq_status_in(info[j].bmap, info[j].tab, info[i].tab);
1437 if (info[j].bmap->n_ineq && !info[j].ineq)
1438 goto error;
1439 if (any(info[j].ineq, info[j].bmap->n_ineq, STATUS_ERROR))
1440 goto error;
1441 if (any(info[j].ineq, info[j].bmap->n_ineq, STATUS_SEPARATE))
1442 goto done;
1444 if (all(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_VALID) &&
1445 all(info[i].ineq, info[i].bmap->n_ineq, STATUS_VALID)) {
1446 drop(&info[j]);
1447 change = isl_change_drop_second;
1448 } else if (all(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_VALID) &&
1449 all(info[j].ineq, info[j].bmap->n_ineq, STATUS_VALID)) {
1450 drop(&info[i]);
1451 change = isl_change_drop_first;
1452 } else if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ADJ_EQ)) {
1453 change = check_eq_adj_eq(i, j, info);
1454 } else if (any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_ADJ_EQ)) {
1455 change = check_eq_adj_eq(j, i, info);
1456 } else if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ADJ_INEQ) ||
1457 any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_ADJ_INEQ)) {
1458 change = check_adj_eq(i, j, info);
1459 } else if (any(info[i].ineq, info[i].bmap->n_ineq, STATUS_ADJ_EQ) ||
1460 any(info[j].ineq, info[j].bmap->n_ineq, STATUS_ADJ_EQ)) {
1461 /* Can't happen */
1462 /* BAD ADJ INEQ */
1463 } else if (any(info[i].ineq, info[i].bmap->n_ineq, STATUS_ADJ_INEQ) ||
1464 any(info[j].ineq, info[j].bmap->n_ineq, STATUS_ADJ_INEQ)) {
1465 change = check_adj_ineq(i, j, info);
1466 } else {
1467 if (!any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_CUT) &&
1468 !any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_CUT))
1469 change = check_facets(i, j, info);
1470 if (change == isl_change_none)
1471 change = check_wrap(i, j, info);
1474 done:
1475 free(info[i].eq);
1476 free(info[j].eq);
1477 free(info[i].ineq);
1478 free(info[j].ineq);
1479 return change;
1480 error:
1481 free(info[i].eq);
1482 free(info[j].eq);
1483 free(info[i].ineq);
1484 free(info[j].ineq);
1485 return isl_change_error;
1488 /* Do the two basic maps live in the same local space, i.e.,
1489 * do they have the same (known) divs?
1490 * If either basic map has any unknown divs, then we can only assume
1491 * that they do not live in the same local space.
1493 static int same_divs(__isl_keep isl_basic_map *bmap1,
1494 __isl_keep isl_basic_map *bmap2)
1496 int i;
1497 int known;
1498 int total;
1500 if (!bmap1 || !bmap2)
1501 return -1;
1502 if (bmap1->n_div != bmap2->n_div)
1503 return 0;
1505 if (bmap1->n_div == 0)
1506 return 1;
1508 known = isl_basic_map_divs_known(bmap1);
1509 if (known < 0 || !known)
1510 return known;
1511 known = isl_basic_map_divs_known(bmap2);
1512 if (known < 0 || !known)
1513 return known;
1515 total = isl_basic_map_total_dim(bmap1);
1516 for (i = 0; i < bmap1->n_div; ++i)
1517 if (!isl_seq_eq(bmap1->div[i], bmap2->div[i], 2 + total))
1518 return 0;
1520 return 1;
1523 /* Does "bmap" contain the basic map represented by the tableau "tab"
1524 * after expanding the divs of "bmap" to match those of "tab"?
1525 * The expansion is performed using the divs "div" and expansion "exp"
1526 * computed by the caller.
1527 * Then we check if all constraints of the expanded "bmap" are valid for "tab".
1529 static int contains_with_expanded_divs(__isl_keep isl_basic_map *bmap,
1530 struct isl_tab *tab, __isl_keep isl_mat *div, int *exp)
1532 int superset = 0;
1533 int *eq_i = NULL;
1534 int *ineq_i = NULL;
1536 bmap = isl_basic_map_copy(bmap);
1537 bmap = isl_basic_set_expand_divs(bmap, isl_mat_copy(div), exp);
1539 if (!bmap)
1540 goto error;
1542 eq_i = eq_status_in(bmap, tab);
1543 if (bmap->n_eq && !eq_i)
1544 goto error;
1545 if (any(eq_i, 2 * bmap->n_eq, STATUS_ERROR))
1546 goto error;
1547 if (any(eq_i, 2 * bmap->n_eq, STATUS_SEPARATE))
1548 goto done;
1550 ineq_i = ineq_status_in(bmap, NULL, tab);
1551 if (bmap->n_ineq && !ineq_i)
1552 goto error;
1553 if (any(ineq_i, bmap->n_ineq, STATUS_ERROR))
1554 goto error;
1555 if (any(ineq_i, bmap->n_ineq, STATUS_SEPARATE))
1556 goto done;
1558 if (all(eq_i, 2 * bmap->n_eq, STATUS_VALID) &&
1559 all(ineq_i, bmap->n_ineq, STATUS_VALID))
1560 superset = 1;
1562 done:
1563 isl_basic_map_free(bmap);
1564 free(eq_i);
1565 free(ineq_i);
1566 return superset;
1567 error:
1568 isl_basic_map_free(bmap);
1569 free(eq_i);
1570 free(ineq_i);
1571 return -1;
1574 /* Does "bmap_i" contain the basic map represented by "info_j"
1575 * after aligning the divs of "bmap_i" to those of "info_j".
1576 * Note that this can only succeed if the number of divs of "bmap_i"
1577 * is smaller than (or equal to) the number of divs of "info_j".
1579 * We first check if the divs of "bmap_i" are all known and form a subset
1580 * of those of "bmap_j". If so, we pass control over to
1581 * contains_with_expanded_divs.
1583 static int contains_after_aligning_divs(__isl_keep isl_basic_map *bmap_i,
1584 struct isl_coalesce_info *info_j)
1586 int known;
1587 isl_mat *div_i, *div_j, *div;
1588 int *exp1 = NULL;
1589 int *exp2 = NULL;
1590 isl_ctx *ctx;
1591 int subset;
1593 known = isl_basic_map_divs_known(bmap_i);
1594 if (known < 0 || !known)
1595 return known;
1597 ctx = isl_basic_map_get_ctx(bmap_i);
1599 div_i = isl_basic_map_get_divs(bmap_i);
1600 div_j = isl_basic_map_get_divs(info_j->bmap);
1602 if (!div_i || !div_j)
1603 goto error;
1605 exp1 = isl_alloc_array(ctx, int, div_i->n_row);
1606 exp2 = isl_alloc_array(ctx, int, div_j->n_row);
1607 if ((div_i->n_row && !exp1) || (div_j->n_row && !exp2))
1608 goto error;
1610 div = isl_merge_divs(div_i, div_j, exp1, exp2);
1611 if (!div)
1612 goto error;
1614 if (div->n_row == div_j->n_row)
1615 subset = contains_with_expanded_divs(bmap_i,
1616 info_j->tab, div, exp1);
1617 else
1618 subset = 0;
1620 isl_mat_free(div);
1622 isl_mat_free(div_i);
1623 isl_mat_free(div_j);
1625 free(exp2);
1626 free(exp1);
1628 return subset;
1629 error:
1630 isl_mat_free(div_i);
1631 isl_mat_free(div_j);
1632 free(exp1);
1633 free(exp2);
1634 return -1;
1637 /* Check if the basic map "j" is a subset of basic map "i",
1638 * if "i" has fewer divs that "j".
1639 * If so, remove basic map "j".
1641 * If the two basic maps have the same number of divs, then
1642 * they must necessarily be different. Otherwise, we would have
1643 * called coalesce_local_pair. We therefore don't try anything
1644 * in this case.
1646 static int coalesced_subset(int i, int j, struct isl_coalesce_info *info)
1648 int superset;
1650 if (info[i].bmap->n_div >= info[j].bmap->n_div)
1651 return 0;
1653 superset = contains_after_aligning_divs(info[i].bmap, &info[j]);
1654 if (superset < 0)
1655 return -1;
1656 if (superset)
1657 drop(&info[j]);
1659 return superset;
1662 /* Check if one of the basic maps is a subset of the other and, if so,
1663 * drop the subset.
1664 * Note that we only perform any test if the number of divs is different
1665 * in the two basic maps. In case the number of divs is the same,
1666 * we have already established that the divs are different
1667 * in the two basic maps.
1668 * In particular, if the number of divs of basic map i is smaller than
1669 * the number of divs of basic map j, then we check if j is a subset of i
1670 * and vice versa.
1672 static enum isl_change check_coalesce_subset(int i, int j,
1673 struct isl_coalesce_info *info)
1675 int changed;
1677 changed = coalesced_subset(i, j, info);
1678 if (changed < 0 || changed)
1679 return changed < 0 ? isl_change_error : isl_change_drop_second;
1681 changed = coalesced_subset(j, i, info);
1682 if (changed < 0 || changed)
1683 return changed < 0 ? isl_change_error : isl_change_drop_first;
1685 return isl_change_none;
1688 /* Check if the union of the given pair of basic maps
1689 * can be represented by a single basic map.
1690 * If so, replace the pair by the single basic map and return
1691 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
1692 * Otherwise, return isl_change_none.
1694 * We first check if the two basic maps live in the same local space.
1695 * If so, we do the complete check. Otherwise, we check if one is
1696 * an obvious subset of the other.
1698 static enum isl_change coalesce_pair(int i, int j,
1699 struct isl_coalesce_info *info)
1701 int same;
1703 same = same_divs(info[i].bmap, info[j].bmap);
1704 if (same < 0)
1705 return isl_change_error;
1706 if (same)
1707 return coalesce_local_pair(i, j, info);
1709 return check_coalesce_subset(i, j, info);
1712 /* Pairwise coalesce the basic maps described by the "n" elements of "info",
1713 * skipping basic maps that have been removed (either before or within
1714 * this function).
1716 * For each basic map i, we check if it can be coalesced with respect
1717 * to any previously considered basic map j.
1718 * If i gets dropped (because it was a subset of some j), then
1719 * we can move on to the next basic map.
1720 * If j gets dropped, we need to continue checking against the other
1721 * previously considered basic maps.
1722 * If the two basic maps got fused, then we recheck the fused basic map
1723 * against the previously considered basic maps.
1725 static int coalesce(isl_ctx *ctx, int n, struct isl_coalesce_info *info)
1727 int i, j;
1729 for (i = n - 2; i >= 0; --i) {
1730 if (info[i].removed)
1731 continue;
1732 for (j = i + 1; j < n; ++j) {
1733 enum isl_change changed;
1735 if (info[j].removed)
1736 continue;
1737 if (info[i].removed)
1738 isl_die(ctx, isl_error_internal,
1739 "basic map unexpectedly removed",
1740 return -1);
1741 changed = coalesce_pair(i, j, info);
1742 switch (changed) {
1743 case isl_change_error:
1744 return -1;
1745 case isl_change_none:
1746 case isl_change_drop_second:
1747 continue;
1748 case isl_change_drop_first:
1749 j = n;
1750 break;
1751 case isl_change_fuse:
1752 j = i;
1753 break;
1758 return 0;
1761 /* Update the basic maps in "map" based on the information in "info".
1762 * In particular, remove the basic maps that have been marked removed and
1763 * update the others based on the information in the corresponding tableau.
1764 * Since we detected implicit equalities without calling
1765 * isl_basic_map_gauss, we need to do it now.
1767 static __isl_give isl_map *update_basic_maps(__isl_take isl_map *map,
1768 int n, struct isl_coalesce_info *info)
1770 int i;
1772 if (!map)
1773 return NULL;
1775 for (i = n - 1; i >= 0; --i) {
1776 if (info[i].removed) {
1777 isl_basic_map_free(map->p[i]);
1778 if (i != map->n - 1)
1779 map->p[i] = map->p[map->n - 1];
1780 map->n--;
1781 continue;
1784 info[i].bmap = isl_basic_map_update_from_tab(info[i].bmap,
1785 info[i].tab);
1786 info[i].bmap = isl_basic_map_gauss(info[i].bmap, NULL);
1787 info[i].bmap = isl_basic_map_finalize(info[i].bmap);
1788 if (!info[i].bmap)
1789 return isl_map_free(map);
1790 ISL_F_SET(info[i].bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1791 ISL_F_SET(info[i].bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1792 isl_basic_map_free(map->p[i]);
1793 map->p[i] = info[i].bmap;
1794 info[i].bmap = NULL;
1797 return map;
1800 /* For each pair of basic maps in the map, check if the union of the two
1801 * can be represented by a single basic map.
1802 * If so, replace the pair by the single basic map and start over.
1804 * Since we are constructing the tableaus of the basic maps anyway,
1805 * we exploit them to detect implicit equalities and redundant constraints.
1806 * This also helps the coalescing as it can ignore the redundant constraints.
1807 * In order to avoid confusion, we make all implicit equalities explicit
1808 * in the basic maps. We don't call isl_basic_map_gauss, though,
1809 * as that may affect the number of constraints.
1810 * This means that we have to call isl_basic_map_gauss at the end
1811 * of the computation (in update_basic_maps) to ensure that
1812 * the basic maps are not left in an unexpected state.
1814 struct isl_map *isl_map_coalesce(struct isl_map *map)
1816 int i;
1817 unsigned n;
1818 isl_ctx *ctx;
1819 struct isl_coalesce_info *info = NULL;
1821 map = isl_map_remove_empty_parts(map);
1822 if (!map)
1823 return NULL;
1825 if (map->n <= 1)
1826 return map;
1828 ctx = isl_map_get_ctx(map);
1829 map = isl_map_sort_divs(map);
1830 map = isl_map_cow(map);
1832 if (!map)
1833 return NULL;
1835 n = map->n;
1837 info = isl_calloc_array(map->ctx, struct isl_coalesce_info, n);
1838 if (!info)
1839 goto error;
1841 for (i = 0; i < map->n; ++i) {
1842 info[i].bmap = isl_basic_map_copy(map->p[i]);
1843 info[i].tab = isl_tab_from_basic_map(info[i].bmap, 0);
1844 if (!info[i].tab)
1845 goto error;
1846 if (!ISL_F_ISSET(info[i].bmap, ISL_BASIC_MAP_NO_IMPLICIT))
1847 if (isl_tab_detect_implicit_equalities(info[i].tab) < 0)
1848 goto error;
1849 info[i].bmap = isl_tab_make_equalities_explicit(info[i].tab,
1850 info[i].bmap);
1851 if (!info[i].bmap)
1852 goto error;
1853 if (!ISL_F_ISSET(info[i].bmap, ISL_BASIC_MAP_NO_REDUNDANT))
1854 if (isl_tab_detect_redundant(info[i].tab) < 0)
1855 goto error;
1857 for (i = map->n - 1; i >= 0; --i)
1858 if (info[i].tab->empty)
1859 drop(&info[i]);
1861 if (coalesce(ctx, n, info) < 0)
1862 goto error;
1864 map = update_basic_maps(map, n, info);
1866 clear_coalesce_info(n, info);
1868 return map;
1869 error:
1870 clear_coalesce_info(n, info);
1871 isl_map_free(map);
1872 return NULL;
1875 /* For each pair of basic sets in the set, check if the union of the two
1876 * can be represented by a single basic set.
1877 * If so, replace the pair by the single basic set and start over.
1879 struct isl_set *isl_set_coalesce(struct isl_set *set)
1881 return (struct isl_set *)isl_map_coalesce((struct isl_map *)set);