isl_coalesce.c: is_adj_eq_extension: allow basic maps to have different divs
[isl.git] / isl_coalesce.c
blobe2d83fd8ff2e2c21d9b26120c742729aaaeba7da
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 * "removed" is set if this basic map has been removed from the map
156 * "simplify" is set if this basic map may have some unknown integer
157 * divisions that were not present in the input basic maps. The basic
158 * map should then be simplified such that we may be able to find
159 * a definition among the constraints.
161 * "eq" and "ineq" are only set if we are currently trying to coalesce
162 * this basic map with another basic map, in which case they represent
163 * the position of the inequalities of this basic map with respect to
164 * the other basic map. The number of elements in the "eq" array
165 * is twice the number of equalities in the "bmap", corresponding
166 * to the two inequalities that make up each equality.
168 struct isl_coalesce_info {
169 isl_basic_map *bmap;
170 struct isl_tab *tab;
171 int removed;
172 int simplify;
173 int *eq;
174 int *ineq;
177 /* Free all the allocated memory in an array
178 * of "n" isl_coalesce_info elements.
180 static void clear_coalesce_info(int n, struct isl_coalesce_info *info)
182 int i;
184 if (!info)
185 return;
187 for (i = 0; i < n; ++i) {
188 isl_basic_map_free(info[i].bmap);
189 isl_tab_free(info[i].tab);
192 free(info);
195 /* Drop the basic map represented by "info".
196 * That is, clear the memory associated to the entry and
197 * mark it as having been removed.
199 static void drop(struct isl_coalesce_info *info)
201 info->bmap = isl_basic_map_free(info->bmap);
202 isl_tab_free(info->tab);
203 info->tab = NULL;
204 info->removed = 1;
207 /* Exchange the information in "info1" with that in "info2".
209 static void exchange(struct isl_coalesce_info *info1,
210 struct isl_coalesce_info *info2)
212 struct isl_coalesce_info info;
214 info = *info1;
215 *info1 = *info2;
216 *info2 = info;
219 /* This type represents the kind of change that has been performed
220 * while trying to coalesce two basic maps.
222 * isl_change_none: nothing was changed
223 * isl_change_drop_first: the first basic map was removed
224 * isl_change_drop_second: the second basic map was removed
225 * isl_change_fuse: the two basic maps were replaced by a new basic map.
227 enum isl_change {
228 isl_change_error = -1,
229 isl_change_none = 0,
230 isl_change_drop_first,
231 isl_change_drop_second,
232 isl_change_fuse,
235 /* Update "change" based on an interchange of the first and the second
236 * basic map. That is, interchange isl_change_drop_first and
237 * isl_change_drop_second.
239 static enum isl_change invert_change(enum isl_change change)
241 switch (change) {
242 case isl_change_error:
243 return isl_change_error;
244 case isl_change_none:
245 return isl_change_none;
246 case isl_change_drop_first:
247 return isl_change_drop_second;
248 case isl_change_drop_second:
249 return isl_change_drop_first;
250 case isl_change_fuse:
251 return isl_change_fuse;
255 /* Add the valid constraints of the basic map represented by "info"
256 * to "bmap". "len" is the size of the constraints.
257 * If only one of the pair of inequalities that make up an equality
258 * is valid, then add that inequality.
260 static __isl_give isl_basic_map *add_valid_constraints(
261 __isl_take isl_basic_map *bmap, struct isl_coalesce_info *info,
262 unsigned len)
264 int k, l;
266 if (!bmap)
267 return NULL;
269 for (k = 0; k < info->bmap->n_eq; ++k) {
270 if (info->eq[2 * k] == STATUS_VALID &&
271 info->eq[2 * k + 1] == STATUS_VALID) {
272 l = isl_basic_map_alloc_equality(bmap);
273 if (l < 0)
274 return isl_basic_map_free(bmap);
275 isl_seq_cpy(bmap->eq[l], info->bmap->eq[k], len);
276 } else if (info->eq[2 * k] == STATUS_VALID) {
277 l = isl_basic_map_alloc_inequality(bmap);
278 if (l < 0)
279 return isl_basic_map_free(bmap);
280 isl_seq_neg(bmap->ineq[l], info->bmap->eq[k], len);
281 } else if (info->eq[2 * k + 1] == STATUS_VALID) {
282 l = isl_basic_map_alloc_inequality(bmap);
283 if (l < 0)
284 return isl_basic_map_free(bmap);
285 isl_seq_cpy(bmap->ineq[l], info->bmap->eq[k], len);
289 for (k = 0; k < info->bmap->n_ineq; ++k) {
290 if (info->ineq[k] != STATUS_VALID)
291 continue;
292 l = isl_basic_map_alloc_inequality(bmap);
293 if (l < 0)
294 return isl_basic_map_free(bmap);
295 isl_seq_cpy(bmap->ineq[l], info->bmap->ineq[k], len);
298 return bmap;
301 /* Is "bmap" defined by a number of (non-redundant) constraints that
302 * is greater than the number of constraints of basic maps i and j combined?
303 * Equalities are counted as two inequalities.
305 static int number_of_constraints_increases(int i, int j,
306 struct isl_coalesce_info *info,
307 __isl_keep isl_basic_map *bmap, struct isl_tab *tab)
309 int k, n_old, n_new;
311 n_old = 2 * info[i].bmap->n_eq + info[i].bmap->n_ineq;
312 n_old += 2 * info[j].bmap->n_eq + info[j].bmap->n_ineq;
314 n_new = 2 * bmap->n_eq;
315 for (k = 0; k < bmap->n_ineq; ++k)
316 if (!isl_tab_is_redundant(tab, bmap->n_eq + k))
317 ++n_new;
319 return n_new > n_old;
322 /* Replace the pair of basic maps i and j by the basic map bounded
323 * by the valid constraints in both basic maps and the constraints
324 * in extra (if not NULL).
325 * Place the fused basic map in the position that is the smallest of i and j.
327 * If "detect_equalities" is set, then look for equalities encoded
328 * as pairs of inequalities.
329 * If "check_number" is set, then the original basic maps are only
330 * replaced if the total number of constraints does not increase.
331 * While the number of integer divisions in the two basic maps
332 * is assumed to be the same, the actual definitions may be different.
333 * We only copy the definition from one of the basic map if it is
334 * the same as that of the other basic map. Otherwise, we mark
335 * the integer division as unknown and schedule for the basic map
336 * to be simplified in an attempt to recover the integer division definition.
338 static enum isl_change fuse(int i, int j, struct isl_coalesce_info *info,
339 __isl_keep isl_mat *extra, int detect_equalities, int check_number)
341 int k, l;
342 struct isl_basic_map *fused = NULL;
343 struct isl_tab *fused_tab = NULL;
344 unsigned total = isl_basic_map_total_dim(info[i].bmap);
345 unsigned extra_rows = extra ? extra->n_row : 0;
346 unsigned n_eq, n_ineq;
348 if (j < i)
349 return fuse(j, i, info, extra, detect_equalities, check_number);
351 n_eq = info[i].bmap->n_eq + info[j].bmap->n_eq;
352 n_ineq = info[i].bmap->n_ineq + info[j].bmap->n_ineq;
353 fused = isl_basic_map_alloc_space(isl_space_copy(info[i].bmap->dim),
354 info[i].bmap->n_div, n_eq, n_eq + n_ineq + extra_rows);
355 fused = add_valid_constraints(fused, &info[i], 1 + total);
356 fused = add_valid_constraints(fused, &info[j], 1 + total);
357 if (!fused)
358 goto error;
360 for (k = 0; k < info[i].bmap->n_div; ++k) {
361 int l = isl_basic_map_alloc_div(fused);
362 if (l < 0)
363 goto error;
364 if (isl_seq_eq(info[i].bmap->div[k], info[j].bmap->div[k],
365 1 + 1 + total)) {
366 isl_seq_cpy(fused->div[l], info[i].bmap->div[k],
367 1 + 1 + total);
368 } else {
369 isl_int_set_si(fused->div[l][0], 0);
370 info[i].simplify = 1;
374 for (k = 0; k < extra_rows; ++k) {
375 l = isl_basic_map_alloc_inequality(fused);
376 if (l < 0)
377 goto error;
378 isl_seq_cpy(fused->ineq[l], extra->row[k], 1 + total);
381 if (detect_equalities)
382 fused = isl_basic_map_detect_inequality_pairs(fused, NULL);
383 fused = isl_basic_map_gauss(fused, NULL);
384 ISL_F_SET(fused, ISL_BASIC_MAP_FINAL);
385 if (ISL_F_ISSET(info[i].bmap, ISL_BASIC_MAP_RATIONAL) &&
386 ISL_F_ISSET(info[j].bmap, ISL_BASIC_MAP_RATIONAL))
387 ISL_F_SET(fused, ISL_BASIC_MAP_RATIONAL);
389 fused_tab = isl_tab_from_basic_map(fused, 0);
390 if (isl_tab_detect_redundant(fused_tab) < 0)
391 goto error;
393 if (check_number &&
394 number_of_constraints_increases(i, j, info, fused, fused_tab)) {
395 isl_tab_free(fused_tab);
396 isl_basic_map_free(fused);
397 return isl_change_none;
400 info[i].simplify |= info[j].simplify;
401 isl_basic_map_free(info[i].bmap);
402 info[i].bmap = fused;
403 isl_tab_free(info[i].tab);
404 info[i].tab = fused_tab;
405 drop(&info[j]);
407 return isl_change_fuse;
408 error:
409 isl_tab_free(fused_tab);
410 isl_basic_map_free(fused);
411 return isl_change_error;
414 /* Given a pair of basic maps i and j such that all constraints are either
415 * "valid" or "cut", check if the facets corresponding to the "cut"
416 * constraints of i lie entirely within basic map j.
417 * If so, replace the pair by the basic map consisting of the valid
418 * constraints in both basic maps.
419 * Checking whether the facet lies entirely within basic map j
420 * is performed by checking whether the constraints of basic map j
421 * are valid for the facet. These tests are performed on a rational
422 * tableau to avoid the theoretical possibility that a constraint
423 * that was considered to be a cut constraint for the entire basic map i
424 * happens to be considered to be a valid constraint for the facet,
425 * even though it cuts off the same rational points.
427 * To see that we are not introducing any extra points, call the
428 * two basic maps A and B and the resulting map U and let x
429 * be an element of U \setminus ( A \cup B ).
430 * A line connecting x with an element of A \cup B meets a facet F
431 * of either A or B. Assume it is a facet of B and let c_1 be
432 * the corresponding facet constraint. We have c_1(x) < 0 and
433 * so c_1 is a cut constraint. This implies that there is some
434 * (possibly rational) point x' satisfying the constraints of A
435 * and the opposite of c_1 as otherwise c_1 would have been marked
436 * valid for A. The line connecting x and x' meets a facet of A
437 * in a (possibly rational) point that also violates c_1, but this
438 * is impossible since all cut constraints of B are valid for all
439 * cut facets of A.
440 * In case F is a facet of A rather than B, then we can apply the
441 * above reasoning to find a facet of B separating x from A \cup B first.
443 static enum isl_change check_facets(int i, int j,
444 struct isl_coalesce_info *info)
446 int k, l;
447 struct isl_tab_undo *snap, *snap2;
448 unsigned n_eq = info[i].bmap->n_eq;
450 snap = isl_tab_snap(info[i].tab);
451 if (isl_tab_mark_rational(info[i].tab) < 0)
452 return isl_change_error;
453 snap2 = isl_tab_snap(info[i].tab);
455 for (k = 0; k < info[i].bmap->n_ineq; ++k) {
456 if (info[i].ineq[k] != STATUS_CUT)
457 continue;
458 if (isl_tab_select_facet(info[i].tab, n_eq + k) < 0)
459 return isl_change_error;
460 for (l = 0; l < info[j].bmap->n_ineq; ++l) {
461 int stat;
462 if (info[j].ineq[l] != STATUS_CUT)
463 continue;
464 stat = status_in(info[j].bmap->ineq[l], info[i].tab);
465 if (stat != STATUS_VALID)
466 break;
468 if (isl_tab_rollback(info[i].tab, snap2) < 0)
469 return isl_change_error;
470 if (l < info[j].bmap->n_ineq)
471 break;
474 if (k < info[i].bmap->n_ineq) {
475 if (isl_tab_rollback(info[i].tab, snap) < 0)
476 return isl_change_error;
477 return isl_change_none;
479 return fuse(i, j, info, NULL, 0, 0);
482 /* Check if info->bmap contains the basic map represented
483 * by the tableau "tab".
484 * For each equality, we check both the constraint itself
485 * (as an inequality) and its negation. Make sure the
486 * equality is returned to its original state before returning.
488 static int contains(struct isl_coalesce_info *info, struct isl_tab *tab)
490 int k;
491 unsigned dim;
492 isl_basic_map *bmap = info->bmap;
494 dim = isl_basic_map_total_dim(bmap);
495 for (k = 0; k < bmap->n_eq; ++k) {
496 int stat;
497 isl_seq_neg(bmap->eq[k], bmap->eq[k], 1 + dim);
498 stat = status_in(bmap->eq[k], tab);
499 isl_seq_neg(bmap->eq[k], bmap->eq[k], 1 + dim);
500 if (stat != STATUS_VALID)
501 return 0;
502 stat = status_in(bmap->eq[k], tab);
503 if (stat != STATUS_VALID)
504 return 0;
507 for (k = 0; k < bmap->n_ineq; ++k) {
508 int stat;
509 if (info->ineq[k] == STATUS_REDUNDANT)
510 continue;
511 stat = status_in(bmap->ineq[k], tab);
512 if (stat != STATUS_VALID)
513 return 0;
515 return 1;
518 /* Basic map "i" has an inequality (say "k") that is adjacent
519 * to some inequality of basic map "j". All the other inequalities
520 * are valid for "j".
521 * Check if basic map "j" forms an extension of basic map "i".
523 * Note that this function is only called if some of the equalities or
524 * inequalities of basic map "j" do cut basic map "i". The function is
525 * correct even if there are no such cut constraints, but in that case
526 * the additional checks performed by this function are overkill.
528 * In particular, we replace constraint k, say f >= 0, by constraint
529 * f <= -1, add the inequalities of "j" that are valid for "i"
530 * and check if the result is a subset of basic map "j".
531 * If so, then we know that this result is exactly equal to basic map "j"
532 * since all its constraints are valid for basic map "j".
533 * By combining the valid constraints of "i" (all equalities and all
534 * inequalities except "k") and the valid constraints of "j" we therefore
535 * obtain a basic map that is equal to their union.
536 * In this case, there is no need to perform a rollback of the tableau
537 * since it is going to be destroyed in fuse().
540 * |\__ |\__
541 * | \__ | \__
542 * | \_ => | \__
543 * |_______| _ |_________\
546 * |\ |\
547 * | \ | \
548 * | \ | \
549 * | | | \
550 * | ||\ => | \
551 * | || \ | \
552 * | || | | |
553 * |__||_/ |_____/
555 static enum isl_change is_adj_ineq_extension(int i, int j,
556 struct isl_coalesce_info *info)
558 int k;
559 struct isl_tab_undo *snap;
560 unsigned n_eq = info[i].bmap->n_eq;
561 unsigned total = isl_basic_map_total_dim(info[i].bmap);
562 int r;
564 if (isl_tab_extend_cons(info[i].tab, 1 + info[j].bmap->n_ineq) < 0)
565 return isl_change_error;
567 for (k = 0; k < info[i].bmap->n_ineq; ++k)
568 if (info[i].ineq[k] == STATUS_ADJ_INEQ)
569 break;
570 if (k >= info[i].bmap->n_ineq)
571 isl_die(isl_basic_map_get_ctx(info[i].bmap), isl_error_internal,
572 "info[i].ineq should have exactly one STATUS_ADJ_INEQ",
573 return isl_change_error);
575 snap = isl_tab_snap(info[i].tab);
577 if (isl_tab_unrestrict(info[i].tab, n_eq + k) < 0)
578 return isl_change_error;
580 isl_seq_neg(info[i].bmap->ineq[k], info[i].bmap->ineq[k], 1 + total);
581 isl_int_sub_ui(info[i].bmap->ineq[k][0], info[i].bmap->ineq[k][0], 1);
582 r = isl_tab_add_ineq(info[i].tab, info[i].bmap->ineq[k]);
583 isl_seq_neg(info[i].bmap->ineq[k], info[i].bmap->ineq[k], 1 + total);
584 isl_int_sub_ui(info[i].bmap->ineq[k][0], info[i].bmap->ineq[k][0], 1);
585 if (r < 0)
586 return isl_change_error;
588 for (k = 0; k < info[j].bmap->n_ineq; ++k) {
589 if (info[j].ineq[k] != STATUS_VALID)
590 continue;
591 if (isl_tab_add_ineq(info[i].tab, info[j].bmap->ineq[k]) < 0)
592 return isl_change_error;
595 if (contains(&info[j], info[i].tab))
596 return fuse(i, j, info, NULL, 0, 0);
598 if (isl_tab_rollback(info[i].tab, snap) < 0)
599 return isl_change_error;
601 return isl_change_none;
605 /* Both basic maps have at least one inequality with and adjacent
606 * (but opposite) inequality in the other basic map.
607 * Check that there are no cut constraints and that there is only
608 * a single pair of adjacent inequalities.
609 * If so, we can replace the pair by a single basic map described
610 * by all but the pair of adjacent inequalities.
611 * Any additional points introduced lie strictly between the two
612 * adjacent hyperplanes and can therefore be integral.
614 * ____ _____
615 * / ||\ / \
616 * / || \ / \
617 * \ || \ => \ \
618 * \ || / \ /
619 * \___||_/ \_____/
621 * The test for a single pair of adjancent inequalities is important
622 * for avoiding the combination of two basic maps like the following
624 * /|
625 * / |
626 * /__|
627 * _____
628 * | |
629 * | |
630 * |___|
632 * If there are some cut constraints on one side, then we may
633 * still be able to fuse the two basic maps, but we need to perform
634 * some additional checks in is_adj_ineq_extension.
636 static enum isl_change check_adj_ineq(int i, int j,
637 struct isl_coalesce_info *info)
639 int count_i, count_j;
640 int cut_i, cut_j;
642 count_i = count(info[i].ineq, info[i].bmap->n_ineq, STATUS_ADJ_INEQ);
643 count_j = count(info[j].ineq, info[j].bmap->n_ineq, STATUS_ADJ_INEQ);
645 if (count_i != 1 && count_j != 1)
646 return isl_change_none;
648 cut_i = any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_CUT) ||
649 any(info[i].ineq, info[i].bmap->n_ineq, STATUS_CUT);
650 cut_j = any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_CUT) ||
651 any(info[j].ineq, info[j].bmap->n_ineq, STATUS_CUT);
653 if (!cut_i && !cut_j && count_i == 1 && count_j == 1)
654 return fuse(i, j, info, NULL, 0, 0);
656 if (count_i == 1 && !cut_i)
657 return is_adj_ineq_extension(i, j, info);
659 if (count_j == 1 && !cut_j)
660 return is_adj_ineq_extension(j, i, info);
662 return isl_change_none;
665 /* Basic map "i" has an inequality "k" that is adjacent to some equality
666 * of basic map "j". All the other inequalities are valid for "j".
667 * Check if basic map "j" forms an extension of basic map "i".
669 * In particular, we relax constraint "k", compute the corresponding
670 * facet and check whether it is included in the other basic map.
671 * If so, we know that relaxing the constraint extends the basic
672 * map with exactly the other basic map (we already know that this
673 * other basic map is included in the extension, because there
674 * were no "cut" inequalities in "i") and we can replace the
675 * two basic maps by this extension.
676 * Each integer division that does not have exactly the same
677 * definition in "i" and "j" is marked unknown and the basic map
678 * is scheduled to be simplified in an attempt to recover
679 * the integer division definition.
680 * Place this extension in the position that is the smallest of i and j.
681 * ____ _____
682 * / || / |
683 * / || / |
684 * \ || => \ |
685 * \ || \ |
686 * \___|| \____|
688 static enum isl_change is_adj_eq_extension(int i, int j, int k,
689 struct isl_coalesce_info *info)
691 int change = isl_change_none;
692 int super;
693 struct isl_tab_undo *snap, *snap2;
694 unsigned n_eq = info[i].bmap->n_eq;
696 if (isl_tab_is_equality(info[i].tab, n_eq + k))
697 return isl_change_none;
699 snap = isl_tab_snap(info[i].tab);
700 if (isl_tab_relax(info[i].tab, n_eq + k) < 0)
701 return isl_change_error;
702 snap2 = isl_tab_snap(info[i].tab);
703 if (isl_tab_select_facet(info[i].tab, n_eq + k) < 0)
704 return isl_change_error;
705 super = contains(&info[j], info[i].tab);
706 if (super) {
707 int l;
708 unsigned total;
710 if (isl_tab_rollback(info[i].tab, snap2) < 0)
711 return isl_change_error;
712 info[i].bmap = isl_basic_map_cow(info[i].bmap);
713 if (!info[i].bmap)
714 return isl_change_error;
715 total = isl_basic_map_total_dim(info[i].bmap);
716 for (l = 0; l < info[i].bmap->n_div; ++l)
717 if (!isl_seq_eq(info[i].bmap->div[l],
718 info[j].bmap->div[l], 1 + 1 + total)) {
719 isl_int_set_si(info[i].bmap->div[l][0], 0);
720 info[i].simplify = 1;
722 isl_int_add_ui(info[i].bmap->ineq[k][0],
723 info[i].bmap->ineq[k][0], 1);
724 ISL_F_SET(info[i].bmap, ISL_BASIC_MAP_FINAL);
725 drop(&info[j]);
726 if (j < i)
727 exchange(&info[i], &info[j]);
728 change = isl_change_fuse;
729 } else
730 if (isl_tab_rollback(info[i].tab, snap) < 0)
731 return isl_change_error;
733 return change;
736 /* Data structure that keeps track of the wrapping constraints
737 * and of information to bound the coefficients of those constraints.
739 * bound is set if we want to apply a bound on the coefficients
740 * mat contains the wrapping constraints
741 * max is the bound on the coefficients (if bound is set)
743 struct isl_wraps {
744 int bound;
745 isl_mat *mat;
746 isl_int max;
749 /* Update wraps->max to be greater than or equal to the coefficients
750 * in the equalities and inequalities of info->bmap that can be removed
751 * if we end up applying wrapping.
753 static void wraps_update_max(struct isl_wraps *wraps,
754 struct isl_coalesce_info *info)
756 int k;
757 isl_int max_k;
758 unsigned total = isl_basic_map_total_dim(info->bmap);
760 isl_int_init(max_k);
762 for (k = 0; k < info->bmap->n_eq; ++k) {
763 if (info->eq[2 * k] == STATUS_VALID &&
764 info->eq[2 * k + 1] == STATUS_VALID)
765 continue;
766 isl_seq_abs_max(info->bmap->eq[k] + 1, total, &max_k);
767 if (isl_int_abs_gt(max_k, wraps->max))
768 isl_int_set(wraps->max, max_k);
771 for (k = 0; k < info->bmap->n_ineq; ++k) {
772 if (info->ineq[k] == STATUS_VALID ||
773 info->ineq[k] == STATUS_REDUNDANT)
774 continue;
775 isl_seq_abs_max(info->bmap->ineq[k] + 1, total, &max_k);
776 if (isl_int_abs_gt(max_k, wraps->max))
777 isl_int_set(wraps->max, max_k);
780 isl_int_clear(max_k);
783 /* Initialize the isl_wraps data structure.
784 * If we want to bound the coefficients of the wrapping constraints,
785 * we set wraps->max to the largest coefficient
786 * in the equalities and inequalities that can be removed if we end up
787 * applying wrapping.
789 static void wraps_init(struct isl_wraps *wraps, __isl_take isl_mat *mat,
790 struct isl_coalesce_info *info, int i, int j)
792 isl_ctx *ctx;
794 wraps->bound = 0;
795 wraps->mat = mat;
796 if (!mat)
797 return;
798 ctx = isl_mat_get_ctx(mat);
799 wraps->bound = isl_options_get_coalesce_bounded_wrapping(ctx);
800 if (!wraps->bound)
801 return;
802 isl_int_init(wraps->max);
803 isl_int_set_si(wraps->max, 0);
804 wraps_update_max(wraps, &info[i]);
805 wraps_update_max(wraps, &info[j]);
808 /* Free the contents of the isl_wraps data structure.
810 static void wraps_free(struct isl_wraps *wraps)
812 isl_mat_free(wraps->mat);
813 if (wraps->bound)
814 isl_int_clear(wraps->max);
817 /* Is the wrapping constraint in row "row" allowed?
819 * If wraps->bound is set, we check that none of the coefficients
820 * is greater than wraps->max.
822 static int allow_wrap(struct isl_wraps *wraps, int row)
824 int i;
826 if (!wraps->bound)
827 return 1;
829 for (i = 1; i < wraps->mat->n_col; ++i)
830 if (isl_int_abs_gt(wraps->mat->row[row][i], wraps->max))
831 return 0;
833 return 1;
836 /* Wrap "ineq" (or its opposite if "negate" is set) around "bound"
837 * to include "set" and add the result in position "w" of "wraps".
838 * "len" is the total number of coefficients in "bound" and "ineq".
839 * Return 1 on success, 0 on failure and -1 on error.
840 * Wrapping can fail if the result of wrapping is equal to "bound"
841 * or if we want to bound the sizes of the coefficients and
842 * the wrapped constraint does not satisfy this bound.
844 static int add_wrap(struct isl_wraps *wraps, int w, isl_int *bound,
845 isl_int *ineq, unsigned len, __isl_keep isl_set *set, int negate)
847 isl_seq_cpy(wraps->mat->row[w], bound, len);
848 if (negate) {
849 isl_seq_neg(wraps->mat->row[w + 1], ineq, len);
850 ineq = wraps->mat->row[w + 1];
852 if (!isl_set_wrap_facet(set, wraps->mat->row[w], ineq))
853 return -1;
854 if (isl_seq_eq(wraps->mat->row[w], bound, len))
855 return 0;
856 if (!allow_wrap(wraps, w))
857 return 0;
858 return 1;
861 /* For each constraint in info->bmap that is not redundant (as determined
862 * by info->tab) and that is not a valid constraint for the other basic map,
863 * wrap the constraint around "bound" such that it includes the whole
864 * set "set" and append the resulting constraint to "wraps".
865 * Note that the constraints that are valid for the other basic map
866 * will be added to the combined basic map by default, so there is
867 * no need to wrap them.
868 * The caller wrap_in_facets even relies on this function not wrapping
869 * any constraints that are already valid.
870 * "wraps" is assumed to have been pre-allocated to the appropriate size.
871 * wraps->n_row is the number of actual wrapped constraints that have
872 * been added.
873 * If any of the wrapping problems results in a constraint that is
874 * identical to "bound", then this means that "set" is unbounded in such
875 * way that no wrapping is possible. If this happens then wraps->n_row
876 * is reset to zero.
877 * Similarly, if we want to bound the coefficients of the wrapping
878 * constraints and a newly added wrapping constraint does not
879 * satisfy the bound, then wraps->n_row is also reset to zero.
881 static int add_wraps(struct isl_wraps *wraps, struct isl_coalesce_info *info,
882 isl_int *bound, __isl_keep isl_set *set)
884 int l, m;
885 int w;
886 int added;
887 isl_basic_map *bmap = info->bmap;
888 unsigned len = 1 + isl_basic_map_total_dim(bmap);
890 w = wraps->mat->n_row;
892 for (l = 0; l < bmap->n_ineq; ++l) {
893 if (info->ineq[l] == STATUS_VALID ||
894 info->ineq[l] == STATUS_REDUNDANT)
895 continue;
896 if (isl_seq_is_neg(bound, bmap->ineq[l], len))
897 continue;
898 if (isl_seq_eq(bound, bmap->ineq[l], len))
899 continue;
900 if (isl_tab_is_redundant(info->tab, bmap->n_eq + l))
901 continue;
903 added = add_wrap(wraps, w, bound, bmap->ineq[l], len, set, 0);
904 if (added < 0)
905 return -1;
906 if (!added)
907 goto unbounded;
908 ++w;
910 for (l = 0; l < bmap->n_eq; ++l) {
911 if (isl_seq_is_neg(bound, bmap->eq[l], len))
912 continue;
913 if (isl_seq_eq(bound, bmap->eq[l], len))
914 continue;
916 for (m = 0; m < 2; ++m) {
917 if (info->eq[2 * l + m] == STATUS_VALID)
918 continue;
919 added = add_wrap(wraps, w, bound, bmap->eq[l], len,
920 set, !m);
921 if (added < 0)
922 return -1;
923 if (!added)
924 goto unbounded;
925 ++w;
929 wraps->mat->n_row = w;
930 return 0;
931 unbounded:
932 wraps->mat->n_row = 0;
933 return 0;
936 /* Check if the constraints in "wraps" from "first" until the last
937 * are all valid for the basic set represented by "tab".
938 * If not, wraps->n_row is set to zero.
940 static int check_wraps(__isl_keep isl_mat *wraps, int first,
941 struct isl_tab *tab)
943 int i;
945 for (i = first; i < wraps->n_row; ++i) {
946 enum isl_ineq_type type;
947 type = isl_tab_ineq_type(tab, wraps->row[i]);
948 if (type == isl_ineq_error)
949 return -1;
950 if (type == isl_ineq_redundant)
951 continue;
952 wraps->n_row = 0;
953 return 0;
956 return 0;
959 /* Return a set that corresponds to the non-redundant constraints
960 * (as recorded in tab) of bmap.
962 * It's important to remove the redundant constraints as some
963 * of the other constraints may have been modified after the
964 * constraints were marked redundant.
965 * In particular, a constraint may have been relaxed.
966 * Redundant constraints are ignored when a constraint is relaxed
967 * and should therefore continue to be ignored ever after.
968 * Otherwise, the relaxation might be thwarted by some of
969 * these constraints.
971 * Update the underlying set to ensure that the dimension doesn't change.
972 * Otherwise the integer divisions could get dropped if the tab
973 * turns out to be empty.
975 static __isl_give isl_set *set_from_updated_bmap(__isl_keep isl_basic_map *bmap,
976 struct isl_tab *tab)
978 isl_basic_set *bset;
980 bmap = isl_basic_map_copy(bmap);
981 bset = isl_basic_map_underlying_set(bmap);
982 bset = isl_basic_set_cow(bset);
983 bset = isl_basic_set_update_from_tab(bset, tab);
984 return isl_set_from_basic_set(bset);
987 /* Wrap the constraints of info->bmap that bound the facet defined
988 * by inequality "k" around (the opposite of) this inequality to
989 * include "set". "bound" may be used to store the negated inequality.
990 * Since the wrapped constraints are not guaranteed to contain the whole
991 * of info->bmap, we check them in check_wraps.
992 * If any of the wrapped constraints turn out to be invalid, then
993 * check_wraps will reset wrap->n_row to zero.
995 static int add_wraps_around_facet(struct isl_wraps *wraps,
996 struct isl_coalesce_info *info, int k, isl_int *bound,
997 __isl_keep isl_set *set)
999 struct isl_tab_undo *snap;
1000 int n;
1001 unsigned total = isl_basic_map_total_dim(info->bmap);
1003 snap = isl_tab_snap(info->tab);
1005 if (isl_tab_select_facet(info->tab, info->bmap->n_eq + k) < 0)
1006 return -1;
1007 if (isl_tab_detect_redundant(info->tab) < 0)
1008 return -1;
1010 isl_seq_neg(bound, info->bmap->ineq[k], 1 + total);
1012 n = wraps->mat->n_row;
1013 if (add_wraps(wraps, info, bound, set) < 0)
1014 return -1;
1016 if (isl_tab_rollback(info->tab, snap) < 0)
1017 return -1;
1018 if (check_wraps(wraps->mat, n, info->tab) < 0)
1019 return -1;
1021 return 0;
1024 /* Given a basic set i with a constraint k that is adjacent to
1025 * basic set j, check if we can wrap
1026 * both the facet corresponding to k (if "wrap_facet" is set) and basic map j
1027 * (always) around their ridges to include the other set.
1028 * If so, replace the pair of basic sets by their union.
1030 * All constraints of i (except k) are assumed to be valid or
1031 * cut constraints for j.
1032 * Wrapping the cut constraints to include basic map j may result
1033 * in constraints that are no longer valid of basic map i
1034 * we have to check that the resulting wrapping constraints are valid for i.
1035 * If "wrap_facet" is not set, then all constraints of i (except k)
1036 * are assumed to be valid for j.
1037 * ____ _____
1038 * / | / \
1039 * / || / |
1040 * \ || => \ |
1041 * \ || \ |
1042 * \___|| \____|
1045 static enum isl_change can_wrap_in_facet(int i, int j, int k,
1046 struct isl_coalesce_info *info, int wrap_facet)
1048 enum isl_change change = isl_change_none;
1049 struct isl_wraps wraps;
1050 isl_ctx *ctx;
1051 isl_mat *mat;
1052 struct isl_set *set_i = NULL;
1053 struct isl_set *set_j = NULL;
1054 struct isl_vec *bound = NULL;
1055 unsigned total = isl_basic_map_total_dim(info[i].bmap);
1057 set_i = set_from_updated_bmap(info[i].bmap, info[i].tab);
1058 set_j = set_from_updated_bmap(info[j].bmap, info[j].tab);
1059 ctx = isl_basic_map_get_ctx(info[i].bmap);
1060 mat = isl_mat_alloc(ctx, 2 * (info[i].bmap->n_eq + info[j].bmap->n_eq) +
1061 info[i].bmap->n_ineq + info[j].bmap->n_ineq,
1062 1 + total);
1063 wraps_init(&wraps, mat, info, i, j);
1064 bound = isl_vec_alloc(ctx, 1 + total);
1065 if (!set_i || !set_j || !wraps.mat || !bound)
1066 goto error;
1068 isl_seq_cpy(bound->el, info[i].bmap->ineq[k], 1 + total);
1069 isl_int_add_ui(bound->el[0], bound->el[0], 1);
1071 isl_seq_cpy(wraps.mat->row[0], bound->el, 1 + total);
1072 wraps.mat->n_row = 1;
1074 if (add_wraps(&wraps, &info[j], bound->el, set_i) < 0)
1075 goto error;
1076 if (!wraps.mat->n_row)
1077 goto unbounded;
1079 if (wrap_facet) {
1080 if (add_wraps_around_facet(&wraps, &info[i], k,
1081 bound->el, set_j) < 0)
1082 goto error;
1083 if (!wraps.mat->n_row)
1084 goto unbounded;
1087 change = fuse(i, j, info, wraps.mat, 0, 0);
1089 unbounded:
1090 wraps_free(&wraps);
1092 isl_set_free(set_i);
1093 isl_set_free(set_j);
1095 isl_vec_free(bound);
1097 return change;
1098 error:
1099 wraps_free(&wraps);
1100 isl_vec_free(bound);
1101 isl_set_free(set_i);
1102 isl_set_free(set_j);
1103 return isl_change_error;
1106 /* Given a pair of basic maps i and j such that j sticks out
1107 * of i at n cut constraints, each time by at most one,
1108 * try to compute wrapping constraints and replace the two
1109 * basic maps by a single basic map.
1110 * The other constraints of i are assumed to be valid for j.
1112 * For each cut constraint t(x) >= 0 of i, we add the relaxed version
1113 * t(x) + 1 >= 0, along with wrapping constraints for all constraints
1114 * of basic map j that bound the part of basic map j that sticks out
1115 * of the cut constraint.
1116 * In particular, we first intersect basic map j with t(x) + 1 = 0.
1117 * If the result is empty, then t(x) >= 0 was actually a valid constraint
1118 * (with respect to the integer points), so we add t(x) >= 0 instead.
1119 * Otherwise, we wrap the constraints of basic map j that are not
1120 * redundant in this intersection and that are not already valid
1121 * for basic map i over basic map i.
1122 * Note that it is sufficient to wrap the constraints to include
1123 * basic map i, because we will only wrap the constraints that do
1124 * not include basic map i already. The wrapped constraint will
1125 * therefore be more relaxed compared to the original constraint.
1126 * Since the original constraint is valid for basic map j, so is
1127 * the wrapped constraint.
1129 * If any wrapping fails, i.e., if we cannot wrap to touch
1130 * the union, then we give up.
1131 * Otherwise, the pair of basic maps is replaced by their union.
1133 static enum isl_change wrap_in_facets(int i, int j, int *cuts, int n,
1134 struct isl_coalesce_info *info)
1136 enum isl_change change = isl_change_none;
1137 struct isl_wraps wraps;
1138 isl_ctx *ctx;
1139 isl_mat *mat;
1140 isl_set *set_i = NULL;
1141 unsigned total = isl_basic_map_total_dim(info[i].bmap);
1142 int max_wrap;
1143 int k, w;
1144 struct isl_tab_undo *snap;
1146 if (isl_tab_extend_cons(info[j].tab, 1) < 0)
1147 goto error;
1149 max_wrap = 1 + 2 * info[j].bmap->n_eq + info[j].bmap->n_ineq;
1150 max_wrap *= n;
1152 set_i = set_from_updated_bmap(info[i].bmap, info[i].tab);
1153 ctx = isl_basic_map_get_ctx(info[i].bmap);
1154 mat = isl_mat_alloc(ctx, max_wrap, 1 + total);
1155 wraps_init(&wraps, mat, info, i, j);
1156 if (!set_i || !wraps.mat)
1157 goto error;
1159 snap = isl_tab_snap(info[j].tab);
1161 wraps.mat->n_row = 0;
1163 for (k = 0; k < n; ++k) {
1164 w = wraps.mat->n_row++;
1165 isl_seq_cpy(wraps.mat->row[w],
1166 info[i].bmap->ineq[cuts[k]], 1 + total);
1167 isl_int_add_ui(wraps.mat->row[w][0], wraps.mat->row[w][0], 1);
1168 if (isl_tab_add_eq(info[j].tab, wraps.mat->row[w]) < 0)
1169 goto error;
1170 if (isl_tab_detect_redundant(info[j].tab) < 0)
1171 goto error;
1173 if (info[j].tab->empty)
1174 isl_int_sub_ui(wraps.mat->row[w][0],
1175 wraps.mat->row[w][0], 1);
1176 else if (add_wraps(&wraps, &info[j],
1177 wraps.mat->row[w], set_i) < 0)
1178 goto error;
1180 if (isl_tab_rollback(info[j].tab, snap) < 0)
1181 goto error;
1183 if (!wraps.mat->n_row)
1184 break;
1187 if (k == n)
1188 change = fuse(i, j, info, wraps.mat, 0, 1);
1190 wraps_free(&wraps);
1191 isl_set_free(set_i);
1193 return change;
1194 error:
1195 wraps_free(&wraps);
1196 isl_set_free(set_i);
1197 return isl_change_error;
1200 /* Given two basic sets i and j such that i has no cut equalities,
1201 * check if relaxing all the cut inequalities of i by one turns
1202 * them into valid constraint for j and check if we can wrap in
1203 * the bits that are sticking out.
1204 * If so, replace the pair by their union.
1206 * We first check if all relaxed cut inequalities of i are valid for j
1207 * and then try to wrap in the intersections of the relaxed cut inequalities
1208 * with j.
1210 * During this wrapping, we consider the points of j that lie at a distance
1211 * of exactly 1 from i. In particular, we ignore the points that lie in
1212 * between this lower-dimensional space and the basic map i.
1213 * We can therefore only apply this to integer maps.
1214 * ____ _____
1215 * / ___|_ / \
1216 * / | | / |
1217 * \ | | => \ |
1218 * \|____| \ |
1219 * \___| \____/
1221 * _____ ______
1222 * | ____|_ | \
1223 * | | | | |
1224 * | | | => | |
1225 * |_| | | |
1226 * |_____| \______|
1228 * _______
1229 * | |
1230 * | |\ |
1231 * | | \ |
1232 * | | \ |
1233 * | | \|
1234 * | | \
1235 * | |_____\
1236 * | |
1237 * |_______|
1239 * Wrapping can fail if the result of wrapping one of the facets
1240 * around its edges does not produce any new facet constraint.
1241 * In particular, this happens when we try to wrap in unbounded sets.
1243 * _______________________________________________________________________
1245 * | ___
1246 * | | |
1247 * |_| |_________________________________________________________________
1248 * |___|
1250 * The following is not an acceptable result of coalescing the above two
1251 * sets as it includes extra integer points.
1252 * _______________________________________________________________________
1254 * |
1255 * |
1257 * \______________________________________________________________________
1259 static enum isl_change can_wrap_in_set(int i, int j,
1260 struct isl_coalesce_info *info)
1262 enum isl_change change = isl_change_none;
1263 int k, m;
1264 int n;
1265 int *cuts = NULL;
1266 isl_ctx *ctx;
1268 if (ISL_F_ISSET(info[i].bmap, ISL_BASIC_MAP_RATIONAL) ||
1269 ISL_F_ISSET(info[j].bmap, ISL_BASIC_MAP_RATIONAL))
1270 return isl_change_none;
1272 n = count(info[i].ineq, info[i].bmap->n_ineq, STATUS_CUT);
1273 if (n == 0)
1274 return isl_change_none;
1276 ctx = isl_basic_map_get_ctx(info[i].bmap);
1277 cuts = isl_alloc_array(ctx, int, n);
1278 if (!cuts)
1279 return isl_change_error;
1281 for (k = 0, m = 0; m < n; ++k) {
1282 enum isl_ineq_type type;
1284 if (info[i].ineq[k] != STATUS_CUT)
1285 continue;
1287 isl_int_add_ui(info[i].bmap->ineq[k][0],
1288 info[i].bmap->ineq[k][0], 1);
1289 type = isl_tab_ineq_type(info[j].tab, info[i].bmap->ineq[k]);
1290 isl_int_sub_ui(info[i].bmap->ineq[k][0],
1291 info[i].bmap->ineq[k][0], 1);
1292 if (type == isl_ineq_error)
1293 goto error;
1294 if (type != isl_ineq_redundant)
1295 break;
1296 cuts[m] = k;
1297 ++m;
1300 if (m == n)
1301 change = wrap_in_facets(i, j, cuts, n, info);
1303 free(cuts);
1305 return change;
1306 error:
1307 free(cuts);
1308 return isl_change_error;
1311 /* Check if either i or j has only cut inequalities that can
1312 * be used to wrap in (a facet of) the other basic set.
1313 * if so, replace the pair by their union.
1315 static enum isl_change check_wrap(int i, int j, struct isl_coalesce_info *info)
1317 enum isl_change change = isl_change_none;
1319 if (!any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_CUT))
1320 change = can_wrap_in_set(i, j, info);
1321 if (change != isl_change_none)
1322 return change;
1324 if (!any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_CUT))
1325 change = can_wrap_in_set(j, i, info);
1326 return change;
1329 /* At least one of the basic maps has an equality that is adjacent
1330 * to inequality. Make sure that only one of the basic maps has
1331 * such an equality and that the other basic map has exactly one
1332 * inequality adjacent to an equality.
1333 * We call the basic map that has the inequality "i" and the basic
1334 * map that has the equality "j".
1335 * If "i" has any "cut" (in)equality, then relaxing the inequality
1336 * by one would not result in a basic map that contains the other
1337 * basic map. However, it may still be possible to wrap in the other
1338 * basic map.
1340 static enum isl_change check_adj_eq(int i, int j,
1341 struct isl_coalesce_info *info)
1343 enum isl_change change = isl_change_none;
1344 int k;
1345 int any_cut;
1347 if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ADJ_INEQ) &&
1348 any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_ADJ_INEQ))
1349 /* ADJ EQ TOO MANY */
1350 return isl_change_none;
1352 if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ADJ_INEQ))
1353 return check_adj_eq(j, i, info);
1355 /* j has an equality adjacent to an inequality in i */
1357 if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_CUT))
1358 return isl_change_none;
1359 any_cut = any(info[i].ineq, info[i].bmap->n_ineq, STATUS_CUT);
1360 if (count(info[i].ineq, info[i].bmap->n_ineq, STATUS_ADJ_EQ) != 1 ||
1361 any(info[j].ineq, info[j].bmap->n_ineq, STATUS_ADJ_EQ) ||
1362 any(info[i].ineq, info[i].bmap->n_ineq, STATUS_ADJ_INEQ) ||
1363 any(info[j].ineq, info[j].bmap->n_ineq, STATUS_ADJ_INEQ))
1364 /* ADJ EQ TOO MANY */
1365 return isl_change_none;
1367 for (k = 0; k < info[i].bmap->n_ineq; ++k)
1368 if (info[i].ineq[k] == STATUS_ADJ_EQ)
1369 break;
1371 if (!any_cut) {
1372 change = is_adj_eq_extension(i, j, k, info);
1373 if (change != isl_change_none)
1374 return change;
1377 change = can_wrap_in_facet(i, j, k, info, any_cut);
1379 return change;
1382 /* The two basic maps lie on adjacent hyperplanes. In particular,
1383 * basic map "i" has an equality that lies parallel to basic map "j".
1384 * Check if we can wrap the facets around the parallel hyperplanes
1385 * to include the other set.
1387 * We perform basically the same operations as can_wrap_in_facet,
1388 * except that we don't need to select a facet of one of the sets.
1390 * \\ \\
1391 * \\ => \\
1392 * \ \|
1394 * If there is more than one equality of "i" adjacent to an equality of "j",
1395 * then the result will satisfy one or more equalities that are a linear
1396 * combination of these equalities. These will be encoded as pairs
1397 * of inequalities in the wrapping constraints and need to be made
1398 * explicit.
1400 static enum isl_change check_eq_adj_eq(int i, int j,
1401 struct isl_coalesce_info *info)
1403 int k;
1404 enum isl_change change = isl_change_none;
1405 int detect_equalities = 0;
1406 struct isl_wraps wraps;
1407 isl_ctx *ctx;
1408 isl_mat *mat;
1409 struct isl_set *set_i = NULL;
1410 struct isl_set *set_j = NULL;
1411 struct isl_vec *bound = NULL;
1412 unsigned total = isl_basic_map_total_dim(info[i].bmap);
1414 if (count(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ADJ_EQ) != 1)
1415 detect_equalities = 1;
1417 for (k = 0; k < 2 * info[i].bmap->n_eq ; ++k)
1418 if (info[i].eq[k] == STATUS_ADJ_EQ)
1419 break;
1421 set_i = set_from_updated_bmap(info[i].bmap, info[i].tab);
1422 set_j = set_from_updated_bmap(info[j].bmap, info[j].tab);
1423 ctx = isl_basic_map_get_ctx(info[i].bmap);
1424 mat = isl_mat_alloc(ctx, 2 * (info[i].bmap->n_eq + info[j].bmap->n_eq) +
1425 info[i].bmap->n_ineq + info[j].bmap->n_ineq,
1426 1 + total);
1427 wraps_init(&wraps, mat, info, i, j);
1428 bound = isl_vec_alloc(ctx, 1 + total);
1429 if (!set_i || !set_j || !wraps.mat || !bound)
1430 goto error;
1432 if (k % 2 == 0)
1433 isl_seq_neg(bound->el, info[i].bmap->eq[k / 2], 1 + total);
1434 else
1435 isl_seq_cpy(bound->el, info[i].bmap->eq[k / 2], 1 + total);
1436 isl_int_add_ui(bound->el[0], bound->el[0], 1);
1438 isl_seq_cpy(wraps.mat->row[0], bound->el, 1 + total);
1439 wraps.mat->n_row = 1;
1441 if (add_wraps(&wraps, &info[j], bound->el, set_i) < 0)
1442 goto error;
1443 if (!wraps.mat->n_row)
1444 goto unbounded;
1446 isl_int_sub_ui(bound->el[0], bound->el[0], 1);
1447 isl_seq_neg(bound->el, bound->el, 1 + total);
1449 isl_seq_cpy(wraps.mat->row[wraps.mat->n_row], bound->el, 1 + total);
1450 wraps.mat->n_row++;
1452 if (add_wraps(&wraps, &info[i], bound->el, set_j) < 0)
1453 goto error;
1454 if (!wraps.mat->n_row)
1455 goto unbounded;
1457 change = fuse(i, j, info, wraps.mat, detect_equalities, 0);
1459 if (0) {
1460 error: change = isl_change_error;
1462 unbounded:
1464 wraps_free(&wraps);
1465 isl_set_free(set_i);
1466 isl_set_free(set_j);
1467 isl_vec_free(bound);
1469 return change;
1472 /* Check if the union of the given pair of basic maps
1473 * can be represented by a single basic map.
1474 * If so, replace the pair by the single basic map and return
1475 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
1476 * Otherwise, return isl_change_none.
1477 * The two basic maps are assumed to live in the same local space.
1479 * We first check the effect of each constraint of one basic map
1480 * on the other basic map.
1481 * The constraint may be
1482 * redundant the constraint is redundant in its own
1483 * basic map and should be ignore and removed
1484 * in the end
1485 * valid all (integer) points of the other basic map
1486 * satisfy the constraint
1487 * separate no (integer) point of the other basic map
1488 * satisfies the constraint
1489 * cut some but not all points of the other basic map
1490 * satisfy the constraint
1491 * adj_eq the given constraint is adjacent (on the outside)
1492 * to an equality of the other basic map
1493 * adj_ineq the given constraint is adjacent (on the outside)
1494 * to an inequality of the other basic map
1496 * We consider seven cases in which we can replace the pair by a single
1497 * basic map. We ignore all "redundant" constraints.
1499 * 1. all constraints of one basic map are valid
1500 * => the other basic map is a subset and can be removed
1502 * 2. all constraints of both basic maps are either "valid" or "cut"
1503 * and the facets corresponding to the "cut" constraints
1504 * of one of the basic maps lies entirely inside the other basic map
1505 * => the pair can be replaced by a basic map consisting
1506 * of the valid constraints in both basic maps
1508 * 3. there is a single pair of adjacent inequalities
1509 * (all other constraints are "valid")
1510 * => the pair can be replaced by a basic map consisting
1511 * of the valid constraints in both basic maps
1513 * 4. one basic map has a single adjacent inequality, while the other
1514 * constraints are "valid". The other basic map has some
1515 * "cut" constraints, but replacing the adjacent inequality by
1516 * its opposite and adding the valid constraints of the other
1517 * basic map results in a subset of the other basic map
1518 * => the pair can be replaced by a basic map consisting
1519 * of the valid constraints in both basic maps
1521 * 5. there is a single adjacent pair of an inequality and an equality,
1522 * the other constraints of the basic map containing the inequality are
1523 * "valid". Moreover, if the inequality the basic map is relaxed
1524 * and then turned into an equality, then resulting facet lies
1525 * entirely inside the other basic map
1526 * => the pair can be replaced by the basic map containing
1527 * the inequality, with the inequality relaxed.
1529 * 6. there is a single adjacent pair of an inequality and an equality,
1530 * the other constraints of the basic map containing the inequality are
1531 * "valid". Moreover, the facets corresponding to both
1532 * the inequality and the equality can be wrapped around their
1533 * ridges to include the other basic map
1534 * => the pair can be replaced by a basic map consisting
1535 * of the valid constraints in both basic maps together
1536 * with all wrapping constraints
1538 * 7. one of the basic maps extends beyond the other by at most one.
1539 * Moreover, the facets corresponding to the cut constraints and
1540 * the pieces of the other basic map at offset one from these cut
1541 * constraints can be wrapped around their ridges to include
1542 * the union of the two basic maps
1543 * => the pair can be replaced by a basic map consisting
1544 * of the valid constraints in both basic maps together
1545 * with all wrapping constraints
1547 * 8. the two basic maps live in adjacent hyperplanes. In principle
1548 * such sets can always be combined through wrapping, but we impose
1549 * that there is only one such pair, to avoid overeager coalescing.
1551 * Throughout the computation, we maintain a collection of tableaus
1552 * corresponding to the basic maps. When the basic maps are dropped
1553 * or combined, the tableaus are modified accordingly.
1555 static enum isl_change coalesce_local_pair(int i, int j,
1556 struct isl_coalesce_info *info)
1558 enum isl_change change = isl_change_none;
1560 info[i].eq = info[i].ineq = NULL;
1561 info[j].eq = info[j].ineq = NULL;
1563 info[i].eq = eq_status_in(info[i].bmap, info[j].tab);
1564 if (info[i].bmap->n_eq && !info[i].eq)
1565 goto error;
1566 if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ERROR))
1567 goto error;
1568 if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_SEPARATE))
1569 goto done;
1571 info[j].eq = eq_status_in(info[j].bmap, info[i].tab);
1572 if (info[j].bmap->n_eq && !info[j].eq)
1573 goto error;
1574 if (any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_ERROR))
1575 goto error;
1576 if (any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_SEPARATE))
1577 goto done;
1579 info[i].ineq = ineq_status_in(info[i].bmap, info[i].tab, info[j].tab);
1580 if (info[i].bmap->n_ineq && !info[i].ineq)
1581 goto error;
1582 if (any(info[i].ineq, info[i].bmap->n_ineq, STATUS_ERROR))
1583 goto error;
1584 if (any(info[i].ineq, info[i].bmap->n_ineq, STATUS_SEPARATE))
1585 goto done;
1587 info[j].ineq = ineq_status_in(info[j].bmap, info[j].tab, info[i].tab);
1588 if (info[j].bmap->n_ineq && !info[j].ineq)
1589 goto error;
1590 if (any(info[j].ineq, info[j].bmap->n_ineq, STATUS_ERROR))
1591 goto error;
1592 if (any(info[j].ineq, info[j].bmap->n_ineq, STATUS_SEPARATE))
1593 goto done;
1595 if (all(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_VALID) &&
1596 all(info[i].ineq, info[i].bmap->n_ineq, STATUS_VALID)) {
1597 drop(&info[j]);
1598 change = isl_change_drop_second;
1599 } else if (all(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_VALID) &&
1600 all(info[j].ineq, info[j].bmap->n_ineq, STATUS_VALID)) {
1601 drop(&info[i]);
1602 change = isl_change_drop_first;
1603 } else if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ADJ_EQ)) {
1604 change = check_eq_adj_eq(i, j, info);
1605 } else if (any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_ADJ_EQ)) {
1606 change = check_eq_adj_eq(j, i, info);
1607 } else if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ADJ_INEQ) ||
1608 any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_ADJ_INEQ)) {
1609 change = check_adj_eq(i, j, info);
1610 } else if (any(info[i].ineq, info[i].bmap->n_ineq, STATUS_ADJ_EQ) ||
1611 any(info[j].ineq, info[j].bmap->n_ineq, STATUS_ADJ_EQ)) {
1612 /* Can't happen */
1613 /* BAD ADJ INEQ */
1614 } else if (any(info[i].ineq, info[i].bmap->n_ineq, STATUS_ADJ_INEQ) ||
1615 any(info[j].ineq, info[j].bmap->n_ineq, STATUS_ADJ_INEQ)) {
1616 change = check_adj_ineq(i, j, info);
1617 } else {
1618 if (!any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_CUT) &&
1619 !any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_CUT))
1620 change = check_facets(i, j, info);
1621 if (change == isl_change_none)
1622 change = check_wrap(i, j, info);
1625 done:
1626 free(info[i].eq);
1627 free(info[j].eq);
1628 free(info[i].ineq);
1629 free(info[j].ineq);
1630 return change;
1631 error:
1632 free(info[i].eq);
1633 free(info[j].eq);
1634 free(info[i].ineq);
1635 free(info[j].ineq);
1636 return isl_change_error;
1639 /* Do the two basic maps live in the same local space, i.e.,
1640 * do they have the same (known) divs?
1641 * If either basic map has any unknown divs, then we can only assume
1642 * that they do not live in the same local space.
1644 static int same_divs(__isl_keep isl_basic_map *bmap1,
1645 __isl_keep isl_basic_map *bmap2)
1647 int i;
1648 int known;
1649 int total;
1651 if (!bmap1 || !bmap2)
1652 return -1;
1653 if (bmap1->n_div != bmap2->n_div)
1654 return 0;
1656 if (bmap1->n_div == 0)
1657 return 1;
1659 known = isl_basic_map_divs_known(bmap1);
1660 if (known < 0 || !known)
1661 return known;
1662 known = isl_basic_map_divs_known(bmap2);
1663 if (known < 0 || !known)
1664 return known;
1666 total = isl_basic_map_total_dim(bmap1);
1667 for (i = 0; i < bmap1->n_div; ++i)
1668 if (!isl_seq_eq(bmap1->div[i], bmap2->div[i], 2 + total))
1669 return 0;
1671 return 1;
1674 /* Does "bmap" contain the basic map represented by the tableau "tab"
1675 * after expanding the divs of "bmap" to match those of "tab"?
1676 * The expansion is performed using the divs "div" and expansion "exp"
1677 * computed by the caller.
1678 * Then we check if all constraints of the expanded "bmap" are valid for "tab".
1680 static int contains_with_expanded_divs(__isl_keep isl_basic_map *bmap,
1681 struct isl_tab *tab, __isl_keep isl_mat *div, int *exp)
1683 int superset = 0;
1684 int *eq_i = NULL;
1685 int *ineq_i = NULL;
1687 bmap = isl_basic_map_copy(bmap);
1688 bmap = isl_basic_set_expand_divs(bmap, isl_mat_copy(div), exp);
1690 if (!bmap)
1691 goto error;
1693 eq_i = eq_status_in(bmap, tab);
1694 if (bmap->n_eq && !eq_i)
1695 goto error;
1696 if (any(eq_i, 2 * bmap->n_eq, STATUS_ERROR))
1697 goto error;
1698 if (any(eq_i, 2 * bmap->n_eq, STATUS_SEPARATE))
1699 goto done;
1701 ineq_i = ineq_status_in(bmap, NULL, tab);
1702 if (bmap->n_ineq && !ineq_i)
1703 goto error;
1704 if (any(ineq_i, bmap->n_ineq, STATUS_ERROR))
1705 goto error;
1706 if (any(ineq_i, bmap->n_ineq, STATUS_SEPARATE))
1707 goto done;
1709 if (all(eq_i, 2 * bmap->n_eq, STATUS_VALID) &&
1710 all(ineq_i, bmap->n_ineq, STATUS_VALID))
1711 superset = 1;
1713 done:
1714 isl_basic_map_free(bmap);
1715 free(eq_i);
1716 free(ineq_i);
1717 return superset;
1718 error:
1719 isl_basic_map_free(bmap);
1720 free(eq_i);
1721 free(ineq_i);
1722 return -1;
1725 /* Does "bmap_i" contain the basic map represented by "info_j"
1726 * after aligning the divs of "bmap_i" to those of "info_j".
1727 * Note that this can only succeed if the number of divs of "bmap_i"
1728 * is smaller than (or equal to) the number of divs of "info_j".
1730 * We first check if the divs of "bmap_i" are all known and form a subset
1731 * of those of "bmap_j". If so, we pass control over to
1732 * contains_with_expanded_divs.
1734 static int contains_after_aligning_divs(__isl_keep isl_basic_map *bmap_i,
1735 struct isl_coalesce_info *info_j)
1737 int known;
1738 isl_mat *div_i, *div_j, *div;
1739 int *exp1 = NULL;
1740 int *exp2 = NULL;
1741 isl_ctx *ctx;
1742 int subset;
1744 known = isl_basic_map_divs_known(bmap_i);
1745 if (known < 0 || !known)
1746 return known;
1748 ctx = isl_basic_map_get_ctx(bmap_i);
1750 div_i = isl_basic_map_get_divs(bmap_i);
1751 div_j = isl_basic_map_get_divs(info_j->bmap);
1753 if (!div_i || !div_j)
1754 goto error;
1756 exp1 = isl_alloc_array(ctx, int, div_i->n_row);
1757 exp2 = isl_alloc_array(ctx, int, div_j->n_row);
1758 if ((div_i->n_row && !exp1) || (div_j->n_row && !exp2))
1759 goto error;
1761 div = isl_merge_divs(div_i, div_j, exp1, exp2);
1762 if (!div)
1763 goto error;
1765 if (div->n_row == div_j->n_row)
1766 subset = contains_with_expanded_divs(bmap_i,
1767 info_j->tab, div, exp1);
1768 else
1769 subset = 0;
1771 isl_mat_free(div);
1773 isl_mat_free(div_i);
1774 isl_mat_free(div_j);
1776 free(exp2);
1777 free(exp1);
1779 return subset;
1780 error:
1781 isl_mat_free(div_i);
1782 isl_mat_free(div_j);
1783 free(exp1);
1784 free(exp2);
1785 return -1;
1788 /* Check if the basic map "j" is a subset of basic map "i",
1789 * if "i" has fewer divs that "j".
1790 * If so, remove basic map "j".
1792 * If the two basic maps have the same number of divs, then
1793 * they must necessarily be different. Otherwise, we would have
1794 * called coalesce_local_pair. We therefore don't try anything
1795 * in this case.
1797 static int coalesced_subset(int i, int j, struct isl_coalesce_info *info)
1799 int superset;
1801 if (info[i].bmap->n_div >= info[j].bmap->n_div)
1802 return 0;
1804 superset = contains_after_aligning_divs(info[i].bmap, &info[j]);
1805 if (superset < 0)
1806 return -1;
1807 if (superset)
1808 drop(&info[j]);
1810 return superset;
1813 /* Check if basic map "j" is a subset of basic map "i" after
1814 * exploiting the extra equalities of "j" to simplify the divs of "i".
1815 * If so, remove basic map "j".
1817 * If "j" does not have any equalities or if they are the same
1818 * as those of "i", then we cannot exploit them to simplify the divs.
1819 * Similarly, if there are no divs in "i", then they cannot be simplified.
1820 * If, on the other hand, the affine hulls of "i" and "j" do not intersect,
1821 * then "j" cannot be a subset of "i".
1823 * Otherwise, we intersect "i" with the affine hull of "j" and then
1824 * check if "j" is a subset of the result after aligning the divs.
1825 * If so, then "j" is definitely a subset of "i" and can be removed.
1826 * Note that if after intersection with the affine hull of "j".
1827 * "i" still has more divs than "j", then there is no way we can
1828 * align the divs of "i" to those of "j".
1830 static int coalesced_subset_with_equalities(int i, int j,
1831 struct isl_coalesce_info *info)
1833 isl_basic_map *hull_i, *hull_j, *bmap_i;
1834 int equal, empty, subset;
1836 if (info[j].bmap->n_eq == 0)
1837 return 0;
1838 if (info[i].bmap->n_div == 0)
1839 return 0;
1841 hull_i = isl_basic_map_copy(info[i].bmap);
1842 hull_i = isl_basic_map_plain_affine_hull(hull_i);
1843 hull_j = isl_basic_map_copy(info[j].bmap);
1844 hull_j = isl_basic_map_plain_affine_hull(hull_j);
1846 hull_j = isl_basic_map_intersect(hull_j, isl_basic_map_copy(hull_i));
1847 equal = isl_basic_map_plain_is_equal(hull_i, hull_j);
1848 empty = isl_basic_map_plain_is_empty(hull_j);
1849 isl_basic_map_free(hull_i);
1851 if (equal < 0 || equal || empty < 0 || empty) {
1852 isl_basic_map_free(hull_j);
1853 return equal < 0 || empty < 0 ? -1 : 0;
1856 bmap_i = isl_basic_map_copy(info[i].bmap);
1857 bmap_i = isl_basic_map_intersect(bmap_i, hull_j);
1858 if (!bmap_i)
1859 return -1;
1861 if (bmap_i->n_div > info[j].bmap->n_div) {
1862 isl_basic_map_free(bmap_i);
1863 return 0;
1866 subset = contains_after_aligning_divs(bmap_i, &info[j]);
1868 isl_basic_map_free(bmap_i);
1870 if (subset < 0)
1871 return -1;
1872 if (subset)
1873 drop(&info[j]);
1875 return subset;
1878 /* Check if one of the basic maps is a subset of the other and, if so,
1879 * drop the subset.
1880 * Note that we only perform any test if the number of divs is different
1881 * in the two basic maps. In case the number of divs is the same,
1882 * we have already established that the divs are different
1883 * in the two basic maps.
1884 * In particular, if the number of divs of basic map i is smaller than
1885 * the number of divs of basic map j, then we check if j is a subset of i
1886 * and vice versa.
1888 static enum isl_change check_coalesce_subset(int i, int j,
1889 struct isl_coalesce_info *info)
1891 int changed;
1893 changed = coalesced_subset(i, j, info);
1894 if (changed < 0 || changed)
1895 return changed < 0 ? isl_change_error : isl_change_drop_second;
1897 changed = coalesced_subset(j, i, info);
1898 if (changed < 0 || changed)
1899 return changed < 0 ? isl_change_error : isl_change_drop_first;
1901 changed = coalesced_subset_with_equalities(i, j, info);
1902 if (changed < 0 || changed)
1903 return changed < 0 ? isl_change_error : isl_change_drop_second;
1905 changed = coalesced_subset_with_equalities(j, i, info);
1906 if (changed < 0 || changed)
1907 return changed < 0 ? isl_change_error : isl_change_drop_first;
1909 return isl_change_none;
1912 /* Does "bmap" involve any divs that themselves refer to divs?
1914 static int has_nested_div(__isl_keep isl_basic_map *bmap)
1916 int i;
1917 unsigned total;
1918 unsigned n_div;
1920 total = isl_basic_map_dim(bmap, isl_dim_all);
1921 n_div = isl_basic_map_dim(bmap, isl_dim_div);
1922 total -= n_div;
1924 for (i = 0; i < n_div; ++i)
1925 if (isl_seq_first_non_zero(bmap->div[i] + 2 + total,
1926 n_div) != -1)
1927 return 1;
1929 return 0;
1932 /* Return a list of affine expressions, one for each integer division
1933 * in "bmap_i". For each integer division that also appears in "bmap_j",
1934 * the affine expression is set to NaN. The number of NaNs in the list
1935 * is equal to the number of integer divisions in "bmap_j".
1936 * For the other integer divisions of "bmap_i", the corresponding
1937 * element in the list is a purely affine expression equal to the integer
1938 * division in "hull".
1939 * If no such list can be constructed, then the number of elements
1940 * in the returned list is smaller than the number of integer divisions
1941 * in "bmap_i".
1943 static __isl_give isl_aff_list *set_up_substitutions(
1944 __isl_keep isl_basic_map *bmap_i, __isl_keep isl_basic_map *bmap_j,
1945 __isl_take isl_basic_map *hull)
1947 unsigned n_div_i, n_div_j, total;
1948 isl_ctx *ctx;
1949 isl_local_space *ls;
1950 isl_basic_set *wrap_hull;
1951 isl_aff *aff_nan;
1952 isl_aff_list *list;
1953 int i, j;
1955 if (!hull)
1956 return NULL;
1958 ctx = isl_basic_map_get_ctx(hull);
1960 n_div_i = isl_basic_map_dim(bmap_i, isl_dim_div);
1961 n_div_j = isl_basic_map_dim(bmap_j, isl_dim_div);
1962 total = isl_basic_map_total_dim(bmap_i) - n_div_i;
1964 ls = isl_basic_map_get_local_space(bmap_i);
1965 ls = isl_local_space_wrap(ls);
1966 wrap_hull = isl_basic_map_wrap(hull);
1968 aff_nan = isl_aff_nan_on_domain(isl_local_space_copy(ls));
1969 list = isl_aff_list_alloc(ctx, n_div_i);
1971 j = 0;
1972 for (i = 0; i < n_div_i; ++i) {
1973 isl_aff *aff;
1975 if (j < n_div_j &&
1976 isl_seq_eq(bmap_i->div[i], bmap_j->div[j], 2 + total)) {
1977 ++j;
1978 list = isl_aff_list_add(list, isl_aff_copy(aff_nan));
1979 continue;
1981 if (n_div_i - i <= n_div_j - j)
1982 break;
1984 aff = isl_local_space_get_div(ls, i);
1985 aff = isl_aff_substitute_equalities(aff,
1986 isl_basic_set_copy(wrap_hull));
1987 aff = isl_aff_floor(aff);
1988 if (!aff)
1989 goto error;
1990 if (isl_aff_dim(aff, isl_dim_div) != 0) {
1991 isl_aff_free(aff);
1992 break;
1995 list = isl_aff_list_add(list, aff);
1998 isl_aff_free(aff_nan);
1999 isl_local_space_free(ls);
2000 isl_basic_set_free(wrap_hull);
2002 return list;
2003 error:
2004 isl_local_space_free(ls);
2005 isl_basic_set_free(wrap_hull);
2006 isl_aff_list_free(list);
2007 return NULL;
2010 /* Add variables to "tab" corresponding to the elements in "list"
2011 * that are not set to NaN. The value of the added variable
2012 * is fixed to the purely affine expression defined by the element.
2013 * "dim" is the offset in the variables of "tab" where we should
2014 * start considering the elements in "list".
2015 * When this function returns, the total number of variables in "tab"
2016 * is equal to "dim" plus the number of elements in "list".
2018 static int add_subs(struct isl_tab *tab, __isl_keep isl_aff_list *list, int dim)
2020 int i, extra;
2021 isl_ctx *ctx;
2022 isl_vec *sub;
2023 isl_aff *aff;
2024 int n;
2026 if (!list)
2027 return -1;
2029 n = isl_aff_list_n_aff(list);
2030 extra = n - (tab->n_var - dim);
2032 if (isl_tab_extend_vars(tab, extra) < 0)
2033 return -1;
2034 if (isl_tab_extend_cons(tab, 2 * extra) < 0)
2035 return -1;
2037 ctx = isl_tab_get_ctx(tab);
2038 sub = isl_vec_alloc(ctx, 1 + dim + n);
2039 if (!sub)
2040 return -1;
2041 isl_seq_clr(sub->el + 1 + dim, n);
2043 for (i = 0; i < n; ++i) {
2044 aff = isl_aff_list_get_aff(list, i);
2045 if (!aff)
2046 goto error;
2047 if (isl_aff_is_nan(aff)) {
2048 isl_aff_free(aff);
2049 continue;
2051 if (isl_tab_insert_var(tab, dim + i) < 0)
2052 goto error;
2053 isl_seq_cpy(sub->el, aff->v->el + 1, 1 + dim);
2054 isl_int_neg(sub->el[1 + dim + i], aff->v->el[0]);
2055 if (isl_tab_add_eq(tab, sub->el) < 0)
2056 goto error;
2057 isl_int_set_si(sub->el[1 + dim + i], 0);
2058 isl_aff_free(aff);
2061 isl_vec_free(sub);
2062 return 0;
2063 error:
2064 isl_aff_free(aff);
2065 isl_vec_free(sub);
2066 return -1;
2069 /* Coalesce basic map "j" into basic map "i" after adding the extra integer
2070 * divisions in "i" but not in "j" to basic map "j", with values
2071 * specified by "list". The total number of elements in "list"
2072 * is equal to the number of integer divisions in "i", while the number
2073 * of NaN elements in the list is equal to the number of integer divisions
2074 * in "j".
2075 * If no coalescing can be performed, then we need to revert basic map "j"
2076 * to its original state. We do the same if basic map "i" gets dropped
2077 * during the coalescing, even though this should not happen in practice
2078 * since we have already checked for "j" being a subset of "i"
2079 * before we reach this stage.
2081 static enum isl_change coalesce_with_subs(int i, int j,
2082 struct isl_coalesce_info *info, __isl_keep isl_aff_list *list)
2084 isl_basic_map *bmap_j;
2085 struct isl_tab_undo *snap;
2086 unsigned dim;
2087 enum isl_change change;
2089 bmap_j = isl_basic_map_copy(info[j].bmap);
2090 info[j].bmap = isl_basic_map_align_divs(info[j].bmap, info[i].bmap);
2091 if (!info[j].bmap)
2092 goto error;
2094 snap = isl_tab_snap(info[j].tab);
2096 dim = isl_basic_map_dim(bmap_j, isl_dim_all);
2097 dim -= isl_basic_map_dim(bmap_j, isl_dim_div);
2098 if (add_subs(info[j].tab, list, dim) < 0)
2099 goto error;
2101 change = coalesce_local_pair(i, j, info);
2102 if (change != isl_change_none && change != isl_change_drop_first) {
2103 isl_basic_map_free(bmap_j);
2104 } else {
2105 isl_basic_map_free(info[j].bmap);
2106 info[j].bmap = bmap_j;
2108 if (isl_tab_rollback(info[j].tab, snap) < 0)
2109 return isl_change_error;
2112 return change;
2113 error:
2114 isl_basic_map_free(bmap_j);
2115 return isl_change_error;
2118 /* Check if we can coalesce basic map "j" into basic map "i" after copying
2119 * those extra integer divisions in "i" that can be simplified away
2120 * using the extra equalities in "j".
2121 * All divs are assumed to be known and not contain any nested divs.
2123 * We first check if there are any extra equalities in "j" that we
2124 * can exploit. Then we check if every integer division in "i"
2125 * either already appears in "j" or can be simplified using the
2126 * extra equalities to a purely affine expression.
2127 * If these tests succeed, then we try to coalesce the two basic maps
2128 * by introducing extra dimensions in "j" corresponding to
2129 * the extra integer divsisions "i" fixed to the corresponding
2130 * purely affine expression.
2132 static enum isl_change check_coalesce_into_eq(int i, int j,
2133 struct isl_coalesce_info *info)
2135 unsigned n_div_i, n_div_j;
2136 isl_basic_map *hull_i, *hull_j;
2137 int equal, empty;
2138 isl_aff_list *list;
2139 enum isl_change change;
2141 n_div_i = isl_basic_map_dim(info[i].bmap, isl_dim_div);
2142 n_div_j = isl_basic_map_dim(info[j].bmap, isl_dim_div);
2143 if (n_div_i <= n_div_j)
2144 return isl_change_none;
2145 if (info[j].bmap->n_eq == 0)
2146 return isl_change_none;
2148 hull_i = isl_basic_map_copy(info[i].bmap);
2149 hull_i = isl_basic_map_plain_affine_hull(hull_i);
2150 hull_j = isl_basic_map_copy(info[j].bmap);
2151 hull_j = isl_basic_map_plain_affine_hull(hull_j);
2153 hull_j = isl_basic_map_intersect(hull_j, isl_basic_map_copy(hull_i));
2154 equal = isl_basic_map_plain_is_equal(hull_i, hull_j);
2155 empty = isl_basic_map_plain_is_empty(hull_j);
2156 isl_basic_map_free(hull_i);
2158 if (equal < 0 || empty < 0)
2159 goto error;
2160 if (equal || empty) {
2161 isl_basic_map_free(hull_j);
2162 return isl_change_none;
2165 list = set_up_substitutions(info[i].bmap, info[j].bmap, hull_j);
2166 if (!list)
2167 goto error;
2168 if (isl_aff_list_n_aff(list) < n_div_i)
2169 change = isl_change_none;
2170 else
2171 change = coalesce_with_subs(i, j, info, list);
2173 isl_aff_list_free(list);
2175 return change;
2176 error:
2177 isl_basic_map_free(hull_j);
2178 return isl_change_error;
2181 /* Check if we can coalesce basic maps "i" and "j" after copying
2182 * those extra integer divisions in one of the basic maps that can
2183 * be simplified away using the extra equalities in the other basic map.
2184 * We require all divs to be known in both basic maps.
2185 * Furthermore, to simplify the comparison of div expressions,
2186 * we do not allow any nested integer divisions.
2188 static enum isl_change check_coalesce_eq(int i, int j,
2189 struct isl_coalesce_info *info)
2191 int known, nested;
2192 enum isl_change change;
2194 known = isl_basic_map_divs_known(info[i].bmap);
2195 if (known < 0 || !known)
2196 return known < 0 ? isl_change_error : isl_change_none;
2197 known = isl_basic_map_divs_known(info[j].bmap);
2198 if (known < 0 || !known)
2199 return known < 0 ? isl_change_error : isl_change_none;
2200 nested = has_nested_div(info[i].bmap);
2201 if (nested < 0 || nested)
2202 return nested < 0 ? isl_change_error : isl_change_none;
2203 nested = has_nested_div(info[j].bmap);
2204 if (nested < 0 || nested)
2205 return nested < 0 ? isl_change_error : isl_change_none;
2207 change = check_coalesce_into_eq(i, j, info);
2208 if (change != isl_change_none)
2209 return change;
2210 change = check_coalesce_into_eq(j, i, info);
2211 if (change != isl_change_none)
2212 return invert_change(change);
2214 return isl_change_none;
2217 /* Check if the union of the given pair of basic maps
2218 * can be represented by a single basic map.
2219 * If so, replace the pair by the single basic map and return
2220 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
2221 * Otherwise, return isl_change_none.
2223 * We first check if the two basic maps live in the same local space.
2224 * If so, we do the complete check. Otherwise, we check if one is
2225 * an obvious subset of the other or if the extra integer divisions
2226 * of one basic map can be simplified away using the extra equalities
2227 * of the other basic map.
2229 static enum isl_change coalesce_pair(int i, int j,
2230 struct isl_coalesce_info *info)
2232 int same;
2233 enum isl_change change;
2235 same = same_divs(info[i].bmap, info[j].bmap);
2236 if (same < 0)
2237 return isl_change_error;
2238 if (same)
2239 return coalesce_local_pair(i, j, info);
2241 change = check_coalesce_subset(i, j, info);
2242 if (change != isl_change_none)
2243 return change;
2245 return check_coalesce_eq(i, j, info);
2248 /* Pairwise coalesce the basic maps described by the "n" elements of "info",
2249 * skipping basic maps that have been removed (either before or within
2250 * this function).
2252 * For each basic map i, we check if it can be coalesced with respect
2253 * to any previously considered basic map j.
2254 * If i gets dropped (because it was a subset of some j), then
2255 * we can move on to the next basic map.
2256 * If j gets dropped, we need to continue checking against the other
2257 * previously considered basic maps.
2258 * If the two basic maps got fused, then we recheck the fused basic map
2259 * against the previously considered basic maps.
2261 static int coalesce(isl_ctx *ctx, int n, struct isl_coalesce_info *info)
2263 int i, j;
2265 for (i = n - 2; i >= 0; --i) {
2266 if (info[i].removed)
2267 continue;
2268 for (j = i + 1; j < n; ++j) {
2269 enum isl_change changed;
2271 if (info[j].removed)
2272 continue;
2273 if (info[i].removed)
2274 isl_die(ctx, isl_error_internal,
2275 "basic map unexpectedly removed",
2276 return -1);
2277 changed = coalesce_pair(i, j, info);
2278 switch (changed) {
2279 case isl_change_error:
2280 return -1;
2281 case isl_change_none:
2282 case isl_change_drop_second:
2283 continue;
2284 case isl_change_drop_first:
2285 j = n;
2286 break;
2287 case isl_change_fuse:
2288 j = i;
2289 break;
2294 return 0;
2297 /* Update the basic maps in "map" based on the information in "info".
2298 * In particular, remove the basic maps that have been marked removed and
2299 * update the others based on the information in the corresponding tableau.
2300 * Since we detected implicit equalities without calling
2301 * isl_basic_map_gauss, we need to do it now.
2302 * Also call isl_basic_map_simplify if we may have lost the definition
2303 * of one or more integer divisions.
2305 static __isl_give isl_map *update_basic_maps(__isl_take isl_map *map,
2306 int n, struct isl_coalesce_info *info)
2308 int i;
2310 if (!map)
2311 return NULL;
2313 for (i = n - 1; i >= 0; --i) {
2314 if (info[i].removed) {
2315 isl_basic_map_free(map->p[i]);
2316 if (i != map->n - 1)
2317 map->p[i] = map->p[map->n - 1];
2318 map->n--;
2319 continue;
2322 info[i].bmap = isl_basic_map_update_from_tab(info[i].bmap,
2323 info[i].tab);
2324 info[i].bmap = isl_basic_map_gauss(info[i].bmap, NULL);
2325 if (info[i].simplify)
2326 info[i].bmap = isl_basic_map_simplify(info[i].bmap);
2327 info[i].bmap = isl_basic_map_finalize(info[i].bmap);
2328 if (!info[i].bmap)
2329 return isl_map_free(map);
2330 ISL_F_SET(info[i].bmap, ISL_BASIC_MAP_NO_IMPLICIT);
2331 ISL_F_SET(info[i].bmap, ISL_BASIC_MAP_NO_REDUNDANT);
2332 isl_basic_map_free(map->p[i]);
2333 map->p[i] = info[i].bmap;
2334 info[i].bmap = NULL;
2337 return map;
2340 /* For each pair of basic maps in the map, check if the union of the two
2341 * can be represented by a single basic map.
2342 * If so, replace the pair by the single basic map and start over.
2344 * We factor out any (hidden) common factor from the constraint
2345 * coefficients to improve the detection of adjacent constraints.
2347 * Since we are constructing the tableaus of the basic maps anyway,
2348 * we exploit them to detect implicit equalities and redundant constraints.
2349 * This also helps the coalescing as it can ignore the redundant constraints.
2350 * In order to avoid confusion, we make all implicit equalities explicit
2351 * in the basic maps. We don't call isl_basic_map_gauss, though,
2352 * as that may affect the number of constraints.
2353 * This means that we have to call isl_basic_map_gauss at the end
2354 * of the computation (in update_basic_maps) to ensure that
2355 * the basic maps are not left in an unexpected state.
2357 struct isl_map *isl_map_coalesce(struct isl_map *map)
2359 int i;
2360 unsigned n;
2361 isl_ctx *ctx;
2362 struct isl_coalesce_info *info = NULL;
2364 map = isl_map_remove_empty_parts(map);
2365 if (!map)
2366 return NULL;
2368 if (map->n <= 1)
2369 return map;
2371 ctx = isl_map_get_ctx(map);
2372 map = isl_map_sort_divs(map);
2373 map = isl_map_cow(map);
2375 if (!map)
2376 return NULL;
2378 n = map->n;
2380 info = isl_calloc_array(map->ctx, struct isl_coalesce_info, n);
2381 if (!info)
2382 goto error;
2384 for (i = 0; i < map->n; ++i) {
2385 map->p[i] = isl_basic_map_reduce_coefficients(map->p[i]);
2386 if (!map->p[i])
2387 goto error;
2388 info[i].bmap = isl_basic_map_copy(map->p[i]);
2389 info[i].tab = isl_tab_from_basic_map(info[i].bmap, 0);
2390 if (!info[i].tab)
2391 goto error;
2392 if (!ISL_F_ISSET(info[i].bmap, ISL_BASIC_MAP_NO_IMPLICIT))
2393 if (isl_tab_detect_implicit_equalities(info[i].tab) < 0)
2394 goto error;
2395 info[i].bmap = isl_tab_make_equalities_explicit(info[i].tab,
2396 info[i].bmap);
2397 if (!info[i].bmap)
2398 goto error;
2399 if (!ISL_F_ISSET(info[i].bmap, ISL_BASIC_MAP_NO_REDUNDANT))
2400 if (isl_tab_detect_redundant(info[i].tab) < 0)
2401 goto error;
2403 for (i = map->n - 1; i >= 0; --i)
2404 if (info[i].tab->empty)
2405 drop(&info[i]);
2407 if (coalesce(ctx, n, info) < 0)
2408 goto error;
2410 map = update_basic_maps(map, n, info);
2412 clear_coalesce_info(n, info);
2414 return map;
2415 error:
2416 clear_coalesce_info(n, info);
2417 isl_map_free(map);
2418 return NULL;
2421 /* For each pair of basic sets in the set, check if the union of the two
2422 * can be represented by a single basic set.
2423 * If so, replace the pair by the single basic set and start over.
2425 struct isl_set *isl_set_coalesce(struct isl_set *set)
2427 return (struct isl_set *)isl_map_coalesce((struct isl_map *)set);