add isl_schedule_intersect_domain
[isl.git] / isl_coalesce.c
blob2ebe600ff6f31a77dc3fd63dd0a90000f8e3b063
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2012-2013 Ecole Normale Superieure
5 * Copyright 2014 INRIA Rocquencourt
7 * Use of this software is governed by the MIT license
9 * Written by Sven Verdoolaege, K.U.Leuven, Departement
10 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
11 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
12 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
13 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
14 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
15 * B.P. 105 - 78153 Le Chesnay, France
18 #include "isl_map_private.h"
19 #include <isl_seq.h>
20 #include <isl/options.h>
21 #include "isl_tab.h"
22 #include <isl_mat_private.h>
23 #include <isl_local_space_private.h>
24 #include <isl_vec_private.h>
25 #include <isl_aff_private.h>
27 #define STATUS_ERROR -1
28 #define STATUS_REDUNDANT 1
29 #define STATUS_VALID 2
30 #define STATUS_SEPARATE 3
31 #define STATUS_CUT 4
32 #define STATUS_ADJ_EQ 5
33 #define STATUS_ADJ_INEQ 6
35 static int status_in(isl_int *ineq, struct isl_tab *tab)
37 enum isl_ineq_type type = isl_tab_ineq_type(tab, ineq);
38 switch (type) {
39 default:
40 case isl_ineq_error: return STATUS_ERROR;
41 case isl_ineq_redundant: return STATUS_VALID;
42 case isl_ineq_separate: return STATUS_SEPARATE;
43 case isl_ineq_cut: return STATUS_CUT;
44 case isl_ineq_adj_eq: return STATUS_ADJ_EQ;
45 case isl_ineq_adj_ineq: return STATUS_ADJ_INEQ;
49 /* Compute the position of the equalities of basic map "bmap_i"
50 * with respect to the basic map represented by "tab_j".
51 * The resulting array has twice as many entries as the number
52 * of equalities corresponding to the two inequalties to which
53 * each equality corresponds.
55 static int *eq_status_in(__isl_keep isl_basic_map *bmap_i,
56 struct isl_tab *tab_j)
58 int k, l;
59 int *eq = isl_calloc_array(bmap_i->ctx, int, 2 * bmap_i->n_eq);
60 unsigned dim;
62 if (!eq)
63 return NULL;
65 dim = isl_basic_map_total_dim(bmap_i);
66 for (k = 0; k < bmap_i->n_eq; ++k) {
67 for (l = 0; l < 2; ++l) {
68 isl_seq_neg(bmap_i->eq[k], bmap_i->eq[k], 1+dim);
69 eq[2 * k + l] = status_in(bmap_i->eq[k], tab_j);
70 if (eq[2 * k + l] == STATUS_ERROR)
71 goto error;
73 if (eq[2 * k] == STATUS_SEPARATE ||
74 eq[2 * k + 1] == STATUS_SEPARATE)
75 break;
78 return eq;
79 error:
80 free(eq);
81 return NULL;
84 /* Compute the position of the inequalities of basic map "bmap_i"
85 * (also represented by "tab_i", if not NULL) with respect to the basic map
86 * represented by "tab_j".
88 static int *ineq_status_in(__isl_keep isl_basic_map *bmap_i,
89 struct isl_tab *tab_i, struct isl_tab *tab_j)
91 int k;
92 unsigned n_eq = bmap_i->n_eq;
93 int *ineq = isl_calloc_array(bmap_i->ctx, int, bmap_i->n_ineq);
95 if (!ineq)
96 return NULL;
98 for (k = 0; k < bmap_i->n_ineq; ++k) {
99 if (tab_i && isl_tab_is_redundant(tab_i, n_eq + k)) {
100 ineq[k] = STATUS_REDUNDANT;
101 continue;
103 ineq[k] = status_in(bmap_i->ineq[k], tab_j);
104 if (ineq[k] == STATUS_ERROR)
105 goto error;
106 if (ineq[k] == STATUS_SEPARATE)
107 break;
110 return ineq;
111 error:
112 free(ineq);
113 return NULL;
116 static int any(int *con, unsigned len, int status)
118 int i;
120 for (i = 0; i < len ; ++i)
121 if (con[i] == status)
122 return 1;
123 return 0;
126 static int count(int *con, unsigned len, int status)
128 int i;
129 int c = 0;
131 for (i = 0; i < len ; ++i)
132 if (con[i] == status)
133 c++;
134 return c;
137 static int all(int *con, unsigned len, int status)
139 int i;
141 for (i = 0; i < len ; ++i) {
142 if (con[i] == STATUS_REDUNDANT)
143 continue;
144 if (con[i] != status)
145 return 0;
147 return 1;
150 /* Internal information associated to a basic map in a map
151 * that is to be coalesced by isl_map_coalesce.
153 * "bmap" is the basic map itself (or NULL if "removed" is set)
154 * "tab" is the corresponding tableau (or NULL if "removed" is set)
155 * "hull_hash" identifies the affine space in which "bmap" lives.
156 * "removed" is set if this basic map has been removed from the map
157 * "simplify" is set if this basic map may have some unknown integer
158 * divisions that were not present in the input basic maps. The basic
159 * map should then be simplified such that we may be able to find
160 * a definition among the constraints.
162 * "eq" and "ineq" are only set if we are currently trying to coalesce
163 * this basic map with another basic map, in which case they represent
164 * the position of the inequalities of this basic map with respect to
165 * the other basic map. The number of elements in the "eq" array
166 * is twice the number of equalities in the "bmap", corresponding
167 * to the two inequalities that make up each equality.
169 struct isl_coalesce_info {
170 isl_basic_map *bmap;
171 struct isl_tab *tab;
172 uint32_t hull_hash;
173 int removed;
174 int simplify;
175 int *eq;
176 int *ineq;
179 /* Compute the hash of the (apparent) affine hull of info->bmap (with
180 * the existentially quantified variables removed) and store it
181 * in info->hash.
183 static int coalesce_info_set_hull_hash(struct isl_coalesce_info *info)
185 isl_basic_map *hull;
186 unsigned n_div;
188 hull = isl_basic_map_copy(info->bmap);
189 hull = isl_basic_map_plain_affine_hull(hull);
190 n_div = isl_basic_map_dim(hull, isl_dim_div);
191 hull = isl_basic_map_drop_constraints_involving_dims(hull,
192 isl_dim_div, 0, n_div);
193 info->hull_hash = isl_basic_map_get_hash(hull);
194 isl_basic_map_free(hull);
196 return hull ? 0 : -1;
199 /* Free all the allocated memory in an array
200 * of "n" isl_coalesce_info elements.
202 static void clear_coalesce_info(int n, struct isl_coalesce_info *info)
204 int i;
206 if (!info)
207 return;
209 for (i = 0; i < n; ++i) {
210 isl_basic_map_free(info[i].bmap);
211 isl_tab_free(info[i].tab);
214 free(info);
217 /* Drop the basic map represented by "info".
218 * That is, clear the memory associated to the entry and
219 * mark it as having been removed.
221 static void drop(struct isl_coalesce_info *info)
223 info->bmap = isl_basic_map_free(info->bmap);
224 isl_tab_free(info->tab);
225 info->tab = NULL;
226 info->removed = 1;
229 /* Exchange the information in "info1" with that in "info2".
231 static void exchange(struct isl_coalesce_info *info1,
232 struct isl_coalesce_info *info2)
234 struct isl_coalesce_info info;
236 info = *info1;
237 *info1 = *info2;
238 *info2 = info;
241 /* This type represents the kind of change that has been performed
242 * while trying to coalesce two basic maps.
244 * isl_change_none: nothing was changed
245 * isl_change_drop_first: the first basic map was removed
246 * isl_change_drop_second: the second basic map was removed
247 * isl_change_fuse: the two basic maps were replaced by a new basic map.
249 enum isl_change {
250 isl_change_error = -1,
251 isl_change_none = 0,
252 isl_change_drop_first,
253 isl_change_drop_second,
254 isl_change_fuse,
257 /* Update "change" based on an interchange of the first and the second
258 * basic map. That is, interchange isl_change_drop_first and
259 * isl_change_drop_second.
261 static enum isl_change invert_change(enum isl_change change)
263 switch (change) {
264 case isl_change_error:
265 return isl_change_error;
266 case isl_change_none:
267 return isl_change_none;
268 case isl_change_drop_first:
269 return isl_change_drop_second;
270 case isl_change_drop_second:
271 return isl_change_drop_first;
272 case isl_change_fuse:
273 return isl_change_fuse;
277 /* Add the valid constraints of the basic map represented by "info"
278 * to "bmap". "len" is the size of the constraints.
279 * If only one of the pair of inequalities that make up an equality
280 * is valid, then add that inequality.
282 static __isl_give isl_basic_map *add_valid_constraints(
283 __isl_take isl_basic_map *bmap, struct isl_coalesce_info *info,
284 unsigned len)
286 int k, l;
288 if (!bmap)
289 return NULL;
291 for (k = 0; k < info->bmap->n_eq; ++k) {
292 if (info->eq[2 * k] == STATUS_VALID &&
293 info->eq[2 * k + 1] == STATUS_VALID) {
294 l = isl_basic_map_alloc_equality(bmap);
295 if (l < 0)
296 return isl_basic_map_free(bmap);
297 isl_seq_cpy(bmap->eq[l], info->bmap->eq[k], len);
298 } else if (info->eq[2 * k] == STATUS_VALID) {
299 l = isl_basic_map_alloc_inequality(bmap);
300 if (l < 0)
301 return isl_basic_map_free(bmap);
302 isl_seq_neg(bmap->ineq[l], info->bmap->eq[k], len);
303 } else if (info->eq[2 * k + 1] == STATUS_VALID) {
304 l = isl_basic_map_alloc_inequality(bmap);
305 if (l < 0)
306 return isl_basic_map_free(bmap);
307 isl_seq_cpy(bmap->ineq[l], info->bmap->eq[k], len);
311 for (k = 0; k < info->bmap->n_ineq; ++k) {
312 if (info->ineq[k] != STATUS_VALID)
313 continue;
314 l = isl_basic_map_alloc_inequality(bmap);
315 if (l < 0)
316 return isl_basic_map_free(bmap);
317 isl_seq_cpy(bmap->ineq[l], info->bmap->ineq[k], len);
320 return bmap;
323 /* Is "bmap" defined by a number of (non-redundant) constraints that
324 * is greater than the number of constraints of basic maps i and j combined?
325 * Equalities are counted as two inequalities.
327 static int number_of_constraints_increases(int i, int j,
328 struct isl_coalesce_info *info,
329 __isl_keep isl_basic_map *bmap, struct isl_tab *tab)
331 int k, n_old, n_new;
333 n_old = 2 * info[i].bmap->n_eq + info[i].bmap->n_ineq;
334 n_old += 2 * info[j].bmap->n_eq + info[j].bmap->n_ineq;
336 n_new = 2 * bmap->n_eq;
337 for (k = 0; k < bmap->n_ineq; ++k)
338 if (!isl_tab_is_redundant(tab, bmap->n_eq + k))
339 ++n_new;
341 return n_new > n_old;
344 /* Replace the pair of basic maps i and j by the basic map bounded
345 * by the valid constraints in both basic maps and the constraints
346 * in extra (if not NULL).
347 * Place the fused basic map in the position that is the smallest of i and j.
349 * If "detect_equalities" is set, then look for equalities encoded
350 * as pairs of inequalities.
351 * If "check_number" is set, then the original basic maps are only
352 * replaced if the total number of constraints does not increase.
353 * While the number of integer divisions in the two basic maps
354 * is assumed to be the same, the actual definitions may be different.
355 * We only copy the definition from one of the basic map if it is
356 * the same as that of the other basic map. Otherwise, we mark
357 * the integer division as unknown and schedule for the basic map
358 * to be simplified in an attempt to recover the integer division definition.
360 static enum isl_change fuse(int i, int j, struct isl_coalesce_info *info,
361 __isl_keep isl_mat *extra, int detect_equalities, int check_number)
363 int k, l;
364 struct isl_basic_map *fused = NULL;
365 struct isl_tab *fused_tab = NULL;
366 unsigned total = isl_basic_map_total_dim(info[i].bmap);
367 unsigned extra_rows = extra ? extra->n_row : 0;
368 unsigned n_eq, n_ineq;
370 if (j < i)
371 return fuse(j, i, info, extra, detect_equalities, check_number);
373 n_eq = info[i].bmap->n_eq + info[j].bmap->n_eq;
374 n_ineq = info[i].bmap->n_ineq + info[j].bmap->n_ineq;
375 fused = isl_basic_map_alloc_space(isl_space_copy(info[i].bmap->dim),
376 info[i].bmap->n_div, n_eq, n_eq + n_ineq + extra_rows);
377 fused = add_valid_constraints(fused, &info[i], 1 + total);
378 fused = add_valid_constraints(fused, &info[j], 1 + total);
379 if (!fused)
380 goto error;
382 for (k = 0; k < info[i].bmap->n_div; ++k) {
383 int l = isl_basic_map_alloc_div(fused);
384 if (l < 0)
385 goto error;
386 if (isl_seq_eq(info[i].bmap->div[k], info[j].bmap->div[k],
387 1 + 1 + total)) {
388 isl_seq_cpy(fused->div[l], info[i].bmap->div[k],
389 1 + 1 + total);
390 } else {
391 isl_int_set_si(fused->div[l][0], 0);
392 info[i].simplify = 1;
396 for (k = 0; k < extra_rows; ++k) {
397 l = isl_basic_map_alloc_inequality(fused);
398 if (l < 0)
399 goto error;
400 isl_seq_cpy(fused->ineq[l], extra->row[k], 1 + total);
403 if (detect_equalities)
404 fused = isl_basic_map_detect_inequality_pairs(fused, NULL);
405 fused = isl_basic_map_gauss(fused, NULL);
406 ISL_F_SET(fused, ISL_BASIC_MAP_FINAL);
407 if (ISL_F_ISSET(info[i].bmap, ISL_BASIC_MAP_RATIONAL) &&
408 ISL_F_ISSET(info[j].bmap, ISL_BASIC_MAP_RATIONAL))
409 ISL_F_SET(fused, ISL_BASIC_MAP_RATIONAL);
411 fused_tab = isl_tab_from_basic_map(fused, 0);
412 if (isl_tab_detect_redundant(fused_tab) < 0)
413 goto error;
415 if (check_number &&
416 number_of_constraints_increases(i, j, info, fused, fused_tab)) {
417 isl_tab_free(fused_tab);
418 isl_basic_map_free(fused);
419 return isl_change_none;
422 info[i].simplify |= info[j].simplify;
423 isl_basic_map_free(info[i].bmap);
424 info[i].bmap = fused;
425 isl_tab_free(info[i].tab);
426 info[i].tab = fused_tab;
427 drop(&info[j]);
429 return isl_change_fuse;
430 error:
431 isl_tab_free(fused_tab);
432 isl_basic_map_free(fused);
433 return isl_change_error;
436 /* Given a pair of basic maps i and j such that all constraints are either
437 * "valid" or "cut", check if the facets corresponding to the "cut"
438 * constraints of i lie entirely within basic map j.
439 * If so, replace the pair by the basic map consisting of the valid
440 * constraints in both basic maps.
441 * Checking whether the facet lies entirely within basic map j
442 * is performed by checking whether the constraints of basic map j
443 * are valid for the facet. These tests are performed on a rational
444 * tableau to avoid the theoretical possibility that a constraint
445 * that was considered to be a cut constraint for the entire basic map i
446 * happens to be considered to be a valid constraint for the facet,
447 * even though it cuts off the same rational points.
449 * To see that we are not introducing any extra points, call the
450 * two basic maps A and B and the resulting map U and let x
451 * be an element of U \setminus ( A \cup B ).
452 * A line connecting x with an element of A \cup B meets a facet F
453 * of either A or B. Assume it is a facet of B and let c_1 be
454 * the corresponding facet constraint. We have c_1(x) < 0 and
455 * so c_1 is a cut constraint. This implies that there is some
456 * (possibly rational) point x' satisfying the constraints of A
457 * and the opposite of c_1 as otherwise c_1 would have been marked
458 * valid for A. The line connecting x and x' meets a facet of A
459 * in a (possibly rational) point that also violates c_1, but this
460 * is impossible since all cut constraints of B are valid for all
461 * cut facets of A.
462 * In case F is a facet of A rather than B, then we can apply the
463 * above reasoning to find a facet of B separating x from A \cup B first.
465 static enum isl_change check_facets(int i, int j,
466 struct isl_coalesce_info *info)
468 int k, l;
469 struct isl_tab_undo *snap, *snap2;
470 unsigned n_eq = info[i].bmap->n_eq;
472 snap = isl_tab_snap(info[i].tab);
473 if (isl_tab_mark_rational(info[i].tab) < 0)
474 return isl_change_error;
475 snap2 = isl_tab_snap(info[i].tab);
477 for (k = 0; k < info[i].bmap->n_ineq; ++k) {
478 if (info[i].ineq[k] != STATUS_CUT)
479 continue;
480 if (isl_tab_select_facet(info[i].tab, n_eq + k) < 0)
481 return isl_change_error;
482 for (l = 0; l < info[j].bmap->n_ineq; ++l) {
483 int stat;
484 if (info[j].ineq[l] != STATUS_CUT)
485 continue;
486 stat = status_in(info[j].bmap->ineq[l], info[i].tab);
487 if (stat != STATUS_VALID)
488 break;
490 if (isl_tab_rollback(info[i].tab, snap2) < 0)
491 return isl_change_error;
492 if (l < info[j].bmap->n_ineq)
493 break;
496 if (k < info[i].bmap->n_ineq) {
497 if (isl_tab_rollback(info[i].tab, snap) < 0)
498 return isl_change_error;
499 return isl_change_none;
501 return fuse(i, j, info, NULL, 0, 0);
504 /* Check if info->bmap contains the basic map represented
505 * by the tableau "tab".
506 * For each equality, we check both the constraint itself
507 * (as an inequality) and its negation. Make sure the
508 * equality is returned to its original state before returning.
510 static int contains(struct isl_coalesce_info *info, struct isl_tab *tab)
512 int k;
513 unsigned dim;
514 isl_basic_map *bmap = info->bmap;
516 dim = isl_basic_map_total_dim(bmap);
517 for (k = 0; k < bmap->n_eq; ++k) {
518 int stat;
519 isl_seq_neg(bmap->eq[k], bmap->eq[k], 1 + dim);
520 stat = status_in(bmap->eq[k], tab);
521 isl_seq_neg(bmap->eq[k], bmap->eq[k], 1 + dim);
522 if (stat != STATUS_VALID)
523 return 0;
524 stat = status_in(bmap->eq[k], tab);
525 if (stat != STATUS_VALID)
526 return 0;
529 for (k = 0; k < bmap->n_ineq; ++k) {
530 int stat;
531 if (info->ineq[k] == STATUS_REDUNDANT)
532 continue;
533 stat = status_in(bmap->ineq[k], tab);
534 if (stat != STATUS_VALID)
535 return 0;
537 return 1;
540 /* Basic map "i" has an inequality (say "k") that is adjacent
541 * to some inequality of basic map "j". All the other inequalities
542 * are valid for "j".
543 * Check if basic map "j" forms an extension of basic map "i".
545 * Note that this function is only called if some of the equalities or
546 * inequalities of basic map "j" do cut basic map "i". The function is
547 * correct even if there are no such cut constraints, but in that case
548 * the additional checks performed by this function are overkill.
550 * In particular, we replace constraint k, say f >= 0, by constraint
551 * f <= -1, add the inequalities of "j" that are valid for "i"
552 * and check if the result is a subset of basic map "j".
553 * If so, then we know that this result is exactly equal to basic map "j"
554 * since all its constraints are valid for basic map "j".
555 * By combining the valid constraints of "i" (all equalities and all
556 * inequalities except "k") and the valid constraints of "j" we therefore
557 * obtain a basic map that is equal to their union.
558 * In this case, there is no need to perform a rollback of the tableau
559 * since it is going to be destroyed in fuse().
562 * |\__ |\__
563 * | \__ | \__
564 * | \_ => | \__
565 * |_______| _ |_________\
568 * |\ |\
569 * | \ | \
570 * | \ | \
571 * | | | \
572 * | ||\ => | \
573 * | || \ | \
574 * | || | | |
575 * |__||_/ |_____/
577 static enum isl_change is_adj_ineq_extension(int i, int j,
578 struct isl_coalesce_info *info)
580 int k;
581 struct isl_tab_undo *snap;
582 unsigned n_eq = info[i].bmap->n_eq;
583 unsigned total = isl_basic_map_total_dim(info[i].bmap);
584 int r;
586 if (isl_tab_extend_cons(info[i].tab, 1 + info[j].bmap->n_ineq) < 0)
587 return isl_change_error;
589 for (k = 0; k < info[i].bmap->n_ineq; ++k)
590 if (info[i].ineq[k] == STATUS_ADJ_INEQ)
591 break;
592 if (k >= info[i].bmap->n_ineq)
593 isl_die(isl_basic_map_get_ctx(info[i].bmap), isl_error_internal,
594 "info[i].ineq should have exactly one STATUS_ADJ_INEQ",
595 return isl_change_error);
597 snap = isl_tab_snap(info[i].tab);
599 if (isl_tab_unrestrict(info[i].tab, n_eq + k) < 0)
600 return isl_change_error;
602 isl_seq_neg(info[i].bmap->ineq[k], info[i].bmap->ineq[k], 1 + total);
603 isl_int_sub_ui(info[i].bmap->ineq[k][0], info[i].bmap->ineq[k][0], 1);
604 r = isl_tab_add_ineq(info[i].tab, info[i].bmap->ineq[k]);
605 isl_seq_neg(info[i].bmap->ineq[k], info[i].bmap->ineq[k], 1 + total);
606 isl_int_sub_ui(info[i].bmap->ineq[k][0], info[i].bmap->ineq[k][0], 1);
607 if (r < 0)
608 return isl_change_error;
610 for (k = 0; k < info[j].bmap->n_ineq; ++k) {
611 if (info[j].ineq[k] != STATUS_VALID)
612 continue;
613 if (isl_tab_add_ineq(info[i].tab, info[j].bmap->ineq[k]) < 0)
614 return isl_change_error;
617 if (contains(&info[j], info[i].tab))
618 return fuse(i, j, info, NULL, 0, 0);
620 if (isl_tab_rollback(info[i].tab, snap) < 0)
621 return isl_change_error;
623 return isl_change_none;
627 /* Both basic maps have at least one inequality with and adjacent
628 * (but opposite) inequality in the other basic map.
629 * Check that there are no cut constraints and that there is only
630 * a single pair of adjacent inequalities.
631 * If so, we can replace the pair by a single basic map described
632 * by all but the pair of adjacent inequalities.
633 * Any additional points introduced lie strictly between the two
634 * adjacent hyperplanes and can therefore be integral.
636 * ____ _____
637 * / ||\ / \
638 * / || \ / \
639 * \ || \ => \ \
640 * \ || / \ /
641 * \___||_/ \_____/
643 * The test for a single pair of adjancent inequalities is important
644 * for avoiding the combination of two basic maps like the following
646 * /|
647 * / |
648 * /__|
649 * _____
650 * | |
651 * | |
652 * |___|
654 * If there are some cut constraints on one side, then we may
655 * still be able to fuse the two basic maps, but we need to perform
656 * some additional checks in is_adj_ineq_extension.
658 static enum isl_change check_adj_ineq(int i, int j,
659 struct isl_coalesce_info *info)
661 int count_i, count_j;
662 int cut_i, cut_j;
664 count_i = count(info[i].ineq, info[i].bmap->n_ineq, STATUS_ADJ_INEQ);
665 count_j = count(info[j].ineq, info[j].bmap->n_ineq, STATUS_ADJ_INEQ);
667 if (count_i != 1 && count_j != 1)
668 return isl_change_none;
670 cut_i = any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_CUT) ||
671 any(info[i].ineq, info[i].bmap->n_ineq, STATUS_CUT);
672 cut_j = any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_CUT) ||
673 any(info[j].ineq, info[j].bmap->n_ineq, STATUS_CUT);
675 if (!cut_i && !cut_j && count_i == 1 && count_j == 1)
676 return fuse(i, j, info, NULL, 0, 0);
678 if (count_i == 1 && !cut_i)
679 return is_adj_ineq_extension(i, j, info);
681 if (count_j == 1 && !cut_j)
682 return is_adj_ineq_extension(j, i, info);
684 return isl_change_none;
687 /* Basic map "i" has an inequality "k" that is adjacent to some equality
688 * of basic map "j". All the other inequalities are valid for "j".
689 * Check if basic map "j" forms an extension of basic map "i".
691 * In particular, we relax constraint "k", compute the corresponding
692 * facet and check whether it is included in the other basic map.
693 * If so, we know that relaxing the constraint extends the basic
694 * map with exactly the other basic map (we already know that this
695 * other basic map is included in the extension, because there
696 * were no "cut" inequalities in "i") and we can replace the
697 * two basic maps by this extension.
698 * Each integer division that does not have exactly the same
699 * definition in "i" and "j" is marked unknown and the basic map
700 * is scheduled to be simplified in an attempt to recover
701 * the integer division definition.
702 * Place this extension in the position that is the smallest of i and j.
703 * ____ _____
704 * / || / |
705 * / || / |
706 * \ || => \ |
707 * \ || \ |
708 * \___|| \____|
710 static enum isl_change is_adj_eq_extension(int i, int j, int k,
711 struct isl_coalesce_info *info)
713 int change = isl_change_none;
714 int super;
715 struct isl_tab_undo *snap, *snap2;
716 unsigned n_eq = info[i].bmap->n_eq;
718 if (isl_tab_is_equality(info[i].tab, n_eq + k))
719 return isl_change_none;
721 snap = isl_tab_snap(info[i].tab);
722 if (isl_tab_relax(info[i].tab, n_eq + k) < 0)
723 return isl_change_error;
724 snap2 = isl_tab_snap(info[i].tab);
725 if (isl_tab_select_facet(info[i].tab, n_eq + k) < 0)
726 return isl_change_error;
727 super = contains(&info[j], info[i].tab);
728 if (super) {
729 int l;
730 unsigned total;
732 if (isl_tab_rollback(info[i].tab, snap2) < 0)
733 return isl_change_error;
734 info[i].bmap = isl_basic_map_cow(info[i].bmap);
735 if (!info[i].bmap)
736 return isl_change_error;
737 total = isl_basic_map_total_dim(info[i].bmap);
738 for (l = 0; l < info[i].bmap->n_div; ++l)
739 if (!isl_seq_eq(info[i].bmap->div[l],
740 info[j].bmap->div[l], 1 + 1 + total)) {
741 isl_int_set_si(info[i].bmap->div[l][0], 0);
742 info[i].simplify = 1;
744 isl_int_add_ui(info[i].bmap->ineq[k][0],
745 info[i].bmap->ineq[k][0], 1);
746 ISL_F_SET(info[i].bmap, ISL_BASIC_MAP_FINAL);
747 drop(&info[j]);
748 if (j < i)
749 exchange(&info[i], &info[j]);
750 change = isl_change_fuse;
751 } else
752 if (isl_tab_rollback(info[i].tab, snap) < 0)
753 return isl_change_error;
755 return change;
758 /* Data structure that keeps track of the wrapping constraints
759 * and of information to bound the coefficients of those constraints.
761 * bound is set if we want to apply a bound on the coefficients
762 * mat contains the wrapping constraints
763 * max is the bound on the coefficients (if bound is set)
765 struct isl_wraps {
766 int bound;
767 isl_mat *mat;
768 isl_int max;
771 /* Update wraps->max to be greater than or equal to the coefficients
772 * in the equalities and inequalities of info->bmap that can be removed
773 * if we end up applying wrapping.
775 static void wraps_update_max(struct isl_wraps *wraps,
776 struct isl_coalesce_info *info)
778 int k;
779 isl_int max_k;
780 unsigned total = isl_basic_map_total_dim(info->bmap);
782 isl_int_init(max_k);
784 for (k = 0; k < info->bmap->n_eq; ++k) {
785 if (info->eq[2 * k] == STATUS_VALID &&
786 info->eq[2 * k + 1] == STATUS_VALID)
787 continue;
788 isl_seq_abs_max(info->bmap->eq[k] + 1, total, &max_k);
789 if (isl_int_abs_gt(max_k, wraps->max))
790 isl_int_set(wraps->max, max_k);
793 for (k = 0; k < info->bmap->n_ineq; ++k) {
794 if (info->ineq[k] == STATUS_VALID ||
795 info->ineq[k] == STATUS_REDUNDANT)
796 continue;
797 isl_seq_abs_max(info->bmap->ineq[k] + 1, total, &max_k);
798 if (isl_int_abs_gt(max_k, wraps->max))
799 isl_int_set(wraps->max, max_k);
802 isl_int_clear(max_k);
805 /* Initialize the isl_wraps data structure.
806 * If we want to bound the coefficients of the wrapping constraints,
807 * we set wraps->max to the largest coefficient
808 * in the equalities and inequalities that can be removed if we end up
809 * applying wrapping.
811 static void wraps_init(struct isl_wraps *wraps, __isl_take isl_mat *mat,
812 struct isl_coalesce_info *info, int i, int j)
814 isl_ctx *ctx;
816 wraps->bound = 0;
817 wraps->mat = mat;
818 if (!mat)
819 return;
820 ctx = isl_mat_get_ctx(mat);
821 wraps->bound = isl_options_get_coalesce_bounded_wrapping(ctx);
822 if (!wraps->bound)
823 return;
824 isl_int_init(wraps->max);
825 isl_int_set_si(wraps->max, 0);
826 wraps_update_max(wraps, &info[i]);
827 wraps_update_max(wraps, &info[j]);
830 /* Free the contents of the isl_wraps data structure.
832 static void wraps_free(struct isl_wraps *wraps)
834 isl_mat_free(wraps->mat);
835 if (wraps->bound)
836 isl_int_clear(wraps->max);
839 /* Is the wrapping constraint in row "row" allowed?
841 * If wraps->bound is set, we check that none of the coefficients
842 * is greater than wraps->max.
844 static int allow_wrap(struct isl_wraps *wraps, int row)
846 int i;
848 if (!wraps->bound)
849 return 1;
851 for (i = 1; i < wraps->mat->n_col; ++i)
852 if (isl_int_abs_gt(wraps->mat->row[row][i], wraps->max))
853 return 0;
855 return 1;
858 /* Wrap "ineq" (or its opposite if "negate" is set) around "bound"
859 * to include "set" and add the result in position "w" of "wraps".
860 * "len" is the total number of coefficients in "bound" and "ineq".
861 * Return 1 on success, 0 on failure and -1 on error.
862 * Wrapping can fail if the result of wrapping is equal to "bound"
863 * or if we want to bound the sizes of the coefficients and
864 * the wrapped constraint does not satisfy this bound.
866 static int add_wrap(struct isl_wraps *wraps, int w, isl_int *bound,
867 isl_int *ineq, unsigned len, __isl_keep isl_set *set, int negate)
869 isl_seq_cpy(wraps->mat->row[w], bound, len);
870 if (negate) {
871 isl_seq_neg(wraps->mat->row[w + 1], ineq, len);
872 ineq = wraps->mat->row[w + 1];
874 if (!isl_set_wrap_facet(set, wraps->mat->row[w], ineq))
875 return -1;
876 if (isl_seq_eq(wraps->mat->row[w], bound, len))
877 return 0;
878 if (!allow_wrap(wraps, w))
879 return 0;
880 return 1;
883 /* For each constraint in info->bmap that is not redundant (as determined
884 * by info->tab) and that is not a valid constraint for the other basic map,
885 * wrap the constraint around "bound" such that it includes the whole
886 * set "set" and append the resulting constraint to "wraps".
887 * Note that the constraints that are valid for the other basic map
888 * will be added to the combined basic map by default, so there is
889 * no need to wrap them.
890 * The caller wrap_in_facets even relies on this function not wrapping
891 * any constraints that are already valid.
892 * "wraps" is assumed to have been pre-allocated to the appropriate size.
893 * wraps->n_row is the number of actual wrapped constraints that have
894 * been added.
895 * If any of the wrapping problems results in a constraint that is
896 * identical to "bound", then this means that "set" is unbounded in such
897 * way that no wrapping is possible. If this happens then wraps->n_row
898 * is reset to zero.
899 * Similarly, if we want to bound the coefficients of the wrapping
900 * constraints and a newly added wrapping constraint does not
901 * satisfy the bound, then wraps->n_row is also reset to zero.
903 static int add_wraps(struct isl_wraps *wraps, struct isl_coalesce_info *info,
904 isl_int *bound, __isl_keep isl_set *set)
906 int l, m;
907 int w;
908 int added;
909 isl_basic_map *bmap = info->bmap;
910 unsigned len = 1 + isl_basic_map_total_dim(bmap);
912 w = wraps->mat->n_row;
914 for (l = 0; l < bmap->n_ineq; ++l) {
915 if (info->ineq[l] == STATUS_VALID ||
916 info->ineq[l] == STATUS_REDUNDANT)
917 continue;
918 if (isl_seq_is_neg(bound, bmap->ineq[l], len))
919 continue;
920 if (isl_seq_eq(bound, bmap->ineq[l], len))
921 continue;
922 if (isl_tab_is_redundant(info->tab, bmap->n_eq + l))
923 continue;
925 added = add_wrap(wraps, w, bound, bmap->ineq[l], len, set, 0);
926 if (added < 0)
927 return -1;
928 if (!added)
929 goto unbounded;
930 ++w;
932 for (l = 0; l < bmap->n_eq; ++l) {
933 if (isl_seq_is_neg(bound, bmap->eq[l], len))
934 continue;
935 if (isl_seq_eq(bound, bmap->eq[l], len))
936 continue;
938 for (m = 0; m < 2; ++m) {
939 if (info->eq[2 * l + m] == STATUS_VALID)
940 continue;
941 added = add_wrap(wraps, w, bound, bmap->eq[l], len,
942 set, !m);
943 if (added < 0)
944 return -1;
945 if (!added)
946 goto unbounded;
947 ++w;
951 wraps->mat->n_row = w;
952 return 0;
953 unbounded:
954 wraps->mat->n_row = 0;
955 return 0;
958 /* Check if the constraints in "wraps" from "first" until the last
959 * are all valid for the basic set represented by "tab".
960 * If not, wraps->n_row is set to zero.
962 static int check_wraps(__isl_keep isl_mat *wraps, int first,
963 struct isl_tab *tab)
965 int i;
967 for (i = first; i < wraps->n_row; ++i) {
968 enum isl_ineq_type type;
969 type = isl_tab_ineq_type(tab, wraps->row[i]);
970 if (type == isl_ineq_error)
971 return -1;
972 if (type == isl_ineq_redundant)
973 continue;
974 wraps->n_row = 0;
975 return 0;
978 return 0;
981 /* Return a set that corresponds to the non-redundant constraints
982 * (as recorded in tab) of bmap.
984 * It's important to remove the redundant constraints as some
985 * of the other constraints may have been modified after the
986 * constraints were marked redundant.
987 * In particular, a constraint may have been relaxed.
988 * Redundant constraints are ignored when a constraint is relaxed
989 * and should therefore continue to be ignored ever after.
990 * Otherwise, the relaxation might be thwarted by some of
991 * these constraints.
993 * Update the underlying set to ensure that the dimension doesn't change.
994 * Otherwise the integer divisions could get dropped if the tab
995 * turns out to be empty.
997 static __isl_give isl_set *set_from_updated_bmap(__isl_keep isl_basic_map *bmap,
998 struct isl_tab *tab)
1000 isl_basic_set *bset;
1002 bmap = isl_basic_map_copy(bmap);
1003 bset = isl_basic_map_underlying_set(bmap);
1004 bset = isl_basic_set_cow(bset);
1005 bset = isl_basic_set_update_from_tab(bset, tab);
1006 return isl_set_from_basic_set(bset);
1009 /* Wrap the constraints of info->bmap that bound the facet defined
1010 * by inequality "k" around (the opposite of) this inequality to
1011 * include "set". "bound" may be used to store the negated inequality.
1012 * Since the wrapped constraints are not guaranteed to contain the whole
1013 * of info->bmap, we check them in check_wraps.
1014 * If any of the wrapped constraints turn out to be invalid, then
1015 * check_wraps will reset wrap->n_row to zero.
1017 static int add_wraps_around_facet(struct isl_wraps *wraps,
1018 struct isl_coalesce_info *info, int k, isl_int *bound,
1019 __isl_keep isl_set *set)
1021 struct isl_tab_undo *snap;
1022 int n;
1023 unsigned total = isl_basic_map_total_dim(info->bmap);
1025 snap = isl_tab_snap(info->tab);
1027 if (isl_tab_select_facet(info->tab, info->bmap->n_eq + k) < 0)
1028 return -1;
1029 if (isl_tab_detect_redundant(info->tab) < 0)
1030 return -1;
1032 isl_seq_neg(bound, info->bmap->ineq[k], 1 + total);
1034 n = wraps->mat->n_row;
1035 if (add_wraps(wraps, info, bound, set) < 0)
1036 return -1;
1038 if (isl_tab_rollback(info->tab, snap) < 0)
1039 return -1;
1040 if (check_wraps(wraps->mat, n, info->tab) < 0)
1041 return -1;
1043 return 0;
1046 /* Given a basic set i with a constraint k that is adjacent to
1047 * basic set j, check if we can wrap
1048 * both the facet corresponding to k (if "wrap_facet" is set) and basic map j
1049 * (always) around their ridges to include the other set.
1050 * If so, replace the pair of basic sets by their union.
1052 * All constraints of i (except k) are assumed to be valid or
1053 * cut constraints for j.
1054 * Wrapping the cut constraints to include basic map j may result
1055 * in constraints that are no longer valid of basic map i
1056 * we have to check that the resulting wrapping constraints are valid for i.
1057 * If "wrap_facet" is not set, then all constraints of i (except k)
1058 * are assumed to be valid for j.
1059 * ____ _____
1060 * / | / \
1061 * / || / |
1062 * \ || => \ |
1063 * \ || \ |
1064 * \___|| \____|
1067 static enum isl_change can_wrap_in_facet(int i, int j, int k,
1068 struct isl_coalesce_info *info, int wrap_facet)
1070 enum isl_change change = isl_change_none;
1071 struct isl_wraps wraps;
1072 isl_ctx *ctx;
1073 isl_mat *mat;
1074 struct isl_set *set_i = NULL;
1075 struct isl_set *set_j = NULL;
1076 struct isl_vec *bound = NULL;
1077 unsigned total = isl_basic_map_total_dim(info[i].bmap);
1079 set_i = set_from_updated_bmap(info[i].bmap, info[i].tab);
1080 set_j = set_from_updated_bmap(info[j].bmap, info[j].tab);
1081 ctx = isl_basic_map_get_ctx(info[i].bmap);
1082 mat = isl_mat_alloc(ctx, 2 * (info[i].bmap->n_eq + info[j].bmap->n_eq) +
1083 info[i].bmap->n_ineq + info[j].bmap->n_ineq,
1084 1 + total);
1085 wraps_init(&wraps, mat, info, i, j);
1086 bound = isl_vec_alloc(ctx, 1 + total);
1087 if (!set_i || !set_j || !wraps.mat || !bound)
1088 goto error;
1090 isl_seq_cpy(bound->el, info[i].bmap->ineq[k], 1 + total);
1091 isl_int_add_ui(bound->el[0], bound->el[0], 1);
1093 isl_seq_cpy(wraps.mat->row[0], bound->el, 1 + total);
1094 wraps.mat->n_row = 1;
1096 if (add_wraps(&wraps, &info[j], bound->el, set_i) < 0)
1097 goto error;
1098 if (!wraps.mat->n_row)
1099 goto unbounded;
1101 if (wrap_facet) {
1102 if (add_wraps_around_facet(&wraps, &info[i], k,
1103 bound->el, set_j) < 0)
1104 goto error;
1105 if (!wraps.mat->n_row)
1106 goto unbounded;
1109 change = fuse(i, j, info, wraps.mat, 0, 0);
1111 unbounded:
1112 wraps_free(&wraps);
1114 isl_set_free(set_i);
1115 isl_set_free(set_j);
1117 isl_vec_free(bound);
1119 return change;
1120 error:
1121 wraps_free(&wraps);
1122 isl_vec_free(bound);
1123 isl_set_free(set_i);
1124 isl_set_free(set_j);
1125 return isl_change_error;
1128 /* Given a pair of basic maps i and j such that j sticks out
1129 * of i at n cut constraints, each time by at most one,
1130 * try to compute wrapping constraints and replace the two
1131 * basic maps by a single basic map.
1132 * The other constraints of i are assumed to be valid for j.
1134 * For each cut constraint t(x) >= 0 of i, we add the relaxed version
1135 * t(x) + 1 >= 0, along with wrapping constraints for all constraints
1136 * of basic map j that bound the part of basic map j that sticks out
1137 * of the cut constraint.
1138 * In particular, we first intersect basic map j with t(x) + 1 = 0.
1139 * If the result is empty, then t(x) >= 0 was actually a valid constraint
1140 * (with respect to the integer points), so we add t(x) >= 0 instead.
1141 * Otherwise, we wrap the constraints of basic map j that are not
1142 * redundant in this intersection and that are not already valid
1143 * for basic map i over basic map i.
1144 * Note that it is sufficient to wrap the constraints to include
1145 * basic map i, because we will only wrap the constraints that do
1146 * not include basic map i already. The wrapped constraint will
1147 * therefore be more relaxed compared to the original constraint.
1148 * Since the original constraint is valid for basic map j, so is
1149 * the wrapped constraint.
1151 * If any wrapping fails, i.e., if we cannot wrap to touch
1152 * the union, then we give up.
1153 * Otherwise, the pair of basic maps is replaced by their union.
1155 static enum isl_change wrap_in_facets(int i, int j, int *cuts, int n,
1156 struct isl_coalesce_info *info)
1158 enum isl_change change = isl_change_none;
1159 struct isl_wraps wraps;
1160 isl_ctx *ctx;
1161 isl_mat *mat;
1162 isl_set *set_i = NULL;
1163 unsigned total = isl_basic_map_total_dim(info[i].bmap);
1164 int max_wrap;
1165 int k, w;
1166 struct isl_tab_undo *snap;
1168 if (isl_tab_extend_cons(info[j].tab, 1) < 0)
1169 goto error;
1171 max_wrap = 1 + 2 * info[j].bmap->n_eq + info[j].bmap->n_ineq;
1172 max_wrap *= n;
1174 set_i = set_from_updated_bmap(info[i].bmap, info[i].tab);
1175 ctx = isl_basic_map_get_ctx(info[i].bmap);
1176 mat = isl_mat_alloc(ctx, max_wrap, 1 + total);
1177 wraps_init(&wraps, mat, info, i, j);
1178 if (!set_i || !wraps.mat)
1179 goto error;
1181 snap = isl_tab_snap(info[j].tab);
1183 wraps.mat->n_row = 0;
1185 for (k = 0; k < n; ++k) {
1186 w = wraps.mat->n_row++;
1187 isl_seq_cpy(wraps.mat->row[w],
1188 info[i].bmap->ineq[cuts[k]], 1 + total);
1189 isl_int_add_ui(wraps.mat->row[w][0], wraps.mat->row[w][0], 1);
1190 if (isl_tab_add_eq(info[j].tab, wraps.mat->row[w]) < 0)
1191 goto error;
1192 if (isl_tab_detect_redundant(info[j].tab) < 0)
1193 goto error;
1195 if (info[j].tab->empty)
1196 isl_int_sub_ui(wraps.mat->row[w][0],
1197 wraps.mat->row[w][0], 1);
1198 else if (add_wraps(&wraps, &info[j],
1199 wraps.mat->row[w], set_i) < 0)
1200 goto error;
1202 if (isl_tab_rollback(info[j].tab, snap) < 0)
1203 goto error;
1205 if (!wraps.mat->n_row)
1206 break;
1209 if (k == n)
1210 change = fuse(i, j, info, wraps.mat, 0, 1);
1212 wraps_free(&wraps);
1213 isl_set_free(set_i);
1215 return change;
1216 error:
1217 wraps_free(&wraps);
1218 isl_set_free(set_i);
1219 return isl_change_error;
1222 /* Given two basic sets i and j such that i has no cut equalities,
1223 * check if relaxing all the cut inequalities of i by one turns
1224 * them into valid constraint for j and check if we can wrap in
1225 * the bits that are sticking out.
1226 * If so, replace the pair by their union.
1228 * We first check if all relaxed cut inequalities of i are valid for j
1229 * and then try to wrap in the intersections of the relaxed cut inequalities
1230 * with j.
1232 * During this wrapping, we consider the points of j that lie at a distance
1233 * of exactly 1 from i. In particular, we ignore the points that lie in
1234 * between this lower-dimensional space and the basic map i.
1235 * We can therefore only apply this to integer maps.
1236 * ____ _____
1237 * / ___|_ / \
1238 * / | | / |
1239 * \ | | => \ |
1240 * \|____| \ |
1241 * \___| \____/
1243 * _____ ______
1244 * | ____|_ | \
1245 * | | | | |
1246 * | | | => | |
1247 * |_| | | |
1248 * |_____| \______|
1250 * _______
1251 * | |
1252 * | |\ |
1253 * | | \ |
1254 * | | \ |
1255 * | | \|
1256 * | | \
1257 * | |_____\
1258 * | |
1259 * |_______|
1261 * Wrapping can fail if the result of wrapping one of the facets
1262 * around its edges does not produce any new facet constraint.
1263 * In particular, this happens when we try to wrap in unbounded sets.
1265 * _______________________________________________________________________
1267 * | ___
1268 * | | |
1269 * |_| |_________________________________________________________________
1270 * |___|
1272 * The following is not an acceptable result of coalescing the above two
1273 * sets as it includes extra integer points.
1274 * _______________________________________________________________________
1276 * |
1277 * |
1279 * \______________________________________________________________________
1281 static enum isl_change can_wrap_in_set(int i, int j,
1282 struct isl_coalesce_info *info)
1284 enum isl_change change = isl_change_none;
1285 int k, m;
1286 int n;
1287 int *cuts = NULL;
1288 isl_ctx *ctx;
1290 if (ISL_F_ISSET(info[i].bmap, ISL_BASIC_MAP_RATIONAL) ||
1291 ISL_F_ISSET(info[j].bmap, ISL_BASIC_MAP_RATIONAL))
1292 return isl_change_none;
1294 n = count(info[i].ineq, info[i].bmap->n_ineq, STATUS_CUT);
1295 if (n == 0)
1296 return isl_change_none;
1298 ctx = isl_basic_map_get_ctx(info[i].bmap);
1299 cuts = isl_alloc_array(ctx, int, n);
1300 if (!cuts)
1301 return isl_change_error;
1303 for (k = 0, m = 0; m < n; ++k) {
1304 enum isl_ineq_type type;
1306 if (info[i].ineq[k] != STATUS_CUT)
1307 continue;
1309 isl_int_add_ui(info[i].bmap->ineq[k][0],
1310 info[i].bmap->ineq[k][0], 1);
1311 type = isl_tab_ineq_type(info[j].tab, info[i].bmap->ineq[k]);
1312 isl_int_sub_ui(info[i].bmap->ineq[k][0],
1313 info[i].bmap->ineq[k][0], 1);
1314 if (type == isl_ineq_error)
1315 goto error;
1316 if (type != isl_ineq_redundant)
1317 break;
1318 cuts[m] = k;
1319 ++m;
1322 if (m == n)
1323 change = wrap_in_facets(i, j, cuts, n, info);
1325 free(cuts);
1327 return change;
1328 error:
1329 free(cuts);
1330 return isl_change_error;
1333 /* Check if either i or j has only cut inequalities that can
1334 * be used to wrap in (a facet of) the other basic set.
1335 * if so, replace the pair by their union.
1337 static enum isl_change check_wrap(int i, int j, struct isl_coalesce_info *info)
1339 enum isl_change change = isl_change_none;
1341 if (!any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_CUT))
1342 change = can_wrap_in_set(i, j, info);
1343 if (change != isl_change_none)
1344 return change;
1346 if (!any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_CUT))
1347 change = can_wrap_in_set(j, i, info);
1348 return change;
1351 /* At least one of the basic maps has an equality that is adjacent
1352 * to inequality. Make sure that only one of the basic maps has
1353 * such an equality and that the other basic map has exactly one
1354 * inequality adjacent to an equality.
1355 * We call the basic map that has the inequality "i" and the basic
1356 * map that has the equality "j".
1357 * If "i" has any "cut" (in)equality, then relaxing the inequality
1358 * by one would not result in a basic map that contains the other
1359 * basic map. However, it may still be possible to wrap in the other
1360 * basic map.
1362 static enum isl_change check_adj_eq(int i, int j,
1363 struct isl_coalesce_info *info)
1365 enum isl_change change = isl_change_none;
1366 int k;
1367 int any_cut;
1369 if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ADJ_INEQ) &&
1370 any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_ADJ_INEQ))
1371 /* ADJ EQ TOO MANY */
1372 return isl_change_none;
1374 if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ADJ_INEQ))
1375 return check_adj_eq(j, i, info);
1377 /* j has an equality adjacent to an inequality in i */
1379 if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_CUT))
1380 return isl_change_none;
1381 any_cut = any(info[i].ineq, info[i].bmap->n_ineq, STATUS_CUT);
1382 if (count(info[i].ineq, info[i].bmap->n_ineq, STATUS_ADJ_EQ) != 1 ||
1383 any(info[j].ineq, info[j].bmap->n_ineq, STATUS_ADJ_EQ) ||
1384 any(info[i].ineq, info[i].bmap->n_ineq, STATUS_ADJ_INEQ) ||
1385 any(info[j].ineq, info[j].bmap->n_ineq, STATUS_ADJ_INEQ))
1386 /* ADJ EQ TOO MANY */
1387 return isl_change_none;
1389 for (k = 0; k < info[i].bmap->n_ineq; ++k)
1390 if (info[i].ineq[k] == STATUS_ADJ_EQ)
1391 break;
1393 if (!any_cut) {
1394 change = is_adj_eq_extension(i, j, k, info);
1395 if (change != isl_change_none)
1396 return change;
1399 change = can_wrap_in_facet(i, j, k, info, any_cut);
1401 return change;
1404 /* The two basic maps lie on adjacent hyperplanes. In particular,
1405 * basic map "i" has an equality that lies parallel to basic map "j".
1406 * Check if we can wrap the facets around the parallel hyperplanes
1407 * to include the other set.
1409 * We perform basically the same operations as can_wrap_in_facet,
1410 * except that we don't need to select a facet of one of the sets.
1412 * \\ \\
1413 * \\ => \\
1414 * \ \|
1416 * If there is more than one equality of "i" adjacent to an equality of "j",
1417 * then the result will satisfy one or more equalities that are a linear
1418 * combination of these equalities. These will be encoded as pairs
1419 * of inequalities in the wrapping constraints and need to be made
1420 * explicit.
1422 static enum isl_change check_eq_adj_eq(int i, int j,
1423 struct isl_coalesce_info *info)
1425 int k;
1426 enum isl_change change = isl_change_none;
1427 int detect_equalities = 0;
1428 struct isl_wraps wraps;
1429 isl_ctx *ctx;
1430 isl_mat *mat;
1431 struct isl_set *set_i = NULL;
1432 struct isl_set *set_j = NULL;
1433 struct isl_vec *bound = NULL;
1434 unsigned total = isl_basic_map_total_dim(info[i].bmap);
1436 if (count(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ADJ_EQ) != 1)
1437 detect_equalities = 1;
1439 for (k = 0; k < 2 * info[i].bmap->n_eq ; ++k)
1440 if (info[i].eq[k] == STATUS_ADJ_EQ)
1441 break;
1443 set_i = set_from_updated_bmap(info[i].bmap, info[i].tab);
1444 set_j = set_from_updated_bmap(info[j].bmap, info[j].tab);
1445 ctx = isl_basic_map_get_ctx(info[i].bmap);
1446 mat = isl_mat_alloc(ctx, 2 * (info[i].bmap->n_eq + info[j].bmap->n_eq) +
1447 info[i].bmap->n_ineq + info[j].bmap->n_ineq,
1448 1 + total);
1449 wraps_init(&wraps, mat, info, i, j);
1450 bound = isl_vec_alloc(ctx, 1 + total);
1451 if (!set_i || !set_j || !wraps.mat || !bound)
1452 goto error;
1454 if (k % 2 == 0)
1455 isl_seq_neg(bound->el, info[i].bmap->eq[k / 2], 1 + total);
1456 else
1457 isl_seq_cpy(bound->el, info[i].bmap->eq[k / 2], 1 + total);
1458 isl_int_add_ui(bound->el[0], bound->el[0], 1);
1460 isl_seq_cpy(wraps.mat->row[0], bound->el, 1 + total);
1461 wraps.mat->n_row = 1;
1463 if (add_wraps(&wraps, &info[j], bound->el, set_i) < 0)
1464 goto error;
1465 if (!wraps.mat->n_row)
1466 goto unbounded;
1468 isl_int_sub_ui(bound->el[0], bound->el[0], 1);
1469 isl_seq_neg(bound->el, bound->el, 1 + total);
1471 isl_seq_cpy(wraps.mat->row[wraps.mat->n_row], bound->el, 1 + total);
1472 wraps.mat->n_row++;
1474 if (add_wraps(&wraps, &info[i], bound->el, set_j) < 0)
1475 goto error;
1476 if (!wraps.mat->n_row)
1477 goto unbounded;
1479 change = fuse(i, j, info, wraps.mat, detect_equalities, 0);
1481 if (0) {
1482 error: change = isl_change_error;
1484 unbounded:
1486 wraps_free(&wraps);
1487 isl_set_free(set_i);
1488 isl_set_free(set_j);
1489 isl_vec_free(bound);
1491 return change;
1494 /* Check if the union of the given pair of basic maps
1495 * can be represented by a single basic map.
1496 * If so, replace the pair by the single basic map and return
1497 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
1498 * Otherwise, return isl_change_none.
1499 * The two basic maps are assumed to live in the same local space.
1501 * We first check the effect of each constraint of one basic map
1502 * on the other basic map.
1503 * The constraint may be
1504 * redundant the constraint is redundant in its own
1505 * basic map and should be ignore and removed
1506 * in the end
1507 * valid all (integer) points of the other basic map
1508 * satisfy the constraint
1509 * separate no (integer) point of the other basic map
1510 * satisfies the constraint
1511 * cut some but not all points of the other basic map
1512 * satisfy the constraint
1513 * adj_eq the given constraint is adjacent (on the outside)
1514 * to an equality of the other basic map
1515 * adj_ineq the given constraint is adjacent (on the outside)
1516 * to an inequality of the other basic map
1518 * We consider seven cases in which we can replace the pair by a single
1519 * basic map. We ignore all "redundant" constraints.
1521 * 1. all constraints of one basic map are valid
1522 * => the other basic map is a subset and can be removed
1524 * 2. all constraints of both basic maps are either "valid" or "cut"
1525 * and the facets corresponding to the "cut" constraints
1526 * of one of the basic maps lies entirely inside the other basic map
1527 * => the pair can be replaced by a basic map consisting
1528 * of the valid constraints in both basic maps
1530 * 3. there is a single pair of adjacent inequalities
1531 * (all other constraints are "valid")
1532 * => the pair can be replaced by a basic map consisting
1533 * of the valid constraints in both basic maps
1535 * 4. one basic map has a single adjacent inequality, while the other
1536 * constraints are "valid". The other basic map has some
1537 * "cut" constraints, but replacing the adjacent inequality by
1538 * its opposite and adding the valid constraints of the other
1539 * basic map results in a subset of the other basic map
1540 * => the pair can be replaced by a basic map consisting
1541 * of the valid constraints in both basic maps
1543 * 5. there is a single adjacent pair of an inequality and an equality,
1544 * the other constraints of the basic map containing the inequality are
1545 * "valid". Moreover, if the inequality the basic map is relaxed
1546 * and then turned into an equality, then resulting facet lies
1547 * entirely inside the other basic map
1548 * => the pair can be replaced by the basic map containing
1549 * the inequality, with the inequality relaxed.
1551 * 6. there is a single adjacent pair of an inequality and an equality,
1552 * the other constraints of the basic map containing the inequality are
1553 * "valid". Moreover, the facets corresponding to both
1554 * the inequality and the equality can be wrapped around their
1555 * ridges to include the other basic map
1556 * => the pair can be replaced by a basic map consisting
1557 * of the valid constraints in both basic maps together
1558 * with all wrapping constraints
1560 * 7. one of the basic maps extends beyond the other by at most one.
1561 * Moreover, the facets corresponding to the cut constraints and
1562 * the pieces of the other basic map at offset one from these cut
1563 * constraints can be wrapped around their ridges to include
1564 * the union of the two basic maps
1565 * => the pair can be replaced by a basic map consisting
1566 * of the valid constraints in both basic maps together
1567 * with all wrapping constraints
1569 * 8. the two basic maps live in adjacent hyperplanes. In principle
1570 * such sets can always be combined through wrapping, but we impose
1571 * that there is only one such pair, to avoid overeager coalescing.
1573 * Throughout the computation, we maintain a collection of tableaus
1574 * corresponding to the basic maps. When the basic maps are dropped
1575 * or combined, the tableaus are modified accordingly.
1577 static enum isl_change coalesce_local_pair(int i, int j,
1578 struct isl_coalesce_info *info)
1580 enum isl_change change = isl_change_none;
1582 info[i].eq = info[i].ineq = NULL;
1583 info[j].eq = info[j].ineq = NULL;
1585 info[i].eq = eq_status_in(info[i].bmap, info[j].tab);
1586 if (info[i].bmap->n_eq && !info[i].eq)
1587 goto error;
1588 if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ERROR))
1589 goto error;
1590 if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_SEPARATE))
1591 goto done;
1593 info[j].eq = eq_status_in(info[j].bmap, info[i].tab);
1594 if (info[j].bmap->n_eq && !info[j].eq)
1595 goto error;
1596 if (any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_ERROR))
1597 goto error;
1598 if (any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_SEPARATE))
1599 goto done;
1601 info[i].ineq = ineq_status_in(info[i].bmap, info[i].tab, info[j].tab);
1602 if (info[i].bmap->n_ineq && !info[i].ineq)
1603 goto error;
1604 if (any(info[i].ineq, info[i].bmap->n_ineq, STATUS_ERROR))
1605 goto error;
1606 if (any(info[i].ineq, info[i].bmap->n_ineq, STATUS_SEPARATE))
1607 goto done;
1609 info[j].ineq = ineq_status_in(info[j].bmap, info[j].tab, info[i].tab);
1610 if (info[j].bmap->n_ineq && !info[j].ineq)
1611 goto error;
1612 if (any(info[j].ineq, info[j].bmap->n_ineq, STATUS_ERROR))
1613 goto error;
1614 if (any(info[j].ineq, info[j].bmap->n_ineq, STATUS_SEPARATE))
1615 goto done;
1617 if (all(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_VALID) &&
1618 all(info[i].ineq, info[i].bmap->n_ineq, STATUS_VALID)) {
1619 drop(&info[j]);
1620 change = isl_change_drop_second;
1621 } else if (all(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_VALID) &&
1622 all(info[j].ineq, info[j].bmap->n_ineq, STATUS_VALID)) {
1623 drop(&info[i]);
1624 change = isl_change_drop_first;
1625 } else if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ADJ_EQ)) {
1626 change = check_eq_adj_eq(i, j, info);
1627 } else if (any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_ADJ_EQ)) {
1628 change = check_eq_adj_eq(j, i, info);
1629 } else if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ADJ_INEQ) ||
1630 any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_ADJ_INEQ)) {
1631 change = check_adj_eq(i, j, info);
1632 } else if (any(info[i].ineq, info[i].bmap->n_ineq, STATUS_ADJ_EQ) ||
1633 any(info[j].ineq, info[j].bmap->n_ineq, STATUS_ADJ_EQ)) {
1634 /* Can't happen */
1635 /* BAD ADJ INEQ */
1636 } else if (any(info[i].ineq, info[i].bmap->n_ineq, STATUS_ADJ_INEQ) ||
1637 any(info[j].ineq, info[j].bmap->n_ineq, STATUS_ADJ_INEQ)) {
1638 change = check_adj_ineq(i, j, info);
1639 } else {
1640 if (!any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_CUT) &&
1641 !any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_CUT))
1642 change = check_facets(i, j, info);
1643 if (change == isl_change_none)
1644 change = check_wrap(i, j, info);
1647 done:
1648 free(info[i].eq);
1649 free(info[j].eq);
1650 free(info[i].ineq);
1651 free(info[j].ineq);
1652 return change;
1653 error:
1654 free(info[i].eq);
1655 free(info[j].eq);
1656 free(info[i].ineq);
1657 free(info[j].ineq);
1658 return isl_change_error;
1661 /* Shift the integer division at position "div" of the basic map
1662 * represented by "info" by "shift".
1664 * That is, if the integer division has the form
1666 * floor(f(x)/d)
1668 * then replace it by
1670 * floor((f(x) + shift * d)/d) - shift
1672 static int shift_div(struct isl_coalesce_info *info, int div, isl_int shift)
1674 unsigned total;
1676 info->bmap = isl_basic_map_shift_div(info->bmap, div, shift);
1677 if (!info->bmap)
1678 return -1;
1680 total = isl_basic_map_dim(info->bmap, isl_dim_all);
1681 total -= isl_basic_map_dim(info->bmap, isl_dim_div);
1682 if (isl_tab_shift_var(info->tab, total + div, shift) < 0)
1683 return -1;
1685 return 0;
1688 /* Check if some of the divs in the basic map represented by "info1"
1689 * are shifts of the corresponding divs in the basic map represented
1690 * by "info2". If so, align them with those of "info2".
1691 * Only do this if "info1" and "info2" have the same number
1692 * of integer divisions.
1694 * An integer division is considered to be a shift of another integer
1695 * division if one is equal to the other plus a constant.
1697 * In particular, for each pair of integer divisions, if both are known,
1698 * have identical coefficients (apart from the constant term) and
1699 * if the difference between the constant terms (taking into account
1700 * the denominator) is an integer, then move the difference outside.
1701 * That is, if one integer division is of the form
1703 * floor((f(x) + c_1)/d)
1705 * while the other is of the form
1707 * floor((f(x) + c_2)/d)
1709 * and n = (c_2 - c_1)/d is an integer, then replace the first
1710 * integer division by
1712 * floor((f(x) + c_1 + n * d)/d) - n = floor((f(x) + c_2)/d) - n
1714 static int harmonize_divs(struct isl_coalesce_info *info1,
1715 struct isl_coalesce_info *info2)
1717 int i;
1718 int total;
1720 if (!info1->bmap || !info2->bmap)
1721 return -1;
1723 if (info1->bmap->n_div != info2->bmap->n_div)
1724 return 0;
1725 if (info1->bmap->n_div == 0)
1726 return 0;
1728 total = isl_basic_map_total_dim(info1->bmap);
1729 for (i = 0; i < info1->bmap->n_div; ++i) {
1730 isl_int d;
1731 int r = 0;
1733 if (isl_int_is_zero(info1->bmap->div[i][0]) ||
1734 isl_int_is_zero(info2->bmap->div[i][0]))
1735 continue;
1736 if (isl_int_ne(info1->bmap->div[i][0], info2->bmap->div[i][0]))
1737 continue;
1738 if (isl_int_eq(info1->bmap->div[i][1], info2->bmap->div[i][1]))
1739 continue;
1740 if (!isl_seq_eq(info1->bmap->div[i] + 2,
1741 info2->bmap->div[i] + 2, total))
1742 continue;
1743 isl_int_init(d);
1744 isl_int_sub(d, info2->bmap->div[i][1], info1->bmap->div[i][1]);
1745 if (isl_int_is_divisible_by(d, info1->bmap->div[i][0])) {
1746 isl_int_divexact(d, d, info1->bmap->div[i][0]);
1747 r = shift_div(info1, i, d);
1749 isl_int_clear(d);
1750 if (r < 0)
1751 return -1;
1754 return 0;
1757 /* Do the two basic maps live in the same local space, i.e.,
1758 * do they have the same (known) divs?
1759 * If either basic map has any unknown divs, then we can only assume
1760 * that they do not live in the same local space.
1762 static int same_divs(__isl_keep isl_basic_map *bmap1,
1763 __isl_keep isl_basic_map *bmap2)
1765 int i;
1766 int known;
1767 int total;
1769 if (!bmap1 || !bmap2)
1770 return -1;
1771 if (bmap1->n_div != bmap2->n_div)
1772 return 0;
1774 if (bmap1->n_div == 0)
1775 return 1;
1777 known = isl_basic_map_divs_known(bmap1);
1778 if (known < 0 || !known)
1779 return known;
1780 known = isl_basic_map_divs_known(bmap2);
1781 if (known < 0 || !known)
1782 return known;
1784 total = isl_basic_map_total_dim(bmap1);
1785 for (i = 0; i < bmap1->n_div; ++i)
1786 if (!isl_seq_eq(bmap1->div[i], bmap2->div[i], 2 + total))
1787 return 0;
1789 return 1;
1792 /* Does "bmap" contain the basic map represented by the tableau "tab"
1793 * after expanding the divs of "bmap" to match those of "tab"?
1794 * The expansion is performed using the divs "div" and expansion "exp"
1795 * computed by the caller.
1796 * Then we check if all constraints of the expanded "bmap" are valid for "tab".
1798 static int contains_with_expanded_divs(__isl_keep isl_basic_map *bmap,
1799 struct isl_tab *tab, __isl_keep isl_mat *div, int *exp)
1801 int superset = 0;
1802 int *eq_i = NULL;
1803 int *ineq_i = NULL;
1805 bmap = isl_basic_map_copy(bmap);
1806 bmap = isl_basic_set_expand_divs(bmap, isl_mat_copy(div), exp);
1808 if (!bmap)
1809 goto error;
1811 eq_i = eq_status_in(bmap, tab);
1812 if (bmap->n_eq && !eq_i)
1813 goto error;
1814 if (any(eq_i, 2 * bmap->n_eq, STATUS_ERROR))
1815 goto error;
1816 if (any(eq_i, 2 * bmap->n_eq, STATUS_SEPARATE))
1817 goto done;
1819 ineq_i = ineq_status_in(bmap, NULL, tab);
1820 if (bmap->n_ineq && !ineq_i)
1821 goto error;
1822 if (any(ineq_i, bmap->n_ineq, STATUS_ERROR))
1823 goto error;
1824 if (any(ineq_i, bmap->n_ineq, STATUS_SEPARATE))
1825 goto done;
1827 if (all(eq_i, 2 * bmap->n_eq, STATUS_VALID) &&
1828 all(ineq_i, bmap->n_ineq, STATUS_VALID))
1829 superset = 1;
1831 done:
1832 isl_basic_map_free(bmap);
1833 free(eq_i);
1834 free(ineq_i);
1835 return superset;
1836 error:
1837 isl_basic_map_free(bmap);
1838 free(eq_i);
1839 free(ineq_i);
1840 return -1;
1843 /* Does "bmap_i" contain the basic map represented by "info_j"
1844 * after aligning the divs of "bmap_i" to those of "info_j".
1845 * Note that this can only succeed if the number of divs of "bmap_i"
1846 * is smaller than (or equal to) the number of divs of "info_j".
1848 * We first check if the divs of "bmap_i" are all known and form a subset
1849 * of those of "bmap_j". If so, we pass control over to
1850 * contains_with_expanded_divs.
1852 static int contains_after_aligning_divs(__isl_keep isl_basic_map *bmap_i,
1853 struct isl_coalesce_info *info_j)
1855 int known;
1856 isl_mat *div_i, *div_j, *div;
1857 int *exp1 = NULL;
1858 int *exp2 = NULL;
1859 isl_ctx *ctx;
1860 int subset;
1862 known = isl_basic_map_divs_known(bmap_i);
1863 if (known < 0 || !known)
1864 return known;
1866 ctx = isl_basic_map_get_ctx(bmap_i);
1868 div_i = isl_basic_map_get_divs(bmap_i);
1869 div_j = isl_basic_map_get_divs(info_j->bmap);
1871 if (!div_i || !div_j)
1872 goto error;
1874 exp1 = isl_alloc_array(ctx, int, div_i->n_row);
1875 exp2 = isl_alloc_array(ctx, int, div_j->n_row);
1876 if ((div_i->n_row && !exp1) || (div_j->n_row && !exp2))
1877 goto error;
1879 div = isl_merge_divs(div_i, div_j, exp1, exp2);
1880 if (!div)
1881 goto error;
1883 if (div->n_row == div_j->n_row)
1884 subset = contains_with_expanded_divs(bmap_i,
1885 info_j->tab, div, exp1);
1886 else
1887 subset = 0;
1889 isl_mat_free(div);
1891 isl_mat_free(div_i);
1892 isl_mat_free(div_j);
1894 free(exp2);
1895 free(exp1);
1897 return subset;
1898 error:
1899 isl_mat_free(div_i);
1900 isl_mat_free(div_j);
1901 free(exp1);
1902 free(exp2);
1903 return -1;
1906 /* Check if the basic map "j" is a subset of basic map "i",
1907 * if "i" has fewer divs that "j".
1908 * If so, remove basic map "j".
1910 * If the two basic maps have the same number of divs, then
1911 * they must necessarily be different. Otherwise, we would have
1912 * called coalesce_local_pair. We therefore don't try anything
1913 * in this case.
1915 static int coalesced_subset(int i, int j, struct isl_coalesce_info *info)
1917 int superset;
1919 if (info[i].bmap->n_div >= info[j].bmap->n_div)
1920 return 0;
1922 superset = contains_after_aligning_divs(info[i].bmap, &info[j]);
1923 if (superset < 0)
1924 return -1;
1925 if (superset)
1926 drop(&info[j]);
1928 return superset;
1931 /* Check if basic map "j" is a subset of basic map "i" after
1932 * exploiting the extra equalities of "j" to simplify the divs of "i".
1933 * If so, remove basic map "j".
1935 * If "j" does not have any equalities or if they are the same
1936 * as those of "i", then we cannot exploit them to simplify the divs.
1937 * Similarly, if there are no divs in "i", then they cannot be simplified.
1938 * If, on the other hand, the affine hulls of "i" and "j" do not intersect,
1939 * then "j" cannot be a subset of "i".
1941 * Otherwise, we intersect "i" with the affine hull of "j" and then
1942 * check if "j" is a subset of the result after aligning the divs.
1943 * If so, then "j" is definitely a subset of "i" and can be removed.
1944 * Note that if after intersection with the affine hull of "j".
1945 * "i" still has more divs than "j", then there is no way we can
1946 * align the divs of "i" to those of "j".
1948 static int coalesced_subset_with_equalities(int i, int j,
1949 struct isl_coalesce_info *info)
1951 isl_basic_map *hull_i, *hull_j, *bmap_i;
1952 int equal, empty, subset;
1954 if (info[j].bmap->n_eq == 0)
1955 return 0;
1956 if (info[i].bmap->n_div == 0)
1957 return 0;
1959 hull_i = isl_basic_map_copy(info[i].bmap);
1960 hull_i = isl_basic_map_plain_affine_hull(hull_i);
1961 hull_j = isl_basic_map_copy(info[j].bmap);
1962 hull_j = isl_basic_map_plain_affine_hull(hull_j);
1964 hull_j = isl_basic_map_intersect(hull_j, isl_basic_map_copy(hull_i));
1965 equal = isl_basic_map_plain_is_equal(hull_i, hull_j);
1966 empty = isl_basic_map_plain_is_empty(hull_j);
1967 isl_basic_map_free(hull_i);
1969 if (equal < 0 || equal || empty < 0 || empty) {
1970 isl_basic_map_free(hull_j);
1971 return equal < 0 || empty < 0 ? -1 : 0;
1974 bmap_i = isl_basic_map_copy(info[i].bmap);
1975 bmap_i = isl_basic_map_intersect(bmap_i, hull_j);
1976 if (!bmap_i)
1977 return -1;
1979 if (bmap_i->n_div > info[j].bmap->n_div) {
1980 isl_basic_map_free(bmap_i);
1981 return 0;
1984 subset = contains_after_aligning_divs(bmap_i, &info[j]);
1986 isl_basic_map_free(bmap_i);
1988 if (subset < 0)
1989 return -1;
1990 if (subset)
1991 drop(&info[j]);
1993 return subset;
1996 /* Check if one of the basic maps is a subset of the other and, if so,
1997 * drop the subset.
1998 * Note that we only perform any test if the number of divs is different
1999 * in the two basic maps. In case the number of divs is the same,
2000 * we have already established that the divs are different
2001 * in the two basic maps.
2002 * In particular, if the number of divs of basic map i is smaller than
2003 * the number of divs of basic map j, then we check if j is a subset of i
2004 * and vice versa.
2006 static enum isl_change check_coalesce_subset(int i, int j,
2007 struct isl_coalesce_info *info)
2009 int changed;
2011 changed = coalesced_subset(i, j, info);
2012 if (changed < 0 || changed)
2013 return changed < 0 ? isl_change_error : isl_change_drop_second;
2015 changed = coalesced_subset(j, i, info);
2016 if (changed < 0 || changed)
2017 return changed < 0 ? isl_change_error : isl_change_drop_first;
2019 changed = coalesced_subset_with_equalities(i, j, info);
2020 if (changed < 0 || changed)
2021 return changed < 0 ? isl_change_error : isl_change_drop_second;
2023 changed = coalesced_subset_with_equalities(j, i, info);
2024 if (changed < 0 || changed)
2025 return changed < 0 ? isl_change_error : isl_change_drop_first;
2027 return isl_change_none;
2030 /* Does "bmap" involve any divs that themselves refer to divs?
2032 static int has_nested_div(__isl_keep isl_basic_map *bmap)
2034 int i;
2035 unsigned total;
2036 unsigned n_div;
2038 total = isl_basic_map_dim(bmap, isl_dim_all);
2039 n_div = isl_basic_map_dim(bmap, isl_dim_div);
2040 total -= n_div;
2042 for (i = 0; i < n_div; ++i)
2043 if (isl_seq_first_non_zero(bmap->div[i] + 2 + total,
2044 n_div) != -1)
2045 return 1;
2047 return 0;
2050 /* Return a list of affine expressions, one for each integer division
2051 * in "bmap_i". For each integer division that also appears in "bmap_j",
2052 * the affine expression is set to NaN. The number of NaNs in the list
2053 * is equal to the number of integer divisions in "bmap_j".
2054 * For the other integer divisions of "bmap_i", the corresponding
2055 * element in the list is a purely affine expression equal to the integer
2056 * division in "hull".
2057 * If no such list can be constructed, then the number of elements
2058 * in the returned list is smaller than the number of integer divisions
2059 * in "bmap_i".
2061 static __isl_give isl_aff_list *set_up_substitutions(
2062 __isl_keep isl_basic_map *bmap_i, __isl_keep isl_basic_map *bmap_j,
2063 __isl_take isl_basic_map *hull)
2065 unsigned n_div_i, n_div_j, total;
2066 isl_ctx *ctx;
2067 isl_local_space *ls;
2068 isl_basic_set *wrap_hull;
2069 isl_aff *aff_nan;
2070 isl_aff_list *list;
2071 int i, j;
2073 if (!hull)
2074 return NULL;
2076 ctx = isl_basic_map_get_ctx(hull);
2078 n_div_i = isl_basic_map_dim(bmap_i, isl_dim_div);
2079 n_div_j = isl_basic_map_dim(bmap_j, isl_dim_div);
2080 total = isl_basic_map_total_dim(bmap_i) - n_div_i;
2082 ls = isl_basic_map_get_local_space(bmap_i);
2083 ls = isl_local_space_wrap(ls);
2084 wrap_hull = isl_basic_map_wrap(hull);
2086 aff_nan = isl_aff_nan_on_domain(isl_local_space_copy(ls));
2087 list = isl_aff_list_alloc(ctx, n_div_i);
2089 j = 0;
2090 for (i = 0; i < n_div_i; ++i) {
2091 isl_aff *aff;
2093 if (j < n_div_j &&
2094 isl_seq_eq(bmap_i->div[i], bmap_j->div[j], 2 + total)) {
2095 ++j;
2096 list = isl_aff_list_add(list, isl_aff_copy(aff_nan));
2097 continue;
2099 if (n_div_i - i <= n_div_j - j)
2100 break;
2102 aff = isl_local_space_get_div(ls, i);
2103 aff = isl_aff_substitute_equalities(aff,
2104 isl_basic_set_copy(wrap_hull));
2105 aff = isl_aff_floor(aff);
2106 if (!aff)
2107 goto error;
2108 if (isl_aff_dim(aff, isl_dim_div) != 0) {
2109 isl_aff_free(aff);
2110 break;
2113 list = isl_aff_list_add(list, aff);
2116 isl_aff_free(aff_nan);
2117 isl_local_space_free(ls);
2118 isl_basic_set_free(wrap_hull);
2120 return list;
2121 error:
2122 isl_local_space_free(ls);
2123 isl_basic_set_free(wrap_hull);
2124 isl_aff_list_free(list);
2125 return NULL;
2128 /* Add variables to "tab" corresponding to the elements in "list"
2129 * that are not set to NaN.
2130 * "dim" is the offset in the variables of "tab" where we should
2131 * start considering the elements in "list".
2132 * When this function returns, the total number of variables in "tab"
2133 * is equal to "dim" plus the number of elements in "list".
2135 static int add_sub_vars(struct isl_tab *tab, __isl_keep isl_aff_list *list,
2136 int dim)
2138 int i, n;
2140 n = isl_aff_list_n_aff(list);
2141 for (i = 0; i < n; ++i) {
2142 int is_nan;
2143 isl_aff *aff;
2145 aff = isl_aff_list_get_aff(list, i);
2146 is_nan = isl_aff_is_nan(aff);
2147 isl_aff_free(aff);
2148 if (is_nan < 0)
2149 return -1;
2151 if (!is_nan && isl_tab_insert_var(tab, dim + i) < 0)
2152 return -1;
2155 return 0;
2158 /* For each element in "list" that is not set to NaN, fix the corresponding
2159 * variable in "tab" to the purely affine expression defined by the element.
2160 * "dim" is the offset in the variables of "tab" where we should
2161 * start considering the elements in "list".
2163 static int add_sub_equalities(struct isl_tab *tab,
2164 __isl_keep isl_aff_list *list, int dim)
2166 int i, n;
2167 isl_ctx *ctx;
2168 isl_vec *sub;
2169 isl_aff *aff;
2171 n = isl_aff_list_n_aff(list);
2173 ctx = isl_tab_get_ctx(tab);
2174 sub = isl_vec_alloc(ctx, 1 + dim + n);
2175 if (!sub)
2176 return -1;
2177 isl_seq_clr(sub->el + 1 + dim, n);
2179 for (i = 0; i < n; ++i) {
2180 aff = isl_aff_list_get_aff(list, i);
2181 if (!aff)
2182 goto error;
2183 if (isl_aff_is_nan(aff)) {
2184 isl_aff_free(aff);
2185 continue;
2187 isl_seq_cpy(sub->el, aff->v->el + 1, 1 + dim);
2188 isl_int_neg(sub->el[1 + dim + i], aff->v->el[0]);
2189 if (isl_tab_add_eq(tab, sub->el) < 0)
2190 goto error;
2191 isl_int_set_si(sub->el[1 + dim + i], 0);
2192 isl_aff_free(aff);
2195 isl_vec_free(sub);
2196 return 0;
2197 error:
2198 isl_aff_free(aff);
2199 isl_vec_free(sub);
2200 return -1;
2203 /* Add variables to info->tab corresponding to the elements in "list"
2204 * that are not set to NaN. The value of the added variable
2205 * is fixed to the purely affine expression defined by the element.
2206 * "dim" is the offset in the variables of info->tab where we should
2207 * start considering the elements in "list".
2208 * When this function returns, the total number of variables in info->tab
2209 * is equal to "dim" plus the number of elements in "list".
2210 * Additionally, add the div constraints that have been added info->bmap
2211 * after the tableau was constructed to info->tab. These constraints
2212 * start at position "n_ineq" in info->bmap.
2213 * The constraints need to be added to the tableau before
2214 * the equalities assigning the purely affine expression
2215 * because the position needs to match that in info->bmap.
2216 * They are frozen because the corresponding added equality is a consequence
2217 * of the two div constraints and the other equalities, meaning that
2218 * the div constraints would otherwise get marked as redundant,
2219 * while they are only redundant with respect to the extra equalities
2220 * added to the tableau, which do not appear explicitly in the basic map.
2222 static int add_subs(struct isl_coalesce_info *info,
2223 __isl_keep isl_aff_list *list, int dim, int n_ineq)
2225 int i, extra_var, extra_con;
2226 int n;
2227 unsigned n_eq = info->bmap->n_eq;
2229 if (!list)
2230 return -1;
2232 n = isl_aff_list_n_aff(list);
2233 extra_var = n - (info->tab->n_var - dim);
2234 extra_con = info->bmap->n_ineq - n_ineq;
2236 if (isl_tab_extend_vars(info->tab, extra_var) < 0)
2237 return -1;
2238 if (isl_tab_extend_cons(info->tab, extra_con + 2 * extra_var) < 0)
2239 return -1;
2240 if (add_sub_vars(info->tab, list, dim) < 0)
2241 return -1;
2243 for (i = n_ineq; i < info->bmap->n_ineq; ++i) {
2244 if (isl_tab_add_ineq(info->tab, info->bmap->ineq[i]) < 0)
2245 return -1;
2246 if (isl_tab_freeze_constraint(info->tab, n_eq + i) < 0)
2247 return -1;
2250 return add_sub_equalities(info->tab, list, dim);
2253 /* Coalesce basic map "j" into basic map "i" after adding the extra integer
2254 * divisions in "i" but not in "j" to basic map "j", with values
2255 * specified by "list". The total number of elements in "list"
2256 * is equal to the number of integer divisions in "i", while the number
2257 * of NaN elements in the list is equal to the number of integer divisions
2258 * in "j".
2259 * Adding extra integer divisions to "j" through isl_basic_map_align_divs
2260 * also adds the corresponding div constraints. These need to be added
2261 * to the corresponding tableau as well in add_subs to maintain consistency.
2263 * If no coalescing can be performed, then we need to revert basic map "j"
2264 * to its original state. We do the same if basic map "i" gets dropped
2265 * during the coalescing, even though this should not happen in practice
2266 * since we have already checked for "j" being a subset of "i"
2267 * before we reach this stage.
2269 static enum isl_change coalesce_with_subs(int i, int j,
2270 struct isl_coalesce_info *info, __isl_keep isl_aff_list *list)
2272 isl_basic_map *bmap_j;
2273 struct isl_tab_undo *snap;
2274 unsigned dim;
2275 enum isl_change change;
2276 int n_ineq;
2278 bmap_j = isl_basic_map_copy(info[j].bmap);
2279 n_ineq = info[j].bmap->n_ineq;
2280 info[j].bmap = isl_basic_map_align_divs(info[j].bmap, info[i].bmap);
2281 if (!info[j].bmap)
2282 goto error;
2284 snap = isl_tab_snap(info[j].tab);
2286 dim = isl_basic_map_dim(bmap_j, isl_dim_all);
2287 dim -= isl_basic_map_dim(bmap_j, isl_dim_div);
2288 if (add_subs(&info[j], list, dim, n_ineq) < 0)
2289 goto error;
2291 change = coalesce_local_pair(i, j, info);
2292 if (change != isl_change_none && change != isl_change_drop_first) {
2293 isl_basic_map_free(bmap_j);
2294 } else {
2295 isl_basic_map_free(info[j].bmap);
2296 info[j].bmap = bmap_j;
2298 if (isl_tab_rollback(info[j].tab, snap) < 0)
2299 return isl_change_error;
2302 return change;
2303 error:
2304 isl_basic_map_free(bmap_j);
2305 return isl_change_error;
2308 /* Check if we can coalesce basic map "j" into basic map "i" after copying
2309 * those extra integer divisions in "i" that can be simplified away
2310 * using the extra equalities in "j".
2311 * All divs are assumed to be known and not contain any nested divs.
2313 * We first check if there are any extra equalities in "j" that we
2314 * can exploit. Then we check if every integer division in "i"
2315 * either already appears in "j" or can be simplified using the
2316 * extra equalities to a purely affine expression.
2317 * If these tests succeed, then we try to coalesce the two basic maps
2318 * by introducing extra dimensions in "j" corresponding to
2319 * the extra integer divsisions "i" fixed to the corresponding
2320 * purely affine expression.
2322 static enum isl_change check_coalesce_into_eq(int i, int j,
2323 struct isl_coalesce_info *info)
2325 unsigned n_div_i, n_div_j;
2326 isl_basic_map *hull_i, *hull_j;
2327 int equal, empty;
2328 isl_aff_list *list;
2329 enum isl_change change;
2331 n_div_i = isl_basic_map_dim(info[i].bmap, isl_dim_div);
2332 n_div_j = isl_basic_map_dim(info[j].bmap, isl_dim_div);
2333 if (n_div_i <= n_div_j)
2334 return isl_change_none;
2335 if (info[j].bmap->n_eq == 0)
2336 return isl_change_none;
2338 hull_i = isl_basic_map_copy(info[i].bmap);
2339 hull_i = isl_basic_map_plain_affine_hull(hull_i);
2340 hull_j = isl_basic_map_copy(info[j].bmap);
2341 hull_j = isl_basic_map_plain_affine_hull(hull_j);
2343 hull_j = isl_basic_map_intersect(hull_j, isl_basic_map_copy(hull_i));
2344 equal = isl_basic_map_plain_is_equal(hull_i, hull_j);
2345 empty = isl_basic_map_plain_is_empty(hull_j);
2346 isl_basic_map_free(hull_i);
2348 if (equal < 0 || empty < 0)
2349 goto error;
2350 if (equal || empty) {
2351 isl_basic_map_free(hull_j);
2352 return isl_change_none;
2355 list = set_up_substitutions(info[i].bmap, info[j].bmap, hull_j);
2356 if (!list)
2357 goto error;
2358 if (isl_aff_list_n_aff(list) < n_div_i)
2359 change = isl_change_none;
2360 else
2361 change = coalesce_with_subs(i, j, info, list);
2363 isl_aff_list_free(list);
2365 return change;
2366 error:
2367 isl_basic_map_free(hull_j);
2368 return isl_change_error;
2371 /* Check if we can coalesce basic maps "i" and "j" after copying
2372 * those extra integer divisions in one of the basic maps that can
2373 * be simplified away using the extra equalities in the other basic map.
2374 * We require all divs to be known in both basic maps.
2375 * Furthermore, to simplify the comparison of div expressions,
2376 * we do not allow any nested integer divisions.
2378 static enum isl_change check_coalesce_eq(int i, int j,
2379 struct isl_coalesce_info *info)
2381 int known, nested;
2382 enum isl_change change;
2384 known = isl_basic_map_divs_known(info[i].bmap);
2385 if (known < 0 || !known)
2386 return known < 0 ? isl_change_error : isl_change_none;
2387 known = isl_basic_map_divs_known(info[j].bmap);
2388 if (known < 0 || !known)
2389 return known < 0 ? isl_change_error : isl_change_none;
2390 nested = has_nested_div(info[i].bmap);
2391 if (nested < 0 || nested)
2392 return nested < 0 ? isl_change_error : isl_change_none;
2393 nested = has_nested_div(info[j].bmap);
2394 if (nested < 0 || nested)
2395 return nested < 0 ? isl_change_error : isl_change_none;
2397 change = check_coalesce_into_eq(i, j, info);
2398 if (change != isl_change_none)
2399 return change;
2400 change = check_coalesce_into_eq(j, i, info);
2401 if (change != isl_change_none)
2402 return invert_change(change);
2404 return isl_change_none;
2407 /* Check if the union of the given pair of basic maps
2408 * can be represented by a single basic map.
2409 * If so, replace the pair by the single basic map and return
2410 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
2411 * Otherwise, return isl_change_none.
2413 * We first check if the two basic maps live in the same local space,
2414 * after aligning the divs that differ by only an integer constant.
2415 * If so, we do the complete check. Otherwise, we check if they have
2416 * the same number of integer divisions and can be coalesced, if one is
2417 * an obvious subset of the other or if the extra integer divisions
2418 * of one basic map can be simplified away using the extra equalities
2419 * of the other basic map.
2421 static enum isl_change coalesce_pair(int i, int j,
2422 struct isl_coalesce_info *info)
2424 int same;
2425 enum isl_change change;
2427 if (harmonize_divs(&info[i], &info[j]) < 0)
2428 return isl_change_error;
2429 same = same_divs(info[i].bmap, info[j].bmap);
2430 if (same < 0)
2431 return isl_change_error;
2432 if (same)
2433 return coalesce_local_pair(i, j, info);
2435 if (info[i].bmap->n_div == info[j].bmap->n_div) {
2436 change = coalesce_local_pair(i, j, info);
2437 if (change != isl_change_none)
2438 return change;
2441 change = check_coalesce_subset(i, j, info);
2442 if (change != isl_change_none)
2443 return change;
2445 return check_coalesce_eq(i, j, info);
2448 /* Return the maximum of "a" and "b".
2450 static inline int max(int a, int b)
2452 return a > b ? a : b;
2455 /* Pairwise coalesce the basic maps in the range [start1, end1[ of "info"
2456 * with those in the range [start2, end2[, skipping basic maps
2457 * that have been removed (either before or within this function).
2459 * For each basic map i in the first range, we check if it can be coalesced
2460 * with respect to any previously considered basic map j in the second range.
2461 * If i gets dropped (because it was a subset of some j), then
2462 * we can move on to the next basic map.
2463 * If j gets dropped, we need to continue checking against the other
2464 * previously considered basic maps.
2465 * If the two basic maps got fused, then we recheck the fused basic map
2466 * against the previously considered basic maps, starting at i + 1
2467 * (even if start2 is greater than i + 1).
2469 static int coalesce_range(isl_ctx *ctx, struct isl_coalesce_info *info,
2470 int start1, int end1, int start2, int end2)
2472 int i, j;
2474 for (i = end1 - 1; i >= start1; --i) {
2475 if (info[i].removed)
2476 continue;
2477 for (j = max(i + 1, start2); j < end2; ++j) {
2478 enum isl_change changed;
2480 if (info[j].removed)
2481 continue;
2482 if (info[i].removed)
2483 isl_die(ctx, isl_error_internal,
2484 "basic map unexpectedly removed",
2485 return -1);
2486 changed = coalesce_pair(i, j, info);
2487 switch (changed) {
2488 case isl_change_error:
2489 return -1;
2490 case isl_change_none:
2491 case isl_change_drop_second:
2492 continue;
2493 case isl_change_drop_first:
2494 j = end2;
2495 break;
2496 case isl_change_fuse:
2497 j = i;
2498 break;
2503 return 0;
2506 /* Pairwise coalesce the basic maps described by the "n" elements of "info".
2508 * We consider groups of basic maps that live in the same apparent
2509 * affine hull and we first coalesce within such a group before we
2510 * coalesce the elements in the group with elements of previously
2511 * considered groups. If a fuse happens during the second phase,
2512 * then we also reconsider the elements within the group.
2514 static int coalesce(isl_ctx *ctx, int n, struct isl_coalesce_info *info)
2516 int start, end;
2518 for (end = n; end > 0; end = start) {
2519 start = end - 1;
2520 while (start >= 1 &&
2521 info[start - 1].hull_hash == info[start].hull_hash)
2522 start--;
2523 if (coalesce_range(ctx, info, start, end, start, end) < 0)
2524 return -1;
2525 if (coalesce_range(ctx, info, start, end, end, n) < 0)
2526 return -1;
2529 return 0;
2532 /* Update the basic maps in "map" based on the information in "info".
2533 * In particular, remove the basic maps that have been marked removed and
2534 * update the others based on the information in the corresponding tableau.
2535 * Since we detected implicit equalities without calling
2536 * isl_basic_map_gauss, we need to do it now.
2537 * Also call isl_basic_map_simplify if we may have lost the definition
2538 * of one or more integer divisions.
2540 static __isl_give isl_map *update_basic_maps(__isl_take isl_map *map,
2541 int n, struct isl_coalesce_info *info)
2543 int i;
2545 if (!map)
2546 return NULL;
2548 for (i = n - 1; i >= 0; --i) {
2549 if (info[i].removed) {
2550 isl_basic_map_free(map->p[i]);
2551 if (i != map->n - 1)
2552 map->p[i] = map->p[map->n - 1];
2553 map->n--;
2554 continue;
2557 info[i].bmap = isl_basic_map_update_from_tab(info[i].bmap,
2558 info[i].tab);
2559 info[i].bmap = isl_basic_map_gauss(info[i].bmap, NULL);
2560 if (info[i].simplify)
2561 info[i].bmap = isl_basic_map_simplify(info[i].bmap);
2562 info[i].bmap = isl_basic_map_finalize(info[i].bmap);
2563 if (!info[i].bmap)
2564 return isl_map_free(map);
2565 ISL_F_SET(info[i].bmap, ISL_BASIC_MAP_NO_IMPLICIT);
2566 ISL_F_SET(info[i].bmap, ISL_BASIC_MAP_NO_REDUNDANT);
2567 isl_basic_map_free(map->p[i]);
2568 map->p[i] = info[i].bmap;
2569 info[i].bmap = NULL;
2572 return map;
2575 /* For each pair of basic maps in the map, check if the union of the two
2576 * can be represented by a single basic map.
2577 * If so, replace the pair by the single basic map and start over.
2579 * We factor out any (hidden) common factor from the constraint
2580 * coefficients to improve the detection of adjacent constraints.
2582 * Since we are constructing the tableaus of the basic maps anyway,
2583 * we exploit them to detect implicit equalities and redundant constraints.
2584 * This also helps the coalescing as it can ignore the redundant constraints.
2585 * In order to avoid confusion, we make all implicit equalities explicit
2586 * in the basic maps. We don't call isl_basic_map_gauss, though,
2587 * as that may affect the number of constraints.
2588 * This means that we have to call isl_basic_map_gauss at the end
2589 * of the computation (in update_basic_maps) to ensure that
2590 * the basic maps are not left in an unexpected state.
2591 * For each basic map, we also compute the hash of the apparent affine hull
2592 * for use in coalesce.
2594 struct isl_map *isl_map_coalesce(struct isl_map *map)
2596 int i;
2597 unsigned n;
2598 isl_ctx *ctx;
2599 struct isl_coalesce_info *info = NULL;
2601 map = isl_map_remove_empty_parts(map);
2602 if (!map)
2603 return NULL;
2605 if (map->n <= 1)
2606 return map;
2608 ctx = isl_map_get_ctx(map);
2609 map = isl_map_sort_divs(map);
2610 map = isl_map_cow(map);
2612 if (!map)
2613 return NULL;
2615 n = map->n;
2617 info = isl_calloc_array(map->ctx, struct isl_coalesce_info, n);
2618 if (!info)
2619 goto error;
2621 for (i = 0; i < map->n; ++i) {
2622 map->p[i] = isl_basic_map_reduce_coefficients(map->p[i]);
2623 if (!map->p[i])
2624 goto error;
2625 info[i].bmap = isl_basic_map_copy(map->p[i]);
2626 info[i].tab = isl_tab_from_basic_map(info[i].bmap, 0);
2627 if (!info[i].tab)
2628 goto error;
2629 if (!ISL_F_ISSET(info[i].bmap, ISL_BASIC_MAP_NO_IMPLICIT))
2630 if (isl_tab_detect_implicit_equalities(info[i].tab) < 0)
2631 goto error;
2632 info[i].bmap = isl_tab_make_equalities_explicit(info[i].tab,
2633 info[i].bmap);
2634 if (!info[i].bmap)
2635 goto error;
2636 if (!ISL_F_ISSET(info[i].bmap, ISL_BASIC_MAP_NO_REDUNDANT))
2637 if (isl_tab_detect_redundant(info[i].tab) < 0)
2638 goto error;
2639 if (coalesce_info_set_hull_hash(&info[i]) < 0)
2640 goto error;
2642 for (i = map->n - 1; i >= 0; --i)
2643 if (info[i].tab->empty)
2644 drop(&info[i]);
2646 if (coalesce(ctx, n, info) < 0)
2647 goto error;
2649 map = update_basic_maps(map, n, info);
2651 clear_coalesce_info(n, info);
2653 return map;
2654 error:
2655 clear_coalesce_info(n, info);
2656 isl_map_free(map);
2657 return NULL;
2660 /* For each pair of basic sets in the set, check if the union of the two
2661 * can be represented by a single basic set.
2662 * If so, replace the pair by the single basic set and start over.
2664 struct isl_set *isl_set_coalesce(struct isl_set *set)
2666 return (struct isl_set *)isl_map_coalesce((struct isl_map *)set);