isl_basic_map_gist: allow existentially quantified variables
[isl.git] / isl_coalesce.c
blob5e2e69b8af8edc2ff9cbaa81213d7b44f7c9c3f8
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
7 * Copyright 2020 Cerebras Systems
9 * Use of this software is governed by the MIT license
11 * Written by Sven Verdoolaege, K.U.Leuven, Departement
12 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
13 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
14 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
15 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
16 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
17 * B.P. 105 - 78153 Le Chesnay, France
18 * and Centre de Recherche Inria de Paris, 2 rue Simone Iff - Voie DQ12,
19 * CS 42112, 75589 Paris Cedex 12, France
20 * and Cerebras Systems, 175 S San Antonio Rd, Los Altos, CA, USA
23 #include <isl_ctx_private.h>
24 #include "isl_map_private.h"
25 #include <isl_seq.h>
26 #include <isl/options.h>
27 #include "isl_tab.h"
28 #include <isl_mat_private.h>
29 #include <isl_local_space_private.h>
30 #include <isl_val_private.h>
31 #include <isl_vec_private.h>
32 #include <isl_aff_private.h>
33 #include <isl_equalities.h>
34 #include <isl_constraint_private.h>
36 #include <set_to_map.c>
37 #include <set_from_map.c>
39 #define STATUS_ERROR -1
40 #define STATUS_REDUNDANT 1
41 #define STATUS_VALID 2
42 #define STATUS_SEPARATE 3
43 #define STATUS_CUT 4
44 #define STATUS_ADJ_EQ 5
45 #define STATUS_ADJ_INEQ 6
47 static int status_in(isl_int *ineq, struct isl_tab *tab)
49 enum isl_ineq_type type = isl_tab_ineq_type(tab, ineq);
50 switch (type) {
51 default:
52 case isl_ineq_error: return STATUS_ERROR;
53 case isl_ineq_redundant: return STATUS_VALID;
54 case isl_ineq_separate: return STATUS_SEPARATE;
55 case isl_ineq_cut: return STATUS_CUT;
56 case isl_ineq_adj_eq: return STATUS_ADJ_EQ;
57 case isl_ineq_adj_ineq: return STATUS_ADJ_INEQ;
61 /* Compute the position of the equalities of basic map "bmap_i"
62 * with respect to the basic map represented by "tab_j".
63 * The resulting array has twice as many entries as the number
64 * of equalities corresponding to the two inequalities to which
65 * each equality corresponds.
67 static int *eq_status_in(__isl_keep isl_basic_map *bmap_i,
68 struct isl_tab *tab_j)
70 int k, l;
71 int *eq;
72 isl_size dim;
74 dim = isl_basic_map_dim(bmap_i, isl_dim_all);
75 if (dim < 0)
76 return NULL;
78 eq = isl_calloc_array(bmap_i->ctx, int, 2 * bmap_i->n_eq);
79 if (!eq)
80 return NULL;
82 for (k = 0; k < bmap_i->n_eq; ++k) {
83 for (l = 0; l < 2; ++l) {
84 isl_seq_neg(bmap_i->eq[k], bmap_i->eq[k], 1+dim);
85 eq[2 * k + l] = status_in(bmap_i->eq[k], tab_j);
86 if (eq[2 * k + l] == STATUS_ERROR)
87 goto error;
91 return eq;
92 error:
93 free(eq);
94 return NULL;
97 /* Compute the position of the inequalities of basic map "bmap_i"
98 * (also represented by "tab_i", if not NULL) with respect to the basic map
99 * represented by "tab_j".
101 static int *ineq_status_in(__isl_keep isl_basic_map *bmap_i,
102 struct isl_tab *tab_i, struct isl_tab *tab_j)
104 int k;
105 unsigned n_eq = bmap_i->n_eq;
106 int *ineq = isl_calloc_array(bmap_i->ctx, int, bmap_i->n_ineq);
108 if (!ineq)
109 return NULL;
111 for (k = 0; k < bmap_i->n_ineq; ++k) {
112 if (tab_i && isl_tab_is_redundant(tab_i, n_eq + k)) {
113 ineq[k] = STATUS_REDUNDANT;
114 continue;
116 ineq[k] = status_in(bmap_i->ineq[k], tab_j);
117 if (ineq[k] == STATUS_ERROR)
118 goto error;
119 if (ineq[k] == STATUS_SEPARATE)
120 break;
123 return ineq;
124 error:
125 free(ineq);
126 return NULL;
129 static int any(int *con, unsigned len, int status)
131 int i;
133 for (i = 0; i < len ; ++i)
134 if (con[i] == status)
135 return 1;
136 return 0;
139 /* Return the first position of "status" in the list "con" of length "len".
140 * Return -1 if there is no such entry.
142 static int find(int *con, unsigned len, int status)
144 int i;
146 for (i = 0; i < len ; ++i)
147 if (con[i] == status)
148 return i;
149 return -1;
152 static int count(int *con, unsigned len, int status)
154 int i;
155 int c = 0;
157 for (i = 0; i < len ; ++i)
158 if (con[i] == status)
159 c++;
160 return c;
163 static int all(int *con, unsigned len, int status)
165 int i;
167 for (i = 0; i < len ; ++i) {
168 if (con[i] == STATUS_REDUNDANT)
169 continue;
170 if (con[i] != status)
171 return 0;
173 return 1;
176 /* Internal information associated to a basic map in a map
177 * that is to be coalesced by isl_map_coalesce.
179 * "bmap" is the basic map itself (or NULL if "removed" is set)
180 * "tab" is the corresponding tableau (or NULL if "removed" is set)
181 * "hull_hash" identifies the affine space in which "bmap" lives.
182 * "modified" is set if this basic map may not be identical
183 * to any of the basic maps in the input.
184 * "removed" is set if this basic map has been removed from the map
185 * "simplify" is set if this basic map may have some unknown integer
186 * divisions that were not present in the input basic maps. The basic
187 * map should then be simplified such that we may be able to find
188 * a definition among the constraints.
190 * "eq" and "ineq" are only set if we are currently trying to coalesce
191 * this basic map with another basic map, in which case they represent
192 * the position of the inequalities of this basic map with respect to
193 * the other basic map. The number of elements in the "eq" array
194 * is twice the number of equalities in the "bmap", corresponding
195 * to the two inequalities that make up each equality.
197 struct isl_coalesce_info {
198 isl_basic_map *bmap;
199 struct isl_tab *tab;
200 uint32_t hull_hash;
201 int modified;
202 int removed;
203 int simplify;
204 int *eq;
205 int *ineq;
208 /* Is there any (half of an) equality constraint in the description
209 * of the basic map represented by "info" that
210 * has position "status" with respect to the other basic map?
212 static int any_eq(struct isl_coalesce_info *info, int status)
214 isl_size n_eq;
216 n_eq = isl_basic_map_n_equality(info->bmap);
217 return any(info->eq, 2 * n_eq, status);
220 /* Is there any inequality constraint in the description
221 * of the basic map represented by "info" that
222 * has position "status" with respect to the other basic map?
224 static int any_ineq(struct isl_coalesce_info *info, int status)
226 isl_size n_ineq;
228 n_ineq = isl_basic_map_n_inequality(info->bmap);
229 return any(info->ineq, n_ineq, status);
232 /* Return the position of the first half on an equality constraint
233 * in the description of the basic map represented by "info" that
234 * has position "status" with respect to the other basic map.
235 * The returned value is twice the position of the equality constraint
236 * plus zero for the negative half and plus one for the positive half.
237 * Return -1 if there is no such entry.
239 static int find_eq(struct isl_coalesce_info *info, int status)
241 isl_size n_eq;
243 n_eq = isl_basic_map_n_equality(info->bmap);
244 return find(info->eq, 2 * n_eq, status);
247 /* Return the position of the first inequality constraint in the description
248 * of the basic map represented by "info" that
249 * has position "status" with respect to the other basic map.
250 * Return -1 if there is no such entry.
252 static int find_ineq(struct isl_coalesce_info *info, int status)
254 isl_size n_ineq;
256 n_ineq = isl_basic_map_n_inequality(info->bmap);
257 return find(info->ineq, n_ineq, status);
260 /* Return the number of (halves of) equality constraints in the description
261 * of the basic map represented by "info" that
262 * have position "status" with respect to the other basic map.
264 static int count_eq(struct isl_coalesce_info *info, int status)
266 isl_size n_eq;
268 n_eq = isl_basic_map_n_equality(info->bmap);
269 return count(info->eq, 2 * n_eq, status);
272 /* Return the number of inequality constraints in the description
273 * of the basic map represented by "info" that
274 * have position "status" with respect to the other basic map.
276 static int count_ineq(struct isl_coalesce_info *info, int status)
278 isl_size n_ineq;
280 n_ineq = isl_basic_map_n_inequality(info->bmap);
281 return count(info->ineq, n_ineq, status);
284 /* Are all non-redundant constraints of the basic map represented by "info"
285 * either valid or cut constraints with respect to the other basic map?
287 static int all_valid_or_cut(struct isl_coalesce_info *info)
289 int i;
291 for (i = 0; i < 2 * info->bmap->n_eq; ++i) {
292 if (info->eq[i] == STATUS_REDUNDANT)
293 continue;
294 if (info->eq[i] == STATUS_VALID)
295 continue;
296 if (info->eq[i] == STATUS_CUT)
297 continue;
298 return 0;
301 for (i = 0; i < info->bmap->n_ineq; ++i) {
302 if (info->ineq[i] == STATUS_REDUNDANT)
303 continue;
304 if (info->ineq[i] == STATUS_VALID)
305 continue;
306 if (info->ineq[i] == STATUS_CUT)
307 continue;
308 return 0;
311 return 1;
314 /* Compute the hash of the (apparent) affine hull of info->bmap (with
315 * the existentially quantified variables removed) and store it
316 * in info->hash.
318 static int coalesce_info_set_hull_hash(struct isl_coalesce_info *info)
320 isl_basic_map *hull;
321 isl_size n_div;
323 hull = isl_basic_map_copy(info->bmap);
324 hull = isl_basic_map_plain_affine_hull(hull);
325 n_div = isl_basic_map_dim(hull, isl_dim_div);
326 if (n_div < 0)
327 hull = isl_basic_map_free(hull);
328 hull = isl_basic_map_drop_constraints_involving_dims(hull,
329 isl_dim_div, 0, n_div);
330 info->hull_hash = isl_basic_map_get_hash(hull);
331 isl_basic_map_free(hull);
333 return hull ? 0 : -1;
336 /* Free all the allocated memory in an array
337 * of "n" isl_coalesce_info elements.
339 static void clear_coalesce_info(int n, struct isl_coalesce_info *info)
341 int i;
343 if (!info)
344 return;
346 for (i = 0; i < n; ++i) {
347 isl_basic_map_free(info[i].bmap);
348 isl_tab_free(info[i].tab);
351 free(info);
354 /* Clear the memory associated to "info".
356 static void clear(struct isl_coalesce_info *info)
358 info->bmap = isl_basic_map_free(info->bmap);
359 isl_tab_free(info->tab);
360 info->tab = NULL;
363 /* Drop the basic map represented by "info".
364 * That is, clear the memory associated to the entry and
365 * mark it as having been removed.
367 static void drop(struct isl_coalesce_info *info)
369 clear(info);
370 info->removed = 1;
373 /* Exchange the information in "info1" with that in "info2".
375 static void exchange(struct isl_coalesce_info *info1,
376 struct isl_coalesce_info *info2)
378 struct isl_coalesce_info info;
380 info = *info1;
381 *info1 = *info2;
382 *info2 = info;
385 /* This type represents the kind of change that has been performed
386 * while trying to coalesce two basic maps.
388 * isl_change_none: nothing was changed
389 * isl_change_drop_first: the first basic map was removed
390 * isl_change_drop_second: the second basic map was removed
391 * isl_change_fuse: the two basic maps were replaced by a new basic map.
393 enum isl_change {
394 isl_change_error = -1,
395 isl_change_none = 0,
396 isl_change_drop_first,
397 isl_change_drop_second,
398 isl_change_fuse,
401 /* Update "change" based on an interchange of the first and the second
402 * basic map. That is, interchange isl_change_drop_first and
403 * isl_change_drop_second.
405 static enum isl_change invert_change(enum isl_change change)
407 switch (change) {
408 case isl_change_error:
409 return isl_change_error;
410 case isl_change_none:
411 return isl_change_none;
412 case isl_change_drop_first:
413 return isl_change_drop_second;
414 case isl_change_drop_second:
415 return isl_change_drop_first;
416 case isl_change_fuse:
417 return isl_change_fuse;
420 return isl_change_error;
423 /* Add the valid constraints of the basic map represented by "info"
424 * to "bmap". "len" is the size of the constraints.
425 * If only one of the pair of inequalities that make up an equality
426 * is valid, then add that inequality.
428 static __isl_give isl_basic_map *add_valid_constraints(
429 __isl_take isl_basic_map *bmap, struct isl_coalesce_info *info,
430 unsigned len)
432 int k, l;
434 if (!bmap)
435 return NULL;
437 for (k = 0; k < info->bmap->n_eq; ++k) {
438 if (info->eq[2 * k] == STATUS_VALID &&
439 info->eq[2 * k + 1] == STATUS_VALID) {
440 l = isl_basic_map_alloc_equality(bmap);
441 if (l < 0)
442 return isl_basic_map_free(bmap);
443 isl_seq_cpy(bmap->eq[l], info->bmap->eq[k], len);
444 } else if (info->eq[2 * k] == STATUS_VALID) {
445 l = isl_basic_map_alloc_inequality(bmap);
446 if (l < 0)
447 return isl_basic_map_free(bmap);
448 isl_seq_neg(bmap->ineq[l], info->bmap->eq[k], len);
449 } else if (info->eq[2 * k + 1] == STATUS_VALID) {
450 l = isl_basic_map_alloc_inequality(bmap);
451 if (l < 0)
452 return isl_basic_map_free(bmap);
453 isl_seq_cpy(bmap->ineq[l], info->bmap->eq[k], len);
457 for (k = 0; k < info->bmap->n_ineq; ++k) {
458 if (info->ineq[k] != STATUS_VALID)
459 continue;
460 l = isl_basic_map_alloc_inequality(bmap);
461 if (l < 0)
462 return isl_basic_map_free(bmap);
463 isl_seq_cpy(bmap->ineq[l], info->bmap->ineq[k], len);
466 return bmap;
469 /* Is "bmap" defined by a number of (non-redundant) constraints that
470 * is greater than the number of constraints of basic maps i and j combined?
471 * Equalities are counted as two inequalities.
473 static int number_of_constraints_increases(int i, int j,
474 struct isl_coalesce_info *info,
475 __isl_keep isl_basic_map *bmap, struct isl_tab *tab)
477 int k, n_old, n_new;
479 n_old = 2 * info[i].bmap->n_eq + info[i].bmap->n_ineq;
480 n_old += 2 * info[j].bmap->n_eq + info[j].bmap->n_ineq;
482 n_new = 2 * bmap->n_eq;
483 for (k = 0; k < bmap->n_ineq; ++k)
484 if (!isl_tab_is_redundant(tab, bmap->n_eq + k))
485 ++n_new;
487 return n_new > n_old;
490 /* Replace the pair of basic maps i and j by the basic map bounded
491 * by the valid constraints in both basic maps and the constraints
492 * in extra (if not NULL).
493 * Place the fused basic map in the position that is the smallest of i and j.
495 * If "detect_equalities" is set, then look for equalities encoded
496 * as pairs of inequalities.
497 * If "check_number" is set, then the original basic maps are only
498 * replaced if the total number of constraints does not increase.
499 * While the number of integer divisions in the two basic maps
500 * is assumed to be the same, the actual definitions may be different.
501 * We only copy the definition from one of the basic maps if it is
502 * the same as that of the other basic map. Otherwise, we mark
503 * the integer division as unknown and simplify the basic map
504 * in an attempt to recover the integer division definition.
505 * If any extra constraints get introduced, then these may
506 * involve integer divisions with a unit coefficient.
507 * Eliminate those that do not appear with any other coefficient
508 * in other constraints, to ensure they get eliminated completely,
509 * improving the chances of further coalescing.
511 * Factor out any (hidden) common factor from the constraint
512 * coefficients of the fused basic map
513 * to improve the detection of adjacent constraints
514 * with respect to other basic maps.
516 static enum isl_change fuse(int i, int j, struct isl_coalesce_info *info,
517 __isl_keep isl_mat *extra, int detect_equalities, int check_number)
519 int k, l;
520 struct isl_basic_map *fused = NULL;
521 struct isl_tab *fused_tab = NULL;
522 isl_size total = isl_basic_map_dim(info[i].bmap, isl_dim_all);
523 unsigned extra_rows = extra ? extra->n_row : 0;
524 unsigned n_eq, n_ineq;
525 int simplify = 0;
527 if (total < 0)
528 return isl_change_error;
529 if (j < i)
530 return fuse(j, i, info, extra, detect_equalities, check_number);
532 n_eq = info[i].bmap->n_eq + info[j].bmap->n_eq;
533 n_ineq = info[i].bmap->n_ineq + info[j].bmap->n_ineq;
534 fused = isl_basic_map_alloc_space(isl_space_copy(info[i].bmap->dim),
535 info[i].bmap->n_div, n_eq, n_eq + n_ineq + extra_rows);
536 fused = add_valid_constraints(fused, &info[i], 1 + total);
537 fused = add_valid_constraints(fused, &info[j], 1 + total);
538 if (!fused)
539 goto error;
540 if (ISL_F_ISSET(info[i].bmap, ISL_BASIC_MAP_RATIONAL) &&
541 ISL_F_ISSET(info[j].bmap, ISL_BASIC_MAP_RATIONAL))
542 ISL_F_SET(fused, ISL_BASIC_MAP_RATIONAL);
544 for (k = 0; k < info[i].bmap->n_div; ++k) {
545 int l = isl_basic_map_alloc_div(fused);
546 if (l < 0)
547 goto error;
548 if (isl_seq_eq(info[i].bmap->div[k], info[j].bmap->div[k],
549 1 + 1 + total)) {
550 isl_seq_cpy(fused->div[l], info[i].bmap->div[k],
551 1 + 1 + total);
552 } else {
553 isl_int_set_si(fused->div[l][0], 0);
554 simplify = 1;
558 for (k = 0; k < extra_rows; ++k) {
559 l = isl_basic_map_alloc_inequality(fused);
560 if (l < 0)
561 goto error;
562 isl_seq_cpy(fused->ineq[l], extra->row[k], 1 + total);
565 if (detect_equalities)
566 fused = isl_basic_map_detect_inequality_pairs(fused, NULL);
567 fused = isl_basic_map_gauss(fused, NULL);
568 if (simplify || info[j].simplify) {
569 fused = isl_basic_map_simplify(fused);
570 info[i].simplify = 0;
571 } else if (extra_rows > 0) {
572 fused = isl_basic_map_eliminate_pure_unit_divs(fused);
574 fused = isl_basic_map_finalize(fused);
575 fused = isl_basic_map_reduce_coefficients(fused);
577 fused_tab = isl_tab_from_basic_map(fused, 0);
578 if (isl_tab_detect_redundant(fused_tab) < 0)
579 goto error;
581 if (check_number &&
582 number_of_constraints_increases(i, j, info, fused, fused_tab)) {
583 isl_tab_free(fused_tab);
584 isl_basic_map_free(fused);
585 return isl_change_none;
588 clear(&info[i]);
589 info[i].bmap = fused;
590 info[i].tab = fused_tab;
591 info[i].modified = 1;
592 drop(&info[j]);
594 return isl_change_fuse;
595 error:
596 isl_tab_free(fused_tab);
597 isl_basic_map_free(fused);
598 return isl_change_error;
601 /* Given a pair of basic maps i and j such that all constraints are either
602 * "valid" or "cut", check if the facets corresponding to the "cut"
603 * constraints of i lie entirely within basic map j.
604 * If so, replace the pair by the basic map consisting of the valid
605 * constraints in both basic maps.
606 * Checking whether the facet lies entirely within basic map j
607 * is performed by checking whether the constraints of basic map j
608 * are valid for the facet. These tests are performed on a rational
609 * tableau to avoid the theoretical possibility that a constraint
610 * that was considered to be a cut constraint for the entire basic map i
611 * happens to be considered to be a valid constraint for the facet,
612 * even though it cuts off the same rational points.
614 * To see that we are not introducing any extra points, call the
615 * two basic maps A and B and the resulting map U and let x
616 * be an element of U \setminus ( A \cup B ).
617 * A line connecting x with an element of A \cup B meets a facet F
618 * of either A or B. Assume it is a facet of B and let c_1 be
619 * the corresponding facet constraint. We have c_1(x) < 0 and
620 * so c_1 is a cut constraint. This implies that there is some
621 * (possibly rational) point x' satisfying the constraints of A
622 * and the opposite of c_1 as otherwise c_1 would have been marked
623 * valid for A. The line connecting x and x' meets a facet of A
624 * in a (possibly rational) point that also violates c_1, but this
625 * is impossible since all cut constraints of B are valid for all
626 * cut facets of A.
627 * In case F is a facet of A rather than B, then we can apply the
628 * above reasoning to find a facet of B separating x from A \cup B first.
630 static enum isl_change check_facets(int i, int j,
631 struct isl_coalesce_info *info)
633 int k, l;
634 struct isl_tab_undo *snap, *snap2;
635 unsigned n_eq = info[i].bmap->n_eq;
637 snap = isl_tab_snap(info[i].tab);
638 if (isl_tab_mark_rational(info[i].tab) < 0)
639 return isl_change_error;
640 snap2 = isl_tab_snap(info[i].tab);
642 for (k = 0; k < info[i].bmap->n_ineq; ++k) {
643 if (info[i].ineq[k] != STATUS_CUT)
644 continue;
645 if (isl_tab_select_facet(info[i].tab, n_eq + k) < 0)
646 return isl_change_error;
647 for (l = 0; l < info[j].bmap->n_ineq; ++l) {
648 int stat;
649 if (info[j].ineq[l] != STATUS_CUT)
650 continue;
651 stat = status_in(info[j].bmap->ineq[l], info[i].tab);
652 if (stat < 0)
653 return isl_change_error;
654 if (stat != STATUS_VALID)
655 break;
657 if (isl_tab_rollback(info[i].tab, snap2) < 0)
658 return isl_change_error;
659 if (l < info[j].bmap->n_ineq)
660 break;
663 if (k < info[i].bmap->n_ineq) {
664 if (isl_tab_rollback(info[i].tab, snap) < 0)
665 return isl_change_error;
666 return isl_change_none;
668 return fuse(i, j, info, NULL, 0, 0);
671 /* Check if info->bmap contains the basic map represented
672 * by the tableau "tab".
673 * For each equality, we check both the constraint itself
674 * (as an inequality) and its negation. Make sure the
675 * equality is returned to its original state before returning.
677 static isl_bool contains(struct isl_coalesce_info *info, struct isl_tab *tab)
679 int k;
680 isl_size dim;
681 isl_basic_map *bmap = info->bmap;
683 dim = isl_basic_map_dim(bmap, isl_dim_all);
684 if (dim < 0)
685 return isl_bool_error;
686 for (k = 0; k < bmap->n_eq; ++k) {
687 int stat;
688 isl_seq_neg(bmap->eq[k], bmap->eq[k], 1 + dim);
689 stat = status_in(bmap->eq[k], tab);
690 isl_seq_neg(bmap->eq[k], bmap->eq[k], 1 + dim);
691 if (stat < 0)
692 return isl_bool_error;
693 if (stat != STATUS_VALID)
694 return isl_bool_false;
695 stat = status_in(bmap->eq[k], tab);
696 if (stat < 0)
697 return isl_bool_error;
698 if (stat != STATUS_VALID)
699 return isl_bool_false;
702 for (k = 0; k < bmap->n_ineq; ++k) {
703 int stat;
704 if (info->ineq[k] == STATUS_REDUNDANT)
705 continue;
706 stat = status_in(bmap->ineq[k], tab);
707 if (stat < 0)
708 return isl_bool_error;
709 if (stat != STATUS_VALID)
710 return isl_bool_false;
712 return isl_bool_true;
715 /* Basic map "i" has an inequality "k" that is adjacent
716 * to some inequality of basic map "j". All the other inequalities
717 * are valid for "j".
718 * If not NULL, then "extra" contains extra wrapping constraints that are valid
719 * for both "i" and "j".
720 * Check if basic map "j" forms an extension of basic map "i",
721 * taking into account the extra constraints, if any.
723 * Note that this function is only called if some of the equalities or
724 * inequalities of basic map "j" do cut basic map "i". The function is
725 * correct even if there are no such cut constraints, but in that case
726 * the additional checks performed by this function are overkill.
728 * In particular, we replace constraint k, say f >= 0, by constraint
729 * f <= -1, add the inequalities of "j" that are valid for "i",
730 * as well as the "extra" constraints, if any,
731 * and check if the result is a subset of basic map "j".
732 * To improve the chances of the subset relation being detected,
733 * any variable that only attains a single integer value
734 * in the tableau of "i" is first fixed to that value.
735 * If the result is a subset, then we know that this result is exactly equal
736 * to basic map "j" since all its constraints are valid for basic map "j".
737 * By combining the valid constraints of "i" (all equalities and all
738 * inequalities except "k"), the valid constraints of "j" and
739 * the "extra" constraints, if any, we therefore
740 * obtain a basic map that is equal to their union.
741 * In this case, there is no need to perform a rollback of the tableau
742 * since it is going to be destroyed in fuse().
745 * |\__ |\__
746 * | \__ | \__
747 * | \_ => | \__
748 * |_______| _ |_________\
751 * |\ |\
752 * | \ | \
753 * | \ | \
754 * | | | \
755 * | ||\ => | \
756 * | || \ | \
757 * | || | | |
758 * |__||_/ |_____/
761 * _______ _______
762 * | | __ | \__
763 * | ||__| => | __|
764 * |_______| |_______/
766 static enum isl_change is_adj_ineq_extension_with_wraps(int i, int j, int k,
767 struct isl_coalesce_info *info, __isl_keep isl_mat *extra)
769 struct isl_tab_undo *snap;
770 isl_size n_eq_i, n_ineq_j, n_extra;
771 isl_size total = isl_basic_map_dim(info[i].bmap, isl_dim_all);
772 isl_stat r;
773 isl_bool super;
775 if (total < 0)
776 return isl_change_error;
778 n_eq_i = isl_basic_map_n_equality(info[i].bmap);
779 n_ineq_j = isl_basic_map_n_inequality(info[j].bmap);
780 n_extra = isl_mat_rows(extra);
781 if (n_eq_i < 0 || n_ineq_j < 0 || n_extra < 0)
782 return isl_change_error;
784 if (isl_tab_extend_cons(info[i].tab, 1 + n_ineq_j + n_extra) < 0)
785 return isl_change_error;
787 snap = isl_tab_snap(info[i].tab);
789 if (isl_tab_unrestrict(info[i].tab, n_eq_i + k) < 0)
790 return isl_change_error;
792 isl_seq_neg(info[i].bmap->ineq[k], info[i].bmap->ineq[k], 1 + total);
793 isl_int_sub_ui(info[i].bmap->ineq[k][0], info[i].bmap->ineq[k][0], 1);
794 r = isl_tab_add_ineq(info[i].tab, info[i].bmap->ineq[k]);
795 isl_seq_neg(info[i].bmap->ineq[k], info[i].bmap->ineq[k], 1 + total);
796 isl_int_sub_ui(info[i].bmap->ineq[k][0], info[i].bmap->ineq[k][0], 1);
797 if (r < 0)
798 return isl_change_error;
800 for (k = 0; k < n_ineq_j; ++k) {
801 if (info[j].ineq[k] != STATUS_VALID)
802 continue;
803 if (isl_tab_add_ineq(info[i].tab, info[j].bmap->ineq[k]) < 0)
804 return isl_change_error;
806 for (k = 0; k < n_extra; ++k) {
807 if (isl_tab_add_ineq(info[i].tab, extra->row[k]) < 0)
808 return isl_change_error;
810 if (isl_tab_detect_constants(info[i].tab) < 0)
811 return isl_change_error;
813 super = contains(&info[j], info[i].tab);
814 if (super < 0)
815 return isl_change_error;
816 if (super)
817 return fuse(i, j, info, extra, 0, 0);
819 if (isl_tab_rollback(info[i].tab, snap) < 0)
820 return isl_change_error;
822 return isl_change_none;
825 /* Given an affine transformation matrix "T", does row "row" represent
826 * anything other than a unit vector (possibly shifted by a constant)
827 * that is not involved in any of the other rows?
829 * That is, if a constraint involves the variable corresponding to
830 * the row, then could its preimage by "T" have any coefficients
831 * that are different from those in the original constraint?
833 static int not_unique_unit_row(__isl_keep isl_mat *T, int row)
835 int i, j;
836 int len = T->n_col - 1;
838 i = isl_seq_first_non_zero(T->row[row] + 1, len);
839 if (i < 0)
840 return 1;
841 if (!isl_int_is_one(T->row[row][1 + i]) &&
842 !isl_int_is_negone(T->row[row][1 + i]))
843 return 1;
845 j = isl_seq_first_non_zero(T->row[row] + 1 + i + 1, len - (i + 1));
846 if (j >= 0)
847 return 1;
849 for (j = 1; j < T->n_row; ++j) {
850 if (j == row)
851 continue;
852 if (!isl_int_is_zero(T->row[j][1 + i]))
853 return 1;
856 return 0;
859 /* Does inequality constraint "ineq" of "bmap" involve any of
860 * the variables marked in "affected"?
861 * "total" is the total number of variables, i.e., the number
862 * of entries in "affected".
864 static isl_bool is_affected(__isl_keep isl_basic_map *bmap, int ineq,
865 int *affected, int total)
867 int i;
869 for (i = 0; i < total; ++i) {
870 if (!affected[i])
871 continue;
872 if (!isl_int_is_zero(bmap->ineq[ineq][1 + i]))
873 return isl_bool_true;
876 return isl_bool_false;
879 /* Given the compressed version of inequality constraint "ineq"
880 * of info->bmap in "v", check if the constraint can be tightened,
881 * where the compression is based on an equality constraint valid
882 * for info->tab.
883 * If so, add the tightened version of the inequality constraint
884 * to info->tab. "v" may be modified by this function.
886 * That is, if the compressed constraint is of the form
888 * m f() + c >= 0
890 * with 0 < c < m, then it is equivalent to
892 * f() >= 0
894 * This means that c can also be subtracted from the original,
895 * uncompressed constraint without affecting the integer points
896 * in info->tab. Add this tightened constraint as an extra row
897 * to info->tab to make this information explicitly available.
899 static __isl_give isl_vec *try_tightening(struct isl_coalesce_info *info,
900 int ineq, __isl_take isl_vec *v)
902 isl_ctx *ctx;
903 isl_stat r;
905 if (!v)
906 return NULL;
908 ctx = isl_vec_get_ctx(v);
909 isl_seq_gcd(v->el + 1, v->size - 1, &ctx->normalize_gcd);
910 if (isl_int_is_zero(ctx->normalize_gcd) ||
911 isl_int_is_one(ctx->normalize_gcd)) {
912 return v;
915 v = isl_vec_cow(v);
916 if (!v)
917 return NULL;
919 isl_int_fdiv_r(v->el[0], v->el[0], ctx->normalize_gcd);
920 if (isl_int_is_zero(v->el[0]))
921 return v;
923 if (isl_tab_extend_cons(info->tab, 1) < 0)
924 return isl_vec_free(v);
926 isl_int_sub(info->bmap->ineq[ineq][0],
927 info->bmap->ineq[ineq][0], v->el[0]);
928 r = isl_tab_add_ineq(info->tab, info->bmap->ineq[ineq]);
929 isl_int_add(info->bmap->ineq[ineq][0],
930 info->bmap->ineq[ineq][0], v->el[0]);
932 if (r < 0)
933 return isl_vec_free(v);
935 return v;
938 /* Tighten the (non-redundant) constraints on the facet represented
939 * by info->tab.
940 * In particular, on input, info->tab represents the result
941 * of relaxing the "n" inequality constraints of info->bmap in "relaxed"
942 * by one, i.e., replacing f_i >= 0 by f_i + 1 >= 0, and then
943 * replacing the one at index "l" by the corresponding equality,
944 * i.e., f_k + 1 = 0, with k = relaxed[l].
946 * Compute a variable compression from the equality constraint f_k + 1 = 0
947 * and use it to tighten the other constraints of info->bmap
948 * (that is, all constraints that have not been relaxed),
949 * updating info->tab (and leaving info->bmap untouched).
950 * The compression handles essentially two cases, one where a variable
951 * is assigned a fixed value and can therefore be eliminated, and one
952 * where one variable is a shifted multiple of some other variable and
953 * can therefore be replaced by that multiple.
954 * Gaussian elimination would also work for the first case, but for
955 * the second case, the effectiveness would depend on the order
956 * of the variables.
957 * After compression, some of the constraints may have coefficients
958 * with a common divisor. If this divisor does not divide the constant
959 * term, then the constraint can be tightened.
960 * The tightening is performed on the tableau info->tab by introducing
961 * extra (temporary) constraints.
963 * Only constraints that are possibly affected by the compression are
964 * considered. In particular, if the constraint only involves variables
965 * that are directly mapped to a distinct set of other variables, then
966 * no common divisor can be introduced and no tightening can occur.
968 * It is important to only consider the non-redundant constraints
969 * since the facet constraint has been relaxed prior to the call
970 * to this function, meaning that the constraints that were redundant
971 * prior to the relaxation may no longer be redundant.
972 * These constraints will be ignored in the fused result, so
973 * the fusion detection should not exploit them.
975 static isl_stat tighten_on_relaxed_facet(struct isl_coalesce_info *info,
976 int n, int *relaxed, int l)
978 isl_size total;
979 isl_ctx *ctx;
980 isl_vec *v = NULL;
981 isl_mat *T;
982 int i;
983 int k;
984 int *affected;
986 k = relaxed[l];
987 ctx = isl_basic_map_get_ctx(info->bmap);
988 total = isl_basic_map_dim(info->bmap, isl_dim_all);
989 if (total < 0)
990 return isl_stat_error;
991 isl_int_add_ui(info->bmap->ineq[k][0], info->bmap->ineq[k][0], 1);
992 T = isl_mat_sub_alloc6(ctx, info->bmap->ineq, k, 1, 0, 1 + total);
993 T = isl_mat_variable_compression(T, NULL);
994 isl_int_sub_ui(info->bmap->ineq[k][0], info->bmap->ineq[k][0], 1);
995 if (!T)
996 return isl_stat_error;
997 if (T->n_col == 0) {
998 isl_mat_free(T);
999 return isl_stat_ok;
1002 affected = isl_alloc_array(ctx, int, total);
1003 if (!affected)
1004 goto error;
1006 for (i = 0; i < total; ++i)
1007 affected[i] = not_unique_unit_row(T, 1 + i);
1009 for (i = 0; i < info->bmap->n_ineq; ++i) {
1010 isl_bool handle;
1011 if (any(relaxed, n, i))
1012 continue;
1013 if (info->ineq[i] == STATUS_REDUNDANT)
1014 continue;
1015 handle = is_affected(info->bmap, i, affected, total);
1016 if (handle < 0)
1017 goto error;
1018 if (!handle)
1019 continue;
1020 v = isl_vec_alloc(ctx, 1 + total);
1021 if (!v)
1022 goto error;
1023 isl_seq_cpy(v->el, info->bmap->ineq[i], 1 + total);
1024 v = isl_vec_mat_product(v, isl_mat_copy(T));
1025 v = try_tightening(info, i, v);
1026 isl_vec_free(v);
1027 if (!v)
1028 goto error;
1031 isl_mat_free(T);
1032 free(affected);
1033 return isl_stat_ok;
1034 error:
1035 isl_mat_free(T);
1036 free(affected);
1037 return isl_stat_error;
1040 /* Replace the basic maps "i" and "j" by an extension of "i"
1041 * along the "n" inequality constraints in "relax" by one.
1042 * The tableau info[i].tab has already been extended.
1043 * Extend info[i].bmap accordingly by relaxing all constraints in "relax"
1044 * by one.
1045 * Each integer division that does not have exactly the same
1046 * definition in "i" and "j" is marked unknown and the basic map
1047 * is scheduled to be simplified in an attempt to recover
1048 * the integer division definition.
1049 * Place the extension in the position that is the smallest of i and j.
1051 static enum isl_change extend(int i, int j, int n, int *relax,
1052 struct isl_coalesce_info *info)
1054 int l;
1055 isl_size total;
1057 info[i].bmap = isl_basic_map_cow(info[i].bmap);
1058 total = isl_basic_map_dim(info[i].bmap, isl_dim_all);
1059 if (total < 0)
1060 return isl_change_error;
1061 for (l = 0; l < info[i].bmap->n_div; ++l)
1062 if (!isl_seq_eq(info[i].bmap->div[l],
1063 info[j].bmap->div[l], 1 + 1 + total)) {
1064 isl_int_set_si(info[i].bmap->div[l][0], 0);
1065 info[i].simplify = 1;
1067 for (l = 0; l < n; ++l)
1068 isl_int_add_ui(info[i].bmap->ineq[relax[l]][0],
1069 info[i].bmap->ineq[relax[l]][0], 1);
1070 ISL_F_CLR(info[i].bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1071 ISL_F_SET(info[i].bmap, ISL_BASIC_MAP_FINAL);
1072 drop(&info[j]);
1073 info[i].modified = 1;
1074 if (j < i)
1075 exchange(&info[i], &info[j]);
1076 return isl_change_fuse;
1079 /* Basic map "i" has "n" inequality constraints (collected in "relax")
1080 * that are such that they include basic map "j" if they are relaxed
1081 * by one. All the other inequalities are valid for "j".
1082 * Check if basic map "j" forms an extension of basic map "i".
1084 * In particular, relax the constraints in "relax", compute the corresponding
1085 * facets one by one and check whether each of these is included
1086 * in the other basic map.
1087 * Before testing for inclusion, the constraints on each facet
1088 * are tightened to increase the chance of an inclusion being detected.
1089 * (Adding the valid constraints of "j" to the tableau of "i", as is done
1090 * in is_adj_ineq_extension, may further increase those chances, but this
1091 * is not currently done.)
1092 * If each facet is included, we know that relaxing the constraints extends
1093 * the basic map with exactly the other basic map (we already know that this
1094 * other basic map is included in the extension, because all other
1095 * inequality constraints are valid of "j") and we can replace the
1096 * two basic maps by this extension.
1098 * If any of the relaxed constraints turn out to be redundant, then bail out.
1099 * isl_tab_select_facet refuses to handle such constraints. It may be
1100 * possible to handle them anyway by making a distinction between
1101 * redundant constraints with a corresponding facet that still intersects
1102 * the set (allowing isl_tab_select_facet to handle them) and
1103 * those where the facet does not intersect the set (which can be ignored
1104 * because the empty facet is trivially included in the other disjunct).
1105 * However, relaxed constraints that turn out to be redundant should
1106 * be fairly rare and no such instance has been reported where
1107 * coalescing would be successful.
1108 * ____ _____
1109 * / || / |
1110 * / || / |
1111 * \ || => \ |
1112 * \ || \ |
1113 * \___|| \____|
1116 * \ |\
1117 * |\\ | \
1118 * | \\ | \
1119 * | | => | /
1120 * | / | /
1121 * |/ |/
1123 static enum isl_change is_relaxed_extension(int i, int j, int n, int *relax,
1124 struct isl_coalesce_info *info)
1126 int l;
1127 isl_bool super;
1128 struct isl_tab_undo *snap, *snap2;
1129 unsigned n_eq = info[i].bmap->n_eq;
1131 for (l = 0; l < n; ++l)
1132 if (isl_tab_is_equality(info[i].tab, n_eq + relax[l]))
1133 return isl_change_none;
1135 snap = isl_tab_snap(info[i].tab);
1136 for (l = 0; l < n; ++l)
1137 if (isl_tab_relax(info[i].tab, n_eq + relax[l]) < 0)
1138 return isl_change_error;
1139 for (l = 0; l < n; ++l) {
1140 if (!isl_tab_is_redundant(info[i].tab, n_eq + relax[l]))
1141 continue;
1142 if (isl_tab_rollback(info[i].tab, snap) < 0)
1143 return isl_change_error;
1144 return isl_change_none;
1146 snap2 = isl_tab_snap(info[i].tab);
1147 for (l = 0; l < n; ++l) {
1148 if (isl_tab_rollback(info[i].tab, snap2) < 0)
1149 return isl_change_error;
1150 if (isl_tab_select_facet(info[i].tab, n_eq + relax[l]) < 0)
1151 return isl_change_error;
1152 if (tighten_on_relaxed_facet(&info[i], n, relax, l) < 0)
1153 return isl_change_error;
1154 super = contains(&info[j], info[i].tab);
1155 if (super < 0)
1156 return isl_change_error;
1157 if (super)
1158 continue;
1159 if (isl_tab_rollback(info[i].tab, snap) < 0)
1160 return isl_change_error;
1161 return isl_change_none;
1164 if (isl_tab_rollback(info[i].tab, snap2) < 0)
1165 return isl_change_error;
1166 return extend(i, j, n, relax, info);
1169 /* Data structure that keeps track of the wrapping constraints
1170 * and of information to bound the coefficients of those constraints.
1172 * "failed" is set if wrapping has failed.
1173 * bound is set if we want to apply a bound on the coefficients
1174 * mat contains the wrapping constraints
1175 * max is the bound on the coefficients (if bound is set)
1177 struct isl_wraps {
1178 int failed;
1179 int bound;
1180 isl_mat *mat;
1181 isl_int max;
1184 /* Update wraps->max to be greater than or equal to the coefficients
1185 * in the equalities and inequalities of info->bmap that can be removed
1186 * if we end up applying wrapping.
1188 static isl_stat wraps_update_max(struct isl_wraps *wraps,
1189 struct isl_coalesce_info *info)
1191 int k;
1192 isl_int max_k;
1193 isl_size total = isl_basic_map_dim(info->bmap, isl_dim_all);
1195 if (total < 0)
1196 return isl_stat_error;
1197 isl_int_init(max_k);
1199 for (k = 0; k < info->bmap->n_eq; ++k) {
1200 if (info->eq[2 * k] == STATUS_VALID &&
1201 info->eq[2 * k + 1] == STATUS_VALID)
1202 continue;
1203 isl_seq_abs_max(info->bmap->eq[k] + 1, total, &max_k);
1204 if (isl_int_abs_gt(max_k, wraps->max))
1205 isl_int_set(wraps->max, max_k);
1208 for (k = 0; k < info->bmap->n_ineq; ++k) {
1209 if (info->ineq[k] == STATUS_VALID ||
1210 info->ineq[k] == STATUS_REDUNDANT)
1211 continue;
1212 isl_seq_abs_max(info->bmap->ineq[k] + 1, total, &max_k);
1213 if (isl_int_abs_gt(max_k, wraps->max))
1214 isl_int_set(wraps->max, max_k);
1217 isl_int_clear(max_k);
1219 return isl_stat_ok;
1222 /* Initialize the isl_wraps data structure.
1223 * If we want to bound the coefficients of the wrapping constraints,
1224 * we set wraps->max to the largest coefficient
1225 * in the equalities and inequalities that can be removed if we end up
1226 * applying wrapping.
1228 static isl_stat wraps_init(struct isl_wraps *wraps, __isl_take isl_mat *mat,
1229 struct isl_coalesce_info *info, int i, int j)
1231 isl_ctx *ctx;
1233 wraps->failed = 0;
1234 wraps->bound = 0;
1235 wraps->mat = mat;
1236 if (!mat)
1237 return isl_stat_error;
1238 wraps->mat->n_row = 0;
1239 ctx = isl_mat_get_ctx(mat);
1240 wraps->bound = isl_options_get_coalesce_bounded_wrapping(ctx);
1241 if (!wraps->bound)
1242 return isl_stat_ok;
1243 isl_int_init(wraps->max);
1244 isl_int_set_si(wraps->max, 0);
1245 if (wraps_update_max(wraps, &info[i]) < 0)
1246 return isl_stat_error;
1247 if (wraps_update_max(wraps, &info[j]) < 0)
1248 return isl_stat_error;
1250 return isl_stat_ok;
1253 /* Free the contents of the isl_wraps data structure.
1255 static void wraps_free(struct isl_wraps *wraps)
1257 isl_mat_free(wraps->mat);
1258 if (wraps->bound)
1259 isl_int_clear(wraps->max);
1262 /* Mark the wrapping as failed.
1264 static isl_stat wraps_mark_failed(struct isl_wraps *wraps)
1266 wraps->failed = 1;
1267 return isl_stat_ok;
1270 /* Is the wrapping constraint in row "row" allowed?
1272 * If wraps->bound is set, we check that none of the coefficients
1273 * is greater than wraps->max.
1275 static int allow_wrap(struct isl_wraps *wraps, int row)
1277 int i;
1279 if (!wraps->bound)
1280 return 1;
1282 for (i = 1; i < wraps->mat->n_col; ++i)
1283 if (isl_int_abs_gt(wraps->mat->row[row][i], wraps->max))
1284 return 0;
1286 return 1;
1289 /* Wrap "ineq" (or its opposite if "negate" is set) around "bound"
1290 * to include "set" and add the result in position "w" of "wraps".
1291 * "len" is the total number of coefficients in "bound" and "ineq".
1292 * Return isl_bool_true on success, isl_bool_false on failure and
1293 * isl_bool_error on error.
1294 * Wrapping can fail if the result of wrapping is equal to "bound"
1295 * or if we want to bound the sizes of the coefficients and
1296 * the wrapped constraint does not satisfy this bound.
1298 static isl_bool add_wrap(struct isl_wraps *wraps, int w, isl_int *bound,
1299 isl_int *ineq, unsigned len, __isl_keep isl_set *set, int negate)
1301 isl_seq_cpy(wraps->mat->row[w], bound, len);
1302 if (negate) {
1303 isl_seq_neg(wraps->mat->row[w + 1], ineq, len);
1304 ineq = wraps->mat->row[w + 1];
1306 if (!isl_set_wrap_facet(set, wraps->mat->row[w], ineq))
1307 return isl_bool_error;
1308 if (isl_seq_eq(wraps->mat->row[w], bound, len))
1309 return isl_bool_false;
1310 if (!allow_wrap(wraps, w))
1311 return isl_bool_false;
1312 return isl_bool_true;
1315 /* This function has two modes of operations.
1317 * If "add_valid" is set, then all the constraints of info->bmap
1318 * (except the opposite of "bound") are valid for the other basic map.
1319 * In this case, attempts are made to wrap some of these valid constraints
1320 * to more tightly fit around "set". Only successful wrappings are recorded
1321 * and failed wrappings are ignored.
1323 * If "add_valid" is not set, then some of the constraints of info->bmap
1324 * are not valid for the other basic map, and only those are considered
1325 * for wrapping. In this case all attempted wrappings need to succeed.
1326 * Otherwise "wraps" is marked as failed.
1327 * Note that the constraints that are valid for the other basic map
1328 * will be added to the combined basic map by default, so there is
1329 * no need to wrap them.
1330 * The caller wrap_in_facets even relies on this function not wrapping
1331 * any constraints that are already valid.
1333 * Only consider constraints that are not redundant (as determined
1334 * by info->tab) and that are valid or invalid depending on "add_valid".
1335 * Wrap each constraint around "bound" such that it includes the whole
1336 * set "set" and append the resulting constraint to "wraps".
1337 * "wraps" is assumed to have been pre-allocated to the appropriate size.
1338 * wraps->n_row is the number of actual wrapped constraints that have
1339 * been added.
1340 * If any of the wrapping problems results in a constraint that is
1341 * identical to "bound", then this means that "set" is unbounded in such
1342 * a way that no wrapping is possible.
1343 * Similarly, if we want to bound the coefficients of the wrapping
1344 * constraints and a newly added wrapping constraint does not
1345 * satisfy the bound, then the wrapping is considered to have failed.
1346 * Note though that "wraps" is only marked failed if "add_valid" is not set.
1348 static isl_stat add_selected_wraps(struct isl_wraps *wraps,
1349 struct isl_coalesce_info *info, isl_int *bound, __isl_keep isl_set *set,
1350 int add_valid)
1352 int l, m;
1353 int w;
1354 isl_bool added;
1355 isl_basic_map *bmap = info->bmap;
1356 isl_size total = isl_basic_map_dim(bmap, isl_dim_all);
1357 unsigned len = 1 + total;
1359 if (total < 0)
1360 return isl_stat_error;
1362 w = wraps->mat->n_row;
1364 for (l = 0; l < bmap->n_ineq; ++l) {
1365 int is_valid = info->ineq[l] == STATUS_VALID;
1366 if ((!add_valid && is_valid) ||
1367 info->ineq[l] == STATUS_REDUNDANT)
1368 continue;
1369 if (isl_seq_is_neg(bound, bmap->ineq[l], len))
1370 continue;
1371 if (isl_seq_eq(bound, bmap->ineq[l], len))
1372 continue;
1373 if (isl_tab_is_redundant(info->tab, bmap->n_eq + l))
1374 continue;
1376 added = add_wrap(wraps, w, bound, bmap->ineq[l], len, set, 0);
1377 if (added < 0)
1378 return isl_stat_error;
1379 if (!added && !is_valid)
1380 goto unbounded;
1381 if (added)
1382 ++w;
1384 for (l = 0; l < bmap->n_eq; ++l) {
1385 if (isl_seq_is_neg(bound, bmap->eq[l], len))
1386 continue;
1387 if (isl_seq_eq(bound, bmap->eq[l], len))
1388 continue;
1390 for (m = 0; m < 2; ++m) {
1391 if (info->eq[2 * l + m] == STATUS_VALID)
1392 continue;
1393 added = add_wrap(wraps, w, bound, bmap->eq[l], len,
1394 set, !m);
1395 if (added < 0)
1396 return isl_stat_error;
1397 if (!added)
1398 goto unbounded;
1399 ++w;
1403 wraps->mat->n_row = w;
1404 return isl_stat_ok;
1405 unbounded:
1406 return wraps_mark_failed(wraps);
1409 /* For each constraint in info->bmap that is not redundant (as determined
1410 * by info->tab) and that is not a valid constraint for the other basic map,
1411 * wrap the constraint around "bound" such that it includes the whole
1412 * set "set" and append the resulting constraint to "wraps".
1413 * Note that the constraints that are valid for the other basic map
1414 * will be added to the combined basic map by default, so there is
1415 * no need to wrap them.
1416 * The caller wrap_in_facets even relies on this function not wrapping
1417 * any constraints that are already valid.
1418 * "wraps" is assumed to have been pre-allocated to the appropriate size.
1419 * wraps->n_row is the number of actual wrapped constraints that have
1420 * been added.
1421 * If any of the wrapping problems results in a constraint that is
1422 * identical to "bound", then this means that "set" is unbounded in such
1423 * a way that no wrapping is possible. If this happens then "wraps"
1424 * is marked as failed.
1425 * Similarly, if we want to bound the coefficients of the wrapping
1426 * constraints and a newly added wrapping constraint does not
1427 * satisfy the bound, then "wraps" is also marked as failed.
1429 static isl_stat add_wraps(struct isl_wraps *wraps,
1430 struct isl_coalesce_info *info, isl_int *bound, __isl_keep isl_set *set)
1432 return add_selected_wraps(wraps, info, bound, set, 0);
1435 /* Check if the constraints in "wraps" from "first" until the last
1436 * are all valid for the basic set represented by "tab",
1437 * dropping the invalid constraints if "keep" is set and
1438 * marking the wrapping as failed if "keep" is not set and
1439 * any constraint turns out to be invalid.
1441 static isl_stat check_wraps(struct isl_wraps *wraps, int first,
1442 struct isl_tab *tab, int keep)
1444 int i;
1446 for (i = wraps->mat->n_row - 1; i >= first; --i) {
1447 enum isl_ineq_type type;
1448 type = isl_tab_ineq_type(tab, wraps->mat->row[i]);
1449 if (type == isl_ineq_error)
1450 return isl_stat_error;
1451 if (type == isl_ineq_redundant)
1452 continue;
1453 if (!keep)
1454 return wraps_mark_failed(wraps);
1455 wraps->mat = isl_mat_drop_rows(wraps->mat, i, 1);
1456 if (!wraps->mat)
1457 return isl_stat_error;
1460 return isl_stat_ok;
1463 /* Return a set that corresponds to the non-redundant constraints
1464 * (as recorded in info->tab) of info->bmap.
1466 * It's important to remove the redundant constraints as some
1467 * of the other constraints may have been modified after the
1468 * constraints were marked redundant.
1469 * In particular, a constraint may have been relaxed.
1470 * Redundant constraints are ignored when a constraint is relaxed
1471 * and should therefore continue to be ignored ever after.
1472 * Otherwise, the relaxation might be thwarted by some of
1473 * these constraints.
1475 * Update the underlying set to ensure that the dimension doesn't change.
1476 * Otherwise the integer divisions could get dropped if the tab
1477 * turns out to be empty.
1479 static __isl_give isl_set *set_from_updated_bmap(struct isl_coalesce_info *info)
1481 isl_basic_map *bmap;
1482 isl_basic_set *bset;
1484 bmap = isl_basic_map_copy(info->bmap);
1485 bset = isl_basic_map_underlying_set(bmap);
1486 bset = isl_basic_set_cow(bset);
1487 bset = isl_basic_set_update_from_tab(bset, info->tab);
1488 return isl_set_from_basic_set(bset);
1491 /* Does "info" have any cut constraints that are redundant?
1493 static isl_bool has_redundant_cuts(struct isl_coalesce_info *info)
1495 int l;
1496 isl_size n_eq, n_ineq;
1498 n_eq = isl_basic_map_n_equality(info->bmap);
1499 n_ineq = isl_basic_map_n_inequality(info->bmap);
1500 if (n_eq < 0 || n_ineq < 0)
1501 return isl_bool_error;
1502 for (l = 0; l < n_ineq; ++l) {
1503 int red;
1505 if (info->ineq[l] != STATUS_CUT)
1506 continue;
1507 red = isl_tab_is_redundant(info->tab, n_eq + l);
1508 if (red < 0)
1509 return isl_bool_error;
1510 if (red)
1511 return isl_bool_true;
1514 return isl_bool_false;
1517 /* Wrap some constraints of info->bmap that bound the facet defined
1518 * by inequality "k" around (the opposite of) this inequality to
1519 * include "set". "bound" may be used to store the negated inequality.
1521 * If "add_valid" is set, then all ridges are already valid and
1522 * the purpose is to wrap "set" more tightly. In this case,
1523 * wrapping doesn't fail, although it is possible that no constraint
1524 * gets wrapped.
1526 * If "add_valid" is not set, then some of the ridges are cut constraints
1527 * and only those are wrapped around "set".
1529 * Since the wrapped constraints are not guaranteed to contain the whole
1530 * of info->bmap, we check them in check_wraps.
1531 * If any of the wrapped constraints turn out to be invalid, then
1532 * check_wraps will mark "wraps" as failed if "add_valid" is not set.
1533 * If "add_valid" is set, then the offending constraints are
1534 * simply removed.
1536 * If the facet turns out to be empty, then no wrapping can be performed.
1537 * This is considered a failure, unless "add_valid" is set.
1539 * If any of the cut constraints of info->bmap turn out
1540 * to be redundant with respect to other constraints
1541 * then these will neither be wrapped nor added directly to the result.
1542 * The result may therefore not be correct.
1543 * Skip wrapping and mark "wraps" as failed in this case.
1545 static isl_stat add_selected_wraps_around_facet(struct isl_wraps *wraps,
1546 struct isl_coalesce_info *info, int k, isl_int *bound,
1547 __isl_keep isl_set *set, int add_valid)
1549 isl_bool nowrap;
1550 struct isl_tab_undo *snap;
1551 int n;
1552 isl_size total = isl_basic_map_dim(info->bmap, isl_dim_all);
1554 if (total < 0)
1555 return isl_stat_error;
1557 snap = isl_tab_snap(info->tab);
1559 if (isl_tab_select_facet(info->tab, info->bmap->n_eq + k) < 0)
1560 return isl_stat_error;
1561 if (isl_tab_detect_redundant(info->tab) < 0)
1562 return isl_stat_error;
1563 if (info->tab->empty) {
1564 if (isl_tab_rollback(info->tab, snap) < 0)
1565 return isl_stat_error;
1566 if (!add_valid)
1567 return wraps_mark_failed(wraps);
1568 return isl_stat_ok;
1570 nowrap = has_redundant_cuts(info);
1571 if (nowrap < 0)
1572 return isl_stat_error;
1574 n = wraps->mat->n_row;
1575 if (!nowrap) {
1576 isl_seq_neg(bound, info->bmap->ineq[k], 1 + total);
1578 if (add_selected_wraps(wraps, info, bound, set, add_valid) < 0)
1579 return isl_stat_error;
1582 if (isl_tab_rollback(info->tab, snap) < 0)
1583 return isl_stat_error;
1584 if (nowrap)
1585 return wraps_mark_failed(wraps);
1586 if (check_wraps(wraps, n, info->tab, add_valid) < 0)
1587 return isl_stat_error;
1589 return isl_stat_ok;
1592 /* Wrap the constraints of info->bmap that bound the facet defined
1593 * by inequality "k" around (the opposite of) this inequality to
1594 * include "set". "bound" may be used to store the negated inequality.
1595 * If any of the wrapped constraints turn out to be invalid for info->bmap
1596 * itself, then mark "wraps" as failed.
1598 static isl_stat add_wraps_around_facet(struct isl_wraps *wraps,
1599 struct isl_coalesce_info *info, int k, isl_int *bound,
1600 __isl_keep isl_set *set)
1602 return add_selected_wraps_around_facet(wraps, info, k, bound, set, 0);
1605 /* Wrap the (valid) constraints of info->bmap that bound the facet defined
1606 * by inequality "k" around (the opposite of) this inequality to
1607 * include "set" more tightly.
1608 * "bound" may be used to store the negated inequality.
1609 * Remove any wrapping constraints that turn out to be invalid
1610 * for info->bmap itself.
1612 static isl_stat add_valid_wraps_around_facet(struct isl_wraps *wraps,
1613 struct isl_coalesce_info *info, int k, isl_int *bound,
1614 __isl_keep isl_set *set)
1616 return add_selected_wraps_around_facet(wraps, info, k, bound, set, 1);
1619 /* Basic map "i" has an inequality (say "k") that is adjacent
1620 * to some inequality of basic map "j". All the other inequalities
1621 * are valid for "j".
1622 * Check if basic map "j" forms an extension of basic map "i".
1624 * Note that this function is only called if some of the equalities or
1625 * inequalities of basic map "j" do cut basic map "i". The function is
1626 * correct even if there are no such cut constraints, but in that case
1627 * the additional checks performed by this function are overkill.
1629 * First try and wrap the ridges of "k" around "j".
1630 * Note that those ridges are already valid for "j",
1631 * but the wrapped versions may wrap "j" more tightly,
1632 * increasing the chances of "j" being detected as an extension of "i"
1634 static enum isl_change is_adj_ineq_extension(int i, int j,
1635 struct isl_coalesce_info *info)
1637 int k;
1638 enum isl_change change;
1639 isl_size total;
1640 isl_size n_eq_i, n_ineq_i;
1641 struct isl_wraps wraps;
1642 isl_ctx *ctx;
1643 isl_mat *mat;
1644 isl_vec *bound;
1645 isl_set *set_j;
1646 isl_stat r;
1648 k = find_ineq(&info[i], STATUS_ADJ_INEQ);
1649 if (k < 0)
1650 isl_die(isl_basic_map_get_ctx(info[i].bmap), isl_error_internal,
1651 "info[i].ineq should have exactly one STATUS_ADJ_INEQ",
1652 return isl_change_error);
1654 total = isl_basic_map_dim(info[i].bmap, isl_dim_all);
1655 n_eq_i = isl_basic_map_n_equality(info[i].bmap);
1656 n_ineq_i = isl_basic_map_n_inequality(info[i].bmap);
1657 if (total < 0 || n_eq_i < 0 || n_ineq_i < 0)
1658 return isl_change_error;
1660 set_j = set_from_updated_bmap(&info[j]);
1661 ctx = isl_basic_map_get_ctx(info[i].bmap);
1662 bound = isl_vec_alloc(ctx, 1 + total);
1663 mat = isl_mat_alloc(ctx, 2 * n_eq_i + n_ineq_i, 1 + total);
1664 if (wraps_init(&wraps, mat, info, i, j) < 0)
1665 goto error;
1666 if (!bound || !set_j)
1667 goto error;
1668 r = add_valid_wraps_around_facet(&wraps, &info[i], k, bound->el, set_j);
1669 if (r < 0)
1670 goto error;
1672 change = is_adj_ineq_extension_with_wraps(i, j, k, info, wraps.mat);
1674 wraps_free(&wraps);
1675 isl_vec_free(bound);
1676 isl_set_free(set_j);
1678 return change;
1679 error:
1680 wraps_free(&wraps);
1681 isl_vec_free(bound);
1682 isl_set_free(set_j);
1683 return isl_change_error;
1686 /* Both basic maps have at least one inequality with and adjacent
1687 * (but opposite) inequality in the other basic map.
1688 * Check that there are no cut constraints and that there is only
1689 * a single pair of adjacent inequalities.
1690 * If so, we can replace the pair by a single basic map described
1691 * by all but the pair of adjacent inequalities.
1692 * Any additional points introduced lie strictly between the two
1693 * adjacent hyperplanes and can therefore be integral.
1695 * ____ _____
1696 * / ||\ / \
1697 * / || \ / \
1698 * \ || \ => \ \
1699 * \ || / \ /
1700 * \___||_/ \_____/
1702 * The test for a single pair of adjacent inequalities is important
1703 * for avoiding the combination of two basic maps like the following
1705 * /|
1706 * / |
1707 * /__|
1708 * _____
1709 * | |
1710 * | |
1711 * |___|
1713 * If there are some cut constraints on one side, then we may
1714 * still be able to fuse the two basic maps, but we need to perform
1715 * some additional checks in is_adj_ineq_extension.
1717 static enum isl_change check_adj_ineq(int i, int j,
1718 struct isl_coalesce_info *info)
1720 int count_i, count_j;
1721 int cut_i, cut_j;
1723 count_i = count_ineq(&info[i], STATUS_ADJ_INEQ);
1724 count_j = count_ineq(&info[j], STATUS_ADJ_INEQ);
1726 if (count_i != 1 && count_j != 1)
1727 return isl_change_none;
1729 cut_i = any_eq(&info[i], STATUS_CUT) || any_ineq(&info[i], STATUS_CUT);
1730 cut_j = any_eq(&info[j], STATUS_CUT) || any_ineq(&info[j], STATUS_CUT);
1732 if (!cut_i && !cut_j && count_i == 1 && count_j == 1)
1733 return fuse(i, j, info, NULL, 0, 0);
1735 if (count_i == 1 && !cut_i)
1736 return is_adj_ineq_extension(i, j, info);
1738 if (count_j == 1 && !cut_j)
1739 return is_adj_ineq_extension(j, i, info);
1741 return isl_change_none;
1744 /* Given a basic set i with a constraint k that is adjacent to
1745 * basic set j, check if we can wrap
1746 * both the facet corresponding to k (if "wrap_facet" is set) and basic map j
1747 * (always) around their ridges to include the other set.
1748 * If so, replace the pair of basic sets by their union.
1750 * All constraints of i (except k) are assumed to be valid or
1751 * cut constraints for j.
1752 * Wrapping the cut constraints to include basic map j may result
1753 * in constraints that are no longer valid of basic map i
1754 * we have to check that the resulting wrapping constraints are valid for i.
1755 * If "wrap_facet" is not set, then all constraints of i (except k)
1756 * are assumed to be valid for j.
1757 * ____ _____
1758 * / | / \
1759 * / || / |
1760 * \ || => \ |
1761 * \ || \ |
1762 * \___|| \____|
1765 static enum isl_change can_wrap_in_facet(int i, int j, int k,
1766 struct isl_coalesce_info *info, int wrap_facet)
1768 enum isl_change change = isl_change_none;
1769 struct isl_wraps wraps;
1770 isl_ctx *ctx;
1771 isl_mat *mat;
1772 struct isl_set *set_i = NULL;
1773 struct isl_set *set_j = NULL;
1774 struct isl_vec *bound = NULL;
1775 isl_size total = isl_basic_map_dim(info[i].bmap, isl_dim_all);
1777 if (total < 0)
1778 return isl_change_error;
1779 set_i = set_from_updated_bmap(&info[i]);
1780 set_j = set_from_updated_bmap(&info[j]);
1781 ctx = isl_basic_map_get_ctx(info[i].bmap);
1782 mat = isl_mat_alloc(ctx, 2 * (info[i].bmap->n_eq + info[j].bmap->n_eq) +
1783 info[i].bmap->n_ineq + info[j].bmap->n_ineq,
1784 1 + total);
1785 if (wraps_init(&wraps, mat, info, i, j) < 0)
1786 goto error;
1787 bound = isl_vec_alloc(ctx, 1 + total);
1788 if (!set_i || !set_j || !bound)
1789 goto error;
1791 isl_seq_cpy(bound->el, info[i].bmap->ineq[k], 1 + total);
1792 isl_int_add_ui(bound->el[0], bound->el[0], 1);
1793 isl_seq_normalize(ctx, bound->el, 1 + total);
1795 isl_seq_cpy(wraps.mat->row[0], bound->el, 1 + total);
1796 wraps.mat->n_row = 1;
1798 if (add_wraps(&wraps, &info[j], bound->el, set_i) < 0)
1799 goto error;
1800 if (wraps.failed)
1801 goto unbounded;
1803 if (wrap_facet) {
1804 if (add_wraps_around_facet(&wraps, &info[i], k,
1805 bound->el, set_j) < 0)
1806 goto error;
1807 if (wraps.failed)
1808 goto unbounded;
1811 change = fuse(i, j, info, wraps.mat, 0, 0);
1813 unbounded:
1814 wraps_free(&wraps);
1816 isl_set_free(set_i);
1817 isl_set_free(set_j);
1819 isl_vec_free(bound);
1821 return change;
1822 error:
1823 wraps_free(&wraps);
1824 isl_vec_free(bound);
1825 isl_set_free(set_i);
1826 isl_set_free(set_j);
1827 return isl_change_error;
1830 /* Given a cut constraint t(x) >= 0 of basic map i, stored in row "w"
1831 * of wrap.mat, replace it by its relaxed version t(x) + 1 >= 0, and
1832 * add wrapping constraints to wrap.mat for all constraints
1833 * of basic map j that bound the part of basic map j that sticks out
1834 * of the cut constraint.
1835 * "set_i" is the underlying set of basic map i.
1836 * If any wrapping fails, then wraps->mat.n_row is reset to zero.
1838 * In particular, we first intersect basic map j with t(x) + 1 = 0.
1839 * If the result is empty, then t(x) >= 0 was actually a valid constraint
1840 * (with respect to the integer points), so we add t(x) >= 0 instead.
1841 * Otherwise, we wrap the constraints of basic map j that are not
1842 * redundant in this intersection and that are not already valid
1843 * for basic map i over basic map i.
1844 * Note that it is sufficient to wrap the constraints to include
1845 * basic map i, because we will only wrap the constraints that do
1846 * not include basic map i already. The wrapped constraint will
1847 * therefore be more relaxed compared to the original constraint.
1848 * Since the original constraint is valid for basic map j, so is
1849 * the wrapped constraint.
1851 static isl_stat wrap_in_facet(struct isl_wraps *wraps, int w,
1852 struct isl_coalesce_info *info_j, __isl_keep isl_set *set_i,
1853 struct isl_tab_undo *snap)
1855 isl_int_add_ui(wraps->mat->row[w][0], wraps->mat->row[w][0], 1);
1856 if (isl_tab_add_eq(info_j->tab, wraps->mat->row[w]) < 0)
1857 return isl_stat_error;
1858 if (isl_tab_detect_redundant(info_j->tab) < 0)
1859 return isl_stat_error;
1861 if (info_j->tab->empty)
1862 isl_int_sub_ui(wraps->mat->row[w][0], wraps->mat->row[w][0], 1);
1863 else if (add_wraps(wraps, info_j, wraps->mat->row[w], set_i) < 0)
1864 return isl_stat_error;
1866 if (isl_tab_rollback(info_j->tab, snap) < 0)
1867 return isl_stat_error;
1869 return isl_stat_ok;
1872 /* Given a pair of basic maps i and j such that j sticks out
1873 * of i at n cut constraints, each time by at most one,
1874 * try to compute wrapping constraints and replace the two
1875 * basic maps by a single basic map.
1876 * The other constraints of i are assumed to be valid for j.
1877 * "set_i" is the underlying set of basic map i.
1878 * "wraps" has been initialized to be of the right size.
1880 * For each cut constraint t(x) >= 0 of i, we add the relaxed version
1881 * t(x) + 1 >= 0, along with wrapping constraints for all constraints
1882 * of basic map j that bound the part of basic map j that sticks out
1883 * of the cut constraint.
1885 * If any wrapping fails, i.e., if we cannot wrap to touch
1886 * the union, then we give up.
1887 * Otherwise, the pair of basic maps is replaced by their union.
1889 static enum isl_change try_wrap_in_facets(int i, int j,
1890 struct isl_coalesce_info *info, struct isl_wraps *wraps,
1891 __isl_keep isl_set *set_i)
1893 int k, l, w;
1894 isl_size total;
1895 struct isl_tab_undo *snap;
1897 total = isl_basic_map_dim(info[i].bmap, isl_dim_all);
1898 if (total < 0)
1899 return isl_change_error;
1901 snap = isl_tab_snap(info[j].tab);
1903 for (k = 0; k < info[i].bmap->n_eq; ++k) {
1904 for (l = 0; l < 2; ++l) {
1905 if (info[i].eq[2 * k + l] != STATUS_CUT)
1906 continue;
1907 w = wraps->mat->n_row++;
1908 if (l == 0)
1909 isl_seq_neg(wraps->mat->row[w],
1910 info[i].bmap->eq[k], 1 + total);
1911 else
1912 isl_seq_cpy(wraps->mat->row[w],
1913 info[i].bmap->eq[k], 1 + total);
1914 if (wrap_in_facet(wraps, w, &info[j], set_i, snap) < 0)
1915 return isl_change_error;
1917 if (wraps->failed)
1918 return isl_change_none;
1922 for (k = 0; k < info[i].bmap->n_ineq; ++k) {
1923 if (info[i].ineq[k] != STATUS_CUT)
1924 continue;
1925 w = wraps->mat->n_row++;
1926 isl_seq_cpy(wraps->mat->row[w],
1927 info[i].bmap->ineq[k], 1 + total);
1928 if (wrap_in_facet(wraps, w, &info[j], set_i, snap) < 0)
1929 return isl_change_error;
1931 if (wraps->failed)
1932 return isl_change_none;
1935 return fuse(i, j, info, wraps->mat, 0, 1);
1938 /* Given a pair of basic maps i and j such that j sticks out
1939 * of i at n cut constraints, each time by at most one,
1940 * try to compute wrapping constraints and replace the two
1941 * basic maps by a single basic map.
1942 * The other constraints of i are assumed to be valid for j.
1944 * The core computation is performed by try_wrap_in_facets.
1945 * This function simply extracts an underlying set representation
1946 * of basic map i and initializes the data structure for keeping
1947 * track of wrapping constraints.
1949 static enum isl_change wrap_in_facets(int i, int j, int n,
1950 struct isl_coalesce_info *info)
1952 enum isl_change change = isl_change_none;
1953 struct isl_wraps wraps;
1954 isl_ctx *ctx;
1955 isl_mat *mat;
1956 isl_set *set_i = NULL;
1957 isl_size total = isl_basic_map_dim(info[i].bmap, isl_dim_all);
1958 int max_wrap;
1960 if (total < 0)
1961 return isl_change_error;
1962 if (isl_tab_extend_cons(info[j].tab, 1) < 0)
1963 return isl_change_error;
1965 max_wrap = 1 + 2 * info[j].bmap->n_eq + info[j].bmap->n_ineq;
1966 max_wrap *= n;
1968 set_i = set_from_updated_bmap(&info[i]);
1969 ctx = isl_basic_map_get_ctx(info[i].bmap);
1970 mat = isl_mat_alloc(ctx, max_wrap, 1 + total);
1971 if (wraps_init(&wraps, mat, info, i, j) < 0)
1972 goto error;
1973 if (!set_i)
1974 goto error;
1976 change = try_wrap_in_facets(i, j, info, &wraps, set_i);
1978 wraps_free(&wraps);
1979 isl_set_free(set_i);
1981 return change;
1982 error:
1983 wraps_free(&wraps);
1984 isl_set_free(set_i);
1985 return isl_change_error;
1988 /* Return the effect of inequality "ineq" on the tableau "tab",
1989 * after relaxing the constant term of "ineq" by one.
1991 static enum isl_ineq_type type_of_relaxed(struct isl_tab *tab, isl_int *ineq)
1993 enum isl_ineq_type type;
1995 isl_int_add_ui(ineq[0], ineq[0], 1);
1996 type = isl_tab_ineq_type(tab, ineq);
1997 isl_int_sub_ui(ineq[0], ineq[0], 1);
1999 return type;
2002 /* Given two basic sets i and j,
2003 * check if relaxing all the cut constraints of i by one turns
2004 * them into valid constraint for j and check if we can wrap in
2005 * the bits that are sticking out.
2006 * If so, replace the pair by their union.
2008 * We first check if all relaxed cut inequalities of i are valid for j
2009 * and then try to wrap in the intersections of the relaxed cut inequalities
2010 * with j.
2012 * During this wrapping, we consider the points of j that lie at a distance
2013 * of exactly 1 from i. In particular, we ignore the points that lie in
2014 * between this lower-dimensional space and the basic map i.
2015 * We can therefore only apply this to integer maps.
2016 * ____ _____
2017 * / ___|_ / \
2018 * / | | / |
2019 * \ | | => \ |
2020 * \|____| \ |
2021 * \___| \____/
2023 * _____ ______
2024 * | ____|_ | \
2025 * | | | | |
2026 * | | | => | |
2027 * |_| | | |
2028 * |_____| \______|
2030 * _______
2031 * | |
2032 * | |\ |
2033 * | | \ |
2034 * | | \ |
2035 * | | \|
2036 * | | \
2037 * | |_____\
2038 * | |
2039 * |_______|
2041 * Wrapping can fail if the result of wrapping one of the facets
2042 * around its edges does not produce any new facet constraint.
2043 * In particular, this happens when we try to wrap in unbounded sets.
2045 * _______________________________________________________________________
2047 * | ___
2048 * | | |
2049 * |_| |_________________________________________________________________
2050 * |___|
2052 * The following is not an acceptable result of coalescing the above two
2053 * sets as it includes extra integer points.
2054 * _______________________________________________________________________
2056 * |
2057 * |
2059 * \______________________________________________________________________
2061 static enum isl_change can_wrap_in_set(int i, int j,
2062 struct isl_coalesce_info *info)
2064 int k, l;
2065 int n;
2066 isl_size total;
2068 if (ISL_F_ISSET(info[i].bmap, ISL_BASIC_MAP_RATIONAL) ||
2069 ISL_F_ISSET(info[j].bmap, ISL_BASIC_MAP_RATIONAL))
2070 return isl_change_none;
2072 n = count_eq(&info[i], STATUS_CUT) + count_ineq(&info[i], STATUS_CUT);
2073 if (n == 0)
2074 return isl_change_none;
2076 total = isl_basic_map_dim(info[i].bmap, isl_dim_all);
2077 if (total < 0)
2078 return isl_change_error;
2079 for (k = 0; k < info[i].bmap->n_eq; ++k) {
2080 for (l = 0; l < 2; ++l) {
2081 enum isl_ineq_type type;
2083 if (info[i].eq[2 * k + l] != STATUS_CUT)
2084 continue;
2086 if (l == 0)
2087 isl_seq_neg(info[i].bmap->eq[k],
2088 info[i].bmap->eq[k], 1 + total);
2089 type = type_of_relaxed(info[j].tab,
2090 info[i].bmap->eq[k]);
2091 if (l == 0)
2092 isl_seq_neg(info[i].bmap->eq[k],
2093 info[i].bmap->eq[k], 1 + total);
2094 if (type == isl_ineq_error)
2095 return isl_change_error;
2096 if (type != isl_ineq_redundant)
2097 return isl_change_none;
2101 for (k = 0; k < info[i].bmap->n_ineq; ++k) {
2102 enum isl_ineq_type type;
2104 if (info[i].ineq[k] != STATUS_CUT)
2105 continue;
2107 type = type_of_relaxed(info[j].tab, info[i].bmap->ineq[k]);
2108 if (type == isl_ineq_error)
2109 return isl_change_error;
2110 if (type != isl_ineq_redundant)
2111 return isl_change_none;
2114 return wrap_in_facets(i, j, n, info);
2117 /* Check if either i or j has only cut constraints that can
2118 * be used to wrap in (a facet of) the other basic set.
2119 * if so, replace the pair by their union.
2121 static enum isl_change check_wrap(int i, int j, struct isl_coalesce_info *info)
2123 enum isl_change change = isl_change_none;
2125 change = can_wrap_in_set(i, j, info);
2126 if (change != isl_change_none)
2127 return change;
2129 change = can_wrap_in_set(j, i, info);
2130 return change;
2133 /* Check if all inequality constraints of "i" that cut "j" cease
2134 * to be cut constraints if they are relaxed by one.
2135 * If so, collect the cut constraints in "list".
2136 * The caller is responsible for allocating "list".
2138 static isl_bool all_cut_by_one(int i, int j, struct isl_coalesce_info *info,
2139 int *list)
2141 int l, n;
2143 n = 0;
2144 for (l = 0; l < info[i].bmap->n_ineq; ++l) {
2145 enum isl_ineq_type type;
2147 if (info[i].ineq[l] != STATUS_CUT)
2148 continue;
2149 type = type_of_relaxed(info[j].tab, info[i].bmap->ineq[l]);
2150 if (type == isl_ineq_error)
2151 return isl_bool_error;
2152 if (type != isl_ineq_redundant)
2153 return isl_bool_false;
2154 list[n++] = l;
2157 return isl_bool_true;
2160 /* Given two basic maps such that "j" has at least one equality constraint
2161 * that is adjacent to an inequality constraint of "i" and such that "i" has
2162 * exactly one inequality constraint that is adjacent to an equality
2163 * constraint of "j", check whether "i" can be extended to include "j" or
2164 * whether "j" can be wrapped into "i".
2165 * All remaining constraints of "i" and "j" are assumed to be valid
2166 * or cut constraints of the other basic map.
2167 * However, none of the equality constraints of "i" are cut constraints.
2169 * If "i" has any "cut" inequality constraints, then check if relaxing
2170 * each of them by one is sufficient for them to become valid.
2171 * If so, check if the inequality constraint adjacent to an equality
2172 * constraint of "j" along with all these cut constraints
2173 * can be relaxed by one to contain exactly "j".
2174 * Otherwise, or if this fails, check if "j" can be wrapped into "i".
2176 static enum isl_change check_single_adj_eq(int i, int j,
2177 struct isl_coalesce_info *info)
2179 enum isl_change change = isl_change_none;
2180 int k;
2181 int n_cut;
2182 int *relax;
2183 isl_ctx *ctx;
2184 isl_bool try_relax;
2186 n_cut = count_ineq(&info[i], STATUS_CUT);
2188 k = find_ineq(&info[i], STATUS_ADJ_EQ);
2190 if (n_cut > 0) {
2191 ctx = isl_basic_map_get_ctx(info[i].bmap);
2192 relax = isl_calloc_array(ctx, int, 1 + n_cut);
2193 if (!relax)
2194 return isl_change_error;
2195 relax[0] = k;
2196 try_relax = all_cut_by_one(i, j, info, relax + 1);
2197 if (try_relax < 0)
2198 change = isl_change_error;
2199 } else {
2200 try_relax = isl_bool_true;
2201 relax = &k;
2203 if (try_relax && change == isl_change_none)
2204 change = is_relaxed_extension(i, j, 1 + n_cut, relax, info);
2205 if (n_cut > 0)
2206 free(relax);
2207 if (change != isl_change_none)
2208 return change;
2210 change = can_wrap_in_facet(i, j, k, info, n_cut > 0);
2212 return change;
2215 /* At least one of the basic maps has an equality that is adjacent
2216 * to an inequality. Make sure that only one of the basic maps has
2217 * such an equality and that the other basic map has exactly one
2218 * inequality adjacent to an equality.
2219 * If the other basic map does not have such an inequality, then
2220 * check if all its constraints are either valid or cut constraints
2221 * and, if so, try wrapping in the first map into the second.
2222 * Otherwise, try to extend one basic map with the other or
2223 * wrap one basic map in the other.
2225 static enum isl_change check_adj_eq(int i, int j,
2226 struct isl_coalesce_info *info)
2228 if (any_eq(&info[i], STATUS_ADJ_INEQ) &&
2229 any_eq(&info[j], STATUS_ADJ_INEQ))
2230 /* ADJ EQ TOO MANY */
2231 return isl_change_none;
2233 if (any_eq(&info[i], STATUS_ADJ_INEQ))
2234 return check_adj_eq(j, i, info);
2236 /* j has an equality adjacent to an inequality in i */
2238 if (count_ineq(&info[i], STATUS_ADJ_EQ) != 1) {
2239 if (all_valid_or_cut(&info[i]))
2240 return can_wrap_in_set(i, j, info);
2241 return isl_change_none;
2243 if (any_eq(&info[i], STATUS_CUT))
2244 return isl_change_none;
2245 if (any_ineq(&info[j], STATUS_ADJ_EQ) ||
2246 any_ineq(&info[i], STATUS_ADJ_INEQ) ||
2247 any_ineq(&info[j], STATUS_ADJ_INEQ))
2248 /* ADJ EQ TOO MANY */
2249 return isl_change_none;
2251 return check_single_adj_eq(i, j, info);
2254 /* Disjunct "j" lies on a hyperplane that is adjacent to disjunct "i".
2255 * In particular, disjunct "i" has an inequality constraint that is adjacent
2256 * to a (combination of) equality constraint(s) of disjunct "j",
2257 * but disjunct "j" has no explicit equality constraint adjacent
2258 * to an inequality constraint of disjunct "i".
2260 * Disjunct "i" is already known not to have any equality constraints
2261 * that are adjacent to an equality or inequality constraint.
2262 * Check that, other than the inequality constraint mentioned above,
2263 * all other constraints of disjunct "i" are valid for disjunct "j".
2264 * If so, try and wrap in disjunct "j".
2266 static enum isl_change check_ineq_adj_eq(int i, int j,
2267 struct isl_coalesce_info *info)
2269 int k;
2271 if (any_eq(&info[i], STATUS_CUT))
2272 return isl_change_none;
2273 if (any_ineq(&info[i], STATUS_CUT))
2274 return isl_change_none;
2275 if (any_ineq(&info[i], STATUS_ADJ_INEQ))
2276 return isl_change_none;
2277 if (count_ineq(&info[i], STATUS_ADJ_EQ) != 1)
2278 return isl_change_none;
2280 k = find_ineq(&info[i], STATUS_ADJ_EQ);
2282 return can_wrap_in_facet(i, j, k, info, 0);
2285 /* The two basic maps lie on adjacent hyperplanes. In particular,
2286 * basic map "i" has an equality that lies parallel to basic map "j".
2287 * Check if we can wrap the facets around the parallel hyperplanes
2288 * to include the other set.
2290 * We perform basically the same operations as can_wrap_in_facet,
2291 * except that we don't need to select a facet of one of the sets.
2293 * \\ \\
2294 * \\ => \\
2295 * \ \|
2297 * If there is more than one equality of "i" adjacent to an equality of "j",
2298 * then the result will satisfy one or more equalities that are a linear
2299 * combination of these equalities. These will be encoded as pairs
2300 * of inequalities in the wrapping constraints and need to be made
2301 * explicit.
2303 static enum isl_change check_eq_adj_eq(int i, int j,
2304 struct isl_coalesce_info *info)
2306 int k;
2307 enum isl_change change = isl_change_none;
2308 int detect_equalities = 0;
2309 struct isl_wraps wraps;
2310 isl_ctx *ctx;
2311 isl_mat *mat;
2312 struct isl_set *set_i = NULL;
2313 struct isl_set *set_j = NULL;
2314 struct isl_vec *bound = NULL;
2315 isl_size total = isl_basic_map_dim(info[i].bmap, isl_dim_all);
2317 if (total < 0)
2318 return isl_change_error;
2319 if (count_eq(&info[i], STATUS_ADJ_EQ) != 1)
2320 detect_equalities = 1;
2322 k = find_eq(&info[i], STATUS_ADJ_EQ);
2324 set_i = set_from_updated_bmap(&info[i]);
2325 set_j = set_from_updated_bmap(&info[j]);
2326 ctx = isl_basic_map_get_ctx(info[i].bmap);
2327 mat = isl_mat_alloc(ctx, 2 * (info[i].bmap->n_eq + info[j].bmap->n_eq) +
2328 info[i].bmap->n_ineq + info[j].bmap->n_ineq,
2329 1 + total);
2330 if (wraps_init(&wraps, mat, info, i, j) < 0)
2331 goto error;
2332 bound = isl_vec_alloc(ctx, 1 + total);
2333 if (!set_i || !set_j || !bound)
2334 goto error;
2336 if (k % 2 == 0)
2337 isl_seq_neg(bound->el, info[i].bmap->eq[k / 2], 1 + total);
2338 else
2339 isl_seq_cpy(bound->el, info[i].bmap->eq[k / 2], 1 + total);
2340 isl_int_add_ui(bound->el[0], bound->el[0], 1);
2342 isl_seq_cpy(wraps.mat->row[0], bound->el, 1 + total);
2343 wraps.mat->n_row = 1;
2345 if (add_wraps(&wraps, &info[j], bound->el, set_i) < 0)
2346 goto error;
2347 if (wraps.failed)
2348 goto unbounded;
2350 isl_int_sub_ui(bound->el[0], bound->el[0], 1);
2351 isl_seq_neg(bound->el, bound->el, 1 + total);
2353 isl_seq_cpy(wraps.mat->row[wraps.mat->n_row], bound->el, 1 + total);
2354 wraps.mat->n_row++;
2356 if (add_wraps(&wraps, &info[i], bound->el, set_j) < 0)
2357 goto error;
2358 if (wraps.failed)
2359 goto unbounded;
2361 change = fuse(i, j, info, wraps.mat, detect_equalities, 0);
2363 if (0) {
2364 error: change = isl_change_error;
2366 unbounded:
2368 wraps_free(&wraps);
2369 isl_set_free(set_i);
2370 isl_set_free(set_j);
2371 isl_vec_free(bound);
2373 return change;
2376 /* Initialize the "eq" and "ineq" fields of "info".
2378 static void init_status(struct isl_coalesce_info *info)
2380 info->eq = info->ineq = NULL;
2383 /* Set info->eq to the positions of the equalities of info->bmap
2384 * with respect to the basic map represented by "tab".
2385 * If info->eq has already been computed, then do not compute it again.
2387 static void set_eq_status_in(struct isl_coalesce_info *info,
2388 struct isl_tab *tab)
2390 if (info->eq)
2391 return;
2392 info->eq = eq_status_in(info->bmap, tab);
2395 /* Set info->ineq to the positions of the inequalities of info->bmap
2396 * with respect to the basic map represented by "tab".
2397 * If info->ineq has already been computed, then do not compute it again.
2399 static void set_ineq_status_in(struct isl_coalesce_info *info,
2400 struct isl_tab *tab)
2402 if (info->ineq)
2403 return;
2404 info->ineq = ineq_status_in(info->bmap, info->tab, tab);
2407 /* Free the memory allocated by the "eq" and "ineq" fields of "info".
2408 * This function assumes that init_status has been called on "info" first,
2409 * after which the "eq" and "ineq" fields may or may not have been
2410 * assigned a newly allocated array.
2412 static void clear_status(struct isl_coalesce_info *info)
2414 free(info->eq);
2415 free(info->ineq);
2418 /* Are all inequality constraints of the basic map represented by "info"
2419 * valid for the other basic map, except for a single constraint
2420 * that is adjacent to an inequality constraint of the other basic map?
2422 static int all_ineq_valid_or_single_adj_ineq(struct isl_coalesce_info *info)
2424 int i;
2425 int k = -1;
2427 for (i = 0; i < info->bmap->n_ineq; ++i) {
2428 if (info->ineq[i] == STATUS_REDUNDANT)
2429 continue;
2430 if (info->ineq[i] == STATUS_VALID)
2431 continue;
2432 if (info->ineq[i] != STATUS_ADJ_INEQ)
2433 return 0;
2434 if (k != -1)
2435 return 0;
2436 k = i;
2439 return k != -1;
2442 /* Basic map "i" has one or more equality constraints that separate it
2443 * from basic map "j". Check if it happens to be an extension
2444 * of basic map "j".
2445 * In particular, check that all constraints of "j" are valid for "i",
2446 * except for one inequality constraint that is adjacent
2447 * to an inequality constraints of "i".
2448 * If so, check for "i" being an extension of "j" by calling
2449 * is_adj_ineq_extension.
2451 * Clean up the memory allocated for keeping track of the status
2452 * of the constraints before returning.
2454 static enum isl_change separating_equality(int i, int j,
2455 struct isl_coalesce_info *info)
2457 enum isl_change change = isl_change_none;
2459 if (all(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_VALID) &&
2460 all_ineq_valid_or_single_adj_ineq(&info[j]))
2461 change = is_adj_ineq_extension(j, i, info);
2463 clear_status(&info[i]);
2464 clear_status(&info[j]);
2465 return change;
2468 /* Check if the union of the given pair of basic maps
2469 * can be represented by a single basic map.
2470 * If so, replace the pair by the single basic map and return
2471 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
2472 * Otherwise, return isl_change_none.
2473 * The two basic maps are assumed to live in the same local space.
2474 * The "eq" and "ineq" fields of info[i] and info[j] are assumed
2475 * to have been initialized by the caller, either to NULL or
2476 * to valid information.
2478 * We first check the effect of each constraint of one basic map
2479 * on the other basic map.
2480 * The constraint may be
2481 * redundant the constraint is redundant in its own
2482 * basic map and should be ignore and removed
2483 * in the end
2484 * valid all (integer) points of the other basic map
2485 * satisfy the constraint
2486 * separate no (integer) point of the other basic map
2487 * satisfies the constraint
2488 * cut some but not all points of the other basic map
2489 * satisfy the constraint
2490 * adj_eq the given constraint is adjacent (on the outside)
2491 * to an equality of the other basic map
2492 * adj_ineq the given constraint is adjacent (on the outside)
2493 * to an inequality of the other basic map
2495 * We consider seven cases in which we can replace the pair by a single
2496 * basic map. We ignore all "redundant" constraints.
2498 * 1. all constraints of one basic map are valid
2499 * => the other basic map is a subset and can be removed
2501 * 2. all constraints of both basic maps are either "valid" or "cut"
2502 * and the facets corresponding to the "cut" constraints
2503 * of one of the basic maps lies entirely inside the other basic map
2504 * => the pair can be replaced by a basic map consisting
2505 * of the valid constraints in both basic maps
2507 * 3. there is a single pair of adjacent inequalities
2508 * (all other constraints are "valid")
2509 * => the pair can be replaced by a basic map consisting
2510 * of the valid constraints in both basic maps
2512 * 4. one basic map has a single adjacent inequality, while the other
2513 * constraints are "valid". The other basic map has some
2514 * "cut" constraints, but replacing the adjacent inequality by
2515 * its opposite and adding the valid constraints of the other
2516 * basic map results in a subset of the other basic map
2517 * => the pair can be replaced by a basic map consisting
2518 * of the valid constraints in both basic maps
2520 * 5. there is a single adjacent pair of an inequality and an equality,
2521 * the other constraints of the basic map containing the inequality are
2522 * "valid". Moreover, if the inequality the basic map is relaxed
2523 * and then turned into an equality, then resulting facet lies
2524 * entirely inside the other basic map
2525 * => the pair can be replaced by the basic map containing
2526 * the inequality, with the inequality relaxed.
2528 * 6. there is a single inequality adjacent to an equality,
2529 * the other constraints of the basic map containing the inequality are
2530 * "valid". Moreover, the facets corresponding to both
2531 * the inequality and the equality can be wrapped around their
2532 * ridges to include the other basic map
2533 * => the pair can be replaced by a basic map consisting
2534 * of the valid constraints in both basic maps together
2535 * with all wrapping constraints
2537 * 7. one of the basic maps extends beyond the other by at most one.
2538 * Moreover, the facets corresponding to the cut constraints and
2539 * the pieces of the other basic map at offset one from these cut
2540 * constraints can be wrapped around their ridges to include
2541 * the union of the two basic maps
2542 * => the pair can be replaced by a basic map consisting
2543 * of the valid constraints in both basic maps together
2544 * with all wrapping constraints
2546 * 8. the two basic maps live in adjacent hyperplanes. In principle
2547 * such sets can always be combined through wrapping, but we impose
2548 * that there is only one such pair, to avoid overeager coalescing.
2550 * Throughout the computation, we maintain a collection of tableaus
2551 * corresponding to the basic maps. When the basic maps are dropped
2552 * or combined, the tableaus are modified accordingly.
2554 static enum isl_change coalesce_local_pair_reuse(int i, int j,
2555 struct isl_coalesce_info *info)
2557 enum isl_change change = isl_change_none;
2559 set_ineq_status_in(&info[i], info[j].tab);
2560 if (info[i].bmap->n_ineq && !info[i].ineq)
2561 goto error;
2562 if (any_ineq(&info[i], STATUS_ERROR))
2563 goto error;
2564 if (any_ineq(&info[i], STATUS_SEPARATE))
2565 goto done;
2567 set_ineq_status_in(&info[j], info[i].tab);
2568 if (info[j].bmap->n_ineq && !info[j].ineq)
2569 goto error;
2570 if (any_ineq(&info[j], STATUS_ERROR))
2571 goto error;
2572 if (any_ineq(&info[j], STATUS_SEPARATE))
2573 goto done;
2575 set_eq_status_in(&info[i], info[j].tab);
2576 if (info[i].bmap->n_eq && !info[i].eq)
2577 goto error;
2578 if (any_eq(&info[i], STATUS_ERROR))
2579 goto error;
2581 set_eq_status_in(&info[j], info[i].tab);
2582 if (info[j].bmap->n_eq && !info[j].eq)
2583 goto error;
2584 if (any_eq(&info[j], STATUS_ERROR))
2585 goto error;
2587 if (any_eq(&info[i], STATUS_SEPARATE))
2588 return separating_equality(i, j, info);
2589 if (any_eq(&info[j], STATUS_SEPARATE))
2590 return separating_equality(j, i, info);
2592 if (all(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_VALID) &&
2593 all(info[i].ineq, info[i].bmap->n_ineq, STATUS_VALID)) {
2594 drop(&info[j]);
2595 change = isl_change_drop_second;
2596 } else if (all(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_VALID) &&
2597 all(info[j].ineq, info[j].bmap->n_ineq, STATUS_VALID)) {
2598 drop(&info[i]);
2599 change = isl_change_drop_first;
2600 } else if (any_eq(&info[i], STATUS_ADJ_EQ)) {
2601 change = check_eq_adj_eq(i, j, info);
2602 } else if (any_eq(&info[j], STATUS_ADJ_EQ)) {
2603 change = check_eq_adj_eq(j, i, info);
2604 } else if (any_eq(&info[i], STATUS_ADJ_INEQ) ||
2605 any_eq(&info[j], STATUS_ADJ_INEQ)) {
2606 change = check_adj_eq(i, j, info);
2607 } else if (any_ineq(&info[i], STATUS_ADJ_EQ)) {
2608 change = check_ineq_adj_eq(i, j, info);
2609 } else if (any_ineq(&info[j], STATUS_ADJ_EQ)) {
2610 change = check_ineq_adj_eq(j, i, info);
2611 } else if (any_ineq(&info[i], STATUS_ADJ_INEQ) ||
2612 any_ineq(&info[j], STATUS_ADJ_INEQ)) {
2613 change = check_adj_ineq(i, j, info);
2614 } else {
2615 if (!any_eq(&info[i], STATUS_CUT) &&
2616 !any_eq(&info[j], STATUS_CUT))
2617 change = check_facets(i, j, info);
2618 if (change == isl_change_none)
2619 change = check_wrap(i, j, info);
2622 done:
2623 clear_status(&info[i]);
2624 clear_status(&info[j]);
2625 return change;
2626 error:
2627 clear_status(&info[i]);
2628 clear_status(&info[j]);
2629 return isl_change_error;
2632 /* Check if the union of the given pair of basic maps
2633 * can be represented by a single basic map.
2634 * If so, replace the pair by the single basic map and return
2635 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
2636 * Otherwise, return isl_change_none.
2637 * The two basic maps are assumed to live in the same local space.
2639 static enum isl_change coalesce_local_pair(int i, int j,
2640 struct isl_coalesce_info *info)
2642 init_status(&info[i]);
2643 init_status(&info[j]);
2644 return coalesce_local_pair_reuse(i, j, info);
2647 /* Shift the integer division at position "div" of the basic map
2648 * represented by "info" by "shift".
2650 * That is, if the integer division has the form
2652 * floor(f(x)/d)
2654 * then replace it by
2656 * floor((f(x) + shift * d)/d) - shift
2658 static isl_stat shift_div(struct isl_coalesce_info *info, int div,
2659 isl_int shift)
2661 isl_size total, n_div;
2663 info->bmap = isl_basic_map_shift_div(info->bmap, div, 0, shift);
2664 if (!info->bmap)
2665 return isl_stat_error;
2667 total = isl_basic_map_dim(info->bmap, isl_dim_all);
2668 n_div = isl_basic_map_dim(info->bmap, isl_dim_div);
2669 if (total < 0 || n_div < 0)
2670 return isl_stat_error;
2671 total -= n_div;
2672 if (isl_tab_shift_var(info->tab, total + div, shift) < 0)
2673 return isl_stat_error;
2675 return isl_stat_ok;
2678 /* If the integer division at position "div" is defined by an equality,
2679 * i.e., a stride constraint, then change the integer division expression
2680 * to have a constant term equal to zero.
2682 * Let the equality constraint be
2684 * c + f + m a = 0
2686 * The integer division expression is then typically of the form
2688 * a = floor((-f - c')/m)
2690 * The integer division is first shifted by t = floor(c/m),
2691 * turning the equality constraint into
2693 * c - m floor(c/m) + f + m a' = 0
2695 * i.e.,
2697 * (c mod m) + f + m a' = 0
2699 * That is,
2701 * a' = (-f - (c mod m))/m = floor((-f)/m)
2703 * because a' is an integer and 0 <= (c mod m) < m.
2704 * The constant term of a' can therefore be zeroed out,
2705 * but only if the integer division expression is of the expected form.
2707 static isl_stat normalize_stride_div(struct isl_coalesce_info *info, int div)
2709 isl_bool defined, valid;
2710 isl_stat r;
2711 isl_constraint *c;
2712 isl_int shift, stride;
2714 defined = isl_basic_map_has_defining_equality(info->bmap, isl_dim_div,
2715 div, &c);
2716 if (defined < 0)
2717 return isl_stat_error;
2718 if (!defined)
2719 return isl_stat_ok;
2720 if (!c)
2721 return isl_stat_error;
2722 valid = isl_constraint_is_div_equality(c, div);
2723 isl_int_init(shift);
2724 isl_int_init(stride);
2725 isl_constraint_get_constant(c, &shift);
2726 isl_constraint_get_coefficient(c, isl_dim_div, div, &stride);
2727 isl_int_fdiv_q(shift, shift, stride);
2728 r = shift_div(info, div, shift);
2729 isl_int_clear(stride);
2730 isl_int_clear(shift);
2731 isl_constraint_free(c);
2732 if (r < 0 || valid < 0)
2733 return isl_stat_error;
2734 if (!valid)
2735 return isl_stat_ok;
2736 info->bmap = isl_basic_map_set_div_expr_constant_num_si_inplace(
2737 info->bmap, div, 0);
2738 if (!info->bmap)
2739 return isl_stat_error;
2740 return isl_stat_ok;
2743 /* The basic maps represented by "info1" and "info2" are known
2744 * to have the same number of integer divisions.
2745 * Check if pairs of integer divisions are equal to each other
2746 * despite the fact that they differ by a rational constant.
2748 * In particular, look for any pair of integer divisions that
2749 * only differ in their constant terms.
2750 * If either of these integer divisions is defined
2751 * by stride constraints, then modify it to have a zero constant term.
2752 * If both are defined by stride constraints then in the end they will have
2753 * the same (zero) constant term.
2755 static isl_stat harmonize_stride_divs(struct isl_coalesce_info *info1,
2756 struct isl_coalesce_info *info2)
2758 int i;
2759 isl_size n;
2761 n = isl_basic_map_dim(info1->bmap, isl_dim_div);
2762 if (n < 0)
2763 return isl_stat_error;
2764 for (i = 0; i < n; ++i) {
2765 isl_bool known, harmonize;
2767 known = isl_basic_map_div_is_known(info1->bmap, i);
2768 if (known >= 0 && known)
2769 known = isl_basic_map_div_is_known(info2->bmap, i);
2770 if (known < 0)
2771 return isl_stat_error;
2772 if (!known)
2773 continue;
2774 harmonize = isl_basic_map_equal_div_expr_except_constant(
2775 info1->bmap, i, info2->bmap, i);
2776 if (harmonize < 0)
2777 return isl_stat_error;
2778 if (!harmonize)
2779 continue;
2780 if (normalize_stride_div(info1, i) < 0)
2781 return isl_stat_error;
2782 if (normalize_stride_div(info2, i) < 0)
2783 return isl_stat_error;
2786 return isl_stat_ok;
2789 /* If "shift" is an integer constant, then shift the integer division
2790 * at position "div" of the basic map represented by "info" by "shift".
2791 * If "shift" is not an integer constant, then do nothing.
2792 * If "shift" is equal to zero, then no shift needs to be performed either.
2794 * That is, if the integer division has the form
2796 * floor(f(x)/d)
2798 * then replace it by
2800 * floor((f(x) + shift * d)/d) - shift
2802 static isl_stat shift_if_cst_int(struct isl_coalesce_info *info, int div,
2803 __isl_keep isl_aff *shift)
2805 isl_bool cst;
2806 isl_stat r;
2807 isl_int d;
2808 isl_val *c;
2810 cst = isl_aff_is_cst(shift);
2811 if (cst < 0 || !cst)
2812 return cst < 0 ? isl_stat_error : isl_stat_ok;
2814 c = isl_aff_get_constant_val(shift);
2815 cst = isl_val_is_int(c);
2816 if (cst >= 0 && cst)
2817 cst = isl_bool_not(isl_val_is_zero(c));
2818 if (cst < 0 || !cst) {
2819 isl_val_free(c);
2820 return cst < 0 ? isl_stat_error : isl_stat_ok;
2823 isl_int_init(d);
2824 r = isl_val_get_num_isl_int(c, &d);
2825 if (r >= 0)
2826 r = shift_div(info, div, d);
2827 isl_int_clear(d);
2829 isl_val_free(c);
2831 return r;
2834 /* Check if some of the divs in the basic map represented by "info1"
2835 * are shifts of the corresponding divs in the basic map represented
2836 * by "info2", taking into account the equality constraints "eq1" of "info1"
2837 * and "eq2" of "info2". If so, align them with those of "info2".
2838 * "info1" and "info2" are assumed to have the same number
2839 * of integer divisions.
2841 * An integer division is considered to be a shift of another integer
2842 * division if, after simplification with respect to the equality
2843 * constraints of the other basic map, one is equal to the other
2844 * plus a constant.
2846 * In particular, for each pair of integer divisions, if both are known,
2847 * have the same denominator and are not already equal to each other,
2848 * simplify each with respect to the equality constraints
2849 * of the other basic map. If the difference is an integer constant,
2850 * then move this difference outside.
2851 * That is, if, after simplification, one integer division is of the form
2853 * floor((f(x) + c_1)/d)
2855 * while the other is of the form
2857 * floor((f(x) + c_2)/d)
2859 * and n = (c_2 - c_1)/d is an integer, then replace the first
2860 * integer division by
2862 * floor((f_1(x) + c_1 + n * d)/d) - n,
2864 * where floor((f_1(x) + c_1 + n * d)/d) = floor((f2(x) + c_2)/d)
2865 * after simplification with respect to the equality constraints.
2867 static isl_stat harmonize_divs_with_hulls(struct isl_coalesce_info *info1,
2868 struct isl_coalesce_info *info2, __isl_keep isl_basic_set *eq1,
2869 __isl_keep isl_basic_set *eq2)
2871 int i;
2872 isl_size total;
2873 isl_local_space *ls1, *ls2;
2875 total = isl_basic_map_dim(info1->bmap, isl_dim_all);
2876 if (total < 0)
2877 return isl_stat_error;
2878 ls1 = isl_local_space_wrap(isl_basic_map_get_local_space(info1->bmap));
2879 ls2 = isl_local_space_wrap(isl_basic_map_get_local_space(info2->bmap));
2880 for (i = 0; i < info1->bmap->n_div; ++i) {
2881 isl_stat r;
2882 isl_aff *div1, *div2;
2884 if (!isl_local_space_div_is_known(ls1, i) ||
2885 !isl_local_space_div_is_known(ls2, i))
2886 continue;
2887 if (isl_int_ne(info1->bmap->div[i][0], info2->bmap->div[i][0]))
2888 continue;
2889 if (isl_seq_eq(info1->bmap->div[i] + 1,
2890 info2->bmap->div[i] + 1, 1 + total))
2891 continue;
2892 div1 = isl_local_space_get_div(ls1, i);
2893 div2 = isl_local_space_get_div(ls2, i);
2894 div1 = isl_aff_substitute_equalities(div1,
2895 isl_basic_set_copy(eq2));
2896 div2 = isl_aff_substitute_equalities(div2,
2897 isl_basic_set_copy(eq1));
2898 div2 = isl_aff_sub(div2, div1);
2899 r = shift_if_cst_int(info1, i, div2);
2900 isl_aff_free(div2);
2901 if (r < 0)
2902 break;
2904 isl_local_space_free(ls1);
2905 isl_local_space_free(ls2);
2907 if (i < info1->bmap->n_div)
2908 return isl_stat_error;
2909 return isl_stat_ok;
2912 /* Check if some of the divs in the basic map represented by "info1"
2913 * are shifts of the corresponding divs in the basic map represented
2914 * by "info2". If so, align them with those of "info2".
2915 * Only do this if "info1" and "info2" have the same number
2916 * of integer divisions.
2918 * An integer division is considered to be a shift of another integer
2919 * division if, after simplification with respect to the equality
2920 * constraints of the other basic map, one is equal to the other
2921 * plus a constant.
2923 * First check if pairs of integer divisions are equal to each other
2924 * despite the fact that they differ by a rational constant.
2925 * If so, try and arrange for them to have the same constant term.
2927 * Then, extract the equality constraints and continue with
2928 * harmonize_divs_with_hulls.
2930 * If the equality constraints of both basic maps are the same,
2931 * then there is no need to perform any shifting since
2932 * the coefficients of the integer divisions should have been
2933 * reduced in the same way.
2935 static isl_stat harmonize_divs(struct isl_coalesce_info *info1,
2936 struct isl_coalesce_info *info2)
2938 isl_bool equal;
2939 isl_basic_map *bmap1, *bmap2;
2940 isl_basic_set *eq1, *eq2;
2941 isl_stat r;
2943 if (!info1->bmap || !info2->bmap)
2944 return isl_stat_error;
2946 if (info1->bmap->n_div != info2->bmap->n_div)
2947 return isl_stat_ok;
2948 if (info1->bmap->n_div == 0)
2949 return isl_stat_ok;
2951 if (harmonize_stride_divs(info1, info2) < 0)
2952 return isl_stat_error;
2954 bmap1 = isl_basic_map_copy(info1->bmap);
2955 bmap2 = isl_basic_map_copy(info2->bmap);
2956 eq1 = isl_basic_map_wrap(isl_basic_map_plain_affine_hull(bmap1));
2957 eq2 = isl_basic_map_wrap(isl_basic_map_plain_affine_hull(bmap2));
2958 equal = isl_basic_set_plain_is_equal(eq1, eq2);
2959 if (equal < 0)
2960 r = isl_stat_error;
2961 else if (equal)
2962 r = isl_stat_ok;
2963 else
2964 r = harmonize_divs_with_hulls(info1, info2, eq1, eq2);
2965 isl_basic_set_free(eq1);
2966 isl_basic_set_free(eq2);
2968 return r;
2971 /* Do the two basic maps live in the same local space, i.e.,
2972 * do they have the same (known) divs?
2973 * If either basic map has any unknown divs, then we can only assume
2974 * that they do not live in the same local space.
2976 static isl_bool same_divs(__isl_keep isl_basic_map *bmap1,
2977 __isl_keep isl_basic_map *bmap2)
2979 int i;
2980 isl_bool known;
2981 isl_size total;
2983 if (!bmap1 || !bmap2)
2984 return isl_bool_error;
2985 if (bmap1->n_div != bmap2->n_div)
2986 return isl_bool_false;
2988 if (bmap1->n_div == 0)
2989 return isl_bool_true;
2991 known = isl_basic_map_divs_known(bmap1);
2992 if (known < 0 || !known)
2993 return known;
2994 known = isl_basic_map_divs_known(bmap2);
2995 if (known < 0 || !known)
2996 return known;
2998 total = isl_basic_map_dim(bmap1, isl_dim_all);
2999 if (total < 0)
3000 return isl_bool_error;
3001 for (i = 0; i < bmap1->n_div; ++i)
3002 if (!isl_seq_eq(bmap1->div[i], bmap2->div[i], 2 + total))
3003 return isl_bool_false;
3005 return isl_bool_true;
3008 /* Assuming that "tab" contains the equality constraints and
3009 * the initial inequality constraints of "bmap", copy the remaining
3010 * inequality constraints of "bmap" to "Tab".
3012 static isl_stat copy_ineq(struct isl_tab *tab, __isl_keep isl_basic_map *bmap)
3014 int i, n_ineq;
3016 if (!bmap)
3017 return isl_stat_error;
3019 n_ineq = tab->n_con - tab->n_eq;
3020 for (i = n_ineq; i < bmap->n_ineq; ++i)
3021 if (isl_tab_add_ineq(tab, bmap->ineq[i]) < 0)
3022 return isl_stat_error;
3024 return isl_stat_ok;
3027 /* Description of an integer division that is added
3028 * during an expansion.
3029 * "pos" is the position of the corresponding variable.
3030 * "cst" indicates whether this integer division has a fixed value.
3031 * "val" contains the fixed value, if the value is fixed.
3033 struct isl_expanded {
3034 int pos;
3035 isl_bool cst;
3036 isl_int val;
3039 /* For each of the "n" integer division variables "expanded",
3040 * if the variable has a fixed value, then add two inequality
3041 * constraints expressing the fixed value.
3042 * Otherwise, add the corresponding div constraints.
3043 * The caller is responsible for removing the div constraints
3044 * that it added for all these "n" integer divisions.
3046 * The div constraints and the pair of inequality constraints
3047 * forcing the fixed value cannot both be added for a given variable
3048 * as the combination may render some of the original constraints redundant.
3049 * These would then be ignored during the coalescing detection,
3050 * while they could remain in the fused result.
3052 * The two added inequality constraints are
3054 * -a + v >= 0
3055 * a - v >= 0
3057 * with "a" the variable and "v" its fixed value.
3058 * The facet corresponding to one of these two constraints is selected
3059 * in the tableau to ensure that the pair of inequality constraints
3060 * is treated as an equality constraint.
3061 * Such implicit equality constraints need to be turned
3062 * into explicit equality constraints to ensure both sides
3063 * of the equality constraints are taken into account.
3065 * The information in info->ineq is thrown away because it was
3066 * computed in terms of div constraints, while some of those
3067 * have now been replaced by these pairs of inequality constraints.
3069 static isl_stat fix_constant_divs(struct isl_coalesce_info *info,
3070 int n, struct isl_expanded *expanded)
3072 unsigned o_div;
3073 int i;
3074 isl_vec *ineq;
3076 o_div = isl_basic_map_offset(info->bmap, isl_dim_div) - 1;
3077 ineq = isl_vec_alloc(isl_tab_get_ctx(info->tab), 1 + info->tab->n_var);
3078 if (!ineq)
3079 return isl_stat_error;
3080 isl_seq_clr(ineq->el + 1, info->tab->n_var);
3082 for (i = 0; i < n; ++i) {
3083 if (!expanded[i].cst) {
3084 info->bmap = isl_basic_map_extend_constraints(
3085 info->bmap, 0, 2);
3086 info->bmap = isl_basic_map_add_div_constraints(
3087 info->bmap, expanded[i].pos - o_div);
3088 } else {
3089 isl_int_set_si(ineq->el[1 + expanded[i].pos], -1);
3090 isl_int_set(ineq->el[0], expanded[i].val);
3091 info->bmap = isl_basic_map_add_ineq(info->bmap,
3092 ineq->el);
3093 isl_int_set_si(ineq->el[1 + expanded[i].pos], 1);
3094 isl_int_neg(ineq->el[0], expanded[i].val);
3095 info->bmap = isl_basic_map_add_ineq(info->bmap,
3096 ineq->el);
3097 isl_int_set_si(ineq->el[1 + expanded[i].pos], 0);
3099 if (copy_ineq(info->tab, info->bmap) < 0)
3100 break;
3101 if (expanded[i].cst &&
3102 isl_tab_select_facet(info->tab, info->tab->n_con - 1) < 0)
3103 break;
3106 isl_vec_free(ineq);
3108 clear_status(info);
3109 init_status(info);
3111 info->bmap = isl_tab_make_equalities_explicit(info->tab, info->bmap);
3113 return i < n ? isl_stat_error : isl_stat_ok;
3116 /* Insert the "n" integer division variables "expanded"
3117 * into info->tab and info->bmap and
3118 * update info->ineq with respect to the redundant constraints
3119 * in the resulting tableau.
3120 * "bmap" contains the result of this insertion in info->bmap,
3121 * while info->bmap is the original version
3122 * of "bmap", i.e., the one that corresponds to the current
3123 * state of info->tab. The number of constraints in info->bmap
3124 * is assumed to be the same as the number of constraints
3125 * in info->tab. This is required to be able to detect
3126 * the extra constraints in "bmap".
3128 * In particular, introduce extra variables corresponding
3129 * to the extra integer divisions and add the div constraints
3130 * that were added to "bmap" after info->tab was created
3131 * from info->bmap.
3132 * Furthermore, check if these extra integer divisions happen
3133 * to attain a fixed integer value in info->tab.
3134 * If so, replace the corresponding div constraints by pairs
3135 * of inequality constraints that fix these
3136 * integer divisions to their single integer values.
3137 * Replace info->bmap by "bmap" to match the changes to info->tab.
3138 * info->ineq was computed without a tableau and therefore
3139 * does not take into account the redundant constraints
3140 * in the tableau. Mark them here.
3141 * There is no need to check the newly added div constraints
3142 * since they cannot be redundant.
3143 * The redundancy check is not performed when constants have been discovered
3144 * since info->ineq is completely thrown away in this case.
3146 static isl_stat tab_insert_divs(struct isl_coalesce_info *info,
3147 int n, struct isl_expanded *expanded, __isl_take isl_basic_map *bmap)
3149 int i, n_ineq;
3150 unsigned n_eq;
3151 struct isl_tab_undo *snap;
3152 int any;
3154 if (!bmap)
3155 return isl_stat_error;
3156 if (info->bmap->n_eq + info->bmap->n_ineq != info->tab->n_con)
3157 isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
3158 "original tableau does not correspond "
3159 "to original basic map", goto error);
3161 if (isl_tab_extend_vars(info->tab, n) < 0)
3162 goto error;
3163 if (isl_tab_extend_cons(info->tab, 2 * n) < 0)
3164 goto error;
3166 for (i = 0; i < n; ++i) {
3167 if (isl_tab_insert_var(info->tab, expanded[i].pos) < 0)
3168 goto error;
3171 snap = isl_tab_snap(info->tab);
3173 n_ineq = info->tab->n_con - info->tab->n_eq;
3174 if (copy_ineq(info->tab, bmap) < 0)
3175 goto error;
3177 isl_basic_map_free(info->bmap);
3178 info->bmap = bmap;
3180 any = 0;
3181 for (i = 0; i < n; ++i) {
3182 expanded[i].cst = isl_tab_is_constant(info->tab,
3183 expanded[i].pos, &expanded[i].val);
3184 if (expanded[i].cst < 0)
3185 return isl_stat_error;
3186 if (expanded[i].cst)
3187 any = 1;
3190 if (any) {
3191 if (isl_tab_rollback(info->tab, snap) < 0)
3192 return isl_stat_error;
3193 info->bmap = isl_basic_map_cow(info->bmap);
3194 info->bmap = isl_basic_map_free_inequality(info->bmap, 2 * n);
3195 if (!info->bmap)
3196 return isl_stat_error;
3198 return fix_constant_divs(info, n, expanded);
3201 n_eq = info->bmap->n_eq;
3202 for (i = 0; i < n_ineq; ++i) {
3203 if (isl_tab_is_redundant(info->tab, n_eq + i))
3204 info->ineq[i] = STATUS_REDUNDANT;
3207 return isl_stat_ok;
3208 error:
3209 isl_basic_map_free(bmap);
3210 return isl_stat_error;
3213 /* Expand info->tab and info->bmap in the same way "bmap" was expanded
3214 * in isl_basic_map_expand_divs using the expansion "exp" and
3215 * update info->ineq with respect to the redundant constraints
3216 * in the resulting tableau. info->bmap is the original version
3217 * of "bmap", i.e., the one that corresponds to the current
3218 * state of info->tab. The number of constraints in info->bmap
3219 * is assumed to be the same as the number of constraints
3220 * in info->tab. This is required to be able to detect
3221 * the extra constraints in "bmap".
3223 * Extract the positions where extra local variables are introduced
3224 * from "exp" and call tab_insert_divs.
3226 static isl_stat expand_tab(struct isl_coalesce_info *info, int *exp,
3227 __isl_take isl_basic_map *bmap)
3229 isl_ctx *ctx;
3230 struct isl_expanded *expanded;
3231 int i, j, k, n;
3232 int extra_var;
3233 isl_size total, n_div;
3234 unsigned pos;
3235 isl_stat r;
3237 total = isl_basic_map_dim(bmap, isl_dim_all);
3238 n_div = isl_basic_map_dim(bmap, isl_dim_div);
3239 if (total < 0 || n_div < 0)
3240 return isl_stat_error;
3241 pos = total - n_div;
3242 extra_var = total - info->tab->n_var;
3243 n = n_div - extra_var;
3245 ctx = isl_basic_map_get_ctx(bmap);
3246 expanded = isl_calloc_array(ctx, struct isl_expanded, extra_var);
3247 if (extra_var && !expanded)
3248 goto error;
3250 i = 0;
3251 k = 0;
3252 for (j = 0; j < n_div; ++j) {
3253 if (i < n && exp[i] == j) {
3254 ++i;
3255 continue;
3257 expanded[k++].pos = pos + j;
3260 for (k = 0; k < extra_var; ++k)
3261 isl_int_init(expanded[k].val);
3263 r = tab_insert_divs(info, extra_var, expanded, bmap);
3265 for (k = 0; k < extra_var; ++k)
3266 isl_int_clear(expanded[k].val);
3267 free(expanded);
3269 return r;
3270 error:
3271 isl_basic_map_free(bmap);
3272 return isl_stat_error;
3275 /* Check if the union of the basic maps represented by info[i] and info[j]
3276 * can be represented by a single basic map,
3277 * after expanding the divs of info[i] to match those of info[j].
3278 * If so, replace the pair by the single basic map and return
3279 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
3280 * Otherwise, return isl_change_none.
3282 * The caller has already checked for info[j] being a subset of info[i].
3283 * If some of the divs of info[j] are unknown, then the expanded info[i]
3284 * will not have the corresponding div constraints. The other patterns
3285 * therefore cannot apply. Skip the computation in this case.
3287 * The expansion is performed using the divs "div" and expansion "exp"
3288 * computed by the caller.
3289 * info[i].bmap has already been expanded and the result is passed in
3290 * as "bmap".
3291 * The "eq" and "ineq" fields of info[i] reflect the status of
3292 * the constraints of the expanded "bmap" with respect to info[j].tab.
3293 * However, inequality constraints that are redundant in info[i].tab
3294 * have not yet been marked as such because no tableau was available.
3296 * Replace info[i].bmap by "bmap" and expand info[i].tab as well,
3297 * updating info[i].ineq with respect to the redundant constraints.
3298 * Then try and coalesce the expanded info[i] with info[j],
3299 * reusing the information in info[i].eq and info[i].ineq.
3300 * If this does not result in any coalescing or if it results in info[j]
3301 * getting dropped (which should not happen in practice, since the case
3302 * of info[j] being a subset of info[i] has already been checked by
3303 * the caller), then revert info[i] to its original state.
3305 static enum isl_change coalesce_expand_tab_divs(__isl_take isl_basic_map *bmap,
3306 int i, int j, struct isl_coalesce_info *info, __isl_keep isl_mat *div,
3307 int *exp)
3309 isl_bool known;
3310 isl_basic_map *bmap_i;
3311 struct isl_tab_undo *snap;
3312 enum isl_change change = isl_change_none;
3314 known = isl_basic_map_divs_known(info[j].bmap);
3315 if (known < 0 || !known) {
3316 clear_status(&info[i]);
3317 isl_basic_map_free(bmap);
3318 return known < 0 ? isl_change_error : isl_change_none;
3321 bmap_i = isl_basic_map_copy(info[i].bmap);
3322 snap = isl_tab_snap(info[i].tab);
3323 if (expand_tab(&info[i], exp, bmap) < 0)
3324 change = isl_change_error;
3326 init_status(&info[j]);
3327 if (change == isl_change_none)
3328 change = coalesce_local_pair_reuse(i, j, info);
3329 else
3330 clear_status(&info[i]);
3331 if (change != isl_change_none && change != isl_change_drop_second) {
3332 isl_basic_map_free(bmap_i);
3333 } else {
3334 isl_basic_map_free(info[i].bmap);
3335 info[i].bmap = bmap_i;
3337 if (isl_tab_rollback(info[i].tab, snap) < 0)
3338 change = isl_change_error;
3341 return change;
3344 /* Check if the union of "bmap" and the basic map represented by info[j]
3345 * can be represented by a single basic map,
3346 * after expanding the divs of "bmap" to match those of info[j].
3347 * If so, replace the pair by the single basic map and return
3348 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
3349 * Otherwise, return isl_change_none.
3351 * In particular, check if the expanded "bmap" contains the basic map
3352 * represented by the tableau info[j].tab.
3353 * The expansion is performed using the divs "div" and expansion "exp"
3354 * computed by the caller.
3355 * Then we check if all constraints of the expanded "bmap" are valid for
3356 * info[j].tab.
3358 * If "i" is not equal to -1, then "bmap" is equal to info[i].bmap.
3359 * In this case, the positions of the constraints of info[i].bmap
3360 * with respect to the basic map represented by info[j] are stored
3361 * in info[i].
3363 * If the expanded "bmap" does not contain the basic map
3364 * represented by the tableau info[j].tab and if "i" is not -1,
3365 * i.e., if the original "bmap" is info[i].bmap, then expand info[i].tab
3366 * as well and check if that results in coalescing.
3368 static enum isl_change coalesce_with_expanded_divs(
3369 __isl_keep isl_basic_map *bmap, int i, int j,
3370 struct isl_coalesce_info *info, __isl_keep isl_mat *div, int *exp)
3372 enum isl_change change = isl_change_none;
3373 struct isl_coalesce_info info_local, *info_i;
3375 info_i = i >= 0 ? &info[i] : &info_local;
3376 init_status(info_i);
3377 bmap = isl_basic_map_copy(bmap);
3378 bmap = isl_basic_map_expand_divs(bmap, isl_mat_copy(div), exp);
3379 bmap = isl_basic_map_mark_final(bmap);
3381 if (!bmap)
3382 goto error;
3384 info_local.bmap = bmap;
3385 info_i->eq = eq_status_in(bmap, info[j].tab);
3386 if (bmap->n_eq && !info_i->eq)
3387 goto error;
3388 if (any_eq(info_i, STATUS_ERROR))
3389 goto error;
3390 if (any_eq(info_i, STATUS_SEPARATE))
3391 goto done;
3393 info_i->ineq = ineq_status_in(bmap, NULL, info[j].tab);
3394 if (bmap->n_ineq && !info_i->ineq)
3395 goto error;
3396 if (any_ineq(info_i, STATUS_ERROR))
3397 goto error;
3398 if (any_ineq(info_i, STATUS_SEPARATE))
3399 goto done;
3401 if (all(info_i->eq, 2 * bmap->n_eq, STATUS_VALID) &&
3402 all(info_i->ineq, bmap->n_ineq, STATUS_VALID)) {
3403 drop(&info[j]);
3404 change = isl_change_drop_second;
3407 if (change == isl_change_none && i != -1)
3408 return coalesce_expand_tab_divs(bmap, i, j, info, div, exp);
3410 done:
3411 isl_basic_map_free(bmap);
3412 clear_status(info_i);
3413 return change;
3414 error:
3415 isl_basic_map_free(bmap);
3416 clear_status(info_i);
3417 return isl_change_error;
3420 /* Check if the union of "bmap_i" and the basic map represented by info[j]
3421 * can be represented by a single basic map,
3422 * after aligning the divs of "bmap_i" to match those of info[j].
3423 * If so, replace the pair by the single basic map and return
3424 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
3425 * Otherwise, return isl_change_none.
3427 * In particular, check if "bmap_i" contains the basic map represented by
3428 * info[j] after aligning the divs of "bmap_i" to those of info[j].
3429 * Note that this can only succeed if the number of divs of "bmap_i"
3430 * is smaller than (or equal to) the number of divs of info[j].
3432 * We first check if the divs of "bmap_i" are all known and form a subset
3433 * of those of info[j].bmap. If so, we pass control over to
3434 * coalesce_with_expanded_divs.
3436 * If "i" is not equal to -1, then "bmap" is equal to info[i].bmap.
3438 static enum isl_change coalesce_after_aligning_divs(
3439 __isl_keep isl_basic_map *bmap_i, int i, int j,
3440 struct isl_coalesce_info *info)
3442 isl_bool known;
3443 isl_mat *div_i, *div_j, *div;
3444 int *exp1 = NULL;
3445 int *exp2 = NULL;
3446 isl_ctx *ctx;
3447 enum isl_change change;
3449 known = isl_basic_map_divs_known(bmap_i);
3450 if (known < 0)
3451 return isl_change_error;
3452 if (!known)
3453 return isl_change_none;
3455 ctx = isl_basic_map_get_ctx(bmap_i);
3457 div_i = isl_basic_map_get_divs(bmap_i);
3458 div_j = isl_basic_map_get_divs(info[j].bmap);
3460 if (!div_i || !div_j)
3461 goto error;
3463 exp1 = isl_alloc_array(ctx, int, div_i->n_row);
3464 exp2 = isl_alloc_array(ctx, int, div_j->n_row);
3465 if ((div_i->n_row && !exp1) || (div_j->n_row && !exp2))
3466 goto error;
3468 div = isl_merge_divs(div_i, div_j, exp1, exp2);
3469 if (!div)
3470 goto error;
3472 if (div->n_row == div_j->n_row)
3473 change = coalesce_with_expanded_divs(bmap_i,
3474 i, j, info, div, exp1);
3475 else
3476 change = isl_change_none;
3478 isl_mat_free(div);
3480 isl_mat_free(div_i);
3481 isl_mat_free(div_j);
3483 free(exp2);
3484 free(exp1);
3486 return change;
3487 error:
3488 isl_mat_free(div_i);
3489 isl_mat_free(div_j);
3490 free(exp1);
3491 free(exp2);
3492 return isl_change_error;
3495 /* Check if basic map "j" is a subset of basic map "i" after
3496 * exploiting the extra equalities of "j" to simplify the divs of "i".
3497 * If so, remove basic map "j" and return isl_change_drop_second.
3499 * If "j" does not have any equalities or if they are the same
3500 * as those of "i", then we cannot exploit them to simplify the divs.
3501 * Similarly, if there are no divs in "i", then they cannot be simplified.
3502 * If, on the other hand, the affine hulls of "i" and "j" do not intersect,
3503 * then "j" cannot be a subset of "i".
3505 * Otherwise, we intersect "i" with the affine hull of "j" and then
3506 * check if "j" is a subset of the result after aligning the divs.
3507 * If so, then "j" is definitely a subset of "i" and can be removed.
3508 * Note that if after intersection with the affine hull of "j".
3509 * "i" still has more divs than "j", then there is no way we can
3510 * align the divs of "i" to those of "j".
3512 static enum isl_change coalesce_subset_with_equalities(int i, int j,
3513 struct isl_coalesce_info *info)
3515 isl_basic_map *hull_i, *hull_j, *bmap_i;
3516 int equal, empty;
3517 enum isl_change change;
3519 if (info[j].bmap->n_eq == 0)
3520 return isl_change_none;
3521 if (info[i].bmap->n_div == 0)
3522 return isl_change_none;
3524 hull_i = isl_basic_map_copy(info[i].bmap);
3525 hull_i = isl_basic_map_plain_affine_hull(hull_i);
3526 hull_j = isl_basic_map_copy(info[j].bmap);
3527 hull_j = isl_basic_map_plain_affine_hull(hull_j);
3529 hull_j = isl_basic_map_intersect(hull_j, isl_basic_map_copy(hull_i));
3530 equal = isl_basic_map_plain_is_equal(hull_i, hull_j);
3531 empty = isl_basic_map_plain_is_empty(hull_j);
3532 isl_basic_map_free(hull_i);
3534 if (equal < 0 || equal || empty < 0 || empty) {
3535 isl_basic_map_free(hull_j);
3536 if (equal < 0 || empty < 0)
3537 return isl_change_error;
3538 return isl_change_none;
3541 bmap_i = isl_basic_map_copy(info[i].bmap);
3542 bmap_i = isl_basic_map_intersect(bmap_i, hull_j);
3543 if (!bmap_i)
3544 return isl_change_error;
3546 if (bmap_i->n_div > info[j].bmap->n_div) {
3547 isl_basic_map_free(bmap_i);
3548 return isl_change_none;
3551 change = coalesce_after_aligning_divs(bmap_i, -1, j, info);
3553 isl_basic_map_free(bmap_i);
3555 return change;
3558 /* Check if the union of the basic maps represented by info[i] and info[j]
3559 * can be represented by a single basic map, by aligning or equating
3560 * their integer divisions.
3561 * If so, replace the pair by the single basic map and return
3562 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
3563 * Otherwise, return isl_change_none.
3565 * Note that we only perform any test if the number of divs is different
3566 * in the two basic maps. In case the number of divs is the same,
3567 * we have already established that the divs are different
3568 * in the two basic maps.
3569 * In particular, if the number of divs of basic map i is smaller than
3570 * the number of divs of basic map j, then we check if j is a subset of i
3571 * and vice versa.
3573 static enum isl_change coalesce_divs(int i, int j,
3574 struct isl_coalesce_info *info)
3576 enum isl_change change = isl_change_none;
3578 if (info[i].bmap->n_div < info[j].bmap->n_div)
3579 change = coalesce_after_aligning_divs(info[i].bmap, i, j, info);
3580 if (change != isl_change_none)
3581 return change;
3583 if (info[j].bmap->n_div < info[i].bmap->n_div)
3584 change = coalesce_after_aligning_divs(info[j].bmap, j, i, info);
3585 if (change != isl_change_none)
3586 return invert_change(change);
3588 change = coalesce_subset_with_equalities(i, j, info);
3589 if (change != isl_change_none)
3590 return change;
3592 change = coalesce_subset_with_equalities(j, i, info);
3593 if (change != isl_change_none)
3594 return invert_change(change);
3596 return isl_change_none;
3599 /* Does "bmap" involve any divs that themselves refer to divs?
3601 static isl_bool has_nested_div(__isl_keep isl_basic_map *bmap)
3603 isl_size total;
3604 isl_size n_div;
3606 total = isl_basic_map_dim(bmap, isl_dim_all);
3607 n_div = isl_basic_map_dim(bmap, isl_dim_div);
3608 if (total < 0 || n_div < 0)
3609 return isl_bool_error;
3610 total -= n_div;
3612 return isl_basic_map_any_div_involves_vars(bmap, total, n_div);
3615 /* Return a list of affine expressions, one for each integer division
3616 * in "bmap_i". For each integer division that also appears in "bmap_j",
3617 * the affine expression is set to NaN. The number of NaNs in the list
3618 * is equal to the number of integer divisions in "bmap_j".
3619 * For the other integer divisions of "bmap_i", the corresponding
3620 * element in the list is a purely affine expression equal to the integer
3621 * division in "hull".
3622 * If no such list can be constructed, then the number of elements
3623 * in the returned list is smaller than the number of integer divisions
3624 * in "bmap_i".
3625 * The integer division of "bmap_i" and "bmap_j" are assumed to be known and
3626 * not contain any nested divs.
3628 static __isl_give isl_aff_list *set_up_substitutions(
3629 __isl_keep isl_basic_map *bmap_i, __isl_keep isl_basic_map *bmap_j,
3630 __isl_take isl_basic_map *hull)
3632 isl_size n_div_i, n_div_j, total;
3633 isl_ctx *ctx;
3634 isl_local_space *ls;
3635 isl_basic_set *wrap_hull;
3636 isl_aff *aff_nan;
3637 isl_aff_list *list;
3638 int i, j;
3640 n_div_i = isl_basic_map_dim(bmap_i, isl_dim_div);
3641 n_div_j = isl_basic_map_dim(bmap_j, isl_dim_div);
3642 total = isl_basic_map_dim(bmap_i, isl_dim_all);
3643 if (!hull || n_div_i < 0 || n_div_j < 0 || total < 0)
3644 return NULL;
3646 ctx = isl_basic_map_get_ctx(hull);
3647 total -= n_div_i;
3649 ls = isl_basic_map_get_local_space(bmap_i);
3650 ls = isl_local_space_wrap(ls);
3651 wrap_hull = isl_basic_map_wrap(hull);
3653 aff_nan = isl_aff_nan_on_domain(isl_local_space_copy(ls));
3654 list = isl_aff_list_alloc(ctx, n_div_i);
3656 j = 0;
3657 for (i = 0; i < n_div_i; ++i) {
3658 isl_aff *aff;
3659 isl_size n_div;
3661 if (j < n_div_j &&
3662 isl_basic_map_equal_div_expr_part(bmap_i, i, bmap_j, j,
3663 0, 2 + total)) {
3664 ++j;
3665 list = isl_aff_list_add(list, isl_aff_copy(aff_nan));
3666 continue;
3668 if (n_div_i - i <= n_div_j - j)
3669 break;
3671 aff = isl_local_space_get_div(ls, i);
3672 aff = isl_aff_substitute_equalities(aff,
3673 isl_basic_set_copy(wrap_hull));
3674 aff = isl_aff_floor(aff);
3675 n_div = isl_aff_dim(aff, isl_dim_div);
3676 if (n_div < 0)
3677 goto error;
3678 if (n_div != 0) {
3679 isl_aff_free(aff);
3680 break;
3683 list = isl_aff_list_add(list, aff);
3686 isl_aff_free(aff_nan);
3687 isl_local_space_free(ls);
3688 isl_basic_set_free(wrap_hull);
3690 return list;
3691 error:
3692 isl_aff_free(aff_nan);
3693 isl_local_space_free(ls);
3694 isl_basic_set_free(wrap_hull);
3695 isl_aff_list_free(list);
3696 return NULL;
3699 /* Add variables to info->bmap and info->tab corresponding to the elements
3700 * in "list" that are not set to NaN.
3701 * "extra_var" is the number of these elements.
3702 * "dim" is the offset in the variables of "tab" where we should
3703 * start considering the elements in "list".
3704 * When this function returns, the total number of variables in "tab"
3705 * is equal to "dim" plus the number of elements in "list".
3707 * The newly added existentially quantified variables are not given
3708 * an explicit representation because the corresponding div constraints
3709 * do not appear in info->bmap. These constraints are not added
3710 * to info->bmap because for internal consistency, they would need to
3711 * be added to info->tab as well, where they could combine with the equality
3712 * that is added later to result in constraints that do not hold
3713 * in the original input.
3715 static isl_stat add_sub_vars(struct isl_coalesce_info *info,
3716 __isl_keep isl_aff_list *list, int dim, int extra_var)
3718 int i, j, d;
3719 isl_size n;
3721 info->bmap = isl_basic_map_cow(info->bmap);
3722 info->bmap = isl_basic_map_extend(info->bmap, extra_var, 0, 0);
3723 n = isl_aff_list_n_aff(list);
3724 if (!info->bmap || n < 0)
3725 return isl_stat_error;
3726 for (i = 0; i < n; ++i) {
3727 int is_nan;
3728 isl_aff *aff;
3730 aff = isl_aff_list_get_aff(list, i);
3731 is_nan = isl_aff_is_nan(aff);
3732 isl_aff_free(aff);
3733 if (is_nan < 0)
3734 return isl_stat_error;
3735 if (is_nan)
3736 continue;
3738 if (isl_tab_insert_var(info->tab, dim + i) < 0)
3739 return isl_stat_error;
3740 d = isl_basic_map_alloc_div(info->bmap);
3741 if (d < 0)
3742 return isl_stat_error;
3743 info->bmap = isl_basic_map_mark_div_unknown(info->bmap, d);
3744 for (j = d; j > i; --j)
3745 info->bmap = isl_basic_map_swap_div(info->bmap,
3746 j - 1, j);
3747 if (!info->bmap)
3748 return isl_stat_error;
3751 return isl_stat_ok;
3754 /* For each element in "list" that is not set to NaN, fix the corresponding
3755 * variable in "tab" to the purely affine expression defined by the element.
3756 * "dim" is the offset in the variables of "tab" where we should
3757 * start considering the elements in "list".
3759 * This function assumes that a sufficient number of rows and
3760 * elements in the constraint array are available in the tableau.
3762 static isl_stat add_sub_equalities(struct isl_tab *tab,
3763 __isl_keep isl_aff_list *list, int dim)
3765 int i;
3766 isl_size n;
3767 isl_ctx *ctx;
3768 isl_vec *sub;
3769 isl_aff *aff;
3771 n = isl_aff_list_n_aff(list);
3772 if (n < 0)
3773 return isl_stat_error;
3775 ctx = isl_tab_get_ctx(tab);
3776 sub = isl_vec_alloc(ctx, 1 + dim + n);
3777 if (!sub)
3778 return isl_stat_error;
3779 isl_seq_clr(sub->el + 1 + dim, n);
3781 for (i = 0; i < n; ++i) {
3782 aff = isl_aff_list_get_aff(list, i);
3783 if (!aff)
3784 goto error;
3785 if (isl_aff_is_nan(aff)) {
3786 isl_aff_free(aff);
3787 continue;
3789 isl_seq_cpy(sub->el, aff->v->el + 1, 1 + dim);
3790 isl_int_neg(sub->el[1 + dim + i], aff->v->el[0]);
3791 if (isl_tab_add_eq(tab, sub->el) < 0)
3792 goto error;
3793 isl_int_set_si(sub->el[1 + dim + i], 0);
3794 isl_aff_free(aff);
3797 isl_vec_free(sub);
3798 return isl_stat_ok;
3799 error:
3800 isl_aff_free(aff);
3801 isl_vec_free(sub);
3802 return isl_stat_error;
3805 /* Add variables to info->tab and info->bmap corresponding to the elements
3806 * in "list" that are not set to NaN. The value of the added variable
3807 * in info->tab is fixed to the purely affine expression defined by the element.
3808 * "dim" is the offset in the variables of info->tab where we should
3809 * start considering the elements in "list".
3810 * When this function returns, the total number of variables in info->tab
3811 * is equal to "dim" plus the number of elements in "list".
3813 static isl_stat add_subs(struct isl_coalesce_info *info,
3814 __isl_keep isl_aff_list *list, int dim)
3816 int extra_var;
3817 isl_size n;
3819 n = isl_aff_list_n_aff(list);
3820 if (n < 0)
3821 return isl_stat_error;
3823 extra_var = n - (info->tab->n_var - dim);
3825 if (isl_tab_extend_vars(info->tab, extra_var) < 0)
3826 return isl_stat_error;
3827 if (isl_tab_extend_cons(info->tab, 2 * extra_var) < 0)
3828 return isl_stat_error;
3829 if (add_sub_vars(info, list, dim, extra_var) < 0)
3830 return isl_stat_error;
3832 return add_sub_equalities(info->tab, list, dim);
3835 /* Coalesce basic map "j" into basic map "i" after adding the extra integer
3836 * divisions in "i" but not in "j" to basic map "j", with values
3837 * specified by "list". The total number of elements in "list"
3838 * is equal to the number of integer divisions in "i", while the number
3839 * of NaN elements in the list is equal to the number of integer divisions
3840 * in "j".
3842 * If no coalescing can be performed, then we need to revert basic map "j"
3843 * to its original state. We do the same if basic map "i" gets dropped
3844 * during the coalescing, even though this should not happen in practice
3845 * since we have already checked for "j" being a subset of "i"
3846 * before we reach this stage.
3848 static enum isl_change coalesce_with_subs(int i, int j,
3849 struct isl_coalesce_info *info, __isl_keep isl_aff_list *list)
3851 isl_basic_map *bmap_j;
3852 struct isl_tab_undo *snap;
3853 isl_size dim, n_div;
3854 enum isl_change change;
3856 bmap_j = isl_basic_map_copy(info[j].bmap);
3857 snap = isl_tab_snap(info[j].tab);
3859 dim = isl_basic_map_dim(bmap_j, isl_dim_all);
3860 n_div = isl_basic_map_dim(bmap_j, isl_dim_div);
3861 if (dim < 0 || n_div < 0)
3862 goto error;
3863 dim -= n_div;
3864 if (add_subs(&info[j], list, dim) < 0)
3865 goto error;
3867 change = coalesce_local_pair(i, j, info);
3868 if (change != isl_change_none && change != isl_change_drop_first) {
3869 isl_basic_map_free(bmap_j);
3870 } else {
3871 isl_basic_map_free(info[j].bmap);
3872 info[j].bmap = bmap_j;
3874 if (isl_tab_rollback(info[j].tab, snap) < 0)
3875 return isl_change_error;
3878 return change;
3879 error:
3880 isl_basic_map_free(bmap_j);
3881 return isl_change_error;
3884 /* Check if we can coalesce basic map "j" into basic map "i" after copying
3885 * those extra integer divisions in "i" that can be simplified away
3886 * using the extra equalities in "j".
3887 * All divs are assumed to be known and not contain any nested divs.
3889 * We first check if there are any extra equalities in "j" that we
3890 * can exploit. Then we check if every integer division in "i"
3891 * either already appears in "j" or can be simplified using the
3892 * extra equalities to a purely affine expression.
3893 * If these tests succeed, then we try to coalesce the two basic maps
3894 * by introducing extra dimensions in "j" corresponding to
3895 * the extra integer divisions "i" fixed to the corresponding
3896 * purely affine expression.
3898 static enum isl_change check_coalesce_into_eq(int i, int j,
3899 struct isl_coalesce_info *info)
3901 isl_size n_div_i, n_div_j, n;
3902 isl_basic_map *hull_i, *hull_j;
3903 isl_bool equal, empty;
3904 isl_aff_list *list;
3905 enum isl_change change;
3907 n_div_i = isl_basic_map_dim(info[i].bmap, isl_dim_div);
3908 n_div_j = isl_basic_map_dim(info[j].bmap, isl_dim_div);
3909 if (n_div_i < 0 || n_div_j < 0)
3910 return isl_change_error;
3911 if (n_div_i <= n_div_j)
3912 return isl_change_none;
3913 if (info[j].bmap->n_eq == 0)
3914 return isl_change_none;
3916 hull_i = isl_basic_map_copy(info[i].bmap);
3917 hull_i = isl_basic_map_plain_affine_hull(hull_i);
3918 hull_j = isl_basic_map_copy(info[j].bmap);
3919 hull_j = isl_basic_map_plain_affine_hull(hull_j);
3921 hull_j = isl_basic_map_intersect(hull_j, isl_basic_map_copy(hull_i));
3922 equal = isl_basic_map_plain_is_equal(hull_i, hull_j);
3923 empty = isl_basic_map_plain_is_empty(hull_j);
3924 isl_basic_map_free(hull_i);
3926 if (equal < 0 || empty < 0)
3927 goto error;
3928 if (equal || empty) {
3929 isl_basic_map_free(hull_j);
3930 return isl_change_none;
3933 list = set_up_substitutions(info[i].bmap, info[j].bmap, hull_j);
3934 if (!list)
3935 return isl_change_error;
3936 n = isl_aff_list_n_aff(list);
3937 if (n < 0)
3938 change = isl_change_error;
3939 else if (n < n_div_i)
3940 change = isl_change_none;
3941 else
3942 change = coalesce_with_subs(i, j, info, list);
3944 isl_aff_list_free(list);
3946 return change;
3947 error:
3948 isl_basic_map_free(hull_j);
3949 return isl_change_error;
3952 /* Check if we can coalesce basic maps "i" and "j" after copying
3953 * those extra integer divisions in one of the basic maps that can
3954 * be simplified away using the extra equalities in the other basic map.
3955 * We require all divs to be known in both basic maps.
3956 * Furthermore, to simplify the comparison of div expressions,
3957 * we do not allow any nested integer divisions.
3959 static enum isl_change check_coalesce_eq(int i, int j,
3960 struct isl_coalesce_info *info)
3962 isl_bool known, nested;
3963 enum isl_change change;
3965 known = isl_basic_map_divs_known(info[i].bmap);
3966 if (known < 0 || !known)
3967 return known < 0 ? isl_change_error : isl_change_none;
3968 known = isl_basic_map_divs_known(info[j].bmap);
3969 if (known < 0 || !known)
3970 return known < 0 ? isl_change_error : isl_change_none;
3971 nested = has_nested_div(info[i].bmap);
3972 if (nested < 0 || nested)
3973 return nested < 0 ? isl_change_error : isl_change_none;
3974 nested = has_nested_div(info[j].bmap);
3975 if (nested < 0 || nested)
3976 return nested < 0 ? isl_change_error : isl_change_none;
3978 change = check_coalesce_into_eq(i, j, info);
3979 if (change != isl_change_none)
3980 return change;
3981 change = check_coalesce_into_eq(j, i, info);
3982 if (change != isl_change_none)
3983 return invert_change(change);
3985 return isl_change_none;
3988 /* Check if the union of the given pair of basic maps
3989 * can be represented by a single basic map.
3990 * If so, replace the pair by the single basic map and return
3991 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
3992 * Otherwise, return isl_change_none.
3994 * We first check if the two basic maps live in the same local space,
3995 * after aligning the divs that differ by only an integer constant.
3996 * If so, we do the complete check. Otherwise, we check if they have
3997 * the same number of integer divisions and can be coalesced, if one is
3998 * an obvious subset of the other or if the extra integer divisions
3999 * of one basic map can be simplified away using the extra equalities
4000 * of the other basic map.
4002 * Note that trying to coalesce pairs of disjuncts with the same
4003 * number, but different local variables may drop the explicit
4004 * representation of some of these local variables.
4005 * This operation is therefore not performed when
4006 * the "coalesce_preserve_locals" option is set.
4008 static enum isl_change coalesce_pair(int i, int j,
4009 struct isl_coalesce_info *info)
4011 int preserve;
4012 isl_bool same;
4013 enum isl_change change;
4014 isl_ctx *ctx;
4016 if (harmonize_divs(&info[i], &info[j]) < 0)
4017 return isl_change_error;
4018 same = same_divs(info[i].bmap, info[j].bmap);
4019 if (same < 0)
4020 return isl_change_error;
4021 if (same)
4022 return coalesce_local_pair(i, j, info);
4024 ctx = isl_basic_map_get_ctx(info[i].bmap);
4025 preserve = isl_options_get_coalesce_preserve_locals(ctx);
4026 if (!preserve && info[i].bmap->n_div == info[j].bmap->n_div) {
4027 change = coalesce_local_pair(i, j, info);
4028 if (change != isl_change_none)
4029 return change;
4032 change = coalesce_divs(i, j, info);
4033 if (change != isl_change_none)
4034 return change;
4036 return check_coalesce_eq(i, j, info);
4039 /* Return the maximum of "a" and "b".
4041 static int isl_max(int a, int b)
4043 return a > b ? a : b;
4046 /* Pairwise coalesce the basic maps in the range [start1, end1[ of "info"
4047 * with those in the range [start2, end2[, skipping basic maps
4048 * that have been removed (either before or within this function).
4050 * For each basic map i in the first range, we check if it can be coalesced
4051 * with respect to any previously considered basic map j in the second range.
4052 * If i gets dropped (because it was a subset of some j), then
4053 * we can move on to the next basic map.
4054 * If j gets dropped, we need to continue checking against the other
4055 * previously considered basic maps.
4056 * If the two basic maps got fused, then we recheck the fused basic map
4057 * against the previously considered basic maps, starting at i + 1
4058 * (even if start2 is greater than i + 1).
4060 static int coalesce_range(isl_ctx *ctx, struct isl_coalesce_info *info,
4061 int start1, int end1, int start2, int end2)
4063 int i, j;
4065 for (i = end1 - 1; i >= start1; --i) {
4066 if (info[i].removed)
4067 continue;
4068 for (j = isl_max(i + 1, start2); j < end2; ++j) {
4069 enum isl_change changed;
4071 if (info[j].removed)
4072 continue;
4073 if (info[i].removed)
4074 isl_die(ctx, isl_error_internal,
4075 "basic map unexpectedly removed",
4076 return -1);
4077 changed = coalesce_pair(i, j, info);
4078 switch (changed) {
4079 case isl_change_error:
4080 return -1;
4081 case isl_change_none:
4082 case isl_change_drop_second:
4083 continue;
4084 case isl_change_drop_first:
4085 j = end2;
4086 break;
4087 case isl_change_fuse:
4088 j = i;
4089 break;
4094 return 0;
4097 /* Pairwise coalesce the basic maps described by the "n" elements of "info".
4099 * We consider groups of basic maps that live in the same apparent
4100 * affine hull and we first coalesce within such a group before we
4101 * coalesce the elements in the group with elements of previously
4102 * considered groups. If a fuse happens during the second phase,
4103 * then we also reconsider the elements within the group.
4105 static int coalesce(isl_ctx *ctx, int n, struct isl_coalesce_info *info)
4107 int start, end;
4109 for (end = n; end > 0; end = start) {
4110 start = end - 1;
4111 while (start >= 1 &&
4112 info[start - 1].hull_hash == info[start].hull_hash)
4113 start--;
4114 if (coalesce_range(ctx, info, start, end, start, end) < 0)
4115 return -1;
4116 if (coalesce_range(ctx, info, start, end, end, n) < 0)
4117 return -1;
4120 return 0;
4123 /* Update the basic maps in "map" based on the information in "info".
4124 * In particular, remove the basic maps that have been marked removed and
4125 * update the others based on the information in the corresponding tableau.
4126 * Since we detected implicit equalities without calling
4127 * isl_basic_map_gauss, we need to do it now.
4128 * Also call isl_basic_map_simplify if we may have lost the definition
4129 * of one or more integer divisions.
4130 * If a basic map is still equal to the one from which the corresponding "info"
4131 * entry was created, then redundant constraint and
4132 * implicit equality constraint detection have been performed
4133 * on the corresponding tableau and the basic map can be marked as such.
4135 static __isl_give isl_map *update_basic_maps(__isl_take isl_map *map,
4136 int n, struct isl_coalesce_info *info)
4138 int i;
4140 if (!map)
4141 return NULL;
4143 for (i = n - 1; i >= 0; --i) {
4144 if (info[i].removed) {
4145 isl_basic_map_free(map->p[i]);
4146 if (i != map->n - 1)
4147 map->p[i] = map->p[map->n - 1];
4148 map->n--;
4149 continue;
4152 info[i].bmap = isl_basic_map_update_from_tab(info[i].bmap,
4153 info[i].tab);
4154 info[i].bmap = isl_basic_map_gauss(info[i].bmap, NULL);
4155 if (info[i].simplify)
4156 info[i].bmap = isl_basic_map_simplify(info[i].bmap);
4157 info[i].bmap = isl_basic_map_finalize(info[i].bmap);
4158 if (!info[i].bmap)
4159 return isl_map_free(map);
4160 if (!info[i].modified) {
4161 ISL_F_SET(info[i].bmap, ISL_BASIC_MAP_NO_IMPLICIT);
4162 ISL_F_SET(info[i].bmap, ISL_BASIC_MAP_NO_REDUNDANT);
4164 isl_basic_map_free(map->p[i]);
4165 map->p[i] = info[i].bmap;
4166 info[i].bmap = NULL;
4169 return map;
4172 /* For each pair of basic maps in the map, check if the union of the two
4173 * can be represented by a single basic map.
4174 * If so, replace the pair by the single basic map and start over.
4176 * We factor out any (hidden) common factor from the constraint
4177 * coefficients to improve the detection of adjacent constraints.
4178 * Note that this function does not call isl_basic_map_gauss,
4179 * but it does make sure that only a single copy of the basic map
4180 * is affected. This means that isl_basic_map_gauss may have
4181 * to be called at the end of the computation (in update_basic_maps)
4182 * on this single copy to ensure that
4183 * the basic maps are not left in an unexpected state.
4185 * Since we are constructing the tableaus of the basic maps anyway,
4186 * we exploit them to detect implicit equalities and redundant constraints.
4187 * This also helps the coalescing as it can ignore the redundant constraints.
4188 * In order to avoid confusion, we make all implicit equalities explicit
4189 * in the basic maps. If the basic map only has a single reference
4190 * (this happens in particular if it was modified by
4191 * isl_basic_map_reduce_coefficients), then isl_basic_map_gauss
4192 * does not get called on the result. The call to
4193 * isl_basic_map_gauss in update_basic_maps resolves this as well.
4194 * For each basic map, we also compute the hash of the apparent affine hull
4195 * for use in coalesce.
4197 __isl_give isl_map *isl_map_coalesce(__isl_take isl_map *map)
4199 int i;
4200 unsigned n;
4201 isl_ctx *ctx;
4202 struct isl_coalesce_info *info = NULL;
4204 map = isl_map_remove_empty_parts(map);
4205 if (!map)
4206 return NULL;
4208 if (map->n <= 1)
4209 return map;
4211 ctx = isl_map_get_ctx(map);
4212 map = isl_map_sort_divs(map);
4213 map = isl_map_cow(map);
4215 if (!map)
4216 return NULL;
4218 n = map->n;
4220 info = isl_calloc_array(map->ctx, struct isl_coalesce_info, n);
4221 if (!info)
4222 goto error;
4224 for (i = 0; i < map->n; ++i) {
4225 map->p[i] = isl_basic_map_reduce_coefficients(map->p[i]);
4226 if (!map->p[i])
4227 goto error;
4228 info[i].bmap = isl_basic_map_copy(map->p[i]);
4229 info[i].tab = isl_tab_from_basic_map(info[i].bmap, 0);
4230 if (!info[i].tab)
4231 goto error;
4232 if (!ISL_F_ISSET(info[i].bmap, ISL_BASIC_MAP_NO_IMPLICIT))
4233 if (isl_tab_detect_implicit_equalities(info[i].tab) < 0)
4234 goto error;
4235 info[i].bmap = isl_tab_make_equalities_explicit(info[i].tab,
4236 info[i].bmap);
4237 if (!info[i].bmap)
4238 goto error;
4239 if (!ISL_F_ISSET(info[i].bmap, ISL_BASIC_MAP_NO_REDUNDANT))
4240 if (isl_tab_detect_redundant(info[i].tab) < 0)
4241 goto error;
4242 if (coalesce_info_set_hull_hash(&info[i]) < 0)
4243 goto error;
4245 for (i = map->n - 1; i >= 0; --i)
4246 if (info[i].tab->empty)
4247 drop(&info[i]);
4249 if (coalesce(ctx, n, info) < 0)
4250 goto error;
4252 map = update_basic_maps(map, n, info);
4254 clear_coalesce_info(n, info);
4256 return map;
4257 error:
4258 clear_coalesce_info(n, info);
4259 isl_map_free(map);
4260 return NULL;
4263 /* For each pair of basic sets in the set, check if the union of the two
4264 * can be represented by a single basic set.
4265 * If so, replace the pair by the single basic set and start over.
4267 __isl_give isl_set *isl_set_coalesce(__isl_take isl_set *set)
4269 return set_from_map(isl_map_coalesce(set_to_map(set)));