isl_scheduler.c: move sort_statements down
[isl.git] / isl_coalesce.c
blob0c26cf0891a56cb49b0d276c567d29899e2619e5
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>
25 #include <isl_aff_private.h>
27 #define STATUS_ERROR -1
28 #define STATUS_REDUNDANT 1
29 #define STATUS_VALID 2
30 #define STATUS_SEPARATE 3
31 #define STATUS_CUT 4
32 #define STATUS_ADJ_EQ 5
33 #define STATUS_ADJ_INEQ 6
35 static int status_in(isl_int *ineq, struct isl_tab *tab)
37 enum isl_ineq_type type = isl_tab_ineq_type(tab, ineq);
38 switch (type) {
39 default:
40 case isl_ineq_error: return STATUS_ERROR;
41 case isl_ineq_redundant: return STATUS_VALID;
42 case isl_ineq_separate: return STATUS_SEPARATE;
43 case isl_ineq_cut: return STATUS_CUT;
44 case isl_ineq_adj_eq: return STATUS_ADJ_EQ;
45 case isl_ineq_adj_ineq: return STATUS_ADJ_INEQ;
49 /* Compute the position of the equalities of basic map "bmap_i"
50 * with respect to the basic map represented by "tab_j".
51 * The resulting array has twice as many entries as the number
52 * of equalities corresponding to the two inequalties to which
53 * each equality corresponds.
55 static int *eq_status_in(__isl_keep isl_basic_map *bmap_i,
56 struct isl_tab *tab_j)
58 int k, l;
59 int *eq = isl_calloc_array(bmap_i->ctx, int, 2 * bmap_i->n_eq);
60 unsigned dim;
62 if (!eq)
63 return NULL;
65 dim = isl_basic_map_total_dim(bmap_i);
66 for (k = 0; k < bmap_i->n_eq; ++k) {
67 for (l = 0; l < 2; ++l) {
68 isl_seq_neg(bmap_i->eq[k], bmap_i->eq[k], 1+dim);
69 eq[2 * k + l] = status_in(bmap_i->eq[k], tab_j);
70 if (eq[2 * k + l] == STATUS_ERROR)
71 goto error;
73 if (eq[2 * k] == STATUS_SEPARATE ||
74 eq[2 * k + 1] == STATUS_SEPARATE)
75 break;
78 return eq;
79 error:
80 free(eq);
81 return NULL;
84 /* Compute the position of the inequalities of basic map "bmap_i"
85 * (also represented by "tab_i", if not NULL) with respect to the basic map
86 * represented by "tab_j".
88 static int *ineq_status_in(__isl_keep isl_basic_map *bmap_i,
89 struct isl_tab *tab_i, struct isl_tab *tab_j)
91 int k;
92 unsigned n_eq = bmap_i->n_eq;
93 int *ineq = isl_calloc_array(bmap_i->ctx, int, bmap_i->n_ineq);
95 if (!ineq)
96 return NULL;
98 for (k = 0; k < bmap_i->n_ineq; ++k) {
99 if (tab_i && isl_tab_is_redundant(tab_i, n_eq + k)) {
100 ineq[k] = STATUS_REDUNDANT;
101 continue;
103 ineq[k] = status_in(bmap_i->ineq[k], tab_j);
104 if (ineq[k] == STATUS_ERROR)
105 goto error;
106 if (ineq[k] == STATUS_SEPARATE)
107 break;
110 return ineq;
111 error:
112 free(ineq);
113 return NULL;
116 static int any(int *con, unsigned len, int status)
118 int i;
120 for (i = 0; i < len ; ++i)
121 if (con[i] == status)
122 return 1;
123 return 0;
126 static int count(int *con, unsigned len, int status)
128 int i;
129 int c = 0;
131 for (i = 0; i < len ; ++i)
132 if (con[i] == status)
133 c++;
134 return c;
137 static int all(int *con, unsigned len, int status)
139 int i;
141 for (i = 0; i < len ; ++i) {
142 if (con[i] == STATUS_REDUNDANT)
143 continue;
144 if (con[i] != status)
145 return 0;
147 return 1;
150 /* Internal information associated to a basic map in a map
151 * that is to be coalesced by isl_map_coalesce.
153 * "bmap" is the basic map itself (or NULL if "removed" is set)
154 * "tab" is the corresponding tableau (or NULL if "removed" is set)
155 * "hull_hash" identifies the affine space in which "bmap" lives.
156 * "removed" is set if this basic map has been removed from the map
157 * "simplify" is set if this basic map may have some unknown integer
158 * divisions that were not present in the input basic maps. The basic
159 * map should then be simplified such that we may be able to find
160 * a definition among the constraints.
162 * "eq" and "ineq" are only set if we are currently trying to coalesce
163 * this basic map with another basic map, in which case they represent
164 * the position of the inequalities of this basic map with respect to
165 * the other basic map. The number of elements in the "eq" array
166 * is twice the number of equalities in the "bmap", corresponding
167 * to the two inequalities that make up each equality.
169 struct isl_coalesce_info {
170 isl_basic_map *bmap;
171 struct isl_tab *tab;
172 uint32_t hull_hash;
173 int removed;
174 int simplify;
175 int *eq;
176 int *ineq;
179 /* Compute the hash of the (apparent) affine hull of info->bmap (with
180 * the existentially quantified variables removed) and store it
181 * in info->hash.
183 static int coalesce_info_set_hull_hash(struct isl_coalesce_info *info)
185 isl_basic_map *hull;
186 unsigned n_div;
188 hull = isl_basic_map_copy(info->bmap);
189 hull = isl_basic_map_plain_affine_hull(hull);
190 n_div = isl_basic_map_dim(hull, isl_dim_div);
191 hull = isl_basic_map_drop_constraints_involving_dims(hull,
192 isl_dim_div, 0, n_div);
193 info->hull_hash = isl_basic_map_get_hash(hull);
194 isl_basic_map_free(hull);
196 return hull ? 0 : -1;
199 /* Free all the allocated memory in an array
200 * of "n" isl_coalesce_info elements.
202 static void clear_coalesce_info(int n, struct isl_coalesce_info *info)
204 int i;
206 if (!info)
207 return;
209 for (i = 0; i < n; ++i) {
210 isl_basic_map_free(info[i].bmap);
211 isl_tab_free(info[i].tab);
214 free(info);
217 /* Drop the basic map represented by "info".
218 * That is, clear the memory associated to the entry and
219 * mark it as having been removed.
221 static void drop(struct isl_coalesce_info *info)
223 info->bmap = isl_basic_map_free(info->bmap);
224 isl_tab_free(info->tab);
225 info->tab = NULL;
226 info->removed = 1;
229 /* Exchange the information in "info1" with that in "info2".
231 static void exchange(struct isl_coalesce_info *info1,
232 struct isl_coalesce_info *info2)
234 struct isl_coalesce_info info;
236 info = *info1;
237 *info1 = *info2;
238 *info2 = info;
241 /* This type represents the kind of change that has been performed
242 * while trying to coalesce two basic maps.
244 * isl_change_none: nothing was changed
245 * isl_change_drop_first: the first basic map was removed
246 * isl_change_drop_second: the second basic map was removed
247 * isl_change_fuse: the two basic maps were replaced by a new basic map.
249 enum isl_change {
250 isl_change_error = -1,
251 isl_change_none = 0,
252 isl_change_drop_first,
253 isl_change_drop_second,
254 isl_change_fuse,
257 /* Update "change" based on an interchange of the first and the second
258 * basic map. That is, interchange isl_change_drop_first and
259 * isl_change_drop_second.
261 static enum isl_change invert_change(enum isl_change change)
263 switch (change) {
264 case isl_change_error:
265 return isl_change_error;
266 case isl_change_none:
267 return isl_change_none;
268 case isl_change_drop_first:
269 return isl_change_drop_second;
270 case isl_change_drop_second:
271 return isl_change_drop_first;
272 case isl_change_fuse:
273 return isl_change_fuse;
277 /* Add the valid constraints of the basic map represented by "info"
278 * to "bmap". "len" is the size of the constraints.
279 * If only one of the pair of inequalities that make up an equality
280 * is valid, then add that inequality.
282 static __isl_give isl_basic_map *add_valid_constraints(
283 __isl_take isl_basic_map *bmap, struct isl_coalesce_info *info,
284 unsigned len)
286 int k, l;
288 if (!bmap)
289 return NULL;
291 for (k = 0; k < info->bmap->n_eq; ++k) {
292 if (info->eq[2 * k] == STATUS_VALID &&
293 info->eq[2 * k + 1] == STATUS_VALID) {
294 l = isl_basic_map_alloc_equality(bmap);
295 if (l < 0)
296 return isl_basic_map_free(bmap);
297 isl_seq_cpy(bmap->eq[l], info->bmap->eq[k], len);
298 } else if (info->eq[2 * k] == STATUS_VALID) {
299 l = isl_basic_map_alloc_inequality(bmap);
300 if (l < 0)
301 return isl_basic_map_free(bmap);
302 isl_seq_neg(bmap->ineq[l], info->bmap->eq[k], len);
303 } else if (info->eq[2 * k + 1] == STATUS_VALID) {
304 l = isl_basic_map_alloc_inequality(bmap);
305 if (l < 0)
306 return isl_basic_map_free(bmap);
307 isl_seq_cpy(bmap->ineq[l], info->bmap->eq[k], len);
311 for (k = 0; k < info->bmap->n_ineq; ++k) {
312 if (info->ineq[k] != STATUS_VALID)
313 continue;
314 l = isl_basic_map_alloc_inequality(bmap);
315 if (l < 0)
316 return isl_basic_map_free(bmap);
317 isl_seq_cpy(bmap->ineq[l], info->bmap->ineq[k], len);
320 return bmap;
323 /* Is "bmap" defined by a number of (non-redundant) constraints that
324 * is greater than the number of constraints of basic maps i and j combined?
325 * Equalities are counted as two inequalities.
327 static int number_of_constraints_increases(int i, int j,
328 struct isl_coalesce_info *info,
329 __isl_keep isl_basic_map *bmap, struct isl_tab *tab)
331 int k, n_old, n_new;
333 n_old = 2 * info[i].bmap->n_eq + info[i].bmap->n_ineq;
334 n_old += 2 * info[j].bmap->n_eq + info[j].bmap->n_ineq;
336 n_new = 2 * bmap->n_eq;
337 for (k = 0; k < bmap->n_ineq; ++k)
338 if (!isl_tab_is_redundant(tab, bmap->n_eq + k))
339 ++n_new;
341 return n_new > n_old;
344 /* Replace the pair of basic maps i and j by the basic map bounded
345 * by the valid constraints in both basic maps and the constraints
346 * in extra (if not NULL).
347 * Place the fused basic map in the position that is the smallest of i and j.
349 * If "detect_equalities" is set, then look for equalities encoded
350 * as pairs of inequalities.
351 * If "check_number" is set, then the original basic maps are only
352 * replaced if the total number of constraints does not increase.
353 * While the number of integer divisions in the two basic maps
354 * is assumed to be the same, the actual definitions may be different.
355 * We only copy the definition from one of the basic map if it is
356 * the same as that of the other basic map. Otherwise, we mark
357 * the integer division as unknown and schedule for the basic map
358 * to be simplified in an attempt to recover the integer division definition.
360 static enum isl_change fuse(int i, int j, struct isl_coalesce_info *info,
361 __isl_keep isl_mat *extra, int detect_equalities, int check_number)
363 int k, l;
364 struct isl_basic_map *fused = NULL;
365 struct isl_tab *fused_tab = NULL;
366 unsigned total = isl_basic_map_total_dim(info[i].bmap);
367 unsigned extra_rows = extra ? extra->n_row : 0;
368 unsigned n_eq, n_ineq;
370 if (j < i)
371 return fuse(j, i, info, extra, detect_equalities, check_number);
373 n_eq = info[i].bmap->n_eq + info[j].bmap->n_eq;
374 n_ineq = info[i].bmap->n_ineq + info[j].bmap->n_ineq;
375 fused = isl_basic_map_alloc_space(isl_space_copy(info[i].bmap->dim),
376 info[i].bmap->n_div, n_eq, n_eq + n_ineq + extra_rows);
377 fused = add_valid_constraints(fused, &info[i], 1 + total);
378 fused = add_valid_constraints(fused, &info[j], 1 + total);
379 if (!fused)
380 goto error;
382 for (k = 0; k < info[i].bmap->n_div; ++k) {
383 int l = isl_basic_map_alloc_div(fused);
384 if (l < 0)
385 goto error;
386 if (isl_seq_eq(info[i].bmap->div[k], info[j].bmap->div[k],
387 1 + 1 + total)) {
388 isl_seq_cpy(fused->div[l], info[i].bmap->div[k],
389 1 + 1 + total);
390 } else {
391 isl_int_set_si(fused->div[l][0], 0);
392 info[i].simplify = 1;
396 for (k = 0; k < extra_rows; ++k) {
397 l = isl_basic_map_alloc_inequality(fused);
398 if (l < 0)
399 goto error;
400 isl_seq_cpy(fused->ineq[l], extra->row[k], 1 + total);
403 if (detect_equalities)
404 fused = isl_basic_map_detect_inequality_pairs(fused, NULL);
405 fused = isl_basic_map_gauss(fused, NULL);
406 ISL_F_SET(fused, ISL_BASIC_MAP_FINAL);
407 if (ISL_F_ISSET(info[i].bmap, ISL_BASIC_MAP_RATIONAL) &&
408 ISL_F_ISSET(info[j].bmap, ISL_BASIC_MAP_RATIONAL))
409 ISL_F_SET(fused, ISL_BASIC_MAP_RATIONAL);
411 fused_tab = isl_tab_from_basic_map(fused, 0);
412 if (isl_tab_detect_redundant(fused_tab) < 0)
413 goto error;
415 if (check_number &&
416 number_of_constraints_increases(i, j, info, fused, fused_tab)) {
417 isl_tab_free(fused_tab);
418 isl_basic_map_free(fused);
419 return isl_change_none;
422 info[i].simplify |= info[j].simplify;
423 isl_basic_map_free(info[i].bmap);
424 info[i].bmap = fused;
425 isl_tab_free(info[i].tab);
426 info[i].tab = fused_tab;
427 drop(&info[j]);
429 return isl_change_fuse;
430 error:
431 isl_tab_free(fused_tab);
432 isl_basic_map_free(fused);
433 return isl_change_error;
436 /* Given a pair of basic maps i and j such that all constraints are either
437 * "valid" or "cut", check if the facets corresponding to the "cut"
438 * constraints of i lie entirely within basic map j.
439 * If so, replace the pair by the basic map consisting of the valid
440 * constraints in both basic maps.
441 * Checking whether the facet lies entirely within basic map j
442 * is performed by checking whether the constraints of basic map j
443 * are valid for the facet. These tests are performed on a rational
444 * tableau to avoid the theoretical possibility that a constraint
445 * that was considered to be a cut constraint for the entire basic map i
446 * happens to be considered to be a valid constraint for the facet,
447 * even though it cuts off the same rational points.
449 * To see that we are not introducing any extra points, call the
450 * two basic maps A and B and the resulting map U and let x
451 * be an element of U \setminus ( A \cup B ).
452 * A line connecting x with an element of A \cup B meets a facet F
453 * of either A or B. Assume it is a facet of B and let c_1 be
454 * the corresponding facet constraint. We have c_1(x) < 0 and
455 * so c_1 is a cut constraint. This implies that there is some
456 * (possibly rational) point x' satisfying the constraints of A
457 * and the opposite of c_1 as otherwise c_1 would have been marked
458 * valid for A. The line connecting x and x' meets a facet of A
459 * in a (possibly rational) point that also violates c_1, but this
460 * is impossible since all cut constraints of B are valid for all
461 * cut facets of A.
462 * In case F is a facet of A rather than B, then we can apply the
463 * above reasoning to find a facet of B separating x from A \cup B first.
465 static enum isl_change check_facets(int i, int j,
466 struct isl_coalesce_info *info)
468 int k, l;
469 struct isl_tab_undo *snap, *snap2;
470 unsigned n_eq = info[i].bmap->n_eq;
472 snap = isl_tab_snap(info[i].tab);
473 if (isl_tab_mark_rational(info[i].tab) < 0)
474 return isl_change_error;
475 snap2 = isl_tab_snap(info[i].tab);
477 for (k = 0; k < info[i].bmap->n_ineq; ++k) {
478 if (info[i].ineq[k] != STATUS_CUT)
479 continue;
480 if (isl_tab_select_facet(info[i].tab, n_eq + k) < 0)
481 return isl_change_error;
482 for (l = 0; l < info[j].bmap->n_ineq; ++l) {
483 int stat;
484 if (info[j].ineq[l] != STATUS_CUT)
485 continue;
486 stat = status_in(info[j].bmap->ineq[l], info[i].tab);
487 if (stat < 0)
488 return isl_change_error;
489 if (stat != STATUS_VALID)
490 break;
492 if (isl_tab_rollback(info[i].tab, snap2) < 0)
493 return isl_change_error;
494 if (l < info[j].bmap->n_ineq)
495 break;
498 if (k < info[i].bmap->n_ineq) {
499 if (isl_tab_rollback(info[i].tab, snap) < 0)
500 return isl_change_error;
501 return isl_change_none;
503 return fuse(i, j, info, NULL, 0, 0);
506 /* Check if info->bmap contains the basic map represented
507 * by the tableau "tab".
508 * For each equality, we check both the constraint itself
509 * (as an inequality) and its negation. Make sure the
510 * equality is returned to its original state before returning.
512 static int contains(struct isl_coalesce_info *info, struct isl_tab *tab)
514 int k;
515 unsigned dim;
516 isl_basic_map *bmap = info->bmap;
518 dim = isl_basic_map_total_dim(bmap);
519 for (k = 0; k < bmap->n_eq; ++k) {
520 int stat;
521 isl_seq_neg(bmap->eq[k], bmap->eq[k], 1 + dim);
522 stat = status_in(bmap->eq[k], tab);
523 isl_seq_neg(bmap->eq[k], bmap->eq[k], 1 + dim);
524 if (stat < 0)
525 return -1;
526 if (stat != STATUS_VALID)
527 return 0;
528 stat = status_in(bmap->eq[k], tab);
529 if (stat < 0)
530 return -1;
531 if (stat != STATUS_VALID)
532 return 0;
535 for (k = 0; k < bmap->n_ineq; ++k) {
536 int stat;
537 if (info->ineq[k] == STATUS_REDUNDANT)
538 continue;
539 stat = status_in(bmap->ineq[k], tab);
540 if (stat < 0)
541 return -1;
542 if (stat != STATUS_VALID)
543 return 0;
545 return 1;
548 /* Basic map "i" has an inequality (say "k") that is adjacent
549 * to some inequality of basic map "j". All the other inequalities
550 * are valid for "j".
551 * Check if basic map "j" forms an extension of basic map "i".
553 * Note that this function is only called if some of the equalities or
554 * inequalities of basic map "j" do cut basic map "i". The function is
555 * correct even if there are no such cut constraints, but in that case
556 * the additional checks performed by this function are overkill.
558 * In particular, we replace constraint k, say f >= 0, by constraint
559 * f <= -1, add the inequalities of "j" that are valid for "i"
560 * and check if the result is a subset of basic map "j".
561 * If so, then we know that this result is exactly equal to basic map "j"
562 * since all its constraints are valid for basic map "j".
563 * By combining the valid constraints of "i" (all equalities and all
564 * inequalities except "k") and the valid constraints of "j" we therefore
565 * obtain a basic map that is equal to their union.
566 * In this case, there is no need to perform a rollback of the tableau
567 * since it is going to be destroyed in fuse().
570 * |\__ |\__
571 * | \__ | \__
572 * | \_ => | \__
573 * |_______| _ |_________\
576 * |\ |\
577 * | \ | \
578 * | \ | \
579 * | | | \
580 * | ||\ => | \
581 * | || \ | \
582 * | || | | |
583 * |__||_/ |_____/
585 static enum isl_change is_adj_ineq_extension(int i, int j,
586 struct isl_coalesce_info *info)
588 int k;
589 struct isl_tab_undo *snap;
590 unsigned n_eq = info[i].bmap->n_eq;
591 unsigned total = isl_basic_map_total_dim(info[i].bmap);
592 int r;
593 int super;
595 if (isl_tab_extend_cons(info[i].tab, 1 + info[j].bmap->n_ineq) < 0)
596 return isl_change_error;
598 for (k = 0; k < info[i].bmap->n_ineq; ++k)
599 if (info[i].ineq[k] == STATUS_ADJ_INEQ)
600 break;
601 if (k >= info[i].bmap->n_ineq)
602 isl_die(isl_basic_map_get_ctx(info[i].bmap), isl_error_internal,
603 "info[i].ineq should have exactly one STATUS_ADJ_INEQ",
604 return isl_change_error);
606 snap = isl_tab_snap(info[i].tab);
608 if (isl_tab_unrestrict(info[i].tab, n_eq + k) < 0)
609 return isl_change_error;
611 isl_seq_neg(info[i].bmap->ineq[k], info[i].bmap->ineq[k], 1 + total);
612 isl_int_sub_ui(info[i].bmap->ineq[k][0], info[i].bmap->ineq[k][0], 1);
613 r = isl_tab_add_ineq(info[i].tab, info[i].bmap->ineq[k]);
614 isl_seq_neg(info[i].bmap->ineq[k], info[i].bmap->ineq[k], 1 + total);
615 isl_int_sub_ui(info[i].bmap->ineq[k][0], info[i].bmap->ineq[k][0], 1);
616 if (r < 0)
617 return isl_change_error;
619 for (k = 0; k < info[j].bmap->n_ineq; ++k) {
620 if (info[j].ineq[k] != STATUS_VALID)
621 continue;
622 if (isl_tab_add_ineq(info[i].tab, info[j].bmap->ineq[k]) < 0)
623 return isl_change_error;
626 super = contains(&info[j], info[i].tab);
627 if (super < 0)
628 return isl_change_error;
629 if (super)
630 return fuse(i, j, info, NULL, 0, 0);
632 if (isl_tab_rollback(info[i].tab, snap) < 0)
633 return isl_change_error;
635 return isl_change_none;
639 /* Both basic maps have at least one inequality with and adjacent
640 * (but opposite) inequality in the other basic map.
641 * Check that there are no cut constraints and that there is only
642 * a single pair of adjacent inequalities.
643 * If so, we can replace the pair by a single basic map described
644 * by all but the pair of adjacent inequalities.
645 * Any additional points introduced lie strictly between the two
646 * adjacent hyperplanes and can therefore be integral.
648 * ____ _____
649 * / ||\ / \
650 * / || \ / \
651 * \ || \ => \ \
652 * \ || / \ /
653 * \___||_/ \_____/
655 * The test for a single pair of adjancent inequalities is important
656 * for avoiding the combination of two basic maps like the following
658 * /|
659 * / |
660 * /__|
661 * _____
662 * | |
663 * | |
664 * |___|
666 * If there are some cut constraints on one side, then we may
667 * still be able to fuse the two basic maps, but we need to perform
668 * some additional checks in is_adj_ineq_extension.
670 static enum isl_change check_adj_ineq(int i, int j,
671 struct isl_coalesce_info *info)
673 int count_i, count_j;
674 int cut_i, cut_j;
676 count_i = count(info[i].ineq, info[i].bmap->n_ineq, STATUS_ADJ_INEQ);
677 count_j = count(info[j].ineq, info[j].bmap->n_ineq, STATUS_ADJ_INEQ);
679 if (count_i != 1 && count_j != 1)
680 return isl_change_none;
682 cut_i = any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_CUT) ||
683 any(info[i].ineq, info[i].bmap->n_ineq, STATUS_CUT);
684 cut_j = any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_CUT) ||
685 any(info[j].ineq, info[j].bmap->n_ineq, STATUS_CUT);
687 if (!cut_i && !cut_j && count_i == 1 && count_j == 1)
688 return fuse(i, j, info, NULL, 0, 0);
690 if (count_i == 1 && !cut_i)
691 return is_adj_ineq_extension(i, j, info);
693 if (count_j == 1 && !cut_j)
694 return is_adj_ineq_extension(j, i, info);
696 return isl_change_none;
699 /* Basic map "i" has an inequality "k" that is adjacent to some equality
700 * of basic map "j". All the other inequalities are valid for "j".
701 * Check if basic map "j" forms an extension of basic map "i".
703 * In particular, we relax constraint "k", compute the corresponding
704 * facet and check whether it is included in the other basic map.
705 * If so, we know that relaxing the constraint extends the basic
706 * map with exactly the other basic map (we already know that this
707 * other basic map is included in the extension, because there
708 * were no "cut" inequalities in "i") and we can replace the
709 * two basic maps by this extension.
710 * Each integer division that does not have exactly the same
711 * definition in "i" and "j" is marked unknown and the basic map
712 * is scheduled to be simplified in an attempt to recover
713 * the integer division definition.
714 * Place this extension in the position that is the smallest of i and j.
715 * ____ _____
716 * / || / |
717 * / || / |
718 * \ || => \ |
719 * \ || \ |
720 * \___|| \____|
722 static enum isl_change is_adj_eq_extension(int i, int j, int k,
723 struct isl_coalesce_info *info)
725 int change = isl_change_none;
726 int super;
727 struct isl_tab_undo *snap, *snap2;
728 unsigned n_eq = info[i].bmap->n_eq;
730 if (isl_tab_is_equality(info[i].tab, n_eq + k))
731 return isl_change_none;
733 snap = isl_tab_snap(info[i].tab);
734 if (isl_tab_relax(info[i].tab, n_eq + k) < 0)
735 return isl_change_error;
736 snap2 = isl_tab_snap(info[i].tab);
737 if (isl_tab_select_facet(info[i].tab, n_eq + k) < 0)
738 return isl_change_error;
739 super = contains(&info[j], info[i].tab);
740 if (super < 0)
741 return isl_change_error;
742 if (super) {
743 int l;
744 unsigned total;
746 if (isl_tab_rollback(info[i].tab, snap2) < 0)
747 return isl_change_error;
748 info[i].bmap = isl_basic_map_cow(info[i].bmap);
749 if (!info[i].bmap)
750 return isl_change_error;
751 total = isl_basic_map_total_dim(info[i].bmap);
752 for (l = 0; l < info[i].bmap->n_div; ++l)
753 if (!isl_seq_eq(info[i].bmap->div[l],
754 info[j].bmap->div[l], 1 + 1 + total)) {
755 isl_int_set_si(info[i].bmap->div[l][0], 0);
756 info[i].simplify = 1;
758 isl_int_add_ui(info[i].bmap->ineq[k][0],
759 info[i].bmap->ineq[k][0], 1);
760 ISL_F_SET(info[i].bmap, ISL_BASIC_MAP_FINAL);
761 drop(&info[j]);
762 if (j < i)
763 exchange(&info[i], &info[j]);
764 change = isl_change_fuse;
765 } else
766 if (isl_tab_rollback(info[i].tab, snap) < 0)
767 return isl_change_error;
769 return change;
772 /* Data structure that keeps track of the wrapping constraints
773 * and of information to bound the coefficients of those constraints.
775 * bound is set if we want to apply a bound on the coefficients
776 * mat contains the wrapping constraints
777 * max is the bound on the coefficients (if bound is set)
779 struct isl_wraps {
780 int bound;
781 isl_mat *mat;
782 isl_int max;
785 /* Update wraps->max to be greater than or equal to the coefficients
786 * in the equalities and inequalities of info->bmap that can be removed
787 * if we end up applying wrapping.
789 static void wraps_update_max(struct isl_wraps *wraps,
790 struct isl_coalesce_info *info)
792 int k;
793 isl_int max_k;
794 unsigned total = isl_basic_map_total_dim(info->bmap);
796 isl_int_init(max_k);
798 for (k = 0; k < info->bmap->n_eq; ++k) {
799 if (info->eq[2 * k] == STATUS_VALID &&
800 info->eq[2 * k + 1] == STATUS_VALID)
801 continue;
802 isl_seq_abs_max(info->bmap->eq[k] + 1, total, &max_k);
803 if (isl_int_abs_gt(max_k, wraps->max))
804 isl_int_set(wraps->max, max_k);
807 for (k = 0; k < info->bmap->n_ineq; ++k) {
808 if (info->ineq[k] == STATUS_VALID ||
809 info->ineq[k] == STATUS_REDUNDANT)
810 continue;
811 isl_seq_abs_max(info->bmap->ineq[k] + 1, total, &max_k);
812 if (isl_int_abs_gt(max_k, wraps->max))
813 isl_int_set(wraps->max, max_k);
816 isl_int_clear(max_k);
819 /* Initialize the isl_wraps data structure.
820 * If we want to bound the coefficients of the wrapping constraints,
821 * we set wraps->max to the largest coefficient
822 * in the equalities and inequalities that can be removed if we end up
823 * applying wrapping.
825 static void wraps_init(struct isl_wraps *wraps, __isl_take isl_mat *mat,
826 struct isl_coalesce_info *info, int i, int j)
828 isl_ctx *ctx;
830 wraps->bound = 0;
831 wraps->mat = mat;
832 if (!mat)
833 return;
834 ctx = isl_mat_get_ctx(mat);
835 wraps->bound = isl_options_get_coalesce_bounded_wrapping(ctx);
836 if (!wraps->bound)
837 return;
838 isl_int_init(wraps->max);
839 isl_int_set_si(wraps->max, 0);
840 wraps_update_max(wraps, &info[i]);
841 wraps_update_max(wraps, &info[j]);
844 /* Free the contents of the isl_wraps data structure.
846 static void wraps_free(struct isl_wraps *wraps)
848 isl_mat_free(wraps->mat);
849 if (wraps->bound)
850 isl_int_clear(wraps->max);
853 /* Is the wrapping constraint in row "row" allowed?
855 * If wraps->bound is set, we check that none of the coefficients
856 * is greater than wraps->max.
858 static int allow_wrap(struct isl_wraps *wraps, int row)
860 int i;
862 if (!wraps->bound)
863 return 1;
865 for (i = 1; i < wraps->mat->n_col; ++i)
866 if (isl_int_abs_gt(wraps->mat->row[row][i], wraps->max))
867 return 0;
869 return 1;
872 /* Wrap "ineq" (or its opposite if "negate" is set) around "bound"
873 * to include "set" and add the result in position "w" of "wraps".
874 * "len" is the total number of coefficients in "bound" and "ineq".
875 * Return 1 on success, 0 on failure and -1 on error.
876 * Wrapping can fail if the result of wrapping is equal to "bound"
877 * or if we want to bound the sizes of the coefficients and
878 * the wrapped constraint does not satisfy this bound.
880 static int add_wrap(struct isl_wraps *wraps, int w, isl_int *bound,
881 isl_int *ineq, unsigned len, __isl_keep isl_set *set, int negate)
883 isl_seq_cpy(wraps->mat->row[w], bound, len);
884 if (negate) {
885 isl_seq_neg(wraps->mat->row[w + 1], ineq, len);
886 ineq = wraps->mat->row[w + 1];
888 if (!isl_set_wrap_facet(set, wraps->mat->row[w], ineq))
889 return -1;
890 if (isl_seq_eq(wraps->mat->row[w], bound, len))
891 return 0;
892 if (!allow_wrap(wraps, w))
893 return 0;
894 return 1;
897 /* For each constraint in info->bmap that is not redundant (as determined
898 * by info->tab) and that is not a valid constraint for the other basic map,
899 * wrap the constraint around "bound" such that it includes the whole
900 * set "set" and append the resulting constraint to "wraps".
901 * Note that the constraints that are valid for the other basic map
902 * will be added to the combined basic map by default, so there is
903 * no need to wrap them.
904 * The caller wrap_in_facets even relies on this function not wrapping
905 * any constraints that are already valid.
906 * "wraps" is assumed to have been pre-allocated to the appropriate size.
907 * wraps->n_row is the number of actual wrapped constraints that have
908 * been added.
909 * If any of the wrapping problems results in a constraint that is
910 * identical to "bound", then this means that "set" is unbounded in such
911 * way that no wrapping is possible. If this happens then wraps->n_row
912 * is reset to zero.
913 * Similarly, if we want to bound the coefficients of the wrapping
914 * constraints and a newly added wrapping constraint does not
915 * satisfy the bound, then wraps->n_row is also reset to zero.
917 static int add_wraps(struct isl_wraps *wraps, struct isl_coalesce_info *info,
918 isl_int *bound, __isl_keep isl_set *set)
920 int l, m;
921 int w;
922 int added;
923 isl_basic_map *bmap = info->bmap;
924 unsigned len = 1 + isl_basic_map_total_dim(bmap);
926 w = wraps->mat->n_row;
928 for (l = 0; l < bmap->n_ineq; ++l) {
929 if (info->ineq[l] == STATUS_VALID ||
930 info->ineq[l] == STATUS_REDUNDANT)
931 continue;
932 if (isl_seq_is_neg(bound, bmap->ineq[l], len))
933 continue;
934 if (isl_seq_eq(bound, bmap->ineq[l], len))
935 continue;
936 if (isl_tab_is_redundant(info->tab, bmap->n_eq + l))
937 continue;
939 added = add_wrap(wraps, w, bound, bmap->ineq[l], len, set, 0);
940 if (added < 0)
941 return -1;
942 if (!added)
943 goto unbounded;
944 ++w;
946 for (l = 0; l < bmap->n_eq; ++l) {
947 if (isl_seq_is_neg(bound, bmap->eq[l], len))
948 continue;
949 if (isl_seq_eq(bound, bmap->eq[l], len))
950 continue;
952 for (m = 0; m < 2; ++m) {
953 if (info->eq[2 * l + m] == STATUS_VALID)
954 continue;
955 added = add_wrap(wraps, w, bound, bmap->eq[l], len,
956 set, !m);
957 if (added < 0)
958 return -1;
959 if (!added)
960 goto unbounded;
961 ++w;
965 wraps->mat->n_row = w;
966 return 0;
967 unbounded:
968 wraps->mat->n_row = 0;
969 return 0;
972 /* Check if the constraints in "wraps" from "first" until the last
973 * are all valid for the basic set represented by "tab".
974 * If not, wraps->n_row is set to zero.
976 static int check_wraps(__isl_keep isl_mat *wraps, int first,
977 struct isl_tab *tab)
979 int i;
981 for (i = first; i < wraps->n_row; ++i) {
982 enum isl_ineq_type type;
983 type = isl_tab_ineq_type(tab, wraps->row[i]);
984 if (type == isl_ineq_error)
985 return -1;
986 if (type == isl_ineq_redundant)
987 continue;
988 wraps->n_row = 0;
989 return 0;
992 return 0;
995 /* Return a set that corresponds to the non-redundant constraints
996 * (as recorded in tab) of bmap.
998 * It's important to remove the redundant constraints as some
999 * of the other constraints may have been modified after the
1000 * constraints were marked redundant.
1001 * In particular, a constraint may have been relaxed.
1002 * Redundant constraints are ignored when a constraint is relaxed
1003 * and should therefore continue to be ignored ever after.
1004 * Otherwise, the relaxation might be thwarted by some of
1005 * these constraints.
1007 * Update the underlying set to ensure that the dimension doesn't change.
1008 * Otherwise the integer divisions could get dropped if the tab
1009 * turns out to be empty.
1011 static __isl_give isl_set *set_from_updated_bmap(__isl_keep isl_basic_map *bmap,
1012 struct isl_tab *tab)
1014 isl_basic_set *bset;
1016 bmap = isl_basic_map_copy(bmap);
1017 bset = isl_basic_map_underlying_set(bmap);
1018 bset = isl_basic_set_cow(bset);
1019 bset = isl_basic_set_update_from_tab(bset, tab);
1020 return isl_set_from_basic_set(bset);
1023 /* Wrap the constraints of info->bmap that bound the facet defined
1024 * by inequality "k" around (the opposite of) this inequality to
1025 * include "set". "bound" may be used to store the negated inequality.
1026 * Since the wrapped constraints are not guaranteed to contain the whole
1027 * of info->bmap, we check them in check_wraps.
1028 * If any of the wrapped constraints turn out to be invalid, then
1029 * check_wraps will reset wrap->n_row to zero.
1031 static int add_wraps_around_facet(struct isl_wraps *wraps,
1032 struct isl_coalesce_info *info, int k, isl_int *bound,
1033 __isl_keep isl_set *set)
1035 struct isl_tab_undo *snap;
1036 int n;
1037 unsigned total = isl_basic_map_total_dim(info->bmap);
1039 snap = isl_tab_snap(info->tab);
1041 if (isl_tab_select_facet(info->tab, info->bmap->n_eq + k) < 0)
1042 return -1;
1043 if (isl_tab_detect_redundant(info->tab) < 0)
1044 return -1;
1046 isl_seq_neg(bound, info->bmap->ineq[k], 1 + total);
1048 n = wraps->mat->n_row;
1049 if (add_wraps(wraps, info, bound, set) < 0)
1050 return -1;
1052 if (isl_tab_rollback(info->tab, snap) < 0)
1053 return -1;
1054 if (check_wraps(wraps->mat, n, info->tab) < 0)
1055 return -1;
1057 return 0;
1060 /* Given a basic set i with a constraint k that is adjacent to
1061 * basic set j, check if we can wrap
1062 * both the facet corresponding to k (if "wrap_facet" is set) and basic map j
1063 * (always) around their ridges to include the other set.
1064 * If so, replace the pair of basic sets by their union.
1066 * All constraints of i (except k) are assumed to be valid or
1067 * cut constraints for j.
1068 * Wrapping the cut constraints to include basic map j may result
1069 * in constraints that are no longer valid of basic map i
1070 * we have to check that the resulting wrapping constraints are valid for i.
1071 * If "wrap_facet" is not set, then all constraints of i (except k)
1072 * are assumed to be valid for j.
1073 * ____ _____
1074 * / | / \
1075 * / || / |
1076 * \ || => \ |
1077 * \ || \ |
1078 * \___|| \____|
1081 static enum isl_change can_wrap_in_facet(int i, int j, int k,
1082 struct isl_coalesce_info *info, int wrap_facet)
1084 enum isl_change change = isl_change_none;
1085 struct isl_wraps wraps;
1086 isl_ctx *ctx;
1087 isl_mat *mat;
1088 struct isl_set *set_i = NULL;
1089 struct isl_set *set_j = NULL;
1090 struct isl_vec *bound = NULL;
1091 unsigned total = isl_basic_map_total_dim(info[i].bmap);
1093 set_i = set_from_updated_bmap(info[i].bmap, info[i].tab);
1094 set_j = set_from_updated_bmap(info[j].bmap, info[j].tab);
1095 ctx = isl_basic_map_get_ctx(info[i].bmap);
1096 mat = isl_mat_alloc(ctx, 2 * (info[i].bmap->n_eq + info[j].bmap->n_eq) +
1097 info[i].bmap->n_ineq + info[j].bmap->n_ineq,
1098 1 + total);
1099 wraps_init(&wraps, mat, info, i, j);
1100 bound = isl_vec_alloc(ctx, 1 + total);
1101 if (!set_i || !set_j || !wraps.mat || !bound)
1102 goto error;
1104 isl_seq_cpy(bound->el, info[i].bmap->ineq[k], 1 + total);
1105 isl_int_add_ui(bound->el[0], bound->el[0], 1);
1107 isl_seq_cpy(wraps.mat->row[0], bound->el, 1 + total);
1108 wraps.mat->n_row = 1;
1110 if (add_wraps(&wraps, &info[j], bound->el, set_i) < 0)
1111 goto error;
1112 if (!wraps.mat->n_row)
1113 goto unbounded;
1115 if (wrap_facet) {
1116 if (add_wraps_around_facet(&wraps, &info[i], k,
1117 bound->el, set_j) < 0)
1118 goto error;
1119 if (!wraps.mat->n_row)
1120 goto unbounded;
1123 change = fuse(i, j, info, wraps.mat, 0, 0);
1125 unbounded:
1126 wraps_free(&wraps);
1128 isl_set_free(set_i);
1129 isl_set_free(set_j);
1131 isl_vec_free(bound);
1133 return change;
1134 error:
1135 wraps_free(&wraps);
1136 isl_vec_free(bound);
1137 isl_set_free(set_i);
1138 isl_set_free(set_j);
1139 return isl_change_error;
1142 /* Given a pair of basic maps i and j such that j sticks out
1143 * of i at n cut constraints, each time by at most one,
1144 * try to compute wrapping constraints and replace the two
1145 * basic maps by a single basic map.
1146 * The other constraints of i are assumed to be valid for j.
1148 * For each cut constraint t(x) >= 0 of i, we add the relaxed version
1149 * t(x) + 1 >= 0, along with wrapping constraints for all constraints
1150 * of basic map j that bound the part of basic map j that sticks out
1151 * of the cut constraint.
1152 * In particular, we first intersect basic map j with t(x) + 1 = 0.
1153 * If the result is empty, then t(x) >= 0 was actually a valid constraint
1154 * (with respect to the integer points), so we add t(x) >= 0 instead.
1155 * Otherwise, we wrap the constraints of basic map j that are not
1156 * redundant in this intersection and that are not already valid
1157 * for basic map i over basic map i.
1158 * Note that it is sufficient to wrap the constraints to include
1159 * basic map i, because we will only wrap the constraints that do
1160 * not include basic map i already. The wrapped constraint will
1161 * therefore be more relaxed compared to the original constraint.
1162 * Since the original constraint is valid for basic map j, so is
1163 * the wrapped constraint.
1165 * If any wrapping fails, i.e., if we cannot wrap to touch
1166 * the union, then we give up.
1167 * Otherwise, the pair of basic maps is replaced by their union.
1169 static enum isl_change wrap_in_facets(int i, int j, int *cuts, int n,
1170 struct isl_coalesce_info *info)
1172 enum isl_change change = isl_change_none;
1173 struct isl_wraps wraps;
1174 isl_ctx *ctx;
1175 isl_mat *mat;
1176 isl_set *set_i = NULL;
1177 unsigned total = isl_basic_map_total_dim(info[i].bmap);
1178 int max_wrap;
1179 int k, w;
1180 struct isl_tab_undo *snap;
1182 if (isl_tab_extend_cons(info[j].tab, 1) < 0)
1183 goto error;
1185 max_wrap = 1 + 2 * info[j].bmap->n_eq + info[j].bmap->n_ineq;
1186 max_wrap *= n;
1188 set_i = set_from_updated_bmap(info[i].bmap, info[i].tab);
1189 ctx = isl_basic_map_get_ctx(info[i].bmap);
1190 mat = isl_mat_alloc(ctx, max_wrap, 1 + total);
1191 wraps_init(&wraps, mat, info, i, j);
1192 if (!set_i || !wraps.mat)
1193 goto error;
1195 snap = isl_tab_snap(info[j].tab);
1197 wraps.mat->n_row = 0;
1199 for (k = 0; k < n; ++k) {
1200 w = wraps.mat->n_row++;
1201 isl_seq_cpy(wraps.mat->row[w],
1202 info[i].bmap->ineq[cuts[k]], 1 + total);
1203 isl_int_add_ui(wraps.mat->row[w][0], wraps.mat->row[w][0], 1);
1204 if (isl_tab_add_eq(info[j].tab, wraps.mat->row[w]) < 0)
1205 goto error;
1206 if (isl_tab_detect_redundant(info[j].tab) < 0)
1207 goto error;
1209 if (info[j].tab->empty)
1210 isl_int_sub_ui(wraps.mat->row[w][0],
1211 wraps.mat->row[w][0], 1);
1212 else if (add_wraps(&wraps, &info[j],
1213 wraps.mat->row[w], set_i) < 0)
1214 goto error;
1216 if (isl_tab_rollback(info[j].tab, snap) < 0)
1217 goto error;
1219 if (!wraps.mat->n_row)
1220 break;
1223 if (k == n)
1224 change = fuse(i, j, info, wraps.mat, 0, 1);
1226 wraps_free(&wraps);
1227 isl_set_free(set_i);
1229 return change;
1230 error:
1231 wraps_free(&wraps);
1232 isl_set_free(set_i);
1233 return isl_change_error;
1236 /* Given two basic sets i and j such that i has no cut equalities,
1237 * check if relaxing all the cut inequalities of i by one turns
1238 * them into valid constraint for j and check if we can wrap in
1239 * the bits that are sticking out.
1240 * If so, replace the pair by their union.
1242 * We first check if all relaxed cut inequalities of i are valid for j
1243 * and then try to wrap in the intersections of the relaxed cut inequalities
1244 * with j.
1246 * During this wrapping, we consider the points of j that lie at a distance
1247 * of exactly 1 from i. In particular, we ignore the points that lie in
1248 * between this lower-dimensional space and the basic map i.
1249 * We can therefore only apply this to integer maps.
1250 * ____ _____
1251 * / ___|_ / \
1252 * / | | / |
1253 * \ | | => \ |
1254 * \|____| \ |
1255 * \___| \____/
1257 * _____ ______
1258 * | ____|_ | \
1259 * | | | | |
1260 * | | | => | |
1261 * |_| | | |
1262 * |_____| \______|
1264 * _______
1265 * | |
1266 * | |\ |
1267 * | | \ |
1268 * | | \ |
1269 * | | \|
1270 * | | \
1271 * | |_____\
1272 * | |
1273 * |_______|
1275 * Wrapping can fail if the result of wrapping one of the facets
1276 * around its edges does not produce any new facet constraint.
1277 * In particular, this happens when we try to wrap in unbounded sets.
1279 * _______________________________________________________________________
1281 * | ___
1282 * | | |
1283 * |_| |_________________________________________________________________
1284 * |___|
1286 * The following is not an acceptable result of coalescing the above two
1287 * sets as it includes extra integer points.
1288 * _______________________________________________________________________
1290 * |
1291 * |
1293 * \______________________________________________________________________
1295 static enum isl_change can_wrap_in_set(int i, int j,
1296 struct isl_coalesce_info *info)
1298 enum isl_change change = isl_change_none;
1299 int k, m;
1300 int n;
1301 int *cuts = NULL;
1302 isl_ctx *ctx;
1304 if (ISL_F_ISSET(info[i].bmap, ISL_BASIC_MAP_RATIONAL) ||
1305 ISL_F_ISSET(info[j].bmap, ISL_BASIC_MAP_RATIONAL))
1306 return isl_change_none;
1308 n = count(info[i].ineq, info[i].bmap->n_ineq, STATUS_CUT);
1309 if (n == 0)
1310 return isl_change_none;
1312 ctx = isl_basic_map_get_ctx(info[i].bmap);
1313 cuts = isl_alloc_array(ctx, int, n);
1314 if (!cuts)
1315 return isl_change_error;
1317 for (k = 0, m = 0; m < n; ++k) {
1318 enum isl_ineq_type type;
1320 if (info[i].ineq[k] != STATUS_CUT)
1321 continue;
1323 isl_int_add_ui(info[i].bmap->ineq[k][0],
1324 info[i].bmap->ineq[k][0], 1);
1325 type = isl_tab_ineq_type(info[j].tab, info[i].bmap->ineq[k]);
1326 isl_int_sub_ui(info[i].bmap->ineq[k][0],
1327 info[i].bmap->ineq[k][0], 1);
1328 if (type == isl_ineq_error)
1329 goto error;
1330 if (type != isl_ineq_redundant)
1331 break;
1332 cuts[m] = k;
1333 ++m;
1336 if (m == n)
1337 change = wrap_in_facets(i, j, cuts, n, info);
1339 free(cuts);
1341 return change;
1342 error:
1343 free(cuts);
1344 return isl_change_error;
1347 /* Check if either i or j has only cut inequalities that can
1348 * be used to wrap in (a facet of) the other basic set.
1349 * if so, replace the pair by their union.
1351 static enum isl_change check_wrap(int i, int j, struct isl_coalesce_info *info)
1353 enum isl_change change = isl_change_none;
1355 if (!any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_CUT))
1356 change = can_wrap_in_set(i, j, info);
1357 if (change != isl_change_none)
1358 return change;
1360 if (!any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_CUT))
1361 change = can_wrap_in_set(j, i, info);
1362 return change;
1365 /* At least one of the basic maps has an equality that is adjacent
1366 * to inequality. Make sure that only one of the basic maps has
1367 * such an equality and that the other basic map has exactly one
1368 * inequality adjacent to an equality.
1369 * We call the basic map that has the inequality "i" and the basic
1370 * map that has the equality "j".
1371 * If "i" has any "cut" (in)equality, then relaxing the inequality
1372 * by one would not result in a basic map that contains the other
1373 * basic map. However, it may still be possible to wrap in the other
1374 * basic map.
1376 static enum isl_change check_adj_eq(int i, int j,
1377 struct isl_coalesce_info *info)
1379 enum isl_change change = isl_change_none;
1380 int k;
1381 int any_cut;
1383 if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ADJ_INEQ) &&
1384 any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_ADJ_INEQ))
1385 /* ADJ EQ TOO MANY */
1386 return isl_change_none;
1388 if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ADJ_INEQ))
1389 return check_adj_eq(j, i, info);
1391 /* j has an equality adjacent to an inequality in i */
1393 if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_CUT))
1394 return isl_change_none;
1395 any_cut = any(info[i].ineq, info[i].bmap->n_ineq, STATUS_CUT);
1396 if (count(info[i].ineq, info[i].bmap->n_ineq, STATUS_ADJ_EQ) != 1 ||
1397 any(info[j].ineq, info[j].bmap->n_ineq, STATUS_ADJ_EQ) ||
1398 any(info[i].ineq, info[i].bmap->n_ineq, STATUS_ADJ_INEQ) ||
1399 any(info[j].ineq, info[j].bmap->n_ineq, STATUS_ADJ_INEQ))
1400 /* ADJ EQ TOO MANY */
1401 return isl_change_none;
1403 for (k = 0; k < info[i].bmap->n_ineq; ++k)
1404 if (info[i].ineq[k] == STATUS_ADJ_EQ)
1405 break;
1407 if (!any_cut) {
1408 change = is_adj_eq_extension(i, j, k, info);
1409 if (change != isl_change_none)
1410 return change;
1413 change = can_wrap_in_facet(i, j, k, info, any_cut);
1415 return change;
1418 /* The two basic maps lie on adjacent hyperplanes. In particular,
1419 * basic map "i" has an equality that lies parallel to basic map "j".
1420 * Check if we can wrap the facets around the parallel hyperplanes
1421 * to include the other set.
1423 * We perform basically the same operations as can_wrap_in_facet,
1424 * except that we don't need to select a facet of one of the sets.
1426 * \\ \\
1427 * \\ => \\
1428 * \ \|
1430 * If there is more than one equality of "i" adjacent to an equality of "j",
1431 * then the result will satisfy one or more equalities that are a linear
1432 * combination of these equalities. These will be encoded as pairs
1433 * of inequalities in the wrapping constraints and need to be made
1434 * explicit.
1436 static enum isl_change check_eq_adj_eq(int i, int j,
1437 struct isl_coalesce_info *info)
1439 int k;
1440 enum isl_change change = isl_change_none;
1441 int detect_equalities = 0;
1442 struct isl_wraps wraps;
1443 isl_ctx *ctx;
1444 isl_mat *mat;
1445 struct isl_set *set_i = NULL;
1446 struct isl_set *set_j = NULL;
1447 struct isl_vec *bound = NULL;
1448 unsigned total = isl_basic_map_total_dim(info[i].bmap);
1450 if (count(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ADJ_EQ) != 1)
1451 detect_equalities = 1;
1453 for (k = 0; k < 2 * info[i].bmap->n_eq ; ++k)
1454 if (info[i].eq[k] == STATUS_ADJ_EQ)
1455 break;
1457 set_i = set_from_updated_bmap(info[i].bmap, info[i].tab);
1458 set_j = set_from_updated_bmap(info[j].bmap, info[j].tab);
1459 ctx = isl_basic_map_get_ctx(info[i].bmap);
1460 mat = isl_mat_alloc(ctx, 2 * (info[i].bmap->n_eq + info[j].bmap->n_eq) +
1461 info[i].bmap->n_ineq + info[j].bmap->n_ineq,
1462 1 + total);
1463 wraps_init(&wraps, mat, info, i, j);
1464 bound = isl_vec_alloc(ctx, 1 + total);
1465 if (!set_i || !set_j || !wraps.mat || !bound)
1466 goto error;
1468 if (k % 2 == 0)
1469 isl_seq_neg(bound->el, info[i].bmap->eq[k / 2], 1 + total);
1470 else
1471 isl_seq_cpy(bound->el, info[i].bmap->eq[k / 2], 1 + total);
1472 isl_int_add_ui(bound->el[0], bound->el[0], 1);
1474 isl_seq_cpy(wraps.mat->row[0], bound->el, 1 + total);
1475 wraps.mat->n_row = 1;
1477 if (add_wraps(&wraps, &info[j], bound->el, set_i) < 0)
1478 goto error;
1479 if (!wraps.mat->n_row)
1480 goto unbounded;
1482 isl_int_sub_ui(bound->el[0], bound->el[0], 1);
1483 isl_seq_neg(bound->el, bound->el, 1 + total);
1485 isl_seq_cpy(wraps.mat->row[wraps.mat->n_row], bound->el, 1 + total);
1486 wraps.mat->n_row++;
1488 if (add_wraps(&wraps, &info[i], bound->el, set_j) < 0)
1489 goto error;
1490 if (!wraps.mat->n_row)
1491 goto unbounded;
1493 change = fuse(i, j, info, wraps.mat, detect_equalities, 0);
1495 if (0) {
1496 error: change = isl_change_error;
1498 unbounded:
1500 wraps_free(&wraps);
1501 isl_set_free(set_i);
1502 isl_set_free(set_j);
1503 isl_vec_free(bound);
1505 return change;
1508 /* Check if the union of the given pair of basic maps
1509 * can be represented by a single basic map.
1510 * If so, replace the pair by the single basic map and return
1511 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
1512 * Otherwise, return isl_change_none.
1513 * The two basic maps are assumed to live in the same local space.
1515 * We first check the effect of each constraint of one basic map
1516 * on the other basic map.
1517 * The constraint may be
1518 * redundant the constraint is redundant in its own
1519 * basic map and should be ignore and removed
1520 * in the end
1521 * valid all (integer) points of the other basic map
1522 * satisfy the constraint
1523 * separate no (integer) point of the other basic map
1524 * satisfies the constraint
1525 * cut some but not all points of the other basic map
1526 * satisfy the constraint
1527 * adj_eq the given constraint is adjacent (on the outside)
1528 * to an equality of the other basic map
1529 * adj_ineq the given constraint is adjacent (on the outside)
1530 * to an inequality of the other basic map
1532 * We consider seven cases in which we can replace the pair by a single
1533 * basic map. We ignore all "redundant" constraints.
1535 * 1. all constraints of one basic map are valid
1536 * => the other basic map is a subset and can be removed
1538 * 2. all constraints of both basic maps are either "valid" or "cut"
1539 * and the facets corresponding to the "cut" constraints
1540 * of one of the basic maps lies entirely inside the other basic map
1541 * => the pair can be replaced by a basic map consisting
1542 * of the valid constraints in both basic maps
1544 * 3. there is a single pair of adjacent inequalities
1545 * (all other constraints are "valid")
1546 * => the pair can be replaced by a basic map consisting
1547 * of the valid constraints in both basic maps
1549 * 4. one basic map has a single adjacent inequality, while the other
1550 * constraints are "valid". The other basic map has some
1551 * "cut" constraints, but replacing the adjacent inequality by
1552 * its opposite and adding the valid constraints of the other
1553 * basic map results in a subset of the other basic map
1554 * => the pair can be replaced by a basic map consisting
1555 * of the valid constraints in both basic maps
1557 * 5. there is a single adjacent pair of an inequality and an equality,
1558 * the other constraints of the basic map containing the inequality are
1559 * "valid". Moreover, if the inequality the basic map is relaxed
1560 * and then turned into an equality, then resulting facet lies
1561 * entirely inside the other basic map
1562 * => the pair can be replaced by the basic map containing
1563 * the inequality, with the inequality relaxed.
1565 * 6. there is a single adjacent pair of an inequality and an equality,
1566 * the other constraints of the basic map containing the inequality are
1567 * "valid". Moreover, the facets corresponding to both
1568 * the inequality and the equality can be wrapped around their
1569 * ridges to include the other basic map
1570 * => the pair can be replaced by a basic map consisting
1571 * of the valid constraints in both basic maps together
1572 * with all wrapping constraints
1574 * 7. one of the basic maps extends beyond the other by at most one.
1575 * Moreover, the facets corresponding to the cut constraints and
1576 * the pieces of the other basic map at offset one from these cut
1577 * constraints can be wrapped around their ridges to include
1578 * the union of the two basic maps
1579 * => the pair can be replaced by a basic map consisting
1580 * of the valid constraints in both basic maps together
1581 * with all wrapping constraints
1583 * 8. the two basic maps live in adjacent hyperplanes. In principle
1584 * such sets can always be combined through wrapping, but we impose
1585 * that there is only one such pair, to avoid overeager coalescing.
1587 * Throughout the computation, we maintain a collection of tableaus
1588 * corresponding to the basic maps. When the basic maps are dropped
1589 * or combined, the tableaus are modified accordingly.
1591 static enum isl_change coalesce_local_pair(int i, int j,
1592 struct isl_coalesce_info *info)
1594 enum isl_change change = isl_change_none;
1596 info[i].eq = info[i].ineq = NULL;
1597 info[j].eq = info[j].ineq = NULL;
1599 info[i].eq = eq_status_in(info[i].bmap, info[j].tab);
1600 if (info[i].bmap->n_eq && !info[i].eq)
1601 goto error;
1602 if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ERROR))
1603 goto error;
1604 if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_SEPARATE))
1605 goto done;
1607 info[j].eq = eq_status_in(info[j].bmap, info[i].tab);
1608 if (info[j].bmap->n_eq && !info[j].eq)
1609 goto error;
1610 if (any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_ERROR))
1611 goto error;
1612 if (any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_SEPARATE))
1613 goto done;
1615 info[i].ineq = ineq_status_in(info[i].bmap, info[i].tab, info[j].tab);
1616 if (info[i].bmap->n_ineq && !info[i].ineq)
1617 goto error;
1618 if (any(info[i].ineq, info[i].bmap->n_ineq, STATUS_ERROR))
1619 goto error;
1620 if (any(info[i].ineq, info[i].bmap->n_ineq, STATUS_SEPARATE))
1621 goto done;
1623 info[j].ineq = ineq_status_in(info[j].bmap, info[j].tab, info[i].tab);
1624 if (info[j].bmap->n_ineq && !info[j].ineq)
1625 goto error;
1626 if (any(info[j].ineq, info[j].bmap->n_ineq, STATUS_ERROR))
1627 goto error;
1628 if (any(info[j].ineq, info[j].bmap->n_ineq, STATUS_SEPARATE))
1629 goto done;
1631 if (all(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_VALID) &&
1632 all(info[i].ineq, info[i].bmap->n_ineq, STATUS_VALID)) {
1633 drop(&info[j]);
1634 change = isl_change_drop_second;
1635 } else if (all(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_VALID) &&
1636 all(info[j].ineq, info[j].bmap->n_ineq, STATUS_VALID)) {
1637 drop(&info[i]);
1638 change = isl_change_drop_first;
1639 } else if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ADJ_EQ)) {
1640 change = check_eq_adj_eq(i, j, info);
1641 } else if (any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_ADJ_EQ)) {
1642 change = check_eq_adj_eq(j, i, info);
1643 } else if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ADJ_INEQ) ||
1644 any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_ADJ_INEQ)) {
1645 change = check_adj_eq(i, j, info);
1646 } else if (any(info[i].ineq, info[i].bmap->n_ineq, STATUS_ADJ_EQ) ||
1647 any(info[j].ineq, info[j].bmap->n_ineq, STATUS_ADJ_EQ)) {
1648 /* Can't happen */
1649 /* BAD ADJ INEQ */
1650 } else if (any(info[i].ineq, info[i].bmap->n_ineq, STATUS_ADJ_INEQ) ||
1651 any(info[j].ineq, info[j].bmap->n_ineq, STATUS_ADJ_INEQ)) {
1652 change = check_adj_ineq(i, j, info);
1653 } else {
1654 if (!any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_CUT) &&
1655 !any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_CUT))
1656 change = check_facets(i, j, info);
1657 if (change == isl_change_none)
1658 change = check_wrap(i, j, info);
1661 done:
1662 free(info[i].eq);
1663 free(info[j].eq);
1664 free(info[i].ineq);
1665 free(info[j].ineq);
1666 return change;
1667 error:
1668 free(info[i].eq);
1669 free(info[j].eq);
1670 free(info[i].ineq);
1671 free(info[j].ineq);
1672 return isl_change_error;
1675 /* Shift the integer division at position "div" of the basic map
1676 * represented by "info" by "shift".
1678 * That is, if the integer division has the form
1680 * floor(f(x)/d)
1682 * then replace it by
1684 * floor((f(x) + shift * d)/d) - shift
1686 static int shift_div(struct isl_coalesce_info *info, int div, isl_int shift)
1688 unsigned total;
1690 info->bmap = isl_basic_map_shift_div(info->bmap, div, shift);
1691 if (!info->bmap)
1692 return -1;
1694 total = isl_basic_map_dim(info->bmap, isl_dim_all);
1695 total -= isl_basic_map_dim(info->bmap, isl_dim_div);
1696 if (isl_tab_shift_var(info->tab, total + div, shift) < 0)
1697 return -1;
1699 return 0;
1702 /* Check if some of the divs in the basic map represented by "info1"
1703 * are shifts of the corresponding divs in the basic map represented
1704 * by "info2". If so, align them with those of "info2".
1705 * Only do this if "info1" and "info2" have the same number
1706 * of integer divisions.
1708 * An integer division is considered to be a shift of another integer
1709 * division if one is equal to the other plus a constant.
1711 * In particular, for each pair of integer divisions, if both are known,
1712 * have identical coefficients (apart from the constant term) and
1713 * if the difference between the constant terms (taking into account
1714 * the denominator) is an integer, then move the difference outside.
1715 * That is, if one integer division is of the form
1717 * floor((f(x) + c_1)/d)
1719 * while the other is of the form
1721 * floor((f(x) + c_2)/d)
1723 * and n = (c_2 - c_1)/d is an integer, then replace the first
1724 * integer division by
1726 * floor((f(x) + c_1 + n * d)/d) - n = floor((f(x) + c_2)/d) - n
1728 static int harmonize_divs(struct isl_coalesce_info *info1,
1729 struct isl_coalesce_info *info2)
1731 int i;
1732 int total;
1734 if (!info1->bmap || !info2->bmap)
1735 return -1;
1737 if (info1->bmap->n_div != info2->bmap->n_div)
1738 return 0;
1739 if (info1->bmap->n_div == 0)
1740 return 0;
1742 total = isl_basic_map_total_dim(info1->bmap);
1743 for (i = 0; i < info1->bmap->n_div; ++i) {
1744 isl_int d;
1745 int r = 0;
1747 if (isl_int_is_zero(info1->bmap->div[i][0]) ||
1748 isl_int_is_zero(info2->bmap->div[i][0]))
1749 continue;
1750 if (isl_int_ne(info1->bmap->div[i][0], info2->bmap->div[i][0]))
1751 continue;
1752 if (isl_int_eq(info1->bmap->div[i][1], info2->bmap->div[i][1]))
1753 continue;
1754 if (!isl_seq_eq(info1->bmap->div[i] + 2,
1755 info2->bmap->div[i] + 2, total))
1756 continue;
1757 isl_int_init(d);
1758 isl_int_sub(d, info2->bmap->div[i][1], info1->bmap->div[i][1]);
1759 if (isl_int_is_divisible_by(d, info1->bmap->div[i][0])) {
1760 isl_int_divexact(d, d, info1->bmap->div[i][0]);
1761 r = shift_div(info1, i, d);
1763 isl_int_clear(d);
1764 if (r < 0)
1765 return -1;
1768 return 0;
1771 /* Do the two basic maps live in the same local space, i.e.,
1772 * do they have the same (known) divs?
1773 * If either basic map has any unknown divs, then we can only assume
1774 * that they do not live in the same local space.
1776 static int same_divs(__isl_keep isl_basic_map *bmap1,
1777 __isl_keep isl_basic_map *bmap2)
1779 int i;
1780 int known;
1781 int total;
1783 if (!bmap1 || !bmap2)
1784 return -1;
1785 if (bmap1->n_div != bmap2->n_div)
1786 return 0;
1788 if (bmap1->n_div == 0)
1789 return 1;
1791 known = isl_basic_map_divs_known(bmap1);
1792 if (known < 0 || !known)
1793 return known;
1794 known = isl_basic_map_divs_known(bmap2);
1795 if (known < 0 || !known)
1796 return known;
1798 total = isl_basic_map_total_dim(bmap1);
1799 for (i = 0; i < bmap1->n_div; ++i)
1800 if (!isl_seq_eq(bmap1->div[i], bmap2->div[i], 2 + total))
1801 return 0;
1803 return 1;
1806 /* Does "bmap" contain the basic map represented by the tableau "tab"
1807 * after expanding the divs of "bmap" to match those of "tab"?
1808 * The expansion is performed using the divs "div" and expansion "exp"
1809 * computed by the caller.
1810 * Then we check if all constraints of the expanded "bmap" are valid for "tab".
1812 static int contains_with_expanded_divs(__isl_keep isl_basic_map *bmap,
1813 struct isl_tab *tab, __isl_keep isl_mat *div, int *exp)
1815 int superset = 0;
1816 int *eq_i = NULL;
1817 int *ineq_i = NULL;
1819 bmap = isl_basic_map_copy(bmap);
1820 bmap = isl_basic_set_expand_divs(bmap, isl_mat_copy(div), exp);
1822 if (!bmap)
1823 goto error;
1825 eq_i = eq_status_in(bmap, tab);
1826 if (bmap->n_eq && !eq_i)
1827 goto error;
1828 if (any(eq_i, 2 * bmap->n_eq, STATUS_ERROR))
1829 goto error;
1830 if (any(eq_i, 2 * bmap->n_eq, STATUS_SEPARATE))
1831 goto done;
1833 ineq_i = ineq_status_in(bmap, NULL, tab);
1834 if (bmap->n_ineq && !ineq_i)
1835 goto error;
1836 if (any(ineq_i, bmap->n_ineq, STATUS_ERROR))
1837 goto error;
1838 if (any(ineq_i, bmap->n_ineq, STATUS_SEPARATE))
1839 goto done;
1841 if (all(eq_i, 2 * bmap->n_eq, STATUS_VALID) &&
1842 all(ineq_i, bmap->n_ineq, STATUS_VALID))
1843 superset = 1;
1845 done:
1846 isl_basic_map_free(bmap);
1847 free(eq_i);
1848 free(ineq_i);
1849 return superset;
1850 error:
1851 isl_basic_map_free(bmap);
1852 free(eq_i);
1853 free(ineq_i);
1854 return -1;
1857 /* Does "bmap_i" contain the basic map represented by "info_j"
1858 * after aligning the divs of "bmap_i" to those of "info_j".
1859 * Note that this can only succeed if the number of divs of "bmap_i"
1860 * is smaller than (or equal to) the number of divs of "info_j".
1862 * We first check if the divs of "bmap_i" are all known and form a subset
1863 * of those of "bmap_j". If so, we pass control over to
1864 * contains_with_expanded_divs.
1866 static int contains_after_aligning_divs(__isl_keep isl_basic_map *bmap_i,
1867 struct isl_coalesce_info *info_j)
1869 int known;
1870 isl_mat *div_i, *div_j, *div;
1871 int *exp1 = NULL;
1872 int *exp2 = NULL;
1873 isl_ctx *ctx;
1874 int subset;
1876 known = isl_basic_map_divs_known(bmap_i);
1877 if (known < 0 || !known)
1878 return known;
1880 ctx = isl_basic_map_get_ctx(bmap_i);
1882 div_i = isl_basic_map_get_divs(bmap_i);
1883 div_j = isl_basic_map_get_divs(info_j->bmap);
1885 if (!div_i || !div_j)
1886 goto error;
1888 exp1 = isl_alloc_array(ctx, int, div_i->n_row);
1889 exp2 = isl_alloc_array(ctx, int, div_j->n_row);
1890 if ((div_i->n_row && !exp1) || (div_j->n_row && !exp2))
1891 goto error;
1893 div = isl_merge_divs(div_i, div_j, exp1, exp2);
1894 if (!div)
1895 goto error;
1897 if (div->n_row == div_j->n_row)
1898 subset = contains_with_expanded_divs(bmap_i,
1899 info_j->tab, div, exp1);
1900 else
1901 subset = 0;
1903 isl_mat_free(div);
1905 isl_mat_free(div_i);
1906 isl_mat_free(div_j);
1908 free(exp2);
1909 free(exp1);
1911 return subset;
1912 error:
1913 isl_mat_free(div_i);
1914 isl_mat_free(div_j);
1915 free(exp1);
1916 free(exp2);
1917 return -1;
1920 /* Check if the basic map "j" is a subset of basic map "i",
1921 * if "i" has fewer divs that "j".
1922 * If so, remove basic map "j".
1924 * If the two basic maps have the same number of divs, then
1925 * they must necessarily be different. Otherwise, we would have
1926 * called coalesce_local_pair. We therefore don't try anything
1927 * in this case.
1929 static int coalesced_subset(int i, int j, struct isl_coalesce_info *info)
1931 int superset;
1933 if (info[i].bmap->n_div >= info[j].bmap->n_div)
1934 return 0;
1936 superset = contains_after_aligning_divs(info[i].bmap, &info[j]);
1937 if (superset < 0)
1938 return -1;
1939 if (superset)
1940 drop(&info[j]);
1942 return superset;
1945 /* Check if basic map "j" is a subset of basic map "i" after
1946 * exploiting the extra equalities of "j" to simplify the divs of "i".
1947 * If so, remove basic map "j".
1949 * If "j" does not have any equalities or if they are the same
1950 * as those of "i", then we cannot exploit them to simplify the divs.
1951 * Similarly, if there are no divs in "i", then they cannot be simplified.
1952 * If, on the other hand, the affine hulls of "i" and "j" do not intersect,
1953 * then "j" cannot be a subset of "i".
1955 * Otherwise, we intersect "i" with the affine hull of "j" and then
1956 * check if "j" is a subset of the result after aligning the divs.
1957 * If so, then "j" is definitely a subset of "i" and can be removed.
1958 * Note that if after intersection with the affine hull of "j".
1959 * "i" still has more divs than "j", then there is no way we can
1960 * align the divs of "i" to those of "j".
1962 static int coalesced_subset_with_equalities(int i, int j,
1963 struct isl_coalesce_info *info)
1965 isl_basic_map *hull_i, *hull_j, *bmap_i;
1966 int equal, empty, subset;
1968 if (info[j].bmap->n_eq == 0)
1969 return 0;
1970 if (info[i].bmap->n_div == 0)
1971 return 0;
1973 hull_i = isl_basic_map_copy(info[i].bmap);
1974 hull_i = isl_basic_map_plain_affine_hull(hull_i);
1975 hull_j = isl_basic_map_copy(info[j].bmap);
1976 hull_j = isl_basic_map_plain_affine_hull(hull_j);
1978 hull_j = isl_basic_map_intersect(hull_j, isl_basic_map_copy(hull_i));
1979 equal = isl_basic_map_plain_is_equal(hull_i, hull_j);
1980 empty = isl_basic_map_plain_is_empty(hull_j);
1981 isl_basic_map_free(hull_i);
1983 if (equal < 0 || equal || empty < 0 || empty) {
1984 isl_basic_map_free(hull_j);
1985 return equal < 0 || empty < 0 ? -1 : 0;
1988 bmap_i = isl_basic_map_copy(info[i].bmap);
1989 bmap_i = isl_basic_map_intersect(bmap_i, hull_j);
1990 if (!bmap_i)
1991 return -1;
1993 if (bmap_i->n_div > info[j].bmap->n_div) {
1994 isl_basic_map_free(bmap_i);
1995 return 0;
1998 subset = contains_after_aligning_divs(bmap_i, &info[j]);
2000 isl_basic_map_free(bmap_i);
2002 if (subset < 0)
2003 return -1;
2004 if (subset)
2005 drop(&info[j]);
2007 return subset;
2010 /* Check if one of the basic maps is a subset of the other and, if so,
2011 * drop the subset.
2012 * Note that we only perform any test if the number of divs is different
2013 * in the two basic maps. In case the number of divs is the same,
2014 * we have already established that the divs are different
2015 * in the two basic maps.
2016 * In particular, if the number of divs of basic map i is smaller than
2017 * the number of divs of basic map j, then we check if j is a subset of i
2018 * and vice versa.
2020 static enum isl_change check_coalesce_subset(int i, int j,
2021 struct isl_coalesce_info *info)
2023 int changed;
2025 changed = coalesced_subset(i, j, info);
2026 if (changed < 0 || changed)
2027 return changed < 0 ? isl_change_error : isl_change_drop_second;
2029 changed = coalesced_subset(j, i, info);
2030 if (changed < 0 || changed)
2031 return changed < 0 ? isl_change_error : isl_change_drop_first;
2033 changed = coalesced_subset_with_equalities(i, j, info);
2034 if (changed < 0 || changed)
2035 return changed < 0 ? isl_change_error : isl_change_drop_second;
2037 changed = coalesced_subset_with_equalities(j, i, info);
2038 if (changed < 0 || changed)
2039 return changed < 0 ? isl_change_error : isl_change_drop_first;
2041 return isl_change_none;
2044 /* Does "bmap" involve any divs that themselves refer to divs?
2046 static int has_nested_div(__isl_keep isl_basic_map *bmap)
2048 int i;
2049 unsigned total;
2050 unsigned n_div;
2052 total = isl_basic_map_dim(bmap, isl_dim_all);
2053 n_div = isl_basic_map_dim(bmap, isl_dim_div);
2054 total -= n_div;
2056 for (i = 0; i < n_div; ++i)
2057 if (isl_seq_first_non_zero(bmap->div[i] + 2 + total,
2058 n_div) != -1)
2059 return 1;
2061 return 0;
2064 /* Return a list of affine expressions, one for each integer division
2065 * in "bmap_i". For each integer division that also appears in "bmap_j",
2066 * the affine expression is set to NaN. The number of NaNs in the list
2067 * is equal to the number of integer divisions in "bmap_j".
2068 * For the other integer divisions of "bmap_i", the corresponding
2069 * element in the list is a purely affine expression equal to the integer
2070 * division in "hull".
2071 * If no such list can be constructed, then the number of elements
2072 * in the returned list is smaller than the number of integer divisions
2073 * in "bmap_i".
2075 static __isl_give isl_aff_list *set_up_substitutions(
2076 __isl_keep isl_basic_map *bmap_i, __isl_keep isl_basic_map *bmap_j,
2077 __isl_take isl_basic_map *hull)
2079 unsigned n_div_i, n_div_j, total;
2080 isl_ctx *ctx;
2081 isl_local_space *ls;
2082 isl_basic_set *wrap_hull;
2083 isl_aff *aff_nan;
2084 isl_aff_list *list;
2085 int i, j;
2087 if (!hull)
2088 return NULL;
2090 ctx = isl_basic_map_get_ctx(hull);
2092 n_div_i = isl_basic_map_dim(bmap_i, isl_dim_div);
2093 n_div_j = isl_basic_map_dim(bmap_j, isl_dim_div);
2094 total = isl_basic_map_total_dim(bmap_i) - n_div_i;
2096 ls = isl_basic_map_get_local_space(bmap_i);
2097 ls = isl_local_space_wrap(ls);
2098 wrap_hull = isl_basic_map_wrap(hull);
2100 aff_nan = isl_aff_nan_on_domain(isl_local_space_copy(ls));
2101 list = isl_aff_list_alloc(ctx, n_div_i);
2103 j = 0;
2104 for (i = 0; i < n_div_i; ++i) {
2105 isl_aff *aff;
2107 if (j < n_div_j &&
2108 isl_seq_eq(bmap_i->div[i], bmap_j->div[j], 2 + total)) {
2109 ++j;
2110 list = isl_aff_list_add(list, isl_aff_copy(aff_nan));
2111 continue;
2113 if (n_div_i - i <= n_div_j - j)
2114 break;
2116 aff = isl_local_space_get_div(ls, i);
2117 aff = isl_aff_substitute_equalities(aff,
2118 isl_basic_set_copy(wrap_hull));
2119 aff = isl_aff_floor(aff);
2120 if (!aff)
2121 goto error;
2122 if (isl_aff_dim(aff, isl_dim_div) != 0) {
2123 isl_aff_free(aff);
2124 break;
2127 list = isl_aff_list_add(list, aff);
2130 isl_aff_free(aff_nan);
2131 isl_local_space_free(ls);
2132 isl_basic_set_free(wrap_hull);
2134 return list;
2135 error:
2136 isl_aff_free(aff_nan);
2137 isl_local_space_free(ls);
2138 isl_basic_set_free(wrap_hull);
2139 isl_aff_list_free(list);
2140 return NULL;
2143 /* Add variables to "tab" corresponding to the elements in "list"
2144 * that are not set to NaN.
2145 * "dim" is the offset in the variables of "tab" where we should
2146 * start considering the elements in "list".
2147 * When this function returns, the total number of variables in "tab"
2148 * is equal to "dim" plus the number of elements in "list".
2150 static int add_sub_vars(struct isl_tab *tab, __isl_keep isl_aff_list *list,
2151 int dim)
2153 int i, n;
2155 n = isl_aff_list_n_aff(list);
2156 for (i = 0; i < n; ++i) {
2157 int is_nan;
2158 isl_aff *aff;
2160 aff = isl_aff_list_get_aff(list, i);
2161 is_nan = isl_aff_is_nan(aff);
2162 isl_aff_free(aff);
2163 if (is_nan < 0)
2164 return -1;
2166 if (!is_nan && isl_tab_insert_var(tab, dim + i) < 0)
2167 return -1;
2170 return 0;
2173 /* For each element in "list" that is not set to NaN, fix the corresponding
2174 * variable in "tab" to the purely affine expression defined by the element.
2175 * "dim" is the offset in the variables of "tab" where we should
2176 * start considering the elements in "list".
2178 static int add_sub_equalities(struct isl_tab *tab,
2179 __isl_keep isl_aff_list *list, int dim)
2181 int i, n;
2182 isl_ctx *ctx;
2183 isl_vec *sub;
2184 isl_aff *aff;
2186 n = isl_aff_list_n_aff(list);
2188 ctx = isl_tab_get_ctx(tab);
2189 sub = isl_vec_alloc(ctx, 1 + dim + n);
2190 if (!sub)
2191 return -1;
2192 isl_seq_clr(sub->el + 1 + dim, n);
2194 for (i = 0; i < n; ++i) {
2195 aff = isl_aff_list_get_aff(list, i);
2196 if (!aff)
2197 goto error;
2198 if (isl_aff_is_nan(aff)) {
2199 isl_aff_free(aff);
2200 continue;
2202 isl_seq_cpy(sub->el, aff->v->el + 1, 1 + dim);
2203 isl_int_neg(sub->el[1 + dim + i], aff->v->el[0]);
2204 if (isl_tab_add_eq(tab, sub->el) < 0)
2205 goto error;
2206 isl_int_set_si(sub->el[1 + dim + i], 0);
2207 isl_aff_free(aff);
2210 isl_vec_free(sub);
2211 return 0;
2212 error:
2213 isl_aff_free(aff);
2214 isl_vec_free(sub);
2215 return -1;
2218 /* Add variables to info->tab corresponding to the elements in "list"
2219 * that are not set to NaN. The value of the added variable
2220 * is fixed to the purely affine expression defined by the element.
2221 * "dim" is the offset in the variables of info->tab where we should
2222 * start considering the elements in "list".
2223 * When this function returns, the total number of variables in info->tab
2224 * is equal to "dim" plus the number of elements in "list".
2225 * Additionally, add the div constraints that have been added info->bmap
2226 * after the tableau was constructed to info->tab. These constraints
2227 * start at position "n_ineq" in info->bmap.
2228 * The constraints need to be added to the tableau before
2229 * the equalities assigning the purely affine expression
2230 * because the position needs to match that in info->bmap.
2231 * They are frozen because the corresponding added equality is a consequence
2232 * of the two div constraints and the other equalities, meaning that
2233 * the div constraints would otherwise get marked as redundant,
2234 * while they are only redundant with respect to the extra equalities
2235 * added to the tableau, which do not appear explicitly in the basic map.
2237 static int add_subs(struct isl_coalesce_info *info,
2238 __isl_keep isl_aff_list *list, int dim, int n_ineq)
2240 int i, extra_var, extra_con;
2241 int n;
2242 unsigned n_eq = info->bmap->n_eq;
2244 if (!list)
2245 return -1;
2247 n = isl_aff_list_n_aff(list);
2248 extra_var = n - (info->tab->n_var - dim);
2249 extra_con = info->bmap->n_ineq - n_ineq;
2251 if (isl_tab_extend_vars(info->tab, extra_var) < 0)
2252 return -1;
2253 if (isl_tab_extend_cons(info->tab, extra_con + 2 * extra_var) < 0)
2254 return -1;
2255 if (add_sub_vars(info->tab, list, dim) < 0)
2256 return -1;
2258 for (i = n_ineq; i < info->bmap->n_ineq; ++i) {
2259 if (isl_tab_add_ineq(info->tab, info->bmap->ineq[i]) < 0)
2260 return -1;
2261 if (isl_tab_freeze_constraint(info->tab, n_eq + i) < 0)
2262 return -1;
2265 return add_sub_equalities(info->tab, list, dim);
2268 /* Coalesce basic map "j" into basic map "i" after adding the extra integer
2269 * divisions in "i" but not in "j" to basic map "j", with values
2270 * specified by "list". The total number of elements in "list"
2271 * is equal to the number of integer divisions in "i", while the number
2272 * of NaN elements in the list is equal to the number of integer divisions
2273 * in "j".
2274 * Adding extra integer divisions to "j" through isl_basic_map_align_divs
2275 * also adds the corresponding div constraints. These need to be added
2276 * to the corresponding tableau as well in add_subs to maintain consistency.
2278 * If no coalescing can be performed, then we need to revert basic map "j"
2279 * to its original state. We do the same if basic map "i" gets dropped
2280 * during the coalescing, even though this should not happen in practice
2281 * since we have already checked for "j" being a subset of "i"
2282 * before we reach this stage.
2284 static enum isl_change coalesce_with_subs(int i, int j,
2285 struct isl_coalesce_info *info, __isl_keep isl_aff_list *list)
2287 isl_basic_map *bmap_j;
2288 struct isl_tab_undo *snap;
2289 unsigned dim;
2290 enum isl_change change;
2291 int n_ineq;
2293 bmap_j = isl_basic_map_copy(info[j].bmap);
2294 n_ineq = info[j].bmap->n_ineq;
2295 info[j].bmap = isl_basic_map_align_divs(info[j].bmap, info[i].bmap);
2296 if (!info[j].bmap)
2297 goto error;
2299 snap = isl_tab_snap(info[j].tab);
2301 dim = isl_basic_map_dim(bmap_j, isl_dim_all);
2302 dim -= isl_basic_map_dim(bmap_j, isl_dim_div);
2303 if (add_subs(&info[j], list, dim, n_ineq) < 0)
2304 goto error;
2306 change = coalesce_local_pair(i, j, info);
2307 if (change != isl_change_none && change != isl_change_drop_first) {
2308 isl_basic_map_free(bmap_j);
2309 } else {
2310 isl_basic_map_free(info[j].bmap);
2311 info[j].bmap = bmap_j;
2313 if (isl_tab_rollback(info[j].tab, snap) < 0)
2314 return isl_change_error;
2317 return change;
2318 error:
2319 isl_basic_map_free(bmap_j);
2320 return isl_change_error;
2323 /* Check if we can coalesce basic map "j" into basic map "i" after copying
2324 * those extra integer divisions in "i" that can be simplified away
2325 * using the extra equalities in "j".
2326 * All divs are assumed to be known and not contain any nested divs.
2328 * We first check if there are any extra equalities in "j" that we
2329 * can exploit. Then we check if every integer division in "i"
2330 * either already appears in "j" or can be simplified using the
2331 * extra equalities to a purely affine expression.
2332 * If these tests succeed, then we try to coalesce the two basic maps
2333 * by introducing extra dimensions in "j" corresponding to
2334 * the extra integer divsisions "i" fixed to the corresponding
2335 * purely affine expression.
2337 static enum isl_change check_coalesce_into_eq(int i, int j,
2338 struct isl_coalesce_info *info)
2340 unsigned n_div_i, n_div_j;
2341 isl_basic_map *hull_i, *hull_j;
2342 int equal, empty;
2343 isl_aff_list *list;
2344 enum isl_change change;
2346 n_div_i = isl_basic_map_dim(info[i].bmap, isl_dim_div);
2347 n_div_j = isl_basic_map_dim(info[j].bmap, isl_dim_div);
2348 if (n_div_i <= n_div_j)
2349 return isl_change_none;
2350 if (info[j].bmap->n_eq == 0)
2351 return isl_change_none;
2353 hull_i = isl_basic_map_copy(info[i].bmap);
2354 hull_i = isl_basic_map_plain_affine_hull(hull_i);
2355 hull_j = isl_basic_map_copy(info[j].bmap);
2356 hull_j = isl_basic_map_plain_affine_hull(hull_j);
2358 hull_j = isl_basic_map_intersect(hull_j, isl_basic_map_copy(hull_i));
2359 equal = isl_basic_map_plain_is_equal(hull_i, hull_j);
2360 empty = isl_basic_map_plain_is_empty(hull_j);
2361 isl_basic_map_free(hull_i);
2363 if (equal < 0 || empty < 0)
2364 goto error;
2365 if (equal || empty) {
2366 isl_basic_map_free(hull_j);
2367 return isl_change_none;
2370 list = set_up_substitutions(info[i].bmap, info[j].bmap, hull_j);
2371 if (!list)
2372 return isl_change_error;
2373 if (isl_aff_list_n_aff(list) < n_div_i)
2374 change = isl_change_none;
2375 else
2376 change = coalesce_with_subs(i, j, info, list);
2378 isl_aff_list_free(list);
2380 return change;
2381 error:
2382 isl_basic_map_free(hull_j);
2383 return isl_change_error;
2386 /* Check if we can coalesce basic maps "i" and "j" after copying
2387 * those extra integer divisions in one of the basic maps that can
2388 * be simplified away using the extra equalities in the other basic map.
2389 * We require all divs to be known in both basic maps.
2390 * Furthermore, to simplify the comparison of div expressions,
2391 * we do not allow any nested integer divisions.
2393 static enum isl_change check_coalesce_eq(int i, int j,
2394 struct isl_coalesce_info *info)
2396 int known, nested;
2397 enum isl_change change;
2399 known = isl_basic_map_divs_known(info[i].bmap);
2400 if (known < 0 || !known)
2401 return known < 0 ? isl_change_error : isl_change_none;
2402 known = isl_basic_map_divs_known(info[j].bmap);
2403 if (known < 0 || !known)
2404 return known < 0 ? isl_change_error : isl_change_none;
2405 nested = has_nested_div(info[i].bmap);
2406 if (nested < 0 || nested)
2407 return nested < 0 ? isl_change_error : isl_change_none;
2408 nested = has_nested_div(info[j].bmap);
2409 if (nested < 0 || nested)
2410 return nested < 0 ? isl_change_error : isl_change_none;
2412 change = check_coalesce_into_eq(i, j, info);
2413 if (change != isl_change_none)
2414 return change;
2415 change = check_coalesce_into_eq(j, i, info);
2416 if (change != isl_change_none)
2417 return invert_change(change);
2419 return isl_change_none;
2422 /* Check if the union of the given pair of basic maps
2423 * can be represented by a single basic map.
2424 * If so, replace the pair by the single basic map and return
2425 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
2426 * Otherwise, return isl_change_none.
2428 * We first check if the two basic maps live in the same local space,
2429 * after aligning the divs that differ by only an integer constant.
2430 * If so, we do the complete check. Otherwise, we check if they have
2431 * the same number of integer divisions and can be coalesced, if one is
2432 * an obvious subset of the other or if the extra integer divisions
2433 * of one basic map can be simplified away using the extra equalities
2434 * of the other basic map.
2436 static enum isl_change coalesce_pair(int i, int j,
2437 struct isl_coalesce_info *info)
2439 int same;
2440 enum isl_change change;
2442 if (harmonize_divs(&info[i], &info[j]) < 0)
2443 return isl_change_error;
2444 same = same_divs(info[i].bmap, info[j].bmap);
2445 if (same < 0)
2446 return isl_change_error;
2447 if (same)
2448 return coalesce_local_pair(i, j, info);
2450 if (info[i].bmap->n_div == info[j].bmap->n_div) {
2451 change = coalesce_local_pair(i, j, info);
2452 if (change != isl_change_none)
2453 return change;
2456 change = check_coalesce_subset(i, j, info);
2457 if (change != isl_change_none)
2458 return change;
2460 return check_coalesce_eq(i, j, info);
2463 /* Return the maximum of "a" and "b".
2465 static inline int max(int a, int b)
2467 return a > b ? a : b;
2470 /* Pairwise coalesce the basic maps in the range [start1, end1[ of "info"
2471 * with those in the range [start2, end2[, skipping basic maps
2472 * that have been removed (either before or within this function).
2474 * For each basic map i in the first range, we check if it can be coalesced
2475 * with respect to any previously considered basic map j in the second range.
2476 * If i gets dropped (because it was a subset of some j), then
2477 * we can move on to the next basic map.
2478 * If j gets dropped, we need to continue checking against the other
2479 * previously considered basic maps.
2480 * If the two basic maps got fused, then we recheck the fused basic map
2481 * against the previously considered basic maps, starting at i + 1
2482 * (even if start2 is greater than i + 1).
2484 static int coalesce_range(isl_ctx *ctx, struct isl_coalesce_info *info,
2485 int start1, int end1, int start2, int end2)
2487 int i, j;
2489 for (i = end1 - 1; i >= start1; --i) {
2490 if (info[i].removed)
2491 continue;
2492 for (j = max(i + 1, start2); j < end2; ++j) {
2493 enum isl_change changed;
2495 if (info[j].removed)
2496 continue;
2497 if (info[i].removed)
2498 isl_die(ctx, isl_error_internal,
2499 "basic map unexpectedly removed",
2500 return -1);
2501 changed = coalesce_pair(i, j, info);
2502 switch (changed) {
2503 case isl_change_error:
2504 return -1;
2505 case isl_change_none:
2506 case isl_change_drop_second:
2507 continue;
2508 case isl_change_drop_first:
2509 j = end2;
2510 break;
2511 case isl_change_fuse:
2512 j = i;
2513 break;
2518 return 0;
2521 /* Pairwise coalesce the basic maps described by the "n" elements of "info".
2523 * We consider groups of basic maps that live in the same apparent
2524 * affine hull and we first coalesce within such a group before we
2525 * coalesce the elements in the group with elements of previously
2526 * considered groups. If a fuse happens during the second phase,
2527 * then we also reconsider the elements within the group.
2529 static int coalesce(isl_ctx *ctx, int n, struct isl_coalesce_info *info)
2531 int start, end;
2533 for (end = n; end > 0; end = start) {
2534 start = end - 1;
2535 while (start >= 1 &&
2536 info[start - 1].hull_hash == info[start].hull_hash)
2537 start--;
2538 if (coalesce_range(ctx, info, start, end, start, end) < 0)
2539 return -1;
2540 if (coalesce_range(ctx, info, start, end, end, n) < 0)
2541 return -1;
2544 return 0;
2547 /* Update the basic maps in "map" based on the information in "info".
2548 * In particular, remove the basic maps that have been marked removed and
2549 * update the others based on the information in the corresponding tableau.
2550 * Since we detected implicit equalities without calling
2551 * isl_basic_map_gauss, we need to do it now.
2552 * Also call isl_basic_map_simplify if we may have lost the definition
2553 * of one or more integer divisions.
2555 static __isl_give isl_map *update_basic_maps(__isl_take isl_map *map,
2556 int n, struct isl_coalesce_info *info)
2558 int i;
2560 if (!map)
2561 return NULL;
2563 for (i = n - 1; i >= 0; --i) {
2564 if (info[i].removed) {
2565 isl_basic_map_free(map->p[i]);
2566 if (i != map->n - 1)
2567 map->p[i] = map->p[map->n - 1];
2568 map->n--;
2569 continue;
2572 info[i].bmap = isl_basic_map_update_from_tab(info[i].bmap,
2573 info[i].tab);
2574 info[i].bmap = isl_basic_map_gauss(info[i].bmap, NULL);
2575 if (info[i].simplify)
2576 info[i].bmap = isl_basic_map_simplify(info[i].bmap);
2577 info[i].bmap = isl_basic_map_finalize(info[i].bmap);
2578 if (!info[i].bmap)
2579 return isl_map_free(map);
2580 ISL_F_SET(info[i].bmap, ISL_BASIC_MAP_NO_IMPLICIT);
2581 ISL_F_SET(info[i].bmap, ISL_BASIC_MAP_NO_REDUNDANT);
2582 isl_basic_map_free(map->p[i]);
2583 map->p[i] = info[i].bmap;
2584 info[i].bmap = NULL;
2587 return map;
2590 /* For each pair of basic maps in the map, check if the union of the two
2591 * can be represented by a single basic map.
2592 * If so, replace the pair by the single basic map and start over.
2594 * We factor out any (hidden) common factor from the constraint
2595 * coefficients to improve the detection of adjacent constraints.
2597 * Since we are constructing the tableaus of the basic maps anyway,
2598 * we exploit them to detect implicit equalities and redundant constraints.
2599 * This also helps the coalescing as it can ignore the redundant constraints.
2600 * In order to avoid confusion, we make all implicit equalities explicit
2601 * in the basic maps. We don't call isl_basic_map_gauss, though,
2602 * as that may affect the number of constraints.
2603 * This means that we have to call isl_basic_map_gauss at the end
2604 * of the computation (in update_basic_maps) to ensure that
2605 * the basic maps are not left in an unexpected state.
2606 * For each basic map, we also compute the hash of the apparent affine hull
2607 * for use in coalesce.
2609 struct isl_map *isl_map_coalesce(struct isl_map *map)
2611 int i;
2612 unsigned n;
2613 isl_ctx *ctx;
2614 struct isl_coalesce_info *info = NULL;
2616 map = isl_map_remove_empty_parts(map);
2617 if (!map)
2618 return NULL;
2620 if (map->n <= 1)
2621 return map;
2623 ctx = isl_map_get_ctx(map);
2624 map = isl_map_sort_divs(map);
2625 map = isl_map_cow(map);
2627 if (!map)
2628 return NULL;
2630 n = map->n;
2632 info = isl_calloc_array(map->ctx, struct isl_coalesce_info, n);
2633 if (!info)
2634 goto error;
2636 for (i = 0; i < map->n; ++i) {
2637 map->p[i] = isl_basic_map_reduce_coefficients(map->p[i]);
2638 if (!map->p[i])
2639 goto error;
2640 info[i].bmap = isl_basic_map_copy(map->p[i]);
2641 info[i].tab = isl_tab_from_basic_map(info[i].bmap, 0);
2642 if (!info[i].tab)
2643 goto error;
2644 if (!ISL_F_ISSET(info[i].bmap, ISL_BASIC_MAP_NO_IMPLICIT))
2645 if (isl_tab_detect_implicit_equalities(info[i].tab) < 0)
2646 goto error;
2647 info[i].bmap = isl_tab_make_equalities_explicit(info[i].tab,
2648 info[i].bmap);
2649 if (!info[i].bmap)
2650 goto error;
2651 if (!ISL_F_ISSET(info[i].bmap, ISL_BASIC_MAP_NO_REDUNDANT))
2652 if (isl_tab_detect_redundant(info[i].tab) < 0)
2653 goto error;
2654 if (coalesce_info_set_hull_hash(&info[i]) < 0)
2655 goto error;
2657 for (i = map->n - 1; i >= 0; --i)
2658 if (info[i].tab->empty)
2659 drop(&info[i]);
2661 if (coalesce(ctx, n, info) < 0)
2662 goto error;
2664 map = update_basic_maps(map, n, info);
2666 clear_coalesce_info(n, info);
2668 return map;
2669 error:
2670 clear_coalesce_info(n, info);
2671 isl_map_free(map);
2672 return NULL;
2675 /* For each pair of basic sets in the set, check if the union of the two
2676 * can be represented by a single basic set.
2677 * If so, replace the pair by the single basic set and start over.
2679 struct isl_set *isl_set_coalesce(struct isl_set *set)
2681 return (struct isl_set *)isl_map_coalesce((struct isl_map *)set);