isl_map_coalesce: allow general coalescing with expanded divs
[isl.git] / isl_coalesce.c
blob86514fd369c2c5f5c367da330541eb623c9e837c
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
6 * Copyright 2016 Sven Verdoolaege
8 * Use of this software is governed by the MIT license
10 * Written by Sven Verdoolaege, K.U.Leuven, Departement
11 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
12 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
13 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
14 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
15 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
16 * B.P. 105 - 78153 Le Chesnay, France
19 #include "isl_map_private.h"
20 #include <isl_seq.h>
21 #include <isl/options.h>
22 #include "isl_tab.h"
23 #include <isl_mat_private.h>
24 #include <isl_local_space_private.h>
25 #include <isl_vec_private.h>
26 #include <isl_aff_private.h>
28 #define STATUS_ERROR -1
29 #define STATUS_REDUNDANT 1
30 #define STATUS_VALID 2
31 #define STATUS_SEPARATE 3
32 #define STATUS_CUT 4
33 #define STATUS_ADJ_EQ 5
34 #define STATUS_ADJ_INEQ 6
36 static int status_in(isl_int *ineq, struct isl_tab *tab)
38 enum isl_ineq_type type = isl_tab_ineq_type(tab, ineq);
39 switch (type) {
40 default:
41 case isl_ineq_error: return STATUS_ERROR;
42 case isl_ineq_redundant: return STATUS_VALID;
43 case isl_ineq_separate: return STATUS_SEPARATE;
44 case isl_ineq_cut: return STATUS_CUT;
45 case isl_ineq_adj_eq: return STATUS_ADJ_EQ;
46 case isl_ineq_adj_ineq: return STATUS_ADJ_INEQ;
50 /* Compute the position of the equalities of basic map "bmap_i"
51 * with respect to the basic map represented by "tab_j".
52 * The resulting array has twice as many entries as the number
53 * of equalities corresponding to the two inequalties to which
54 * each equality corresponds.
56 static int *eq_status_in(__isl_keep isl_basic_map *bmap_i,
57 struct isl_tab *tab_j)
59 int k, l;
60 int *eq = isl_calloc_array(bmap_i->ctx, int, 2 * bmap_i->n_eq);
61 unsigned dim;
63 if (!eq)
64 return NULL;
66 dim = isl_basic_map_total_dim(bmap_i);
67 for (k = 0; k < bmap_i->n_eq; ++k) {
68 for (l = 0; l < 2; ++l) {
69 isl_seq_neg(bmap_i->eq[k], bmap_i->eq[k], 1+dim);
70 eq[2 * k + l] = status_in(bmap_i->eq[k], tab_j);
71 if (eq[2 * k + l] == STATUS_ERROR)
72 goto error;
74 if (eq[2 * k] == STATUS_SEPARATE ||
75 eq[2 * k + 1] == STATUS_SEPARATE)
76 break;
79 return eq;
80 error:
81 free(eq);
82 return NULL;
85 /* Compute the position of the inequalities of basic map "bmap_i"
86 * (also represented by "tab_i", if not NULL) with respect to the basic map
87 * represented by "tab_j".
89 static int *ineq_status_in(__isl_keep isl_basic_map *bmap_i,
90 struct isl_tab *tab_i, struct isl_tab *tab_j)
92 int k;
93 unsigned n_eq = bmap_i->n_eq;
94 int *ineq = isl_calloc_array(bmap_i->ctx, int, bmap_i->n_ineq);
96 if (!ineq)
97 return NULL;
99 for (k = 0; k < bmap_i->n_ineq; ++k) {
100 if (tab_i && isl_tab_is_redundant(tab_i, n_eq + k)) {
101 ineq[k] = STATUS_REDUNDANT;
102 continue;
104 ineq[k] = status_in(bmap_i->ineq[k], tab_j);
105 if (ineq[k] == STATUS_ERROR)
106 goto error;
107 if (ineq[k] == STATUS_SEPARATE)
108 break;
111 return ineq;
112 error:
113 free(ineq);
114 return NULL;
117 static int any(int *con, unsigned len, int status)
119 int i;
121 for (i = 0; i < len ; ++i)
122 if (con[i] == status)
123 return 1;
124 return 0;
127 static int count(int *con, unsigned len, int status)
129 int i;
130 int c = 0;
132 for (i = 0; i < len ; ++i)
133 if (con[i] == status)
134 c++;
135 return c;
138 static int all(int *con, unsigned len, int status)
140 int i;
142 for (i = 0; i < len ; ++i) {
143 if (con[i] == STATUS_REDUNDANT)
144 continue;
145 if (con[i] != status)
146 return 0;
148 return 1;
151 /* Internal information associated to a basic map in a map
152 * that is to be coalesced by isl_map_coalesce.
154 * "bmap" is the basic map itself (or NULL if "removed" is set)
155 * "tab" is the corresponding tableau (or NULL if "removed" is set)
156 * "hull_hash" identifies the affine space in which "bmap" lives.
157 * "removed" is set if this basic map has been removed from the map
158 * "simplify" is set if this basic map may have some unknown integer
159 * divisions that were not present in the input basic maps. The basic
160 * map should then be simplified such that we may be able to find
161 * a definition among the constraints.
163 * "eq" and "ineq" are only set if we are currently trying to coalesce
164 * this basic map with another basic map, in which case they represent
165 * the position of the inequalities of this basic map with respect to
166 * the other basic map. The number of elements in the "eq" array
167 * is twice the number of equalities in the "bmap", corresponding
168 * to the two inequalities that make up each equality.
170 struct isl_coalesce_info {
171 isl_basic_map *bmap;
172 struct isl_tab *tab;
173 uint32_t hull_hash;
174 int removed;
175 int simplify;
176 int *eq;
177 int *ineq;
180 /* Compute the hash of the (apparent) affine hull of info->bmap (with
181 * the existentially quantified variables removed) and store it
182 * in info->hash.
184 static int coalesce_info_set_hull_hash(struct isl_coalesce_info *info)
186 isl_basic_map *hull;
187 unsigned n_div;
189 hull = isl_basic_map_copy(info->bmap);
190 hull = isl_basic_map_plain_affine_hull(hull);
191 n_div = isl_basic_map_dim(hull, isl_dim_div);
192 hull = isl_basic_map_drop_constraints_involving_dims(hull,
193 isl_dim_div, 0, n_div);
194 info->hull_hash = isl_basic_map_get_hash(hull);
195 isl_basic_map_free(hull);
197 return hull ? 0 : -1;
200 /* Free all the allocated memory in an array
201 * of "n" isl_coalesce_info elements.
203 static void clear_coalesce_info(int n, struct isl_coalesce_info *info)
205 int i;
207 if (!info)
208 return;
210 for (i = 0; i < n; ++i) {
211 isl_basic_map_free(info[i].bmap);
212 isl_tab_free(info[i].tab);
215 free(info);
218 /* Drop the basic map represented by "info".
219 * That is, clear the memory associated to the entry and
220 * mark it as having been removed.
222 static void drop(struct isl_coalesce_info *info)
224 info->bmap = isl_basic_map_free(info->bmap);
225 isl_tab_free(info->tab);
226 info->tab = NULL;
227 info->removed = 1;
230 /* Exchange the information in "info1" with that in "info2".
232 static void exchange(struct isl_coalesce_info *info1,
233 struct isl_coalesce_info *info2)
235 struct isl_coalesce_info info;
237 info = *info1;
238 *info1 = *info2;
239 *info2 = info;
242 /* This type represents the kind of change that has been performed
243 * while trying to coalesce two basic maps.
245 * isl_change_none: nothing was changed
246 * isl_change_drop_first: the first basic map was removed
247 * isl_change_drop_second: the second basic map was removed
248 * isl_change_fuse: the two basic maps were replaced by a new basic map.
250 enum isl_change {
251 isl_change_error = -1,
252 isl_change_none = 0,
253 isl_change_drop_first,
254 isl_change_drop_second,
255 isl_change_fuse,
258 /* Update "change" based on an interchange of the first and the second
259 * basic map. That is, interchange isl_change_drop_first and
260 * isl_change_drop_second.
262 static enum isl_change invert_change(enum isl_change change)
264 switch (change) {
265 case isl_change_error:
266 return isl_change_error;
267 case isl_change_none:
268 return isl_change_none;
269 case isl_change_drop_first:
270 return isl_change_drop_second;
271 case isl_change_drop_second:
272 return isl_change_drop_first;
273 case isl_change_fuse:
274 return isl_change_fuse;
277 return isl_change_error;
280 /* Add the valid constraints of the basic map represented by "info"
281 * to "bmap". "len" is the size of the constraints.
282 * If only one of the pair of inequalities that make up an equality
283 * is valid, then add that inequality.
285 static __isl_give isl_basic_map *add_valid_constraints(
286 __isl_take isl_basic_map *bmap, struct isl_coalesce_info *info,
287 unsigned len)
289 int k, l;
291 if (!bmap)
292 return NULL;
294 for (k = 0; k < info->bmap->n_eq; ++k) {
295 if (info->eq[2 * k] == STATUS_VALID &&
296 info->eq[2 * k + 1] == STATUS_VALID) {
297 l = isl_basic_map_alloc_equality(bmap);
298 if (l < 0)
299 return isl_basic_map_free(bmap);
300 isl_seq_cpy(bmap->eq[l], info->bmap->eq[k], len);
301 } else if (info->eq[2 * k] == STATUS_VALID) {
302 l = isl_basic_map_alloc_inequality(bmap);
303 if (l < 0)
304 return isl_basic_map_free(bmap);
305 isl_seq_neg(bmap->ineq[l], info->bmap->eq[k], len);
306 } else if (info->eq[2 * k + 1] == STATUS_VALID) {
307 l = isl_basic_map_alloc_inequality(bmap);
308 if (l < 0)
309 return isl_basic_map_free(bmap);
310 isl_seq_cpy(bmap->ineq[l], info->bmap->eq[k], len);
314 for (k = 0; k < info->bmap->n_ineq; ++k) {
315 if (info->ineq[k] != STATUS_VALID)
316 continue;
317 l = isl_basic_map_alloc_inequality(bmap);
318 if (l < 0)
319 return isl_basic_map_free(bmap);
320 isl_seq_cpy(bmap->ineq[l], info->bmap->ineq[k], len);
323 return bmap;
326 /* Is "bmap" defined by a number of (non-redundant) constraints that
327 * is greater than the number of constraints of basic maps i and j combined?
328 * Equalities are counted as two inequalities.
330 static int number_of_constraints_increases(int i, int j,
331 struct isl_coalesce_info *info,
332 __isl_keep isl_basic_map *bmap, struct isl_tab *tab)
334 int k, n_old, n_new;
336 n_old = 2 * info[i].bmap->n_eq + info[i].bmap->n_ineq;
337 n_old += 2 * info[j].bmap->n_eq + info[j].bmap->n_ineq;
339 n_new = 2 * bmap->n_eq;
340 for (k = 0; k < bmap->n_ineq; ++k)
341 if (!isl_tab_is_redundant(tab, bmap->n_eq + k))
342 ++n_new;
344 return n_new > n_old;
347 /* Replace the pair of basic maps i and j by the basic map bounded
348 * by the valid constraints in both basic maps and the constraints
349 * in extra (if not NULL).
350 * Place the fused basic map in the position that is the smallest of i and j.
352 * If "detect_equalities" is set, then look for equalities encoded
353 * as pairs of inequalities.
354 * If "check_number" is set, then the original basic maps are only
355 * replaced if the total number of constraints does not increase.
356 * While the number of integer divisions in the two basic maps
357 * is assumed to be the same, the actual definitions may be different.
358 * We only copy the definition from one of the basic map if it is
359 * the same as that of the other basic map. Otherwise, we mark
360 * the integer division as unknown and schedule for the basic map
361 * to be simplified in an attempt to recover the integer division definition.
363 static enum isl_change fuse(int i, int j, struct isl_coalesce_info *info,
364 __isl_keep isl_mat *extra, int detect_equalities, int check_number)
366 int k, l;
367 struct isl_basic_map *fused = NULL;
368 struct isl_tab *fused_tab = NULL;
369 unsigned total = isl_basic_map_total_dim(info[i].bmap);
370 unsigned extra_rows = extra ? extra->n_row : 0;
371 unsigned n_eq, n_ineq;
373 if (j < i)
374 return fuse(j, i, info, extra, detect_equalities, check_number);
376 n_eq = info[i].bmap->n_eq + info[j].bmap->n_eq;
377 n_ineq = info[i].bmap->n_ineq + info[j].bmap->n_ineq;
378 fused = isl_basic_map_alloc_space(isl_space_copy(info[i].bmap->dim),
379 info[i].bmap->n_div, n_eq, n_eq + n_ineq + extra_rows);
380 fused = add_valid_constraints(fused, &info[i], 1 + total);
381 fused = add_valid_constraints(fused, &info[j], 1 + total);
382 if (!fused)
383 goto error;
385 for (k = 0; k < info[i].bmap->n_div; ++k) {
386 int l = isl_basic_map_alloc_div(fused);
387 if (l < 0)
388 goto error;
389 if (isl_seq_eq(info[i].bmap->div[k], info[j].bmap->div[k],
390 1 + 1 + total)) {
391 isl_seq_cpy(fused->div[l], info[i].bmap->div[k],
392 1 + 1 + total);
393 } else {
394 isl_int_set_si(fused->div[l][0], 0);
395 info[i].simplify = 1;
399 for (k = 0; k < extra_rows; ++k) {
400 l = isl_basic_map_alloc_inequality(fused);
401 if (l < 0)
402 goto error;
403 isl_seq_cpy(fused->ineq[l], extra->row[k], 1 + total);
406 if (detect_equalities)
407 fused = isl_basic_map_detect_inequality_pairs(fused, NULL);
408 fused = isl_basic_map_gauss(fused, NULL);
409 ISL_F_SET(fused, ISL_BASIC_MAP_FINAL);
410 if (ISL_F_ISSET(info[i].bmap, ISL_BASIC_MAP_RATIONAL) &&
411 ISL_F_ISSET(info[j].bmap, ISL_BASIC_MAP_RATIONAL))
412 ISL_F_SET(fused, ISL_BASIC_MAP_RATIONAL);
414 fused_tab = isl_tab_from_basic_map(fused, 0);
415 if (isl_tab_detect_redundant(fused_tab) < 0)
416 goto error;
418 if (check_number &&
419 number_of_constraints_increases(i, j, info, fused, fused_tab)) {
420 isl_tab_free(fused_tab);
421 isl_basic_map_free(fused);
422 return isl_change_none;
425 info[i].simplify |= info[j].simplify;
426 isl_basic_map_free(info[i].bmap);
427 info[i].bmap = fused;
428 isl_tab_free(info[i].tab);
429 info[i].tab = fused_tab;
430 drop(&info[j]);
432 return isl_change_fuse;
433 error:
434 isl_tab_free(fused_tab);
435 isl_basic_map_free(fused);
436 return isl_change_error;
439 /* Given a pair of basic maps i and j such that all constraints are either
440 * "valid" or "cut", check if the facets corresponding to the "cut"
441 * constraints of i lie entirely within basic map j.
442 * If so, replace the pair by the basic map consisting of the valid
443 * constraints in both basic maps.
444 * Checking whether the facet lies entirely within basic map j
445 * is performed by checking whether the constraints of basic map j
446 * are valid for the facet. These tests are performed on a rational
447 * tableau to avoid the theoretical possibility that a constraint
448 * that was considered to be a cut constraint for the entire basic map i
449 * happens to be considered to be a valid constraint for the facet,
450 * even though it cuts off the same rational points.
452 * To see that we are not introducing any extra points, call the
453 * two basic maps A and B and the resulting map U and let x
454 * be an element of U \setminus ( A \cup B ).
455 * A line connecting x with an element of A \cup B meets a facet F
456 * of either A or B. Assume it is a facet of B and let c_1 be
457 * the corresponding facet constraint. We have c_1(x) < 0 and
458 * so c_1 is a cut constraint. This implies that there is some
459 * (possibly rational) point x' satisfying the constraints of A
460 * and the opposite of c_1 as otherwise c_1 would have been marked
461 * valid for A. The line connecting x and x' meets a facet of A
462 * in a (possibly rational) point that also violates c_1, but this
463 * is impossible since all cut constraints of B are valid for all
464 * cut facets of A.
465 * In case F is a facet of A rather than B, then we can apply the
466 * above reasoning to find a facet of B separating x from A \cup B first.
468 static enum isl_change check_facets(int i, int j,
469 struct isl_coalesce_info *info)
471 int k, l;
472 struct isl_tab_undo *snap, *snap2;
473 unsigned n_eq = info[i].bmap->n_eq;
475 snap = isl_tab_snap(info[i].tab);
476 if (isl_tab_mark_rational(info[i].tab) < 0)
477 return isl_change_error;
478 snap2 = isl_tab_snap(info[i].tab);
480 for (k = 0; k < info[i].bmap->n_ineq; ++k) {
481 if (info[i].ineq[k] != STATUS_CUT)
482 continue;
483 if (isl_tab_select_facet(info[i].tab, n_eq + k) < 0)
484 return isl_change_error;
485 for (l = 0; l < info[j].bmap->n_ineq; ++l) {
486 int stat;
487 if (info[j].ineq[l] != STATUS_CUT)
488 continue;
489 stat = status_in(info[j].bmap->ineq[l], info[i].tab);
490 if (stat < 0)
491 return isl_change_error;
492 if (stat != STATUS_VALID)
493 break;
495 if (isl_tab_rollback(info[i].tab, snap2) < 0)
496 return isl_change_error;
497 if (l < info[j].bmap->n_ineq)
498 break;
501 if (k < info[i].bmap->n_ineq) {
502 if (isl_tab_rollback(info[i].tab, snap) < 0)
503 return isl_change_error;
504 return isl_change_none;
506 return fuse(i, j, info, NULL, 0, 0);
509 /* Check if info->bmap contains the basic map represented
510 * by the tableau "tab".
511 * For each equality, we check both the constraint itself
512 * (as an inequality) and its negation. Make sure the
513 * equality is returned to its original state before returning.
515 static int contains(struct isl_coalesce_info *info, struct isl_tab *tab)
517 int k;
518 unsigned dim;
519 isl_basic_map *bmap = info->bmap;
521 dim = isl_basic_map_total_dim(bmap);
522 for (k = 0; k < bmap->n_eq; ++k) {
523 int stat;
524 isl_seq_neg(bmap->eq[k], bmap->eq[k], 1 + dim);
525 stat = status_in(bmap->eq[k], tab);
526 isl_seq_neg(bmap->eq[k], bmap->eq[k], 1 + dim);
527 if (stat < 0)
528 return -1;
529 if (stat != STATUS_VALID)
530 return 0;
531 stat = status_in(bmap->eq[k], tab);
532 if (stat < 0)
533 return -1;
534 if (stat != STATUS_VALID)
535 return 0;
538 for (k = 0; k < bmap->n_ineq; ++k) {
539 int stat;
540 if (info->ineq[k] == STATUS_REDUNDANT)
541 continue;
542 stat = status_in(bmap->ineq[k], tab);
543 if (stat < 0)
544 return -1;
545 if (stat != STATUS_VALID)
546 return 0;
548 return 1;
551 /* Basic map "i" has an inequality (say "k") that is adjacent
552 * to some inequality of basic map "j". All the other inequalities
553 * are valid for "j".
554 * Check if basic map "j" forms an extension of basic map "i".
556 * Note that this function is only called if some of the equalities or
557 * inequalities of basic map "j" do cut basic map "i". The function is
558 * correct even if there are no such cut constraints, but in that case
559 * the additional checks performed by this function are overkill.
561 * In particular, we replace constraint k, say f >= 0, by constraint
562 * f <= -1, add the inequalities of "j" that are valid for "i"
563 * and check if the result is a subset of basic map "j".
564 * If so, then we know that this result is exactly equal to basic map "j"
565 * since all its constraints are valid for basic map "j".
566 * By combining the valid constraints of "i" (all equalities and all
567 * inequalities except "k") and the valid constraints of "j" we therefore
568 * obtain a basic map that is equal to their union.
569 * In this case, there is no need to perform a rollback of the tableau
570 * since it is going to be destroyed in fuse().
573 * |\__ |\__
574 * | \__ | \__
575 * | \_ => | \__
576 * |_______| _ |_________\
579 * |\ |\
580 * | \ | \
581 * | \ | \
582 * | | | \
583 * | ||\ => | \
584 * | || \ | \
585 * | || | | |
586 * |__||_/ |_____/
588 static enum isl_change is_adj_ineq_extension(int i, int j,
589 struct isl_coalesce_info *info)
591 int k;
592 struct isl_tab_undo *snap;
593 unsigned n_eq = info[i].bmap->n_eq;
594 unsigned total = isl_basic_map_total_dim(info[i].bmap);
595 int r;
596 int super;
598 if (isl_tab_extend_cons(info[i].tab, 1 + info[j].bmap->n_ineq) < 0)
599 return isl_change_error;
601 for (k = 0; k < info[i].bmap->n_ineq; ++k)
602 if (info[i].ineq[k] == STATUS_ADJ_INEQ)
603 break;
604 if (k >= info[i].bmap->n_ineq)
605 isl_die(isl_basic_map_get_ctx(info[i].bmap), isl_error_internal,
606 "info[i].ineq should have exactly one STATUS_ADJ_INEQ",
607 return isl_change_error);
609 snap = isl_tab_snap(info[i].tab);
611 if (isl_tab_unrestrict(info[i].tab, n_eq + k) < 0)
612 return isl_change_error;
614 isl_seq_neg(info[i].bmap->ineq[k], info[i].bmap->ineq[k], 1 + total);
615 isl_int_sub_ui(info[i].bmap->ineq[k][0], info[i].bmap->ineq[k][0], 1);
616 r = isl_tab_add_ineq(info[i].tab, info[i].bmap->ineq[k]);
617 isl_seq_neg(info[i].bmap->ineq[k], info[i].bmap->ineq[k], 1 + total);
618 isl_int_sub_ui(info[i].bmap->ineq[k][0], info[i].bmap->ineq[k][0], 1);
619 if (r < 0)
620 return isl_change_error;
622 for (k = 0; k < info[j].bmap->n_ineq; ++k) {
623 if (info[j].ineq[k] != STATUS_VALID)
624 continue;
625 if (isl_tab_add_ineq(info[i].tab, info[j].bmap->ineq[k]) < 0)
626 return isl_change_error;
629 super = contains(&info[j], info[i].tab);
630 if (super < 0)
631 return isl_change_error;
632 if (super)
633 return fuse(i, j, info, NULL, 0, 0);
635 if (isl_tab_rollback(info[i].tab, snap) < 0)
636 return isl_change_error;
638 return isl_change_none;
642 /* Both basic maps have at least one inequality with and adjacent
643 * (but opposite) inequality in the other basic map.
644 * Check that there are no cut constraints and that there is only
645 * a single pair of adjacent inequalities.
646 * If so, we can replace the pair by a single basic map described
647 * by all but the pair of adjacent inequalities.
648 * Any additional points introduced lie strictly between the two
649 * adjacent hyperplanes and can therefore be integral.
651 * ____ _____
652 * / ||\ / \
653 * / || \ / \
654 * \ || \ => \ \
655 * \ || / \ /
656 * \___||_/ \_____/
658 * The test for a single pair of adjancent inequalities is important
659 * for avoiding the combination of two basic maps like the following
661 * /|
662 * / |
663 * /__|
664 * _____
665 * | |
666 * | |
667 * |___|
669 * If there are some cut constraints on one side, then we may
670 * still be able to fuse the two basic maps, but we need to perform
671 * some additional checks in is_adj_ineq_extension.
673 static enum isl_change check_adj_ineq(int i, int j,
674 struct isl_coalesce_info *info)
676 int count_i, count_j;
677 int cut_i, cut_j;
679 count_i = count(info[i].ineq, info[i].bmap->n_ineq, STATUS_ADJ_INEQ);
680 count_j = count(info[j].ineq, info[j].bmap->n_ineq, STATUS_ADJ_INEQ);
682 if (count_i != 1 && count_j != 1)
683 return isl_change_none;
685 cut_i = any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_CUT) ||
686 any(info[i].ineq, info[i].bmap->n_ineq, STATUS_CUT);
687 cut_j = any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_CUT) ||
688 any(info[j].ineq, info[j].bmap->n_ineq, STATUS_CUT);
690 if (!cut_i && !cut_j && count_i == 1 && count_j == 1)
691 return fuse(i, j, info, NULL, 0, 0);
693 if (count_i == 1 && !cut_i)
694 return is_adj_ineq_extension(i, j, info);
696 if (count_j == 1 && !cut_j)
697 return is_adj_ineq_extension(j, i, info);
699 return isl_change_none;
702 /* Basic map "i" has an inequality "k" that is adjacent to some equality
703 * of basic map "j". All the other inequalities are valid for "j".
704 * Check if basic map "j" forms an extension of basic map "i".
706 * In particular, we relax constraint "k", compute the corresponding
707 * facet and check whether it is included in the other basic map.
708 * If so, we know that relaxing the constraint extends the basic
709 * map with exactly the other basic map (we already know that this
710 * other basic map is included in the extension, because there
711 * were no "cut" inequalities in "i") and we can replace the
712 * two basic maps by this extension.
713 * Each integer division that does not have exactly the same
714 * definition in "i" and "j" is marked unknown and the basic map
715 * is scheduled to be simplified in an attempt to recover
716 * the integer division definition.
717 * Place this extension in the position that is the smallest of i and j.
718 * ____ _____
719 * / || / |
720 * / || / |
721 * \ || => \ |
722 * \ || \ |
723 * \___|| \____|
725 static enum isl_change is_adj_eq_extension(int i, int j, int k,
726 struct isl_coalesce_info *info)
728 int change = isl_change_none;
729 int super;
730 struct isl_tab_undo *snap, *snap2;
731 unsigned n_eq = info[i].bmap->n_eq;
733 if (isl_tab_is_equality(info[i].tab, n_eq + k))
734 return isl_change_none;
736 snap = isl_tab_snap(info[i].tab);
737 if (isl_tab_relax(info[i].tab, n_eq + k) < 0)
738 return isl_change_error;
739 snap2 = isl_tab_snap(info[i].tab);
740 if (isl_tab_select_facet(info[i].tab, n_eq + k) < 0)
741 return isl_change_error;
742 super = contains(&info[j], info[i].tab);
743 if (super < 0)
744 return isl_change_error;
745 if (super) {
746 int l;
747 unsigned total;
749 if (isl_tab_rollback(info[i].tab, snap2) < 0)
750 return isl_change_error;
751 info[i].bmap = isl_basic_map_cow(info[i].bmap);
752 if (!info[i].bmap)
753 return isl_change_error;
754 total = isl_basic_map_total_dim(info[i].bmap);
755 for (l = 0; l < info[i].bmap->n_div; ++l)
756 if (!isl_seq_eq(info[i].bmap->div[l],
757 info[j].bmap->div[l], 1 + 1 + total)) {
758 isl_int_set_si(info[i].bmap->div[l][0], 0);
759 info[i].simplify = 1;
761 isl_int_add_ui(info[i].bmap->ineq[k][0],
762 info[i].bmap->ineq[k][0], 1);
763 ISL_F_SET(info[i].bmap, ISL_BASIC_MAP_FINAL);
764 drop(&info[j]);
765 if (j < i)
766 exchange(&info[i], &info[j]);
767 change = isl_change_fuse;
768 } else
769 if (isl_tab_rollback(info[i].tab, snap) < 0)
770 return isl_change_error;
772 return change;
775 /* Data structure that keeps track of the wrapping constraints
776 * and of information to bound the coefficients of those constraints.
778 * bound is set if we want to apply a bound on the coefficients
779 * mat contains the wrapping constraints
780 * max is the bound on the coefficients (if bound is set)
782 struct isl_wraps {
783 int bound;
784 isl_mat *mat;
785 isl_int max;
788 /* Update wraps->max to be greater than or equal to the coefficients
789 * in the equalities and inequalities of info->bmap that can be removed
790 * if we end up applying wrapping.
792 static void wraps_update_max(struct isl_wraps *wraps,
793 struct isl_coalesce_info *info)
795 int k;
796 isl_int max_k;
797 unsigned total = isl_basic_map_total_dim(info->bmap);
799 isl_int_init(max_k);
801 for (k = 0; k < info->bmap->n_eq; ++k) {
802 if (info->eq[2 * k] == STATUS_VALID &&
803 info->eq[2 * k + 1] == STATUS_VALID)
804 continue;
805 isl_seq_abs_max(info->bmap->eq[k] + 1, total, &max_k);
806 if (isl_int_abs_gt(max_k, wraps->max))
807 isl_int_set(wraps->max, max_k);
810 for (k = 0; k < info->bmap->n_ineq; ++k) {
811 if (info->ineq[k] == STATUS_VALID ||
812 info->ineq[k] == STATUS_REDUNDANT)
813 continue;
814 isl_seq_abs_max(info->bmap->ineq[k] + 1, total, &max_k);
815 if (isl_int_abs_gt(max_k, wraps->max))
816 isl_int_set(wraps->max, max_k);
819 isl_int_clear(max_k);
822 /* Initialize the isl_wraps data structure.
823 * If we want to bound the coefficients of the wrapping constraints,
824 * we set wraps->max to the largest coefficient
825 * in the equalities and inequalities that can be removed if we end up
826 * applying wrapping.
828 static void wraps_init(struct isl_wraps *wraps, __isl_take isl_mat *mat,
829 struct isl_coalesce_info *info, int i, int j)
831 isl_ctx *ctx;
833 wraps->bound = 0;
834 wraps->mat = mat;
835 if (!mat)
836 return;
837 ctx = isl_mat_get_ctx(mat);
838 wraps->bound = isl_options_get_coalesce_bounded_wrapping(ctx);
839 if (!wraps->bound)
840 return;
841 isl_int_init(wraps->max);
842 isl_int_set_si(wraps->max, 0);
843 wraps_update_max(wraps, &info[i]);
844 wraps_update_max(wraps, &info[j]);
847 /* Free the contents of the isl_wraps data structure.
849 static void wraps_free(struct isl_wraps *wraps)
851 isl_mat_free(wraps->mat);
852 if (wraps->bound)
853 isl_int_clear(wraps->max);
856 /* Is the wrapping constraint in row "row" allowed?
858 * If wraps->bound is set, we check that none of the coefficients
859 * is greater than wraps->max.
861 static int allow_wrap(struct isl_wraps *wraps, int row)
863 int i;
865 if (!wraps->bound)
866 return 1;
868 for (i = 1; i < wraps->mat->n_col; ++i)
869 if (isl_int_abs_gt(wraps->mat->row[row][i], wraps->max))
870 return 0;
872 return 1;
875 /* Wrap "ineq" (or its opposite if "negate" is set) around "bound"
876 * to include "set" and add the result in position "w" of "wraps".
877 * "len" is the total number of coefficients in "bound" and "ineq".
878 * Return 1 on success, 0 on failure and -1 on error.
879 * Wrapping can fail if the result of wrapping is equal to "bound"
880 * or if we want to bound the sizes of the coefficients and
881 * the wrapped constraint does not satisfy this bound.
883 static int add_wrap(struct isl_wraps *wraps, int w, isl_int *bound,
884 isl_int *ineq, unsigned len, __isl_keep isl_set *set, int negate)
886 isl_seq_cpy(wraps->mat->row[w], bound, len);
887 if (negate) {
888 isl_seq_neg(wraps->mat->row[w + 1], ineq, len);
889 ineq = wraps->mat->row[w + 1];
891 if (!isl_set_wrap_facet(set, wraps->mat->row[w], ineq))
892 return -1;
893 if (isl_seq_eq(wraps->mat->row[w], bound, len))
894 return 0;
895 if (!allow_wrap(wraps, w))
896 return 0;
897 return 1;
900 /* For each constraint in info->bmap that is not redundant (as determined
901 * by info->tab) and that is not a valid constraint for the other basic map,
902 * wrap the constraint around "bound" such that it includes the whole
903 * set "set" and append the resulting constraint to "wraps".
904 * Note that the constraints that are valid for the other basic map
905 * will be added to the combined basic map by default, so there is
906 * no need to wrap them.
907 * The caller wrap_in_facets even relies on this function not wrapping
908 * any constraints that are already valid.
909 * "wraps" is assumed to have been pre-allocated to the appropriate size.
910 * wraps->n_row is the number of actual wrapped constraints that have
911 * been added.
912 * If any of the wrapping problems results in a constraint that is
913 * identical to "bound", then this means that "set" is unbounded in such
914 * way that no wrapping is possible. If this happens then wraps->n_row
915 * is reset to zero.
916 * Similarly, if we want to bound the coefficients of the wrapping
917 * constraints and a newly added wrapping constraint does not
918 * satisfy the bound, then wraps->n_row is also reset to zero.
920 static int add_wraps(struct isl_wraps *wraps, struct isl_coalesce_info *info,
921 isl_int *bound, __isl_keep isl_set *set)
923 int l, m;
924 int w;
925 int added;
926 isl_basic_map *bmap = info->bmap;
927 unsigned len = 1 + isl_basic_map_total_dim(bmap);
929 w = wraps->mat->n_row;
931 for (l = 0; l < bmap->n_ineq; ++l) {
932 if (info->ineq[l] == STATUS_VALID ||
933 info->ineq[l] == STATUS_REDUNDANT)
934 continue;
935 if (isl_seq_is_neg(bound, bmap->ineq[l], len))
936 continue;
937 if (isl_seq_eq(bound, bmap->ineq[l], len))
938 continue;
939 if (isl_tab_is_redundant(info->tab, bmap->n_eq + l))
940 continue;
942 added = add_wrap(wraps, w, bound, bmap->ineq[l], len, set, 0);
943 if (added < 0)
944 return -1;
945 if (!added)
946 goto unbounded;
947 ++w;
949 for (l = 0; l < bmap->n_eq; ++l) {
950 if (isl_seq_is_neg(bound, bmap->eq[l], len))
951 continue;
952 if (isl_seq_eq(bound, bmap->eq[l], len))
953 continue;
955 for (m = 0; m < 2; ++m) {
956 if (info->eq[2 * l + m] == STATUS_VALID)
957 continue;
958 added = add_wrap(wraps, w, bound, bmap->eq[l], len,
959 set, !m);
960 if (added < 0)
961 return -1;
962 if (!added)
963 goto unbounded;
964 ++w;
968 wraps->mat->n_row = w;
969 return 0;
970 unbounded:
971 wraps->mat->n_row = 0;
972 return 0;
975 /* Check if the constraints in "wraps" from "first" until the last
976 * are all valid for the basic set represented by "tab".
977 * If not, wraps->n_row is set to zero.
979 static int check_wraps(__isl_keep isl_mat *wraps, int first,
980 struct isl_tab *tab)
982 int i;
984 for (i = first; i < wraps->n_row; ++i) {
985 enum isl_ineq_type type;
986 type = isl_tab_ineq_type(tab, wraps->row[i]);
987 if (type == isl_ineq_error)
988 return -1;
989 if (type == isl_ineq_redundant)
990 continue;
991 wraps->n_row = 0;
992 return 0;
995 return 0;
998 /* Return a set that corresponds to the non-redundant constraints
999 * (as recorded in tab) of bmap.
1001 * It's important to remove the redundant constraints as some
1002 * of the other constraints may have been modified after the
1003 * constraints were marked redundant.
1004 * In particular, a constraint may have been relaxed.
1005 * Redundant constraints are ignored when a constraint is relaxed
1006 * and should therefore continue to be ignored ever after.
1007 * Otherwise, the relaxation might be thwarted by some of
1008 * these constraints.
1010 * Update the underlying set to ensure that the dimension doesn't change.
1011 * Otherwise the integer divisions could get dropped if the tab
1012 * turns out to be empty.
1014 static __isl_give isl_set *set_from_updated_bmap(__isl_keep isl_basic_map *bmap,
1015 struct isl_tab *tab)
1017 isl_basic_set *bset;
1019 bmap = isl_basic_map_copy(bmap);
1020 bset = isl_basic_map_underlying_set(bmap);
1021 bset = isl_basic_set_cow(bset);
1022 bset = isl_basic_set_update_from_tab(bset, tab);
1023 return isl_set_from_basic_set(bset);
1026 /* Wrap the constraints of info->bmap that bound the facet defined
1027 * by inequality "k" around (the opposite of) this inequality to
1028 * include "set". "bound" may be used to store the negated inequality.
1029 * Since the wrapped constraints are not guaranteed to contain the whole
1030 * of info->bmap, we check them in check_wraps.
1031 * If any of the wrapped constraints turn out to be invalid, then
1032 * check_wraps will reset wrap->n_row to zero.
1034 static int add_wraps_around_facet(struct isl_wraps *wraps,
1035 struct isl_coalesce_info *info, int k, isl_int *bound,
1036 __isl_keep isl_set *set)
1038 struct isl_tab_undo *snap;
1039 int n;
1040 unsigned total = isl_basic_map_total_dim(info->bmap);
1042 snap = isl_tab_snap(info->tab);
1044 if (isl_tab_select_facet(info->tab, info->bmap->n_eq + k) < 0)
1045 return -1;
1046 if (isl_tab_detect_redundant(info->tab) < 0)
1047 return -1;
1049 isl_seq_neg(bound, info->bmap->ineq[k], 1 + total);
1051 n = wraps->mat->n_row;
1052 if (add_wraps(wraps, info, bound, set) < 0)
1053 return -1;
1055 if (isl_tab_rollback(info->tab, snap) < 0)
1056 return -1;
1057 if (check_wraps(wraps->mat, n, info->tab) < 0)
1058 return -1;
1060 return 0;
1063 /* Given a basic set i with a constraint k that is adjacent to
1064 * basic set j, check if we can wrap
1065 * both the facet corresponding to k (if "wrap_facet" is set) and basic map j
1066 * (always) around their ridges to include the other set.
1067 * If so, replace the pair of basic sets by their union.
1069 * All constraints of i (except k) are assumed to be valid or
1070 * cut constraints for j.
1071 * Wrapping the cut constraints to include basic map j may result
1072 * in constraints that are no longer valid of basic map i
1073 * we have to check that the resulting wrapping constraints are valid for i.
1074 * If "wrap_facet" is not set, then all constraints of i (except k)
1075 * are assumed to be valid for j.
1076 * ____ _____
1077 * / | / \
1078 * / || / |
1079 * \ || => \ |
1080 * \ || \ |
1081 * \___|| \____|
1084 static enum isl_change can_wrap_in_facet(int i, int j, int k,
1085 struct isl_coalesce_info *info, int wrap_facet)
1087 enum isl_change change = isl_change_none;
1088 struct isl_wraps wraps;
1089 isl_ctx *ctx;
1090 isl_mat *mat;
1091 struct isl_set *set_i = NULL;
1092 struct isl_set *set_j = NULL;
1093 struct isl_vec *bound = NULL;
1094 unsigned total = isl_basic_map_total_dim(info[i].bmap);
1096 set_i = set_from_updated_bmap(info[i].bmap, info[i].tab);
1097 set_j = set_from_updated_bmap(info[j].bmap, info[j].tab);
1098 ctx = isl_basic_map_get_ctx(info[i].bmap);
1099 mat = isl_mat_alloc(ctx, 2 * (info[i].bmap->n_eq + info[j].bmap->n_eq) +
1100 info[i].bmap->n_ineq + info[j].bmap->n_ineq,
1101 1 + total);
1102 wraps_init(&wraps, mat, info, i, j);
1103 bound = isl_vec_alloc(ctx, 1 + total);
1104 if (!set_i || !set_j || !wraps.mat || !bound)
1105 goto error;
1107 isl_seq_cpy(bound->el, info[i].bmap->ineq[k], 1 + total);
1108 isl_int_add_ui(bound->el[0], bound->el[0], 1);
1110 isl_seq_cpy(wraps.mat->row[0], bound->el, 1 + total);
1111 wraps.mat->n_row = 1;
1113 if (add_wraps(&wraps, &info[j], bound->el, set_i) < 0)
1114 goto error;
1115 if (!wraps.mat->n_row)
1116 goto unbounded;
1118 if (wrap_facet) {
1119 if (add_wraps_around_facet(&wraps, &info[i], k,
1120 bound->el, set_j) < 0)
1121 goto error;
1122 if (!wraps.mat->n_row)
1123 goto unbounded;
1126 change = fuse(i, j, info, wraps.mat, 0, 0);
1128 unbounded:
1129 wraps_free(&wraps);
1131 isl_set_free(set_i);
1132 isl_set_free(set_j);
1134 isl_vec_free(bound);
1136 return change;
1137 error:
1138 wraps_free(&wraps);
1139 isl_vec_free(bound);
1140 isl_set_free(set_i);
1141 isl_set_free(set_j);
1142 return isl_change_error;
1145 /* Given a pair of basic maps i and j such that j sticks out
1146 * of i at n cut constraints, each time by at most one,
1147 * try to compute wrapping constraints and replace the two
1148 * basic maps by a single basic map.
1149 * The other constraints of i are assumed to be valid for j.
1151 * For each cut constraint t(x) >= 0 of i, we add the relaxed version
1152 * t(x) + 1 >= 0, along with wrapping constraints for all constraints
1153 * of basic map j that bound the part of basic map j that sticks out
1154 * of the cut constraint.
1155 * In particular, we first intersect basic map j with t(x) + 1 = 0.
1156 * If the result is empty, then t(x) >= 0 was actually a valid constraint
1157 * (with respect to the integer points), so we add t(x) >= 0 instead.
1158 * Otherwise, we wrap the constraints of basic map j that are not
1159 * redundant in this intersection and that are not already valid
1160 * for basic map i over basic map i.
1161 * Note that it is sufficient to wrap the constraints to include
1162 * basic map i, because we will only wrap the constraints that do
1163 * not include basic map i already. The wrapped constraint will
1164 * therefore be more relaxed compared to the original constraint.
1165 * Since the original constraint is valid for basic map j, so is
1166 * the wrapped constraint.
1168 * If any wrapping fails, i.e., if we cannot wrap to touch
1169 * the union, then we give up.
1170 * Otherwise, the pair of basic maps is replaced by their union.
1172 static enum isl_change wrap_in_facets(int i, int j, int *cuts, int n,
1173 struct isl_coalesce_info *info)
1175 enum isl_change change = isl_change_none;
1176 struct isl_wraps wraps;
1177 isl_ctx *ctx;
1178 isl_mat *mat;
1179 isl_set *set_i = NULL;
1180 unsigned total = isl_basic_map_total_dim(info[i].bmap);
1181 int max_wrap;
1182 int k, w;
1183 struct isl_tab_undo *snap;
1185 if (isl_tab_extend_cons(info[j].tab, 1) < 0)
1186 return isl_change_error;
1188 max_wrap = 1 + 2 * info[j].bmap->n_eq + info[j].bmap->n_ineq;
1189 max_wrap *= n;
1191 set_i = set_from_updated_bmap(info[i].bmap, info[i].tab);
1192 ctx = isl_basic_map_get_ctx(info[i].bmap);
1193 mat = isl_mat_alloc(ctx, max_wrap, 1 + total);
1194 wraps_init(&wraps, mat, info, i, j);
1195 if (!set_i || !wraps.mat)
1196 goto error;
1198 snap = isl_tab_snap(info[j].tab);
1200 wraps.mat->n_row = 0;
1202 for (k = 0; k < n; ++k) {
1203 w = wraps.mat->n_row++;
1204 isl_seq_cpy(wraps.mat->row[w],
1205 info[i].bmap->ineq[cuts[k]], 1 + total);
1206 isl_int_add_ui(wraps.mat->row[w][0], wraps.mat->row[w][0], 1);
1207 if (isl_tab_add_eq(info[j].tab, wraps.mat->row[w]) < 0)
1208 goto error;
1209 if (isl_tab_detect_redundant(info[j].tab) < 0)
1210 goto error;
1212 if (info[j].tab->empty)
1213 isl_int_sub_ui(wraps.mat->row[w][0],
1214 wraps.mat->row[w][0], 1);
1215 else if (add_wraps(&wraps, &info[j],
1216 wraps.mat->row[w], set_i) < 0)
1217 goto error;
1219 if (isl_tab_rollback(info[j].tab, snap) < 0)
1220 goto error;
1222 if (!wraps.mat->n_row)
1223 break;
1226 if (k == n)
1227 change = fuse(i, j, info, wraps.mat, 0, 1);
1229 wraps_free(&wraps);
1230 isl_set_free(set_i);
1232 return change;
1233 error:
1234 wraps_free(&wraps);
1235 isl_set_free(set_i);
1236 return isl_change_error;
1239 /* Given two basic sets i and j such that i has no cut equalities,
1240 * check if relaxing all the cut inequalities of i by one turns
1241 * them into valid constraint for j and check if we can wrap in
1242 * the bits that are sticking out.
1243 * If so, replace the pair by their union.
1245 * We first check if all relaxed cut inequalities of i are valid for j
1246 * and then try to wrap in the intersections of the relaxed cut inequalities
1247 * with j.
1249 * During this wrapping, we consider the points of j that lie at a distance
1250 * of exactly 1 from i. In particular, we ignore the points that lie in
1251 * between this lower-dimensional space and the basic map i.
1252 * We can therefore only apply this to integer maps.
1253 * ____ _____
1254 * / ___|_ / \
1255 * / | | / |
1256 * \ | | => \ |
1257 * \|____| \ |
1258 * \___| \____/
1260 * _____ ______
1261 * | ____|_ | \
1262 * | | | | |
1263 * | | | => | |
1264 * |_| | | |
1265 * |_____| \______|
1267 * _______
1268 * | |
1269 * | |\ |
1270 * | | \ |
1271 * | | \ |
1272 * | | \|
1273 * | | \
1274 * | |_____\
1275 * | |
1276 * |_______|
1278 * Wrapping can fail if the result of wrapping one of the facets
1279 * around its edges does not produce any new facet constraint.
1280 * In particular, this happens when we try to wrap in unbounded sets.
1282 * _______________________________________________________________________
1284 * | ___
1285 * | | |
1286 * |_| |_________________________________________________________________
1287 * |___|
1289 * The following is not an acceptable result of coalescing the above two
1290 * sets as it includes extra integer points.
1291 * _______________________________________________________________________
1293 * |
1294 * |
1296 * \______________________________________________________________________
1298 static enum isl_change can_wrap_in_set(int i, int j,
1299 struct isl_coalesce_info *info)
1301 enum isl_change change = isl_change_none;
1302 int k, m;
1303 int n;
1304 int *cuts = NULL;
1305 isl_ctx *ctx;
1307 if (ISL_F_ISSET(info[i].bmap, ISL_BASIC_MAP_RATIONAL) ||
1308 ISL_F_ISSET(info[j].bmap, ISL_BASIC_MAP_RATIONAL))
1309 return isl_change_none;
1311 n = count(info[i].ineq, info[i].bmap->n_ineq, STATUS_CUT);
1312 if (n == 0)
1313 return isl_change_none;
1315 ctx = isl_basic_map_get_ctx(info[i].bmap);
1316 cuts = isl_alloc_array(ctx, int, n);
1317 if (!cuts)
1318 return isl_change_error;
1320 for (k = 0, m = 0; m < n; ++k) {
1321 enum isl_ineq_type type;
1323 if (info[i].ineq[k] != STATUS_CUT)
1324 continue;
1326 isl_int_add_ui(info[i].bmap->ineq[k][0],
1327 info[i].bmap->ineq[k][0], 1);
1328 type = isl_tab_ineq_type(info[j].tab, info[i].bmap->ineq[k]);
1329 isl_int_sub_ui(info[i].bmap->ineq[k][0],
1330 info[i].bmap->ineq[k][0], 1);
1331 if (type == isl_ineq_error)
1332 goto error;
1333 if (type != isl_ineq_redundant)
1334 break;
1335 cuts[m] = k;
1336 ++m;
1339 if (m == n)
1340 change = wrap_in_facets(i, j, cuts, n, info);
1342 free(cuts);
1344 return change;
1345 error:
1346 free(cuts);
1347 return isl_change_error;
1350 /* Check if either i or j has only cut inequalities that can
1351 * be used to wrap in (a facet of) the other basic set.
1352 * if so, replace the pair by their union.
1354 static enum isl_change check_wrap(int i, int j, struct isl_coalesce_info *info)
1356 enum isl_change change = isl_change_none;
1358 if (!any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_CUT))
1359 change = can_wrap_in_set(i, j, info);
1360 if (change != isl_change_none)
1361 return change;
1363 if (!any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_CUT))
1364 change = can_wrap_in_set(j, i, info);
1365 return change;
1368 /* At least one of the basic maps has an equality that is adjacent
1369 * to inequality. Make sure that only one of the basic maps has
1370 * such an equality and that the other basic map has exactly one
1371 * inequality adjacent to an equality.
1372 * We call the basic map that has the inequality "i" and the basic
1373 * map that has the equality "j".
1374 * If "i" has any "cut" (in)equality, then relaxing the inequality
1375 * by one would not result in a basic map that contains the other
1376 * basic map. However, it may still be possible to wrap in the other
1377 * basic map.
1379 static enum isl_change check_adj_eq(int i, int j,
1380 struct isl_coalesce_info *info)
1382 enum isl_change change = isl_change_none;
1383 int k;
1384 int any_cut;
1386 if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ADJ_INEQ) &&
1387 any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_ADJ_INEQ))
1388 /* ADJ EQ TOO MANY */
1389 return isl_change_none;
1391 if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ADJ_INEQ))
1392 return check_adj_eq(j, i, info);
1394 /* j has an equality adjacent to an inequality in i */
1396 if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_CUT))
1397 return isl_change_none;
1398 any_cut = any(info[i].ineq, info[i].bmap->n_ineq, STATUS_CUT);
1399 if (count(info[i].ineq, info[i].bmap->n_ineq, STATUS_ADJ_EQ) != 1 ||
1400 any(info[j].ineq, info[j].bmap->n_ineq, STATUS_ADJ_EQ) ||
1401 any(info[i].ineq, info[i].bmap->n_ineq, STATUS_ADJ_INEQ) ||
1402 any(info[j].ineq, info[j].bmap->n_ineq, STATUS_ADJ_INEQ))
1403 /* ADJ EQ TOO MANY */
1404 return isl_change_none;
1406 for (k = 0; k < info[i].bmap->n_ineq; ++k)
1407 if (info[i].ineq[k] == STATUS_ADJ_EQ)
1408 break;
1410 if (!any_cut) {
1411 change = is_adj_eq_extension(i, j, k, info);
1412 if (change != isl_change_none)
1413 return change;
1416 change = can_wrap_in_facet(i, j, k, info, any_cut);
1418 return change;
1421 /* The two basic maps lie on adjacent hyperplanes. In particular,
1422 * basic map "i" has an equality that lies parallel to basic map "j".
1423 * Check if we can wrap the facets around the parallel hyperplanes
1424 * to include the other set.
1426 * We perform basically the same operations as can_wrap_in_facet,
1427 * except that we don't need to select a facet of one of the sets.
1429 * \\ \\
1430 * \\ => \\
1431 * \ \|
1433 * If there is more than one equality of "i" adjacent to an equality of "j",
1434 * then the result will satisfy one or more equalities that are a linear
1435 * combination of these equalities. These will be encoded as pairs
1436 * of inequalities in the wrapping constraints and need to be made
1437 * explicit.
1439 static enum isl_change check_eq_adj_eq(int i, int j,
1440 struct isl_coalesce_info *info)
1442 int k;
1443 enum isl_change change = isl_change_none;
1444 int detect_equalities = 0;
1445 struct isl_wraps wraps;
1446 isl_ctx *ctx;
1447 isl_mat *mat;
1448 struct isl_set *set_i = NULL;
1449 struct isl_set *set_j = NULL;
1450 struct isl_vec *bound = NULL;
1451 unsigned total = isl_basic_map_total_dim(info[i].bmap);
1453 if (count(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ADJ_EQ) != 1)
1454 detect_equalities = 1;
1456 for (k = 0; k < 2 * info[i].bmap->n_eq ; ++k)
1457 if (info[i].eq[k] == STATUS_ADJ_EQ)
1458 break;
1460 set_i = set_from_updated_bmap(info[i].bmap, info[i].tab);
1461 set_j = set_from_updated_bmap(info[j].bmap, info[j].tab);
1462 ctx = isl_basic_map_get_ctx(info[i].bmap);
1463 mat = isl_mat_alloc(ctx, 2 * (info[i].bmap->n_eq + info[j].bmap->n_eq) +
1464 info[i].bmap->n_ineq + info[j].bmap->n_ineq,
1465 1 + total);
1466 wraps_init(&wraps, mat, info, i, j);
1467 bound = isl_vec_alloc(ctx, 1 + total);
1468 if (!set_i || !set_j || !wraps.mat || !bound)
1469 goto error;
1471 if (k % 2 == 0)
1472 isl_seq_neg(bound->el, info[i].bmap->eq[k / 2], 1 + total);
1473 else
1474 isl_seq_cpy(bound->el, info[i].bmap->eq[k / 2], 1 + total);
1475 isl_int_add_ui(bound->el[0], bound->el[0], 1);
1477 isl_seq_cpy(wraps.mat->row[0], bound->el, 1 + total);
1478 wraps.mat->n_row = 1;
1480 if (add_wraps(&wraps, &info[j], bound->el, set_i) < 0)
1481 goto error;
1482 if (!wraps.mat->n_row)
1483 goto unbounded;
1485 isl_int_sub_ui(bound->el[0], bound->el[0], 1);
1486 isl_seq_neg(bound->el, bound->el, 1 + total);
1488 isl_seq_cpy(wraps.mat->row[wraps.mat->n_row], bound->el, 1 + total);
1489 wraps.mat->n_row++;
1491 if (add_wraps(&wraps, &info[i], bound->el, set_j) < 0)
1492 goto error;
1493 if (!wraps.mat->n_row)
1494 goto unbounded;
1496 change = fuse(i, j, info, wraps.mat, detect_equalities, 0);
1498 if (0) {
1499 error: change = isl_change_error;
1501 unbounded:
1503 wraps_free(&wraps);
1504 isl_set_free(set_i);
1505 isl_set_free(set_j);
1506 isl_vec_free(bound);
1508 return change;
1511 /* Initialize the "eq" and "ineq" fields of "info".
1513 static void init_status(struct isl_coalesce_info *info)
1515 info->eq = info->ineq = NULL;
1518 /* Set info->eq to the positions of the equalities of info->bmap
1519 * with respect to the basic map represented by "tab".
1520 * If info->eq has already been computed, then do not compute it again.
1522 static void set_eq_status_in(struct isl_coalesce_info *info,
1523 struct isl_tab *tab)
1525 if (info->eq)
1526 return;
1527 info->eq = eq_status_in(info->bmap, tab);
1530 /* Set info->ineq to the positions of the inequalities of info->bmap
1531 * with respect to the basic map represented by "tab".
1532 * If info->ineq has already been computed, then do not compute it again.
1534 static void set_ineq_status_in(struct isl_coalesce_info *info,
1535 struct isl_tab *tab)
1537 if (info->ineq)
1538 return;
1539 info->ineq = ineq_status_in(info->bmap, info->tab, tab);
1542 /* Free the memory allocated by the "eq" and "ineq" fields of "info".
1543 * This function assumes that init_status has been called on "info" first,
1544 * after which the "eq" and "ineq" fields may or may not have been
1545 * assigned a newly allocated array.
1547 static void clear_status(struct isl_coalesce_info *info)
1549 free(info->eq);
1550 free(info->ineq);
1553 /* Check if the union of the given pair of basic maps
1554 * can be represented by a single basic map.
1555 * If so, replace the pair by the single basic map and return
1556 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
1557 * Otherwise, return isl_change_none.
1558 * The two basic maps are assumed to live in the same local space.
1559 * The "eq" and "ineq" fields of info[i] and info[j] are assumed
1560 * to have been initialized by the caller, either to NULL or
1561 * to valid information.
1563 * We first check the effect of each constraint of one basic map
1564 * on the other basic map.
1565 * The constraint may be
1566 * redundant the constraint is redundant in its own
1567 * basic map and should be ignore and removed
1568 * in the end
1569 * valid all (integer) points of the other basic map
1570 * satisfy the constraint
1571 * separate no (integer) point of the other basic map
1572 * satisfies the constraint
1573 * cut some but not all points of the other basic map
1574 * satisfy the constraint
1575 * adj_eq the given constraint is adjacent (on the outside)
1576 * to an equality of the other basic map
1577 * adj_ineq the given constraint is adjacent (on the outside)
1578 * to an inequality of the other basic map
1580 * We consider seven cases in which we can replace the pair by a single
1581 * basic map. We ignore all "redundant" constraints.
1583 * 1. all constraints of one basic map are valid
1584 * => the other basic map is a subset and can be removed
1586 * 2. all constraints of both basic maps are either "valid" or "cut"
1587 * and the facets corresponding to the "cut" constraints
1588 * of one of the basic maps lies entirely inside the other basic map
1589 * => the pair can be replaced by a basic map consisting
1590 * of the valid constraints in both basic maps
1592 * 3. there is a single pair of adjacent inequalities
1593 * (all other constraints are "valid")
1594 * => the pair can be replaced by a basic map consisting
1595 * of the valid constraints in both basic maps
1597 * 4. one basic map has a single adjacent inequality, while the other
1598 * constraints are "valid". The other basic map has some
1599 * "cut" constraints, but replacing the adjacent inequality by
1600 * its opposite and adding the valid constraints of the other
1601 * basic map results in a subset of the other basic map
1602 * => the pair can be replaced by a basic map consisting
1603 * of the valid constraints in both basic maps
1605 * 5. there is a single adjacent pair of an inequality and an equality,
1606 * the other constraints of the basic map containing the inequality are
1607 * "valid". Moreover, if the inequality the basic map is relaxed
1608 * and then turned into an equality, then resulting facet lies
1609 * entirely inside the other basic map
1610 * => the pair can be replaced by the basic map containing
1611 * the inequality, with the inequality relaxed.
1613 * 6. there is a single adjacent pair of an inequality and an equality,
1614 * the other constraints of the basic map containing the inequality are
1615 * "valid". Moreover, the facets corresponding to both
1616 * the inequality and the equality can be wrapped around their
1617 * ridges to include the other basic map
1618 * => the pair can be replaced by a basic map consisting
1619 * of the valid constraints in both basic maps together
1620 * with all wrapping constraints
1622 * 7. one of the basic maps extends beyond the other by at most one.
1623 * Moreover, the facets corresponding to the cut constraints and
1624 * the pieces of the other basic map at offset one from these cut
1625 * constraints can be wrapped around their ridges to include
1626 * the union of the two basic maps
1627 * => the pair can be replaced by a basic map consisting
1628 * of the valid constraints in both basic maps together
1629 * with all wrapping constraints
1631 * 8. the two basic maps live in adjacent hyperplanes. In principle
1632 * such sets can always be combined through wrapping, but we impose
1633 * that there is only one such pair, to avoid overeager coalescing.
1635 * Throughout the computation, we maintain a collection of tableaus
1636 * corresponding to the basic maps. When the basic maps are dropped
1637 * or combined, the tableaus are modified accordingly.
1639 static enum isl_change coalesce_local_pair_reuse(int i, int j,
1640 struct isl_coalesce_info *info)
1642 enum isl_change change = isl_change_none;
1644 set_eq_status_in(&info[i], info[j].tab);
1645 if (info[i].bmap->n_eq && !info[i].eq)
1646 goto error;
1647 if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ERROR))
1648 goto error;
1649 if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_SEPARATE))
1650 goto done;
1652 set_eq_status_in(&info[j], info[i].tab);
1653 if (info[j].bmap->n_eq && !info[j].eq)
1654 goto error;
1655 if (any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_ERROR))
1656 goto error;
1657 if (any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_SEPARATE))
1658 goto done;
1660 set_ineq_status_in(&info[i], info[j].tab);
1661 if (info[i].bmap->n_ineq && !info[i].ineq)
1662 goto error;
1663 if (any(info[i].ineq, info[i].bmap->n_ineq, STATUS_ERROR))
1664 goto error;
1665 if (any(info[i].ineq, info[i].bmap->n_ineq, STATUS_SEPARATE))
1666 goto done;
1668 set_ineq_status_in(&info[j], info[i].tab);
1669 if (info[j].bmap->n_ineq && !info[j].ineq)
1670 goto error;
1671 if (any(info[j].ineq, info[j].bmap->n_ineq, STATUS_ERROR))
1672 goto error;
1673 if (any(info[j].ineq, info[j].bmap->n_ineq, STATUS_SEPARATE))
1674 goto done;
1676 if (all(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_VALID) &&
1677 all(info[i].ineq, info[i].bmap->n_ineq, STATUS_VALID)) {
1678 drop(&info[j]);
1679 change = isl_change_drop_second;
1680 } else if (all(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_VALID) &&
1681 all(info[j].ineq, info[j].bmap->n_ineq, STATUS_VALID)) {
1682 drop(&info[i]);
1683 change = isl_change_drop_first;
1684 } else if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ADJ_EQ)) {
1685 change = check_eq_adj_eq(i, j, info);
1686 } else if (any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_ADJ_EQ)) {
1687 change = check_eq_adj_eq(j, i, info);
1688 } else if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ADJ_INEQ) ||
1689 any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_ADJ_INEQ)) {
1690 change = check_adj_eq(i, j, info);
1691 } else if (any(info[i].ineq, info[i].bmap->n_ineq, STATUS_ADJ_EQ) ||
1692 any(info[j].ineq, info[j].bmap->n_ineq, STATUS_ADJ_EQ)) {
1693 /* Can't happen */
1694 /* BAD ADJ INEQ */
1695 } else if (any(info[i].ineq, info[i].bmap->n_ineq, STATUS_ADJ_INEQ) ||
1696 any(info[j].ineq, info[j].bmap->n_ineq, STATUS_ADJ_INEQ)) {
1697 change = check_adj_ineq(i, j, info);
1698 } else {
1699 if (!any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_CUT) &&
1700 !any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_CUT))
1701 change = check_facets(i, j, info);
1702 if (change == isl_change_none)
1703 change = check_wrap(i, j, info);
1706 done:
1707 clear_status(&info[i]);
1708 clear_status(&info[j]);
1709 return change;
1710 error:
1711 clear_status(&info[i]);
1712 clear_status(&info[j]);
1713 return isl_change_error;
1716 /* Check if the union of the given pair of basic maps
1717 * can be represented by a single basic map.
1718 * If so, replace the pair by the single basic map and return
1719 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
1720 * Otherwise, return isl_change_none.
1721 * The two basic maps are assumed to live in the same local space.
1723 static enum isl_change coalesce_local_pair(int i, int j,
1724 struct isl_coalesce_info *info)
1726 init_status(&info[i]);
1727 init_status(&info[j]);
1728 return coalesce_local_pair_reuse(i, j, info);
1731 /* Shift the integer division at position "div" of the basic map
1732 * represented by "info" by "shift".
1734 * That is, if the integer division has the form
1736 * floor(f(x)/d)
1738 * then replace it by
1740 * floor((f(x) + shift * d)/d) - shift
1742 static int shift_div(struct isl_coalesce_info *info, int div, isl_int shift)
1744 unsigned total;
1746 info->bmap = isl_basic_map_shift_div(info->bmap, div, 0, shift);
1747 if (!info->bmap)
1748 return -1;
1750 total = isl_basic_map_dim(info->bmap, isl_dim_all);
1751 total -= isl_basic_map_dim(info->bmap, isl_dim_div);
1752 if (isl_tab_shift_var(info->tab, total + div, shift) < 0)
1753 return -1;
1755 return 0;
1758 /* Check if some of the divs in the basic map represented by "info1"
1759 * are shifts of the corresponding divs in the basic map represented
1760 * by "info2". If so, align them with those of "info2".
1761 * Only do this if "info1" and "info2" have the same number
1762 * of integer divisions.
1764 * An integer division is considered to be a shift of another integer
1765 * division if one is equal to the other plus a constant.
1767 * In particular, for each pair of integer divisions, if both are known,
1768 * have identical coefficients (apart from the constant term) and
1769 * if the difference between the constant terms (taking into account
1770 * the denominator) is an integer, then move the difference outside.
1771 * That is, if one integer division is of the form
1773 * floor((f(x) + c_1)/d)
1775 * while the other is of the form
1777 * floor((f(x) + c_2)/d)
1779 * and n = (c_2 - c_1)/d is an integer, then replace the first
1780 * integer division by
1782 * floor((f(x) + c_1 + n * d)/d) - n = floor((f(x) + c_2)/d) - n
1784 static int harmonize_divs(struct isl_coalesce_info *info1,
1785 struct isl_coalesce_info *info2)
1787 int i;
1788 int total;
1790 if (!info1->bmap || !info2->bmap)
1791 return -1;
1793 if (info1->bmap->n_div != info2->bmap->n_div)
1794 return 0;
1795 if (info1->bmap->n_div == 0)
1796 return 0;
1798 total = isl_basic_map_total_dim(info1->bmap);
1799 for (i = 0; i < info1->bmap->n_div; ++i) {
1800 isl_int d;
1801 int r = 0;
1803 if (isl_int_is_zero(info1->bmap->div[i][0]) ||
1804 isl_int_is_zero(info2->bmap->div[i][0]))
1805 continue;
1806 if (isl_int_ne(info1->bmap->div[i][0], info2->bmap->div[i][0]))
1807 continue;
1808 if (isl_int_eq(info1->bmap->div[i][1], info2->bmap->div[i][1]))
1809 continue;
1810 if (!isl_seq_eq(info1->bmap->div[i] + 2,
1811 info2->bmap->div[i] + 2, total))
1812 continue;
1813 isl_int_init(d);
1814 isl_int_sub(d, info2->bmap->div[i][1], info1->bmap->div[i][1]);
1815 if (isl_int_is_divisible_by(d, info1->bmap->div[i][0])) {
1816 isl_int_divexact(d, d, info1->bmap->div[i][0]);
1817 r = shift_div(info1, i, d);
1819 isl_int_clear(d);
1820 if (r < 0)
1821 return -1;
1824 return 0;
1827 /* Do the two basic maps live in the same local space, i.e.,
1828 * do they have the same (known) divs?
1829 * If either basic map has any unknown divs, then we can only assume
1830 * that they do not live in the same local space.
1832 static int same_divs(__isl_keep isl_basic_map *bmap1,
1833 __isl_keep isl_basic_map *bmap2)
1835 int i;
1836 int known;
1837 int total;
1839 if (!bmap1 || !bmap2)
1840 return -1;
1841 if (bmap1->n_div != bmap2->n_div)
1842 return 0;
1844 if (bmap1->n_div == 0)
1845 return 1;
1847 known = isl_basic_map_divs_known(bmap1);
1848 if (known < 0 || !known)
1849 return known;
1850 known = isl_basic_map_divs_known(bmap2);
1851 if (known < 0 || !known)
1852 return known;
1854 total = isl_basic_map_total_dim(bmap1);
1855 for (i = 0; i < bmap1->n_div; ++i)
1856 if (!isl_seq_eq(bmap1->div[i], bmap2->div[i], 2 + total))
1857 return 0;
1859 return 1;
1862 /* Expand info->tab in the same way info->bmap was expanded in
1863 * isl_basic_map_expand_divs using the expansion "exp" and
1864 * update info->ineq with respect to the redundant constraints
1865 * in the resulting tableau.
1866 * In particular, introduce extra variables corresponding
1867 * to the extra integer divisions and add the div constraints
1868 * that were added to info->bmap after info->tab was created
1869 * from the original info->bmap.
1870 * info->ineq was computed without a tableau and therefore
1871 * does not take into account the redundant constraints
1872 * in the tableau. Mark them here.
1874 static isl_stat expand_tab(struct isl_coalesce_info *info, int *exp)
1876 unsigned total, pos, n_div;
1877 int extra_var;
1878 int i, n, j, n_ineq;
1879 unsigned n_eq;
1881 total = isl_basic_map_dim(info->bmap, isl_dim_all);
1882 n_div = isl_basic_map_dim(info->bmap, isl_dim_div);
1883 pos = total - n_div;
1884 extra_var = total - info->tab->n_var;
1885 n = n_div - extra_var;
1887 if (isl_tab_extend_vars(info->tab, extra_var) < 0)
1888 return isl_stat_error;
1889 if (isl_tab_extend_cons(info->tab, 2 * extra_var) < 0)
1890 return isl_stat_error;
1892 i = 0;
1893 for (j = 0; j < n_div; ++j) {
1894 if (i < n && exp[i] == j) {
1895 ++i;
1896 continue;
1898 if (isl_tab_insert_var(info->tab, pos + j) < 0)
1899 return isl_stat_error;
1902 n_ineq = info->tab->n_con - info->tab->n_eq;
1903 for (i = n_ineq; i < info->bmap->n_ineq; ++i)
1904 if (isl_tab_add_ineq(info->tab, info->bmap->ineq[i]) < 0)
1905 return isl_stat_error;
1907 n_eq = info->bmap->n_eq;
1908 for (i = 0; i < info->bmap->n_ineq; ++i) {
1909 if (isl_tab_is_redundant(info->tab, n_eq + i))
1910 info->ineq[i] = STATUS_REDUNDANT;
1913 return isl_stat_ok;
1916 /* Check if the union of the basic maps represented by info[i] and info[j]
1917 * can be represented by a single basic map,
1918 * after expanding the divs of info[i] to match those of info[j].
1919 * If so, replace the pair by the single basic map and return
1920 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
1921 * Otherwise, return isl_change_none.
1923 * The caller has already checked for info[j] being a subset of info[i].
1924 * If some of the divs of info[j] are unknown, then the expanded info[i]
1925 * will not have the corresponding div constraints. The other patterns
1926 * therefore cannot apply. Skip the computation in this case.
1928 * The expansion is performed using the divs "div" and expansion "exp"
1929 * computed by the caller.
1930 * info[i].bmap has already been expanded and the result is passed in
1931 * as "bmap".
1932 * The "eq" and "ineq" fields of info[i] reflect the status of
1933 * the constraints of the expanded "bmap" with respect to info[j].tab.
1934 * However, inequality constraints that are redundant in info[i].tab
1935 * have not yet been marked as such because no tableau was available.
1937 * Replace info[i].bmap by "bmap" and expand info[i].tab as well,
1938 * updating info[i].ineq with respect to the redundant constraints.
1939 * Then try and coalesce the expanded info[i] with info[j],
1940 * reusing the information in info[i].eq and info[i].ineq.
1941 * If this does not result in any coalescing or if it results in info[j]
1942 * getting dropped (which should not happen in practice, since the case
1943 * of info[j] being a subset of info[i] has already been checked by
1944 * the caller), then revert info[i] to its original state.
1946 static enum isl_change coalesce_expand_tab_divs(__isl_take isl_basic_map *bmap,
1947 int i, int j, struct isl_coalesce_info *info, __isl_keep isl_mat *div,
1948 int *exp)
1950 isl_bool known;
1951 isl_basic_map *bmap_i;
1952 struct isl_tab_undo *snap;
1953 enum isl_change change = isl_change_none;
1955 known = isl_basic_map_divs_known(info[j].bmap);
1956 if (known < 0 || !known) {
1957 clear_status(&info[i]);
1958 isl_basic_map_free(bmap);
1959 return known < 0 ? isl_change_error : isl_change_none;
1962 bmap_i = info[i].bmap;
1963 info[i].bmap = isl_basic_map_copy(bmap);
1964 snap = isl_tab_snap(info[i].tab);
1965 if (!info[i].bmap || expand_tab(&info[i], exp) < 0)
1966 change = isl_change_error;
1968 init_status(&info[j]);
1969 if (change == isl_change_none)
1970 change = coalesce_local_pair_reuse(i, j, info);
1971 else
1972 clear_status(&info[i]);
1973 if (change != isl_change_none && change != isl_change_drop_second) {
1974 isl_basic_map_free(bmap_i);
1975 } else {
1976 isl_basic_map_free(info[i].bmap);
1977 info[i].bmap = bmap_i;
1979 if (isl_tab_rollback(info[i].tab, snap) < 0)
1980 change = isl_change_error;
1983 isl_basic_map_free(bmap);
1984 return change;
1987 /* Check if the union of "bmap" and the basic map represented by info[j]
1988 * can be represented by a single basic map,
1989 * after expanding the divs of "bmap" to match those of info[j].
1990 * If so, replace the pair by the single basic map and return
1991 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
1992 * Otherwise, return isl_change_none.
1994 * In particular, check if the expanded "bmap" contains the basic map
1995 * represented by the tableau info[j].tab.
1996 * The expansion is performed using the divs "div" and expansion "exp"
1997 * computed by the caller.
1998 * Then we check if all constraints of the expanded "bmap" are valid for
1999 * info[j].tab.
2001 * If "i" is not equal to -1, then "bmap" is equal to info[i].bmap.
2002 * In this case, the positions of the constraints of info[i].bmap
2003 * with respect to the basic map represented by info[j] are stored
2004 * in info[i].
2006 * If the expanded "bmap" does not contain the basic map
2007 * represented by the tableau info[j].tab and if "i" is not -1,
2008 * i.e., if the original "bmap" is info[i].bmap, then expand info[i].tab
2009 * as well and check if that results in coalescing.
2011 static enum isl_change coalesce_with_expanded_divs(
2012 __isl_keep isl_basic_map *bmap, int i, int j,
2013 struct isl_coalesce_info *info, __isl_keep isl_mat *div, int *exp)
2015 enum isl_change change = isl_change_none;
2016 struct isl_coalesce_info info_local, *info_i;
2018 info_i = i >= 0 ? &info[i] : &info_local;
2019 init_status(info_i);
2020 bmap = isl_basic_map_copy(bmap);
2021 bmap = isl_basic_map_expand_divs(bmap, isl_mat_copy(div), exp);
2023 if (!bmap)
2024 goto error;
2026 info_i->eq = eq_status_in(bmap, info[j].tab);
2027 if (bmap->n_eq && !info_i->eq)
2028 goto error;
2029 if (any(info_i->eq, 2 * bmap->n_eq, STATUS_ERROR))
2030 goto error;
2031 if (any(info_i->eq, 2 * bmap->n_eq, STATUS_SEPARATE))
2032 goto done;
2034 info_i->ineq = ineq_status_in(bmap, NULL, info[j].tab);
2035 if (bmap->n_ineq && !info_i->ineq)
2036 goto error;
2037 if (any(info_i->ineq, bmap->n_ineq, STATUS_ERROR))
2038 goto error;
2039 if (any(info_i->ineq, bmap->n_ineq, STATUS_SEPARATE))
2040 goto done;
2042 if (all(info_i->eq, 2 * bmap->n_eq, STATUS_VALID) &&
2043 all(info_i->ineq, bmap->n_ineq, STATUS_VALID)) {
2044 drop(&info[j]);
2045 change = isl_change_drop_second;
2048 if (change == isl_change_none && i != -1)
2049 return coalesce_expand_tab_divs(bmap, i, j, info, div, exp);
2051 done:
2052 isl_basic_map_free(bmap);
2053 clear_status(info_i);
2054 return change;
2055 error:
2056 isl_basic_map_free(bmap);
2057 clear_status(info_i);
2058 return isl_change_error;
2061 /* Check if the union of "bmap_i" and the basic map represented by info[j]
2062 * can be represented by a single basic map,
2063 * after aligning the divs of "bmap_i" to match those of info[j].
2064 * If so, replace the pair by the single basic map and return
2065 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
2066 * Otherwise, return isl_change_none.
2068 * In particular, check if "bmap_i" contains the basic map represented by
2069 * info[j] after aligning the divs of "bmap_i" to those of info[j].
2070 * Note that this can only succeed if the number of divs of "bmap_i"
2071 * is smaller than (or equal to) the number of divs of info[j].
2073 * We first check if the divs of "bmap_i" are all known and form a subset
2074 * of those of info[j].bmap. If so, we pass control over to
2075 * coalesce_with_expanded_divs.
2077 * If "i" is not equal to -1, then "bmap" is equal to info[i].bmap.
2079 static enum isl_change coalesce_after_aligning_divs(
2080 __isl_keep isl_basic_map *bmap_i, int i, int j,
2081 struct isl_coalesce_info *info)
2083 int known;
2084 isl_mat *div_i, *div_j, *div;
2085 int *exp1 = NULL;
2086 int *exp2 = NULL;
2087 isl_ctx *ctx;
2088 enum isl_change change;
2090 known = isl_basic_map_divs_known(bmap_i);
2091 if (known < 0 || !known)
2092 return known;
2094 ctx = isl_basic_map_get_ctx(bmap_i);
2096 div_i = isl_basic_map_get_divs(bmap_i);
2097 div_j = isl_basic_map_get_divs(info[j].bmap);
2099 if (!div_i || !div_j)
2100 goto error;
2102 exp1 = isl_alloc_array(ctx, int, div_i->n_row);
2103 exp2 = isl_alloc_array(ctx, int, div_j->n_row);
2104 if ((div_i->n_row && !exp1) || (div_j->n_row && !exp2))
2105 goto error;
2107 div = isl_merge_divs(div_i, div_j, exp1, exp2);
2108 if (!div)
2109 goto error;
2111 if (div->n_row == div_j->n_row)
2112 change = coalesce_with_expanded_divs(bmap_i,
2113 i, j, info, div, exp1);
2114 else
2115 change = isl_change_none;
2117 isl_mat_free(div);
2119 isl_mat_free(div_i);
2120 isl_mat_free(div_j);
2122 free(exp2);
2123 free(exp1);
2125 return change;
2126 error:
2127 isl_mat_free(div_i);
2128 isl_mat_free(div_j);
2129 free(exp1);
2130 free(exp2);
2131 return isl_change_error;
2134 /* Check if basic map "j" is a subset of basic map "i" after
2135 * exploiting the extra equalities of "j" to simplify the divs of "i".
2136 * If so, remove basic map "j" and return isl_change_drop_second.
2138 * If "j" does not have any equalities or if they are the same
2139 * as those of "i", then we cannot exploit them to simplify the divs.
2140 * Similarly, if there are no divs in "i", then they cannot be simplified.
2141 * If, on the other hand, the affine hulls of "i" and "j" do not intersect,
2142 * then "j" cannot be a subset of "i".
2144 * Otherwise, we intersect "i" with the affine hull of "j" and then
2145 * check if "j" is a subset of the result after aligning the divs.
2146 * If so, then "j" is definitely a subset of "i" and can be removed.
2147 * Note that if after intersection with the affine hull of "j".
2148 * "i" still has more divs than "j", then there is no way we can
2149 * align the divs of "i" to those of "j".
2151 static enum isl_change coalesce_subset_with_equalities(int i, int j,
2152 struct isl_coalesce_info *info)
2154 isl_basic_map *hull_i, *hull_j, *bmap_i;
2155 int equal, empty;
2156 enum isl_change change;
2158 if (info[j].bmap->n_eq == 0)
2159 return isl_change_none;
2160 if (info[i].bmap->n_div == 0)
2161 return isl_change_none;
2163 hull_i = isl_basic_map_copy(info[i].bmap);
2164 hull_i = isl_basic_map_plain_affine_hull(hull_i);
2165 hull_j = isl_basic_map_copy(info[j].bmap);
2166 hull_j = isl_basic_map_plain_affine_hull(hull_j);
2168 hull_j = isl_basic_map_intersect(hull_j, isl_basic_map_copy(hull_i));
2169 equal = isl_basic_map_plain_is_equal(hull_i, hull_j);
2170 empty = isl_basic_map_plain_is_empty(hull_j);
2171 isl_basic_map_free(hull_i);
2173 if (equal < 0 || equal || empty < 0 || empty) {
2174 isl_basic_map_free(hull_j);
2175 if (equal < 0 || empty < 0)
2176 return isl_change_error;
2177 return isl_change_none;
2180 bmap_i = isl_basic_map_copy(info[i].bmap);
2181 bmap_i = isl_basic_map_intersect(bmap_i, hull_j);
2182 if (!bmap_i)
2183 return isl_change_error;
2185 if (bmap_i->n_div > info[j].bmap->n_div) {
2186 isl_basic_map_free(bmap_i);
2187 return isl_change_none;
2190 change = coalesce_after_aligning_divs(bmap_i, -1, j, info);
2192 isl_basic_map_free(bmap_i);
2194 return change;
2197 /* Check if the union of and the basic maps represented by info[i] and info[j]
2198 * can be represented by a single basic map, by aligning or equating
2199 * their integer divisions.
2200 * If so, replace the pair by the single basic map and return
2201 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
2202 * Otherwise, return isl_change_none.
2204 * Note that we only perform any test if the number of divs is different
2205 * in the two basic maps. In case the number of divs is the same,
2206 * we have already established that the divs are different
2207 * in the two basic maps.
2208 * In particular, if the number of divs of basic map i is smaller than
2209 * the number of divs of basic map j, then we check if j is a subset of i
2210 * and vice versa.
2212 static enum isl_change coalesce_divs(int i, int j,
2213 struct isl_coalesce_info *info)
2215 enum isl_change change = isl_change_none;
2217 if (info[i].bmap->n_div < info[j].bmap->n_div)
2218 change = coalesce_after_aligning_divs(info[i].bmap, i, j, info);
2219 if (change != isl_change_none)
2220 return change;
2222 if (info[j].bmap->n_div < info[i].bmap->n_div)
2223 change = coalesce_after_aligning_divs(info[j].bmap, j, i, info);
2224 if (change != isl_change_none)
2225 return invert_change(change);
2227 change = coalesce_subset_with_equalities(i, j, info);
2228 if (change != isl_change_none)
2229 return change;
2231 change = coalesce_subset_with_equalities(j, i, info);
2232 if (change != isl_change_none)
2233 return invert_change(change);
2235 return isl_change_none;
2238 /* Does "bmap" involve any divs that themselves refer to divs?
2240 static int has_nested_div(__isl_keep isl_basic_map *bmap)
2242 int i;
2243 unsigned total;
2244 unsigned n_div;
2246 total = isl_basic_map_dim(bmap, isl_dim_all);
2247 n_div = isl_basic_map_dim(bmap, isl_dim_div);
2248 total -= n_div;
2250 for (i = 0; i < n_div; ++i)
2251 if (isl_seq_first_non_zero(bmap->div[i] + 2 + total,
2252 n_div) != -1)
2253 return 1;
2255 return 0;
2258 /* Return a list of affine expressions, one for each integer division
2259 * in "bmap_i". For each integer division that also appears in "bmap_j",
2260 * the affine expression is set to NaN. The number of NaNs in the list
2261 * is equal to the number of integer divisions in "bmap_j".
2262 * For the other integer divisions of "bmap_i", the corresponding
2263 * element in the list is a purely affine expression equal to the integer
2264 * division in "hull".
2265 * If no such list can be constructed, then the number of elements
2266 * in the returned list is smaller than the number of integer divisions
2267 * in "bmap_i".
2269 static __isl_give isl_aff_list *set_up_substitutions(
2270 __isl_keep isl_basic_map *bmap_i, __isl_keep isl_basic_map *bmap_j,
2271 __isl_take isl_basic_map *hull)
2273 unsigned n_div_i, n_div_j, total;
2274 isl_ctx *ctx;
2275 isl_local_space *ls;
2276 isl_basic_set *wrap_hull;
2277 isl_aff *aff_nan;
2278 isl_aff_list *list;
2279 int i, j;
2281 if (!hull)
2282 return NULL;
2284 ctx = isl_basic_map_get_ctx(hull);
2286 n_div_i = isl_basic_map_dim(bmap_i, isl_dim_div);
2287 n_div_j = isl_basic_map_dim(bmap_j, isl_dim_div);
2288 total = isl_basic_map_total_dim(bmap_i) - n_div_i;
2290 ls = isl_basic_map_get_local_space(bmap_i);
2291 ls = isl_local_space_wrap(ls);
2292 wrap_hull = isl_basic_map_wrap(hull);
2294 aff_nan = isl_aff_nan_on_domain(isl_local_space_copy(ls));
2295 list = isl_aff_list_alloc(ctx, n_div_i);
2297 j = 0;
2298 for (i = 0; i < n_div_i; ++i) {
2299 isl_aff *aff;
2301 if (j < n_div_j &&
2302 isl_seq_eq(bmap_i->div[i], bmap_j->div[j], 2 + total)) {
2303 ++j;
2304 list = isl_aff_list_add(list, isl_aff_copy(aff_nan));
2305 continue;
2307 if (n_div_i - i <= n_div_j - j)
2308 break;
2310 aff = isl_local_space_get_div(ls, i);
2311 aff = isl_aff_substitute_equalities(aff,
2312 isl_basic_set_copy(wrap_hull));
2313 aff = isl_aff_floor(aff);
2314 if (!aff)
2315 goto error;
2316 if (isl_aff_dim(aff, isl_dim_div) != 0) {
2317 isl_aff_free(aff);
2318 break;
2321 list = isl_aff_list_add(list, aff);
2324 isl_aff_free(aff_nan);
2325 isl_local_space_free(ls);
2326 isl_basic_set_free(wrap_hull);
2328 return list;
2329 error:
2330 isl_aff_free(aff_nan);
2331 isl_local_space_free(ls);
2332 isl_basic_set_free(wrap_hull);
2333 isl_aff_list_free(list);
2334 return NULL;
2337 /* Add variables to info->bmap and info->tab corresponding to the elements
2338 * in "list" that are not set to NaN.
2339 * "extra_var" is the number of these elements.
2340 * "dim" is the offset in the variables of "tab" where we should
2341 * start considering the elements in "list".
2342 * When this function returns, the total number of variables in "tab"
2343 * is equal to "dim" plus the number of elements in "list".
2345 * The newly added existentially quantified variables are not given
2346 * an explicit representation because the corresponding div constraints
2347 * do not appear in info->bmap. These constraints are not added
2348 * to info->bmap because for internal consistency, they would need to
2349 * be added to info->tab as well, where they could combine with the equality
2350 * that is added later to result in constraints that do not hold
2351 * in the original input.
2353 static int add_sub_vars(struct isl_coalesce_info *info,
2354 __isl_keep isl_aff_list *list, int dim, int extra_var)
2356 int i, j, n, d;
2357 isl_space *space;
2359 space = isl_basic_map_get_space(info->bmap);
2360 info->bmap = isl_basic_map_cow(info->bmap);
2361 info->bmap = isl_basic_map_extend_space(info->bmap, space,
2362 extra_var, 0, 0);
2363 if (!info->bmap)
2364 return -1;
2365 n = isl_aff_list_n_aff(list);
2366 for (i = 0; i < n; ++i) {
2367 int is_nan;
2368 isl_aff *aff;
2370 aff = isl_aff_list_get_aff(list, i);
2371 is_nan = isl_aff_is_nan(aff);
2372 isl_aff_free(aff);
2373 if (is_nan < 0)
2374 return -1;
2375 if (is_nan)
2376 continue;
2378 if (isl_tab_insert_var(info->tab, dim + i) < 0)
2379 return -1;
2380 d = isl_basic_map_alloc_div(info->bmap);
2381 if (d < 0)
2382 return -1;
2383 info->bmap = isl_basic_map_mark_div_unknown(info->bmap, d);
2384 if (!info->bmap)
2385 return -1;
2386 for (j = d; j > i; --j)
2387 isl_basic_map_swap_div(info->bmap, j - 1, j);
2390 return 0;
2393 /* For each element in "list" that is not set to NaN, fix the corresponding
2394 * variable in "tab" to the purely affine expression defined by the element.
2395 * "dim" is the offset in the variables of "tab" where we should
2396 * start considering the elements in "list".
2398 * This function assumes that a sufficient number of rows and
2399 * elements in the constraint array are available in the tableau.
2401 static int add_sub_equalities(struct isl_tab *tab,
2402 __isl_keep isl_aff_list *list, int dim)
2404 int i, n;
2405 isl_ctx *ctx;
2406 isl_vec *sub;
2407 isl_aff *aff;
2409 n = isl_aff_list_n_aff(list);
2411 ctx = isl_tab_get_ctx(tab);
2412 sub = isl_vec_alloc(ctx, 1 + dim + n);
2413 if (!sub)
2414 return -1;
2415 isl_seq_clr(sub->el + 1 + dim, n);
2417 for (i = 0; i < n; ++i) {
2418 aff = isl_aff_list_get_aff(list, i);
2419 if (!aff)
2420 goto error;
2421 if (isl_aff_is_nan(aff)) {
2422 isl_aff_free(aff);
2423 continue;
2425 isl_seq_cpy(sub->el, aff->v->el + 1, 1 + dim);
2426 isl_int_neg(sub->el[1 + dim + i], aff->v->el[0]);
2427 if (isl_tab_add_eq(tab, sub->el) < 0)
2428 goto error;
2429 isl_int_set_si(sub->el[1 + dim + i], 0);
2430 isl_aff_free(aff);
2433 isl_vec_free(sub);
2434 return 0;
2435 error:
2436 isl_aff_free(aff);
2437 isl_vec_free(sub);
2438 return -1;
2441 /* Add variables to info->tab and info->bmap corresponding to the elements
2442 * in "list" that are not set to NaN. The value of the added variable
2443 * in info->tab is fixed to the purely affine expression defined by the element.
2444 * "dim" is the offset in the variables of info->tab where we should
2445 * start considering the elements in "list".
2446 * When this function returns, the total number of variables in info->tab
2447 * is equal to "dim" plus the number of elements in "list".
2449 static int add_subs(struct isl_coalesce_info *info,
2450 __isl_keep isl_aff_list *list, int dim)
2452 int extra_var;
2453 int n;
2455 if (!list)
2456 return -1;
2458 n = isl_aff_list_n_aff(list);
2459 extra_var = n - (info->tab->n_var - dim);
2461 if (isl_tab_extend_vars(info->tab, extra_var) < 0)
2462 return -1;
2463 if (isl_tab_extend_cons(info->tab, 2 * extra_var) < 0)
2464 return -1;
2465 if (add_sub_vars(info, list, dim, extra_var) < 0)
2466 return -1;
2468 return add_sub_equalities(info->tab, list, dim);
2471 /* Coalesce basic map "j" into basic map "i" after adding the extra integer
2472 * divisions in "i" but not in "j" to basic map "j", with values
2473 * specified by "list". The total number of elements in "list"
2474 * is equal to the number of integer divisions in "i", while the number
2475 * of NaN elements in the list is equal to the number of integer divisions
2476 * in "j".
2478 * If no coalescing can be performed, then we need to revert basic map "j"
2479 * to its original state. We do the same if basic map "i" gets dropped
2480 * during the coalescing, even though this should not happen in practice
2481 * since we have already checked for "j" being a subset of "i"
2482 * before we reach this stage.
2484 static enum isl_change coalesce_with_subs(int i, int j,
2485 struct isl_coalesce_info *info, __isl_keep isl_aff_list *list)
2487 isl_basic_map *bmap_j;
2488 struct isl_tab_undo *snap;
2489 unsigned dim;
2490 enum isl_change change;
2492 bmap_j = isl_basic_map_copy(info[j].bmap);
2493 snap = isl_tab_snap(info[j].tab);
2495 dim = isl_basic_map_dim(bmap_j, isl_dim_all);
2496 dim -= isl_basic_map_dim(bmap_j, isl_dim_div);
2497 if (add_subs(&info[j], list, dim) < 0)
2498 goto error;
2500 change = coalesce_local_pair(i, j, info);
2501 if (change != isl_change_none && change != isl_change_drop_first) {
2502 isl_basic_map_free(bmap_j);
2503 } else {
2504 isl_basic_map_free(info[j].bmap);
2505 info[j].bmap = bmap_j;
2507 if (isl_tab_rollback(info[j].tab, snap) < 0)
2508 return isl_change_error;
2511 return change;
2512 error:
2513 isl_basic_map_free(bmap_j);
2514 return isl_change_error;
2517 /* Check if we can coalesce basic map "j" into basic map "i" after copying
2518 * those extra integer divisions in "i" that can be simplified away
2519 * using the extra equalities in "j".
2520 * All divs are assumed to be known and not contain any nested divs.
2522 * We first check if there are any extra equalities in "j" that we
2523 * can exploit. Then we check if every integer division in "i"
2524 * either already appears in "j" or can be simplified using the
2525 * extra equalities to a purely affine expression.
2526 * If these tests succeed, then we try to coalesce the two basic maps
2527 * by introducing extra dimensions in "j" corresponding to
2528 * the extra integer divsisions "i" fixed to the corresponding
2529 * purely affine expression.
2531 static enum isl_change check_coalesce_into_eq(int i, int j,
2532 struct isl_coalesce_info *info)
2534 unsigned n_div_i, n_div_j;
2535 isl_basic_map *hull_i, *hull_j;
2536 int equal, empty;
2537 isl_aff_list *list;
2538 enum isl_change change;
2540 n_div_i = isl_basic_map_dim(info[i].bmap, isl_dim_div);
2541 n_div_j = isl_basic_map_dim(info[j].bmap, isl_dim_div);
2542 if (n_div_i <= n_div_j)
2543 return isl_change_none;
2544 if (info[j].bmap->n_eq == 0)
2545 return isl_change_none;
2547 hull_i = isl_basic_map_copy(info[i].bmap);
2548 hull_i = isl_basic_map_plain_affine_hull(hull_i);
2549 hull_j = isl_basic_map_copy(info[j].bmap);
2550 hull_j = isl_basic_map_plain_affine_hull(hull_j);
2552 hull_j = isl_basic_map_intersect(hull_j, isl_basic_map_copy(hull_i));
2553 equal = isl_basic_map_plain_is_equal(hull_i, hull_j);
2554 empty = isl_basic_map_plain_is_empty(hull_j);
2555 isl_basic_map_free(hull_i);
2557 if (equal < 0 || empty < 0)
2558 goto error;
2559 if (equal || empty) {
2560 isl_basic_map_free(hull_j);
2561 return isl_change_none;
2564 list = set_up_substitutions(info[i].bmap, info[j].bmap, hull_j);
2565 if (!list)
2566 return isl_change_error;
2567 if (isl_aff_list_n_aff(list) < n_div_i)
2568 change = isl_change_none;
2569 else
2570 change = coalesce_with_subs(i, j, info, list);
2572 isl_aff_list_free(list);
2574 return change;
2575 error:
2576 isl_basic_map_free(hull_j);
2577 return isl_change_error;
2580 /* Check if we can coalesce basic maps "i" and "j" after copying
2581 * those extra integer divisions in one of the basic maps that can
2582 * be simplified away using the extra equalities in the other basic map.
2583 * We require all divs to be known in both basic maps.
2584 * Furthermore, to simplify the comparison of div expressions,
2585 * we do not allow any nested integer divisions.
2587 static enum isl_change check_coalesce_eq(int i, int j,
2588 struct isl_coalesce_info *info)
2590 int known, nested;
2591 enum isl_change change;
2593 known = isl_basic_map_divs_known(info[i].bmap);
2594 if (known < 0 || !known)
2595 return known < 0 ? isl_change_error : isl_change_none;
2596 known = isl_basic_map_divs_known(info[j].bmap);
2597 if (known < 0 || !known)
2598 return known < 0 ? isl_change_error : isl_change_none;
2599 nested = has_nested_div(info[i].bmap);
2600 if (nested < 0 || nested)
2601 return nested < 0 ? isl_change_error : isl_change_none;
2602 nested = has_nested_div(info[j].bmap);
2603 if (nested < 0 || nested)
2604 return nested < 0 ? isl_change_error : isl_change_none;
2606 change = check_coalesce_into_eq(i, j, info);
2607 if (change != isl_change_none)
2608 return change;
2609 change = check_coalesce_into_eq(j, i, info);
2610 if (change != isl_change_none)
2611 return invert_change(change);
2613 return isl_change_none;
2616 /* Check if the union of the given pair of basic maps
2617 * can be represented by a single basic map.
2618 * If so, replace the pair by the single basic map and return
2619 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
2620 * Otherwise, return isl_change_none.
2622 * We first check if the two basic maps live in the same local space,
2623 * after aligning the divs that differ by only an integer constant.
2624 * If so, we do the complete check. Otherwise, we check if they have
2625 * the same number of integer divisions and can be coalesced, if one is
2626 * an obvious subset of the other or if the extra integer divisions
2627 * of one basic map can be simplified away using the extra equalities
2628 * of the other basic map.
2630 static enum isl_change coalesce_pair(int i, int j,
2631 struct isl_coalesce_info *info)
2633 int same;
2634 enum isl_change change;
2636 if (harmonize_divs(&info[i], &info[j]) < 0)
2637 return isl_change_error;
2638 same = same_divs(info[i].bmap, info[j].bmap);
2639 if (same < 0)
2640 return isl_change_error;
2641 if (same)
2642 return coalesce_local_pair(i, j, info);
2644 if (info[i].bmap->n_div == info[j].bmap->n_div) {
2645 change = coalesce_local_pair(i, j, info);
2646 if (change != isl_change_none)
2647 return change;
2650 change = coalesce_divs(i, j, info);
2651 if (change != isl_change_none)
2652 return change;
2654 return check_coalesce_eq(i, j, info);
2657 /* Return the maximum of "a" and "b".
2659 static int isl_max(int a, int b)
2661 return a > b ? a : b;
2664 /* Pairwise coalesce the basic maps in the range [start1, end1[ of "info"
2665 * with those in the range [start2, end2[, skipping basic maps
2666 * that have been removed (either before or within this function).
2668 * For each basic map i in the first range, we check if it can be coalesced
2669 * with respect to any previously considered basic map j in the second range.
2670 * If i gets dropped (because it was a subset of some j), then
2671 * we can move on to the next basic map.
2672 * If j gets dropped, we need to continue checking against the other
2673 * previously considered basic maps.
2674 * If the two basic maps got fused, then we recheck the fused basic map
2675 * against the previously considered basic maps, starting at i + 1
2676 * (even if start2 is greater than i + 1).
2678 static int coalesce_range(isl_ctx *ctx, struct isl_coalesce_info *info,
2679 int start1, int end1, int start2, int end2)
2681 int i, j;
2683 for (i = end1 - 1; i >= start1; --i) {
2684 if (info[i].removed)
2685 continue;
2686 for (j = isl_max(i + 1, start2); j < end2; ++j) {
2687 enum isl_change changed;
2689 if (info[j].removed)
2690 continue;
2691 if (info[i].removed)
2692 isl_die(ctx, isl_error_internal,
2693 "basic map unexpectedly removed",
2694 return -1);
2695 changed = coalesce_pair(i, j, info);
2696 switch (changed) {
2697 case isl_change_error:
2698 return -1;
2699 case isl_change_none:
2700 case isl_change_drop_second:
2701 continue;
2702 case isl_change_drop_first:
2703 j = end2;
2704 break;
2705 case isl_change_fuse:
2706 j = i;
2707 break;
2712 return 0;
2715 /* Pairwise coalesce the basic maps described by the "n" elements of "info".
2717 * We consider groups of basic maps that live in the same apparent
2718 * affine hull and we first coalesce within such a group before we
2719 * coalesce the elements in the group with elements of previously
2720 * considered groups. If a fuse happens during the second phase,
2721 * then we also reconsider the elements within the group.
2723 static int coalesce(isl_ctx *ctx, int n, struct isl_coalesce_info *info)
2725 int start, end;
2727 for (end = n; end > 0; end = start) {
2728 start = end - 1;
2729 while (start >= 1 &&
2730 info[start - 1].hull_hash == info[start].hull_hash)
2731 start--;
2732 if (coalesce_range(ctx, info, start, end, start, end) < 0)
2733 return -1;
2734 if (coalesce_range(ctx, info, start, end, end, n) < 0)
2735 return -1;
2738 return 0;
2741 /* Update the basic maps in "map" based on the information in "info".
2742 * In particular, remove the basic maps that have been marked removed and
2743 * update the others based on the information in the corresponding tableau.
2744 * Since we detected implicit equalities without calling
2745 * isl_basic_map_gauss, we need to do it now.
2746 * Also call isl_basic_map_simplify if we may have lost the definition
2747 * of one or more integer divisions.
2749 static __isl_give isl_map *update_basic_maps(__isl_take isl_map *map,
2750 int n, struct isl_coalesce_info *info)
2752 int i;
2754 if (!map)
2755 return NULL;
2757 for (i = n - 1; i >= 0; --i) {
2758 if (info[i].removed) {
2759 isl_basic_map_free(map->p[i]);
2760 if (i != map->n - 1)
2761 map->p[i] = map->p[map->n - 1];
2762 map->n--;
2763 continue;
2766 info[i].bmap = isl_basic_map_update_from_tab(info[i].bmap,
2767 info[i].tab);
2768 info[i].bmap = isl_basic_map_gauss(info[i].bmap, NULL);
2769 if (info[i].simplify)
2770 info[i].bmap = isl_basic_map_simplify(info[i].bmap);
2771 info[i].bmap = isl_basic_map_finalize(info[i].bmap);
2772 if (!info[i].bmap)
2773 return isl_map_free(map);
2774 ISL_F_SET(info[i].bmap, ISL_BASIC_MAP_NO_IMPLICIT);
2775 ISL_F_SET(info[i].bmap, ISL_BASIC_MAP_NO_REDUNDANT);
2776 isl_basic_map_free(map->p[i]);
2777 map->p[i] = info[i].bmap;
2778 info[i].bmap = NULL;
2781 return map;
2784 /* For each pair of basic maps in the map, check if the union of the two
2785 * can be represented by a single basic map.
2786 * If so, replace the pair by the single basic map and start over.
2788 * We factor out any (hidden) common factor from the constraint
2789 * coefficients to improve the detection of adjacent constraints.
2791 * Since we are constructing the tableaus of the basic maps anyway,
2792 * we exploit them to detect implicit equalities and redundant constraints.
2793 * This also helps the coalescing as it can ignore the redundant constraints.
2794 * In order to avoid confusion, we make all implicit equalities explicit
2795 * in the basic maps. We don't call isl_basic_map_gauss, though,
2796 * as that may affect the number of constraints.
2797 * This means that we have to call isl_basic_map_gauss at the end
2798 * of the computation (in update_basic_maps) to ensure that
2799 * the basic maps are not left in an unexpected state.
2800 * For each basic map, we also compute the hash of the apparent affine hull
2801 * for use in coalesce.
2803 struct isl_map *isl_map_coalesce(struct isl_map *map)
2805 int i;
2806 unsigned n;
2807 isl_ctx *ctx;
2808 struct isl_coalesce_info *info = NULL;
2810 map = isl_map_remove_empty_parts(map);
2811 if (!map)
2812 return NULL;
2814 if (map->n <= 1)
2815 return map;
2817 ctx = isl_map_get_ctx(map);
2818 map = isl_map_sort_divs(map);
2819 map = isl_map_cow(map);
2821 if (!map)
2822 return NULL;
2824 n = map->n;
2826 info = isl_calloc_array(map->ctx, struct isl_coalesce_info, n);
2827 if (!info)
2828 goto error;
2830 for (i = 0; i < map->n; ++i) {
2831 map->p[i] = isl_basic_map_reduce_coefficients(map->p[i]);
2832 if (!map->p[i])
2833 goto error;
2834 info[i].bmap = isl_basic_map_copy(map->p[i]);
2835 info[i].tab = isl_tab_from_basic_map(info[i].bmap, 0);
2836 if (!info[i].tab)
2837 goto error;
2838 if (!ISL_F_ISSET(info[i].bmap, ISL_BASIC_MAP_NO_IMPLICIT))
2839 if (isl_tab_detect_implicit_equalities(info[i].tab) < 0)
2840 goto error;
2841 info[i].bmap = isl_tab_make_equalities_explicit(info[i].tab,
2842 info[i].bmap);
2843 if (!info[i].bmap)
2844 goto error;
2845 if (!ISL_F_ISSET(info[i].bmap, ISL_BASIC_MAP_NO_REDUNDANT))
2846 if (isl_tab_detect_redundant(info[i].tab) < 0)
2847 goto error;
2848 if (coalesce_info_set_hull_hash(&info[i]) < 0)
2849 goto error;
2851 for (i = map->n - 1; i >= 0; --i)
2852 if (info[i].tab->empty)
2853 drop(&info[i]);
2855 if (coalesce(ctx, n, info) < 0)
2856 goto error;
2858 map = update_basic_maps(map, n, info);
2860 clear_coalesce_info(n, info);
2862 return map;
2863 error:
2864 clear_coalesce_info(n, info);
2865 isl_map_free(map);
2866 return NULL;
2869 /* For each pair of basic sets in the set, check if the union of the two
2870 * can be represented by a single basic set.
2871 * If so, replace the pair by the single basic set and start over.
2873 struct isl_set *isl_set_coalesce(struct isl_set *set)
2875 return (struct isl_set *)isl_map_coalesce((struct isl_map *)set);