add isl_local_space_wrap
[isl.git] / isl_coalesce.c
blobd15f7d28507d5df616c8fe3955a13371085d01f6
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2012-2013 Ecole Normale Superieure
5 * Copyright 2014 INRIA Rocquencourt
7 * Use of this software is governed by the MIT license
9 * Written by Sven Verdoolaege, K.U.Leuven, Departement
10 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
11 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
12 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
13 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
14 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
15 * B.P. 105 - 78153 Le Chesnay, France
18 #include "isl_map_private.h"
19 #include <isl_seq.h>
20 #include <isl/options.h>
21 #include "isl_tab.h"
22 #include <isl_mat_private.h>
23 #include <isl_local_space_private.h>
24 #include <isl_vec_private.h>
26 #define STATUS_ERROR -1
27 #define STATUS_REDUNDANT 1
28 #define STATUS_VALID 2
29 #define STATUS_SEPARATE 3
30 #define STATUS_CUT 4
31 #define STATUS_ADJ_EQ 5
32 #define STATUS_ADJ_INEQ 6
34 static int status_in(isl_int *ineq, struct isl_tab *tab)
36 enum isl_ineq_type type = isl_tab_ineq_type(tab, ineq);
37 switch (type) {
38 default:
39 case isl_ineq_error: return STATUS_ERROR;
40 case isl_ineq_redundant: return STATUS_VALID;
41 case isl_ineq_separate: return STATUS_SEPARATE;
42 case isl_ineq_cut: return STATUS_CUT;
43 case isl_ineq_adj_eq: return STATUS_ADJ_EQ;
44 case isl_ineq_adj_ineq: return STATUS_ADJ_INEQ;
48 /* Compute the position of the equalities of basic map "bmap_i"
49 * with respect to the basic map represented by "tab_j".
50 * The resulting array has twice as many entries as the number
51 * of equalities corresponding to the two inequalties to which
52 * each equality corresponds.
54 static int *eq_status_in(__isl_keep isl_basic_map *bmap_i,
55 struct isl_tab *tab_j)
57 int k, l;
58 int *eq = isl_calloc_array(bmap_i->ctx, int, 2 * bmap_i->n_eq);
59 unsigned dim;
61 if (!eq)
62 return NULL;
64 dim = isl_basic_map_total_dim(bmap_i);
65 for (k = 0; k < bmap_i->n_eq; ++k) {
66 for (l = 0; l < 2; ++l) {
67 isl_seq_neg(bmap_i->eq[k], bmap_i->eq[k], 1+dim);
68 eq[2 * k + l] = status_in(bmap_i->eq[k], tab_j);
69 if (eq[2 * k + l] == STATUS_ERROR)
70 goto error;
72 if (eq[2 * k] == STATUS_SEPARATE ||
73 eq[2 * k + 1] == STATUS_SEPARATE)
74 break;
77 return eq;
78 error:
79 free(eq);
80 return NULL;
83 /* Compute the position of the inequalities of basic map "bmap_i"
84 * (also represented by "tab_i", if not NULL) with respect to the basic map
85 * represented by "tab_j".
87 static int *ineq_status_in(__isl_keep isl_basic_map *bmap_i,
88 struct isl_tab *tab_i, struct isl_tab *tab_j)
90 int k;
91 unsigned n_eq = bmap_i->n_eq;
92 int *ineq = isl_calloc_array(bmap_i->ctx, int, bmap_i->n_ineq);
94 if (!ineq)
95 return NULL;
97 for (k = 0; k < bmap_i->n_ineq; ++k) {
98 if (tab_i && isl_tab_is_redundant(tab_i, n_eq + k)) {
99 ineq[k] = STATUS_REDUNDANT;
100 continue;
102 ineq[k] = status_in(bmap_i->ineq[k], tab_j);
103 if (ineq[k] == STATUS_ERROR)
104 goto error;
105 if (ineq[k] == STATUS_SEPARATE)
106 break;
109 return ineq;
110 error:
111 free(ineq);
112 return NULL;
115 static int any(int *con, unsigned len, int status)
117 int i;
119 for (i = 0; i < len ; ++i)
120 if (con[i] == status)
121 return 1;
122 return 0;
125 static int count(int *con, unsigned len, int status)
127 int i;
128 int c = 0;
130 for (i = 0; i < len ; ++i)
131 if (con[i] == status)
132 c++;
133 return c;
136 static int all(int *con, unsigned len, int status)
138 int i;
140 for (i = 0; i < len ; ++i) {
141 if (con[i] == STATUS_REDUNDANT)
142 continue;
143 if (con[i] != status)
144 return 0;
146 return 1;
149 /* Internal information associated to a basic map in a map
150 * that is to be coalesced by isl_map_coalesce.
152 * "bmap" is the basic map itself (or NULL if "removed" is set)
153 * "tab" is the corresponding tableau (or NULL if "removed" is set)
154 * "removed" is set if this basic map has been removed from the map
156 * "eq" and "ineq" are only set if we are currently trying to coalesce
157 * this basic map with another basic map, in which case they represent
158 * the position of the inequalities of this basic map with respect to
159 * the other basic map. The number of elements in the "eq" array
160 * is twice the number of equalities in the "bmap", corresponding
161 * to the two inequalities that make up each equality.
163 struct isl_coalesce_info {
164 isl_basic_map *bmap;
165 struct isl_tab *tab;
166 int removed;
167 int *eq;
168 int *ineq;
171 /* Free all the allocated memory in an array
172 * of "n" isl_coalesce_info elements.
174 static void clear_coalesce_info(int n, struct isl_coalesce_info *info)
176 int i;
178 if (!info)
179 return;
181 for (i = 0; i < n; ++i) {
182 isl_basic_map_free(info[i].bmap);
183 isl_tab_free(info[i].tab);
186 free(info);
189 /* Drop the basic map represented by "info".
190 * That is, clear the memory associated to the entry and
191 * mark it as having been removed.
193 static void drop(struct isl_coalesce_info *info)
195 info->bmap = isl_basic_map_free(info->bmap);
196 isl_tab_free(info->tab);
197 info->tab = NULL;
198 info->removed = 1;
201 /* Exchange the information in "info1" with that in "info2".
203 static void exchange(struct isl_coalesce_info *info1,
204 struct isl_coalesce_info *info2)
206 struct isl_coalesce_info info;
208 info = *info1;
209 *info1 = *info2;
210 *info2 = info;
213 /* This type represents the kind of change that has been performed
214 * while trying to coalesce two basic maps.
216 * isl_change_none: nothing was changed
217 * isl_change_drop_first: the first basic map was removed
218 * isl_change_drop_second: the second basic map was removed
219 * isl_change_fuse: the two basic maps were replaced by a new basic map.
221 enum isl_change {
222 isl_change_error = -1,
223 isl_change_none = 0,
224 isl_change_drop_first,
225 isl_change_drop_second,
226 isl_change_fuse,
229 /* Add the valid constraints of the basic map represented by "info"
230 * to "bmap". "len" is the size of the constraints.
231 * If only one of the pair of inequalities that make up an equality
232 * is valid, then add that inequality.
234 static __isl_give isl_basic_map *add_valid_constraints(
235 __isl_take isl_basic_map *bmap, struct isl_coalesce_info *info,
236 unsigned len)
238 int k, l;
240 if (!bmap)
241 return NULL;
243 for (k = 0; k < info->bmap->n_eq; ++k) {
244 if (info->eq[2 * k] == STATUS_VALID &&
245 info->eq[2 * k + 1] == STATUS_VALID) {
246 l = isl_basic_map_alloc_equality(bmap);
247 if (l < 0)
248 return isl_basic_map_free(bmap);
249 isl_seq_cpy(bmap->eq[l], info->bmap->eq[k], len);
250 } else if (info->eq[2 * k] == STATUS_VALID) {
251 l = isl_basic_map_alloc_inequality(bmap);
252 if (l < 0)
253 return isl_basic_map_free(bmap);
254 isl_seq_neg(bmap->ineq[l], info->bmap->eq[k], len);
255 } else if (info->eq[2 * k + 1] == STATUS_VALID) {
256 l = isl_basic_map_alloc_inequality(bmap);
257 if (l < 0)
258 return isl_basic_map_free(bmap);
259 isl_seq_cpy(bmap->ineq[l], info->bmap->eq[k], len);
263 for (k = 0; k < info->bmap->n_ineq; ++k) {
264 if (info->ineq[k] != STATUS_VALID)
265 continue;
266 l = isl_basic_map_alloc_inequality(bmap);
267 if (l < 0)
268 return isl_basic_map_free(bmap);
269 isl_seq_cpy(bmap->ineq[l], info->bmap->ineq[k], len);
272 return bmap;
275 /* Is "bmap" defined by a number of (non-redundant) constraints that
276 * is greater than the number of constraints of basic maps i and j combined?
277 * Equalities are counted as two inequalities.
279 static int number_of_constraints_increases(int i, int j,
280 struct isl_coalesce_info *info,
281 __isl_keep isl_basic_map *bmap, struct isl_tab *tab)
283 int k, n_old, n_new;
285 n_old = 2 * info[i].bmap->n_eq + info[i].bmap->n_ineq;
286 n_old += 2 * info[j].bmap->n_eq + info[j].bmap->n_ineq;
288 n_new = 2 * bmap->n_eq;
289 for (k = 0; k < bmap->n_ineq; ++k)
290 if (!isl_tab_is_redundant(tab, bmap->n_eq + k))
291 ++n_new;
293 return n_new > n_old;
296 /* Replace the pair of basic maps i and j by the basic map bounded
297 * by the valid constraints in both basic maps and the constraints
298 * in extra (if not NULL).
299 * Place the fused basic map in the position that is the smallest of i and j.
301 * If "detect_equalities" is set, then look for equalities encoded
302 * as pairs of inequalities.
303 * If "check_number" is set, then the original basic maps are only
304 * replaced if the total number of constraints does not increase.
306 static enum isl_change fuse(int i, int j, struct isl_coalesce_info *info,
307 __isl_keep isl_mat *extra, int detect_equalities, int check_number)
309 int k, l;
310 struct isl_basic_map *fused = NULL;
311 struct isl_tab *fused_tab = NULL;
312 unsigned total = isl_basic_map_total_dim(info[i].bmap);
313 unsigned extra_rows = extra ? extra->n_row : 0;
314 unsigned n_eq, n_ineq;
316 if (j < i)
317 return fuse(j, i, info, extra, detect_equalities, check_number);
319 n_eq = info[i].bmap->n_eq + info[j].bmap->n_eq;
320 n_ineq = info[i].bmap->n_ineq + info[j].bmap->n_ineq;
321 fused = isl_basic_map_alloc_space(isl_space_copy(info[i].bmap->dim),
322 info[i].bmap->n_div, n_eq, n_eq + n_ineq + extra_rows);
323 fused = add_valid_constraints(fused, &info[i], 1 + total);
324 fused = add_valid_constraints(fused, &info[j], 1 + total);
325 if (!fused)
326 goto error;
328 for (k = 0; k < info[i].bmap->n_div; ++k) {
329 int l = isl_basic_map_alloc_div(fused);
330 if (l < 0)
331 goto error;
332 isl_seq_cpy(fused->div[l], info[i].bmap->div[k], 1 + 1 + total);
335 for (k = 0; k < extra_rows; ++k) {
336 l = isl_basic_map_alloc_inequality(fused);
337 if (l < 0)
338 goto error;
339 isl_seq_cpy(fused->ineq[l], extra->row[k], 1 + total);
342 if (detect_equalities)
343 fused = isl_basic_map_detect_inequality_pairs(fused, NULL);
344 fused = isl_basic_map_gauss(fused, NULL);
345 ISL_F_SET(fused, ISL_BASIC_MAP_FINAL);
346 if (ISL_F_ISSET(info[i].bmap, ISL_BASIC_MAP_RATIONAL) &&
347 ISL_F_ISSET(info[j].bmap, ISL_BASIC_MAP_RATIONAL))
348 ISL_F_SET(fused, ISL_BASIC_MAP_RATIONAL);
350 fused_tab = isl_tab_from_basic_map(fused, 0);
351 if (isl_tab_detect_redundant(fused_tab) < 0)
352 goto error;
354 if (check_number &&
355 number_of_constraints_increases(i, j, info, fused, fused_tab)) {
356 isl_tab_free(fused_tab);
357 isl_basic_map_free(fused);
358 return isl_change_none;
361 isl_basic_map_free(info[i].bmap);
362 info[i].bmap = fused;
363 isl_tab_free(info[i].tab);
364 info[i].tab = fused_tab;
365 drop(&info[j]);
367 return isl_change_fuse;
368 error:
369 isl_tab_free(fused_tab);
370 isl_basic_map_free(fused);
371 return isl_change_error;
374 /* Given a pair of basic maps i and j such that all constraints are either
375 * "valid" or "cut", check if the facets corresponding to the "cut"
376 * constraints of i lie entirely within basic map j.
377 * If so, replace the pair by the basic map consisting of the valid
378 * constraints in both basic maps.
379 * Checking whether the facet lies entirely within basic map j
380 * is performed by checking whether the constraints of basic map j
381 * are valid for the facet. These tests are performed on a rational
382 * tableau to avoid the theoretical possibility that a constraint
383 * that was considered to be a cut constraint for the entire basic map i
384 * happens to be considered to be a valid constraint for the facet,
385 * even though it cuts off the same rational points.
387 * To see that we are not introducing any extra points, call the
388 * two basic maps A and B and the resulting map U and let x
389 * be an element of U \setminus ( A \cup B ).
390 * A line connecting x with an element of A \cup B meets a facet F
391 * of either A or B. Assume it is a facet of B and let c_1 be
392 * the corresponding facet constraint. We have c_1(x) < 0 and
393 * so c_1 is a cut constraint. This implies that there is some
394 * (possibly rational) point x' satisfying the constraints of A
395 * and the opposite of c_1 as otherwise c_1 would have been marked
396 * valid for A. The line connecting x and x' meets a facet of A
397 * in a (possibly rational) point that also violates c_1, but this
398 * is impossible since all cut constraints of B are valid for all
399 * cut facets of A.
400 * In case F is a facet of A rather than B, then we can apply the
401 * above reasoning to find a facet of B separating x from A \cup B first.
403 static enum isl_change check_facets(int i, int j,
404 struct isl_coalesce_info *info)
406 int k, l;
407 struct isl_tab_undo *snap, *snap2;
408 unsigned n_eq = info[i].bmap->n_eq;
410 snap = isl_tab_snap(info[i].tab);
411 if (isl_tab_mark_rational(info[i].tab) < 0)
412 return isl_change_error;
413 snap2 = isl_tab_snap(info[i].tab);
415 for (k = 0; k < info[i].bmap->n_ineq; ++k) {
416 if (info[i].ineq[k] != STATUS_CUT)
417 continue;
418 if (isl_tab_select_facet(info[i].tab, n_eq + k) < 0)
419 return isl_change_error;
420 for (l = 0; l < info[j].bmap->n_ineq; ++l) {
421 int stat;
422 if (info[j].ineq[l] != STATUS_CUT)
423 continue;
424 stat = status_in(info[j].bmap->ineq[l], info[i].tab);
425 if (stat != STATUS_VALID)
426 break;
428 if (isl_tab_rollback(info[i].tab, snap2) < 0)
429 return isl_change_error;
430 if (l < info[j].bmap->n_ineq)
431 break;
434 if (k < info[i].bmap->n_ineq) {
435 if (isl_tab_rollback(info[i].tab, snap) < 0)
436 return isl_change_error;
437 return isl_change_none;
439 return fuse(i, j, info, NULL, 0, 0);
442 /* Check if info->bmap contains the basic map represented
443 * by the tableau "tab".
444 * For each equality, we check both the constraint itself
445 * (as an inequality) and its negation. Make sure the
446 * equality is returned to its original state before returning.
448 static int contains(struct isl_coalesce_info *info, struct isl_tab *tab)
450 int k;
451 unsigned dim;
452 isl_basic_map *bmap = info->bmap;
454 dim = isl_basic_map_total_dim(bmap);
455 for (k = 0; k < bmap->n_eq; ++k) {
456 int stat;
457 isl_seq_neg(bmap->eq[k], bmap->eq[k], 1 + dim);
458 stat = status_in(bmap->eq[k], tab);
459 isl_seq_neg(bmap->eq[k], bmap->eq[k], 1 + dim);
460 if (stat != STATUS_VALID)
461 return 0;
462 stat = status_in(bmap->eq[k], tab);
463 if (stat != STATUS_VALID)
464 return 0;
467 for (k = 0; k < bmap->n_ineq; ++k) {
468 int stat;
469 if (info->ineq[k] == STATUS_REDUNDANT)
470 continue;
471 stat = status_in(bmap->ineq[k], tab);
472 if (stat != STATUS_VALID)
473 return 0;
475 return 1;
478 /* Basic map "i" has an inequality (say "k") that is adjacent
479 * to some inequality of basic map "j". All the other inequalities
480 * are valid for "j".
481 * Check if basic map "j" forms an extension of basic map "i".
483 * Note that this function is only called if some of the equalities or
484 * inequalities of basic map "j" do cut basic map "i". The function is
485 * correct even if there are no such cut constraints, but in that case
486 * the additional checks performed by this function are overkill.
488 * In particular, we replace constraint k, say f >= 0, by constraint
489 * f <= -1, add the inequalities of "j" that are valid for "i"
490 * and check if the result is a subset of basic map "j".
491 * If so, then we know that this result is exactly equal to basic map "j"
492 * since all its constraints are valid for basic map "j".
493 * By combining the valid constraints of "i" (all equalities and all
494 * inequalities except "k") and the valid constraints of "j" we therefore
495 * obtain a basic map that is equal to their union.
496 * In this case, there is no need to perform a rollback of the tableau
497 * since it is going to be destroyed in fuse().
500 * |\__ |\__
501 * | \__ | \__
502 * | \_ => | \__
503 * |_______| _ |_________\
506 * |\ |\
507 * | \ | \
508 * | \ | \
509 * | | | \
510 * | ||\ => | \
511 * | || \ | \
512 * | || | | |
513 * |__||_/ |_____/
515 static enum isl_change is_adj_ineq_extension(int i, int j,
516 struct isl_coalesce_info *info)
518 int k;
519 struct isl_tab_undo *snap;
520 unsigned n_eq = info[i].bmap->n_eq;
521 unsigned total = isl_basic_map_total_dim(info[i].bmap);
522 int r;
524 if (isl_tab_extend_cons(info[i].tab, 1 + info[j].bmap->n_ineq) < 0)
525 return isl_change_error;
527 for (k = 0; k < info[i].bmap->n_ineq; ++k)
528 if (info[i].ineq[k] == STATUS_ADJ_INEQ)
529 break;
530 if (k >= info[i].bmap->n_ineq)
531 isl_die(isl_basic_map_get_ctx(info[i].bmap), isl_error_internal,
532 "info[i].ineq should have exactly one STATUS_ADJ_INEQ",
533 return isl_change_error);
535 snap = isl_tab_snap(info[i].tab);
537 if (isl_tab_unrestrict(info[i].tab, n_eq + k) < 0)
538 return isl_change_error;
540 isl_seq_neg(info[i].bmap->ineq[k], info[i].bmap->ineq[k], 1 + total);
541 isl_int_sub_ui(info[i].bmap->ineq[k][0], info[i].bmap->ineq[k][0], 1);
542 r = isl_tab_add_ineq(info[i].tab, info[i].bmap->ineq[k]);
543 isl_seq_neg(info[i].bmap->ineq[k], info[i].bmap->ineq[k], 1 + total);
544 isl_int_sub_ui(info[i].bmap->ineq[k][0], info[i].bmap->ineq[k][0], 1);
545 if (r < 0)
546 return isl_change_error;
548 for (k = 0; k < info[j].bmap->n_ineq; ++k) {
549 if (info[j].ineq[k] != STATUS_VALID)
550 continue;
551 if (isl_tab_add_ineq(info[i].tab, info[j].bmap->ineq[k]) < 0)
552 return isl_change_error;
555 if (contains(&info[j], info[i].tab))
556 return fuse(i, j, info, NULL, 0, 0);
558 if (isl_tab_rollback(info[i].tab, snap) < 0)
559 return isl_change_error;
561 return isl_change_none;
565 /* Both basic maps have at least one inequality with and adjacent
566 * (but opposite) inequality in the other basic map.
567 * Check that there are no cut constraints and that there is only
568 * a single pair of adjacent inequalities.
569 * If so, we can replace the pair by a single basic map described
570 * by all but the pair of adjacent inequalities.
571 * Any additional points introduced lie strictly between the two
572 * adjacent hyperplanes and can therefore be integral.
574 * ____ _____
575 * / ||\ / \
576 * / || \ / \
577 * \ || \ => \ \
578 * \ || / \ /
579 * \___||_/ \_____/
581 * The test for a single pair of adjancent inequalities is important
582 * for avoiding the combination of two basic maps like the following
584 * /|
585 * / |
586 * /__|
587 * _____
588 * | |
589 * | |
590 * |___|
592 * If there are some cut constraints on one side, then we may
593 * still be able to fuse the two basic maps, but we need to perform
594 * some additional checks in is_adj_ineq_extension.
596 static enum isl_change check_adj_ineq(int i, int j,
597 struct isl_coalesce_info *info)
599 int count_i, count_j;
600 int cut_i, cut_j;
602 count_i = count(info[i].ineq, info[i].bmap->n_ineq, STATUS_ADJ_INEQ);
603 count_j = count(info[j].ineq, info[j].bmap->n_ineq, STATUS_ADJ_INEQ);
605 if (count_i != 1 && count_j != 1)
606 return isl_change_none;
608 cut_i = any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_CUT) ||
609 any(info[i].ineq, info[i].bmap->n_ineq, STATUS_CUT);
610 cut_j = any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_CUT) ||
611 any(info[j].ineq, info[j].bmap->n_ineq, STATUS_CUT);
613 if (!cut_i && !cut_j && count_i == 1 && count_j == 1)
614 return fuse(i, j, info, NULL, 0, 0);
616 if (count_i == 1 && !cut_i)
617 return is_adj_ineq_extension(i, j, info);
619 if (count_j == 1 && !cut_j)
620 return is_adj_ineq_extension(j, i, info);
622 return isl_change_none;
625 /* Basic map "i" has an inequality "k" that is adjacent to some equality
626 * of basic map "j". All the other inequalities are valid for "j".
627 * Check if basic map "j" forms an extension of basic map "i".
629 * In particular, we relax constraint "k", compute the corresponding
630 * facet and check whether it is included in the other basic map.
631 * If so, we know that relaxing the constraint extends the basic
632 * map with exactly the other basic map (we already know that this
633 * other basic map is included in the extension, because there
634 * were no "cut" inequalities in "i") and we can replace the
635 * two basic maps by this extension.
636 * Place this extension in the position that is the smallest of i and j.
637 * ____ _____
638 * / || / |
639 * / || / |
640 * \ || => \ |
641 * \ || \ |
642 * \___|| \____|
644 static enum isl_change is_adj_eq_extension(int i, int j, int k,
645 struct isl_coalesce_info *info)
647 int change = isl_change_none;
648 int super;
649 struct isl_tab_undo *snap, *snap2;
650 unsigned n_eq = info[i].bmap->n_eq;
652 if (isl_tab_is_equality(info[i].tab, n_eq + k))
653 return isl_change_none;
655 snap = isl_tab_snap(info[i].tab);
656 if (isl_tab_relax(info[i].tab, n_eq + k) < 0)
657 return isl_change_error;
658 snap2 = isl_tab_snap(info[i].tab);
659 if (isl_tab_select_facet(info[i].tab, n_eq + k) < 0)
660 return isl_change_error;
661 super = contains(&info[j], info[i].tab);
662 if (super) {
663 if (isl_tab_rollback(info[i].tab, snap2) < 0)
664 return isl_change_error;
665 info[i].bmap = isl_basic_map_cow(info[i].bmap);
666 if (!info[i].bmap)
667 return isl_change_error;
668 isl_int_add_ui(info[i].bmap->ineq[k][0],
669 info[i].bmap->ineq[k][0], 1);
670 ISL_F_SET(info[i].bmap, ISL_BASIC_MAP_FINAL);
671 drop(&info[j]);
672 if (j < i)
673 exchange(&info[i], &info[j]);
674 change = isl_change_fuse;
675 } else
676 if (isl_tab_rollback(info[i].tab, snap) < 0)
677 return isl_change_error;
679 return change;
682 /* Data structure that keeps track of the wrapping constraints
683 * and of information to bound the coefficients of those constraints.
685 * bound is set if we want to apply a bound on the coefficients
686 * mat contains the wrapping constraints
687 * max is the bound on the coefficients (if bound is set)
689 struct isl_wraps {
690 int bound;
691 isl_mat *mat;
692 isl_int max;
695 /* Update wraps->max to be greater than or equal to the coefficients
696 * in the equalities and inequalities of info->bmap that can be removed
697 * if we end up applying wrapping.
699 static void wraps_update_max(struct isl_wraps *wraps,
700 struct isl_coalesce_info *info)
702 int k;
703 isl_int max_k;
704 unsigned total = isl_basic_map_total_dim(info->bmap);
706 isl_int_init(max_k);
708 for (k = 0; k < info->bmap->n_eq; ++k) {
709 if (info->eq[2 * k] == STATUS_VALID &&
710 info->eq[2 * k + 1] == STATUS_VALID)
711 continue;
712 isl_seq_abs_max(info->bmap->eq[k] + 1, total, &max_k);
713 if (isl_int_abs_gt(max_k, wraps->max))
714 isl_int_set(wraps->max, max_k);
717 for (k = 0; k < info->bmap->n_ineq; ++k) {
718 if (info->ineq[k] == STATUS_VALID ||
719 info->ineq[k] == STATUS_REDUNDANT)
720 continue;
721 isl_seq_abs_max(info->bmap->ineq[k] + 1, total, &max_k);
722 if (isl_int_abs_gt(max_k, wraps->max))
723 isl_int_set(wraps->max, max_k);
726 isl_int_clear(max_k);
729 /* Initialize the isl_wraps data structure.
730 * If we want to bound the coefficients of the wrapping constraints,
731 * we set wraps->max to the largest coefficient
732 * in the equalities and inequalities that can be removed if we end up
733 * applying wrapping.
735 static void wraps_init(struct isl_wraps *wraps, __isl_take isl_mat *mat,
736 struct isl_coalesce_info *info, int i, int j)
738 isl_ctx *ctx;
740 wraps->bound = 0;
741 wraps->mat = mat;
742 if (!mat)
743 return;
744 ctx = isl_mat_get_ctx(mat);
745 wraps->bound = isl_options_get_coalesce_bounded_wrapping(ctx);
746 if (!wraps->bound)
747 return;
748 isl_int_init(wraps->max);
749 isl_int_set_si(wraps->max, 0);
750 wraps_update_max(wraps, &info[i]);
751 wraps_update_max(wraps, &info[j]);
754 /* Free the contents of the isl_wraps data structure.
756 static void wraps_free(struct isl_wraps *wraps)
758 isl_mat_free(wraps->mat);
759 if (wraps->bound)
760 isl_int_clear(wraps->max);
763 /* Is the wrapping constraint in row "row" allowed?
765 * If wraps->bound is set, we check that none of the coefficients
766 * is greater than wraps->max.
768 static int allow_wrap(struct isl_wraps *wraps, int row)
770 int i;
772 if (!wraps->bound)
773 return 1;
775 for (i = 1; i < wraps->mat->n_col; ++i)
776 if (isl_int_abs_gt(wraps->mat->row[row][i], wraps->max))
777 return 0;
779 return 1;
782 /* Wrap "ineq" (or its opposite if "negate" is set) around "bound"
783 * to include "set" and add the result in position "w" of "wraps".
784 * "len" is the total number of coefficients in "bound" and "ineq".
785 * Return 1 on success, 0 on failure and -1 on error.
786 * Wrapping can fail if the result of wrapping is equal to "bound"
787 * or if we want to bound the sizes of the coefficients and
788 * the wrapped constraint does not satisfy this bound.
790 static int add_wrap(struct isl_wraps *wraps, int w, isl_int *bound,
791 isl_int *ineq, unsigned len, __isl_keep isl_set *set, int negate)
793 isl_seq_cpy(wraps->mat->row[w], bound, len);
794 if (negate) {
795 isl_seq_neg(wraps->mat->row[w + 1], ineq, len);
796 ineq = wraps->mat->row[w + 1];
798 if (!isl_set_wrap_facet(set, wraps->mat->row[w], ineq))
799 return -1;
800 if (isl_seq_eq(wraps->mat->row[w], bound, len))
801 return 0;
802 if (!allow_wrap(wraps, w))
803 return 0;
804 return 1;
807 /* For each constraint in info->bmap that is not redundant (as determined
808 * by info->tab) and that is not a valid constraint for the other basic map,
809 * wrap the constraint around "bound" such that it includes the whole
810 * set "set" and append the resulting constraint to "wraps".
811 * Note that the constraints that are valid for the other basic map
812 * will be added to the combined basic map by default, so there is
813 * no need to wrap them.
814 * The caller wrap_in_facets even relies on this function not wrapping
815 * any constraints that are already valid.
816 * "wraps" is assumed to have been pre-allocated to the appropriate size.
817 * wraps->n_row is the number of actual wrapped constraints that have
818 * been added.
819 * If any of the wrapping problems results in a constraint that is
820 * identical to "bound", then this means that "set" is unbounded in such
821 * way that no wrapping is possible. If this happens then wraps->n_row
822 * is reset to zero.
823 * Similarly, if we want to bound the coefficients of the wrapping
824 * constraints and a newly added wrapping constraint does not
825 * satisfy the bound, then wraps->n_row is also reset to zero.
827 static int add_wraps(struct isl_wraps *wraps, struct isl_coalesce_info *info,
828 isl_int *bound, __isl_keep isl_set *set)
830 int l, m;
831 int w;
832 int added;
833 isl_basic_map *bmap = info->bmap;
834 unsigned len = 1 + isl_basic_map_total_dim(bmap);
836 w = wraps->mat->n_row;
838 for (l = 0; l < bmap->n_ineq; ++l) {
839 if (info->ineq[l] == STATUS_VALID ||
840 info->ineq[l] == STATUS_REDUNDANT)
841 continue;
842 if (isl_seq_is_neg(bound, bmap->ineq[l], len))
843 continue;
844 if (isl_seq_eq(bound, bmap->ineq[l], len))
845 continue;
846 if (isl_tab_is_redundant(info->tab, bmap->n_eq + l))
847 continue;
849 added = add_wrap(wraps, w, bound, bmap->ineq[l], len, set, 0);
850 if (added < 0)
851 return -1;
852 if (!added)
853 goto unbounded;
854 ++w;
856 for (l = 0; l < bmap->n_eq; ++l) {
857 if (isl_seq_is_neg(bound, bmap->eq[l], len))
858 continue;
859 if (isl_seq_eq(bound, bmap->eq[l], len))
860 continue;
862 for (m = 0; m < 2; ++m) {
863 if (info->eq[2 * l + m] == STATUS_VALID)
864 continue;
865 added = add_wrap(wraps, w, bound, bmap->eq[l], len,
866 set, !m);
867 if (added < 0)
868 return -1;
869 if (!added)
870 goto unbounded;
871 ++w;
875 wraps->mat->n_row = w;
876 return 0;
877 unbounded:
878 wraps->mat->n_row = 0;
879 return 0;
882 /* Check if the constraints in "wraps" from "first" until the last
883 * are all valid for the basic set represented by "tab".
884 * If not, wraps->n_row is set to zero.
886 static int check_wraps(__isl_keep isl_mat *wraps, int first,
887 struct isl_tab *tab)
889 int i;
891 for (i = first; i < wraps->n_row; ++i) {
892 enum isl_ineq_type type;
893 type = isl_tab_ineq_type(tab, wraps->row[i]);
894 if (type == isl_ineq_error)
895 return -1;
896 if (type == isl_ineq_redundant)
897 continue;
898 wraps->n_row = 0;
899 return 0;
902 return 0;
905 /* Return a set that corresponds to the non-redundant constraints
906 * (as recorded in tab) of bmap.
908 * It's important to remove the redundant constraints as some
909 * of the other constraints may have been modified after the
910 * constraints were marked redundant.
911 * In particular, a constraint may have been relaxed.
912 * Redundant constraints are ignored when a constraint is relaxed
913 * and should therefore continue to be ignored ever after.
914 * Otherwise, the relaxation might be thwarted by some of
915 * these constraints.
917 * Update the underlying set to ensure that the dimension doesn't change.
918 * Otherwise the integer divisions could get dropped if the tab
919 * turns out to be empty.
921 static __isl_give isl_set *set_from_updated_bmap(__isl_keep isl_basic_map *bmap,
922 struct isl_tab *tab)
924 isl_basic_set *bset;
926 bmap = isl_basic_map_copy(bmap);
927 bset = isl_basic_map_underlying_set(bmap);
928 bset = isl_basic_set_cow(bset);
929 bset = isl_basic_set_update_from_tab(bset, tab);
930 return isl_set_from_basic_set(bset);
933 /* Wrap the constraints of info->bmap that bound the facet defined
934 * by inequality "k" around (the opposite of) this inequality to
935 * include "set". "bound" may be used to store the negated inequality.
936 * Since the wrapped constraints are not guaranteed to contain the whole
937 * of info->bmap, we check them in check_wraps.
938 * If any of the wrapped constraints turn out to be invalid, then
939 * check_wraps will reset wrap->n_row to zero.
941 static int add_wraps_around_facet(struct isl_wraps *wraps,
942 struct isl_coalesce_info *info, int k, isl_int *bound,
943 __isl_keep isl_set *set)
945 struct isl_tab_undo *snap;
946 int n;
947 unsigned total = isl_basic_map_total_dim(info->bmap);
949 snap = isl_tab_snap(info->tab);
951 if (isl_tab_select_facet(info->tab, info->bmap->n_eq + k) < 0)
952 return -1;
953 if (isl_tab_detect_redundant(info->tab) < 0)
954 return -1;
956 isl_seq_neg(bound, info->bmap->ineq[k], 1 + total);
958 n = wraps->mat->n_row;
959 if (add_wraps(wraps, info, bound, set) < 0)
960 return -1;
962 if (isl_tab_rollback(info->tab, snap) < 0)
963 return -1;
964 if (check_wraps(wraps->mat, n, info->tab) < 0)
965 return -1;
967 return 0;
970 /* Given a basic set i with a constraint k that is adjacent to
971 * basic set j, check if we can wrap
972 * both the facet corresponding to k (if "wrap_facet" is set) and basic map j
973 * (always) around their ridges to include the other set.
974 * If so, replace the pair of basic sets by their union.
976 * All constraints of i (except k) are assumed to be valid or
977 * cut constraints for j.
978 * Wrapping the cut constraints to include basic map j may result
979 * in constraints that are no longer valid of basic map i
980 * we have to check that the resulting wrapping constraints are valid for i.
981 * If "wrap_facet" is not set, then all constraints of i (except k)
982 * are assumed to be valid for j.
983 * ____ _____
984 * / | / \
985 * / || / |
986 * \ || => \ |
987 * \ || \ |
988 * \___|| \____|
991 static enum isl_change can_wrap_in_facet(int i, int j, int k,
992 struct isl_coalesce_info *info, int wrap_facet)
994 enum isl_change change = isl_change_none;
995 struct isl_wraps wraps;
996 isl_ctx *ctx;
997 isl_mat *mat;
998 struct isl_set *set_i = NULL;
999 struct isl_set *set_j = NULL;
1000 struct isl_vec *bound = NULL;
1001 unsigned total = isl_basic_map_total_dim(info[i].bmap);
1003 set_i = set_from_updated_bmap(info[i].bmap, info[i].tab);
1004 set_j = set_from_updated_bmap(info[j].bmap, info[j].tab);
1005 ctx = isl_basic_map_get_ctx(info[i].bmap);
1006 mat = isl_mat_alloc(ctx, 2 * (info[i].bmap->n_eq + info[j].bmap->n_eq) +
1007 info[i].bmap->n_ineq + info[j].bmap->n_ineq,
1008 1 + total);
1009 wraps_init(&wraps, mat, info, i, j);
1010 bound = isl_vec_alloc(ctx, 1 + total);
1011 if (!set_i || !set_j || !wraps.mat || !bound)
1012 goto error;
1014 isl_seq_cpy(bound->el, info[i].bmap->ineq[k], 1 + total);
1015 isl_int_add_ui(bound->el[0], bound->el[0], 1);
1017 isl_seq_cpy(wraps.mat->row[0], bound->el, 1 + total);
1018 wraps.mat->n_row = 1;
1020 if (add_wraps(&wraps, &info[j], bound->el, set_i) < 0)
1021 goto error;
1022 if (!wraps.mat->n_row)
1023 goto unbounded;
1025 if (wrap_facet) {
1026 if (add_wraps_around_facet(&wraps, &info[i], k,
1027 bound->el, set_j) < 0)
1028 goto error;
1029 if (!wraps.mat->n_row)
1030 goto unbounded;
1033 change = fuse(i, j, info, wraps.mat, 0, 0);
1035 unbounded:
1036 wraps_free(&wraps);
1038 isl_set_free(set_i);
1039 isl_set_free(set_j);
1041 isl_vec_free(bound);
1043 return change;
1044 error:
1045 wraps_free(&wraps);
1046 isl_vec_free(bound);
1047 isl_set_free(set_i);
1048 isl_set_free(set_j);
1049 return isl_change_error;
1052 /* Given a pair of basic maps i and j such that j sticks out
1053 * of i at n cut constraints, each time by at most one,
1054 * try to compute wrapping constraints and replace the two
1055 * basic maps by a single basic map.
1056 * The other constraints of i are assumed to be valid for j.
1058 * For each cut constraint t(x) >= 0 of i, we add the relaxed version
1059 * t(x) + 1 >= 0, along with wrapping constraints for all constraints
1060 * of basic map j that bound the part of basic map j that sticks out
1061 * of the cut constraint.
1062 * In particular, we first intersect basic map j with t(x) + 1 = 0.
1063 * If the result is empty, then t(x) >= 0 was actually a valid constraint
1064 * (with respect to the integer points), so we add t(x) >= 0 instead.
1065 * Otherwise, we wrap the constraints of basic map j that are not
1066 * redundant in this intersection and that are not already valid
1067 * for basic map i over basic map i.
1068 * Note that it is sufficient to wrap the constraints to include
1069 * basic map i, because we will only wrap the constraints that do
1070 * not include basic map i already. The wrapped constraint will
1071 * therefore be more relaxed compared to the original constraint.
1072 * Since the original constraint is valid for basic map j, so is
1073 * the wrapped constraint.
1075 * If any wrapping fails, i.e., if we cannot wrap to touch
1076 * the union, then we give up.
1077 * Otherwise, the pair of basic maps is replaced by their union.
1079 static enum isl_change wrap_in_facets(int i, int j, int *cuts, int n,
1080 struct isl_coalesce_info *info)
1082 enum isl_change change = isl_change_none;
1083 struct isl_wraps wraps;
1084 isl_ctx *ctx;
1085 isl_mat *mat;
1086 isl_set *set_i = NULL;
1087 unsigned total = isl_basic_map_total_dim(info[i].bmap);
1088 int max_wrap;
1089 int k, w;
1090 struct isl_tab_undo *snap;
1092 if (isl_tab_extend_cons(info[j].tab, 1) < 0)
1093 goto error;
1095 max_wrap = 1 + 2 * info[j].bmap->n_eq + info[j].bmap->n_ineq;
1096 max_wrap *= n;
1098 set_i = set_from_updated_bmap(info[i].bmap, info[i].tab);
1099 ctx = isl_basic_map_get_ctx(info[i].bmap);
1100 mat = isl_mat_alloc(ctx, max_wrap, 1 + total);
1101 wraps_init(&wraps, mat, info, i, j);
1102 if (!set_i || !wraps.mat)
1103 goto error;
1105 snap = isl_tab_snap(info[j].tab);
1107 wraps.mat->n_row = 0;
1109 for (k = 0; k < n; ++k) {
1110 w = wraps.mat->n_row++;
1111 isl_seq_cpy(wraps.mat->row[w],
1112 info[i].bmap->ineq[cuts[k]], 1 + total);
1113 isl_int_add_ui(wraps.mat->row[w][0], wraps.mat->row[w][0], 1);
1114 if (isl_tab_add_eq(info[j].tab, wraps.mat->row[w]) < 0)
1115 goto error;
1116 if (isl_tab_detect_redundant(info[j].tab) < 0)
1117 goto error;
1119 if (info[j].tab->empty)
1120 isl_int_sub_ui(wraps.mat->row[w][0],
1121 wraps.mat->row[w][0], 1);
1122 else if (add_wraps(&wraps, &info[j],
1123 wraps.mat->row[w], set_i) < 0)
1124 goto error;
1126 if (isl_tab_rollback(info[j].tab, snap) < 0)
1127 goto error;
1129 if (!wraps.mat->n_row)
1130 break;
1133 if (k == n)
1134 change = fuse(i, j, info, wraps.mat, 0, 1);
1136 wraps_free(&wraps);
1137 isl_set_free(set_i);
1139 return change;
1140 error:
1141 wraps_free(&wraps);
1142 isl_set_free(set_i);
1143 return isl_change_error;
1146 /* Given two basic sets i and j such that i has no cut equalities,
1147 * check if relaxing all the cut inequalities of i by one turns
1148 * them into valid constraint for j and check if we can wrap in
1149 * the bits that are sticking out.
1150 * If so, replace the pair by their union.
1152 * We first check if all relaxed cut inequalities of i are valid for j
1153 * and then try to wrap in the intersections of the relaxed cut inequalities
1154 * with j.
1156 * During this wrapping, we consider the points of j that lie at a distance
1157 * of exactly 1 from i. In particular, we ignore the points that lie in
1158 * between this lower-dimensional space and the basic map i.
1159 * We can therefore only apply this to integer maps.
1160 * ____ _____
1161 * / ___|_ / \
1162 * / | | / |
1163 * \ | | => \ |
1164 * \|____| \ |
1165 * \___| \____/
1167 * _____ ______
1168 * | ____|_ | \
1169 * | | | | |
1170 * | | | => | |
1171 * |_| | | |
1172 * |_____| \______|
1174 * _______
1175 * | |
1176 * | |\ |
1177 * | | \ |
1178 * | | \ |
1179 * | | \|
1180 * | | \
1181 * | |_____\
1182 * | |
1183 * |_______|
1185 * Wrapping can fail if the result of wrapping one of the facets
1186 * around its edges does not produce any new facet constraint.
1187 * In particular, this happens when we try to wrap in unbounded sets.
1189 * _______________________________________________________________________
1191 * | ___
1192 * | | |
1193 * |_| |_________________________________________________________________
1194 * |___|
1196 * The following is not an acceptable result of coalescing the above two
1197 * sets as it includes extra integer points.
1198 * _______________________________________________________________________
1200 * |
1201 * |
1203 * \______________________________________________________________________
1205 static enum isl_change can_wrap_in_set(int i, int j,
1206 struct isl_coalesce_info *info)
1208 enum isl_change change = isl_change_none;
1209 int k, m;
1210 int n;
1211 int *cuts = NULL;
1212 isl_ctx *ctx;
1214 if (ISL_F_ISSET(info[i].bmap, ISL_BASIC_MAP_RATIONAL) ||
1215 ISL_F_ISSET(info[j].bmap, ISL_BASIC_MAP_RATIONAL))
1216 return isl_change_none;
1218 n = count(info[i].ineq, info[i].bmap->n_ineq, STATUS_CUT);
1219 if (n == 0)
1220 return isl_change_none;
1222 ctx = isl_basic_map_get_ctx(info[i].bmap);
1223 cuts = isl_alloc_array(ctx, int, n);
1224 if (!cuts)
1225 return isl_change_error;
1227 for (k = 0, m = 0; m < n; ++k) {
1228 enum isl_ineq_type type;
1230 if (info[i].ineq[k] != STATUS_CUT)
1231 continue;
1233 isl_int_add_ui(info[i].bmap->ineq[k][0],
1234 info[i].bmap->ineq[k][0], 1);
1235 type = isl_tab_ineq_type(info[j].tab, info[i].bmap->ineq[k]);
1236 isl_int_sub_ui(info[i].bmap->ineq[k][0],
1237 info[i].bmap->ineq[k][0], 1);
1238 if (type == isl_ineq_error)
1239 goto error;
1240 if (type != isl_ineq_redundant)
1241 break;
1242 cuts[m] = k;
1243 ++m;
1246 if (m == n)
1247 change = wrap_in_facets(i, j, cuts, n, info);
1249 free(cuts);
1251 return change;
1252 error:
1253 free(cuts);
1254 return isl_change_error;
1257 /* Check if either i or j has only cut inequalities that can
1258 * be used to wrap in (a facet of) the other basic set.
1259 * if so, replace the pair by their union.
1261 static enum isl_change check_wrap(int i, int j, struct isl_coalesce_info *info)
1263 enum isl_change change = isl_change_none;
1265 if (!any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_CUT))
1266 change = can_wrap_in_set(i, j, info);
1267 if (change != isl_change_none)
1268 return change;
1270 if (!any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_CUT))
1271 change = can_wrap_in_set(j, i, info);
1272 return change;
1275 /* At least one of the basic maps has an equality that is adjacent
1276 * to inequality. Make sure that only one of the basic maps has
1277 * such an equality and that the other basic map has exactly one
1278 * inequality adjacent to an equality.
1279 * We call the basic map that has the inequality "i" and the basic
1280 * map that has the equality "j".
1281 * If "i" has any "cut" (in)equality, then relaxing the inequality
1282 * by one would not result in a basic map that contains the other
1283 * basic map. However, it may still be possible to wrap in the other
1284 * basic map.
1286 static enum isl_change check_adj_eq(int i, int j,
1287 struct isl_coalesce_info *info)
1289 enum isl_change change = isl_change_none;
1290 int k;
1291 int any_cut;
1293 if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ADJ_INEQ) &&
1294 any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_ADJ_INEQ))
1295 /* ADJ EQ TOO MANY */
1296 return isl_change_none;
1298 if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ADJ_INEQ))
1299 return check_adj_eq(j, i, info);
1301 /* j has an equality adjacent to an inequality in i */
1303 if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_CUT))
1304 return isl_change_none;
1305 any_cut = any(info[i].ineq, info[i].bmap->n_ineq, STATUS_CUT);
1306 if (count(info[i].ineq, info[i].bmap->n_ineq, STATUS_ADJ_EQ) != 1 ||
1307 any(info[j].ineq, info[j].bmap->n_ineq, STATUS_ADJ_EQ) ||
1308 any(info[i].ineq, info[i].bmap->n_ineq, STATUS_ADJ_INEQ) ||
1309 any(info[j].ineq, info[j].bmap->n_ineq, STATUS_ADJ_INEQ))
1310 /* ADJ EQ TOO MANY */
1311 return isl_change_none;
1313 for (k = 0; k < info[i].bmap->n_ineq; ++k)
1314 if (info[i].ineq[k] == STATUS_ADJ_EQ)
1315 break;
1317 if (!any_cut) {
1318 change = is_adj_eq_extension(i, j, k, info);
1319 if (change != isl_change_none)
1320 return change;
1323 change = can_wrap_in_facet(i, j, k, info, any_cut);
1325 return change;
1328 /* The two basic maps lie on adjacent hyperplanes. In particular,
1329 * basic map "i" has an equality that lies parallel to basic map "j".
1330 * Check if we can wrap the facets around the parallel hyperplanes
1331 * to include the other set.
1333 * We perform basically the same operations as can_wrap_in_facet,
1334 * except that we don't need to select a facet of one of the sets.
1336 * \\ \\
1337 * \\ => \\
1338 * \ \|
1340 * If there is more than one equality of "i" adjacent to an equality of "j",
1341 * then the result will satisfy one or more equalities that are a linear
1342 * combination of these equalities. These will be encoded as pairs
1343 * of inequalities in the wrapping constraints and need to be made
1344 * explicit.
1346 static enum isl_change check_eq_adj_eq(int i, int j,
1347 struct isl_coalesce_info *info)
1349 int k;
1350 enum isl_change change = isl_change_none;
1351 int detect_equalities = 0;
1352 struct isl_wraps wraps;
1353 isl_ctx *ctx;
1354 isl_mat *mat;
1355 struct isl_set *set_i = NULL;
1356 struct isl_set *set_j = NULL;
1357 struct isl_vec *bound = NULL;
1358 unsigned total = isl_basic_map_total_dim(info[i].bmap);
1360 if (count(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ADJ_EQ) != 1)
1361 detect_equalities = 1;
1363 for (k = 0; k < 2 * info[i].bmap->n_eq ; ++k)
1364 if (info[i].eq[k] == STATUS_ADJ_EQ)
1365 break;
1367 set_i = set_from_updated_bmap(info[i].bmap, info[i].tab);
1368 set_j = set_from_updated_bmap(info[j].bmap, info[j].tab);
1369 ctx = isl_basic_map_get_ctx(info[i].bmap);
1370 mat = isl_mat_alloc(ctx, 2 * (info[i].bmap->n_eq + info[j].bmap->n_eq) +
1371 info[i].bmap->n_ineq + info[j].bmap->n_ineq,
1372 1 + total);
1373 wraps_init(&wraps, mat, info, i, j);
1374 bound = isl_vec_alloc(ctx, 1 + total);
1375 if (!set_i || !set_j || !wraps.mat || !bound)
1376 goto error;
1378 if (k % 2 == 0)
1379 isl_seq_neg(bound->el, info[i].bmap->eq[k / 2], 1 + total);
1380 else
1381 isl_seq_cpy(bound->el, info[i].bmap->eq[k / 2], 1 + total);
1382 isl_int_add_ui(bound->el[0], bound->el[0], 1);
1384 isl_seq_cpy(wraps.mat->row[0], bound->el, 1 + total);
1385 wraps.mat->n_row = 1;
1387 if (add_wraps(&wraps, &info[j], bound->el, set_i) < 0)
1388 goto error;
1389 if (!wraps.mat->n_row)
1390 goto unbounded;
1392 isl_int_sub_ui(bound->el[0], bound->el[0], 1);
1393 isl_seq_neg(bound->el, bound->el, 1 + total);
1395 isl_seq_cpy(wraps.mat->row[wraps.mat->n_row], bound->el, 1 + total);
1396 wraps.mat->n_row++;
1398 if (add_wraps(&wraps, &info[i], bound->el, set_j) < 0)
1399 goto error;
1400 if (!wraps.mat->n_row)
1401 goto unbounded;
1403 change = fuse(i, j, info, wraps.mat, detect_equalities, 0);
1405 if (0) {
1406 error: change = isl_change_error;
1408 unbounded:
1410 wraps_free(&wraps);
1411 isl_set_free(set_i);
1412 isl_set_free(set_j);
1413 isl_vec_free(bound);
1415 return change;
1418 /* Check if the union of the given pair of basic maps
1419 * can be represented by a single basic map.
1420 * If so, replace the pair by the single basic map and return
1421 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
1422 * Otherwise, return isl_change_none.
1423 * The two basic maps are assumed to live in the same local space.
1425 * We first check the effect of each constraint of one basic map
1426 * on the other basic map.
1427 * The constraint may be
1428 * redundant the constraint is redundant in its own
1429 * basic map and should be ignore and removed
1430 * in the end
1431 * valid all (integer) points of the other basic map
1432 * satisfy the constraint
1433 * separate no (integer) point of the other basic map
1434 * satisfies the constraint
1435 * cut some but not all points of the other basic map
1436 * satisfy the constraint
1437 * adj_eq the given constraint is adjacent (on the outside)
1438 * to an equality of the other basic map
1439 * adj_ineq the given constraint is adjacent (on the outside)
1440 * to an inequality of the other basic map
1442 * We consider seven cases in which we can replace the pair by a single
1443 * basic map. We ignore all "redundant" constraints.
1445 * 1. all constraints of one basic map are valid
1446 * => the other basic map is a subset and can be removed
1448 * 2. all constraints of both basic maps are either "valid" or "cut"
1449 * and the facets corresponding to the "cut" constraints
1450 * of one of the basic maps lies entirely inside the other basic map
1451 * => the pair can be replaced by a basic map consisting
1452 * of the valid constraints in both basic maps
1454 * 3. there is a single pair of adjacent inequalities
1455 * (all other constraints are "valid")
1456 * => the pair can be replaced by a basic map consisting
1457 * of the valid constraints in both basic maps
1459 * 4. one basic map has a single adjacent inequality, while the other
1460 * constraints are "valid". The other basic map has some
1461 * "cut" constraints, but replacing the adjacent inequality by
1462 * its opposite and adding the valid constraints of the other
1463 * basic map results in a subset of the other basic map
1464 * => the pair can be replaced by a basic map consisting
1465 * of the valid constraints in both basic maps
1467 * 5. there is a single adjacent pair of an inequality and an equality,
1468 * the other constraints of the basic map containing the inequality are
1469 * "valid". Moreover, if the inequality the basic map is relaxed
1470 * and then turned into an equality, then resulting facet lies
1471 * entirely inside the other basic map
1472 * => the pair can be replaced by the basic map containing
1473 * the inequality, with the inequality relaxed.
1475 * 6. there is a single adjacent pair of an inequality and an equality,
1476 * the other constraints of the basic map containing the inequality are
1477 * "valid". Moreover, the facets corresponding to both
1478 * the inequality and the equality can be wrapped around their
1479 * ridges to include the other basic map
1480 * => the pair can be replaced by a basic map consisting
1481 * of the valid constraints in both basic maps together
1482 * with all wrapping constraints
1484 * 7. one of the basic maps extends beyond the other by at most one.
1485 * Moreover, the facets corresponding to the cut constraints and
1486 * the pieces of the other basic map at offset one from these cut
1487 * constraints can be wrapped around their ridges to include
1488 * the union of the two basic maps
1489 * => the pair can be replaced by a basic map consisting
1490 * of the valid constraints in both basic maps together
1491 * with all wrapping constraints
1493 * 8. the two basic maps live in adjacent hyperplanes. In principle
1494 * such sets can always be combined through wrapping, but we impose
1495 * that there is only one such pair, to avoid overeager coalescing.
1497 * Throughout the computation, we maintain a collection of tableaus
1498 * corresponding to the basic maps. When the basic maps are dropped
1499 * or combined, the tableaus are modified accordingly.
1501 static enum isl_change coalesce_local_pair(int i, int j,
1502 struct isl_coalesce_info *info)
1504 enum isl_change change = isl_change_none;
1506 info[i].eq = info[i].ineq = NULL;
1507 info[j].eq = info[j].ineq = NULL;
1509 info[i].eq = eq_status_in(info[i].bmap, info[j].tab);
1510 if (info[i].bmap->n_eq && !info[i].eq)
1511 goto error;
1512 if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ERROR))
1513 goto error;
1514 if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_SEPARATE))
1515 goto done;
1517 info[j].eq = eq_status_in(info[j].bmap, info[i].tab);
1518 if (info[j].bmap->n_eq && !info[j].eq)
1519 goto error;
1520 if (any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_ERROR))
1521 goto error;
1522 if (any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_SEPARATE))
1523 goto done;
1525 info[i].ineq = ineq_status_in(info[i].bmap, info[i].tab, info[j].tab);
1526 if (info[i].bmap->n_ineq && !info[i].ineq)
1527 goto error;
1528 if (any(info[i].ineq, info[i].bmap->n_ineq, STATUS_ERROR))
1529 goto error;
1530 if (any(info[i].ineq, info[i].bmap->n_ineq, STATUS_SEPARATE))
1531 goto done;
1533 info[j].ineq = ineq_status_in(info[j].bmap, info[j].tab, info[i].tab);
1534 if (info[j].bmap->n_ineq && !info[j].ineq)
1535 goto error;
1536 if (any(info[j].ineq, info[j].bmap->n_ineq, STATUS_ERROR))
1537 goto error;
1538 if (any(info[j].ineq, info[j].bmap->n_ineq, STATUS_SEPARATE))
1539 goto done;
1541 if (all(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_VALID) &&
1542 all(info[i].ineq, info[i].bmap->n_ineq, STATUS_VALID)) {
1543 drop(&info[j]);
1544 change = isl_change_drop_second;
1545 } else if (all(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_VALID) &&
1546 all(info[j].ineq, info[j].bmap->n_ineq, STATUS_VALID)) {
1547 drop(&info[i]);
1548 change = isl_change_drop_first;
1549 } else if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ADJ_EQ)) {
1550 change = check_eq_adj_eq(i, j, info);
1551 } else if (any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_ADJ_EQ)) {
1552 change = check_eq_adj_eq(j, i, info);
1553 } else if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ADJ_INEQ) ||
1554 any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_ADJ_INEQ)) {
1555 change = check_adj_eq(i, j, info);
1556 } else if (any(info[i].ineq, info[i].bmap->n_ineq, STATUS_ADJ_EQ) ||
1557 any(info[j].ineq, info[j].bmap->n_ineq, STATUS_ADJ_EQ)) {
1558 /* Can't happen */
1559 /* BAD ADJ INEQ */
1560 } else if (any(info[i].ineq, info[i].bmap->n_ineq, STATUS_ADJ_INEQ) ||
1561 any(info[j].ineq, info[j].bmap->n_ineq, STATUS_ADJ_INEQ)) {
1562 change = check_adj_ineq(i, j, info);
1563 } else {
1564 if (!any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_CUT) &&
1565 !any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_CUT))
1566 change = check_facets(i, j, info);
1567 if (change == isl_change_none)
1568 change = check_wrap(i, j, info);
1571 done:
1572 free(info[i].eq);
1573 free(info[j].eq);
1574 free(info[i].ineq);
1575 free(info[j].ineq);
1576 return change;
1577 error:
1578 free(info[i].eq);
1579 free(info[j].eq);
1580 free(info[i].ineq);
1581 free(info[j].ineq);
1582 return isl_change_error;
1585 /* Do the two basic maps live in the same local space, i.e.,
1586 * do they have the same (known) divs?
1587 * If either basic map has any unknown divs, then we can only assume
1588 * that they do not live in the same local space.
1590 static int same_divs(__isl_keep isl_basic_map *bmap1,
1591 __isl_keep isl_basic_map *bmap2)
1593 int i;
1594 int known;
1595 int total;
1597 if (!bmap1 || !bmap2)
1598 return -1;
1599 if (bmap1->n_div != bmap2->n_div)
1600 return 0;
1602 if (bmap1->n_div == 0)
1603 return 1;
1605 known = isl_basic_map_divs_known(bmap1);
1606 if (known < 0 || !known)
1607 return known;
1608 known = isl_basic_map_divs_known(bmap2);
1609 if (known < 0 || !known)
1610 return known;
1612 total = isl_basic_map_total_dim(bmap1);
1613 for (i = 0; i < bmap1->n_div; ++i)
1614 if (!isl_seq_eq(bmap1->div[i], bmap2->div[i], 2 + total))
1615 return 0;
1617 return 1;
1620 /* Does "bmap" contain the basic map represented by the tableau "tab"
1621 * after expanding the divs of "bmap" to match those of "tab"?
1622 * The expansion is performed using the divs "div" and expansion "exp"
1623 * computed by the caller.
1624 * Then we check if all constraints of the expanded "bmap" are valid for "tab".
1626 static int contains_with_expanded_divs(__isl_keep isl_basic_map *bmap,
1627 struct isl_tab *tab, __isl_keep isl_mat *div, int *exp)
1629 int superset = 0;
1630 int *eq_i = NULL;
1631 int *ineq_i = NULL;
1633 bmap = isl_basic_map_copy(bmap);
1634 bmap = isl_basic_set_expand_divs(bmap, isl_mat_copy(div), exp);
1636 if (!bmap)
1637 goto error;
1639 eq_i = eq_status_in(bmap, tab);
1640 if (bmap->n_eq && !eq_i)
1641 goto error;
1642 if (any(eq_i, 2 * bmap->n_eq, STATUS_ERROR))
1643 goto error;
1644 if (any(eq_i, 2 * bmap->n_eq, STATUS_SEPARATE))
1645 goto done;
1647 ineq_i = ineq_status_in(bmap, NULL, tab);
1648 if (bmap->n_ineq && !ineq_i)
1649 goto error;
1650 if (any(ineq_i, bmap->n_ineq, STATUS_ERROR))
1651 goto error;
1652 if (any(ineq_i, bmap->n_ineq, STATUS_SEPARATE))
1653 goto done;
1655 if (all(eq_i, 2 * bmap->n_eq, STATUS_VALID) &&
1656 all(ineq_i, bmap->n_ineq, STATUS_VALID))
1657 superset = 1;
1659 done:
1660 isl_basic_map_free(bmap);
1661 free(eq_i);
1662 free(ineq_i);
1663 return superset;
1664 error:
1665 isl_basic_map_free(bmap);
1666 free(eq_i);
1667 free(ineq_i);
1668 return -1;
1671 /* Does "bmap_i" contain the basic map represented by "info_j"
1672 * after aligning the divs of "bmap_i" to those of "info_j".
1673 * Note that this can only succeed if the number of divs of "bmap_i"
1674 * is smaller than (or equal to) the number of divs of "info_j".
1676 * We first check if the divs of "bmap_i" are all known and form a subset
1677 * of those of "bmap_j". If so, we pass control over to
1678 * contains_with_expanded_divs.
1680 static int contains_after_aligning_divs(__isl_keep isl_basic_map *bmap_i,
1681 struct isl_coalesce_info *info_j)
1683 int known;
1684 isl_mat *div_i, *div_j, *div;
1685 int *exp1 = NULL;
1686 int *exp2 = NULL;
1687 isl_ctx *ctx;
1688 int subset;
1690 known = isl_basic_map_divs_known(bmap_i);
1691 if (known < 0 || !known)
1692 return known;
1694 ctx = isl_basic_map_get_ctx(bmap_i);
1696 div_i = isl_basic_map_get_divs(bmap_i);
1697 div_j = isl_basic_map_get_divs(info_j->bmap);
1699 if (!div_i || !div_j)
1700 goto error;
1702 exp1 = isl_alloc_array(ctx, int, div_i->n_row);
1703 exp2 = isl_alloc_array(ctx, int, div_j->n_row);
1704 if ((div_i->n_row && !exp1) || (div_j->n_row && !exp2))
1705 goto error;
1707 div = isl_merge_divs(div_i, div_j, exp1, exp2);
1708 if (!div)
1709 goto error;
1711 if (div->n_row == div_j->n_row)
1712 subset = contains_with_expanded_divs(bmap_i,
1713 info_j->tab, div, exp1);
1714 else
1715 subset = 0;
1717 isl_mat_free(div);
1719 isl_mat_free(div_i);
1720 isl_mat_free(div_j);
1722 free(exp2);
1723 free(exp1);
1725 return subset;
1726 error:
1727 isl_mat_free(div_i);
1728 isl_mat_free(div_j);
1729 free(exp1);
1730 free(exp2);
1731 return -1;
1734 /* Check if the basic map "j" is a subset of basic map "i",
1735 * if "i" has fewer divs that "j".
1736 * If so, remove basic map "j".
1738 * If the two basic maps have the same number of divs, then
1739 * they must necessarily be different. Otherwise, we would have
1740 * called coalesce_local_pair. We therefore don't try anything
1741 * in this case.
1743 static int coalesced_subset(int i, int j, struct isl_coalesce_info *info)
1745 int superset;
1747 if (info[i].bmap->n_div >= info[j].bmap->n_div)
1748 return 0;
1750 superset = contains_after_aligning_divs(info[i].bmap, &info[j]);
1751 if (superset < 0)
1752 return -1;
1753 if (superset)
1754 drop(&info[j]);
1756 return superset;
1759 /* Check if basic map "j" is a subset of basic map "i" after
1760 * exploiting the extra equalities of "j" to simplify the divs of "i".
1761 * If so, remove basic map "j".
1763 * If "j" does not have any equalities or if they are the same
1764 * as those of "i", then we cannot exploit them to simplify the divs.
1765 * Similarly, if there are no divs in "i", then they cannot be simplified.
1766 * If, on the other hand, the affine hulls of "i" and "j" do not intersect,
1767 * then "j" cannot be a subset of "i".
1769 * Otherwise, we intersect "i" with the affine hull of "j" and then
1770 * check if "j" is a subset of the result after aligning the divs.
1771 * If so, then "j" is definitely a subset of "i" and can be removed.
1772 * Note that if after intersection with the affine hull of "j".
1773 * "i" still has more divs than "j", then there is no way we can
1774 * align the divs of "i" to those of "j".
1776 static int coalesced_subset_with_equalities(int i, int j,
1777 struct isl_coalesce_info *info)
1779 isl_basic_map *hull_i, *hull_j, *bmap_i;
1780 int equal, empty, subset;
1782 if (info[j].bmap->n_eq == 0)
1783 return 0;
1784 if (info[i].bmap->n_div == 0)
1785 return 0;
1787 hull_i = isl_basic_map_copy(info[i].bmap);
1788 hull_i = isl_basic_map_plain_affine_hull(hull_i);
1789 hull_j = isl_basic_map_copy(info[j].bmap);
1790 hull_j = isl_basic_map_plain_affine_hull(hull_j);
1792 hull_j = isl_basic_map_intersect(hull_j, isl_basic_map_copy(hull_i));
1793 equal = isl_basic_map_plain_is_equal(hull_i, hull_j);
1794 empty = isl_basic_map_plain_is_empty(hull_j);
1795 isl_basic_map_free(hull_i);
1797 if (equal < 0 || equal || empty < 0 || empty) {
1798 isl_basic_map_free(hull_j);
1799 return equal < 0 || empty < 0 ? -1 : 0;
1802 bmap_i = isl_basic_map_copy(info[i].bmap);
1803 bmap_i = isl_basic_map_intersect(bmap_i, hull_j);
1804 if (!bmap_i)
1805 return -1;
1807 if (bmap_i->n_div > info[j].bmap->n_div) {
1808 isl_basic_map_free(bmap_i);
1809 return 0;
1812 subset = contains_after_aligning_divs(bmap_i, &info[j]);
1814 isl_basic_map_free(bmap_i);
1816 if (subset < 0)
1817 return -1;
1818 if (subset)
1819 drop(&info[j]);
1821 return subset;
1824 /* Check if one of the basic maps is a subset of the other and, if so,
1825 * drop the subset.
1826 * Note that we only perform any test if the number of divs is different
1827 * in the two basic maps. In case the number of divs is the same,
1828 * we have already established that the divs are different
1829 * in the two basic maps.
1830 * In particular, if the number of divs of basic map i is smaller than
1831 * the number of divs of basic map j, then we check if j is a subset of i
1832 * and vice versa.
1834 static enum isl_change check_coalesce_subset(int i, int j,
1835 struct isl_coalesce_info *info)
1837 int changed;
1839 changed = coalesced_subset(i, j, info);
1840 if (changed < 0 || changed)
1841 return changed < 0 ? isl_change_error : isl_change_drop_second;
1843 changed = coalesced_subset(j, i, info);
1844 if (changed < 0 || changed)
1845 return changed < 0 ? isl_change_error : isl_change_drop_first;
1847 changed = coalesced_subset_with_equalities(i, j, info);
1848 if (changed < 0 || changed)
1849 return changed < 0 ? isl_change_error : isl_change_drop_second;
1851 changed = coalesced_subset_with_equalities(j, i, info);
1852 if (changed < 0 || changed)
1853 return changed < 0 ? isl_change_error : isl_change_drop_first;
1855 return isl_change_none;
1858 /* Check if the union of the given pair of basic maps
1859 * can be represented by a single basic map.
1860 * If so, replace the pair by the single basic map and return
1861 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
1862 * Otherwise, return isl_change_none.
1864 * We first check if the two basic maps live in the same local space.
1865 * If so, we do the complete check. Otherwise, we check if one is
1866 * an obvious subset of the other.
1868 static enum isl_change coalesce_pair(int i, int j,
1869 struct isl_coalesce_info *info)
1871 int same;
1873 same = same_divs(info[i].bmap, info[j].bmap);
1874 if (same < 0)
1875 return isl_change_error;
1876 if (same)
1877 return coalesce_local_pair(i, j, info);
1879 return check_coalesce_subset(i, j, info);
1882 /* Pairwise coalesce the basic maps described by the "n" elements of "info",
1883 * skipping basic maps that have been removed (either before or within
1884 * this function).
1886 * For each basic map i, we check if it can be coalesced with respect
1887 * to any previously considered basic map j.
1888 * If i gets dropped (because it was a subset of some j), then
1889 * we can move on to the next basic map.
1890 * If j gets dropped, we need to continue checking against the other
1891 * previously considered basic maps.
1892 * If the two basic maps got fused, then we recheck the fused basic map
1893 * against the previously considered basic maps.
1895 static int coalesce(isl_ctx *ctx, int n, struct isl_coalesce_info *info)
1897 int i, j;
1899 for (i = n - 2; i >= 0; --i) {
1900 if (info[i].removed)
1901 continue;
1902 for (j = i + 1; j < n; ++j) {
1903 enum isl_change changed;
1905 if (info[j].removed)
1906 continue;
1907 if (info[i].removed)
1908 isl_die(ctx, isl_error_internal,
1909 "basic map unexpectedly removed",
1910 return -1);
1911 changed = coalesce_pair(i, j, info);
1912 switch (changed) {
1913 case isl_change_error:
1914 return -1;
1915 case isl_change_none:
1916 case isl_change_drop_second:
1917 continue;
1918 case isl_change_drop_first:
1919 j = n;
1920 break;
1921 case isl_change_fuse:
1922 j = i;
1923 break;
1928 return 0;
1931 /* Update the basic maps in "map" based on the information in "info".
1932 * In particular, remove the basic maps that have been marked removed and
1933 * update the others based on the information in the corresponding tableau.
1934 * Since we detected implicit equalities without calling
1935 * isl_basic_map_gauss, we need to do it now.
1937 static __isl_give isl_map *update_basic_maps(__isl_take isl_map *map,
1938 int n, struct isl_coalesce_info *info)
1940 int i;
1942 if (!map)
1943 return NULL;
1945 for (i = n - 1; i >= 0; --i) {
1946 if (info[i].removed) {
1947 isl_basic_map_free(map->p[i]);
1948 if (i != map->n - 1)
1949 map->p[i] = map->p[map->n - 1];
1950 map->n--;
1951 continue;
1954 info[i].bmap = isl_basic_map_update_from_tab(info[i].bmap,
1955 info[i].tab);
1956 info[i].bmap = isl_basic_map_gauss(info[i].bmap, NULL);
1957 info[i].bmap = isl_basic_map_finalize(info[i].bmap);
1958 if (!info[i].bmap)
1959 return isl_map_free(map);
1960 ISL_F_SET(info[i].bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1961 ISL_F_SET(info[i].bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1962 isl_basic_map_free(map->p[i]);
1963 map->p[i] = info[i].bmap;
1964 info[i].bmap = NULL;
1967 return map;
1970 /* For each pair of basic maps in the map, check if the union of the two
1971 * can be represented by a single basic map.
1972 * If so, replace the pair by the single basic map and start over.
1974 * Since we are constructing the tableaus of the basic maps anyway,
1975 * we exploit them to detect implicit equalities and redundant constraints.
1976 * This also helps the coalescing as it can ignore the redundant constraints.
1977 * In order to avoid confusion, we make all implicit equalities explicit
1978 * in the basic maps. We don't call isl_basic_map_gauss, though,
1979 * as that may affect the number of constraints.
1980 * This means that we have to call isl_basic_map_gauss at the end
1981 * of the computation (in update_basic_maps) to ensure that
1982 * the basic maps are not left in an unexpected state.
1984 struct isl_map *isl_map_coalesce(struct isl_map *map)
1986 int i;
1987 unsigned n;
1988 isl_ctx *ctx;
1989 struct isl_coalesce_info *info = NULL;
1991 map = isl_map_remove_empty_parts(map);
1992 if (!map)
1993 return NULL;
1995 if (map->n <= 1)
1996 return map;
1998 ctx = isl_map_get_ctx(map);
1999 map = isl_map_sort_divs(map);
2000 map = isl_map_cow(map);
2002 if (!map)
2003 return NULL;
2005 n = map->n;
2007 info = isl_calloc_array(map->ctx, struct isl_coalesce_info, n);
2008 if (!info)
2009 goto error;
2011 for (i = 0; i < map->n; ++i) {
2012 info[i].bmap = isl_basic_map_copy(map->p[i]);
2013 info[i].tab = isl_tab_from_basic_map(info[i].bmap, 0);
2014 if (!info[i].tab)
2015 goto error;
2016 if (!ISL_F_ISSET(info[i].bmap, ISL_BASIC_MAP_NO_IMPLICIT))
2017 if (isl_tab_detect_implicit_equalities(info[i].tab) < 0)
2018 goto error;
2019 info[i].bmap = isl_tab_make_equalities_explicit(info[i].tab,
2020 info[i].bmap);
2021 if (!info[i].bmap)
2022 goto error;
2023 if (!ISL_F_ISSET(info[i].bmap, ISL_BASIC_MAP_NO_REDUNDANT))
2024 if (isl_tab_detect_redundant(info[i].tab) < 0)
2025 goto error;
2027 for (i = map->n - 1; i >= 0; --i)
2028 if (info[i].tab->empty)
2029 drop(&info[i]);
2031 if (coalesce(ctx, n, info) < 0)
2032 goto error;
2034 map = update_basic_maps(map, n, info);
2036 clear_coalesce_info(n, info);
2038 return map;
2039 error:
2040 clear_coalesce_info(n, info);
2041 isl_map_free(map);
2042 return NULL;
2045 /* For each pair of basic sets in the set, check if the union of the two
2046 * can be represented by a single basic set.
2047 * If so, replace the pair by the single basic set and start over.
2049 struct isl_set *isl_set_coalesce(struct isl_set *set)
2051 return (struct isl_set *)isl_map_coalesce((struct isl_map *)set);