isl_coalesce.c: fuse: copy valid half of equality
[isl.git] / isl_coalesce.c
blob4fce0ba19bf37e629f95271a4ce11531d94db85b
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2012-2013 Ecole Normale Superieure
5 * Copyright 2014 INRIA Rocquencourt
7 * Use of this software is governed by the MIT license
9 * Written by Sven Verdoolaege, K.U.Leuven, Departement
10 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
11 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
12 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
13 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
14 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
15 * B.P. 105 - 78153 Le Chesnay, France
18 #include "isl_map_private.h"
19 #include <isl_seq.h>
20 #include <isl/options.h>
21 #include "isl_tab.h"
22 #include <isl_mat_private.h>
23 #include <isl_local_space_private.h>
24 #include <isl_vec_private.h>
26 #define STATUS_ERROR -1
27 #define STATUS_REDUNDANT 1
28 #define STATUS_VALID 2
29 #define STATUS_SEPARATE 3
30 #define STATUS_CUT 4
31 #define STATUS_ADJ_EQ 5
32 #define STATUS_ADJ_INEQ 6
34 static int status_in(isl_int *ineq, struct isl_tab *tab)
36 enum isl_ineq_type type = isl_tab_ineq_type(tab, ineq);
37 switch (type) {
38 default:
39 case isl_ineq_error: return STATUS_ERROR;
40 case isl_ineq_redundant: return STATUS_VALID;
41 case isl_ineq_separate: return STATUS_SEPARATE;
42 case isl_ineq_cut: return STATUS_CUT;
43 case isl_ineq_adj_eq: return STATUS_ADJ_EQ;
44 case isl_ineq_adj_ineq: return STATUS_ADJ_INEQ;
48 /* Compute the position of the equalities of basic map "bmap_i"
49 * with respect to the basic map represented by "tab_j".
50 * The resulting array has twice as many entries as the number
51 * of equalities corresponding to the two inequalties to which
52 * each equality corresponds.
54 static int *eq_status_in(__isl_keep isl_basic_map *bmap_i,
55 struct isl_tab *tab_j)
57 int k, l;
58 int *eq = isl_calloc_array(bmap_i->ctx, int, 2 * bmap_i->n_eq);
59 unsigned dim;
61 if (!eq)
62 return NULL;
64 dim = isl_basic_map_total_dim(bmap_i);
65 for (k = 0; k < bmap_i->n_eq; ++k) {
66 for (l = 0; l < 2; ++l) {
67 isl_seq_neg(bmap_i->eq[k], bmap_i->eq[k], 1+dim);
68 eq[2 * k + l] = status_in(bmap_i->eq[k], tab_j);
69 if (eq[2 * k + l] == STATUS_ERROR)
70 goto error;
72 if (eq[2 * k] == STATUS_SEPARATE ||
73 eq[2 * k + 1] == STATUS_SEPARATE)
74 break;
77 return eq;
78 error:
79 free(eq);
80 return NULL;
83 /* Compute the position of the inequalities of basic map "bmap_i"
84 * (also represented by "tab_i", if not NULL) with respect to the basic map
85 * represented by "tab_j".
87 static int *ineq_status_in(__isl_keep isl_basic_map *bmap_i,
88 struct isl_tab *tab_i, struct isl_tab *tab_j)
90 int k;
91 unsigned n_eq = bmap_i->n_eq;
92 int *ineq = isl_calloc_array(bmap_i->ctx, int, bmap_i->n_ineq);
94 if (!ineq)
95 return NULL;
97 for (k = 0; k < bmap_i->n_ineq; ++k) {
98 if (tab_i && isl_tab_is_redundant(tab_i, n_eq + k)) {
99 ineq[k] = STATUS_REDUNDANT;
100 continue;
102 ineq[k] = status_in(bmap_i->ineq[k], tab_j);
103 if (ineq[k] == STATUS_ERROR)
104 goto error;
105 if (ineq[k] == STATUS_SEPARATE)
106 break;
109 return ineq;
110 error:
111 free(ineq);
112 return NULL;
115 static int any(int *con, unsigned len, int status)
117 int i;
119 for (i = 0; i < len ; ++i)
120 if (con[i] == status)
121 return 1;
122 return 0;
125 static int count(int *con, unsigned len, int status)
127 int i;
128 int c = 0;
130 for (i = 0; i < len ; ++i)
131 if (con[i] == status)
132 c++;
133 return c;
136 static int all(int *con, unsigned len, int status)
138 int i;
140 for (i = 0; i < len ; ++i) {
141 if (con[i] == STATUS_REDUNDANT)
142 continue;
143 if (con[i] != status)
144 return 0;
146 return 1;
149 /* Internal information associated to a basic map in a map
150 * that is to be coalesced by isl_map_coalesce.
152 * "bmap" is the basic map itself (or NULL if "removed" is set)
153 * "tab" is the corresponding tableau (or NULL if "removed" is set)
154 * "removed" is set if this basic map has been removed from the map
156 * "eq" and "ineq" are only set if we are currently trying to coalesce
157 * this basic map with another basic map, in which case they represent
158 * the position of the inequalities of this basic map with respect to
159 * the other basic map. The number of elements in the "eq" array
160 * is twice the number of equalities in the "bmap", corresponding
161 * to the two inequalities that make up each equality.
163 struct isl_coalesce_info {
164 isl_basic_map *bmap;
165 struct isl_tab *tab;
166 int removed;
167 int *eq;
168 int *ineq;
171 /* Free all the allocated memory in an array
172 * of "n" isl_coalesce_info elements.
174 static void clear_coalesce_info(int n, struct isl_coalesce_info *info)
176 int i;
178 if (!info)
179 return;
181 for (i = 0; i < n; ++i) {
182 isl_basic_map_free(info[i].bmap);
183 isl_tab_free(info[i].tab);
186 free(info);
189 /* Drop the basic map represented by "info".
190 * That is, clear the memory associated to the entry and
191 * mark it as having been removed.
193 static void drop(struct isl_coalesce_info *info)
195 info->bmap = isl_basic_map_free(info->bmap);
196 isl_tab_free(info->tab);
197 info->tab = NULL;
198 info->removed = 1;
201 /* Exchange the information in "info1" with that in "info2".
203 static void exchange(struct isl_coalesce_info *info1,
204 struct isl_coalesce_info *info2)
206 struct isl_coalesce_info info;
208 info = *info1;
209 *info1 = *info2;
210 *info2 = info;
213 /* This type represents the kind of change that has been performed
214 * while trying to coalesce two basic maps.
216 * isl_change_none: nothing was changed
217 * isl_change_drop_first: the first basic map was removed
218 * isl_change_drop_second: the second basic map was removed
219 * isl_change_fuse: the two basic maps were replaced by a new basic map.
221 enum isl_change {
222 isl_change_error = -1,
223 isl_change_none = 0,
224 isl_change_drop_first,
225 isl_change_drop_second,
226 isl_change_fuse,
229 /* Add the valid constraints of the basic map represented by "info"
230 * to "bmap". "len" is the size of the constraints.
231 * If only one of the pair of inequalities that make up an equality
232 * is valid, then add that inequality.
234 static __isl_give isl_basic_map *add_valid_constraints(
235 __isl_take isl_basic_map *bmap, struct isl_coalesce_info *info,
236 unsigned len)
238 int k, l;
240 if (!bmap)
241 return NULL;
243 for (k = 0; k < info->bmap->n_eq; ++k) {
244 if (info->eq[2 * k] == STATUS_VALID &&
245 info->eq[2 * k + 1] == STATUS_VALID) {
246 l = isl_basic_map_alloc_equality(bmap);
247 if (l < 0)
248 return isl_basic_map_free(bmap);
249 isl_seq_cpy(bmap->eq[l], info->bmap->eq[k], len);
250 } else if (info->eq[2 * k] == STATUS_VALID) {
251 l = isl_basic_map_alloc_inequality(bmap);
252 if (l < 0)
253 return isl_basic_map_free(bmap);
254 isl_seq_neg(bmap->ineq[l], info->bmap->eq[k], len);
255 } else if (info->eq[2 * k + 1] == STATUS_VALID) {
256 l = isl_basic_map_alloc_inequality(bmap);
257 if (l < 0)
258 return isl_basic_map_free(bmap);
259 isl_seq_cpy(bmap->ineq[l], info->bmap->eq[k], len);
263 for (k = 0; k < info->bmap->n_ineq; ++k) {
264 if (info->ineq[k] != STATUS_VALID)
265 continue;
266 l = isl_basic_map_alloc_inequality(bmap);
267 if (l < 0)
268 return isl_basic_map_free(bmap);
269 isl_seq_cpy(bmap->ineq[l], info->bmap->ineq[k], len);
272 return bmap;
275 /* Replace the pair of basic maps i and j by the basic map bounded
276 * by the valid constraints in both basic maps and the constraints
277 * in extra (if not NULL).
278 * Place the fused basic map in the position that is the smallest of i and j.
280 * If "detect_equalities" is set, then look for equalities encoded
281 * as pairs of inequalities.
283 static enum isl_change fuse(int i, int j, struct isl_coalesce_info *info,
284 __isl_keep isl_mat *extra, int detect_equalities)
286 int k, l;
287 struct isl_basic_map *fused = NULL;
288 struct isl_tab *fused_tab = NULL;
289 unsigned total = isl_basic_map_total_dim(info[i].bmap);
290 unsigned extra_rows = extra ? extra->n_row : 0;
291 unsigned n_eq, n_ineq;
293 if (j < i)
294 return fuse(j, i, info, extra, detect_equalities);
296 n_eq = info[i].bmap->n_eq + info[j].bmap->n_eq;
297 n_ineq = info[i].bmap->n_ineq + info[j].bmap->n_ineq;
298 fused = isl_basic_map_alloc_space(isl_space_copy(info[i].bmap->dim),
299 info[i].bmap->n_div, n_eq, n_eq + n_ineq + extra_rows);
300 fused = add_valid_constraints(fused, &info[i], 1 + total);
301 fused = add_valid_constraints(fused, &info[j], 1 + total);
302 if (!fused)
303 goto error;
305 for (k = 0; k < info[i].bmap->n_div; ++k) {
306 int l = isl_basic_map_alloc_div(fused);
307 if (l < 0)
308 goto error;
309 isl_seq_cpy(fused->div[l], info[i].bmap->div[k], 1 + 1 + total);
312 for (k = 0; k < extra_rows; ++k) {
313 l = isl_basic_map_alloc_inequality(fused);
314 if (l < 0)
315 goto error;
316 isl_seq_cpy(fused->ineq[l], extra->row[k], 1 + total);
319 if (detect_equalities)
320 fused = isl_basic_map_detect_inequality_pairs(fused, NULL);
321 fused = isl_basic_map_gauss(fused, NULL);
322 ISL_F_SET(fused, ISL_BASIC_MAP_FINAL);
323 if (ISL_F_ISSET(info[i].bmap, ISL_BASIC_MAP_RATIONAL) &&
324 ISL_F_ISSET(info[j].bmap, ISL_BASIC_MAP_RATIONAL))
325 ISL_F_SET(fused, ISL_BASIC_MAP_RATIONAL);
327 fused_tab = isl_tab_from_basic_map(fused, 0);
328 if (isl_tab_detect_redundant(fused_tab) < 0)
329 goto error;
331 isl_basic_map_free(info[i].bmap);
332 info[i].bmap = fused;
333 isl_tab_free(info[i].tab);
334 info[i].tab = fused_tab;
335 drop(&info[j]);
337 return isl_change_fuse;
338 error:
339 isl_tab_free(fused_tab);
340 isl_basic_map_free(fused);
341 return isl_change_error;
344 /* Given a pair of basic maps i and j such that all constraints are either
345 * "valid" or "cut", check if the facets corresponding to the "cut"
346 * constraints of i lie entirely within basic map j.
347 * If so, replace the pair by the basic map consisting of the valid
348 * constraints in both basic maps.
349 * Checking whether the facet lies entirely within basic map j
350 * is performed by checking whether the constraints of basic map j
351 * are valid for the facet. These tests are performed on a rational
352 * tableau to avoid the theoretical possibility that a constraint
353 * that was considered to be a cut constraint for the entire basic map i
354 * happens to be considered to be a valid constraint for the facet,
355 * even though it cuts off the same rational points.
357 * To see that we are not introducing any extra points, call the
358 * two basic maps A and B and the resulting map U and let x
359 * be an element of U \setminus ( A \cup B ).
360 * A line connecting x with an element of A \cup B meets a facet F
361 * of either A or B. Assume it is a facet of B and let c_1 be
362 * the corresponding facet constraint. We have c_1(x) < 0 and
363 * so c_1 is a cut constraint. This implies that there is some
364 * (possibly rational) point x' satisfying the constraints of A
365 * and the opposite of c_1 as otherwise c_1 would have been marked
366 * valid for A. The line connecting x and x' meets a facet of A
367 * in a (possibly rational) point that also violates c_1, but this
368 * is impossible since all cut constraints of B are valid for all
369 * cut facets of A.
370 * In case F is a facet of A rather than B, then we can apply the
371 * above reasoning to find a facet of B separating x from A \cup B first.
373 static enum isl_change check_facets(int i, int j,
374 struct isl_coalesce_info *info)
376 int k, l;
377 struct isl_tab_undo *snap, *snap2;
378 unsigned n_eq = info[i].bmap->n_eq;
380 snap = isl_tab_snap(info[i].tab);
381 if (isl_tab_mark_rational(info[i].tab) < 0)
382 return isl_change_error;
383 snap2 = isl_tab_snap(info[i].tab);
385 for (k = 0; k < info[i].bmap->n_ineq; ++k) {
386 if (info[i].ineq[k] != STATUS_CUT)
387 continue;
388 if (isl_tab_select_facet(info[i].tab, n_eq + k) < 0)
389 return isl_change_error;
390 for (l = 0; l < info[j].bmap->n_ineq; ++l) {
391 int stat;
392 if (info[j].ineq[l] != STATUS_CUT)
393 continue;
394 stat = status_in(info[j].bmap->ineq[l], info[i].tab);
395 if (stat != STATUS_VALID)
396 break;
398 if (isl_tab_rollback(info[i].tab, snap2) < 0)
399 return isl_change_error;
400 if (l < info[j].bmap->n_ineq)
401 break;
404 if (k < info[i].bmap->n_ineq) {
405 if (isl_tab_rollback(info[i].tab, snap) < 0)
406 return isl_change_error;
407 return isl_change_none;
409 return fuse(i, j, info, NULL, 0);
412 /* Check if info->bmap contains the basic map represented
413 * by the tableau "tab".
414 * For each equality, we check both the constraint itself
415 * (as an inequality) and its negation. Make sure the
416 * equality is returned to its original state before returning.
418 static int contains(struct isl_coalesce_info *info, struct isl_tab *tab)
420 int k;
421 unsigned dim;
422 isl_basic_map *bmap = info->bmap;
424 dim = isl_basic_map_total_dim(bmap);
425 for (k = 0; k < bmap->n_eq; ++k) {
426 int stat;
427 isl_seq_neg(bmap->eq[k], bmap->eq[k], 1 + dim);
428 stat = status_in(bmap->eq[k], tab);
429 isl_seq_neg(bmap->eq[k], bmap->eq[k], 1 + dim);
430 if (stat != STATUS_VALID)
431 return 0;
432 stat = status_in(bmap->eq[k], tab);
433 if (stat != STATUS_VALID)
434 return 0;
437 for (k = 0; k < bmap->n_ineq; ++k) {
438 int stat;
439 if (info->ineq[k] == STATUS_REDUNDANT)
440 continue;
441 stat = status_in(bmap->ineq[k], tab);
442 if (stat != STATUS_VALID)
443 return 0;
445 return 1;
448 /* Basic map "i" has an inequality (say "k") that is adjacent
449 * to some inequality of basic map "j". All the other inequalities
450 * are valid for "j".
451 * Check if basic map "j" forms an extension of basic map "i".
453 * Note that this function is only called if some of the equalities or
454 * inequalities of basic map "j" do cut basic map "i". The function is
455 * correct even if there are no such cut constraints, but in that case
456 * the additional checks performed by this function are overkill.
458 * In particular, we replace constraint k, say f >= 0, by constraint
459 * f <= -1, add the inequalities of "j" that are valid for "i"
460 * and check if the result is a subset of basic map "j".
461 * If so, then we know that this result is exactly equal to basic map "j"
462 * since all its constraints are valid for basic map "j".
463 * By combining the valid constraints of "i" (all equalities and all
464 * inequalities except "k") and the valid constraints of "j" we therefore
465 * obtain a basic map that is equal to their union.
466 * In this case, there is no need to perform a rollback of the tableau
467 * since it is going to be destroyed in fuse().
470 * |\__ |\__
471 * | \__ | \__
472 * | \_ => | \__
473 * |_______| _ |_________\
476 * |\ |\
477 * | \ | \
478 * | \ | \
479 * | | | \
480 * | ||\ => | \
481 * | || \ | \
482 * | || | | |
483 * |__||_/ |_____/
485 static enum isl_change is_adj_ineq_extension(int i, int j,
486 struct isl_coalesce_info *info)
488 int k;
489 struct isl_tab_undo *snap;
490 unsigned n_eq = info[i].bmap->n_eq;
491 unsigned total = isl_basic_map_total_dim(info[i].bmap);
492 int r;
494 if (isl_tab_extend_cons(info[i].tab, 1 + info[j].bmap->n_ineq) < 0)
495 return isl_change_error;
497 for (k = 0; k < info[i].bmap->n_ineq; ++k)
498 if (info[i].ineq[k] == STATUS_ADJ_INEQ)
499 break;
500 if (k >= info[i].bmap->n_ineq)
501 isl_die(isl_basic_map_get_ctx(info[i].bmap), isl_error_internal,
502 "info[i].ineq should have exactly one STATUS_ADJ_INEQ",
503 return isl_change_error);
505 snap = isl_tab_snap(info[i].tab);
507 if (isl_tab_unrestrict(info[i].tab, n_eq + k) < 0)
508 return isl_change_error;
510 isl_seq_neg(info[i].bmap->ineq[k], info[i].bmap->ineq[k], 1 + total);
511 isl_int_sub_ui(info[i].bmap->ineq[k][0], info[i].bmap->ineq[k][0], 1);
512 r = isl_tab_add_ineq(info[i].tab, info[i].bmap->ineq[k]);
513 isl_seq_neg(info[i].bmap->ineq[k], info[i].bmap->ineq[k], 1 + total);
514 isl_int_sub_ui(info[i].bmap->ineq[k][0], info[i].bmap->ineq[k][0], 1);
515 if (r < 0)
516 return isl_change_error;
518 for (k = 0; k < info[j].bmap->n_ineq; ++k) {
519 if (info[j].ineq[k] != STATUS_VALID)
520 continue;
521 if (isl_tab_add_ineq(info[i].tab, info[j].bmap->ineq[k]) < 0)
522 return isl_change_error;
525 if (contains(&info[j], info[i].tab))
526 return fuse(i, j, info, NULL, 0);
528 if (isl_tab_rollback(info[i].tab, snap) < 0)
529 return isl_change_error;
531 return isl_change_none;
535 /* Both basic maps have at least one inequality with and adjacent
536 * (but opposite) inequality in the other basic map.
537 * Check that there are no cut constraints and that there is only
538 * a single pair of adjacent inequalities.
539 * If so, we can replace the pair by a single basic map described
540 * by all but the pair of adjacent inequalities.
541 * Any additional points introduced lie strictly between the two
542 * adjacent hyperplanes and can therefore be integral.
544 * ____ _____
545 * / ||\ / \
546 * / || \ / \
547 * \ || \ => \ \
548 * \ || / \ /
549 * \___||_/ \_____/
551 * The test for a single pair of adjancent inequalities is important
552 * for avoiding the combination of two basic maps like the following
554 * /|
555 * / |
556 * /__|
557 * _____
558 * | |
559 * | |
560 * |___|
562 * If there are some cut constraints on one side, then we may
563 * still be able to fuse the two basic maps, but we need to perform
564 * some additional checks in is_adj_ineq_extension.
566 static enum isl_change check_adj_ineq(int i, int j,
567 struct isl_coalesce_info *info)
569 int count_i, count_j;
570 int cut_i, cut_j;
572 count_i = count(info[i].ineq, info[i].bmap->n_ineq, STATUS_ADJ_INEQ);
573 count_j = count(info[j].ineq, info[j].bmap->n_ineq, STATUS_ADJ_INEQ);
575 if (count_i != 1 && count_j != 1)
576 return isl_change_none;
578 cut_i = any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_CUT) ||
579 any(info[i].ineq, info[i].bmap->n_ineq, STATUS_CUT);
580 cut_j = any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_CUT) ||
581 any(info[j].ineq, info[j].bmap->n_ineq, STATUS_CUT);
583 if (!cut_i && !cut_j && count_i == 1 && count_j == 1)
584 return fuse(i, j, info, NULL, 0);
586 if (count_i == 1 && !cut_i)
587 return is_adj_ineq_extension(i, j, info);
589 if (count_j == 1 && !cut_j)
590 return is_adj_ineq_extension(j, i, info);
592 return isl_change_none;
595 /* Basic map "i" has an inequality "k" that is adjacent to some equality
596 * of basic map "j". All the other inequalities are valid for "j".
597 * Check if basic map "j" forms an extension of basic map "i".
599 * In particular, we relax constraint "k", compute the corresponding
600 * facet and check whether it is included in the other basic map.
601 * If so, we know that relaxing the constraint extends the basic
602 * map with exactly the other basic map (we already know that this
603 * other basic map is included in the extension, because there
604 * were no "cut" inequalities in "i") and we can replace the
605 * two basic maps by this extension.
606 * Place this extension in the position that is the smallest of i and j.
607 * ____ _____
608 * / || / |
609 * / || / |
610 * \ || => \ |
611 * \ || \ |
612 * \___|| \____|
614 static enum isl_change is_adj_eq_extension(int i, int j, int k,
615 struct isl_coalesce_info *info)
617 int change = isl_change_none;
618 int super;
619 struct isl_tab_undo *snap, *snap2;
620 unsigned n_eq = info[i].bmap->n_eq;
622 if (isl_tab_is_equality(info[i].tab, n_eq + k))
623 return isl_change_none;
625 snap = isl_tab_snap(info[i].tab);
626 if (isl_tab_relax(info[i].tab, n_eq + k) < 0)
627 return isl_change_error;
628 snap2 = isl_tab_snap(info[i].tab);
629 if (isl_tab_select_facet(info[i].tab, n_eq + k) < 0)
630 return isl_change_error;
631 super = contains(&info[j], info[i].tab);
632 if (super) {
633 if (isl_tab_rollback(info[i].tab, snap2) < 0)
634 return isl_change_error;
635 info[i].bmap = isl_basic_map_cow(info[i].bmap);
636 if (!info[i].bmap)
637 return isl_change_error;
638 isl_int_add_ui(info[i].bmap->ineq[k][0],
639 info[i].bmap->ineq[k][0], 1);
640 ISL_F_SET(info[i].bmap, ISL_BASIC_MAP_FINAL);
641 drop(&info[j]);
642 if (j < i)
643 exchange(&info[i], &info[j]);
644 change = isl_change_fuse;
645 } else
646 if (isl_tab_rollback(info[i].tab, snap) < 0)
647 return isl_change_error;
649 return change;
652 /* Data structure that keeps track of the wrapping constraints
653 * and of information to bound the coefficients of those constraints.
655 * bound is set if we want to apply a bound on the coefficients
656 * mat contains the wrapping constraints
657 * max is the bound on the coefficients (if bound is set)
659 struct isl_wraps {
660 int bound;
661 isl_mat *mat;
662 isl_int max;
665 /* Update wraps->max to be greater than or equal to the coefficients
666 * in the equalities and inequalities of info->bmap that can be removed
667 * if we end up applying wrapping.
669 static void wraps_update_max(struct isl_wraps *wraps,
670 struct isl_coalesce_info *info)
672 int k;
673 isl_int max_k;
674 unsigned total = isl_basic_map_total_dim(info->bmap);
676 isl_int_init(max_k);
678 for (k = 0; k < info->bmap->n_eq; ++k) {
679 if (info->eq[2 * k] == STATUS_VALID &&
680 info->eq[2 * k + 1] == STATUS_VALID)
681 continue;
682 isl_seq_abs_max(info->bmap->eq[k] + 1, total, &max_k);
683 if (isl_int_abs_gt(max_k, wraps->max))
684 isl_int_set(wraps->max, max_k);
687 for (k = 0; k < info->bmap->n_ineq; ++k) {
688 if (info->ineq[k] == STATUS_VALID ||
689 info->ineq[k] == STATUS_REDUNDANT)
690 continue;
691 isl_seq_abs_max(info->bmap->ineq[k] + 1, total, &max_k);
692 if (isl_int_abs_gt(max_k, wraps->max))
693 isl_int_set(wraps->max, max_k);
696 isl_int_clear(max_k);
699 /* Initialize the isl_wraps data structure.
700 * If we want to bound the coefficients of the wrapping constraints,
701 * we set wraps->max to the largest coefficient
702 * in the equalities and inequalities that can be removed if we end up
703 * applying wrapping.
705 static void wraps_init(struct isl_wraps *wraps, __isl_take isl_mat *mat,
706 struct isl_coalesce_info *info, int i, int j)
708 isl_ctx *ctx;
710 wraps->bound = 0;
711 wraps->mat = mat;
712 if (!mat)
713 return;
714 ctx = isl_mat_get_ctx(mat);
715 wraps->bound = isl_options_get_coalesce_bounded_wrapping(ctx);
716 if (!wraps->bound)
717 return;
718 isl_int_init(wraps->max);
719 isl_int_set_si(wraps->max, 0);
720 wraps_update_max(wraps, &info[i]);
721 wraps_update_max(wraps, &info[j]);
724 /* Free the contents of the isl_wraps data structure.
726 static void wraps_free(struct isl_wraps *wraps)
728 isl_mat_free(wraps->mat);
729 if (wraps->bound)
730 isl_int_clear(wraps->max);
733 /* Is the wrapping constraint in row "row" allowed?
735 * If wraps->bound is set, we check that none of the coefficients
736 * is greater than wraps->max.
738 static int allow_wrap(struct isl_wraps *wraps, int row)
740 int i;
742 if (!wraps->bound)
743 return 1;
745 for (i = 1; i < wraps->mat->n_col; ++i)
746 if (isl_int_abs_gt(wraps->mat->row[row][i], wraps->max))
747 return 0;
749 return 1;
752 /* For each non-redundant constraint in info->bmap (as determined by info->tab),
753 * wrap the constraint around "bound" such that it includes the whole
754 * set "set" and append the resulting constraint to "wraps".
755 * "wraps" is assumed to have been pre-allocated to the appropriate size.
756 * wraps->n_row is the number of actual wrapped constraints that have
757 * been added.
758 * If any of the wrapping problems results in a constraint that is
759 * identical to "bound", then this means that "set" is unbounded in such
760 * way that no wrapping is possible. If this happens then wraps->n_row
761 * is reset to zero.
762 * Similarly, if we want to bound the coefficients of the wrapping
763 * constraints and a newly added wrapping constraint does not
764 * satisfy the bound, then wraps->n_row is also reset to zero.
766 static int add_wraps(struct isl_wraps *wraps, struct isl_coalesce_info *info,
767 isl_int *bound, __isl_keep isl_set *set)
769 int l;
770 int w;
771 isl_basic_map *bmap = info->bmap;
772 unsigned total = isl_basic_map_total_dim(bmap);
774 w = wraps->mat->n_row;
776 for (l = 0; l < bmap->n_ineq; ++l) {
777 if (isl_seq_is_neg(bound, bmap->ineq[l], 1 + total))
778 continue;
779 if (isl_seq_eq(bound, bmap->ineq[l], 1 + total))
780 continue;
781 if (isl_tab_is_redundant(info->tab, bmap->n_eq + l))
782 continue;
784 isl_seq_cpy(wraps->mat->row[w], bound, 1 + total);
785 if (!isl_set_wrap_facet(set, wraps->mat->row[w], bmap->ineq[l]))
786 return -1;
787 if (isl_seq_eq(wraps->mat->row[w], bound, 1 + total))
788 goto unbounded;
789 if (!allow_wrap(wraps, w))
790 goto unbounded;
791 ++w;
793 for (l = 0; l < bmap->n_eq; ++l) {
794 if (isl_seq_is_neg(bound, bmap->eq[l], 1 + total))
795 continue;
796 if (isl_seq_eq(bound, bmap->eq[l], 1 + total))
797 continue;
799 isl_seq_cpy(wraps->mat->row[w], bound, 1 + total);
800 isl_seq_neg(wraps->mat->row[w + 1], bmap->eq[l], 1 + total);
801 if (!isl_set_wrap_facet(set, wraps->mat->row[w],
802 wraps->mat->row[w + 1]))
803 return -1;
804 if (isl_seq_eq(wraps->mat->row[w], bound, 1 + total))
805 goto unbounded;
806 if (!allow_wrap(wraps, w))
807 goto unbounded;
808 ++w;
810 isl_seq_cpy(wraps->mat->row[w], bound, 1 + total);
811 if (!isl_set_wrap_facet(set, wraps->mat->row[w], bmap->eq[l]))
812 return -1;
813 if (isl_seq_eq(wraps->mat->row[w], bound, 1 + total))
814 goto unbounded;
815 if (!allow_wrap(wraps, w))
816 goto unbounded;
817 ++w;
820 wraps->mat->n_row = w;
821 return 0;
822 unbounded:
823 wraps->mat->n_row = 0;
824 return 0;
827 /* Check if the constraints in "wraps" from "first" until the last
828 * are all valid for the basic set represented by "tab".
829 * If not, wraps->n_row is set to zero.
831 static int check_wraps(__isl_keep isl_mat *wraps, int first,
832 struct isl_tab *tab)
834 int i;
836 for (i = first; i < wraps->n_row; ++i) {
837 enum isl_ineq_type type;
838 type = isl_tab_ineq_type(tab, wraps->row[i]);
839 if (type == isl_ineq_error)
840 return -1;
841 if (type == isl_ineq_redundant)
842 continue;
843 wraps->n_row = 0;
844 return 0;
847 return 0;
850 /* Return a set that corresponds to the non-redundant constraints
851 * (as recorded in tab) of bmap.
853 * It's important to remove the redundant constraints as some
854 * of the other constraints may have been modified after the
855 * constraints were marked redundant.
856 * In particular, a constraint may have been relaxed.
857 * Redundant constraints are ignored when a constraint is relaxed
858 * and should therefore continue to be ignored ever after.
859 * Otherwise, the relaxation might be thwarted by some of
860 * these constraints.
862 * Update the underlying set to ensure that the dimension doesn't change.
863 * Otherwise the integer divisions could get dropped if the tab
864 * turns out to be empty.
866 static __isl_give isl_set *set_from_updated_bmap(__isl_keep isl_basic_map *bmap,
867 struct isl_tab *tab)
869 isl_basic_set *bset;
871 bmap = isl_basic_map_copy(bmap);
872 bset = isl_basic_map_underlying_set(bmap);
873 bset = isl_basic_set_cow(bset);
874 bset = isl_basic_set_update_from_tab(bset, tab);
875 return isl_set_from_basic_set(bset);
878 /* Given a basic set i with a constraint k that is adjacent to
879 * basic set j, check if we can wrap
880 * both the facet corresponding to k and basic map j
881 * around their ridges to include the other set.
882 * If so, replace the pair of basic sets by their union.
884 * All constraints of i (except k) are assumed to be valid for j.
885 * This means that there is no real need to wrap the ridges of
886 * the faces of basic map i around basic map j but since we do,
887 * we have to check that the resulting wrapping constraints are valid for i.
888 * ____ _____
889 * / | / \
890 * / || / |
891 * \ || => \ |
892 * \ || \ |
893 * \___|| \____|
896 static enum isl_change can_wrap_in_facet(int i, int j, int k,
897 struct isl_coalesce_info *info)
899 enum isl_change change = isl_change_none;
900 struct isl_wraps wraps;
901 isl_ctx *ctx;
902 isl_mat *mat;
903 struct isl_set *set_i = NULL;
904 struct isl_set *set_j = NULL;
905 struct isl_vec *bound = NULL;
906 unsigned total = isl_basic_map_total_dim(info[i].bmap);
907 struct isl_tab_undo *snap;
908 int n;
910 set_i = set_from_updated_bmap(info[i].bmap, info[i].tab);
911 set_j = set_from_updated_bmap(info[j].bmap, info[j].tab);
912 ctx = isl_basic_map_get_ctx(info[i].bmap);
913 mat = isl_mat_alloc(ctx, 2 * (info[i].bmap->n_eq + info[j].bmap->n_eq) +
914 info[i].bmap->n_ineq + info[j].bmap->n_ineq,
915 1 + total);
916 wraps_init(&wraps, mat, info, i, j);
917 bound = isl_vec_alloc(ctx, 1 + total);
918 if (!set_i || !set_j || !wraps.mat || !bound)
919 goto error;
921 isl_seq_cpy(bound->el, info[i].bmap->ineq[k], 1 + total);
922 isl_int_add_ui(bound->el[0], bound->el[0], 1);
924 isl_seq_cpy(wraps.mat->row[0], bound->el, 1 + total);
925 wraps.mat->n_row = 1;
927 if (add_wraps(&wraps, &info[j], bound->el, set_i) < 0)
928 goto error;
929 if (!wraps.mat->n_row)
930 goto unbounded;
932 snap = isl_tab_snap(info[i].tab);
934 if (isl_tab_select_facet(info[i].tab, info[i].bmap->n_eq + k) < 0)
935 goto error;
936 if (isl_tab_detect_redundant(info[i].tab) < 0)
937 goto error;
939 isl_seq_neg(bound->el, info[i].bmap->ineq[k], 1 + total);
941 n = wraps.mat->n_row;
942 if (add_wraps(&wraps, &info[i], bound->el, set_j) < 0)
943 goto error;
945 if (isl_tab_rollback(info[i].tab, snap) < 0)
946 goto error;
947 if (check_wraps(wraps.mat, n, info[i].tab) < 0)
948 goto error;
949 if (!wraps.mat->n_row)
950 goto unbounded;
952 change = fuse(i, j, info, wraps.mat, 0);
954 unbounded:
955 wraps_free(&wraps);
957 isl_set_free(set_i);
958 isl_set_free(set_j);
960 isl_vec_free(bound);
962 return change;
963 error:
964 wraps_free(&wraps);
965 isl_vec_free(bound);
966 isl_set_free(set_i);
967 isl_set_free(set_j);
968 return isl_change_error;
971 /* Given a pair of basic maps i and j such that j sticks out
972 * of i at n cut constraints, each time by at most one,
973 * try to compute wrapping constraints and replace the two
974 * basic maps by a single basic map.
975 * The other constraints of i are assumed to be valid for j.
977 * For each cut constraint t(x) >= 0 of i, we add the relaxed version
978 * t(x) + 1 >= 0, along with wrapping constraints for all constraints
979 * of basic map j that bound the part of basic map j that sticks out
980 * of the cut constraint.
981 * In particular, we first intersect basic map j with t(x) + 1 = 0.
982 * If the result is empty, then t(x) >= 0 was actually a valid constraint
983 * (with respect to the integer points), so we add t(x) >= 0 instead.
984 * Otherwise, we wrap the constraints of basic map j that are not
985 * redundant in this intersection over the union of the two basic maps.
987 * If any wrapping fails, i.e., if we cannot wrap to touch
988 * the union, then we give up.
989 * Otherwise, the pair of basic maps is replaced by their union.
991 static enum isl_change wrap_in_facets(int i, int j, int *cuts, int n,
992 struct isl_coalesce_info *info)
994 enum isl_change change = isl_change_none;
995 struct isl_wraps wraps;
996 isl_ctx *ctx;
997 isl_mat *mat;
998 isl_set *set = NULL;
999 unsigned total = isl_basic_map_total_dim(info[i].bmap);
1000 int max_wrap;
1001 int k, w;
1002 struct isl_tab_undo *snap;
1004 if (isl_tab_extend_cons(info[j].tab, 1) < 0)
1005 goto error;
1007 max_wrap = 1 + 2 * info[j].bmap->n_eq + info[j].bmap->n_ineq;
1008 max_wrap *= n;
1010 set = isl_set_union(set_from_updated_bmap(info[i].bmap, info[i].tab),
1011 set_from_updated_bmap(info[j].bmap, info[j].tab));
1012 ctx = isl_basic_map_get_ctx(info[i].bmap);
1013 mat = isl_mat_alloc(ctx, max_wrap, 1 + total);
1014 wraps_init(&wraps, mat, info, i, j);
1015 if (!set || !wraps.mat)
1016 goto error;
1018 snap = isl_tab_snap(info[j].tab);
1020 wraps.mat->n_row = 0;
1022 for (k = 0; k < n; ++k) {
1023 w = wraps.mat->n_row++;
1024 isl_seq_cpy(wraps.mat->row[w],
1025 info[i].bmap->ineq[cuts[k]], 1 + total);
1026 isl_int_add_ui(wraps.mat->row[w][0], wraps.mat->row[w][0], 1);
1027 if (isl_tab_add_eq(info[j].tab, wraps.mat->row[w]) < 0)
1028 goto error;
1029 if (isl_tab_detect_redundant(info[j].tab) < 0)
1030 goto error;
1032 if (info[j].tab->empty)
1033 isl_int_sub_ui(wraps.mat->row[w][0],
1034 wraps.mat->row[w][0], 1);
1035 else if (add_wraps(&wraps, &info[j],
1036 wraps.mat->row[w], set) < 0)
1037 goto error;
1039 if (isl_tab_rollback(info[j].tab, snap) < 0)
1040 goto error;
1042 if (!wraps.mat->n_row)
1043 break;
1046 if (k == n)
1047 change = fuse(i, j, info, wraps.mat, 0);
1049 wraps_free(&wraps);
1050 isl_set_free(set);
1052 return change;
1053 error:
1054 wraps_free(&wraps);
1055 isl_set_free(set);
1056 return isl_change_error;
1059 /* Given two basic sets i and j such that i has no cut equalities,
1060 * check if relaxing all the cut inequalities of i by one turns
1061 * them into valid constraint for j and check if we can wrap in
1062 * the bits that are sticking out.
1063 * If so, replace the pair by their union.
1065 * We first check if all relaxed cut inequalities of i are valid for j
1066 * and then try to wrap in the intersections of the relaxed cut inequalities
1067 * with j.
1069 * During this wrapping, we consider the points of j that lie at a distance
1070 * of exactly 1 from i. In particular, we ignore the points that lie in
1071 * between this lower-dimensional space and the basic map i.
1072 * We can therefore only apply this to integer maps.
1073 * ____ _____
1074 * / ___|_ / \
1075 * / | | / |
1076 * \ | | => \ |
1077 * \|____| \ |
1078 * \___| \____/
1080 * _____ ______
1081 * | ____|_ | \
1082 * | | | | |
1083 * | | | => | |
1084 * |_| | | |
1085 * |_____| \______|
1087 * _______
1088 * | |
1089 * | |\ |
1090 * | | \ |
1091 * | | \ |
1092 * | | \|
1093 * | | \
1094 * | |_____\
1095 * | |
1096 * |_______|
1098 * Wrapping can fail if the result of wrapping one of the facets
1099 * around its edges does not produce any new facet constraint.
1100 * In particular, this happens when we try to wrap in unbounded sets.
1102 * _______________________________________________________________________
1104 * | ___
1105 * | | |
1106 * |_| |_________________________________________________________________
1107 * |___|
1109 * The following is not an acceptable result of coalescing the above two
1110 * sets as it includes extra integer points.
1111 * _______________________________________________________________________
1113 * |
1114 * |
1116 * \______________________________________________________________________
1118 static enum isl_change can_wrap_in_set(int i, int j,
1119 struct isl_coalesce_info *info)
1121 enum isl_change change = isl_change_none;
1122 int k, m;
1123 int n;
1124 int *cuts = NULL;
1125 isl_ctx *ctx;
1127 if (ISL_F_ISSET(info[i].bmap, ISL_BASIC_MAP_RATIONAL) ||
1128 ISL_F_ISSET(info[j].bmap, ISL_BASIC_MAP_RATIONAL))
1129 return isl_change_none;
1131 n = count(info[i].ineq, info[i].bmap->n_ineq, STATUS_CUT);
1132 if (n == 0)
1133 return isl_change_none;
1135 ctx = isl_basic_map_get_ctx(info[i].bmap);
1136 cuts = isl_alloc_array(ctx, int, n);
1137 if (!cuts)
1138 return isl_change_error;
1140 for (k = 0, m = 0; m < n; ++k) {
1141 enum isl_ineq_type type;
1143 if (info[i].ineq[k] != STATUS_CUT)
1144 continue;
1146 isl_int_add_ui(info[i].bmap->ineq[k][0],
1147 info[i].bmap->ineq[k][0], 1);
1148 type = isl_tab_ineq_type(info[j].tab, info[i].bmap->ineq[k]);
1149 isl_int_sub_ui(info[i].bmap->ineq[k][0],
1150 info[i].bmap->ineq[k][0], 1);
1151 if (type == isl_ineq_error)
1152 goto error;
1153 if (type != isl_ineq_redundant)
1154 break;
1155 cuts[m] = k;
1156 ++m;
1159 if (m == n)
1160 change = wrap_in_facets(i, j, cuts, n, info);
1162 free(cuts);
1164 return change;
1165 error:
1166 free(cuts);
1167 return isl_change_error;
1170 /* Check if either i or j has only cut inequalities that can
1171 * be used to wrap in (a facet of) the other basic set.
1172 * if so, replace the pair by their union.
1174 static enum isl_change check_wrap(int i, int j, struct isl_coalesce_info *info)
1176 enum isl_change change = isl_change_none;
1178 if (!any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_CUT))
1179 change = can_wrap_in_set(i, j, info);
1180 if (change != isl_change_none)
1181 return change;
1183 if (!any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_CUT))
1184 change = can_wrap_in_set(j, i, info);
1185 return change;
1188 /* At least one of the basic maps has an equality that is adjacent
1189 * to inequality. Make sure that only one of the basic maps has
1190 * such an equality and that the other basic map has exactly one
1191 * inequality adjacent to an equality.
1192 * We call the basic map that has the inequality "i" and the basic
1193 * map that has the equality "j".
1194 * If "i" has any "cut" (in)equality, then relaxing the inequality
1195 * by one would not result in a basic map that contains the other
1196 * basic map.
1198 static enum isl_change check_adj_eq(int i, int j,
1199 struct isl_coalesce_info *info)
1201 enum isl_change change = isl_change_none;
1202 int k;
1204 if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ADJ_INEQ) &&
1205 any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_ADJ_INEQ))
1206 /* ADJ EQ TOO MANY */
1207 return isl_change_none;
1209 if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ADJ_INEQ))
1210 return check_adj_eq(j, i, info);
1212 /* j has an equality adjacent to an inequality in i */
1214 if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_CUT))
1215 return isl_change_none;
1216 if (any(info[i].ineq, info[i].bmap->n_ineq, STATUS_CUT))
1217 /* ADJ EQ CUT */
1218 return isl_change_none;
1219 if (count(info[i].ineq, info[i].bmap->n_ineq, STATUS_ADJ_EQ) != 1 ||
1220 any(info[j].ineq, info[j].bmap->n_ineq, STATUS_ADJ_EQ) ||
1221 any(info[i].ineq, info[i].bmap->n_ineq, STATUS_ADJ_INEQ) ||
1222 any(info[j].ineq, info[j].bmap->n_ineq, STATUS_ADJ_INEQ))
1223 /* ADJ EQ TOO MANY */
1224 return isl_change_none;
1226 for (k = 0; k < info[i].bmap->n_ineq; ++k)
1227 if (info[i].ineq[k] == STATUS_ADJ_EQ)
1228 break;
1230 change = is_adj_eq_extension(i, j, k, info);
1231 if (change != isl_change_none)
1232 return change;
1234 if (count(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_ADJ_INEQ) != 1)
1235 return isl_change_none;
1237 change = can_wrap_in_facet(i, j, k, info);
1239 return change;
1242 /* The two basic maps lie on adjacent hyperplanes. In particular,
1243 * basic map "i" has an equality that lies parallel to basic map "j".
1244 * Check if we can wrap the facets around the parallel hyperplanes
1245 * to include the other set.
1247 * We perform basically the same operations as can_wrap_in_facet,
1248 * except that we don't need to select a facet of one of the sets.
1250 * \\ \\
1251 * \\ => \\
1252 * \ \|
1254 * If there is more than one equality of "i" adjacent to an equality of "j",
1255 * then the result will satisfy one or more equalities that are a linear
1256 * combination of these equalities. These will be encoded as pairs
1257 * of inequalities in the wrapping constraints and need to be made
1258 * explicit.
1260 static enum isl_change check_eq_adj_eq(int i, int j,
1261 struct isl_coalesce_info *info)
1263 int k;
1264 enum isl_change change = isl_change_none;
1265 int detect_equalities = 0;
1266 struct isl_wraps wraps;
1267 isl_ctx *ctx;
1268 isl_mat *mat;
1269 struct isl_set *set_i = NULL;
1270 struct isl_set *set_j = NULL;
1271 struct isl_vec *bound = NULL;
1272 unsigned total = isl_basic_map_total_dim(info[i].bmap);
1274 if (count(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ADJ_EQ) != 1)
1275 detect_equalities = 1;
1277 for (k = 0; k < 2 * info[i].bmap->n_eq ; ++k)
1278 if (info[i].eq[k] == STATUS_ADJ_EQ)
1279 break;
1281 set_i = set_from_updated_bmap(info[i].bmap, info[i].tab);
1282 set_j = set_from_updated_bmap(info[j].bmap, info[j].tab);
1283 ctx = isl_basic_map_get_ctx(info[i].bmap);
1284 mat = isl_mat_alloc(ctx, 2 * (info[i].bmap->n_eq + info[j].bmap->n_eq) +
1285 info[i].bmap->n_ineq + info[j].bmap->n_ineq,
1286 1 + total);
1287 wraps_init(&wraps, mat, info, i, j);
1288 bound = isl_vec_alloc(ctx, 1 + total);
1289 if (!set_i || !set_j || !wraps.mat || !bound)
1290 goto error;
1292 if (k % 2 == 0)
1293 isl_seq_neg(bound->el, info[i].bmap->eq[k / 2], 1 + total);
1294 else
1295 isl_seq_cpy(bound->el, info[i].bmap->eq[k / 2], 1 + total);
1296 isl_int_add_ui(bound->el[0], bound->el[0], 1);
1298 isl_seq_cpy(wraps.mat->row[0], bound->el, 1 + total);
1299 wraps.mat->n_row = 1;
1301 if (add_wraps(&wraps, &info[j], bound->el, set_i) < 0)
1302 goto error;
1303 if (!wraps.mat->n_row)
1304 goto unbounded;
1306 isl_int_sub_ui(bound->el[0], bound->el[0], 1);
1307 isl_seq_neg(bound->el, bound->el, 1 + total);
1309 isl_seq_cpy(wraps.mat->row[wraps.mat->n_row], bound->el, 1 + total);
1310 wraps.mat->n_row++;
1312 if (add_wraps(&wraps, &info[i], bound->el, set_j) < 0)
1313 goto error;
1314 if (!wraps.mat->n_row)
1315 goto unbounded;
1317 change = fuse(i, j, info, wraps.mat, detect_equalities);
1319 if (0) {
1320 error: change = isl_change_error;
1322 unbounded:
1324 wraps_free(&wraps);
1325 isl_set_free(set_i);
1326 isl_set_free(set_j);
1327 isl_vec_free(bound);
1329 return change;
1332 /* Check if the union of the given pair of basic maps
1333 * can be represented by a single basic map.
1334 * If so, replace the pair by the single basic map and return
1335 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
1336 * Otherwise, return isl_change_none.
1337 * The two basic maps are assumed to live in the same local space.
1339 * We first check the effect of each constraint of one basic map
1340 * on the other basic map.
1341 * The constraint may be
1342 * redundant the constraint is redundant in its own
1343 * basic map and should be ignore and removed
1344 * in the end
1345 * valid all (integer) points of the other basic map
1346 * satisfy the constraint
1347 * separate no (integer) point of the other basic map
1348 * satisfies the constraint
1349 * cut some but not all points of the other basic map
1350 * satisfy the constraint
1351 * adj_eq the given constraint is adjacent (on the outside)
1352 * to an equality of the other basic map
1353 * adj_ineq the given constraint is adjacent (on the outside)
1354 * to an inequality of the other basic map
1356 * We consider seven cases in which we can replace the pair by a single
1357 * basic map. We ignore all "redundant" constraints.
1359 * 1. all constraints of one basic map are valid
1360 * => the other basic map is a subset and can be removed
1362 * 2. all constraints of both basic maps are either "valid" or "cut"
1363 * and the facets corresponding to the "cut" constraints
1364 * of one of the basic maps lies entirely inside the other basic map
1365 * => the pair can be replaced by a basic map consisting
1366 * of the valid constraints in both basic maps
1368 * 3. there is a single pair of adjacent inequalities
1369 * (all other constraints are "valid")
1370 * => the pair can be replaced by a basic map consisting
1371 * of the valid constraints in both basic maps
1373 * 4. one basic map has a single adjacent inequality, while the other
1374 * constraints are "valid". The other basic map has some
1375 * "cut" constraints, but replacing the adjacent inequality by
1376 * its opposite and adding the valid constraints of the other
1377 * basic map results in a subset of the other basic map
1378 * => the pair can be replaced by a basic map consisting
1379 * of the valid constraints in both basic maps
1381 * 5. there is a single adjacent pair of an inequality and an equality,
1382 * the other constraints of the basic map containing the inequality are
1383 * "valid". Moreover, if the inequality the basic map is relaxed
1384 * and then turned into an equality, then resulting facet lies
1385 * entirely inside the other basic map
1386 * => the pair can be replaced by the basic map containing
1387 * the inequality, with the inequality relaxed.
1389 * 6. there is a single adjacent pair of an inequality and an equality,
1390 * the other constraints of the basic map containing the inequality are
1391 * "valid". Moreover, the facets corresponding to both
1392 * the inequality and the equality can be wrapped around their
1393 * ridges to include the other basic map
1394 * => the pair can be replaced by a basic map consisting
1395 * of the valid constraints in both basic maps together
1396 * with all wrapping constraints
1398 * 7. one of the basic maps extends beyond the other by at most one.
1399 * Moreover, the facets corresponding to the cut constraints and
1400 * the pieces of the other basic map at offset one from these cut
1401 * constraints can be wrapped around their ridges to include
1402 * the union of the two basic maps
1403 * => the pair can be replaced by a basic map consisting
1404 * of the valid constraints in both basic maps together
1405 * with all wrapping constraints
1407 * 8. the two basic maps live in adjacent hyperplanes. In principle
1408 * such sets can always be combined through wrapping, but we impose
1409 * that there is only one such pair, to avoid overeager coalescing.
1411 * Throughout the computation, we maintain a collection of tableaus
1412 * corresponding to the basic maps. When the basic maps are dropped
1413 * or combined, the tableaus are modified accordingly.
1415 static enum isl_change coalesce_local_pair(int i, int j,
1416 struct isl_coalesce_info *info)
1418 enum isl_change change = isl_change_none;
1420 info[i].eq = info[i].ineq = NULL;
1421 info[j].eq = info[j].ineq = NULL;
1423 info[i].eq = eq_status_in(info[i].bmap, info[j].tab);
1424 if (info[i].bmap->n_eq && !info[i].eq)
1425 goto error;
1426 if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ERROR))
1427 goto error;
1428 if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_SEPARATE))
1429 goto done;
1431 info[j].eq = eq_status_in(info[j].bmap, info[i].tab);
1432 if (info[j].bmap->n_eq && !info[j].eq)
1433 goto error;
1434 if (any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_ERROR))
1435 goto error;
1436 if (any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_SEPARATE))
1437 goto done;
1439 info[i].ineq = ineq_status_in(info[i].bmap, info[i].tab, info[j].tab);
1440 if (info[i].bmap->n_ineq && !info[i].ineq)
1441 goto error;
1442 if (any(info[i].ineq, info[i].bmap->n_ineq, STATUS_ERROR))
1443 goto error;
1444 if (any(info[i].ineq, info[i].bmap->n_ineq, STATUS_SEPARATE))
1445 goto done;
1447 info[j].ineq = ineq_status_in(info[j].bmap, info[j].tab, info[i].tab);
1448 if (info[j].bmap->n_ineq && !info[j].ineq)
1449 goto error;
1450 if (any(info[j].ineq, info[j].bmap->n_ineq, STATUS_ERROR))
1451 goto error;
1452 if (any(info[j].ineq, info[j].bmap->n_ineq, STATUS_SEPARATE))
1453 goto done;
1455 if (all(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_VALID) &&
1456 all(info[i].ineq, info[i].bmap->n_ineq, STATUS_VALID)) {
1457 drop(&info[j]);
1458 change = isl_change_drop_second;
1459 } else if (all(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_VALID) &&
1460 all(info[j].ineq, info[j].bmap->n_ineq, STATUS_VALID)) {
1461 drop(&info[i]);
1462 change = isl_change_drop_first;
1463 } else if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ADJ_EQ)) {
1464 change = check_eq_adj_eq(i, j, info);
1465 } else if (any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_ADJ_EQ)) {
1466 change = check_eq_adj_eq(j, i, info);
1467 } else if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ADJ_INEQ) ||
1468 any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_ADJ_INEQ)) {
1469 change = check_adj_eq(i, j, info);
1470 } else if (any(info[i].ineq, info[i].bmap->n_ineq, STATUS_ADJ_EQ) ||
1471 any(info[j].ineq, info[j].bmap->n_ineq, STATUS_ADJ_EQ)) {
1472 /* Can't happen */
1473 /* BAD ADJ INEQ */
1474 } else if (any(info[i].ineq, info[i].bmap->n_ineq, STATUS_ADJ_INEQ) ||
1475 any(info[j].ineq, info[j].bmap->n_ineq, STATUS_ADJ_INEQ)) {
1476 change = check_adj_ineq(i, j, info);
1477 } else {
1478 if (!any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_CUT) &&
1479 !any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_CUT))
1480 change = check_facets(i, j, info);
1481 if (change == isl_change_none)
1482 change = check_wrap(i, j, info);
1485 done:
1486 free(info[i].eq);
1487 free(info[j].eq);
1488 free(info[i].ineq);
1489 free(info[j].ineq);
1490 return change;
1491 error:
1492 free(info[i].eq);
1493 free(info[j].eq);
1494 free(info[i].ineq);
1495 free(info[j].ineq);
1496 return isl_change_error;
1499 /* Do the two basic maps live in the same local space, i.e.,
1500 * do they have the same (known) divs?
1501 * If either basic map has any unknown divs, then we can only assume
1502 * that they do not live in the same local space.
1504 static int same_divs(__isl_keep isl_basic_map *bmap1,
1505 __isl_keep isl_basic_map *bmap2)
1507 int i;
1508 int known;
1509 int total;
1511 if (!bmap1 || !bmap2)
1512 return -1;
1513 if (bmap1->n_div != bmap2->n_div)
1514 return 0;
1516 if (bmap1->n_div == 0)
1517 return 1;
1519 known = isl_basic_map_divs_known(bmap1);
1520 if (known < 0 || !known)
1521 return known;
1522 known = isl_basic_map_divs_known(bmap2);
1523 if (known < 0 || !known)
1524 return known;
1526 total = isl_basic_map_total_dim(bmap1);
1527 for (i = 0; i < bmap1->n_div; ++i)
1528 if (!isl_seq_eq(bmap1->div[i], bmap2->div[i], 2 + total))
1529 return 0;
1531 return 1;
1534 /* Does "bmap" contain the basic map represented by the tableau "tab"
1535 * after expanding the divs of "bmap" to match those of "tab"?
1536 * The expansion is performed using the divs "div" and expansion "exp"
1537 * computed by the caller.
1538 * Then we check if all constraints of the expanded "bmap" are valid for "tab".
1540 static int contains_with_expanded_divs(__isl_keep isl_basic_map *bmap,
1541 struct isl_tab *tab, __isl_keep isl_mat *div, int *exp)
1543 int superset = 0;
1544 int *eq_i = NULL;
1545 int *ineq_i = NULL;
1547 bmap = isl_basic_map_copy(bmap);
1548 bmap = isl_basic_set_expand_divs(bmap, isl_mat_copy(div), exp);
1550 if (!bmap)
1551 goto error;
1553 eq_i = eq_status_in(bmap, tab);
1554 if (bmap->n_eq && !eq_i)
1555 goto error;
1556 if (any(eq_i, 2 * bmap->n_eq, STATUS_ERROR))
1557 goto error;
1558 if (any(eq_i, 2 * bmap->n_eq, STATUS_SEPARATE))
1559 goto done;
1561 ineq_i = ineq_status_in(bmap, NULL, tab);
1562 if (bmap->n_ineq && !ineq_i)
1563 goto error;
1564 if (any(ineq_i, bmap->n_ineq, STATUS_ERROR))
1565 goto error;
1566 if (any(ineq_i, bmap->n_ineq, STATUS_SEPARATE))
1567 goto done;
1569 if (all(eq_i, 2 * bmap->n_eq, STATUS_VALID) &&
1570 all(ineq_i, bmap->n_ineq, STATUS_VALID))
1571 superset = 1;
1573 done:
1574 isl_basic_map_free(bmap);
1575 free(eq_i);
1576 free(ineq_i);
1577 return superset;
1578 error:
1579 isl_basic_map_free(bmap);
1580 free(eq_i);
1581 free(ineq_i);
1582 return -1;
1585 /* Does "bmap_i" contain the basic map represented by "info_j"
1586 * after aligning the divs of "bmap_i" to those of "info_j".
1587 * Note that this can only succeed if the number of divs of "bmap_i"
1588 * is smaller than (or equal to) the number of divs of "info_j".
1590 * We first check if the divs of "bmap_i" are all known and form a subset
1591 * of those of "bmap_j". If so, we pass control over to
1592 * contains_with_expanded_divs.
1594 static int contains_after_aligning_divs(__isl_keep isl_basic_map *bmap_i,
1595 struct isl_coalesce_info *info_j)
1597 int known;
1598 isl_mat *div_i, *div_j, *div;
1599 int *exp1 = NULL;
1600 int *exp2 = NULL;
1601 isl_ctx *ctx;
1602 int subset;
1604 known = isl_basic_map_divs_known(bmap_i);
1605 if (known < 0 || !known)
1606 return known;
1608 ctx = isl_basic_map_get_ctx(bmap_i);
1610 div_i = isl_basic_map_get_divs(bmap_i);
1611 div_j = isl_basic_map_get_divs(info_j->bmap);
1613 if (!div_i || !div_j)
1614 goto error;
1616 exp1 = isl_alloc_array(ctx, int, div_i->n_row);
1617 exp2 = isl_alloc_array(ctx, int, div_j->n_row);
1618 if ((div_i->n_row && !exp1) || (div_j->n_row && !exp2))
1619 goto error;
1621 div = isl_merge_divs(div_i, div_j, exp1, exp2);
1622 if (!div)
1623 goto error;
1625 if (div->n_row == div_j->n_row)
1626 subset = contains_with_expanded_divs(bmap_i,
1627 info_j->tab, div, exp1);
1628 else
1629 subset = 0;
1631 isl_mat_free(div);
1633 isl_mat_free(div_i);
1634 isl_mat_free(div_j);
1636 free(exp2);
1637 free(exp1);
1639 return subset;
1640 error:
1641 isl_mat_free(div_i);
1642 isl_mat_free(div_j);
1643 free(exp1);
1644 free(exp2);
1645 return -1;
1648 /* Check if the basic map "j" is a subset of basic map "i",
1649 * if "i" has fewer divs that "j".
1650 * If so, remove basic map "j".
1652 * If the two basic maps have the same number of divs, then
1653 * they must necessarily be different. Otherwise, we would have
1654 * called coalesce_local_pair. We therefore don't try anything
1655 * in this case.
1657 static int coalesced_subset(int i, int j, struct isl_coalesce_info *info)
1659 int superset;
1661 if (info[i].bmap->n_div >= info[j].bmap->n_div)
1662 return 0;
1664 superset = contains_after_aligning_divs(info[i].bmap, &info[j]);
1665 if (superset < 0)
1666 return -1;
1667 if (superset)
1668 drop(&info[j]);
1670 return superset;
1673 /* Check if one of the basic maps is a subset of the other and, if so,
1674 * drop the subset.
1675 * Note that we only perform any test if the number of divs is different
1676 * in the two basic maps. In case the number of divs is the same,
1677 * we have already established that the divs are different
1678 * in the two basic maps.
1679 * In particular, if the number of divs of basic map i is smaller than
1680 * the number of divs of basic map j, then we check if j is a subset of i
1681 * and vice versa.
1683 static enum isl_change check_coalesce_subset(int i, int j,
1684 struct isl_coalesce_info *info)
1686 int changed;
1688 changed = coalesced_subset(i, j, info);
1689 if (changed < 0 || changed)
1690 return changed < 0 ? isl_change_error : isl_change_drop_second;
1692 changed = coalesced_subset(j, i, info);
1693 if (changed < 0 || changed)
1694 return changed < 0 ? isl_change_error : isl_change_drop_first;
1696 return isl_change_none;
1699 /* Check if the union of the given pair of basic maps
1700 * can be represented by a single basic map.
1701 * If so, replace the pair by the single basic map and return
1702 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
1703 * Otherwise, return isl_change_none.
1705 * We first check if the two basic maps live in the same local space.
1706 * If so, we do the complete check. Otherwise, we check if one is
1707 * an obvious subset of the other.
1709 static enum isl_change coalesce_pair(int i, int j,
1710 struct isl_coalesce_info *info)
1712 int same;
1714 same = same_divs(info[i].bmap, info[j].bmap);
1715 if (same < 0)
1716 return isl_change_error;
1717 if (same)
1718 return coalesce_local_pair(i, j, info);
1720 return check_coalesce_subset(i, j, info);
1723 /* Pairwise coalesce the basic maps described by the "n" elements of "info",
1724 * skipping basic maps that have been removed (either before or within
1725 * this function).
1727 * For each basic map i, we check if it can be coalesced with respect
1728 * to any previously considered basic map j.
1729 * If i gets dropped (because it was a subset of some j), then
1730 * we can move on to the next basic map.
1731 * If j gets dropped, we need to continue checking against the other
1732 * previously considered basic maps.
1733 * If the two basic maps got fused, then we recheck the fused basic map
1734 * against the previously considered basic maps.
1736 static int coalesce(isl_ctx *ctx, int n, struct isl_coalesce_info *info)
1738 int i, j;
1740 for (i = n - 2; i >= 0; --i) {
1741 if (info[i].removed)
1742 continue;
1743 for (j = i + 1; j < n; ++j) {
1744 enum isl_change changed;
1746 if (info[j].removed)
1747 continue;
1748 if (info[i].removed)
1749 isl_die(ctx, isl_error_internal,
1750 "basic map unexpectedly removed",
1751 return -1);
1752 changed = coalesce_pair(i, j, info);
1753 switch (changed) {
1754 case isl_change_error:
1755 return -1;
1756 case isl_change_none:
1757 case isl_change_drop_second:
1758 continue;
1759 case isl_change_drop_first:
1760 j = n;
1761 break;
1762 case isl_change_fuse:
1763 j = i;
1764 break;
1769 return 0;
1772 /* Update the basic maps in "map" based on the information in "info".
1773 * In particular, remove the basic maps that have been marked removed and
1774 * update the others based on the information in the corresponding tableau.
1775 * Since we detected implicit equalities without calling
1776 * isl_basic_map_gauss, we need to do it now.
1778 static __isl_give isl_map *update_basic_maps(__isl_take isl_map *map,
1779 int n, struct isl_coalesce_info *info)
1781 int i;
1783 if (!map)
1784 return NULL;
1786 for (i = n - 1; i >= 0; --i) {
1787 if (info[i].removed) {
1788 isl_basic_map_free(map->p[i]);
1789 if (i != map->n - 1)
1790 map->p[i] = map->p[map->n - 1];
1791 map->n--;
1792 continue;
1795 info[i].bmap = isl_basic_map_update_from_tab(info[i].bmap,
1796 info[i].tab);
1797 info[i].bmap = isl_basic_map_gauss(info[i].bmap, NULL);
1798 info[i].bmap = isl_basic_map_finalize(info[i].bmap);
1799 if (!info[i].bmap)
1800 return isl_map_free(map);
1801 ISL_F_SET(info[i].bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1802 ISL_F_SET(info[i].bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1803 isl_basic_map_free(map->p[i]);
1804 map->p[i] = info[i].bmap;
1805 info[i].bmap = NULL;
1808 return map;
1811 /* For each pair of basic maps in the map, check if the union of the two
1812 * can be represented by a single basic map.
1813 * If so, replace the pair by the single basic map and start over.
1815 * Since we are constructing the tableaus of the basic maps anyway,
1816 * we exploit them to detect implicit equalities and redundant constraints.
1817 * This also helps the coalescing as it can ignore the redundant constraints.
1818 * In order to avoid confusion, we make all implicit equalities explicit
1819 * in the basic maps. We don't call isl_basic_map_gauss, though,
1820 * as that may affect the number of constraints.
1821 * This means that we have to call isl_basic_map_gauss at the end
1822 * of the computation (in update_basic_maps) to ensure that
1823 * the basic maps are not left in an unexpected state.
1825 struct isl_map *isl_map_coalesce(struct isl_map *map)
1827 int i;
1828 unsigned n;
1829 isl_ctx *ctx;
1830 struct isl_coalesce_info *info = NULL;
1832 map = isl_map_remove_empty_parts(map);
1833 if (!map)
1834 return NULL;
1836 if (map->n <= 1)
1837 return map;
1839 ctx = isl_map_get_ctx(map);
1840 map = isl_map_sort_divs(map);
1841 map = isl_map_cow(map);
1843 if (!map)
1844 return NULL;
1846 n = map->n;
1848 info = isl_calloc_array(map->ctx, struct isl_coalesce_info, n);
1849 if (!info)
1850 goto error;
1852 for (i = 0; i < map->n; ++i) {
1853 info[i].bmap = isl_basic_map_copy(map->p[i]);
1854 info[i].tab = isl_tab_from_basic_map(info[i].bmap, 0);
1855 if (!info[i].tab)
1856 goto error;
1857 if (!ISL_F_ISSET(info[i].bmap, ISL_BASIC_MAP_NO_IMPLICIT))
1858 if (isl_tab_detect_implicit_equalities(info[i].tab) < 0)
1859 goto error;
1860 info[i].bmap = isl_tab_make_equalities_explicit(info[i].tab,
1861 info[i].bmap);
1862 if (!info[i].bmap)
1863 goto error;
1864 if (!ISL_F_ISSET(info[i].bmap, ISL_BASIC_MAP_NO_REDUNDANT))
1865 if (isl_tab_detect_redundant(info[i].tab) < 0)
1866 goto error;
1868 for (i = map->n - 1; i >= 0; --i)
1869 if (info[i].tab->empty)
1870 drop(&info[i]);
1872 if (coalesce(ctx, n, info) < 0)
1873 goto error;
1875 map = update_basic_maps(map, n, info);
1877 clear_coalesce_info(n, info);
1879 return map;
1880 error:
1881 clear_coalesce_info(n, info);
1882 isl_map_free(map);
1883 return NULL;
1886 /* For each pair of basic sets in the set, check if the union of the two
1887 * can be represented by a single basic set.
1888 * If so, replace the pair by the single basic set and start over.
1890 struct isl_set *isl_set_coalesce(struct isl_set *set)
1892 return (struct isl_set *)isl_map_coalesce((struct isl_map *)set);