hide isl_map_add_basic_map
[isl.git] / isl_coalesce.c
blob5e7f9a8d9e7335ed774ce038ae1b71f42b07d49a
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;
276 return isl_change_error;
279 /* Add the valid constraints of the basic map represented by "info"
280 * to "bmap". "len" is the size of the constraints.
281 * If only one of the pair of inequalities that make up an equality
282 * is valid, then add that inequality.
284 static __isl_give isl_basic_map *add_valid_constraints(
285 __isl_take isl_basic_map *bmap, struct isl_coalesce_info *info,
286 unsigned len)
288 int k, l;
290 if (!bmap)
291 return NULL;
293 for (k = 0; k < info->bmap->n_eq; ++k) {
294 if (info->eq[2 * k] == STATUS_VALID &&
295 info->eq[2 * k + 1] == STATUS_VALID) {
296 l = isl_basic_map_alloc_equality(bmap);
297 if (l < 0)
298 return isl_basic_map_free(bmap);
299 isl_seq_cpy(bmap->eq[l], info->bmap->eq[k], len);
300 } else if (info->eq[2 * k] == STATUS_VALID) {
301 l = isl_basic_map_alloc_inequality(bmap);
302 if (l < 0)
303 return isl_basic_map_free(bmap);
304 isl_seq_neg(bmap->ineq[l], info->bmap->eq[k], len);
305 } else if (info->eq[2 * k + 1] == STATUS_VALID) {
306 l = isl_basic_map_alloc_inequality(bmap);
307 if (l < 0)
308 return isl_basic_map_free(bmap);
309 isl_seq_cpy(bmap->ineq[l], info->bmap->eq[k], len);
313 for (k = 0; k < info->bmap->n_ineq; ++k) {
314 if (info->ineq[k] != STATUS_VALID)
315 continue;
316 l = isl_basic_map_alloc_inequality(bmap);
317 if (l < 0)
318 return isl_basic_map_free(bmap);
319 isl_seq_cpy(bmap->ineq[l], info->bmap->ineq[k], len);
322 return bmap;
325 /* Is "bmap" defined by a number of (non-redundant) constraints that
326 * is greater than the number of constraints of basic maps i and j combined?
327 * Equalities are counted as two inequalities.
329 static int number_of_constraints_increases(int i, int j,
330 struct isl_coalesce_info *info,
331 __isl_keep isl_basic_map *bmap, struct isl_tab *tab)
333 int k, n_old, n_new;
335 n_old = 2 * info[i].bmap->n_eq + info[i].bmap->n_ineq;
336 n_old += 2 * info[j].bmap->n_eq + info[j].bmap->n_ineq;
338 n_new = 2 * bmap->n_eq;
339 for (k = 0; k < bmap->n_ineq; ++k)
340 if (!isl_tab_is_redundant(tab, bmap->n_eq + k))
341 ++n_new;
343 return n_new > n_old;
346 /* Replace the pair of basic maps i and j by the basic map bounded
347 * by the valid constraints in both basic maps and the constraints
348 * in extra (if not NULL).
349 * Place the fused basic map in the position that is the smallest of i and j.
351 * If "detect_equalities" is set, then look for equalities encoded
352 * as pairs of inequalities.
353 * If "check_number" is set, then the original basic maps are only
354 * replaced if the total number of constraints does not increase.
355 * While the number of integer divisions in the two basic maps
356 * is assumed to be the same, the actual definitions may be different.
357 * We only copy the definition from one of the basic map if it is
358 * the same as that of the other basic map. Otherwise, we mark
359 * the integer division as unknown and schedule for the basic map
360 * to be simplified in an attempt to recover the integer division definition.
362 static enum isl_change fuse(int i, int j, struct isl_coalesce_info *info,
363 __isl_keep isl_mat *extra, int detect_equalities, int check_number)
365 int k, l;
366 struct isl_basic_map *fused = NULL;
367 struct isl_tab *fused_tab = NULL;
368 unsigned total = isl_basic_map_total_dim(info[i].bmap);
369 unsigned extra_rows = extra ? extra->n_row : 0;
370 unsigned n_eq, n_ineq;
372 if (j < i)
373 return fuse(j, i, info, extra, detect_equalities, check_number);
375 n_eq = info[i].bmap->n_eq + info[j].bmap->n_eq;
376 n_ineq = info[i].bmap->n_ineq + info[j].bmap->n_ineq;
377 fused = isl_basic_map_alloc_space(isl_space_copy(info[i].bmap->dim),
378 info[i].bmap->n_div, n_eq, n_eq + n_ineq + extra_rows);
379 fused = add_valid_constraints(fused, &info[i], 1 + total);
380 fused = add_valid_constraints(fused, &info[j], 1 + total);
381 if (!fused)
382 goto error;
384 for (k = 0; k < info[i].bmap->n_div; ++k) {
385 int l = isl_basic_map_alloc_div(fused);
386 if (l < 0)
387 goto error;
388 if (isl_seq_eq(info[i].bmap->div[k], info[j].bmap->div[k],
389 1 + 1 + total)) {
390 isl_seq_cpy(fused->div[l], info[i].bmap->div[k],
391 1 + 1 + total);
392 } else {
393 isl_int_set_si(fused->div[l][0], 0);
394 info[i].simplify = 1;
398 for (k = 0; k < extra_rows; ++k) {
399 l = isl_basic_map_alloc_inequality(fused);
400 if (l < 0)
401 goto error;
402 isl_seq_cpy(fused->ineq[l], extra->row[k], 1 + total);
405 if (detect_equalities)
406 fused = isl_basic_map_detect_inequality_pairs(fused, NULL);
407 fused = isl_basic_map_gauss(fused, NULL);
408 ISL_F_SET(fused, ISL_BASIC_MAP_FINAL);
409 if (ISL_F_ISSET(info[i].bmap, ISL_BASIC_MAP_RATIONAL) &&
410 ISL_F_ISSET(info[j].bmap, ISL_BASIC_MAP_RATIONAL))
411 ISL_F_SET(fused, ISL_BASIC_MAP_RATIONAL);
413 fused_tab = isl_tab_from_basic_map(fused, 0);
414 if (isl_tab_detect_redundant(fused_tab) < 0)
415 goto error;
417 if (check_number &&
418 number_of_constraints_increases(i, j, info, fused, fused_tab)) {
419 isl_tab_free(fused_tab);
420 isl_basic_map_free(fused);
421 return isl_change_none;
424 info[i].simplify |= info[j].simplify;
425 isl_basic_map_free(info[i].bmap);
426 info[i].bmap = fused;
427 isl_tab_free(info[i].tab);
428 info[i].tab = fused_tab;
429 drop(&info[j]);
431 return isl_change_fuse;
432 error:
433 isl_tab_free(fused_tab);
434 isl_basic_map_free(fused);
435 return isl_change_error;
438 /* Given a pair of basic maps i and j such that all constraints are either
439 * "valid" or "cut", check if the facets corresponding to the "cut"
440 * constraints of i lie entirely within basic map j.
441 * If so, replace the pair by the basic map consisting of the valid
442 * constraints in both basic maps.
443 * Checking whether the facet lies entirely within basic map j
444 * is performed by checking whether the constraints of basic map j
445 * are valid for the facet. These tests are performed on a rational
446 * tableau to avoid the theoretical possibility that a constraint
447 * that was considered to be a cut constraint for the entire basic map i
448 * happens to be considered to be a valid constraint for the facet,
449 * even though it cuts off the same rational points.
451 * To see that we are not introducing any extra points, call the
452 * two basic maps A and B and the resulting map U and let x
453 * be an element of U \setminus ( A \cup B ).
454 * A line connecting x with an element of A \cup B meets a facet F
455 * of either A or B. Assume it is a facet of B and let c_1 be
456 * the corresponding facet constraint. We have c_1(x) < 0 and
457 * so c_1 is a cut constraint. This implies that there is some
458 * (possibly rational) point x' satisfying the constraints of A
459 * and the opposite of c_1 as otherwise c_1 would have been marked
460 * valid for A. The line connecting x and x' meets a facet of A
461 * in a (possibly rational) point that also violates c_1, but this
462 * is impossible since all cut constraints of B are valid for all
463 * cut facets of A.
464 * In case F is a facet of A rather than B, then we can apply the
465 * above reasoning to find a facet of B separating x from A \cup B first.
467 static enum isl_change check_facets(int i, int j,
468 struct isl_coalesce_info *info)
470 int k, l;
471 struct isl_tab_undo *snap, *snap2;
472 unsigned n_eq = info[i].bmap->n_eq;
474 snap = isl_tab_snap(info[i].tab);
475 if (isl_tab_mark_rational(info[i].tab) < 0)
476 return isl_change_error;
477 snap2 = isl_tab_snap(info[i].tab);
479 for (k = 0; k < info[i].bmap->n_ineq; ++k) {
480 if (info[i].ineq[k] != STATUS_CUT)
481 continue;
482 if (isl_tab_select_facet(info[i].tab, n_eq + k) < 0)
483 return isl_change_error;
484 for (l = 0; l < info[j].bmap->n_ineq; ++l) {
485 int stat;
486 if (info[j].ineq[l] != STATUS_CUT)
487 continue;
488 stat = status_in(info[j].bmap->ineq[l], info[i].tab);
489 if (stat < 0)
490 return isl_change_error;
491 if (stat != STATUS_VALID)
492 break;
494 if (isl_tab_rollback(info[i].tab, snap2) < 0)
495 return isl_change_error;
496 if (l < info[j].bmap->n_ineq)
497 break;
500 if (k < info[i].bmap->n_ineq) {
501 if (isl_tab_rollback(info[i].tab, snap) < 0)
502 return isl_change_error;
503 return isl_change_none;
505 return fuse(i, j, info, NULL, 0, 0);
508 /* Check if info->bmap contains the basic map represented
509 * by the tableau "tab".
510 * For each equality, we check both the constraint itself
511 * (as an inequality) and its negation. Make sure the
512 * equality is returned to its original state before returning.
514 static int contains(struct isl_coalesce_info *info, struct isl_tab *tab)
516 int k;
517 unsigned dim;
518 isl_basic_map *bmap = info->bmap;
520 dim = isl_basic_map_total_dim(bmap);
521 for (k = 0; k < bmap->n_eq; ++k) {
522 int stat;
523 isl_seq_neg(bmap->eq[k], bmap->eq[k], 1 + dim);
524 stat = status_in(bmap->eq[k], tab);
525 isl_seq_neg(bmap->eq[k], bmap->eq[k], 1 + dim);
526 if (stat < 0)
527 return -1;
528 if (stat != STATUS_VALID)
529 return 0;
530 stat = status_in(bmap->eq[k], tab);
531 if (stat < 0)
532 return -1;
533 if (stat != STATUS_VALID)
534 return 0;
537 for (k = 0; k < bmap->n_ineq; ++k) {
538 int stat;
539 if (info->ineq[k] == STATUS_REDUNDANT)
540 continue;
541 stat = status_in(bmap->ineq[k], tab);
542 if (stat < 0)
543 return -1;
544 if (stat != STATUS_VALID)
545 return 0;
547 return 1;
550 /* Basic map "i" has an inequality (say "k") that is adjacent
551 * to some inequality of basic map "j". All the other inequalities
552 * are valid for "j".
553 * Check if basic map "j" forms an extension of basic map "i".
555 * Note that this function is only called if some of the equalities or
556 * inequalities of basic map "j" do cut basic map "i". The function is
557 * correct even if there are no such cut constraints, but in that case
558 * the additional checks performed by this function are overkill.
560 * In particular, we replace constraint k, say f >= 0, by constraint
561 * f <= -1, add the inequalities of "j" that are valid for "i"
562 * and check if the result is a subset of basic map "j".
563 * If so, then we know that this result is exactly equal to basic map "j"
564 * since all its constraints are valid for basic map "j".
565 * By combining the valid constraints of "i" (all equalities and all
566 * inequalities except "k") and the valid constraints of "j" we therefore
567 * obtain a basic map that is equal to their union.
568 * In this case, there is no need to perform a rollback of the tableau
569 * since it is going to be destroyed in fuse().
572 * |\__ |\__
573 * | \__ | \__
574 * | \_ => | \__
575 * |_______| _ |_________\
578 * |\ |\
579 * | \ | \
580 * | \ | \
581 * | | | \
582 * | ||\ => | \
583 * | || \ | \
584 * | || | | |
585 * |__||_/ |_____/
587 static enum isl_change is_adj_ineq_extension(int i, int j,
588 struct isl_coalesce_info *info)
590 int k;
591 struct isl_tab_undo *snap;
592 unsigned n_eq = info[i].bmap->n_eq;
593 unsigned total = isl_basic_map_total_dim(info[i].bmap);
594 int r;
595 int super;
597 if (isl_tab_extend_cons(info[i].tab, 1 + info[j].bmap->n_ineq) < 0)
598 return isl_change_error;
600 for (k = 0; k < info[i].bmap->n_ineq; ++k)
601 if (info[i].ineq[k] == STATUS_ADJ_INEQ)
602 break;
603 if (k >= info[i].bmap->n_ineq)
604 isl_die(isl_basic_map_get_ctx(info[i].bmap), isl_error_internal,
605 "info[i].ineq should have exactly one STATUS_ADJ_INEQ",
606 return isl_change_error);
608 snap = isl_tab_snap(info[i].tab);
610 if (isl_tab_unrestrict(info[i].tab, n_eq + k) < 0)
611 return isl_change_error;
613 isl_seq_neg(info[i].bmap->ineq[k], info[i].bmap->ineq[k], 1 + total);
614 isl_int_sub_ui(info[i].bmap->ineq[k][0], info[i].bmap->ineq[k][0], 1);
615 r = isl_tab_add_ineq(info[i].tab, info[i].bmap->ineq[k]);
616 isl_seq_neg(info[i].bmap->ineq[k], info[i].bmap->ineq[k], 1 + total);
617 isl_int_sub_ui(info[i].bmap->ineq[k][0], info[i].bmap->ineq[k][0], 1);
618 if (r < 0)
619 return isl_change_error;
621 for (k = 0; k < info[j].bmap->n_ineq; ++k) {
622 if (info[j].ineq[k] != STATUS_VALID)
623 continue;
624 if (isl_tab_add_ineq(info[i].tab, info[j].bmap->ineq[k]) < 0)
625 return isl_change_error;
628 super = contains(&info[j], info[i].tab);
629 if (super < 0)
630 return isl_change_error;
631 if (super)
632 return fuse(i, j, info, NULL, 0, 0);
634 if (isl_tab_rollback(info[i].tab, snap) < 0)
635 return isl_change_error;
637 return isl_change_none;
641 /* Both basic maps have at least one inequality with and adjacent
642 * (but opposite) inequality in the other basic map.
643 * Check that there are no cut constraints and that there is only
644 * a single pair of adjacent inequalities.
645 * If so, we can replace the pair by a single basic map described
646 * by all but the pair of adjacent inequalities.
647 * Any additional points introduced lie strictly between the two
648 * adjacent hyperplanes and can therefore be integral.
650 * ____ _____
651 * / ||\ / \
652 * / || \ / \
653 * \ || \ => \ \
654 * \ || / \ /
655 * \___||_/ \_____/
657 * The test for a single pair of adjancent inequalities is important
658 * for avoiding the combination of two basic maps like the following
660 * /|
661 * / |
662 * /__|
663 * _____
664 * | |
665 * | |
666 * |___|
668 * If there are some cut constraints on one side, then we may
669 * still be able to fuse the two basic maps, but we need to perform
670 * some additional checks in is_adj_ineq_extension.
672 static enum isl_change check_adj_ineq(int i, int j,
673 struct isl_coalesce_info *info)
675 int count_i, count_j;
676 int cut_i, cut_j;
678 count_i = count(info[i].ineq, info[i].bmap->n_ineq, STATUS_ADJ_INEQ);
679 count_j = count(info[j].ineq, info[j].bmap->n_ineq, STATUS_ADJ_INEQ);
681 if (count_i != 1 && count_j != 1)
682 return isl_change_none;
684 cut_i = any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_CUT) ||
685 any(info[i].ineq, info[i].bmap->n_ineq, STATUS_CUT);
686 cut_j = any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_CUT) ||
687 any(info[j].ineq, info[j].bmap->n_ineq, STATUS_CUT);
689 if (!cut_i && !cut_j && count_i == 1 && count_j == 1)
690 return fuse(i, j, info, NULL, 0, 0);
692 if (count_i == 1 && !cut_i)
693 return is_adj_ineq_extension(i, j, info);
695 if (count_j == 1 && !cut_j)
696 return is_adj_ineq_extension(j, i, info);
698 return isl_change_none;
701 /* Basic map "i" has an inequality "k" that is adjacent to some equality
702 * of basic map "j". All the other inequalities are valid for "j".
703 * Check if basic map "j" forms an extension of basic map "i".
705 * In particular, we relax constraint "k", compute the corresponding
706 * facet and check whether it is included in the other basic map.
707 * If so, we know that relaxing the constraint extends the basic
708 * map with exactly the other basic map (we already know that this
709 * other basic map is included in the extension, because there
710 * were no "cut" inequalities in "i") and we can replace the
711 * two basic maps by this extension.
712 * Each integer division that does not have exactly the same
713 * definition in "i" and "j" is marked unknown and the basic map
714 * is scheduled to be simplified in an attempt to recover
715 * the integer division definition.
716 * Place this extension in the position that is the smallest of i and j.
717 * ____ _____
718 * / || / |
719 * / || / |
720 * \ || => \ |
721 * \ || \ |
722 * \___|| \____|
724 static enum isl_change is_adj_eq_extension(int i, int j, int k,
725 struct isl_coalesce_info *info)
727 int change = isl_change_none;
728 int super;
729 struct isl_tab_undo *snap, *snap2;
730 unsigned n_eq = info[i].bmap->n_eq;
732 if (isl_tab_is_equality(info[i].tab, n_eq + k))
733 return isl_change_none;
735 snap = isl_tab_snap(info[i].tab);
736 if (isl_tab_relax(info[i].tab, n_eq + k) < 0)
737 return isl_change_error;
738 snap2 = isl_tab_snap(info[i].tab);
739 if (isl_tab_select_facet(info[i].tab, n_eq + k) < 0)
740 return isl_change_error;
741 super = contains(&info[j], info[i].tab);
742 if (super < 0)
743 return isl_change_error;
744 if (super) {
745 int l;
746 unsigned total;
748 if (isl_tab_rollback(info[i].tab, snap2) < 0)
749 return isl_change_error;
750 info[i].bmap = isl_basic_map_cow(info[i].bmap);
751 if (!info[i].bmap)
752 return isl_change_error;
753 total = isl_basic_map_total_dim(info[i].bmap);
754 for (l = 0; l < info[i].bmap->n_div; ++l)
755 if (!isl_seq_eq(info[i].bmap->div[l],
756 info[j].bmap->div[l], 1 + 1 + total)) {
757 isl_int_set_si(info[i].bmap->div[l][0], 0);
758 info[i].simplify = 1;
760 isl_int_add_ui(info[i].bmap->ineq[k][0],
761 info[i].bmap->ineq[k][0], 1);
762 ISL_F_SET(info[i].bmap, ISL_BASIC_MAP_FINAL);
763 drop(&info[j]);
764 if (j < i)
765 exchange(&info[i], &info[j]);
766 change = isl_change_fuse;
767 } else
768 if (isl_tab_rollback(info[i].tab, snap) < 0)
769 return isl_change_error;
771 return change;
774 /* Data structure that keeps track of the wrapping constraints
775 * and of information to bound the coefficients of those constraints.
777 * bound is set if we want to apply a bound on the coefficients
778 * mat contains the wrapping constraints
779 * max is the bound on the coefficients (if bound is set)
781 struct isl_wraps {
782 int bound;
783 isl_mat *mat;
784 isl_int max;
787 /* Update wraps->max to be greater than or equal to the coefficients
788 * in the equalities and inequalities of info->bmap that can be removed
789 * if we end up applying wrapping.
791 static void wraps_update_max(struct isl_wraps *wraps,
792 struct isl_coalesce_info *info)
794 int k;
795 isl_int max_k;
796 unsigned total = isl_basic_map_total_dim(info->bmap);
798 isl_int_init(max_k);
800 for (k = 0; k < info->bmap->n_eq; ++k) {
801 if (info->eq[2 * k] == STATUS_VALID &&
802 info->eq[2 * k + 1] == STATUS_VALID)
803 continue;
804 isl_seq_abs_max(info->bmap->eq[k] + 1, total, &max_k);
805 if (isl_int_abs_gt(max_k, wraps->max))
806 isl_int_set(wraps->max, max_k);
809 for (k = 0; k < info->bmap->n_ineq; ++k) {
810 if (info->ineq[k] == STATUS_VALID ||
811 info->ineq[k] == STATUS_REDUNDANT)
812 continue;
813 isl_seq_abs_max(info->bmap->ineq[k] + 1, total, &max_k);
814 if (isl_int_abs_gt(max_k, wraps->max))
815 isl_int_set(wraps->max, max_k);
818 isl_int_clear(max_k);
821 /* Initialize the isl_wraps data structure.
822 * If we want to bound the coefficients of the wrapping constraints,
823 * we set wraps->max to the largest coefficient
824 * in the equalities and inequalities that can be removed if we end up
825 * applying wrapping.
827 static void wraps_init(struct isl_wraps *wraps, __isl_take isl_mat *mat,
828 struct isl_coalesce_info *info, int i, int j)
830 isl_ctx *ctx;
832 wraps->bound = 0;
833 wraps->mat = mat;
834 if (!mat)
835 return;
836 ctx = isl_mat_get_ctx(mat);
837 wraps->bound = isl_options_get_coalesce_bounded_wrapping(ctx);
838 if (!wraps->bound)
839 return;
840 isl_int_init(wraps->max);
841 isl_int_set_si(wraps->max, 0);
842 wraps_update_max(wraps, &info[i]);
843 wraps_update_max(wraps, &info[j]);
846 /* Free the contents of the isl_wraps data structure.
848 static void wraps_free(struct isl_wraps *wraps)
850 isl_mat_free(wraps->mat);
851 if (wraps->bound)
852 isl_int_clear(wraps->max);
855 /* Is the wrapping constraint in row "row" allowed?
857 * If wraps->bound is set, we check that none of the coefficients
858 * is greater than wraps->max.
860 static int allow_wrap(struct isl_wraps *wraps, int row)
862 int i;
864 if (!wraps->bound)
865 return 1;
867 for (i = 1; i < wraps->mat->n_col; ++i)
868 if (isl_int_abs_gt(wraps->mat->row[row][i], wraps->max))
869 return 0;
871 return 1;
874 /* Wrap "ineq" (or its opposite if "negate" is set) around "bound"
875 * to include "set" and add the result in position "w" of "wraps".
876 * "len" is the total number of coefficients in "bound" and "ineq".
877 * Return 1 on success, 0 on failure and -1 on error.
878 * Wrapping can fail if the result of wrapping is equal to "bound"
879 * or if we want to bound the sizes of the coefficients and
880 * the wrapped constraint does not satisfy this bound.
882 static int add_wrap(struct isl_wraps *wraps, int w, isl_int *bound,
883 isl_int *ineq, unsigned len, __isl_keep isl_set *set, int negate)
885 isl_seq_cpy(wraps->mat->row[w], bound, len);
886 if (negate) {
887 isl_seq_neg(wraps->mat->row[w + 1], ineq, len);
888 ineq = wraps->mat->row[w + 1];
890 if (!isl_set_wrap_facet(set, wraps->mat->row[w], ineq))
891 return -1;
892 if (isl_seq_eq(wraps->mat->row[w], bound, len))
893 return 0;
894 if (!allow_wrap(wraps, w))
895 return 0;
896 return 1;
899 /* For each constraint in info->bmap that is not redundant (as determined
900 * by info->tab) and that is not a valid constraint for the other basic map,
901 * wrap the constraint around "bound" such that it includes the whole
902 * set "set" and append the resulting constraint to "wraps".
903 * Note that the constraints that are valid for the other basic map
904 * will be added to the combined basic map by default, so there is
905 * no need to wrap them.
906 * The caller wrap_in_facets even relies on this function not wrapping
907 * any constraints that are already valid.
908 * "wraps" is assumed to have been pre-allocated to the appropriate size.
909 * wraps->n_row is the number of actual wrapped constraints that have
910 * been added.
911 * If any of the wrapping problems results in a constraint that is
912 * identical to "bound", then this means that "set" is unbounded in such
913 * way that no wrapping is possible. If this happens then wraps->n_row
914 * is reset to zero.
915 * Similarly, if we want to bound the coefficients of the wrapping
916 * constraints and a newly added wrapping constraint does not
917 * satisfy the bound, then wraps->n_row is also reset to zero.
919 static int add_wraps(struct isl_wraps *wraps, struct isl_coalesce_info *info,
920 isl_int *bound, __isl_keep isl_set *set)
922 int l, m;
923 int w;
924 int added;
925 isl_basic_map *bmap = info->bmap;
926 unsigned len = 1 + isl_basic_map_total_dim(bmap);
928 w = wraps->mat->n_row;
930 for (l = 0; l < bmap->n_ineq; ++l) {
931 if (info->ineq[l] == STATUS_VALID ||
932 info->ineq[l] == STATUS_REDUNDANT)
933 continue;
934 if (isl_seq_is_neg(bound, bmap->ineq[l], len))
935 continue;
936 if (isl_seq_eq(bound, bmap->ineq[l], len))
937 continue;
938 if (isl_tab_is_redundant(info->tab, bmap->n_eq + l))
939 continue;
941 added = add_wrap(wraps, w, bound, bmap->ineq[l], len, set, 0);
942 if (added < 0)
943 return -1;
944 if (!added)
945 goto unbounded;
946 ++w;
948 for (l = 0; l < bmap->n_eq; ++l) {
949 if (isl_seq_is_neg(bound, bmap->eq[l], len))
950 continue;
951 if (isl_seq_eq(bound, bmap->eq[l], len))
952 continue;
954 for (m = 0; m < 2; ++m) {
955 if (info->eq[2 * l + m] == STATUS_VALID)
956 continue;
957 added = add_wrap(wraps, w, bound, bmap->eq[l], len,
958 set, !m);
959 if (added < 0)
960 return -1;
961 if (!added)
962 goto unbounded;
963 ++w;
967 wraps->mat->n_row = w;
968 return 0;
969 unbounded:
970 wraps->mat->n_row = 0;
971 return 0;
974 /* Check if the constraints in "wraps" from "first" until the last
975 * are all valid for the basic set represented by "tab".
976 * If not, wraps->n_row is set to zero.
978 static int check_wraps(__isl_keep isl_mat *wraps, int first,
979 struct isl_tab *tab)
981 int i;
983 for (i = first; i < wraps->n_row; ++i) {
984 enum isl_ineq_type type;
985 type = isl_tab_ineq_type(tab, wraps->row[i]);
986 if (type == isl_ineq_error)
987 return -1;
988 if (type == isl_ineq_redundant)
989 continue;
990 wraps->n_row = 0;
991 return 0;
994 return 0;
997 /* Return a set that corresponds to the non-redundant constraints
998 * (as recorded in tab) of bmap.
1000 * It's important to remove the redundant constraints as some
1001 * of the other constraints may have been modified after the
1002 * constraints were marked redundant.
1003 * In particular, a constraint may have been relaxed.
1004 * Redundant constraints are ignored when a constraint is relaxed
1005 * and should therefore continue to be ignored ever after.
1006 * Otherwise, the relaxation might be thwarted by some of
1007 * these constraints.
1009 * Update the underlying set to ensure that the dimension doesn't change.
1010 * Otherwise the integer divisions could get dropped if the tab
1011 * turns out to be empty.
1013 static __isl_give isl_set *set_from_updated_bmap(__isl_keep isl_basic_map *bmap,
1014 struct isl_tab *tab)
1016 isl_basic_set *bset;
1018 bmap = isl_basic_map_copy(bmap);
1019 bset = isl_basic_map_underlying_set(bmap);
1020 bset = isl_basic_set_cow(bset);
1021 bset = isl_basic_set_update_from_tab(bset, tab);
1022 return isl_set_from_basic_set(bset);
1025 /* Wrap the constraints of info->bmap that bound the facet defined
1026 * by inequality "k" around (the opposite of) this inequality to
1027 * include "set". "bound" may be used to store the negated inequality.
1028 * Since the wrapped constraints are not guaranteed to contain the whole
1029 * of info->bmap, we check them in check_wraps.
1030 * If any of the wrapped constraints turn out to be invalid, then
1031 * check_wraps will reset wrap->n_row to zero.
1033 static int add_wraps_around_facet(struct isl_wraps *wraps,
1034 struct isl_coalesce_info *info, int k, isl_int *bound,
1035 __isl_keep isl_set *set)
1037 struct isl_tab_undo *snap;
1038 int n;
1039 unsigned total = isl_basic_map_total_dim(info->bmap);
1041 snap = isl_tab_snap(info->tab);
1043 if (isl_tab_select_facet(info->tab, info->bmap->n_eq + k) < 0)
1044 return -1;
1045 if (isl_tab_detect_redundant(info->tab) < 0)
1046 return -1;
1048 isl_seq_neg(bound, info->bmap->ineq[k], 1 + total);
1050 n = wraps->mat->n_row;
1051 if (add_wraps(wraps, info, bound, set) < 0)
1052 return -1;
1054 if (isl_tab_rollback(info->tab, snap) < 0)
1055 return -1;
1056 if (check_wraps(wraps->mat, n, info->tab) < 0)
1057 return -1;
1059 return 0;
1062 /* Given a basic set i with a constraint k that is adjacent to
1063 * basic set j, check if we can wrap
1064 * both the facet corresponding to k (if "wrap_facet" is set) and basic map j
1065 * (always) around their ridges to include the other set.
1066 * If so, replace the pair of basic sets by their union.
1068 * All constraints of i (except k) are assumed to be valid or
1069 * cut constraints for j.
1070 * Wrapping the cut constraints to include basic map j may result
1071 * in constraints that are no longer valid of basic map i
1072 * we have to check that the resulting wrapping constraints are valid for i.
1073 * If "wrap_facet" is not set, then all constraints of i (except k)
1074 * are assumed to be valid for j.
1075 * ____ _____
1076 * / | / \
1077 * / || / |
1078 * \ || => \ |
1079 * \ || \ |
1080 * \___|| \____|
1083 static enum isl_change can_wrap_in_facet(int i, int j, int k,
1084 struct isl_coalesce_info *info, int wrap_facet)
1086 enum isl_change change = isl_change_none;
1087 struct isl_wraps wraps;
1088 isl_ctx *ctx;
1089 isl_mat *mat;
1090 struct isl_set *set_i = NULL;
1091 struct isl_set *set_j = NULL;
1092 struct isl_vec *bound = NULL;
1093 unsigned total = isl_basic_map_total_dim(info[i].bmap);
1095 set_i = set_from_updated_bmap(info[i].bmap, info[i].tab);
1096 set_j = set_from_updated_bmap(info[j].bmap, info[j].tab);
1097 ctx = isl_basic_map_get_ctx(info[i].bmap);
1098 mat = isl_mat_alloc(ctx, 2 * (info[i].bmap->n_eq + info[j].bmap->n_eq) +
1099 info[i].bmap->n_ineq + info[j].bmap->n_ineq,
1100 1 + total);
1101 wraps_init(&wraps, mat, info, i, j);
1102 bound = isl_vec_alloc(ctx, 1 + total);
1103 if (!set_i || !set_j || !wraps.mat || !bound)
1104 goto error;
1106 isl_seq_cpy(bound->el, info[i].bmap->ineq[k], 1 + total);
1107 isl_int_add_ui(bound->el[0], bound->el[0], 1);
1109 isl_seq_cpy(wraps.mat->row[0], bound->el, 1 + total);
1110 wraps.mat->n_row = 1;
1112 if (add_wraps(&wraps, &info[j], bound->el, set_i) < 0)
1113 goto error;
1114 if (!wraps.mat->n_row)
1115 goto unbounded;
1117 if (wrap_facet) {
1118 if (add_wraps_around_facet(&wraps, &info[i], k,
1119 bound->el, set_j) < 0)
1120 goto error;
1121 if (!wraps.mat->n_row)
1122 goto unbounded;
1125 change = fuse(i, j, info, wraps.mat, 0, 0);
1127 unbounded:
1128 wraps_free(&wraps);
1130 isl_set_free(set_i);
1131 isl_set_free(set_j);
1133 isl_vec_free(bound);
1135 return change;
1136 error:
1137 wraps_free(&wraps);
1138 isl_vec_free(bound);
1139 isl_set_free(set_i);
1140 isl_set_free(set_j);
1141 return isl_change_error;
1144 /* Given a pair of basic maps i and j such that j sticks out
1145 * of i at n cut constraints, each time by at most one,
1146 * try to compute wrapping constraints and replace the two
1147 * basic maps by a single basic map.
1148 * The other constraints of i are assumed to be valid for j.
1150 * For each cut constraint t(x) >= 0 of i, we add the relaxed version
1151 * t(x) + 1 >= 0, along with wrapping constraints for all constraints
1152 * of basic map j that bound the part of basic map j that sticks out
1153 * of the cut constraint.
1154 * In particular, we first intersect basic map j with t(x) + 1 = 0.
1155 * If the result is empty, then t(x) >= 0 was actually a valid constraint
1156 * (with respect to the integer points), so we add t(x) >= 0 instead.
1157 * Otherwise, we wrap the constraints of basic map j that are not
1158 * redundant in this intersection and that are not already valid
1159 * for basic map i over basic map i.
1160 * Note that it is sufficient to wrap the constraints to include
1161 * basic map i, because we will only wrap the constraints that do
1162 * not include basic map i already. The wrapped constraint will
1163 * therefore be more relaxed compared to the original constraint.
1164 * Since the original constraint is valid for basic map j, so is
1165 * the wrapped constraint.
1167 * If any wrapping fails, i.e., if we cannot wrap to touch
1168 * the union, then we give up.
1169 * Otherwise, the pair of basic maps is replaced by their union.
1171 static enum isl_change wrap_in_facets(int i, int j, int *cuts, int n,
1172 struct isl_coalesce_info *info)
1174 enum isl_change change = isl_change_none;
1175 struct isl_wraps wraps;
1176 isl_ctx *ctx;
1177 isl_mat *mat;
1178 isl_set *set_i = NULL;
1179 unsigned total = isl_basic_map_total_dim(info[i].bmap);
1180 int max_wrap;
1181 int k, w;
1182 struct isl_tab_undo *snap;
1184 if (isl_tab_extend_cons(info[j].tab, 1) < 0)
1185 goto error;
1187 max_wrap = 1 + 2 * info[j].bmap->n_eq + info[j].bmap->n_ineq;
1188 max_wrap *= n;
1190 set_i = set_from_updated_bmap(info[i].bmap, info[i].tab);
1191 ctx = isl_basic_map_get_ctx(info[i].bmap);
1192 mat = isl_mat_alloc(ctx, max_wrap, 1 + total);
1193 wraps_init(&wraps, mat, info, i, j);
1194 if (!set_i || !wraps.mat)
1195 goto error;
1197 snap = isl_tab_snap(info[j].tab);
1199 wraps.mat->n_row = 0;
1201 for (k = 0; k < n; ++k) {
1202 w = wraps.mat->n_row++;
1203 isl_seq_cpy(wraps.mat->row[w],
1204 info[i].bmap->ineq[cuts[k]], 1 + total);
1205 isl_int_add_ui(wraps.mat->row[w][0], wraps.mat->row[w][0], 1);
1206 if (isl_tab_add_eq(info[j].tab, wraps.mat->row[w]) < 0)
1207 goto error;
1208 if (isl_tab_detect_redundant(info[j].tab) < 0)
1209 goto error;
1211 if (info[j].tab->empty)
1212 isl_int_sub_ui(wraps.mat->row[w][0],
1213 wraps.mat->row[w][0], 1);
1214 else if (add_wraps(&wraps, &info[j],
1215 wraps.mat->row[w], set_i) < 0)
1216 goto error;
1218 if (isl_tab_rollback(info[j].tab, snap) < 0)
1219 goto error;
1221 if (!wraps.mat->n_row)
1222 break;
1225 if (k == n)
1226 change = fuse(i, j, info, wraps.mat, 0, 1);
1228 wraps_free(&wraps);
1229 isl_set_free(set_i);
1231 return change;
1232 error:
1233 wraps_free(&wraps);
1234 isl_set_free(set_i);
1235 return isl_change_error;
1238 /* Given two basic sets i and j such that i has no cut equalities,
1239 * check if relaxing all the cut inequalities of i by one turns
1240 * them into valid constraint for j and check if we can wrap in
1241 * the bits that are sticking out.
1242 * If so, replace the pair by their union.
1244 * We first check if all relaxed cut inequalities of i are valid for j
1245 * and then try to wrap in the intersections of the relaxed cut inequalities
1246 * with j.
1248 * During this wrapping, we consider the points of j that lie at a distance
1249 * of exactly 1 from i. In particular, we ignore the points that lie in
1250 * between this lower-dimensional space and the basic map i.
1251 * We can therefore only apply this to integer maps.
1252 * ____ _____
1253 * / ___|_ / \
1254 * / | | / |
1255 * \ | | => \ |
1256 * \|____| \ |
1257 * \___| \____/
1259 * _____ ______
1260 * | ____|_ | \
1261 * | | | | |
1262 * | | | => | |
1263 * |_| | | |
1264 * |_____| \______|
1266 * _______
1267 * | |
1268 * | |\ |
1269 * | | \ |
1270 * | | \ |
1271 * | | \|
1272 * | | \
1273 * | |_____\
1274 * | |
1275 * |_______|
1277 * Wrapping can fail if the result of wrapping one of the facets
1278 * around its edges does not produce any new facet constraint.
1279 * In particular, this happens when we try to wrap in unbounded sets.
1281 * _______________________________________________________________________
1283 * | ___
1284 * | | |
1285 * |_| |_________________________________________________________________
1286 * |___|
1288 * The following is not an acceptable result of coalescing the above two
1289 * sets as it includes extra integer points.
1290 * _______________________________________________________________________
1292 * |
1293 * |
1295 * \______________________________________________________________________
1297 static enum isl_change can_wrap_in_set(int i, int j,
1298 struct isl_coalesce_info *info)
1300 enum isl_change change = isl_change_none;
1301 int k, m;
1302 int n;
1303 int *cuts = NULL;
1304 isl_ctx *ctx;
1306 if (ISL_F_ISSET(info[i].bmap, ISL_BASIC_MAP_RATIONAL) ||
1307 ISL_F_ISSET(info[j].bmap, ISL_BASIC_MAP_RATIONAL))
1308 return isl_change_none;
1310 n = count(info[i].ineq, info[i].bmap->n_ineq, STATUS_CUT);
1311 if (n == 0)
1312 return isl_change_none;
1314 ctx = isl_basic_map_get_ctx(info[i].bmap);
1315 cuts = isl_alloc_array(ctx, int, n);
1316 if (!cuts)
1317 return isl_change_error;
1319 for (k = 0, m = 0; m < n; ++k) {
1320 enum isl_ineq_type type;
1322 if (info[i].ineq[k] != STATUS_CUT)
1323 continue;
1325 isl_int_add_ui(info[i].bmap->ineq[k][0],
1326 info[i].bmap->ineq[k][0], 1);
1327 type = isl_tab_ineq_type(info[j].tab, info[i].bmap->ineq[k]);
1328 isl_int_sub_ui(info[i].bmap->ineq[k][0],
1329 info[i].bmap->ineq[k][0], 1);
1330 if (type == isl_ineq_error)
1331 goto error;
1332 if (type != isl_ineq_redundant)
1333 break;
1334 cuts[m] = k;
1335 ++m;
1338 if (m == n)
1339 change = wrap_in_facets(i, j, cuts, n, info);
1341 free(cuts);
1343 return change;
1344 error:
1345 free(cuts);
1346 return isl_change_error;
1349 /* Check if either i or j has only cut inequalities that can
1350 * be used to wrap in (a facet of) the other basic set.
1351 * if so, replace the pair by their union.
1353 static enum isl_change check_wrap(int i, int j, struct isl_coalesce_info *info)
1355 enum isl_change change = isl_change_none;
1357 if (!any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_CUT))
1358 change = can_wrap_in_set(i, j, info);
1359 if (change != isl_change_none)
1360 return change;
1362 if (!any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_CUT))
1363 change = can_wrap_in_set(j, i, info);
1364 return change;
1367 /* At least one of the basic maps has an equality that is adjacent
1368 * to inequality. Make sure that only one of the basic maps has
1369 * such an equality and that the other basic map has exactly one
1370 * inequality adjacent to an equality.
1371 * We call the basic map that has the inequality "i" and the basic
1372 * map that has the equality "j".
1373 * If "i" has any "cut" (in)equality, then relaxing the inequality
1374 * by one would not result in a basic map that contains the other
1375 * basic map. However, it may still be possible to wrap in the other
1376 * basic map.
1378 static enum isl_change check_adj_eq(int i, int j,
1379 struct isl_coalesce_info *info)
1381 enum isl_change change = isl_change_none;
1382 int k;
1383 int any_cut;
1385 if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ADJ_INEQ) &&
1386 any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_ADJ_INEQ))
1387 /* ADJ EQ TOO MANY */
1388 return isl_change_none;
1390 if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ADJ_INEQ))
1391 return check_adj_eq(j, i, info);
1393 /* j has an equality adjacent to an inequality in i */
1395 if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_CUT))
1396 return isl_change_none;
1397 any_cut = any(info[i].ineq, info[i].bmap->n_ineq, STATUS_CUT);
1398 if (count(info[i].ineq, info[i].bmap->n_ineq, STATUS_ADJ_EQ) != 1 ||
1399 any(info[j].ineq, info[j].bmap->n_ineq, STATUS_ADJ_EQ) ||
1400 any(info[i].ineq, info[i].bmap->n_ineq, STATUS_ADJ_INEQ) ||
1401 any(info[j].ineq, info[j].bmap->n_ineq, STATUS_ADJ_INEQ))
1402 /* ADJ EQ TOO MANY */
1403 return isl_change_none;
1405 for (k = 0; k < info[i].bmap->n_ineq; ++k)
1406 if (info[i].ineq[k] == STATUS_ADJ_EQ)
1407 break;
1409 if (!any_cut) {
1410 change = is_adj_eq_extension(i, j, k, info);
1411 if (change != isl_change_none)
1412 return change;
1415 change = can_wrap_in_facet(i, j, k, info, any_cut);
1417 return change;
1420 /* The two basic maps lie on adjacent hyperplanes. In particular,
1421 * basic map "i" has an equality that lies parallel to basic map "j".
1422 * Check if we can wrap the facets around the parallel hyperplanes
1423 * to include the other set.
1425 * We perform basically the same operations as can_wrap_in_facet,
1426 * except that we don't need to select a facet of one of the sets.
1428 * \\ \\
1429 * \\ => \\
1430 * \ \|
1432 * If there is more than one equality of "i" adjacent to an equality of "j",
1433 * then the result will satisfy one or more equalities that are a linear
1434 * combination of these equalities. These will be encoded as pairs
1435 * of inequalities in the wrapping constraints and need to be made
1436 * explicit.
1438 static enum isl_change check_eq_adj_eq(int i, int j,
1439 struct isl_coalesce_info *info)
1441 int k;
1442 enum isl_change change = isl_change_none;
1443 int detect_equalities = 0;
1444 struct isl_wraps wraps;
1445 isl_ctx *ctx;
1446 isl_mat *mat;
1447 struct isl_set *set_i = NULL;
1448 struct isl_set *set_j = NULL;
1449 struct isl_vec *bound = NULL;
1450 unsigned total = isl_basic_map_total_dim(info[i].bmap);
1452 if (count(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ADJ_EQ) != 1)
1453 detect_equalities = 1;
1455 for (k = 0; k < 2 * info[i].bmap->n_eq ; ++k)
1456 if (info[i].eq[k] == STATUS_ADJ_EQ)
1457 break;
1459 set_i = set_from_updated_bmap(info[i].bmap, info[i].tab);
1460 set_j = set_from_updated_bmap(info[j].bmap, info[j].tab);
1461 ctx = isl_basic_map_get_ctx(info[i].bmap);
1462 mat = isl_mat_alloc(ctx, 2 * (info[i].bmap->n_eq + info[j].bmap->n_eq) +
1463 info[i].bmap->n_ineq + info[j].bmap->n_ineq,
1464 1 + total);
1465 wraps_init(&wraps, mat, info, i, j);
1466 bound = isl_vec_alloc(ctx, 1 + total);
1467 if (!set_i || !set_j || !wraps.mat || !bound)
1468 goto error;
1470 if (k % 2 == 0)
1471 isl_seq_neg(bound->el, info[i].bmap->eq[k / 2], 1 + total);
1472 else
1473 isl_seq_cpy(bound->el, info[i].bmap->eq[k / 2], 1 + total);
1474 isl_int_add_ui(bound->el[0], bound->el[0], 1);
1476 isl_seq_cpy(wraps.mat->row[0], bound->el, 1 + total);
1477 wraps.mat->n_row = 1;
1479 if (add_wraps(&wraps, &info[j], bound->el, set_i) < 0)
1480 goto error;
1481 if (!wraps.mat->n_row)
1482 goto unbounded;
1484 isl_int_sub_ui(bound->el[0], bound->el[0], 1);
1485 isl_seq_neg(bound->el, bound->el, 1 + total);
1487 isl_seq_cpy(wraps.mat->row[wraps.mat->n_row], bound->el, 1 + total);
1488 wraps.mat->n_row++;
1490 if (add_wraps(&wraps, &info[i], bound->el, set_j) < 0)
1491 goto error;
1492 if (!wraps.mat->n_row)
1493 goto unbounded;
1495 change = fuse(i, j, info, wraps.mat, detect_equalities, 0);
1497 if (0) {
1498 error: change = isl_change_error;
1500 unbounded:
1502 wraps_free(&wraps);
1503 isl_set_free(set_i);
1504 isl_set_free(set_j);
1505 isl_vec_free(bound);
1507 return change;
1510 /* Check if the union of the given pair of basic maps
1511 * can be represented by a single basic map.
1512 * If so, replace the pair by the single basic map and return
1513 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
1514 * Otherwise, return isl_change_none.
1515 * The two basic maps are assumed to live in the same local space.
1517 * We first check the effect of each constraint of one basic map
1518 * on the other basic map.
1519 * The constraint may be
1520 * redundant the constraint is redundant in its own
1521 * basic map and should be ignore and removed
1522 * in the end
1523 * valid all (integer) points of the other basic map
1524 * satisfy the constraint
1525 * separate no (integer) point of the other basic map
1526 * satisfies the constraint
1527 * cut some but not all points of the other basic map
1528 * satisfy the constraint
1529 * adj_eq the given constraint is adjacent (on the outside)
1530 * to an equality of the other basic map
1531 * adj_ineq the given constraint is adjacent (on the outside)
1532 * to an inequality of the other basic map
1534 * We consider seven cases in which we can replace the pair by a single
1535 * basic map. We ignore all "redundant" constraints.
1537 * 1. all constraints of one basic map are valid
1538 * => the other basic map is a subset and can be removed
1540 * 2. all constraints of both basic maps are either "valid" or "cut"
1541 * and the facets corresponding to the "cut" constraints
1542 * of one of the basic maps lies entirely inside the other basic map
1543 * => the pair can be replaced by a basic map consisting
1544 * of the valid constraints in both basic maps
1546 * 3. there is a single pair of adjacent inequalities
1547 * (all other constraints are "valid")
1548 * => the pair can be replaced by a basic map consisting
1549 * of the valid constraints in both basic maps
1551 * 4. one basic map has a single adjacent inequality, while the other
1552 * constraints are "valid". The other basic map has some
1553 * "cut" constraints, but replacing the adjacent inequality by
1554 * its opposite and adding the valid constraints of the other
1555 * basic map results in a subset of the other basic map
1556 * => the pair can be replaced by a basic map consisting
1557 * of the valid constraints in both basic maps
1559 * 5. there is a single adjacent pair of an inequality and an equality,
1560 * the other constraints of the basic map containing the inequality are
1561 * "valid". Moreover, if the inequality the basic map is relaxed
1562 * and then turned into an equality, then resulting facet lies
1563 * entirely inside the other basic map
1564 * => the pair can be replaced by the basic map containing
1565 * the inequality, with the inequality relaxed.
1567 * 6. there is a single adjacent pair of an inequality and an equality,
1568 * the other constraints of the basic map containing the inequality are
1569 * "valid". Moreover, the facets corresponding to both
1570 * the inequality and the equality can be wrapped around their
1571 * ridges to include the other basic map
1572 * => the pair can be replaced by a basic map consisting
1573 * of the valid constraints in both basic maps together
1574 * with all wrapping constraints
1576 * 7. one of the basic maps extends beyond the other by at most one.
1577 * Moreover, the facets corresponding to the cut constraints and
1578 * the pieces of the other basic map at offset one from these cut
1579 * constraints can be wrapped around their ridges to include
1580 * the union of the two basic maps
1581 * => the pair can be replaced by a basic map consisting
1582 * of the valid constraints in both basic maps together
1583 * with all wrapping constraints
1585 * 8. the two basic maps live in adjacent hyperplanes. In principle
1586 * such sets can always be combined through wrapping, but we impose
1587 * that there is only one such pair, to avoid overeager coalescing.
1589 * Throughout the computation, we maintain a collection of tableaus
1590 * corresponding to the basic maps. When the basic maps are dropped
1591 * or combined, the tableaus are modified accordingly.
1593 static enum isl_change coalesce_local_pair(int i, int j,
1594 struct isl_coalesce_info *info)
1596 enum isl_change change = isl_change_none;
1598 info[i].eq = info[i].ineq = NULL;
1599 info[j].eq = info[j].ineq = NULL;
1601 info[i].eq = eq_status_in(info[i].bmap, info[j].tab);
1602 if (info[i].bmap->n_eq && !info[i].eq)
1603 goto error;
1604 if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ERROR))
1605 goto error;
1606 if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_SEPARATE))
1607 goto done;
1609 info[j].eq = eq_status_in(info[j].bmap, info[i].tab);
1610 if (info[j].bmap->n_eq && !info[j].eq)
1611 goto error;
1612 if (any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_ERROR))
1613 goto error;
1614 if (any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_SEPARATE))
1615 goto done;
1617 info[i].ineq = ineq_status_in(info[i].bmap, info[i].tab, info[j].tab);
1618 if (info[i].bmap->n_ineq && !info[i].ineq)
1619 goto error;
1620 if (any(info[i].ineq, info[i].bmap->n_ineq, STATUS_ERROR))
1621 goto error;
1622 if (any(info[i].ineq, info[i].bmap->n_ineq, STATUS_SEPARATE))
1623 goto done;
1625 info[j].ineq = ineq_status_in(info[j].bmap, info[j].tab, info[i].tab);
1626 if (info[j].bmap->n_ineq && !info[j].ineq)
1627 goto error;
1628 if (any(info[j].ineq, info[j].bmap->n_ineq, STATUS_ERROR))
1629 goto error;
1630 if (any(info[j].ineq, info[j].bmap->n_ineq, STATUS_SEPARATE))
1631 goto done;
1633 if (all(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_VALID) &&
1634 all(info[i].ineq, info[i].bmap->n_ineq, STATUS_VALID)) {
1635 drop(&info[j]);
1636 change = isl_change_drop_second;
1637 } else if (all(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_VALID) &&
1638 all(info[j].ineq, info[j].bmap->n_ineq, STATUS_VALID)) {
1639 drop(&info[i]);
1640 change = isl_change_drop_first;
1641 } else if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ADJ_EQ)) {
1642 change = check_eq_adj_eq(i, j, info);
1643 } else if (any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_ADJ_EQ)) {
1644 change = check_eq_adj_eq(j, i, info);
1645 } else if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ADJ_INEQ) ||
1646 any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_ADJ_INEQ)) {
1647 change = check_adj_eq(i, j, info);
1648 } else if (any(info[i].ineq, info[i].bmap->n_ineq, STATUS_ADJ_EQ) ||
1649 any(info[j].ineq, info[j].bmap->n_ineq, STATUS_ADJ_EQ)) {
1650 /* Can't happen */
1651 /* BAD ADJ INEQ */
1652 } else if (any(info[i].ineq, info[i].bmap->n_ineq, STATUS_ADJ_INEQ) ||
1653 any(info[j].ineq, info[j].bmap->n_ineq, STATUS_ADJ_INEQ)) {
1654 change = check_adj_ineq(i, j, info);
1655 } else {
1656 if (!any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_CUT) &&
1657 !any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_CUT))
1658 change = check_facets(i, j, info);
1659 if (change == isl_change_none)
1660 change = check_wrap(i, j, info);
1663 done:
1664 free(info[i].eq);
1665 free(info[j].eq);
1666 free(info[i].ineq);
1667 free(info[j].ineq);
1668 return change;
1669 error:
1670 free(info[i].eq);
1671 free(info[j].eq);
1672 free(info[i].ineq);
1673 free(info[j].ineq);
1674 return isl_change_error;
1677 /* Shift the integer division at position "div" of the basic map
1678 * represented by "info" by "shift".
1680 * That is, if the integer division has the form
1682 * floor(f(x)/d)
1684 * then replace it by
1686 * floor((f(x) + shift * d)/d) - shift
1688 static int shift_div(struct isl_coalesce_info *info, int div, isl_int shift)
1690 unsigned total;
1692 info->bmap = isl_basic_map_shift_div(info->bmap, div, shift);
1693 if (!info->bmap)
1694 return -1;
1696 total = isl_basic_map_dim(info->bmap, isl_dim_all);
1697 total -= isl_basic_map_dim(info->bmap, isl_dim_div);
1698 if (isl_tab_shift_var(info->tab, total + div, shift) < 0)
1699 return -1;
1701 return 0;
1704 /* Check if some of the divs in the basic map represented by "info1"
1705 * are shifts of the corresponding divs in the basic map represented
1706 * by "info2". If so, align them with those of "info2".
1707 * Only do this if "info1" and "info2" have the same number
1708 * of integer divisions.
1710 * An integer division is considered to be a shift of another integer
1711 * division if one is equal to the other plus a constant.
1713 * In particular, for each pair of integer divisions, if both are known,
1714 * have identical coefficients (apart from the constant term) and
1715 * if the difference between the constant terms (taking into account
1716 * the denominator) is an integer, then move the difference outside.
1717 * That is, if one integer division is of the form
1719 * floor((f(x) + c_1)/d)
1721 * while the other is of the form
1723 * floor((f(x) + c_2)/d)
1725 * and n = (c_2 - c_1)/d is an integer, then replace the first
1726 * integer division by
1728 * floor((f(x) + c_1 + n * d)/d) - n = floor((f(x) + c_2)/d) - n
1730 static int harmonize_divs(struct isl_coalesce_info *info1,
1731 struct isl_coalesce_info *info2)
1733 int i;
1734 int total;
1736 if (!info1->bmap || !info2->bmap)
1737 return -1;
1739 if (info1->bmap->n_div != info2->bmap->n_div)
1740 return 0;
1741 if (info1->bmap->n_div == 0)
1742 return 0;
1744 total = isl_basic_map_total_dim(info1->bmap);
1745 for (i = 0; i < info1->bmap->n_div; ++i) {
1746 isl_int d;
1747 int r = 0;
1749 if (isl_int_is_zero(info1->bmap->div[i][0]) ||
1750 isl_int_is_zero(info2->bmap->div[i][0]))
1751 continue;
1752 if (isl_int_ne(info1->bmap->div[i][0], info2->bmap->div[i][0]))
1753 continue;
1754 if (isl_int_eq(info1->bmap->div[i][1], info2->bmap->div[i][1]))
1755 continue;
1756 if (!isl_seq_eq(info1->bmap->div[i] + 2,
1757 info2->bmap->div[i] + 2, total))
1758 continue;
1759 isl_int_init(d);
1760 isl_int_sub(d, info2->bmap->div[i][1], info1->bmap->div[i][1]);
1761 if (isl_int_is_divisible_by(d, info1->bmap->div[i][0])) {
1762 isl_int_divexact(d, d, info1->bmap->div[i][0]);
1763 r = shift_div(info1, i, d);
1765 isl_int_clear(d);
1766 if (r < 0)
1767 return -1;
1770 return 0;
1773 /* Do the two basic maps live in the same local space, i.e.,
1774 * do they have the same (known) divs?
1775 * If either basic map has any unknown divs, then we can only assume
1776 * that they do not live in the same local space.
1778 static int same_divs(__isl_keep isl_basic_map *bmap1,
1779 __isl_keep isl_basic_map *bmap2)
1781 int i;
1782 int known;
1783 int total;
1785 if (!bmap1 || !bmap2)
1786 return -1;
1787 if (bmap1->n_div != bmap2->n_div)
1788 return 0;
1790 if (bmap1->n_div == 0)
1791 return 1;
1793 known = isl_basic_map_divs_known(bmap1);
1794 if (known < 0 || !known)
1795 return known;
1796 known = isl_basic_map_divs_known(bmap2);
1797 if (known < 0 || !known)
1798 return known;
1800 total = isl_basic_map_total_dim(bmap1);
1801 for (i = 0; i < bmap1->n_div; ++i)
1802 if (!isl_seq_eq(bmap1->div[i], bmap2->div[i], 2 + total))
1803 return 0;
1805 return 1;
1808 /* Does "bmap" contain the basic map represented by the tableau "tab"
1809 * after expanding the divs of "bmap" to match those of "tab"?
1810 * The expansion is performed using the divs "div" and expansion "exp"
1811 * computed by the caller.
1812 * Then we check if all constraints of the expanded "bmap" are valid for "tab".
1814 static int contains_with_expanded_divs(__isl_keep isl_basic_map *bmap,
1815 struct isl_tab *tab, __isl_keep isl_mat *div, int *exp)
1817 int superset = 0;
1818 int *eq_i = NULL;
1819 int *ineq_i = NULL;
1821 bmap = isl_basic_map_copy(bmap);
1822 bmap = isl_basic_set_expand_divs(bmap, isl_mat_copy(div), exp);
1824 if (!bmap)
1825 goto error;
1827 eq_i = eq_status_in(bmap, tab);
1828 if (bmap->n_eq && !eq_i)
1829 goto error;
1830 if (any(eq_i, 2 * bmap->n_eq, STATUS_ERROR))
1831 goto error;
1832 if (any(eq_i, 2 * bmap->n_eq, STATUS_SEPARATE))
1833 goto done;
1835 ineq_i = ineq_status_in(bmap, NULL, tab);
1836 if (bmap->n_ineq && !ineq_i)
1837 goto error;
1838 if (any(ineq_i, bmap->n_ineq, STATUS_ERROR))
1839 goto error;
1840 if (any(ineq_i, bmap->n_ineq, STATUS_SEPARATE))
1841 goto done;
1843 if (all(eq_i, 2 * bmap->n_eq, STATUS_VALID) &&
1844 all(ineq_i, bmap->n_ineq, STATUS_VALID))
1845 superset = 1;
1847 done:
1848 isl_basic_map_free(bmap);
1849 free(eq_i);
1850 free(ineq_i);
1851 return superset;
1852 error:
1853 isl_basic_map_free(bmap);
1854 free(eq_i);
1855 free(ineq_i);
1856 return -1;
1859 /* Does "bmap_i" contain the basic map represented by "info_j"
1860 * after aligning the divs of "bmap_i" to those of "info_j".
1861 * Note that this can only succeed if the number of divs of "bmap_i"
1862 * is smaller than (or equal to) the number of divs of "info_j".
1864 * We first check if the divs of "bmap_i" are all known and form a subset
1865 * of those of "bmap_j". If so, we pass control over to
1866 * contains_with_expanded_divs.
1868 static int contains_after_aligning_divs(__isl_keep isl_basic_map *bmap_i,
1869 struct isl_coalesce_info *info_j)
1871 int known;
1872 isl_mat *div_i, *div_j, *div;
1873 int *exp1 = NULL;
1874 int *exp2 = NULL;
1875 isl_ctx *ctx;
1876 int subset;
1878 known = isl_basic_map_divs_known(bmap_i);
1879 if (known < 0 || !known)
1880 return known;
1882 ctx = isl_basic_map_get_ctx(bmap_i);
1884 div_i = isl_basic_map_get_divs(bmap_i);
1885 div_j = isl_basic_map_get_divs(info_j->bmap);
1887 if (!div_i || !div_j)
1888 goto error;
1890 exp1 = isl_alloc_array(ctx, int, div_i->n_row);
1891 exp2 = isl_alloc_array(ctx, int, div_j->n_row);
1892 if ((div_i->n_row && !exp1) || (div_j->n_row && !exp2))
1893 goto error;
1895 div = isl_merge_divs(div_i, div_j, exp1, exp2);
1896 if (!div)
1897 goto error;
1899 if (div->n_row == div_j->n_row)
1900 subset = contains_with_expanded_divs(bmap_i,
1901 info_j->tab, div, exp1);
1902 else
1903 subset = 0;
1905 isl_mat_free(div);
1907 isl_mat_free(div_i);
1908 isl_mat_free(div_j);
1910 free(exp2);
1911 free(exp1);
1913 return subset;
1914 error:
1915 isl_mat_free(div_i);
1916 isl_mat_free(div_j);
1917 free(exp1);
1918 free(exp2);
1919 return -1;
1922 /* Check if the basic map "j" is a subset of basic map "i",
1923 * if "i" has fewer divs that "j".
1924 * If so, remove basic map "j".
1926 * If the two basic maps have the same number of divs, then
1927 * they must necessarily be different. Otherwise, we would have
1928 * called coalesce_local_pair. We therefore don't try anything
1929 * in this case.
1931 static int coalesced_subset(int i, int j, struct isl_coalesce_info *info)
1933 int superset;
1935 if (info[i].bmap->n_div >= info[j].bmap->n_div)
1936 return 0;
1938 superset = contains_after_aligning_divs(info[i].bmap, &info[j]);
1939 if (superset < 0)
1940 return -1;
1941 if (superset)
1942 drop(&info[j]);
1944 return superset;
1947 /* Check if basic map "j" is a subset of basic map "i" after
1948 * exploiting the extra equalities of "j" to simplify the divs of "i".
1949 * If so, remove basic map "j".
1951 * If "j" does not have any equalities or if they are the same
1952 * as those of "i", then we cannot exploit them to simplify the divs.
1953 * Similarly, if there are no divs in "i", then they cannot be simplified.
1954 * If, on the other hand, the affine hulls of "i" and "j" do not intersect,
1955 * then "j" cannot be a subset of "i".
1957 * Otherwise, we intersect "i" with the affine hull of "j" and then
1958 * check if "j" is a subset of the result after aligning the divs.
1959 * If so, then "j" is definitely a subset of "i" and can be removed.
1960 * Note that if after intersection with the affine hull of "j".
1961 * "i" still has more divs than "j", then there is no way we can
1962 * align the divs of "i" to those of "j".
1964 static int coalesced_subset_with_equalities(int i, int j,
1965 struct isl_coalesce_info *info)
1967 isl_basic_map *hull_i, *hull_j, *bmap_i;
1968 int equal, empty, subset;
1970 if (info[j].bmap->n_eq == 0)
1971 return 0;
1972 if (info[i].bmap->n_div == 0)
1973 return 0;
1975 hull_i = isl_basic_map_copy(info[i].bmap);
1976 hull_i = isl_basic_map_plain_affine_hull(hull_i);
1977 hull_j = isl_basic_map_copy(info[j].bmap);
1978 hull_j = isl_basic_map_plain_affine_hull(hull_j);
1980 hull_j = isl_basic_map_intersect(hull_j, isl_basic_map_copy(hull_i));
1981 equal = isl_basic_map_plain_is_equal(hull_i, hull_j);
1982 empty = isl_basic_map_plain_is_empty(hull_j);
1983 isl_basic_map_free(hull_i);
1985 if (equal < 0 || equal || empty < 0 || empty) {
1986 isl_basic_map_free(hull_j);
1987 return equal < 0 || empty < 0 ? -1 : 0;
1990 bmap_i = isl_basic_map_copy(info[i].bmap);
1991 bmap_i = isl_basic_map_intersect(bmap_i, hull_j);
1992 if (!bmap_i)
1993 return -1;
1995 if (bmap_i->n_div > info[j].bmap->n_div) {
1996 isl_basic_map_free(bmap_i);
1997 return 0;
2000 subset = contains_after_aligning_divs(bmap_i, &info[j]);
2002 isl_basic_map_free(bmap_i);
2004 if (subset < 0)
2005 return -1;
2006 if (subset)
2007 drop(&info[j]);
2009 return subset;
2012 /* Check if one of the basic maps is a subset of the other and, if so,
2013 * drop the subset.
2014 * Note that we only perform any test if the number of divs is different
2015 * in the two basic maps. In case the number of divs is the same,
2016 * we have already established that the divs are different
2017 * in the two basic maps.
2018 * In particular, if the number of divs of basic map i is smaller than
2019 * the number of divs of basic map j, then we check if j is a subset of i
2020 * and vice versa.
2022 static enum isl_change check_coalesce_subset(int i, int j,
2023 struct isl_coalesce_info *info)
2025 int changed;
2027 changed = coalesced_subset(i, j, info);
2028 if (changed < 0 || changed)
2029 return changed < 0 ? isl_change_error : isl_change_drop_second;
2031 changed = coalesced_subset(j, i, info);
2032 if (changed < 0 || changed)
2033 return changed < 0 ? isl_change_error : isl_change_drop_first;
2035 changed = coalesced_subset_with_equalities(i, j, info);
2036 if (changed < 0 || changed)
2037 return changed < 0 ? isl_change_error : isl_change_drop_second;
2039 changed = coalesced_subset_with_equalities(j, i, info);
2040 if (changed < 0 || changed)
2041 return changed < 0 ? isl_change_error : isl_change_drop_first;
2043 return isl_change_none;
2046 /* Does "bmap" involve any divs that themselves refer to divs?
2048 static int has_nested_div(__isl_keep isl_basic_map *bmap)
2050 int i;
2051 unsigned total;
2052 unsigned n_div;
2054 total = isl_basic_map_dim(bmap, isl_dim_all);
2055 n_div = isl_basic_map_dim(bmap, isl_dim_div);
2056 total -= n_div;
2058 for (i = 0; i < n_div; ++i)
2059 if (isl_seq_first_non_zero(bmap->div[i] + 2 + total,
2060 n_div) != -1)
2061 return 1;
2063 return 0;
2066 /* Return a list of affine expressions, one for each integer division
2067 * in "bmap_i". For each integer division that also appears in "bmap_j",
2068 * the affine expression is set to NaN. The number of NaNs in the list
2069 * is equal to the number of integer divisions in "bmap_j".
2070 * For the other integer divisions of "bmap_i", the corresponding
2071 * element in the list is a purely affine expression equal to the integer
2072 * division in "hull".
2073 * If no such list can be constructed, then the number of elements
2074 * in the returned list is smaller than the number of integer divisions
2075 * in "bmap_i".
2077 static __isl_give isl_aff_list *set_up_substitutions(
2078 __isl_keep isl_basic_map *bmap_i, __isl_keep isl_basic_map *bmap_j,
2079 __isl_take isl_basic_map *hull)
2081 unsigned n_div_i, n_div_j, total;
2082 isl_ctx *ctx;
2083 isl_local_space *ls;
2084 isl_basic_set *wrap_hull;
2085 isl_aff *aff_nan;
2086 isl_aff_list *list;
2087 int i, j;
2089 if (!hull)
2090 return NULL;
2092 ctx = isl_basic_map_get_ctx(hull);
2094 n_div_i = isl_basic_map_dim(bmap_i, isl_dim_div);
2095 n_div_j = isl_basic_map_dim(bmap_j, isl_dim_div);
2096 total = isl_basic_map_total_dim(bmap_i) - n_div_i;
2098 ls = isl_basic_map_get_local_space(bmap_i);
2099 ls = isl_local_space_wrap(ls);
2100 wrap_hull = isl_basic_map_wrap(hull);
2102 aff_nan = isl_aff_nan_on_domain(isl_local_space_copy(ls));
2103 list = isl_aff_list_alloc(ctx, n_div_i);
2105 j = 0;
2106 for (i = 0; i < n_div_i; ++i) {
2107 isl_aff *aff;
2109 if (j < n_div_j &&
2110 isl_seq_eq(bmap_i->div[i], bmap_j->div[j], 2 + total)) {
2111 ++j;
2112 list = isl_aff_list_add(list, isl_aff_copy(aff_nan));
2113 continue;
2115 if (n_div_i - i <= n_div_j - j)
2116 break;
2118 aff = isl_local_space_get_div(ls, i);
2119 aff = isl_aff_substitute_equalities(aff,
2120 isl_basic_set_copy(wrap_hull));
2121 aff = isl_aff_floor(aff);
2122 if (!aff)
2123 goto error;
2124 if (isl_aff_dim(aff, isl_dim_div) != 0) {
2125 isl_aff_free(aff);
2126 break;
2129 list = isl_aff_list_add(list, aff);
2132 isl_aff_free(aff_nan);
2133 isl_local_space_free(ls);
2134 isl_basic_set_free(wrap_hull);
2136 return list;
2137 error:
2138 isl_aff_free(aff_nan);
2139 isl_local_space_free(ls);
2140 isl_basic_set_free(wrap_hull);
2141 isl_aff_list_free(list);
2142 return NULL;
2145 /* Add variables to "tab" corresponding to the elements in "list"
2146 * that are not set to NaN.
2147 * "dim" is the offset in the variables of "tab" where we should
2148 * start considering the elements in "list".
2149 * When this function returns, the total number of variables in "tab"
2150 * is equal to "dim" plus the number of elements in "list".
2152 static int add_sub_vars(struct isl_tab *tab, __isl_keep isl_aff_list *list,
2153 int dim)
2155 int i, n;
2157 n = isl_aff_list_n_aff(list);
2158 for (i = 0; i < n; ++i) {
2159 int is_nan;
2160 isl_aff *aff;
2162 aff = isl_aff_list_get_aff(list, i);
2163 is_nan = isl_aff_is_nan(aff);
2164 isl_aff_free(aff);
2165 if (is_nan < 0)
2166 return -1;
2168 if (!is_nan && isl_tab_insert_var(tab, dim + i) < 0)
2169 return -1;
2172 return 0;
2175 /* For each element in "list" that is not set to NaN, fix the corresponding
2176 * variable in "tab" to the purely affine expression defined by the element.
2177 * "dim" is the offset in the variables of "tab" where we should
2178 * start considering the elements in "list".
2180 static int add_sub_equalities(struct isl_tab *tab,
2181 __isl_keep isl_aff_list *list, int dim)
2183 int i, n;
2184 isl_ctx *ctx;
2185 isl_vec *sub;
2186 isl_aff *aff;
2188 n = isl_aff_list_n_aff(list);
2190 ctx = isl_tab_get_ctx(tab);
2191 sub = isl_vec_alloc(ctx, 1 + dim + n);
2192 if (!sub)
2193 return -1;
2194 isl_seq_clr(sub->el + 1 + dim, n);
2196 for (i = 0; i < n; ++i) {
2197 aff = isl_aff_list_get_aff(list, i);
2198 if (!aff)
2199 goto error;
2200 if (isl_aff_is_nan(aff)) {
2201 isl_aff_free(aff);
2202 continue;
2204 isl_seq_cpy(sub->el, aff->v->el + 1, 1 + dim);
2205 isl_int_neg(sub->el[1 + dim + i], aff->v->el[0]);
2206 if (isl_tab_add_eq(tab, sub->el) < 0)
2207 goto error;
2208 isl_int_set_si(sub->el[1 + dim + i], 0);
2209 isl_aff_free(aff);
2212 isl_vec_free(sub);
2213 return 0;
2214 error:
2215 isl_aff_free(aff);
2216 isl_vec_free(sub);
2217 return -1;
2220 /* Add variables to info->tab corresponding to the elements in "list"
2221 * that are not set to NaN. The value of the added variable
2222 * is fixed to the purely affine expression defined by the element.
2223 * "dim" is the offset in the variables of info->tab where we should
2224 * start considering the elements in "list".
2225 * When this function returns, the total number of variables in info->tab
2226 * is equal to "dim" plus the number of elements in "list".
2227 * Additionally, add the div constraints that have been added info->bmap
2228 * after the tableau was constructed to info->tab. These constraints
2229 * start at position "n_ineq" in info->bmap.
2230 * The constraints need to be added to the tableau before
2231 * the equalities assigning the purely affine expression
2232 * because the position needs to match that in info->bmap.
2233 * They are frozen because the corresponding added equality is a consequence
2234 * of the two div constraints and the other equalities, meaning that
2235 * the div constraints would otherwise get marked as redundant,
2236 * while they are only redundant with respect to the extra equalities
2237 * added to the tableau, which do not appear explicitly in the basic map.
2239 static int add_subs(struct isl_coalesce_info *info,
2240 __isl_keep isl_aff_list *list, int dim, int n_ineq)
2242 int i, extra_var, extra_con;
2243 int n;
2244 unsigned n_eq = info->bmap->n_eq;
2246 if (!list)
2247 return -1;
2249 n = isl_aff_list_n_aff(list);
2250 extra_var = n - (info->tab->n_var - dim);
2251 extra_con = info->bmap->n_ineq - n_ineq;
2253 if (isl_tab_extend_vars(info->tab, extra_var) < 0)
2254 return -1;
2255 if (isl_tab_extend_cons(info->tab, extra_con + 2 * extra_var) < 0)
2256 return -1;
2257 if (add_sub_vars(info->tab, list, dim) < 0)
2258 return -1;
2260 for (i = n_ineq; i < info->bmap->n_ineq; ++i) {
2261 if (isl_tab_add_ineq(info->tab, info->bmap->ineq[i]) < 0)
2262 return -1;
2263 if (isl_tab_freeze_constraint(info->tab, n_eq + i) < 0)
2264 return -1;
2267 return add_sub_equalities(info->tab, list, dim);
2270 /* Coalesce basic map "j" into basic map "i" after adding the extra integer
2271 * divisions in "i" but not in "j" to basic map "j", with values
2272 * specified by "list". The total number of elements in "list"
2273 * is equal to the number of integer divisions in "i", while the number
2274 * of NaN elements in the list is equal to the number of integer divisions
2275 * in "j".
2276 * Adding extra integer divisions to "j" through isl_basic_map_align_divs
2277 * also adds the corresponding div constraints. These need to be added
2278 * to the corresponding tableau as well in add_subs to maintain consistency.
2280 * If no coalescing can be performed, then we need to revert basic map "j"
2281 * to its original state. We do the same if basic map "i" gets dropped
2282 * during the coalescing, even though this should not happen in practice
2283 * since we have already checked for "j" being a subset of "i"
2284 * before we reach this stage.
2286 static enum isl_change coalesce_with_subs(int i, int j,
2287 struct isl_coalesce_info *info, __isl_keep isl_aff_list *list)
2289 isl_basic_map *bmap_j;
2290 struct isl_tab_undo *snap;
2291 unsigned dim;
2292 enum isl_change change;
2293 int n_ineq;
2295 bmap_j = isl_basic_map_copy(info[j].bmap);
2296 n_ineq = info[j].bmap->n_ineq;
2297 info[j].bmap = isl_basic_map_align_divs(info[j].bmap, info[i].bmap);
2298 if (!info[j].bmap)
2299 goto error;
2301 snap = isl_tab_snap(info[j].tab);
2303 dim = isl_basic_map_dim(bmap_j, isl_dim_all);
2304 dim -= isl_basic_map_dim(bmap_j, isl_dim_div);
2305 if (add_subs(&info[j], list, dim, n_ineq) < 0)
2306 goto error;
2308 change = coalesce_local_pair(i, j, info);
2309 if (change != isl_change_none && change != isl_change_drop_first) {
2310 isl_basic_map_free(bmap_j);
2311 } else {
2312 isl_basic_map_free(info[j].bmap);
2313 info[j].bmap = bmap_j;
2315 if (isl_tab_rollback(info[j].tab, snap) < 0)
2316 return isl_change_error;
2319 return change;
2320 error:
2321 isl_basic_map_free(bmap_j);
2322 return isl_change_error;
2325 /* Check if we can coalesce basic map "j" into basic map "i" after copying
2326 * those extra integer divisions in "i" that can be simplified away
2327 * using the extra equalities in "j".
2328 * All divs are assumed to be known and not contain any nested divs.
2330 * We first check if there are any extra equalities in "j" that we
2331 * can exploit. Then we check if every integer division in "i"
2332 * either already appears in "j" or can be simplified using the
2333 * extra equalities to a purely affine expression.
2334 * If these tests succeed, then we try to coalesce the two basic maps
2335 * by introducing extra dimensions in "j" corresponding to
2336 * the extra integer divsisions "i" fixed to the corresponding
2337 * purely affine expression.
2339 static enum isl_change check_coalesce_into_eq(int i, int j,
2340 struct isl_coalesce_info *info)
2342 unsigned n_div_i, n_div_j;
2343 isl_basic_map *hull_i, *hull_j;
2344 int equal, empty;
2345 isl_aff_list *list;
2346 enum isl_change change;
2348 n_div_i = isl_basic_map_dim(info[i].bmap, isl_dim_div);
2349 n_div_j = isl_basic_map_dim(info[j].bmap, isl_dim_div);
2350 if (n_div_i <= n_div_j)
2351 return isl_change_none;
2352 if (info[j].bmap->n_eq == 0)
2353 return isl_change_none;
2355 hull_i = isl_basic_map_copy(info[i].bmap);
2356 hull_i = isl_basic_map_plain_affine_hull(hull_i);
2357 hull_j = isl_basic_map_copy(info[j].bmap);
2358 hull_j = isl_basic_map_plain_affine_hull(hull_j);
2360 hull_j = isl_basic_map_intersect(hull_j, isl_basic_map_copy(hull_i));
2361 equal = isl_basic_map_plain_is_equal(hull_i, hull_j);
2362 empty = isl_basic_map_plain_is_empty(hull_j);
2363 isl_basic_map_free(hull_i);
2365 if (equal < 0 || empty < 0)
2366 goto error;
2367 if (equal || empty) {
2368 isl_basic_map_free(hull_j);
2369 return isl_change_none;
2372 list = set_up_substitutions(info[i].bmap, info[j].bmap, hull_j);
2373 if (!list)
2374 return isl_change_error;
2375 if (isl_aff_list_n_aff(list) < n_div_i)
2376 change = isl_change_none;
2377 else
2378 change = coalesce_with_subs(i, j, info, list);
2380 isl_aff_list_free(list);
2382 return change;
2383 error:
2384 isl_basic_map_free(hull_j);
2385 return isl_change_error;
2388 /* Check if we can coalesce basic maps "i" and "j" after copying
2389 * those extra integer divisions in one of the basic maps that can
2390 * be simplified away using the extra equalities in the other basic map.
2391 * We require all divs to be known in both basic maps.
2392 * Furthermore, to simplify the comparison of div expressions,
2393 * we do not allow any nested integer divisions.
2395 static enum isl_change check_coalesce_eq(int i, int j,
2396 struct isl_coalesce_info *info)
2398 int known, nested;
2399 enum isl_change change;
2401 known = isl_basic_map_divs_known(info[i].bmap);
2402 if (known < 0 || !known)
2403 return known < 0 ? isl_change_error : isl_change_none;
2404 known = isl_basic_map_divs_known(info[j].bmap);
2405 if (known < 0 || !known)
2406 return known < 0 ? isl_change_error : isl_change_none;
2407 nested = has_nested_div(info[i].bmap);
2408 if (nested < 0 || nested)
2409 return nested < 0 ? isl_change_error : isl_change_none;
2410 nested = has_nested_div(info[j].bmap);
2411 if (nested < 0 || nested)
2412 return nested < 0 ? isl_change_error : isl_change_none;
2414 change = check_coalesce_into_eq(i, j, info);
2415 if (change != isl_change_none)
2416 return change;
2417 change = check_coalesce_into_eq(j, i, info);
2418 if (change != isl_change_none)
2419 return invert_change(change);
2421 return isl_change_none;
2424 /* Check if the union of the given pair of basic maps
2425 * can be represented by a single basic map.
2426 * If so, replace the pair by the single basic map and return
2427 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
2428 * Otherwise, return isl_change_none.
2430 * We first check if the two basic maps live in the same local space,
2431 * after aligning the divs that differ by only an integer constant.
2432 * If so, we do the complete check. Otherwise, we check if they have
2433 * the same number of integer divisions and can be coalesced, if one is
2434 * an obvious subset of the other or if the extra integer divisions
2435 * of one basic map can be simplified away using the extra equalities
2436 * of the other basic map.
2438 static enum isl_change coalesce_pair(int i, int j,
2439 struct isl_coalesce_info *info)
2441 int same;
2442 enum isl_change change;
2444 if (harmonize_divs(&info[i], &info[j]) < 0)
2445 return isl_change_error;
2446 same = same_divs(info[i].bmap, info[j].bmap);
2447 if (same < 0)
2448 return isl_change_error;
2449 if (same)
2450 return coalesce_local_pair(i, j, info);
2452 if (info[i].bmap->n_div == info[j].bmap->n_div) {
2453 change = coalesce_local_pair(i, j, info);
2454 if (change != isl_change_none)
2455 return change;
2458 change = check_coalesce_subset(i, j, info);
2459 if (change != isl_change_none)
2460 return change;
2462 return check_coalesce_eq(i, j, info);
2465 /* Return the maximum of "a" and "b".
2467 static inline int max(int a, int b)
2469 return a > b ? a : b;
2472 /* Pairwise coalesce the basic maps in the range [start1, end1[ of "info"
2473 * with those in the range [start2, end2[, skipping basic maps
2474 * that have been removed (either before or within this function).
2476 * For each basic map i in the first range, we check if it can be coalesced
2477 * with respect to any previously considered basic map j in the second range.
2478 * If i gets dropped (because it was a subset of some j), then
2479 * we can move on to the next basic map.
2480 * If j gets dropped, we need to continue checking against the other
2481 * previously considered basic maps.
2482 * If the two basic maps got fused, then we recheck the fused basic map
2483 * against the previously considered basic maps, starting at i + 1
2484 * (even if start2 is greater than i + 1).
2486 static int coalesce_range(isl_ctx *ctx, struct isl_coalesce_info *info,
2487 int start1, int end1, int start2, int end2)
2489 int i, j;
2491 for (i = end1 - 1; i >= start1; --i) {
2492 if (info[i].removed)
2493 continue;
2494 for (j = max(i + 1, start2); j < end2; ++j) {
2495 enum isl_change changed;
2497 if (info[j].removed)
2498 continue;
2499 if (info[i].removed)
2500 isl_die(ctx, isl_error_internal,
2501 "basic map unexpectedly removed",
2502 return -1);
2503 changed = coalesce_pair(i, j, info);
2504 switch (changed) {
2505 case isl_change_error:
2506 return -1;
2507 case isl_change_none:
2508 case isl_change_drop_second:
2509 continue;
2510 case isl_change_drop_first:
2511 j = end2;
2512 break;
2513 case isl_change_fuse:
2514 j = i;
2515 break;
2520 return 0;
2523 /* Pairwise coalesce the basic maps described by the "n" elements of "info".
2525 * We consider groups of basic maps that live in the same apparent
2526 * affine hull and we first coalesce within such a group before we
2527 * coalesce the elements in the group with elements of previously
2528 * considered groups. If a fuse happens during the second phase,
2529 * then we also reconsider the elements within the group.
2531 static int coalesce(isl_ctx *ctx, int n, struct isl_coalesce_info *info)
2533 int start, end;
2535 for (end = n; end > 0; end = start) {
2536 start = end - 1;
2537 while (start >= 1 &&
2538 info[start - 1].hull_hash == info[start].hull_hash)
2539 start--;
2540 if (coalesce_range(ctx, info, start, end, start, end) < 0)
2541 return -1;
2542 if (coalesce_range(ctx, info, start, end, end, n) < 0)
2543 return -1;
2546 return 0;
2549 /* Update the basic maps in "map" based on the information in "info".
2550 * In particular, remove the basic maps that have been marked removed and
2551 * update the others based on the information in the corresponding tableau.
2552 * Since we detected implicit equalities without calling
2553 * isl_basic_map_gauss, we need to do it now.
2554 * Also call isl_basic_map_simplify if we may have lost the definition
2555 * of one or more integer divisions.
2557 static __isl_give isl_map *update_basic_maps(__isl_take isl_map *map,
2558 int n, struct isl_coalesce_info *info)
2560 int i;
2562 if (!map)
2563 return NULL;
2565 for (i = n - 1; i >= 0; --i) {
2566 if (info[i].removed) {
2567 isl_basic_map_free(map->p[i]);
2568 if (i != map->n - 1)
2569 map->p[i] = map->p[map->n - 1];
2570 map->n--;
2571 continue;
2574 info[i].bmap = isl_basic_map_update_from_tab(info[i].bmap,
2575 info[i].tab);
2576 info[i].bmap = isl_basic_map_gauss(info[i].bmap, NULL);
2577 if (info[i].simplify)
2578 info[i].bmap = isl_basic_map_simplify(info[i].bmap);
2579 info[i].bmap = isl_basic_map_finalize(info[i].bmap);
2580 if (!info[i].bmap)
2581 return isl_map_free(map);
2582 ISL_F_SET(info[i].bmap, ISL_BASIC_MAP_NO_IMPLICIT);
2583 ISL_F_SET(info[i].bmap, ISL_BASIC_MAP_NO_REDUNDANT);
2584 isl_basic_map_free(map->p[i]);
2585 map->p[i] = info[i].bmap;
2586 info[i].bmap = NULL;
2589 return map;
2592 /* For each pair of basic maps in the map, check if the union of the two
2593 * can be represented by a single basic map.
2594 * If so, replace the pair by the single basic map and start over.
2596 * We factor out any (hidden) common factor from the constraint
2597 * coefficients to improve the detection of adjacent constraints.
2599 * Since we are constructing the tableaus of the basic maps anyway,
2600 * we exploit them to detect implicit equalities and redundant constraints.
2601 * This also helps the coalescing as it can ignore the redundant constraints.
2602 * In order to avoid confusion, we make all implicit equalities explicit
2603 * in the basic maps. We don't call isl_basic_map_gauss, though,
2604 * as that may affect the number of constraints.
2605 * This means that we have to call isl_basic_map_gauss at the end
2606 * of the computation (in update_basic_maps) to ensure that
2607 * the basic maps are not left in an unexpected state.
2608 * For each basic map, we also compute the hash of the apparent affine hull
2609 * for use in coalesce.
2611 struct isl_map *isl_map_coalesce(struct isl_map *map)
2613 int i;
2614 unsigned n;
2615 isl_ctx *ctx;
2616 struct isl_coalesce_info *info = NULL;
2618 map = isl_map_remove_empty_parts(map);
2619 if (!map)
2620 return NULL;
2622 if (map->n <= 1)
2623 return map;
2625 ctx = isl_map_get_ctx(map);
2626 map = isl_map_sort_divs(map);
2627 map = isl_map_cow(map);
2629 if (!map)
2630 return NULL;
2632 n = map->n;
2634 info = isl_calloc_array(map->ctx, struct isl_coalesce_info, n);
2635 if (!info)
2636 goto error;
2638 for (i = 0; i < map->n; ++i) {
2639 map->p[i] = isl_basic_map_reduce_coefficients(map->p[i]);
2640 if (!map->p[i])
2641 goto error;
2642 info[i].bmap = isl_basic_map_copy(map->p[i]);
2643 info[i].tab = isl_tab_from_basic_map(info[i].bmap, 0);
2644 if (!info[i].tab)
2645 goto error;
2646 if (!ISL_F_ISSET(info[i].bmap, ISL_BASIC_MAP_NO_IMPLICIT))
2647 if (isl_tab_detect_implicit_equalities(info[i].tab) < 0)
2648 goto error;
2649 info[i].bmap = isl_tab_make_equalities_explicit(info[i].tab,
2650 info[i].bmap);
2651 if (!info[i].bmap)
2652 goto error;
2653 if (!ISL_F_ISSET(info[i].bmap, ISL_BASIC_MAP_NO_REDUNDANT))
2654 if (isl_tab_detect_redundant(info[i].tab) < 0)
2655 goto error;
2656 if (coalesce_info_set_hull_hash(&info[i]) < 0)
2657 goto error;
2659 for (i = map->n - 1; i >= 0; --i)
2660 if (info[i].tab->empty)
2661 drop(&info[i]);
2663 if (coalesce(ctx, n, info) < 0)
2664 goto error;
2666 map = update_basic_maps(map, n, info);
2668 clear_coalesce_info(n, info);
2670 return map;
2671 error:
2672 clear_coalesce_info(n, info);
2673 isl_map_free(map);
2674 return NULL;
2677 /* For each pair of basic sets in the set, check if the union of the two
2678 * can be represented by a single basic set.
2679 * If so, replace the pair by the single basic set and start over.
2681 struct isl_set *isl_set_coalesce(struct isl_set *set)
2683 return (struct isl_set *)isl_map_coalesce((struct isl_map *)set);