isl_coalesce.c: coalesce_with_expanded_divs: store status in isl_coalesce_info
[isl.git] / isl_coalesce.c
blob06f266011c6b8fe9347381ce37ed449cca1323c4
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2012-2013 Ecole Normale Superieure
5 * Copyright 2014 INRIA Rocquencourt
7 * Use of this software is governed by the MIT license
9 * Written by Sven Verdoolaege, K.U.Leuven, Departement
10 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
11 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
12 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
13 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
14 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
15 * B.P. 105 - 78153 Le Chesnay, France
18 #include "isl_map_private.h"
19 #include <isl_seq.h>
20 #include <isl/options.h>
21 #include "isl_tab.h"
22 #include <isl_mat_private.h>
23 #include <isl_local_space_private.h>
24 #include <isl_vec_private.h>
25 #include <isl_aff_private.h>
27 #define STATUS_ERROR -1
28 #define STATUS_REDUNDANT 1
29 #define STATUS_VALID 2
30 #define STATUS_SEPARATE 3
31 #define STATUS_CUT 4
32 #define STATUS_ADJ_EQ 5
33 #define STATUS_ADJ_INEQ 6
35 static int status_in(isl_int *ineq, struct isl_tab *tab)
37 enum isl_ineq_type type = isl_tab_ineq_type(tab, ineq);
38 switch (type) {
39 default:
40 case isl_ineq_error: return STATUS_ERROR;
41 case isl_ineq_redundant: return STATUS_VALID;
42 case isl_ineq_separate: return STATUS_SEPARATE;
43 case isl_ineq_cut: return STATUS_CUT;
44 case isl_ineq_adj_eq: return STATUS_ADJ_EQ;
45 case isl_ineq_adj_ineq: return STATUS_ADJ_INEQ;
49 /* Compute the position of the equalities of basic map "bmap_i"
50 * with respect to the basic map represented by "tab_j".
51 * The resulting array has twice as many entries as the number
52 * of equalities corresponding to the two inequalties to which
53 * each equality corresponds.
55 static int *eq_status_in(__isl_keep isl_basic_map *bmap_i,
56 struct isl_tab *tab_j)
58 int k, l;
59 int *eq = isl_calloc_array(bmap_i->ctx, int, 2 * bmap_i->n_eq);
60 unsigned dim;
62 if (!eq)
63 return NULL;
65 dim = isl_basic_map_total_dim(bmap_i);
66 for (k = 0; k < bmap_i->n_eq; ++k) {
67 for (l = 0; l < 2; ++l) {
68 isl_seq_neg(bmap_i->eq[k], bmap_i->eq[k], 1+dim);
69 eq[2 * k + l] = status_in(bmap_i->eq[k], tab_j);
70 if (eq[2 * k + l] == STATUS_ERROR)
71 goto error;
73 if (eq[2 * k] == STATUS_SEPARATE ||
74 eq[2 * k + 1] == STATUS_SEPARATE)
75 break;
78 return eq;
79 error:
80 free(eq);
81 return NULL;
84 /* Compute the position of the inequalities of basic map "bmap_i"
85 * (also represented by "tab_i", if not NULL) with respect to the basic map
86 * represented by "tab_j".
88 static int *ineq_status_in(__isl_keep isl_basic_map *bmap_i,
89 struct isl_tab *tab_i, struct isl_tab *tab_j)
91 int k;
92 unsigned n_eq = bmap_i->n_eq;
93 int *ineq = isl_calloc_array(bmap_i->ctx, int, bmap_i->n_ineq);
95 if (!ineq)
96 return NULL;
98 for (k = 0; k < bmap_i->n_ineq; ++k) {
99 if (tab_i && isl_tab_is_redundant(tab_i, n_eq + k)) {
100 ineq[k] = STATUS_REDUNDANT;
101 continue;
103 ineq[k] = status_in(bmap_i->ineq[k], tab_j);
104 if (ineq[k] == STATUS_ERROR)
105 goto error;
106 if (ineq[k] == STATUS_SEPARATE)
107 break;
110 return ineq;
111 error:
112 free(ineq);
113 return NULL;
116 static int any(int *con, unsigned len, int status)
118 int i;
120 for (i = 0; i < len ; ++i)
121 if (con[i] == status)
122 return 1;
123 return 0;
126 static int count(int *con, unsigned len, int status)
128 int i;
129 int c = 0;
131 for (i = 0; i < len ; ++i)
132 if (con[i] == status)
133 c++;
134 return c;
137 static int all(int *con, unsigned len, int status)
139 int i;
141 for (i = 0; i < len ; ++i) {
142 if (con[i] == STATUS_REDUNDANT)
143 continue;
144 if (con[i] != status)
145 return 0;
147 return 1;
150 /* Internal information associated to a basic map in a map
151 * that is to be coalesced by isl_map_coalesce.
153 * "bmap" is the basic map itself (or NULL if "removed" is set)
154 * "tab" is the corresponding tableau (or NULL if "removed" is set)
155 * "hull_hash" identifies the affine space in which "bmap" lives.
156 * "removed" is set if this basic map has been removed from the map
157 * "simplify" is set if this basic map may have some unknown integer
158 * divisions that were not present in the input basic maps. The basic
159 * map should then be simplified such that we may be able to find
160 * a definition among the constraints.
162 * "eq" and "ineq" are only set if we are currently trying to coalesce
163 * this basic map with another basic map, in which case they represent
164 * the position of the inequalities of this basic map with respect to
165 * the other basic map. The number of elements in the "eq" array
166 * is twice the number of equalities in the "bmap", corresponding
167 * to the two inequalities that make up each equality.
169 struct isl_coalesce_info {
170 isl_basic_map *bmap;
171 struct isl_tab *tab;
172 uint32_t hull_hash;
173 int removed;
174 int simplify;
175 int *eq;
176 int *ineq;
179 /* Compute the hash of the (apparent) affine hull of info->bmap (with
180 * the existentially quantified variables removed) and store it
181 * in info->hash.
183 static int coalesce_info_set_hull_hash(struct isl_coalesce_info *info)
185 isl_basic_map *hull;
186 unsigned n_div;
188 hull = isl_basic_map_copy(info->bmap);
189 hull = isl_basic_map_plain_affine_hull(hull);
190 n_div = isl_basic_map_dim(hull, isl_dim_div);
191 hull = isl_basic_map_drop_constraints_involving_dims(hull,
192 isl_dim_div, 0, n_div);
193 info->hull_hash = isl_basic_map_get_hash(hull);
194 isl_basic_map_free(hull);
196 return hull ? 0 : -1;
199 /* Free all the allocated memory in an array
200 * of "n" isl_coalesce_info elements.
202 static void clear_coalesce_info(int n, struct isl_coalesce_info *info)
204 int i;
206 if (!info)
207 return;
209 for (i = 0; i < n; ++i) {
210 isl_basic_map_free(info[i].bmap);
211 isl_tab_free(info[i].tab);
214 free(info);
217 /* Drop the basic map represented by "info".
218 * That is, clear the memory associated to the entry and
219 * mark it as having been removed.
221 static void drop(struct isl_coalesce_info *info)
223 info->bmap = isl_basic_map_free(info->bmap);
224 isl_tab_free(info->tab);
225 info->tab = NULL;
226 info->removed = 1;
229 /* Exchange the information in "info1" with that in "info2".
231 static void exchange(struct isl_coalesce_info *info1,
232 struct isl_coalesce_info *info2)
234 struct isl_coalesce_info info;
236 info = *info1;
237 *info1 = *info2;
238 *info2 = info;
241 /* This type represents the kind of change that has been performed
242 * while trying to coalesce two basic maps.
244 * isl_change_none: nothing was changed
245 * isl_change_drop_first: the first basic map was removed
246 * isl_change_drop_second: the second basic map was removed
247 * isl_change_fuse: the two basic maps were replaced by a new basic map.
249 enum isl_change {
250 isl_change_error = -1,
251 isl_change_none = 0,
252 isl_change_drop_first,
253 isl_change_drop_second,
254 isl_change_fuse,
257 /* Update "change" based on an interchange of the first and the second
258 * basic map. That is, interchange isl_change_drop_first and
259 * isl_change_drop_second.
261 static enum isl_change invert_change(enum isl_change change)
263 switch (change) {
264 case isl_change_error:
265 return isl_change_error;
266 case isl_change_none:
267 return isl_change_none;
268 case isl_change_drop_first:
269 return isl_change_drop_second;
270 case isl_change_drop_second:
271 return isl_change_drop_first;
272 case isl_change_fuse:
273 return isl_change_fuse;
276 return isl_change_error;
279 /* Add the valid constraints of the basic map represented by "info"
280 * to "bmap". "len" is the size of the constraints.
281 * If only one of the pair of inequalities that make up an equality
282 * is valid, then add that inequality.
284 static __isl_give isl_basic_map *add_valid_constraints(
285 __isl_take isl_basic_map *bmap, struct isl_coalesce_info *info,
286 unsigned len)
288 int k, l;
290 if (!bmap)
291 return NULL;
293 for (k = 0; k < info->bmap->n_eq; ++k) {
294 if (info->eq[2 * k] == STATUS_VALID &&
295 info->eq[2 * k + 1] == STATUS_VALID) {
296 l = isl_basic_map_alloc_equality(bmap);
297 if (l < 0)
298 return isl_basic_map_free(bmap);
299 isl_seq_cpy(bmap->eq[l], info->bmap->eq[k], len);
300 } else if (info->eq[2 * k] == STATUS_VALID) {
301 l = isl_basic_map_alloc_inequality(bmap);
302 if (l < 0)
303 return isl_basic_map_free(bmap);
304 isl_seq_neg(bmap->ineq[l], info->bmap->eq[k], len);
305 } else if (info->eq[2 * k + 1] == STATUS_VALID) {
306 l = isl_basic_map_alloc_inequality(bmap);
307 if (l < 0)
308 return isl_basic_map_free(bmap);
309 isl_seq_cpy(bmap->ineq[l], info->bmap->eq[k], len);
313 for (k = 0; k < info->bmap->n_ineq; ++k) {
314 if (info->ineq[k] != STATUS_VALID)
315 continue;
316 l = isl_basic_map_alloc_inequality(bmap);
317 if (l < 0)
318 return isl_basic_map_free(bmap);
319 isl_seq_cpy(bmap->ineq[l], info->bmap->ineq[k], len);
322 return bmap;
325 /* Is "bmap" defined by a number of (non-redundant) constraints that
326 * is greater than the number of constraints of basic maps i and j combined?
327 * Equalities are counted as two inequalities.
329 static int number_of_constraints_increases(int i, int j,
330 struct isl_coalesce_info *info,
331 __isl_keep isl_basic_map *bmap, struct isl_tab *tab)
333 int k, n_old, n_new;
335 n_old = 2 * info[i].bmap->n_eq + info[i].bmap->n_ineq;
336 n_old += 2 * info[j].bmap->n_eq + info[j].bmap->n_ineq;
338 n_new = 2 * bmap->n_eq;
339 for (k = 0; k < bmap->n_ineq; ++k)
340 if (!isl_tab_is_redundant(tab, bmap->n_eq + k))
341 ++n_new;
343 return n_new > n_old;
346 /* Replace the pair of basic maps i and j by the basic map bounded
347 * by the valid constraints in both basic maps and the constraints
348 * in extra (if not NULL).
349 * Place the fused basic map in the position that is the smallest of i and j.
351 * If "detect_equalities" is set, then look for equalities encoded
352 * as pairs of inequalities.
353 * If "check_number" is set, then the original basic maps are only
354 * replaced if the total number of constraints does not increase.
355 * While the number of integer divisions in the two basic maps
356 * is assumed to be the same, the actual definitions may be different.
357 * We only copy the definition from one of the basic map if it is
358 * the same as that of the other basic map. Otherwise, we mark
359 * the integer division as unknown and schedule for the basic map
360 * to be simplified in an attempt to recover the integer division definition.
362 static enum isl_change fuse(int i, int j, struct isl_coalesce_info *info,
363 __isl_keep isl_mat *extra, int detect_equalities, int check_number)
365 int k, l;
366 struct isl_basic_map *fused = NULL;
367 struct isl_tab *fused_tab = NULL;
368 unsigned total = isl_basic_map_total_dim(info[i].bmap);
369 unsigned extra_rows = extra ? extra->n_row : 0;
370 unsigned n_eq, n_ineq;
372 if (j < i)
373 return fuse(j, i, info, extra, detect_equalities, check_number);
375 n_eq = info[i].bmap->n_eq + info[j].bmap->n_eq;
376 n_ineq = info[i].bmap->n_ineq + info[j].bmap->n_ineq;
377 fused = isl_basic_map_alloc_space(isl_space_copy(info[i].bmap->dim),
378 info[i].bmap->n_div, n_eq, n_eq + n_ineq + extra_rows);
379 fused = add_valid_constraints(fused, &info[i], 1 + total);
380 fused = add_valid_constraints(fused, &info[j], 1 + total);
381 if (!fused)
382 goto error;
384 for (k = 0; k < info[i].bmap->n_div; ++k) {
385 int l = isl_basic_map_alloc_div(fused);
386 if (l < 0)
387 goto error;
388 if (isl_seq_eq(info[i].bmap->div[k], info[j].bmap->div[k],
389 1 + 1 + total)) {
390 isl_seq_cpy(fused->div[l], info[i].bmap->div[k],
391 1 + 1 + total);
392 } else {
393 isl_int_set_si(fused->div[l][0], 0);
394 info[i].simplify = 1;
398 for (k = 0; k < extra_rows; ++k) {
399 l = isl_basic_map_alloc_inequality(fused);
400 if (l < 0)
401 goto error;
402 isl_seq_cpy(fused->ineq[l], extra->row[k], 1 + total);
405 if (detect_equalities)
406 fused = isl_basic_map_detect_inequality_pairs(fused, NULL);
407 fused = isl_basic_map_gauss(fused, NULL);
408 ISL_F_SET(fused, ISL_BASIC_MAP_FINAL);
409 if (ISL_F_ISSET(info[i].bmap, ISL_BASIC_MAP_RATIONAL) &&
410 ISL_F_ISSET(info[j].bmap, ISL_BASIC_MAP_RATIONAL))
411 ISL_F_SET(fused, ISL_BASIC_MAP_RATIONAL);
413 fused_tab = isl_tab_from_basic_map(fused, 0);
414 if (isl_tab_detect_redundant(fused_tab) < 0)
415 goto error;
417 if (check_number &&
418 number_of_constraints_increases(i, j, info, fused, fused_tab)) {
419 isl_tab_free(fused_tab);
420 isl_basic_map_free(fused);
421 return isl_change_none;
424 info[i].simplify |= info[j].simplify;
425 isl_basic_map_free(info[i].bmap);
426 info[i].bmap = fused;
427 isl_tab_free(info[i].tab);
428 info[i].tab = fused_tab;
429 drop(&info[j]);
431 return isl_change_fuse;
432 error:
433 isl_tab_free(fused_tab);
434 isl_basic_map_free(fused);
435 return isl_change_error;
438 /* Given a pair of basic maps i and j such that all constraints are either
439 * "valid" or "cut", check if the facets corresponding to the "cut"
440 * constraints of i lie entirely within basic map j.
441 * If so, replace the pair by the basic map consisting of the valid
442 * constraints in both basic maps.
443 * Checking whether the facet lies entirely within basic map j
444 * is performed by checking whether the constraints of basic map j
445 * are valid for the facet. These tests are performed on a rational
446 * tableau to avoid the theoretical possibility that a constraint
447 * that was considered to be a cut constraint for the entire basic map i
448 * happens to be considered to be a valid constraint for the facet,
449 * even though it cuts off the same rational points.
451 * To see that we are not introducing any extra points, call the
452 * two basic maps A and B and the resulting map U and let x
453 * be an element of U \setminus ( A \cup B ).
454 * A line connecting x with an element of A \cup B meets a facet F
455 * of either A or B. Assume it is a facet of B and let c_1 be
456 * the corresponding facet constraint. We have c_1(x) < 0 and
457 * so c_1 is a cut constraint. This implies that there is some
458 * (possibly rational) point x' satisfying the constraints of A
459 * and the opposite of c_1 as otherwise c_1 would have been marked
460 * valid for A. The line connecting x and x' meets a facet of A
461 * in a (possibly rational) point that also violates c_1, but this
462 * is impossible since all cut constraints of B are valid for all
463 * cut facets of A.
464 * In case F is a facet of A rather than B, then we can apply the
465 * above reasoning to find a facet of B separating x from A \cup B first.
467 static enum isl_change check_facets(int i, int j,
468 struct isl_coalesce_info *info)
470 int k, l;
471 struct isl_tab_undo *snap, *snap2;
472 unsigned n_eq = info[i].bmap->n_eq;
474 snap = isl_tab_snap(info[i].tab);
475 if (isl_tab_mark_rational(info[i].tab) < 0)
476 return isl_change_error;
477 snap2 = isl_tab_snap(info[i].tab);
479 for (k = 0; k < info[i].bmap->n_ineq; ++k) {
480 if (info[i].ineq[k] != STATUS_CUT)
481 continue;
482 if (isl_tab_select_facet(info[i].tab, n_eq + k) < 0)
483 return isl_change_error;
484 for (l = 0; l < info[j].bmap->n_ineq; ++l) {
485 int stat;
486 if (info[j].ineq[l] != STATUS_CUT)
487 continue;
488 stat = status_in(info[j].bmap->ineq[l], info[i].tab);
489 if (stat < 0)
490 return isl_change_error;
491 if (stat != STATUS_VALID)
492 break;
494 if (isl_tab_rollback(info[i].tab, snap2) < 0)
495 return isl_change_error;
496 if (l < info[j].bmap->n_ineq)
497 break;
500 if (k < info[i].bmap->n_ineq) {
501 if (isl_tab_rollback(info[i].tab, snap) < 0)
502 return isl_change_error;
503 return isl_change_none;
505 return fuse(i, j, info, NULL, 0, 0);
508 /* Check if info->bmap contains the basic map represented
509 * by the tableau "tab".
510 * For each equality, we check both the constraint itself
511 * (as an inequality) and its negation. Make sure the
512 * equality is returned to its original state before returning.
514 static int contains(struct isl_coalesce_info *info, struct isl_tab *tab)
516 int k;
517 unsigned dim;
518 isl_basic_map *bmap = info->bmap;
520 dim = isl_basic_map_total_dim(bmap);
521 for (k = 0; k < bmap->n_eq; ++k) {
522 int stat;
523 isl_seq_neg(bmap->eq[k], bmap->eq[k], 1 + dim);
524 stat = status_in(bmap->eq[k], tab);
525 isl_seq_neg(bmap->eq[k], bmap->eq[k], 1 + dim);
526 if (stat < 0)
527 return -1;
528 if (stat != STATUS_VALID)
529 return 0;
530 stat = status_in(bmap->eq[k], tab);
531 if (stat < 0)
532 return -1;
533 if (stat != STATUS_VALID)
534 return 0;
537 for (k = 0; k < bmap->n_ineq; ++k) {
538 int stat;
539 if (info->ineq[k] == STATUS_REDUNDANT)
540 continue;
541 stat = status_in(bmap->ineq[k], tab);
542 if (stat < 0)
543 return -1;
544 if (stat != STATUS_VALID)
545 return 0;
547 return 1;
550 /* Basic map "i" has an inequality (say "k") that is adjacent
551 * to some inequality of basic map "j". All the other inequalities
552 * are valid for "j".
553 * Check if basic map "j" forms an extension of basic map "i".
555 * Note that this function is only called if some of the equalities or
556 * inequalities of basic map "j" do cut basic map "i". The function is
557 * correct even if there are no such cut constraints, but in that case
558 * the additional checks performed by this function are overkill.
560 * In particular, we replace constraint k, say f >= 0, by constraint
561 * f <= -1, add the inequalities of "j" that are valid for "i"
562 * and check if the result is a subset of basic map "j".
563 * If so, then we know that this result is exactly equal to basic map "j"
564 * since all its constraints are valid for basic map "j".
565 * By combining the valid constraints of "i" (all equalities and all
566 * inequalities except "k") and the valid constraints of "j" we therefore
567 * obtain a basic map that is equal to their union.
568 * In this case, there is no need to perform a rollback of the tableau
569 * since it is going to be destroyed in fuse().
572 * |\__ |\__
573 * | \__ | \__
574 * | \_ => | \__
575 * |_______| _ |_________\
578 * |\ |\
579 * | \ | \
580 * | \ | \
581 * | | | \
582 * | ||\ => | \
583 * | || \ | \
584 * | || | | |
585 * |__||_/ |_____/
587 static enum isl_change is_adj_ineq_extension(int i, int j,
588 struct isl_coalesce_info *info)
590 int k;
591 struct isl_tab_undo *snap;
592 unsigned n_eq = info[i].bmap->n_eq;
593 unsigned total = isl_basic_map_total_dim(info[i].bmap);
594 int r;
595 int super;
597 if (isl_tab_extend_cons(info[i].tab, 1 + info[j].bmap->n_ineq) < 0)
598 return isl_change_error;
600 for (k = 0; k < info[i].bmap->n_ineq; ++k)
601 if (info[i].ineq[k] == STATUS_ADJ_INEQ)
602 break;
603 if (k >= info[i].bmap->n_ineq)
604 isl_die(isl_basic_map_get_ctx(info[i].bmap), isl_error_internal,
605 "info[i].ineq should have exactly one STATUS_ADJ_INEQ",
606 return isl_change_error);
608 snap = isl_tab_snap(info[i].tab);
610 if (isl_tab_unrestrict(info[i].tab, n_eq + k) < 0)
611 return isl_change_error;
613 isl_seq_neg(info[i].bmap->ineq[k], info[i].bmap->ineq[k], 1 + total);
614 isl_int_sub_ui(info[i].bmap->ineq[k][0], info[i].bmap->ineq[k][0], 1);
615 r = isl_tab_add_ineq(info[i].tab, info[i].bmap->ineq[k]);
616 isl_seq_neg(info[i].bmap->ineq[k], info[i].bmap->ineq[k], 1 + total);
617 isl_int_sub_ui(info[i].bmap->ineq[k][0], info[i].bmap->ineq[k][0], 1);
618 if (r < 0)
619 return isl_change_error;
621 for (k = 0; k < info[j].bmap->n_ineq; ++k) {
622 if (info[j].ineq[k] != STATUS_VALID)
623 continue;
624 if (isl_tab_add_ineq(info[i].tab, info[j].bmap->ineq[k]) < 0)
625 return isl_change_error;
628 super = contains(&info[j], info[i].tab);
629 if (super < 0)
630 return isl_change_error;
631 if (super)
632 return fuse(i, j, info, NULL, 0, 0);
634 if (isl_tab_rollback(info[i].tab, snap) < 0)
635 return isl_change_error;
637 return isl_change_none;
641 /* Both basic maps have at least one inequality with and adjacent
642 * (but opposite) inequality in the other basic map.
643 * Check that there are no cut constraints and that there is only
644 * a single pair of adjacent inequalities.
645 * If so, we can replace the pair by a single basic map described
646 * by all but the pair of adjacent inequalities.
647 * Any additional points introduced lie strictly between the two
648 * adjacent hyperplanes and can therefore be integral.
650 * ____ _____
651 * / ||\ / \
652 * / || \ / \
653 * \ || \ => \ \
654 * \ || / \ /
655 * \___||_/ \_____/
657 * The test for a single pair of adjancent inequalities is important
658 * for avoiding the combination of two basic maps like the following
660 * /|
661 * / |
662 * /__|
663 * _____
664 * | |
665 * | |
666 * |___|
668 * If there are some cut constraints on one side, then we may
669 * still be able to fuse the two basic maps, but we need to perform
670 * some additional checks in is_adj_ineq_extension.
672 static enum isl_change check_adj_ineq(int i, int j,
673 struct isl_coalesce_info *info)
675 int count_i, count_j;
676 int cut_i, cut_j;
678 count_i = count(info[i].ineq, info[i].bmap->n_ineq, STATUS_ADJ_INEQ);
679 count_j = count(info[j].ineq, info[j].bmap->n_ineq, STATUS_ADJ_INEQ);
681 if (count_i != 1 && count_j != 1)
682 return isl_change_none;
684 cut_i = any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_CUT) ||
685 any(info[i].ineq, info[i].bmap->n_ineq, STATUS_CUT);
686 cut_j = any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_CUT) ||
687 any(info[j].ineq, info[j].bmap->n_ineq, STATUS_CUT);
689 if (!cut_i && !cut_j && count_i == 1 && count_j == 1)
690 return fuse(i, j, info, NULL, 0, 0);
692 if (count_i == 1 && !cut_i)
693 return is_adj_ineq_extension(i, j, info);
695 if (count_j == 1 && !cut_j)
696 return is_adj_ineq_extension(j, i, info);
698 return isl_change_none;
701 /* Basic map "i" has an inequality "k" that is adjacent to some equality
702 * of basic map "j". All the other inequalities are valid for "j".
703 * Check if basic map "j" forms an extension of basic map "i".
705 * In particular, we relax constraint "k", compute the corresponding
706 * facet and check whether it is included in the other basic map.
707 * If so, we know that relaxing the constraint extends the basic
708 * map with exactly the other basic map (we already know that this
709 * other basic map is included in the extension, because there
710 * were no "cut" inequalities in "i") and we can replace the
711 * two basic maps by this extension.
712 * Each integer division that does not have exactly the same
713 * definition in "i" and "j" is marked unknown and the basic map
714 * is scheduled to be simplified in an attempt to recover
715 * the integer division definition.
716 * Place this extension in the position that is the smallest of i and j.
717 * ____ _____
718 * / || / |
719 * / || / |
720 * \ || => \ |
721 * \ || \ |
722 * \___|| \____|
724 static enum isl_change is_adj_eq_extension(int i, int j, int k,
725 struct isl_coalesce_info *info)
727 int change = isl_change_none;
728 int super;
729 struct isl_tab_undo *snap, *snap2;
730 unsigned n_eq = info[i].bmap->n_eq;
732 if (isl_tab_is_equality(info[i].tab, n_eq + k))
733 return isl_change_none;
735 snap = isl_tab_snap(info[i].tab);
736 if (isl_tab_relax(info[i].tab, n_eq + k) < 0)
737 return isl_change_error;
738 snap2 = isl_tab_snap(info[i].tab);
739 if (isl_tab_select_facet(info[i].tab, n_eq + k) < 0)
740 return isl_change_error;
741 super = contains(&info[j], info[i].tab);
742 if (super < 0)
743 return isl_change_error;
744 if (super) {
745 int l;
746 unsigned total;
748 if (isl_tab_rollback(info[i].tab, snap2) < 0)
749 return isl_change_error;
750 info[i].bmap = isl_basic_map_cow(info[i].bmap);
751 if (!info[i].bmap)
752 return isl_change_error;
753 total = isl_basic_map_total_dim(info[i].bmap);
754 for (l = 0; l < info[i].bmap->n_div; ++l)
755 if (!isl_seq_eq(info[i].bmap->div[l],
756 info[j].bmap->div[l], 1 + 1 + total)) {
757 isl_int_set_si(info[i].bmap->div[l][0], 0);
758 info[i].simplify = 1;
760 isl_int_add_ui(info[i].bmap->ineq[k][0],
761 info[i].bmap->ineq[k][0], 1);
762 ISL_F_SET(info[i].bmap, ISL_BASIC_MAP_FINAL);
763 drop(&info[j]);
764 if (j < i)
765 exchange(&info[i], &info[j]);
766 change = isl_change_fuse;
767 } else
768 if (isl_tab_rollback(info[i].tab, snap) < 0)
769 return isl_change_error;
771 return change;
774 /* Data structure that keeps track of the wrapping constraints
775 * and of information to bound the coefficients of those constraints.
777 * bound is set if we want to apply a bound on the coefficients
778 * mat contains the wrapping constraints
779 * max is the bound on the coefficients (if bound is set)
781 struct isl_wraps {
782 int bound;
783 isl_mat *mat;
784 isl_int max;
787 /* Update wraps->max to be greater than or equal to the coefficients
788 * in the equalities and inequalities of info->bmap that can be removed
789 * if we end up applying wrapping.
791 static void wraps_update_max(struct isl_wraps *wraps,
792 struct isl_coalesce_info *info)
794 int k;
795 isl_int max_k;
796 unsigned total = isl_basic_map_total_dim(info->bmap);
798 isl_int_init(max_k);
800 for (k = 0; k < info->bmap->n_eq; ++k) {
801 if (info->eq[2 * k] == STATUS_VALID &&
802 info->eq[2 * k + 1] == STATUS_VALID)
803 continue;
804 isl_seq_abs_max(info->bmap->eq[k] + 1, total, &max_k);
805 if (isl_int_abs_gt(max_k, wraps->max))
806 isl_int_set(wraps->max, max_k);
809 for (k = 0; k < info->bmap->n_ineq; ++k) {
810 if (info->ineq[k] == STATUS_VALID ||
811 info->ineq[k] == STATUS_REDUNDANT)
812 continue;
813 isl_seq_abs_max(info->bmap->ineq[k] + 1, total, &max_k);
814 if (isl_int_abs_gt(max_k, wraps->max))
815 isl_int_set(wraps->max, max_k);
818 isl_int_clear(max_k);
821 /* Initialize the isl_wraps data structure.
822 * If we want to bound the coefficients of the wrapping constraints,
823 * we set wraps->max to the largest coefficient
824 * in the equalities and inequalities that can be removed if we end up
825 * applying wrapping.
827 static void wraps_init(struct isl_wraps *wraps, __isl_take isl_mat *mat,
828 struct isl_coalesce_info *info, int i, int j)
830 isl_ctx *ctx;
832 wraps->bound = 0;
833 wraps->mat = mat;
834 if (!mat)
835 return;
836 ctx = isl_mat_get_ctx(mat);
837 wraps->bound = isl_options_get_coalesce_bounded_wrapping(ctx);
838 if (!wraps->bound)
839 return;
840 isl_int_init(wraps->max);
841 isl_int_set_si(wraps->max, 0);
842 wraps_update_max(wraps, &info[i]);
843 wraps_update_max(wraps, &info[j]);
846 /* Free the contents of the isl_wraps data structure.
848 static void wraps_free(struct isl_wraps *wraps)
850 isl_mat_free(wraps->mat);
851 if (wraps->bound)
852 isl_int_clear(wraps->max);
855 /* Is the wrapping constraint in row "row" allowed?
857 * If wraps->bound is set, we check that none of the coefficients
858 * is greater than wraps->max.
860 static int allow_wrap(struct isl_wraps *wraps, int row)
862 int i;
864 if (!wraps->bound)
865 return 1;
867 for (i = 1; i < wraps->mat->n_col; ++i)
868 if (isl_int_abs_gt(wraps->mat->row[row][i], wraps->max))
869 return 0;
871 return 1;
874 /* Wrap "ineq" (or its opposite if "negate" is set) around "bound"
875 * to include "set" and add the result in position "w" of "wraps".
876 * "len" is the total number of coefficients in "bound" and "ineq".
877 * Return 1 on success, 0 on failure and -1 on error.
878 * Wrapping can fail if the result of wrapping is equal to "bound"
879 * or if we want to bound the sizes of the coefficients and
880 * the wrapped constraint does not satisfy this bound.
882 static int add_wrap(struct isl_wraps *wraps, int w, isl_int *bound,
883 isl_int *ineq, unsigned len, __isl_keep isl_set *set, int negate)
885 isl_seq_cpy(wraps->mat->row[w], bound, len);
886 if (negate) {
887 isl_seq_neg(wraps->mat->row[w + 1], ineq, len);
888 ineq = wraps->mat->row[w + 1];
890 if (!isl_set_wrap_facet(set, wraps->mat->row[w], ineq))
891 return -1;
892 if (isl_seq_eq(wraps->mat->row[w], bound, len))
893 return 0;
894 if (!allow_wrap(wraps, w))
895 return 0;
896 return 1;
899 /* For each constraint in info->bmap that is not redundant (as determined
900 * by info->tab) and that is not a valid constraint for the other basic map,
901 * wrap the constraint around "bound" such that it includes the whole
902 * set "set" and append the resulting constraint to "wraps".
903 * Note that the constraints that are valid for the other basic map
904 * will be added to the combined basic map by default, so there is
905 * no need to wrap them.
906 * The caller wrap_in_facets even relies on this function not wrapping
907 * any constraints that are already valid.
908 * "wraps" is assumed to have been pre-allocated to the appropriate size.
909 * wraps->n_row is the number of actual wrapped constraints that have
910 * been added.
911 * If any of the wrapping problems results in a constraint that is
912 * identical to "bound", then this means that "set" is unbounded in such
913 * way that no wrapping is possible. If this happens then wraps->n_row
914 * is reset to zero.
915 * Similarly, if we want to bound the coefficients of the wrapping
916 * constraints and a newly added wrapping constraint does not
917 * satisfy the bound, then wraps->n_row is also reset to zero.
919 static int add_wraps(struct isl_wraps *wraps, struct isl_coalesce_info *info,
920 isl_int *bound, __isl_keep isl_set *set)
922 int l, m;
923 int w;
924 int added;
925 isl_basic_map *bmap = info->bmap;
926 unsigned len = 1 + isl_basic_map_total_dim(bmap);
928 w = wraps->mat->n_row;
930 for (l = 0; l < bmap->n_ineq; ++l) {
931 if (info->ineq[l] == STATUS_VALID ||
932 info->ineq[l] == STATUS_REDUNDANT)
933 continue;
934 if (isl_seq_is_neg(bound, bmap->ineq[l], len))
935 continue;
936 if (isl_seq_eq(bound, bmap->ineq[l], len))
937 continue;
938 if (isl_tab_is_redundant(info->tab, bmap->n_eq + l))
939 continue;
941 added = add_wrap(wraps, w, bound, bmap->ineq[l], len, set, 0);
942 if (added < 0)
943 return -1;
944 if (!added)
945 goto unbounded;
946 ++w;
948 for (l = 0; l < bmap->n_eq; ++l) {
949 if (isl_seq_is_neg(bound, bmap->eq[l], len))
950 continue;
951 if (isl_seq_eq(bound, bmap->eq[l], len))
952 continue;
954 for (m = 0; m < 2; ++m) {
955 if (info->eq[2 * l + m] == STATUS_VALID)
956 continue;
957 added = add_wrap(wraps, w, bound, bmap->eq[l], len,
958 set, !m);
959 if (added < 0)
960 return -1;
961 if (!added)
962 goto unbounded;
963 ++w;
967 wraps->mat->n_row = w;
968 return 0;
969 unbounded:
970 wraps->mat->n_row = 0;
971 return 0;
974 /* Check if the constraints in "wraps" from "first" until the last
975 * are all valid for the basic set represented by "tab".
976 * If not, wraps->n_row is set to zero.
978 static int check_wraps(__isl_keep isl_mat *wraps, int first,
979 struct isl_tab *tab)
981 int i;
983 for (i = first; i < wraps->n_row; ++i) {
984 enum isl_ineq_type type;
985 type = isl_tab_ineq_type(tab, wraps->row[i]);
986 if (type == isl_ineq_error)
987 return -1;
988 if (type == isl_ineq_redundant)
989 continue;
990 wraps->n_row = 0;
991 return 0;
994 return 0;
997 /* Return a set that corresponds to the non-redundant constraints
998 * (as recorded in tab) of bmap.
1000 * It's important to remove the redundant constraints as some
1001 * of the other constraints may have been modified after the
1002 * constraints were marked redundant.
1003 * In particular, a constraint may have been relaxed.
1004 * Redundant constraints are ignored when a constraint is relaxed
1005 * and should therefore continue to be ignored ever after.
1006 * Otherwise, the relaxation might be thwarted by some of
1007 * these constraints.
1009 * Update the underlying set to ensure that the dimension doesn't change.
1010 * Otherwise the integer divisions could get dropped if the tab
1011 * turns out to be empty.
1013 static __isl_give isl_set *set_from_updated_bmap(__isl_keep isl_basic_map *bmap,
1014 struct isl_tab *tab)
1016 isl_basic_set *bset;
1018 bmap = isl_basic_map_copy(bmap);
1019 bset = isl_basic_map_underlying_set(bmap);
1020 bset = isl_basic_set_cow(bset);
1021 bset = isl_basic_set_update_from_tab(bset, tab);
1022 return isl_set_from_basic_set(bset);
1025 /* Wrap the constraints of info->bmap that bound the facet defined
1026 * by inequality "k" around (the opposite of) this inequality to
1027 * include "set". "bound" may be used to store the negated inequality.
1028 * Since the wrapped constraints are not guaranteed to contain the whole
1029 * of info->bmap, we check them in check_wraps.
1030 * If any of the wrapped constraints turn out to be invalid, then
1031 * check_wraps will reset wrap->n_row to zero.
1033 static int add_wraps_around_facet(struct isl_wraps *wraps,
1034 struct isl_coalesce_info *info, int k, isl_int *bound,
1035 __isl_keep isl_set *set)
1037 struct isl_tab_undo *snap;
1038 int n;
1039 unsigned total = isl_basic_map_total_dim(info->bmap);
1041 snap = isl_tab_snap(info->tab);
1043 if (isl_tab_select_facet(info->tab, info->bmap->n_eq + k) < 0)
1044 return -1;
1045 if (isl_tab_detect_redundant(info->tab) < 0)
1046 return -1;
1048 isl_seq_neg(bound, info->bmap->ineq[k], 1 + total);
1050 n = wraps->mat->n_row;
1051 if (add_wraps(wraps, info, bound, set) < 0)
1052 return -1;
1054 if (isl_tab_rollback(info->tab, snap) < 0)
1055 return -1;
1056 if (check_wraps(wraps->mat, n, info->tab) < 0)
1057 return -1;
1059 return 0;
1062 /* Given a basic set i with a constraint k that is adjacent to
1063 * basic set j, check if we can wrap
1064 * both the facet corresponding to k (if "wrap_facet" is set) and basic map j
1065 * (always) around their ridges to include the other set.
1066 * If so, replace the pair of basic sets by their union.
1068 * All constraints of i (except k) are assumed to be valid or
1069 * cut constraints for j.
1070 * Wrapping the cut constraints to include basic map j may result
1071 * in constraints that are no longer valid of basic map i
1072 * we have to check that the resulting wrapping constraints are valid for i.
1073 * If "wrap_facet" is not set, then all constraints of i (except k)
1074 * are assumed to be valid for j.
1075 * ____ _____
1076 * / | / \
1077 * / || / |
1078 * \ || => \ |
1079 * \ || \ |
1080 * \___|| \____|
1083 static enum isl_change can_wrap_in_facet(int i, int j, int k,
1084 struct isl_coalesce_info *info, int wrap_facet)
1086 enum isl_change change = isl_change_none;
1087 struct isl_wraps wraps;
1088 isl_ctx *ctx;
1089 isl_mat *mat;
1090 struct isl_set *set_i = NULL;
1091 struct isl_set *set_j = NULL;
1092 struct isl_vec *bound = NULL;
1093 unsigned total = isl_basic_map_total_dim(info[i].bmap);
1095 set_i = set_from_updated_bmap(info[i].bmap, info[i].tab);
1096 set_j = set_from_updated_bmap(info[j].bmap, info[j].tab);
1097 ctx = isl_basic_map_get_ctx(info[i].bmap);
1098 mat = isl_mat_alloc(ctx, 2 * (info[i].bmap->n_eq + info[j].bmap->n_eq) +
1099 info[i].bmap->n_ineq + info[j].bmap->n_ineq,
1100 1 + total);
1101 wraps_init(&wraps, mat, info, i, j);
1102 bound = isl_vec_alloc(ctx, 1 + total);
1103 if (!set_i || !set_j || !wraps.mat || !bound)
1104 goto error;
1106 isl_seq_cpy(bound->el, info[i].bmap->ineq[k], 1 + total);
1107 isl_int_add_ui(bound->el[0], bound->el[0], 1);
1109 isl_seq_cpy(wraps.mat->row[0], bound->el, 1 + total);
1110 wraps.mat->n_row = 1;
1112 if (add_wraps(&wraps, &info[j], bound->el, set_i) < 0)
1113 goto error;
1114 if (!wraps.mat->n_row)
1115 goto unbounded;
1117 if (wrap_facet) {
1118 if (add_wraps_around_facet(&wraps, &info[i], k,
1119 bound->el, set_j) < 0)
1120 goto error;
1121 if (!wraps.mat->n_row)
1122 goto unbounded;
1125 change = fuse(i, j, info, wraps.mat, 0, 0);
1127 unbounded:
1128 wraps_free(&wraps);
1130 isl_set_free(set_i);
1131 isl_set_free(set_j);
1133 isl_vec_free(bound);
1135 return change;
1136 error:
1137 wraps_free(&wraps);
1138 isl_vec_free(bound);
1139 isl_set_free(set_i);
1140 isl_set_free(set_j);
1141 return isl_change_error;
1144 /* Given a pair of basic maps i and j such that j sticks out
1145 * of i at n cut constraints, each time by at most one,
1146 * try to compute wrapping constraints and replace the two
1147 * basic maps by a single basic map.
1148 * The other constraints of i are assumed to be valid for j.
1150 * For each cut constraint t(x) >= 0 of i, we add the relaxed version
1151 * t(x) + 1 >= 0, along with wrapping constraints for all constraints
1152 * of basic map j that bound the part of basic map j that sticks out
1153 * of the cut constraint.
1154 * In particular, we first intersect basic map j with t(x) + 1 = 0.
1155 * If the result is empty, then t(x) >= 0 was actually a valid constraint
1156 * (with respect to the integer points), so we add t(x) >= 0 instead.
1157 * Otherwise, we wrap the constraints of basic map j that are not
1158 * redundant in this intersection and that are not already valid
1159 * for basic map i over basic map i.
1160 * Note that it is sufficient to wrap the constraints to include
1161 * basic map i, because we will only wrap the constraints that do
1162 * not include basic map i already. The wrapped constraint will
1163 * therefore be more relaxed compared to the original constraint.
1164 * Since the original constraint is valid for basic map j, so is
1165 * the wrapped constraint.
1167 * If any wrapping fails, i.e., if we cannot wrap to touch
1168 * the union, then we give up.
1169 * Otherwise, the pair of basic maps is replaced by their union.
1171 static enum isl_change wrap_in_facets(int i, int j, int *cuts, int n,
1172 struct isl_coalesce_info *info)
1174 enum isl_change change = isl_change_none;
1175 struct isl_wraps wraps;
1176 isl_ctx *ctx;
1177 isl_mat *mat;
1178 isl_set *set_i = NULL;
1179 unsigned total = isl_basic_map_total_dim(info[i].bmap);
1180 int max_wrap;
1181 int k, w;
1182 struct isl_tab_undo *snap;
1184 if (isl_tab_extend_cons(info[j].tab, 1) < 0)
1185 return isl_change_error;
1187 max_wrap = 1 + 2 * info[j].bmap->n_eq + info[j].bmap->n_ineq;
1188 max_wrap *= n;
1190 set_i = set_from_updated_bmap(info[i].bmap, info[i].tab);
1191 ctx = isl_basic_map_get_ctx(info[i].bmap);
1192 mat = isl_mat_alloc(ctx, max_wrap, 1 + total);
1193 wraps_init(&wraps, mat, info, i, j);
1194 if (!set_i || !wraps.mat)
1195 goto error;
1197 snap = isl_tab_snap(info[j].tab);
1199 wraps.mat->n_row = 0;
1201 for (k = 0; k < n; ++k) {
1202 w = wraps.mat->n_row++;
1203 isl_seq_cpy(wraps.mat->row[w],
1204 info[i].bmap->ineq[cuts[k]], 1 + total);
1205 isl_int_add_ui(wraps.mat->row[w][0], wraps.mat->row[w][0], 1);
1206 if (isl_tab_add_eq(info[j].tab, wraps.mat->row[w]) < 0)
1207 goto error;
1208 if (isl_tab_detect_redundant(info[j].tab) < 0)
1209 goto error;
1211 if (info[j].tab->empty)
1212 isl_int_sub_ui(wraps.mat->row[w][0],
1213 wraps.mat->row[w][0], 1);
1214 else if (add_wraps(&wraps, &info[j],
1215 wraps.mat->row[w], set_i) < 0)
1216 goto error;
1218 if (isl_tab_rollback(info[j].tab, snap) < 0)
1219 goto error;
1221 if (!wraps.mat->n_row)
1222 break;
1225 if (k == n)
1226 change = fuse(i, j, info, wraps.mat, 0, 1);
1228 wraps_free(&wraps);
1229 isl_set_free(set_i);
1231 return change;
1232 error:
1233 wraps_free(&wraps);
1234 isl_set_free(set_i);
1235 return isl_change_error;
1238 /* Given two basic sets i and j such that i has no cut equalities,
1239 * check if relaxing all the cut inequalities of i by one turns
1240 * them into valid constraint for j and check if we can wrap in
1241 * the bits that are sticking out.
1242 * If so, replace the pair by their union.
1244 * We first check if all relaxed cut inequalities of i are valid for j
1245 * and then try to wrap in the intersections of the relaxed cut inequalities
1246 * with j.
1248 * During this wrapping, we consider the points of j that lie at a distance
1249 * of exactly 1 from i. In particular, we ignore the points that lie in
1250 * between this lower-dimensional space and the basic map i.
1251 * We can therefore only apply this to integer maps.
1252 * ____ _____
1253 * / ___|_ / \
1254 * / | | / |
1255 * \ | | => \ |
1256 * \|____| \ |
1257 * \___| \____/
1259 * _____ ______
1260 * | ____|_ | \
1261 * | | | | |
1262 * | | | => | |
1263 * |_| | | |
1264 * |_____| \______|
1266 * _______
1267 * | |
1268 * | |\ |
1269 * | | \ |
1270 * | | \ |
1271 * | | \|
1272 * | | \
1273 * | |_____\
1274 * | |
1275 * |_______|
1277 * Wrapping can fail if the result of wrapping one of the facets
1278 * around its edges does not produce any new facet constraint.
1279 * In particular, this happens when we try to wrap in unbounded sets.
1281 * _______________________________________________________________________
1283 * | ___
1284 * | | |
1285 * |_| |_________________________________________________________________
1286 * |___|
1288 * The following is not an acceptable result of coalescing the above two
1289 * sets as it includes extra integer points.
1290 * _______________________________________________________________________
1292 * |
1293 * |
1295 * \______________________________________________________________________
1297 static enum isl_change can_wrap_in_set(int i, int j,
1298 struct isl_coalesce_info *info)
1300 enum isl_change change = isl_change_none;
1301 int k, m;
1302 int n;
1303 int *cuts = NULL;
1304 isl_ctx *ctx;
1306 if (ISL_F_ISSET(info[i].bmap, ISL_BASIC_MAP_RATIONAL) ||
1307 ISL_F_ISSET(info[j].bmap, ISL_BASIC_MAP_RATIONAL))
1308 return isl_change_none;
1310 n = count(info[i].ineq, info[i].bmap->n_ineq, STATUS_CUT);
1311 if (n == 0)
1312 return isl_change_none;
1314 ctx = isl_basic_map_get_ctx(info[i].bmap);
1315 cuts = isl_alloc_array(ctx, int, n);
1316 if (!cuts)
1317 return isl_change_error;
1319 for (k = 0, m = 0; m < n; ++k) {
1320 enum isl_ineq_type type;
1322 if (info[i].ineq[k] != STATUS_CUT)
1323 continue;
1325 isl_int_add_ui(info[i].bmap->ineq[k][0],
1326 info[i].bmap->ineq[k][0], 1);
1327 type = isl_tab_ineq_type(info[j].tab, info[i].bmap->ineq[k]);
1328 isl_int_sub_ui(info[i].bmap->ineq[k][0],
1329 info[i].bmap->ineq[k][0], 1);
1330 if (type == isl_ineq_error)
1331 goto error;
1332 if (type != isl_ineq_redundant)
1333 break;
1334 cuts[m] = k;
1335 ++m;
1338 if (m == n)
1339 change = wrap_in_facets(i, j, cuts, n, info);
1341 free(cuts);
1343 return change;
1344 error:
1345 free(cuts);
1346 return isl_change_error;
1349 /* Check if either i or j has only cut inequalities that can
1350 * be used to wrap in (a facet of) the other basic set.
1351 * if so, replace the pair by their union.
1353 static enum isl_change check_wrap(int i, int j, struct isl_coalesce_info *info)
1355 enum isl_change change = isl_change_none;
1357 if (!any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_CUT))
1358 change = can_wrap_in_set(i, j, info);
1359 if (change != isl_change_none)
1360 return change;
1362 if (!any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_CUT))
1363 change = can_wrap_in_set(j, i, info);
1364 return change;
1367 /* At least one of the basic maps has an equality that is adjacent
1368 * to inequality. Make sure that only one of the basic maps has
1369 * such an equality and that the other basic map has exactly one
1370 * inequality adjacent to an equality.
1371 * We call the basic map that has the inequality "i" and the basic
1372 * map that has the equality "j".
1373 * If "i" has any "cut" (in)equality, then relaxing the inequality
1374 * by one would not result in a basic map that contains the other
1375 * basic map. However, it may still be possible to wrap in the other
1376 * basic map.
1378 static enum isl_change check_adj_eq(int i, int j,
1379 struct isl_coalesce_info *info)
1381 enum isl_change change = isl_change_none;
1382 int k;
1383 int any_cut;
1385 if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ADJ_INEQ) &&
1386 any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_ADJ_INEQ))
1387 /* ADJ EQ TOO MANY */
1388 return isl_change_none;
1390 if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ADJ_INEQ))
1391 return check_adj_eq(j, i, info);
1393 /* j has an equality adjacent to an inequality in i */
1395 if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_CUT))
1396 return isl_change_none;
1397 any_cut = any(info[i].ineq, info[i].bmap->n_ineq, STATUS_CUT);
1398 if (count(info[i].ineq, info[i].bmap->n_ineq, STATUS_ADJ_EQ) != 1 ||
1399 any(info[j].ineq, info[j].bmap->n_ineq, STATUS_ADJ_EQ) ||
1400 any(info[i].ineq, info[i].bmap->n_ineq, STATUS_ADJ_INEQ) ||
1401 any(info[j].ineq, info[j].bmap->n_ineq, STATUS_ADJ_INEQ))
1402 /* ADJ EQ TOO MANY */
1403 return isl_change_none;
1405 for (k = 0; k < info[i].bmap->n_ineq; ++k)
1406 if (info[i].ineq[k] == STATUS_ADJ_EQ)
1407 break;
1409 if (!any_cut) {
1410 change = is_adj_eq_extension(i, j, k, info);
1411 if (change != isl_change_none)
1412 return change;
1415 change = can_wrap_in_facet(i, j, k, info, any_cut);
1417 return change;
1420 /* The two basic maps lie on adjacent hyperplanes. In particular,
1421 * basic map "i" has an equality that lies parallel to basic map "j".
1422 * Check if we can wrap the facets around the parallel hyperplanes
1423 * to include the other set.
1425 * We perform basically the same operations as can_wrap_in_facet,
1426 * except that we don't need to select a facet of one of the sets.
1428 * \\ \\
1429 * \\ => \\
1430 * \ \|
1432 * If there is more than one equality of "i" adjacent to an equality of "j",
1433 * then the result will satisfy one or more equalities that are a linear
1434 * combination of these equalities. These will be encoded as pairs
1435 * of inequalities in the wrapping constraints and need to be made
1436 * explicit.
1438 static enum isl_change check_eq_adj_eq(int i, int j,
1439 struct isl_coalesce_info *info)
1441 int k;
1442 enum isl_change change = isl_change_none;
1443 int detect_equalities = 0;
1444 struct isl_wraps wraps;
1445 isl_ctx *ctx;
1446 isl_mat *mat;
1447 struct isl_set *set_i = NULL;
1448 struct isl_set *set_j = NULL;
1449 struct isl_vec *bound = NULL;
1450 unsigned total = isl_basic_map_total_dim(info[i].bmap);
1452 if (count(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ADJ_EQ) != 1)
1453 detect_equalities = 1;
1455 for (k = 0; k < 2 * info[i].bmap->n_eq ; ++k)
1456 if (info[i].eq[k] == STATUS_ADJ_EQ)
1457 break;
1459 set_i = set_from_updated_bmap(info[i].bmap, info[i].tab);
1460 set_j = set_from_updated_bmap(info[j].bmap, info[j].tab);
1461 ctx = isl_basic_map_get_ctx(info[i].bmap);
1462 mat = isl_mat_alloc(ctx, 2 * (info[i].bmap->n_eq + info[j].bmap->n_eq) +
1463 info[i].bmap->n_ineq + info[j].bmap->n_ineq,
1464 1 + total);
1465 wraps_init(&wraps, mat, info, i, j);
1466 bound = isl_vec_alloc(ctx, 1 + total);
1467 if (!set_i || !set_j || !wraps.mat || !bound)
1468 goto error;
1470 if (k % 2 == 0)
1471 isl_seq_neg(bound->el, info[i].bmap->eq[k / 2], 1 + total);
1472 else
1473 isl_seq_cpy(bound->el, info[i].bmap->eq[k / 2], 1 + total);
1474 isl_int_add_ui(bound->el[0], bound->el[0], 1);
1476 isl_seq_cpy(wraps.mat->row[0], bound->el, 1 + total);
1477 wraps.mat->n_row = 1;
1479 if (add_wraps(&wraps, &info[j], bound->el, set_i) < 0)
1480 goto error;
1481 if (!wraps.mat->n_row)
1482 goto unbounded;
1484 isl_int_sub_ui(bound->el[0], bound->el[0], 1);
1485 isl_seq_neg(bound->el, bound->el, 1 + total);
1487 isl_seq_cpy(wraps.mat->row[wraps.mat->n_row], bound->el, 1 + total);
1488 wraps.mat->n_row++;
1490 if (add_wraps(&wraps, &info[i], bound->el, set_j) < 0)
1491 goto error;
1492 if (!wraps.mat->n_row)
1493 goto unbounded;
1495 change = fuse(i, j, info, wraps.mat, detect_equalities, 0);
1497 if (0) {
1498 error: change = isl_change_error;
1500 unbounded:
1502 wraps_free(&wraps);
1503 isl_set_free(set_i);
1504 isl_set_free(set_j);
1505 isl_vec_free(bound);
1507 return change;
1510 /* Initialize the "eq" and "ineq" fields of "info".
1512 static void init_status(struct isl_coalesce_info *info)
1514 info->eq = info->ineq = NULL;
1517 /* Set info->eq to the positions of the equalities of info->bmap
1518 * with respect to the basic map represented by "tab".
1519 * If info->eq has already been computed, then do not compute it again.
1521 static void set_eq_status_in(struct isl_coalesce_info *info,
1522 struct isl_tab *tab)
1524 if (info->eq)
1525 return;
1526 info->eq = eq_status_in(info->bmap, tab);
1529 /* Set info->ineq to the positions of the inequalities of info->bmap
1530 * with respect to the basic map represented by "tab".
1531 * If info->ineq has already been computed, then do not compute it again.
1533 static void set_ineq_status_in(struct isl_coalesce_info *info,
1534 struct isl_tab *tab)
1536 if (info->ineq)
1537 return;
1538 info->ineq = ineq_status_in(info->bmap, info->tab, tab);
1541 /* Free the memory allocated by the "eq" and "ineq" fields of "info".
1542 * This function assumes that init_status has been called on "info" first,
1543 * after which the "eq" and "ineq" fields may or may not have been
1544 * assigned a newly allocated array.
1546 static void clear_status(struct isl_coalesce_info *info)
1548 free(info->eq);
1549 free(info->ineq);
1552 /* Check if the union of the given pair of basic maps
1553 * can be represented by a single basic map.
1554 * If so, replace the pair by the single basic map and return
1555 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
1556 * Otherwise, return isl_change_none.
1557 * The two basic maps are assumed to live in the same local space.
1558 * The "eq" and "ineq" fields of info[i] and info[j] are assumed
1559 * to have been initialized by the caller, either to NULL or
1560 * to valid information.
1562 * We first check the effect of each constraint of one basic map
1563 * on the other basic map.
1564 * The constraint may be
1565 * redundant the constraint is redundant in its own
1566 * basic map and should be ignore and removed
1567 * in the end
1568 * valid all (integer) points of the other basic map
1569 * satisfy the constraint
1570 * separate no (integer) point of the other basic map
1571 * satisfies the constraint
1572 * cut some but not all points of the other basic map
1573 * satisfy the constraint
1574 * adj_eq the given constraint is adjacent (on the outside)
1575 * to an equality of the other basic map
1576 * adj_ineq the given constraint is adjacent (on the outside)
1577 * to an inequality of the other basic map
1579 * We consider seven cases in which we can replace the pair by a single
1580 * basic map. We ignore all "redundant" constraints.
1582 * 1. all constraints of one basic map are valid
1583 * => the other basic map is a subset and can be removed
1585 * 2. all constraints of both basic maps are either "valid" or "cut"
1586 * and the facets corresponding to the "cut" constraints
1587 * of one of the basic maps lies entirely inside the other basic map
1588 * => the pair can be replaced by a basic map consisting
1589 * of the valid constraints in both basic maps
1591 * 3. there is a single pair of adjacent inequalities
1592 * (all other constraints are "valid")
1593 * => the pair can be replaced by a basic map consisting
1594 * of the valid constraints in both basic maps
1596 * 4. one basic map has a single adjacent inequality, while the other
1597 * constraints are "valid". The other basic map has some
1598 * "cut" constraints, but replacing the adjacent inequality by
1599 * its opposite and adding the valid constraints of the other
1600 * basic map results in a subset of the other basic map
1601 * => the pair can be replaced by a basic map consisting
1602 * of the valid constraints in both basic maps
1604 * 5. there is a single adjacent pair of an inequality and an equality,
1605 * the other constraints of the basic map containing the inequality are
1606 * "valid". Moreover, if the inequality the basic map is relaxed
1607 * and then turned into an equality, then resulting facet lies
1608 * entirely inside the other basic map
1609 * => the pair can be replaced by the basic map containing
1610 * the inequality, with the inequality relaxed.
1612 * 6. there is a single adjacent pair of an inequality and an equality,
1613 * the other constraints of the basic map containing the inequality are
1614 * "valid". Moreover, the facets corresponding to both
1615 * the inequality and the equality can be wrapped around their
1616 * ridges to include the other basic map
1617 * => the pair can be replaced by a basic map consisting
1618 * of the valid constraints in both basic maps together
1619 * with all wrapping constraints
1621 * 7. one of the basic maps extends beyond the other by at most one.
1622 * Moreover, the facets corresponding to the cut constraints and
1623 * the pieces of the other basic map at offset one from these cut
1624 * constraints can be wrapped around their ridges to include
1625 * the union of the two basic maps
1626 * => the pair can be replaced by a basic map consisting
1627 * of the valid constraints in both basic maps together
1628 * with all wrapping constraints
1630 * 8. the two basic maps live in adjacent hyperplanes. In principle
1631 * such sets can always be combined through wrapping, but we impose
1632 * that there is only one such pair, to avoid overeager coalescing.
1634 * Throughout the computation, we maintain a collection of tableaus
1635 * corresponding to the basic maps. When the basic maps are dropped
1636 * or combined, the tableaus are modified accordingly.
1638 static enum isl_change coalesce_local_pair_reuse(int i, int j,
1639 struct isl_coalesce_info *info)
1641 enum isl_change change = isl_change_none;
1643 set_eq_status_in(&info[i], info[j].tab);
1644 if (info[i].bmap->n_eq && !info[i].eq)
1645 goto error;
1646 if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ERROR))
1647 goto error;
1648 if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_SEPARATE))
1649 goto done;
1651 set_eq_status_in(&info[j], info[i].tab);
1652 if (info[j].bmap->n_eq && !info[j].eq)
1653 goto error;
1654 if (any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_ERROR))
1655 goto error;
1656 if (any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_SEPARATE))
1657 goto done;
1659 set_ineq_status_in(&info[i], info[j].tab);
1660 if (info[i].bmap->n_ineq && !info[i].ineq)
1661 goto error;
1662 if (any(info[i].ineq, info[i].bmap->n_ineq, STATUS_ERROR))
1663 goto error;
1664 if (any(info[i].ineq, info[i].bmap->n_ineq, STATUS_SEPARATE))
1665 goto done;
1667 set_ineq_status_in(&info[j], info[i].tab);
1668 if (info[j].bmap->n_ineq && !info[j].ineq)
1669 goto error;
1670 if (any(info[j].ineq, info[j].bmap->n_ineq, STATUS_ERROR))
1671 goto error;
1672 if (any(info[j].ineq, info[j].bmap->n_ineq, STATUS_SEPARATE))
1673 goto done;
1675 if (all(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_VALID) &&
1676 all(info[i].ineq, info[i].bmap->n_ineq, STATUS_VALID)) {
1677 drop(&info[j]);
1678 change = isl_change_drop_second;
1679 } else if (all(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_VALID) &&
1680 all(info[j].ineq, info[j].bmap->n_ineq, STATUS_VALID)) {
1681 drop(&info[i]);
1682 change = isl_change_drop_first;
1683 } else if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ADJ_EQ)) {
1684 change = check_eq_adj_eq(i, j, info);
1685 } else if (any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_ADJ_EQ)) {
1686 change = check_eq_adj_eq(j, i, info);
1687 } else if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ADJ_INEQ) ||
1688 any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_ADJ_INEQ)) {
1689 change = check_adj_eq(i, j, info);
1690 } else if (any(info[i].ineq, info[i].bmap->n_ineq, STATUS_ADJ_EQ) ||
1691 any(info[j].ineq, info[j].bmap->n_ineq, STATUS_ADJ_EQ)) {
1692 /* Can't happen */
1693 /* BAD ADJ INEQ */
1694 } else if (any(info[i].ineq, info[i].bmap->n_ineq, STATUS_ADJ_INEQ) ||
1695 any(info[j].ineq, info[j].bmap->n_ineq, STATUS_ADJ_INEQ)) {
1696 change = check_adj_ineq(i, j, info);
1697 } else {
1698 if (!any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_CUT) &&
1699 !any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_CUT))
1700 change = check_facets(i, j, info);
1701 if (change == isl_change_none)
1702 change = check_wrap(i, j, info);
1705 done:
1706 clear_status(&info[i]);
1707 clear_status(&info[j]);
1708 return change;
1709 error:
1710 clear_status(&info[i]);
1711 clear_status(&info[j]);
1712 return isl_change_error;
1715 /* Check if the union of the given pair of basic maps
1716 * can be represented by a single basic map.
1717 * If so, replace the pair by the single basic map and return
1718 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
1719 * Otherwise, return isl_change_none.
1720 * The two basic maps are assumed to live in the same local space.
1722 static enum isl_change coalesce_local_pair(int i, int j,
1723 struct isl_coalesce_info *info)
1725 init_status(&info[i]);
1726 init_status(&info[j]);
1727 return coalesce_local_pair_reuse(i, j, info);
1730 /* Shift the integer division at position "div" of the basic map
1731 * represented by "info" by "shift".
1733 * That is, if the integer division has the form
1735 * floor(f(x)/d)
1737 * then replace it by
1739 * floor((f(x) + shift * d)/d) - shift
1741 static int shift_div(struct isl_coalesce_info *info, int div, isl_int shift)
1743 unsigned total;
1745 info->bmap = isl_basic_map_shift_div(info->bmap, div, 0, shift);
1746 if (!info->bmap)
1747 return -1;
1749 total = isl_basic_map_dim(info->bmap, isl_dim_all);
1750 total -= isl_basic_map_dim(info->bmap, isl_dim_div);
1751 if (isl_tab_shift_var(info->tab, total + div, shift) < 0)
1752 return -1;
1754 return 0;
1757 /* Check if some of the divs in the basic map represented by "info1"
1758 * are shifts of the corresponding divs in the basic map represented
1759 * by "info2". If so, align them with those of "info2".
1760 * Only do this if "info1" and "info2" have the same number
1761 * of integer divisions.
1763 * An integer division is considered to be a shift of another integer
1764 * division if one is equal to the other plus a constant.
1766 * In particular, for each pair of integer divisions, if both are known,
1767 * have identical coefficients (apart from the constant term) and
1768 * if the difference between the constant terms (taking into account
1769 * the denominator) is an integer, then move the difference outside.
1770 * That is, if one integer division is of the form
1772 * floor((f(x) + c_1)/d)
1774 * while the other is of the form
1776 * floor((f(x) + c_2)/d)
1778 * and n = (c_2 - c_1)/d is an integer, then replace the first
1779 * integer division by
1781 * floor((f(x) + c_1 + n * d)/d) - n = floor((f(x) + c_2)/d) - n
1783 static int harmonize_divs(struct isl_coalesce_info *info1,
1784 struct isl_coalesce_info *info2)
1786 int i;
1787 int total;
1789 if (!info1->bmap || !info2->bmap)
1790 return -1;
1792 if (info1->bmap->n_div != info2->bmap->n_div)
1793 return 0;
1794 if (info1->bmap->n_div == 0)
1795 return 0;
1797 total = isl_basic_map_total_dim(info1->bmap);
1798 for (i = 0; i < info1->bmap->n_div; ++i) {
1799 isl_int d;
1800 int r = 0;
1802 if (isl_int_is_zero(info1->bmap->div[i][0]) ||
1803 isl_int_is_zero(info2->bmap->div[i][0]))
1804 continue;
1805 if (isl_int_ne(info1->bmap->div[i][0], info2->bmap->div[i][0]))
1806 continue;
1807 if (isl_int_eq(info1->bmap->div[i][1], info2->bmap->div[i][1]))
1808 continue;
1809 if (!isl_seq_eq(info1->bmap->div[i] + 2,
1810 info2->bmap->div[i] + 2, total))
1811 continue;
1812 isl_int_init(d);
1813 isl_int_sub(d, info2->bmap->div[i][1], info1->bmap->div[i][1]);
1814 if (isl_int_is_divisible_by(d, info1->bmap->div[i][0])) {
1815 isl_int_divexact(d, d, info1->bmap->div[i][0]);
1816 r = shift_div(info1, i, d);
1818 isl_int_clear(d);
1819 if (r < 0)
1820 return -1;
1823 return 0;
1826 /* Do the two basic maps live in the same local space, i.e.,
1827 * do they have the same (known) divs?
1828 * If either basic map has any unknown divs, then we can only assume
1829 * that they do not live in the same local space.
1831 static int same_divs(__isl_keep isl_basic_map *bmap1,
1832 __isl_keep isl_basic_map *bmap2)
1834 int i;
1835 int known;
1836 int total;
1838 if (!bmap1 || !bmap2)
1839 return -1;
1840 if (bmap1->n_div != bmap2->n_div)
1841 return 0;
1843 if (bmap1->n_div == 0)
1844 return 1;
1846 known = isl_basic_map_divs_known(bmap1);
1847 if (known < 0 || !known)
1848 return known;
1849 known = isl_basic_map_divs_known(bmap2);
1850 if (known < 0 || !known)
1851 return known;
1853 total = isl_basic_map_total_dim(bmap1);
1854 for (i = 0; i < bmap1->n_div; ++i)
1855 if (!isl_seq_eq(bmap1->div[i], bmap2->div[i], 2 + total))
1856 return 0;
1858 return 1;
1861 /* Check if the union of "bmap" and the basic map represented by info[j]
1862 * can be represented by a single basic map,
1863 * after expanding the divs of "bmap" to match those of info[j].
1864 * If so, replace the pair by the single basic map and return
1865 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
1866 * Otherwise, return isl_change_none.
1868 * In particular, check if the expanded "bmap" contains the basic map
1869 * represented by the tableau info[j].tab. That is, this function
1870 * can only return isl_change_drop_second or isl_change_none.
1871 * The expansion is performed using the divs "div" and expansion "exp"
1872 * computed by the caller.
1873 * Then we check if all constraints of the expanded "bmap" are valid for
1874 * info[j].tab.
1876 * If "i" is not equal to -1, then "bmap" is equal to info[i].bmap.
1877 * In this case, the positions of the constraints of info[i].bmap
1878 * with respect to the basic map represented by info[j] are stored
1879 * in info[i].
1881 static enum isl_change coalesce_with_expanded_divs(
1882 __isl_keep isl_basic_map *bmap, int i, int j,
1883 struct isl_coalesce_info *info, __isl_keep isl_mat *div, int *exp)
1885 enum isl_change change = isl_change_none;
1886 struct isl_coalesce_info info_local, *info_i;
1888 info_i = i >= 0 ? &info[i] : &info_local;
1889 init_status(info_i);
1890 bmap = isl_basic_map_copy(bmap);
1891 bmap = isl_basic_map_expand_divs(bmap, isl_mat_copy(div), exp);
1893 if (!bmap)
1894 goto error;
1896 info_i->eq = eq_status_in(bmap, info[j].tab);
1897 if (bmap->n_eq && !info_i->eq)
1898 goto error;
1899 if (any(info_i->eq, 2 * bmap->n_eq, STATUS_ERROR))
1900 goto error;
1901 if (any(info_i->eq, 2 * bmap->n_eq, STATUS_SEPARATE))
1902 goto done;
1904 info_i->ineq = ineq_status_in(bmap, NULL, info[j].tab);
1905 if (bmap->n_ineq && !info_i->ineq)
1906 goto error;
1907 if (any(info_i->ineq, bmap->n_ineq, STATUS_ERROR))
1908 goto error;
1909 if (any(info_i->ineq, bmap->n_ineq, STATUS_SEPARATE))
1910 goto done;
1912 if (all(info_i->eq, 2 * bmap->n_eq, STATUS_VALID) &&
1913 all(info_i->ineq, bmap->n_ineq, STATUS_VALID)) {
1914 drop(&info[j]);
1915 change = isl_change_drop_second;
1918 done:
1919 isl_basic_map_free(bmap);
1920 clear_status(info_i);
1921 return change;
1922 error:
1923 isl_basic_map_free(bmap);
1924 clear_status(info_i);
1925 return isl_change_error;
1928 /* Check if the union of "bmap_i" and the basic map represented by info[j]
1929 * can be represented by a single basic map,
1930 * after aligning the divs of "bmap_i" to match those of info[j].
1931 * If so, replace the pair by the single basic map and return
1932 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
1933 * Otherwise, return isl_change_none.
1935 * In particular, check if "bmap_i" contains the basic map represented by
1936 * info[j] after aligning the divs of "bmap_i" to those of info[j].
1937 * Note that this can only succeed if the number of divs of "bmap_i"
1938 * is smaller than (or equal to) the number of divs of info[j].
1940 * We first check if the divs of "bmap_i" are all known and form a subset
1941 * of those of info[j].bmap. If so, we pass control over to
1942 * coalesce_with_expanded_divs.
1944 * If "i" is not equal to -1, then "bmap" is equal to info[i].bmap.
1946 static enum isl_change coalesce_after_aligning_divs(
1947 __isl_keep isl_basic_map *bmap_i, int i, int j,
1948 struct isl_coalesce_info *info)
1950 int known;
1951 isl_mat *div_i, *div_j, *div;
1952 int *exp1 = NULL;
1953 int *exp2 = NULL;
1954 isl_ctx *ctx;
1955 enum isl_change change;
1957 known = isl_basic_map_divs_known(bmap_i);
1958 if (known < 0 || !known)
1959 return known;
1961 ctx = isl_basic_map_get_ctx(bmap_i);
1963 div_i = isl_basic_map_get_divs(bmap_i);
1964 div_j = isl_basic_map_get_divs(info[j].bmap);
1966 if (!div_i || !div_j)
1967 goto error;
1969 exp1 = isl_alloc_array(ctx, int, div_i->n_row);
1970 exp2 = isl_alloc_array(ctx, int, div_j->n_row);
1971 if ((div_i->n_row && !exp1) || (div_j->n_row && !exp2))
1972 goto error;
1974 div = isl_merge_divs(div_i, div_j, exp1, exp2);
1975 if (!div)
1976 goto error;
1978 if (div->n_row == div_j->n_row)
1979 change = coalesce_with_expanded_divs(bmap_i,
1980 i, j, info, div, exp1);
1981 else
1982 change = isl_change_none;
1984 isl_mat_free(div);
1986 isl_mat_free(div_i);
1987 isl_mat_free(div_j);
1989 free(exp2);
1990 free(exp1);
1992 return change;
1993 error:
1994 isl_mat_free(div_i);
1995 isl_mat_free(div_j);
1996 free(exp1);
1997 free(exp2);
1998 return isl_change_error;
2001 /* Check if basic map "j" is a subset of basic map "i" after
2002 * exploiting the extra equalities of "j" to simplify the divs of "i".
2003 * If so, remove basic map "j" and return isl_change_drop_second.
2005 * If "j" does not have any equalities or if they are the same
2006 * as those of "i", then we cannot exploit them to simplify the divs.
2007 * Similarly, if there are no divs in "i", then they cannot be simplified.
2008 * If, on the other hand, the affine hulls of "i" and "j" do not intersect,
2009 * then "j" cannot be a subset of "i".
2011 * Otherwise, we intersect "i" with the affine hull of "j" and then
2012 * check if "j" is a subset of the result after aligning the divs.
2013 * If so, then "j" is definitely a subset of "i" and can be removed.
2014 * Note that if after intersection with the affine hull of "j".
2015 * "i" still has more divs than "j", then there is no way we can
2016 * align the divs of "i" to those of "j".
2018 static enum isl_change coalesce_subset_with_equalities(int i, int j,
2019 struct isl_coalesce_info *info)
2021 isl_basic_map *hull_i, *hull_j, *bmap_i;
2022 int equal, empty;
2023 enum isl_change change;
2025 if (info[j].bmap->n_eq == 0)
2026 return isl_change_none;
2027 if (info[i].bmap->n_div == 0)
2028 return isl_change_none;
2030 hull_i = isl_basic_map_copy(info[i].bmap);
2031 hull_i = isl_basic_map_plain_affine_hull(hull_i);
2032 hull_j = isl_basic_map_copy(info[j].bmap);
2033 hull_j = isl_basic_map_plain_affine_hull(hull_j);
2035 hull_j = isl_basic_map_intersect(hull_j, isl_basic_map_copy(hull_i));
2036 equal = isl_basic_map_plain_is_equal(hull_i, hull_j);
2037 empty = isl_basic_map_plain_is_empty(hull_j);
2038 isl_basic_map_free(hull_i);
2040 if (equal < 0 || equal || empty < 0 || empty) {
2041 isl_basic_map_free(hull_j);
2042 if (equal < 0 || empty < 0)
2043 return isl_change_error;
2044 return isl_change_none;
2047 bmap_i = isl_basic_map_copy(info[i].bmap);
2048 bmap_i = isl_basic_map_intersect(bmap_i, hull_j);
2049 if (!bmap_i)
2050 return isl_change_error;
2052 if (bmap_i->n_div > info[j].bmap->n_div) {
2053 isl_basic_map_free(bmap_i);
2054 return isl_change_none;
2057 change = coalesce_after_aligning_divs(bmap_i, -1, j, info);
2059 isl_basic_map_free(bmap_i);
2061 return change;
2064 /* Check if the union of and the basic maps represented by info[i] and info[j]
2065 * can be represented by a single basic map, by aligning or equating
2066 * their integer divisions.
2067 * If so, replace the pair by the single basic map and return
2068 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
2069 * Otherwise, return isl_change_none.
2071 * Note that we only perform any test if the number of divs is different
2072 * in the two basic maps. In case the number of divs is the same,
2073 * we have already established that the divs are different
2074 * in the two basic maps.
2075 * In particular, if the number of divs of basic map i is smaller than
2076 * the number of divs of basic map j, then we check if j is a subset of i
2077 * and vice versa.
2079 static enum isl_change coalesce_divs(int i, int j,
2080 struct isl_coalesce_info *info)
2082 enum isl_change change = isl_change_none;
2084 if (info[i].bmap->n_div < info[j].bmap->n_div)
2085 change = coalesce_after_aligning_divs(info[i].bmap, i, j, info);
2086 if (change != isl_change_none)
2087 return change;
2089 if (info[j].bmap->n_div < info[i].bmap->n_div)
2090 change = coalesce_after_aligning_divs(info[j].bmap, j, i, info);
2091 if (change != isl_change_none)
2092 return invert_change(change);
2094 change = coalesce_subset_with_equalities(i, j, info);
2095 if (change != isl_change_none)
2096 return change;
2098 change = coalesce_subset_with_equalities(j, i, info);
2099 if (change != isl_change_none)
2100 return invert_change(change);
2102 return isl_change_none;
2105 /* Does "bmap" involve any divs that themselves refer to divs?
2107 static int has_nested_div(__isl_keep isl_basic_map *bmap)
2109 int i;
2110 unsigned total;
2111 unsigned n_div;
2113 total = isl_basic_map_dim(bmap, isl_dim_all);
2114 n_div = isl_basic_map_dim(bmap, isl_dim_div);
2115 total -= n_div;
2117 for (i = 0; i < n_div; ++i)
2118 if (isl_seq_first_non_zero(bmap->div[i] + 2 + total,
2119 n_div) != -1)
2120 return 1;
2122 return 0;
2125 /* Return a list of affine expressions, one for each integer division
2126 * in "bmap_i". For each integer division that also appears in "bmap_j",
2127 * the affine expression is set to NaN. The number of NaNs in the list
2128 * is equal to the number of integer divisions in "bmap_j".
2129 * For the other integer divisions of "bmap_i", the corresponding
2130 * element in the list is a purely affine expression equal to the integer
2131 * division in "hull".
2132 * If no such list can be constructed, then the number of elements
2133 * in the returned list is smaller than the number of integer divisions
2134 * in "bmap_i".
2136 static __isl_give isl_aff_list *set_up_substitutions(
2137 __isl_keep isl_basic_map *bmap_i, __isl_keep isl_basic_map *bmap_j,
2138 __isl_take isl_basic_map *hull)
2140 unsigned n_div_i, n_div_j, total;
2141 isl_ctx *ctx;
2142 isl_local_space *ls;
2143 isl_basic_set *wrap_hull;
2144 isl_aff *aff_nan;
2145 isl_aff_list *list;
2146 int i, j;
2148 if (!hull)
2149 return NULL;
2151 ctx = isl_basic_map_get_ctx(hull);
2153 n_div_i = isl_basic_map_dim(bmap_i, isl_dim_div);
2154 n_div_j = isl_basic_map_dim(bmap_j, isl_dim_div);
2155 total = isl_basic_map_total_dim(bmap_i) - n_div_i;
2157 ls = isl_basic_map_get_local_space(bmap_i);
2158 ls = isl_local_space_wrap(ls);
2159 wrap_hull = isl_basic_map_wrap(hull);
2161 aff_nan = isl_aff_nan_on_domain(isl_local_space_copy(ls));
2162 list = isl_aff_list_alloc(ctx, n_div_i);
2164 j = 0;
2165 for (i = 0; i < n_div_i; ++i) {
2166 isl_aff *aff;
2168 if (j < n_div_j &&
2169 isl_seq_eq(bmap_i->div[i], bmap_j->div[j], 2 + total)) {
2170 ++j;
2171 list = isl_aff_list_add(list, isl_aff_copy(aff_nan));
2172 continue;
2174 if (n_div_i - i <= n_div_j - j)
2175 break;
2177 aff = isl_local_space_get_div(ls, i);
2178 aff = isl_aff_substitute_equalities(aff,
2179 isl_basic_set_copy(wrap_hull));
2180 aff = isl_aff_floor(aff);
2181 if (!aff)
2182 goto error;
2183 if (isl_aff_dim(aff, isl_dim_div) != 0) {
2184 isl_aff_free(aff);
2185 break;
2188 list = isl_aff_list_add(list, aff);
2191 isl_aff_free(aff_nan);
2192 isl_local_space_free(ls);
2193 isl_basic_set_free(wrap_hull);
2195 return list;
2196 error:
2197 isl_aff_free(aff_nan);
2198 isl_local_space_free(ls);
2199 isl_basic_set_free(wrap_hull);
2200 isl_aff_list_free(list);
2201 return NULL;
2204 /* Add variables to info->bmap and info->tab corresponding to the elements
2205 * in "list" that are not set to NaN.
2206 * "extra_var" is the number of these elements.
2207 * "dim" is the offset in the variables of "tab" where we should
2208 * start considering the elements in "list".
2209 * When this function returns, the total number of variables in "tab"
2210 * is equal to "dim" plus the number of elements in "list".
2212 * The newly added existentially quantified variables are not given
2213 * an explicit representation because the corresponding div constraints
2214 * do not appear in info->bmap. These constraints are not added
2215 * to info->bmap because for internal consistency, they would need to
2216 * be added to info->tab as well, where they could combine with the equality
2217 * that is added later to result in constraints that do not hold
2218 * in the original input.
2220 static int add_sub_vars(struct isl_coalesce_info *info,
2221 __isl_keep isl_aff_list *list, int dim, int extra_var)
2223 int i, j, n, d;
2224 isl_space *space;
2226 space = isl_basic_map_get_space(info->bmap);
2227 info->bmap = isl_basic_map_cow(info->bmap);
2228 info->bmap = isl_basic_map_extend_space(info->bmap, space,
2229 extra_var, 0, 0);
2230 if (!info->bmap)
2231 return -1;
2232 n = isl_aff_list_n_aff(list);
2233 for (i = 0; i < n; ++i) {
2234 int is_nan;
2235 isl_aff *aff;
2237 aff = isl_aff_list_get_aff(list, i);
2238 is_nan = isl_aff_is_nan(aff);
2239 isl_aff_free(aff);
2240 if (is_nan < 0)
2241 return -1;
2242 if (is_nan)
2243 continue;
2245 if (isl_tab_insert_var(info->tab, dim + i) < 0)
2246 return -1;
2247 d = isl_basic_map_alloc_div(info->bmap);
2248 if (d < 0)
2249 return -1;
2250 info->bmap = isl_basic_map_mark_div_unknown(info->bmap, d);
2251 if (!info->bmap)
2252 return -1;
2253 for (j = d; j > i; --j)
2254 isl_basic_map_swap_div(info->bmap, j - 1, j);
2257 return 0;
2260 /* For each element in "list" that is not set to NaN, fix the corresponding
2261 * variable in "tab" to the purely affine expression defined by the element.
2262 * "dim" is the offset in the variables of "tab" where we should
2263 * start considering the elements in "list".
2265 * This function assumes that a sufficient number of rows and
2266 * elements in the constraint array are available in the tableau.
2268 static int add_sub_equalities(struct isl_tab *tab,
2269 __isl_keep isl_aff_list *list, int dim)
2271 int i, n;
2272 isl_ctx *ctx;
2273 isl_vec *sub;
2274 isl_aff *aff;
2276 n = isl_aff_list_n_aff(list);
2278 ctx = isl_tab_get_ctx(tab);
2279 sub = isl_vec_alloc(ctx, 1 + dim + n);
2280 if (!sub)
2281 return -1;
2282 isl_seq_clr(sub->el + 1 + dim, n);
2284 for (i = 0; i < n; ++i) {
2285 aff = isl_aff_list_get_aff(list, i);
2286 if (!aff)
2287 goto error;
2288 if (isl_aff_is_nan(aff)) {
2289 isl_aff_free(aff);
2290 continue;
2292 isl_seq_cpy(sub->el, aff->v->el + 1, 1 + dim);
2293 isl_int_neg(sub->el[1 + dim + i], aff->v->el[0]);
2294 if (isl_tab_add_eq(tab, sub->el) < 0)
2295 goto error;
2296 isl_int_set_si(sub->el[1 + dim + i], 0);
2297 isl_aff_free(aff);
2300 isl_vec_free(sub);
2301 return 0;
2302 error:
2303 isl_aff_free(aff);
2304 isl_vec_free(sub);
2305 return -1;
2308 /* Add variables to info->tab and info->bmap corresponding to the elements
2309 * in "list" that are not set to NaN. The value of the added variable
2310 * in info->tab is fixed to the purely affine expression defined by the element.
2311 * "dim" is the offset in the variables of info->tab where we should
2312 * start considering the elements in "list".
2313 * When this function returns, the total number of variables in info->tab
2314 * is equal to "dim" plus the number of elements in "list".
2316 static int add_subs(struct isl_coalesce_info *info,
2317 __isl_keep isl_aff_list *list, int dim)
2319 int extra_var;
2320 int n;
2322 if (!list)
2323 return -1;
2325 n = isl_aff_list_n_aff(list);
2326 extra_var = n - (info->tab->n_var - dim);
2328 if (isl_tab_extend_vars(info->tab, extra_var) < 0)
2329 return -1;
2330 if (isl_tab_extend_cons(info->tab, 2 * extra_var) < 0)
2331 return -1;
2332 if (add_sub_vars(info, list, dim, extra_var) < 0)
2333 return -1;
2335 return add_sub_equalities(info->tab, list, dim);
2338 /* Coalesce basic map "j" into basic map "i" after adding the extra integer
2339 * divisions in "i" but not in "j" to basic map "j", with values
2340 * specified by "list". The total number of elements in "list"
2341 * is equal to the number of integer divisions in "i", while the number
2342 * of NaN elements in the list is equal to the number of integer divisions
2343 * in "j".
2345 * If no coalescing can be performed, then we need to revert basic map "j"
2346 * to its original state. We do the same if basic map "i" gets dropped
2347 * during the coalescing, even though this should not happen in practice
2348 * since we have already checked for "j" being a subset of "i"
2349 * before we reach this stage.
2351 static enum isl_change coalesce_with_subs(int i, int j,
2352 struct isl_coalesce_info *info, __isl_keep isl_aff_list *list)
2354 isl_basic_map *bmap_j;
2355 struct isl_tab_undo *snap;
2356 unsigned dim;
2357 enum isl_change change;
2359 bmap_j = isl_basic_map_copy(info[j].bmap);
2360 snap = isl_tab_snap(info[j].tab);
2362 dim = isl_basic_map_dim(bmap_j, isl_dim_all);
2363 dim -= isl_basic_map_dim(bmap_j, isl_dim_div);
2364 if (add_subs(&info[j], list, dim) < 0)
2365 goto error;
2367 change = coalesce_local_pair(i, j, info);
2368 if (change != isl_change_none && change != isl_change_drop_first) {
2369 isl_basic_map_free(bmap_j);
2370 } else {
2371 isl_basic_map_free(info[j].bmap);
2372 info[j].bmap = bmap_j;
2374 if (isl_tab_rollback(info[j].tab, snap) < 0)
2375 return isl_change_error;
2378 return change;
2379 error:
2380 isl_basic_map_free(bmap_j);
2381 return isl_change_error;
2384 /* Check if we can coalesce basic map "j" into basic map "i" after copying
2385 * those extra integer divisions in "i" that can be simplified away
2386 * using the extra equalities in "j".
2387 * All divs are assumed to be known and not contain any nested divs.
2389 * We first check if there are any extra equalities in "j" that we
2390 * can exploit. Then we check if every integer division in "i"
2391 * either already appears in "j" or can be simplified using the
2392 * extra equalities to a purely affine expression.
2393 * If these tests succeed, then we try to coalesce the two basic maps
2394 * by introducing extra dimensions in "j" corresponding to
2395 * the extra integer divsisions "i" fixed to the corresponding
2396 * purely affine expression.
2398 static enum isl_change check_coalesce_into_eq(int i, int j,
2399 struct isl_coalesce_info *info)
2401 unsigned n_div_i, n_div_j;
2402 isl_basic_map *hull_i, *hull_j;
2403 int equal, empty;
2404 isl_aff_list *list;
2405 enum isl_change change;
2407 n_div_i = isl_basic_map_dim(info[i].bmap, isl_dim_div);
2408 n_div_j = isl_basic_map_dim(info[j].bmap, isl_dim_div);
2409 if (n_div_i <= n_div_j)
2410 return isl_change_none;
2411 if (info[j].bmap->n_eq == 0)
2412 return isl_change_none;
2414 hull_i = isl_basic_map_copy(info[i].bmap);
2415 hull_i = isl_basic_map_plain_affine_hull(hull_i);
2416 hull_j = isl_basic_map_copy(info[j].bmap);
2417 hull_j = isl_basic_map_plain_affine_hull(hull_j);
2419 hull_j = isl_basic_map_intersect(hull_j, isl_basic_map_copy(hull_i));
2420 equal = isl_basic_map_plain_is_equal(hull_i, hull_j);
2421 empty = isl_basic_map_plain_is_empty(hull_j);
2422 isl_basic_map_free(hull_i);
2424 if (equal < 0 || empty < 0)
2425 goto error;
2426 if (equal || empty) {
2427 isl_basic_map_free(hull_j);
2428 return isl_change_none;
2431 list = set_up_substitutions(info[i].bmap, info[j].bmap, hull_j);
2432 if (!list)
2433 return isl_change_error;
2434 if (isl_aff_list_n_aff(list) < n_div_i)
2435 change = isl_change_none;
2436 else
2437 change = coalesce_with_subs(i, j, info, list);
2439 isl_aff_list_free(list);
2441 return change;
2442 error:
2443 isl_basic_map_free(hull_j);
2444 return isl_change_error;
2447 /* Check if we can coalesce basic maps "i" and "j" after copying
2448 * those extra integer divisions in one of the basic maps that can
2449 * be simplified away using the extra equalities in the other basic map.
2450 * We require all divs to be known in both basic maps.
2451 * Furthermore, to simplify the comparison of div expressions,
2452 * we do not allow any nested integer divisions.
2454 static enum isl_change check_coalesce_eq(int i, int j,
2455 struct isl_coalesce_info *info)
2457 int known, nested;
2458 enum isl_change change;
2460 known = isl_basic_map_divs_known(info[i].bmap);
2461 if (known < 0 || !known)
2462 return known < 0 ? isl_change_error : isl_change_none;
2463 known = isl_basic_map_divs_known(info[j].bmap);
2464 if (known < 0 || !known)
2465 return known < 0 ? isl_change_error : isl_change_none;
2466 nested = has_nested_div(info[i].bmap);
2467 if (nested < 0 || nested)
2468 return nested < 0 ? isl_change_error : isl_change_none;
2469 nested = has_nested_div(info[j].bmap);
2470 if (nested < 0 || nested)
2471 return nested < 0 ? isl_change_error : isl_change_none;
2473 change = check_coalesce_into_eq(i, j, info);
2474 if (change != isl_change_none)
2475 return change;
2476 change = check_coalesce_into_eq(j, i, info);
2477 if (change != isl_change_none)
2478 return invert_change(change);
2480 return isl_change_none;
2483 /* Check if the union of the given pair of basic maps
2484 * can be represented by a single basic map.
2485 * If so, replace the pair by the single basic map and return
2486 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
2487 * Otherwise, return isl_change_none.
2489 * We first check if the two basic maps live in the same local space,
2490 * after aligning the divs that differ by only an integer constant.
2491 * If so, we do the complete check. Otherwise, we check if they have
2492 * the same number of integer divisions and can be coalesced, if one is
2493 * an obvious subset of the other or if the extra integer divisions
2494 * of one basic map can be simplified away using the extra equalities
2495 * of the other basic map.
2497 static enum isl_change coalesce_pair(int i, int j,
2498 struct isl_coalesce_info *info)
2500 int same;
2501 enum isl_change change;
2503 if (harmonize_divs(&info[i], &info[j]) < 0)
2504 return isl_change_error;
2505 same = same_divs(info[i].bmap, info[j].bmap);
2506 if (same < 0)
2507 return isl_change_error;
2508 if (same)
2509 return coalesce_local_pair(i, j, info);
2511 if (info[i].bmap->n_div == info[j].bmap->n_div) {
2512 change = coalesce_local_pair(i, j, info);
2513 if (change != isl_change_none)
2514 return change;
2517 change = coalesce_divs(i, j, info);
2518 if (change != isl_change_none)
2519 return change;
2521 return check_coalesce_eq(i, j, info);
2524 /* Return the maximum of "a" and "b".
2526 static int isl_max(int a, int b)
2528 return a > b ? a : b;
2531 /* Pairwise coalesce the basic maps in the range [start1, end1[ of "info"
2532 * with those in the range [start2, end2[, skipping basic maps
2533 * that have been removed (either before or within this function).
2535 * For each basic map i in the first range, we check if it can be coalesced
2536 * with respect to any previously considered basic map j in the second range.
2537 * If i gets dropped (because it was a subset of some j), then
2538 * we can move on to the next basic map.
2539 * If j gets dropped, we need to continue checking against the other
2540 * previously considered basic maps.
2541 * If the two basic maps got fused, then we recheck the fused basic map
2542 * against the previously considered basic maps, starting at i + 1
2543 * (even if start2 is greater than i + 1).
2545 static int coalesce_range(isl_ctx *ctx, struct isl_coalesce_info *info,
2546 int start1, int end1, int start2, int end2)
2548 int i, j;
2550 for (i = end1 - 1; i >= start1; --i) {
2551 if (info[i].removed)
2552 continue;
2553 for (j = isl_max(i + 1, start2); j < end2; ++j) {
2554 enum isl_change changed;
2556 if (info[j].removed)
2557 continue;
2558 if (info[i].removed)
2559 isl_die(ctx, isl_error_internal,
2560 "basic map unexpectedly removed",
2561 return -1);
2562 changed = coalesce_pair(i, j, info);
2563 switch (changed) {
2564 case isl_change_error:
2565 return -1;
2566 case isl_change_none:
2567 case isl_change_drop_second:
2568 continue;
2569 case isl_change_drop_first:
2570 j = end2;
2571 break;
2572 case isl_change_fuse:
2573 j = i;
2574 break;
2579 return 0;
2582 /* Pairwise coalesce the basic maps described by the "n" elements of "info".
2584 * We consider groups of basic maps that live in the same apparent
2585 * affine hull and we first coalesce within such a group before we
2586 * coalesce the elements in the group with elements of previously
2587 * considered groups. If a fuse happens during the second phase,
2588 * then we also reconsider the elements within the group.
2590 static int coalesce(isl_ctx *ctx, int n, struct isl_coalesce_info *info)
2592 int start, end;
2594 for (end = n; end > 0; end = start) {
2595 start = end - 1;
2596 while (start >= 1 &&
2597 info[start - 1].hull_hash == info[start].hull_hash)
2598 start--;
2599 if (coalesce_range(ctx, info, start, end, start, end) < 0)
2600 return -1;
2601 if (coalesce_range(ctx, info, start, end, end, n) < 0)
2602 return -1;
2605 return 0;
2608 /* Update the basic maps in "map" based on the information in "info".
2609 * In particular, remove the basic maps that have been marked removed and
2610 * update the others based on the information in the corresponding tableau.
2611 * Since we detected implicit equalities without calling
2612 * isl_basic_map_gauss, we need to do it now.
2613 * Also call isl_basic_map_simplify if we may have lost the definition
2614 * of one or more integer divisions.
2616 static __isl_give isl_map *update_basic_maps(__isl_take isl_map *map,
2617 int n, struct isl_coalesce_info *info)
2619 int i;
2621 if (!map)
2622 return NULL;
2624 for (i = n - 1; i >= 0; --i) {
2625 if (info[i].removed) {
2626 isl_basic_map_free(map->p[i]);
2627 if (i != map->n - 1)
2628 map->p[i] = map->p[map->n - 1];
2629 map->n--;
2630 continue;
2633 info[i].bmap = isl_basic_map_update_from_tab(info[i].bmap,
2634 info[i].tab);
2635 info[i].bmap = isl_basic_map_gauss(info[i].bmap, NULL);
2636 if (info[i].simplify)
2637 info[i].bmap = isl_basic_map_simplify(info[i].bmap);
2638 info[i].bmap = isl_basic_map_finalize(info[i].bmap);
2639 if (!info[i].bmap)
2640 return isl_map_free(map);
2641 ISL_F_SET(info[i].bmap, ISL_BASIC_MAP_NO_IMPLICIT);
2642 ISL_F_SET(info[i].bmap, ISL_BASIC_MAP_NO_REDUNDANT);
2643 isl_basic_map_free(map->p[i]);
2644 map->p[i] = info[i].bmap;
2645 info[i].bmap = NULL;
2648 return map;
2651 /* For each pair of basic maps in the map, check if the union of the two
2652 * can be represented by a single basic map.
2653 * If so, replace the pair by the single basic map and start over.
2655 * We factor out any (hidden) common factor from the constraint
2656 * coefficients to improve the detection of adjacent constraints.
2658 * Since we are constructing the tableaus of the basic maps anyway,
2659 * we exploit them to detect implicit equalities and redundant constraints.
2660 * This also helps the coalescing as it can ignore the redundant constraints.
2661 * In order to avoid confusion, we make all implicit equalities explicit
2662 * in the basic maps. We don't call isl_basic_map_gauss, though,
2663 * as that may affect the number of constraints.
2664 * This means that we have to call isl_basic_map_gauss at the end
2665 * of the computation (in update_basic_maps) to ensure that
2666 * the basic maps are not left in an unexpected state.
2667 * For each basic map, we also compute the hash of the apparent affine hull
2668 * for use in coalesce.
2670 struct isl_map *isl_map_coalesce(struct isl_map *map)
2672 int i;
2673 unsigned n;
2674 isl_ctx *ctx;
2675 struct isl_coalesce_info *info = NULL;
2677 map = isl_map_remove_empty_parts(map);
2678 if (!map)
2679 return NULL;
2681 if (map->n <= 1)
2682 return map;
2684 ctx = isl_map_get_ctx(map);
2685 map = isl_map_sort_divs(map);
2686 map = isl_map_cow(map);
2688 if (!map)
2689 return NULL;
2691 n = map->n;
2693 info = isl_calloc_array(map->ctx, struct isl_coalesce_info, n);
2694 if (!info)
2695 goto error;
2697 for (i = 0; i < map->n; ++i) {
2698 map->p[i] = isl_basic_map_reduce_coefficients(map->p[i]);
2699 if (!map->p[i])
2700 goto error;
2701 info[i].bmap = isl_basic_map_copy(map->p[i]);
2702 info[i].tab = isl_tab_from_basic_map(info[i].bmap, 0);
2703 if (!info[i].tab)
2704 goto error;
2705 if (!ISL_F_ISSET(info[i].bmap, ISL_BASIC_MAP_NO_IMPLICIT))
2706 if (isl_tab_detect_implicit_equalities(info[i].tab) < 0)
2707 goto error;
2708 info[i].bmap = isl_tab_make_equalities_explicit(info[i].tab,
2709 info[i].bmap);
2710 if (!info[i].bmap)
2711 goto error;
2712 if (!ISL_F_ISSET(info[i].bmap, ISL_BASIC_MAP_NO_REDUNDANT))
2713 if (isl_tab_detect_redundant(info[i].tab) < 0)
2714 goto error;
2715 if (coalesce_info_set_hull_hash(&info[i]) < 0)
2716 goto error;
2718 for (i = map->n - 1; i >= 0; --i)
2719 if (info[i].tab->empty)
2720 drop(&info[i]);
2722 if (coalesce(ctx, n, info) < 0)
2723 goto error;
2725 map = update_basic_maps(map, n, info);
2727 clear_coalesce_info(n, info);
2729 return map;
2730 error:
2731 clear_coalesce_info(n, info);
2732 isl_map_free(map);
2733 return NULL;
2736 /* For each pair of basic sets in the set, check if the union of the two
2737 * can be represented by a single basic set.
2738 * If so, replace the pair by the single basic set and start over.
2740 struct isl_set *isl_set_coalesce(struct isl_set *set)
2742 return (struct isl_set *)isl_map_coalesce((struct isl_map *)set);