isl_basic_map_make_strides_explicit: move down isl_basic_map_get_ctx call
[isl.git] / isl_coalesce.c
blob00a3ca8bf8f92b6e53b7bd96da6a5a0ae377b52d
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2012-2013 Ecole Normale Superieure
5 * Copyright 2014 INRIA Rocquencourt
6 * Copyright 2016 INRIA Paris
8 * Use of this software is governed by the MIT license
10 * Written by Sven Verdoolaege, K.U.Leuven, Departement
11 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
12 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
13 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
14 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
15 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
16 * B.P. 105 - 78153 Le Chesnay, France
17 * and Centre de Recherche Inria de Paris, 2 rue Simone Iff - Voie DQ12,
18 * CS 42112, 75589 Paris Cedex 12, France
21 #include <isl_ctx_private.h>
22 #include "isl_map_private.h"
23 #include <isl_seq.h>
24 #include <isl/options.h>
25 #include "isl_tab.h"
26 #include <isl_mat_private.h>
27 #include <isl_local_space_private.h>
28 #include <isl_val_private.h>
29 #include <isl_vec_private.h>
30 #include <isl_aff_private.h>
31 #include <isl_equalities.h>
32 #include <isl_constraint_private.h>
34 #include <set_to_map.c>
35 #include <set_from_map.c>
37 #define STATUS_ERROR -1
38 #define STATUS_REDUNDANT 1
39 #define STATUS_VALID 2
40 #define STATUS_SEPARATE 3
41 #define STATUS_CUT 4
42 #define STATUS_ADJ_EQ 5
43 #define STATUS_ADJ_INEQ 6
45 static int status_in(isl_int *ineq, struct isl_tab *tab)
47 enum isl_ineq_type type = isl_tab_ineq_type(tab, ineq);
48 switch (type) {
49 default:
50 case isl_ineq_error: return STATUS_ERROR;
51 case isl_ineq_redundant: return STATUS_VALID;
52 case isl_ineq_separate: return STATUS_SEPARATE;
53 case isl_ineq_cut: return STATUS_CUT;
54 case isl_ineq_adj_eq: return STATUS_ADJ_EQ;
55 case isl_ineq_adj_ineq: return STATUS_ADJ_INEQ;
59 /* Compute the position of the equalities of basic map "bmap_i"
60 * with respect to the basic map represented by "tab_j".
61 * The resulting array has twice as many entries as the number
62 * of equalities corresponding to the two inequalities to which
63 * each equality corresponds.
65 static int *eq_status_in(__isl_keep isl_basic_map *bmap_i,
66 struct isl_tab *tab_j)
68 int k, l;
69 int *eq;
70 isl_size dim;
72 dim = isl_basic_map_dim(bmap_i, isl_dim_all);
73 if (dim < 0)
74 return NULL;
76 eq = isl_calloc_array(bmap_i->ctx, int, 2 * bmap_i->n_eq);
77 if (!eq)
78 return NULL;
80 for (k = 0; k < bmap_i->n_eq; ++k) {
81 for (l = 0; l < 2; ++l) {
82 isl_seq_neg(bmap_i->eq[k], bmap_i->eq[k], 1+dim);
83 eq[2 * k + l] = status_in(bmap_i->eq[k], tab_j);
84 if (eq[2 * k + l] == STATUS_ERROR)
85 goto error;
89 return eq;
90 error:
91 free(eq);
92 return NULL;
95 /* Compute the position of the inequalities of basic map "bmap_i"
96 * (also represented by "tab_i", if not NULL) with respect to the basic map
97 * represented by "tab_j".
99 static int *ineq_status_in(__isl_keep isl_basic_map *bmap_i,
100 struct isl_tab *tab_i, struct isl_tab *tab_j)
102 int k;
103 unsigned n_eq = bmap_i->n_eq;
104 int *ineq = isl_calloc_array(bmap_i->ctx, int, bmap_i->n_ineq);
106 if (!ineq)
107 return NULL;
109 for (k = 0; k < bmap_i->n_ineq; ++k) {
110 if (tab_i && isl_tab_is_redundant(tab_i, n_eq + k)) {
111 ineq[k] = STATUS_REDUNDANT;
112 continue;
114 ineq[k] = status_in(bmap_i->ineq[k], tab_j);
115 if (ineq[k] == STATUS_ERROR)
116 goto error;
117 if (ineq[k] == STATUS_SEPARATE)
118 break;
121 return ineq;
122 error:
123 free(ineq);
124 return NULL;
127 static int any(int *con, unsigned len, int status)
129 int i;
131 for (i = 0; i < len ; ++i)
132 if (con[i] == status)
133 return 1;
134 return 0;
137 /* Return the first position of "status" in the list "con" of length "len".
138 * Return -1 if there is no such entry.
140 static int find(int *con, unsigned len, int status)
142 int i;
144 for (i = 0; i < len ; ++i)
145 if (con[i] == status)
146 return i;
147 return -1;
150 static int count(int *con, unsigned len, int status)
152 int i;
153 int c = 0;
155 for (i = 0; i < len ; ++i)
156 if (con[i] == status)
157 c++;
158 return c;
161 static int all(int *con, unsigned len, int status)
163 int i;
165 for (i = 0; i < len ; ++i) {
166 if (con[i] == STATUS_REDUNDANT)
167 continue;
168 if (con[i] != status)
169 return 0;
171 return 1;
174 /* Internal information associated to a basic map in a map
175 * that is to be coalesced by isl_map_coalesce.
177 * "bmap" is the basic map itself (or NULL if "removed" is set)
178 * "tab" is the corresponding tableau (or NULL if "removed" is set)
179 * "hull_hash" identifies the affine space in which "bmap" lives.
180 * "modified" is set if this basic map may not be identical
181 * to any of the basic maps in the input.
182 * "removed" is set if this basic map has been removed from the map
183 * "simplify" is set if this basic map may have some unknown integer
184 * divisions that were not present in the input basic maps. The basic
185 * map should then be simplified such that we may be able to find
186 * a definition among the constraints.
188 * "eq" and "ineq" are only set if we are currently trying to coalesce
189 * this basic map with another basic map, in which case they represent
190 * the position of the inequalities of this basic map with respect to
191 * the other basic map. The number of elements in the "eq" array
192 * is twice the number of equalities in the "bmap", corresponding
193 * to the two inequalities that make up each equality.
195 struct isl_coalesce_info {
196 isl_basic_map *bmap;
197 struct isl_tab *tab;
198 uint32_t hull_hash;
199 int modified;
200 int removed;
201 int simplify;
202 int *eq;
203 int *ineq;
206 /* Is there any (half of an) equality constraint in the description
207 * of the basic map represented by "info" that
208 * has position "status" with respect to the other basic map?
210 static int any_eq(struct isl_coalesce_info *info, int status)
212 isl_size n_eq;
214 n_eq = isl_basic_map_n_equality(info->bmap);
215 return any(info->eq, 2 * n_eq, status);
218 /* Is there any inequality constraint in the description
219 * of the basic map represented by "info" that
220 * has position "status" with respect to the other basic map?
222 static int any_ineq(struct isl_coalesce_info *info, int status)
224 isl_size n_ineq;
226 n_ineq = isl_basic_map_n_inequality(info->bmap);
227 return any(info->ineq, n_ineq, status);
230 /* Return the position of the first half on an equality constraint
231 * in the description of the basic map represented by "info" that
232 * has position "status" with respect to the other basic map.
233 * The returned value is twice the position of the equality constraint
234 * plus zero for the negative half and plus one for the positive half.
235 * Return -1 if there is no such entry.
237 static int find_eq(struct isl_coalesce_info *info, int status)
239 isl_size n_eq;
241 n_eq = isl_basic_map_n_equality(info->bmap);
242 return find(info->eq, 2 * n_eq, status);
245 /* Return the position of the first inequality constraint in the description
246 * of the basic map represented by "info" that
247 * has position "status" with respect to the other basic map.
248 * Return -1 if there is no such entry.
250 static int find_ineq(struct isl_coalesce_info *info, int status)
252 isl_size n_ineq;
254 n_ineq = isl_basic_map_n_inequality(info->bmap);
255 return find(info->ineq, n_ineq, status);
258 /* Return the number of (halves of) equality constraints in the description
259 * of the basic map represented by "info" that
260 * have position "status" with respect to the other basic map.
262 static int count_eq(struct isl_coalesce_info *info, int status)
264 isl_size n_eq;
266 n_eq = isl_basic_map_n_equality(info->bmap);
267 return count(info->eq, 2 * n_eq, status);
270 /* Return the number of inequality constraints in the description
271 * of the basic map represented by "info" that
272 * have position "status" with respect to the other basic map.
274 static int count_ineq(struct isl_coalesce_info *info, int status)
276 isl_size n_ineq;
278 n_ineq = isl_basic_map_n_inequality(info->bmap);
279 return count(info->ineq, n_ineq, status);
282 /* Are all non-redundant constraints of the basic map represented by "info"
283 * either valid or cut constraints with respect to the other basic map?
285 static int all_valid_or_cut(struct isl_coalesce_info *info)
287 int i;
289 for (i = 0; i < 2 * info->bmap->n_eq; ++i) {
290 if (info->eq[i] == STATUS_REDUNDANT)
291 continue;
292 if (info->eq[i] == STATUS_VALID)
293 continue;
294 if (info->eq[i] == STATUS_CUT)
295 continue;
296 return 0;
299 for (i = 0; i < info->bmap->n_ineq; ++i) {
300 if (info->ineq[i] == STATUS_REDUNDANT)
301 continue;
302 if (info->ineq[i] == STATUS_VALID)
303 continue;
304 if (info->ineq[i] == STATUS_CUT)
305 continue;
306 return 0;
309 return 1;
312 /* Compute the hash of the (apparent) affine hull of info->bmap (with
313 * the existentially quantified variables removed) and store it
314 * in info->hash.
316 static int coalesce_info_set_hull_hash(struct isl_coalesce_info *info)
318 isl_basic_map *hull;
319 isl_size n_div;
321 hull = isl_basic_map_copy(info->bmap);
322 hull = isl_basic_map_plain_affine_hull(hull);
323 n_div = isl_basic_map_dim(hull, isl_dim_div);
324 if (n_div < 0)
325 hull = isl_basic_map_free(hull);
326 hull = isl_basic_map_drop_constraints_involving_dims(hull,
327 isl_dim_div, 0, n_div);
328 info->hull_hash = isl_basic_map_get_hash(hull);
329 isl_basic_map_free(hull);
331 return hull ? 0 : -1;
334 /* Free all the allocated memory in an array
335 * of "n" isl_coalesce_info elements.
337 static void clear_coalesce_info(int n, struct isl_coalesce_info *info)
339 int i;
341 if (!info)
342 return;
344 for (i = 0; i < n; ++i) {
345 isl_basic_map_free(info[i].bmap);
346 isl_tab_free(info[i].tab);
349 free(info);
352 /* Clear the memory associated to "info".
354 static void clear(struct isl_coalesce_info *info)
356 info->bmap = isl_basic_map_free(info->bmap);
357 isl_tab_free(info->tab);
358 info->tab = NULL;
361 /* Drop the basic map represented by "info".
362 * That is, clear the memory associated to the entry and
363 * mark it as having been removed.
365 static void drop(struct isl_coalesce_info *info)
367 clear(info);
368 info->removed = 1;
371 /* Exchange the information in "info1" with that in "info2".
373 static void exchange(struct isl_coalesce_info *info1,
374 struct isl_coalesce_info *info2)
376 struct isl_coalesce_info info;
378 info = *info1;
379 *info1 = *info2;
380 *info2 = info;
383 /* This type represents the kind of change that has been performed
384 * while trying to coalesce two basic maps.
386 * isl_change_none: nothing was changed
387 * isl_change_drop_first: the first basic map was removed
388 * isl_change_drop_second: the second basic map was removed
389 * isl_change_fuse: the two basic maps were replaced by a new basic map.
391 enum isl_change {
392 isl_change_error = -1,
393 isl_change_none = 0,
394 isl_change_drop_first,
395 isl_change_drop_second,
396 isl_change_fuse,
399 /* Update "change" based on an interchange of the first and the second
400 * basic map. That is, interchange isl_change_drop_first and
401 * isl_change_drop_second.
403 static enum isl_change invert_change(enum isl_change change)
405 switch (change) {
406 case isl_change_error:
407 return isl_change_error;
408 case isl_change_none:
409 return isl_change_none;
410 case isl_change_drop_first:
411 return isl_change_drop_second;
412 case isl_change_drop_second:
413 return isl_change_drop_first;
414 case isl_change_fuse:
415 return isl_change_fuse;
418 return isl_change_error;
421 /* Add the valid constraints of the basic map represented by "info"
422 * to "bmap". "len" is the size of the constraints.
423 * If only one of the pair of inequalities that make up an equality
424 * is valid, then add that inequality.
426 static __isl_give isl_basic_map *add_valid_constraints(
427 __isl_take isl_basic_map *bmap, struct isl_coalesce_info *info,
428 unsigned len)
430 int k, l;
432 if (!bmap)
433 return NULL;
435 for (k = 0; k < info->bmap->n_eq; ++k) {
436 if (info->eq[2 * k] == STATUS_VALID &&
437 info->eq[2 * k + 1] == STATUS_VALID) {
438 l = isl_basic_map_alloc_equality(bmap);
439 if (l < 0)
440 return isl_basic_map_free(bmap);
441 isl_seq_cpy(bmap->eq[l], info->bmap->eq[k], len);
442 } else if (info->eq[2 * k] == STATUS_VALID) {
443 l = isl_basic_map_alloc_inequality(bmap);
444 if (l < 0)
445 return isl_basic_map_free(bmap);
446 isl_seq_neg(bmap->ineq[l], info->bmap->eq[k], len);
447 } else if (info->eq[2 * k + 1] == STATUS_VALID) {
448 l = isl_basic_map_alloc_inequality(bmap);
449 if (l < 0)
450 return isl_basic_map_free(bmap);
451 isl_seq_cpy(bmap->ineq[l], info->bmap->eq[k], len);
455 for (k = 0; k < info->bmap->n_ineq; ++k) {
456 if (info->ineq[k] != STATUS_VALID)
457 continue;
458 l = isl_basic_map_alloc_inequality(bmap);
459 if (l < 0)
460 return isl_basic_map_free(bmap);
461 isl_seq_cpy(bmap->ineq[l], info->bmap->ineq[k], len);
464 return bmap;
467 /* Is "bmap" defined by a number of (non-redundant) constraints that
468 * is greater than the number of constraints of basic maps i and j combined?
469 * Equalities are counted as two inequalities.
471 static int number_of_constraints_increases(int i, int j,
472 struct isl_coalesce_info *info,
473 __isl_keep isl_basic_map *bmap, struct isl_tab *tab)
475 int k, n_old, n_new;
477 n_old = 2 * info[i].bmap->n_eq + info[i].bmap->n_ineq;
478 n_old += 2 * info[j].bmap->n_eq + info[j].bmap->n_ineq;
480 n_new = 2 * bmap->n_eq;
481 for (k = 0; k < bmap->n_ineq; ++k)
482 if (!isl_tab_is_redundant(tab, bmap->n_eq + k))
483 ++n_new;
485 return n_new > n_old;
488 /* Replace the pair of basic maps i and j by the basic map bounded
489 * by the valid constraints in both basic maps and the constraints
490 * in extra (if not NULL).
491 * Place the fused basic map in the position that is the smallest of i and j.
493 * If "detect_equalities" is set, then look for equalities encoded
494 * as pairs of inequalities.
495 * If "check_number" is set, then the original basic maps are only
496 * replaced if the total number of constraints does not increase.
497 * While the number of integer divisions in the two basic maps
498 * is assumed to be the same, the actual definitions may be different.
499 * We only copy the definition from one of the basic map if it is
500 * the same as that of the other basic map. Otherwise, we mark
501 * the integer division as unknown and simplify the basic map
502 * in an attempt to recover the integer division definition.
503 * If any extra constraints get introduced, then these may
504 * involve integer divisions with a unit coefficient.
505 * Eliminate those that do not appear with any other coefficient
506 * in other constraints, to ensure they get eliminated completely,
507 * improving the chances of further coalescing.
509 static enum isl_change fuse(int i, int j, struct isl_coalesce_info *info,
510 __isl_keep isl_mat *extra, int detect_equalities, int check_number)
512 int k, l;
513 struct isl_basic_map *fused = NULL;
514 struct isl_tab *fused_tab = NULL;
515 isl_size total = isl_basic_map_dim(info[i].bmap, isl_dim_all);
516 unsigned extra_rows = extra ? extra->n_row : 0;
517 unsigned n_eq, n_ineq;
518 int simplify = 0;
520 if (total < 0)
521 return isl_change_error;
522 if (j < i)
523 return fuse(j, i, info, extra, detect_equalities, check_number);
525 n_eq = info[i].bmap->n_eq + info[j].bmap->n_eq;
526 n_ineq = info[i].bmap->n_ineq + info[j].bmap->n_ineq;
527 fused = isl_basic_map_alloc_space(isl_space_copy(info[i].bmap->dim),
528 info[i].bmap->n_div, n_eq, n_eq + n_ineq + extra_rows);
529 fused = add_valid_constraints(fused, &info[i], 1 + total);
530 fused = add_valid_constraints(fused, &info[j], 1 + total);
531 if (!fused)
532 goto error;
533 if (ISL_F_ISSET(info[i].bmap, ISL_BASIC_MAP_RATIONAL) &&
534 ISL_F_ISSET(info[j].bmap, ISL_BASIC_MAP_RATIONAL))
535 ISL_F_SET(fused, ISL_BASIC_MAP_RATIONAL);
537 for (k = 0; k < info[i].bmap->n_div; ++k) {
538 int l = isl_basic_map_alloc_div(fused);
539 if (l < 0)
540 goto error;
541 if (isl_seq_eq(info[i].bmap->div[k], info[j].bmap->div[k],
542 1 + 1 + total)) {
543 isl_seq_cpy(fused->div[l], info[i].bmap->div[k],
544 1 + 1 + total);
545 } else {
546 isl_int_set_si(fused->div[l][0], 0);
547 simplify = 1;
551 for (k = 0; k < extra_rows; ++k) {
552 l = isl_basic_map_alloc_inequality(fused);
553 if (l < 0)
554 goto error;
555 isl_seq_cpy(fused->ineq[l], extra->row[k], 1 + total);
558 if (detect_equalities)
559 fused = isl_basic_map_detect_inequality_pairs(fused, NULL);
560 fused = isl_basic_map_gauss(fused, NULL);
561 if (simplify || info[j].simplify) {
562 fused = isl_basic_map_simplify(fused);
563 info[i].simplify = 0;
564 } else if (extra_rows > 0) {
565 fused = isl_basic_map_eliminate_pure_unit_divs(fused);
567 fused = isl_basic_map_finalize(fused);
569 fused_tab = isl_tab_from_basic_map(fused, 0);
570 if (isl_tab_detect_redundant(fused_tab) < 0)
571 goto error;
573 if (check_number &&
574 number_of_constraints_increases(i, j, info, fused, fused_tab)) {
575 isl_tab_free(fused_tab);
576 isl_basic_map_free(fused);
577 return isl_change_none;
580 clear(&info[i]);
581 info[i].bmap = fused;
582 info[i].tab = fused_tab;
583 info[i].modified = 1;
584 drop(&info[j]);
586 return isl_change_fuse;
587 error:
588 isl_tab_free(fused_tab);
589 isl_basic_map_free(fused);
590 return isl_change_error;
593 /* Given a pair of basic maps i and j such that all constraints are either
594 * "valid" or "cut", check if the facets corresponding to the "cut"
595 * constraints of i lie entirely within basic map j.
596 * If so, replace the pair by the basic map consisting of the valid
597 * constraints in both basic maps.
598 * Checking whether the facet lies entirely within basic map j
599 * is performed by checking whether the constraints of basic map j
600 * are valid for the facet. These tests are performed on a rational
601 * tableau to avoid the theoretical possibility that a constraint
602 * that was considered to be a cut constraint for the entire basic map i
603 * happens to be considered to be a valid constraint for the facet,
604 * even though it cuts off the same rational points.
606 * To see that we are not introducing any extra points, call the
607 * two basic maps A and B and the resulting map U and let x
608 * be an element of U \setminus ( A \cup B ).
609 * A line connecting x with an element of A \cup B meets a facet F
610 * of either A or B. Assume it is a facet of B and let c_1 be
611 * the corresponding facet constraint. We have c_1(x) < 0 and
612 * so c_1 is a cut constraint. This implies that there is some
613 * (possibly rational) point x' satisfying the constraints of A
614 * and the opposite of c_1 as otherwise c_1 would have been marked
615 * valid for A. The line connecting x and x' meets a facet of A
616 * in a (possibly rational) point that also violates c_1, but this
617 * is impossible since all cut constraints of B are valid for all
618 * cut facets of A.
619 * In case F is a facet of A rather than B, then we can apply the
620 * above reasoning to find a facet of B separating x from A \cup B first.
622 static enum isl_change check_facets(int i, int j,
623 struct isl_coalesce_info *info)
625 int k, l;
626 struct isl_tab_undo *snap, *snap2;
627 unsigned n_eq = info[i].bmap->n_eq;
629 snap = isl_tab_snap(info[i].tab);
630 if (isl_tab_mark_rational(info[i].tab) < 0)
631 return isl_change_error;
632 snap2 = isl_tab_snap(info[i].tab);
634 for (k = 0; k < info[i].bmap->n_ineq; ++k) {
635 if (info[i].ineq[k] != STATUS_CUT)
636 continue;
637 if (isl_tab_select_facet(info[i].tab, n_eq + k) < 0)
638 return isl_change_error;
639 for (l = 0; l < info[j].bmap->n_ineq; ++l) {
640 int stat;
641 if (info[j].ineq[l] != STATUS_CUT)
642 continue;
643 stat = status_in(info[j].bmap->ineq[l], info[i].tab);
644 if (stat < 0)
645 return isl_change_error;
646 if (stat != STATUS_VALID)
647 break;
649 if (isl_tab_rollback(info[i].tab, snap2) < 0)
650 return isl_change_error;
651 if (l < info[j].bmap->n_ineq)
652 break;
655 if (k < info[i].bmap->n_ineq) {
656 if (isl_tab_rollback(info[i].tab, snap) < 0)
657 return isl_change_error;
658 return isl_change_none;
660 return fuse(i, j, info, NULL, 0, 0);
663 /* Check if info->bmap contains the basic map represented
664 * by the tableau "tab".
665 * For each equality, we check both the constraint itself
666 * (as an inequality) and its negation. Make sure the
667 * equality is returned to its original state before returning.
669 static isl_bool contains(struct isl_coalesce_info *info, struct isl_tab *tab)
671 int k;
672 isl_size dim;
673 isl_basic_map *bmap = info->bmap;
675 dim = isl_basic_map_dim(bmap, isl_dim_all);
676 if (dim < 0)
677 return isl_bool_error;
678 for (k = 0; k < bmap->n_eq; ++k) {
679 int stat;
680 isl_seq_neg(bmap->eq[k], bmap->eq[k], 1 + dim);
681 stat = status_in(bmap->eq[k], tab);
682 isl_seq_neg(bmap->eq[k], bmap->eq[k], 1 + dim);
683 if (stat < 0)
684 return isl_bool_error;
685 if (stat != STATUS_VALID)
686 return isl_bool_false;
687 stat = status_in(bmap->eq[k], tab);
688 if (stat < 0)
689 return isl_bool_error;
690 if (stat != STATUS_VALID)
691 return isl_bool_false;
694 for (k = 0; k < bmap->n_ineq; ++k) {
695 int stat;
696 if (info->ineq[k] == STATUS_REDUNDANT)
697 continue;
698 stat = status_in(bmap->ineq[k], tab);
699 if (stat < 0)
700 return isl_bool_error;
701 if (stat != STATUS_VALID)
702 return isl_bool_false;
704 return isl_bool_true;
707 /* Basic map "i" has an inequality (say "k") that is adjacent
708 * to some inequality of basic map "j". All the other inequalities
709 * are valid for "j".
710 * Check if basic map "j" forms an extension of basic map "i".
712 * Note that this function is only called if some of the equalities or
713 * inequalities of basic map "j" do cut basic map "i". The function is
714 * correct even if there are no such cut constraints, but in that case
715 * the additional checks performed by this function are overkill.
717 * In particular, we replace constraint k, say f >= 0, by constraint
718 * f <= -1, add the inequalities of "j" that are valid for "i"
719 * and check if the result is a subset of basic map "j".
720 * To improve the chances of the subset relation being detected,
721 * any variable that only attains a single integer value
722 * in the tableau of "i" is first fixed to that value.
723 * If the result is a subset, then we know that this result is exactly equal
724 * to basic map "j" since all its constraints are valid for basic map "j".
725 * By combining the valid constraints of "i" (all equalities and all
726 * inequalities except "k") and the valid constraints of "j" we therefore
727 * obtain a basic map that is equal to their union.
728 * In this case, there is no need to perform a rollback of the tableau
729 * since it is going to be destroyed in fuse().
732 * |\__ |\__
733 * | \__ | \__
734 * | \_ => | \__
735 * |_______| _ |_________\
738 * |\ |\
739 * | \ | \
740 * | \ | \
741 * | | | \
742 * | ||\ => | \
743 * | || \ | \
744 * | || | | |
745 * |__||_/ |_____/
747 static enum isl_change is_adj_ineq_extension(int i, int j,
748 struct isl_coalesce_info *info)
750 int k;
751 struct isl_tab_undo *snap;
752 unsigned n_eq = info[i].bmap->n_eq;
753 isl_size total = isl_basic_map_dim(info[i].bmap, isl_dim_all);
754 isl_stat r;
755 isl_bool super;
757 if (total < 0)
758 return isl_change_error;
759 if (isl_tab_extend_cons(info[i].tab, 1 + info[j].bmap->n_ineq) < 0)
760 return isl_change_error;
762 k = find_ineq(&info[i], STATUS_ADJ_INEQ);
763 if (k < 0)
764 isl_die(isl_basic_map_get_ctx(info[i].bmap), isl_error_internal,
765 "info[i].ineq should have exactly one STATUS_ADJ_INEQ",
766 return isl_change_error);
768 snap = isl_tab_snap(info[i].tab);
770 if (isl_tab_unrestrict(info[i].tab, n_eq + k) < 0)
771 return isl_change_error;
773 isl_seq_neg(info[i].bmap->ineq[k], info[i].bmap->ineq[k], 1 + total);
774 isl_int_sub_ui(info[i].bmap->ineq[k][0], info[i].bmap->ineq[k][0], 1);
775 r = isl_tab_add_ineq(info[i].tab, info[i].bmap->ineq[k]);
776 isl_seq_neg(info[i].bmap->ineq[k], info[i].bmap->ineq[k], 1 + total);
777 isl_int_sub_ui(info[i].bmap->ineq[k][0], info[i].bmap->ineq[k][0], 1);
778 if (r < 0)
779 return isl_change_error;
781 for (k = 0; k < info[j].bmap->n_ineq; ++k) {
782 if (info[j].ineq[k] != STATUS_VALID)
783 continue;
784 if (isl_tab_add_ineq(info[i].tab, info[j].bmap->ineq[k]) < 0)
785 return isl_change_error;
787 if (isl_tab_detect_constants(info[i].tab) < 0)
788 return isl_change_error;
790 super = contains(&info[j], info[i].tab);
791 if (super < 0)
792 return isl_change_error;
793 if (super)
794 return fuse(i, j, info, NULL, 0, 0);
796 if (isl_tab_rollback(info[i].tab, snap) < 0)
797 return isl_change_error;
799 return isl_change_none;
803 /* Both basic maps have at least one inequality with and adjacent
804 * (but opposite) inequality in the other basic map.
805 * Check that there are no cut constraints and that there is only
806 * a single pair of adjacent inequalities.
807 * If so, we can replace the pair by a single basic map described
808 * by all but the pair of adjacent inequalities.
809 * Any additional points introduced lie strictly between the two
810 * adjacent hyperplanes and can therefore be integral.
812 * ____ _____
813 * / ||\ / \
814 * / || \ / \
815 * \ || \ => \ \
816 * \ || / \ /
817 * \___||_/ \_____/
819 * The test for a single pair of adjancent inequalities is important
820 * for avoiding the combination of two basic maps like the following
822 * /|
823 * / |
824 * /__|
825 * _____
826 * | |
827 * | |
828 * |___|
830 * If there are some cut constraints on one side, then we may
831 * still be able to fuse the two basic maps, but we need to perform
832 * some additional checks in is_adj_ineq_extension.
834 static enum isl_change check_adj_ineq(int i, int j,
835 struct isl_coalesce_info *info)
837 int count_i, count_j;
838 int cut_i, cut_j;
840 count_i = count_ineq(&info[i], STATUS_ADJ_INEQ);
841 count_j = count_ineq(&info[j], STATUS_ADJ_INEQ);
843 if (count_i != 1 && count_j != 1)
844 return isl_change_none;
846 cut_i = any_eq(&info[i], STATUS_CUT) || any_ineq(&info[i], STATUS_CUT);
847 cut_j = any_eq(&info[j], STATUS_CUT) || any_ineq(&info[j], STATUS_CUT);
849 if (!cut_i && !cut_j && count_i == 1 && count_j == 1)
850 return fuse(i, j, info, NULL, 0, 0);
852 if (count_i == 1 && !cut_i)
853 return is_adj_ineq_extension(i, j, info);
855 if (count_j == 1 && !cut_j)
856 return is_adj_ineq_extension(j, i, info);
858 return isl_change_none;
861 /* Given an affine transformation matrix "T", does row "row" represent
862 * anything other than a unit vector (possibly shifted by a constant)
863 * that is not involved in any of the other rows?
865 * That is, if a constraint involves the variable corresponding to
866 * the row, then could its preimage by "T" have any coefficients
867 * that are different from those in the original constraint?
869 static int not_unique_unit_row(__isl_keep isl_mat *T, int row)
871 int i, j;
872 int len = T->n_col - 1;
874 i = isl_seq_first_non_zero(T->row[row] + 1, len);
875 if (i < 0)
876 return 1;
877 if (!isl_int_is_one(T->row[row][1 + i]) &&
878 !isl_int_is_negone(T->row[row][1 + i]))
879 return 1;
881 j = isl_seq_first_non_zero(T->row[row] + 1 + i + 1, len - (i + 1));
882 if (j >= 0)
883 return 1;
885 for (j = 1; j < T->n_row; ++j) {
886 if (j == row)
887 continue;
888 if (!isl_int_is_zero(T->row[j][1 + i]))
889 return 1;
892 return 0;
895 /* Does inequality constraint "ineq" of "bmap" involve any of
896 * the variables marked in "affected"?
897 * "total" is the total number of variables, i.e., the number
898 * of entries in "affected".
900 static isl_bool is_affected(__isl_keep isl_basic_map *bmap, int ineq,
901 int *affected, int total)
903 int i;
905 for (i = 0; i < total; ++i) {
906 if (!affected[i])
907 continue;
908 if (!isl_int_is_zero(bmap->ineq[ineq][1 + i]))
909 return isl_bool_true;
912 return isl_bool_false;
915 /* Given the compressed version of inequality constraint "ineq"
916 * of info->bmap in "v", check if the constraint can be tightened,
917 * where the compression is based on an equality constraint valid
918 * for info->tab.
919 * If so, add the tightened version of the inequality constraint
920 * to info->tab. "v" may be modified by this function.
922 * That is, if the compressed constraint is of the form
924 * m f() + c >= 0
926 * with 0 < c < m, then it is equivalent to
928 * f() >= 0
930 * This means that c can also be subtracted from the original,
931 * uncompressed constraint without affecting the integer points
932 * in info->tab. Add this tightened constraint as an extra row
933 * to info->tab to make this information explicitly available.
935 static __isl_give isl_vec *try_tightening(struct isl_coalesce_info *info,
936 int ineq, __isl_take isl_vec *v)
938 isl_ctx *ctx;
939 isl_stat r;
941 if (!v)
942 return NULL;
944 ctx = isl_vec_get_ctx(v);
945 isl_seq_gcd(v->el + 1, v->size - 1, &ctx->normalize_gcd);
946 if (isl_int_is_zero(ctx->normalize_gcd) ||
947 isl_int_is_one(ctx->normalize_gcd)) {
948 return v;
951 v = isl_vec_cow(v);
952 if (!v)
953 return NULL;
955 isl_int_fdiv_r(v->el[0], v->el[0], ctx->normalize_gcd);
956 if (isl_int_is_zero(v->el[0]))
957 return v;
959 if (isl_tab_extend_cons(info->tab, 1) < 0)
960 return isl_vec_free(v);
962 isl_int_sub(info->bmap->ineq[ineq][0],
963 info->bmap->ineq[ineq][0], v->el[0]);
964 r = isl_tab_add_ineq(info->tab, info->bmap->ineq[ineq]);
965 isl_int_add(info->bmap->ineq[ineq][0],
966 info->bmap->ineq[ineq][0], v->el[0]);
968 if (r < 0)
969 return isl_vec_free(v);
971 return v;
974 /* Tighten the (non-redundant) constraints on the facet represented
975 * by info->tab.
976 * In particular, on input, info->tab represents the result
977 * of relaxing the "n" inequality constraints of info->bmap in "relaxed"
978 * by one, i.e., replacing f_i >= 0 by f_i + 1 >= 0, and then
979 * replacing the one at index "l" by the corresponding equality,
980 * i.e., f_k + 1 = 0, with k = relaxed[l].
982 * Compute a variable compression from the equality constraint f_k + 1 = 0
983 * and use it to tighten the other constraints of info->bmap
984 * (that is, all constraints that have not been relaxed),
985 * updating info->tab (and leaving info->bmap untouched).
986 * The compression handles essentially two cases, one where a variable
987 * is assigned a fixed value and can therefore be eliminated, and one
988 * where one variable is a shifted multiple of some other variable and
989 * can therefore be replaced by that multiple.
990 * Gaussian elimination would also work for the first case, but for
991 * the second case, the effectiveness would depend on the order
992 * of the variables.
993 * After compression, some of the constraints may have coefficients
994 * with a common divisor. If this divisor does not divide the constant
995 * term, then the constraint can be tightened.
996 * The tightening is performed on the tableau info->tab by introducing
997 * extra (temporary) constraints.
999 * Only constraints that are possibly affected by the compression are
1000 * considered. In particular, if the constraint only involves variables
1001 * that are directly mapped to a distinct set of other variables, then
1002 * no common divisor can be introduced and no tightening can occur.
1004 * It is important to only consider the non-redundant constraints
1005 * since the facet constraint has been relaxed prior to the call
1006 * to this function, meaning that the constraints that were redundant
1007 * prior to the relaxation may no longer be redundant.
1008 * These constraints will be ignored in the fused result, so
1009 * the fusion detection should not exploit them.
1011 static isl_stat tighten_on_relaxed_facet(struct isl_coalesce_info *info,
1012 int n, int *relaxed, int l)
1014 isl_size total;
1015 isl_ctx *ctx;
1016 isl_vec *v = NULL;
1017 isl_mat *T;
1018 int i;
1019 int k;
1020 int *affected;
1022 k = relaxed[l];
1023 ctx = isl_basic_map_get_ctx(info->bmap);
1024 total = isl_basic_map_dim(info->bmap, isl_dim_all);
1025 if (total < 0)
1026 return isl_stat_error;
1027 isl_int_add_ui(info->bmap->ineq[k][0], info->bmap->ineq[k][0], 1);
1028 T = isl_mat_sub_alloc6(ctx, info->bmap->ineq, k, 1, 0, 1 + total);
1029 T = isl_mat_variable_compression(T, NULL);
1030 isl_int_sub_ui(info->bmap->ineq[k][0], info->bmap->ineq[k][0], 1);
1031 if (!T)
1032 return isl_stat_error;
1033 if (T->n_col == 0) {
1034 isl_mat_free(T);
1035 return isl_stat_ok;
1038 affected = isl_alloc_array(ctx, int, total);
1039 if (!affected)
1040 goto error;
1042 for (i = 0; i < total; ++i)
1043 affected[i] = not_unique_unit_row(T, 1 + i);
1045 for (i = 0; i < info->bmap->n_ineq; ++i) {
1046 isl_bool handle;
1047 if (any(relaxed, n, i))
1048 continue;
1049 if (info->ineq[i] == STATUS_REDUNDANT)
1050 continue;
1051 handle = is_affected(info->bmap, i, affected, total);
1052 if (handle < 0)
1053 goto error;
1054 if (!handle)
1055 continue;
1056 v = isl_vec_alloc(ctx, 1 + total);
1057 if (!v)
1058 goto error;
1059 isl_seq_cpy(v->el, info->bmap->ineq[i], 1 + total);
1060 v = isl_vec_mat_product(v, isl_mat_copy(T));
1061 v = try_tightening(info, i, v);
1062 isl_vec_free(v);
1063 if (!v)
1064 goto error;
1067 isl_mat_free(T);
1068 free(affected);
1069 return isl_stat_ok;
1070 error:
1071 isl_mat_free(T);
1072 free(affected);
1073 return isl_stat_error;
1076 /* Replace the basic maps "i" and "j" by an extension of "i"
1077 * along the "n" inequality constraints in "relax" by one.
1078 * The tableau info[i].tab has already been extended.
1079 * Extend info[i].bmap accordingly by relaxing all constraints in "relax"
1080 * by one.
1081 * Each integer division that does not have exactly the same
1082 * definition in "i" and "j" is marked unknown and the basic map
1083 * is scheduled to be simplified in an attempt to recover
1084 * the integer division definition.
1085 * Place the extension in the position that is the smallest of i and j.
1087 static enum isl_change extend(int i, int j, int n, int *relax,
1088 struct isl_coalesce_info *info)
1090 int l;
1091 isl_size total;
1093 info[i].bmap = isl_basic_map_cow(info[i].bmap);
1094 total = isl_basic_map_dim(info[i].bmap, isl_dim_all);
1095 if (total < 0)
1096 return isl_change_error;
1097 for (l = 0; l < info[i].bmap->n_div; ++l)
1098 if (!isl_seq_eq(info[i].bmap->div[l],
1099 info[j].bmap->div[l], 1 + 1 + total)) {
1100 isl_int_set_si(info[i].bmap->div[l][0], 0);
1101 info[i].simplify = 1;
1103 for (l = 0; l < n; ++l)
1104 isl_int_add_ui(info[i].bmap->ineq[relax[l]][0],
1105 info[i].bmap->ineq[relax[l]][0], 1);
1106 ISL_F_CLR(info[i].bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1107 ISL_F_SET(info[i].bmap, ISL_BASIC_MAP_FINAL);
1108 drop(&info[j]);
1109 info[i].modified = 1;
1110 if (j < i)
1111 exchange(&info[i], &info[j]);
1112 return isl_change_fuse;
1115 /* Basic map "i" has "n" inequality constraints (collected in "relax")
1116 * that are such that they include basic map "j" if they are relaxed
1117 * by one. All the other inequalities are valid for "j".
1118 * Check if basic map "j" forms an extension of basic map "i".
1120 * In particular, relax the constraints in "relax", compute the corresponding
1121 * facets one by one and check whether each of these is included
1122 * in the other basic map.
1123 * Before testing for inclusion, the constraints on each facet
1124 * are tightened to increase the chance of an inclusion being detected.
1125 * (Adding the valid constraints of "j" to the tableau of "i", as is done
1126 * in is_adj_ineq_extension, may further increase those chances, but this
1127 * is not currently done.)
1128 * If each facet is included, we know that relaxing the constraints extends
1129 * the basic map with exactly the other basic map (we already know that this
1130 * other basic map is included in the extension, because all other
1131 * inequality constraints are valid of "j") and we can replace the
1132 * two basic maps by this extension.
1134 * If any of the relaxed constraints turn out to be redundant, then bail out.
1135 * isl_tab_select_facet refuses to handle such constraints. It may be
1136 * possible to handle them anyway by making a distinction between
1137 * redundant constraints with a corresponding facet that still intersects
1138 * the set (allowing isl_tab_select_facet to handle them) and
1139 * those where the facet does not intersect the set (which can be ignored
1140 * because the empty facet is trivially included in the other disjunct).
1141 * However, relaxed constraints that turn out to be redundant should
1142 * be fairly rare and no such instance has been reported where
1143 * coalescing would be successful.
1144 * ____ _____
1145 * / || / |
1146 * / || / |
1147 * \ || => \ |
1148 * \ || \ |
1149 * \___|| \____|
1152 * \ |\
1153 * |\\ | \
1154 * | \\ | \
1155 * | | => | /
1156 * | / | /
1157 * |/ |/
1159 static enum isl_change is_relaxed_extension(int i, int j, int n, int *relax,
1160 struct isl_coalesce_info *info)
1162 int l;
1163 isl_bool super;
1164 struct isl_tab_undo *snap, *snap2;
1165 unsigned n_eq = info[i].bmap->n_eq;
1167 for (l = 0; l < n; ++l)
1168 if (isl_tab_is_equality(info[i].tab, n_eq + relax[l]))
1169 return isl_change_none;
1171 snap = isl_tab_snap(info[i].tab);
1172 for (l = 0; l < n; ++l)
1173 if (isl_tab_relax(info[i].tab, n_eq + relax[l]) < 0)
1174 return isl_change_error;
1175 for (l = 0; l < n; ++l) {
1176 if (!isl_tab_is_redundant(info[i].tab, n_eq + relax[l]))
1177 continue;
1178 if (isl_tab_rollback(info[i].tab, snap) < 0)
1179 return isl_change_error;
1180 return isl_change_none;
1182 snap2 = isl_tab_snap(info[i].tab);
1183 for (l = 0; l < n; ++l) {
1184 if (isl_tab_rollback(info[i].tab, snap2) < 0)
1185 return isl_change_error;
1186 if (isl_tab_select_facet(info[i].tab, n_eq + relax[l]) < 0)
1187 return isl_change_error;
1188 if (tighten_on_relaxed_facet(&info[i], n, relax, l) < 0)
1189 return isl_change_error;
1190 super = contains(&info[j], info[i].tab);
1191 if (super < 0)
1192 return isl_change_error;
1193 if (super)
1194 continue;
1195 if (isl_tab_rollback(info[i].tab, snap) < 0)
1196 return isl_change_error;
1197 return isl_change_none;
1200 if (isl_tab_rollback(info[i].tab, snap2) < 0)
1201 return isl_change_error;
1202 return extend(i, j, n, relax, info);
1205 /* Data structure that keeps track of the wrapping constraints
1206 * and of information to bound the coefficients of those constraints.
1208 * bound is set if we want to apply a bound on the coefficients
1209 * mat contains the wrapping constraints
1210 * max is the bound on the coefficients (if bound is set)
1212 struct isl_wraps {
1213 int bound;
1214 isl_mat *mat;
1215 isl_int max;
1218 /* Update wraps->max to be greater than or equal to the coefficients
1219 * in the equalities and inequalities of info->bmap that can be removed
1220 * if we end up applying wrapping.
1222 static isl_stat wraps_update_max(struct isl_wraps *wraps,
1223 struct isl_coalesce_info *info)
1225 int k;
1226 isl_int max_k;
1227 isl_size total = isl_basic_map_dim(info->bmap, isl_dim_all);
1229 if (total < 0)
1230 return isl_stat_error;
1231 isl_int_init(max_k);
1233 for (k = 0; k < info->bmap->n_eq; ++k) {
1234 if (info->eq[2 * k] == STATUS_VALID &&
1235 info->eq[2 * k + 1] == STATUS_VALID)
1236 continue;
1237 isl_seq_abs_max(info->bmap->eq[k] + 1, total, &max_k);
1238 if (isl_int_abs_gt(max_k, wraps->max))
1239 isl_int_set(wraps->max, max_k);
1242 for (k = 0; k < info->bmap->n_ineq; ++k) {
1243 if (info->ineq[k] == STATUS_VALID ||
1244 info->ineq[k] == STATUS_REDUNDANT)
1245 continue;
1246 isl_seq_abs_max(info->bmap->ineq[k] + 1, total, &max_k);
1247 if (isl_int_abs_gt(max_k, wraps->max))
1248 isl_int_set(wraps->max, max_k);
1251 isl_int_clear(max_k);
1253 return isl_stat_ok;
1256 /* Initialize the isl_wraps data structure.
1257 * If we want to bound the coefficients of the wrapping constraints,
1258 * we set wraps->max to the largest coefficient
1259 * in the equalities and inequalities that can be removed if we end up
1260 * applying wrapping.
1262 static isl_stat wraps_init(struct isl_wraps *wraps, __isl_take isl_mat *mat,
1263 struct isl_coalesce_info *info, int i, int j)
1265 isl_ctx *ctx;
1267 wraps->bound = 0;
1268 wraps->mat = mat;
1269 if (!mat)
1270 return isl_stat_error;
1271 ctx = isl_mat_get_ctx(mat);
1272 wraps->bound = isl_options_get_coalesce_bounded_wrapping(ctx);
1273 if (!wraps->bound)
1274 return isl_stat_ok;
1275 isl_int_init(wraps->max);
1276 isl_int_set_si(wraps->max, 0);
1277 if (wraps_update_max(wraps, &info[i]) < 0)
1278 return isl_stat_error;
1279 if (wraps_update_max(wraps, &info[j]) < 0)
1280 return isl_stat_error;
1282 return isl_stat_ok;
1285 /* Free the contents of the isl_wraps data structure.
1287 static void wraps_free(struct isl_wraps *wraps)
1289 isl_mat_free(wraps->mat);
1290 if (wraps->bound)
1291 isl_int_clear(wraps->max);
1294 /* Mark the wrapping as failed by resetting wraps->mat->n_row to zero.
1296 static isl_stat wraps_mark_failed(struct isl_wraps *wraps)
1298 wraps->mat->n_row = 0;
1299 return isl_stat_ok;
1302 /* Is the wrapping constraint in row "row" allowed?
1304 * If wraps->bound is set, we check that none of the coefficients
1305 * is greater than wraps->max.
1307 static int allow_wrap(struct isl_wraps *wraps, int row)
1309 int i;
1311 if (!wraps->bound)
1312 return 1;
1314 for (i = 1; i < wraps->mat->n_col; ++i)
1315 if (isl_int_abs_gt(wraps->mat->row[row][i], wraps->max))
1316 return 0;
1318 return 1;
1321 /* Wrap "ineq" (or its opposite if "negate" is set) around "bound"
1322 * to include "set" and add the result in position "w" of "wraps".
1323 * "len" is the total number of coefficients in "bound" and "ineq".
1324 * Return 1 on success, 0 on failure and -1 on error.
1325 * Wrapping can fail if the result of wrapping is equal to "bound"
1326 * or if we want to bound the sizes of the coefficients and
1327 * the wrapped constraint does not satisfy this bound.
1329 static int add_wrap(struct isl_wraps *wraps, int w, isl_int *bound,
1330 isl_int *ineq, unsigned len, __isl_keep isl_set *set, int negate)
1332 isl_seq_cpy(wraps->mat->row[w], bound, len);
1333 if (negate) {
1334 isl_seq_neg(wraps->mat->row[w + 1], ineq, len);
1335 ineq = wraps->mat->row[w + 1];
1337 if (!isl_set_wrap_facet(set, wraps->mat->row[w], ineq))
1338 return -1;
1339 if (isl_seq_eq(wraps->mat->row[w], bound, len))
1340 return 0;
1341 if (!allow_wrap(wraps, w))
1342 return 0;
1343 return 1;
1346 /* For each constraint in info->bmap that is not redundant (as determined
1347 * by info->tab) and that is not a valid constraint for the other basic map,
1348 * wrap the constraint around "bound" such that it includes the whole
1349 * set "set" and append the resulting constraint to "wraps".
1350 * Note that the constraints that are valid for the other basic map
1351 * will be added to the combined basic map by default, so there is
1352 * no need to wrap them.
1353 * The caller wrap_in_facets even relies on this function not wrapping
1354 * any constraints that are already valid.
1355 * "wraps" is assumed to have been pre-allocated to the appropriate size.
1356 * wraps->n_row is the number of actual wrapped constraints that have
1357 * been added.
1358 * If any of the wrapping problems results in a constraint that is
1359 * identical to "bound", then this means that "set" is unbounded in such
1360 * way that no wrapping is possible. If this happens then wraps->n_row
1361 * is reset to zero.
1362 * Similarly, if we want to bound the coefficients of the wrapping
1363 * constraints and a newly added wrapping constraint does not
1364 * satisfy the bound, then wraps->n_row is also reset to zero.
1366 static isl_stat add_wraps(struct isl_wraps *wraps,
1367 struct isl_coalesce_info *info, isl_int *bound, __isl_keep isl_set *set)
1369 int l, m;
1370 int w;
1371 int added;
1372 isl_basic_map *bmap = info->bmap;
1373 isl_size total = isl_basic_map_dim(bmap, isl_dim_all);
1374 unsigned len = 1 + total;
1376 if (total < 0)
1377 return isl_stat_error;
1379 w = wraps->mat->n_row;
1381 for (l = 0; l < bmap->n_ineq; ++l) {
1382 if (info->ineq[l] == STATUS_VALID ||
1383 info->ineq[l] == STATUS_REDUNDANT)
1384 continue;
1385 if (isl_seq_is_neg(bound, bmap->ineq[l], len))
1386 continue;
1387 if (isl_seq_eq(bound, bmap->ineq[l], len))
1388 continue;
1389 if (isl_tab_is_redundant(info->tab, bmap->n_eq + l))
1390 continue;
1392 added = add_wrap(wraps, w, bound, bmap->ineq[l], len, set, 0);
1393 if (added < 0)
1394 return isl_stat_error;
1395 if (!added)
1396 goto unbounded;
1397 ++w;
1399 for (l = 0; l < bmap->n_eq; ++l) {
1400 if (isl_seq_is_neg(bound, bmap->eq[l], len))
1401 continue;
1402 if (isl_seq_eq(bound, bmap->eq[l], len))
1403 continue;
1405 for (m = 0; m < 2; ++m) {
1406 if (info->eq[2 * l + m] == STATUS_VALID)
1407 continue;
1408 added = add_wrap(wraps, w, bound, bmap->eq[l], len,
1409 set, !m);
1410 if (added < 0)
1411 return isl_stat_error;
1412 if (!added)
1413 goto unbounded;
1414 ++w;
1418 wraps->mat->n_row = w;
1419 return isl_stat_ok;
1420 unbounded:
1421 return wraps_mark_failed(wraps);
1424 /* Check if the constraints in "wraps" from "first" until the last
1425 * are all valid for the basic set represented by "tab".
1426 * If not, wraps->n_row is set to zero.
1428 static int check_wraps(__isl_keep isl_mat *wraps, int first,
1429 struct isl_tab *tab)
1431 int i;
1433 for (i = first; i < wraps->n_row; ++i) {
1434 enum isl_ineq_type type;
1435 type = isl_tab_ineq_type(tab, wraps->row[i]);
1436 if (type == isl_ineq_error)
1437 return -1;
1438 if (type == isl_ineq_redundant)
1439 continue;
1440 wraps->n_row = 0;
1441 return 0;
1444 return 0;
1447 /* Return a set that corresponds to the non-redundant constraints
1448 * (as recorded in tab) of bmap.
1450 * It's important to remove the redundant constraints as some
1451 * of the other constraints may have been modified after the
1452 * constraints were marked redundant.
1453 * In particular, a constraint may have been relaxed.
1454 * Redundant constraints are ignored when a constraint is relaxed
1455 * and should therefore continue to be ignored ever after.
1456 * Otherwise, the relaxation might be thwarted by some of
1457 * these constraints.
1459 * Update the underlying set to ensure that the dimension doesn't change.
1460 * Otherwise the integer divisions could get dropped if the tab
1461 * turns out to be empty.
1463 static __isl_give isl_set *set_from_updated_bmap(__isl_keep isl_basic_map *bmap,
1464 struct isl_tab *tab)
1466 isl_basic_set *bset;
1468 bmap = isl_basic_map_copy(bmap);
1469 bset = isl_basic_map_underlying_set(bmap);
1470 bset = isl_basic_set_cow(bset);
1471 bset = isl_basic_set_update_from_tab(bset, tab);
1472 return isl_set_from_basic_set(bset);
1475 /* Does "info" have any cut constraints that are redundant?
1477 static isl_bool has_redundant_cuts(struct isl_coalesce_info *info)
1479 int l;
1480 isl_size n_eq, n_ineq;
1482 n_eq = isl_basic_map_n_equality(info->bmap);
1483 n_ineq = isl_basic_map_n_inequality(info->bmap);
1484 if (n_eq < 0 || n_ineq < 0)
1485 return isl_bool_error;
1486 for (l = 0; l < n_ineq; ++l) {
1487 int red;
1489 if (info->ineq[l] != STATUS_CUT)
1490 continue;
1491 red = isl_tab_is_redundant(info->tab, n_eq + l);
1492 if (red < 0)
1493 return isl_bool_error;
1494 if (red)
1495 return isl_bool_true;
1498 return isl_bool_false;
1501 /* Wrap the constraints of info->bmap that bound the facet defined
1502 * by inequality "k" around (the opposite of) this inequality to
1503 * include "set". "bound" may be used to store the negated inequality.
1504 * Since the wrapped constraints are not guaranteed to contain the whole
1505 * of info->bmap, we check them in check_wraps.
1506 * If any of the wrapped constraints turn out to be invalid, then
1507 * check_wraps will reset wrap->n_row to zero.
1509 * If any of the cut constraints of info->bmap turn out
1510 * to be redundant with respect to other constraints
1511 * then these will neither be wrapped nor added directly to the result.
1512 * The result may therefore not be correct.
1513 * Skip wrapping and reset wrap->mat->n_row to zero in this case.
1515 static isl_stat add_wraps_around_facet(struct isl_wraps *wraps,
1516 struct isl_coalesce_info *info, int k, isl_int *bound,
1517 __isl_keep isl_set *set)
1519 isl_bool nowrap;
1520 struct isl_tab_undo *snap;
1521 int n;
1522 isl_size total = isl_basic_map_dim(info->bmap, isl_dim_all);
1524 if (total < 0)
1525 return isl_stat_error;
1527 snap = isl_tab_snap(info->tab);
1529 if (isl_tab_select_facet(info->tab, info->bmap->n_eq + k) < 0)
1530 return isl_stat_error;
1531 if (isl_tab_detect_redundant(info->tab) < 0)
1532 return isl_stat_error;
1533 nowrap = has_redundant_cuts(info);
1534 if (nowrap < 0)
1535 return isl_stat_error;
1537 n = wraps->mat->n_row;
1538 if (!nowrap) {
1539 isl_seq_neg(bound, info->bmap->ineq[k], 1 + total);
1541 if (add_wraps(wraps, info, bound, set) < 0)
1542 return isl_stat_error;
1545 if (isl_tab_rollback(info->tab, snap) < 0)
1546 return isl_stat_error;
1547 if (nowrap)
1548 return wraps_mark_failed(wraps);
1549 if (check_wraps(wraps->mat, n, info->tab) < 0)
1550 return isl_stat_error;
1552 return isl_stat_ok;
1555 /* Given a basic set i with a constraint k that is adjacent to
1556 * basic set j, check if we can wrap
1557 * both the facet corresponding to k (if "wrap_facet" is set) and basic map j
1558 * (always) around their ridges to include the other set.
1559 * If so, replace the pair of basic sets by their union.
1561 * All constraints of i (except k) are assumed to be valid or
1562 * cut constraints for j.
1563 * Wrapping the cut constraints to include basic map j may result
1564 * in constraints that are no longer valid of basic map i
1565 * we have to check that the resulting wrapping constraints are valid for i.
1566 * If "wrap_facet" is not set, then all constraints of i (except k)
1567 * are assumed to be valid for j.
1568 * ____ _____
1569 * / | / \
1570 * / || / |
1571 * \ || => \ |
1572 * \ || \ |
1573 * \___|| \____|
1576 static enum isl_change can_wrap_in_facet(int i, int j, int k,
1577 struct isl_coalesce_info *info, int wrap_facet)
1579 enum isl_change change = isl_change_none;
1580 struct isl_wraps wraps;
1581 isl_ctx *ctx;
1582 isl_mat *mat;
1583 struct isl_set *set_i = NULL;
1584 struct isl_set *set_j = NULL;
1585 struct isl_vec *bound = NULL;
1586 isl_size total = isl_basic_map_dim(info[i].bmap, isl_dim_all);
1588 if (total < 0)
1589 return isl_change_error;
1590 set_i = set_from_updated_bmap(info[i].bmap, info[i].tab);
1591 set_j = set_from_updated_bmap(info[j].bmap, info[j].tab);
1592 ctx = isl_basic_map_get_ctx(info[i].bmap);
1593 mat = isl_mat_alloc(ctx, 2 * (info[i].bmap->n_eq + info[j].bmap->n_eq) +
1594 info[i].bmap->n_ineq + info[j].bmap->n_ineq,
1595 1 + total);
1596 if (wraps_init(&wraps, mat, info, i, j) < 0)
1597 goto error;
1598 bound = isl_vec_alloc(ctx, 1 + total);
1599 if (!set_i || !set_j || !bound)
1600 goto error;
1602 isl_seq_cpy(bound->el, info[i].bmap->ineq[k], 1 + total);
1603 isl_int_add_ui(bound->el[0], bound->el[0], 1);
1604 isl_seq_normalize(ctx, bound->el, 1 + total);
1606 isl_seq_cpy(wraps.mat->row[0], bound->el, 1 + total);
1607 wraps.mat->n_row = 1;
1609 if (add_wraps(&wraps, &info[j], bound->el, set_i) < 0)
1610 goto error;
1611 if (!wraps.mat->n_row)
1612 goto unbounded;
1614 if (wrap_facet) {
1615 if (add_wraps_around_facet(&wraps, &info[i], k,
1616 bound->el, set_j) < 0)
1617 goto error;
1618 if (!wraps.mat->n_row)
1619 goto unbounded;
1622 change = fuse(i, j, info, wraps.mat, 0, 0);
1624 unbounded:
1625 wraps_free(&wraps);
1627 isl_set_free(set_i);
1628 isl_set_free(set_j);
1630 isl_vec_free(bound);
1632 return change;
1633 error:
1634 wraps_free(&wraps);
1635 isl_vec_free(bound);
1636 isl_set_free(set_i);
1637 isl_set_free(set_j);
1638 return isl_change_error;
1641 /* Given a cut constraint t(x) >= 0 of basic map i, stored in row "w"
1642 * of wrap.mat, replace it by its relaxed version t(x) + 1 >= 0, and
1643 * add wrapping constraints to wrap.mat for all constraints
1644 * of basic map j that bound the part of basic map j that sticks out
1645 * of the cut constraint.
1646 * "set_i" is the underlying set of basic map i.
1647 * If any wrapping fails, then wraps->mat.n_row is reset to zero.
1649 * In particular, we first intersect basic map j with t(x) + 1 = 0.
1650 * If the result is empty, then t(x) >= 0 was actually a valid constraint
1651 * (with respect to the integer points), so we add t(x) >= 0 instead.
1652 * Otherwise, we wrap the constraints of basic map j that are not
1653 * redundant in this intersection and that are not already valid
1654 * for basic map i over basic map i.
1655 * Note that it is sufficient to wrap the constraints to include
1656 * basic map i, because we will only wrap the constraints that do
1657 * not include basic map i already. The wrapped constraint will
1658 * therefore be more relaxed compared to the original constraint.
1659 * Since the original constraint is valid for basic map j, so is
1660 * the wrapped constraint.
1662 static isl_stat wrap_in_facet(struct isl_wraps *wraps, int w,
1663 struct isl_coalesce_info *info_j, __isl_keep isl_set *set_i,
1664 struct isl_tab_undo *snap)
1666 isl_int_add_ui(wraps->mat->row[w][0], wraps->mat->row[w][0], 1);
1667 if (isl_tab_add_eq(info_j->tab, wraps->mat->row[w]) < 0)
1668 return isl_stat_error;
1669 if (isl_tab_detect_redundant(info_j->tab) < 0)
1670 return isl_stat_error;
1672 if (info_j->tab->empty)
1673 isl_int_sub_ui(wraps->mat->row[w][0], wraps->mat->row[w][0], 1);
1674 else if (add_wraps(wraps, info_j, wraps->mat->row[w], set_i) < 0)
1675 return isl_stat_error;
1677 if (isl_tab_rollback(info_j->tab, snap) < 0)
1678 return isl_stat_error;
1680 return isl_stat_ok;
1683 /* Given a pair of basic maps i and j such that j sticks out
1684 * of i at n cut constraints, each time by at most one,
1685 * try to compute wrapping constraints and replace the two
1686 * basic maps by a single basic map.
1687 * The other constraints of i are assumed to be valid for j.
1688 * "set_i" is the underlying set of basic map i.
1689 * "wraps" has been initialized to be of the right size.
1691 * For each cut constraint t(x) >= 0 of i, we add the relaxed version
1692 * t(x) + 1 >= 0, along with wrapping constraints for all constraints
1693 * of basic map j that bound the part of basic map j that sticks out
1694 * of the cut constraint.
1696 * If any wrapping fails, i.e., if we cannot wrap to touch
1697 * the union, then we give up.
1698 * Otherwise, the pair of basic maps is replaced by their union.
1700 static enum isl_change try_wrap_in_facets(int i, int j,
1701 struct isl_coalesce_info *info, struct isl_wraps *wraps,
1702 __isl_keep isl_set *set_i)
1704 int k, l, w;
1705 isl_size total;
1706 struct isl_tab_undo *snap;
1708 total = isl_basic_map_dim(info[i].bmap, isl_dim_all);
1709 if (total < 0)
1710 return isl_change_error;
1712 snap = isl_tab_snap(info[j].tab);
1714 wraps->mat->n_row = 0;
1716 for (k = 0; k < info[i].bmap->n_eq; ++k) {
1717 for (l = 0; l < 2; ++l) {
1718 if (info[i].eq[2 * k + l] != STATUS_CUT)
1719 continue;
1720 w = wraps->mat->n_row++;
1721 if (l == 0)
1722 isl_seq_neg(wraps->mat->row[w],
1723 info[i].bmap->eq[k], 1 + total);
1724 else
1725 isl_seq_cpy(wraps->mat->row[w],
1726 info[i].bmap->eq[k], 1 + total);
1727 if (wrap_in_facet(wraps, w, &info[j], set_i, snap) < 0)
1728 return isl_change_error;
1730 if (!wraps->mat->n_row)
1731 return isl_change_none;
1735 for (k = 0; k < info[i].bmap->n_ineq; ++k) {
1736 if (info[i].ineq[k] != STATUS_CUT)
1737 continue;
1738 w = wraps->mat->n_row++;
1739 isl_seq_cpy(wraps->mat->row[w],
1740 info[i].bmap->ineq[k], 1 + total);
1741 if (wrap_in_facet(wraps, w, &info[j], set_i, snap) < 0)
1742 return isl_change_error;
1744 if (!wraps->mat->n_row)
1745 return isl_change_none;
1748 return fuse(i, j, info, wraps->mat, 0, 1);
1751 /* Given a pair of basic maps i and j such that j sticks out
1752 * of i at n cut constraints, each time by at most one,
1753 * try to compute wrapping constraints and replace the two
1754 * basic maps by a single basic map.
1755 * The other constraints of i are assumed to be valid for j.
1757 * The core computation is performed by try_wrap_in_facets.
1758 * This function simply extracts an underlying set representation
1759 * of basic map i and initializes the data structure for keeping
1760 * track of wrapping constraints.
1762 static enum isl_change wrap_in_facets(int i, int j, int n,
1763 struct isl_coalesce_info *info)
1765 enum isl_change change = isl_change_none;
1766 struct isl_wraps wraps;
1767 isl_ctx *ctx;
1768 isl_mat *mat;
1769 isl_set *set_i = NULL;
1770 isl_size total = isl_basic_map_dim(info[i].bmap, isl_dim_all);
1771 int max_wrap;
1773 if (total < 0)
1774 return isl_change_error;
1775 if (isl_tab_extend_cons(info[j].tab, 1) < 0)
1776 return isl_change_error;
1778 max_wrap = 1 + 2 * info[j].bmap->n_eq + info[j].bmap->n_ineq;
1779 max_wrap *= n;
1781 set_i = set_from_updated_bmap(info[i].bmap, info[i].tab);
1782 ctx = isl_basic_map_get_ctx(info[i].bmap);
1783 mat = isl_mat_alloc(ctx, max_wrap, 1 + total);
1784 if (wraps_init(&wraps, mat, info, i, j) < 0)
1785 goto error;
1786 if (!set_i)
1787 goto error;
1789 change = try_wrap_in_facets(i, j, info, &wraps, set_i);
1791 wraps_free(&wraps);
1792 isl_set_free(set_i);
1794 return change;
1795 error:
1796 wraps_free(&wraps);
1797 isl_set_free(set_i);
1798 return isl_change_error;
1801 /* Return the effect of inequality "ineq" on the tableau "tab",
1802 * after relaxing the constant term of "ineq" by one.
1804 static enum isl_ineq_type type_of_relaxed(struct isl_tab *tab, isl_int *ineq)
1806 enum isl_ineq_type type;
1808 isl_int_add_ui(ineq[0], ineq[0], 1);
1809 type = isl_tab_ineq_type(tab, ineq);
1810 isl_int_sub_ui(ineq[0], ineq[0], 1);
1812 return type;
1815 /* Given two basic sets i and j,
1816 * check if relaxing all the cut constraints of i by one turns
1817 * them into valid constraint for j and check if we can wrap in
1818 * the bits that are sticking out.
1819 * If so, replace the pair by their union.
1821 * We first check if all relaxed cut inequalities of i are valid for j
1822 * and then try to wrap in the intersections of the relaxed cut inequalities
1823 * with j.
1825 * During this wrapping, we consider the points of j that lie at a distance
1826 * of exactly 1 from i. In particular, we ignore the points that lie in
1827 * between this lower-dimensional space and the basic map i.
1828 * We can therefore only apply this to integer maps.
1829 * ____ _____
1830 * / ___|_ / \
1831 * / | | / |
1832 * \ | | => \ |
1833 * \|____| \ |
1834 * \___| \____/
1836 * _____ ______
1837 * | ____|_ | \
1838 * | | | | |
1839 * | | | => | |
1840 * |_| | | |
1841 * |_____| \______|
1843 * _______
1844 * | |
1845 * | |\ |
1846 * | | \ |
1847 * | | \ |
1848 * | | \|
1849 * | | \
1850 * | |_____\
1851 * | |
1852 * |_______|
1854 * Wrapping can fail if the result of wrapping one of the facets
1855 * around its edges does not produce any new facet constraint.
1856 * In particular, this happens when we try to wrap in unbounded sets.
1858 * _______________________________________________________________________
1860 * | ___
1861 * | | |
1862 * |_| |_________________________________________________________________
1863 * |___|
1865 * The following is not an acceptable result of coalescing the above two
1866 * sets as it includes extra integer points.
1867 * _______________________________________________________________________
1869 * |
1870 * |
1872 * \______________________________________________________________________
1874 static enum isl_change can_wrap_in_set(int i, int j,
1875 struct isl_coalesce_info *info)
1877 int k, l;
1878 int n;
1879 isl_size total;
1881 if (ISL_F_ISSET(info[i].bmap, ISL_BASIC_MAP_RATIONAL) ||
1882 ISL_F_ISSET(info[j].bmap, ISL_BASIC_MAP_RATIONAL))
1883 return isl_change_none;
1885 n = count_eq(&info[i], STATUS_CUT) + count_ineq(&info[i], STATUS_CUT);
1886 if (n == 0)
1887 return isl_change_none;
1889 total = isl_basic_map_dim(info[i].bmap, isl_dim_all);
1890 if (total < 0)
1891 return isl_change_error;
1892 for (k = 0; k < info[i].bmap->n_eq; ++k) {
1893 for (l = 0; l < 2; ++l) {
1894 enum isl_ineq_type type;
1896 if (info[i].eq[2 * k + l] != STATUS_CUT)
1897 continue;
1899 if (l == 0)
1900 isl_seq_neg(info[i].bmap->eq[k],
1901 info[i].bmap->eq[k], 1 + total);
1902 type = type_of_relaxed(info[j].tab,
1903 info[i].bmap->eq[k]);
1904 if (l == 0)
1905 isl_seq_neg(info[i].bmap->eq[k],
1906 info[i].bmap->eq[k], 1 + total);
1907 if (type == isl_ineq_error)
1908 return isl_change_error;
1909 if (type != isl_ineq_redundant)
1910 return isl_change_none;
1914 for (k = 0; k < info[i].bmap->n_ineq; ++k) {
1915 enum isl_ineq_type type;
1917 if (info[i].ineq[k] != STATUS_CUT)
1918 continue;
1920 type = type_of_relaxed(info[j].tab, info[i].bmap->ineq[k]);
1921 if (type == isl_ineq_error)
1922 return isl_change_error;
1923 if (type != isl_ineq_redundant)
1924 return isl_change_none;
1927 return wrap_in_facets(i, j, n, info);
1930 /* Check if either i or j has only cut constraints that can
1931 * be used to wrap in (a facet of) the other basic set.
1932 * if so, replace the pair by their union.
1934 static enum isl_change check_wrap(int i, int j, struct isl_coalesce_info *info)
1936 enum isl_change change = isl_change_none;
1938 change = can_wrap_in_set(i, j, info);
1939 if (change != isl_change_none)
1940 return change;
1942 change = can_wrap_in_set(j, i, info);
1943 return change;
1946 /* Check if all inequality constraints of "i" that cut "j" cease
1947 * to be cut constraints if they are relaxed by one.
1948 * If so, collect the cut constraints in "list".
1949 * The caller is responsible for allocating "list".
1951 static isl_bool all_cut_by_one(int i, int j, struct isl_coalesce_info *info,
1952 int *list)
1954 int l, n;
1956 n = 0;
1957 for (l = 0; l < info[i].bmap->n_ineq; ++l) {
1958 enum isl_ineq_type type;
1960 if (info[i].ineq[l] != STATUS_CUT)
1961 continue;
1962 type = type_of_relaxed(info[j].tab, info[i].bmap->ineq[l]);
1963 if (type == isl_ineq_error)
1964 return isl_bool_error;
1965 if (type != isl_ineq_redundant)
1966 return isl_bool_false;
1967 list[n++] = l;
1970 return isl_bool_true;
1973 /* Given two basic maps such that "j" has at least one equality constraint
1974 * that is adjacent to an inequality constraint of "i" and such that "i" has
1975 * exactly one inequality constraint that is adjacent to an equality
1976 * constraint of "j", check whether "i" can be extended to include "j" or
1977 * whether "j" can be wrapped into "i".
1978 * All remaining constraints of "i" and "j" are assumed to be valid
1979 * or cut constraints of the other basic map.
1980 * However, none of the equality constraints of "i" are cut constraints.
1982 * If "i" has any "cut" inequality constraints, then check if relaxing
1983 * each of them by one is sufficient for them to become valid.
1984 * If so, check if the inequality constraint adjacent to an equality
1985 * constraint of "j" along with all these cut constraints
1986 * can be relaxed by one to contain exactly "j".
1987 * Otherwise, or if this fails, check if "j" can be wrapped into "i".
1989 static enum isl_change check_single_adj_eq(int i, int j,
1990 struct isl_coalesce_info *info)
1992 enum isl_change change = isl_change_none;
1993 int k;
1994 int n_cut;
1995 int *relax;
1996 isl_ctx *ctx;
1997 isl_bool try_relax;
1999 n_cut = count_ineq(&info[i], STATUS_CUT);
2001 k = find_ineq(&info[i], STATUS_ADJ_EQ);
2003 if (n_cut > 0) {
2004 ctx = isl_basic_map_get_ctx(info[i].bmap);
2005 relax = isl_calloc_array(ctx, int, 1 + n_cut);
2006 if (!relax)
2007 return isl_change_error;
2008 relax[0] = k;
2009 try_relax = all_cut_by_one(i, j, info, relax + 1);
2010 if (try_relax < 0)
2011 change = isl_change_error;
2012 } else {
2013 try_relax = isl_bool_true;
2014 relax = &k;
2016 if (try_relax && change == isl_change_none)
2017 change = is_relaxed_extension(i, j, 1 + n_cut, relax, info);
2018 if (n_cut > 0)
2019 free(relax);
2020 if (change != isl_change_none)
2021 return change;
2023 change = can_wrap_in_facet(i, j, k, info, n_cut > 0);
2025 return change;
2028 /* At least one of the basic maps has an equality that is adjacent
2029 * to an inequality. Make sure that only one of the basic maps has
2030 * such an equality and that the other basic map has exactly one
2031 * inequality adjacent to an equality.
2032 * If the other basic map does not have such an inequality, then
2033 * check if all its constraints are either valid or cut constraints
2034 * and, if so, try wrapping in the first map into the second.
2035 * Otherwise, try to extend one basic map with the other or
2036 * wrap one basic map in the other.
2038 static enum isl_change check_adj_eq(int i, int j,
2039 struct isl_coalesce_info *info)
2041 if (any_eq(&info[i], STATUS_ADJ_INEQ) &&
2042 any_eq(&info[j], STATUS_ADJ_INEQ))
2043 /* ADJ EQ TOO MANY */
2044 return isl_change_none;
2046 if (any_eq(&info[i], STATUS_ADJ_INEQ))
2047 return check_adj_eq(j, i, info);
2049 /* j has an equality adjacent to an inequality in i */
2051 if (count_ineq(&info[i], STATUS_ADJ_EQ) != 1) {
2052 if (all_valid_or_cut(&info[i]))
2053 return can_wrap_in_set(i, j, info);
2054 return isl_change_none;
2056 if (any_eq(&info[i], STATUS_CUT))
2057 return isl_change_none;
2058 if (any_ineq(&info[j], STATUS_ADJ_EQ) ||
2059 any_ineq(&info[i], STATUS_ADJ_INEQ) ||
2060 any_ineq(&info[j], STATUS_ADJ_INEQ))
2061 /* ADJ EQ TOO MANY */
2062 return isl_change_none;
2064 return check_single_adj_eq(i, j, info);
2067 /* Disjunct "j" lies on a hyperplane that is adjacent to disjunct "i".
2068 * In particular, disjunct "i" has an inequality constraint that is adjacent
2069 * to a (combination of) equality constraint(s) of disjunct "j",
2070 * but disjunct "j" has no explicit equality constraint adjacent
2071 * to an inequality constraint of disjunct "i".
2073 * Disjunct "i" is already known not to have any equality constraints
2074 * that are adjacent to an equality or inequality constraint.
2075 * Check that, other than the inequality constraint mentioned above,
2076 * all other constraints of disjunct "i" are valid for disjunct "j".
2077 * If so, try and wrap in disjunct "j".
2079 static enum isl_change check_ineq_adj_eq(int i, int j,
2080 struct isl_coalesce_info *info)
2082 int k;
2084 if (any_eq(&info[i], STATUS_CUT))
2085 return isl_change_none;
2086 if (any_ineq(&info[i], STATUS_CUT))
2087 return isl_change_none;
2088 if (any_ineq(&info[i], STATUS_ADJ_INEQ))
2089 return isl_change_none;
2090 if (count_ineq(&info[i], STATUS_ADJ_EQ) != 1)
2091 return isl_change_none;
2093 k = find_ineq(&info[i], STATUS_ADJ_EQ);
2095 return can_wrap_in_facet(i, j, k, info, 0);
2098 /* The two basic maps lie on adjacent hyperplanes. In particular,
2099 * basic map "i" has an equality that lies parallel to basic map "j".
2100 * Check if we can wrap the facets around the parallel hyperplanes
2101 * to include the other set.
2103 * We perform basically the same operations as can_wrap_in_facet,
2104 * except that we don't need to select a facet of one of the sets.
2106 * \\ \\
2107 * \\ => \\
2108 * \ \|
2110 * If there is more than one equality of "i" adjacent to an equality of "j",
2111 * then the result will satisfy one or more equalities that are a linear
2112 * combination of these equalities. These will be encoded as pairs
2113 * of inequalities in the wrapping constraints and need to be made
2114 * explicit.
2116 static enum isl_change check_eq_adj_eq(int i, int j,
2117 struct isl_coalesce_info *info)
2119 int k;
2120 enum isl_change change = isl_change_none;
2121 int detect_equalities = 0;
2122 struct isl_wraps wraps;
2123 isl_ctx *ctx;
2124 isl_mat *mat;
2125 struct isl_set *set_i = NULL;
2126 struct isl_set *set_j = NULL;
2127 struct isl_vec *bound = NULL;
2128 isl_size total = isl_basic_map_dim(info[i].bmap, isl_dim_all);
2130 if (total < 0)
2131 return isl_change_error;
2132 if (count_eq(&info[i], STATUS_ADJ_EQ) != 1)
2133 detect_equalities = 1;
2135 k = find_eq(&info[i], STATUS_ADJ_EQ);
2137 set_i = set_from_updated_bmap(info[i].bmap, info[i].tab);
2138 set_j = set_from_updated_bmap(info[j].bmap, info[j].tab);
2139 ctx = isl_basic_map_get_ctx(info[i].bmap);
2140 mat = isl_mat_alloc(ctx, 2 * (info[i].bmap->n_eq + info[j].bmap->n_eq) +
2141 info[i].bmap->n_ineq + info[j].bmap->n_ineq,
2142 1 + total);
2143 if (wraps_init(&wraps, mat, info, i, j) < 0)
2144 goto error;
2145 bound = isl_vec_alloc(ctx, 1 + total);
2146 if (!set_i || !set_j || !bound)
2147 goto error;
2149 if (k % 2 == 0)
2150 isl_seq_neg(bound->el, info[i].bmap->eq[k / 2], 1 + total);
2151 else
2152 isl_seq_cpy(bound->el, info[i].bmap->eq[k / 2], 1 + total);
2153 isl_int_add_ui(bound->el[0], bound->el[0], 1);
2155 isl_seq_cpy(wraps.mat->row[0], bound->el, 1 + total);
2156 wraps.mat->n_row = 1;
2158 if (add_wraps(&wraps, &info[j], bound->el, set_i) < 0)
2159 goto error;
2160 if (!wraps.mat->n_row)
2161 goto unbounded;
2163 isl_int_sub_ui(bound->el[0], bound->el[0], 1);
2164 isl_seq_neg(bound->el, bound->el, 1 + total);
2166 isl_seq_cpy(wraps.mat->row[wraps.mat->n_row], bound->el, 1 + total);
2167 wraps.mat->n_row++;
2169 if (add_wraps(&wraps, &info[i], bound->el, set_j) < 0)
2170 goto error;
2171 if (!wraps.mat->n_row)
2172 goto unbounded;
2174 change = fuse(i, j, info, wraps.mat, detect_equalities, 0);
2176 if (0) {
2177 error: change = isl_change_error;
2179 unbounded:
2181 wraps_free(&wraps);
2182 isl_set_free(set_i);
2183 isl_set_free(set_j);
2184 isl_vec_free(bound);
2186 return change;
2189 /* Initialize the "eq" and "ineq" fields of "info".
2191 static void init_status(struct isl_coalesce_info *info)
2193 info->eq = info->ineq = NULL;
2196 /* Set info->eq to the positions of the equalities of info->bmap
2197 * with respect to the basic map represented by "tab".
2198 * If info->eq has already been computed, then do not compute it again.
2200 static void set_eq_status_in(struct isl_coalesce_info *info,
2201 struct isl_tab *tab)
2203 if (info->eq)
2204 return;
2205 info->eq = eq_status_in(info->bmap, tab);
2208 /* Set info->ineq to the positions of the inequalities of info->bmap
2209 * with respect to the basic map represented by "tab".
2210 * If info->ineq has already been computed, then do not compute it again.
2212 static void set_ineq_status_in(struct isl_coalesce_info *info,
2213 struct isl_tab *tab)
2215 if (info->ineq)
2216 return;
2217 info->ineq = ineq_status_in(info->bmap, info->tab, tab);
2220 /* Free the memory allocated by the "eq" and "ineq" fields of "info".
2221 * This function assumes that init_status has been called on "info" first,
2222 * after which the "eq" and "ineq" fields may or may not have been
2223 * assigned a newly allocated array.
2225 static void clear_status(struct isl_coalesce_info *info)
2227 free(info->eq);
2228 free(info->ineq);
2231 /* Are all inequality constraints of the basic map represented by "info"
2232 * valid for the other basic map, except for a single constraint
2233 * that is adjacent to an inequality constraint of the other basic map?
2235 static int all_ineq_valid_or_single_adj_ineq(struct isl_coalesce_info *info)
2237 int i;
2238 int k = -1;
2240 for (i = 0; i < info->bmap->n_ineq; ++i) {
2241 if (info->ineq[i] == STATUS_REDUNDANT)
2242 continue;
2243 if (info->ineq[i] == STATUS_VALID)
2244 continue;
2245 if (info->ineq[i] != STATUS_ADJ_INEQ)
2246 return 0;
2247 if (k != -1)
2248 return 0;
2249 k = i;
2252 return k != -1;
2255 /* Basic map "i" has one or more equality constraints that separate it
2256 * from basic map "j". Check if it happens to be an extension
2257 * of basic map "j".
2258 * In particular, check that all constraints of "j" are valid for "i",
2259 * except for one inequality constraint that is adjacent
2260 * to an inequality constraints of "i".
2261 * If so, check for "i" being an extension of "j" by calling
2262 * is_adj_ineq_extension.
2264 * Clean up the memory allocated for keeping track of the status
2265 * of the constraints before returning.
2267 static enum isl_change separating_equality(int i, int j,
2268 struct isl_coalesce_info *info)
2270 enum isl_change change = isl_change_none;
2272 if (all(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_VALID) &&
2273 all_ineq_valid_or_single_adj_ineq(&info[j]))
2274 change = is_adj_ineq_extension(j, i, info);
2276 clear_status(&info[i]);
2277 clear_status(&info[j]);
2278 return change;
2281 /* Check if the union of the given pair of basic maps
2282 * can be represented by a single basic map.
2283 * If so, replace the pair by the single basic map and return
2284 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
2285 * Otherwise, return isl_change_none.
2286 * The two basic maps are assumed to live in the same local space.
2287 * The "eq" and "ineq" fields of info[i] and info[j] are assumed
2288 * to have been initialized by the caller, either to NULL or
2289 * to valid information.
2291 * We first check the effect of each constraint of one basic map
2292 * on the other basic map.
2293 * The constraint may be
2294 * redundant the constraint is redundant in its own
2295 * basic map and should be ignore and removed
2296 * in the end
2297 * valid all (integer) points of the other basic map
2298 * satisfy the constraint
2299 * separate no (integer) point of the other basic map
2300 * satisfies the constraint
2301 * cut some but not all points of the other basic map
2302 * satisfy the constraint
2303 * adj_eq the given constraint is adjacent (on the outside)
2304 * to an equality of the other basic map
2305 * adj_ineq the given constraint is adjacent (on the outside)
2306 * to an inequality of the other basic map
2308 * We consider seven cases in which we can replace the pair by a single
2309 * basic map. We ignore all "redundant" constraints.
2311 * 1. all constraints of one basic map are valid
2312 * => the other basic map is a subset and can be removed
2314 * 2. all constraints of both basic maps are either "valid" or "cut"
2315 * and the facets corresponding to the "cut" constraints
2316 * of one of the basic maps lies entirely inside the other basic map
2317 * => the pair can be replaced by a basic map consisting
2318 * of the valid constraints in both basic maps
2320 * 3. there is a single pair of adjacent inequalities
2321 * (all other constraints are "valid")
2322 * => the pair can be replaced by a basic map consisting
2323 * of the valid constraints in both basic maps
2325 * 4. one basic map has a single adjacent inequality, while the other
2326 * constraints are "valid". The other basic map has some
2327 * "cut" constraints, but replacing the adjacent inequality by
2328 * its opposite and adding the valid constraints of the other
2329 * basic map results in a subset of the other basic map
2330 * => the pair can be replaced by a basic map consisting
2331 * of the valid constraints in both basic maps
2333 * 5. there is a single adjacent pair of an inequality and an equality,
2334 * the other constraints of the basic map containing the inequality are
2335 * "valid". Moreover, if the inequality the basic map is relaxed
2336 * and then turned into an equality, then resulting facet lies
2337 * entirely inside the other basic map
2338 * => the pair can be replaced by the basic map containing
2339 * the inequality, with the inequality relaxed.
2341 * 6. there is a single inequality adjacent to an equality,
2342 * the other constraints of the basic map containing the inequality are
2343 * "valid". Moreover, the facets corresponding to both
2344 * the inequality and the equality can be wrapped around their
2345 * ridges to include the other basic map
2346 * => the pair can be replaced by a basic map consisting
2347 * of the valid constraints in both basic maps together
2348 * with all wrapping constraints
2350 * 7. one of the basic maps extends beyond the other by at most one.
2351 * Moreover, the facets corresponding to the cut constraints and
2352 * the pieces of the other basic map at offset one from these cut
2353 * constraints can be wrapped around their ridges to include
2354 * the union of the two basic maps
2355 * => the pair can be replaced by a basic map consisting
2356 * of the valid constraints in both basic maps together
2357 * with all wrapping constraints
2359 * 8. the two basic maps live in adjacent hyperplanes. In principle
2360 * such sets can always be combined through wrapping, but we impose
2361 * that there is only one such pair, to avoid overeager coalescing.
2363 * Throughout the computation, we maintain a collection of tableaus
2364 * corresponding to the basic maps. When the basic maps are dropped
2365 * or combined, the tableaus are modified accordingly.
2367 static enum isl_change coalesce_local_pair_reuse(int i, int j,
2368 struct isl_coalesce_info *info)
2370 enum isl_change change = isl_change_none;
2372 set_ineq_status_in(&info[i], info[j].tab);
2373 if (info[i].bmap->n_ineq && !info[i].ineq)
2374 goto error;
2375 if (any_ineq(&info[i], STATUS_ERROR))
2376 goto error;
2377 if (any_ineq(&info[i], STATUS_SEPARATE))
2378 goto done;
2380 set_ineq_status_in(&info[j], info[i].tab);
2381 if (info[j].bmap->n_ineq && !info[j].ineq)
2382 goto error;
2383 if (any_ineq(&info[j], STATUS_ERROR))
2384 goto error;
2385 if (any_ineq(&info[j], STATUS_SEPARATE))
2386 goto done;
2388 set_eq_status_in(&info[i], info[j].tab);
2389 if (info[i].bmap->n_eq && !info[i].eq)
2390 goto error;
2391 if (any_eq(&info[i], STATUS_ERROR))
2392 goto error;
2394 set_eq_status_in(&info[j], info[i].tab);
2395 if (info[j].bmap->n_eq && !info[j].eq)
2396 goto error;
2397 if (any_eq(&info[j], STATUS_ERROR))
2398 goto error;
2400 if (any_eq(&info[i], STATUS_SEPARATE))
2401 return separating_equality(i, j, info);
2402 if (any_eq(&info[j], STATUS_SEPARATE))
2403 return separating_equality(j, i, info);
2405 if (all(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_VALID) &&
2406 all(info[i].ineq, info[i].bmap->n_ineq, STATUS_VALID)) {
2407 drop(&info[j]);
2408 change = isl_change_drop_second;
2409 } else if (all(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_VALID) &&
2410 all(info[j].ineq, info[j].bmap->n_ineq, STATUS_VALID)) {
2411 drop(&info[i]);
2412 change = isl_change_drop_first;
2413 } else if (any_eq(&info[i], STATUS_ADJ_EQ)) {
2414 change = check_eq_adj_eq(i, j, info);
2415 } else if (any_eq(&info[j], STATUS_ADJ_EQ)) {
2416 change = check_eq_adj_eq(j, i, info);
2417 } else if (any_eq(&info[i], STATUS_ADJ_INEQ) ||
2418 any_eq(&info[j], STATUS_ADJ_INEQ)) {
2419 change = check_adj_eq(i, j, info);
2420 } else if (any_ineq(&info[i], STATUS_ADJ_EQ)) {
2421 change = check_ineq_adj_eq(i, j, info);
2422 } else if (any_ineq(&info[j], STATUS_ADJ_EQ)) {
2423 change = check_ineq_adj_eq(j, i, info);
2424 } else if (any_ineq(&info[i], STATUS_ADJ_INEQ) ||
2425 any_ineq(&info[j], STATUS_ADJ_INEQ)) {
2426 change = check_adj_ineq(i, j, info);
2427 } else {
2428 if (!any_eq(&info[i], STATUS_CUT) &&
2429 !any_eq(&info[j], STATUS_CUT))
2430 change = check_facets(i, j, info);
2431 if (change == isl_change_none)
2432 change = check_wrap(i, j, info);
2435 done:
2436 clear_status(&info[i]);
2437 clear_status(&info[j]);
2438 return change;
2439 error:
2440 clear_status(&info[i]);
2441 clear_status(&info[j]);
2442 return isl_change_error;
2445 /* Check if the union of the given pair of basic maps
2446 * can be represented by a single basic map.
2447 * If so, replace the pair by the single basic map and return
2448 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
2449 * Otherwise, return isl_change_none.
2450 * The two basic maps are assumed to live in the same local space.
2452 static enum isl_change coalesce_local_pair(int i, int j,
2453 struct isl_coalesce_info *info)
2455 init_status(&info[i]);
2456 init_status(&info[j]);
2457 return coalesce_local_pair_reuse(i, j, info);
2460 /* Shift the integer division at position "div" of the basic map
2461 * represented by "info" by "shift".
2463 * That is, if the integer division has the form
2465 * floor(f(x)/d)
2467 * then replace it by
2469 * floor((f(x) + shift * d)/d) - shift
2471 static isl_stat shift_div(struct isl_coalesce_info *info, int div,
2472 isl_int shift)
2474 isl_size total, n_div;
2476 info->bmap = isl_basic_map_shift_div(info->bmap, div, 0, shift);
2477 if (!info->bmap)
2478 return isl_stat_error;
2480 total = isl_basic_map_dim(info->bmap, isl_dim_all);
2481 n_div = isl_basic_map_dim(info->bmap, isl_dim_div);
2482 if (total < 0 || n_div < 0)
2483 return isl_stat_error;
2484 total -= n_div;
2485 if (isl_tab_shift_var(info->tab, total + div, shift) < 0)
2486 return isl_stat_error;
2488 return isl_stat_ok;
2491 /* If the integer division at position "div" is defined by an equality,
2492 * i.e., a stride constraint, then change the integer division expression
2493 * to have a constant term equal to zero.
2495 * Let the equality constraint be
2497 * c + f + m a = 0
2499 * The integer division expression is then typically of the form
2501 * a = floor((-f - c')/m)
2503 * The integer division is first shifted by t = floor(c/m),
2504 * turning the equality constraint into
2506 * c - m floor(c/m) + f + m a' = 0
2508 * i.e.,
2510 * (c mod m) + f + m a' = 0
2512 * That is,
2514 * a' = (-f - (c mod m))/m = floor((-f)/m)
2516 * because a' is an integer and 0 <= (c mod m) < m.
2517 * The constant term of a' can therefore be zeroed out,
2518 * but only if the integer division expression is of the expected form.
2520 static isl_stat normalize_stride_div(struct isl_coalesce_info *info, int div)
2522 isl_bool defined, valid;
2523 isl_stat r;
2524 isl_constraint *c;
2525 isl_int shift, stride;
2527 defined = isl_basic_map_has_defining_equality(info->bmap, isl_dim_div,
2528 div, &c);
2529 if (defined < 0)
2530 return isl_stat_error;
2531 if (!defined)
2532 return isl_stat_ok;
2533 if (!c)
2534 return isl_stat_error;
2535 valid = isl_constraint_is_div_equality(c, div);
2536 isl_int_init(shift);
2537 isl_int_init(stride);
2538 isl_constraint_get_constant(c, &shift);
2539 isl_constraint_get_coefficient(c, isl_dim_div, div, &stride);
2540 isl_int_fdiv_q(shift, shift, stride);
2541 r = shift_div(info, div, shift);
2542 isl_int_clear(stride);
2543 isl_int_clear(shift);
2544 isl_constraint_free(c);
2545 if (r < 0 || valid < 0)
2546 return isl_stat_error;
2547 if (!valid)
2548 return isl_stat_ok;
2549 info->bmap = isl_basic_map_set_div_expr_constant_num_si_inplace(
2550 info->bmap, div, 0);
2551 if (!info->bmap)
2552 return isl_stat_error;
2553 return isl_stat_ok;
2556 /* The basic maps represented by "info1" and "info2" are known
2557 * to have the same number of integer divisions.
2558 * Check if pairs of integer divisions are equal to each other
2559 * despite the fact that they differ by a rational constant.
2561 * In particular, look for any pair of integer divisions that
2562 * only differ in their constant terms.
2563 * If either of these integer divisions is defined
2564 * by stride constraints, then modify it to have a zero constant term.
2565 * If both are defined by stride constraints then in the end they will have
2566 * the same (zero) constant term.
2568 static isl_stat harmonize_stride_divs(struct isl_coalesce_info *info1,
2569 struct isl_coalesce_info *info2)
2571 int i;
2572 isl_size n;
2574 n = isl_basic_map_dim(info1->bmap, isl_dim_div);
2575 if (n < 0)
2576 return isl_stat_error;
2577 for (i = 0; i < n; ++i) {
2578 isl_bool known, harmonize;
2580 known = isl_basic_map_div_is_known(info1->bmap, i);
2581 if (known >= 0 && known)
2582 known = isl_basic_map_div_is_known(info2->bmap, i);
2583 if (known < 0)
2584 return isl_stat_error;
2585 if (!known)
2586 continue;
2587 harmonize = isl_basic_map_equal_div_expr_except_constant(
2588 info1->bmap, i, info2->bmap, i);
2589 if (harmonize < 0)
2590 return isl_stat_error;
2591 if (!harmonize)
2592 continue;
2593 if (normalize_stride_div(info1, i) < 0)
2594 return isl_stat_error;
2595 if (normalize_stride_div(info2, i) < 0)
2596 return isl_stat_error;
2599 return isl_stat_ok;
2602 /* If "shift" is an integer constant, then shift the integer division
2603 * at position "div" of the basic map represented by "info" by "shift".
2604 * If "shift" is not an integer constant, then do nothing.
2605 * If "shift" is equal to zero, then no shift needs to be performed either.
2607 * That is, if the integer division has the form
2609 * floor(f(x)/d)
2611 * then replace it by
2613 * floor((f(x) + shift * d)/d) - shift
2615 static isl_stat shift_if_cst_int(struct isl_coalesce_info *info, int div,
2616 __isl_keep isl_aff *shift)
2618 isl_bool cst;
2619 isl_stat r;
2620 isl_int d;
2621 isl_val *c;
2623 cst = isl_aff_is_cst(shift);
2624 if (cst < 0 || !cst)
2625 return cst < 0 ? isl_stat_error : isl_stat_ok;
2627 c = isl_aff_get_constant_val(shift);
2628 cst = isl_val_is_int(c);
2629 if (cst >= 0 && cst)
2630 cst = isl_bool_not(isl_val_is_zero(c));
2631 if (cst < 0 || !cst) {
2632 isl_val_free(c);
2633 return cst < 0 ? isl_stat_error : isl_stat_ok;
2636 isl_int_init(d);
2637 r = isl_val_get_num_isl_int(c, &d);
2638 if (r >= 0)
2639 r = shift_div(info, div, d);
2640 isl_int_clear(d);
2642 isl_val_free(c);
2644 return r;
2647 /* Check if some of the divs in the basic map represented by "info1"
2648 * are shifts of the corresponding divs in the basic map represented
2649 * by "info2", taking into account the equality constraints "eq1" of "info1"
2650 * and "eq2" of "info2". If so, align them with those of "info2".
2651 * "info1" and "info2" are assumed to have the same number
2652 * of integer divisions.
2654 * An integer division is considered to be a shift of another integer
2655 * division if, after simplification with respect to the equality
2656 * constraints of the other basic map, one is equal to the other
2657 * plus a constant.
2659 * In particular, for each pair of integer divisions, if both are known,
2660 * have the same denominator and are not already equal to each other,
2661 * simplify each with respect to the equality constraints
2662 * of the other basic map. If the difference is an integer constant,
2663 * then move this difference outside.
2664 * That is, if, after simplification, one integer division is of the form
2666 * floor((f(x) + c_1)/d)
2668 * while the other is of the form
2670 * floor((f(x) + c_2)/d)
2672 * and n = (c_2 - c_1)/d is an integer, then replace the first
2673 * integer division by
2675 * floor((f_1(x) + c_1 + n * d)/d) - n,
2677 * where floor((f_1(x) + c_1 + n * d)/d) = floor((f2(x) + c_2)/d)
2678 * after simplification with respect to the equality constraints.
2680 static isl_stat harmonize_divs_with_hulls(struct isl_coalesce_info *info1,
2681 struct isl_coalesce_info *info2, __isl_keep isl_basic_set *eq1,
2682 __isl_keep isl_basic_set *eq2)
2684 int i;
2685 isl_size total;
2686 isl_local_space *ls1, *ls2;
2688 total = isl_basic_map_dim(info1->bmap, isl_dim_all);
2689 if (total < 0)
2690 return isl_stat_error;
2691 ls1 = isl_local_space_wrap(isl_basic_map_get_local_space(info1->bmap));
2692 ls2 = isl_local_space_wrap(isl_basic_map_get_local_space(info2->bmap));
2693 for (i = 0; i < info1->bmap->n_div; ++i) {
2694 isl_stat r;
2695 isl_aff *div1, *div2;
2697 if (!isl_local_space_div_is_known(ls1, i) ||
2698 !isl_local_space_div_is_known(ls2, i))
2699 continue;
2700 if (isl_int_ne(info1->bmap->div[i][0], info2->bmap->div[i][0]))
2701 continue;
2702 if (isl_seq_eq(info1->bmap->div[i] + 1,
2703 info2->bmap->div[i] + 1, 1 + total))
2704 continue;
2705 div1 = isl_local_space_get_div(ls1, i);
2706 div2 = isl_local_space_get_div(ls2, i);
2707 div1 = isl_aff_substitute_equalities(div1,
2708 isl_basic_set_copy(eq2));
2709 div2 = isl_aff_substitute_equalities(div2,
2710 isl_basic_set_copy(eq1));
2711 div2 = isl_aff_sub(div2, div1);
2712 r = shift_if_cst_int(info1, i, div2);
2713 isl_aff_free(div2);
2714 if (r < 0)
2715 break;
2717 isl_local_space_free(ls1);
2718 isl_local_space_free(ls2);
2720 if (i < info1->bmap->n_div)
2721 return isl_stat_error;
2722 return isl_stat_ok;
2725 /* Check if some of the divs in the basic map represented by "info1"
2726 * are shifts of the corresponding divs in the basic map represented
2727 * by "info2". If so, align them with those of "info2".
2728 * Only do this if "info1" and "info2" have the same number
2729 * of integer divisions.
2731 * An integer division is considered to be a shift of another integer
2732 * division if, after simplification with respect to the equality
2733 * constraints of the other basic map, one is equal to the other
2734 * plus a constant.
2736 * First check if pairs of integer divisions are equal to each other
2737 * despite the fact that they differ by a rational constant.
2738 * If so, try and arrange for them to have the same constant term.
2740 * Then, extract the equality constraints and continue with
2741 * harmonize_divs_with_hulls.
2743 * If the equality constraints of both basic maps are the same,
2744 * then there is no need to perform any shifting since
2745 * the coefficients of the integer divisions should have been
2746 * reduced in the same way.
2748 static isl_stat harmonize_divs(struct isl_coalesce_info *info1,
2749 struct isl_coalesce_info *info2)
2751 isl_bool equal;
2752 isl_basic_map *bmap1, *bmap2;
2753 isl_basic_set *eq1, *eq2;
2754 isl_stat r;
2756 if (!info1->bmap || !info2->bmap)
2757 return isl_stat_error;
2759 if (info1->bmap->n_div != info2->bmap->n_div)
2760 return isl_stat_ok;
2761 if (info1->bmap->n_div == 0)
2762 return isl_stat_ok;
2764 if (harmonize_stride_divs(info1, info2) < 0)
2765 return isl_stat_error;
2767 bmap1 = isl_basic_map_copy(info1->bmap);
2768 bmap2 = isl_basic_map_copy(info2->bmap);
2769 eq1 = isl_basic_map_wrap(isl_basic_map_plain_affine_hull(bmap1));
2770 eq2 = isl_basic_map_wrap(isl_basic_map_plain_affine_hull(bmap2));
2771 equal = isl_basic_set_plain_is_equal(eq1, eq2);
2772 if (equal < 0)
2773 r = isl_stat_error;
2774 else if (equal)
2775 r = isl_stat_ok;
2776 else
2777 r = harmonize_divs_with_hulls(info1, info2, eq1, eq2);
2778 isl_basic_set_free(eq1);
2779 isl_basic_set_free(eq2);
2781 return r;
2784 /* Do the two basic maps live in the same local space, i.e.,
2785 * do they have the same (known) divs?
2786 * If either basic map has any unknown divs, then we can only assume
2787 * that they do not live in the same local space.
2789 static isl_bool same_divs(__isl_keep isl_basic_map *bmap1,
2790 __isl_keep isl_basic_map *bmap2)
2792 int i;
2793 isl_bool known;
2794 isl_size total;
2796 if (!bmap1 || !bmap2)
2797 return isl_bool_error;
2798 if (bmap1->n_div != bmap2->n_div)
2799 return isl_bool_false;
2801 if (bmap1->n_div == 0)
2802 return isl_bool_true;
2804 known = isl_basic_map_divs_known(bmap1);
2805 if (known < 0 || !known)
2806 return known;
2807 known = isl_basic_map_divs_known(bmap2);
2808 if (known < 0 || !known)
2809 return known;
2811 total = isl_basic_map_dim(bmap1, isl_dim_all);
2812 if (total < 0)
2813 return isl_bool_error;
2814 for (i = 0; i < bmap1->n_div; ++i)
2815 if (!isl_seq_eq(bmap1->div[i], bmap2->div[i], 2 + total))
2816 return isl_bool_false;
2818 return isl_bool_true;
2821 /* Assuming that "tab" contains the equality constraints and
2822 * the initial inequality constraints of "bmap", copy the remaining
2823 * inequality constraints of "bmap" to "Tab".
2825 static isl_stat copy_ineq(struct isl_tab *tab, __isl_keep isl_basic_map *bmap)
2827 int i, n_ineq;
2829 if (!bmap)
2830 return isl_stat_error;
2832 n_ineq = tab->n_con - tab->n_eq;
2833 for (i = n_ineq; i < bmap->n_ineq; ++i)
2834 if (isl_tab_add_ineq(tab, bmap->ineq[i]) < 0)
2835 return isl_stat_error;
2837 return isl_stat_ok;
2840 /* Description of an integer division that is added
2841 * during an expansion.
2842 * "pos" is the position of the corresponding variable.
2843 * "cst" indicates whether this integer division has a fixed value.
2844 * "val" contains the fixed value, if the value is fixed.
2846 struct isl_expanded {
2847 int pos;
2848 isl_bool cst;
2849 isl_int val;
2852 /* For each of the "n" integer division variables "expanded",
2853 * if the variable has a fixed value, then add two inequality
2854 * constraints expressing the fixed value.
2855 * Otherwise, add the corresponding div constraints.
2856 * The caller is responsible for removing the div constraints
2857 * that it added for all these "n" integer divisions.
2859 * The div constraints and the pair of inequality constraints
2860 * forcing the fixed value cannot both be added for a given variable
2861 * as the combination may render some of the original constraints redundant.
2862 * These would then be ignored during the coalescing detection,
2863 * while they could remain in the fused result.
2865 * The two added inequality constraints are
2867 * -a + v >= 0
2868 * a - v >= 0
2870 * with "a" the variable and "v" its fixed value.
2871 * The facet corresponding to one of these two constraints is selected
2872 * in the tableau to ensure that the pair of inequality constraints
2873 * is treated as an equality constraint.
2875 * The information in info->ineq is thrown away because it was
2876 * computed in terms of div constraints, while some of those
2877 * have now been replaced by these pairs of inequality constraints.
2879 static isl_stat fix_constant_divs(struct isl_coalesce_info *info,
2880 int n, struct isl_expanded *expanded)
2882 unsigned o_div;
2883 int i;
2884 isl_vec *ineq;
2886 o_div = isl_basic_map_offset(info->bmap, isl_dim_div) - 1;
2887 ineq = isl_vec_alloc(isl_tab_get_ctx(info->tab), 1 + info->tab->n_var);
2888 if (!ineq)
2889 return isl_stat_error;
2890 isl_seq_clr(ineq->el + 1, info->tab->n_var);
2892 for (i = 0; i < n; ++i) {
2893 if (!expanded[i].cst) {
2894 info->bmap = isl_basic_map_extend_constraints(
2895 info->bmap, 0, 2);
2896 info->bmap = isl_basic_map_add_div_constraints(
2897 info->bmap, expanded[i].pos - o_div);
2898 } else {
2899 isl_int_set_si(ineq->el[1 + expanded[i].pos], -1);
2900 isl_int_set(ineq->el[0], expanded[i].val);
2901 info->bmap = isl_basic_map_add_ineq(info->bmap,
2902 ineq->el);
2903 isl_int_set_si(ineq->el[1 + expanded[i].pos], 1);
2904 isl_int_neg(ineq->el[0], expanded[i].val);
2905 info->bmap = isl_basic_map_add_ineq(info->bmap,
2906 ineq->el);
2907 isl_int_set_si(ineq->el[1 + expanded[i].pos], 0);
2909 if (copy_ineq(info->tab, info->bmap) < 0)
2910 break;
2911 if (expanded[i].cst &&
2912 isl_tab_select_facet(info->tab, info->tab->n_con - 1) < 0)
2913 break;
2916 isl_vec_free(ineq);
2918 clear_status(info);
2919 init_status(info);
2921 return i < n ? isl_stat_error : isl_stat_ok;
2924 /* Insert the "n" integer division variables "expanded"
2925 * into info->tab and info->bmap and
2926 * update info->ineq with respect to the redundant constraints
2927 * in the resulting tableau.
2928 * "bmap" contains the result of this insertion in info->bmap,
2929 * while info->bmap is the original version
2930 * of "bmap", i.e., the one that corresponds to the current
2931 * state of info->tab. The number of constraints in info->bmap
2932 * is assumed to be the same as the number of constraints
2933 * in info->tab. This is required to be able to detect
2934 * the extra constraints in "bmap".
2936 * In particular, introduce extra variables corresponding
2937 * to the extra integer divisions and add the div constraints
2938 * that were added to "bmap" after info->tab was created
2939 * from info->bmap.
2940 * Furthermore, check if these extra integer divisions happen
2941 * to attain a fixed integer value in info->tab.
2942 * If so, replace the corresponding div constraints by pairs
2943 * of inequality constraints that fix these
2944 * integer divisions to their single integer values.
2945 * Replace info->bmap by "bmap" to match the changes to info->tab.
2946 * info->ineq was computed without a tableau and therefore
2947 * does not take into account the redundant constraints
2948 * in the tableau. Mark them here.
2949 * There is no need to check the newly added div constraints
2950 * since they cannot be redundant.
2951 * The redundancy check is not performed when constants have been discovered
2952 * since info->ineq is completely thrown away in this case.
2954 static isl_stat tab_insert_divs(struct isl_coalesce_info *info,
2955 int n, struct isl_expanded *expanded, __isl_take isl_basic_map *bmap)
2957 int i, n_ineq;
2958 unsigned n_eq;
2959 struct isl_tab_undo *snap;
2960 int any;
2962 if (!bmap)
2963 return isl_stat_error;
2964 if (info->bmap->n_eq + info->bmap->n_ineq != info->tab->n_con)
2965 isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
2966 "original tableau does not correspond "
2967 "to original basic map", goto error);
2969 if (isl_tab_extend_vars(info->tab, n) < 0)
2970 goto error;
2971 if (isl_tab_extend_cons(info->tab, 2 * n) < 0)
2972 goto error;
2974 for (i = 0; i < n; ++i) {
2975 if (isl_tab_insert_var(info->tab, expanded[i].pos) < 0)
2976 goto error;
2979 snap = isl_tab_snap(info->tab);
2981 n_ineq = info->tab->n_con - info->tab->n_eq;
2982 if (copy_ineq(info->tab, bmap) < 0)
2983 goto error;
2985 isl_basic_map_free(info->bmap);
2986 info->bmap = bmap;
2988 any = 0;
2989 for (i = 0; i < n; ++i) {
2990 expanded[i].cst = isl_tab_is_constant(info->tab,
2991 expanded[i].pos, &expanded[i].val);
2992 if (expanded[i].cst < 0)
2993 return isl_stat_error;
2994 if (expanded[i].cst)
2995 any = 1;
2998 if (any) {
2999 if (isl_tab_rollback(info->tab, snap) < 0)
3000 return isl_stat_error;
3001 info->bmap = isl_basic_map_cow(info->bmap);
3002 info->bmap = isl_basic_map_free_inequality(info->bmap, 2 * n);
3003 if (info->bmap < 0)
3004 return isl_stat_error;
3006 return fix_constant_divs(info, n, expanded);
3009 n_eq = info->bmap->n_eq;
3010 for (i = 0; i < n_ineq; ++i) {
3011 if (isl_tab_is_redundant(info->tab, n_eq + i))
3012 info->ineq[i] = STATUS_REDUNDANT;
3015 return isl_stat_ok;
3016 error:
3017 isl_basic_map_free(bmap);
3018 return isl_stat_error;
3021 /* Expand info->tab and info->bmap in the same way "bmap" was expanded
3022 * in isl_basic_map_expand_divs using the expansion "exp" and
3023 * update info->ineq with respect to the redundant constraints
3024 * in the resulting tableau. info->bmap is the original version
3025 * of "bmap", i.e., the one that corresponds to the current
3026 * state of info->tab. The number of constraints in info->bmap
3027 * is assumed to be the same as the number of constraints
3028 * in info->tab. This is required to be able to detect
3029 * the extra constraints in "bmap".
3031 * Extract the positions where extra local variables are introduced
3032 * from "exp" and call tab_insert_divs.
3034 static isl_stat expand_tab(struct isl_coalesce_info *info, int *exp,
3035 __isl_take isl_basic_map *bmap)
3037 isl_ctx *ctx;
3038 struct isl_expanded *expanded;
3039 int i, j, k, n;
3040 int extra_var;
3041 isl_size total, n_div;
3042 unsigned pos;
3043 isl_stat r;
3045 total = isl_basic_map_dim(bmap, isl_dim_all);
3046 n_div = isl_basic_map_dim(bmap, isl_dim_div);
3047 if (total < 0 || n_div < 0)
3048 return isl_stat_error;
3049 pos = total - n_div;
3050 extra_var = total - info->tab->n_var;
3051 n = n_div - extra_var;
3053 ctx = isl_basic_map_get_ctx(bmap);
3054 expanded = isl_calloc_array(ctx, struct isl_expanded, extra_var);
3055 if (extra_var && !expanded)
3056 goto error;
3058 i = 0;
3059 k = 0;
3060 for (j = 0; j < n_div; ++j) {
3061 if (i < n && exp[i] == j) {
3062 ++i;
3063 continue;
3065 expanded[k++].pos = pos + j;
3068 for (k = 0; k < extra_var; ++k)
3069 isl_int_init(expanded[k].val);
3071 r = tab_insert_divs(info, extra_var, expanded, bmap);
3073 for (k = 0; k < extra_var; ++k)
3074 isl_int_clear(expanded[k].val);
3075 free(expanded);
3077 return r;
3078 error:
3079 isl_basic_map_free(bmap);
3080 return isl_stat_error;
3083 /* Check if the union of the basic maps represented by info[i] and info[j]
3084 * can be represented by a single basic map,
3085 * after expanding the divs of info[i] to match those of info[j].
3086 * If so, replace the pair by the single basic map and return
3087 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
3088 * Otherwise, return isl_change_none.
3090 * The caller has already checked for info[j] being a subset of info[i].
3091 * If some of the divs of info[j] are unknown, then the expanded info[i]
3092 * will not have the corresponding div constraints. The other patterns
3093 * therefore cannot apply. Skip the computation in this case.
3095 * The expansion is performed using the divs "div" and expansion "exp"
3096 * computed by the caller.
3097 * info[i].bmap has already been expanded and the result is passed in
3098 * as "bmap".
3099 * The "eq" and "ineq" fields of info[i] reflect the status of
3100 * the constraints of the expanded "bmap" with respect to info[j].tab.
3101 * However, inequality constraints that are redundant in info[i].tab
3102 * have not yet been marked as such because no tableau was available.
3104 * Replace info[i].bmap by "bmap" and expand info[i].tab as well,
3105 * updating info[i].ineq with respect to the redundant constraints.
3106 * Then try and coalesce the expanded info[i] with info[j],
3107 * reusing the information in info[i].eq and info[i].ineq.
3108 * If this does not result in any coalescing or if it results in info[j]
3109 * getting dropped (which should not happen in practice, since the case
3110 * of info[j] being a subset of info[i] has already been checked by
3111 * the caller), then revert info[i] to its original state.
3113 static enum isl_change coalesce_expand_tab_divs(__isl_take isl_basic_map *bmap,
3114 int i, int j, struct isl_coalesce_info *info, __isl_keep isl_mat *div,
3115 int *exp)
3117 isl_bool known;
3118 isl_basic_map *bmap_i;
3119 struct isl_tab_undo *snap;
3120 enum isl_change change = isl_change_none;
3122 known = isl_basic_map_divs_known(info[j].bmap);
3123 if (known < 0 || !known) {
3124 clear_status(&info[i]);
3125 isl_basic_map_free(bmap);
3126 return known < 0 ? isl_change_error : isl_change_none;
3129 bmap_i = isl_basic_map_copy(info[i].bmap);
3130 snap = isl_tab_snap(info[i].tab);
3131 if (expand_tab(&info[i], exp, bmap) < 0)
3132 change = isl_change_error;
3134 init_status(&info[j]);
3135 if (change == isl_change_none)
3136 change = coalesce_local_pair_reuse(i, j, info);
3137 else
3138 clear_status(&info[i]);
3139 if (change != isl_change_none && change != isl_change_drop_second) {
3140 isl_basic_map_free(bmap_i);
3141 } else {
3142 isl_basic_map_free(info[i].bmap);
3143 info[i].bmap = bmap_i;
3145 if (isl_tab_rollback(info[i].tab, snap) < 0)
3146 change = isl_change_error;
3149 return change;
3152 /* Check if the union of "bmap" and the basic map represented by info[j]
3153 * can be represented by a single basic map,
3154 * after expanding the divs of "bmap" to match those of info[j].
3155 * If so, replace the pair by the single basic map and return
3156 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
3157 * Otherwise, return isl_change_none.
3159 * In particular, check if the expanded "bmap" contains the basic map
3160 * represented by the tableau info[j].tab.
3161 * The expansion is performed using the divs "div" and expansion "exp"
3162 * computed by the caller.
3163 * Then we check if all constraints of the expanded "bmap" are valid for
3164 * info[j].tab.
3166 * If "i" is not equal to -1, then "bmap" is equal to info[i].bmap.
3167 * In this case, the positions of the constraints of info[i].bmap
3168 * with respect to the basic map represented by info[j] are stored
3169 * in info[i].
3171 * If the expanded "bmap" does not contain the basic map
3172 * represented by the tableau info[j].tab and if "i" is not -1,
3173 * i.e., if the original "bmap" is info[i].bmap, then expand info[i].tab
3174 * as well and check if that results in coalescing.
3176 static enum isl_change coalesce_with_expanded_divs(
3177 __isl_keep isl_basic_map *bmap, int i, int j,
3178 struct isl_coalesce_info *info, __isl_keep isl_mat *div, int *exp)
3180 enum isl_change change = isl_change_none;
3181 struct isl_coalesce_info info_local, *info_i;
3183 info_i = i >= 0 ? &info[i] : &info_local;
3184 init_status(info_i);
3185 bmap = isl_basic_map_copy(bmap);
3186 bmap = isl_basic_map_expand_divs(bmap, isl_mat_copy(div), exp);
3187 bmap = isl_basic_map_mark_final(bmap);
3189 if (!bmap)
3190 goto error;
3192 info_local.bmap = bmap;
3193 info_i->eq = eq_status_in(bmap, info[j].tab);
3194 if (bmap->n_eq && !info_i->eq)
3195 goto error;
3196 if (any_eq(info_i, STATUS_ERROR))
3197 goto error;
3198 if (any_eq(info_i, STATUS_SEPARATE))
3199 goto done;
3201 info_i->ineq = ineq_status_in(bmap, NULL, info[j].tab);
3202 if (bmap->n_ineq && !info_i->ineq)
3203 goto error;
3204 if (any_ineq(info_i, STATUS_ERROR))
3205 goto error;
3206 if (any_ineq(info_i, STATUS_SEPARATE))
3207 goto done;
3209 if (all(info_i->eq, 2 * bmap->n_eq, STATUS_VALID) &&
3210 all(info_i->ineq, bmap->n_ineq, STATUS_VALID)) {
3211 drop(&info[j]);
3212 change = isl_change_drop_second;
3215 if (change == isl_change_none && i != -1)
3216 return coalesce_expand_tab_divs(bmap, i, j, info, div, exp);
3218 done:
3219 isl_basic_map_free(bmap);
3220 clear_status(info_i);
3221 return change;
3222 error:
3223 isl_basic_map_free(bmap);
3224 clear_status(info_i);
3225 return isl_change_error;
3228 /* Check if the union of "bmap_i" and the basic map represented by info[j]
3229 * can be represented by a single basic map,
3230 * after aligning the divs of "bmap_i" to match those of info[j].
3231 * If so, replace the pair by the single basic map and return
3232 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
3233 * Otherwise, return isl_change_none.
3235 * In particular, check if "bmap_i" contains the basic map represented by
3236 * info[j] after aligning the divs of "bmap_i" to those of info[j].
3237 * Note that this can only succeed if the number of divs of "bmap_i"
3238 * is smaller than (or equal to) the number of divs of info[j].
3240 * We first check if the divs of "bmap_i" are all known and form a subset
3241 * of those of info[j].bmap. If so, we pass control over to
3242 * coalesce_with_expanded_divs.
3244 * If "i" is not equal to -1, then "bmap" is equal to info[i].bmap.
3246 static enum isl_change coalesce_after_aligning_divs(
3247 __isl_keep isl_basic_map *bmap_i, int i, int j,
3248 struct isl_coalesce_info *info)
3250 isl_bool known;
3251 isl_mat *div_i, *div_j, *div;
3252 int *exp1 = NULL;
3253 int *exp2 = NULL;
3254 isl_ctx *ctx;
3255 enum isl_change change;
3257 known = isl_basic_map_divs_known(bmap_i);
3258 if (known < 0)
3259 return isl_change_error;
3260 if (!known)
3261 return isl_change_none;
3263 ctx = isl_basic_map_get_ctx(bmap_i);
3265 div_i = isl_basic_map_get_divs(bmap_i);
3266 div_j = isl_basic_map_get_divs(info[j].bmap);
3268 if (!div_i || !div_j)
3269 goto error;
3271 exp1 = isl_alloc_array(ctx, int, div_i->n_row);
3272 exp2 = isl_alloc_array(ctx, int, div_j->n_row);
3273 if ((div_i->n_row && !exp1) || (div_j->n_row && !exp2))
3274 goto error;
3276 div = isl_merge_divs(div_i, div_j, exp1, exp2);
3277 if (!div)
3278 goto error;
3280 if (div->n_row == div_j->n_row)
3281 change = coalesce_with_expanded_divs(bmap_i,
3282 i, j, info, div, exp1);
3283 else
3284 change = isl_change_none;
3286 isl_mat_free(div);
3288 isl_mat_free(div_i);
3289 isl_mat_free(div_j);
3291 free(exp2);
3292 free(exp1);
3294 return change;
3295 error:
3296 isl_mat_free(div_i);
3297 isl_mat_free(div_j);
3298 free(exp1);
3299 free(exp2);
3300 return isl_change_error;
3303 /* Check if basic map "j" is a subset of basic map "i" after
3304 * exploiting the extra equalities of "j" to simplify the divs of "i".
3305 * If so, remove basic map "j" and return isl_change_drop_second.
3307 * If "j" does not have any equalities or if they are the same
3308 * as those of "i", then we cannot exploit them to simplify the divs.
3309 * Similarly, if there are no divs in "i", then they cannot be simplified.
3310 * If, on the other hand, the affine hulls of "i" and "j" do not intersect,
3311 * then "j" cannot be a subset of "i".
3313 * Otherwise, we intersect "i" with the affine hull of "j" and then
3314 * check if "j" is a subset of the result after aligning the divs.
3315 * If so, then "j" is definitely a subset of "i" and can be removed.
3316 * Note that if after intersection with the affine hull of "j".
3317 * "i" still has more divs than "j", then there is no way we can
3318 * align the divs of "i" to those of "j".
3320 static enum isl_change coalesce_subset_with_equalities(int i, int j,
3321 struct isl_coalesce_info *info)
3323 isl_basic_map *hull_i, *hull_j, *bmap_i;
3324 int equal, empty;
3325 enum isl_change change;
3327 if (info[j].bmap->n_eq == 0)
3328 return isl_change_none;
3329 if (info[i].bmap->n_div == 0)
3330 return isl_change_none;
3332 hull_i = isl_basic_map_copy(info[i].bmap);
3333 hull_i = isl_basic_map_plain_affine_hull(hull_i);
3334 hull_j = isl_basic_map_copy(info[j].bmap);
3335 hull_j = isl_basic_map_plain_affine_hull(hull_j);
3337 hull_j = isl_basic_map_intersect(hull_j, isl_basic_map_copy(hull_i));
3338 equal = isl_basic_map_plain_is_equal(hull_i, hull_j);
3339 empty = isl_basic_map_plain_is_empty(hull_j);
3340 isl_basic_map_free(hull_i);
3342 if (equal < 0 || equal || empty < 0 || empty) {
3343 isl_basic_map_free(hull_j);
3344 if (equal < 0 || empty < 0)
3345 return isl_change_error;
3346 return isl_change_none;
3349 bmap_i = isl_basic_map_copy(info[i].bmap);
3350 bmap_i = isl_basic_map_intersect(bmap_i, hull_j);
3351 if (!bmap_i)
3352 return isl_change_error;
3354 if (bmap_i->n_div > info[j].bmap->n_div) {
3355 isl_basic_map_free(bmap_i);
3356 return isl_change_none;
3359 change = coalesce_after_aligning_divs(bmap_i, -1, j, info);
3361 isl_basic_map_free(bmap_i);
3363 return change;
3366 /* Check if the union of and the basic maps represented by info[i] and info[j]
3367 * can be represented by a single basic map, by aligning or equating
3368 * their integer divisions.
3369 * If so, replace the pair by the single basic map and return
3370 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
3371 * Otherwise, return isl_change_none.
3373 * Note that we only perform any test if the number of divs is different
3374 * in the two basic maps. In case the number of divs is the same,
3375 * we have already established that the divs are different
3376 * in the two basic maps.
3377 * In particular, if the number of divs of basic map i is smaller than
3378 * the number of divs of basic map j, then we check if j is a subset of i
3379 * and vice versa.
3381 static enum isl_change coalesce_divs(int i, int j,
3382 struct isl_coalesce_info *info)
3384 enum isl_change change = isl_change_none;
3386 if (info[i].bmap->n_div < info[j].bmap->n_div)
3387 change = coalesce_after_aligning_divs(info[i].bmap, i, j, info);
3388 if (change != isl_change_none)
3389 return change;
3391 if (info[j].bmap->n_div < info[i].bmap->n_div)
3392 change = coalesce_after_aligning_divs(info[j].bmap, j, i, info);
3393 if (change != isl_change_none)
3394 return invert_change(change);
3396 change = coalesce_subset_with_equalities(i, j, info);
3397 if (change != isl_change_none)
3398 return change;
3400 change = coalesce_subset_with_equalities(j, i, info);
3401 if (change != isl_change_none)
3402 return invert_change(change);
3404 return isl_change_none;
3407 /* Does "bmap" involve any divs that themselves refer to divs?
3409 static isl_bool has_nested_div(__isl_keep isl_basic_map *bmap)
3411 int i;
3412 isl_size total;
3413 isl_size n_div;
3415 total = isl_basic_map_dim(bmap, isl_dim_all);
3416 n_div = isl_basic_map_dim(bmap, isl_dim_div);
3417 if (total < 0 || n_div < 0)
3418 return isl_bool_error;
3419 total -= n_div;
3421 for (i = 0; i < n_div; ++i)
3422 if (isl_seq_first_non_zero(bmap->div[i] + 2 + total,
3423 n_div) != -1)
3424 return isl_bool_true;
3426 return isl_bool_false;
3429 /* Return a list of affine expressions, one for each integer division
3430 * in "bmap_i". For each integer division that also appears in "bmap_j",
3431 * the affine expression is set to NaN. The number of NaNs in the list
3432 * is equal to the number of integer divisions in "bmap_j".
3433 * For the other integer divisions of "bmap_i", the corresponding
3434 * element in the list is a purely affine expression equal to the integer
3435 * division in "hull".
3436 * If no such list can be constructed, then the number of elements
3437 * in the returned list is smaller than the number of integer divisions
3438 * in "bmap_i".
3440 static __isl_give isl_aff_list *set_up_substitutions(
3441 __isl_keep isl_basic_map *bmap_i, __isl_keep isl_basic_map *bmap_j,
3442 __isl_take isl_basic_map *hull)
3444 isl_size n_div_i, n_div_j, total;
3445 isl_ctx *ctx;
3446 isl_local_space *ls;
3447 isl_basic_set *wrap_hull;
3448 isl_aff *aff_nan;
3449 isl_aff_list *list;
3450 int i, j;
3452 n_div_i = isl_basic_map_dim(bmap_i, isl_dim_div);
3453 n_div_j = isl_basic_map_dim(bmap_j, isl_dim_div);
3454 total = isl_basic_map_dim(bmap_i, isl_dim_all);
3455 if (!hull || n_div_i < 0 || n_div_j < 0 || total < 0)
3456 return NULL;
3458 ctx = isl_basic_map_get_ctx(hull);
3459 total -= n_div_i;
3461 ls = isl_basic_map_get_local_space(bmap_i);
3462 ls = isl_local_space_wrap(ls);
3463 wrap_hull = isl_basic_map_wrap(hull);
3465 aff_nan = isl_aff_nan_on_domain(isl_local_space_copy(ls));
3466 list = isl_aff_list_alloc(ctx, n_div_i);
3468 j = 0;
3469 for (i = 0; i < n_div_i; ++i) {
3470 isl_aff *aff;
3471 isl_size n_div;
3473 if (j < n_div_j &&
3474 isl_basic_map_equal_div_expr_part(bmap_i, i, bmap_j, j,
3475 0, 2 + total)) {
3476 ++j;
3477 list = isl_aff_list_add(list, isl_aff_copy(aff_nan));
3478 continue;
3480 if (n_div_i - i <= n_div_j - j)
3481 break;
3483 aff = isl_local_space_get_div(ls, i);
3484 aff = isl_aff_substitute_equalities(aff,
3485 isl_basic_set_copy(wrap_hull));
3486 aff = isl_aff_floor(aff);
3487 n_div = isl_aff_dim(aff, isl_dim_div);
3488 if (n_div < 0)
3489 goto error;
3490 if (n_div != 0) {
3491 isl_aff_free(aff);
3492 break;
3495 list = isl_aff_list_add(list, aff);
3498 isl_aff_free(aff_nan);
3499 isl_local_space_free(ls);
3500 isl_basic_set_free(wrap_hull);
3502 return list;
3503 error:
3504 isl_aff_free(aff_nan);
3505 isl_local_space_free(ls);
3506 isl_basic_set_free(wrap_hull);
3507 isl_aff_list_free(list);
3508 return NULL;
3511 /* Add variables to info->bmap and info->tab corresponding to the elements
3512 * in "list" that are not set to NaN.
3513 * "extra_var" is the number of these elements.
3514 * "dim" is the offset in the variables of "tab" where we should
3515 * start considering the elements in "list".
3516 * When this function returns, the total number of variables in "tab"
3517 * is equal to "dim" plus the number of elements in "list".
3519 * The newly added existentially quantified variables are not given
3520 * an explicit representation because the corresponding div constraints
3521 * do not appear in info->bmap. These constraints are not added
3522 * to info->bmap because for internal consistency, they would need to
3523 * be added to info->tab as well, where they could combine with the equality
3524 * that is added later to result in constraints that do not hold
3525 * in the original input.
3527 static isl_stat add_sub_vars(struct isl_coalesce_info *info,
3528 __isl_keep isl_aff_list *list, int dim, int extra_var)
3530 int i, j, d;
3531 isl_size n;
3533 info->bmap = isl_basic_map_cow(info->bmap);
3534 info->bmap = isl_basic_map_extend(info->bmap, extra_var, 0, 0);
3535 n = isl_aff_list_n_aff(list);
3536 if (!info->bmap || n < 0)
3537 return isl_stat_error;
3538 for (i = 0; i < n; ++i) {
3539 int is_nan;
3540 isl_aff *aff;
3542 aff = isl_aff_list_get_aff(list, i);
3543 is_nan = isl_aff_is_nan(aff);
3544 isl_aff_free(aff);
3545 if (is_nan < 0)
3546 return isl_stat_error;
3547 if (is_nan)
3548 continue;
3550 if (isl_tab_insert_var(info->tab, dim + i) < 0)
3551 return isl_stat_error;
3552 d = isl_basic_map_alloc_div(info->bmap);
3553 if (d < 0)
3554 return isl_stat_error;
3555 info->bmap = isl_basic_map_mark_div_unknown(info->bmap, d);
3556 for (j = d; j > i; --j)
3557 info->bmap = isl_basic_map_swap_div(info->bmap,
3558 j - 1, j);
3559 if (!info->bmap)
3560 return isl_stat_error;
3563 return isl_stat_ok;
3566 /* For each element in "list" that is not set to NaN, fix the corresponding
3567 * variable in "tab" to the purely affine expression defined by the element.
3568 * "dim" is the offset in the variables of "tab" where we should
3569 * start considering the elements in "list".
3571 * This function assumes that a sufficient number of rows and
3572 * elements in the constraint array are available in the tableau.
3574 static isl_stat add_sub_equalities(struct isl_tab *tab,
3575 __isl_keep isl_aff_list *list, int dim)
3577 int i;
3578 isl_size n;
3579 isl_ctx *ctx;
3580 isl_vec *sub;
3581 isl_aff *aff;
3583 n = isl_aff_list_n_aff(list);
3584 if (n < 0)
3585 return isl_stat_error;
3587 ctx = isl_tab_get_ctx(tab);
3588 sub = isl_vec_alloc(ctx, 1 + dim + n);
3589 if (!sub)
3590 return isl_stat_error;
3591 isl_seq_clr(sub->el + 1 + dim, n);
3593 for (i = 0; i < n; ++i) {
3594 aff = isl_aff_list_get_aff(list, i);
3595 if (!aff)
3596 goto error;
3597 if (isl_aff_is_nan(aff)) {
3598 isl_aff_free(aff);
3599 continue;
3601 isl_seq_cpy(sub->el, aff->v->el + 1, 1 + dim);
3602 isl_int_neg(sub->el[1 + dim + i], aff->v->el[0]);
3603 if (isl_tab_add_eq(tab, sub->el) < 0)
3604 goto error;
3605 isl_int_set_si(sub->el[1 + dim + i], 0);
3606 isl_aff_free(aff);
3609 isl_vec_free(sub);
3610 return isl_stat_ok;
3611 error:
3612 isl_aff_free(aff);
3613 isl_vec_free(sub);
3614 return isl_stat_error;
3617 /* Add variables to info->tab and info->bmap corresponding to the elements
3618 * in "list" that are not set to NaN. The value of the added variable
3619 * in info->tab is fixed to the purely affine expression defined by the element.
3620 * "dim" is the offset in the variables of info->tab where we should
3621 * start considering the elements in "list".
3622 * When this function returns, the total number of variables in info->tab
3623 * is equal to "dim" plus the number of elements in "list".
3625 static isl_stat add_subs(struct isl_coalesce_info *info,
3626 __isl_keep isl_aff_list *list, int dim)
3628 int extra_var;
3629 isl_size n;
3631 n = isl_aff_list_n_aff(list);
3632 if (n < 0)
3633 return isl_stat_error;
3635 extra_var = n - (info->tab->n_var - dim);
3637 if (isl_tab_extend_vars(info->tab, extra_var) < 0)
3638 return isl_stat_error;
3639 if (isl_tab_extend_cons(info->tab, 2 * extra_var) < 0)
3640 return isl_stat_error;
3641 if (add_sub_vars(info, list, dim, extra_var) < 0)
3642 return isl_stat_error;
3644 return add_sub_equalities(info->tab, list, dim);
3647 /* Coalesce basic map "j" into basic map "i" after adding the extra integer
3648 * divisions in "i" but not in "j" to basic map "j", with values
3649 * specified by "list". The total number of elements in "list"
3650 * is equal to the number of integer divisions in "i", while the number
3651 * of NaN elements in the list is equal to the number of integer divisions
3652 * in "j".
3654 * If no coalescing can be performed, then we need to revert basic map "j"
3655 * to its original state. We do the same if basic map "i" gets dropped
3656 * during the coalescing, even though this should not happen in practice
3657 * since we have already checked for "j" being a subset of "i"
3658 * before we reach this stage.
3660 static enum isl_change coalesce_with_subs(int i, int j,
3661 struct isl_coalesce_info *info, __isl_keep isl_aff_list *list)
3663 isl_basic_map *bmap_j;
3664 struct isl_tab_undo *snap;
3665 isl_size dim, n_div;
3666 enum isl_change change;
3668 bmap_j = isl_basic_map_copy(info[j].bmap);
3669 snap = isl_tab_snap(info[j].tab);
3671 dim = isl_basic_map_dim(bmap_j, isl_dim_all);
3672 n_div = isl_basic_map_dim(bmap_j, isl_dim_div);
3673 if (dim < 0 || n_div < 0)
3674 goto error;
3675 dim -= n_div;
3676 if (add_subs(&info[j], list, dim) < 0)
3677 goto error;
3679 change = coalesce_local_pair(i, j, info);
3680 if (change != isl_change_none && change != isl_change_drop_first) {
3681 isl_basic_map_free(bmap_j);
3682 } else {
3683 isl_basic_map_free(info[j].bmap);
3684 info[j].bmap = bmap_j;
3686 if (isl_tab_rollback(info[j].tab, snap) < 0)
3687 return isl_change_error;
3690 return change;
3691 error:
3692 isl_basic_map_free(bmap_j);
3693 return isl_change_error;
3696 /* Check if we can coalesce basic map "j" into basic map "i" after copying
3697 * those extra integer divisions in "i" that can be simplified away
3698 * using the extra equalities in "j".
3699 * All divs are assumed to be known and not contain any nested divs.
3701 * We first check if there are any extra equalities in "j" that we
3702 * can exploit. Then we check if every integer division in "i"
3703 * either already appears in "j" or can be simplified using the
3704 * extra equalities to a purely affine expression.
3705 * If these tests succeed, then we try to coalesce the two basic maps
3706 * by introducing extra dimensions in "j" corresponding to
3707 * the extra integer divisions "i" fixed to the corresponding
3708 * purely affine expression.
3710 static enum isl_change check_coalesce_into_eq(int i, int j,
3711 struct isl_coalesce_info *info)
3713 isl_size n_div_i, n_div_j, n;
3714 isl_basic_map *hull_i, *hull_j;
3715 isl_bool equal, empty;
3716 isl_aff_list *list;
3717 enum isl_change change;
3719 n_div_i = isl_basic_map_dim(info[i].bmap, isl_dim_div);
3720 n_div_j = isl_basic_map_dim(info[j].bmap, isl_dim_div);
3721 if (n_div_i < 0 || n_div_j < 0)
3722 return isl_change_error;
3723 if (n_div_i <= n_div_j)
3724 return isl_change_none;
3725 if (info[j].bmap->n_eq == 0)
3726 return isl_change_none;
3728 hull_i = isl_basic_map_copy(info[i].bmap);
3729 hull_i = isl_basic_map_plain_affine_hull(hull_i);
3730 hull_j = isl_basic_map_copy(info[j].bmap);
3731 hull_j = isl_basic_map_plain_affine_hull(hull_j);
3733 hull_j = isl_basic_map_intersect(hull_j, isl_basic_map_copy(hull_i));
3734 equal = isl_basic_map_plain_is_equal(hull_i, hull_j);
3735 empty = isl_basic_map_plain_is_empty(hull_j);
3736 isl_basic_map_free(hull_i);
3738 if (equal < 0 || empty < 0)
3739 goto error;
3740 if (equal || empty) {
3741 isl_basic_map_free(hull_j);
3742 return isl_change_none;
3745 list = set_up_substitutions(info[i].bmap, info[j].bmap, hull_j);
3746 if (!list)
3747 return isl_change_error;
3748 n = isl_aff_list_n_aff(list);
3749 if (n < 0)
3750 change = isl_change_error;
3751 else if (n < n_div_i)
3752 change = isl_change_none;
3753 else
3754 change = coalesce_with_subs(i, j, info, list);
3756 isl_aff_list_free(list);
3758 return change;
3759 error:
3760 isl_basic_map_free(hull_j);
3761 return isl_change_error;
3764 /* Check if we can coalesce basic maps "i" and "j" after copying
3765 * those extra integer divisions in one of the basic maps that can
3766 * be simplified away using the extra equalities in the other basic map.
3767 * We require all divs to be known in both basic maps.
3768 * Furthermore, to simplify the comparison of div expressions,
3769 * we do not allow any nested integer divisions.
3771 static enum isl_change check_coalesce_eq(int i, int j,
3772 struct isl_coalesce_info *info)
3774 isl_bool known, nested;
3775 enum isl_change change;
3777 known = isl_basic_map_divs_known(info[i].bmap);
3778 if (known < 0 || !known)
3779 return known < 0 ? isl_change_error : isl_change_none;
3780 known = isl_basic_map_divs_known(info[j].bmap);
3781 if (known < 0 || !known)
3782 return known < 0 ? isl_change_error : isl_change_none;
3783 nested = has_nested_div(info[i].bmap);
3784 if (nested < 0 || nested)
3785 return nested < 0 ? isl_change_error : isl_change_none;
3786 nested = has_nested_div(info[j].bmap);
3787 if (nested < 0 || nested)
3788 return nested < 0 ? isl_change_error : isl_change_none;
3790 change = check_coalesce_into_eq(i, j, info);
3791 if (change != isl_change_none)
3792 return change;
3793 change = check_coalesce_into_eq(j, i, info);
3794 if (change != isl_change_none)
3795 return invert_change(change);
3797 return isl_change_none;
3800 /* Check if the union of the given pair of basic maps
3801 * can be represented by a single basic map.
3802 * If so, replace the pair by the single basic map and return
3803 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
3804 * Otherwise, return isl_change_none.
3806 * We first check if the two basic maps live in the same local space,
3807 * after aligning the divs that differ by only an integer constant.
3808 * If so, we do the complete check. Otherwise, we check if they have
3809 * the same number of integer divisions and can be coalesced, if one is
3810 * an obvious subset of the other or if the extra integer divisions
3811 * of one basic map can be simplified away using the extra equalities
3812 * of the other basic map.
3814 * Note that trying to coalesce pairs of disjuncts with the same
3815 * number, but different local variables may drop the explicit
3816 * representation of some of these local variables.
3817 * This operation is therefore not performed when
3818 * the "coalesce_preserve_locals" option is set.
3820 static enum isl_change coalesce_pair(int i, int j,
3821 struct isl_coalesce_info *info)
3823 int preserve;
3824 isl_bool same;
3825 enum isl_change change;
3826 isl_ctx *ctx;
3828 if (harmonize_divs(&info[i], &info[j]) < 0)
3829 return isl_change_error;
3830 same = same_divs(info[i].bmap, info[j].bmap);
3831 if (same < 0)
3832 return isl_change_error;
3833 if (same)
3834 return coalesce_local_pair(i, j, info);
3836 ctx = isl_basic_map_get_ctx(info[i].bmap);
3837 preserve = isl_options_get_coalesce_preserve_locals(ctx);
3838 if (!preserve && info[i].bmap->n_div == info[j].bmap->n_div) {
3839 change = coalesce_local_pair(i, j, info);
3840 if (change != isl_change_none)
3841 return change;
3844 change = coalesce_divs(i, j, info);
3845 if (change != isl_change_none)
3846 return change;
3848 return check_coalesce_eq(i, j, info);
3851 /* Return the maximum of "a" and "b".
3853 static int isl_max(int a, int b)
3855 return a > b ? a : b;
3858 /* Pairwise coalesce the basic maps in the range [start1, end1[ of "info"
3859 * with those in the range [start2, end2[, skipping basic maps
3860 * that have been removed (either before or within this function).
3862 * For each basic map i in the first range, we check if it can be coalesced
3863 * with respect to any previously considered basic map j in the second range.
3864 * If i gets dropped (because it was a subset of some j), then
3865 * we can move on to the next basic map.
3866 * If j gets dropped, we need to continue checking against the other
3867 * previously considered basic maps.
3868 * If the two basic maps got fused, then we recheck the fused basic map
3869 * against the previously considered basic maps, starting at i + 1
3870 * (even if start2 is greater than i + 1).
3872 static int coalesce_range(isl_ctx *ctx, struct isl_coalesce_info *info,
3873 int start1, int end1, int start2, int end2)
3875 int i, j;
3877 for (i = end1 - 1; i >= start1; --i) {
3878 if (info[i].removed)
3879 continue;
3880 for (j = isl_max(i + 1, start2); j < end2; ++j) {
3881 enum isl_change changed;
3883 if (info[j].removed)
3884 continue;
3885 if (info[i].removed)
3886 isl_die(ctx, isl_error_internal,
3887 "basic map unexpectedly removed",
3888 return -1);
3889 changed = coalesce_pair(i, j, info);
3890 switch (changed) {
3891 case isl_change_error:
3892 return -1;
3893 case isl_change_none:
3894 case isl_change_drop_second:
3895 continue;
3896 case isl_change_drop_first:
3897 j = end2;
3898 break;
3899 case isl_change_fuse:
3900 j = i;
3901 break;
3906 return 0;
3909 /* Pairwise coalesce the basic maps described by the "n" elements of "info".
3911 * We consider groups of basic maps that live in the same apparent
3912 * affine hull and we first coalesce within such a group before we
3913 * coalesce the elements in the group with elements of previously
3914 * considered groups. If a fuse happens during the second phase,
3915 * then we also reconsider the elements within the group.
3917 static int coalesce(isl_ctx *ctx, int n, struct isl_coalesce_info *info)
3919 int start, end;
3921 for (end = n; end > 0; end = start) {
3922 start = end - 1;
3923 while (start >= 1 &&
3924 info[start - 1].hull_hash == info[start].hull_hash)
3925 start--;
3926 if (coalesce_range(ctx, info, start, end, start, end) < 0)
3927 return -1;
3928 if (coalesce_range(ctx, info, start, end, end, n) < 0)
3929 return -1;
3932 return 0;
3935 /* Update the basic maps in "map" based on the information in "info".
3936 * In particular, remove the basic maps that have been marked removed and
3937 * update the others based on the information in the corresponding tableau.
3938 * Since we detected implicit equalities without calling
3939 * isl_basic_map_gauss, we need to do it now.
3940 * Also call isl_basic_map_simplify if we may have lost the definition
3941 * of one or more integer divisions.
3942 * If a basic map is still equal to the one from which the corresponding "info"
3943 * entry was created, then redundant constraint and
3944 * implicit equality constraint detection have been performed
3945 * on the corresponding tableau and the basic map can be marked as such.
3947 static __isl_give isl_map *update_basic_maps(__isl_take isl_map *map,
3948 int n, struct isl_coalesce_info *info)
3950 int i;
3952 if (!map)
3953 return NULL;
3955 for (i = n - 1; i >= 0; --i) {
3956 if (info[i].removed) {
3957 isl_basic_map_free(map->p[i]);
3958 if (i != map->n - 1)
3959 map->p[i] = map->p[map->n - 1];
3960 map->n--;
3961 continue;
3964 info[i].bmap = isl_basic_map_update_from_tab(info[i].bmap,
3965 info[i].tab);
3966 info[i].bmap = isl_basic_map_gauss(info[i].bmap, NULL);
3967 if (info[i].simplify)
3968 info[i].bmap = isl_basic_map_simplify(info[i].bmap);
3969 info[i].bmap = isl_basic_map_finalize(info[i].bmap);
3970 if (!info[i].bmap)
3971 return isl_map_free(map);
3972 if (!info[i].modified) {
3973 ISL_F_SET(info[i].bmap, ISL_BASIC_MAP_NO_IMPLICIT);
3974 ISL_F_SET(info[i].bmap, ISL_BASIC_MAP_NO_REDUNDANT);
3976 isl_basic_map_free(map->p[i]);
3977 map->p[i] = info[i].bmap;
3978 info[i].bmap = NULL;
3981 return map;
3984 /* For each pair of basic maps in the map, check if the union of the two
3985 * can be represented by a single basic map.
3986 * If so, replace the pair by the single basic map and start over.
3988 * We factor out any (hidden) common factor from the constraint
3989 * coefficients to improve the detection of adjacent constraints.
3990 * Note that this function does not call isl_basic_map_gauss,
3991 * but it does make sure that only a single copy of the basic map
3992 * is affected. This means that isl_basic_map_gauss may have
3993 * to be called at the end of the computation (in update_basic_maps)
3994 * on this single copy to ensure that
3995 * the basic maps are not left in an unexpected state.
3997 * Since we are constructing the tableaus of the basic maps anyway,
3998 * we exploit them to detect implicit equalities and redundant constraints.
3999 * This also helps the coalescing as it can ignore the redundant constraints.
4000 * In order to avoid confusion, we make all implicit equalities explicit
4001 * in the basic maps. If the basic map only has a single reference
4002 * (this happens in particular if it was modified by
4003 * isl_basic_map_reduce_coefficients), then isl_basic_map_gauss
4004 * does not get called on the result. The call to
4005 * isl_basic_map_gauss in update_basic_maps resolves this as well.
4006 * For each basic map, we also compute the hash of the apparent affine hull
4007 * for use in coalesce.
4009 __isl_give isl_map *isl_map_coalesce(__isl_take isl_map *map)
4011 int i;
4012 unsigned n;
4013 isl_ctx *ctx;
4014 struct isl_coalesce_info *info = NULL;
4016 map = isl_map_remove_empty_parts(map);
4017 if (!map)
4018 return NULL;
4020 if (map->n <= 1)
4021 return map;
4023 ctx = isl_map_get_ctx(map);
4024 map = isl_map_sort_divs(map);
4025 map = isl_map_cow(map);
4027 if (!map)
4028 return NULL;
4030 n = map->n;
4032 info = isl_calloc_array(map->ctx, struct isl_coalesce_info, n);
4033 if (!info)
4034 goto error;
4036 for (i = 0; i < map->n; ++i) {
4037 map->p[i] = isl_basic_map_reduce_coefficients(map->p[i]);
4038 if (!map->p[i])
4039 goto error;
4040 info[i].bmap = isl_basic_map_copy(map->p[i]);
4041 info[i].tab = isl_tab_from_basic_map(info[i].bmap, 0);
4042 if (!info[i].tab)
4043 goto error;
4044 if (!ISL_F_ISSET(info[i].bmap, ISL_BASIC_MAP_NO_IMPLICIT))
4045 if (isl_tab_detect_implicit_equalities(info[i].tab) < 0)
4046 goto error;
4047 info[i].bmap = isl_tab_make_equalities_explicit(info[i].tab,
4048 info[i].bmap);
4049 if (!info[i].bmap)
4050 goto error;
4051 if (!ISL_F_ISSET(info[i].bmap, ISL_BASIC_MAP_NO_REDUNDANT))
4052 if (isl_tab_detect_redundant(info[i].tab) < 0)
4053 goto error;
4054 if (coalesce_info_set_hull_hash(&info[i]) < 0)
4055 goto error;
4057 for (i = map->n - 1; i >= 0; --i)
4058 if (info[i].tab->empty)
4059 drop(&info[i]);
4061 if (coalesce(ctx, n, info) < 0)
4062 goto error;
4064 map = update_basic_maps(map, n, info);
4066 clear_coalesce_info(n, info);
4068 return map;
4069 error:
4070 clear_coalesce_info(n, info);
4071 isl_map_free(map);
4072 return NULL;
4075 /* For each pair of basic sets in the set, check if the union of the two
4076 * can be represented by a single basic set.
4077 * If so, replace the pair by the single basic set and start over.
4079 __isl_give isl_set *isl_set_coalesce(__isl_take isl_set *set)
4081 return set_from_map(isl_map_coalesce(set_to_map(set)));